Пример #1
0
 public function __construct()
 {
     parent::__construct();
     if (Glucose::config('user', 'stats')) {
         http_auth(Glucose::config('user', 'stats'), Glucose::config('pass', 'stats'));
     }
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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;
 }
Пример #4
0
 /**
  * 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;
 }
Пример #5
0
/**
 * 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');
    }
}
Пример #6
0
 public function set($key, $value, $compressed = null, $time = null)
 {
     $this->quick_cache[$key] = $value;
     if (!$this->memcached) {
         return false;
     }
     $key = $this->key_prefix . $key;
     $compressed = $compressed ? MEMCACHE_COMPRESSED : 0;
     $time = $time !== null ? $time : time() + Glucose::config('expiration', 'memcached');
     return $this->memcached->set($key, $value, $compressed, $time);
 }
Пример #7
0
 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);
 }
Пример #8
0
 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));
 }
Пример #9
0
 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));
 }
Пример #10
0
 public function __construct()
 {
     if (!($this->dbh = mysql_connect(Glucose::config('host', 'database'), config_item('user', 'database'), config_item('pass', 'database'))) || !mysql_select_db(Glucose::config('database', 'database'), $this->dbh)) {
         fatal_error('Could not connect to database.');
     }
 }
Пример #11
0
 /**
  * conversations
  *
  * Retrieve conversations surrounding a URL
  *
  * @param string url
  * @return mixed
  */
 public function conversations($url)
 {
     // Check cache
     $cached = $this->memcached->get('conversations:' . $url);
     if ($cached !== false) {
         return $cached === '' ? false : $cached;
     }
     $curlh = curl_init();
     curl_setopt($curlh, CURLOPT_URL, 'http://api.contextvoice.com/1.1/reactions/?apikey=' . Glucose::config('contextvoice_api_key', 'misc') . '&format=json&url=' . urlencode($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_RETURNTRANSFER, true);
     $response = curl_exec($curlh);
     $info = curl_getinfo($curlh);
     curl_close($curlh);
     if ($info['http_code'] === 200) {
         $this->memcached->set('conversations:' . $url, (array) json_decode($response));
         return (array) json_decode($response);
     }
     $this->memcached->set('conversations:' . $url, '');
     return false;
 }
Пример #12
0
 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');
     }
 }
Пример #13
0
 /**
  * @todo This is messy. Clean it up.
  */
 private function versionFileAssets($url)
 {
     if (strpos($url, 'http') === 0) {
         return $url;
     }
     $cache_name = md5($url) . '.versioned.css';
     $cache_path = BASE_PATH . Glucose::config('cache_dir', 'template') . $cache_name;
     $orig_mtime = Glucose::config('dynamic', 'assets') == true ? filemtime($this->urlToPath($url)) : static_asset_version();
     if (file_exists($cache_path) && $orig_mtime < filemtime($cache_path)) {
         $file = Glucose::config('cache_dir', 'template') . $cache_name;
         $file_path = $cache_path;
         if (Glucose::config('dynmaic', 'assets') == false) {
             return $file;
         }
     } else {
         $file = $url;
         $file_path = $this->urlToPath($url);
     }
     $file_contents = file_get_contents($file_path);
     $new_contents = $this->versionAssets($file_contents);
     if ($file_contents !== $new_contents) {
         file_put_contents($cache_path, $new_contents);
         return Glucose::config('cache_dir', 'template') . $cache_name;
     } else {
         return $file;
     }
 }
Пример #14
0
 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);
 }
Пример #15
0
 /**
  * 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;
     }
 }
Пример #16
0
 public function _error_404()
 {
     $this->GC->response->addHeader('HTTP/1.1 404 Not Found', true, 404);
     Glucose::fatalError('404 Not Found');
 }