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

/www/system/library/Zend/Service/SlideShare.php

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