/** * upload * * @return boolean */ public function upload($formData) { $this->setFormData($formData); // Additional validation if (!$this->validate()) { return false; } $existingIds = getExistingInstagramIds(); foreach ($this->formData['photos'] as $data) { list($sourceId, $thumbnail, $medium, $full, $caption) = explode('|', $data); // Skip existing photos if (isset($existingIds[$sourceId])) { continue; } // Save external paths $sql = "INSERT INTO `fcms_gallery_external_photo`\n (`source_id`, `thumbnail`, `medium`, `full`)\n VALUES\n (?, ?, ?, ?)"; $params = array($sourceId, $thumbnail, $medium, $full); $id = $this->fcmsDatabase->insert($sql, $params); if ($id === false) { return false; } $this->newPhotoIds[] = $id; // Insert new photo $sql = "INSERT INTO `fcms_gallery_photos`\n (`date`, `external_id`, `caption`, `category`, `user`)\n VALUES\n (NOW(), ?, ?, ?, ?)"; $params = array($id, $caption, $this->newCategoryId, $this->fcmsUser->id); if (!$this->fcmsDatabase->insert($sql, $params)) { return false; } } return true; }
/** * runInstagramJob * * @return void */ function runInstagramJob() { require_once 'inc/config_inc.php'; require_once 'inc/constants.php'; require_once 'inc/socialmedia.php'; require_once 'inc/utils.php'; require_once THIRDPARTY . 'gettext.inc'; require_once THIRDPARTY . 'Instagram.php'; $fcmsError = FCMS_Error::getInstance(); $fcmsDatabase = Database::getInstance($fcmsError); // Get user's access tokens $sql = "SELECT u.`id`, s.`instagram_access_token`\n FROM `fcms_user_settings` AS s, `fcms_users` AS u\n WHERE s.`user` = u.`id`\n AND s.`instagram_auto_upload` = 1\n AND s.`instagram_access_token` IS NOT NULL"; $rows = $fcmsDatabase->getRows($sql); if ($rows === false) { logError(__FILE__ . ' [' . __LINE__ . '] - Could not get instagram access tokens.'); die; } $accessTokens = array(); foreach ($rows as $row) { $accessTokens[$row['id']] = $row['instagram_access_token']; } $config = getInstagramConfigData(); $existingIds = getExistingInstagramIds(); // Get pics for each user foreach ($accessTokens as $userId => $token) { $categoryId = getUserInstagramCategory($userId); $instagram = new Instagram($config['instagram_client_id'], $config['instagram_client_secret'], $token); try { $feed = $instagram->get('users/self/media/recent'); } catch (InstagramApiError $e) { logError(__FILE__ . ' [' . __LINE__ . '] - Could not get user instagram data. - ' . $e->getMessage()); die; } $sql = "INSERT INTO `fcms_gallery_photos`\n (`date`, `path`, `caption`, `category`, `user`)\n VALUES "; foreach ($feed->data as $photo) { $sourceId = $photo->id; $thumbnail = $photo->images->thumbnail->url; $medium = $photo->images->low_resolution->url; $full = $photo->images->standard_resolution->url; $caption = $photo->caption->text; $caption .= ' [' . sprintf(T_('Filter: %s.'), $photo->filter) . ']'; // Skip existing photos if (isset($existingIds[$sourceId])) { continue; } // Save external paths $sql = "INSERT INTO `fcms_gallery_external_photo`\n (`source_id`, `thumbnail`, `medium`, `full`)\n VALUES\n (?, ?, ?, ?)"; $params = array($sourceId, $thumbnail, $medium, $full); $id = $fcmsDatabase->insert($sql, $params); if ($id === false) { logError(__FILE__ . ' [' . __LINE__ . '] - Could not save external photos.'); die; } // Insert new photo $sql = "INSERT INTO `fcms_gallery_photos`\n (`date`, `external_id`, `caption`, `category`, `user`)\n VALUES\n (NOW(), ?, ?, ?, ?)"; $params = array($id, $caption, $categoryId, $userId); if (!$fcmsDatabase->insert($sql, $params)) { logError(__FILE__ . ' [' . __LINE__ . '] - Could not insert new photo.'); die; } } } // Update date we last ran this job updateLastRun(date('Y-m-d H:i:s'), 'instagram'); }