PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Service/SlideShare.php

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