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

Source for file Blogging.php

Documentation is available at Blogging.php

  1. <?php
  2. require_once 'Services/Blogging/Exception.php';
  3.  
  4. /**
  5. * Generic package for several Blogging APIs.
  6. *
  7. * Create a new instance via
  8. * Services_Blogging::factory($driver, $username, $password, $server, $path),
  9. * or more easy via
  10. * Services_Blogging::discoverDriver($url, $username, $password) .
  11. *
  12. * Note that some Blogging APIs allow multiple blogs with one
  13. * account. These drivers implement Services_Blogging_MultipleBlogsInterface
  14. * - you need to call setBlogId($id) before you can use the driver in that case.
  15. *
  16. @category Services
  17. @package  Services_Blogging
  18. @author   Christian Weiske <cweiske@php.net>
  19. @author   Anant Narayanan <anant@php.net>
  20. @license  http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  21. @version  CVS: $Id: Blogging.php,v 1.3 2008/02/07 13:33:31 cweiske Exp $
  22. @link     http://pear.php.net/package/Services_Blogging
  23. *
  24. @todo
  25. *  Missing drivers
  26. *  - MovableType
  27. *  - Conversant
  28. *  - Manila
  29. *  - MetaWiki
  30. *  - Antville
  31. */
  32. {
  33.  
  34.     /**
  35.      * Exception codes and messages that are thrown by the class.
  36.      */
  37.     const ERROR_DRIVER                 = 101;
  38.     const ERROR_BLOGHASNTAUTODISCOVERY = 102;
  39.     const ERROR_NOSUPPORTEDDRIVER      = 103;
  40.     const ERROR_NOTSUPPORTED           = 104;
  41.     
  42.     /**
  43.      * USER_AGENT to send along during requests.
  44.      */
  45.     const USER_AGENT = 'PHP/Services_Blogging @package_version@';
  46.  
  47.  
  48.     /**
  49.     * The factory function that instantiates the appropriate class and returns
  50.     * the object, so that further methods may be executed. This function serves
  51.     * as the single entry point for the class.
  52.     *
  53.     * @param string $driver   The driver name, currently either "Blogger"
  54.     *                           or "metaWeblog".
  55.     * @param string $username The username of the blog account to connect to.
  56.     * @param string $password The password of the blog account to connect to.
  57.     * @param string $server   The URI of the blog's server.
  58.     * @param string $path     The location of the XML-RPC server script.
  59.     *
  60.     * @return Services_Blogging_Driver Blogging driver instance
  61.     */
  62.     public static function factory($driver$username$password$server$path)
  63.     {
  64.         include_once 'Services/Blogging/Driver/' $driver '.php';
  65.         $strClass 'Services_Blogging_Driver_' $driver;
  66.         if (!class_exists($strClass)) {
  67.             throw new Services_Blogging_Exception(
  68.                 'Invalid driver "' $driver '" specified!'self::ERROR_DRIVER
  69.             );
  70.         }
  71.  
  72.         $class = new $strClass($username$password$server$path);
  73.         return $class;
  74.     }//public static function factory($driver, $username, $password, $server, $path)
  75.  
  76.  
  77.  
  78.     /**
  79.     * Autodiscover the driver settings from the given blog URL
  80.     * and create a driver instance.
  81.     *
  82.     * @param string $url      Blog URL
  83.     * @param string $username Username for the blog account
  84.     * @param string $password Password for the blog account
  85.     *
  86.     * @return Services_Blogging_Driver The driver object if all goes ok
  87.     *
  88.     * @throws Services_Blogging_Exception If an error occured
  89.     */
  90.     public static function discoverDriver($url$username$password)
  91.     {
  92.         $settings = self::discoverSettings($url);
  93.         if ($settings === false{
  94.             throw new Services_Blogging_Exception(
  95.                 'Autodiscovery of settings not supported by the blog',
  96.                 self::ERROR_BLOGHASNTAUTODISCOVERY
  97.             );
  98.         }
  99.         $driver = self::getBestAvailableDriver($settings);
  100.         if ($driver === false{
  101.             throw new Services_Blogging_Exception(
  102.                 'None of the supported drivers available',
  103.                 self::ERROR_NOSUPPORTEDDRIVER
  104.             );
  105.         }
  106.  
  107.         return self::factory(
  108.             $driver,
  109.             $username,
  110.             $password,
  111.             $settings['apis'][$driver]['server'],
  112.             $settings['apis'][$driver]['path']
  113.         );
  114.     }//public static function discoverDriver($url, $username, $password)
  115.  
  116.  
  117.  
  118.     /**
  119.     * Tries to auto-discover the driver settings for the blog
  120.     * at the given URL.
  121.     * Internally, an RSD page is tried to load and read.
  122.     *
  123.     * @param string $url Url of the blog
  124.     *
  125.     * @return mixed FALSE if nothing found, OR array of settings:
  126.     *                   - engineName
  127.     *                   - engineLink
  128.     *                   - homePageLink
  129.     *                   - apis => array (key is name
  130.     *                       - name
  131.     *                       - preferred
  132.     *                       - apiLink (url)
  133.     *                       - server (for factory())
  134.     *                       - path   (for factory())
  135.     *
  136.     *  @link http://archipelago.phrasewise.com/display?page=oldsite/1330.html
  137.     */
  138.     public static function discoverSettings($url)
  139.     {
  140.         $content file_get_contents($url);
  141.         if ($content === false{
  142.             return false;
  143.         }
  144.  
  145.         //search for a line like this:
  146.         //<link rel="EditURI" type="application/rsd+xml" title="RSD"
  147.         // href="http://blog.bogo/xmlrpc.php?rsd" />
  148.         if (!preg_match_all('|<link\\s+rel="EditURI".+href="(.+?)"|',
  149.                 $content$matches)
  150.         {
  151.             return false;
  152.         }
  153.  
  154.         $rsdUrl reset($matches[1]);
  155.         $root   simplexml_load_string(file_get_contents($rsdUrl));
  156.         if ($root === false{
  157.             return false;
  158.         }
  159.  
  160.         $apis = array();
  161.         foreach ($root->service->apis->api as $api{
  162.             $ap         = array();
  163.             $ap['name'= (string)$api['name'];
  164.             if ($ap['name'== 'Movable Type'{
  165.                 $ap['name''MovableType';
  166.             }
  167.             $ap['preferred'$api['preferred'== 1 || $api['preferred'== 'true';
  168.             $ap['apiLink']   = (string)$api['apiLink'];
  169.  
  170.             //try to get server and path
  171.             $dslashpos strpos($ap['apiLink']'//');
  172.             if ($dslashpos === false{
  173.                 $nBegin = 0;
  174.             else {
  175.                 $nBegin $dslashpos + 2;
  176.             }
  177.             $slashpos     strpos($ap['apiLink']'/'$nBegin);
  178.             $ap['server'substr($ap['apiLink']0$slashpos);
  179.             $ap['path']   substr($ap['apiLink']$slashpos);
  180.  
  181.             $apis[$ap['name']] $ap;
  182.         }
  183.  
  184.         $data = array(
  185.             'engineName'   => (string)$root->service->engineName,
  186.             'engineLink'   => (string)$root->service->engineLink,
  187.             'homePageLink' => (string)$root->service->homePageLink,
  188.             'apis'         => $apis
  189.         );
  190.         return $data;
  191.     }//public static function discoverSettings($url)
  192.  
  193.  
  194.  
  195.     /**
  196.     * Tries to return the best available driver for the given
  197.     * settings array. The settings array is returned by
  198.     * Services_Blogging::discoverSettings()
  199.     *
  200.     * @param array $arSettings Settings array
  201.     *
  202.     * @return string The driver to use, false if none found
  203.     */
  204.     public static function getBestAvailableDriver($arSettings)
  205.     {
  206.         if (isset($arSettings['apis'])) {
  207.             $arSettings $arSettings['apis'];
  208.         }
  209.         //find preferred one
  210.         $driver = null;
  211.         foreach ($arSettings as $id => $api{
  212.             if ($api['preferred'=== true{
  213.                 if (self::driverExists($api['name'])) {
  214.                     return $api['name'];
  215.                 }
  216.                 unset($arSettings[$id]);
  217.             }
  218.         }
  219.         foreach ($arSettings as $id => $api{
  220.             if (self::driverExists($api['name'])) {
  221.                 return $api['name'];
  222.             }
  223.         }
  224.  
  225.         return false;
  226.     }//public static function getBestAvailableDriver($arSettings)
  227.  
  228.  
  229.  
  230.     /**
  231.     * Tries to include the driver file and checks if
  232.     * the driver class exists.
  233.     *
  234.     * @param string $driver Driver to check
  235.     *
  236.     * @return boolean If the driver exists
  237.     */
  238.     protected static function driverExists($driver)
  239.     {
  240.         @include_once 'Services/Blogging/Driver/' $driver '.php';
  241.         return class_exists('Services_Blogging_Driver_' $driver);
  242.     }//protected static function driverExists($driver)
  243.  
  244. }//class Services_Blogging
  245. ?>

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