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 / MetaWeblog.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  11.5 KB  |  343 lines

  1. <?php
  2. require_once 'Services/Blogging/Driver/Exception.php';
  3. require_once 'Services/Blogging/ExtendedDriver.php';
  4. require_once 'Services/Blogging/Post.php';
  5. require_once 'Services/Blogging/XmlRpc.php';
  6. require_once 'XML/RPC.php';
  7.  
  8. /**
  9. * metaWeblog API implementation.
  10. * http://www.xmlrpc.com/metaWeblogApi
  11. * http://www.movabletype.org/mt-static/docs/mtmanual_programmatic.html
  12. *
  13. * @category Services
  14. * @package  Services_Blogging
  15. * @author   Anant Narayanan <anant@php.net>
  16. * @author   Christian Weiske <cweiske@php.net>
  17. * @license  http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  18. * @link     http://pear.php.net/package/Services_Blogging
  19. */
  20. class Services_Blogging_Driver_MetaWeblog extends Services_Blogging_ExtendedDriver
  21. {
  22.  
  23.     /**
  24.     * Internal list with user data.
  25.     * @var array
  26.     */
  27.     protected $userdata = array();
  28.  
  29.     protected $arSupportedPostProperties = array(
  30.         Services_Blogging_Post::TITLE,
  31.         Services_Blogging_Post::CONTENT,
  32.         Services_Blogging_Post::DATE,
  33.         Services_Blogging_Post::URL,
  34.         Services_Blogging_Post::CATEGORIES,
  35.     );
  36.  
  37.  
  38.  
  39.     /**
  40.     * Constructor for the metaWeblog driver class.
  41.     *
  42.     * @param string $user   The username of the blog account.
  43.     * @param string $pass   The password of the blog account.
  44.     * @param string $server The URI of the server to connect to.
  45.     * @param string $path   The path to the XML-RPC server script.
  46.     *
  47.     * @throws Services_Blogging_Exception  If authentication fails
  48.     */
  49.     public function __construct($user, $pass, $server, $path)
  50.     {
  51.         $this->userdata = array(
  52.             'user'      => $user,
  53.             'pass'      => $pass,
  54.             'server'    => $server,
  55.             'path'      => $path,
  56.             'rpc_user'  => new XML_RPC_Value($user, 'string'),
  57.             'rpc_pass'  => new XML_RPC_Value($pass, 'string'),
  58.             'rpc_blogid'=> new XML_RPC_Value($user, 'string'),
  59.         );
  60.  
  61.         $this->rpc_client = new XML_RPC_Client(
  62.             $this->userdata['path'],
  63.             $this->userdata['server']
  64.         );
  65.         //$this->rpc_client->setDebug(true);
  66.     }//public function __construct($userid, $pass, $server, $path)
  67.  
  68.  
  69.  
  70.     /**
  71.     * Save a new post into the blog.
  72.     *
  73.     * @param Services_Blogging_Post $post Post object to put online
  74.     *
  75.     * @return void
  76.     *
  77.     * @throws Services_Blogging_Exception If an error occured
  78.     */
  79.     public function savePost(Services_Blogging_Post $post)
  80.     {
  81.         if ($post->id === null) {
  82.             //post is new and has no Id => create new one
  83.             $request = new XML_RPC_Message('metaWeblog.newPost',
  84.                 array(
  85.                     $this->userdata['rpc_blogid'],
  86.                     $this->userdata['rpc_user'],
  87.                     $this->userdata['rpc_pass'],
  88.                     self::convertPostToStruct($post),
  89.                     new XML_RPC_Value(true, 'boolean')
  90.                 )
  91.             );
  92.             $nPostId = Services_Blogging_XmlRpc::sendRequest(
  93.                 $request, $this->rpc_client
  94.             );
  95.             $post->setId($nPostId);
  96.         } else {
  97.             //edit the post; it already exists
  98.             $request = new XML_RPC_Message('metaWeblog.editPost',
  99.                 array(
  100.                     new XML_RPC_Value($post->id, 'string'),
  101.                     $this->userdata['rpc_user'],
  102.                     $this->userdata['rpc_pass'],
  103.                     self::convertPostToStruct($post),
  104.                     new XML_RPC_Value(true, 'boolean')
  105.                 )
  106.             );
  107.             Services_Blogging_XmlRpc::sendRequest($request, $this->rpc_client);
  108.         }
  109.     }//public function savePost(Services_Blogging_Post $post)
  110.  
  111.  
  112.  
  113.     /**
  114.     * The getPost method is intended to retrive a given post as an object of
  115.     * the Services_Blogging_Post class; given the unique post id which is passed
  116.     * as a parameter to the function.
  117.     *
  118.     * @param string $id The PostID of the post to be retrieved. (As
  119.     *                    returned by newPost() defined in
  120.     *                    Services_Blogging_driver).
  121.     *
  122.     * @return Services_Blogging_Post The elements of the post returned as an
  123.     *                                object of the Services_Blogging_Post class.
  124.     *
  125.     * @throws Services_Blogging_Exception If the post does not exist
  126.     */
  127.     public function getPost($id)
  128.     {
  129.         $request = new XML_RPC_Message('metaWeblog.getPost',
  130.             array(
  131.                 new XML_RPC_Value($id, 'int'),
  132.                 $this->userdata['rpc_user'],
  133.                 $this->userdata['rpc_pass'],
  134.             )
  135.         );
  136.         $arData = Services_Blogging_XmlRpc::sendRequest(
  137.             $request, $this->rpc_client
  138.         );
  139.         return $this->convertStructToPost($arData);
  140.     }//public function getPost($id)
  141.  
  142.  
  143.  
  144.     /**
  145.     * Delete a given post.
  146.     * The deletePost method in metaWeblog is just
  147.     *  an alias to the deletePost blogger method
  148.     *
  149.     * @param mixed $post Services_Blogging_Post object to delete,
  150.     *                     or post id (integer) to delete
  151.     *
  152.     * @return boolean True if deleted, false if not.
  153.     */
  154.     public function deletePost($post)
  155.     {
  156.         if (!($post instanceof Services_Blogging_Post)) {
  157.             $nPostId = $post;
  158.             $post    = new Services_Blogging_Post();
  159.             $post->setId($nPostId);
  160.         }
  161.  
  162.         $request = new XML_RPC_Message('metaWeblog.deletePost',
  163.             array(
  164.                 //dummy API key
  165.                 new XML_RPC_Value('0123456789ABCDEF', 'string'),
  166.                 new XML_RPC_Value($post->id, 'int'),
  167.                 $this->userdata['rpc_user'],
  168.                 $this->userdata['rpc_pass'],
  169.                 new XML_RPC_Value(true, 'boolean')
  170.             )
  171.         );
  172.         Services_Blogging_XmlRpc::sendRequest($request, $this->rpc_client);
  173.     }//public function deletePost($post)
  174.  
  175.  
  176.  
  177.     /**
  178.     * Returns an array of recent posts as Services_Blogging_Post objects
  179.     *
  180.     * @param int $number The number of posts to be retrieved.
  181.     *                     Defaults to 15
  182.     *
  183.     * @return Array An array of objects of the Services_Blogging_Post class that
  184.     *                correspond to the number of posts requested.
  185.     */
  186.     public function getRecentPosts($number = 15)
  187.     {
  188.         $request = new XML_RPC_Message('metaWeblog.getRecentPosts',
  189.             array(
  190.                 $this->userdata['rpc_blogid'],
  191.                 $this->userdata['rpc_user'],
  192.                 $this->userdata['rpc_pass'],
  193.                 new XML_RPC_Value($number, 'int')
  194.             )
  195.         );
  196.         $arData = Services_Blogging_XmlRpc::sendRequest(
  197.             $request, $this->rpc_client
  198.         );
  199.         $arPosts = array();
  200.         foreach ($arData as $data) {
  201.             $post               = $this->convertStructToPost($data);
  202.             $arPosts[$post->id] = $post;
  203.         }
  204.         return $arPosts;
  205.     }//public function getRecentPosts($number = 15)
  206.  
  207.  
  208.  
  209.     /**
  210.     * The getRecentPostTitles method is intended to retrieve the given number of
  211.     * posts titles from a blog.
  212.     * The posts themselves can be retrieved with getPost() or getRecentPosts().
  213.     *
  214.     * There is no direct getRecentPostTitles method in metaWeblog. So
  215.     * we internally call getRecentPosts() and strip out ids and titles of
  216.     * the post. So this method is slow here, because all post data needs
  217.     * to be transmitted.
  218.     *
  219.     * @param int $number The number of posts to be retrieved.
  220.     *
  221.     * @return Array An array of int => strings representing the
  222.     *                post ids (key) and their title (value).
  223.     */
  224.     public function getRecentPostTitles($number = 15)
  225.     {
  226.         $arPosts  = $this->getRecentPosts($number);
  227.         $arTitles = array();
  228.         foreach ($arPosts as $post) {
  229.             $arTitles[$post->id] = $post->{Services_Blogging_Post::TITLE};
  230.         }
  231.         return $arTitles;
  232.     }//public function getRecentPostTitles($number = 15)
  233.  
  234.  
  235.  
  236.     /**
  237.     * Returns an array of strings thay define
  238.     * the properties that a post to this blog may
  239.     * have.
  240.     *
  241.     * @param string $strPostType Type of post to create.
  242.     *                            @see getSupportedPostTypes()
  243.     *
  244.     * @return array Array of strings
  245.     */
  246.     public function getSupportedPostProperties($strPostType = 'post')
  247.     {
  248.         return $this->arSupportedPostProperties;
  249.     }//public function getSupportedPostProperties(..)
  250.  
  251.  
  252.  
  253.     /**
  254.     * Checks if the given property name/id is supported
  255.     * for this driver.
  256.     *
  257.     * @param string $strProperty Property name/id to check
  258.     * @param string $strPostType Type of post to create.
  259.     *                            @see getSupportedPostTypes()
  260.     *
  261.     * @return boolean If the property is supported
  262.     */
  263.     public function isPostPropertySupported($strProperty, $strPostType = 'post')
  264.     {
  265.         return in_array($strProperty, $this->arSupportedPostProperties);
  266.     }//public function isPostPropertySupported(..)
  267.  
  268.  
  269.  
  270.     /**
  271.     * Converts a struct returned by the webservice to
  272.     * a Services_Blogging_Post object
  273.     *
  274.     * @param array $arStruct Struct to convert
  275.     *
  276.     * @return Services_Blogging_Post Converted post
  277.     */
  278.     protected function convertStructToPost($arStruct)
  279.     {
  280.         $post = new Services_Blogging_Post($this);
  281.  
  282.         $post->{Services_Blogging_Post::CONTENT} = $arStruct['description'];
  283.         $post->{Services_Blogging_Post::TITLE}   = $arStruct['title'];
  284.         //0123456789012345678
  285.         //20060514T09:19:33
  286.         $post->{Services_Blogging_Post::DATE} = mktime(
  287.             substr($arStruct['dateCreated'],  9, 2), //hour
  288.             substr($arStruct['dateCreated'], 12, 2), //minute
  289.             substr($arStruct['dateCreated'], 15, 2), //second
  290.             substr($arStruct['dateCreated'],  4, 2), //month
  291.             substr($arStruct['dateCreated'],  6, 2), //day
  292.             substr($arStruct['dateCreated'],  0, 4)  //year
  293.         );
  294.         $post->{Services_Blogging_Post::URL} = $arStruct['link'];
  295.         if (!isset($arStruct['categories'])) {
  296.             $arStruct['categories'] = array();
  297.         }
  298.         $post->{Services_Blogging_Post::CATEGORIES} = $arStruct['categories'];
  299.         $post->setId($arStruct['postid']);
  300.  
  301.         return $post;
  302.     }//protected function convertStructToPost($arStruct)
  303.  
  304.  
  305.  
  306.     /**
  307.     * Converts Services_Blogging_Post object to
  308.     * an XML-RPC struct that can be sent to the server.
  309.     *
  310.     * @param Services_Blogging_Post $post Post object to convert
  311.     *
  312.     * @return void
  313.     */
  314.     protected function convertPostToStruct($post)
  315.     {
  316.         $time = $post->{Services_Blogging_Post::DATE};
  317.         if ($time == ''  || $time == 0) {
  318.             $time = time();
  319.         }
  320.         $categories = $post->{Services_Blogging_Post::CATEGORIES};
  321.         if (!is_array($categories)) {
  322.             $categories = array();
  323.         } else {
  324.             $catstr     = $categories;
  325.             $categories = array();
  326.             foreach ($catstr as $cat) {
  327.                 $categories[] = new XML_RPC_Value($cat, 'string');
  328.             }
  329.         }
  330.  
  331.         return new XML_RPC_Value(
  332.             array(
  333.                 'categories'  => new XML_RPC_Value($categories, 'array'),
  334.                 'dateCreated' => new XML_RPC_Value(date('Ymd\\TH:i:s', $time), 'dateTime.iso8601'),
  335.                 'description' => new XML_RPC_Value($post->{Services_Blogging_Post::CONTENT}, 'string'),
  336.                 'title'       => new XML_RPC_Value($post->{Services_Blogging_Post::TITLE}, 'string')
  337.             ),
  338.             'struct'
  339.         );
  340.     }//protected function convertPostToStruct($post)
  341.  
  342. }//class Services_Blogging_Driver_MetaWeblog extends Services_Blogging_ExtendedDriver
  343. ?>