PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/video.php

https://bitbucket.org/ryanhowdy/family-connections
PHP | 1388 lines | 999 code | 195 blank | 194 comment | 76 complexity | d663288c8f19d34bdd1e70d0d0683fc3 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * Video
  4. *
  5. * PHP version 5
  6. *
  7. * @category FCMS
  8. * @package FamilyConnections
  9. * @author Ryan Haudenschilt <r.haudenschilt@gmail.com>
  10. * @copyright 2011 Haudenschilt LLC
  11. * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
  12. * @link http://www.familycms.com/wiki/
  13. * @since 2.6
  14. */
  15. session_start();
  16. define('URL_PREFIX', '');
  17. define('GALLERY_PREFIX', 'gallery/');
  18. require 'fcms.php';
  19. load('datetime', 'socialmedia', 'youtube', 'comments');
  20. init();
  21. $TMPL = array(
  22. 'currentUserId' => $fcmsUser->id,
  23. 'sitename' => getSiteName(),
  24. 'nav-link' => getNavLinks(),
  25. 'pagetitle' => T_('Video Gallery'),
  26. 'path' => URL_PREFIX,
  27. 'displayname' => $fcmsUser->displayName,
  28. 'version' => getCurrentVersion(),
  29. 'year' => date('Y')
  30. );
  31. control();
  32. exit();
  33. /**
  34. * control
  35. *
  36. * The controlling structure for this script.
  37. *
  38. * @return void
  39. */
  40. function control ()
  41. {
  42. global $fcmsUser;
  43. // AJAX
  44. if (isset($_GET['check_status']))
  45. {
  46. if (isset($_SESSION['source_id']))
  47. {
  48. $sessionToken = getSessionToken($fcmsUser->id);
  49. echo getUploadStatus($_SESSION['source_id'], $sessionToken);
  50. return;
  51. }
  52. echo 'n/a';
  53. return;
  54. }
  55. if (isset($_GET['upload']))
  56. {
  57. // YouTube
  58. if ($_GET['upload'] == 'youtube')
  59. {
  60. // Step 3 - YouTube Response
  61. if (isset($_GET['status']) && isset($_GET['id']))
  62. {
  63. displayYouTubeUploadStatusPage();
  64. }
  65. // Step 2 - Video
  66. elseif (isset($_POST['upload_data']))
  67. {
  68. displayYouTubeUploadFilePage();
  69. }
  70. // Step 1 - Title/Desc
  71. else
  72. {
  73. displayYouTubeUploadPage();
  74. }
  75. }
  76. // Vimeo
  77. else
  78. {
  79. displayVimeoUploadPage();
  80. }
  81. }
  82. elseif (isset($_GET['u']))
  83. {
  84. if (isset($_GET['id']))
  85. {
  86. if (isset($_POST['addcomment']))
  87. {
  88. displayCommentSubmit();
  89. }
  90. elseif (isset($_POST['remove_video']))
  91. {
  92. displayRemoveVideoSubmit();
  93. }
  94. elseif (isset($_POST['delete_video']))
  95. {
  96. displayDeleteVideoSubmit();
  97. }
  98. else
  99. {
  100. displayVideoPage();
  101. }
  102. }
  103. else
  104. {
  105. displayUserVideosPage();
  106. }
  107. }
  108. elseif (isset($_GET['members']))
  109. {
  110. displayMembersListPage();
  111. }
  112. else
  113. {
  114. displayLatestPage();
  115. }
  116. }
  117. /**
  118. * displayHeader
  119. *
  120. * @return void
  121. */
  122. function displayHeader ()
  123. {
  124. global $fcmsUser, $TMPL;
  125. $TMPL['javascript'] = '
  126. <script type="text/javascript">
  127. //<![CDATA[
  128. Event.observe(window, \'load\', function() {
  129. initChatBar(\''.T_('Chat').'\', \''.$TMPL['path'].'\');
  130. initYouTubeVideoStatus(\''.T_('This page will automatically refresh').'\');
  131. initHideVideoEdit(\''.T_('Edit Video').'\');
  132. });
  133. //]]>
  134. </script>';
  135. include_once getTheme($fcmsUser->id).'header.php';
  136. echo '
  137. <div id="video" class="centercontent">
  138. <div id="actions_menu">
  139. <ul>
  140. <li><a href="?upload=youtube">'.T_('Upload to YouTube').'</a></li>
  141. </ul>
  142. </div>';
  143. }
  144. /**
  145. * displayFooter
  146. *
  147. * @return void
  148. */
  149. function displayFooter ()
  150. {
  151. global $fcmsUser, $TMPL;
  152. echo '
  153. </div><!-- /centercontent -->';
  154. include_once getTheme($fcmsUser->id).'footer.php';
  155. }
  156. /**
  157. * checkUserAuthedYouTube
  158. *
  159. * Check to make sure the user is connected and authed at YouTube.
  160. *
  161. * Assumed displayHeader() already sent
  162. *
  163. * @return void
  164. */
  165. function checkUserAuthedYouTube ()
  166. {
  167. global $fcmsUser;
  168. // Get session token
  169. $sql = "SELECT `youtube_session_token`
  170. FROM `fcms_user_settings`
  171. WHERE `user` = '$fcmsUser->id'
  172. AND `youtube_session_token` IS NOT NULL
  173. AND `youtube_session_token` != ''";
  174. $result = mysql_query($sql);
  175. if (!$result)
  176. {
  177. displaySqlError($sql, mysql_error());
  178. displayFooter();
  179. return;
  180. }
  181. if (mysql_num_rows($result) <= 0)
  182. {
  183. echo '
  184. <div class="info-alert">
  185. <h2>'.T_('Not connected to YouTube.').'</h2>
  186. <p>'.T_('The video gallery relies on YouTube. You must create a YouTube account and connect it with your Family Connections account.').'</p>
  187. <p><a href="settings.php?view=socialmedia">'.T_('Connect to YouTube').'</a></p>
  188. </div>';
  189. displayFooter();
  190. die();
  191. }
  192. $row = mysql_fetch_assoc($result);
  193. $_SESSION['youtube_session_token'] = $row['youtube_session_token'];
  194. $youtubeConfig = getYouTubeConfigData();
  195. $httpClient = getAuthSubHttpClient($youtubeConfig['youtube_key'], $row['youtube_session_token']);
  196. if ($httpClient === false)
  197. {
  198. // Error message was already displayed by getAuthSubHttpClient()
  199. displayFooter();
  200. die();
  201. }
  202. }
  203. /**
  204. * displayYouTubeUploadPage
  205. *
  206. * @return void
  207. */
  208. function displayYouTubeUploadPage ()
  209. {
  210. displayHeader();
  211. checkUserAuthedYouTube();
  212. $youtubeConfig = getYouTubeConfigData();
  213. $httpClient = getAuthSubHttpClient($youtubeConfig['youtube_key']);
  214. if ($httpClient === false)
  215. {
  216. // Error message was already displayed by getAuthSubHttpClient()
  217. displayFooter();
  218. die();
  219. }
  220. $youTubeService = new Zend_Gdata_YouTube($httpClient);
  221. $feed = $youTubeService->getUserProfile('default');
  222. if (!$feed instanceof Zend_Gdata_YouTube_UserProfileEntry)
  223. {
  224. print '
  225. <div class="error-alert">'.T_('Could not get YouTube data for user.').'</div>';
  226. return;
  227. }
  228. $username = $feed->getUsername();
  229. echo '
  230. <form action="video.php?upload=youtube" method="post">
  231. <fieldset>
  232. <legend><span>'.T_('Upload YouTube Video').'</span></legend>
  233. <div class="field-row">
  234. <div class="field-label"><label><b>'.T_('YouTube Account').'</b></label></div>
  235. <div class="field-widget">'.$username.'
  236. </div>
  237. </div>
  238. <div class="field-row">
  239. <div class="field-label"><label><b>'.T_('Title').'</b></label></div>
  240. <div class="field-widget">
  241. <input type="text" name="title" size="50"/>
  242. </div>
  243. </div>
  244. <div class="field-row">
  245. <div class="field-label"><label><b>'.T_('Description').'</b></label></div>
  246. <div class="field-widget">
  247. <textarea cols="50" name="description"></textarea>
  248. </div>
  249. </div>
  250. <div class="field-row">
  251. <div class="field-label"><label><b>'.T_('Category').'</b></label></div>
  252. <div class="field-widget">
  253. <select name="category">
  254. <option value="Autos">'.T_('Autos &amp; Vehicles').'</option>
  255. <option value="Music">'.T_('Music').'</option>
  256. <option value="Animals">'.T_('Pets &amp; Animals').'</option>
  257. <option value="Sports">'.T_('Sports').'</option>
  258. <option value="Travel">'.T_('Travel &amp; Events').'</option>
  259. <option value="Games">'.T_('Gadgets &amp; Games').'</option>
  260. <option value="Comedy">'.T_('Comedy').'</option>
  261. <option value="People">'.T_('People &amp; Blogs').'</option>
  262. <option value="News">'.T_('News &amp; Politics').'</option>
  263. <option value="Entertainment">'.T_('Entertainment').'</option>
  264. <option value="Education">'.T_('Education').'</option>
  265. <option value="Howto">'.T_('Howto &amp; Style').'</option>
  266. <option value="Nonprofit">'.T_('Nonprofit &amp; Activism').'</option>
  267. <option value="Tech">'.T_('Science &amp; Technology').'</option>
  268. </select>
  269. </div>
  270. </div>
  271. <div class="field-row">
  272. <div class="field-label"><label for="unlisted"><b>'.T_('Unlisted').'</b></label></div>
  273. <div class="field-widget">
  274. <input type="checkbox" name="unlisted" id="unlisted_" value="yes" checked="checked"><br/>
  275. <small>'.T_('"Unlisted" means that only people who know the link to the video can view it. The video will not appear in any of YouTube\'s public spaces, such as search results, your channel, or the Browse page, but the link can be shared with anyone.').'</small>
  276. </div>
  277. </div>
  278. <input class="sub1" type="submit" id="upload_data" name="upload_data" value="'.T_('Next').'"/>
  279. &nbsp;'.T_('or').' &nbsp;
  280. <a href="video.php">'.T_('Cancel').'</a>
  281. </fieldset>
  282. </form>';
  283. displayFooter();
  284. }
  285. /**
  286. * displayYouTubeUploadFilePage
  287. *
  288. * Takes the post data from the previous form, sends to youtube, creates new entry,
  289. * and prints the video file upload form.
  290. *
  291. * @return void
  292. */
  293. function displayYouTubeUploadFilePage ()
  294. {
  295. global $fcmsUser;
  296. displayHeader();
  297. $videoTitle = '';
  298. $videoDescription = '';
  299. $cleanVideoTitle = '';
  300. $cleanVideoDescription = '';
  301. if (isset($_POST['title']))
  302. {
  303. $videoTitle = strip_tags($_POST['title']);
  304. $cleanVideoTitle = escape_string($videoTitle);
  305. }
  306. if (isset($_POST['description']))
  307. {
  308. $videoDescription = strip_tags($_POST['description']);
  309. $cleanVideoDescription = escape_string($videoDescription);
  310. }
  311. $videoCategory = isset($_POST['category']) ? escape_string($_POST['category']) : '';
  312. $videoUnlisted = isset($_POST['unlisted']) ? true : false;
  313. // Create fcms video - we update after the youtube video is created
  314. $sql = "INSERT INTO `fcms_video` (
  315. `source_id`,
  316. `title`,
  317. `description`,
  318. `source`,
  319. `created`,
  320. `created_id`,
  321. `updated`,
  322. `updated_id`
  323. )
  324. VALUES (
  325. '0',
  326. '$cleanVideoTitle',
  327. '$cleanVideoDescription',
  328. 'youtube',
  329. NOW(),
  330. '$fcmsUser->id',
  331. NOW(),
  332. '$fcmsUser->id'
  333. )";
  334. if (!mysql_query($sql))
  335. {
  336. displaySqlError($sql, mysql_error());
  337. displayFooter();
  338. return;
  339. }
  340. // Save fcms video id
  341. $_SESSION['fcmsVideoId'] = mysql_insert_id();
  342. $sessionToken = getSessionToken($fcmsUser->id);
  343. $youtubeConfig = getYouTubeConfigData();
  344. $httpClient = getAuthSubHttpClient($youtubeConfig['youtube_key'], $sessionToken);
  345. if ($httpClient === false)
  346. {
  347. // Error message was already displayed by getAuthSubHttpClient()
  348. displayFooter();
  349. die();
  350. }
  351. $youTubeService = new Zend_Gdata_YouTube($httpClient);
  352. $newVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
  353. $newVideoEntry->setVideoTitle($videoTitle);
  354. $newVideoEntry->setVideoDescription($videoDescription);
  355. $newVideoEntry->setVideoCategory($videoCategory);
  356. // make video unlisted
  357. if ($videoUnlisted)
  358. {
  359. $unlisted = new Zend_Gdata_App_Extension_Element('yt:accessControl', 'yt', 'http://gdata.youtube.com/schemas/2007', '');
  360. $unlisted->setExtensionAttributes(array(
  361. array('namespaceUri' => '', 'name' => 'action', 'value' => 'list'),
  362. array('namespaceUri' => '', 'name' => 'permission', 'value' => 'denied')
  363. ));
  364. $newVideoEntry->setExtensionElements(array($unlisted));
  365. }
  366. try
  367. {
  368. $tokenArray = $youTubeService->getFormUploadToken($newVideoEntry, 'http://gdata.youtube.com/action/GetUploadToken');
  369. }
  370. catch (Exception $e)
  371. {
  372. echo '
  373. <div class="error-alert">
  374. <p>'.T('Could not retrieve token for syndicated upload.').'</p>
  375. <p>'.$e->getMessage().'</p>
  376. </div>';
  377. displayFooter();
  378. return;
  379. }
  380. $tokenValue = $tokenArray['token'];
  381. $postUrl = $tokenArray['url'];
  382. $nextUrl = getDomainAndDir().'video.php?upload=youtube';
  383. echo '
  384. <form action="'.$postUrl.'?nexturl='.$nextUrl.'" method="post" enctype="multipart/form-data">
  385. <fieldset>
  386. <legend><span>'.T_('Upload YouTube Video').'</span></legend>
  387. <div class="field-row">
  388. <div class="field-label"><label><b>'.T_('Title').'</b></label></div>
  389. <div class="field-widget"><b>'.$videoTitle.'</b></div>
  390. </div>
  391. <div class="field-row">
  392. <div class="field-label"><label><b>'.T_('Video').'</b></label></div>
  393. <div class="field-widget">
  394. <input type="file" name="file" size="50"/>
  395. </div>
  396. </div>
  397. <input name="token" type="hidden" value="'.$tokenValue.'"/>
  398. <input class="sub1" type="submit" id="upload_file" name="upload_file" value="'.T_('Upload').'"/>
  399. </fieldset>
  400. </form>';
  401. displayFooter();
  402. }
  403. /**
  404. * displayYouTubeUploadStatusPage
  405. *
  406. * @return void
  407. */
  408. function displayYouTubeUploadStatusPage ()
  409. {
  410. global $fcmsUser;
  411. $sourceId = $_GET['id'];
  412. $status = $_GET['status'];
  413. $videoId = (int)$_SESSION['fcmsVideoId'];
  414. unset($_SESSION['fcmsVideoId']);
  415. switch ($status)
  416. {
  417. case $status < 400:
  418. // Connect to YouTube and get more info about this video
  419. $youtubeConfig = getYouTubeConfigData();
  420. $httpClient = getAuthSubHttpClient($youtubeConfig['youtube_key']);
  421. if ($httpClient === false)
  422. {
  423. // Error message was already displayed by getAuthSubHttpClient()
  424. displayFooter();
  425. die();
  426. }
  427. $youTubeService = new Zend_Gdata_YouTube($httpClient);
  428. $videoEntry = $youTubeService->getVideoEntry($sourceId);
  429. $duration = $videoEntry->getVideoDuration();
  430. $thumbs = $videoEntry->getVideoThumbnails();
  431. $height = '420';
  432. $width = '780';
  433. if (count($thumbs) > 0)
  434. {
  435. $height = escape_string($thumbs[0]['height']);
  436. $width = escape_string($thumbs[0]['width']);
  437. }
  438. // Update fcms video
  439. $sql = "UPDATE `fcms_video`
  440. SET `source_id` = '".escape_string($sourceId)."',
  441. `height` = '$height',
  442. `width` = '$width',
  443. `updated` = NOW()
  444. WHERE `id` = '$videoId'";
  445. if (!mysql_query($sql))
  446. {
  447. displayHeader();
  448. displaySqlError($sql, mysql_error());
  449. displayFooter();
  450. return;
  451. }
  452. // Create fcms video
  453. header("Location: video.php?u=$fcmsUser->id&id=$videoId");
  454. break;
  455. default:
  456. displayHeader();
  457. echo '
  458. <div class="error-alert">
  459. <p>'.T_('An error occurred with you video upload.').'</p>
  460. <p>'.getUploadStatus($videoId).'</p>
  461. </div>';
  462. displayFooter();
  463. break;
  464. }
  465. }
  466. /**
  467. * displayVimeoUploadPage
  468. *
  469. * @return void
  470. */
  471. function displayVimeoUploadPage ()
  472. {
  473. displayHeader();
  474. echo 'Vimeo not implemented yet';
  475. displayFooter();
  476. }
  477. /**
  478. * displayLatestPage
  479. *
  480. * @return void
  481. */
  482. function displayLatestPage ()
  483. {
  484. global $fcmsUser;
  485. displayHeader();
  486. // Get Last 6 videos
  487. $sql = "SELECT v.`id`, v.`source_id`, v.`title`, v.`created`, v.`created_id`, u.`id` AS user_id, u.`fname`, u.`lname`
  488. FROM `fcms_video` AS v
  489. LEFT JOIN `fcms_users` AS u ON v.`created_id` = u.`id`
  490. WHERE `active` = 1
  491. ORDER BY v.`updated` DESC
  492. LIMIT 6";
  493. $result = mysql_query($sql);
  494. if (!$result)
  495. {
  496. displaySqlError($sql, mysql_error());
  497. displayFooter();
  498. return;
  499. }
  500. displayVideoStartCode();
  501. if (mysql_num_rows($result) <= 0)
  502. {
  503. // TODO move js
  504. echo '
  505. <div class="info-alert">
  506. <h2>'.T_('No Videos').'</h2>
  507. <p>'.T_('Unfortunately no videos have been added yet.').'</p>
  508. <div id="help"><br/>
  509. <p><b>'.T_('How do I add videos?').'</b></p>
  510. <ol>
  511. <li><a href="http://www.youtube.com">'.T_('Create a YouTube account').'</a></li>
  512. <li><a href="settings.php?view=socialmedia">'.T_('Connect your YouTube account with Family Connections').'</a></li>
  513. <li><a href="?upload=youtube">'.T_('Upload Videos').'</a></li>
  514. </ol><br/>
  515. <p><b>'.T_('Why aren\'t my videos showing up?').'</b></p>
  516. <p>'.T_('Depending on your setup and the size of the video uploaded, it can take over an hour before your videos show up on the site.').'</p>
  517. </div>
  518. </div>
  519. <script type="text/javascript">
  520. if ($("help")) {
  521. var div = $("help");
  522. div.hide();
  523. var a = new Element("a", { href: "#" }).update("'.T_('Learn more.').'");
  524. a.onclick = function() { $("help").toggle(); return false; };
  525. div.insert({"before":a});
  526. }
  527. </script>';
  528. displayFooter();
  529. return;
  530. }
  531. echo '
  532. <div id="video_content">
  533. <h2>'.T_('Latest Videos').'<h2>
  534. <ul class="categories">';
  535. while ($row = mysql_fetch_assoc($result))
  536. {
  537. $name = cleanOutput($row['fname']).' '.cleanOutput($row['lname']);
  538. $date = fixDate('Y-m-d', '', $row['created'], $row['created_id']);
  539. echo '
  540. <li class="category">
  541. <a href="?u='.$row['user_id'].'&amp;id='.$row['id'].'"><img src="http://i.ytimg.com/vi/'.$row['source_id'].'/default.jpg"/></a>
  542. <span>
  543. <strong>'.cleanOutput($row['title']).'</strong>
  544. <i>'.sprintf(T_pgettext('%s is a person\'s name', 'by %s'), $name).'</i>
  545. <i>'.sprintf(T_pgettext('%s is a date', 'on %s'), $date).'</i>
  546. </span>
  547. </li>';
  548. }
  549. echo '
  550. </ul>';
  551. // Get Last 8 users
  552. $sql = "SELECT v.`id`, COUNT(*) AS 'count', v.`created_id` AS 'user_id', u.`fname`, u.`lname`, u.`avatar`, u.`gravatar`
  553. FROM `fcms_video` AS v
  554. LEFT JOIN `fcms_users` AS u ON v.`created_id` = u.`id`
  555. WHERE `active` = 1
  556. GROUP BY v.`created_id`
  557. ORDER BY v.`updated` DESC
  558. LIMIT 8";
  559. $result = mysql_query($sql);
  560. if (!$result)
  561. {
  562. displaySqlError($sql, mysql_error());
  563. displayFooter();
  564. return;
  565. }
  566. echo '
  567. <div><a href="?members=all">'.T_('Members').'</a></div>
  568. <ul id="small_video_users">';
  569. while ($row = mysql_fetch_assoc($result))
  570. {
  571. $name = cleanOutput($row['fname']).' '.cleanOutput($row['lname']);
  572. $avatarPath = getAvatarPath($row['avatar'], $row['gravatar']);
  573. echo '
  574. <li>
  575. <a href="?u='.$row['user_id'].'"><img src="'.$avatarPath.'" alt="'.$name.'"/></a>
  576. <a href="?u='.$row['user_id'].'">'.$name.'</a>
  577. <span>'.sprintf(T_ngettext('%d video', '%d videos', $row['count']), $row['count']).'</span>
  578. </li>';
  579. }
  580. echo '
  581. <li>
  582. <a href="?members=all">'.T_('See all members.').'</a>
  583. </li>
  584. </ul>
  585. </div>';
  586. displayFooter();
  587. }
  588. /**
  589. * displayVideoPage
  590. *
  591. * @return void
  592. */
  593. function displayVideoPage ()
  594. {
  595. $id = (int)$_GET['id'];
  596. $sql = "SELECT `id`, `source_id`, `title`, `description`, `height`, `width`, `created`, `created_id`
  597. FROM `fcms_video`
  598. WHERE `id` = '$id'
  599. AND `active` = 1";
  600. $result = mysql_query($sql);
  601. if (!$result)
  602. {
  603. displayHeader();
  604. displaySqlError($sql, mysql_error());
  605. displayFooter();
  606. return;
  607. }
  608. $video = mysql_fetch_assoc($result);
  609. // YouTube or Vimeo
  610. displayYouTubeVideoPage($video);
  611. }
  612. /**
  613. * displayYouTubeVideoPage
  614. *
  615. * @param array $video
  616. *
  617. * @return void
  618. */
  619. function displayYouTubeVideoPage ($video)
  620. {
  621. global $fcmsUser;
  622. // Save video id for ajax call
  623. $_SESSION['source_id'] = $video['source_id'];
  624. displayHeader();
  625. // Video not found in db
  626. if (!is_array($video))
  627. {
  628. echo '
  629. <div class="info-alert">
  630. <h2>'.T_('Missing or Invalid Video.').'</h2>
  631. <p>'.T_('This video cannot be found. Are you sure you typed in URL correctly?').'</p>
  632. </div>';
  633. displayFooter();
  634. return;
  635. }
  636. // Video upload failed
  637. elseif ($video['source_id'] == '0')
  638. {
  639. displayVideoNotFound($video, 'YouTube');
  640. return;
  641. }
  642. $youTubeService = new Zend_Gdata_YouTube();
  643. $status = null;
  644. // Get video entry
  645. try
  646. {
  647. $videoEntry = $youTubeService->getVideoEntry($video['source_id']);
  648. }
  649. catch (Exception $e)
  650. {
  651. $response = $e->getRawResponseBody();
  652. $private = stripos($response, 'Private video');
  653. $notFound = stripos($response, 'Video not found');
  654. // Video not found at YouTube
  655. if ($notFound !== false)
  656. {
  657. displayVideoNotFound($video, 'YouTube');
  658. return;
  659. }
  660. // Video is private
  661. elseif ($private !== false)
  662. {
  663. echo '
  664. <div class="error-alert">
  665. <p>'.T_('Sorry, this video is private.').'</p>
  666. <p>'.$e->getMessage().'</p>
  667. </div>';
  668. displayFooter();
  669. return;
  670. }
  671. else
  672. {
  673. echo '
  674. <div class="error-alert">
  675. <p>'.T_('Could not get video information.').'</p>
  676. <p>'.$e->getMessage().'</p>
  677. </div>';
  678. displayFooter();
  679. return;
  680. }
  681. }
  682. // Video is public/unlisted
  683. if ($status == null)
  684. {
  685. $status = getUploadStatus($video['source_id']);
  686. }
  687. $url = 'video.php?u='.$video['created_id'].'&amp;id='.$video['id'];
  688. // Is youtube processing finished?
  689. if ($status !== 'Finished')
  690. {
  691. echo '
  692. <div class="ok-alert">
  693. <p><b>'.T_('Your video was uploaded to YouTube successfully.').'</b></p>
  694. <p>'.T_('However it may take a few moments before you video is viewable. Please check back later.').'</p>
  695. <p id="js_msg"></p><br/>
  696. <p>'.T_('Current status: ').'<span id="current_status">'.$status.'</span></p>
  697. <p id="refresh"><a href="'.$url.'">'.T_('Refresh').'</a></p>
  698. </div>';
  699. displayFooter();
  700. return;
  701. }
  702. // Ajax is done at this point, we don't need the id anymore
  703. unset($_SESSION['source_id']);
  704. $videoUrl = 'http://www.youtube.com/e/'.$video['source_id'].'?version=3&enablejsapi=1&rel=0&wmode=transparent';
  705. displayVideoStartCode();
  706. echo '
  707. <div id="sections_menu">
  708. <ul>
  709. <li><a href="video.php">'.T_('Latest Videos').'</a></li>
  710. <li><a href="video.php?u='.$video['created_id'].'">'.getUserDisplayName($video['created_id'], 2).'</a></li>
  711. </ul>
  712. </div>';
  713. // Can you edit/delete this video?
  714. if ($video['created_id'] == $fcmsUser->id || checkAccess($fcmsUser->id) == 1)
  715. {
  716. echo '
  717. <div id="video_edit">
  718. <form action="'.$url.'" method="post">
  719. <div id="delete">
  720. <input type="hidden" id="id" name="id" value="'.$video['id'].'"/>
  721. <input type="hidden" id="source_id" name="source_id" value="'.$video['source_id'].'"/>
  722. <input class="btn" type="submit" id="remove_video" name="remove_video" value="'.T_('Remove Video').'"/>
  723. <label for="delete_youtube">'.T_('Delete from YouTube?').'</label>
  724. <input type="checkbox" id="delete_youtube" name="delete_youtube"/>
  725. </div>
  726. </form>
  727. </div>';
  728. }
  729. echo '
  730. <div id="video_desc">
  731. <img src="'.getCurrentAvatar($video['created_id']).'"/>
  732. <h2>'.cleanOutput($video['title']).'</h2>
  733. <p>'.cleanOutput($video['description']).'</p>
  734. </div>
  735. <div id="video_content">
  736. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'.$video['width'].'" height="'.$video['height'].'">
  737. <param name="movie" value="'.$videoUrl.'" />
  738. <param name="wmode" value="transparent"></param>
  739. <!--[if !IE]>-->
  740. <object type="application/x-shockwave-flash" data="'.$videoUrl.'" wmode="transparent" width="'.$video['width'].'" height="'.$video['height'].'">
  741. <!--<![endif]-->
  742. <div class="info-alert">
  743. '.T_('You need Flash player to view this video.').'<br/>
  744. <a href="http://www.adobe.com/go/getflashplayer">
  745. <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="'.T_('Get Adobe Flash player').'"/>
  746. </a>
  747. </div>
  748. <!--[if !IE]>-->
  749. </object>
  750. <!--<![endif]-->
  751. </object>
  752. </div>';
  753. echo '<p>'.T_('Views').': '.$videoEntry->getVideoViewCount().'</p>';
  754. $params = array(
  755. 'currentUserId' => $fcmsUser->id,
  756. 'id' => $video['id']
  757. );
  758. displayComments($url, 'video', $params);
  759. displayFooter();
  760. }
  761. /**
  762. * displayVideoStartCode
  763. *
  764. * @return void
  765. */
  766. function displayVideoStartCode ()
  767. {
  768. echo '
  769. <noscript>
  770. <style type="text/css">
  771. #video_content {display: none;}
  772. #noscript {padding:1em;}
  773. #noscript p {background-color:#ff9; padding:3em; font-size:130%; line-height:200%;}
  774. #noscript p span {font-size:60%;}
  775. </style>
  776. <div id="noscript">
  777. <p>
  778. '.T_('JavaScript must be enabled in order for you to use the Video Gallery. However, it seems JavaScript is either disabled or not supported by your browser.').'
  779. <br/><br/>
  780. '.T_('Please enable JavaScript by changing your browser options.').'
  781. </p>
  782. </div>
  783. </noscript>
  784. <script type="text/javascript" src="ttp://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>';
  785. }
  786. /**
  787. * getUploadStatus
  788. *
  789. * Check the upload status of a video. If the session token is provided
  790. * it's because the video is private and we need to auth to get the
  791. * status of the video.
  792. *
  793. * @param string $videoId
  794. * @param string $sessionToken
  795. *
  796. * @return string
  797. */
  798. function getUploadStatus ($videoId, $sessionToken = false)
  799. {
  800. $youtubeConfig = getYouTubeConfigData();
  801. $youTubeService = new Zend_Gdata_YouTube();
  802. if ($sessionToken !== false)
  803. {
  804. $httpClient = getAuthSubHttpClient($youtubeConfig['youtube_key'], $sessionToken);
  805. $youTubeService = new Zend_Gdata_YouTube($httpClient);
  806. if ($httpClient === false)
  807. {
  808. // Error message was already displayed by getAuthSubHttpClient()
  809. die();
  810. }
  811. }
  812. $videoEntry = $youTubeService->getVideoEntry($videoId);
  813. try
  814. {
  815. $control = $videoEntry->getControl();
  816. }
  817. catch (Exception $e)
  818. {
  819. return T_('Could not retrieve video status: ').$e->getMessage();
  820. }
  821. $message = 'Finished';
  822. if ($control instanceof Zend_Gdata_App_Extension_Control)
  823. {
  824. if (($control->getDraft() != null) && ($control->getDraft()->getText() == 'yes'))
  825. {
  826. $state = $videoEntry->getVideoState();
  827. if ($state instanceof Zend_Gdata_YouTube_Extension_State)
  828. {
  829. $message = $state->getName().' '.$state->getText();
  830. }
  831. else
  832. {
  833. return $message;
  834. }
  835. }
  836. }
  837. return $message;
  838. }
  839. /**
  840. * displayCommentSubmit
  841. *
  842. * @return void
  843. */
  844. function displayCommentSubmit ()
  845. {
  846. global $fcmsUser;
  847. $userId = (int)$_GET['u'];
  848. $videoId = escape_string($_GET['id']);
  849. $comments = strip_tags($_POST['comments']);
  850. $comments = escape_string($comments);
  851. if (!empty($comments))
  852. {
  853. $sql = "INSERT INTO `fcms_video_comment` (
  854. `video_id`, `comment`, `created`, `created_id`, `updated`, `updated_id`
  855. )
  856. VALUES (
  857. '$videoId',
  858. '$comments',
  859. NOW(),
  860. '$fcmsUser->id',
  861. NOW(),
  862. '$fcmsUser->id'
  863. )";
  864. if (!mysql_query($sql))
  865. {
  866. displayHeader();
  867. displaySqlError($sql, mysql_error());
  868. displayFooter();
  869. return;
  870. }
  871. }
  872. header("Location: video.php?u=$userId&id=$videoId#comments");
  873. }
  874. /**
  875. * getSessionToken
  876. *
  877. * Will return the session token for the given user.
  878. *
  879. * @param int userId
  880. *
  881. * @return string | false
  882. */
  883. function getSessionToken ($userId)
  884. {
  885. $userId = (int)$userId;
  886. $sql = "SELECT `youtube_session_token`
  887. FROM `fcms_user_settings`
  888. WHERE `user` = '$userId'
  889. AND `youtube_session_token` IS NOT NULL
  890. AND `youtube_session_token` != ''";
  891. $result = mysql_query($sql);
  892. if (!$result)
  893. {
  894. displaySqlError($sql, mysql_error());
  895. return false;
  896. }
  897. if (mysql_num_rows($result) <= 0)
  898. {
  899. echo '<p class="error-alert">'.T_('Could not find session token for user.').'</p>';
  900. return false;
  901. }
  902. $row = mysql_fetch_assoc($result);
  903. return $row['youtube_session_token'];
  904. }
  905. /**
  906. * displayMembersListPage
  907. *
  908. * @return void
  909. */
  910. function displayMembersListPage ()
  911. {
  912. displayHeader();
  913. $sql = "SELECT v.`id`, COUNT(*) AS 'count', v.`created_id` AS 'user_id', u.`fname`, u.`lname`, u.`avatar`, u.`gravatar`
  914. FROM `fcms_video` AS v
  915. LEFT JOIN `fcms_users` AS u ON v.`created_id` = u.`id`
  916. WHERE `active` = 1
  917. GROUP BY v.`created_id`
  918. ORDER BY v.`updated` DESC";
  919. $result = mysql_query($sql);
  920. if (!$result)
  921. {
  922. displaySqlError($sql, mysql_error());
  923. displayFooter();
  924. return;
  925. }
  926. echo '
  927. <div id="sections_menu">
  928. <ul>
  929. <li><a href="video.php">Latest Videos</a></li>
  930. </ul>
  931. </div>
  932. <ul id="large_video_users">';
  933. while ($row = mysql_fetch_assoc($result))
  934. {
  935. $name = cleanOutput($row['fname']).' '.cleanOutput($row['lname']);
  936. $avatarPath = getAvatarPath($row['avatar'], $row['gravatar']);
  937. echo '
  938. <li>
  939. <a href="?u='.$row['user_id'].'"><img src="'.$avatarPath.'" alt="'.$name.'"/></a><br/>
  940. <a href="?u='.$row['user_id'].'">'.$name.'</a>
  941. <span>'.sprintf(T_ngettext('%d video', '%d videos', $row['count']), $row['count']).'</span>
  942. </li>';
  943. }
  944. echo '
  945. </ul>
  946. </div>';
  947. displayFooter();
  948. }
  949. /**
  950. * displayUserVideosPage
  951. *
  952. * @return void
  953. */
  954. function displayUserVideosPage ()
  955. {
  956. global $fcmsUser;
  957. displayHeader();
  958. $userId = (int)$_GET['u'];
  959. if (isset($_SESSION['message']))
  960. {
  961. displayMessage($_SESSION['message']);
  962. }
  963. // Get user info
  964. $sql = "SELECT 'id', `fname`, `lname`, `avatar`, `gravatar`
  965. FROM `fcms_users`
  966. WHERE `id` = '$userId'";
  967. $result = mysql_query($sql);
  968. if (!$result)
  969. {
  970. displaySqlError($sql, mysql_error());
  971. displayFooter();
  972. return;
  973. }
  974. if (mysql_num_rows($result) <= 0)
  975. {
  976. echo '<div class="error-alert">'.T_('Member not found.').'</div>';
  977. displayFooter();
  978. return;
  979. }
  980. $row = mysql_fetch_assoc($result);
  981. $name = cleanOutput($row['fname']).' '.cleanOutput($row['lname']);
  982. $avatarPath = getAvatarPath($row['avatar'], $row['gravatar']);
  983. echo '
  984. <div id="sections_menu">
  985. <ul>
  986. <li><a href="video.php">Latest Videos</a></li>
  987. <li><a href="video.php?members=all">Members</a></li>
  988. </ul>
  989. </div>
  990. <div id="video_content">
  991. <div id="member">
  992. <img src="'.$avatarPath.'" titl="'.$name.'"/>
  993. <span>'.T_('Videos For:').'</span>
  994. <h2>'.$name.'</h2>
  995. </div>
  996. <ul class="categories>';
  997. // Get videos
  998. $sql = "SELECT `id`, `source_id`, `title`, `active`, `created`, `created_id`
  999. FROM `fcms_video`
  1000. WHERE `created_id` = '$userId'
  1001. ORDER BY `updated` DESC";
  1002. $result = mysql_query($sql);
  1003. if (!$result)
  1004. {
  1005. displaySqlError($sql, mysql_error());
  1006. displayFooter();
  1007. return;
  1008. }
  1009. if (mysql_num_rows($result) <= 0)
  1010. {
  1011. echo '<div class="error-alert">'.T_('No videos found.').'</div>';
  1012. displayFooter();
  1013. return;
  1014. }
  1015. while ($row = mysql_fetch_assoc($result))
  1016. {
  1017. $class = '';
  1018. if ($row['active'] == '0')
  1019. {
  1020. if ($row['created_id'] != $fcmsUser->id)
  1021. {
  1022. continue;
  1023. }
  1024. $class = 'removed';
  1025. }
  1026. $date = fixDate('Y-m-d', '', $row['created'], $fcmsUser->id);
  1027. echo '
  1028. <li class="category '.$class.'">
  1029. <a href="?u='.$userId.'&amp;id='.$row['id'].'"><img src="http://i.ytimg.com/vi/'.$row['source_id'].'/default.jpg"/></a>
  1030. <span>
  1031. <strong>'.cleanOutput($row['title']).'</strong>
  1032. <i>'.sprintf(T_pgettext('%s is a date', 'on %s'), $date).'</i>
  1033. </span>
  1034. </li>';
  1035. }
  1036. displayFooter();
  1037. }
  1038. /**
  1039. * displayRemoveVideoSubmit
  1040. *
  1041. * Remove video doesn't actually physically delete the video from FCMS, it
  1042. * just sets the video to in-active in the DB, which removes it from view.
  1043. *
  1044. * We don't want to delete these entries from the db, because the cron importer
  1045. * will just continue to import them.
  1046. *
  1047. * @return void
  1048. */
  1049. function displayRemoveVideoSubmit ()
  1050. {
  1051. global $fcmsUser;
  1052. if (!isset($_POST['id']) || !isset($_POST['source_id']))
  1053. {
  1054. displayHeader();
  1055. echo '<div class="error_alert">'.T_('Can\'t remove video. Missing video id.').'</div>';
  1056. displayFooter();
  1057. return;
  1058. }
  1059. $userId = (int)$_GET['u'];
  1060. $id = (int)$_POST['id'];
  1061. $sourceId = $_POST['source_id'];
  1062. $sql = "UPDATE `fcms_video`
  1063. SET `active` = 0,
  1064. `updated` = NOW(),
  1065. `updated_id` = '$fcmsUser->id'
  1066. WHERE `id` = '$id'";
  1067. if (!mysql_query($sql))
  1068. {
  1069. displayFooter();
  1070. displaySqlError($sql, mysql_error());
  1071. displayFooter();
  1072. return;
  1073. }
  1074. if (isset($_POST['delete_youtube']))
  1075. {
  1076. $sessionToken = getSessionToken($fcmsUser->id);
  1077. $youtubeConfig = getYouTubeConfigData();
  1078. $httpClient = getAuthSubHttpClient($youtubeConfig['youtube_key'], $sessionToken);
  1079. if ($httpClient === false)
  1080. {
  1081. // Error message was already displayed by getAuthSubHttpClient()
  1082. displayFooter();
  1083. return;
  1084. }
  1085. $youTubeService = new Zend_Gdata_YouTube($httpClient);
  1086. $videoEntry = $youTubeService->getVideoEntry($sourceId);
  1087. // Set message
  1088. $_SESSION['message'] = 'delete_video_youtube';
  1089. $youTubeService->delete($videoEntry);
  1090. }
  1091. // Set message
  1092. if (!isset($_SESSION['message']))
  1093. {
  1094. $_SESSION['message'] = 'remove_video';
  1095. }
  1096. // Send back to user's video listing
  1097. header("Location: video.php?u=$userId");
  1098. }
  1099. /**
  1100. * displayDeleteVideoSubmit
  1101. *
  1102. * Will delete the video entry from the FCMS db. This is done when the video
  1103. * at YouTube or Vimeo has been removed.
  1104. *
  1105. * @return void
  1106. */
  1107. function displayDeleteVideoSubmit ()
  1108. {
  1109. global $fcmsUser;
  1110. if (!isset($_POST['id']) || !isset($_POST['source_id']))
  1111. {
  1112. displayHeader();
  1113. echo '<div class="error_alert">'.T_('Can\'t delete video. Missing video id.').'</div>';
  1114. displayFooter();
  1115. return;
  1116. }
  1117. $userId = (int)$_GET['u'];
  1118. $id = (int)$_POST['id'];
  1119. $sourceId = $_POST['source_id'];
  1120. $sql = "DELETE FROM `fcms_video_comment`
  1121. WHERE `video_id` = '$id'";
  1122. if (!mysql_query($sql))
  1123. {
  1124. displayHeader();
  1125. displaySqlError($sql, mysql_error());
  1126. displayFooter();
  1127. return;
  1128. }
  1129. $sql = "DELETE FROM `fcms_video`
  1130. WHERE `id` = '$id'";
  1131. if (!mysql_query($sql))
  1132. {
  1133. displayHeader();
  1134. displaySqlError($sql, mysql_error());
  1135. displayFooter();
  1136. return;
  1137. }
  1138. // Set message
  1139. $_SESSION['message'] = 'delete_video';
  1140. // Send back to user's video listing
  1141. header("Location: video.php?u=$userId");
  1142. }
  1143. /**
  1144. * displayMessage
  1145. *
  1146. * @param string $message
  1147. *
  1148. * @return void
  1149. */
  1150. function displayMessage ($message)
  1151. {
  1152. unset($_SESSION['message']);
  1153. switch ($message)
  1154. {
  1155. case 'remove_video':
  1156. displayOkMessage(T_('Video removed successfully.'));
  1157. break;
  1158. case 'delete_video':
  1159. displayOkMessage(T_('Video deleted successfully.'));
  1160. break;
  1161. case 'delete_video_youtube':
  1162. displayOkMessage(T_('Video removed and deleted from YouTube successfully.'), '5000');
  1163. }
  1164. }
  1165. /**
  1166. * displayVideoNotFound
  1167. *
  1168. * @param array $video
  1169. * @param string $source
  1170. *
  1171. * @return void
  1172. */
  1173. function displayVideoNotFound ($video, $source)
  1174. {
  1175. $userId = (int)$_GET['u'];
  1176. $videoId = (int)$video['id'];
  1177. $url = 'video.php?u='.$userId.'&amp;id='.$videoId;
  1178. echo '
  1179. <div class="info-alert">
  1180. <h2>'.T_('Source Video cannot be found.').'</h2><br/>
  1181. <p>'.sprintf(T_('The video file for this video could not be found at %s.'), $source).'</p>
  1182. <p>'.T_('Would you like to delete this video?').'</p>
  1183. <form action="'.$url.'" method="post">
  1184. <input type="hidden" id="id" name="id" value="'.$video['id'].'"/>
  1185. <input type="hidden" id="source_id" name="source_id" value="'.$video['source_id'].'"/>
  1186. <input class="sub1" type="submit" id="delete_video" name="delete_video" value="'.T_('Yes').'"/>
  1187. &nbsp; &nbsp; '.T_('or').' &nbsp; &nbsp;
  1188. <a href="video.php">'.T_('No').'</a>
  1189. </form>
  1190. </div>';
  1191. }