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
 /**
  * Mostramos el template.
  */
 public function __destruct()
 {
     if (is_object($this->template) && !Request::is_ajax() && !Error::$has_error) {
         $this->template->assign('execution', get_readable_file_size(memory_get_peak_usage() - START_MEMORY));
         $this->template->show();
     }
 }
Example #3
0
 /**
  * Constructor
  *
  * @param Request  $request
  * @param Response $response
  */
 public function __construct(Request $request, Response $response)
 {
     // Ajax-like request setting if HMVC call or POST request with param `is_ajax` == `true`
     if ($request->is_ajax() or $request !== Request::initial() or $request->method() === HTTP_Request::POST and $request->post('is_ajax') === 'true') {
         $request->requested_with('xmlhttprequest');
     }
     parent::__construct($request, $response);
 }
Example #4
0
 public function send()
 {
     die(json_encode($this));
     if (Request::is_ajax()) {
         header('Cache-Control: no-cache, must-revalidate');
         header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
         header('Content-type: application/json');
         die(json_encode($this));
     }
     die($this->msg);
 }
Example #5
0
 /**
  * Renders all subviews passed in. Handles nested views within arrays. Uses recursion / No depth restriction.
  * 
  * @param array $subviews An array of subviews. Can contain nested arrays with additional views.
  * @return string All HTML from the subviews returned as a big string of HTML.
  */
 public function render($subviews, $section = NULL)
 {
     $html = '';
     if (!is_array($subviews)) {
         $subviews = array($subviews);
     }
     if (!is_null($section) and !is_array($section)) {
         $section = array($section);
     }
     if (is_array($section)) {
         $section = array_map('strtolower', $section);
     }
     foreach ($subviews as $subview) {
         // Handle nested views
         if (is_array($subview)) {
             self::render($subview, $section);
         } else {
             if (!$subview instanceof View) {
                 kohana::log('error', 'Can not render a subview that is not an instance of View');
                 continue;
             }
             if (isset($subview->render_conditional)) {
                 $renderConditions = $subview->render_conditional;
                 if (!is_array($renderConditions)) {
                     $renderConditions = array($renderConditions => FALSE);
                 }
                 if (Request::is_ajax()) {
                     if (isset($renderConditions['ajax']) and $renderConditions['ajax'] === FALSE) {
                         kohana::log('debug', 'Not rendering view due to ajax request');
                         continue;
                     }
                 }
                 if (isset($_REQUEST['qtipAjaxForm']) and $renderConditions['qtipAjaxForm'] === FALSE) {
                     kohana::log('debug', 'Not rendering view due to qtipAjaxForm request');
                     continue;
                 }
             }
             if (is_null($section)) {
                 $html .= $subview->render();
                 continue;
             }
             if (!isset($subview->section)) {
                 continue;
             }
             $subviewSection = strtolower($subview->section);
             if (in_array($subviewSection, $section)) {
                 $html .= $subview->render();
             }
         }
     }
     return $html;
 }
Example #6
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 #7
0
 public function after($controller, $metadata, &$data)
 {
     if ($metadata->or_ajax == 'true') {
         $ismobile = Request::is_ajax();
     } else {
         $ismobile = Dispatcher::RequestType() == 'mobile' && Request::is_ajax();
     }
     if ($ismobile) {
         if ($metadata->key) {
             $data = $data[$metadata->key];
         }
         $data = Model::Flatten($data);
         echo json_encode($data);
         die;
     }
 }
Example #8
0
 /**
  * Выброс ошибки 503 - Сервис недоступен
  *
  * @param string $detals - опциональное сообщенние
  */
 public static function e503($detals = null)
 {
     Header::h503();
     ob_end_clean_all();
     if (!Request::is_ajax()) {
         echo View::make('error.e503')->with('detals', $detals)->compile();
     } else {
         $outputData = array();
         $outputData['status'] = '503 Service Unavailable';
         if (!is_null($detals)) {
             $outputData['detals'] = $detals;
         }
         echo json_encode($outputData, JSON_UNESCAPED_SLASHES);
     }
     exit;
 }
Example #9
0
 public static function load($uri, $as_ajax = FALSE)
 {
     if ($as_ajax) {
         $ajax = Request::$is_ajax;
         Request::$is_ajax = TRUE;
         $request = Request::factory($uri);
         try {
             $request->execute()->send_headers()->response;
         } catch (Error404_Exception $e) {
             $request = Request::factory('404')->execute();
         }
         Request::$is_ajax = $ajax;
         return $request;
     }
     return Request::factory($uri)->execute()->send_headers()->response;
 }
Example #10
0
 /**
  * This function sets a generic message with options
  *
  * @param string the message to display
  * @param array a list of options to apply to the message
  * @return void
  */
 public static function set($text = '', $options = array())
 {
     if (!is_array($options)) {
         $options = array('type' => $options);
     }
     // populate the defualt options
     $options += array('key' => 'default', 'type' => 'error', 'translate' => TRUE, 'redirect' => FALSE);
     // if this is an unknown type then invalid
     if (!in_array($options['type'], self::$logLevels)) {
         kohana::log('error', 'Invalid message type ' . $options['type']);
         return FALSE;
     }
     // if there is no message then invalid
     if (empty($text)) {
         kohana::log('error', 'A message must be supplied.');
         return FALSE;
     }
     // log a success message as debug
     if ($options['type'] == 'success') {
         kohana::log('debug', $options['key'] . ' - ' . $text);
     } else {
         kohana::log($options['type'], $options['key'] . ' - ' . $text);
     }
     // translate the message unless we have been asked not to
     if (empty($options['translate'])) {
         $options['text'] = $text;
     } else {
         $options['text'] = __($text);
     }
     // append this message into any existing session bluebox_messages
     $currentMessages = Session::instance()->get('bluebox_message', array());
     $currentMessages[] = $options;
     Session::instance()->set('bluebox_message', $currentMessages);
     // If given a redirect URL then go to it
     if (!empty($options['redirect']) && !Request::is_ajax()) {
         url::redirect($options['redirect']);
     }
     return TRUE;
 }
function is_ajax()
{
    return Request::is_ajax();
}
Example #12
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 #13
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 #14
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;
 }
Example #15
0
function my_cache_key_generator(Request $request)
{
    $is_ajax = $request->is_ajax() ? '~ajax' : '';
    $uri = $request->uri();
    $query = $request->query();
    if (!empty($query['region'])) {
        unset($query['region']);
    }
    $region = $request->site_code;
    return sha1($uri . '?' . http_build_query($query, NULL, '&') . ':' . $region . $is_ajax);
}