Example #1
0
 public static function instance(&$uri = TRUE)
 {
     if (!Request::$instance) {
         if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) {
             Request::$protocol = 'https';
         }
         if (isset($_SERVER['REQUEST_METHOD'])) {
             Request::$method = $_SERVER['REQUEST_METHOD'];
         }
         if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
             Request::$is_ajax = TRUE;
         }
         Request::$client_ip = self::get_client_ip();
         if (Request::$method !== 'GET' and Request::$method !== 'POST') {
             parse_str(file_get_contents('php://input'), $_POST);
         }
         if (isset($_SERVER['HTTP_USER_AGENT'])) {
             Request::$user_agent = $_SERVER['HTTP_USER_AGENT'];
         }
         if (isset($_SERVER['HTTP_REFERER'])) {
             Request::$referrer = $_SERVER['HTTP_REFERER'];
         }
         if ($uri === TRUE) {
             $uri = Request::detect_uri();
         }
         $uri = preg_replace('#//+#', '/', $uri);
         $uri = preg_replace('#\\.[\\s./]*/#', '', $uri);
         Request::$instance = new Request($uri);
     }
     return Request::$instance;
 }
Example #2
0
 /**
  * Create a new log
  *
  * @param string     $alias   An identifier to help index logs
  * @param string     $type    Module name that's logging (e.g. item, pet,..)
  * @param string     $message A general message describing the action
  * @param array      $params  Parameters that give insight into the action that has been performed
  * @param Model_User $user    The user that did an action(defaults to the logged in user if null)
  *
  * @return Log
  */
 public static function log($alias, $type, $message, $params = array(), $user = NULL)
 {
     if ($user == NULL) {
         $user = Auth::instance()->get_user();
     }
     $values = array('alias' => $alias, 'message' => $message, 'user_id' => $user->id, 'username' => $user->username, 'agent' => implode('-', Request::user_agent(array('browser', 'platform'))), 'ip' => Request::$client_ip, 'location' => Request::current()->uri(), 'type' => $type, 'params' => $params);
     $log = ORM::factory('Log')->values($values)->create();
     return new Journal($log);
 }
Example #3
0
File: Auth.php Project: Konro1/pms
 public function setUp()
 {
     parent::setUp();
     $_COOKIE = array();
     $_SESSION = array();
     Request::$client_ip = '8.8.8.8';
     Request::$user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19';
     $this->auth = new Auth_Jam_Test(Kohana::$config->load('auth'));
     $this->auth->session()->restart();
     Database::instance(Kohana::TESTING)->begin();
 }
Example #4
0
 /**
  * Overwrite Functions and Profile
  */
 protected function _load($path)
 {
     // @TODO: mobile cookie...
     if (Request::user_agent('mobile')) {
         $path = 'mobile/' . $path;
     } else {
         $path = 'desktop/' . $path;
     }
     $token = Profiler::start(__CLASS__, __FUNCTION__);
     $r = parent::_load($path);
     Profiler::stop($token);
     return $r;
 }
Example #5
0
 /**
  * Main request singleton instance. If no URI is provided, the URI will
  * be automatically detected using PATH_INFO, REQUEST_URI, or PHP_SELF.
  *
  *     $request = Request::instance();
  *
  * @param   string   URI of the request
  * @return  Request
  */
 public static function instance($uri = TRUE)
 {
     if (!Request::$instance) {
         if (isset($_SERVER['REQUEST_METHOD'])) {
             // Use the server request method
             Request::$method = $_SERVER['REQUEST_METHOD'];
         }
         if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) {
             // This request is secure
             Request::$protocol = 'https';
         }
         if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
             // This request is an AJAX request
             Request::$is_ajax = TRUE;
         }
         if (isset($_SERVER['HTTP_REFERER'])) {
             // There is a referrer for this request
             Request::$referrer = $_SERVER['HTTP_REFERER'];
         }
         if (isset($_SERVER['HTTP_USER_AGENT'])) {
             // Set the client user agent
             Request::$user_agent = $_SERVER['HTTP_USER_AGENT'];
         }
         if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
             // Use the forwarded IP address, typically set when the
             // client is using a proxy server.
             Request::$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
         } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
             // Use the forwarded IP address, typically set when the
             // client is using a proxy server.
             Request::$client_ip = $_SERVER['HTTP_CLIENT_IP'];
         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
             // The remote IP address
             Request::$client_ip = $_SERVER['REMOTE_ADDR'];
         }
         if (Request::$method !== 'GET' and Request::$method !== 'POST') {
             // Methods besides GET and POST do not properly parse the form-encoded
             // query string into the $_POST array, so we overload it manually.
             parse_str(file_get_contents('php://input'), $_POST);
         }
         if ($uri === TRUE) {
             // If the route wasn't passed, just generate the route from $_POST or $_GET values
             $uri = array_merge(JRequest::get('get'), JRequest::get('post'));
         }
         // Set the default request client
         $client = JFactory::getApplication()->isAdmin() ? 'admin' : 'site';
         // Create the instance singleton
         Request::$instance = Request::$current = new Request($uri, $client);
     }
     return Request::$instance;
 }
 public static function factory($uri = TRUE, $client_params = array(), $allow_external = TRUE, $injected_routes = array())
 {
     if (!Request::$initial) {
         $protocol = HTTP::$protocol;
         if (isset($_SERVER['REQUEST_METHOD'])) {
             $method = $_SERVER['REQUEST_METHOD'];
         } else {
             $method = HTTP_Request::GET;
         }
         if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN) or isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) {
             $secure = TRUE;
         }
         if (isset($_SERVER['HTTP_REFERER'])) {
             $referrer = $_SERVER['HTTP_REFERER'];
         }
         if (isset($_SERVER['HTTP_USER_AGENT'])) {
             Request::$user_agent = $_SERVER['HTTP_USER_AGENT'];
         }
         if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
             $requested_with = $_SERVER['HTTP_X_REQUESTED_WITH'];
         }
         if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) {
             $client_ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
             Request::$client_ip = array_shift($client_ips);
             unset($client_ips);
         } elseif (isset($_SERVER['HTTP_CLIENT_IP']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) {
             $client_ips = explode(',', $_SERVER['HTTP_CLIENT_IP']);
             Request::$client_ip = array_shift($client_ips);
             unset($client_ips);
         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
             // The remote IP address
             Request::$client_ip = $_SERVER['REMOTE_ADDR'];
         }
         if ($method !== HTTP_Request::GET) {
             // Ensure the raw body is saved for future use
             $body = file_get_contents('php://input');
         }
         if ($uri === TRUE) {
             // Attempt to guess the proper URI
             $uri = Request::detect_uri();
         }
         $cookies = array();
         if ($cookie_keys = array_keys($_COOKIE)) {
             foreach ($cookie_keys as $key) {
                 $cookies[$key] = Cookie::get($key);
             }
         }
         // Create the instance singleton
         Request::$initial = $request = new Request($uri, $client_params, $allow_external, $injected_routes);
         // Store global GET and POST data in the initial request only
         $request->protocol($protocol)->query($_GET)->post($_POST);
         if (isset($secure)) {
             // Set the request security
             $request->secure($secure);
         }
         if (isset($method)) {
             // Set the request method
             $request->method($method);
         }
         if (isset($referrer)) {
             // Set the referrer
             $request->referrer($referrer);
         }
         if (isset($requested_with)) {
             // Apply the requested with variable
             $request->requested_with($requested_with);
         }
         if (isset($body)) {
             // Set the request body (probably a PUT type)
             $request->body($body);
         }
         if (isset($cookies)) {
             $request->cookie($cookies);
         }
     } else {
         $request = new Request($uri, $client_params, $allow_external, $injected_routes);
     }
     return $request;
 }
Example #7
0
 /**
  * Send file download as the response
  *
  * All execution will be halted when this method is called!
  * Use TRUE for the filename to send the current response as the file content.
  * The third parameter allows the following options to be set:
  *
  * Type      | Option    | Description                        | Default Value
  * ----------|-----------|------------------------------------|--------------
  * `boolean` | inline    | Display inline instead of download | `FALSE`
  * `string`  | mime_type | Manual mime type                   | Automatic
  * `boolean` | delete    | Delete the file after sending      | `FALSE`
  *
  * Example:
  * ~~~
  * // Download a file that already exists
  * $request->send_file('media/packages/gleez.zip');
  *
  * // Download generated content as a file:
  * $request->response($content);
  * $request->send_file(TRUE, $filename);
  * ~~~
  *
  * [!!] Note: No further processing can be done after this method is called!
  *
  * @param   string  $filename  Filename with path, or TRUE for the current response
  * @param   string  $download  Downloaded file name [Optional]
  * @param   array   $options   Additional options [Optional]
  *
  * @return  void
  *
  * @throws  Gleez_Exception
  *
  * @uses    File::mime_by_ext
  * @uses    File::mime
  * @uses    Request::send_headers
  * @uses    Gleez_Exception::text
  */
 public function send_file($filename, $download = NULL, array $options = NULL)
 {
     if (!empty($options['mime_type'])) {
         // The mime-type has been manually set
         $mime = $options['mime_type'];
     }
     if ($filename === TRUE) {
         if (empty($download)) {
             throw new Gleez_Exception('Download name must be provided for streaming files');
         }
         // Temporary files will automatically be deleted
         $options['delete'] = FALSE;
         if (!isset($mime)) {
             // Guess the mime using the file extension
             $mime = File::mime_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION)));
         }
         // Force the data to be rendered if
         $file_data = (string) $this->_body;
         // Get the content size
         $size = strlen($file_data);
         // Create a temporary file to hold the current response
         $file = tmpfile();
         // Write the current response into the file
         fwrite($file, $file_data);
         // File data is no longer needed
         unset($file_data);
     } else {
         // Get the complete file path
         $filename = realpath($filename);
         if (empty($download)) {
             // Use the file name as the download file name
             $download = pathinfo($filename, PATHINFO_BASENAME);
         }
         // Get the file size
         $size = filesize($filename);
         if (!isset($mime)) {
             // Get the mime type
             $mime = File::mime_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION)));
         }
         // Open the file for reading
         $file = fopen($filename, 'rb');
     }
     if (!is_resource($file)) {
         throw new Gleez_Exception('Could not read file to send: :file', array(':file' => $download));
     }
     // Inline or download?
     $disposition = empty($options['inline']) ? 'attachment' : 'inline';
     // Calculate byte range to download.
     list($start, $end) = $this->_calculate_byte_range($size);
     if (!empty($options['resumable'])) {
         if ($start > 0 or $end < $size - 1) {
             // Partial Content
             $this->_status = 206;
         }
         // Range of bytes being sent
         $this->_header['content-range'] = 'bytes ' . $start . '-' . $end . '/' . $size;
         $this->_header['accept-ranges'] = 'bytes';
     }
     // Set the headers for a download
     $this->_header['content-disposition'] = $disposition . '; filename="' . $download . '"';
     $this->_header['content-type'] = $mime;
     $this->_header['content-length'] = (string) ($end - $start + 1);
     if (Request::user_agent('browser') === 'Internet Explorer') {
         // Naturally, IE does not act like a real browser...
         if (Request::$initial->secure()) {
             // http://support.microsoft.com/kb/316431
             $this->_header['pragma'] = $this->_header['cache-control'] = 'public';
         }
         if (version_compare(Request::user_agent('version'), '8.0', '>=')) {
             // http://ajaxian.com/archives/ie-8-security
             $this->_header['x-content-type-options'] = 'nosniff';
         }
     }
     // Send all headers now
     $this->send_headers();
     while (ob_get_level()) {
         // Flush all output buffers
         ob_end_flush();
     }
     // Manually stop execution
     ignore_user_abort(TRUE);
     if (!Kohana::$safe_mode) {
         // Keep the script running forever
         set_time_limit(0);
     }
     // Send data in 16kb blocks
     $block = 1024 * 16;
     fseek($file, $start);
     while (!feof($file) and ($pos = ftell($file)) <= $end) {
         if (connection_aborted()) {
             break;
         }
         if ($pos + $block > $end) {
             // Don't read past the buffer.
             $block = $end - $pos + 1;
         }
         // Output a block of the file
         echo fread($file, $block);
         // Send the data now
         flush();
     }
     // Close the file
     fclose($file);
     if (!empty($options['delete'])) {
         try {
             // Attempt to remove the file
             unlink($filename);
         } catch (Exception $e) {
             // Create a text version of the exception
             $error = Gleez_Exception::text($e);
             if (is_object(Kohana::$log)) {
                 // Add this exception to the log
                 // And make sure the logs are written
                 Log::error($error)->write();
             }
             // Do NOT display the exception, it will corrupt the output!
         }
     }
     // Stop execution
     exit;
 }
Example #8
0
 /**
  * Returns information about the client user agent.
  *
  *     // Returns "Chrome" when using Google Chrome
  *     $browser = Request::user_agent('browser');
  *
  * Multiple values can be returned at once by using an array:
  *
  *     // Get the browser and platform with a single call
  *     $info = Request::user_agent(array('browser', 'platform'));
  *
  * When using an array for the value, an associative array will be returned.
  *
  * @param   mixed   $value String to return: browser, version, robot, mobile, platform; or array of values
  * @return  mixed   requested information, FALSE if nothing is found
  * @uses    Kohana::$config
  * @uses    Request::$user_agent
  */
 public static function user_agent($value)
 {
     if (is_array($value)) {
         $agent = array();
         foreach ($value as $v) {
             // Add each key to the set
             $agent[$v] = Request::user_agent($v);
         }
         return $agent;
     }
     static $info;
     if (isset($info[$value])) {
         // This value has already been found
         return $info[$value];
     }
     if ($value === 'browser' or $value == 'version') {
         // Load browsers
         $browsers = Kohana::$config->load('user_agents')->browser;
         foreach ($browsers as $search => $name) {
             if (stripos(Request::$user_agent, $search) !== FALSE) {
                 // Set the browser name
                 $info['browser'] = $name;
                 if (preg_match('#' . preg_quote($search) . '[^0-9.]*+([0-9.][0-9.a-z]*)#i', Request::$user_agent, $matches)) {
                     // Set the version number
                     $info['version'] = $matches[1];
                 } else {
                     // No version number found
                     $info['version'] = FALSE;
                 }
                 return $info[$value];
             }
         }
     } else {
         // Load the search group for this type
         $group = Kohana::$config->load('user_agents')->{$value};
         foreach ($group as $search => $name) {
             if (stripos(Request::$user_agent, $search) !== FALSE) {
                 // Set the value name
                 return $info[$value] = $name;
             }
         }
     }
     // The value requested could not be found
     return $info[$value] = FALSE;
 }
 /**
  * Send file download as the response. All execution will be halted when
  * this method is called! Use TRUE for the filename to send the current
  * response as the file content. The third parameter allows the following
  * options to be set:
  *
  * Type      | Option    | Description                        | Default Value
  * ----------|-----------|------------------------------------|--------------
  * `boolean` | inline    | Display inline instead of download | `FALSE`
  * `string`  | mime_type | Manual mime type                   | Automatic
  * `boolean` | delete    | Delete the file after sending      | `FALSE`
  *
  * Download a file that already exists:
  *
  *     $request->send_file('media/packages/JsonApiApplication.zip');
  *
  * Download generated content as a file:
  *
  *     $request->response($content);
  *     $request->send_file(TRUE, $filename);
  *
  * [!!] No further processing can be done after this method is called!
  *
  * @param   string  $filename   filename with path, or TRUE for the current response
  * @param   string  $download   downloaded file name
  * @param   array   $options    additional options
  * @return  void
  * @throws  JsonApiApplication_Exception
  * @uses    File::mime_by_ext
  * @uses    File::mime
  * @uses    Request::send_headers
  */
 public function send_file($filename, $download = NULL, array $options = NULL)
 {
     if (!empty($options['mime_type'])) {
         $mime = $options['mime_type'];
     }
     if ($filename === TRUE) {
         if (empty($download)) {
             throw new JsonApiApplication_Exception('Download name must be provided for streaming files');
         }
         $options['delete'] = FALSE;
         if (!isset($mime)) {
             $mime = File::mime_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION)));
         }
         $file_data = (string) $this->_body;
         $size = strlen($file_data);
         $file = tmpfile();
         fwrite($file, $file_data);
         unset($file_data);
     } else {
         $filename = realpath($filename);
         if (empty($download)) {
             $download = pathinfo($filename, PATHINFO_BASENAME);
         }
         $size = filesize($filename);
         if (!isset($mime)) {
             $mime = File::mime_by_ext(pathinfo($download, PATHINFO_EXTENSION));
         }
         $file = fopen($filename, 'rb');
     }
     if (!is_resource($file)) {
         throw new JsonApiApplication_Exception('Could not read file to send: :file', array(':file' => $download));
     }
     $disposition = empty($options['inline']) ? 'attachment' : 'inline';
     list($start, $end) = $this->_calculate_byte_range($size);
     if (!empty($options['resumable'])) {
         if ($start > 0 or $end < $size - 1) {
             $this->_status = 206;
         }
         $this->_header['content-range'] = 'bytes ' . $start . '-' . $end . '/' . $size;
         $this->_header['accept-ranges'] = 'bytes';
     }
     $this->_header['content-disposition'] = $disposition . '; filename="' . $download . '"';
     $this->_header['content-type'] = $mime;
     $this->_header['content-length'] = (string) ($end - $start + 1);
     if (Request::user_agent('browser') === 'Internet Explorer') {
         if (Request::$initial->secure()) {
             // http://support.microsoft.com/kb/316431
             $this->_header['pragma'] = $this->_header['cache-control'] = 'public';
         }
         if (version_compare(Request::user_agent('version'), '8.0', '>=')) {
             // http://ajaxian.com/archives/ie-8-security
             $this->_header['x-content-type-options'] = 'nosniff';
         }
     }
     $this->send_headers();
     while (ob_get_level()) {
         ob_end_flush();
     }
     ignore_user_abort(TRUE);
     if (!JsonApiApplication::$safe_mode) {
         set_time_limit(0);
     }
     $block = 1024 * 16;
     fseek($file, $start);
     while (!feof($file) and ($pos = ftell($file)) <= $end) {
         if (connection_aborted()) {
             break;
         }
         if ($pos + $block > $end) {
             $block = $end - $pos + 1;
         }
         echo fread($file, $block);
         flush();
     }
     fclose($file);
     if (!empty($options['delete'])) {
         try {
             unlink($filename);
         } catch (Exception $e) {
             $error = JsonApiApplication_Exception::text($e);
             if (is_object(JsonApiApplication::$log)) {
                 JsonApiApplication::$log->add(Log::ERROR, $error);
                 JsonApiApplication::$log->write();
             }
         }
     }
     exit;
 }
Example #10
0
 /**
  * Main request singleton instance. If no URI is provided, the URI will
  * be automatically detected using PATH_INFO, REQUEST_URI, or PHP_SELF.
  *
  *     $request = Request::instance();
  *
  * @param   string   URI of the request
  * @return  Request
  */
 public static function instance(&$uri = TRUE)
 {
     if (!Request::$instance) {
         if (Kohana::$is_cli) {
             // Default protocol for command line is cli://
             Request::$protocol = 'cli';
             // Get the command line options
             $options = CLI::options('uri', 'method', 'get', 'post');
             if (isset($options['uri'])) {
                 // Use the specified URI
                 $uri = $options['uri'];
             }
             if (isset($options['method'])) {
                 // Use the specified method
                 Request::$method = strtoupper($options['method']);
             }
             if (isset($options['get'])) {
                 // Overload the global GET data
                 parse_str($options['get'], $_GET);
             }
             if (isset($options['post'])) {
                 // Overload the global POST data
                 parse_str($options['post'], $_POST);
             }
         } else {
             if (isset($_SERVER['REQUEST_METHOD'])) {
                 // Use the server request method
                 Request::$method = $_SERVER['REQUEST_METHOD'];
             }
             if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) {
                 // This request is secure
                 Request::$protocol = 'https';
             }
             if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
                 // This request is an AJAX request
                 Request::$is_ajax = TRUE;
             }
             if (isset($_SERVER['HTTP_REFERER'])) {
                 // There is a referrer for this request
                 Request::$referrer = $_SERVER['HTTP_REFERER'];
             }
             if (isset($_SERVER['HTTP_USER_AGENT'])) {
                 // Set the client user agent
                 Request::$user_agent = $_SERVER['HTTP_USER_AGENT'];
             }
             if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                 // Use the forwarded IP address, typically set when the
                 // client is using a proxy server.
                 Request::$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
             } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
                 // Use the forwarded IP address, typically set when the
                 // client is using a proxy server.
                 Request::$client_ip = $_SERVER['HTTP_CLIENT_IP'];
             } elseif (isset($_SERVER['REMOTE_ADDR'])) {
                 // The remote IP address
                 Request::$client_ip = $_SERVER['REMOTE_ADDR'];
             }
             if (Request::$method !== 'GET' and Request::$method !== 'POST') {
                 // Methods besides GET and POST do not properly parse the form-encoded
                 // query string into the $_POST array, so we overload it manually.
                 parse_str(file_get_contents('php://input'), $_POST);
             }
             if ($uri === TRUE) {
                 if (!empty($_SERVER['PATH_INFO'])) {
                     // PATH_INFO does not contain the docroot or index
                     $uri = $_SERVER['PATH_INFO'];
                 } else {
                     // REQUEST_URI and PHP_SELF include the docroot and index
                     if (isset($_SERVER['REQUEST_URI'])) {
                         // REQUEST_URI includes the query string, remove it
                         $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
                     } elseif (isset($_SERVER['PHP_SELF'])) {
                         $uri = $_SERVER['PHP_SELF'];
                     } elseif (isset($_SERVER['REDIRECT_URL'])) {
                         $uri = $_SERVER['REDIRECT_URL'];
                     } else {
                         // If you ever see this error, please report an issue at and include a dump of $_SERVER
                         // http://dev.kohanaphp.com/projects/kohana3/issues
                         throw new Kohana_Exception('Unable to detect the URI using PATH_INFO, REQUEST_URI, or PHP_SELF');
                     }
                     // Get the path from the base URL, including the index file
                     $base_url = parse_url(Kohana::$base_url, PHP_URL_PATH);
                     if (strpos($uri, $base_url) === 0) {
                         // Remove the base URL from the URI
                         $uri = substr($uri, strlen($base_url));
                     }
                     if (Kohana::$index_file and strpos($uri, Kohana::$index_file) === 0) {
                         // Remove the index file from the URI
                         $uri = substr($uri, strlen(Kohana::$index_file));
                     }
                 }
             }
         }
         // Reduce multiple slashes to a single slash
         $uri = preg_replace('#//+#', '/', $uri);
         // Remove all dot-paths from the URI, they are not valid
         $uri = preg_replace('#\\.[\\s./]*/#', '', $uri);
         // Create the instance singleton
         Request::$instance = Request::$current = new Request($uri);
         // Add the default Content-Type header
         Request::$instance->headers['Content-Type'] = 'text/html; charset=' . Kohana::$charset;
     }
     return Request::$instance;
 }
Example #11
0
 public static function add_log(Model_User $user, $alias, $message, array $params = array(), $type = "app")
 {
     $values = array('alias' => $alias, 'message' => $message, 'user_id' => $user->id, 'agent' => Request::user_agent(array('browser', 'platform')), 'ip' => Request::$client_ip, 'location' => Request::current()->uri(), 'type' => $type, 'params' => $params);
     return ORM::factory('Log')->values($values)->create();
 }
Example #12
0
 /**
  * Send file download as the response. All execution will be halted when
  * this method is called! Use TRUE for the filename to send the current
  * response as the file content. The third parameter allows the following
  * options to be set:
  *
  * Type      | Option    | Description                        | Default Value
  * ----------|-----------|------------------------------------|--------------
  * `boolean` | inline    | Display inline instead of download | `FALSE`
  * `string`  | mime_type | Manual mime type                   | Automatic
  * `boolean` | delete    | Delete the file after sending      | `FALSE`
  *
  * Download a file that already exists:
  *
  *     $request->send_file('media/packages/kohana.zip');
  *
  * Download generated content as a file:
  *
  *     $request->response = $content;
  *     $request->send_file(TRUE, $filename);
  *
  * [!!] No further processing can be done after this method is called!
  *
  * @param   string   filename with path, or TRUE for the current response
  * @param   string   downloaded file name
  * @param   array    additional options
  * @return  void
  * @throws  Kohana_Exception
  * @uses    File::mime_by_ext
  * @uses    File::mime
  * @uses    Request::send_headers
  */
 public function send_file($filename, $download = NULL, array $options = NULL)
 {
     if (!empty($options['mime_type'])) {
         // The mime-type has been manually set
         $mime = $options['mime_type'];
     }
     if ($filename === TRUE) {
         if (empty($download)) {
             throw new Kohana_Exception('Download name must be provided for streaming files');
         }
         // Temporary files will automatically be deleted
         $options['delete'] = FALSE;
         if (!isset($mime)) {
             // Guess the mime using the file extension
             $mime = File::mime_by_ext(strtolower(pathinfo($download, PATHINFO_EXTENSION)));
         }
         // Force the data to be rendered if
         $file_data = (string) $this->response;
         // Get the content size
         $size = strlen($file_data);
         // Create a temporary file to hold the current response
         $file = tmpfile();
         // Write the current response into the file
         fwrite($file, $file_data);
         // Prepare the file for reading
         fseek($file, 0);
         // File data is no longer needed
         unset($file_data);
     } else {
         // Get the complete file path
         $filename = realpath($filename);
         if (empty($download)) {
             // Use the file name as the download file name
             $download = pathinfo($filename, PATHINFO_BASENAME);
         }
         // Get the file size
         $size = filesize($filename);
         if (!isset($mime)) {
             // Get the mime type
             $mime = File::mime($filename);
         }
         // Open the file for reading
         $file = fopen($filename, 'rb');
     }
     if (!is_resource($file)) {
         throw new Kohana_Exception('Could not read file to send: :file', array(':file' => $download));
     }
     // Inline or download?
     $disposition = empty($options['inline']) ? 'attachment' : 'inline';
     // Set the headers for a download
     $this->headers['Content-Disposition'] = $disposition . '; filename="' . $download . '"';
     $this->headers['Content-Type'] = $mime;
     $this->headers['Content-Length'] = $size;
     if (Request::user_agent('browser') === 'Internet Explorer') {
         // Naturally, IE does not act like a real browser...
         if (Request::$protocol === 'https') {
             // http://support.microsoft.com/kb/316431
             $this->headers['Pragma'] = $this->headers['Cache-Control'] = 'public';
         }
         if (version_compare(Request::user_agent('version'), '8.0', '>=')) {
             // http://ajaxian.com/archives/ie-8-security
             $this->headers['X-Content-Type-Options'] = 'nosniff';
         }
     }
     if (!empty($options['resumable'])) {
         // @todo: ranged download processing
     }
     // Send all headers now
     $this->send_headers();
     while (ob_get_level()) {
         // Flush all output buffers
         ob_end_flush();
     }
     // Manually stop execution
     ignore_user_abort(TRUE);
     // Keep the script running forever
     set_time_limit(0);
     // Send data in 16kb blocks
     $block = 1024 * 16;
     while (!feof($file)) {
         if (connection_aborted()) {
             break;
         }
         // Output a block of the file
         echo fread($file, $block);
         // Send the data now
         flush();
     }
     // Close the file
     fclose($file);
     if (!empty($options['delete'])) {
         try {
             // Attempt to remove the file
             unlink($filename);
         } catch (Exception $e) {
             // Create a text version of the exception
             $error = Kohana::exception_text($e);
             if (is_object(Kohana::$log)) {
                 // Add this exception to the log
                 Kohana::$log->add(Kohana::ERROR, $error);
                 // Make sure the logs are written
                 Kohana::$log->write();
             }
             // Do NOT display the exception, it will corrupt the output!
         }
     }
     // Stop execution
     exit;
 }
Example #13
0
 /**
  * @param string $uri
  * @param array $client_params
  * @param bool $allow_external
  * @param array $injected_routes
  * @internal param \HTTP_Cache $cache
  * @return \HAPI_Request|\Request|void
  */
 public static function factory($uri = '', $client_params = array(), $allow_external = TRUE, $injected_routes = array())
 {
     // If this is the initial request
     if (!Request::$initial) {
         if (isset($_SERVER['SERVER_PROTOCOL'])) {
             $protocol = $_SERVER['SERVER_PROTOCOL'];
         } else {
             $protocol = HTTP::$protocol;
         }
         if (isset($_SERVER['REQUEST_METHOD'])) {
             // Use the server request method
             $method = $_SERVER['REQUEST_METHOD'];
         } else {
             // Default to GET requests
             $method = HTTP_Request::GET;
         }
         if (!empty($_SERVER['HTTPS']) and filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN)) {
             // This request is secure
             $secure = TRUE;
         }
         if (isset($_SERVER['HTTP_REFERER'])) {
             // There is a referrer for this request
             $referrer = $_SERVER['HTTP_REFERER'];
         }
         if (isset($_SERVER['HTTP_USER_AGENT'])) {
             // Browser type
             Request::$user_agent = $_SERVER['HTTP_USER_AGENT'];
         }
         if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
             // Typically used to denote AJAX requests
             $requested_with = $_SERVER['HTTP_X_REQUESTED_WITH'];
         }
         if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) {
             // Use the forwarded IP address, typically set when the
             // client is using a proxy server.
             // Format: "X-Forwarded-For: client1, proxy1, proxy2"
             $client_ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
             Request::$client_ip = array_shift($client_ips);
             unset($client_ips);
         } elseif (isset($_SERVER['HTTP_CLIENT_IP']) and isset($_SERVER['REMOTE_ADDR']) and in_array($_SERVER['REMOTE_ADDR'], Request::$trusted_proxies)) {
             // Use the forwarded IP address, typically set when the
             // client is using a proxy server.
             $client_ips = explode(',', $_SERVER['HTTP_CLIENT_IP']);
             Request::$client_ip = array_shift($client_ips);
             unset($client_ips);
         } elseif (isset($_SERVER['REMOTE_ADDR'])) {
             // The remote IP address
             Request::$client_ip = $_SERVER['REMOTE_ADDR'];
         }
         if ($method !== HTTP_Request::GET) {
             // Ensure the raw body is saved for future use
             $body = file_get_contents('php://input');
         }
         $cookies = array();
         if ($cookie_keys = array_keys($_COOKIE)) {
             foreach ($cookie_keys as $key) {
                 $cookies[$key] = Cookie::get($key);
             }
         }
         // Create the instance singleton
         Request::$initial = $request = new Request($uri, $client_params, $allow_external, $injected_routes);
         // Store global GET and POST data in the initial request only
         $request->protocol($protocol)->query($_GET)->post($_POST);
         if (isset($secure)) {
             // Set the request security
             $request->secure($secure);
         }
         if (isset($method)) {
             // Set the request method
             $request->method($method);
         }
         if (isset($referrer)) {
             // Set the referrer
             $request->referrer($referrer);
         }
         if (isset($requested_with)) {
             // Apply the requested with variable
             $request->requested_with($requested_with);
         }
         if (isset($body)) {
             // Set the request body (probably a PUT type)
             $request->body($body);
         }
         if (isset($cookies)) {
             $request->cookie($cookies);
         }
     } else {
         $request = new HAPI_Request($uri, $client_params, $allow_external, $injected_routes);
     }
     return $request;
 }
Example #14
0
		<footer class="foot">
		</footer>
	</div> <!--! end of #container -->


<!-- Javascript at the bottom for fast page loading -->

<!-- Grab Google CDN's jQuery. fall back to local if necessary -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="<?php 
echo url::base();
?>
js/jquery-1.4.2.min.js"><\/script>')</script>

<script type="text/javascript">
	<? $mobile = (bool) Request::user_agent('mobile'); ?>
	var mobile = <?php 
echo $mobile ? 'true' : 'false';
?>
;
</script>
<!-- Load the google maps JS -->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=<?php 
echo $mobile ? 'true' : 'false';
?>
"></script>

<script src="<?php 
echo url::base();
?>
js/plugins.js?v=1"></script>