$thing = $_match[1]; $Post = $Database->where('id', $_match[2])->getOne("{$thing}s"); if (empty($Post)) { Response::fail("The specified {$thing} does not exist"); } // Link is already full size, we're done if (preg_match($FULLSIZE_MATCH_REGEX, $Post->fullsize)) { Response::done(array('fullsize' => $Post->fullsize)); } // Reverse submission lookup $StashItem = $Database->where('fullsize', $Post->fullsize)->orWhere('preview', $Post->preview)->getOne('deviation_cache', 'id,fullsize,preview'); if (empty($StashItem['id'])) { Response::fail('Stash URL lookup failed'); } try { $fullsize = CoreUtils::getFullsizeURL($StashItem['id'], 'sta.sh'); if (!is_string($fullsize)) { if ($fullsize === 404) { $Database->where('provider', 'sta.sh')->where('id', $StashItem['id'])->delete('deviation_cache'); $Database->where('preview', $StashItem['preview'])->orWhere('fullsize', $StashItem['fullsize'])->update('requests', array('fullsize' => null, 'preview' => null)); $Database->where('preview', $StashItem['preview'])->orWhere('fullsize', $StashItem['fullsize'])->update('reservations', array('fullsize' => null, 'preview' => null)); Response::fail('The original image has been deleted from Sta.sh', array('rmdirect' => true)); } else { throw new Exception("Code {$fullsize}; Could not find the URL"); } } } catch (Exception $e) { Response::fail('Error while finding URL: ' . $e->getMessage()); } // Check image availability if (!!DeviantArt::isImageAvailable($fullsize)) {
/** * Caches information about a deviation in the 'deviation_cache' table * Returns null on failure * * @param string $ID * @param null|string $type * @param bool $mass * * @return array|null */ static function getCachedSubmission($ID, $type = 'fav.me', $mass = false) { global $Database, $FULLSIZE_MATCH_REGEX; if ($type === 'sta.sh') { $ID = CoreUtils::nomralizeStashID($ID); } $Deviation = $Database->where('id', $ID)->where('provider', $type)->getOne('deviation_cache'); $cacheExhausted = self::$_MASS_CACHE_USED > self::$_MASS_CACHE_LIMIT; $cacheExpired = empty($Deviation['updated_on']) ? true : strtotime($Deviation['updated_on']) + Time::$IN_SECONDS['hour'] * 12 < time(); $lastRequestSuccessful = !self::$_CACHE_BAILOUT; $localDataMissing = empty($Deviation); $massCachingWithinLimit = $mass && !$cacheExhausted; $notMassCachingAndCacheExpired = !$mass && $cacheExpired; if ($lastRequestSuccessful && ($localDataMissing || ($massCachingWithinLimit && $cacheExpired || $notMassCachingAndCacheExpired))) { try { $json = self::oEmbed($ID, $type); if (empty($json)) { throw new \Exception(); } } catch (\Exception $e) { if (!empty($Deviation)) { $Database->where('id', $Deviation['id'])->update('deviation_cache', array('updated_on' => date('c', time() + Time::$IN_SECONDS['minute']))); } $ErrorMSG = "Saving local data for {$ID}@{$type} failed: " . $e->getMessage(); if (!Permission::sufficient('developer')) { trigger_error($ErrorMSG); } if (POST_REQUEST) { Response::fail($ErrorMSG); } else { echo "<div class='notice fail'><label>da_cache_deviation({$ID}, {$type})</label><p>{$ErrorMSG}</p></div>"; } self::$_CACHE_BAILOUT = true; return $Deviation; } $insert = array('title' => preg_replace(new RegExp('\\\\\''), "'", $json['title']), 'preview' => URL::makeHttps($json['thumbnail_url']), 'fullsize' => URL::makeHttps(isset($json['fullsize_url']) ? $json['fullsize_url'] : $json['url']), 'provider' => $type, 'author' => $json['author_name'], 'updated_on' => date('c')); if (!preg_match($FULLSIZE_MATCH_REGEX, $insert['fullsize'])) { $fullsize_attempt = CoreUtils::getFullsizeURL($ID, $type); if (is_string($fullsize_attempt)) { $insert['fullsize'] = $fullsize_attempt; } } if (empty($Deviation)) { $Deviation = $Database->where('id', $ID)->where('provider', $type)->getOne('deviation_cache'); } if (empty($Deviation)) { $insert['id'] = $ID; $Database->insert('deviation_cache', $insert); } else { $Database->where('id', $Deviation['id'])->update('deviation_cache', $insert); $insert['id'] = $ID; } self::$_MASS_CACHE_USED++; $Deviation = $insert; } else { if (!empty($Deviation['updated_on'])) { $Deviation['updated_on'] = date('c', strtotime($Deviation['updated_on'])); if (self::$_CACHE_BAILOUT) { $Database->where('id', $Deviation['id'])->update('deviation_cache', array('updated_on' => $Deviation['updated_on'])); } } } return $Deviation; }