File indexing completed on 2024-12-22 05:37:06
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service 0017 * @subpackage SlideShare 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * Zend_Http_Client 0025 */ 0026 // require_once 'Zend/Http/Client.php'; 0027 0028 /** 0029 * Zend_Cache 0030 */ 0031 // require_once 'Zend/Cache.php'; 0032 0033 /** 0034 * Zend_Service_SlideShare_SlideShow 0035 */ 0036 // require_once 'Zend/Service/SlideShare/SlideShow.php'; 0037 0038 /** Zend_Xml_Security */ 0039 // require_once 'Zend/Xml/Security.php'; 0040 0041 /** 0042 * The Zend_Service_SlideShare component is used to interface with the 0043 * slideshare.net web server to retrieve slide shows hosted on the web site for 0044 * display or other processing. 0045 * 0046 * @category Zend 0047 * @package Zend_Service 0048 * @subpackage SlideShare 0049 * @throws Zend_Service_SlideShare_Exception 0050 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0051 * @license http://framework.zend.com/license/new-bsd New BSD License 0052 */ 0053 class Zend_Service_SlideShare 0054 { 0055 /** 0056 * Web service result code mapping 0057 */ 0058 const SERVICE_ERROR_BAD_APIKEY = 1; 0059 const SERVICE_ERROR_BAD_AUTH = 2; 0060 const SERVICE_ERROR_MISSING_TITLE = 3; 0061 const SERVICE_ERROR_MISSING_FILE = 4; 0062 const SERVICE_ERROR_EMPTY_TITLE = 5; 0063 const SERVICE_ERROR_NOT_SOURCEOBJ = 6; 0064 const SERVICE_ERROR_INVALID_EXT = 7; 0065 const SERVICE_ERROR_FILE_TOO_BIG = 8; 0066 const SERVICE_ERROR_SHOW_NOT_FOUND = 9; 0067 const SERVICE_ERROR_USER_NOT_FOUND = 10; 0068 const SERVICE_ERROR_GROUP_NOT_FOUND = 11; 0069 const SERVICE_ERROR_MISSING_TAG = 12; 0070 const SERVICE_ERROR_DAILY_LIMIT = 99; 0071 const SERVICE_ERROR_ACCOUNT_BLOCKED = 100; 0072 0073 /** 0074 * Slide share Web service communication URIs 0075 */ 0076 const SERVICE_UPLOAD_URI = 'https://www.slideshare.net/api/2/upload_slideshow'; 0077 const SERVICE_GET_SHOW_URI = 'https://www.slideshare.net/api/2/get_slideshow'; 0078 const SERVICE_GET_SHOW_BY_USER_URI = 'https://www.slideshare.net/api/2/get_slideshows_by_user'; 0079 const SERVICE_GET_SHOW_BY_TAG_URI = 'https://www.slideshare.net/api/2/get_slideshows_by_tag'; 0080 const SERVICE_GET_SHOW_BY_GROUP_URI = 'https://www.slideshare.net/api/2/get_slideshows_by_group'; 0081 0082 /** 0083 * The MIME type of Slideshow files 0084 * 0085 */ 0086 const POWERPOINT_MIME_TYPE = "application/vnd.ms-powerpoint"; 0087 0088 /** 0089 * The API key to use in requests 0090 * 0091 * @var string The API key 0092 */ 0093 protected $_apiKey; 0094 0095 /** 0096 * The shared secret to use in requests 0097 * 0098 * @var string the Shared secret 0099 */ 0100 protected $_sharedSecret; 0101 0102 /** 0103 * The username to use in requests 0104 * 0105 * @var string the username 0106 */ 0107 protected $_username; 0108 0109 /** 0110 * The password to use in requests 0111 * 0112 * @var string the password 0113 */ 0114 protected $_password; 0115 0116 /** 0117 * The HTTP Client object to use to perform requests 0118 * 0119 * @var Zend_Http_Client 0120 */ 0121 protected $_httpclient; 0122 0123 /** 0124 * The Cache object to use to perform caching 0125 * 0126 * @var Zend_Cache_Core 0127 */ 0128 protected $_cacheobject; 0129 0130 /** 0131 * Sets the Zend_Http_Client object to use in requests. If not provided a 0132 * default will be used. 0133 * 0134 * @param Zend_Http_Client $client The HTTP client instance to use 0135 * @return Zend_Service_SlideShare 0136 */ 0137 public function setHttpClient(Zend_Http_Client $client) 0138 { 0139 $this->_httpclient = $client; 0140 return $this; 0141 } 0142 0143 /** 0144 * Returns the instance of the Zend_Http_Client which will be used. Creates 0145 * an instance of Zend_Http_Client if no previous client was set. 0146 * 0147 * @return Zend_Http_Client The HTTP client which will be used 0148 */ 0149 public function getHttpClient() 0150 { 0151 0152 if (!($this->_httpclient instanceof Zend_Http_Client)) { 0153 $client = new Zend_Http_Client(); 0154 $client->setConfig( 0155 array( 0156 'maxredirects' => 2, 0157 'timeout' => 5 0158 ) 0159 ); 0160 0161 $this->setHttpClient($client); 0162 } 0163 0164 $this->_httpclient->resetParameters(); 0165 0166 return $this->_httpclient; 0167 } 0168 0169 /** 0170 * Sets the Zend_Cache object to use to cache the results of API queries 0171 * 0172 * @param Zend_Cache_Core $cacheobject The Zend_Cache object used 0173 * @return Zend_Service_SlideShare 0174 */ 0175 public function setCacheObject(Zend_Cache_Core $cacheobject) 0176 { 0177 $this->_cacheobject = $cacheobject; 0178 return $this; 0179 } 0180 0181 /** 0182 * Gets the Zend_Cache object which will be used to cache API queries. If no 0183 * cache object was previously set the the default will be used (Filesystem 0184 * caching in /tmp with a life time of 43200 seconds) 0185 * 0186 * @return Zend_Cache_Core The object used in caching 0187 */ 0188 public function getCacheObject() 0189 { 0190 0191 if (!($this->_cacheobject instanceof Zend_Cache_Core)) { 0192 $cache = Zend_Cache::factory( 0193 'Core', 0194 'File', 0195 array( 0196 'lifetime' => 43200, 0197 'automatic_serialization' => true 0198 ), 0199 array('cache_dir' => '/tmp') 0200 ); 0201 0202 $this->setCacheObject($cache); 0203 } 0204 0205 return $this->_cacheobject; 0206 } 0207 0208 /** 0209 * Returns the user name used for API calls 0210 * 0211 * @return string The username 0212 */ 0213 public function getUserName() 0214 { 0215 return $this->_username; 0216 } 0217 0218 /** 0219 * Sets the user name to use for API calls 0220 * 0221 * @param string $un The username to use 0222 * @return Zend_Service_SlideShare 0223 */ 0224 public function setUserName($un) 0225 { 0226 $this->_username = $un; 0227 return $this; 0228 } 0229 0230 /** 0231 * Gets the password to use in API calls 0232 * 0233 * @return string the password to use in API calls 0234 */ 0235 public function getPassword() 0236 { 0237 return $this->_password; 0238 } 0239 0240 /** 0241 * Sets the password to use in API calls 0242 * 0243 * @param string $pw The password to use 0244 * @return Zend_Service_SlideShare 0245 */ 0246 public function setPassword($pw) 0247 { 0248 $this->_password = (string)$pw; 0249 return $this; 0250 } 0251 0252 /** 0253 * Gets the API key to be used in making API calls 0254 * 0255 * @return string the API Key 0256 */ 0257 public function getApiKey() 0258 { 0259 return $this->_apiKey; 0260 } 0261 0262 /** 0263 * Sets the API key to be used in making API calls 0264 * 0265 * @param string $key The API key to use 0266 * @return Zend_Service_SlideShare 0267 */ 0268 public function setApiKey($key) 0269 { 0270 $this->_apiKey = (string)$key; 0271 return $this; 0272 } 0273 0274 /** 0275 * Gets the shared secret used in making API calls 0276 * 0277 * @return string the Shared secret 0278 */ 0279 public function getSharedSecret() 0280 { 0281 return $this->_sharedSecret; 0282 } 0283 0284 /** 0285 * Sets the shared secret used in making API calls 0286 * 0287 * @param string $secret the shared secret 0288 * @return Zend_Service_SlideShare 0289 */ 0290 public function setSharedSecret($secret) 0291 { 0292 $this->_sharedSecret = (string)$secret; 0293 return $this; 0294 } 0295 0296 /** 0297 * The Constructor 0298 * 0299 * @param string $apikey The API key 0300 * @param string $sharedSecret The shared secret 0301 * @param string $username The username 0302 * @param string $password The password 0303 */ 0304 public function __construct( 0305 $apikey, $sharedSecret, $username = null, $password = null 0306 ) 0307 { 0308 $this->setApiKey($apikey) 0309 ->setSharedSecret($sharedSecret) 0310 ->setUserName($username) 0311 ->setPassword($password); 0312 0313 $this->_httpclient = new Zend_Http_Client(); 0314 } 0315 0316 /** 0317 * Uploads the specified Slide show the the server 0318 * 0319 * @param Zend_Service_SlideShare_SlideShow $ss The slide show 0320 * object representing the 0321 * slide show to upload 0322 * @param boolean $makeSrcPublic Determines if the slide 0323 * show's source file is public 0324 * or not upon upload 0325 * @return Zend_Service_SlideShare_SlideShow The passed Slide show object, 0326 * with the new assigned ID 0327 * provided 0328 * @throws Zend_Service_SlideShare_Exception 0329 */ 0330 public function uploadSlideShow( 0331 Zend_Service_SlideShare_SlideShow $ss, $makeSrcPublic = true 0332 ) 0333 { 0334 $timestamp = time(); 0335 0336 $params = array( 0337 'api_key' => $this->getApiKey(), 0338 'ts' => $timestamp, 0339 'hash' => sha1($this->getSharedSecret() . $timestamp), 0340 'username' => $this->getUserName(), 0341 'password' => $this->getPassword(), 0342 'slideshow_title' => $ss->getTitle() 0343 ); 0344 0345 $description = $ss->getDescription(); 0346 $tags = $ss->getTags(); 0347 0348 $filename = $ss->getFilename(); 0349 0350 if (!file_exists($filename) || !is_readable($filename)) { 0351 // require_once 'Zend/Service/SlideShare/Exception.php'; 0352 throw new Zend_Service_SlideShare_Exception( 0353 'Specified Slideshow for upload not found or unreadable' 0354 ); 0355 } 0356 0357 if (!empty($description)) { 0358 $params['slideshow_description'] = $description; 0359 } else { 0360 $params['slideshow_description'] = ""; 0361 } 0362 0363 if (!empty($tags)) { 0364 $tmp = array(); 0365 foreach ($tags as $tag) { 0366 $tmp[] = "\"$tag\""; 0367 } 0368 $params['slideshow_tags'] = implode(' ', $tmp); 0369 } else { 0370 $params['slideshow_tags'] = ""; 0371 } 0372 0373 $client = $this->getHttpClient(); 0374 $client->setUri(self::SERVICE_UPLOAD_URI); 0375 $client->setParameterPost($params); 0376 $client->setFileUpload($filename, "slideshow_srcfile"); 0377 0378 // require_once 'Zend/Http/Client/Exception.php'; 0379 try { 0380 $response = $client->request('POST'); 0381 } catch (Zend_Http_Client_Exception $e) { 0382 // require_once 'Zend/Service/SlideShare/Exception.php'; 0383 throw new Zend_Service_SlideShare_Exception( 0384 "Service Request Failed: {$e->getMessage()}", 0, $e 0385 ); 0386 } 0387 0388 $sxe = Zend_Xml_Security::scan($response->getBody()); 0389 0390 if ($sxe->getName() == "SlideShareServiceError") { 0391 $message = (string)$sxe->Message[0]; 0392 list($code, $errorStr) = explode(':', $message); 0393 // require_once 'Zend/Service/SlideShare/Exception.php'; 0394 throw new Zend_Service_SlideShare_Exception( 0395 trim($errorStr), 0396 $code 0397 ); 0398 } 0399 0400 if (!$sxe->getName() == "SlideShowUploaded") { 0401 // require_once 'Zend/Service/SlideShare/Exception.php'; 0402 throw new Zend_Service_SlideShare_Exception( 0403 'Unknown XML Respons Received' 0404 ); 0405 } 0406 0407 $ss->setId((int)(string)$sxe->SlideShowID); 0408 0409 return $ss; 0410 } 0411 0412 /** 0413 * Retrieves a slide show's information based on slide show ID 0414 * 0415 * @param int $ssId The slide show ID 0416 * @return Zend_Service_SlideShare_SlideShow the Slideshow object 0417 * @throws Zend_Service_SlideShare_Exception 0418 */ 0419 public function getSlideShow($ssId) 0420 { 0421 $timestamp = time(); 0422 0423 $params = array( 0424 'api_key' => $this->getApiKey(), 0425 'ts' => $timestamp, 0426 'hash' => sha1($this->getSharedSecret() . $timestamp), 0427 'slideshow_id' => $ssId 0428 ); 0429 0430 $cache = $this->getCacheObject(); 0431 $cacheKey = md5("__zendslideshare_cache_$ssId"); 0432 0433 if (!$retval = $cache->load($cacheKey)) { 0434 $client = $this->getHttpClient(); 0435 0436 $client->setUri(self::SERVICE_GET_SHOW_URI); 0437 $client->setParameterPost($params); 0438 0439 // require_once 'Zend/Http/Client/Exception.php'; 0440 try { 0441 $response = $client->request('POST'); 0442 } catch (Zend_Http_Client_Exception $e) { 0443 // require_once 'Zend/Service/SlideShare/Exception.php'; 0444 throw new Zend_Service_SlideShare_Exception( 0445 "Service Request Failed: {$e->getMessage()}", 0, $e 0446 ); 0447 } 0448 0449 $sxe = Zend_Xml_Security::scan($response->getBody()); 0450 0451 if ($sxe->getName() == "SlideShareServiceError") { 0452 $message = (string)$sxe->Message[0]; 0453 list($code, $errorStr) = explode(':', $message); 0454 // require_once 'Zend/Service/SlideShare/Exception.php'; 0455 throw new Zend_Service_SlideShare_Exception( 0456 trim($errorStr), 0457 $code 0458 ); 0459 } 0460 0461 if (!($sxe->getName() == 'Slideshow')) { 0462 // require_once 'Zend/Service/SlideShare/Exception.php'; 0463 throw new Zend_Service_SlideShare_Exception( 0464 'Unknown XML Repsonse Received' 0465 ); 0466 } 0467 $retval = $this->_slideShowNodeToObject(clone $sxe); 0468 0469 $cache->save($retval, $cacheKey); 0470 } 0471 0472 return $retval; 0473 } 0474 0475 /** 0476 * Retrieves an array of slide shows for a given username 0477 * 0478 * @param string $username The username to retrieve slide shows from 0479 * @param int $offset The offset of the list to start retrieving from 0480 * @param int $limit The maximum number of slide shows to retrieve 0481 * @return array An array of Zend_Service_SlideShare_SlideShow objects 0482 */ 0483 public function getSlideShowsByUsername( 0484 $username, $offset = null, $limit = null 0485 ) 0486 { 0487 return $this->_getSlideShowsByType( 0488 'username_for', $username, $offset, $limit 0489 ); 0490 } 0491 0492 /** 0493 * Retrieves an array of slide shows based on tag 0494 * 0495 * @param string $tag The tag to retrieve slide shows with 0496 * @param int $offset The offset of the list to start retrieving from 0497 * @param int $limit The maximum number of slide shows to retrieve 0498 * @return array An array of Zend_Service_SlideShare_SlideShow objects 0499 */ 0500 public function getSlideShowsByTag($tag, $offset = null, $limit = null) 0501 { 0502 0503 if (is_array($tag)) { 0504 $tmp = array(); 0505 foreach ($tag as $t) { 0506 $tmp[] = "\"$t\""; 0507 } 0508 0509 $tag = implode(" ", $tmp); 0510 } 0511 0512 return $this->_getSlideShowsByType('tag', $tag, $offset, $limit); 0513 } 0514 0515 /** 0516 * Retrieves an array of slide shows based on group name 0517 * 0518 * @param string $group The group name to retrieve slide shows for 0519 * @param int $offset The offset of the list to start retrieving from 0520 * @param int $limit The maximum number of slide shows to retrieve 0521 * @return array An array of Zend_Service_SlideShare_SlideShow objects 0522 */ 0523 public function getSlideShowsByGroup($group, $offset = null, $limit = null) 0524 { 0525 return $this->_getSlideShowsByType('group_name', $group, $offset, $limit); 0526 } 0527 0528 /** 0529 * Retrieves Zend_Service_SlideShare_SlideShow object arrays based on the type of 0530 * list desired 0531 * 0532 * @param string $key The type of slide show object to retrieve 0533 * @param string $value The specific search query for the slide show type to look up 0534 * @param int $offset The offset of the list to start retrieving from 0535 * @param int $limit The maximum number of slide shows to retrieve 0536 * @return array An array of Zend_Service_SlideShare_SlideShow objects 0537 * @throws Zend_Service_SlideShare_Exception 0538 */ 0539 protected function _getSlideShowsByType( 0540 $key, $value, $offset = null, $limit = null 0541 ) 0542 { 0543 $key = strtolower($key); 0544 0545 switch ($key) { 0546 case 'username_for': 0547 $responseTag = 'User'; 0548 $queryUri = self::SERVICE_GET_SHOW_BY_USER_URI; 0549 break; 0550 case 'group_name': 0551 $responseTag = 'Group'; 0552 $queryUri = self::SERVICE_GET_SHOW_BY_GROUP_URI; 0553 break; 0554 case 'tag': 0555 $responseTag = 'Tag'; 0556 $queryUri = self::SERVICE_GET_SHOW_BY_TAG_URI; 0557 break; 0558 default: 0559 // require_once 'Zend/Service/SlideShare/Exception.php'; 0560 throw new Zend_Service_SlideShare_Exception( 0561 'Invalid SlideShare Query' 0562 ); 0563 } 0564 0565 $timestamp = time(); 0566 0567 $params = array( 0568 'api_key' => $this->getApiKey(), 0569 'ts' => $timestamp, 0570 'hash' => sha1($this->getSharedSecret() . $timestamp), 0571 $key => $value 0572 ); 0573 0574 if ($offset !== null) { 0575 $params['offset'] = (int)$offset; 0576 } 0577 0578 if ($limit !== null) { 0579 $params['limit'] = (int)$limit; 0580 } 0581 0582 $cache = $this->getCacheObject(); 0583 $cacheKey = md5($key . $value . $offset . $limit); 0584 0585 if (!$retval = $cache->load($cacheKey)) { 0586 $client = $this->getHttpClient(); 0587 0588 $client->setUri($queryUri); 0589 $client->setParameterPost($params); 0590 0591 // require_once 'Zend/Http/Client/Exception.php'; 0592 try { 0593 $response = $client->request('POST'); 0594 } catch (Zend_Http_Client_Exception $e) { 0595 // require_once 'Zend/Service/SlideShare/Exception.php'; 0596 throw new Zend_Service_SlideShare_Exception( 0597 "Service Request Failed: {$e->getMessage()}", 0, $e 0598 ); 0599 } 0600 0601 $sxe = Zend_Xml_Security::scan($response->getBody()); 0602 0603 if ($sxe->getName() == "SlideShareServiceError") { 0604 $message = (string)$sxe->Message[0]; 0605 list($code, $errorStr) = explode(':', $message); 0606 // require_once 'Zend/Service/SlideShare/Exception.php'; 0607 throw new Zend_Service_SlideShare_Exception( 0608 trim($errorStr), $code 0609 ); 0610 } 0611 0612 if (!$sxe->getName() == $responseTag) { 0613 // require_once 'Zend/Service/SlideShare/Exception.php'; 0614 throw new Zend_Service_SlideShare_Exception( 0615 'Unknown or Invalid XML Repsonse Received' 0616 ); 0617 } 0618 0619 $retval = array(); 0620 0621 foreach ($sxe->children() as $node) { 0622 if ($node->getName() == 'Slideshow') { 0623 $retval[] = $this->_slideShowNodeToObject($node); 0624 } 0625 } 0626 0627 $cache->save($retval, $cacheKey); 0628 } 0629 0630 return $retval; 0631 } 0632 0633 /** 0634 * Converts a SimpleXMLElement object representing a response from the service 0635 * into a Zend_Service_SlideShare_SlideShow object 0636 * 0637 * @see http://www.slideshare.net/developers/documentation#get_slideshow 0638 * 0639 * @param SimpleXMLElement $node The input XML from the slideshare.net service 0640 * @return Zend_Service_SlideShare_SlideShow The resulting object 0641 * @throws Zend_Service_SlideShare_Exception 0642 */ 0643 protected function _slideShowNodeToObject(SimpleXMLElement $node) 0644 { 0645 0646 if ($node->getName() == 'Slideshow') { 0647 $ss = new Zend_Service_SlideShare_SlideShow(); 0648 0649 $ss->setId((string)$node->ID); 0650 $ss->setDescription((string)$node->Description); 0651 $ss->setEmbedCode((string)$node->Embed); 0652 $ss->setNumViews((string)$node->Views); 0653 $ss->setUrl((string)$node->URL); 0654 $ss->setStatus((string)$node->Status); 0655 $ss->setStatusDescription((string)$node->StatusDescription); 0656 0657 foreach (explode(",", (string)$node->Tags) as $tag) { 0658 if (!in_array($tag, $ss->getTags())) { 0659 $ss->addTag($tag); 0660 } 0661 } 0662 0663 $ss->setThumbnailUrl((string)$node->ThumbnailURL); 0664 $ss->setTitle((string)$node->Title); 0665 $ss->setLocation((string)$node->Location); 0666 $ss->setTranscript((string)$node->Transcript); 0667 0668 return $ss; 0669 } 0670 0671 // require_once 'Zend/Service/SlideShare/Exception.php'; 0672 throw new Zend_Service_SlideShare_Exception( 0673 'Was not provided the expected XML Node for processing' 0674 ); 0675 } 0676 }