home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Services / Blogging / Driver / Blogger.php next >
Encoding:
PHP Script  |  2008-07-02  |  11.6 KB  |  369 lines

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