/** * display * * @return void */ public function display() { $_SESSION['fcms_uploader_type'] = 'picasa'; $googleClient = getAuthedGoogleClient($this->fcmsUser->id); $picasaInfo = ''; $token = ''; $js = ''; if ($this->fcmsError->hasError()) { $this->fcmsError->displayError(); return; } else { if ($googleClient === false) { $picasaInfo = ' <div class="info-alert"> <h2>' . T_('Not connected to Google.') . '</h2> <p>' . T_('You must connect your Family Connections account to Google before you can begin importing photos from Google.') . '</p> <p><a href="../settings.php?view=google">' . T_('Connect to Google') . '</a></p> </div>'; } else { $json = json_decode($_SESSION['googleSessionToken']); $token = $json->access_token; $picasaInfo = '<p></p>'; $js = 'loadPicasaAlbums("' . $token . '", "' . T_('Could not get albums.') . '");'; } } // Display the form echo ' <form method="post" class="photo-uploader" action="index.php?action=upload&type=picasa"> <div class="header"> <label>' . T_('Category') . '</label> ' . $this->getCategoryInputs() . ' </div> <ul class="upload-types"> ' . $this->getUploadTypesNavigation('picasa') . ' </ul> <div class="upload-area"> <div class="picasa"> ' . $picasaInfo . ' </div> </div> <div class="footer"> <input class="sub1" type="submit" value="' . T_('Upload') . '" id="submit-photos" name="picasa"/> </div> </form> <script type="text/javascript"> ' . $js . ' $("#submit-photos").click(function(e) { ' . $this->getJsUploadValidation() . ' }); </script>'; }
/** * getAjaxPicasaPhotos * * Will get the first 25 photos for the given album id. Then calls js to get next 25. * Or, if photos have been loaded, will return all photos from the session. * * @return string */ function getAjaxPicasaPhotos() { $token = $_POST['picasa_session_token']; $albumId = $_POST['albumId']; $photos = ''; $googleClient = getAuthedGoogleClient($this->fcmsUser->id); if (isset($_SESSION['picasa_album_done']) && isset($_SESSION['picasa_album_id']) && $_SESSION['picasa_album_id'] == $albumId) { $photos .= '<input type="hidden" name="picasa_user" value="' . $_SESSION['picasa_user'] . '"/>'; $i = 1; foreach ($_SESSION['picasa_photos'] as $id => $data) { $photos .= '<li>'; $photos .= '<label for="picasa' . $i . '">'; $photos .= '<img src="' . $data['thumbnail'] . '" style="width:' . $data['width'] . ' height:' . $data['height'] . '"/>'; $photos .= '<span style="display:none"></span>'; $photos .= '</label>'; $photos .= '<input type="checkbox" id="picasa' . $i . '" name="photos[]" value="' . $id . '"/>'; $photos .= '</li>'; $i++; } } else { unset($_SESSION['picasa_album_done']); $_SESSION['picasa_album_id'] = $albumId; // Get the album data $curl = curl_init(); curl_setopt_array($curl, array(CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_URL => 'https://picasaweb.google.com/data/feed/api/user/default/albumid/' . $albumId . '?max-results=25', CURLOPT_HTTPHEADER => array('GData-Version: 2', 'Authorization: Bearer ' . $token), CURLOPT_RETURNTRANSFER => 1)); $response = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($httpCode !== 200) { echo ' <p class="error-alert"> ' . T_('Could not get Picasa album data.') . ' </p>'; logError(__FILE__ . ' [' . __LINE__ . '] - Could not get Picasa album data. - ' . $response); return; } $xml = new SimpleXMLElement($response); $_SESSION['picasa_photos'] = array(); $_SESSION['picasa_user'] = (string) $xml->title; $i = 1; foreach ($xml->entry as $photo) { $group = $photo->children('media', true)->group; // skip videos foreach ($group->content as $content) { if ($content->attributes()->medium == 'video') { continue 2; } } $sourceId = (int) $photo->children('gphoto', true)->id; $w = (int) $photo->children('gphoto', true)->width; $h = (int) $photo->children('gphoto', true)->height; $width = '100%;'; $height = 'auto;'; if ($w > $h) { $width = 'auto;'; $height = '100%;'; } $thumbnail = (string) $group->thumbnail[1]->attributes()->url; $_SESSION['picasa_photos'][$sourceId] = array('thumbnail' => $thumbnail, 'width' => $width, 'height' => $height); $photos .= '<li>'; $photos .= '<label for="picasa' . $i . '">'; $photos .= '<img src="' . $thumbnail . '" style="width:' . $width . ' height:' . $height . '"/>'; $photos .= '<span style="display:none"></span>'; $photos .= '</label>'; $photos .= '<input type="checkbox" id="picasa' . $i . '" name="photos[]" value="' . $sourceId . '"/>'; $photos .= '</li>'; $i++; } if ($i - 1 >= 25) { $photos .= '<script type="text/javascript">loadMorePicasaPhotos(26, "' . $token . '", "' . T_('Could not get additional photos.') . '");</script>'; } } if ($i <= 1 && empty($photos)) { $photos = '<p class="info-alert">' . T_('No photos were found in this album') . '</p>'; } $photos .= '<input type="hidden" name="picasa_user" value="' . $_SESSION['picasa_user'] . '"/>'; echo $photos; }
/** * runYouTubeJob * * Imports YouTube videos. * * @return void */ function runYouTubeJob() { require_once 'constants.php'; require_once 'socialmedia.php'; require_once 'datetime.php'; require_once THIRDPARTY . 'gettext.inc'; set_include_path(get_include_path() . PATH_SEPARATOR . THIRDPARTY . 'google-api-php-client/src/'); require_once THIRDPARTY . 'google-api-php-client/src/Google/autoload.php'; $fcmsError = FCMS_Error::getInstance(); $fcmsDatabase = Database::getInstance($fcmsError); $existingIds = getExistingYouTubeIds(); // Get all google session tokens $sql = "SELECT u.`id`, s.`google_session_token`\n FROM `fcms_user_settings` AS s\n LEFT JOIN `fcms_users` AS u ON s.`user` = u.`id`\n WHERE s.`google_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['google_session_token']; } // Get videos for each user foreach ($sessionTokens as $userId => $token) { $values = ''; $videoCount = 0; $params = array(); try { $googleClient = getAuthedGoogleClient($userId); $youtube = new Google_Service_YouTube($googleClient); $channelsResponse = $youtube->channels->listChannels('id,snippet,status,contentDetails,statistics', array('mine' => true)); foreach ($channelsResponse['items'] as $channel) { $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads']; $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array('playlistId' => $uploadsListId, 'maxResults' => 50)); foreach ($playlistItemsResponse['items'] as $playlistItem) { $id = $playlistItem['snippet']['resourceId']['videoId']; $title = $playlistItem['snippet']['title']; $description = $playlistItem['snippet']['description']; $created = formatDate('Y-m-d H:i:s', $playlistItem['snippet']['publishedAt']); if (isset($existingIds[$id])) { continue; } $values .= "(?, ?, ?, 'youtube', ?, ?, NOW(), ?),"; $params[] = $id; $params[] = $title; $params[] = $description; $params[] = $created; $params[] = $userId; $params[] = $userId; $videoCount++; } } } catch (Exception $e) { $errors = print_r($e, true); logError(__FILE__ . ' [' . __LINE__ . '] - Could not upload videos to YouTube. ' . $errors); die; } if ($videoCount > 0) { $values = substr($values, 0, -1); // remove comma $sql = "INSERT INTO `fcms_video`\n (`source_id`, `title`, `description`, `source`, `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'); }
/** * 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->displayHeader(); $this->fcmsError->displayError(); $this->displayFooter(); return; } if (isset($_POST['delete_youtube'])) { try { $googleClient = getAuthedGoogleClient($this->fcmsUser->id); $youtube = new Google_Service_YouTube($googleClient); $youtube->videos->delete($sourceId); } catch (Exception $e) { $this->displayHeader(); $this->fcmsError->add(array('type' => 'operation', 'message' => 'Could not upload video to YouTube.', 'error' => $e, 'file' => __FILE__, 'line' => __LINE__)); $this->fcmsError->displayError(); $this->displayFooter(); return; } // Set message $_SESSION['message'] = 'delete_video_youtube'; } // Set message if (!isset($_SESSION['message'])) { $_SESSION['message'] = 'remove_video'; } // Send back to user's video listing header("Location: video.php?u={$userId}"); }
/** * setFormData * * Saves all the data passed in from the form upload. * * @param array $formData * * @return void */ public function setFormData($formData) { $this->formData = $formData; $albumId = $formData['albums']; $user = $formData['picasa_user']; $googleClient = getAuthedGoogleClient($this->fcmsUser->id); $json = json_decode($_SESSION['googleSessionToken']); $token = $json->access_token; $curl = curl_init(); $thumbSizes = '150c,600'; if ($this->usingFullSizePhotos) { $thumbSizes .= ',d'; } $url = 'https://picasaweb.google.com/data/feed/api/user/default/albumid/' . $albumId . '?thumbsize=' . $thumbSizes; curl_setopt_array($curl, array(CURLOPT_CUSTOMREQUEST => 'GET', CURLOPT_URL => $url, CURLOPT_HTTPHEADER => array('GData-Version: 2', 'Authorization: Bearer ' . $token), CURLOPT_RETURNTRANSFER => 1)); $response = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); if ($httpCode !== 200) { $this->fcmsError->add(array('type' => 'operation', 'message' => T_('Could not get Picasa data.'), 'error' => $response, 'file' => __FILE__, 'line' => __LINE__)); return false; } $xml = new SimpleXMLElement($response); $this->albumFeed = $xml; }