/** * 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_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, false); curl_setopt($curlh, CURLOPT_NOBODY, false); $response = curl_exec($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); $doc = new DOMDocument(); @$doc->loadHTML($response); $elms = $doc->getElementsByTagName('frame'); if ($elms->length > 0) { return trim($elms->item(1)->getAttribute('src')); } return false; }
/** * 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_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($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); $doc = new DOMDocument(); @$doc->loadHTML($response); $xpath = new DOMXpath($doc); $elms = $xpath->query("//meta[@http-equiv='refresh']"); if ($elms->length > 0) { preg_match('#url=(.*)$#i', $elms->item(0)->getAttribute('content'), $matches); return trim($matches[1]); } return false; }
/** * 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_FOLLOWLOCATION, true); curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlh, CURLOPT_HEADER, false); curl_setopt($curlh, CURLOPT_NOBODY, false); $response = curl_exec($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); preg_match_all('#<frame src="([^"]+)"#i', $response, $matches); if (isset($matches[1]) && isset($matches[1][1])) { return trim($matches[1][1]); } return false; }
/** * http_auth * * Use HTTP-AUTH to authenticate users * * @access private * @param string username * @param string password * @param string authentication realm */ function http_auth($user, $pass, $realm = 'Restricted Area') { if (!(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']) && $_SERVER['PHP_AUTH_USER'] == $user && $_SERVER['PHP_AUTH_PW'] == $pass)) { $realm = addcslashes($realm, '"'); header("WWW-Authenticate: Basic realm=\"{$realm}\""); header('HTTP/1.0 401 Unauthorized'); Glucose::fatalError('Error 401: Unauthorized Access'); } }
private function bootstrap() { if (!@(include BASE_PATH . '/config.php')) { Glucose::fatalError('Could not load configuration file.'); } self::$config = $config; $this->loadClass('request'); $this->loadClass('response'); $this->loadClass('template'); $this->loadClass('memcached'); $this->loadClass('router'); $this->router->dispatch($this->request); }
public function expand() { if (!get('url')) { $this->GC->response->addHeader('HTTP/1.1 400 Bad Request', true, 400); Glucose::fatalError('Missing required parameter: URL.'); } $short_url = trim(get('url')); if (!preg_match('#^[a-z]+://#', $short_url)) { $short_url = 'http://' . $short_url; } $data['long-url'] = $this->api->long_url($short_url); // Get the extra info that might be requested if (get('all-redirects')) { $data['all-redirects'] = $this->api->all_redirects($short_url); } if (get('content-type')) { $data['content-type'] = $this->api->content_type($data['long-url']); } if (get('response-code')) { $data['response-code'] = $this->api->response_code($data['long-url']); } if (get('title')) { $data['title'] = $this->api->title($data['long-url']); } if (get('rel-canonical')) { $data['rel-canonical'] = $this->api->rel_canonical($data['long-url']); } if (get('meta-keywords')) { $data['meta-keywords'] = $this->api->meta_keywords($data['long-url']); } if (get('meta-description')) { $data['meta-description'] = $this->api->meta_description($data['long-url']); } if (get('safe-browsing')) { $data['safe-browsing'] = $this->api->safe_browsing($data['long-url']); } if (get('oembed')) { $data['oembed'] = $this->api->oembed($data['long-url']); } $data = array_filter($data); $this->GC->loadView('expand', array('response' => $data)); }
public function expand() { if (!get('url')) { $this->GC->response->addHeader('HTTP/1.1 400 Bad Request', true, 400); Glucose::fatalError('Missing required parameter: URL.'); } $short_url = trim(get('url')); if (!preg_match('#^[a-z]+://#', $short_url)) { $short_url = 'http://' . $short_url; } $data['long_url'] = $this->api->expand($short_url); if (!$data['long_url']) { Glucose::fatalError('Could not expand URL. Please check that you have submitted a valid URL.', true); } // If there's a title for this URL, add it to the output $data['title'] = $this->api->get_title($data['long_url']); // If there's an oembed for this URL, add it to the output $data['oembed'] = $this->api->get_oembed($data['long_url']); $data = array_filter($data); $this->GC->loadView('expand', array('response' => $data)); }
public function dispatch($request) { $this->applyCustomRoutes($request->url); $this->createRoute($request->url); $filename = BASE_PATH . '/controllers/'; $filename .= $this->module !== null ? $this->module . '/' : ''; $filename .= $this->controller . '.php'; if (is_file($filename)) { include_once BASE_PATH . '/classes/Controller.php'; include_once $filename; $controller = new $this->controller(); if (!in_array(strtolower($this->action), array_map('strtolower', get_class_methods($controller)))) { $this->action = '_error_404'; } call_user_func_array(array(&$controller, $this->action), $this->args); } else { $this->GC->response->addHeader('HTTP/1.0 404 Not Found', true, 404); Glucose::fatalError('404 Not Found'); } }
public function expand() { if (!get('url')) { return $this->GC->loadView('expand'); } $data['short_url'] = trim(get('url')); if (!preg_match('#^[a-z]+://#', $data['short_url'])) { $data['short_url'] = 'http://' . $data['short_url']; } include BASE_PATH . '/libraries/LongURL_API_v2.php'; $api = new LongURL_API_v2(); $data['long_url'] = $api->long_url($data['short_url']); if (!$data['long_url']) { return Glucose::fatalError('Could not expand URL. Please check that you have submitted a valid URL.'); } $data['all_redirects'] = $api->all_redirects($data['short_url']); $data['title'] = $api->title($data['long_url']); $data['conversations'] = $api->conversations($data['long_url']); $data['meta_keywords'] = $api->meta_keywords($data['long_url']); $data['meta_description'] = $api->meta_description($data['long_url']); $data['content_type'] = $api->content_type($data['long_url']); $this->GC->loadView('expand', $data); }
/** * 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; } }
public function _error_404() { $this->GC->response->addHeader('HTTP/1.1 404 Not Found', true, 404); Glucose::fatalError('404 Not Found'); }