/** * displayFormPage * * Displays the form for configuring a youtube app. * * @return void */ function displayFormPage() { global $fcmsUser; $this->displayHeader(); if (isset($_SESSION['success'])) { echo ' <div class="alert-message success"> <a class="close" href="#" onclick="$(this).up(\'div\').hide(); return false;">×</a> ' . T_('Changes Updated Successfully') . ' </div>'; unset($_SESSION['success']); } $r = getYouTubeConfigData(); $key = isset($r['youtube_key']) ? cleanOutput($r['youtube_key']) : ''; echo ' <div class="alert-message block-message info"> <h1>' . T_('YouTube Integration') . '</h1> <p> ' . T_('In order to integrate Family Connections with YouTube, you must get a Developer Key from Google, and provide that Key to Family Connections.') . ' </p> </div>'; if (empty($key)) { echo ' <div class="row"> <div class="span4"> <h2>' . T_('Step 1') . '</h2> <p> ' . T_('Got to Google and create a new YouTube Application.') . ' </p> </div> <div class="span12"> <h3> <a href="http://code.google.com/apis/youtube/dashboard/">' . T_('Create Youtube Application') . '</a><br/> </h3> </div><!-- /span12 --> </div><!-- /row --> <p> </p> <p> </p> <p> </p> <div class="row"> <div class="span4"> <h2>' . T_('Step 2') . '</h2> <p> ' . T_('Fill out the form below with the YouTube Developer Key provided by Google.') . ' </p> </div> <div class="span12">'; } echo ' <form method="post" action="youtube.php"> <fieldset> <legend>' . T_('YouTube') . '</legend> <div class="clearfix"> <label for="key">' . T_('Developer Key') . '</label> <div class="input"> <input class="span6" type="text" name="key" id="key" value="' . $key . '"/> </div> </div> <div class="actions"> <input class="btn primary" type="submit" name="submit" value="' . T_('Save') . '"/> </div> </fieldset> </form>'; if (empty($key)) { echo ' </div><!-- /span12 --> </div><!-- /row -->'; } $this->displayFooter(); }
/** * displayRemoveVideoSubmit * * Remove video doesn't actually physically delete the video from FCMS, it * just sets the video to in-active in the DB, which removes it from view. * * We don't want to delete these entries from the db, because the cron importer * will just continue to import them. * * @return void */ function displayRemoveVideoSubmit() { if (!isset($_POST['id']) || !isset($_POST['source_id'])) { $this->displayHeader(); echo '<div class="error_alert">' . T_('Can\'t remove video. Missing video id.') . '</div>'; $this->displayFooter(); return; } $userId = (int) $_GET['u']; $id = (int) $_POST['id']; $sourceId = $_POST['source_id']; $sql = "UPDATE `fcms_video`\n SET `active` = 0,\n `updated` = NOW(),\n `updated_id` = ?\n WHERE `id` = ?"; if (!$this->fcmsDatabase->update($sql, array($this->fcmsUser->id, $id))) { $this->displayFooter(); $this->fcmsError->displayError(); $this->displayFooter(); return; } if (isset($_POST['delete_youtube'])) { $sessionToken = $this->getSessionToken($this->fcmsUser->id); $youtubeConfig = getYouTubeConfigData(); $httpClient = getYouTubeAuthSubHttpClient($youtubeConfig['youtube_key'], $sessionToken); if ($httpClient === false) { // Error message was already displayed by getYouTubeAuthSubHttpClient() $this->displayFooter(); return; } $youTubeService = new Zend_Gdata_YouTube($httpClient); $videoEntry = $youTubeService->getVideoEntry($sourceId); // Set message $_SESSION['message'] = 'delete_video_youtube'; $youTubeService->delete($videoEntry); } // Set message if (!isset($_SESSION['message'])) { $_SESSION['message'] = 'remove_video'; } // Send back to user's video listing header("Location: video.php?u={$userId}"); }
/** * runYouTubeJob * * Imports YouTube videos. * * @return void */ function runYouTubeJob() { global $file; require_once 'constants.php'; require_once 'socialmedia.php'; require_once 'datetime.php'; require_once THIRDPARTY . 'gettext.inc'; set_include_path(THIRDPARTY); require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Gdata_YouTube'); Zend_Loader::loadClass('Zend_Gdata_AuthSub'); Zend_Loader::loadClass('Zend_Gdata_App_Exception'); $fcmsError = FCMS_Error::getInstance(); $fcmsDatabase = Database::getInstance($fcmsError); $existingIds = getExistingYouTubeIds(); // Get user's session tokens $sql = "SELECT u.`id`, s.`youtube_session_token`\n FROM `fcms_user_settings` AS s, `fcms_users` AS u\n WHERE s.`user` = u.`id`\n AND s.`youtube_session_token` IS NOT NULL"; $rows = $fcmsDatabase->getRows($sql); if ($rows === false) { logError(__FILE__ . ' [' . __LINE__ . '] - Could not get youtube tokens.'); die; } $sessionTokens = array(); foreach ($rows as $row) { $sessionTokens[$row['id']] = $row['youtube_session_token']; } $youtubeConfig = getYouTubeConfigData(); // Get videos for each user foreach ($sessionTokens as $userId => $token) { // Setup youtube api $httpClient = getYouTubeAuthSubHttpClient($youtubeConfig['youtube_key'], $token); $youTubeService = new Zend_Gdata_YouTube($httpClient); $feed = $youTubeService->getUserUploads('default'); $values = ''; $videoCount = 0; $params = array(); foreach ($feed as $entry) { $id = $entry->getVideoId(); if (isset($existingIds[$id])) { continue; } $title = htmlspecialchars($entry->getVideoTitle()); $description = htmlspecialchars($entry->getVideoDescription()); $created = formatDate('Y-m-d H:i:s', $entry->published); $duration = $entry->getVideoDuration(); $height = '420'; $width = '780'; $thumbs = $entry->getVideoThumbnails(); if (count($thumbs) > 0) { $height = $thumbs[0]['height']; $width = $thumbs[0]['width']; } $values .= "(?, ?, ?, 'youtube', ?, ?, ?, ?, NOW(), ?),"; $params[] = $id; $params[] = $title; $params[] = $description; $params[] = $height; $params[] = $width; $params[] = $created; $params[] = $userId; $params[] = $userId; $videoCount++; } if ($videoCount > 0) { $values = substr($values, 0, -1); // remove comma $sql = "INSERT INTO `fcms_video`\n (`source_id`, `title`, `description`, `source`, `height`, `width`, `created`, `created_id`, `updated`, `updated_id`)\n VALUES {$values}"; if (!$fcmsDatabase->insert($sql, $params)) { logError(__FILE__ . ' [' . __LINE__ . '] - Could not insert new video to db.'); die; } } } // Update date we last ran this job updateLastRun(date('Y-m-d H:i:s'), 'youtube'); }
/** * displayEditYouTubeSubmit * * @return void */ function displayEditYouTubeSubmit() { $data = getYouTubeConfigData(); $singleUseToken = $_GET['token']; if (!empty($data['youtube_key'])) { // Exchange single use token for a session token try { $sessionToken = Zend_Gdata_AuthSub::getAuthSubSessionToken($singleUseToken); } catch (Zend_Gdata_App_Exception $e) { $this->displayHeader(); echo ' <div class="error-alert">ERROR - Token upgrade for [' . $singleUseToken . '] failed: ' . $e->getMessage(); $this->displayFooter(); return; } $_SESSION['sessionToken'] = $sessionToken; $sql = "UPDATE `fcms_user_settings`\n SET `youtube_session_token` = ?\n WHERE `user` = ?"; $params = array($sessionToken, $this->fcmsUser->id); if (!$this->fcmsDatabase->update($sql, $params)) { $this->displayHeader(); $this->fcmsError->displayError(); $this->displayFooter(); return; } } else { $this->displayHeader(); echo ' <div class="info-alert"> <h2>' . T_('YouTube isn\'t Configured Yet.') . '</h2> <p>' . T_('Unfortunately, your website administrator has not set up YouTube yet.') . '</p> </div>'; $this->displayFooter(); return; } header("Location: settings.php?view=youtube"); }