Advertisement
Guest User

Untitled

a guest
Jun 14th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.63 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * The Zend_Service_SlideShare component is used to interface with the
  5.  * slideshare.net web server to retrieve slide shows hosted on the web site for
  6.  * display or other processing.
  7.  *
  8.  * @category   Zend
  9.  * @package    Zend_Service
  10.  * @subpackage SlideShare
  11.  * @throws     Zend_Service_SlideShare_Exception
  12.  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  13.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  14.  */
  15. class Zend_Service_SlideShare
  16. {
  17.     /**
  18.      * The Constructor
  19.      *
  20.      * @param string $apikey The API key
  21.      * @param string $sharedSecret The shared secret
  22.      * @param string $username The username
  23.      * @param string $password The password
  24.      */
  25.     public function __construct($apikey, $sharedSecret, $username = null, $password = null)
  26.     {
  27.         $this->setApiKey($apikey)
  28.              ->setSharedSecret($sharedSecret)
  29.              ->setUserName($username)
  30.              ->setPassword($password);
  31.  
  32.         $this->_httpclient = new Zend_Http_Client();
  33.     }
  34.  
  35.     ...
  36.  
  37.     /**
  38.      * Uploads the specified Slide show the the server
  39.      *
  40.      * @param Zend_Service_SlideShare_SlideShow $ss The slide show object representing the slide show to upload
  41.      * @param boolean $make_src_public Determines if the the slide show's source file is public or not upon upload
  42.      * @return Zend_Service_SlideShare_SlideShow The passed Slide show object, with the new assigned ID provided
  43.      */
  44.     public function uploadSlideShow(Zend_Service_SlideShare_SlideShow $ss, $make_src_public = true)
  45.     {
  46.  
  47.         $timestamp = time();
  48.  
  49.         $params = array('api_key' => $this->getApiKey(),
  50.                         'ts' => $timestamp,
  51.                         'hash' => sha1($this->getSharedSecret().$timestamp),
  52.                         'username' => $this->getUserName(),
  53.                         'password' => $this->getPassword(),
  54.                         'slideshow_title' => $ss->getTitle());
  55.  
  56.         $description = $ss->getDescription();
  57.         $tags = $ss->getTags();
  58.  
  59.         $filename = $ss->getFilename();
  60.  
  61.         if(!file_exists($filename) || !is_readable($filename)) {
  62.             require_once 'Zend/Service/SlideShare/Exception.php';
  63.             throw new Zend_Service_SlideShare_Exception("Specified Slideshow for upload not found or unreadable");
  64.         }
  65.  
  66.         if(!empty($description)) {
  67.             $params['slideshow_description'] = $description;
  68.         } else {
  69.             $params['slideshow_description'] = "";
  70.         }
  71.  
  72.         if(!empty($tags)) {
  73.             $tmp = array();
  74.             foreach($tags as $tag) {
  75.                 $tmp[] = "\"$tag\"";
  76.             }
  77.             $params['slideshow_tags'] = implode(' ', $tmp);
  78.         } else {
  79.             $params['slideshow_tags'] = "";
  80.         }
  81.  
  82.  
  83.         $client = $this->getHttpClient();
  84.         $client->setUri(self::SERVICE_UPLOAD_URI);
  85.         $client->setParameterPost($params);
  86.         $client->setFileUpload($filename, "slideshow_srcfile");
  87.  
  88.         require_once 'Zend/Http/Client/Exception.php';
  89.         try {
  90.             $response = $client->request('POST');
  91.         } catch(Zend_Http_Client_Exception $e) {
  92.             require_once 'Zend/Service/SlideShare/Exception.php';
  93.             throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}", 0, $e);
  94.         }
  95.  
  96.         $sxe = simplexml_load_string($response->getBody());
  97.  
  98.         if($sxe->getName() == "SlideShareServiceError") {
  99.             $message = (string)$sxe->Message[0];
  100.             list($code, $error_str) = explode(':', $message);
  101.             require_once 'Zend/Service/SlideShare/Exception.php';
  102.             throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
  103.         }
  104.  
  105.         if(!$sxe->getName() == "SlideShowUploaded") {
  106.             require_once 'Zend/Service/SlideShare/Exception.php';
  107.             throw new Zend_Service_SlideShare_Exception("Unknown XML Respons Received");
  108.         }
  109.  
  110.         $ss->setId((int)(string)$sxe->SlideShowID);
  111.  
  112.         return $ss;
  113.     }
  114.  
  115.     /**
  116.      * Retrieves a slide show's information based on slide show ID
  117.      *
  118.      * @param int $ss_id The slide show ID
  119.      * @return Zend_Service_SlideShare_SlideShow the Slideshow object
  120.      */
  121.     public function getSlideShow($ss_id)
  122.     {
  123.         $timestamp = time();
  124.  
  125.         $params = array('api_key' => $this->getApiKey(),
  126.                         'ts' => $timestamp,
  127.                         'hash' => sha1($this->getSharedSecret().$timestamp),
  128.                         'slideshow_id' => $ss_id);
  129.  
  130.         $cache = $this->getCacheObject();
  131.  
  132.         $cache_key = md5("__zendslideshare_cache_$ss_id");
  133.  
  134.         if(!$retval = $cache->load($cache_key)) {
  135.             $client = $this->getHttpClient();
  136.  
  137.             $client->setUri(self::SERVICE_GET_SHOW_URI);
  138.             $client->setParameterPost($params);
  139.  
  140.             require_once 'Zend/Http/Client/Exception.php';
  141.             try {
  142.                 $response = $client->request('POST');
  143.             } catch(Zend_Http_Client_Exception $e) {
  144.                 require_once 'Zend/Service/SlideShare/Exception.php';
  145.                 throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}", 0, $e);
  146.             }
  147.  
  148.             $sxe = simplexml_load_string($response->getBody());
  149.  
  150.             if($sxe->getName() == "SlideShareServiceError") {
  151.                 $message = (string)$sxe->Message[0];
  152.                 list($code, $error_str) = explode(':', $message);
  153.                 require_once 'Zend/Service/SlideShare/Exception.php';
  154.                 throw new Zend_Service_SlideShare_Exception(trim($error_str), $code);
  155.             }
  156.  
  157.             if(!$sxe->getName() == 'Slideshows') {
  158.                 require_once 'Zend/Service/SlideShare/Exception.php';
  159.                 throw new Zend_Service_SlideShare_Exception('Unknown XML Repsonse Received');
  160.             }
  161.  
  162.             $retval = $this->_slideShowNodeToObject(clone $sxe->Slideshow[0]);
  163.  
  164.             $cache->save($retval, $cache_key);
  165.         }
  166.  
  167.         return $retval;
  168.     }
  169.  
  170.     /**
  171.      * Converts a SimpleXMLElement object representing a response from the service
  172.      * into a Zend_Service_SlideShare_SlideShow object
  173.      *
  174.      * @param SimpleXMLElement $node The input XML from the slideshare.net service
  175.      * @return Zend_Service_SlideShare_SlideShow The resulting object
  176.      */
  177.     protected function _slideShowNodeToObject(SimpleXMLElement $node)
  178.     {
  179.  
  180.         if($node->getName() == 'Slideshow') {
  181.  
  182.             $ss = new Zend_Service_SlideShare_SlideShow();
  183.  
  184.             $ss->setId((string)$node->ID);
  185.             $ss->setDescription((string)$node->Description);
  186.             $ss->setEmbedCode((string)$node->EmbedCode);
  187.             $ss->setNumViews((string)$node->Views);
  188.             $ss->setPermaLink((string)$node->Permalink);
  189.             $ss->setStatus((string)$node->Status);
  190.             $ss->setStatusDescription((string)$node->StatusDescription);
  191.  
  192.             foreach(explode(",", (string)$node->Tags) as $tag) {
  193.  
  194.                 if(!in_array($tag, $ss->getTags())) {
  195.                     $ss->addTag($tag);
  196.                 }
  197.             }
  198.  
  199.             $ss->setThumbnailUrl((string)$node->Thumbnail);
  200.             $ss->setTitle((string)$node->Title);
  201.             $ss->setLocation((string)$node->Location);
  202.             $ss->setTranscript((string)$node->Transcript);
  203.  
  204.             return $ss;
  205.  
  206.         }
  207.  
  208.         require_once 'Zend/Service/SlideShare/Exception.php';
  209.         throw new Zend_Service_SlideShare_Exception("Was not provided the expected XML Node for processing");
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement