Services_Blogging
[ class tree: Services_Blogging ] [ index: Services_Blogging ] [ all elements ]

Source for file Blogger.php

Documentation is available at Blogger.php

  1. <?php
  2. require_once 'Services/Blogging/Driver.php';
  3. require_once 'Services/Blogging/Exception.php';
  4. require_once 'Services/Blogging/MultipleBlogsInterface.php';
  5. require_once 'Services/Blogging/XmlRpc.php';
  6. require_once 'XML/RPC.php';
  7.  
  8. /**
  9. *   Blogger API implementation.
  10. *
  11. *   This class implements the Blogger XML-RPC API described at
  12. *   http://www.blogger.com/developers/api/1_docs/
  13. *
  14. *   @category    Services
  15. *   @package     Services_Blogging
  16. *   @author      Anant Narayanan <anant@php.net>
  17. *   @author      Christian Weiske <cweiske@php.net>
  18. *   @license     http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  19. */
  20.     extends Services_Blogging_Driver
  21. {
  22.     /**
  23.     *   Requests shall be sent to here
  24.     */
  25.     const XML_RPC_SERVER = 'http://plant.blogger.com';
  26.     const XML_RPC_PATH   = '/api/RPC2';
  27.  
  28.     /**
  29.     *   Id of the blog to be used.
  30.     *   Some blogs support multiple blogs with one account.
  31.     *   @var int 
  32.     */
  33.     protected $nBlogId = null;
  34.  
  35.     /**
  36.     *   Internal list with user data.
  37.     *   @var array 
  38.     */
  39.     protected $userdata = array();
  40.  
  41.     const ERROR_UNKNOWN_TEMPLATE= 112;
  42.  
  43.  
  44.  
  45.     /**
  46.     *   Constructor for the Blogger class that authenticates the user and sets class
  47.     *   properties. It will return the userinfo if authentication was successful, an
  48.     *   exception if authentication failed. The username, password, path to the
  49.     *   XML-RPC client and server URI are passed as parameters.
  50.     *
  51.     *   If $server and $path are set to NULL, the default
  52.     *    blogger.com address is used.
  53.     *
  54.     *   @param   string  $user       The username of the blog account.
  55.     *   @param   string  $pass       The password of the blog account.
  56.     *   @param   string  $server     The URI of the server to connect to.
  57.     *   @param   string  $path       The path to the XML-RPC server script.
  58.     *
  59.     *   @throws Services_Blogging_Exception  If authentication fails
  60.     */
  61.     public function __construct($user$pass$server = null$path = null)
  62.     {
  63.         if ($server === null{
  64.             $server = self::XML_RPC_SERVER;
  65.             $path   = self::XML_RPC_PATH;
  66.         }
  67.         $this->userdata = array(
  68.             'user'  => $user,
  69.             'pass'  => $pass,
  70.             'server'=> $server,
  71.             'path'  => $path,
  72.             'rpc_user'  => new XML_RPC_Value($user'string'),
  73.             'rpc_pass'  => new XML_RPC_Value($pass'string'),
  74.             'rpc_blogid'=> new XML_RPC_Value(''   'string'),
  75.             'rpc_key'   => new XML_RPC_Value('0123456789ABCDEF''string')
  76.         );
  77.  
  78.         $authenticate = new XML_RPC_Message(
  79.             'blogger.getUserInfo',
  80.             array(
  81.                 $this->userdata['rpc_key'],
  82.                 $this->userdata['rpc_user'],
  83.                 $this->userdata['rpc_pass']
  84.             )
  85.         );
  86.         $this->rpc_client = new XML_RPC_Client(
  87.             $this->userdata['path'],
  88.             $this->userdata['server']
  89.         );
  90.  
  91.         //FIXME: store the userinfo somewhere and make it available
  92.         $userInfo Services_Blogging_XmlRpc::sendRequest($authenticate$this->rpc_client);
  93.     }//public function __construct($userid, $pass, $server = null, $path = null)
  94.  
  95.  
  96.  
  97.     /**
  98.     *   Save a new post into the blog.
  99.     *
  100.     *   @param Services_Blogging_Post $post     Post object to put online
  101.     *
  102.     *   @throws Services_Blogging_Exception If an error occured
  103.     */
  104.     public function savePost(Services_Blogging_Post $post)
  105.     {
  106.         if ($post->id === null{
  107.             //post is new and has no Id => create new one
  108.             $request = new XML_RPC_Message('blogger.newPost',
  109.                 array(
  110.                     $this->userdata['rpc_key'],
  111.                     $this->userdata['rpc_blogid'],
  112.                     $this->userdata['rpc_user'],
  113.                     $this->userdata['rpc_pass'],
  114.                     new XML_RPC_Value($post->{Services_Blogging_Post::CONTENT}'string'),
  115.                     new XML_RPC_Value(true'boolean')
  116.                 )
  117.             );
  118.             $nPostId Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  119.             $post->setId($nPostId);
  120.         else {
  121.             //edit the post; it already exists
  122.             $request = new XML_RPC_Message('blogger.editPost',
  123.                 array(
  124.                     $this->userdata['rpc_key'],
  125.                     new XML_RPC_Value($post->id'string'),
  126.                     $this->userdata['rpc_user'],
  127.                     $this->userdata['rpc_pass'],
  128.                     new XML_RPC_Value($post->{Services_Blogging_Post::CONTENT}'string'),
  129.                     new XML_RPC_Value(true'boolean')
  130.                 )
  131.             );
  132.             Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  133.         }
  134.     }//public function savePost(Services_Blogging_Post $post)
  135.  
  136.  
  137.  
  138.     /**
  139.     *   Delete a given post
  140.     *
  141.     *   @param mixed  $post   Services_Blogging_Post object to delete,
  142.     *                           or post id (integer) to delete
  143.     *   @return boolean     True if deleted, false if not.
  144.     */
  145.     public function deletePost($post)
  146.     {
  147.         if ($post instanceof Services_Blogging_Post{
  148.             $id = new XML_RPC_Value($post->id'string');
  149.         else {
  150.             $id = new XML_RPC_Value($post'string');
  151.         }
  152.  
  153.         $request = new XML_RPC_Message(
  154.             'blogger.deletePost',
  155.             array(
  156.                 $this->userdata['rpc_key'],
  157.                 $id,
  158.                 $this->userdata['rpc_user'],
  159.                 $this->userdata['rpc_pass'],
  160.                 new XML_RPC_Value(true'boolean')
  161.             )
  162.         );
  163.         $response Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  164.         return (bool)$response;
  165.     }//public function deletePost($post)
  166.  
  167.  
  168.  
  169.     /**
  170.     *   Returns an array of strings thay define
  171.     *   the properties that a post to this blog may
  172.     *   have.
  173.     *
  174.     *   @return array   Array of strings
  175.     */
  176.     public function getSupportedPostProperties()
  177.     {
  178.         return array(Services_Blogging_Post::CONTENT);
  179.     }//public function getSupportedPostProperties()
  180.  
  181.  
  182.  
  183.     /**
  184.     *   Checks if the given property name/id is supported
  185.     *   for this driver.
  186.     *
  187.     *   @param string $strProperty  Property name/id to check
  188.     *
  189.     *   @return boolean     If the property is supported
  190.     */
  191.     public function isPostPropertySupported($strProperty)
  192.     {
  193.         return $strProperty == Services_Blogging_Post::CONTENT;
  194.     }//public function isPostPropertySupported($strProperty)
  195.  
  196.  
  197.  
  198.     /**
  199.     *   Sets the blog id to use (some blogging APIs support multiple
  200.     *   blogs with one account)
  201.     *
  202.     *   @param int  $nBlogId    Id of the blog to use
  203.     */
  204.     public function setBlogId($nBlogId)
  205.     {
  206.         $this->nBlogId = $nBlogId;
  207.         $this->userdata['rpc_blogid'= new XML_RPC_Value($nBlogId'string');
  208.     }//public function setBlogId($nBlogId)
  209.  
  210.  
  211.  
  212.     /**
  213.     *   Returns the id of the currently used blog.
  214.     *
  215.     *   @return int     Blog id
  216.     */
  217.     public function getBlogId()
  218.     {
  219.         return $this->nBlogId;
  220.     }//public function getBlogId()
  221.  
  222.  
  223.  
  224.     /**
  225.     *   Returns an array of blogs for that account.
  226.     *
  227.     *   @return array     Array of Services_Blogging_Blog
  228.     */
  229.     public function getBlogs()
  230.     {
  231.         $request = new XML_RPC_Message(
  232.             'blogger.getUsersBlogs',
  233.             array(
  234.                 $this->userdata['rpc_key'],
  235.                 $this->userdata['rpc_user'],
  236.                 $this->userdata['rpc_pass']
  237.             )
  238.         );
  239.         $blogs Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  240.         $arBlogs = array();
  241.         foreach ($blogs as $blog{
  242.             $arBlogs[= new Services_Blogging_Blog($blog['blogid']$blog['blogName']$blog['url']);
  243.         }
  244.         return $arBlogs;
  245.     }//public function getBlogs()
  246.  
  247.  
  248.  
  249.     //{{{ getTemplate()
  250.  
  251.  
  252.     /**
  253.      * Implements the blogger.getTemplate() method. The BlogID of the blog for which
  254.      * the template must be retrieved and the template type are passed as parameters.
  255.      * The template type is usually one of 'main' or 'archiveIndex'. The template in
  256.      * HTML format is returned.
  257.      *
  258.      * A template is the HTML code that represents the format of your blog. It is
  259.      * best to first examine the code that is returned by using this method;
  260.      * modifying it to suit your requirements, and then updating the template
  261.      * using the setTemplate() method.
  262.      *
  263.      * @param   string  $tempType   The type of template to retrived. Usually either
  264.      *                               'main' or 'archiveIndex'.
  265.      *
  266.      * @return  string  The template in HTML form.
  267.      */
  268.     public function getTemplate($tempType)
  269.     {
  270.         if ($tempType != 'main' && $tempType != 'archiveIndex'{
  271.             throw new Services_Blogging_Exception('Unknown template "' $tempType '"',
  272.                                  self::ERROR_UNKNOWN_TEMPLATE);
  273.         }
  274.  
  275.         $request = new XML_RPC_Message(
  276.             'blogger.getTemplate',
  277.             array(
  278.                 $this->userdata['rpc_key'],
  279.                 $this->userdata['rpc_blogid'],
  280.                 $this->userdata['rpc_user'],
  281.                 $this->userdata['rpc_pass'],
  282.                 new XML_RPC_Value($tempType'string')
  283.             )
  284.         );
  285.         return Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  286.     }//public function getTemplate($tempType)
  287.  
  288.  
  289.  
  290.     /**
  291.      * Implements the blogger.setTemplate() method. The BlogID of the blog for which
  292.      * the template is to be set, the template type (again: 'main' or 'archiveIndex')
  293.      * and the actual template in the HTML format are passed as parameters.
  294.      *
  295.      * See the docblock for the getTemplate() to find out what a template is.
  296.      *
  297.      * @param   string  $blogid     The BlogID of the blog for which the template is
  298.      *                               to be set. (As returned by the getUsersBlogs()
  299.      *                               method).
  300.      * @param   string  $tempType   The type of the template being set. Either 'main'
  301.      *                               or 'archiveIndex'.
  302.      * @param   string  $template   The actual template in the HTML format.
  303.      *
  304.      * @return  boolean Whether or not the template was set.
  305.      */
  306.     public function setTemplate($tempType$template)
  307.     {
  308.         if($tempType != 'main' && $tempType != 'archiveIndex'{
  309.             throw new Services_Blogging_Exception('Unknown template "' $tempType '"',
  310.                                  self::ERROR_UNKNOWN_TEMPLATE);
  311.         }
  312.  
  313.         $request = new XML_RPC_Message(
  314.             'blogger.setTemplate',
  315.             array(
  316.                 $this->userdata['rpc_key'],
  317.                 $this->userdata['rpc_blogid'],
  318.                 $this->userdata['rpc_user'],
  319.                 $this->userdata['rpc_pass'],
  320.                 new XML_RPC_Value($tempType'string'),
  321.                 new XML_RPC_Value($template'string')
  322.             )
  323.         );
  324.         return Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  325.     }//public function setTemplate($tempType, $template)
  326.  
  327.  
  328.  
  329.     public function getSupportedTemplates()
  330.     {
  331.         return array('main''archiveIndex');
  332.     }//public function getSupportedTemplates()
  333.  
  334. }//class Services_Blogging_Driver_Blogger extends Services_Blogging_Driver implements Services_Blogging_MultipleBlogsInterface
  335. ?>

Documentation generated on Mon, 11 Mar 2019 14:57:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.