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

Documentation generated on Sat, 27 Jan 2007 12:00:06 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.