PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Service/SlideShare.php

https://github.com/ostric/e-learning
PHP | 609 lines | 294 code | 99 blank | 216 comment | 32 complexity | b7fd36959c6a131f59eee0d8b06c2a59 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Service
  17. * @subpackage SlideShare
  18. * @copyright Copyright (c) 2005-2008 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 9094 2008-03-30 18:36:55Z thomas $
  21. */
  22. /**
  23. * Zend_Http_Client
  24. */
  25. require_once 'Zend/Http/Client.php';
  26. /**
  27. * Zend_Cache
  28. */
  29. require_once 'Zend/Cache.php';
  30. /**
  31. * Zend_Service_SlideShare_Exception
  32. */
  33. require_once 'Zend/Service/SlideShare/Exception.php';
  34. /**
  35. * Zend_Service_SlideShare_SlideShow
  36. */
  37. require_once 'Zend/Service/SlideShare/SlideShow.php';
  38. /**
  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.
  42. *
  43. * @category Zend
  44. * @package Zend_Service
  45. * @subpackage SlideShare
  46. * @throws Zend_Service_SlideShare_Exception
  47. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  48. * @license http://framework.zend.com/license/new-bsd New BSD License
  49. */
  50. class Zend_Service_SlideShare
  51. {
  52. /**
  53. * Web service result code mapping
  54. */
  55. const SERVICE_ERROR_BAD_APIKEY = 1;
  56. const SERVICE_ERROR_BAD_AUTH = 2;
  57. const SERVICE_ERROR_MISSING_TITLE = 3;
  58. const SERVICE_ERROR_MISSING_FILE = 4;
  59. const SERVICE_ERROR_EMPTY_TITLE = 5;
  60. const SERVICE_ERROR_NOT_SOURCEOBJ = 6;
  61. const SERVICE_ERROR_INVALID_EXT = 7;
  62. const SERVICE_ERROR_FILE_TOO_BIG = 8;
  63. const SERVICE_ERROR_SHOW_NOT_FOUND = 9;
  64. const SERVICE_ERROR_USER_NOT_FOUND = 10;
  65. const SERVICE_ERROR_GROUP_NOT_FOUND = 11;
  66. const SERVICE_ERROR_MISSING_TAG = 12;
  67. const SERVICE_ERROR_DAILY_LIMIT = 99;
  68. const SERVICE_ERROR_ACCOUNT_BLOCKED = 100;
  69. /**
  70. * Slide share Web service communication URIs
  71. */
  72. const SERVICE_UPLOAD_URI = 'http://www.slideshare.net/api/1/upload_slideshow';
  73. const SERVICE_GET_SHOW_URI = 'http://www.slideshare.net/api/1/get_slideshow';
  74. const SERVICE_GET_SHOW_BY_USER_URI = 'http://www.slideshare.net/api/1/get_slideshow_by_user';
  75. const SERVICE_GET_SHOW_BY_TAG_URI = 'http://www.slideshare.net/api/1/get_slideshow_by_tag';
  76. const SERVICE_GET_SHOW_BY_GROUP_URI = 'http://www.slideshare.net/api/1/get_slideshows_from_group';
  77. /**
  78. * The MIME type of Slideshow files
  79. *
  80. */
  81. const POWERPOINT_MIME_TYPE = "application/vnd.ms-powerpoint";
  82. /**
  83. * The API key to use in requests
  84. *
  85. * @var string The API key
  86. */
  87. protected $_apiKey;
  88. /**
  89. * The shared secret to use in requests
  90. *
  91. * @var string the Shared secret
  92. */
  93. protected $_sharedSecret;
  94. /**
  95. * The username to use in requests
  96. *
  97. * @var string the username
  98. */
  99. protected $_username;
  100. /**
  101. * The password to use in requests
  102. *
  103. * @var string the password
  104. */
  105. protected $_password;
  106. /**
  107. * The HTTP Client object to use to perform requests
  108. *
  109. * @var Zend_Http_Client
  110. */
  111. protected $_httpclient;
  112. /**
  113. * The Cache object to use to perform caching
  114. *
  115. * @var Zend_Cache_Core
  116. */
  117. protected $_cacheobject;
  118. /**
  119. * Sets the Zend_Http_Client object to use in requests. If not provided a default will
  120. * be used.
  121. *
  122. * @param Zend_Http_Client $client The HTTP client instance to use
  123. * @return Zend_Service_SlideShare
  124. */
  125. public function setHttpClient(Zend_Http_Client $client)
  126. {
  127. $this->_httpclient = $client;
  128. return $this;
  129. }
  130. /**
  131. * Returns the instance of the Zend_Http_Client which will be used. Creates an instance
  132. * of Zend_Http_Client if no previous client was set.
  133. *
  134. * @return Zend_Http_Client The HTTP client which will be used
  135. */
  136. public function getHttpClient()
  137. {
  138. if(!($this->_httpclient instanceof Zend_Http_Client)) {
  139. $client = new Zend_Http_Client();
  140. $client->setConfig(array('maxredirects' => 2,
  141. 'timeout' => 5));
  142. $this->setHttpClient($client);
  143. }
  144. $this->_httpclient->resetParameters();
  145. return $this->_httpclient;
  146. }
  147. /**
  148. * Sets the Zend_Cache object to use to cache the results of API queries
  149. *
  150. * @param Zend_Cache_Core $cacheobject The Zend_Cache object used
  151. * @return Zend_Service_SlideShare
  152. */
  153. public function setCacheObject(Zend_Cache_Core $cacheobject)
  154. {
  155. $this->_cacheobject = $cacheobject;
  156. return $this;
  157. }
  158. /**
  159. * Gets the Zend_Cache object which will be used to cache API queries. If no cache object
  160. * was previously set the the default will be used (Filesystem caching in /tmp with a life
  161. * time of 43200 seconds)
  162. *
  163. * @return Zend_Cache_Core The object used in caching
  164. */
  165. public function getCacheObject()
  166. {
  167. if(!($this->_cacheobject instanceof Zend_Cache_Core)) {
  168. $cache = Zend_Cache::factory('Core', 'File', array('lifetime' => 43200,
  169. 'automatic_serialization' => true),
  170. array('cache_dir' => '/tmp'));
  171. $this->setCacheObject($cache);
  172. }
  173. return $this->_cacheobject;
  174. }
  175. /**
  176. * Returns the user name used for API calls
  177. *
  178. * @return string The username
  179. */
  180. public function getUserName()
  181. {
  182. return $this->_username;
  183. }
  184. /**
  185. * Sets the user name to use for API calls
  186. *
  187. * @param string $un The username to use
  188. * @return Zend_Service_SlideShare
  189. */
  190. public function setUserName($un)
  191. {
  192. $this->_username = $un;
  193. return $this;
  194. }
  195. /**
  196. * Gets the password to use in API calls
  197. *
  198. * @return string the password to use in API calls
  199. */
  200. public function getPassword()
  201. {
  202. return $this->_password;
  203. }
  204. /**
  205. * Sets the password to use in API calls
  206. *
  207. * @param string $pw The password to use
  208. * @return Zend_Service_SlideShare
  209. */
  210. public function setPassword($pw)
  211. {
  212. $this->_password = (string)$pw;
  213. return $this;
  214. }
  215. /**
  216. * Gets the API key to be used in making API calls
  217. *
  218. * @return string the API Key
  219. */
  220. public function getApiKey()
  221. {
  222. return $this->_apiKey;
  223. }
  224. /**
  225. * Sets the API key to be used in making API calls
  226. *
  227. * @param string $key The API key to use
  228. * @return Zend_Service_SlideShare
  229. */
  230. public function setApiKey($key)
  231. {
  232. $this->_apiKey = (string)$key;
  233. return $this;
  234. }
  235. /**
  236. * Gets the shared secret used in making API calls
  237. *
  238. * @return string the Shared secret
  239. */
  240. public function getSharedSecret()
  241. {
  242. return $this->_sharedSecret;
  243. }
  244. /**
  245. * Sets the shared secret used in making API calls
  246. *
  247. * @param string $secret the shared secret
  248. * @return Zend_Service_SlideShare
  249. */
  250. public function setSharedSecret($secret)
  251. {
  252. $this->_sharedSecret = (string)$secret;
  253. return $this;
  254. }
  255. /**
  256. * The Constructor
  257. *
  258. * @param string $apikey The API key
  259. * @param string $sharedSecret The shared secret
  260. * @param string $username The username
  261. * @param string $password The password
  262. */
  263. public function __construct($apikey, $sharedSecret, $username = null, $password = null)
  264. {
  265. $this->setApiKey($apikey)
  266. ->setSharedSecret($sharedSecret)
  267. ->setUserName($username)
  268. ->setPassword($password);
  269. $this->_httpclient = new Zend_Http_Client();
  270. }
  271. /**
  272. * Uploads the specified Slide show the the server
  273. *
  274. * @param Zend_Service_SlideShare_SlideShow $ss The slide show object representing the slide show to upload
  275. * @param boolean $make_src_public Determines if the the slide show's source file is public or not upon upload
  276. * @return Zend_Service_SlideShare_SlideShow The passed Slide show object, with the new assigned ID provided
  277. */
  278. public function uploadSlideShow(Zend_Service_SlideShare_SlideShow $ss, $make_src_public = true)
  279. {
  280. $timestamp = time();
  281. $params = array('api_key' => $this->getApiKey(),
  282. 'ts' => $timestamp,
  283. 'hash' => sha1($this->getSharedSecret().$timestamp),
  284. 'username' => $this->getUserName(),
  285. 'password' => $this->getPassword(),
  286. 'slideshow_title' => $ss->getTitle());
  287. $description = $ss->getDescription();
  288. $tags = $ss->getTags();
  289. $filename = $ss->getFilename();
  290. if(!file_exists($filename) || !is_readable($filename)) {
  291. throw new Zend_Service_SlideShare_Exception("Specified Slideshow for upload not found or unreadable");
  292. }
  293. if(!empty($description)) {
  294. $params['slideshow_description'] = $description;
  295. } else {
  296. $params['slideshow_description'] = "";
  297. }
  298. if(!empty($tags)) {
  299. $tmp = array();
  300. foreach($tags as $tag) {
  301. $tmp[] = "\"$tag\"";
  302. }
  303. $params['slideshow_tags'] = implode(' ', $tmp);
  304. } else {
  305. $params['slideshow_tags'] = "";
  306. }
  307. $client = $this->getHttpClient();
  308. $client->setUri(self::SERVICE_UPLOAD_URI);
  309. $client->setParameterPost($params);
  310. $client->setFileUpload($filename, "slideshow_srcfile");
  311. try {
  312. $response = $client->request('POST');
  313. } catch(Zend_Http_Client_Exception $e) {
  314. throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");
  315. }
  316. $sxe = simplexml_load_string($response->getBody());
  317. if($sxe->getName() == "SlideShareServiceError") {
  318. $message = (string)$sxe->Message[0];
  319. list($code, $error_str) = explode(':', $message);
  320. throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
  321. }
  322. if(!$sxe->getName() == "SlideShowUploaded") {
  323. throw new Zend_Service_SlideShare_Exception("Unknown XML Respons Received");
  324. }
  325. $ss->setId((int)(string)$sxe->SlideShowID);
  326. return $ss;
  327. }
  328. /**
  329. * Retrieves a slide show's information based on slide show ID
  330. *
  331. * @param int $ss_id The slide show ID
  332. * @return Zend_Service_SlideShare_SlideShow the Slideshow object
  333. */
  334. public function getSlideShow($ss_id)
  335. {
  336. $timestamp = time();
  337. $params = array('api_key' => $this->getApiKey(),
  338. 'ts' => $timestamp,
  339. 'hash' => sha1($this->getSharedSecret().$timestamp),
  340. 'slideshow_id' => $ss_id);
  341. $cache = $this->getCacheObject();
  342. $cache_key = md5("__zendslideshare_cache_$ss_id");
  343. if(!$retval = $cache->load($cache_key)) {
  344. $client = $this->getHttpClient();
  345. $client->setUri(self::SERVICE_GET_SHOW_URI);
  346. $client->setParameterPost($params);
  347. try {
  348. $response = $client->request('POST');
  349. } catch(Zend_Http_Client_Exception $e) {
  350. throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");
  351. }
  352. $sxe = simplexml_load_string($response->getBody());
  353. if($sxe->getName() == "SlideShareServiceError") {
  354. $message = (string)$sxe->Message[0];
  355. list($code, $error_str) = explode(':', $message);
  356. throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
  357. }
  358. if(!$sxe->getName() == 'Slideshows') {
  359. throw new Zend_Service_SlideShare_Exception('Unknown XML Repsonse Received');
  360. }
  361. $retval = $this->_slideShowNodeToObject(clone $sxe->Slideshow[0]);
  362. $cache->save($retval, $cache_key);
  363. }
  364. return $retval;
  365. }
  366. /**
  367. * Retrieves an array of slide shows for a given username
  368. *
  369. * @param string $username The username to retrieve slide shows from
  370. * @param int $offset The offset of the list to start retrieving from
  371. * @param int $limit The maximum number of slide shows to retrieve
  372. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  373. */
  374. public function getSlideShowsByUsername($username, $offset = null, $limit = null)
  375. {
  376. return $this->_getSlideShowsByType('username_for', $username, $offset, $limit);
  377. }
  378. /**
  379. * Retrieves an array of slide shows based on tag
  380. *
  381. * @param string $tag The tag to retrieve slide shows with
  382. * @param int $offset The offset of the list to start retrieving from
  383. * @param int $limit The maximum number of slide shows to retrieve
  384. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  385. */
  386. public function getSlideShowsByTag($tag, $offset = null, $limit = null)
  387. {
  388. if(is_array($tag)) {
  389. $tmp = array();
  390. foreach($tag as $t) {
  391. $tmp[] = "\"$t\"";
  392. }
  393. $tag = implode(" ", $tmp);
  394. }
  395. return $this->_getSlideShowsByType('tag', $tag, $offset, $limit);
  396. }
  397. /**
  398. * Retrieves an array of slide shows based on group name
  399. *
  400. * @param string $group The group name to retrieve slide shows for
  401. * @param int $offset The offset of the list to start retrieving from
  402. * @param int $limit The maximum number of slide shows to retrieve
  403. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  404. */
  405. public function getSlideShowsByGroup($group, $offset = null, $limit = null)
  406. {
  407. return $this->_getSlideShowsByType('group_name', $group, $offset, $limit);
  408. }
  409. /**
  410. * Retrieves Zend_Service_SlideShare_SlideShow object arrays based on the type of
  411. * list desired
  412. *
  413. * @param string $key The type of slide show object to retrieve
  414. * @param string $value The specific search query for the slide show type to look up
  415. * @param int $offset The offset of the list to start retrieving from
  416. * @param int $limit The maximum number of slide shows to retrieve
  417. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  418. */
  419. protected function _getSlideShowsByType($key, $value, $offset = null, $limit = null)
  420. {
  421. $key = strtolower($key);
  422. switch($key) {
  423. case 'username_for':
  424. $responseTag = 'User';
  425. $queryUri = self::SERVICE_GET_SHOW_BY_USER_URI;
  426. break;
  427. case 'group_name':
  428. $responseTag = 'Group';
  429. $queryUri = self::SERVICE_GET_SHOW_BY_GROUP_URI;
  430. break;
  431. case 'tag':
  432. $responseTag = 'Tag';
  433. $queryUri = self::SERVICE_GET_SHOW_BY_TAG_URI;
  434. break;
  435. default:
  436. throw new Zend_Service_SlideShare_Exception("Invalid SlideShare Query");
  437. }
  438. $timestamp = time();
  439. $params = array('api_key' => $this->getApiKey(),
  440. 'ts' => $timestamp,
  441. 'hash' => sha1($this->getSharedSecret().$timestamp),
  442. $key => $value);
  443. if(!is_null($offset)) {
  444. $params['offset'] = (int)$offset;
  445. }
  446. if(!is_null($limit)) {
  447. $params['limit'] = (int)$limit;
  448. }
  449. $cache = $this->getCacheObject();
  450. $cache_key = md5($key.$value.$offset.$limit);
  451. if(!$retval = $cache->load($cache_key)) {
  452. $client = $this->getHttpClient();
  453. $client->setUri($queryUri);
  454. $client->setParameterPost($params);
  455. try {
  456. $response = $client->request('POST');
  457. } catch(Zend_Http_Client_Exception $e) {
  458. throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}");
  459. }
  460. $sxe = simplexml_load_string($response->getBody());
  461. if($sxe->getName() == "SlideShareServiceError") {
  462. $message = (string)$sxe->Message[0];
  463. list($code, $error_str) = explode(':', $message);
  464. throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
  465. }
  466. if(!$sxe->getName() == $responseTag) {
  467. throw new Zend_Service_SlideShare_Exception('Unknown or Invalid XML Repsonse Received');
  468. }
  469. $retval = array();
  470. foreach($sxe->children() as $node) {
  471. if($node->getName() == 'Slideshow') {
  472. $retval[] = $this->_slideShowNodeToObject($node);
  473. }
  474. }
  475. $cache->save($retval, $cache_key);
  476. }
  477. return $retval;
  478. }
  479. /**
  480. * Converts a SimpleXMLElement object representing a response from the service
  481. * into a Zend_Service_SlideShare_SlideShow object
  482. *
  483. * @param SimpleXMLElement $node The input XML from the slideshare.net service
  484. * @return Zend_Service_SlideShare_SlideShow The resulting object
  485. */
  486. protected function _slideShowNodeToObject(SimpleXMLElement $node)
  487. {
  488. if($node->getName() == 'Slideshow') {
  489. $ss = new Zend_Service_SlideShare_SlideShow();
  490. $ss->setId((string)$node->ID);
  491. $ss->setDescription((string)$node->Description);
  492. $ss->setEmbedCode((string)$node->EmbedCode);
  493. $ss->setNumViews((string)$node->Views);
  494. $ss->setPermaLink((string)$node->Permalink);
  495. $ss->setStatus((string)$node->Status);
  496. $ss->setStatusDescription((string)$node->StatusDescription);
  497. foreach(explode(",", (string)$node->Tags) as $tag) {
  498. if(!in_array($tag, $ss->getTags())) {
  499. $ss->addTag($tag);
  500. }
  501. }
  502. $ss->setThumbnailUrl((string)$node->Thumbnail);
  503. $ss->setTitle((string)$node->Title);
  504. $ss->setLocation((string)$node->Location);
  505. $ss->setTranscript((string)$node->Transcript);
  506. return $ss;
  507. }
  508. throw new Zend_Service_SlideShare_Exception("Was not provided the expected XML Node for processing");
  509. }
  510. }