/** * Create a new cache driver instance, based on its lowercase identifier * * @param string|array s * @param string The lowercase cache driver name * @return Cache\Driver\* A new instance to the cache driver * @throws \BadMethodCallException Thrown when the */ private static function driver($driver) { switch ($driver) { case 'apc': return new Driver\APC(static::$configuration['key']); case 'file': return new Driver\File(static::$configuration['key'], static::$configuration['path']); case 'memcache': return new Driver\Memcache(static::$configuration['key'], Memcache::instance()); case 'memcached': return new Driver\Memcached(static::$configuration['key'], Memcached::instance()); default: throw new \InvalidArgumentException('Cachew driver [' . $driver . '] is not currently supported'); break; } }
public function __construct() { $this->memcached = Memcached::instance(); include BASE_PATH . '/libraries/known_services.php'; $this->known_services = $known_services; }
/** * long_url * * Expand a shortened URL to its original, long form * * @param string short_url * @return mixed */ public function long_url($short_url) { $curlh = curl_init(); curl_setopt($curlh, CURLOPT_URL, $short_url); curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curlh, CURLOPT_TIMEOUT, 10); curl_setopt($curlh, CURLOPT_USERAGENT, 'LongURL API'); curl_setopt($curlh, CURLOPT_REFERER, 'http://longurl.org'); curl_setopt($curlh, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_MAXREDIRS, 10); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, true); // Include header in output curl_setopt($curlh, CURLOPT_NOBODY, true); // Don't include body in output $response = curl_exec($curlh); $info = curl_getinfo($curlh); if ($info['http_code'] === 405) { // HEAD requests aren't allowed $curlh = curl_init(); curl_setopt($curlh, CURLOPT_URL, $short_url); curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($curlh, CURLOPT_TIMEOUT, 10); curl_setopt($curlh, CURLOPT_USERAGENT, 'LongURL API'); curl_setopt($curlh, CURLOPT_REFERER, 'http://longurl.org'); curl_setopt($curlh, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_MAXREDIRS, 10); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, true); // Include header in output $response = curl_exec($curlh); $info = curl_getinfo($curlh); } if (!$response) { $error = curl_error($curlh); if (strpos($error, 'timed out') !== false) { Glucose::instance()->response->addHeader('HTTP/1.1 504 Gateway Timeout', true, 504); error_log($error . ':' . $short_url); Glucose::fatalError('Connection timeout'); } else { Glucose::instance()->response->addHeader('HTTP/1.1 400 Bad Request', true, 400); error_log($error . ':' . $short_url); Glucose::fatalError('Could not expand URL. Please check that you have submitted a valid URL.'); } } curl_close($curlh); if ($response) { // Cache content-type Memcached::instance()->set('content-type:' . $info['url'], $info['content_type']); // Extract and cache all redirects preg_match_all('#Location:\\s?([^\\s]+)#i', $response, $matches); Memcached::instance()->set('all-redirects:' . $short_url, $matches[1]); // Cache response-code Memcached::instance()->set('response-code:' . $info['url'], $info['http_code']); return $info['url']; } else { return false; } }