PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/plugins/viddlersilo/viddlersilo.plugin.php

https://github.com/HabariMag/habarimag-old
PHP | 1045 lines | 563 code | 125 blank | 357 comment | 89 complexity | 923ae6ecc9da3e459d152a83bf0c5c3d MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /*
  3. #######################################################
  4. # Viddler API / PHP Wrapper
  5. # By: Colin Devroe | cdevroe@viddler.com
  6. #
  7. # Docs: http://wiki.developers.viddler.com/index.php/Phpviddler
  8. #
  9. # License(s): Licensed under:
  10. # MIT (MIT-LICENSE.txt)
  11. #
  12. # Third-party code:
  13. # XML Library by Keith Devens
  14. # xmlparser.php
  15. #
  16. # Version 0.3
  17. ########################################################
  18. */
  19. class Phpviddler {
  20. var $apiKey = '01172c643b9743485249534a4441564953151';
  21. var $viddlerREST = 'http://api.viddler.com/rest/v1/'; // REST URL Version 1.0
  22. var $xml; // Raw XML returned by API
  23. var $response; // Array of results
  24. var $parser = false; // Use the included XML parser? Default: true.
  25. // Optional
  26. var $uploaddir = false; // Used for temporary upload directory.
  27. /*########## User functions ########### */
  28. /* viddler.users.auth
  29. / accepts: $userInfo = array
  30. / returns: array - sessionid (if not asking for record_token)
  31. / returns: array - sessionid and recordtoken (if asking for record token)
  32. / doc: http://wiki.developers.viddler.com/index.php/Viddler.users.auth
  33. */
  34. function user_authenticate($userInfo) {
  35. $args = '';
  36. $args = $this->buildArguments($userInfo); // Arguments as string
  37. //var_dump( $args ); exit;
  38. $xml = $this->sendRequest('viddler.users.auth',$args); // Get XML response
  39. // Use included parser?
  40. if ($this->parser) {
  41. // Several steps
  42. // 1. Convert XML into an array
  43. // 2. Check for errors
  44. // i. Return array of error number, short, and long description
  45. // 3. Return multidimensional array of response.
  46. $response = $this->checkErrors(XML_unserialize($xml));
  47. if ($response['error']) {
  48. return $response;
  49. } else {
  50. // Return record token too? Or just session id?
  51. if (!$args['record_token']) {
  52. return $response['auth']['sessionid'];
  53. } else {
  54. return $response['auth'];
  55. }
  56. }
  57. } else {
  58. return $xml;
  59. }
  60. return false;
  61. }
  62. /* viddler.users.getProfile
  63. / accepts: $username = string
  64. / returns: array - user's profile
  65. / doc: http://wiki.developers.viddler.com/index.php/Viddler.users.getProfile
  66. */
  67. function user_profile($username) {
  68. $xml = $this->sendRequest('viddler.users.getProfile','user='.$username); // Get XML response
  69. // Use included parser?
  70. if ($this->parser) {
  71. // Several steps
  72. // 1. Convert XML into an array
  73. // 2. Check for errors
  74. // i. Return array of error number, short, and long description
  75. // 3. Return multidimensional array of response.
  76. $response = $this->checkErrors(XML_unserialize($xml));
  77. if ($response['error']) {
  78. return $response;
  79. } else {
  80. return $response['user'];
  81. }
  82. } else {
  83. return $xml;
  84. }
  85. return false;
  86. }
  87. /* viddler.users.setOptions
  88. / accepts: $options = array
  89. / returns: string - number of options updated
  90. / doc: http://wiki.developers.viddler.com/index.php/Viddler.users.setOptions
  91. */
  92. function user_setoptions($options) {
  93. $args = '';
  94. $args = $this->buildArguments($options); // Arguments as string
  95. $xml = $this->sendRequest('viddler.users.setOptions',$args); // Get XML response
  96. // Use included parser?
  97. if ($this->parser) {
  98. // Several steps
  99. // 1. Convert XML into an array
  100. // 2. Check for errors
  101. // i. Return array of error number, short, and long description
  102. // 3. Return multidimensional array of response.
  103. $response = $this->checkErrors(XML_unserialize($xml));
  104. if ($response['error']) {
  105. return $response;
  106. } else {
  107. return $response['user'];
  108. }
  109. } else {
  110. return $xml;
  111. }
  112. return false;
  113. }
  114. /*########## Video functions ########### */
  115. /* viddler.videos.upload
  116. / requires: POST HTTP Method
  117. / accepts: $videoInfo = array
  118. / returns: array - info about video uploaded
  119. doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.upload
  120. */
  121. function video_upload($videoInfo) {
  122. $args = '';
  123. $args = $this->buildArguments($videoInfo,'array'); // Arguments as array
  124. $xml = $this->sendRequest('viddler.videos.upload',$args); // Get XML response
  125. // Use included parser?
  126. if ($this->parser) {
  127. // Several steps
  128. // 1. Convert XML into an array
  129. // 2. Check for errors
  130. // i. Return array of error number, short, and long description
  131. // 3. Return multidimensional array of response.
  132. $response = $this->checkErrors(XML_unserialize($xml));
  133. // Deletes the uploaded file from the server
  134. //if ($this->uploaddir) {
  135. // unlink($this->uploaddir.$_FILES['file']['name']);
  136. //}
  137. if ($response['error']) {
  138. return $response;
  139. } else {
  140. return $response['video'];
  141. }
  142. } else {
  143. return $xml;
  144. }
  145. return false;
  146. }
  147. /* viddler.videos.getRecordToken
  148. / accepts: $sessionid = string
  149. / returns: string - token needed for recording with webcam
  150. }
  151. / doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getRecordToken
  152. / Instructions for use: http://wiki.developers.viddler.com/index.php/Record_With_Webcam_API
  153. */
  154. function video_getrecordtoken($sessionid) {
  155. $xml = $this->sendRequest('viddler.videos.getRecordToken','sessionid='.$sessionid); // Get XML response
  156. // Use included parser?
  157. if ($this->parser) {
  158. // Several steps
  159. // 1. Convert XML into an array
  160. // 2. Check for errors
  161. // i. Return array of error number, short, and long description
  162. // 3. Return multidimensional array of response.
  163. $response = $this->checkErrors(XML_unserialize($xml));
  164. if ($response['error']) {
  165. return $response;
  166. } else {
  167. return $response['record_token'];
  168. }
  169. } else {
  170. return $xml;
  171. }
  172. return false;
  173. }
  174. /* Not a method...
  175. / accepts: $token = string
  176. / returns: string - html for recorder embed with token included.
  177. }
  178. / doc: None.
  179. */
  180. function video_getrecordembed($token) {
  181. if (!$token) return false;
  182. $html = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="449" height="400" id="viddler_recorder" align="middle">
  183. <param name="allowScriptAccess" value="always" />
  184. <param name="allowNetworking" value="all" />
  185. <param name="movie" value="http://www.viddler.com/flash/recorder.swf" />
  186. <param name="quality" value="high" />
  187. <param name="scale" value="noScale">
  188. <param name="bgcolor" value="#000000" />
  189. <param name="flashvars" value="fake=1&recordToken='.$token.'" />
  190. <embed src="http://www.viddler.com/flash/recorder.swf" quality="high" scale="noScale" bgcolor="#000000" allowScriptAccess="always" allowNetworking="all" width="449" height="400" name="viddler_recorder" flashvars="fake=1&recordToken='.$token.'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
  191. </object>';
  192. return $html;
  193. }
  194. /* viddler.videos.getStatus
  195. / accepts: $videoID = string
  196. / returns: array - info about video's upload status
  197. / Method specific responses {
  198. 1: Waiting in encode queue (failed=0)
  199. 2: Encoding (failed=0)
  200. 3: Encoding process failed (failed=1)
  201. 4: Ready (failed=0)
  202. 5: Deleted (failed=0)
  203. 6: Wrong priviledges (failed=0)
  204. }
  205. / doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getStatus
  206. */
  207. function video_status($videoID) {
  208. $xml = $this->sendRequest('video.status','video_id='.$videoID); // Get XML response
  209. // Use included parser?
  210. if ($this->parser) {
  211. // Several steps
  212. // 1. Convert XML into an array
  213. // 2. Check for errors
  214. // i. Return array of error number, short, and long description
  215. // 3. Return multidimensional array of response.
  216. $response = $this->checkErrors(XML_unserialize($xml));
  217. if ($response['error']) {
  218. return $response;
  219. } else {
  220. return $response['video_status'];
  221. }
  222. } else {
  223. return $xml;
  224. }
  225. return false;
  226. }
  227. /* viddler.videos.getDetails
  228. / accepts: $sessionid and $videoID = string
  229. / returns: array - info about video's upload status
  230. / Method specific responses {
  231. 1: Waiting in encode queue (failed=0)
  232. 2: Encoding (failed=0)
  233. 3: Encoding process failed (failed=1)
  234. 4: Ready (failed=0)
  235. 5: Deleted (failed=0)
  236. 6: Wrong priviledges (failed=0)
  237. }
  238. / doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getDetails
  239. */
  240. function video_details($sessionid,$videoID) {
  241. $xml = $this->sendRequest('viddler.videos.getDetails','sessionid='.$sessionid.'&video_id='.$videoID); // Get XML response
  242. // Use included parser?
  243. if ($this->parser) {
  244. // Several steps
  245. // 1. Convert XML into an array
  246. // 2. Check for errors
  247. // i. Return array of error number, short, and long description
  248. // 3. Return multidimensional array of response.
  249. $response = $this->checkErrors(XML_unserialize($xml));
  250. if ($response['error']) {
  251. return $response;
  252. } else {
  253. return $response['video'];
  254. }
  255. } else {
  256. return $xml;
  257. }
  258. return false;
  259. }
  260. /* viddler.videos.getByUser
  261. / accepts: $user = string, $page = string, $per_page = string
  262. / returns: array - information about the videos matching this user
  263. / doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getByUser
  264. */
  265. function videos_listbyuser($user,$page =1,$per_page =5) {
  266. // Get XML response
  267. $xml = $this->sendRequest('viddler.videos.getByUser','user='.$user.'&page='.$page.'&per_page='.$per_page);
  268. // Use included parser?
  269. if ($this->parser) {
  270. // Several steps
  271. // 1. Convert XML into an array
  272. // 2. Check for errors
  273. // i. Return array of error number, short, and long description
  274. // 3. Return multidimensional array of response.
  275. $response = $this->checkErrors(XML_unserialize($xml));
  276. if ($response['error']) {
  277. return $response;
  278. } else {
  279. return $response['video_list'];
  280. }
  281. } else {
  282. return $xml;
  283. }
  284. }
  285. /* viddler.videos.getByTag
  286. / accepts: $tag = string, $page = string, $per_page = string
  287. / returns: array - information about videos matching this tag
  288. / doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getByTag
  289. */
  290. function videos_listbytag($tag,$page =1,$per_page =5) {
  291. // Get XML response
  292. $xml = $this->sendRequest('viddler.videos.getByTag','tag='.$tag.'&page='.$page.'&per_page='.$per_page);
  293. // Use included parser?
  294. if ($this->parser) {
  295. // Several steps
  296. // 1. Convert XML into an array
  297. // 2. Check for errors
  298. // i. Return array of error number, short, and long description
  299. // 3. Return multidimensional array of response.
  300. $response = $this->checkErrors(XML_unserialize($xml));
  301. if ($response['error']) {
  302. return $response;
  303. } else {
  304. return $response['video_list'];
  305. }
  306. } else {
  307. return $xml;
  308. }
  309. return false;
  310. }
  311. /* viddler.videos.getFeatured
  312. / accepts: nothing.
  313. / returns: array - information about videos that are featured
  314. / doc: http://wiki.developers.viddler.com/index.php/Viddler.videos.getFeatured
  315. */
  316. function videos_listfeatured() {
  317. // Get XML response
  318. $xml = $this->sendRequest('viddler.videos.getFeatured','');
  319. // Use included parser?
  320. if ($this->parser) {
  321. // Several steps
  322. // 1. Convert XML into an array
  323. // 2. Check for errors
  324. // i. Return array of error number, short, and long description
  325. // 3. Return multidimensional array of response.
  326. $response = $this->checkErrors(XML_unserialize($xml));
  327. if ($response['error']) {
  328. return $response;
  329. } else {
  330. return $response['video_list'];
  331. }
  332. } else {
  333. return $xml;
  334. }
  335. return false;
  336. }
  337. /*########## Misc. Functions ########### */
  338. // Error checking
  339. function checkErrors($response) {
  340. if ($response['error']) {
  341. switch ($response['error']) {
  342. case '1':
  343. $errorshort = 'An internal error has occurred.';
  344. break;
  345. case '2':
  346. $errorshort = 'Bad argument format.';
  347. break;
  348. case '3':
  349. $errorshort = 'Unknown argument specified.';
  350. break;
  351. case '4':
  352. $errorshort = 'Missing required argument for this method.';
  353. break;
  354. case '5':
  355. $errorshort = 'No method specified.';
  356. break;
  357. case '6':
  358. $errorshort = 'Unknown method specified.';
  359. break;
  360. case '7':
  361. $errorshort = 'API key missing.';
  362. break;
  363. case '8':
  364. $errorshort = 'Invalid or unknown API key specified.';
  365. break;
  366. case '9':
  367. $errorshort = 'Invalid or expired sessionid.';
  368. break;
  369. case '10':
  370. $errorshort = 'HTTP method used not allowed on this method.';
  371. break;
  372. case '100':
  373. $errorshort = 'Video could not be found.';
  374. break;
  375. case '101':
  376. $errorshort = 'Username not found.';
  377. break;
  378. case '102':
  379. $errorshort = 'This account has been suspended.';
  380. break;
  381. case '103':
  382. $errorshort = 'Password incorrect for this username.';
  383. break;
  384. case '104':
  385. $errorshort = 'Terms of Service not accepted by user.';
  386. break;
  387. case '105':
  388. $errorshort = 'Username already in use.';
  389. break;
  390. case '106':
  391. $errorshort = 'Email address already in use.';
  392. break;
  393. case '200':
  394. $errorshort = 'The file is too large. Please limit to 500Mb.';
  395. break;
  396. default:
  397. $errorshort = 'An unknown error has occured.';
  398. break;
  399. } // End switch
  400. $error = array('error' => array('number'=>$response['error'],'shortdesc'=>$errorshort));
  401. return $error;
  402. }
  403. return $response;
  404. }
  405. /**
  406. * Build arguments to use for a request to the API
  407. *
  408. * @param array $p An associative array of parameters
  409. * @param string $return_type Optional return type of parameters ['array'|'string']
  410. * @return mixed The filtered arguments in the format requested
  411. */
  412. public function buildArguments( $p, $return_type = 'string') {
  413. $args = array();
  414. foreach ($p as $key => $value) {
  415. // Skip method request name and submit button
  416. switch ($key) {
  417. case 'method':
  418. case 'submit':
  419. case 'MAX_FILE_SIZE':
  420. continue;
  421. }
  422. $args[$key] = $value;
  423. } // end foreach
  424. switch ($return_type) {
  425. // If array assume uploading
  426. case 'array':
  427. $uploadfile = $this->uploaddir . basename($_FILES['file']['name']);
  428. if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
  429. $args['file'] = "@{$uploadfile}";
  430. }
  431. break;
  432. // If String, chop off last ampersand
  433. case 'string':
  434. default:
  435. $args = http_build_query($args, null, '&');
  436. break;
  437. }
  438. return $args;
  439. }
  440. // Send REST request
  441. function sendRequest($method,$args) {
  442. // Build Request URL
  443. $reqURL = $this->viddlerREST.'?api_key='.$this->apiKey.'&method='.$method;
  444. if ($method != 'viddler.videos.upload') $reqURL .= '&'.$args;
  445. // Send request via CURL
  446. $curl_handle = curl_init();
  447. curl_setopt ($curl_handle, CURLOPT_URL, $reqURL); // Request URL
  448. curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1); // Return as string
  449. curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 1); // Fail if timeout
  450. // If sending a file, change to POST instead of GET
  451. if ($method == 'viddler.videos.upload') {
  452. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
  453. }
  454. $response = curl_exec($curl_handle); // Call!
  455. if (!$response) $response = curl_error($curl_handle);
  456. curl_close($curl_handle); // Close connection
  457. // When CURL doesn't work, use this.
  458. //$response = file_get_contents($reqURL);
  459. // Return XML response as string
  460. if (!$response) {
  461. return false; // 'There is no response. CURL might not by allowed? <br />'.$reqURL.'<br /><br />';
  462. }
  463. return $response;
  464. } // End sentReq();
  465. }
  466. /**
  467. * Viddler Silo
  468. */
  469. class ViddlerSilo extends Plugin implements MediaSilo
  470. {
  471. const SILO_NAME = 'Viddler';
  472. protected $cache = array();
  473. /*
  474. // add a rewrite rule for our auto-generated video page.
  475. public function filter_rewrite_rules( $rules ) {
  476. $rules[] = new RewriteRule(array(
  477. 'name' => 'viddlerlist',
  478. 'parse_regex' => '/^sillytv[\/]{0,1}$/i',
  479. 'build_str' => 'viddlerlist',
  480. 'handler' => 'ViddlerAdminHandler',
  481. 'action' => 'display_viddlerlist',
  482. 'priority' => 7,
  483. 'is_active' => 1,
  484. ));
  485. return $rules;
  486. }
  487. */
  488. /**
  489. * Initialize some internal values when plugin initializes
  490. */
  491. public function action_init()
  492. {
  493. // add some js to the admin header
  494. Stack::add( 'admin_header_javascript', $this->get_url(true) . 'vidfuncs.js', 'viddlerjs', array('media','jquery') );
  495. $this->viddler = new Phpviddler();
  496. }
  497. /**
  498. * Return basic information about this silo
  499. * name- The name of the silo, used as the root directory for media in this silo
  500. */
  501. public function silo_info()
  502. {
  503. if ($this->is_auth()) {
  504. return array('name' => self::SILO_NAME, 'icon' => URL::get_from_filesystem(__FILE__) . '/icon.png');
  505. }
  506. else {
  507. return array();
  508. }
  509. }
  510. /**
  511. * Return directory contents for the silo path
  512. *
  513. * @param string $path The path to retrieve the contents of
  514. * @return array An array of MediaAssets describing the contents of the directory
  515. */
  516. public function silo_dir($path)
  517. {
  518. $user = User::identify()->info->viddler__username;
  519. if (Cache::has('viddler:videos:' . $user)) {
  520. $pre = Cache::get('viddler:videos:' . $user);
  521. }
  522. else {
  523. $pre = $this->viddler->videos_listbyuser( $user, '', 20 );
  524. Cache::set('viddler:videos:' . $user, $pre);
  525. }
  526. $xml = new SimpleXMLElement( $pre );
  527. $results = array();
  528. foreach( $xml->video as $video ) {
  529. $props = array();
  530. foreach($video->children() as $name => $value) {
  531. $props[$name] = (string)$value;
  532. }
  533. $props['filetype'] = 'viddler';
  534. $asset = new MediaAsset(
  535. self::SILO_NAME . '/' . $path . ($path == '' ? '' : '/') . $video->id,
  536. false,
  537. $props
  538. );
  539. $results[] = $asset;
  540. //echo '<div class="media"><img src="' . $video->thumbnail_url . '" width="150px"><div class="foroutput"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="437" height="370" id="viddler_chrisjdavis_' . $video->id . '"><param name="movie" value="http://www.viddler.com/player/' . $video->id . '/" /><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><embed src="http://www.viddler.com/player/' . $video->id . '/" width="437" height="370" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" name="viddler_chrisjdavis" ></embed></object></div></div>';
  541. }
  542. return $results;
  543. }
  544. /**
  545. * Get the file from the specified path
  546. *
  547. * @param string $path The path of the file to retrieve
  548. * @param array $qualities Qualities that specify the version of the file to retrieve.
  549. * @return MediaAsset The requested asset
  550. */
  551. public function silo_get($path, $qualities = null)
  552. {
  553. }
  554. /**
  555. * Get the direct URL of the file of the specified path
  556. *
  557. * @param string $path The path of the file to retrieve
  558. * @param array $qualities Qualities that specify the version of the file to retrieve.
  559. * @return string The requested url
  560. */
  561. public function silo_url( $path, $qualities = null )
  562. {
  563. $id = basename($path);
  564. $video = $this->cache[$id];
  565. if (isset($qualities['size']) && $qualities['size']=='thumbnail') {
  566. $url = $video->thumbnail_url;
  567. }
  568. else {
  569. $url = $video->url;
  570. }
  571. return $url;
  572. }
  573. /**
  574. * Create a new asset instance for the specified path
  575. *
  576. * @param string $path The path of the new file to create
  577. * @return MediaAsset The requested asset
  578. */
  579. public function silo_new($path)
  580. {
  581. }
  582. /**
  583. * Store the specified media at the specified path
  584. *
  585. * @param string $path The path of the file to retrieve
  586. * @param MediaAsset $ The asset to store
  587. */
  588. public function silo_put($path, $filedata)
  589. {
  590. }
  591. /**
  592. * Delete the file at the specified path
  593. *
  594. * @param string $path The path of the file to retrieve
  595. */
  596. public function silo_delete($path)
  597. {
  598. }
  599. /**
  600. * Retrieve a set of highlights from this silo
  601. * This would include things like recently uploaded assets, or top downloads
  602. *
  603. * @return array An array of MediaAssets to highlihgt from this silo
  604. */
  605. public function silo_highlights()
  606. {
  607. }
  608. public function controls()
  609. {
  610. echo '<ul class="silo-controls"><li id="vupload"><a href="#upload" title="Upload a video">Upload</a></li<li id="vrecord"><a href="#record" title="Record a video">Record</a></li><li id="vstream"><a class="final" href="#stream" title="View your video stream">Viddler Stream</a></li></ul>';
  611. }
  612. public function silo_upload_form()
  613. {
  614. echo '<form name="viddler_upload"></form>';
  615. }
  616. public function upload_form()
  617. {
  618. ?>
  619. <div id="viddlerform" class="container" style="display:none;">
  620. <h2>Upload a Video</h2>
  621. <form name="viddler_upload">
  622. <div class="container">
  623. <p class="column span-5">Video</p>
  624. <p class="column span-14 last"><input type="file" name="viddler_file"></p>
  625. </div>
  626. <hr>
  627. <div class="container">
  628. <p class="column span-5">Video Title</p>
  629. <p class="column span-14 last"><input type="text" name="viddler_title"></p>
  630. </div>
  631. <hr>
  632. <div class="container">
  633. <p class="column span-5">Video Tags</p>
  634. <p class="column span-14 last"><input type="text" name="viddler_tags"></p>
  635. </div>
  636. <hr>
  637. <div class="container">
  638. <p class="column span-5">Video Description</p>
  639. <p class="column span-14 last"><textarea name="viddler_desc"></textarea></p>
  640. <p class="column span-5 prepend-5"><input type="submit" value="Upload Video" id="execute_viddler"></p>
  641. </div>
  642. </form>
  643. </div>
  644. <div id="viddlerrecord" class="container" style="display:none;">
  645. <div class="column span-14 prepend-4">
  646. <h2>Record a Video</h2>
  647. <p><?php
  648. $user = User::identify()->info->viddler__username;
  649. $pass = User::identify()->info->viddler__password;
  650. $auth = $this->viddler->user_authenticate( array( 'user' => $user, 'password' => $pass, 'get_record_token' => 1 ) );
  651. $sid = new SimpleXMLElement( $auth );
  652. $mt = $test->video_getrecordtoken( $sid->sessionid );
  653. $token = new SimpleXMLElement( $mt );
  654. $embed = $test->video_getrecordembed( $token );
  655. echo $embed;
  656. ?></p>
  657. </div>
  658. <?php
  659. }
  660. /**
  661. * Retrieve the permissions for the current user to access the specified path
  662. *
  663. * @param string $path The path to retrieve permissions for
  664. * @return array An array of permissions constants (MediaSilo::PERM_READ, MediaSilo::PERM_WRITE)
  665. */
  666. public function silo_permissions($path)
  667. {
  668. }
  669. /**
  670. * Add actions to the plugin page for this plugin
  671. * The authorization should probably be done per-user.
  672. *
  673. * @param array $actions An array of actions that apply to this plugin
  674. * @param string $plugin_id The string id of a plugin, generated by the system
  675. * @return array The array of actions to attach to the specified $plugin_id
  676. */
  677. public function filter_plugin_config( $actions, $plugin_id ) {
  678. if ( $plugin_id == $this->plugin_id() ) {
  679. $viddler_ok = $this->is_auth();
  680. if ( $viddler_ok ) {
  681. $actions[]= 'Log Out';
  682. } else {
  683. $actions[]= 'Log In';
  684. }
  685. }
  686. return $actions;
  687. }
  688. /**
  689. * Respond to the user selecting an action on the plugin page
  690. *
  691. * @param string $plugin_id The string id of the acted-upon plugin
  692. * @param string $action The action string supplied via the filter_plugin_config hook
  693. */
  694. public function action_plugin_ui( $plugin_id, $action ) {
  695. switch ( $action ) {
  696. case 'Log In':
  697. $ui = new FormUI( strtolower( get_class( $this ) ) );
  698. $ui->append('text', 'username', 'user:viddler__username', 'Viddler Username:');
  699. $ui->append('password', 'password', 'user:viddler__password', 'Viddler Password:');
  700. $ui->append('submit', 'submit', 'Log In');
  701. $ui->on_success( array( $this, 'updated_config' ) );
  702. $ui->out();
  703. /*
  704. if ( $this->is_auth() ) {
  705. $deauth_url= URL::get('admin', array('page' => 'plugins', 'configure' => $this->plugin_id(), 'action' => 'Log Out')) . '#plugin_options';
  706. echo "<p>You have successfully logged into Vidder via Habari.</p>";
  707. echo "<p>Do you want to <a href=\"{$deauth_url}\">log out</a>?</p>";
  708. }
  709. else {
  710. $username = $ui->username->value;
  711. $password = $ui->password->value;
  712. if ($username != '' && $password != '') {
  713. $auth= $this->viddler->user_authenticate( array( 'user' => $username, 'password' => $password ) );
  714. $xml= new SimpleXMLElement( $auth );
  715. User::identify()->info->viddler__token = (string) $xml->session_id;
  716. }
  717. }
  718. */
  719. break;
  720. case 'Log Out':
  721. unset(User::identify()->info->viddler__token);
  722. $reauth_url = URL::get('admin', array('page' => 'plugins', 'configure' => $this->plugin_id(), 'action' => 'Log In')) . '#plugin_options';
  723. echo '<p>The Viddler Silo Plugin session has been logged out.<p>';
  724. echo "<p>Do you want to <a href=\"{$reauth_url}\">log in again</a>?<p>";
  725. break;
  726. }
  727. }
  728. /**
  729. * Returns true if plugin config form values defined in action_plugin_ui should be stored in options by Habari
  730. * @return boolean True if options should be stored
  731. **/
  732. public function updated_config($ui)
  733. {
  734. $username = $ui->username->value;
  735. $password = $ui->password->value;
  736. if ($username != '' && $password != '') {
  737. $auth = $this->viddler->user_authenticate( array( 'user' => $username, 'password' => $password ) );
  738. $xml = new SimpleXMLElement( $auth );
  739. $token = (string) $xml->sessionid;
  740. if ($token == '') {
  741. $ui->set_option('success_message', _t('The username and password you supplied failed to log in to Viddler.'));
  742. }
  743. else {
  744. $ui->save();
  745. User::identify()->info->viddler__token = $token;
  746. User::identify()->info->commit();
  747. return '<p>' . _t('You have logged in to Viddler.') . '</p>';
  748. }
  749. }
  750. return false;
  751. }
  752. private function is_auth()
  753. {
  754. $token = User::identify()->info->viddler__token;
  755. if ( $token != '' ) {
  756. return true;
  757. } else {
  758. return false;
  759. }
  760. }
  761. public function action_admin_header( $theme )
  762. {
  763. if (Controller::get_var('page') == 'publish') {
  764. $header = <<< HEADER
  765. habari.media.output.viddler = {display: function(index, fileobj) {
  766. habari.editor.insertSelection( ''+
  767. '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="437" height="370" id="viddler_' + fileobj.basename + '">' +
  768. '<param name="movie" value="http://www.viddler.com/player/' + fileobj.basename + '/" />' +
  769. '<param name="allowScriptAccess" value="always" />' +
  770. '<param name="allowFullScreen" value="true" />' +
  771. '<embed src="http://www.viddler.com/player/' + fileobj.basename + '/" width="437" height="370" type="application/x-shockwave-flash" allowScriptAccess="always" allowFullScreen="true" ></embed>' +
  772. '</object>'
  773. );
  774. }}
  775. HEADER;
  776. Stack::add( 'admin_header_javascript', $header, 'viddlerinline', array('viddlerjs', 'media','jquery') );
  777. }
  778. }
  779. /**
  780. * Produce a link for the media control bar that causes a specific panel to be displayed
  781. *
  782. * @param string $path The path to pass
  783. * @param string $path The panel to display
  784. * @param string $title The text to use for the link in the control bar
  785. * @return string The link to create
  786. */
  787. public function link_panel( $path, $panel, $title )
  788. {
  789. return '<a href="#" onclick="habari.media.showpanel(\''.$path.'\', \''.$panel.'\');return false;">' . $title . '</a>';
  790. }
  791. /**
  792. * Provide controls for the media control bar
  793. *
  794. * @param array $controls Incoming controls from other plugins
  795. * @param MediaSilo $silo An instance of a MediaSilo
  796. * @param string $path The path to get controls for
  797. * @param string $panelname The name of the requested panel, if none then emptystring
  798. * @return array The altered $controls array with new (or removed) controls
  799. *
  800. * @todo This should really use FormUI, but FormUI needs a way to submit forms via ajax
  801. */
  802. public function filter_media_controls( $controls, $silo, $path, $panelname )
  803. {
  804. $class = __CLASS__;
  805. if ($silo instanceof $class) {
  806. if (User::identify()->can('upload_viddler')) {
  807. $controls[] = $this->link_panel(self::SILO_NAME . '/' . $path, 'upload', 'Upload');
  808. $controls[] = $this->link_panel(self::SILO_NAME . '/' . $path, 'record', 'Record');
  809. }
  810. }
  811. return $controls;
  812. }
  813. /**
  814. * Provide requested media panels for this plugin
  815. *
  816. * @param string $panel The HTML content of the panel to be output in the media bar
  817. * @param MediaSilo $silo The silo for which the panel was requested
  818. * @param string $path The path within the silo (silo root omitted) for which the panel was requested
  819. * @param string $panelname The name of the requested panel
  820. * @return string The modified $panel to contain the HTML output for the requested panel
  821. *
  822. * @todo Move the uploaded file from the temporary location to the location indicated by the path field.
  823. */
  824. public function filter_media_panels( $panel, $silo, $path, $panelname)
  825. {
  826. $class = __CLASS__;
  827. if ($silo instanceof $class) {
  828. switch ($panelname) {
  829. case 'record':
  830. $user = User::identify()->info->viddler__username;
  831. $pass = User::identify()->info->viddler__password;
  832. $auth = $this->viddler->user_authenticate( array( 'user' => $user, 'password' => $pass, 'get_record_token' => 1, 'record_token' => 1 ) );
  833. $sid = new SimpleXMLElement( $auth );
  834. $rt = $sid->record_token;
  835. $panel .= "<div class=\"span-18\" style=\"padding-top:30px;color: #e0e0e0;margin: 0px auto;\">";
  836. $panel .= $this->viddler->video_getrecordembed( $rt );
  837. $panel .= '</div>';
  838. break;
  839. case 'upload':
  840. if (isset($_FILES['file'])) {
  841. $size = Utils::human_size($_FILES['file']['size']);
  842. $panel .= "<div class=\"span-18\" style=\"padding-top:30px;color: #e0e0e0;margin: 0px auto;\"><p>File Uploaded: {$_FILES['file']['name']} ($size)</p>";
  843. $path = self::SILO_NAME . '/' . preg_replace('%\.{2,}%', '.', $path). '/' . $_FILES['file']['name'];
  844. $asset = new MediaAsset($path, false);
  845. $asset->upload($_FILES['file']);
  846. if ($asset->put()) {
  847. $panel .= '<p>File added successfully.</p>';
  848. }
  849. else {
  850. $panel .= '<p>File could not be added to the silo.</p>';
  851. }
  852. $panel .= '<p><a href="#" onclick="habari.media.forceReload();habari.media.showdir(\'' . dirname($path) . '\');">Browse the current silo path.</a></p></div>';
  853. }
  854. else {
  855. $fullpath = self::SILO_NAME . '/' . $path;
  856. $form_action = URL::get('admin_ajax', array('context' => 'media_panel'));
  857. $panel .= <<< UPLOAD_FORM
  858. <form enctype="multipart/form-data" method="post" id="simple_upload" target="simple_upload_frame" action="{$form_action}" class="span-10" style="margin:0px auto;text-align: center">
  859. <p style="padding-top:30px;">Upload to: <b style="font-weight:normal;color: #e0e0e0;font-size: 1.2em;">/{$path}</b></p>
  860. <p><input type="file" name="file"><input type="submit" name="upload" value="Upload">
  861. <input type="hidden" name="path" value="{$fullpath}">
  862. <input type="hidden" name="panel" value="{$panelname}">
  863. </p>
  864. </form>
  865. <iframe id="simple_upload_frame" name="simple_upload_frame" style="width:1px;height:1px;" onload="simple_uploaded();"></iframe>
  866. <script type="text/javascript">
  867. var responsedata;
  868. function simple_uploaded() {
  869. if (!$('#simple_upload_frame')[0].contentWindow) return;
  870. var response = $($('#simple_upload_frame')[0].contentWindow.document.body).text();
  871. if (response) {
  872. eval('responsedata = ' + response);
  873. window.setTimeout(simple_uploaded_complete, 500);
  874. }
  875. }
  876. function simple_uploaded_complete() {
  877. habari.media.jsonpanel(responsedata);
  878. }
  879. </script>
  880. UPLOAD_FORM;
  881. }
  882. }
  883. }
  884. return $panel;
  885. }
  886. }
  887. /*
  888. // This is an auto-generated video page. There is a rewrite rules array in the plugin
  889. // above that needs to be uncommented as well. Not sure if we actually want this in
  890. // the plugin, which is why it is commented out.
  891. class ViddlerAdminHandler extends ActionHandler
  892. {
  893. private $vids;
  894. private $theme= null;
  895. public function __construct() {
  896. $this->theme= Themes::create();
  897. }
  898. public function act_display_viddlerlist() {
  899. $this->load_vids();
  900. $this->theme->assign( 'vids', $this->vid );
  901. $this->theme->display( 'vids' );
  902. }
  903. public function load_vids() {
  904. $user= Options::get( 'viddlersilo:username_' . User::identify()->id );
  905. $viddler= new Phpviddler();
  906. $pre= $viddler->videos_listbyuser( $user, '', 20, 'view_count' );
  907. $xml= new SimpleXMLElement( $pre );
  908. foreach( $xml->video as $video ) {
  909. $this->vid[]= $video;
  910. }
  911. }
  912. }
  913. */
  914. ?>