[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Service/ -> SlideShare.php (source)

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1