add scribd service to the hatchery
authorChuck Hagenbuch <chuck@horde.org>
Sat, 15 Nov 2008 17:23:46 +0000 (12:23 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Sat, 15 Nov 2008 17:23:46 +0000 (12:23 -0500)
framework/Service_Scribd/lib/Horde/Service/Scribd.php [new file with mode: 0644]
framework/Service_Scribd/lib/Horde/Service/Scribd/Exception.php [new file with mode: 0644]
framework/Service_Scribd/lib/Horde/Service/Scribd/Request.php [new file with mode: 0644]
framework/Service_Scribd/lib/Horde/Service/Scribd/Response.php [new file with mode: 0644]
framework/Service_Scribd/lib/Horde/Service/Scribd/Result.php [new file with mode: 0644]
framework/Service_Scribd/lib/Horde/Service/Scribd/ResultSet.php [new file with mode: 0644]
framework/Service_Scribd/package.xml [new file with mode: 0644]

diff --git a/framework/Service_Scribd/lib/Horde/Service/Scribd.php b/framework/Service_Scribd/lib/Horde/Service/Scribd.php
new file mode 100644 (file)
index 0000000..265e373
--- /dev/null
@@ -0,0 +1,287 @@
+<?php
+/**
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * http://www.scribd.com/platform/documentation/api?method_name=Authentication
+ * http://www.scribd.com/platform/account
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+
+/**
+ * Scribd client class
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+class Horde_Service_Scribd
+{
+    const ENDPOINT = 'http://api.scribd.com/api';
+
+    /**
+     * HTTP client object to use for accessing the Scribd API.
+     * @var Horde_Http_Client
+     */
+    protected static $_httpClient = null;
+
+    /**
+     * Set the HTTP client instance
+     *
+     * Sets the HTTP client object to use for Scribd requests. If none is set,
+     * the default Horde_Http_Client will be used.
+     *
+     * @param Horde_Http_Client $httpClient
+     */
+    public static function setHttpClient($httpClient)
+    {
+        self::$_httpClient = $httpClient;
+    }
+
+    /**
+     * Gets the HTTP client object.
+     *
+     * @return Horde_Http_Client
+     */
+    public static function getHttpClient()
+    {
+        if (!self::$_httpClient) {
+            self::$_httpClient = new Horde_Http_Client;
+        }
+
+        return self::$_httpClient;
+    }
+
+    /**
+     * @var array
+     */
+    protected $_config = array();
+
+    /**
+     * Constructor
+     *
+     * @param array  API parameters:
+     *   api_key
+     *   api_secret
+     *   session_key
+     *   my_user_id
+     *
+     * @link http://www.scribd.com/platform/documentation/api
+     */
+    public function __construct($config)
+    {
+        $this->_config = $config;
+    }
+
+    /**
+     * Upload a local file.
+     *
+     * @param string   $file     Local file
+     * @param string   $docType  Document type: PDF, DOC, TXT, PPT, etc.
+     * @param string   $access   Document visibility. 'public' or 'private', default 'public'
+     * @param integer  $rev_id   The doc_id to save uploaded file as a revision to
+     *
+     * @return array   [doc_id, access_key, [secret_password]]
+     */
+    public function upload($file, $doc_type = null, $access = null, $revId = null)
+    {
+        $args = array('file' => $file);
+        if ($docType !== null) $args['doc_type'] = $docType;
+        if ($access !== null) $args['access'] = $access;
+        if ($revId !== null) $args['rev_id'] = $revId;
+
+        $response = $this->newRequest('docs.upload', $args)->run();
+        /*@TODO*/
+    }
+
+    /**
+     * Upload a document from a publicly accessible URL.
+     *
+     * @param string   $url      Document location
+     * @param string   $docType  Document type: PDF, DOC, TXT, PPT, etc.
+     * @param string   $access   Document visibility. 'public' or 'private', default 'public'
+     * @param integer  $rev_id   The doc_id to save uploaded file as a revision to
+     *
+     * @return array   [doc_id, access_key, [secret_password]]
+     */
+    public function uploadFromUrl($url, $doc_type = null, $access = null, $rev_id = null)
+    {
+        $args = array('url' => $url);
+        if ($docType !== null) $args['doc_type'] = $docType;
+        if ($access !== null) $args['access'] = $access;
+        if ($revId !== null) $args['rev_id'] = $revId;
+
+        $response = $this->newRequest('docs.uploadFromUrl', $args)->run();
+        /*@TODO*/
+    }
+
+    /**
+     * Return an iterator over the authorized user's documents.
+     *
+     * @return Traversable
+     */
+    public function getList()
+    {
+        return $this->newRequest('docs.getList')->run()->getResultSet();
+    }
+
+    /**
+     * Get the current conversion status of a document.
+     *
+     * @param integer  $docId  Document id to get status for
+     *
+     * @return string  "DISPLAYABLE", "DONE", "ERROR", or "PROCESSING"
+     */
+    public function getConversionStatus($docId)
+    {
+        return (string)$this->newRequest('docs.getConversionStatus', array('doc_id' => $docId))->run()->conversion_status;
+    }
+
+    /**
+     * Get a document's settings
+     *
+     * @param integer  $docId  Document id to get status for
+     *
+     * @return array  [doc_id, title, description, access, license, tags[], show_ads, access_key, thumbnail_url, secret_password]
+     */
+    public function getSettings($docId)
+    {
+        $response = $this->newRequest('docs.getSettings', array('doc_id' => $docId))->run();
+        return array(
+            'doc_id' => $response->doc_id(),
+            'title' => $response->title(),
+            'description' => $response->description(),
+            'access' => $response->access(),
+            'license' => $response->license(),
+            'tags' => strpos($response->tags(), ',') !== false ? explode(',', $response->tags()) : array(),
+            'show_ads' => $response->show_ads(),
+            'access_key' => $response->access_key(),
+            'thumbnail_url' => $response->thumbnail_url(),
+            'secret_password' => $response->secret_password(),
+        );
+    }
+
+    /**
+     * Change a document's settings.
+     *
+     * @param mixed  $docIds    One or more document ids to change.
+     * @param array  $settings  The values to set for each $docId. Possible keys:
+     *                            title:          string
+     *                            description:    string
+     *                            access:         ["public", "private"]
+     *                            license:        ["by", "by-nc", "by-nc-nd", "by-nc-sa", "by-nd", "by-sa", "c", "pd"
+     *                            show_ads:       ["default", "true", "false"]
+     *                            link_back_url:  string
+     *                            tags:           comma-separated stringlist (or PHP array)
+     *
+     * @return true
+     */
+    public function changeSettings($docIds, $settings)
+    {
+        $args = array('doc_ids' => is_array($docIds) ? implode(',', $docIds) : $docIds);
+        foreach (array('title', 'description', 'access', 'license', 'show_ads', 'link_back_url') as $key) {
+            if (isset($settings[$key])) $args[$key] = $settings[$key];
+        }
+        if (isset($settings['tags'])) {
+            $args['tags'] = is_array($settings['tags']) ? implode(',', $settings['tags']) : $settings['tags'];
+        }
+
+        $this->newRequest('docs.changeSettings', $args)->run();
+        return true;
+    }
+
+    /**
+     * Delete a document.
+     *
+     * @param integer  $docId  The document to delete
+     *
+     * @return true
+     */
+    public function delete($docId)
+    {
+        $this->newRequest('docs.delete', array('doc_id' => $docId))->run();
+        return true;
+    }
+
+    /**
+     * Search the Scribd database
+     *
+     * @param string $query : search query
+     * @param int $num_results : number of results to return (10 default, 1000 max)
+     * @param int $num_start : number to start from
+     * @param string $scope : scope of search, "all" or "user"
+     *
+     * @return array of results, each of which contain doc_id, secret password, access_key, title, and description
+     */
+    public function search($query, $num_results = null, $num_start = null, $scope = null)
+    {
+        $params['query'] = $query;
+        $params['num_results'] = $num_results;
+        $params['num_start'] = $num_start;
+        $params['scope'] = $scope;
+
+        return $this->newRequest('docs.search', $args)->run()->getResultSet();
+    }
+
+    /**
+     * Log in as a user
+     *
+     * @param string $username : username of user to log in
+     * @param string $password : password of user to log in
+     *
+     * @return array containing session_key, name, username, and user_id of the user
+     */
+    public function login($username, $password)
+    {
+        $method = "user.login";
+        $params['username'] = $username;
+        $params['password'] = $password;
+
+        $result = $this->postRequest($method, $params);
+        $this->_config['session_key'] = $response->session_key();
+        return $result;
+    }
+
+    /**
+     * Sign up a new user
+     *
+     * @param string $username : username of user to create
+     * @param string $password : password of user to create
+     * @param string $email : email address of user
+     * @param string $name : name of user
+     *
+     * @return array containing session_key, name, username, and user_id of the user
+     */
+    public function signup($username, $password, $email, $name = null)
+    {
+        $method = "user.signup";
+        $params['username'] = $username;
+        $params['password'] = $password;
+        $params['name'] = $name;
+        $params['email'] = $email;
+
+        $result = $this->postRequest($method, $params);
+        $this->_config['session_key'] = $response->session_key();
+        return $result;
+    }
+
+    /**
+     * Create an API request for $method with $args
+     *
+     * @param string $method  The API method to call.
+     * @param string $args    Method arguments
+     *
+     * @return Horde_Service_Scribd_Request
+     */
+    public function newRequest($method, $args = array())
+    {
+        $request = new Horde_Service_Scribd_Request($method, $args);
+        $request->setConfig($this->_config);
+        return $request;
+    }
+
+}
diff --git a/framework/Service_Scribd/lib/Horde/Service/Scribd/Exception.php b/framework/Service_Scribd/lib/Horde/Service/Scribd/Exception.php
new file mode 100644 (file)
index 0000000..48f23a1
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+
+/**
+ * Scribd exception class
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+class Horde_Service_Scribd_Exception extends Exception
+{
+}
diff --git a/framework/Service_Scribd/lib/Horde/Service/Scribd/Request.php b/framework/Service_Scribd/lib/Horde/Service/Scribd/Request.php
new file mode 100644 (file)
index 0000000..a335e49
--- /dev/null
@@ -0,0 +1,71 @@
+<?php
+/**
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+
+/**
+ * Scribd request class
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+class Horde_Service_Scribd_Request
+{
+    protected $_args = array();
+    protected $_config = array();
+    protected $_method;
+
+    public function __construct($method, $args = array())
+    {
+        $this->_method = $method;
+        $this->_args = $args;
+    }
+
+    public function run()
+    {
+        $args = array_merge(
+            $this->_args,
+            $this->_config,
+            array(
+                'method' => $this->_method,
+            )
+        );
+        if (!empty($this->_config['api_secret'])) {
+            $args['api_sig'] = $this->_sign($args);
+        }
+
+        $client = Horde_Service_Scribd::getHttpClient();
+        $response = $client->post(Horde_Service_Scribd::ENDPOINT, $args);
+        return new Horde_Service_Scribd_Response($response->getBody());
+    }
+
+    /**
+     * @param array  $config
+     */
+    public function setConfig($config)
+    {
+        $this->_config = $config;
+    }
+
+    /**
+     * @param array  $args
+     */
+    protected function _sign($args)
+    {
+        $signature = $this->_config['api_secret'];
+        ksort($args);
+        foreach ($args as $k => $v) {
+            $signature .= $k . $v;
+        }
+
+        return md5($signature);
+    }
+
+}
diff --git a/framework/Service_Scribd/lib/Horde/Service/Scribd/Response.php b/framework/Service_Scribd/lib/Horde/Service/Scribd/Response.php
new file mode 100644 (file)
index 0000000..ecd9e50
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+/**
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+
+/**
+ * Scribd response class
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+class Horde_Service_Scribd_Response extends Horde_Xml_Element
+{
+    /**
+     * Constructor. Do basic error checking on the resposne.
+     *
+     * @param DOMElement $element The DOM element we're encapsulating.
+     */
+    public function __construct($element = null)
+    {
+        parent::__construct($element);
+
+        if ($this['stat'] != 'ok') {
+            throw new Horde_Service_Scribd_Exception($this->error['message'], $this->error['code']);
+        }
+    }
+
+    /*
+            if($result['stat'] == "ok"){
+
+                //This is shifty. Works currently though.
+                $result = $this->convert_simplexml_to_array($result);
+                if(urlencode((string)$result) == "%0A%0A" && $this->error == 0){
+                    $result = "1";
+                    return $result;
+                }else{
+                    return $result;
+                }
+            }
+    */
+
+    public function getResultSet()
+    {
+        return new Horde_Service_Scribd_ResultSet($this->resultset);
+    }
+
+}
diff --git a/framework/Service_Scribd/lib/Horde/Service/Scribd/Result.php b/framework/Service_Scribd/lib/Horde/Service/Scribd/Result.php
new file mode 100644 (file)
index 0000000..b6a91b9
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+
+/**
+ * Scribd result class
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+class Horde_Service_Scribd_Result extends Horde_Xml_Element
+{
+}
diff --git a/framework/Service_Scribd/lib/Horde/Service/Scribd/ResultSet.php b/framework/Service_Scribd/lib/Horde/Service/Scribd/ResultSet.php
new file mode 100644 (file)
index 0000000..f79386b
--- /dev/null
@@ -0,0 +1,43 @@
+<?php
+/**
+ * Copyright 2008 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+
+/**
+ * Scribd result set class
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Service_Scribd
+ */
+class Horde_Service_Scribd_ResultSet extends Horde_Xml_Element_List
+{
+    /**
+     * The classname for individual feed elements.
+     * @var string
+     */
+    protected $_listItemClassName = 'Horde_Service_Scribd_Result';
+
+    /**
+     * Cache the individual list items so they don't need to be
+     * searched for on every operation.
+     */
+    protected function _buildListItemCache()
+    {
+        $results = array();
+        foreach ($this->_element->childNodes as $child) {
+            if ($child->localName == 'result') {
+                $results[] = $child;
+            }
+        }
+
+        return $results;
+    }
+
+}
diff --git a/framework/Service_Scribd/package.xml b/framework/Service_Scribd/package.xml
new file mode 100644 (file)
index 0000000..dcd8ec8
--- /dev/null
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package packagerversion="1.4.9" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
+http://pear.php.net/dtd/tasks-1.0.xsd
+http://pear.php.net/dtd/package-2.0
+http://pear.php.net/dtd/package-2.0.xsd">
+ <name>Service_Scribd</name>
+ <channel>pear.horde.org</channel>
+ <summary>Horde Scribd client</summary>
+ <description>This package provides client libraries for the Scribd API
+ </description>
+ <lead>
+  <name>Chuck Hagenbuch</name>
+  <user>chuck</user>
+  <email>chuck@horde.org</email>
+  <active>yes</active>
+ </lead>
+ <date>2008-08-01</date>
+ <version>
+  <release>0.1.0</release>
+  <api>0.1.0</api>
+ </version>
+ <stability>
+  <release>alpha</release>
+  <api>alpha</api>
+ </stability>
+ <license uri="http://opensource.org/licenses/bsd-license.php">BSD</license>
+ <notes>
+* Initial release
+ </notes>
+ <contents>
+  <dir name="/">
+   <dir name="lib">
+    <dir name="Horde">
+     <dir name="Service">
+      <dir name="Scribd">
+       <file name="Exception.php" role="php" />
+       <file name="Request.php" role="php" />
+       <file name="Response.php" role="php" />
+       <file name="Result.php" role="php" />
+       <file name="ResultSet.php" role="php" />
+      </dir> <!-- /lib/Horde/Service/Scribd -->
+      <file name="Scribd.php" role="php" />
+     </dir> <!-- /lib/Horde/Service -->
+    </dir> <!-- /lib/Horde -->
+   </dir> <!-- /lib -->
+  </dir> <!-- / -->
+ </contents>
+ <dependencies>
+  <required>
+   <php>
+    <min>5.2.0</min>
+   </php>
+   <pearinstaller>
+    <min>1.5.0</min>
+   </pearinstaller>
+   <package>
+    <name>Http_Client</name>
+    <channel>pear.horde.org</channel>
+   </package>
+   <package>
+    <name>Xml_Element</name>
+    <channel>pear.horde.org</channel>
+   </package>
+  </required>
+ </dependencies>
+ <phprelease>
+  <filelist>
+   <install name="lib/Horde/Service/Scribd/Exception.php" as="Horde/Service/Scribd/Exception.php" />
+   <install name="lib/Horde/Service/Scribd/Request.php" as="Horde/Service/Scribd/Request.php" />
+   <install name="lib/Horde/Service/Scribd/Response.php" as="Horde/Service/Scribd/Response.php" />
+   <install name="lib/Horde/Service/Scribd/Result.php" as="Horde/Service/Scribd/Result.php" />
+   <install name="lib/Horde/Service/Scribd/ResultSet.php" as="Horde/Service/Scribd/ResultSet.php" />
+   <install name="lib/Horde/Service/Scribd.php" as="Horde/Service/Scribd.php" />
+  </filelist>
+ </phprelease>
+</package>