SlideShare.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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-2015 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-2015 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
  118. * default will 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
  130. * an instance 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
  162. * cache object was previously set the the default will be used (Filesystem
  163. * caching in /tmp with a life 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
  285. * object representing the
  286. * slide show to upload
  287. * @param boolean $makeSrcPublic Determines if the slide
  288. * show's source file is public
  289. * or not upon upload
  290. * @return Zend_Service_SlideShare_SlideShow The passed Slide show object,
  291. * with the new assigned ID
  292. * provided
  293. * @throws Zend_Service_SlideShare_Exception
  294. */
  295. public function uploadSlideShow(
  296. Zend_Service_SlideShare_SlideShow $ss, $makeSrcPublic = true
  297. )
  298. {
  299. $timestamp = time();
  300. $params = array(
  301. 'api_key' => $this->getApiKey(),
  302. 'ts' => $timestamp,
  303. 'hash' => sha1($this->getSharedSecret() . $timestamp),
  304. 'username' => $this->getUserName(),
  305. 'password' => $this->getPassword(),
  306. 'slideshow_title' => $ss->getTitle()
  307. );
  308. $description = $ss->getDescription();
  309. $tags = $ss->getTags();
  310. $filename = $ss->getFilename();
  311. if (!file_exists($filename) || !is_readable($filename)) {
  312. require_once 'Zend/Service/SlideShare/Exception.php';
  313. throw new Zend_Service_SlideShare_Exception(
  314. 'Specified Slideshow for upload not found or unreadable'
  315. );
  316. }
  317. if (!empty($description)) {
  318. $params['slideshow_description'] = $description;
  319. } else {
  320. $params['slideshow_description'] = "";
  321. }
  322. if (!empty($tags)) {
  323. $tmp = array();
  324. foreach ($tags as $tag) {
  325. $tmp[] = "\"$tag\"";
  326. }
  327. $params['slideshow_tags'] = implode(' ', $tmp);
  328. } else {
  329. $params['slideshow_tags'] = "";
  330. }
  331. $client = $this->getHttpClient();
  332. $client->setUri(self::SERVICE_UPLOAD_URI);
  333. $client->setParameterPost($params);
  334. $client->setFileUpload($filename, "slideshow_srcfile");
  335. require_once 'Zend/Http/Client/Exception.php';
  336. try {
  337. $response = $client->request('POST');
  338. } catch (Zend_Http_Client_Exception $e) {
  339. require_once 'Zend/Service/SlideShare/Exception.php';
  340. throw new Zend_Service_SlideShare_Exception(
  341. "Service Request Failed: {$e->getMessage()}", 0, $e
  342. );
  343. }
  344. $sxe = Zend_Xml_Security::scan($response->getBody());
  345. if ($sxe->getName() == "SlideShareServiceError") {
  346. $message = (string)$sxe->Message[0];
  347. list($code, $errorStr) = explode(':', $message);
  348. require_once 'Zend/Service/SlideShare/Exception.php';
  349. throw new Zend_Service_SlideShare_Exception(
  350. trim($errorStr),
  351. $code
  352. );
  353. }
  354. if (!$sxe->getName() == "SlideShowUploaded") {
  355. require_once 'Zend/Service/SlideShare/Exception.php';
  356. throw new Zend_Service_SlideShare_Exception(
  357. 'Unknown XML Respons Received'
  358. );
  359. }
  360. $ss->setId((int)(string)$sxe->SlideShowID);
  361. return $ss;
  362. }
  363. /**
  364. * Retrieves a slide show's information based on slide show ID
  365. *
  366. * @param int $ssId The slide show ID
  367. * @return Zend_Service_SlideShare_SlideShow the Slideshow object
  368. * @throws Zend_Service_SlideShare_Exception
  369. */
  370. public function getSlideShow($ssId)
  371. {
  372. $timestamp = time();
  373. $params = array(
  374. 'api_key' => $this->getApiKey(),
  375. 'ts' => $timestamp,
  376. 'hash' => sha1($this->getSharedSecret() . $timestamp),
  377. 'slideshow_id' => $ssId
  378. );
  379. $cache = $this->getCacheObject();
  380. $cacheKey = md5("__zendslideshare_cache_$ssId");
  381. if (!$retval = $cache->load($cacheKey)) {
  382. $client = $this->getHttpClient();
  383. $client->setUri(self::SERVICE_GET_SHOW_URI);
  384. $client->setParameterPost($params);
  385. require_once 'Zend/Http/Client/Exception.php';
  386. try {
  387. $response = $client->request('POST');
  388. } catch (Zend_Http_Client_Exception $e) {
  389. require_once 'Zend/Service/SlideShare/Exception.php';
  390. throw new Zend_Service_SlideShare_Exception(
  391. "Service Request Failed: {$e->getMessage()}", 0, $e
  392. );
  393. }
  394. $sxe = Zend_Xml_Security::scan($response->getBody());
  395. if ($sxe->getName() == "SlideShareServiceError") {
  396. $message = (string)$sxe->Message[0];
  397. list($code, $errorStr) = explode(':', $message);
  398. require_once 'Zend/Service/SlideShare/Exception.php';
  399. throw new Zend_Service_SlideShare_Exception(
  400. trim($errorStr),
  401. $code
  402. );
  403. }
  404. if (!($sxe->getName() == 'Slideshow')) {
  405. require_once 'Zend/Service/SlideShare/Exception.php';
  406. throw new Zend_Service_SlideShare_Exception(
  407. 'Unknown XML Repsonse Received'
  408. );
  409. }
  410. $retval = $this->_slideShowNodeToObject(clone $sxe);
  411. $cache->save($retval, $cacheKey);
  412. }
  413. return $retval;
  414. }
  415. /**
  416. * Retrieves an array of slide shows for a given username
  417. *
  418. * @param string $username The username to retrieve slide shows from
  419. * @param int $offset The offset of the list to start retrieving from
  420. * @param int $limit The maximum number of slide shows to retrieve
  421. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  422. */
  423. public function getSlideShowsByUsername(
  424. $username, $offset = null, $limit = null
  425. )
  426. {
  427. return $this->_getSlideShowsByType(
  428. 'username_for', $username, $offset, $limit
  429. );
  430. }
  431. /**
  432. * Retrieves an array of slide shows based on tag
  433. *
  434. * @param string $tag The tag to retrieve slide shows with
  435. * @param int $offset The offset of the list to start retrieving from
  436. * @param int $limit The maximum number of slide shows to retrieve
  437. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  438. */
  439. public function getSlideShowsByTag($tag, $offset = null, $limit = null)
  440. {
  441. if (is_array($tag)) {
  442. $tmp = array();
  443. foreach ($tag as $t) {
  444. $tmp[] = "\"$t\"";
  445. }
  446. $tag = implode(" ", $tmp);
  447. }
  448. return $this->_getSlideShowsByType('tag', $tag, $offset, $limit);
  449. }
  450. /**
  451. * Retrieves an array of slide shows based on group name
  452. *
  453. * @param string $group The group name to retrieve slide shows for
  454. * @param int $offset The offset of the list to start retrieving from
  455. * @param int $limit The maximum number of slide shows to retrieve
  456. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  457. */
  458. public function getSlideShowsByGroup($group, $offset = null, $limit = null)
  459. {
  460. return $this->_getSlideShowsByType('group_name', $group, $offset, $limit);
  461. }
  462. /**
  463. * Retrieves Zend_Service_SlideShare_SlideShow object arrays based on the type of
  464. * list desired
  465. *
  466. * @param string $key The type of slide show object to retrieve
  467. * @param string $value The specific search query for the slide show type to look up
  468. * @param int $offset The offset of the list to start retrieving from
  469. * @param int $limit The maximum number of slide shows to retrieve
  470. * @return array An array of Zend_Service_SlideShare_SlideShow objects
  471. * @throws Zend_Service_SlideShare_Exception
  472. */
  473. protected function _getSlideShowsByType(
  474. $key, $value, $offset = null, $limit = null
  475. )
  476. {
  477. $key = strtolower($key);
  478. switch ($key) {
  479. case 'username_for':
  480. $responseTag = 'User';
  481. $queryUri = self::SERVICE_GET_SHOW_BY_USER_URI;
  482. break;
  483. case 'group_name':
  484. $responseTag = 'Group';
  485. $queryUri = self::SERVICE_GET_SHOW_BY_GROUP_URI;
  486. break;
  487. case 'tag':
  488. $responseTag = 'Tag';
  489. $queryUri = self::SERVICE_GET_SHOW_BY_TAG_URI;
  490. break;
  491. default:
  492. require_once 'Zend/Service/SlideShare/Exception.php';
  493. throw new Zend_Service_SlideShare_Exception(
  494. 'Invalid SlideShare Query'
  495. );
  496. }
  497. $timestamp = time();
  498. $params = array(
  499. 'api_key' => $this->getApiKey(),
  500. 'ts' => $timestamp,
  501. 'hash' => sha1($this->getSharedSecret() . $timestamp),
  502. $key => $value
  503. );
  504. if ($offset !== null) {
  505. $params['offset'] = (int)$offset;
  506. }
  507. if ($limit !== null) {
  508. $params['limit'] = (int)$limit;
  509. }
  510. $cache = $this->getCacheObject();
  511. $cacheKey = md5($key . $value . $offset . $limit);
  512. if (!$retval = $cache->load($cacheKey)) {
  513. $client = $this->getHttpClient();
  514. $client->setUri($queryUri);
  515. $client->setParameterPost($params);
  516. require_once 'Zend/Http/Client/Exception.php';
  517. try {
  518. $response = $client->request('POST');
  519. } catch (Zend_Http_Client_Exception $e) {
  520. require_once 'Zend/Service/SlideShare/Exception.php';
  521. throw new Zend_Service_SlideShare_Exception(
  522. "Service Request Failed: {$e->getMessage()}", 0, $e
  523. );
  524. }
  525. $sxe = Zend_Xml_Security::scan($response->getBody());
  526. if ($sxe->getName() == "SlideShareServiceError") {
  527. $message = (string)$sxe->Message[0];
  528. list($code, $errorStr) = explode(':', $message);
  529. require_once 'Zend/Service/SlideShare/Exception.php';
  530. throw new Zend_Service_SlideShare_Exception(
  531. trim($errorStr), $code
  532. );
  533. }
  534. if (!$sxe->getName() == $responseTag) {
  535. require_once 'Zend/Service/SlideShare/Exception.php';
  536. throw new Zend_Service_SlideShare_Exception(
  537. 'Unknown or Invalid XML Repsonse Received'
  538. );
  539. }
  540. $retval = array();
  541. foreach ($sxe->children() as $node) {
  542. if ($node->getName() == 'Slideshow') {
  543. $retval[] = $this->_slideShowNodeToObject($node);
  544. }
  545. }
  546. $cache->save($retval, $cacheKey);
  547. }
  548. return $retval;
  549. }
  550. /**
  551. * Converts a SimpleXMLElement object representing a response from the service
  552. * into a Zend_Service_SlideShare_SlideShow object
  553. *
  554. * @see http://www.slideshare.net/developers/documentation#get_slideshow
  555. *
  556. * @param SimpleXMLElement $node The input XML from the slideshare.net service
  557. * @return Zend_Service_SlideShare_SlideShow The resulting object
  558. * @throws Zend_Service_SlideShare_Exception
  559. */
  560. protected function _slideShowNodeToObject(SimpleXMLElement $node)
  561. {
  562. if ($node->getName() == 'Slideshow') {
  563. $ss = new Zend_Service_SlideShare_SlideShow();
  564. $ss->setId((string)$node->ID);
  565. $ss->setDescription((string)$node->Description);
  566. $ss->setEmbedCode((string)$node->Embed);
  567. $ss->setNumViews((string)$node->Views);
  568. $ss->setUrl((string)$node->URL);
  569. $ss->setStatus((string)$node->Status);
  570. $ss->setStatusDescription((string)$node->StatusDescription);
  571. foreach (explode(",", (string)$node->Tags) as $tag) {
  572. if (!in_array($tag, $ss->getTags())) {
  573. $ss->addTag($tag);
  574. }
  575. }
  576. $ss->setThumbnailUrl((string)$node->ThumbnailURL);
  577. $ss->setTitle((string)$node->Title);
  578. $ss->setLocation((string)$node->Location);
  579. $ss->setTranscript((string)$node->Transcript);
  580. return $ss;
  581. }
  582. require_once 'Zend/Service/SlideShare/Exception.php';
  583. throw new Zend_Service_SlideShare_Exception(
  584. 'Was not provided the expected XML Node for processing'
  585. );
  586. }
  587. }