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

Documentation generated on Mon, 11 Mar 2019 14:57:58 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.