1: <?php
2: /**
3: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
4: *
5: * http://www.scribd.com/platform/documentation/api?method_name=Authentication
6: * http://www.scribd.com/platform/account
7: * http://github.com/richid/services_scribd/tree/master
8: *
9: * @author Chuck Hagenbuch <chuck@horde.org>
10: * @license http://www.horde.org/licenses/bsd BSD
11: * @category Horde
12: * @package Service_Scribd
13: */
14:
15: /**
16: * Scribd client class
17: *
18: * @author Chuck Hagenbuch <chuck@horde.org>
19: * @license http://www.horde.org/licenses/bsd BSD
20: * @category Horde
21: * @package Service_Scribd
22: */
23: class Horde_Service_Scribd
24: {
25: const ENDPOINT = 'http://api.scribd.com/api';
26:
27: /**
28: * HTTP client object to use for accessing the Scribd API.
29: * @var Horde_Http_Client
30: */
31: protected static $_httpClient = null;
32:
33: /**
34: * Set the HTTP client instance
35: *
36: * Sets the HTTP client object to use for Scribd requests. If none is set,
37: * the default Horde_Http_Client will be used.
38: *
39: * @param Horde_Http_Client $httpClient
40: */
41: public static function setHttpClient($httpClient)
42: {
43: self::$_httpClient = $httpClient;
44: }
45:
46: /**
47: * Gets the HTTP client object.
48: *
49: * @return Horde_Http_Client
50: */
51: public static function getHttpClient()
52: {
53: if (!self::$_httpClient) {
54: self::$_httpClient = new Horde_Http_Client;
55: }
56:
57: return self::$_httpClient;
58: }
59:
60: /**
61: * @var array
62: */
63: protected $_config = array();
64:
65: /**
66: * Constructor
67: *
68: * @param array API parameters:
69: * api_key
70: * api_secret
71: * session_key
72: * my_user_id
73: *
74: * @link http://www.scribd.com/platform/documentation/api
75: */
76: public function __construct($config)
77: {
78: $this->_config = $config;
79: }
80:
81: /**
82: * Upload a local file.
83: *
84: * @param string $file Local file
85: * @param string $docType Document type: PDF, DOC, TXT, PPT, etc.
86: * @param string $access Document visibility. 'public' or 'private', default 'public'
87: * @param integer $rev_id The doc_id to save uploaded file as a revision to
88: *
89: * @return array [doc_id, access_key, [secret_password]]
90: */
91: public function upload($file, $doc_type = null, $access = null, $revId = null)
92: {
93: $args = array('file' => $file);
94: if ($docType !== null) $args['doc_type'] = $docType;
95: if ($access !== null) $args['access'] = $access;
96: if ($revId !== null) $args['rev_id'] = $revId;
97:
98: $response = $this->newRequest('docs.upload', $args)->run();
99: /*@TODO*/
100: }
101:
102: /**
103: * Upload a document from a publicly accessible URL.
104: *
105: * @param string $url Document location
106: * @param string $docType Document type: PDF, DOC, TXT, PPT, etc.
107: * @param string $access Document visibility. 'public' or 'private', default 'public'
108: * @param integer $rev_id The doc_id to save uploaded file as a revision to
109: *
110: * @return array [doc_id, access_key, [secret_password]]
111: */
112: public function uploadFromUrl($url, $doc_type = null, $access = null, $rev_id = null)
113: {
114: $args = array('url' => $url);
115: if ($docType !== null) $args['doc_type'] = $docType;
116: if ($access !== null) $args['access'] = $access;
117: if ($revId !== null) $args['rev_id'] = $revId;
118:
119: $response = $this->newRequest('docs.uploadFromUrl', $args)->run();
120: /*@TODO*/
121: }
122:
123: /**
124: * Return an iterator over the authorized user's documents.
125: *
126: * @return Traversable
127: */
128: public function getList()
129: {
130: return $this->newRequest('docs.getList')->run()->getResultSet();
131: }
132:
133: /**
134: * Get the current conversion status of a document.
135: *
136: * @param integer $docId Document id to get status for
137: *
138: * @return string "DISPLAYABLE", "DONE", "ERROR", or "PROCESSING"
139: */
140: public function getConversionStatus($docId)
141: {
142: return (string)$this->newRequest('docs.getConversionStatus', array('doc_id' => $docId))->run()->conversion_status;
143: }
144:
145: /**
146: * Get a document's settings
147: *
148: * @param integer $docId Document id to get status for
149: *
150: * @return array [doc_id, title, description, access, license, tags[], show_ads, access_key, thumbnail_url, secret_password]
151: */
152: public function getSettings($docId)
153: {
154: $response = $this->newRequest('docs.getSettings', array('doc_id' => $docId))->run();
155: return array(
156: 'doc_id' => $response->doc_id(),
157: 'title' => $response->title(),
158: 'description' => $response->description(),
159: 'access' => $response->access(),
160: 'license' => $response->license(),
161: 'tags' => strpos($response->tags(), ',') !== false ? explode(',', $response->tags()) : array(),
162: 'show_ads' => $response->show_ads(),
163: 'access_key' => $response->access_key(),
164: 'thumbnail_url' => $response->thumbnail_url(),
165: 'secret_password' => $response->secret_password(),
166: );
167: }
168:
169: /**
170: * Change a document's settings.
171: *
172: * @param mixed $docIds One or more document ids to change.
173: * @param array $settings The values to set for each $docId. Possible keys:
174: * title: string
175: * description: string
176: * access: ["public", "private"]
177: * license: ["by", "by-nc", "by-nc-nd", "by-nc-sa", "by-nd", "by-sa", "c", "pd"
178: * show_ads: ["default", "true", "false"]
179: * link_back_url: string
180: * tags: comma-separated stringlist (or PHP array)
181: *
182: * @return true
183: */
184: public function changeSettings($docIds, $settings)
185: {
186: $args = array('doc_ids' => is_array($docIds) ? implode(',', $docIds) : $docIds);
187: foreach (array('title', 'description', 'access', 'license', 'show_ads', 'link_back_url') as $key) {
188: if (isset($settings[$key])) $args[$key] = $settings[$key];
189: }
190: if (isset($settings['tags'])) {
191: $args['tags'] = is_array($settings['tags']) ? implode(',', $settings['tags']) : $settings['tags'];
192: }
193:
194: $this->newRequest('docs.changeSettings', $args)->run();
195: return true;
196: }
197:
198: /**
199: * Delete a document.
200: *
201: * @param integer $docId The document to delete
202: *
203: * @return true
204: */
205: public function delete($docId)
206: {
207: $this->newRequest('docs.delete', array('doc_id' => $docId))->run();
208: return true;
209: }
210:
211: /**
212: * Search the Scribd database
213: *
214: * @param string $query : search query
215: * @param int $num_results : number of results to return (10 default, 1000 max)
216: * @param int $num_start : number to start from
217: * @param string $scope : scope of search, "all" or "user"
218: *
219: * @return array of results, each of which contain doc_id, secret password, access_key, title, and description
220: */
221: public function search($query, $num_results = null, $num_start = null, $scope = null)
222: {
223: $params['query'] = $query;
224: $params['num_results'] = $num_results;
225: $params['num_start'] = $num_start;
226: $params['scope'] = $scope;
227:
228: return $this->newRequest('docs.search', $args)->run()->getResultSet();
229: }
230:
231: /**
232: * Log in as a user
233: *
234: * @param string $username : username of user to log in
235: * @param string $password : password of user to log in
236: *
237: * @return array containing session_key, name, username, and user_id of the user
238: */
239: public function login($username, $password)
240: {
241: $method = "user.login";
242: $params['username'] = $username;
243: $params['password'] = $password;
244:
245: $result = $this->postRequest($method, $params);
246: $this->_config['session_key'] = $response->session_key();
247: return $result;
248: }
249:
250: /**
251: * Sign up a new user
252: *
253: * @param string $username : username of user to create
254: * @param string $password : password of user to create
255: * @param string $email : email address of user
256: * @param string $name : name of user
257: *
258: * @return array containing session_key, name, username, and user_id of the user
259: */
260: public function signup($username, $password, $email, $name = null)
261: {
262: $method = "user.signup";
263: $params['username'] = $username;
264: $params['password'] = $password;
265: $params['name'] = $name;
266: $params['email'] = $email;
267:
268: $result = $this->postRequest($method, $params);
269: $this->_config['session_key'] = $response->session_key();
270: return $result;
271: }
272:
273: /**
274: * Create an API request for $method with $args
275: *
276: * @param string $method The API method to call.
277: * @param string $args Method arguments
278: *
279: * @return Horde_Service_Scribd_Request
280: */
281: public function newRequest($method, $args = array())
282: {
283: $request = new Horde_Service_Scribd_Request($method, $args);
284: $request->setConfig($this->_config);
285: return $request;
286: }
287:
288: }
289: