SlideShare.php 20 KB

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