From 06f945f27840b53e57795dadbc38e76f7e11ab1c Mon Sep 17 00:00:00 2001 From: Horus3 Date: Mon, 24 Feb 2014 16:42:14 +0100 Subject: init --- zend/demos/Zend/Gdata/Photos.php | 904 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 904 insertions(+) create mode 100755 zend/demos/Zend/Gdata/Photos.php (limited to 'zend/demos/Zend/Gdata/Photos.php') diff --git a/zend/demos/Zend/Gdata/Photos.php b/zend/demos/Zend/Gdata/Photos.php new file mode 100755 index 0000000..4c3e257 --- /dev/null +++ b/zend/demos/Zend/Gdata/Photos.php @@ -0,0 +1,904 @@ += 5.2.11 + * + * You can run this sample from a web browser. + * + * NOTE: You must ensure that Zend Framework is in your PHP include + * path. You can do this via php.ini settings, or by modifying the + * argument to set_include_path in the code below. + * + * NOTE: As this is sample code, not all of the functions do full error + * handling. + */ + +/** + * @see Zend_Loader + */ +require_once 'Zend/Loader.php'; + +/** + * @see Zend_Gdata + */ +Zend_Loader::loadClass('Zend_Gdata'); + +/** + * @see Zend_Gdata_AuthSub + */ +Zend_Loader::loadClass('Zend_Gdata_AuthSub'); + +/** + * @see Zend_Gdata_Photos + */ +Zend_Loader::loadClass('Zend_Gdata_Photos'); + +/** + * @see Zend_Gdata_Photos_UserQuery + */ +Zend_Loader::loadClass('Zend_Gdata_Photos_UserQuery'); + +/** + * @see Zend_Gdata_Photos_AlbumQuery + */ +Zend_Loader::loadClass('Zend_Gdata_Photos_AlbumQuery'); + +/** + * @see Zend_Gdata_Photos_PhotoQuery + */ +Zend_Loader::loadClass('Zend_Gdata_Photos_PhotoQuery'); + +/** + * @see Zend_Gdata_App_Extension_Category + */ +Zend_Loader::loadClass('Zend_Gdata_App_Extension_Category'); + +session_start(); + + +/** + * Adds a new photo to the specified album + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param integer $albumId The album's id + * @param array $photo The uploaded photo + * @return void + */ +function addPhoto($client, $user, $albumId, $photo) +{ + $photos = new Zend_Gdata_Photos($client); + + $fd = $photos->newMediaFileSource($photo["tmp_name"]); + $fd->setContentType($photo["type"]); + + $entry = new Zend_Gdata_Photos_PhotoEntry(); + $entry->setMediaSource($fd); + $entry->setTitle($photos->newTitle($photo["name"])); + + $albumQuery = new Zend_Gdata_Photos_AlbumQuery; + $albumQuery->setUser($user); + $albumQuery->setAlbumId($albumId); + + $albumEntry = $photos->getAlbumEntry($albumQuery); + + $result = $photos->insertPhotoEntry($entry, $albumEntry); + if ($result) { + outputAlbumFeed($client, $user, $albumId); + } else { + echo "There was an issue with the file upload."; + } +} + +/** + * Deletes the specified photo + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param integer $albumId The album's id + * @param integer $photoId The photo's id + * @return void + */ +function deletePhoto($client, $user, $albumId, $photoId) +{ + $photos = new Zend_Gdata_Photos($client); + + $photoQuery = new Zend_Gdata_Photos_PhotoQuery; + $photoQuery->setUser($user); + $photoQuery->setAlbumId($albumId); + $photoQuery->setPhotoId($photoId); + $photoQuery->setType('entry'); + + $entry = $photos->getPhotoEntry($photoQuery); + + $photos->deletePhotoEntry($entry, true); + + outputAlbumFeed($client, $user, $albumId); +} + +/** + * Adds a new album to the specified user's album + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param string $name The name of the new album + * @return void + */ +function addAlbum($client, $user, $name) +{ + $photos = new Zend_Gdata_Photos($client); + + $entry = new Zend_Gdata_Photos_AlbumEntry(); + $entry->setTitle($photos->newTitle($name)); + + $result = $photos->insertAlbumEntry($entry); + if ($result) { + outputUserFeed($client, $user); + } else { + echo "There was an issue with the album creation."; + } +} + +/** + * Deletes the specified album + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param integer $albumId The album's id + * @return void + */ +function deleteAlbum($client, $user, $albumId) +{ + $photos = new Zend_Gdata_Photos($client); + + $albumQuery = new Zend_Gdata_Photos_AlbumQuery; + $albumQuery->setUser($user); + $albumQuery->setAlbumId($albumId); + $albumQuery->setType('entry'); + + $entry = $photos->getAlbumEntry($albumQuery); + + $photos->deleteAlbumEntry($entry, true); + + outputUserFeed($client, $user); +} + +/** + * Adds a new comment to the specified photo + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param integer $albumId The album's id + * @param integer $photoId The photo's id + * @param string $comment The comment to add + * @return void + */ +function addComment($client, $user, $album, $photo, $comment) +{ + $photos = new Zend_Gdata_Photos($client); + + $entry = new Zend_Gdata_Photos_CommentEntry(); + $entry->setTitle($photos->newTitle($comment)); + $entry->setContent($photos->newContent($comment)); + + $photoQuery = new Zend_Gdata_Photos_PhotoQuery; + $photoQuery->setUser($user); + $photoQuery->setAlbumId($album); + $photoQuery->setPhotoId($photo); + $photoQuery->setType('entry'); + + $photoEntry = $photos->getPhotoEntry($photoQuery); + + $result = $photos->insertCommentEntry($entry, $photoEntry); + if ($result) { + outputPhotoFeed($client, $user, $album, $photo); + } else { + echo "There was an issue with the comment creation."; + } +} + +/** + * Deletes the specified comment + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param integer $albumId The album's id + * @param integer $photoId The photo's id + * @param integer $commentId The comment's id + * @return void + */ +function deleteComment($client, $user, $albumId, $photoId, $commentId) +{ + $photos = new Zend_Gdata_Photos($client); + + $photoQuery = new Zend_Gdata_Photos_PhotoQuery; + $photoQuery->setUser($user); + $photoQuery->setAlbumId($albumId); + $photoQuery->setPhotoId($photoId); + $photoQuery->setType('entry'); + + $path = $photoQuery->getQueryUrl() . '/commentid/' . $commentId; + + $entry = $photos->getCommentEntry($path); + + $photos->deleteCommentEntry($entry, true); + + outputPhotoFeed($client, $user, $albumId, $photoId); +} + +/** + * Adds a new tag to the specified photo + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param integer $album The album's id + * @param integer $photo The photo's id + * @param string $tag The tag to add to the photo + * @return void + */ +function addTag($client, $user, $album, $photo, $tag) +{ + $photos = new Zend_Gdata_Photos($client); + + $entry = new Zend_Gdata_Photos_TagEntry(); + $entry->setTitle($photos->newTitle($tag)); + + $photoQuery = new Zend_Gdata_Photos_PhotoQuery; + $photoQuery->setUser($user); + $photoQuery->setAlbumId($album); + $photoQuery->setPhotoId($photo); + $photoQuery->setType('entry'); + + $photoEntry = $photos->getPhotoEntry($photoQuery); + + $result = $photos->insertTagEntry($entry, $photoEntry); + if ($result) { + outputPhotoFeed($client, $user, $album, $photo); + } else { + echo "There was an issue with the tag creation."; + } +} + +/** + * Deletes the specified tag + * + * @param Zend_Http_Client $client The authenticated client + * @param string $user The user's account name + * @param integer $albumId The album's id + * @param integer $photoId The photo's id + * @param string $tagContent The name of the tag to be deleted + * @return void + */ +function deleteTag($client, $user, $albumId, $photoId, $tagContent) +{ + $photos = new Zend_Gdata_Photos($client); + + $photoQuery = new Zend_Gdata_Photos_PhotoQuery; + $photoQuery->setUser($user); + $photoQuery->setAlbumId($albumId); + $photoQuery->setPhotoId($photoId); + $query = $photoQuery->getQueryUrl() . "?kind=tag"; + + $photoFeed = $photos->getPhotoFeed($query); + + foreach ($photoFeed as $entry) { + if ($entry instanceof Zend_Gdata_Photos_TagEntry) { + if ($entry->getContent() == $tagContent) { + $tagEntry = $entry; + } + } + } + + $photos->deleteTagEntry($tagEntry, true); + + outputPhotoFeed($client, $user, $albumId, $photoId); +} + +/** + * Returns the path to the current script, without any query params + * + * Env variables used: + * $_SERVER['PHP_SELF'] + * + * @return string Current script path + */ +function getCurrentScript() +{ + global $_SERVER; + return $_SERVER["PHP_SELF"]; +} + +/** + * Returns the full URL of the current page, based upon env variables + * + * Env variables used: + * $_SERVER['HTTPS'] = (on|off|) + * $_SERVER['HTTP_HOST'] = value of the Host: header + * $_SERVER['SERVER_PORT'] = port number (only used if not http/80,https/443) + * $_SERVER['REQUEST_URI'] = the URI after the method of the HTTP request + * + * @return string Current URL + */ +function getCurrentUrl() +{ + global $_SERVER; + + /** + * Filter php_self to avoid a security vulnerability. + */ + $php_request_uri = htmlentities(substr($_SERVER['REQUEST_URI'], 0, + strcspn($_SERVER['REQUEST_URI'], "\n\r")), ENT_QUOTES); + + if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') { + $protocol = 'https://'; + } else { + $protocol = 'http://'; + } + $host = $_SERVER['HTTP_HOST']; + if ($_SERVER['SERVER_PORT'] != '' && + (($protocol == 'http://' && $_SERVER['SERVER_PORT'] != '80') || + ($protocol == 'https://' && $_SERVER['SERVER_PORT'] != '443'))) { + $port = ':' . $_SERVER['SERVER_PORT']; + } else { + $port = ''; + } + return $protocol . $host . $port . $php_request_uri; +} + +/** + * Returns the AuthSub URL which the user must visit to authenticate requests + * from this application. + * + * Uses getCurrentUrl() to get the next URL which the user will be redirected + * to after successfully authenticating with the Google service. + * + * @return string AuthSub URL + */ +function getAuthSubUrl() +{ + $next = getCurrentUrl(); + $scope = 'http://picasaweb.google.com/data'; + $secure = false; + $session = true; + return Zend_Gdata_AuthSub::getAuthSubTokenUri($next, $scope, $secure, + $session); +} + +/** + * Outputs a request to the user to login to their Google account, including + * a link to the AuthSub URL. + * + * Uses getAuthSubUrl() to get the URL which the user must visit to authenticate + * + * @return void + */ +function requestUserLogin($linkText) +{ + $authSubUrl = getAuthSubUrl(); + echo "{$linkText}"; +} + +/** + * Returns a HTTP client object with the appropriate headers for communicating + * with Google using AuthSub authentication. + * + * Uses the $_SESSION['sessionToken'] to store the AuthSub session token after + * it is obtained. The single use token supplied in the URL when redirected + * after the user succesfully authenticated to Google is retrieved from the + * $_GET['token'] variable. + * + * @return Zend_Http_Client + */ +function getAuthSubHttpClient() +{ + global $_SESSION, $_GET; + if (!isset($_SESSION['sessionToken']) && isset($_GET['token'])) { + $_SESSION['sessionToken'] = + Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']); + } + $client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['sessionToken']); + return $client; +} + +/** + * Processes loading of this sample code through a web browser. Uses AuthSub + * authentication and outputs a list of a user's albums if succesfully + * authenticated. + * + * @return void + */ +function processPageLoad() +{ + global $_SESSION, $_GET; + if (!isset($_SESSION['sessionToken']) && !isset($_GET['token'])) { + requestUserLogin('Please login to your Google Account.'); + } else { + $client = getAuthSubHttpClient(); + if (!empty($_REQUEST['command'])) { + switch ($_REQUEST['command']) { + case 'retrieveSelf': + outputUserFeed($client, "default"); + break; + case 'retrieveUser': + outputUserFeed($client, $_REQUEST['user']); + break; + case 'retrieveAlbumFeed': + outputAlbumFeed($client, $_REQUEST['user'], $_REQUEST['album']); + break; + case 'retrievePhotoFeed': + outputPhotoFeed($client, $_REQUEST['user'], $_REQUEST['album'], + $_REQUEST['photo']); + break; + } + } + + // Now we handle the potentially destructive commands, which have to + // be submitted by POST only. + if (!empty($_POST['command'])) { + switch ($_POST['command']) { + case 'addPhoto': + addPhoto($client, $_POST['user'], $_POST['album'], $_FILES['photo']); + break; + case 'deletePhoto': + deletePhoto($client, $_POST['user'], $_POST['album'], + $_POST['photo']); + break; + case 'addAlbum': + addAlbum($client, $_POST['user'], $_POST['name']); + break; + case 'deleteAlbum': + deleteAlbum($client, $_POST['user'], $_POST['album']); + break; + case 'addComment': + addComment($client, $_POST['user'], $_POST['album'], $_POST['photo'], + $_POST['comment']); + break; + case 'addTag': + addTag($client, $_POST['user'], $_POST['album'], $_POST['photo'], + $_POST['tag']); + break; + case 'deleteComment': + deleteComment($client, $_POST['user'], $_POST['album'], + $_POST['photo'], $_POST['comment']); + break; + case 'deleteTag': + deleteTag($client, $_POST['user'], $_POST['album'], $_POST['photo'], + $_POST['tag']); + break; + default: + break; + } + } + + // If a menu parameter is available, display a submenu. + if (!empty($_REQUEST['menu'])) { + switch ($_REQUEST['menu']) { + case 'user': + displayUserMenu(); + break; + case 'photo': + displayPhotoMenu(); + break; + case 'album': + displayAlbumMenu(); + break; + case 'logout': + logout(); + break; + default: + header('HTTP/1.1 400 Bad Request'); + echo "
Please check your request and try again.
"; + } + } + + if (empty($_REQUEST['menu']) && empty($_REQUEST['command'])) { + displayMenu(); + } + } +} + +/** + * Displays the main menu, allowing the user to select from a list of actions. + * + * @return void + */ +function displayMenu() +{ +?> +Welcome to the Photos API demo page. Please select + from one of the following four options to fetch information.
+ +