|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Http/Client.php'; 00027 00031 require_once 'Zend/Cache.php'; 00032 00036 require_once 'Zend/Service/SlideShare/SlideShow.php'; 00037 00050 class Zend_Service_SlideShare 00051 { 00052 00056 const SERVICE_ERROR_BAD_APIKEY = 1; 00057 const SERVICE_ERROR_BAD_AUTH = 2; 00058 const SERVICE_ERROR_MISSING_TITLE = 3; 00059 const SERVICE_ERROR_MISSING_FILE = 4; 00060 const SERVICE_ERROR_EMPTY_TITLE = 5; 00061 const SERVICE_ERROR_NOT_SOURCEOBJ = 6; 00062 const SERVICE_ERROR_INVALID_EXT = 7; 00063 const SERVICE_ERROR_FILE_TOO_BIG = 8; 00064 const SERVICE_ERROR_SHOW_NOT_FOUND = 9; 00065 const SERVICE_ERROR_USER_NOT_FOUND = 10; 00066 const SERVICE_ERROR_GROUP_NOT_FOUND = 11; 00067 const SERVICE_ERROR_MISSING_TAG = 12; 00068 const SERVICE_ERROR_DAILY_LIMIT = 99; 00069 const SERVICE_ERROR_ACCOUNT_BLOCKED = 100; 00070 00074 const SERVICE_UPLOAD_URI = 'http://www.slideshare.net/api/1/upload_slideshow'; 00075 const SERVICE_GET_SHOW_URI = 'http://www.slideshare.net/api/1/get_slideshow'; 00076 const SERVICE_GET_SHOW_BY_USER_URI = 'http://www.slideshare.net/api/1/get_slideshow_by_user'; 00077 const SERVICE_GET_SHOW_BY_TAG_URI = 'http://www.slideshare.net/api/1/get_slideshow_by_tag'; 00078 const SERVICE_GET_SHOW_BY_GROUP_URI = 'http://www.slideshare.net/api/1/get_slideshows_from_group'; 00079 00084 const POWERPOINT_MIME_TYPE = "application/vnd.ms-powerpoint"; 00085 00091 protected $_apiKey; 00092 00098 protected $_sharedSecret; 00099 00105 protected $_username; 00106 00112 protected $_password; 00113 00119 protected $_httpclient; 00120 00126 protected $_cacheobject; 00127 00135 public function setHttpClient(Zend_Http_Client $client) 00136 { 00137 $this->_httpclient = $client; 00138 return $this; 00139 } 00140 00147 public function getHttpClient() 00148 { 00149 00150 if(!($this->_httpclient instanceof Zend_Http_Client)) { 00151 $client = new Zend_Http_Client(); 00152 $client->setConfig(array('maxredirects' => 2, 00153 'timeout' => 5)); 00154 00155 $this->setHttpClient($client); 00156 } 00157 00158 $this->_httpclient->resetParameters(); 00159 return $this->_httpclient; 00160 } 00161 00168 public function setCacheObject(Zend_Cache_Core $cacheobject) 00169 { 00170 $this->_cacheobject = $cacheobject; 00171 return $this; 00172 } 00173 00181 public function getCacheObject() 00182 { 00183 00184 if(!($this->_cacheobject instanceof Zend_Cache_Core)) { 00185 $cache = Zend_Cache::factory('Core', 'File', array('lifetime' => 43200, 00186 'automatic_serialization' => true), 00187 array('cache_dir' => '/tmp')); 00188 00189 $this->setCacheObject($cache); 00190 } 00191 00192 return $this->_cacheobject; 00193 } 00194 00200 public function getUserName() 00201 { 00202 return $this->_username; 00203 } 00204 00211 public function setUserName($un) 00212 { 00213 $this->_username = $un; 00214 return $this; 00215 } 00216 00222 public function getPassword() 00223 { 00224 return $this->_password; 00225 } 00226 00233 public function setPassword($pw) 00234 { 00235 $this->_password = (string)$pw; 00236 return $this; 00237 } 00238 00244 public function getApiKey() 00245 { 00246 return $this->_apiKey; 00247 } 00248 00255 public function setApiKey($key) 00256 { 00257 $this->_apiKey = (string)$key; 00258 return $this; 00259 } 00260 00266 public function getSharedSecret() 00267 { 00268 return $this->_sharedSecret; 00269 } 00270 00277 public function setSharedSecret($secret) 00278 { 00279 $this->_sharedSecret = (string)$secret; 00280 return $this; 00281 } 00282 00291 public function __construct($apikey, $sharedSecret, $username = null, $password = null) 00292 { 00293 $this->setApiKey($apikey) 00294 ->setSharedSecret($sharedSecret) 00295 ->setUserName($username) 00296 ->setPassword($password); 00297 00298 $this->_httpclient = new Zend_Http_Client(); 00299 } 00300 00308 public function uploadSlideShow(Zend_Service_SlideShare_SlideShow $ss, $make_src_public = true) 00309 { 00310 00311 $timestamp = time(); 00312 00313 $params = array('api_key' => $this->getApiKey(), 00314 'ts' => $timestamp, 00315 'hash' => sha1($this->getSharedSecret().$timestamp), 00316 'username' => $this->getUserName(), 00317 'password' => $this->getPassword(), 00318 'slideshow_title' => $ss->getTitle()); 00319 00320 $description = $ss->getDescription(); 00321 $tags = $ss->getTags(); 00322 00323 $filename = $ss->getFilename(); 00324 00325 if(!file_exists($filename) || !is_readable($filename)) { 00326 require_once 'Zend/Service/SlideShare/Exception.php'; 00327 throw new Zend_Service_SlideShare_Exception("Specified Slideshow for upload not found or unreadable"); 00328 } 00329 00330 if(!empty($description)) { 00331 $params['slideshow_description'] = $description; 00332 } else { 00333 $params['slideshow_description'] = ""; 00334 } 00335 00336 if(!empty($tags)) { 00337 $tmp = array(); 00338 foreach($tags as $tag) { 00339 $tmp[] = "\"$tag\""; 00340 } 00341 $params['slideshow_tags'] = implode(' ', $tmp); 00342 } else { 00343 $params['slideshow_tags'] = ""; 00344 } 00345 00346 00347 $client = $this->getHttpClient(); 00348 $client->setUri(self::SERVICE_UPLOAD_URI); 00349 $client->setParameterPost($params); 00350 $client->setFileUpload($filename, "slideshow_srcfile"); 00351 00352 require_once 'Zend/Http/Client/Exception.php'; 00353 try { 00354 $response = $client->request('POST'); 00355 } catch(Zend_Http_Client_Exception $e) { 00356 require_once 'Zend/Service/SlideShare/Exception.php'; 00357 throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}", 0, $e); 00358 } 00359 00360 $sxe = simplexml_load_string($response->getBody()); 00361 00362 if($sxe->getName() == "SlideShareServiceError") { 00363 $message = (string)$sxe->Message[0]; 00364 list($code, $error_str) = explode(':', $message); 00365 require_once 'Zend/Service/SlideShare/Exception.php'; 00366 throw new Zend_Service_SlideShare_Exception(trim($error_str), $code); 00367 } 00368 00369 if(!$sxe->getName() == "SlideShowUploaded") { 00370 require_once 'Zend/Service/SlideShare/Exception.php'; 00371 throw new Zend_Service_SlideShare_Exception("Unknown XML Respons Received"); 00372 } 00373 00374 $ss->setId((int)(string)$sxe->SlideShowID); 00375 00376 return $ss; 00377 } 00378 00385 public function getSlideShow($ss_id) 00386 { 00387 $timestamp = time(); 00388 00389 $params = array('api_key' => $this->getApiKey(), 00390 'ts' => $timestamp, 00391 'hash' => sha1($this->getSharedSecret().$timestamp), 00392 'slideshow_id' => $ss_id); 00393 00394 $cache = $this->getCacheObject(); 00395 00396 $cache_key = md5("__zendslideshare_cache_$ss_id"); 00397 00398 if(!$retval = $cache->load($cache_key)) { 00399 $client = $this->getHttpClient(); 00400 00401 $client->setUri(self::SERVICE_GET_SHOW_URI); 00402 $client->setParameterPost($params); 00403 00404 require_once 'Zend/Http/Client/Exception.php'; 00405 try { 00406 $response = $client->request('POST'); 00407 } catch(Zend_Http_Client_Exception $e) { 00408 require_once 'Zend/Service/SlideShare/Exception.php'; 00409 throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}", 0, $e); 00410 } 00411 00412 $sxe = simplexml_load_string($response->getBody()); 00413 00414 if($sxe->getName() == "SlideShareServiceError") { 00415 $message = (string)$sxe->Message[0]; 00416 list($code, $error_str) = explode(':', $message); 00417 require_once 'Zend/Service/SlideShare/Exception.php'; 00418 throw new Zend_Service_SlideShare_Exception(trim($error_str), $code); 00419 } 00420 00421 if(!$sxe->getName() == 'Slideshows') { 00422 require_once 'Zend/Service/SlideShare/Exception.php'; 00423 throw new Zend_Service_SlideShare_Exception('Unknown XML Repsonse Received'); 00424 } 00425 00426 $retval = $this->_slideShowNodeToObject(clone $sxe->Slideshow[0]); 00427 00428 $cache->save($retval, $cache_key); 00429 } 00430 00431 return $retval; 00432 } 00433 00442 public function getSlideShowsByUsername($username, $offset = null, $limit = null) 00443 { 00444 return $this->_getSlideShowsByType('username_for', $username, $offset, $limit); 00445 } 00446 00455 public function getSlideShowsByTag($tag, $offset = null, $limit = null) 00456 { 00457 00458 if(is_array($tag)) { 00459 $tmp = array(); 00460 foreach($tag as $t) { 00461 $tmp[] = "\"$t\""; 00462 } 00463 00464 $tag = implode(" ", $tmp); 00465 } 00466 00467 return $this->_getSlideShowsByType('tag', $tag, $offset, $limit); 00468 } 00469 00478 public function getSlideShowsByGroup($group, $offset = null, $limit = null) 00479 { 00480 return $this->_getSlideShowsByType('group_name', $group, $offset, $limit); 00481 } 00482 00493 protected function _getSlideShowsByType($key, $value, $offset = null, $limit = null) 00494 { 00495 00496 $key = strtolower($key); 00497 00498 switch($key) { 00499 case 'username_for': 00500 $responseTag = 'User'; 00501 $queryUri = self::SERVICE_GET_SHOW_BY_USER_URI; 00502 break; 00503 case 'group_name': 00504 $responseTag = 'Group'; 00505 $queryUri = self::SERVICE_GET_SHOW_BY_GROUP_URI; 00506 break; 00507 case 'tag': 00508 $responseTag = 'Tag'; 00509 $queryUri = self::SERVICE_GET_SHOW_BY_TAG_URI; 00510 break; 00511 default: 00512 require_once 'Zend/Service/SlideShare/Exception.php'; 00513 throw new Zend_Service_SlideShare_Exception("Invalid SlideShare Query"); 00514 } 00515 00516 $timestamp = time(); 00517 00518 $params = array('api_key' => $this->getApiKey(), 00519 'ts' => $timestamp, 00520 'hash' => sha1($this->getSharedSecret().$timestamp), 00521 $key => $value); 00522 00523 if($offset !== null) { 00524 $params['offset'] = (int)$offset; 00525 } 00526 00527 if($limit !== null) { 00528 $params['limit'] = (int)$limit; 00529 } 00530 00531 $cache = $this->getCacheObject(); 00532 00533 $cache_key = md5($key.$value.$offset.$limit); 00534 00535 if(!$retval = $cache->load($cache_key)) { 00536 00537 $client = $this->getHttpClient(); 00538 00539 $client->setUri($queryUri); 00540 $client->setParameterPost($params); 00541 00542 require_once 'Zend/Http/Client/Exception.php'; 00543 try { 00544 $response = $client->request('POST'); 00545 } catch(Zend_Http_Client_Exception $e) { 00546 require_once 'Zend/Service/SlideShare/Exception.php'; 00547 throw new Zend_Service_SlideShare_Exception("Service Request Failed: {$e->getMessage()}", 0, $e); 00548 } 00549 00550 $sxe = simplexml_load_string($response->getBody()); 00551 00552 if($sxe->getName() == "SlideShareServiceError") { 00553 $message = (string)$sxe->Message[0]; 00554 list($code, $error_str) = explode(':', $message); 00555 require_once 'Zend/Service/SlideShare/Exception.php'; 00556 throw new Zend_Service_SlideShare_Exception(trim($error_str), $code); 00557 } 00558 00559 if(!$sxe->getName() == $responseTag) { 00560 require_once 'Zend/Service/SlideShare/Exception.php'; 00561 throw new Zend_Service_SlideShare_Exception('Unknown or Invalid XML Repsonse Received'); 00562 } 00563 00564 $retval = array(); 00565 00566 foreach($sxe->children() as $node) { 00567 if($node->getName() == 'Slideshow') { 00568 $retval[] = $this->_slideShowNodeToObject($node); 00569 } 00570 } 00571 00572 $cache->save($retval, $cache_key); 00573 } 00574 00575 return $retval; 00576 } 00577 00585 protected function _slideShowNodeToObject(SimpleXMLElement $node) 00586 { 00587 00588 if($node->getName() == 'Slideshow') { 00589 00590 $ss = new Zend_Service_SlideShare_SlideShow(); 00591 00592 $ss->setId((string)$node->ID); 00593 $ss->setDescription((string)$node->Description); 00594 $ss->setEmbedCode((string)$node->EmbedCode); 00595 $ss->setNumViews((string)$node->Views); 00596 $ss->setPermaLink((string)$node->Permalink); 00597 $ss->setStatus((string)$node->Status); 00598 $ss->setStatusDescription((string)$node->StatusDescription); 00599 00600 foreach(explode(",", (string)$node->Tags) as $tag) { 00601 00602 if(!in_array($tag, $ss->getTags())) { 00603 $ss->addTag($tag); 00604 } 00605 } 00606 00607 $ss->setThumbnailUrl((string)$node->Thumbnail); 00608 $ss->setTitle((string)$node->Title); 00609 $ss->setLocation((string)$node->Location); 00610 $ss->setTranscript((string)$node->Transcript); 00611 00612 return $ss; 00613 00614 } 00615 00616 require_once 'Zend/Service/SlideShare/Exception.php'; 00617 throw new Zend_Service_SlideShare_Exception("Was not provided the expected XML Node for processing"); 00618 } 00619 }