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.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  8.0 KB  |  246 lines

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