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
 /**
  * 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;
 }
Example #3
0
File: auth.php Project: ajb/rfpez
 public function action_create()
 {
     Session::regenerate();
     $credentials = array('username' => Input::get('email'), 'password' => Input::get('password'), 'remember' => Input::has('remember') ? true : false);
     if (Auth::attempt($credentials)) {
         Auth::user()->track_signin();
         if (Auth::user()->banned_at) {
             Auth::logout();
             return Redirect::to('/')->with('errors', array(__("r.flashes.account_banned")));
         }
         if (Input::has('modal') && Request::referrer() != route('signout')) {
             return Redirect::back();
         }
         if (($url = Input::get('redirect_to')) && Input::get('redirect_to') != route('signout')) {
             return Redirect::to($url);
         }
         return Redirect::to('/');
     } else {
         return Redirect::to_route('signin')->with('errors', array(__("r.flashes.login_fail")))->with('redirect_to', Request::referrer())->with_input();
     }
 }
 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 #5
0
 /**
  * Creates a new request object for the given URI. New requests should be
  * created using the [Request::instance] or [Request::factory] methods.
  *
  *     $request = Request::factory($uri);
  *
  * If $cache parameter is set, the response for the request will attempt to
  * be retrieved from the cache.
  *
  * @param   string  $uri URI of the request
  * @param   Cache   $cache
  * @param   array   $injected_routes an array of routes to use, for testing
  * @return  void
  * @throws  Request_Exception
  * @uses    Route::all
  * @uses    Route::matches
  */
 public static function factory($uri = TRUE, HTTP_Cache $cache = NULL, $injected_routes = array())
 {
     // If this is the initial request
     if (!Request::$initial) {
         if (Kohana::$is_cli) {
             // Default protocol for command line is cli://
             $protocol = 'cli';
             // Get the command line options
             $options = CLI::options('uri', 'method', 'get', 'post', 'referrer');
             if (isset($options['uri'])) {
                 // Use the specified URI
                 $uri = $options['uri'];
             } elseif ($uri === TRUE) {
                 $uri = '';
             }
             if (isset($options['method'])) {
                 // Use the specified method
                 $method = strtoupper($options['method']);
             } else {
                 // Default to GET requests
                 $method = HTTP_Request::GET;
             }
             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);
             }
             if (isset($options['referrer'])) {
                 $referrer = $options['referrer'];
             }
         } else {
             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');
             }
             if ($uri === TRUE) {
                 // Attempt to guess the proper URI
                 $uri = Request::detect_uri();
             }
         }
         // Create the instance singleton
         Request::$initial = $request = new Request($uri, $cache);
         // 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);
         }
     } else {
         $request = new Request($uri, $cache, $injected_routes);
     }
     return $request;
 }
Example #6
0
 static function format_404($str, array $info, $page, $error_page)
 {
     $replaces = array('%404_URL%' => URL::current(), '%404_LANG%' => static::lang(), '%404_ROOT%' => static::relative_path($page, $error_page), '%404_PATH%' => $info['root'] . "/{$page}", '%404_PAGE%' => $page, '%404_REFERRER%' => Request::referrer());
     foreach ($replaces as $key => $value) {
         $replaces[substr($key, 0, -1) . 'Q%'] = HTML::entities($value);
     }
     $replaces = array_map('e', $replaces);
     return strtr((string) $str, $replaces);
 }
Example #7
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 #8
0
 /**
  * 
  * Delete release.
  * @param type $id
  * @return type
  */
 public function get_delete($id = null)
 {
     $release = new Deploys();
     $release = $release->findWithProject($id);
     if (!$release) {
         $m = new Messages();
         $m->add('error', "Unknown release!");
         return Redirect::to(handles('orchestra::resources/githubdeploys'))->with('message', $m->serialize());
     }
     $project = (object) $release[0]->relationships['projects'];
     $release = (object) $release[0]->attributes;
     $rel = new \Githubdeploys\Release();
     $response = $rel->deleteRelease($project, $release);
     $m = new Messages();
     if (isset($response['error'])) {
         foreach ($response['error'] as $r) {
             $m->add('error', $r);
         }
     } else {
         foreach ($response['success'] as $r) {
             $m->add('success', $r);
         }
         /*
          * We have to reinitialise $release because it's not an instance
          * of the model \Githubdeploys\Deploys anymore. Read above the code^
          */
         if (Deploys::find($release->id)->delete()) {
             $m->add('success', sprintf("Release %i (%s) has been deleted from the database.", $release->id, $release->release));
         } else {
             $m->add('error', sprintf("Could not delete release %i from the database.", $release->id));
         }
     }
     return Redirect::to(Request::referrer())->with('message', $m->serialize());
 }
Example #9
0
	/**
	 * Main request singleton instance. If no URI is provided, the URI will
	 * be automatically detected.
	 *
	 *     $request = Request::instance();
	 *
	 * @param   string   URI of the request
	 * @return  Request
	 * @uses    Request::detect_uri
	 */
	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)
				{
					$uri = Request::detect_uri();
				}
			}

			// 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 #10
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 #11
0
 /**
  * Main request singleton instance. If no URI is provided, the URI will
  * be automatically detected using PATH_INFO, REQUEST_URI, or PHP_SELF.
  *
  * @param   string   URI of the request
  * @return  Request
  */
 public static function instance(&$uri = FALSE)
 {
     if (Request::$instance === NULL) {
         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 (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 === FALSE) {
                 if (isset($_SERVER['PATH_INFO'])) {
                     // PATH_INFO is most realiable way to handle routing, as it
                     // does not include the document root or index file
                     $uri = $_SERVER['PATH_INFO'];
                 } else {
                     // REQUEST_URI and PHP_SELF both provide the full path,
                     // including the document root and index file
                     if (isset($_SERVER['REQUEST_URI'])) {
                         $uri = $_SERVER['REQUEST_URI'];
                     } elseif (isset($_SERVER['PHP_SELF'])) {
                         $uri = $_SERVER['PHP_SELF'];
                     }
                     if (isset($_SERVER['SCRIPT_NAME']) and strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
                         // Remove the document root and index file from the URI
                         $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
                     }
                 }
             }
         }
         // Create the instance singleton
         Request::$instance = new Request($uri);
     }
     return Request::$instance;
 }