7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Service
17 * @subpackage SlideShare
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: SlideShare.php 16211 2009-06-21 19:23:55Z thomas $
26 require_once 'Zend/Http/Client.php';
31 require_once 'Zend/Cache.php';
34 * Zend_Service_SlideShare_SlideShow
36 require_once 'Zend/Service/SlideShare/SlideShow.php';
39 * The Zend_Service_SlideShare component is used to interface with the
40 * slideshare.net web server to retrieve slide shows hosted on the web site for
41 * display or other processing.
44 * @package Zend_Service
45 * @subpackage SlideShare
46 * @throws Zend_Service_SlideShare_Exception
47 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
48 * @license http://framework.zend.com/license/new-bsd New BSD License
50 class Zend_Service_SlideShare
54 * Web service result code mapping
56 const SERVICE_ERROR_BAD_APIKEY
= 1;
57 const SERVICE_ERROR_BAD_AUTH
= 2;
58 const SERVICE_ERROR_MISSING_TITLE
= 3;
59 const SERVICE_ERROR_MISSING_FILE
= 4;
60 const SERVICE_ERROR_EMPTY_TITLE
= 5;
61 const SERVICE_ERROR_NOT_SOURCEOBJ
= 6;
62 const SERVICE_ERROR_INVALID_EXT
= 7;
63 const SERVICE_ERROR_FILE_TOO_BIG
= 8;
64 const SERVICE_ERROR_SHOW_NOT_FOUND
= 9;
65 const SERVICE_ERROR_USER_NOT_FOUND
= 10;
66 const SERVICE_ERROR_GROUP_NOT_FOUND
= 11;
67 const SERVICE_ERROR_MISSING_TAG
= 12;
68 const SERVICE_ERROR_DAILY_LIMIT
= 99;
69 const SERVICE_ERROR_ACCOUNT_BLOCKED
= 100;
72 * Slide share Web service communication URIs
74 const SERVICE_UPLOAD_URI
= 'http://www.slideshare.net/api/1/upload_slideshow';
75 const SERVICE_GET_SHOW_URI
= 'http://www.slideshare.net/api/1/get_slideshow';
76 const SERVICE_GET_SHOW_BY_USER_URI
= 'http://www.slideshare.net/api/1/get_slideshow_by_user';
77 const SERVICE_GET_SHOW_BY_TAG_URI
= 'http://www.slideshare.net/api/1/get_slideshow_by_tag';
78 const SERVICE_GET_SHOW_BY_GROUP_URI
= 'http://www.slideshare.net/api/1/get_slideshows_from_group';
81 * The MIME type of Slideshow files
84 const POWERPOINT_MIME_TYPE
= "application/vnd.ms-powerpoint";
87 * The API key to use in requests
89 * @var string The API key
94 * The shared secret to use in requests
96 * @var string the Shared secret
98 protected $_sharedSecret;
101 * The username to use in requests
103 * @var string the username
105 protected $_username;
108 * The password to use in requests
110 * @var string the password
112 protected $_password;
115 * The HTTP Client object to use to perform requests
117 * @var Zend_Http_Client
119 protected $_httpclient;
122 * The Cache object to use to perform caching
124 * @var Zend_Cache_Core
126 protected $_cacheobject;
129 * Sets the Zend_Http_Client object to use in requests. If not provided a default will
132 * @param Zend_Http_Client $client The HTTP client instance to use
133 * @return Zend_Service_SlideShare
135 public function setHttpClient(Zend_Http_Client
$client)
137 $this->_httpclient
= $client;
142 * Returns the instance of the Zend_Http_Client which will be used. Creates an instance
143 * of Zend_Http_Client if no previous client was set.
145 * @return Zend_Http_Client The HTTP client which will be used
147 public function getHttpClient()
150 if(!($this->_httpclient
instanceof Zend_Http_Client
)) {
151 $client = new Zend_Http_Client();
152 $client->setConfig(array('maxredirects' => 2,
155 $this->setHttpClient($client);
158 $this->_httpclient
->resetParameters();
159 return $this->_httpclient
;
163 * Sets the Zend_Cache object to use to cache the results of API queries
165 * @param Zend_Cache_Core $cacheobject The Zend_Cache object used
166 * @return Zend_Service_SlideShare
168 public function setCacheObject(Zend_Cache_Core
$cacheobject)
170 $this->_cacheobject
= $cacheobject;
175 * Gets the Zend_Cache object which will be used to cache API queries. If no cache object
176 * was previously set the the default will be used (Filesystem caching in /tmp with a life
177 * time of 43200 seconds)
179 * @return Zend_Cache_Core The object used in caching
181 public function getCacheObject()
184 if(!($this->_cacheobject
instanceof Zend_Cache_Core
)) {
185 $cache = Zend_Cache
::factory('Core', 'File', array('lifetime' => 43200,
186 'automatic_serialization' => true),
187 array('cache_dir' => '/tmp'));
189 $this->setCacheObject($cache);
192 return $this->_cacheobject
;
196 * Returns the user name used for API calls
198 * @return string The username
200 public function getUserName()
202 return $this->_username
;
206 * Sets the user name to use for API calls
208 * @param string $un The username to use
209 * @return Zend_Service_SlideShare
211 public function setUserName($un)
213 $this->_username
= $un;
218 * Gets the password to use in API calls
220 * @return string the password to use in API calls
222 public function getPassword()
224 return $this->_password
;
228 * Sets the password to use in API calls
230 * @param string $pw The password to use
231 * @return Zend_Service_SlideShare
233 public function setPassword($pw)
235 $this->_password
= (string)$pw;
240 * Gets the API key to be used in making API calls
242 * @return string the API Key
244 public function getApiKey()
246 return $this->_apiKey
;
250 * Sets the API key to be used in making API calls
252 * @param string $key The API key to use
253 * @return Zend_Service_SlideShare
255 public function setApiKey($key)
257 $this->_apiKey
= (string)$key;
262 * Gets the shared secret used in making API calls
264 * @return string the Shared secret
266 public function getSharedSecret()
268 return $this->_sharedSecret
;
272 * Sets the shared secret used in making API calls
274 * @param string $secret the shared secret
275 * @return Zend_Service_SlideShare
277 public function setSharedSecret($secret)
279 $this->_sharedSecret
= (string)$secret;
286 * @param string $apikey The API key
287 * @param string $sharedSecret The shared secret
288 * @param string $username The username
289 * @param string $password The password
291 public function __construct($apikey, $sharedSecret, $username = null, $password = null)
293 $this->setApiKey($apikey)
294 ->setSharedSecret($sharedSecret)
295 ->setUserName($username)
296 ->setPassword($password);
298 $this->_httpclient
= new Zend_Http_Client();
302 * Uploads the specified Slide show the the server
304 * @param Zend_Service_SlideShare_SlideShow $ss The slide show object representing the slide show to upload
305 * @param boolean $make_src_public Determines if the the slide show's source file is public or not upon upload
306 * @return Zend_Service_SlideShare_SlideShow The passed Slide show object, with the new assigned ID provided
308 public function uploadSlideShow(Zend_Service_SlideShare_SlideShow
$ss, $make_src_public = true)
313 $params = array('api_key' => $this->getApiKey(),
315 'hash' => sha1($this->getSharedSecret().$timestamp),
316 'username' => $this->getUserName(),
317 'password' => $this->getPassword(),
318 'slideshow_title' => $ss->getTitle());
320 $description = $ss->getDescription();
321 $tags = $ss->getTags();
323 $filename = $ss->getFilename();
325 if(!file_exists($filename) ||
!is_readable($filename)) {
326 require_once 'Zend/Service/SlideShare/Exception.php';
327 throw new Zend_Service_SlideShare_Exception("Specified Slideshow for upload not found or unreadable");
330 if(!empty($description)) {
331 $params['slideshow_description'] = $description;
333 $params['slideshow_description'] = "";
338 foreach($tags as $tag) {
341 $params['slideshow_tags'] = implode(' ', $tmp);
343 $params['slideshow_tags'] = "";
347 $client = $this->getHttpClient();
348 $client->setUri(self
::SERVICE_UPLOAD_URI
);
349 $client->setParameterPost($params);
350 $client->setFileUpload($filename, "slideshow_srcfile");
352 require_once 'Zend/Http/Client/Exception.php';
354 $response = $client->request('POST');
355 } catch(Zend_Http_Client_Exception
$e) {
356 require_once 'Zend/Service/SlideShare/Exception.php';
357 throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");
360 $sxe = simplexml_load_string($response->getBody());
362 if($sxe->getName() == "SlideShareServiceError") {
363 $message = (string)$sxe->Message
[0];
364 list($code, $error_str) = explode(':', $message);
365 require_once 'Zend/Service/SlideShare/Exception.php';
366 throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
369 if(!$sxe->getName() == "SlideShowUploaded") {
370 require_once 'Zend/Service/SlideShare/Exception.php';
371 throw new Zend_Service_SlideShare_Exception("Unknown XML Respons Received");
374 $ss->setId((int)(string)$sxe->SlideShowID
);
380 * Retrieves a slide show's information based on slide show ID
382 * @param int $ss_id The slide show ID
383 * @return Zend_Service_SlideShare_SlideShow the Slideshow object
385 public function getSlideShow($ss_id)
389 $params = array('api_key' => $this->getApiKey(),
391 'hash' => sha1($this->getSharedSecret().$timestamp),
392 'slideshow_id' => $ss_id);
394 $cache = $this->getCacheObject();
396 $cache_key = md5("__zendslideshare_cache_$ss_id");
398 if(!$retval = $cache->load($cache_key)) {
399 $client = $this->getHttpClient();
401 $client->setUri(self
::SERVICE_GET_SHOW_URI
);
402 $client->setParameterPost($params);
404 require_once 'Zend/Http/Client/Exception.php';
406 $response = $client->request('POST');
407 } catch(Zend_Http_Client_Exception
$e) {
408 require_once 'Zend/Service/SlideShare/Exception.php';
409 throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");
412 $sxe = simplexml_load_string($response->getBody());
414 if($sxe->getName() == "SlideShareServiceError") {
415 $message = (string)$sxe->Message
[0];
416 list($code, $error_str) = explode(':', $message);
417 require_once 'Zend/Service/SlideShare/Exception.php';
418 throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
421 if(!$sxe->getName() == 'Slideshows') {
422 require_once 'Zend/Service/SlideShare/Exception.php';
423 throw new Zend_Service_SlideShare_Exception('Unknown XML Repsonse Received');
426 $retval = $this->_slideShowNodeToObject(clone $sxe->Slideshow
[0]);
428 $cache->save($retval, $cache_key);
435 * Retrieves an array of slide shows for a given username
437 * @param string $username The username to retrieve slide shows from
438 * @param int $offset The offset of the list to start retrieving from
439 * @param int $limit The maximum number of slide shows to retrieve
440 * @return array An array of Zend_Service_SlideShare_SlideShow objects
442 public function getSlideShowsByUsername($username, $offset = null, $limit = null)
444 return $this->_getSlideShowsByType('username_for', $username, $offset, $limit);
448 * Retrieves an array of slide shows based on tag
450 * @param string $tag The tag to retrieve slide shows with
451 * @param int $offset The offset of the list to start retrieving from
452 * @param int $limit The maximum number of slide shows to retrieve
453 * @return array An array of Zend_Service_SlideShare_SlideShow objects
455 public function getSlideShowsByTag($tag, $offset = null, $limit = null)
460 foreach($tag as $t) {
464 $tag = implode(" ", $tmp);
467 return $this->_getSlideShowsByType('tag', $tag, $offset, $limit);
471 * Retrieves an array of slide shows based on group name
473 * @param string $group The group name to retrieve slide shows for
474 * @param int $offset The offset of the list to start retrieving from
475 * @param int $limit The maximum number of slide shows to retrieve
476 * @return array An array of Zend_Service_SlideShare_SlideShow objects
478 public function getSlideShowsByGroup($group, $offset = null, $limit = null)
480 return $this->_getSlideShowsByType('group_name', $group, $offset, $limit);
484 * Retrieves Zend_Service_SlideShare_SlideShow object arrays based on the type of
487 * @param string $key The type of slide show object to retrieve
488 * @param string $value The specific search query for the slide show type to look up
489 * @param int $offset The offset of the list to start retrieving from
490 * @param int $limit The maximum number of slide shows to retrieve
491 * @return array An array of Zend_Service_SlideShare_SlideShow objects
493 protected function _getSlideShowsByType($key, $value, $offset = null, $limit = null)
496 $key = strtolower($key);
500 $responseTag = 'User';
501 $queryUri = self
::SERVICE_GET_SHOW_BY_USER_URI
;
504 $responseTag = 'Group';
505 $queryUri = self
::SERVICE_GET_SHOW_BY_GROUP_URI
;
508 $responseTag = 'Tag';
509 $queryUri = self
::SERVICE_GET_SHOW_BY_TAG_URI
;
512 require_once 'Zend/Service/SlideShare/Exception.php';
513 throw new Zend_Service_SlideShare_Exception("Invalid SlideShare Query");
518 $params = array('api_key' => $this->getApiKey(),
520 'hash' => sha1($this->getSharedSecret().$timestamp),
523 if($offset !== null) {
524 $params['offset'] = (int)$offset;
527 if($limit !== null) {
528 $params['limit'] = (int)$limit;
531 $cache = $this->getCacheObject();
533 $cache_key = md5($key.$value.$offset.$limit);
535 if(!$retval = $cache->load($cache_key)) {
537 $client = $this->getHttpClient();
539 $client->setUri($queryUri);
540 $client->setParameterPost($params);
542 require_once 'Zend/Http/Client/Exception.php';
544 $response = $client->request('POST');
545 } catch(Zend_Http_Client_Exception
$e) {
546 require_once 'Zend/Service/SlideShare/Exception.php';
547 throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");
550 $sxe = simplexml_load_string($response->getBody());
552 if($sxe->getName() == "SlideShareServiceError") {
553 $message = (string)$sxe->Message
[0];
554 list($code, $error_str) = explode(':', $message);
555 require_once 'Zend/Service/SlideShare/Exception.php';
556 throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
559 if(!$sxe->getName() == $responseTag) {
560 require_once 'Zend/Service/SlideShare/Exception.php';
561 throw new Zend_Service_SlideShare_Exception('Unknown or Invalid XML Repsonse Received');
566 foreach($sxe->children() as $node) {
567 if($node->getName() == 'Slideshow') {
568 $retval[] = $this->_slideShowNodeToObject($node);
572 $cache->save($retval, $cache_key);
579 * Converts a SimpleXMLElement object representing a response from the service
580 * into a Zend_Service_SlideShare_SlideShow object
582 * @param SimpleXMLElement $node The input XML from the slideshare.net service
583 * @return Zend_Service_SlideShare_SlideShow The resulting object
585 protected function _slideShowNodeToObject(SimpleXMLElement
$node)
588 if($node->getName() == 'Slideshow') {
590 $ss = new Zend_Service_SlideShare_SlideShow();
592 $ss->setId((string)$node->ID
);
593 $ss->setDescription((string)$node->Description
);
594 $ss->setEmbedCode((string)$node->EmbedCode
);
595 $ss->setNumViews((string)$node->Views
);
596 $ss->setPermaLink((string)$node->Permalink
);
597 $ss->setStatus((string)$node->Status
);
598 $ss->setStatusDescription((string)$node->StatusDescription
);
600 foreach(explode(",", (string)$node->Tags
) as $tag) {
602 if(!in_array($tag, $ss->getTags())) {
607 $ss->setThumbnailUrl((string)$node->Thumbnail
);
608 $ss->setTitle((string)$node->Title
);
609 $ss->setLocation((string)$node->Location
);
610 $ss->setTranscript((string)$node->Transcript
);
616 require_once 'Zend/Service/SlideShare/Exception.php';
617 throw new Zend_Service_SlideShare_Exception("Was not provided the expected XML Node for processing");