From 06f945f27840b53e57795dadbc38e76f7e11ab1c Mon Sep 17 00:00:00 2001
From: Horus3
Date: Mon, 24 Feb 2014 16:42:14 +0100
Subject: init
---
.../Zend/Gdata/YouTubeVideoApp/operations.php | 1097 ++++++++++++++++++++
1 file changed, 1097 insertions(+)
create mode 100755 zend/demos/Zend/Gdata/YouTubeVideoApp/operations.php
(limited to 'zend/demos/Zend/Gdata/YouTubeVideoApp/operations.php')
diff --git a/zend/demos/Zend/Gdata/YouTubeVideoApp/operations.php b/zend/demos/Zend/Gdata/YouTubeVideoApp/operations.php
new file mode 100755
index 0000000..c149583
--- /dev/null
+++ b/zend/demos/Zend/Gdata/YouTubeVideoApp/operations.php
@@ -0,0 +1,1097 @@
+= 5.2.11
+ * This sample is run from within a web browser. These files are required:
+ * session_details.php - a script to view log output and session variables
+ * operations.php - the main logic, which interfaces with the YouTube API
+ * index.php - the HTML to represent the web UI, contains some PHP
+ * video_app.css - the CSS to define the interface style
+ * video_app.js - the JavaScript used to provide the video list AJAX interface
+ *
+ * NOTE: If using in production, some additional precautions with regards
+ * to filtering the input data should be used. This code is designed only
+ * for demonstration purposes.
+ */
+require_once 'Zend/Loader.php';
+Zend_Loader::loadClass('Zend_Gdata_YouTube');
+Zend_Loader::loadClass('Zend_Gdata_AuthSub');
+Zend_Loader::loadClass('Zend_Gdata_App_Exception');
+
+/*
+ * The main controller logic.
+ *
+ * POST used for all authenticated requests
+ * otherwise use GET for retrieve and supplementary values
+ */
+session_start();
+setLogging('on');
+generateUrlInformation();
+
+if (!isset($_POST['operation'])) {
+ // if a GET variable is set then process the token upgrade
+ if (isset($_GET['token'])) {
+ updateAuthSubToken($_GET['token']);
+ } else {
+ if (loggingEnabled()) {
+ logMessage('reached operations.php without $_POST or $_GET variables set', 'error');
+ header('Location: index.php');
+ }
+ }
+}
+
+$operation = $_POST['operation'];
+
+switch ($operation) {
+
+ case 'create_upload_form':
+ createUploadForm($_POST['videoTitle'],
+ $_POST['videoDescription'],
+ $_POST['videoCategory'],
+ $_POST['videoTags']);
+ break;
+
+ case 'edit_meta_data':
+ editVideoData($_POST['newVideoTitle'],
+ $_POST['newVideoDescription'],
+ $_POST['newVideoCategory'],
+ $_POST['newVideoTags'],
+ $_POST['videoId']);
+ break;
+
+ case 'check_upload_status':
+ checkUpload($_POST['videoId']);
+ break;
+
+ case 'delete_video':
+ deleteVideo($_POST['videoId']);
+ break;
+
+ case 'auth_sub_request':
+ generateAuthSubRequestLink();
+ break;
+
+ case 'auth_sub_token_upgrade':
+ updateAuthSubToken($_GET['token']);
+ break;
+
+ case 'clear_session_var':
+ clearSessionVar($_POST['name']);
+ break;
+
+ case 'retrieve_playlists':
+ retrievePlaylists();
+ break;
+
+ case 'create_playlist':
+ createPlaylist($_POST['playlistTitle'], $_POST['playlistDescription']);
+ break;
+
+ case 'delete_playlist':
+ deletePlaylist($_POST['playlistTitle']);
+ break;
+
+ case 'update_playlist':
+ updatePlaylist($_POST['newPlaylistTitle'],
+ $_POST['newPlaylistDescription'],
+ $_POST['oldPlaylistTitle']);
+ break;
+
+ case (strcmp(substr($operation, 0, 7), 'search_') == 0):
+ // initialize search specific information
+ $searchType = substr($operation, 7);
+ searchVideos($searchType, $_POST['searchTerm'], $_POST['startIndex'],
+ $_POST['maxResults']);
+ break;
+
+ case 'show_video':
+ echoVideoPlayer($_POST['videoId']);
+ break;
+
+ default:
+ unsupportedOperation($_POST);
+ break;
+}
+
+/**
+ * Perform a search on youtube. Passes the result feed to echoVideoList.
+ *
+ * @param string $searchType The type of search to perform.
+ * If set to 'owner' then attempt to authenticate.
+ * @param string $searchTerm The term to search on.
+ * @param string $startIndex Start retrieving search results from this index.
+ * @param string $maxResults The number of results to retrieve.
+ * @return void
+ */
+function searchVideos($searchType, $searchTerm, $startIndex, $maxResults)
+{
+ // create an unauthenticated service object
+ $youTubeService = new Zend_Gdata_YouTube();
+ $query = $youTubeService->newVideoQuery();
+ $query->setQuery($searchTerm);
+ $query->setStartIndex($startIndex);
+ $query->setMaxResults($maxResults);
+
+ switch ($searchType) {
+ case 'most_viewed':
+ $query->setFeedType('most viewed');
+ $query->setTime('this_week');
+ $feed = $youTubeService->getVideoFeed($query);
+ break;
+ case 'most_recent':
+ $query->setFeedType('most recent');
+ $query->setTime('this_week');
+ $feed = $youTubeService->getVideoFeed($query);
+ break;
+ case 'recently_featured':
+ $query->setFeedType('recently featured');
+ $feed = $youTubeService->getVideoFeed($query);
+ break;
+ case 'top_rated':
+ $query->setFeedType('top rated');
+ $query->setTime('this_week');
+ $feed = $youTubeService->getVideoFeed($query);
+ break;
+ case 'username':
+ $feed = $youTubeService->getUserUploads($searchTerm);
+ break;
+ case 'all':
+ $feed = $youTubeService->getVideoFeed($query);
+ break;
+ case 'owner':
+ $httpClient = getAuthSubHttpClient();
+ $youTubeService = new Zend_Gdata_YouTube($httpClient);
+ try {
+ $feed = $youTubeService->getUserUploads('default');
+ if (loggingEnabled()) {
+ logMessage($httpClient->getLastRequest(), 'request');
+ logMessage($httpClient->getLastResponse()->getBody(),
+ 'response');
+ }
+ } catch (Zend_Gdata_App_HttpException $httpException) {
+ print 'ERROR ' . $httpException->getMessage()
+ . ' HTTP details
'
+ . ''
+ . 'click here to view details of last request
';
+ return;
+ } catch (Zend_Gdata_App_Exception $e) {
+ print 'ERROR - Could not retrieve users video feed: '
+ . $e->getMessage() . '
';
+ return;
+ }
+ echoVideoList($feed, true);
+ return;
+
+ default:
+ echo 'ERROR - Unknown search type - \'' . $searchType . '\'';
+ return;
+ }
+
+ if (loggingEnabled()) {
+ $httpClient = $youTubeService->getHttpClient();
+ logMessage($httpClient->getLastRequest(), 'request');
+ logMessage($httpClient->getLastResponse()->getBody(), 'response');
+ }
+ echoVideoList($feed);
+}
+
+/**
+ * Finds the URL for the flash representation of the specified video.
+ *
+ * @param Zend_Gdata_YouTube_VideoEntry $entry The video entry
+ * @return (string|null) The URL or null, if the URL is not found
+ */
+function findFlashUrl($entry)
+{
+ foreach ($entry->mediaGroup->content as $content) {
+ if ($content->type === 'application/x-shockwave-flash') {
+ return $content->url;
+ }
+ }
+ return null;
+}
+
+/**
+ * Check the upload status of a video
+ *
+ * @param string $videoId The video to check.
+ * @return string A message about the video's status.
+ */
+function checkUpload($videoId)
+{
+ $httpClient = getAuthSubHttpClient();
+ $youTubeService = new Zend_Gdata_YouTube($httpClient);
+
+ $feed = $youTubeService->getuserUploads('default');
+ $message = 'No further status information available yet.';
+
+ foreach($feed as $videoEntry) {
+ if ($videoEntry->getVideoId() == $videoId) {
+ // check if video is in draft status
+ try {
+ $control = $videoEntry->getControl();
+ } catch (Zend_Gdata_App_Exception $e) {
+ print 'ERROR - not able to retrieve control element '
+ . $e->getMessage();
+ return;
+ }
+
+ if ($control instanceof Zend_Gdata_App_Extension_Control) {
+ if (($control->getDraft() != null) &&
+ ($control->getDraft()->getText() == 'yes')) {
+ $state = $videoEntry->getVideoState();
+ if ($state instanceof Zend_Gdata_YouTube_Extension_State) {
+ $message = 'Upload status: ' . $state->getName() . ' '
+ . $state->getText();
+ } else {
+ print $message;
+ }
+ }
+ }
+ }
+ }
+ print $message;
+}
+
+/**
+ * Store location of the demo application into session variables.
+ *
+ * @return void
+ */
+function generateUrlInformation()
+{
+ if (!isset($_SESSION['operationsUrl']) || !isset($_SESSION['homeUrl'])) {
+ $_SESSION['operationsUrl'] = 'http://'. $_SERVER['HTTP_HOST']
+ . $_SERVER['PHP_SELF'];
+ $path = explode('/', $_SERVER['PHP_SELF']);
+ $path[count($path)-1] = 'index.php';
+ $_SESSION['homeUrl'] = 'http://'. $_SERVER['HTTP_HOST']
+ . implode('/', $path);
+ }
+}
+
+/**
+ * Log a message to the session variable array.
+ *
+ * @param string $message The message to log.
+ * @param string $messageType The type of message to log.
+ * @return void
+ */
+function logMessage($message, $messageType)
+{
+ if (!isset($_SESSION['log_maxLogEntries'])) {
+ $_SESSION['log_maxLogEntries'] = 20;
+ }
+
+ if (!isset($_SESSION['log_currentCounter'])) {
+ $_SESSION['log_currentCounter'] = 0;
+ }
+
+ $currentCounter = $_SESSION['log_currentCounter'];
+ $currentCounter++;
+
+ if ($currentCounter > $_SESSION['log_maxLogEntries']) {
+ $_SESSION['log_currentCounter'] = 0;
+ }
+
+ $logLocation = 'log_entry_'. $currentCounter . '_' . $messageType;
+ $_SESSION[$logLocation] = $message;
+ $_SESSION['log_currentCounter'] = $currentCounter;
+}
+
+/**
+ * Update an existing video's meta-data.
+ *
+ * @param string $newVideoTitle The new title for the video entry.
+ * @param string $newVideoDescription The new description for the video entry.
+ * @param string $newVideoCategory The new category for the video entry.
+ * @param string $newVideoTags The new set of tags for the video entry (whitespace separated).
+ * @param string $videoId The video id for the video to be edited.
+ * @return void
+ */
+function editVideoData($newVideoTitle, $newVideoDescription, $newVideoCategory, $newVideoTags, $videoId)
+{
+ $httpClient = getAuthSubHttpClient();
+ $youTubeService = new Zend_Gdata_YouTube($httpClient);
+ $feed = $youTubeService->getVideoFeed('https://gdata.youtube.com/feeds/users/default/uploads');
+ $videoEntryToUpdate = null;
+
+ foreach($feed as $entry) {
+ if ($entry->getVideoId() == $videoId) {
+ $videoEntryToUpdate = $entry;
+ break;
+ }
+ }
+
+ if (!$videoEntryToUpdate instanceof Zend_Gdata_YouTube_VideoEntry) {
+ print 'ERROR - Could not find a video entry with id ' . $videoId
+ . '
' . printCacheWarning();
+ return;
+ }
+
+ try {
+ $putUrl = $videoEntryToUpdate->getEditLink()->getHref();
+ } catch (Zend_Gdata_App_Exception $e) {
+ print 'ERROR - Could not obtain video entry\'s edit link: '
+ . $e->getMessage() . '
';
+ return;
+ }
+
+ $videoEntryToUpdate->setVideoTitle($newVideoTitle);
+ $videoEntryToUpdate->setVideoDescription($newVideoDescription);
+ $videoEntryToUpdate->setVideoCategory($newVideoCategory);
+
+ // convert tags from space separated to comma separated
+ $videoTagsArray = explode(' ', trim($newVideoTags));
+
+ // strip out empty array elements
+ foreach($videoTagsArray as $key => $value) {
+ if (strlen($value) < 2) {
+ unset($videoTagsArray[$key]);
+ }
+ }
+
+ $videoEntryToUpdate->setVideoTags(implode(', ', $videoTagsArray));
+
+ try {
+ $updatedEntry = $youTubeService->updateEntry($videoEntryToUpdate, $putUrl);
+ if (loggingEnabled()) {
+ logMessage($httpClient->getLastRequest(), 'request');
+ logMessage($httpClient->getLastResponse()->getBody(), 'response');
+ }
+ } catch (Zend_Gdata_App_HttpException $httpException) {
+ print 'ERROR ' . $httpException->getMessage()
+ . ' HTTP details
'
+ . ''
+ . 'click here to view details of last request
';
+ return;
+ } catch (Zend_Gdata_App_Exception $e) {
+ print 'ERROR - Could not post video meta-data: ' . $e->getMessage();
+ return;
+ }
+ print 'Entry updated successfully.
'
+ . '(refresh your video listing)
'
+ . printCacheWarning();
+}
+
+/**
+ * Create upload form by sending the incoming video meta-data to youtube and
+ * retrieving a new entry. Prints form HTML to page.
+ *
+ * @param string $VideoTitle The title for the video entry.
+ * @param string $VideoDescription The description for the video entry.
+ * @param string $VideoCategory The category for the video entry.
+ * @param string $VideoTags The set of tags for the video entry (whitespace separated).
+ * @param string $nextUrl (optional) The URL to redirect back to after form upload has completed.
+ * @return void
+ */
+function createUploadForm($videoTitle, $videoDescription, $videoCategory, $videoTags, $nextUrl = null)
+{
+ $httpClient = getAuthSubHttpClient();
+ $youTubeService = new Zend_Gdata_YouTube($httpClient);
+ $newVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
+
+ $newVideoEntry->setVideoTitle($videoTitle);
+ $newVideoEntry->setVideoDescription($videoDescription);
+
+ //make sure first character in category is capitalized
+ $videoCategory = strtoupper(substr($videoCategory, 0, 1))
+ . substr($videoCategory, 1);
+ $newVideoEntry->setVideoCategory($videoCategory);
+
+ // convert videoTags from whitespace separated into comma separated
+ $videoTagsArray = explode(' ', trim($videoTags));
+ $newVideoEntry->setVideoTags(implode(', ', $videoTagsArray));
+
+ $tokenHandlerUrl = 'https://gdata.youtube.com/action/GetUploadToken';
+ try {
+ $tokenArray = $youTubeService->getFormUploadToken($newVideoEntry, $tokenHandlerUrl);
+ if (loggingEnabled()) {
+ logMessage($httpClient->getLastRequest(), 'request');
+ logMessage($httpClient->getLastResponse()->getBody(), 'response');
+ }
+ } catch (Zend_Gdata_App_HttpException $httpException) {
+ print 'ERROR ' . $httpException->getMessage()
+ . ' HTTP details
'
+ . ''
+ . 'click here to view details of last request
';
+ return;
+ } catch (Zend_Gdata_App_Exception $e) {
+ print 'ERROR - Could not retrieve token for syndicated upload. '
+ . $e->getMessage()
+ . '
'
+ . 'click here to view details of last request
';
+ return;
+ }
+
+ $tokenValue = $tokenArray['token'];
+ $postUrl = $tokenArray['url'];
+
+ // place to redirect user after upload
+ if (!$nextUrl) {
+ $nextUrl = $_SESSION['homeUrl'];
+ }
+
+ print <<< END
+
| '. stripslashes($videoTitle) . ''
+ . ' ' + . stripslashes($videoDescription) . ' ' + . 'category: ' . $videoCategory + . ' tagged: ' + . htmlspecialchars(implode(', ', $videoTags)) . ' '; + + if ($authenticated) { + $table .= '' + . 'edit video data | ' + . 'delete this video '; + } + + $table .= ' |
'
+ . 'Please note that the change may not be reflected in the API '
+ . 'immediately due to caching.
'
+ . 'Please refer to the API documentation for more details.