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/Driver/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. @link     http://pear.php.net/package/Services_Blogging
  20. */
  21.     extends Services_Blogging_Driver
  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(
  94.             $authenticate$this->rpc_client
  95.         );
  96.     }//public function __construct($userid, $pass, $server = null, $path = null)
  97.  
  98.  
  99.  
  100.     /**
  101.     * Save a new post into the blog.
  102.     *
  103.     * @param Services_Blogging_Post $post Post object to put online
  104.     *
  105.     * @return void 
  106.     *
  107.     * @throws Services_Blogging_Exception If an error occured
  108.     */
  109.     public function savePost(Services_Blogging_Post $post)
  110.     {
  111.         if ($post->id === null{
  112.             //post is new and has no Id => create new one
  113.             $request = new XML_RPC_Message('blogger.newPost',
  114.                 array(
  115.                     $this->userdata['rpc_key'],
  116.                     $this->userdata['rpc_blogid'],
  117.                     $this->userdata['rpc_user'],
  118.                     $this->userdata['rpc_pass'],
  119.                     new XML_RPC_Value($post->{Services_Blogging_Post::CONTENT}'string'),
  120.                     new XML_RPC_Value(true'boolean')
  121.                 )
  122.             );
  123.             $nPostId Services_Blogging_XmlRpc::sendRequest(
  124.                 $request$this->rpc_client
  125.             );
  126.             $post->setId($nPostId);
  127.         else {
  128.             //edit the post; it already exists
  129.             $request = new XML_RPC_Message('blogger.editPost',
  130.                 array(
  131.                     $this->userdata['rpc_key'],
  132.                     new XML_RPC_Value($post->id'string'),
  133.                     $this->userdata['rpc_user'],
  134.                     $this->userdata['rpc_pass'],
  135.                     new XML_RPC_Value($post->{Services_Blogging_Post::CONTENT}'string'),
  136.                     new XML_RPC_Value(true'boolean')
  137.                 )
  138.             );
  139.             Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  140.         }
  141.     }//public function savePost(Services_Blogging_Post $post)
  142.  
  143.  
  144.  
  145.     /**
  146.     * Delete a given post
  147.     *
  148.     * @param mixed $post Services_Blogging_Post object to delete,
  149.     *                      or post id (integer) to delete
  150.     *
  151.     * @return boolean True if deleted, false if not.
  152.     */
  153.     public function deletePost($post)
  154.     {
  155.         if ($post instanceof Services_Blogging_Post{
  156.             $id = new XML_RPC_Value($post->id'string');
  157.         else {
  158.             $id = new XML_RPC_Value($post'string');
  159.         }
  160.  
  161.         $request = new XML_RPC_Message(
  162.             'blogger.deletePost',
  163.             array(
  164.                 $this->userdata['rpc_key'],
  165.                 $id,
  166.                 $this->userdata['rpc_user'],
  167.                 $this->userdata['rpc_pass'],
  168.                 new XML_RPC_Value(true'boolean')
  169.             )
  170.         );
  171.         $response Services_Blogging_XmlRpc::sendRequest(
  172.             $request$this->rpc_client
  173.         );
  174.         return (bool)$response;
  175.     }//public function deletePost($post)
  176.  
  177.  
  178.  
  179.     /**
  180.     * Returns an array of strings thay define
  181.     * the properties that a post to this blog may
  182.     * have.
  183.     *
  184.     * @param string $strPostType Type of post to create.
  185.     *                            @see getSupportedPostTypes()
  186.     *
  187.     * @return array Array of strings
  188.     */
  189.     public function getSupportedPostProperties($strPostType 'post')
  190.     {
  191.         return array(Services_Blogging_Post::CONTENT);
  192.     }//public function getSupportedPostProperties(..)
  193.  
  194.  
  195.  
  196.     /**
  197.     * Checks if the given property name/id is supported
  198.     * for this driver.
  199.     *
  200.     * @param string $strProperty Property name/id to check
  201.     * @param string $strPostType Type of post to create.
  202.     *                            @see getSupportedPostTypes()
  203.     *
  204.     * @return boolean If the property is supported
  205.     */
  206.     public function isPostPropertySupported($strProperty$strPostType 'post')
  207.     {
  208.         return $strProperty == Services_Blogging_Post::CONTENT;
  209.     }//public function isPostPropertySupported(..)
  210.  
  211.  
  212.  
  213.     /**
  214.     * Sets the blog id to use (some blogging APIs support multiple
  215.     * blogs with one account)
  216.     *
  217.     * @param int $nBlogId Id of the blog to use
  218.     *
  219.     * @return void 
  220.     */
  221.     public function setBlogId($nBlogId)
  222.     {
  223.         $this->nBlogId                = $nBlogId;
  224.         $this->userdata['rpc_blogid'= new XML_RPC_Value($nBlogId'string');
  225.     }//public function setBlogId($nBlogId)
  226.  
  227.  
  228.  
  229.     /**
  230.     * Returns the id of the currently used blog.
  231.     *
  232.     * @return int Blog id
  233.     */
  234.     public function getBlogId()
  235.     {
  236.         return $this->nBlogId;
  237.     }//public function getBlogId()
  238.  
  239.  
  240.  
  241.     /**
  242.     * Returns an array of blogs for that account.
  243.     *
  244.     * @return array Array of Services_Blogging_Blog objects
  245.     */
  246.     public function getBlogs()
  247.     {
  248.         $request = new XML_RPC_Message(
  249.             'blogger.getUsersBlogs',
  250.             array(
  251.                 $this->userdata['rpc_key'],
  252.                 $this->userdata['rpc_user'],
  253.                 $this->userdata['rpc_pass']
  254.             )
  255.         );
  256.         $blogs Services_Blogging_XmlRpc::sendRequest(
  257.             $request$this->rpc_client
  258.         );
  259.         $arBlogs = array();
  260.         foreach ($blogs as $blog{
  261.             $arBlogs[= new Services_Blogging_Blog(
  262.                 $blog['blogid']$blog['blogName']$blog['url']
  263.             );
  264.         }
  265.         return $arBlogs;
  266.     }//public function getBlogs()
  267.  
  268.  
  269.  
  270.     //{{{ getTemplate()
  271.  
  272.  
  273.     /**
  274.     * Implements the blogger.getTemplate() method. The BlogID of the blog for
  275.     * which the template must be retrieved and the template type are passed as
  276.     * parameters.
  277.     * The template type is usually one of 'main' or 'archiveIndex'.
  278.     * The template in HTML format is returned.
  279.     *
  280.     * A template is the HTML code that represents the format of your blog. It is
  281.     * best to first examine the code that is returned by using this method;
  282.     * modifying it to suit your requirements, and then updating the template
  283.     * using the setTemplate() method.
  284.     *
  285.     * @param string $tempType The type of template to retrived. Usually either
  286.     *                           'main' or 'archiveIndex'.
  287.     *
  288.     * @return string The template in HTML form.
  289.     */
  290.     public function getTemplate($tempType)
  291.     {
  292.         if ($tempType != 'main' && $tempType != 'archiveIndex'{
  293.             throw new Services_Blogging_Driver_Exception(
  294.                 'Unknown template "' $tempType '"',
  295.                 self::ERROR_UNKNOWN_TEMPLATE
  296.             );
  297.         }
  298.  
  299.         $request = new XML_RPC_Message(
  300.             'blogger.getTemplate',
  301.             array(
  302.                 $this->userdata['rpc_key'],
  303.                 $this->userdata['rpc_blogid'],
  304.                 $this->userdata['rpc_user'],
  305.                 $this->userdata['rpc_pass'],
  306.                 new XML_RPC_Value($tempType'string')
  307.             )
  308.         );
  309.         return Services_Blogging_XmlRpc::sendRequest(
  310.             $request$this->rpc_client
  311.         );
  312.     }//public function getTemplate($tempType)
  313.  
  314.  
  315.  
  316.     /**
  317.      * Implements the blogger.setTemplate() method. The BlogID of the blog for
  318.      * which the template is to be set, the template type
  319.      * (again: 'main' or 'archiveIndex')
  320.      * and the actual template in the HTML format are passed as parameters.
  321.      *
  322.      * See the docblock for the getTemplate() to find out what a template is.
  323.      *
  324.      * @param string $tempType The type of the template being set. Either 'main'
  325.      *                           or 'archiveIndex'.
  326.      * @param string $template The actual template in the HTML format.
  327.      *
  328.      * @return  boolean Whether or not the template was set.
  329.      */
  330.     public function setTemplate($tempType$template)
  331.     {
  332.         if ($tempType != 'main' && $tempType != 'archiveIndex'{
  333.             throw new Services_Blogging_Driver_Exception(
  334.                 'Unknown template "' $tempType '"',
  335.                 self::ERROR_UNKNOWN_TEMPLATE
  336.             );
  337.         }
  338.  
  339.         $request = new XML_RPC_Message(
  340.             'blogger.setTemplate',
  341.             array(
  342.                 $this->userdata['rpc_key'],
  343.                 $this->userdata['rpc_blogid'],
  344.                 $this->userdata['rpc_user'],
  345.                 $this->userdata['rpc_pass'],
  346.                 new XML_RPC_Value($tempType'string'),
  347.                 new XML_RPC_Value($template'string')
  348.             )
  349.         );
  350.         return Services_Blogging_XmlRpc::sendRequest($request$this->rpc_client);
  351.     }//public function setTemplate($tempType, $template)
  352.  
  353.  
  354.  
  355.     /**
  356.     * Returns an array of supported Templates
  357.     *
  358.     * @return array Array of templates (strings)
  359.     */
  360.     public function getSupportedTemplates()
  361.     {
  362.         return array('main''archiveIndex');
  363.     }//public function getSupportedTemplates()
  364.  
  365. }//class Services_Blogging_Driver_Blogger extends Services_Blogging_Driver
  366. // implements Services_Blogging_MultipleBlogsInterface
  367. ?>

Documentation generated on Mon, 11 Mar 2019 15:25:12 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.