SlideShare.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2012 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 = 'https://www.slideshare.net/api/2/upload_slideshow';
  69. const SERVICE_GET_SHOW_URI = 'https://www.slideshare.net/api/2/get_slideshow';
  70. const SERVICE_GET_SHOW_BY_USER_URI = 'https://www.slideshare.net/api/2/get_slideshows_by_user';
  71. const SERVICE_GET_SHOW_BY_TAG_URI = 'https://www.slideshare.net/api/2/get_slideshows_by_tag';
  72. const SERVICE_GET_SHOW_BY_GROUP_URI = 'https://www.slideshare.net/api/2/get_slideshows_by_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(
  137. array(
  138. 'maxredirects' => 2,
  139. 'timeout' => 5
  140. )
  141. );
  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(
  169. 'Core',
  170. 'File',
  171. array(
  172. 'lifetime' => 43200,
  173. 'automatic_serialization' => true
  174. ),
  175. array('cache_dir' => '/tmp')
  176. );
  177. $this->setCacheObject($cache);
  178. }
  179. return $this->_cacheobject;
  180. }
  181. /**
  182. * Returns the user name used for API calls
  183. *
  184. * @return string The username
  185. */
  186. public function getUserName()
  187. {
  188. return $this->_username;
  189. }
  190. /**
  191. * Sets the user name to use for API calls
  192. *
  193. * @param string $un The username to use
  194. * @return Zend_Service_SlideShare
  195. */
  196. public function setUserName($un)
  197. {
  198. $this->_username = $un;
  199. return $this;
  200. }
  201. /**
  202. * Gets the password to use in API calls
  203. *
  204. * @return string the password to use in API calls
  205. */
  206. public function getPassword()
  207. {
  208. return $this->_password;
  209. }
  210. /**
  211. * Sets the password to use in API calls
  212. *
  213. * @param string $pw The password to use
  214. * @return Zend_Service_SlideShare
  215. */
  216. public function setPassword($pw)
  217. {
  218. $this->_password = (string)$pw;
  219. return $this;
  220. }
  221. /**
  222. * Gets the API key to be used in making API calls
  223. *
  224. * @return string the API Key
  225. */
  226. public function getApiKey()
  227. {
  228. return $this->_apiKey;
  229. }
  230. /**
  231. * Sets the API key to be used in making API calls
  232. *
  233. * @param string $key The API key to use
  234. * @return Zend_Service_SlideShare
  235. */
  236. public function setApiKey($key)
  237. {
  238. $this->_apiKey = (string)$key;
  239. return $this;
  240. }
  241. /**
  242. * Gets the shared secret used in making API calls
  243. *
  244. * @return string the Shared secret
  245. */
  246. public function getSharedSecret()
  247. {
  248. return $this->_sharedSecret;
  249. }
  250. /**
  251. * Sets the shared secret used in making API calls
  252. *
  253. * @param string $secret the shared secret
  254. * @return Zend_Service_SlideShare
  255. */
  256. public function setSharedSecret($secret)
  257. {
  258. $this->_sharedSecret = (string)$secret;
  259. return $this;
  260. }
  261. /**
  262. * The Constructor
  263. *
  264. * @param string $apikey The API key
  265. * @param string $sharedSecret The shared secret
  266. * @param string $username The username
  267. * @param string $password The password
  268. */
  269. public function __construct(
  270. $apikey, $sharedSecret, $username = null, $password = null
  271. )
  272. {
  273. $this->setApiKey($apikey)
  274. ->setSharedSecret($sharedSecret)
  275. ->setUserName($username)
  276. ->setPassword($password);
  277. $this->_httpclient = new Zend_Http_Client();
  278. }
  279. /**
  280. * Uploads the specified Slide show the the server
  281. *
  282. * @param Zend_Service_SlideShare_SlideShow $ss The slide show object representing the slide show to upload
  283. * @param boolean $makeSrcPublic Determines if the the slide show's source file is public or not upon upload
  284. * @return Zend_Service_SlideShare_SlideShow The passed Slide show object, with the new assigned ID provided
  285. * @throws Zend_Service_SlideShare_Exception
  286. */
  287. public function uploadSlideShow(
  288. Zend_Service_SlideShare_SlideShow $ss, $makeSrcPublic = true
  289. )
  290. {
  291. $timestamp = time();
  292. $params = array(
  293. 'api_key' => $this->getApiKey(),
  294. 'ts' => $timestamp,
  295. 'hash' => sha1($this->getSharedSecret() . $timestamp),
  296. 'username' => $this->getUserName(),
  297. 'password' => $this->getPassword(),
  298. 'slideshow_title' => $ss->getTitle()
  299. );
  300. $description = $ss->getDescription();
  301. $tags = $ss->getTags();
  302. $filename = $ss->getFilename();
  303. if (!file_exists($filename) || !is_readable($filename)) {
  304. require_once 'Zend/Service/SlideShare/Exception.php';
  305. throw new Zend_Service_SlideShare_Exception(
  306. 'Specified Slideshow for upload not found or unreadable'
  307. );
  308. }
  309. if (!empty($description)) {
  310. $params['slideshow_description'] = $description;
  311. } else {
  312. $params['slideshow_description'] = "";
  313. }
  314. if (!empty($tags)) {
  315. $tmp = array();
  316. foreach ($tags as $tag) {
  317. $tmp[] = "\"$tag\"";
  318. }
  319. $params['slideshow_tags'] = implode(' ', $tmp);
  320. } else {
  321. $params['slideshow_tags'] = "";
  322. }
  323. $client = $this->getHttpClient();
  324. $client->setUri(self::SERVICE_UPLOAD_URI);
  325. $client->setParameterPost($params);
  326. $client->setFileUpload($filename, "slideshow_srcfile");
  327. require_once 'Zend/Http/Client/Exception.php';
  328. try {
  329. $response = $client->request('POST');
  330. } catch (Zend_Http_Client_Exception $e) {
  331. require_once 'Zend/Service/SlideShare/Exception.php';
  332. throw new Zend_Service_SlideShare_Exception(
  333. "Service Request Failed: {$e->getMessage()}", 0, $e
  334. );
  335. }
  336. $sxe = simplexml_load_string($response->getBody());
  337. if ($sxe->getName() == "SlideShareServiceError") {
  338. $message = (string)$sxe->Message[0];
  339. list($code, $error_str) = explode(':', $message);
  340. require_once 'Zend/Service/SlideShare/Exception.php';
  341. throw new Zend_Service_SlideShare_Exception(trim(
  342. $error_str
  343. ), $code);
  344. }
  345. if (!$sxe->getName() == "SlideShowUploaded") {
  346. require_once 'Zend/Service/SlideShare/Exception.php';
  347. throw new Zend_Service_SlideShare_Exception(
  348. 'Unknown XML Respons Received'
  349. );
  350. }
  351. $ss->setId((int)(string)$sxe->SlideShowID);
  352. return $ss;
  353. }
  354. /**
  355. * Retrieves a slide show's information based on slide show ID
  356. *
  357. * @param int $ss_id The slide show ID
  358. * @return Zend_Service_SlideShare_SlideShow the Slideshow object
  359. * @throws Zend_Service_SlideShare_Exception
  360. */
  361. public function getSlideShow($ss_id)
  362. {
  363. $timestamp = time();
  364. $params = array(
  365. 'api_key' => $this->getApiKey(),
  366. 'ts' => $timestamp,
  367. 'hash' => sha1($this->getSharedSecret() . $timestamp),
  368. 'slideshow_id' => $ss_id
  369. );
  370. $cache = $this->getCacheObject();
  371. $cache_key = md5("__zendslideshare_cache_$ss_id");
  372. if (!$retval = $cache->load($cache_key)) {
  373. $client = $this->getHttpClient();
  374. $client->setUri(self::SERVICE_GET_SHOW_URI);
  375. $client->setParameterPost($params);
  376. require_once 'Zend/Http/Client/Exception.php';
  377. try {
  378. $response = $client->request('POST');
  379. } catch (Zend_Http_Client_Exception $e) {
  380. require_once 'Zend/Service/SlideShare/Exception.php';
  381. throw new Zend_Service_SlideShare_Exception(
  382. "Service Request Failed: {$e->getMessage()}", 0, $e
  383. );
  384. }
  385. $sxe = simplexml_load_string($response->getBody());
  386. if ($sxe->getName() == "SlideShareServiceError") {
  387. $message = (string)$sxe->Message[0];
  388. list($code, $error_str) = explode(':', $message);
  389. require_once 'Zend/Service/SlideShare/Exception.php';
  390. throw new Zend_Service_SlideShare_Exception(trim(
  391. $error_str
  392. ), $code);
  393. }
  394. if (!($sxe->getName() == 'Slideshow')) {
  395. require_once 'Zend/Service/SlideShare/Exception.php';
  396. throw new Zend_Service_SlideShare_Exception(
  397. 'Unknown XML Repsonse Received'
  398. );
  399. }
  400. $retval = $this->_slideShowNodeToObject(clone $sxe);
  401. $cache->save($retval, $cache_key);
  402. }
  403. return $retval;
  404. }
  405. /**
  406. * Retrieves an array of slide shows for a given username
  407. *
  408. * @param string $username The username to retrieve slide shows from
  409. * @param int $offset The offset of the list to start retrieving from
  410. * @param int $limit The maximum number of slide shows to retrieve
  411. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  412. */
  413. public function getSlideShowsByUsername(
  414. $username, $offset = null, $limit = null
  415. )
  416. {
  417. return $this->_getSlideShowsByType(
  418. 'username_for', $username, $offset, $limit
  419. );
  420. }
  421. /**
  422. * Retrieves an array of slide shows based on tag
  423. *
  424. * @param string $tag The tag to retrieve slide shows with
  425. * @param int $offset The offset of the list to start retrieving from
  426. * @param int $limit The maximum number of slide shows to retrieve
  427. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  428. */
  429. public function getSlideShowsByTag($tag, $offset = null, $limit = null)
  430. {
  431. if (is_array($tag)) {
  432. $tmp = array();
  433. foreach ($tag as $t) {
  434. $tmp[] = "\"$t\"";
  435. }
  436. $tag = implode(" ", $tmp);
  437. }
  438. return $this->_getSlideShowsByType('tag', $tag, $offset, $limit);
  439. }
  440. /**
  441. * Retrieves an array of slide shows based on group name
  442. *
  443. * @param string $group The group name to retrieve slide shows for
  444. * @param int $offset The offset of the list to start retrieving from
  445. * @param int $limit The maximum number of slide shows to retrieve
  446. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  447. */
  448. public function getSlideShowsByGroup($group, $offset = null, $limit = null)
  449. {
  450. return $this->_getSlideShowsByType('group_name', $group, $offset, $limit);
  451. }
  452. /**
  453. * Retrieves Zend_Service_SlideShare_SlideShow object arrays based on the type of
  454. * list desired
  455. *
  456. * @param string $key The type of slide show object to retrieve
  457. * @param string $value The specific search query for the slide show type to look up
  458. * @param int $offset The offset of the list to start retrieving from
  459. * @param int $limit The maximum number of slide shows to retrieve
  460. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  461. * @throws Zend_Service_SlideShare_Exception
  462. */
  463. protected function _getSlideShowsByType($key, $value, $offset = null, $limit = null)
  464. {
  465. $key = strtolower($key);
  466. switch ($key) {
  467. case 'username_for':
  468. $responseTag = 'User';
  469. $queryUri = self::SERVICE_GET_SHOW_BY_USER_URI;
  470. break;
  471. case 'group_name':
  472. $responseTag = 'Group';
  473. $queryUri = self::SERVICE_GET_SHOW_BY_GROUP_URI;
  474. break;
  475. case 'tag':
  476. $responseTag = 'Tag';
  477. $queryUri = self::SERVICE_GET_SHOW_BY_TAG_URI;
  478. break;
  479. default:
  480. require_once 'Zend/Service/SlideShare/Exception.php';
  481. throw new Zend_Service_SlideShare_Exception(
  482. 'Invalid SlideShare Query'
  483. );
  484. }
  485. $timestamp = time();
  486. $params = array('api_key' => $this->getApiKey(),
  487. 'ts' => $timestamp,
  488. 'hash' => sha1($this->getSharedSecret().$timestamp),
  489. $key => $value);
  490. if ($offset !== null) {
  491. $params['offset'] = (int)$offset;
  492. }
  493. if ($limit !== null) {
  494. $params['limit'] = (int)$limit;
  495. }
  496. $cache = $this->getCacheObject();
  497. $cache_key = md5($key.$value.$offset.$limit);
  498. if (!$retval = $cache->load($cache_key)) {
  499. $client = $this->getHttpClient();
  500. $client->setUri($queryUri);
  501. $client->setParameterPost($params);
  502. require_once 'Zend/Http/Client/Exception.php';
  503. try {
  504. $response = $client->request('POST');
  505. } catch (Zend_Http_Client_Exception $e) {
  506. require_once 'Zend/Service/SlideShare/Exception.php';
  507. throw new Zend_Service_SlideShare_Exception(
  508. "Service Request Failed: {$e->getMessage()}", 0, $e
  509. );
  510. }
  511. $sxe = simplexml_load_string($response->getBody());
  512. if ($sxe->getName() == "SlideShareServiceError") {
  513. $message = (string)$sxe->Message[0];
  514. list($code, $error_str) = explode(':', $message);
  515. require_once 'Zend/Service/SlideShare/Exception.php';
  516. throw new Zend_Service_SlideShare_Exception(
  517. trim($error_str), $code
  518. );
  519. }
  520. if (!$sxe->getName() == $responseTag) {
  521. require_once 'Zend/Service/SlideShare/Exception.php';
  522. throw new Zend_Service_SlideShare_Exception(
  523. 'Unknown or Invalid XML Repsonse Received'
  524. );
  525. }
  526. $retval = array();
  527. foreach ($sxe->children() as $node) {
  528. if ($node->getName() == 'Slideshow') {
  529. $retval[] = $this->_slideShowNodeToObject($node);
  530. }
  531. }
  532. $cache->save($retval, $cache_key);
  533. }
  534. return $retval;
  535. }
  536. /**
  537. * Converts a SimpleXMLElement object representing a response from the service
  538. * into a Zend_Service_SlideShare_SlideShow object
  539. *
  540. * @param SimpleXMLElement $node The input XML from the slideshare.net service
  541. * @return Zend_Service_SlideShare_SlideShow The resulting object
  542. * @throws Zend_Service_SlideShare_Exception
  543. */
  544. protected function _slideShowNodeToObject(SimpleXMLElement $node)
  545. {
  546. if($node->getName() == 'Slideshow') {
  547. $ss = new Zend_Service_SlideShare_SlideShow();
  548. $ss->setId((string)$node->ID);
  549. $ss->setDescription((string)$node->Description);
  550. $ss->setEmbedCode((string)$node->EmbedCode);
  551. $ss->setNumViews((string)$node->Views);
  552. $ss->setPermaLink((string)$node->Permalink);
  553. $ss->setStatus((string)$node->Status);
  554. $ss->setStatusDescription((string)$node->StatusDescription);
  555. foreach (explode(",", (string)$node->Tags) as $tag) {
  556. if (!in_array($tag, $ss->getTags())) {
  557. $ss->addTag($tag);
  558. }
  559. }
  560. $ss->setThumbnailUrl((string)$node->Thumbnail);
  561. $ss->setTitle((string)$node->Title);
  562. $ss->setLocation((string)$node->Location);
  563. $ss->setTranscript((string)$node->Transcript);
  564. return $ss;
  565. }
  566. require_once 'Zend/Service/SlideShare/Exception.php';
  567. throw new Zend_Service_SlideShare_Exception(
  568. 'Was not provided the expected XML Node for processing'
  569. );
  570. }
  571. }