code stringlengths 1 2.01M | repo_name stringlengths 3 62 | path stringlengths 1 267 | language stringclasses 231
values | license stringclasses 13
values | size int64 1 2.01M |
|---|---|---|---|---|---|
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_Exception extends Kohana_Kohana_Exception {}
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/exception.php | PHP | mit | 120 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* File-based configuration reader. Multiple configuration directories can be
* used by attaching multiple instances of this class to [Config].
*
* @package Kohana
* @category Configuration
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Config_File extends Config_Reader {
/**
* @var string Configuration group name
*/
protected $_configuration_group;
/**
* @var bool Has the config group changed?
*/
protected $_configuration_modified = FALSE;
public function __construct($directory = 'config')
{
// Set the configuration directory name
$this->_directory = trim($directory, '/');
// Load the empty array
parent::__construct();
}
/**
* Load and merge all of the configuration files in this group.
*
* $config->load($name);
*
* @param string configuration group name
* @param array configuration array
* @return $this clone of the current object
* @uses Kohana::load
*/
public function load($group, array $config = NULL)
{
if ($files = Kohana::find_file($this->_directory, $group, NULL, TRUE))
{
// Initialize the config array
$config = array();
foreach ($files as $file)
{
// Merge each file to the configuration array
$config = Arr::merge($config, Kohana::load($file));
}
}
return parent::load($group, $config);
}
} // End Kohana_Config_File
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/config/file.php | PHP | mit | 1,510 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Abstract configuration reader. All configuration readers must extend
* this class.
*
* @package Kohana
* @category Configuration
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class Kohana_Config_Reader extends ArrayObject {
/**
* @var string Configuration group name
*/
protected $_configuration_group;
/**
* Loads an empty array as the initial configuration and enables array
* keys to be used as properties.
*
* @return void
*/
public function __construct()
{
parent::__construct(array(), ArrayObject::ARRAY_AS_PROPS);
}
/**
* Return the current group in serialized form.
*
* echo $config;
*
* @return string
*/
public function __toString()
{
return serialize($this->getArrayCopy());
}
/**
* Loads a configuration group.
*
* $config->load($name, $array);
*
* This method must be extended by all readers. After the group has been
* loaded, call `parent::load($group, $config)` for final preparation.
*
* @param string configuration group name
* @param array configuration array
* @return $this a clone of this object
*/
public function load($group, array $config = NULL)
{
if ($config === NULL)
{
return FALSE;
}
// Clone the current object
$object = clone $this;
// Set the group name
$object->_configuration_group = $group;
// Swap the array with the actual configuration
$object->exchangeArray($config);
return $object;
}
/**
* Return the raw array that is being used for this object.
*
* $array = $config->as_array();
*
* @return array
*/
public function as_array()
{
return $this->getArrayCopy();
}
/**
* Get a variable from the configuration or return the default value.
*
* $value = $config->get($key);
*
* @param string array key
* @param mixed default value
* @return mixed
*/
public function get($key, $default = NULL)
{
return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
}
/**
* Sets a value in the configuration array.
*
* $config->set($key, $new_value);
*
* @param string array key
* @param mixed array value
* @return $this
*/
public function set($key, $value)
{
$this->offsetSet($key, $value);
return $this;
}
} // End Kohana_Config_Reader
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/config/reader.php | PHP | mit | 2,458 |
<?php defined('SYSPATH') or die('No direct access');
/**
* Kohana exception class. Translates exceptions using the [I18n] class.
*
* @package Kohana
* @category Exceptions
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Kohana_Exception extends Exception {
/**
* @var array PHP error code => human readable name
*/
public static $php_errors = array(
E_ERROR => 'Fatal Error',
E_USER_ERROR => 'User Error',
E_PARSE => 'Parse Error',
E_WARNING => 'Warning',
E_USER_WARNING => 'User Warning',
E_STRICT => 'Strict',
E_NOTICE => 'Notice',
E_RECOVERABLE_ERROR => 'Recoverable Error',
);
/**
* @var string error rendering view
*/
public static $error_view = 'kohana/error';
/**
* Creates a new translated exception.
*
* throw new Kohana_Exception('Something went terrible wrong, :user',
* array(':user' => $user));
*
* @param string error message
* @param array translation variables
* @param integer the exception code
* @return void
*/
public function __construct($message, array $variables = NULL, $code = 0)
{
if (defined('E_DEPRECATED'))
{
// E_DEPRECATED only exists in PHP >= 5.3.0
Kohana_Exception::$php_errors[E_DEPRECATED] = 'Deprecated';
}
// Set the message
$message = __($message, $variables);
// Pass the message to the parent
parent::__construct($message, $code);
}
/**
* Magic object-to-string method.
*
* echo $exception;
*
* @uses Kohana_Exception::text
* @return string
*/
public function __toString()
{
return Kohana_Exception::text($this);
}
/**
* Inline exception handler, displays the error message, source of the
* exception, and the stack trace of the error.
*
* @uses Kohana_Exception::text
* @param object exception object
* @return boolean
*/
public static function handler(Exception $e)
{
try
{
// Get the exception information
$type = get_class($e);
$code = $e->getCode();
$message = $e->getMessage();
$file = $e->getFile();
$line = $e->getLine();
// Get the exception backtrace
$trace = $e->getTrace();
if ($e instanceof ErrorException)
{
if (isset(Kohana_Exception::$php_errors[$code]))
{
// Use the human-readable error name
$code = Kohana_Exception::$php_errors[$code];
}
if (version_compare(PHP_VERSION, '5.3', '<'))
{
// Workaround for a bug in ErrorException::getTrace() that exists in
// all PHP 5.2 versions. @see http://bugs.php.net/bug.php?id=45895
for ($i = count($trace) - 1; $i > 0; --$i)
{
if (isset($trace[$i - 1]['args']))
{
// Re-position the args
$trace[$i]['args'] = $trace[$i - 1]['args'];
// Remove the args
unset($trace[$i - 1]['args']);
}
}
}
}
// 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(Log::ERROR, $error);
// Make sure the logs are written
Kohana::$log->write();
}
if (Kohana::$is_cli)
{
// Just display the text of the exception
echo "\n{$error}\n";
return TRUE;
}
if ( ! headers_sent())
{
// Make sure the proper http header is sent
$http_header_status = ($e instanceof HTTP_Exception) ? $code : 500;
header('Content-Type: text/html; charset='.Kohana::$charset, TRUE, $http_header_status);
}
// Start an output buffer
ob_start();
// Include the exception HTML
if ($view_file = Kohana::find_file('views', Kohana_Exception::$error_view))
{
include $view_file;
}
else
{
throw new Kohana_Exception('Error view file does not exist: views/:file', array(
':file' => Kohana_Exception::$error_view,
));
}
// Display the contents of the output buffer
echo ob_get_clean();
return TRUE;
}
catch (Exception $e)
{
// Clean the output buffer if one exists
ob_get_level() and ob_clean();
// Display the exception text
echo Kohana_Exception::text($e), "\n";
// Exit with an error status
exit(1);
}
}
/**
* Get a single line of text representing the exception:
*
* Error [ Code ]: Message ~ File [ Line ]
*
* @param object Exception
* @return string
*/
public static function text(Exception $e)
{
return sprintf('%s [ %s ]: %s ~ %s [ %d ]',
get_class($e), $e->getCode(), strip_tags($e->getMessage()), Debug::path($e->getFile()), $e->getLine());
}
} // End Kohana_Exception
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/kohana/exception.php | PHP | mit | 4,721 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Security helper class.
*
* @package Kohana
* @category Security
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Security {
/**
* @var string key name used for token storage
*/
public static $token_name = 'security_token';
/**
* Generate and store a unique token which can be used to help prevent
* [CSRF](http://wikipedia.org/wiki/Cross_Site_Request_Forgery) attacks.
*
* $token = Security::token();
*
* You can insert this token into your forms as a hidden field:
*
* echo Form::hidden('csrf', Security::token());
*
* And then check it when using [Validation]:
*
* $array->rules('csrf', array(
* 'not_empty' => NULL,
* 'Security::check' => NULL,
* ));
*
* This provides a basic, but effective, method of preventing CSRF attacks.
*
* @param boolean force a new token to be generated?
* @return string
* @uses Session::instance
*/
public static function token($new = FALSE)
{
$session = Session::instance();
// Get the current token
$token = $session->get(Security::$token_name);
if ($new === TRUE OR ! $token)
{
// Generate a new unique token
$token = sha1(uniqid(NULL, TRUE));
// Store the new token
$session->set(Security::$token_name, $token);
}
return $token;
}
/**
* Check that the given token matches the currently stored security token.
*
* if (Security::check($token))
* {
* // Pass
* }
*
* @param string token to check
* @return boolean
* @uses Security::token
*/
public static function check($token)
{
return Security::token() === $token;
}
/**
* Remove image tags from a string.
*
* $str = Security::strip_image_tags($str);
*
* @param string string to sanitize
* @return string
*/
public static function strip_image_tags($str)
{
return preg_replace('#<img\s.*?(?:src\s*=\s*["\']?([^"\'<>\s]*)["\']?[^>]*)?>#is', '$1', $str);
}
/**
* Encodes PHP tags in a string.
*
* $str = Security::encode_php_tags($str);
*
* @param string string to sanitize
* @return string
*/
public static function encode_php_tags($str)
{
return str_replace(array('<?', '?>'), array('<?', '?>'), $str);
}
} // End security
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/security.php | PHP | mit | 2,424 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Acts as an object wrapper for HTML pages with embedded PHP, called "views".
* Variables can be assigned with the view object and referenced locally within
* the view.
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_View {
// Array of global variables
protected static $_global_data = array();
/**
* Returns a new View object. If you do not define the "file" parameter,
* you must call [View::set_filename].
*
* $view = View::factory($file);
*
* @param string view filename
* @param array array of values
* @return View
*/
public static function factory($file = NULL, array $data = NULL)
{
return new View($file, $data);
}
/**
* Captures the output that is generated when a view is included.
* The view data will be extracted to make local variables. This method
* is static to prevent object scope resolution.
*
* $output = View::capture($file, $data);
*
* @param string filename
* @param array variables
* @return string
*/
protected static function capture($kohana_view_filename, array $kohana_view_data)
{
// Import the view variables to local namespace
extract($kohana_view_data, EXTR_SKIP);
if (View::$_global_data)
{
// Import the global view variables to local namespace
extract(View::$_global_data, EXTR_SKIP);
}
// Capture the view output
ob_start();
try
{
// Load the view within the current scope
include $kohana_view_filename;
}
catch (Exception $e)
{
// Delete the output buffer
ob_end_clean();
// Re-throw the exception
throw $e;
}
// Get the captured output and close the buffer
return ob_get_clean();
}
/**
* Sets a global variable, similar to [View::set], except that the
* variable will be accessible to all views.
*
* View::set_global($name, $value);
*
* @param string variable name or an array of variables
* @param mixed value
* @return void
*/
public static function set_global($key, $value = NULL)
{
if (is_array($key))
{
foreach ($key as $key2 => $value)
{
View::$_global_data[$key2] = $value;
}
}
else
{
View::$_global_data[$key] = $value;
}
}
/**
* Assigns a global variable by reference, similar to [View::bind], except
* that the variable will be accessible to all views.
*
* View::bind_global($key, $value);
*
* @param string variable name
* @param mixed referenced variable
* @return void
*/
public static function bind_global($key, & $value)
{
View::$_global_data[$key] =& $value;
}
// View filename
protected $_file;
// Array of local variables
protected $_data = array();
/**
* Sets the initial view filename and local data. Views should almost
* always only be created using [View::factory].
*
* $view = new View($file);
*
* @param string view filename
* @param array array of values
* @return void
* @uses View::set_filename
*/
public function __construct($file = NULL, array $data = NULL)
{
if ($file !== NULL)
{
$this->set_filename($file);
}
if ($data !== NULL)
{
// Add the values to the current data
$this->_data = $data + $this->_data;
}
}
/**
* Magic method, searches for the given variable and returns its value.
* Local variables will be returned before global variables.
*
* $value = $view->foo;
*
* [!!] If the variable has not yet been set, an exception will be thrown.
*
* @param string variable name
* @return mixed
* @throws Kohana_Exception
*/
public function & __get($key)
{
if (array_key_exists($key, $this->_data))
{
return $this->_data[$key];
}
elseif (array_key_exists($key, View::$_global_data))
{
return View::$_global_data[$key];
}
else
{
throw new Kohana_Exception('View variable is not set: :var',
array(':var' => $key));
}
}
/**
* Magic method, calls [View::set] with the same parameters.
*
* $view->foo = 'something';
*
* @param string variable name
* @param mixed value
* @return void
*/
public function __set($key, $value)
{
$this->set($key, $value);
}
/**
* Magic method, determines if a variable is set.
*
* isset($view->foo);
*
* [!!] `NULL` variables are not considered to be set by [isset](http://php.net/isset).
*
* @param string variable name
* @return boolean
*/
public function __isset($key)
{
return (isset($this->_data[$key]) OR isset(View::$_global_data[$key]));
}
/**
* Magic method, unsets a given variable.
*
* unset($view->foo);
*
* @param string variable name
* @return void
*/
public function __unset($key)
{
unset($this->_data[$key], View::$_global_data[$key]);
}
/**
* Magic method, returns the output of [View::render].
*
* @return string
* @uses View::render
*/
public function __toString()
{
try
{
return $this->render();
}
catch (Exception $e)
{
// Display the exception message
Kohana_Exception::handler($e);
return '';
}
}
/**
* Sets the view filename.
*
* $view->set_filename($file);
*
* @param string view filename
* @return View
* @throws Kohana_View_Exception
*/
public function set_filename($file)
{
if (($path = Kohana::find_file('views', $file)) === FALSE)
{
throw new Kohana_View_Exception('The requested view :file could not be found', array(
':file' => $file,
));
}
// Store the file path locally
$this->_file = $path;
return $this;
}
/**
* Assigns a variable by name. Assigned values will be available as a
* variable within the view file:
*
* // This value can be accessed as $foo within the view
* $view->set('foo', 'my value');
*
* You can also use an array to set several values at once:
*
* // Create the values $food and $beverage in the view
* $view->set(array('food' => 'bread', 'beverage' => 'water'));
*
* @param string variable name or an array of variables
* @param mixed value
* @return $this
*/
public function set($key, $value = NULL)
{
if (is_array($key))
{
foreach ($key as $name => $value)
{
$this->_data[$name] = $value;
}
}
else
{
$this->_data[$key] = $value;
}
return $this;
}
/**
* Assigns a value by reference. The benefit of binding is that values can
* be altered without re-setting them. It is also possible to bind variables
* before they have values. Assigned values will be available as a
* variable within the view file:
*
* // This reference can be accessed as $ref within the view
* $view->bind('ref', $bar);
*
* @param string variable name
* @param mixed referenced variable
* @return $this
*/
public function bind($key, & $value)
{
$this->_data[$key] =& $value;
return $this;
}
/**
* Renders the view object to a string. Global and local data are merged
* and extracted to create local variables within the view file.
*
* $output = $view->render();
*
* [!!] Global variables with the same key name as local variables will be
* overwritten by the local variable.
*
* @param string view filename
* @return string
* @throws Kohana_View_Exception
* @uses View::capture
*/
public function render($file = NULL)
{
if ($file !== NULL)
{
$this->set_filename($file);
}
if (empty($this->_file))
{
throw new Kohana_View_Exception('You must set the file to use within your view before rendering');
}
// Combine local and global data and capture the output
return View::capture($this->_file, $this->_data);
}
} // End View
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/view.php | PHP | mit | 7,828 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Model base class. All models should extend this class.
*
* @package Kohana
* @category Models
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class Kohana_Model {
/**
* Create a new model instance.
*
* $model = Model::factory($name);
*
* @param string model name
* @return Model
*/
public static function factory($name)
{
// Add the model prefix
$class = 'Model_'.$name;
return new $class;
}
} // End Model
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/model.php | PHP | mit | 602 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Date helper.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Date {
// Second amounts for various time increments
const YEAR = 31556926;
const MONTH = 2629744;
const WEEK = 604800;
const DAY = 86400;
const HOUR = 3600;
const MINUTE = 60;
// Available formats for Date::months()
const MONTHS_LONG = '%B';
const MONTHS_SHORT = '%b';
/**
* Default timestamp format for formatted_time
* @var string
*/
public static $timestamp_format = 'Y-m-d H:i:s';
/**
* Timezone for formatted_time
* @link http://uk2.php.net/manual/en/timezones.php
* @var string
*/
public static $timezone;
/**
* Returns the offset (in seconds) between two time zones. Use this to
* display dates to users in different time zones.
*
* $seconds = Date::offset('America/Chicago', 'GMT');
*
* [!!] A list of time zones that PHP supports can be found at
* <http://php.net/timezones>.
*
* @param string timezone that to find the offset of
* @param string timezone used as the baseline
* @param mixed UNIX timestamp or date string
* @return integer
*/
public static function offset($remote, $local = NULL, $now = NULL)
{
if ($local === NULL)
{
// Use the default timezone
$local = date_default_timezone_get();
}
if (is_int($now))
{
// Convert the timestamp into a string
$now = date(DateTime::RFC2822, $now);
}
// Create timezone objects
$zone_remote = new DateTimeZone($remote);
$zone_local = new DateTimeZone($local);
// Create date objects from timezones
$time_remote = new DateTime($now, $zone_remote);
$time_local = new DateTime($now, $zone_local);
// Find the offset
$offset = $zone_remote->getOffset($time_remote) - $zone_local->getOffset($time_local);
return $offset;
}
/**
* Number of seconds in a minute, incrementing by a step. Typically used as
* a shortcut for generating a list that can used in a form.
*
* $seconds = Date::seconds(); // 01, 02, 03, ..., 58, 59, 60
*
* @param integer amount to increment each step by, 1 to 30
* @param integer start value
* @param integer end value
* @return array A mirrored (foo => foo) array from 1-60.
*/
public static function seconds($step = 1, $start = 0, $end = 60)
{
// Always integer
$step = (int) $step;
$seconds = array();
for ($i = $start; $i < $end; $i += $step)
{
$seconds[$i] = sprintf('%02d', $i);
}
return $seconds;
}
/**
* Number of minutes in an hour, incrementing by a step. Typically used as
* a shortcut for generating a list that can be used in a form.
*
* $minutes = Date::minutes(); // 05, 10, 15, ..., 50, 55, 60
*
* @uses Date::seconds
* @param integer amount to increment each step by, 1 to 30
* @return array A mirrored (foo => foo) array from 1-60.
*/
public static function minutes($step = 5)
{
// Because there are the same number of minutes as seconds in this set,
// we choose to re-use seconds(), rather than creating an entirely new
// function. Shhhh, it's cheating! ;) There are several more of these
// in the following methods.
return Date::seconds($step);
}
/**
* Number of hours in a day. Typically used as a shortcut for generating a
* list that can be used in a form.
*
* $hours = Date::hours(); // 01, 02, 03, ..., 10, 11, 12
*
* @param integer amount to increment each step by
* @param boolean use 24-hour time
* @param integer the hour to start at
* @return array A mirrored (foo => foo) array from start-12 or start-23.
*/
public static function hours($step = 1, $long = FALSE, $start = NULL)
{
// Default values
$step = (int) $step;
$long = (bool) $long;
$hours = array();
// Set the default start if none was specified.
if ($start === NULL)
{
$start = ($long === FALSE) ? 1 : 0;
}
$hours = array();
// 24-hour time has 24 hours, instead of 12
$size = ($long === TRUE) ? 23 : 12;
for ($i = $start; $i <= $size; $i += $step)
{
$hours[$i] = (string) $i;
}
return $hours;
}
/**
* Returns AM or PM, based on a given hour (in 24 hour format).
*
* $type = Date::ampm(12); // PM
* $type = Date::ampm(1); // AM
*
* @param integer number of the hour
* @return string
*/
public static function ampm($hour)
{
// Always integer
$hour = (int) $hour;
return ($hour > 11) ? 'PM' : 'AM';
}
/**
* Adjusts a non-24-hour number into a 24-hour number.
*
* $hour = Date::adjust(3, 'pm'); // 15
*
* @param integer hour to adjust
* @param string AM or PM
* @return string
*/
public static function adjust($hour, $ampm)
{
$hour = (int) $hour;
$ampm = strtolower($ampm);
switch ($ampm)
{
case 'am':
if ($hour == 12)
{
$hour = 0;
}
break;
case 'pm':
if ($hour < 12)
{
$hour += 12;
}
break;
}
return sprintf('%02d', $hour);
}
/**
* Number of days in a given month and year. Typically used as a shortcut
* for generating a list that can be used in a form.
*
* Date::days(4, 2010); // 1, 2, 3, ..., 28, 29, 30
*
* @param integer number of month
* @param integer number of year to check month, defaults to the current year
* @return array A mirrored (foo => foo) array of the days.
*/
public static function days($month, $year = FALSE)
{
static $months;
if ($year === FALSE)
{
// Use the current year by default
$year = date('Y');
}
// Always integers
$month = (int) $month;
$year = (int) $year;
// We use caching for months, because time functions are used
if (empty($months[$year][$month]))
{
$months[$year][$month] = array();
// Use date to find the number of days in the given month
$total = date('t', mktime(1, 0, 0, $month, 1, $year)) + 1;
for ($i = 1; $i < $total; $i++)
{
$months[$year][$month][$i] = (string) $i;
}
}
return $months[$year][$month];
}
/**
* Number of months in a year. Typically used as a shortcut for generating
* a list that can be used in a form.
*
* By default a mirrored array of $month_number => $month_number is returned
*
* Date::months();
* // aray(1 => 1, 2 => 2, 3 => 3, ..., 12 => 12)
*
* But you can customise this by passing in either Date::MONTHS_LONG
*
* Date::months(Date::MONTHS_LONG);
* // array(1 => 'January', 2 => 'February', ..., 12 => 'December')
*
* Or Date::MONTHS_SHORT
*
* Date::months(Date::MONTHS_SHORT);
* // array(1 => 'Jan', 2 => 'Feb', ..., 12 => 'Dec')
*
* @uses Date::hours
* @param string The format to use for months
* @return array An array of months based on the specified format
*/
public static function months($format = NULL)
{
$months = array();
if ($format === DATE::MONTHS_LONG OR $format === DATE::MONTHS_SHORT)
{
for ($i = 1; $i <= 12; ++$i)
{
$months[$i] = strftime($format, mktime(0, 0, 0, $i, 1));
}
}
else
{
$months = Date::hours();
}
return $months;
}
/**
* Returns an array of years between a starting and ending year. By default,
* the the current year - 5 and current year + 5 will be used. Typically used
* as a shortcut for generating a list that can be used in a form.
*
* $years = Date::years(2000, 2010); // 2000, 2001, ..., 2009, 2010
*
* @param integer starting year (default is current year - 5)
* @param integer ending year (default is current year + 5)
* @return array
*/
public static function years($start = FALSE, $end = FALSE)
{
// Default values
$start = ($start === FALSE) ? (date('Y') - 5) : (int) $start;
$end = ($end === FALSE) ? (date('Y') + 5) : (int) $end;
$years = array();
for ($i = $start; $i <= $end; $i++)
{
$years[$i] = (string) $i;
}
return $years;
}
/**
* Returns time difference between two timestamps, in human readable format.
* If the second timestamp is not given, the current time will be used.
* Also consider using [Date::fuzzy_span] when displaying a span.
*
* $span = Date::span(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2)
* $span = Date::span(60, 182, 'minutes'); // 2
*
* @param integer timestamp to find the span of
* @param integer timestamp to use as the baseline
* @param string formatting string
* @return string when only a single output is requested
* @return array associative list of all outputs requested
*/
public static function span($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
{
// Normalize output
$output = trim(strtolower( (string) $output));
if ( ! $output)
{
// Invalid output
return FALSE;
}
// Array with the output formats
$output = preg_split('/[^a-z]+/', $output);
// Convert the list of outputs to an associative array
$output = array_combine($output, array_fill(0, count($output), 0));
// Make the output values into keys
extract(array_flip($output), EXTR_SKIP);
if ($local === NULL)
{
// Calculate the span from the current time
$local = time();
}
// Calculate timespan (seconds)
$timespan = abs($remote - $local);
if (isset($output['years']))
{
$timespan -= Date::YEAR * ($output['years'] = (int) floor($timespan / Date::YEAR));
}
if (isset($output['months']))
{
$timespan -= Date::MONTH * ($output['months'] = (int) floor($timespan / Date::MONTH));
}
if (isset($output['weeks']))
{
$timespan -= Date::WEEK * ($output['weeks'] = (int) floor($timespan / Date::WEEK));
}
if (isset($output['days']))
{
$timespan -= Date::DAY * ($output['days'] = (int) floor($timespan / Date::DAY));
}
if (isset($output['hours']))
{
$timespan -= Date::HOUR * ($output['hours'] = (int) floor($timespan / Date::HOUR));
}
if (isset($output['minutes']))
{
$timespan -= Date::MINUTE * ($output['minutes'] = (int) floor($timespan / Date::MINUTE));
}
// Seconds ago, 1
if (isset($output['seconds']))
{
$output['seconds'] = $timespan;
}
if (count($output) === 1)
{
// Only a single output was requested, return it
return array_pop($output);
}
// Return array
return $output;
}
/**
* Returns the difference between a time and now in a "fuzzy" way.
* Displaying a fuzzy time instead of a date is usually faster to read and understand.
*
* $span = Date::fuzzy_span(time() - 10); // "moments ago"
* $span = Date::fuzzy_span(time() + 20); // "in moments"
*
* A second parameter is available to manually set the "local" timestamp,
* however this parameter shouldn't be needed in normal usage and is only
* included for unit tests
*
* @param integer "remote" timestamp
* @param integer "local" timestamp, defaults to time()
* @return string
*/
public static function fuzzy_span($timestamp, $local_timestamp = NULL)
{
$local_timestamp = ($local_timestamp === NULL) ? time() : (int) $local_timestamp;
// Determine the difference in seconds
$offset = abs($local_timestamp - $timestamp);
if ($offset <= Date::MINUTE)
{
$span = 'moments';
}
elseif ($offset < (Date::MINUTE * 20))
{
$span = 'a few minutes';
}
elseif ($offset < Date::HOUR)
{
$span = 'less than an hour';
}
elseif ($offset < (Date::HOUR * 4))
{
$span = 'a couple of hours';
}
elseif ($offset < Date::DAY)
{
$span = 'less than a day';
}
elseif ($offset < (Date::DAY * 2))
{
$span = 'about a day';
}
elseif ($offset < (Date::DAY * 4))
{
$span = 'a couple of days';
}
elseif ($offset < Date::WEEK)
{
$span = 'less than a week';
}
elseif ($offset < (Date::WEEK * 2))
{
$span = 'about a week';
}
elseif ($offset < Date::MONTH)
{
$span = 'less than a month';
}
elseif ($offset < (Date::MONTH * 2))
{
$span = 'about a month';
}
elseif ($offset < (Date::MONTH * 4))
{
$span = 'a couple of months';
}
elseif ($offset < Date::YEAR)
{
$span = 'less than a year';
}
elseif ($offset < (Date::YEAR * 2))
{
$span = 'about a year';
}
elseif ($offset < (Date::YEAR * 4))
{
$span = 'a couple of years';
}
elseif ($offset < (Date::YEAR * 8))
{
$span = 'a few years';
}
elseif ($offset < (Date::YEAR * 12))
{
$span = 'about a decade';
}
elseif ($offset < (Date::YEAR * 24))
{
$span = 'a couple of decades';
}
elseif ($offset < (Date::YEAR * 64))
{
$span = 'several decades';
}
else
{
$span = 'a long time';
}
if ($timestamp <= $local_timestamp)
{
// This is in the past
return $span.' ago';
}
else
{
// This in the future
return 'in '.$span;
}
}
/**
* Converts a UNIX timestamp to DOS format. There are very few cases where
* this is needed, but some binary formats use it (eg: zip files.)
* Converting the other direction is done using {@link Date::dos2unix}.
*
* $dos = Date::unix2dos($unix);
*
* @param integer UNIX timestamp
* @return integer
*/
public static function unix2dos($timestamp = FALSE)
{
$timestamp = ($timestamp === FALSE) ? getdate() : getdate($timestamp);
if ($timestamp['year'] < 1980)
{
return (1 << 21 | 1 << 16);
}
$timestamp['year'] -= 1980;
// What voodoo is this? I have no idea... Geert can explain it though,
// and that's good enough for me.
return ($timestamp['year'] << 25 | $timestamp['mon'] << 21 |
$timestamp['mday'] << 16 | $timestamp['hours'] << 11 |
$timestamp['minutes'] << 5 | $timestamp['seconds'] >> 1);
}
/**
* Converts a DOS timestamp to UNIX format.There are very few cases where
* this is needed, but some binary formats use it (eg: zip files.)
* Converting the other direction is done using {@link Date::unix2dos}.
*
* $unix = Date::dos2unix($dos);
*
* @param integer DOS timestamp
* @return integer
*/
public static function dos2unix($timestamp = FALSE)
{
$sec = 2 * ($timestamp & 0x1f);
$min = ($timestamp >> 5) & 0x3f;
$hrs = ($timestamp >> 11) & 0x1f;
$day = ($timestamp >> 16) & 0x1f;
$mon = ($timestamp >> 21) & 0x0f;
$year = ($timestamp >> 25) & 0x7f;
return mktime($hrs, $min, $sec, $mon, $day, $year + 1980);
}
/**
* Returns a date/time string with the specified timestamp format
*
* $time = Date::formatted_time('5 minutes ago');
*
* @see http://php.net/manual/en/datetime.construct.php
* @param string datetime_str datetime string
* @param string timestamp_format timestamp format
* @return string
*/
public static function formatted_time($datetime_str = 'now', $timestamp_format = NULL, $timezone = NULL)
{
$timestamp_format = ($timestamp_format == NULL) ? Date::$timestamp_format : $timestamp_format;
$timezone = ($timezone === NULL) ? Date::$timezone : $timezone;
$time = new DateTime($datetime_str, new DateTimeZone(
$timezone ? $timezone : date_default_timezone_get()
));
return $time->format($timestamp_format);
}
} // End date
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/date.php | PHP | mit | 15,244 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Contains the most low-level helpers methods in Kohana:
*
* - Environment initialization
* - Locating files within the cascading filesystem
* - Auto-loading and transparent extension of classes
* - Variable and path debugging
*
* @package Kohana
* @category HTTP
* @author Kohana Team
* @since 3.1.0
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
abstract class Kohana_HTTP {
/**
* @var The default protocol to use if it cannot be detected
*/
public static $protocol = 'http';
/**
* @var The default protocol version to use if cannot be detected
*/
public static $version = '1.1';
/**
* Parses a HTTP header string into an associative array
*
* @param string $header_string Header string to parse
* @return HTTP_Header
*/
public static function parse_header_string($header_string)
{
// If the PECL HTTP extension is loaded
if (extension_loaded('http'))
{
// Use the fast method to parse header string
return new HTTP_Header(http_parse_headers($header_string));
}
// Otherwise we use the slower PHP parsing
$headers = array();
// Match all HTTP headers
if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header_string, $matches))
{
// Parse each matched header
foreach ($matches[0] as $key => $value)
{
// If the header has not already been set
if ( ! isset($headers[$matches[1][$key]]))
{
// Apply the header directly
$headers[$matches[1][$key]] = $matches[2][$key];
}
// Otherwise there is an existing entry
else
{
// If the entry is an array
if (is_array($headers[$matches[1][$key]]))
{
// Apply the new entry to the array
$headers[$matches[1][$key]][] = $matches[2][$key];
}
// Otherwise create a new array with the entries
else
{
$headers[$matches[1][$key]] = array(
$headers[$matches[1][$key]],
$matches[2][$key],
);
}
}
}
}
// Return the headers
return new HTTP_Header($headers);
}
/**
* Parses the the HTTP request headers and returns an array containing
* key value pairs. This method is slow, but provides an accurate
* representation of the HTTP request.
*
* // Get http headers into the request
* $request->headers = HTTP::request_headers();
*
* @return HTTP_Header
*/
public static function request_headers()
{
// If running on apache server
if (function_exists('apache_request_headers'))
{
// Return the much faster method
return new HTTP_Header(apache_request_headers());
}
// If the PECL HTTP tools are installed
elseif (extension_loaded('http'))
{
// Return the much faster method
return new HTTP_Header(http_get_request_headers());
}
// Setup the output
$headers = array();
// Parse the content type
if ( ! empty($_SERVER['CONTENT_TYPE']))
{
$headers['content-type'] = $_SERVER['CONTENT_TYPE'];
}
// Parse the content length
if ( ! empty($_SERVER['CONTENT_LENGTH']))
{
$headers['content-length'] = $_SERVER['CONTENT_LENGTH'];
}
foreach ($_SERVER as $key => $value)
{
// If there is no HTTP header here, skip
if (strpos($key, 'HTTP_') !== 0)
{
continue;
}
// This is a dirty hack to ensure HTTP_X_FOO_BAR becomes x-foo-bar
$headers[str_replace(array('HTTP_', '_'), array('', '-'), $key)] = $value;
}
return new HTTP_Header($headers);
}
/**
* Processes an array of key value pairs and encodes
* the values to meet RFC 3986
*
* @param array $params Params
* @return string
*/
public static function www_form_urlencode(array $params = array())
{
if ( ! $params)
return;
$encoded = array();
foreach ($params as $key => $value)
{
$encoded[] = $key.'='.rawurlencode($value);
}
return implode('&', $encoded);
}
} // End Kohana_HTTP | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http.php | PHP | mit | 3,966 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Wrapper for configuration arrays. Multiple configuration readers can be
* attached to allow loading configuration from files, database, etc.
*
* @package Kohana
* @category Configuration
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Config {
/**
* @var Kohana_Config Singleton static instance
*/
protected static $_instance;
/**
* Get the singleton instance of Config.
*
* $config = Config::instance();
*
* @return Config
*/
public static function instance()
{
if (Config::$_instance === NULL)
{
// Create a new instance
Config::$_instance = new Config;
}
return Config::$_instance;
}
/**
* @var array Configuration readers
*/
protected $_readers = array();
/**
* Attach a configuration reader. By default, the reader will be added as
* the first used reader. However, if the reader should be used only when
* all other readers fail, use `FALSE` for the second parameter.
*
* $config->attach($reader); // Try first
* $config->attach($reader, FALSE); // Try last
*
* @param object Config_Reader instance
* @param boolean add the reader as the first used object
* @return $this
*/
public function attach(Config_Reader $reader, $first = TRUE)
{
if ($first === TRUE)
{
// Place the log reader at the top of the stack
array_unshift($this->_readers, $reader);
}
else
{
// Place the reader at the bottom of the stack
$this->_readers[] = $reader;
}
return $this;
}
/**
* Detach a configuration reader.
*
* $config->detach($reader);
*
* @param object Config_Reader instance
* @return $this
*/
public function detach(Config_Reader $reader)
{
if (($key = array_search($reader, $this->_readers)) !== FALSE)
{
// Remove the writer
unset($this->_readers[$key]);
}
return $this;
}
/**
* Load a configuration group. Searches the readers in order until the
* group is found. If the group does not exist, an empty configuration
* array will be loaded using the first reader.
*
* $array = $config->load($name);
*
* @param string configuration group name
* @return Config_Reader
* @throws Kohana_Exception
*/
public function load($group)
{
foreach ($this->_readers as $reader)
{
if ($config = $reader->load($group))
{
// Found a reader for this configuration group
return $config;
}
}
// Reset the iterator
reset($this->_readers);
if ( ! is_object($config = current($this->_readers)))
{
throw new Kohana_Exception('No configuration readers attached');
}
// Load the reader as an empty array
return $config->load($group, array());
}
/**
* Copy one configuration group to all of the other readers.
*
* $config->copy($name);
*
* @param string configuration group name
* @return $this
*/
public function copy($group)
{
// Load the configuration group
$config = $this->load($group);
foreach ($this->_readers as $reader)
{
if ($config instanceof $reader)
{
// Do not copy the config to the same group
continue;
}
// Load the configuration object
$object = $reader->load($group, array());
foreach ($config as $key => $value)
{
// Copy each value in the config
$object->offsetSet($key, $value);
}
}
return $this;
}
} // End Kohana_Config
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/config.php | PHP | mit | 3,513 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* File helper class.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_File {
/**
* Attempt to get the mime type from a file. This method is horribly
* unreliable, due to PHP being horribly unreliable when it comes to
* determining the mime type of a file.
*
* $mime = File::mime($file);
*
* @param string file name or path
* @return string mime type on success
* @return FALSE on failure
*/
public static function mime($filename)
{
// Get the complete path to the file
$filename = realpath($filename);
// Get the extension from the filename
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension))
{
// Use getimagesize() to find the mime type on images
$file = getimagesize($filename);
if (isset($file['mime']))
return $file['mime'];
}
if (class_exists('finfo', FALSE))
{
if ($info = new finfo(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME))
{
return $info->file($filename);
}
}
if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type'))
{
// The mime_content_type function is only useful with a magic file
return mime_content_type($filename);
}
if ( ! empty($extension))
{
return File::mime_by_ext($extension);
}
// Unable to find the mime-type
return FALSE;
}
/**
* Return the mime type of an extension.
*
* $mime = File::mime_by_ext('png'); // "image/png"
*
* @param string extension: php, pdf, txt, etc
* @return string mime type on success
* @return FALSE on failure
*/
public static function mime_by_ext($extension)
{
// Load all of the mime types
$mimes = Kohana::config('mimes');
return isset($mimes[$extension]) ? $mimes[$extension][0] : FALSE;
}
/**
* Lookup MIME types for a file
*
* @see Kohana_File::mime_by_ext()
* @param string $extension Extension to lookup
* @return array Array of MIMEs associated with the specified extension
*/
public static function mimes_by_ext($extension)
{
// Load all of the mime types
$mimes = Kohana::config('mimes');
return isset($mimes[$extension]) ? ( (array) $mimes[$extension]) : array();
}
/**
* Lookup file extensions by MIME type
*
* @param string $type File MIME type
* @return array File extensions matching MIME type
*/
public static function exts_by_mime($type)
{
static $types = array();
// Fill the static array
if (empty($types))
{
foreach (Kohana::config('mimes') as $ext => $mimes)
{
foreach ($mimes as $mime)
{
if ($mime == 'application/octet-stream')
{
// octet-stream is a generic binary
continue;
}
if ( ! isset($types[$mime]))
{
$types[$mime] = array( (string) $ext);
}
elseif ( ! in_array($ext, $types[$mime]))
{
$types[$mime][] = (string) $ext;
}
}
}
}
return isset($types[$type]) ? $types[$type] : FALSE;
}
/**
* Lookup a single file extension by MIME type.
*
* @param string $type MIME type to lookup
* @return mixed First file extension matching or false
*/
public static function ext_by_mime($type)
{
return current(File::exts_by_mime($type));
}
/**
* Split a file into pieces matching a specific size. Used when you need to
* split large files into smaller pieces for easy transmission.
*
* $count = File::split($file);
*
* @param string file to be split
* @param string directory to output to, defaults to the same directory as the file
* @param integer size, in MB, for each piece to be
* @return integer The number of pieces that were created
*/
public static function split($filename, $piece_size = 10)
{
// Open the input file
$file = fopen($filename, 'rb');
// Change the piece size to bytes
$piece_size = floor($piece_size * 1024 * 1024);
// Write files in 8k blocks
$block_size = 1024 * 8;
// Total number of peices
$peices = 0;
while ( ! feof($file))
{
// Create another piece
$peices += 1;
// Create a new file piece
$piece = str_pad($peices, 3, '0', STR_PAD_LEFT);
$piece = fopen($filename.'.'.$piece, 'wb+');
// Number of bytes read
$read = 0;
do
{
// Transfer the data in blocks
fwrite($piece, fread($file, $block_size));
// Another block has been read
$read += $block_size;
}
while ($read < $piece_size);
// Close the piece
fclose($piece);
}
// Close the file
fclose($file);
return $peices;
}
/**
* Join a split file into a whole file. Does the reverse of [File::split].
*
* $count = File::join($file);
*
* @param string split filename, without .000 extension
* @param string output filename, if different then an the filename
* @return integer The number of pieces that were joined.
*/
public static function join($filename)
{
// Open the file
$file = fopen($filename, 'wb+');
// Read files in 8k blocks
$block_size = 1024 * 8;
// Total number of peices
$pieces = 0;
while (is_file($piece = $filename.'.'.str_pad($pieces + 1, 3, '0', STR_PAD_LEFT)))
{
// Read another piece
$pieces += 1;
// Open the piece for reading
$piece = fopen($piece, 'rb');
while ( ! feof($piece))
{
// Transfer the data in blocks
fwrite($file, fread($piece, $block_size));
}
// Close the peice
fclose($piece);
}
return $pieces;
}
} // End file
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/file.php | PHP | mit | 5,691 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Array and variable validation.
*
* @package Kohana
* @category Security
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Validation extends ArrayObject {
/**
* Creates a new Validation instance.
*
* @param array array to use for validation
* @return Validation
*/
public static function factory(array $array)
{
return new Validation($array);
}
// Bound values
protected $_bound = array();
// Field rules
protected $_rules = array();
// Field labels
protected $_labels = array();
// Rules that are executed even when the value is empty
protected $_empty_rules = array('not_empty', 'matches');
// Error list, field => rule
protected $_errors = array();
/**
* Sets the unique "any field" key and creates an ArrayObject from the
* passed array.
*
* @param array array to validate
* @return void
*/
public function __construct(array $array)
{
parent::__construct($array, ArrayObject::STD_PROP_LIST);
}
/**
* Copies the current rule to a new array.
*
* $copy = $array->copy($new_data);
*
* @param array new data set
* @return Validation
* @since 3.0.5
*/
public function copy(array $array)
{
// Create a copy of the current validation set
$copy = clone $this;
// Replace the data set
$copy->exchangeArray($array);
return $copy;
}
/**
* Returns the array representation of the current object.
*
* @return array
*/
public function as_array()
{
return $this->getArrayCopy();
}
/**
* Sets or overwrites the label name for a field.
*
* @param string field name
* @param string label
* @return $this
*/
public function label($field, $label)
{
// Set the label for this field
$this->_labels[$field] = $label;
return $this;
}
/**
* Sets labels using an array.
*
* @param array list of field => label names
* @return $this
*/
public function labels(array $labels)
{
$this->_labels = $labels + $this->_labels;
return $this;
}
/**
* Overwrites or appends rules to a field. Each rule will be executed once.
* All rules must be string names of functions method names. Parameters must
* match the parameters of the callback function exactly
*
* Aliases you can use in callback parameters:
* - :validation - the validation object
* - :field - the field name
* - :value - the value of the field
*
* // The "username" must not be empty and have a minimum length of 4
* $validation->rule('username', 'not_empty')
* ->rule('username', 'min_length', array('username', 4));
*
* // The "password" field must match the "password_repeat" field
* $validation->rule('password', 'matches', array(':validation', 'password', 'password_repeat'));
*
* @param string field name
* @param callback valid PHP callback
* @param array extra parameters for the rule
* @return $this
*/
public function rule($field, $rule, array $params = NULL)
{
if ($params === NULL)
{
// Default to array(':value')
$params = array(':value');
}
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
}
// Store the rule and params for this rule
$this->_rules[$field][] = array($rule, $params);
return $this;
}
/**
* Add rules using an array.
*
* @param string field name
* @param array list of callbacks
* @return $this
*/
public function rules($field, array $rules)
{
foreach ($rules as $rule)
{
$this->rule($field, $rule[0], Arr::get($rule, 1));
}
return $this;
}
/**
* Bind a value to a parameter definition.
*
* // This allows you to use :model in the parameter definition of rules
* $validation->bind(':model', $model)
* ->rule('status', 'valid_status', array(':model'));
*
* @param string variable name or an array of variables
* @param mixed value
* @return $this
*/
public function bind($key, $value = NULL)
{
if (is_array($key))
{
foreach ($key as $name => $value)
{
$this->_bound[$name] = $value;
}
}
else
{
$this->_bound[$key] = $value;
}
return $this;
}
/**
* Executes all validation rules. This should
* typically be called within an if/else block.
*
* if ($validation->check())
* {
* // The data is valid, do something here
* }
*
* @param boolean allow empty array?
* @return boolean
*/
public function check()
{
if (Kohana::$profiling === TRUE)
{
// Start a new benchmark
$benchmark = Profiler::start('Validation', __FUNCTION__);
}
// New data set
$data = $this->_errors = array();
// Store the original data because this class should not modify it post-validation
$original = $this->getArrayCopy();
// Get a list of the expected fields
$expected = Arr::merge(array_keys($original), array_keys($this->_labels));
// Import the rules locally
$rules = $this->_rules;
foreach ($expected as $field)
{
// Use the submitted value or NULL if no data exists
$data[$field] = Arr::get($this, $field);
if (isset($rules[TRUE]))
{
if ( ! isset($rules[$field]))
{
// Initialize the rules for this field
$rules[$field] = array();
}
// Append the rules
$rules[$field] = array_merge($rules[$field], $rules[TRUE]);
}
}
// Overload the current array with the new one
$this->exchangeArray($data);
// Remove the rules that apply to every field
unset($rules[TRUE]);
// Bind the validation object to :validation
$this->bind(':validation', $this);
// Execute the rules
foreach ($rules as $field => $set)
{
// Get the field value
$value = $this[$field];
// Bind the field name and value to :field and :value respectively
$this->bind(array
(
':field' => $field,
':value' => $value,
));
foreach ($set as $array)
{
// Rules are defined as array($rule, $params)
list($rule, $params) = $array;
foreach ($params as $key => $param)
{
if (is_string($param) AND array_key_exists($param, $this->_bound))
{
// Replace with bound value
$params[$key] = $this->_bound[$param];
}
}
// Default the error name to be the rule (except array and lambda rules)
$error_name = $rule;
if (is_array($rule))
{
// This is an array callback, the method name is the error name
$error_name = $rule[1];
$passed = call_user_func_array($rule, $params);
}
elseif ( ! is_string($rule))
{
// This is a lambda function, there is no error name (errors must be added manually)
$error_name = FALSE;
$passed = call_user_func_array($rule, $params);
}
elseif (method_exists('Valid', $rule))
{
// Use a method in this object
$method = new ReflectionMethod('Valid', $rule);
// Call static::$rule($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
elseif (strpos($rule, '::') === FALSE)
{
// Use a function call
$function = new ReflectionFunction($rule);
// Call $function($this[$field], $param, ...) with Reflection
$passed = $function->invokeArgs($params);
}
else
{
// Split the class and method of the rule
list($class, $method) = explode('::', $rule, 2);
// Use a static method call
$method = new ReflectionMethod($class, $method);
// Call $Class::$method($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
// Ignore return values from rules when the field is empty
if ( ! in_array($rule, $this->_empty_rules) AND ! Valid::not_empty($value))
continue;
if ($passed === FALSE AND $error_name !== FALSE)
{
// Add the rule to the errors
$this->error($field, $error_name, $params);
// This field has an error, stop executing rules
break;
}
}
}
// Restore the data to its original form
$this->exchangeArray($original);
if (isset($benchmark))
{
// Stop benchmarking
Profiler::stop($benchmark);
}
return empty($this->_errors);
}
/**
* Add an error to a field.
*
* @param string field name
* @param string error message
* @return $this
*/
public function error($field, $error, array $params = NULL)
{
$this->_errors[$field] = array($error, $params);
return $this;
}
/**
* Returns the error messages. If no file is specified, the error message
* will be the name of the rule that failed. When a file is specified, the
* message will be loaded from "field/rule", or if no rule-specific message
* exists, "field/default" will be used. If neither is set, the returned
* message will be "file/field/rule".
*
* By default all messages are translated using the default language.
* A string can be used as the second parameter to specified the language
* that the message was written in.
*
* // Get errors from messages/forms/login.php
* $errors = $Validation->errors('forms/login');
*
* @uses Kohana::message
* @param string file to load error messages from
* @param mixed translate the message
* @return array
*/
public function errors($file = NULL, $translate = TRUE)
{
if ($file === NULL)
{
// Return the error list
return $this->_errors;
}
// Create a new message list
$messages = array();
foreach ($this->_errors as $field => $set)
{
list($error, $params) = $set;
// Get the label for this field
$label = $this->_labels[$field];
if ($translate)
{
if (is_string($translate))
{
// Translate the label using the specified language
$label = __($label, NULL, $translate);
}
else
{
// Translate the label
$label = __($label);
}
}
// Start the translation values list
$values = array(
':field' => $label,
':value' => Arr::get($this, $field),
);
if (is_array($values[':value']))
{
// All values must be strings
$values[':value'] = implode(', ', Arr::flatten($values[':value']));
}
if ($params)
{
foreach ($params as $key => $value)
{
if (is_array($value))
{
// All values must be strings
$value = implode(', ', Arr::flatten($value));
}
elseif (is_object($value))
{
// Objects cannot be used in message files
continue;
}
// Check if a label for this parameter exists
if (isset($this->_labels[$value]))
{
// Use the label as the value, eg: related field name for "matches"
$value = $this->_labels[$value];
if ($translate)
{
if (is_string($translate))
{
// Translate the value using the specified language
$value = __($value, NULL, $translate);
}
else
{
// Translate the value
$value = __($value);
}
}
}
// Add each parameter as a numbered value, starting from 1
$values[':param'.($key + 1)] = $value;
}
}
if ($message = Kohana::message($file, "{$field}.{$error}"))
{
// Found a message for this field and error
}
elseif ($message = Kohana::message($file, "{$field}.default"))
{
// Found a default message for this field
}
elseif ($message = Kohana::message($file, $error))
{
// Found a default message for this error
}
elseif ($message = Kohana::message('validation', $error))
{
// Found a default message for this error
}
else
{
// No message exists, display the path expected
$message = "{$file}.{$field}.{$error}";
}
if ($translate)
{
if (is_string($translate))
{
// Translate the message using specified language
$message = __($message, $values, $translate);
}
else
{
// Translate the message using the default language
$message = __($message, $values);
}
}
else
{
// Do not translate, just replace the values
$message = strtr($message, $values);
}
// Set the message for this field
$messages[$field] = $message;
}
return $messages;
}
} // End Validation
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/validation.php | PHP | mit | 12,371 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Routes are used to determine the controller and action for a requested URI.
* Every route generates a regular expression which is used to match a URI
* and a route. Routes may also contain keys which can be used to set the
* controller, action, and parameters.
*
* Each <key> will be translated to a regular expression using a default
* regular expression pattern. You can override the default pattern by providing
* a pattern for the key:
*
* // This route will only match when <id> is a digit
* Route::set('user', 'user/<action>/<id>', array('id' => '\d+'));
*
* // This route will match when <path> is anything
* Route::set('file', '<path>', array('path' => '.*'));
*
* It is also possible to create optional segments by using parentheses in
* the URI definition:
*
* // This is the standard default route, and no keys are required
* Route::set('default', '(<controller>(/<action>(/<id>)))');
*
* // This route only requires the <file> key
* Route::set('file', '(<path>/)<file>(.<format>)', array('path' => '.*', 'format' => '\w+'));
*
* Routes also provide a way to generate URIs (called "reverse routing"), which
* makes them an extremely powerful and flexible way to generate internal links.
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Route {
// Defines the pattern of a <segment>
const REGEX_KEY = '<([a-zA-Z0-9_]++)>';
// What can be part of a <segment> value
const REGEX_SEGMENT = '[^/.,;?\n]++';
// What must be escaped in the route regex
const REGEX_ESCAPE = '[.\\+*?[^\\]${}=!|]';
/**
* @var string default protocol for all routes
*
* @example 'http://'
*/
public static $default_protocol = 'http://';
/**
* @var array list of valid localhost entries
*/
public static $localhosts = array(FALSE, '', 'local', 'localhost');
/**
* @var string default action for all routes
*/
public static $default_action = 'index';
/**
* @var bool Indicates whether routes are cached
*/
public static $cache = FALSE;
/**
* @var array
*/
protected static $_routes = array();
/**
* Stores a named route and returns it. The "action" will always be set to
* "index" if it is not defined.
*
* Route::set('default', '(<controller>(/<action>(/<id>)))')
* ->defaults(array(
* 'controller' => 'welcome',
* ));
*
* @param string route name
* @param string URI pattern
* @param array regex patterns for route keys
* @return Route
*/
public static function set($name, $uri_callback = NULL, $regex = NULL)
{
return Route::$_routes[$name] = new Route($uri_callback, $regex);
}
/**
* Retrieves a named route.
*
* $route = Route::get('default');
*
* @param string route name
* @return Route
* @throws Kohana_Exception
*/
public static function get($name)
{
if ( ! isset(Route::$_routes[$name]))
{
throw new Kohana_Exception('The requested route does not exist: :route',
array(':route' => $name));
}
return Route::$_routes[$name];
}
/**
* Retrieves all named routes.
*
* $routes = Route::all();
*
* @return array routes by name
*/
public static function all()
{
return Route::$_routes;
}
/**
* Get the name of a route.
*
* $name = Route::name($route)
*
* @param object Route instance
* @return string
*/
public static function name(Route $route)
{
return array_search($route, Route::$_routes);
}
/**
* Saves or loads the route cache. If your routes will remain the same for
* a long period of time, use this to reload the routes from the cache
* rather than redefining them on every page load.
*
* if ( ! Route::cache())
* {
* // Set routes here
* Route::cache(TRUE);
* }
*
* @param boolean cache the current routes
* @return void when saving routes
* @return boolean when loading routes
* @uses Kohana::cache
*/
public static function cache($save = FALSE)
{
if ($save === TRUE)
{
// Cache all defined routes
Kohana::cache('Route::cache()', Route::$_routes);
}
else
{
if ($routes = Kohana::cache('Route::cache()'))
{
Route::$_routes = $routes;
// Routes were cached
return Route::$cache = TRUE;
}
else
{
// Routes were not cached
return Route::$cache = FALSE;
}
}
}
/**
* Create a URL from a route name. This is a shortcut for:
*
* echo URL::site(Route::get($name)->uri($params), $protocol);
*
* @param string route name
* @param array URI parameters
* @param mixed protocol string or boolean, adds protocol and domain
* @return string
* @since 3.0.7
* @uses URL::site
*/
public static function url($name, array $params = NULL, $protocol = NULL)
{
$route = Route::get($name);
// Create a URI with the route and convert it to a URL
if ($route->is_external())
return Route::get($name)->uri($params);
else
return URL::site(Route::get($name)->uri($params), $protocol);
}
/**
* Returns the compiled regular expression for the route. This translates
* keys and optional groups to a proper PCRE regular expression.
*
* $compiled = Route::compile(
* '<controller>(/<action>(/<id>))',
* array(
* 'controller' => '[a-z]+',
* 'id' => '\d+',
* )
* );
*
* @return string
* @uses Route::REGEX_ESCAPE
* @uses Route::REGEX_SEGMENT
*/
public static function compile($uri, array $regex = NULL)
{
if ( ! is_string($uri))
return;
// The URI should be considered literal except for keys and optional parts
// Escape everything preg_quote would escape except for : ( ) < >
$expression = preg_replace('#'.Route::REGEX_ESCAPE.'#', '\\\\$0', $uri);
if (strpos($expression, '(') !== FALSE)
{
// Make optional parts of the URI non-capturing and optional
$expression = str_replace(array('(', ')'), array('(?:', ')?'), $expression);
}
// Insert default regex for keys
$expression = str_replace(array('<', '>'), array('(?P<', '>'.Route::REGEX_SEGMENT.')'), $expression);
if ($regex)
{
$search = $replace = array();
foreach ($regex as $key => $value)
{
$search[] = "<$key>".Route::REGEX_SEGMENT;
$replace[] = "<$key>$value";
}
// Replace the default regex with the user-specified regex
$expression = str_replace($search, $replace, $expression);
}
return '#^'.$expression.'$#uD';
}
/**
* @var callback The callback method for routes
*/
protected $_callback;
/**
* @var string route URI
*/
protected $_uri = '';
/**
* @var array
*/
protected $_regex = array();
/**
* @var array
*/
protected $_defaults = array('action' => 'index', 'host' => FALSE);
/**
* @var string
*/
protected $_route_regex;
/**
* Creates a new route. Sets the URI and regular expressions for keys.
* Routes should always be created with [Route::set] or they will not
* be properly stored.
*
* $route = new Route($uri, $regex);
*
* The $uri parameter can either be a string for basic regex matching or it
* can be a valid callback or anonymous function (php 5.3+). If you use a
* callback or anonymous function, your method should return an array
* containing the proper keys for the route. If you want the route to be
* "reversable", you need pass the route string as the third parameter.
*
* $route = new Route(function($uri)
* {
* if (list($controller, $action, $param) = explode('/', $uri) AND $controller == 'foo' AND $action == 'bar')
* {
* return array(
* 'controller' => 'foobar',
* 'action' => $action,
* 'id' => $param,
* );
* },
* 'foo/bar/<id>'
* });
*
* @param mixed route URI pattern or lambda/callback function
* @param array key patterns
* @return void
* @uses Route::_compile
*/
public function __construct($uri = NULL, $regex = NULL)
{
if ($uri === NULL)
{
// Assume the route is from cache
return;
}
if ( ! is_string($uri) AND is_callable($uri))
{
$this->_callback = $uri;
$this->_uri = $regex;
$regex = NULL;
}
elseif ( ! empty($uri))
{
$this->_uri = $uri;
}
if ( ! empty($regex))
{
$this->_regex = $regex;
}
// Store the compiled regex locally
$this->_route_regex = Route::compile($uri, $regex);
}
/**
* Provides default values for keys when they are not present. The default
* action will always be "index" unless it is overloaded here.
*
* $route->defaults(array(
* 'controller' => 'welcome',
* 'action' => 'index'
* ));
*
* @param array key values
* @return $this
*/
public function defaults(array $defaults = NULL)
{
$this->_defaults = $defaults;
return $this;
}
/**
* Tests if the route matches a given URI. A successful match will return
* all of the routed parameters as an array. A failed match will return
* boolean FALSE.
*
* // Params: controller = users, action = edit, id = 10
* $params = $route->matches('users/edit/10');
*
* This method should almost always be used within an if/else block:
*
* if ($params = $route->matches($uri))
* {
* // Parse the parameters
* }
*
* @param string URI to match
* @return array on success
* @return FALSE on failure
*/
public function matches($uri)
{
if ($this->_callback)
{
$closure = $this->_callback;
$params = call_user_func($closure, $uri);
if ( ! is_array($params))
return FALSE;
}
else
{
if ( ! preg_match($this->_route_regex, $uri, $matches))
return FALSE;
$params = array();
foreach ($matches as $key => $value)
{
if (is_int($key))
{
// Skip all unnamed keys
continue;
}
// Set the value for all matched keys
$params[$key] = $value;
}
}
foreach ($this->_defaults as $key => $value)
{
if ( ! isset($params[$key]) OR $params[$key] === '')
{
// Set default values for any key that was not matched
$params[$key] = $value;
}
}
return $params;
}
/**
* Returns whether this route is an external route
* to a remote controller.
*
* @return boolean
*/
public function is_external()
{
return ! in_array(Arr::get($this->_defaults, 'host', FALSE), Route::$localhosts);
}
/**
* Generates a URI for the current route based on the parameters given.
*
* // Using the "default" route: "users/profile/10"
* $route->uri(array(
* 'controller' => 'users',
* 'action' => 'profile',
* 'id' => '10'
* ));
*
* @param array URI parameters
* @return string
* @throws Kohana_Exception
* @uses Route::REGEX_Key
*/
public function uri(array $params = NULL)
{
// Start with the routed URI
$uri = $this->_uri;
if (strpos($uri, '<') === FALSE AND strpos($uri, '(') === FALSE)
{
// This is a static route, no need to replace anything
if ( ! $this->is_external())
return $uri;
// If the localhost setting does not have a protocol
if (strpos($this->_defaults['host'], '://') === FALSE)
{
// Use the default defined protocol
$params['host'] = Route::$default_protocol.$this->_defaults['host'];
}
else
{
// Use the supplied host with protocol
$params['host'] = $this->_defaults['host'];
}
// Compile the final uri and return it
return rtrim($params['host'], '/').'/'.$uri;
}
while (preg_match('#\([^()]++\)#', $uri, $match))
{
// Search for the matched value
$search = $match[0];
// Remove the parenthesis from the match as the replace
$replace = substr($match[0], 1, -1);
while (preg_match('#'.Route::REGEX_KEY.'#', $replace, $match))
{
list($key, $param) = $match;
if (isset($params[$param]))
{
// Replace the key with the parameter value
$replace = str_replace($key, $params[$param], $replace);
}
else
{
// This group has missing parameters
$replace = '';
break;
}
}
// Replace the group in the URI
$uri = str_replace($search, $replace, $uri);
}
while (preg_match('#'.Route::REGEX_KEY.'#', $uri, $match))
{
list($key, $param) = $match;
if ( ! isset($params[$param]))
{
// Look for a default
if (isset($this->_defaults[$param]))
{
$params[$param] = $this->_defaults[$param];
}
else
{
// Ungrouped parameters are required
throw new Kohana_Exception('Required route parameter not passed: :param', array(
':param' => $param,
));
}
}
$uri = str_replace($key, $params[$param], $uri);
}
// Trim all extra slashes from the URI
$uri = preg_replace('#//+#', '/', rtrim($uri, '/'));
if ($this->is_external())
{
// Need to add the host to the URI
$host = $this->_defaults['host'];
if (strpos($host, '://') === FALSE)
{
// Use the default defined protocol
$host = Route::$default_protocol.$host;
}
// Clean up the host and prepend it to the URI
$uri = rtrim($host, '/').'/'.$uri;
}
return $uri;
}
} // End Route
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/route.php | PHP | mit | 13,406 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* @package Kohana
* @category Exceptions
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_View_Exception extends Kohana_Exception { }
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/view/exception.php | PHP | mit | 293 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Contains debugging and dumping tools.
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Debug {
/**
* Returns an HTML string of debugging information about any number of
* variables, each wrapped in a "pre" tag:
*
* // Displays the type and value of each variable
* echo Debug::vars($foo, $bar, $baz);
*
* @param mixed variable to debug
* @param ...
* @return string
*/
public static function vars()
{
if (func_num_args() === 0)
return;
// Get all passed variables
$variables = func_get_args();
$output = array();
foreach ($variables as $var)
{
$output[] = Debug::_dump($var, 1024);
}
return '<pre class="debug">'.implode("\n", $output).'</pre>';
}
/**
* Returns an HTML string of information about a single variable.
*
* Borrows heavily on concepts from the Debug class of [Nette](http://nettephp.com/).
*
* @param mixed variable to dump
* @param integer maximum length of strings
* @return string
*/
public static function dump($value, $length = 128)
{
return Debug::_dump($value, $length);
}
/**
* Helper for Debug::dump(), handles recursion in arrays and objects.
*
* @param mixed variable to dump
* @param integer maximum length of strings
* @param integer recursion level (internal)
* @return string
*/
protected static function _dump( & $var, $length = 128, $level = 0)
{
if ($var === NULL)
{
return '<small>NULL</small>';
}
elseif (is_bool($var))
{
return '<small>bool</small> '.($var ? 'TRUE' : 'FALSE');
}
elseif (is_float($var))
{
return '<small>float</small> '.$var;
}
elseif (is_resource($var))
{
if (($type = get_resource_type($var)) === 'stream' AND $meta = stream_get_meta_data($var))
{
$meta = stream_get_meta_data($var);
if (isset($meta['uri']))
{
$file = $meta['uri'];
if (function_exists('stream_is_local'))
{
// Only exists on PHP >= 5.2.4
if (stream_is_local($file))
{
$file = Debug::path($file);
}
}
return '<small>resource</small><span>('.$type.')</span> '.htmlspecialchars($file, ENT_NOQUOTES, Kohana::$charset);
}
}
else
{
return '<small>resource</small><span>('.$type.')</span>';
}
}
elseif (is_string($var))
{
// Clean invalid multibyte characters. iconv is only invoked
// if there are non ASCII characters in the string, so this
// isn't too much of a hit.
$var = UTF8::clean($var, Kohana::$charset);
if (UTF8::strlen($var) > $length)
{
// Encode the truncated string
$str = htmlspecialchars(UTF8::substr($var, 0, $length), ENT_NOQUOTES, Kohana::$charset).' …';
}
else
{
// Encode the string
$str = htmlspecialchars($var, ENT_NOQUOTES, Kohana::$charset);
}
return '<small>string</small><span>('.strlen($var).')</span> "'.$str.'"';
}
elseif (is_array($var))
{
$output = array();
// Indentation for this variable
$space = str_repeat($s = ' ', $level);
static $marker;
if ($marker === NULL)
{
// Make a unique marker
$marker = uniqid("\x00");
}
if (empty($var))
{
// Do nothing
}
elseif (isset($var[$marker]))
{
$output[] = "(\n$space$s*RECURSION*\n$space)";
}
elseif ($level < 5)
{
$output[] = "<span>(";
$var[$marker] = TRUE;
foreach ($var as $key => & $val)
{
if ($key === $marker) continue;
if ( ! is_int($key))
{
$key = '"'.htmlspecialchars($key, ENT_NOQUOTES, Kohana::$charset).'"';
}
$output[] = "$space$s$key => ".Debug::_dump($val, $length, $level + 1);
}
unset($var[$marker]);
$output[] = "$space)</span>";
}
else
{
// Depth too great
$output[] = "(\n$space$s...\n$space)";
}
return '<small>array</small><span>('.count($var).')</span> '.implode("\n", $output);
}
elseif (is_object($var))
{
// Copy the object as an array
$array = (array) $var;
$output = array();
// Indentation for this variable
$space = str_repeat($s = ' ', $level);
$hash = spl_object_hash($var);
// Objects that are being dumped
static $objects = array();
if (empty($var))
{
// Do nothing
}
elseif (isset($objects[$hash]))
{
$output[] = "{\n$space$s*RECURSION*\n$space}";
}
elseif ($level < 10)
{
$output[] = "<code>{";
$objects[$hash] = TRUE;
foreach ($array as $key => & $val)
{
if ($key[0] === "\x00")
{
// Determine if the access is protected or protected
$access = '<small>'.(($key[1] === '*') ? 'protected' : 'private').'</small>';
// Remove the access level from the variable name
$key = substr($key, strrpos($key, "\x00") + 1);
}
else
{
$access = '<small>public</small>';
}
$output[] = "$space$s$access $key => ".Debug::_dump($val, $length, $level + 1);
}
unset($objects[$hash]);
$output[] = "$space}</code>";
}
else
{
// Depth too great
$output[] = "{\n$space$s...\n$space}";
}
return '<small>object</small> <span>'.get_class($var).'('.count($array).')</span> '.implode("\n", $output);
}
else
{
return '<small>'.gettype($var).'</small> '.htmlspecialchars(print_r($var, TRUE), ENT_NOQUOTES, Kohana::$charset);
}
}
/**
* Removes application, system, modpath, or docroot from a filename,
* replacing them with the plain text equivalents. Useful for debugging
* when you want to display a shorter path.
*
* // Displays SYSPATH/classes/kohana.php
* echo Debug::path(Kohana::find_file('classes', 'kohana'));
*
* @param string path to debug
* @return string
*/
public static function path($file)
{
if (strpos($file, APPPATH) === 0)
{
$file = 'APPPATH'.DIRECTORY_SEPARATOR.substr($file, strlen(APPPATH));
}
elseif (strpos($file, SYSPATH) === 0)
{
$file = 'SYSPATH'.DIRECTORY_SEPARATOR.substr($file, strlen(SYSPATH));
}
elseif (strpos($file, MODPATH) === 0)
{
$file = 'MODPATH'.DIRECTORY_SEPARATOR.substr($file, strlen(MODPATH));
}
elseif (strpos($file, DOCROOT) === 0)
{
$file = 'DOCROOT'.DIRECTORY_SEPARATOR.substr($file, strlen(DOCROOT));
}
return $file;
}
/**
* Returns an HTML string, highlighting a specific line of a file, with some
* number of lines padded above and below.
*
* // Highlights the current line of the current file
* echo Debug::source(__FILE__, __LINE__);
*
* @param string file to open
* @param integer line number to highlight
* @param integer number of padding lines
* @return string source of file
* @return FALSE file is unreadable
*/
public static function source($file, $line_number, $padding = 5)
{
if ( ! $file OR ! is_readable($file))
{
// Continuing will cause errors
return FALSE;
}
// Open the file and set the line position
$file = fopen($file, 'r');
$line = 0;
// Set the reading range
$range = array('start' => $line_number - $padding, 'end' => $line_number + $padding);
// Set the zero-padding amount for line numbers
$format = '% '.strlen($range['end']).'d';
$source = '';
while (($row = fgets($file)) !== FALSE)
{
// Increment the line number
if (++$line > $range['end'])
break;
if ($line >= $range['start'])
{
// Make the row safe for output
$row = htmlspecialchars($row, ENT_NOQUOTES, Kohana::$charset);
// Trim whitespace and sanitize the row
$row = '<span class="number">'.sprintf($format, $line).'</span> '.$row;
if ($line === $line_number)
{
// Apply highlighting to this row
$row = '<span class="line highlight">'.$row.'</span>';
}
else
{
$row = '<span class="line">'.$row.'</span>';
}
// Add to the captured source
$source .= $row;
}
}
// Close the file
fclose($file);
return '<pre class="source"><code>'.$source.'</code></pre>';
}
/**
* Returns an array of HTML strings that represent each step in the backtrace.
*
* // Displays the entire current backtrace
* echo implode('<br/>', Debug::trace());
*
* @param string path to debug
* @return string
*/
public static function trace(array $trace = NULL)
{
if ($trace === NULL)
{
// Start a new trace
$trace = debug_backtrace();
}
// Non-standard function calls
$statements = array('include', 'include_once', 'require', 'require_once');
$output = array();
foreach ($trace as $step)
{
if ( ! isset($step['function']))
{
// Invalid trace step
continue;
}
if (isset($step['file']) AND isset($step['line']))
{
// Include the source of this step
$source = Debug::source($step['file'], $step['line']);
}
if (isset($step['file']))
{
$file = $step['file'];
if (isset($step['line']))
{
$line = $step['line'];
}
}
// function()
$function = $step['function'];
if (in_array($step['function'], $statements))
{
if (empty($step['args']))
{
// No arguments
$args = array();
}
else
{
// Sanitize the file path
$args = array($step['args'][0]);
}
}
elseif (isset($step['args']))
{
if ( ! function_exists($step['function']) OR strpos($step['function'], '{closure}') !== FALSE)
{
// Introspection on closures or language constructs in a stack trace is impossible
$params = NULL;
}
else
{
if (isset($step['class']))
{
if (method_exists($step['class'], $step['function']))
{
$reflection = new ReflectionMethod($step['class'], $step['function']);
}
else
{
$reflection = new ReflectionMethod($step['class'], '__call');
}
}
else
{
$reflection = new ReflectionFunction($step['function']);
}
// Get the function parameters
$params = $reflection->getParameters();
}
$args = array();
foreach ($step['args'] as $i => $arg)
{
if (isset($params[$i]))
{
// Assign the argument by the parameter name
$args[$params[$i]->name] = $arg;
}
else
{
// Assign the argument by number
$args[$i] = $arg;
}
}
}
if (isset($step['class']))
{
// Class->method() or Class::method()
$function = $step['class'].$step['type'].$step['function'];
}
$output[] = array(
'function' => $function,
'args' => isset($args) ? $args : NULL,
'file' => isset($file) ? $file : NULL,
'line' => isset($line) ? $line : NULL,
'source' => isset($source) ? $source : NULL,
);
unset($function, $args, $file, $line, $source);
}
return $output;
}
}
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/debug.php | PHP | mit | 10,889 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Number helper class. Provides additional formatting methods that for working
* with numbers.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Num {
const ROUND_HALF_UP = 1;
const ROUND_HALF_DOWN = 2;
const ROUND_HALF_EVEN = 3;
const ROUND_HALF_ODD = 4;
/**
* @var array Valid byte units => power of 2 that defines the unit's size
*/
public static $byte_units = array
(
'B' => 0,
'K' => 10,
'Ki' => 10,
'KB' => 10,
'KiB' => 10,
'M' => 20,
'Mi' => 20,
'MB' => 20,
'MiB' => 20,
'G' => 30,
'Gi' => 30,
'GB' => 30,
'GiB' => 30,
'T' => 40,
'Ti' => 40,
'TB' => 40,
'TiB' => 40,
'P' => 50,
'Pi' => 50,
'PB' => 50,
'PiB' => 50,
'E' => 60,
'Ei' => 60,
'EB' => 60,
'EiB' => 60,
'Z' => 70,
'Zi' => 70,
'ZB' => 70,
'ZiB' => 70,
'Y' => 80,
'Yi' => 80,
'YB' => 80,
'YiB' => 80,
);
/**
* Returns the English ordinal suffix (th, st, nd, etc) of a number.
*
* echo 2, Num::ordinal(2); // "2nd"
* echo 10, Num::ordinal(10); // "10th"
* echo 33, Num::ordinal(33); // "33rd"
*
* @param integer number
* @return string
*/
public static function ordinal($number)
{
if ($number % 100 > 10 AND $number % 100 < 14)
{
return 'th';
}
switch ($number % 10)
{
case 1:
return 'st';
case 2:
return 'nd';
case 3:
return 'rd';
default:
return 'th';
}
}
/**
* Locale-aware number and monetary formatting.
*
* // In English, "1,200.05"
* // In Spanish, "1200,05"
* // In Portuguese, "1 200,05"
* echo Num::format(1200.05, 2);
*
* // In English, "1,200.05"
* // In Spanish, "1.200,05"
* // In Portuguese, "1.200.05"
* echo Num::format(1200.05, 2, TRUE);
*
* @param float number to format
* @param integer decimal places
* @param boolean monetary formatting?
* @return string
* @since 3.0.2
*/
public static function format($number, $places, $monetary = FALSE)
{
$info = localeconv();
if ($monetary)
{
$decimal = $info['mon_decimal_point'];
$thousands = $info['mon_thousands_sep'];
}
else
{
$decimal = $info['decimal_point'];
$thousands = $info['thousands_sep'];
}
return number_format($number, $places, $decimal, $thousands);
}
/**
* Round a number to a specified precision, using a specified tie breaking technique
*
* @param float $value Number to round
* @param integer $precision Desired precision
* @param integer $mode Tie breaking mode, accepts the PHP_ROUND_HALF_* constants
* @param boolean $native Set to false to force use of the userland implementation
* @return float Rounded number
*/
public static function round($value, $precision = 0, $mode = self::ROUND_HALF_UP, $native = true)
{
if (version_compare(PHP_VERSION, '5.3', '>=') AND $native)
{
return round($value, $precision, $mode);
}
if ($mode === self::ROUND_HALF_UP)
{
return round($value, $precision);
}
else
{
$factor = ($precision === 0) ? 1 : pow(10, $precision);
switch ($mode)
{
case self::ROUND_HALF_DOWN:
case self::ROUND_HALF_EVEN:
case self::ROUND_HALF_ODD:
// Check if we have a rounding tie, otherwise we can just call round()
if (($value * $factor) - floor($value * $factor) === 0.5)
{
if ($mode === self::ROUND_HALF_DOWN)
{
// Round down operation, so we round down unless the value
// is -ve because up is down and down is up down there. ;)
$up = ($value < 0);
}
else
{
// Round up if the integer is odd and the round mode is set to even
// or the integer is even and the round mode is set to odd.
// Any other instance round down.
$up = ( ! ( ! (floor($value * $factor) & 1)) === ($mode === self::ROUND_HALF_EVEN));
}
if ($up)
{
$value = ceil($value * $factor);
}
else
{
$value = floor($value * $factor);
}
return $value / $factor;
}
else
{
return round($value, $precision);
}
break;
}
}
}
/**
* Converts a file size number to a byte value. File sizes are defined in
* the format: SB, where S is the size (1, 8.5, 300, etc.) and B is the
* byte unit (K, MiB, GB, etc.). All valid byte units are defined in
* Num::$byte_units
*
* echo Num::bytes('200K'); // 204800
* echo Num::bytes('5MiB'); // 5242880
* echo Num::bytes('1000'); // 1000
* echo Num::bytes('2.5GB'); // 2684354560
*
* @param string file size in SB format
* @return float
*/
public static function bytes($size)
{
// Prepare the size
$size = trim( (string) $size);
// Construct an OR list of byte units for the regex
$accepted = implode('|', array_keys(Num::$byte_units));
// Construct the regex pattern for verifying the size format
$pattern = '/^([0-9]+(?:\.[0-9]+)?)('.$accepted.')?$/Di';
// Verify the size format and store the matching parts
if ( ! preg_match($pattern, $size, $matches))
throw new Kohana_Exception('The byte unit size, ":size", is improperly formatted.', array(
':size' => $size,
));
// Find the float value of the size
$size = (float) $matches[1];
// Find the actual unit, assume B if no unit specified
$unit = Arr::get($matches, 2, 'B');
// Convert the size into bytes
$bytes = $size * pow(2, Num::$byte_units[$unit]);
return $bytes;
}
} // End num
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/num.php | PHP | mit | 5,673 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Response wrapper. Created as the result of any [Request] execution
* or utility method (i.e. Redirect). Implements standard HTTP
* response format.
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
* @since 3.1.0
*/
class Kohana_Response implements HTTP_Response, Serializable {
/**
* Factory method to create a new [Response]. Pass properties
* in using an associative array.
*
* // Create a new response
* $response = Response::factory();
*
* // Create a new response with headers
* $response = Response::factory(array('status' => 200));
*
* @param array $config Setup the response object
* @return Response
*/
public static function factory(array $config = array())
{
return new Response($config);
}
/**
* Generates a [Cache-Control HTTP](http://en.wikipedia.org/wiki/List_of_HTTP_headers)
* header based on the supplied array.
*
* // Set the cache control headers you want to use
* $cache_control = array(
* 'max-age' => 3600,
* 'must-revalidate' => NULL,
* 'public' => NULL
* );
*
* // Create the cache control header, creates :
* // cache-control: max-age=3600, must-revalidate, public
* $response->header['cache-control'] = Response::create_cache_control($cache_control);
*
* @param array $cache_control Cache_control parts to render
* @return string
*/
public static function create_cache_control(array $cache_control)
{
// Create a buffer
$parts = array();
// Foreach cache control entry
foreach ($cache_control as $key => $value)
{
// Create a cache control fragment
$parts[] = empty($value) ? $key : ($key.'='.$value);
}
// Return the rendered parts
return implode(', ', $parts);
}
/**
* Parses the Cache-Control header and returning an array representation of the Cache-Control
* header.
*
* // Create the cache control header
* $response->header['cache-control'] = 'max-age=3600, must-revalidate, public';
*
* // Parse the cache control header
* if ($cache_control = Request::parse_cache_control($response->header['cache-control']))
* {
* // Cache-Control header was found
* $maxage = $cache_control['max-age'];
* }
*
* @param array $cache_control Array of headers
* @return mixed
*/
public static function parse_cache_control($cache_control)
{
// If no Cache-Control parts are detected
if ( (bool) preg_match_all('/(?<key>[a-z\-]+)=?(?<value>\w+)?/', $cache_control, $matches))
{
// Return combined cache-control key/value pairs
return array_combine($matches['key'], $matches['value']);
}
else
{
// Return
return FALSE;
}
}
// HTTP status codes and messages
public static $messages = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect',
// Client Error 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Timeout',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
// Server Error 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported',
509 => 'Bandwidth Limit Exceeded'
);
/**
* @var integer The response http status
*/
protected $_status = 200;
/**
* @var HTTP_Header Headers returned in the response
*/
protected $_header;
/**
* @var string The response body
*/
protected $_body = '';
/**
* @var array Cookies to be returned in the response
*/
protected $_cookies = array();
/**
* @var string The response protocol
*/
protected $_protocol;
/**
* Sets up the response object
*
* @param array $config Setup the response object
* @return void
*/
public function __construct(array $config = array())
{
$this->_header = new HTTP_Header(array());
foreach ($config as $key => $value)
{
if (property_exists($this, $key))
{
if ($key == '_header')
{
$this->headers($value);
}
else
{
$this->$key = $value;
}
}
}
}
/**
* Outputs the body when cast to string
*
* @return string
*/
public function __toString()
{
return $this->_body;
}
/**
* Gets or sets the body of the response
*
* @return mixed
*/
public function body($content = NULL)
{
if ($content === NULL)
return $this->_body;
$this->_body = (string) $content;
return $this;
}
/**
* Gets or sets the HTTP protocol. The standard protocol to use
* is `HTTP/1.1`.
*
* @param string $protocol Protocol to set to the request/response
* @return mixed
*/
public function protocol($protocol = NULL)
{
if ($protocol)
{
$this->_protocol = $protocol;
return $this;
}
return $this->_protocol;
}
/**
* Sets or gets the HTTP status from this response.
*
* // Set the HTTP status to 404 Not Found
* $response = Response::factory()
* ->status(404);
*
* // Get the current status
* $status = $response->status();
*
* @param integer $status Status to set to this response
* @return mixed
*/
public function status($status = NULL)
{
if ($status === NULL)
{
return $this->_status;
}
elseif (array_key_exists($status, Response::$messages))
{
$this->_status = (int) $status;
return $this;
}
else
{
throw new Kohana_Exception(__METHOD__.' unknown status value : :value', array(':value' => $status));
}
}
/**
* Gets and sets headers to the [Response], allowing chaining
* of response methods. If chaining isn't required, direct
* access to the property should be used instead.
*
* // Get a header
* $accept = $response->headers('Content-Type');
*
* // Set a header
* $response->headers('Content-Type', 'text/html');
*
* // Get all headers
* $headers = $response->headers();
*
* // Set multiple headers
* $response->headers(array('Content-Type' => 'text/html', 'Cache-Control' => 'no-cache'));
*
* @param mixed $key
* @param string $value
* @return mixed
*/
public function headers($key = NULL, $value = NULL)
{
if ($key === NULL)
{
return $this->_header;
}
elseif (is_array($key))
{
$this->_header->exchangeArray($key);
return $this;
}
elseif ($value === NULL)
{
return Arr::get($this->_header, $key);
}
else
{
$this->_header[$key] = $value;
return $this;
}
}
/**
* Returns the length of the body for use with
* content header
*
* @return integer
*/
public function content_length()
{
return strlen($this->_body);
}
/**
* Set and get cookies values for this response.
*
* // Get the cookies set to the response
* $cookies = $response->cookie();
*
* // Set a cookie to the response
* $response->cookie('session', array(
* 'value' => $value,
* 'expiration' => 12352234
* ));
*
* @param mixed cookie name, or array of cookie values
* @param string value to set to cookie
* @return string
* @return void
* @return [Response]
*/
public function cookie($key = NULL, $value = NULL)
{
// Handle the get cookie calls
if ($key === NULL)
return $this->_cookies;
elseif ( ! is_array($key) AND ! $value)
return Arr::get($this->_cookies, $key);
// Handle the set cookie calls
if (is_array($key))
{
reset($key);
while (list($_key, $_value) = each($key))
{
$this->cookie($_key, $_value);
}
}
else
{
if ( ! is_array($value))
{
$value = array(
'value' => $value,
'expiration' => Cookie::$expiration
);
}
elseif ( ! isset($value['expiration']))
{
$value['expiration'] = Cookie::$expiration;
}
$this->_cookies[$key] = $value;
}
return $this;
}
/**
* Deletes a cookie set to the response
*
* @param string name
* @return Response
*/
public function delete_cookie($name)
{
unset($this->_cookies[$name]);
return $this;
}
/**
* Deletes all cookies from this response
*
* @return Response
*/
public function delete_cookies()
{
$this->_cookies = array();
return $this;
}
/**
* Sends the response status and all set headers.
*
* @return Response
*/
public function send_headers()
{
if ( ! headers_sent())
{
if (isset($_SERVER['SERVER_PROTOCOL']))
{
// Use the default server protocol
$protocol = $_SERVER['SERVER_PROTOCOL'];
}
else
{
// Default to using newer protocol
$protocol = strtoupper(HTTP::$protocol).'/'.HTTP::$version;
}
// Default to text/html; charset=utf8 if no content type set
if ( ! $this->_header->offsetExists('content-type'))
{
$this->_header['content-type'] = Kohana::$content_type.'; charset='.Kohana::$charset;
}
// Add the X-Powered-By header
if (Kohana::$expose)
{
$this->_header['x-powered-by'] = 'Kohana Framework '.Kohana::VERSION.' ('.Kohana::CODENAME.')';
}
if ( ! Kohana::$is_cli)
{
// HTTP status line
header($protocol.' '.$this->_status.' '.Response::$messages[$this->_status]);
foreach ($this->_header as $name => $value)
{
if (is_string($name))
{
// Combine the name and value to make a raw header
$value = $name.': '.$value;
}
// Send the raw header
header($value, TRUE);
}
}
// Send cookies
foreach ($this->_cookies as $name => $value)
{
Cookie::set($name, $value['value'], $value['expiration']);
}
}
return $this;
}
/**
* 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->_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($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';
// 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->protocol() === 'https')
{
// 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 = Kohana_Exception::text($e);
if (is_object(Kohana::$log))
{
// Add this exception to the log
Kohana::$log->add(Log::ERROR, $error);
// Make sure the logs are written
Kohana::$log->write();
}
// Do NOT display the exception, it will corrupt the output!
}
}
// Stop execution
exit;
}
/**
* Renders the HTTP_Interaction to a string, producing
*
* - Protocol
* - Headers
* - Body
*
* @return string
*/
public function render()
{
if ( ! $this->_header->offsetExists('content-type'))
{
// Add the default Content-Type header if required
$this->_header['content-type'] = Kohana::$content_type.'; charset='.Kohana::$charset;
}
$content_length = $this->content_length();
// Set the content length for the body if required
if ($content_length > 0)
{
$this->_header['content-length'] = (string) $content_length;
}
// Prepare cookies
if ($this->_cookies)
{
if (extension_loaded('http'))
{
$this->_header['set-cookie'] = http_build_cookie($this->_cookies);
}
else
{
$cookies = array();
// Parse each
foreach ($this->_cookies as $key => $value)
{
$string = $key.'='.$value['value'].'; expires='.date('l, d M Y H:i:s T', $value['expiration']);
$cookies[] = $string;
}
// Create the cookie string
$this->_header['set-cookie'] = $cookies;
}
}
$output = $this->_protocol.' '.$this->_status.' '.Response::$messages[$this->_status]."\n";
$output .= (string) $this->_header;
$output .= $this->_body;
return $output;
}
/**
* Generate ETag
* Generates an ETag from the response ready to be returned
*
* @throws Kohana_Request_Exception
* @return String Generated ETag
*/
public function generate_etag()
{
if ($this->_body === NULL)
{
throw new Kohana_Request_Exception('No response yet associated with request - cannot auto generate resource ETag');
}
// Generate a unique hash for the response
return '"'.sha1($this->render()).'"';
}
/**
* Check Cache
* Checks the browser cache to see the response needs to be returned
*
* @param string $etag Resource ETag
* @param Request $request The request to test against
* @return Response
* @throws Kohana_Request_Exception
*/
public function check_cache($etag = NULL, Request $request = NULL)
{
if ( ! $etag)
{
$etag = $this->generate_etag();
}
if ( ! $request)
throw new Kohana_Request_Exception('A Request object must be supplied with an etag for evaluation');
// Set the ETag header
$this->_header['etag'] = $etag;
// Add the Cache-Control header if it is not already set
// This allows etags to be used with max-age, etc
if ($this->_header->offsetExists('cache-control'))
{
if (is_array($this->_header['cache-control']))
{
$this->_header['cache-control'][] = new HTTP_Header_Value('must-revalidate');
}
else
{
$this->_header['cache-control'] = $this->_header['cache-control'].', must-revalidate';
}
}
else
{
$this->_header['cache-control'] = 'must-revalidate';
}
if ($request->headers('if-none-match') AND (string) $request->headers('if-none-match') === $etag)
{
// No need to send data again
$this->_status = 304;
$this->send_headers();
// Stop execution
exit;
}
return $this;
}
/**
* Serializes the object to json - handy if you
* need to pass the response data to other
* systems
*
* @param array array of data to serialize
* @return string
* @throws Kohana_Exception
*/
public function serialize(array $to_serialize = array())
{
// Serialize the class properties
$to_serialize += array
(
'_status' => $this->_status,
'_header' => $this->_header,
'_cookies' => $this->_cookies,
'_body' => $this->_body
);
$serialized = serialize($to_serialize);
if (is_string($serialized))
{
return $serialized;
}
else
{
throw new Kohana_Exception('Unable to serialize object');
}
}
/**
* JSON encoded object
*
* @param string json encoded object
* @return bool
* @throws Kohana_Exception
*/
public function unserialize($string)
{
// Unserialise object
$unserialized = unserialize($string);
// If failed
if ($unserialized === NULL)
{
// Throw exception
throw new Kohana_Exception('Unable to correctly unserialize string: :string', array(':string' => $string));
}
// Foreach key/value pair
foreach ($unserialized as $key => $value)
{
$this->$key = $value;
}
return TRUE;
}
/**
* Parse the byte ranges from the HTTP_RANGE header used for
* resumable downloads.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
* @return array|FALSE
*/
protected function _parse_byte_range()
{
if ( ! isset($_SERVER['HTTP_RANGE']))
{
return FALSE;
}
// TODO, speed this up with the use of string functions.
preg_match_all('/(-?[0-9]++(?:-(?![0-9]++))?)(?:-?([0-9]++))?/', $_SERVER['HTTP_RANGE'], $matches, PREG_SET_ORDER);
return $matches[0];
}
/**
* Calculates the byte range to use with send_file. If HTTP_RANGE doesn't
* exist then the complete byte range is returned
*
* @param integer $size
* @return array
*/
protected function _calculate_byte_range($size)
{
// Defaults to start with when the HTTP_RANGE header doesn't exist.
$start = 0;
$end = $size - 1;
if ($range = $this->_parse_byte_range())
{
// We have a byte range from HTTP_RANGE
$start = $range[1];
if ($start[0] === '-')
{
// A negative value means we start from the end, so -500 would be the
// last 500 bytes.
$start = $size - abs($start);
}
if (isset($range[2]))
{
// Set the end range
$end = $range[2];
}
}
// Normalize values.
$start = abs(intval($start));
// Keep the the end value in bounds and normalize it.
$end = min(abs(intval($end)), $size - 1);
// Keep the start in bounds.
$start = ($end < $start) ? 0 : max($start, 0);
return array($start, $end);
}
} // End Kohana_Response
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/response.php | PHP | mit | 21,691 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Helper functions for working in a command-line environment.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_CLI {
/**
* Returns one or more command-line options. Options are specified using
* standard CLI syntax:
*
* php index.php --username=john.smith --password=secret --var="some value with spaces"
*
* // Get the values of "username" and "password"
* $auth = CLI::options('username', 'password');
*
* @param string option name
* @param ...
* @return array
*/
public static function options($options)
{
// Get all of the requested options
$options = func_get_args();
// Found option values
$values = array();
// Skip the first option, it is always the file executed
for ($i = 1; $i < $_SERVER['argc']; $i++)
{
if ( ! isset($_SERVER['argv'][$i]))
{
// No more args left
break;
}
// Get the option
$opt = $_SERVER['argv'][$i];
if (substr($opt, 0, 2) !== '--')
{
// This is not an option argument
continue;
}
// Remove the "--" prefix
$opt = substr($opt, 2);
if (strpos($opt, '='))
{
// Separate the name and value
list ($opt, $value) = explode('=', $opt, 2);
}
else
{
$value = NULL;
}
if (in_array($opt, $options))
{
// Set the given value
$values[$opt] = $value;
}
}
return $values;
}
} // End CLI
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/cli.php | PHP | mit | 1,570 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Contains the most low-level helpers methods in Kohana:
*
* - Environment initialization
* - Locating files within the cascading filesystem
* - Auto-loading and transparent extension of classes
* - Variable and path debugging
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Core {
// Release version and codename
const VERSION = '3.1.2';
const CODENAME = 'Hirondelle';
// Common environment type constants for consistency and convenience
const PRODUCTION = 1;
const STAGING = 2;
const TESTING = 3;
const DEVELOPMENT = 4;
// Security check that is added to all generated PHP files
const FILE_SECURITY = '<?php defined(\'SYSPATH\') or die(\'No direct script access.\');';
// Format of cache files: header, cache name, and data
const FILE_CACHE = ":header \n\n// :name\n\n:data\n";
/**
* @var string Current environment name
*/
public static $environment = Kohana::DEVELOPMENT;
/**
* @var boolean True if Kohana is running from the command line
*/
public static $is_cli = FALSE;
/**
* @var boolean True if Kohana is running on windows
*/
public static $is_windows = FALSE;
/**
* @var boolean True if [magic quotes](http://php.net/manual/en/security.magicquotes.php) is enabled.
*/
public static $magic_quotes = FALSE;
/**
* @var boolean Should errors and exceptions be logged
*/
public static $log_errors = FALSE;
/**
* @var boolean TRUE if PHP safe mode is on
*/
public static $safe_mode = FALSE;
/**
* @var string
*/
public static $content_type = 'text/html';
/**
* @var string character set of input and output
*/
public static $charset = 'utf-8';
/**
* @var string the name of the server Kohana is hosted upon
*/
public static $server_name = '';
/**
* @var array list of valid host names for this instance
*/
public static $hostnames = array();
/**
* @var string base URL to the application
*/
public static $base_url = '/';
/**
* @var string Application index file, added to links generated by Kohana. Set by [Kohana::init]
*/
public static $index_file = 'index.php';
/**
* @var string Cache directory, used by [Kohana::cache]. Set by [Kohana::init]
*/
public static $cache_dir;
/**
* @var integer Default lifetime for caching, in seconds, used by [Kohana::cache]. Set by [Kohana::init]
*/
public static $cache_life = 60;
/**
* @var boolean Whether to use internal caching for [Kohana::find_file], does not apply to [Kohana::cache]. Set by [Kohana::init]
*/
public static $caching = FALSE;
/**
* @var boolean Whether to enable [profiling](kohana/profiling). Set by [Kohana::init]
*/
public static $profiling = TRUE;
/**
* @var boolean Enable Kohana catching and displaying PHP errors and exceptions. Set by [Kohana::init]
*/
public static $errors = TRUE;
/**
* @var array Types of errors to display at shutdown
*/
public static $shutdown_errors = array(E_PARSE, E_ERROR, E_USER_ERROR);
/**
* @var boolean set the X-Powered-By header
*/
public static $expose = FALSE;
/**
* @var Log logging object
*/
public static $log;
/**
* @var Config config object
*/
public static $config;
/**
* @var boolean Has [Kohana::init] been called?
*/
protected static $_init = FALSE;
/**
* @var array Currently active modules
*/
protected static $_modules = array();
/**
* @var array Include paths that are used to find files
*/
protected static $_paths = array(APPPATH, SYSPATH);
/**
* @var array File path cache, used when caching is true in [Kohana::init]
*/
protected static $_files = array();
/**
* @var boolean Has the file path cache changed during this execution? Used internally when when caching is true in [Kohana::init]
*/
protected static $_files_changed = FALSE;
/**
* Initializes the environment:
*
* - Disables register_globals and magic_quotes_gpc
* - Determines the current environment
* - Set global settings
* - Sanitizes GET, POST, and COOKIE variables
* - Converts GET, POST, and COOKIE variables to the global character set
*
* The following settings can be set:
*
* Type | Setting | Description | Default Value
* ----------|------------|------------------------------------------------|---------------
* `string` | base_url | The base URL for your application. This should be the *relative* path from your DOCROOT to your `index.php` file, in other words, if Kohana is in a subfolder, set this to the subfolder name, otherwise leave it as the default. **The leading slash is required**, trailing slash is optional. | `"/"`
* `string` | index_file | The name of the [front controller](http://en.wikipedia.org/wiki/Front_Controller_pattern). This is used by Kohana to generate relative urls like [HTML::anchor()] and [URL::base()]. This is usually `index.php`. To [remove index.php from your urls](tutorials/clean-urls), set this to `FALSE`. | `"index.php"`
* `string` | charset | Character set used for all input and output | `"utf-8"`
* `string` | cache_dir | Kohana's cache directory. Used by [Kohana::cache] for simple internal caching, like [Fragments](kohana/fragments) and **\[caching database queries](this should link somewhere)**. This has nothing to do with the [Cache module](cache). | `APPPATH."cache"`
* `integer` | cache_life | Lifetime, in seconds, of items cached by [Kohana::cache] | `60`
* `boolean` | errors | Should Kohana catch PHP errors and uncaught Exceptions and show the `error_view`. See [Error Handling](kohana/errors) for more info. <br /> <br /> Recommended setting: `TRUE` while developing, `FALSE` on production servers. | `TRUE`
* `boolean` | profile | Whether to enable the [Profiler](kohana/profiling). <br /> <br />Recommended setting: `TRUE` while developing, `FALSE` on production servers. | `TRUE` * `boolean` | caching | Cache file locations to speed up [Kohana::find_file]. This has nothing to do with [Kohana::cache], [Fragments](kohana/fragments) or the [Cache module](cache). <br /> <br /> Recommended setting: `FALSE` while developing, `TRUE` on production servers. | `FALSE`
*
* @throws Kohana_Exception
* @param array Array of settings. See above.
* @return void
* @uses Kohana::globals
* @uses Kohana::sanitize
* @uses Kohana::cache
* @uses Profiler
*/
public static function init(array $settings = NULL)
{
if (Kohana::$_init)
{
// Do not allow execution twice
return;
}
// Kohana is now initialized
Kohana::$_init = TRUE;
if (isset($settings['profile']))
{
// Enable profiling
Kohana::$profiling = (bool) $settings['profile'];
}
// Start an output buffer
ob_start();
if (isset($settings['errors']))
{
// Enable error handling
Kohana::$errors = (bool) $settings['errors'];
}
if (Kohana::$errors === TRUE)
{
// Enable Kohana exception handling, adds stack traces and error source.
set_exception_handler(array('Kohana_Exception', 'handler'));
// Enable Kohana error handling, converts all PHP errors to exceptions.
set_error_handler(array('Kohana', 'error_handler'));
}
// Enable the Kohana shutdown handler, which catches E_FATAL errors.
register_shutdown_function(array('Kohana', 'shutdown_handler'));
if (ini_get('register_globals'))
{
// Reverse the effects of register_globals
Kohana::globals();
}
if (isset($settings['expose']))
{
Kohana::$expose = (bool) $settings['expose'];
}
// Determine if we are running in a command line environment
Kohana::$is_cli = (PHP_SAPI === 'cli');
// Determine if we are running in a Windows environment
Kohana::$is_windows = (DIRECTORY_SEPARATOR === '\\');
// Determine if we are running in safe mode
Kohana::$safe_mode = (bool) ini_get('safe_mode');
if (isset($settings['cache_dir']))
{
if ( ! is_dir($settings['cache_dir']))
{
try
{
// Create the cache directory
mkdir($settings['cache_dir'], 0755, TRUE);
// Set permissions (must be manually set to fix umask issues)
chmod($settings['cache_dir'], 0755);
}
catch (Exception $e)
{
throw new Kohana_Exception('Could not create cache directory :dir',
array(':dir' => Debug::path($settings['cache_dir'])));
}
}
// Set the cache directory path
Kohana::$cache_dir = realpath($settings['cache_dir']);
}
else
{
// Use the default cache directory
Kohana::$cache_dir = APPPATH.'cache';
}
if ( ! is_writable(Kohana::$cache_dir))
{
throw new Kohana_Exception('Directory :dir must be writable',
array(':dir' => Debug::path(Kohana::$cache_dir)));
}
if (isset($settings['cache_life']))
{
// Set the default cache lifetime
Kohana::$cache_life = (int) $settings['cache_life'];
}
if (isset($settings['caching']))
{
// Enable or disable internal caching
Kohana::$caching = (bool) $settings['caching'];
}
if (Kohana::$caching === TRUE)
{
// Load the file path cache
Kohana::$_files = Kohana::cache('Kohana::find_file()');
}
if (isset($settings['charset']))
{
// Set the system character set
Kohana::$charset = strtolower($settings['charset']);
}
if (function_exists('mb_internal_encoding'))
{
// Set the MB extension encoding to the same character set
mb_internal_encoding(Kohana::$charset);
}
if (isset($settings['base_url']))
{
// Set the base URL
Kohana::$base_url = rtrim($settings['base_url'], '/').'/';
}
if (isset($settings['index_file']))
{
// Set the index file
Kohana::$index_file = trim($settings['index_file'], '/');
}
// Determine if the extremely evil magic quotes are enabled
Kohana::$magic_quotes = (bool) get_magic_quotes_gpc();
// Sanitize all request variables
$_GET = Kohana::sanitize($_GET);
$_POST = Kohana::sanitize($_POST);
$_COOKIE = Kohana::sanitize($_COOKIE);
// Load the logger
Kohana::$log = Log::instance();
// Load the config
Kohana::$config = Config::instance();
}
/**
* Cleans up the environment:
*
* - Restore the previous error and exception handlers
* - Destroy the Kohana::$log and Kohana::$config objects
*
* @return void
*/
public static function deinit()
{
if (Kohana::$_init)
{
// Removed the autoloader
spl_autoload_unregister(array('Kohana', 'auto_load'));
if (Kohana::$errors)
{
// Go back to the previous error handler
restore_error_handler();
// Go back to the previous exception handler
restore_exception_handler();
}
// Destroy objects created by init
Kohana::$log = Kohana::$config = NULL;
// Reset internal storage
Kohana::$_modules = Kohana::$_files = array();
Kohana::$_paths = array(APPPATH, SYSPATH);
// Reset file cache status
Kohana::$_files_changed = FALSE;
// Kohana is no longer initialized
Kohana::$_init = FALSE;
}
}
/**
* Reverts the effects of the `register_globals` PHP setting by unsetting
* all global varibles except for the default super globals (GPCS, etc),
* which is a [potential security hole.][ref-wikibooks]
*
* This is called automatically by [Kohana::init] if `register_globals` is
* on.
*
*
* [ref-wikibooks]: http://en.wikibooks.org/wiki/PHP_Programming/Register_Globals
*
* @return void
*/
public static function globals()
{
if (isset($_REQUEST['GLOBALS']) OR isset($_FILES['GLOBALS']))
{
// Prevent malicious GLOBALS overload attack
echo "Global variable overload attack detected! Request aborted.\n";
// Exit with an error status
exit(1);
}
// Get the variable names of all globals
$global_variables = array_keys($GLOBALS);
// Remove the standard global variables from the list
$global_variables = array_diff($global_variables, array(
'_COOKIE',
'_ENV',
'_GET',
'_FILES',
'_POST',
'_REQUEST',
'_SERVER',
'_SESSION',
'GLOBALS',
));
foreach ($global_variables as $name)
{
// Unset the global variable, effectively disabling register_globals
unset($GLOBALS[$name]);
}
}
/**
* Recursively sanitizes an input variable:
*
* - Strips slashes if magic quotes are enabled
* - Normalizes all newlines to LF
*
* @param mixed any variable
* @return mixed sanitized variable
*/
public static function sanitize($value)
{
if (is_array($value) OR is_object($value))
{
foreach ($value as $key => $val)
{
// Recursively clean each value
$value[$key] = Kohana::sanitize($val);
}
}
elseif (is_string($value))
{
if (Kohana::$magic_quotes === TRUE)
{
// Remove slashes added by magic quotes
$value = stripslashes($value);
}
if (strpos($value, "\r") !== FALSE)
{
// Standardize newlines
$value = str_replace(array("\r\n", "\r"), "\n", $value);
}
}
return $value;
}
/**
* Provides auto-loading support of classes that follow Kohana's [class
* naming conventions](kohana/conventions#class-names-and-file-location).
* See [Loading Classes](kohana/autoloading) for more information.
*
* Class names are converted to file names by making the class name
* lowercase and converting underscores to slashes:
*
* // Loads classes/my/class/name.php
* Kohana::auto_load('My_Class_Name');
*
* You should never have to call this function, as simply calling a class
* will cause it to be called.
*
* This function must be enabled as an autoloader in the bootstrap:
*
* spl_autoload_register(array('Kohana', 'auto_load'));
*
* @param string class name
* @return boolean
*/
public static function auto_load($class)
{
try
{
// Transform the class name into a path
$file = str_replace('_', '/', strtolower($class));
if ($path = Kohana::find_file('classes', $file))
{
// Load the class file
require $path;
// Class has been found
return TRUE;
}
// Class is not in the filesystem
return FALSE;
}
catch (Exception $e)
{
Kohana_Exception::handler($e);
die;
}
}
/**
* Changes the currently enabled modules. Module paths may be relative
* or absolute, but must point to a directory:
*
* Kohana::modules(array('modules/foo', MODPATH.'bar'));
*
* @param array list of module paths
* @return array enabled modules
*/
public static function modules(array $modules = NULL)
{
if ($modules === NULL)
{
// Not changing modules, just return the current set
return Kohana::$_modules;
}
// Start a new list of include paths, APPPATH first
$paths = array(APPPATH);
foreach ($modules as $name => $path)
{
if (is_dir($path))
{
// Add the module to include paths
$paths[] = $modules[$name] = realpath($path).DIRECTORY_SEPARATOR;
}
else
{
// This module is invalid, remove it
unset($modules[$name]);
}
}
// Finish the include paths by adding SYSPATH
$paths[] = SYSPATH;
// Set the new include paths
Kohana::$_paths = $paths;
// Set the current module list
Kohana::$_modules = $modules;
foreach (Kohana::$_modules as $path)
{
$init = $path.'init'.EXT;
if (is_file($init))
{
// Include the module initialization file once
require_once $init;
}
}
return Kohana::$_modules;
}
/**
* Returns the the currently active include paths, including the
* application, system, and each module's path.
*
* @return array
*/
public static function include_paths()
{
return Kohana::$_paths;
}
/**
* Searches for a file in the [Cascading Filesystem](kohana/files), and
* returns the path to the file that has the highest precedence, so that it
* can be included.
*
* When searching the "config", "messages", or "i18n" directories, or when
* the `$array` flag is set to true, an array of all the files that match
* that path in the [Cascading Filesystem](kohana/files) will be returned.
* These files will return arrays which must be merged together.
*
* If no extension is given, the default extension (`EXT` set in
* `index.php`) will be used.
*
* // Returns an absolute path to views/template.php
* Kohana::find_file('views', 'template');
*
* // Returns an absolute path to media/css/style.css
* Kohana::find_file('media', 'css/style', 'css');
*
* // Returns an array of all the "mimes" configuration files
* Kohana::find_file('config', 'mimes');
*
* @param string directory name (views, i18n, classes, extensions, etc.)
* @param string filename with subdirectory
* @param string extension to search for
* @param boolean return an array of files?
* @return array a list of files when $array is TRUE
* @return string single file path
*/
public static function find_file($dir, $file, $ext = NULL, $array = FALSE)
{
if ($ext === NULL)
{
// Use the default extension
$ext = EXT;
}
elseif ($ext)
{
// Prefix the extension with a period
$ext = ".{$ext}";
}
else
{
// Use no extension
$ext = '';
}
// Create a partial path of the filename
$path = $dir.DIRECTORY_SEPARATOR.$file.$ext;
if (Kohana::$caching === TRUE AND isset(Kohana::$_files[$path.($array ? '_array' : '_path')]))
{
// This path has been cached
return Kohana::$_files[$path.($array ? '_array' : '_path')];
}
if (Kohana::$profiling === TRUE AND class_exists('Profiler', FALSE))
{
// Start a new benchmark
$benchmark = Profiler::start('Kohana', __FUNCTION__);
}
if ($array OR $dir === 'config' OR $dir === 'i18n' OR $dir === 'messages')
{
// Include paths must be searched in reverse
$paths = array_reverse(Kohana::$_paths);
// Array of files that have been found
$found = array();
foreach ($paths as $dir)
{
if (is_file($dir.$path))
{
// This path has a file, add it to the list
$found[] = $dir.$path;
}
}
}
else
{
// The file has not been found yet
$found = FALSE;
foreach (Kohana::$_paths as $dir)
{
if (is_file($dir.$path))
{
// A path has been found
$found = $dir.$path;
// Stop searching
break;
}
}
}
if (Kohana::$caching === TRUE)
{
// Add the path to the cache
Kohana::$_files[$path.($array ? '_array' : '_path')] = $found;
// Files have been changed
Kohana::$_files_changed = TRUE;
}
if (isset($benchmark))
{
// Stop the benchmark
Profiler::stop($benchmark);
}
return $found;
}
/**
* Recursively finds all of the files in the specified directory at any
* location in the [Cascading Filesystem](kohana/files), and returns an
* array of all the files found, sorted alphabetically.
*
* // Find all view files.
* $views = Kohana::list_files('views');
*
* @param string directory name
* @param array list of paths to search
* @return array
*/
public static function list_files($directory = NULL, array $paths = NULL)
{
if ($directory !== NULL)
{
// Add the directory separator
$directory .= DIRECTORY_SEPARATOR;
}
if ($paths === NULL)
{
// Use the default paths
$paths = Kohana::$_paths;
}
// Create an array for the files
$found = array();
foreach ($paths as $path)
{
if (is_dir($path.$directory))
{
// Create a new directory iterator
$dir = new DirectoryIterator($path.$directory);
foreach ($dir as $file)
{
// Get the file name
$filename = $file->getFilename();
if ($filename[0] === '.' OR $filename[strlen($filename)-1] === '~')
{
// Skip all hidden files and UNIX backup files
continue;
}
// Relative filename is the array key
$key = $directory.$filename;
if ($file->isDir())
{
if ($sub_dir = Kohana::list_files($key, $paths))
{
if (isset($found[$key]))
{
// Append the sub-directory list
$found[$key] += $sub_dir;
}
else
{
// Create a new sub-directory list
$found[$key] = $sub_dir;
}
}
}
else
{
if ( ! isset($found[$key]))
{
// Add new files to the list
$found[$key] = realpath($file->getPathName());
}
}
}
}
}
// Sort the results alphabetically
ksort($found);
return $found;
}
/**
* Loads a file within a totally empty scope and returns the output:
*
* $foo = Kohana::load('foo.php');
*
* @param string
* @return mixed
*/
public static function load($file)
{
return include $file;
}
/**
* Returns the configuration array for the requested group. See
* [configuration files](kohana/files/config) for more information.
*
* // Get all the configuration in config/database.php
* $config = Kohana::config('database');
*
* // Get only the default connection configuration
* $default = Kohana::config('database.default')
*
* // Get only the hostname of the default connection
* $host = Kohana::config('database.default.connection.hostname')
*
* @param string group name
* @return Config
*/
public static function config($group)
{
static $config;
if (strpos($group, '.') !== FALSE)
{
// Split the config group and path
list ($group, $path) = explode('.', $group, 2);
}
if ( ! isset($config[$group]))
{
// Load the config group into the cache
$config[$group] = Kohana::$config->load($group);
}
if (isset($path))
{
return Arr::path($config[$group], $path, NULL, '.');
}
else
{
return $config[$group];
}
}
/**
* Provides simple file-based caching for strings and arrays:
*
* // Set the "foo" cache
* Kohana::cache('foo', 'hello, world');
*
* // Get the "foo" cache
* $foo = Kohana::cache('foo');
*
* All caches are stored as PHP code, generated with [var_export][ref-var].
* Caching objects may not work as expected. Storing references or an
* object or array that has recursion will cause an E_FATAL.
*
* The cache directory and default cache lifetime is set by [Kohana::init]
*
* [ref-var]: http://php.net/var_export
*
* @throws Kohana_Exception
* @param string name of the cache
* @param mixed data to cache
* @param integer number of seconds the cache is valid for
* @return mixed for getting
* @return boolean for setting
*/
public static function cache($name, $data = NULL, $lifetime = NULL)
{
// Cache file is a hash of the name
$file = sha1($name).'.txt';
// Cache directories are split by keys to prevent filesystem overload
$dir = Kohana::$cache_dir.DIRECTORY_SEPARATOR.$file[0].$file[1].DIRECTORY_SEPARATOR;
if ($lifetime === NULL)
{
// Use the default lifetime
$lifetime = Kohana::$cache_life;
}
if ($data === NULL)
{
if (is_file($dir.$file))
{
if ((time() - filemtime($dir.$file)) < $lifetime)
{
// Return the cache
try
{
return unserialize(file_get_contents($dir.$file));
}
catch (Exception $e)
{
// Cache is corrupt, let return happen normally.
}
}
else
{
try
{
// Cache has expired
unlink($dir.$file);
}
catch (Exception $e)
{
// Cache has mostly likely already been deleted,
// let return happen normally.
}
}
}
// Cache not found
return NULL;
}
if ( ! is_dir($dir))
{
// Create the cache directory
mkdir($dir, 0777, TRUE);
// Set permissions (must be manually set to fix umask issues)
chmod($dir, 0777);
}
// Force the data to be a string
$data = serialize($data);
try
{
// Write the cache
return (bool) file_put_contents($dir.$file, $data, LOCK_EX);
}
catch (Exception $e)
{
// Failed to write cache
return FALSE;
}
}
/**
* Get a message from a file. Messages are arbitary strings that are stored
* in the `messages/` directory and reference by a key. Translation is not
* performed on the returned values. See [message files](kohana/files/messages)
* for more information.
*
* // Get "username" from messages/text.php
* $username = Kohana::message('text', 'username');
*
* @param string file name
* @param string key path to get
* @param mixed default value if the path does not exist
* @return string message string for the given path
* @return array complete message list, when no path is specified
* @uses Arr::merge
* @uses Arr::path
*/
public static function message($file, $path = NULL, $default = NULL)
{
static $messages;
if ( ! isset($messages[$file]))
{
// Create a new message list
$messages[$file] = array();
if ($files = Kohana::find_file('messages', $file))
{
foreach ($files as $f)
{
// Combine all the messages recursively
$messages[$file] = Arr::merge($messages[$file], Kohana::load($f));
}
}
}
if ($path === NULL)
{
// Return all of the messages
return $messages[$file];
}
else
{
// Get a message using the path
return Arr::path($messages[$file], $path, $default);
}
}
/**
* PHP error handler, converts all errors into ErrorExceptions. This handler
* respects error_reporting settings.
*
* @throws ErrorException
* @return TRUE
*/
public static function error_handler($code, $error, $file = NULL, $line = NULL)
{
if (error_reporting() & $code)
{
// This error is not suppressed by current error reporting settings
// Convert the error into an ErrorException
throw new ErrorException($error, $code, 0, $file, $line);
}
// Do not execute the PHP error handler
return TRUE;
}
/**
* Catches errors that are not caught by the error handler, such as E_PARSE.
*
* @uses Kohana_Exception::handler
* @return void
*/
public static function shutdown_handler()
{
if ( ! Kohana::$_init)
{
// Do not execute when not active
return;
}
try
{
if (Kohana::$caching === TRUE AND Kohana::$_files_changed === TRUE)
{
// Write the file path cache
Kohana::cache('Kohana::find_file()', Kohana::$_files);
}
}
catch (Exception $e)
{
// Pass the exception to the handler
Kohana_Exception::handler($e);
}
if (Kohana::$errors AND $error = error_get_last() AND in_array($error['type'], Kohana::$shutdown_errors))
{
// Clean the output buffer
ob_get_level() and ob_clean();
// Fake an exception for nice debugging
Kohana_Exception::handler(new ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line']));
// Shutdown now to avoid a "death loop"
exit(1);
}
}
} // End Kohana
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/core.php | PHP | mit | 26,882 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* View fragment caching. This is primarily used to cache small parts of a view
* that rarely change. For instance, you may want to cache the footer of your
* template because it has very little dynamic content. Or you could cache a
* user profile page and delete the fragment when the user updates.
*
* For obvious reasons, fragment caching should not be applied to any
* content that contains forms.
*
* [!!] Multiple language (I18n) support was added in v3.0.4.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
* @uses Kohana::cache
*/
class Kohana_Fragment {
/**
* @var integer default number of seconds to cache for
*/
public static $lifetime = 30;
/**
* @var boolean use multilingual fragment support?
*/
public static $i18n = FALSE;
/**
* @var array list of buffer => cache key
*/
protected static $_caches = array();
/**
* Generate the cache key name for a fragment.
*
* $key = Fragment::_cache_key('footer', TRUE);
*
* @param string fragment name
* @param boolean multilingual fragment support
* @return string
* @uses I18n::lang
* @since 3.0.4
*/
protected static function _cache_key($name, $i18n = NULL)
{
if ($i18n === NULL)
{
// Use the default setting
$i18n = Fragment::$i18n;
}
// Language prefix for cache key
$i18n = ($i18n === TRUE) ? I18n::lang() : '';
// Note: $i18n and $name need to be delimited to prevent naming collisions
return 'Fragment::cache('.$i18n.'+'.$name.')';
}
/**
* Load a fragment from cache and display it. Multiple fragments can
* be nested with different life times.
*
* if ( ! Fragment::load('footer')) {
* // Anything that is echo'ed here will be saved
* Fragment::save();
* }
*
* @param string fragment name
* @param integer fragment cache lifetime
* @param boolean multilingual fragment support
* @return boolean
*/
public static function load($name, $lifetime = NULL, $i18n = NULL)
{
// Set the cache lifetime
$lifetime = ($lifetime === NULL) ? Fragment::$lifetime : (int) $lifetime;
// Get the cache key name
$cache_key = Fragment::_cache_key($name, $i18n);
if ($fragment = Kohana::cache($cache_key, NULL, $lifetime))
{
// Display the cached fragment now
echo $fragment;
return TRUE;
}
else
{
// Start the output buffer
ob_start();
// Store the cache key by the buffer level
Fragment::$_caches[ob_get_level()] = $cache_key;
return FALSE;
}
}
/**
* Saves the currently open fragment in the cache.
*
* Fragment::save();
*
* @return void
*/
public static function save()
{
// Get the buffer level
$level = ob_get_level();
if (isset(Fragment::$_caches[$level]))
{
// Get the cache key based on the level
$cache_key = Fragment::$_caches[$level];
// Delete the cache key, we don't need it anymore
unset(Fragment::$_caches[$level]);
// Get the output buffer and display it at the same time
$fragment = ob_get_flush();
// Cache the fragment
Kohana::cache($cache_key, $fragment);
}
}
/**
* Delete a cached fragment.
*
* Fragment::delete($key);
*
* @param string fragment name
* @param boolean multilingual fragment support
* @return void
*/
public static function delete($name, $i18n = NULL)
{
// Invalid the cache
Kohana::cache(Fragment::_cache_key($name, $i18n), NULL, -3600);
}
} // End Fragment
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/fragment.php | PHP | mit | 3,638 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Base session class.
*
* @package Kohana
* @category Session
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class Kohana_Session {
/**
* @var string default session adapter
*/
public static $default = 'native';
/**
* @var array session instances
*/
public static $instances = array();
/**
* Creates a singleton session of the given type. Some session types
* (native, database) also support restarting a session by passing a
* session id as the second parameter.
*
* $session = Session::instance();
*
* [!!] [Session::write] will automatically be called when the request ends.
*
* @param string type of session (native, cookie, etc)
* @param string session identifier
* @return Session
* @uses Kohana::config
*/
public static function instance($type = NULL, $id = NULL)
{
if ($type === NULL)
{
// Use the default type
$type = Session::$default;
}
if ( ! isset(Session::$instances[$type]))
{
// Load the configuration for this type
$config = Kohana::config('session')->get($type);
// Set the session class name
$class = 'Session_'.ucfirst($type);
// Create a new session instance
Session::$instances[$type] = $session = new $class($config, $id);
// Write the session at shutdown
register_shutdown_function(array($session, 'write'));
}
return Session::$instances[$type];
}
/**
* @var string cookie name
*/
protected $_name = 'session';
/**
* @var int cookie lifetime
*/
protected $_lifetime = 0;
/**
* @var bool encrypt session data?
*/
protected $_encrypted = FALSE;
/**
* @var array session data
*/
protected $_data = array();
/**
* @var bool session destroyed?
*/
protected $_destroyed = FALSE;
/**
* Overloads the name, lifetime, and encrypted session settings.
*
* [!!] Sessions can only be created using the [Session::instance] method.
*
* @param array configuration
* @param string session id
* @return void
* @uses Session::read
*/
public function __construct(array $config = NULL, $id = NULL)
{
if (isset($config['name']))
{
// Cookie name to store the session id in
$this->_name = (string) $config['name'];
}
if (isset($config['lifetime']))
{
// Cookie lifetime
$this->_lifetime = (int) $config['lifetime'];
}
if (isset($config['encrypted']))
{
if ($config['encrypted'] === TRUE)
{
// Use the default Encrypt instance
$config['encrypted'] = 'default';
}
// Enable or disable encryption of data
$this->_encrypted = $config['encrypted'];
}
// Load the session
$this->read($id);
}
/**
* Session object is rendered to a serialized string. If encryption is
* enabled, the session will be encrypted. If not, the output string will
* be encoded using [base64_encode].
*
* echo $session;
*
* @return string
* @uses Encrypt::encode
*/
public function __toString()
{
// Serialize the data array
$data = serialize($this->_data);
if ($this->_encrypted)
{
// Encrypt the data using the default key
$data = Encrypt::instance($this->_encrypted)->encode($data);
}
else
{
// Obfuscate the data with base64 encoding
$data = base64_encode($data);
}
return $data;
}
/**
* Returns the current session array. The returned array can also be
* assigned by reference.
*
* // Get a copy of the current session data
* $data = $session->as_array();
*
* // Assign by reference for modification
* $data =& $session->as_array();
*
* @return array
*/
public function & as_array()
{
return $this->_data;
}
/**
* Get the current session id, if the session supports it.
*
* $id = $session->id();
*
* [!!] Not all session types have ids.
*
* @return string
* @since 3.0.8
*/
public function id()
{
return NULL;
}
/**
* Get the current session cookie name.
*
* $name = $session->name();
*
* @return string
* @since 3.0.8
*/
public function name()
{
return $this->_name;
}
/**
* Get a variable from the session array.
*
* $foo = $session->get('foo');
*
* @param string variable name
* @param mixed default value to return
* @return mixed
*/
public function get($key, $default = NULL)
{
return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
}
/**
* Get and delete a variable from the session array.
*
* $bar = $session->get_once('bar');
*
* @param string variable name
* @param mixed default value to return
* @return mixed
*/
public function get_once($key, $default = NULL)
{
$value = $this->get($key, $default);
unset($this->_data[$key]);
return $value;
}
/**
* Set a variable in the session array.
*
* $session->set('foo', 'bar');
*
* @param string variable name
* @param mixed value
* @return $this
*/
public function set($key, $value)
{
$this->_data[$key] = $value;
return $this;
}
/**
* Set a variable by reference.
*
* $session->bind('foo', $foo);
*
* @param string variable name
* @param mixed referenced value
* @return $this
*/
public function bind($key, & $value)
{
$this->_data[$key] =& $value;
return $this;
}
/**
* Removes a variable in the session array.
*
* $session->delete('foo');
*
* @param string variable name
* @param ...
* @return $this
*/
public function delete($key)
{
$args = func_get_args();
foreach ($args as $key)
{
unset($this->_data[$key]);
}
return $this;
}
/**
* Loads existing session data.
*
* $session->read();
*
* @param string session id
* @return void
*/
public function read($id = NULL)
{
if (is_string($data = $this->_read($id)))
{
try
{
if ($this->_encrypted)
{
// Decrypt the data using the default key
$data = Encrypt::instance($this->_encrypted)->decode($data);
}
else
{
// Decode the base64 encoded data
$data = base64_decode($data);
}
// Unserialize the data
$data = unserialize($data);
}
catch (Exception $e)
{
// Ignore all reading errors
}
}
if (is_array($data))
{
// Load the data locally
$this->_data = $data;
}
}
/**
* Generates a new session id and returns it.
*
* $id = $session->regenerate();
*
* @return string
*/
public function regenerate()
{
return $this->_regenerate();
}
/**
* Sets the last_active timestamp and saves the session.
*
* $session->write();
*
* [!!] Any errors that occur during session writing will be logged,
* but not displayed, because sessions are written after output has
* been sent.
*
* @return boolean
* @uses Kohana::$log
*/
public function write()
{
if (headers_sent() OR $this->_destroyed)
{
// Session cannot be written when the headers are sent or when
// the session has been destroyed
return FALSE;
}
// Set the last active timestamp
$this->_data['last_active'] = time();
try
{
return $this->_write();
}
catch (Exception $e)
{
// Log & ignore all errors when a write fails
Kohana::$log->add(Log::ERROR, Kohana_Exception::text($e))->write();
return FALSE;
}
}
/**
* Completely destroy the current session.
*
* $success = $session->destroy();
*
* @return boolean
*/
public function destroy()
{
if ($this->_destroyed === FALSE)
{
if ($this->_destroyed = $this->_destroy())
{
// The session has been destroyed, clear all data
$this->_data = array();
}
}
return $this->_destroyed;
}
/**
* Loads the raw session data string and returns it.
*
* @param string session id
* @return string
*/
abstract protected function _read($id = NULL);
/**
* Generate a new session id and return it.
*
* @return string
*/
abstract protected function _regenerate();
/**
* Writes the current session.
*
* @return boolean
*/
abstract protected function _write();
/**
* Destroys the current session.
*
* @return boolean
*/
abstract protected function _destroy();
} // End Session
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/session.php | PHP | mit | 8,350 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception extends Kohana_Exception {
/**
* @var int http status code
*/
protected $_code = 0;
/**
* Creates a new translated exception.
*
* throw new Kohana_Exception('Something went terrible wrong, :user',
* array(':user' => $user));
*
* @param string status message, custom content to display with error
* @param array translation variables
* @param integer the http status code
* @return void
*/
public function __construct($message = NULL, array $variables = NULL, $code = 0)
{
if ($code == 0)
{
$code = $this->_code;
}
if ( ! isset(Response::$messages[$code]))
throw new Kohana_Exception('Unrecognized HTTP status code: :code . Only valid HTTP status codes are acceptable, see RFC 2616.', array(':code' => $code));
parent::__construct($message, $variables, $code);
}
} // End Kohana_HTTP_Exception | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception.php | PHP | mit | 967 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_503 extends HTTP_Exception {
/**
* @var integer HTTP 503 Service Unavailable
*/
protected $_code = 503;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/503.php | PHP | mit | 208 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_414 extends HTTP_Exception {
/**
* @var integer HTTP 414 Request-URI Too Long
*/
protected $_code = 414;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/414.php | PHP | mit | 209 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_411 extends HTTP_Exception {
/**
* @var integer HTTP 411 Length Required
*/
protected $_code = 411;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/411.php | PHP | mit | 204 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_416 extends HTTP_Exception {
/**
* @var integer HTTP 416 Request Range Not Satisfiable
*/
protected $_code = 416;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/416.php | PHP | mit | 218 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_504 extends HTTP_Exception {
/**
* @var integer HTTP 504 Gateway Timeout
*/
protected $_code = 504;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/504.php | PHP | mit | 204 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_417 extends HTTP_Exception {
/**
* @var integer HTTP 417 Expectation Failed
*/
protected $_code = 417;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/417.php | PHP | mit | 207 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_409 extends HTTP_Exception {
/**
* @var integer HTTP 409 Conflict
*/
protected $_code = 409;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/409.php | PHP | mit | 197 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_413 extends HTTP_Exception {
/**
* @var integer HTTP 413 Request Entity Too Large
*/
protected $_code = 413;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/413.php | PHP | mit | 213 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_400 extends HTTP_Exception {
/**
* @var integer HTTP 400 Bad Request
*/
protected $_code = 400;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/400.php | PHP | mit | 200 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_502 extends HTTP_Exception {
/**
* @var integer HTTP 502 Bad Gateway
*/
protected $_code = 502;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/502.php | PHP | mit | 200 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_410 extends HTTP_Exception {
/**
* @var integer HTTP 410 Gone
*/
protected $_code = 410;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/410.php | PHP | mit | 193 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_405 extends HTTP_Exception {
/**
* @var integer HTTP 405 Method Not Allowed
*/
protected $_code = 405;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/405.php | PHP | mit | 207 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_406 extends HTTP_Exception {
/**
* @var integer HTTP 406 Not Acceptable
*/
protected $_code = 406;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/406.php | PHP | mit | 203 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_408 extends HTTP_Exception {
/**
* @var integer HTTP 408 Request Timeout
*/
protected $_code = 408;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/408.php | PHP | mit | 204 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_404 extends HTTP_Exception {
/**
* @var integer HTTP 404 Not Found
*/
protected $_code = 404;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/404.php | PHP | mit | 198 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_500 extends HTTP_Exception {
/**
* @var integer HTTP 500 Internal Server Error
*/
protected $_code = 500;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/500.php | PHP | mit | 210 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_401 extends HTTP_Exception {
/**
* @var integer HTTP 401 Unauthorized
*/
protected $_code = 401;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/401.php | PHP | mit | 201 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_403 extends HTTP_Exception {
/**
* @var integer HTTP 403 Forbidden
*/
protected $_code = 403;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/403.php | PHP | mit | 198 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_501 extends HTTP_Exception {
/**
* @var integer HTTP 501 Not Implemented
*/
protected $_code = 501;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/501.php | PHP | mit | 204 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_412 extends HTTP_Exception {
/**
* @var integer HTTP 412 Precondition Failed
*/
protected $_code = 412;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/412.php | PHP | mit | 208 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_407 extends HTTP_Exception {
/**
* @var integer HTTP 407 Proxy Authentication Required
*/
protected $_code = 407;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/407.php | PHP | mit | 218 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_402 extends HTTP_Exception {
/**
* @var integer HTTP 402 Payment Required
*/
protected $_code = 402;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/402.php | PHP | mit | 205 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_505 extends HTTP_Exception {
/**
* @var integer HTTP 505 HTTP Version Not Supported
*/
protected $_code = 505;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/505.php | PHP | mit | 215 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_HTTP_Exception_415 extends HTTP_Exception {
/**
* @var integer HTTP 415 Unsupported Media Type
*/
protected $_code = 415;
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/exception/415.php | PHP | mit | 211 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* A HTTP Reponse specific interface that adds the methods required
* by HTTP responses. Over and above [Kohana_HTTP_Interaction], this
* interface provides status.
*
* @package Kohana
* @category HTTP
* @author Kohana Team
* @since 3.1.0
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
interface Kohana_HTTP_Response extends HTTP_Interaction {
/**
* Sets or gets the HTTP status from this response.
*
* // Set the HTTP status to 404 Not Found
* $response = Response::factory()
* ->status(404);
*
* // Get the current status
* $status = $response->status();
*
* @param integer $code Status to set to this response
* @return mixed
*/
public function status($code = NULL);
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/response.php | PHP | mit | 865 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* The HTTP Interaction interface providing the core HTTP methods that
* should be implemented by any HTTP request or response class.
*
* @package Kohana
* @category HTTP
* @author Kohana Team
* @since 3.1.0
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
interface Kohana_HTTP_Interaction {
/**
* Gets or sets the HTTP protocol. The standard protocol to use
* is `HTTP/1.1`.
*
* @param string $protocol Protocol to set to the request/response
* @return mixed
*/
public function protocol($protocol = NULL);
/**
* Gets or sets HTTP headers to the request or response. All headers
* are included immediately after the HTTP protocol definition during
* transmission. This method provides a simple array or key/value
* interface to the headers.
*
* @param mixed $key Key or array of key/value pairs to set
* @param string $value Value to set to the supplied key
* @return mixed
*/
public function headers($key = NULL, $value = NULL);
/**
* Gets or sets the HTTP body to the request or response. The body is
* included after the header, separated by a single empty new line.
*
* @param string $content Content to set to the object
* @return string
* @return void
*/
public function body($content = NULL);
/**
* Renders the HTTP_Interaction to a string, producing
*
* - Protocol
* - Headers
* - Body
*
* @return string
*/
public function render();
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/interaction.php | PHP | mit | 1,571 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* The Kohana_HTTP_Header class provides an Object-Orientated interface
* to HTTP headers. This can parse header arrays returned from the
* PHP functions `apache_request_headers()` or the `http_parse_headers()`
* function available within the PECL HTTP library.
*
* @package Kohana
* @category HTTP
* @author Kohana Team
* @since 3.1.0
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_HTTP_Header extends ArrayObject {
/**
* @var boolean Controls whether to automatically sort headers by quality value
*/
public static $sort_by_quality = FALSE;
/**
* @var array Default positive filter for sorting header values
*/
public static $default_sort_filter = array('accept','accept-charset','accept-encoding','accept-language');
/**
* Parses HTTP Header values and creating an appropriate object
* depending on type; i.e. accept-type, accept-char, cache-control etc.
*
* $header_values_array = HTTP_Header::parse_header_values(array('cache-control' => 'max-age=200; public'));
*
* @param array $header_values Values to parse
* @param array $header_commas_allowed Header values where commas are not delimiters (usually date)
* @return array
*/
public static function parse_header_values(array $header_values, array $header_commas_allowed = array('user-agent', 'date', 'expires'))
{
/**
* @see http://www.w3.org/Protocols/rfc2616/rfc2616.html
*
* HTTP header declarations should be treated as case-insensitive
*/
$header_values = array_change_key_case($header_values, CASE_LOWER);
// Foreach of the header values applied
foreach ($header_values as $key => $value)
{
if (is_array($value))
{
$values = array();
if (Arr::is_assoc($value))
{
foreach ($value as $k => $v)
{
$values[] = HTTP_Header::parse_header_values($v);
}
}
else
{
// RFC 2616 allows multiple headers with same name if they can be
// concatinated using commas without altering the original message.
// This usually occurs with multiple Set-Cookie: headers
$array = array();
foreach ($value as $k => $v)
{
// Break value into component parts
$v = explode(';', $v);
// Do some nasty parsing to flattern the array into components,
// parsing key values
$array = Arr::flatten(array_map('HTTP_Header_Value::parse_key_value', $v));
// Get the K/V component and extract the first element
$key_value_component = array_slice($array, 0, 1, TRUE);
array_shift($array);
// Create the HTTP_Header_Value component array
$http_header['key'] = key($key_value_component);
$http_header['value'] = current($key_value_component);
$http_header['properties'] = $array;
// Create the HTTP_Header_Value
$values[] = new HTTP_Header_Value($http_header);
}
}
// Assign HTTP_Header_Value array to the header
$header_values[$key] = $values;
continue;
}
// If the key allows commas or no commas are found
if (in_array($key, $header_commas_allowed) or (strpos($value, ',') === FALSE))
{
// If the key is user-agent, we don't want to parse the string
if ($key === 'user-agent')
{
$header_values[$key] = new HTTP_Header_Value($value, TRUE);
}
// Else, behave normally
else
{
$header_values[$key] = new HTTP_Header_Value($value);
}
// Move to next header
continue;
}
// Create an array of the values and clear any whitespace
$value = array_map('trim', explode(',', $value));
$parsed_values = array();
// Foreach value
foreach ($value as $v)
{
$v = new HTTP_Header_Value($v);
// Convert the value string into an object
if ($v->key === NULL)
{
$parsed_values[] = $v;
}
else
{
$parsed_values[$v->key] = $v;
}
}
// Apply parsed value to the header
$header_values[$key] = $parsed_values;
}
// Return the parsed header values
return $header_values;
}
/**
* Constructor method for [Kohana_HTTP_Header]. Uses the standard constructor
* of the parent `ArrayObject` class.
*
* $header_object = new HTTP_Header(array('x-powered-by' => 'Kohana 3.1.x', 'expires' => '...'));
*
* @param mixed Input array
* @param int Flags
* @param string The iterator class to use
*/
public function __construct($input, $flags = NULL, $iterator_class = 'ArrayIterator')
{
// Parse the values into [HTTP_Header_Values]
parent::__construct(HTTP_Header::parse_header_values($input), $flags, $iterator_class);
// If sort by quality is set, sort the fields by q=0.0 value
if (HTTP_Header::$sort_by_quality)
{
$this->sort_values_by_quality();
}
}
/**
* Returns the header object as a string, including
* the terminating new line
*
* // Return the header as a string
* echo (string) $request->headers();
*
* @return string
*/
public function __toString()
{
$header = '';
foreach ($this as $key => $value)
{
if (is_array($value))
{
$header .= $key.': '.(implode(', ', $value))."\r\n";
}
else
{
$header .= $key.': '.$value."\r\n";
}
}
return $header."\n";
}
/**
* Overloads the `ArrayObject::exchangeArray()` method to ensure all
* values passed are parsed correctly into a [Kohana_HTTP_Header_Value].
*
* // Input new headers
* $headers->exchangeArray(array(
* 'date' => 'Wed, 24 Nov 2010 21:09:23 GMT',
* 'cache-control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
* ));
*
* @param array $array Array to exchange
* @return array
*/
public function exchangeArray($array)
{
return parent::exchangeArray(HTTP_Header::parse_header_values($array));
}
/**
* Overloads the `ArrayObject::offsetSet` method to ensure any
* access is correctly converted to the correct object type.
*
* // Add a new header from encoded string
* $headers['cache-control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0'
*
* @param mixed $index Key
* @param mixed $newval Value
* @return void
*/
public function offsetSet($index, $newval)
{
if (is_array($newval) AND (current($newval) instanceof HTTP_Header_Value))
return parent::offsetSet(strtolower($index), $newval);
elseif ( ! $newval instanceof HTTP_Header_Value)
{
$newval = new HTTP_Header_Value($newval);
}
parent::offsetSet(strtolower($index), $newval);
}
/**
* Sort the headers by quality property if the header matches the
* [Kohana_HTTP_Header::$default_sort_filter] definition.
*
* #### Default sort values
*
* - Accept
* - Accept-Chars
* - Accept-Encoding
* - Accept-Lang
*
* @param array $filter Header fields to parse
* @return self
*/
public function sort_values_by_quality(array $filter = array())
{
// If a filter argument is supplied
if ($filter)
{
// Apply filter and store previous
$previous_filter = HTTP_Header::$default_sort_filter;
HTTP_Header::$default_sort_filter = $filter;
}
// Get a copy of this ArrayObject
$values = $this->getArrayCopy();
foreach ($values as $key => $value)
{
if ( ! is_array($value) or ! in_array($key, HTTP_Header::$default_sort_filter))
{
unset($values[$key]);
continue;
}
// Sort them by comparison
uasort($value, array($this, '_sort_by_comparison'));
$values[$key] = $value;
}
// Return filter to previous state if required
if ($filter)
{
HTTP_Header::$default_sort_filter = $previous_filter;
}
foreach ($values as $key => $value)
{
$this[$key] = $value;
}
// Return this
return $this;
}
protected function _sort_by_comparison($value_a, $value_b)
{
// Test for correct instance type
if ( ! $value_a instanceof HTTP_Header_Value OR ! $value_b instanceof HTTP_Header_Value)
{
// Return neutral if cannot test value
return 0;
}
// Extract the qualities
$a = (float) Arr::get($value_a->properties, 'q', HTTP_Header_Value::$default_quality);
$b = (float) Arr::get($value_b->properties, 'q', HTTP_Header_Value::$default_quality);
if ($a == $b)
return 0;
elseif ($a < $b)
return 1;
elseif ($a > $b)
return -1;
}
} // End Kohana_HTTP_Header | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/header.php | PHP | mit | 8,465 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* A HTTP Request specific interface that adds the methods required
* by HTTP requests. Over and above [Kohana_HTTP_Interaction], this
* interface provides method, uri, get and post methods.
*
* @package Kohana
* @category HTTP
* @author Kohana Team
* @since 3.1.0
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
interface Kohana_HTTP_Request extends HTTP_Interaction {
// HTTP Methods
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
const HEAD = 'HEAD';
const OPTIONS = 'OPTIONS';
const TRACE = 'TRACE';
const CONNECT = 'CONNECT';
/**
* Gets or sets the HTTP method. Usually GET, POST, PUT or DELETE in
* traditional CRUD applications.
*
* @param string $method Method to use for this request
* @return mixed
*/
public function method($method = NULL);
/**
* Gets the URI of this request, optionally allows setting
* of [Route] specific parameters during the URI generation.
* If no parameters are passed, the request will use the
* default values defined in the Route.
*
* @param array $params Optional parameters to include in uri generation
* @return string
*/
public function uri(array $params = array());
/**
* Gets or sets HTTP query string.
*
* @param mixed $key Key or key value pairs to set
* @param string $value Value to set to a key
* @return mixed
*/
public function query($key = NULL, $value = NULL);
/**
* Gets or sets HTTP POST parameters to the request.
*
* @param mixed $key Key or key value pairs to set
* @param string $value Value to set to a key
* @return mixed
*/
public function post($key = NULL, $value = NULL);
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/request.php | PHP | mit | 1,844 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Kohana_HTTP_Header_Value represents a value assigned to an HTTP header, i.e.
*
* Accept: [key=]value[; property[=property_value][; ...]]
*
* Values are either single values,
*
* @package Kohana
* @category HTTP
* @author Kohana Team
* @since 3.1.0
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_HTTP_Header_Value {
/**
* @var float The default quality header property value
*/
public static $default_quality = 1.0;
/**
* Detects and returns key/value pairs
*
* @param string $string String to parse
* @param string $separator
* @return array
*/
public static function parse_key_value($string, $separator = '=')
{
$parts = explode($separator, trim($string), 2);
if (count($parts) == 1)
{
return $parts;
}
else
{
return array($parts[0] => $parts[1]);
}
}
/**
* @var array
*/
public $properties = array();
/**
* @var void|string
*/
public $key;
/**
* @var array
*/
public $value = array();
/**
* Builds the header field
*
* @param mixed value configuration array passed
* @param boolean no_parse skip parsing of the string (i.e. user-agent)
* @throws Kohana_HTTP_Exception
*/
public function __construct($value, $no_parse = FALSE)
{
// If no parse is set, set the value and get out of here (user-agent)
if ($no_parse)
{
$this->key = NULL;
$this->value = $value;
return;
}
// If configuration array passed
if (is_array($value))
{
// Parse each value
foreach ($value as $k => $v)
{
// If the key is a property
if (property_exists($this, $k))
{
// Map values
$this->$k = $v;
}
}
}
// If value is a string
elseif (is_string($value))
{
// Detect properties
if (strpos($value, ';') !== FALSE)
{
// Remove properties from the string
$parts = explode(';', $value);
$value = array_shift($parts);
// Parse the properties
$properties = array();
// Foreach part
foreach ($parts as $part)
{
// Merge the parsed values
$properties = array_merge(HTTP_Header_Value::parse_key_value($part), $properties);
}
// Apply the parsed values
$this->properties = $properties;
}
// Parse the value and get key
$value = HTTP_Header_Value::parse_key_value($value);
$key = key($value);
// If the key is a string
if (is_string($key))
{
// Apply the key as a property
$this->key = $key;
}
// Apply the value
$this->value = current($value);
}
// Unrecognised value type
else
{
throw new HTTP_Exception_500(__METHOD__.' unknown header value type: :type. array or string allowed.', array(':type' => gettype($value)));
}
}
/**
* Provides direct access to the key of this header value
*
* @param string $key Key value to set
* @return mixed
*/
public function key($key = NULL)
{
if ($key === NULL)
{
return $this->key;
}
else
{
$this->key = $key;
return $this;
}
}
/**
* Provides direct access to the value of this header value
*
* @param string $value Value to set
* @return mixed
*/
public function value($value = NULL)
{
if ($value === NULL)
{
return $this->value;
}
else
{
$this->value = $value;
return $this;
}
}
/**
* Provides direct access to the properties of this header value
*
* @param array $properties Properties to set to this value
* @return mixed
*/
public function properties(array $properties = array())
{
if ( ! $properties)
{
return $this->properties;
}
else
{
$this->properties = $properties;
return $this;
}
}
/**
* Magic method to handle object being cast to
* string. Produces the following header value syntax
*
* [key=]value[; property[=property_value][; ... ]]
*
* @return string
*/
public function __toString()
{
$string = ($this->key !== NULL) ? ($this->key.'='.$this->value) : $this->value;
if ($this->properties)
{
$props = array($string);
foreach ($this->properties as $k => $v)
{
$props[] = is_int($k) ? $v : ($k.'='.$v);
}
$string = implode('; ', $props);
}
return $string;
}
} // End Kohana_HTTP_Header_Value | 12-05-2011-wharfland-project | trunk/system/classes/kohana/http/header/value.php | PHP | mit | 4,350 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Validation rules.
*
* @package Kohana
* @category Security
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Valid {
/**
* Checks if a field is not empty.
*
* @return boolean
*/
public static function not_empty($value)
{
if (is_object($value) AND $value instanceof ArrayObject)
{
// Get the array from the ArrayObject
$value = $value->getArrayCopy();
}
// Value cannot be NULL, FALSE, '', or an empty array
return ! in_array($value, array(NULL, FALSE, '', array()), TRUE);
}
/**
* Checks a field against a regular expression.
*
* @param string value
* @param string regular expression to match (including delimiters)
* @return boolean
*/
public static function regex($value, $expression)
{
return (bool) preg_match($expression, (string) $value);
}
/**
* Checks that a field is long enough.
*
* @param string value
* @param integer minimum length required
* @return boolean
*/
public static function min_length($value, $length)
{
return UTF8::strlen($value) >= $length;
}
/**
* Checks that a field is short enough.
*
* @param string value
* @param integer maximum length required
* @return boolean
*/
public static function max_length($value, $length)
{
return UTF8::strlen($value) <= $length;
}
/**
* Checks that a field is exactly the right length.
*
* @param string value
* @param integer exact length required
* @return boolean
*/
public static function exact_length($value, $length)
{
return UTF8::strlen($value) === $length;
}
/**
* Checks that a field is exactly the value required.
*
* @param string value
* @param string required value
* @return boolean
*/
public static function equals($value, $required)
{
return ($value === $required);
}
/**
* Check an email address for correct format.
*
* @link http://www.iamcal.com/publish/articles/php/parsing_email/
* @link http://www.w3.org/Protocols/rfc822/
*
* @param string email address
* @param boolean strict RFC compatibility
* @return boolean
*/
public static function email($email, $strict = FALSE)
{
if ($strict === TRUE)
{
$qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
$dtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
$atom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
$pair = '\\x5c[\\x00-\\x7f]';
$domain_literal = "\\x5b($dtext|$pair)*\\x5d";
$quoted_string = "\\x22($qtext|$pair)*\\x22";
$sub_domain = "($atom|$domain_literal)";
$word = "($atom|$quoted_string)";
$domain = "$sub_domain(\\x2e$sub_domain)*";
$local_part = "$word(\\x2e$word)*";
$expression = "/^$local_part\\x40$domain$/D";
}
else
{
$expression = '/^[-_a-z0-9\'+*$^&%=~!?{}]++(?:\.[-_a-z0-9\'+*$^&%=~!?{}]+)*+@(?:(?![-.])[-a-z0-9.]+(?<![-.])\.[a-z]{2,6}|\d{1,3}(?:\.\d{1,3}){3})(?::\d++)?$/iD';
}
return (bool) preg_match($expression, (string) $email);
}
/**
* Validate the domain of an email address by checking if the domain has a
* valid MX record.
*
* @link http://php.net/checkdnsrr not added to Windows until PHP 5.3.0
*
* @param string email address
* @return boolean
*/
public static function email_domain($email)
{
// Check if the email domain has a valid MX record
return (bool) checkdnsrr(preg_replace('/^[^@]++@/', '', $email), 'MX');
}
/**
* Validate a URL.
*
* @param string URL
* @return boolean
*/
public static function url($url)
{
// Based on http://www.apps.ietf.org/rfc/rfc1738.html#sec-5
if ( ! preg_match(
'~^
# scheme
[-a-z0-9+.]++://
# username:password (optional)
(?:
[-a-z0-9$_.+!*\'(),;?&=%]++ # username
(?::[-a-z0-9$_.+!*\'(),;?&=%]++)? # password (optional)
@
)?
(?:
# ip address
\d{1,3}+(?:\.\d{1,3}+){3}+
| # or
# hostname (captured)
(
(?!-)[-a-z0-9]{1,63}+(?<!-)
(?:\.(?!-)[-a-z0-9]{1,63}+(?<!-)){0,126}+
)
)
# port (optional)
(?::\d{1,5}+)?
# path (optional)
(?:/.*)?
$~iDx', $url, $matches))
return FALSE;
// We matched an IP address
if ( ! isset($matches[1]))
return TRUE;
// Check maximum length of the whole hostname
// http://en.wikipedia.org/wiki/Domain_name#cite_note-0
if (strlen($matches[1]) > 253)
return FALSE;
// An extra check for the top level domain
// It must start with a letter
$tld = ltrim(substr($matches[1], (int) strrpos($matches[1], '.')), '.');
return ctype_alpha($tld[0]);
}
/**
* Validate an IP.
*
* @param string IP address
* @param boolean allow private IP networks
* @return boolean
*/
public static function ip($ip, $allow_private = TRUE)
{
// Do not allow reserved addresses
$flags = FILTER_FLAG_NO_RES_RANGE;
if ($allow_private === FALSE)
{
// Do not allow private or reserved addresses
$flags = $flags | FILTER_FLAG_NO_PRIV_RANGE;
}
return (bool) filter_var($ip, FILTER_VALIDATE_IP, $flags);
}
/**
* Validates a credit card number, with a Luhn check if possible.
*
* @param integer credit card number
* @param string|array card type, or an array of card types
* @return boolean
* @uses Validate::luhn
*/
public static function credit_card($number, $type = NULL)
{
// Remove all non-digit characters from the number
if (($number = preg_replace('/\D+/', '', $number)) === '')
return FALSE;
if ($type == NULL)
{
// Use the default type
$type = 'default';
}
elseif (is_array($type))
{
foreach ($type as $t)
{
// Test each type for validity
if (Valid::credit_card($number, $t))
return TRUE;
}
return FALSE;
}
$cards = Kohana::config('credit_cards');
// Check card type
$type = strtolower($type);
if ( ! isset($cards[$type]))
return FALSE;
// Check card number length
$length = strlen($number);
// Validate the card length by the card type
if ( ! in_array($length, preg_split('/\D+/', $cards[$type]['length'])))
return FALSE;
// Check card number prefix
if ( ! preg_match('/^'.$cards[$type]['prefix'].'/', $number))
return FALSE;
// No Luhn check required
if ($cards[$type]['luhn'] == FALSE)
return TRUE;
return Valid::luhn($number);
}
/**
* Validate a number against the [Luhn](http://en.wikipedia.org/wiki/Luhn_algorithm)
* (mod10) formula.
*
* @param string number to check
* @return boolean
*/
public static function luhn($number)
{
// Force the value to be a string as this method uses string functions.
// Converting to an integer may pass PHP_INT_MAX and result in an error!
$number = (string) $number;
if ( ! ctype_digit($number))
{
// Luhn can only be used on numbers!
return FALSE;
}
// Check number length
$length = strlen($number);
// Checksum of the card number
$checksum = 0;
for ($i = $length - 1; $i >= 0; $i -= 2)
{
// Add up every 2nd digit, starting from the right
$checksum += substr($number, $i, 1);
}
for ($i = $length - 2; $i >= 0; $i -= 2)
{
// Add up every 2nd digit doubled, starting from the right
$double = substr($number, $i, 1) * 2;
// Subtract 9 from the double where value is greater than 10
$checksum += ($double >= 10) ? ($double - 9) : $double;
}
// If the checksum is a multiple of 10, the number is valid
return ($checksum % 10 === 0);
}
/**
* Checks if a phone number is valid.
*
* @param string phone number to check
* @return boolean
*/
public static function phone($number, $lengths = NULL)
{
if ( ! is_array($lengths))
{
$lengths = array(7,10,11);
}
// Remove all non-digit characters from the number
$number = preg_replace('/\D+/', '', $number);
// Check if the number is within range
return in_array(strlen($number), $lengths);
}
/**
* Tests if a string is a valid date string.
*
* @param string date to check
* @return boolean
*/
public static function date($str)
{
return (strtotime($str) !== FALSE);
}
/**
* Checks whether a string consists of alphabetical characters only.
*
* @param string input string
* @param boolean trigger UTF-8 compatibility
* @return boolean
*/
public static function alpha($str, $utf8 = FALSE)
{
$str = (string) $str;
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pL++$/uD', $str);
}
else
{
return ctype_alpha($str);
}
}
/**
* Checks whether a string consists of alphabetical characters and numbers only.
*
* @param string input string
* @param boolean trigger UTF-8 compatibility
* @return boolean
*/
public static function alpha_numeric($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^[\pL\pN]++$/uD', $str);
}
else
{
return ctype_alnum($str);
}
}
/**
* Checks whether a string consists of alphabetical characters, numbers, underscores and dashes only.
*
* @param string input string
* @param boolean trigger UTF-8 compatibility
* @return boolean
*/
public static function alpha_dash($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
$regex = '/^[-\pL\pN_]++$/uD';
}
else
{
$regex = '/^[-a-z0-9_]++$/iD';
}
return (bool) preg_match($regex, $str);
}
/**
* Checks whether a string consists of digits only (no dots or dashes).
*
* @param string input string
* @param boolean trigger UTF-8 compatibility
* @return boolean
*/
public static function digit($str, $utf8 = FALSE)
{
if ($utf8 === TRUE)
{
return (bool) preg_match('/^\pN++$/uD', $str);
}
else
{
return (is_int($str) AND $str >= 0) OR ctype_digit($str);
}
}
/**
* Checks whether a string is a valid number (negative and decimal numbers allowed).
*
* Uses {@link http://www.php.net/manual/en/function.localeconv.php locale conversion}
* to allow decimal point to be locale specific.
*
* @param string input string
* @return boolean
*/
public static function numeric($str)
{
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
// A lookahead is used to make sure the string contains at least one digit (before or after the decimal point)
return (bool) preg_match('/^-?+(?=.*[0-9])[0-9]*+'.preg_quote($decimal).'?+[0-9]*+$/D', (string) $str);
}
/**
* Tests if a number is within a range.
*
* @param string number to check
* @param integer minimum value
* @param integer maximum value
* @return boolean
*/
public static function range($number, $min, $max)
{
return ($number >= $min AND $number <= $max);
}
/**
* Checks if a string is a proper decimal format. Optionally, a specific
* number of digits can be checked too.
*
* @param string number to check
* @param integer number of decimal places
* @param integer number of digits
* @return boolean
*/
public static function decimal($str, $places = 2, $digits = NULL)
{
if ($digits > 0)
{
// Specific number of digits
$digits = '{'.( (int) $digits).'}';
}
else
{
// Any number of digits
$digits = '+';
}
// Get the decimal point for the current locale
list($decimal) = array_values(localeconv());
return (bool) preg_match('/^[0-9]'.$digits.preg_quote($decimal).'[0-9]{'.( (int) $places).'}$/D', $str);
}
/**
* Checks if a string is a proper hexadecimal HTML color value. The validation
* is quite flexible as it does not require an initial "#" and also allows for
* the short notation using only three instead of six hexadecimal characters.
*
* @param string input string
* @return boolean
*/
public static function color($str)
{
return (bool) preg_match('/^#?+[0-9a-f]{3}(?:[0-9a-f]{3})?$/iD', $str);
}
/**
* Checks if a field matches the value of another field.
*
* @param array array of values
* @param string field name
* @param string field name to match
* @return boolean
*/
public static function matches($array, $field, $match)
{
return ($array[$field] === $array[$match]);
}
} // End Valid
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/valid.php | PHP | mit | 12,302 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* RSS and Atom feed helper.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Feed {
/**
* Parses a remote feed into an array.
*
* @param string remote feed URL
* @param integer item limit to fetch
* @return array
*/
public static function parse($feed, $limit = 0)
{
// Check if SimpleXML is installed
if ( ! function_exists('simplexml_load_file'))
throw new Kohana_Exception('SimpleXML must be installed!');
// Make limit an integer
$limit = (int) $limit;
// Disable error reporting while opening the feed
$error_level = error_reporting(0);
// Allow loading by filename or raw XML string
$load = (is_file($feed) OR Valid::url($feed)) ? 'simplexml_load_file' : 'simplexml_load_string';
// Load the feed
$feed = $load($feed, 'SimpleXMLElement', LIBXML_NOCDATA);
// Restore error reporting
error_reporting($error_level);
// Feed could not be loaded
if ($feed === FALSE)
return array();
$namespaces = $feed->getNamespaces(true);
// Detect the feed type. RSS 1.0/2.0 and Atom 1.0 are supported.
$feed = isset($feed->channel) ? $feed->xpath('//item') : $feed->entry;
$i = 0;
$items = array();
foreach ($feed as $item)
{
if ($limit > 0 AND $i++ === $limit)
break;
$item_fields = (array) $item;
// get namespaced tags
foreach ($namespaces as $ns)
{
$item_fields += (array) $item->children($ns);
}
$items[] = $item_fields;
}
return $items;
}
/**
* Creates a feed from the given parameters.
*
* @param array feed information
* @param array items to add to the feed
* @param string define which format to use (only rss2 is supported)
* @param string define which encoding to use
* @return string
*/
public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8')
{
$info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP');
$feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"><channel></channel></rss>';
$feed = simplexml_load_string($feed);
foreach ($info as $name => $value)
{
if ($name === 'image')
{
// Create an image element
$image = $feed->channel->addChild('image');
if ( ! isset($value['link'], $value['url'], $value['title']))
{
throw new Kohana_Exception('Feed images require a link, url, and title');
}
if (strpos($value['link'], '://') === FALSE)
{
// Convert URIs to URLs
$value['link'] = URL::site($value['link'], 'http');
}
if (strpos($value['url'], '://') === FALSE)
{
// Convert URIs to URLs
$value['url'] = URL::site($value['url'], 'http');
}
// Create the image elements
$image->addChild('link', $value['link']);
$image->addChild('url', $value['url']);
$image->addChild('title', $value['title']);
}
else
{
if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value)))
{
// Convert timestamps to RFC 822 formatted dates
$value = date('r', $value);
}
elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE)
{
// Convert URIs to URLs
$value = URL::site($value, 'http');
}
// Add the info to the channel
$feed->channel->addChild($name, $value);
}
}
foreach ($items as $item)
{
// Add the item to the channel
$row = $feed->channel->addChild('item');
foreach ($item as $name => $value)
{
if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value)))
{
// Convert timestamps to RFC 822 formatted dates
$value = date('r', $value);
}
elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE)
{
// Convert URIs to URLs
$value = URL::site($value, 'http');
}
// Add the info to the row
$row->addChild($name, $value);
}
}
if (function_exists('dom_import_simplexml'))
{
// Convert the feed object to a DOM object
$feed = dom_import_simplexml($feed)->ownerDocument;
// DOM generates more readable XML
$feed->formatOutput = TRUE;
// Export the document as XML
$feed = $feed->saveXML();
}
else
{
// Export the document as XML
$feed = $feed->asXML();
}
return $feed;
}
} // End Feed
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/feed.php | PHP | mit | 4,490 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* A port of [phputf8](http://phputf8.sourceforge.net/) to a unified set
* of files. Provides multi-byte aware replacement string functions.
*
* For UTF-8 support to work correctly, the following requirements must be met:
*
* - PCRE needs to be compiled with UTF-8 support (--enable-utf8)
* - Support for [Unicode properties](http://php.net/manual/reference.pcre.pattern.modifiers.php)
* is highly recommended (--enable-unicode-properties)
* - UTF-8 conversion will be much more reliable if the
* [iconv extension](http://php.net/iconv) is loaded
* - The [mbstring extension](http://php.net/mbstring) is highly recommended,
* but must not be overloading string functions
*
* [!!] This file is licensed differently from the rest of Kohana. As a port of
* [phputf8](http://phputf8.sourceforge.net/), this file is released under the LGPL.
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @copyright (c) 2005 Harry Fuecks
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Kohana_UTF8 {
/**
* @var boolean Does the server support UTF-8 natively?
*/
public static $server_utf8 = NULL;
/**
* @var array List of called methods that have had their required file included.
*/
public static $called = array();
/**
* Recursively cleans arrays, objects, and strings. Removes ASCII control
* codes and converts to the requested charset while silently discarding
* incompatible characters.
*
* UTF8::clean($_GET); // Clean GET data
*
* [!!] This method requires [Iconv](http://php.net/iconv)
*
* @param mixed variable to clean
* @param string character set, defaults to Kohana::$charset
* @return mixed
* @uses UTF8::strip_ascii_ctrl
* @uses UTF8::is_ascii
*/
public static function clean($var, $charset = NULL)
{
if ( ! $charset)
{
// Use the application character set
$charset = Kohana::$charset;
}
if (is_array($var) OR is_object($var))
{
foreach ($var as $key => $val)
{
// Recursion!
$var[self::clean($key)] = self::clean($val);
}
}
elseif (is_string($var) AND $var !== '')
{
// Remove control characters
$var = self::strip_ascii_ctrl($var);
if ( ! self::is_ascii($var))
{
// Disable notices
$error_reporting = error_reporting(~E_NOTICE);
// iconv is expensive, so it is only used when needed
$var = iconv($charset, $charset.'//IGNORE', $var);
// Turn notices back on
error_reporting($error_reporting);
}
}
return $var;
}
/**
* Tests whether a string contains only 7-bit ASCII bytes. This is used to
* determine when to use native functions or UTF-8 functions.
*
* $ascii = UTF8::is_ascii($str);
*
* @param mixed string or array of strings to check
* @return boolean
*/
public static function is_ascii($str)
{
if (is_array($str))
{
$str = implode($str);
}
return ! preg_match('/[^\x00-\x7F]/S', $str);
}
/**
* Strips out device control codes in the ASCII range.
*
* $str = UTF8::strip_ascii_ctrl($str);
*
* @param string string to clean
* @return string
*/
public static function strip_ascii_ctrl($str)
{
return preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $str);
}
/**
* Strips out all non-7bit ASCII bytes.
*
* $str = UTF8::strip_non_ascii($str);
*
* @param string string to clean
* @return string
*/
public static function strip_non_ascii($str)
{
return preg_replace('/[^\x00-\x7F]+/S', '', $str);
}
/**
* Replaces special/accented UTF-8 characters by ASCII-7 "equivalents".
*
* $ascii = UTF8::transliterate_to_ascii($utf8);
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param string string to transliterate
* @param integer -1 lowercase only, +1 uppercase only, 0 both cases
* @return string
*/
public static function transliterate_to_ascii($str, $case = 0)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _transliterate_to_ascii($str, $case);
}
/**
* Returns the length of the given string. This is a UTF8-aware version
* of [strlen](http://php.net/strlen).
*
* $length = UTF8::strlen($str);
*
* @param string string being measured for length
* @return integer
* @uses UTF8::$server_utf8
*/
public static function strlen($str)
{
if (UTF8::$server_utf8)
return mb_strlen($str, Kohana::$charset);
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strlen($str);
}
/**
* Finds position of first occurrence of a UTF-8 string. This is a
* UTF8-aware version of [strpos](http://php.net/strpos).
*
* $position = UTF8::strpos($str, $search);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string haystack
* @param string needle
* @param integer offset from which character in haystack to start searching
* @return integer position of needle
* @return boolean FALSE if the needle is not found
* @uses UTF8::$server_utf8
*/
public static function strpos($str, $search, $offset = 0)
{
if (UTF8::$server_utf8)
return mb_strpos($str, $search, $offset, Kohana::$charset);
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strpos($str, $search, $offset);
}
/**
* Finds position of last occurrence of a char in a UTF-8 string. This is
* a UTF8-aware version of [strrpos](http://php.net/strrpos).
*
* $position = UTF8::strrpos($str, $search);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string haystack
* @param string needle
* @param integer offset from which character in haystack to start searching
* @return integer position of needle
* @return boolean FALSE if the needle is not found
* @uses UTF8::$server_utf8
*/
public static function strrpos($str, $search, $offset = 0)
{
if (UTF8::$server_utf8)
return mb_strrpos($str, $search, $offset, Kohana::$charset);
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strrpos($str, $search, $offset);
}
/**
* Returns part of a UTF-8 string. This is a UTF8-aware version
* of [substr](http://php.net/substr).
*
* $sub = UTF8::substr($str, $offset);
*
* @author Chris Smith <chris@jalakai.co.uk>
* @param string input string
* @param integer offset
* @param integer length limit
* @return string
* @uses UTF8::$server_utf8
* @uses Kohana::$charset
*/
public static function substr($str, $offset, $length = NULL)
{
if (UTF8::$server_utf8)
return ($length === NULL)
? mb_substr($str, $offset, mb_strlen($str), Kohana::$charset)
: mb_substr($str, $offset, $length, Kohana::$charset);
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _substr($str, $offset, $length);
}
/**
* Replaces text within a portion of a UTF-8 string. This is a UTF8-aware
* version of [substr_replace](http://php.net/substr_replace).
*
* $str = UTF8::substr_replace($str, $replacement, $offset);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string input string
* @param string replacement string
* @param integer offset
* @return string
*/
public static function substr_replace($str, $replacement, $offset, $length = NULL)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _substr_replace($str, $replacement, $offset, $length);
}
/**
* Makes a UTF-8 string lowercase. This is a UTF8-aware version
* of [strtolower](http://php.net/strtolower).
*
* $str = UTF8::strtolower($str);
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param string mixed case string
* @return string
* @uses UTF8::$server_utf8
*/
public static function strtolower($str)
{
if (UTF8::$server_utf8)
return mb_strtolower($str, Kohana::$charset);
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strtolower($str);
}
/**
* Makes a UTF-8 string uppercase. This is a UTF8-aware version
* of [strtoupper](http://php.net/strtoupper).
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param string mixed case string
* @return string
* @uses UTF8::$server_utf8
* @uses Kohana::$charset
*/
public static function strtoupper($str)
{
if (UTF8::$server_utf8)
return mb_strtoupper($str, Kohana::$charset);
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strtoupper($str);
}
/**
* Makes a UTF-8 string's first character uppercase. This is a UTF8-aware
* version of [ucfirst](http://php.net/ucfirst).
*
* $str = UTF8::ucfirst($str);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string mixed case string
* @return string
*/
public static function ucfirst($str)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _ucfirst($str);
}
/**
* Makes the first character of every word in a UTF-8 string uppercase.
* This is a UTF8-aware version of [ucwords](http://php.net/ucwords).
*
* $str = UTF8::ucwords($str);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string mixed case string
* @return string
* @uses UTF8::$server_utf8
*/
public static function ucwords($str)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _ucwords($str);
}
/**
* Case-insensitive UTF-8 string comparison. This is a UTF8-aware version
* of [strcasecmp](http://php.net/strcasecmp).
*
* $compare = UTF8::strcasecmp($str1, $str2);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string string to compare
* @param string string to compare
* @return integer less than 0 if str1 is less than str2
* @return integer greater than 0 if str1 is greater than str2
* @return integer 0 if they are equal
*/
public static function strcasecmp($str1, $str2)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strcasecmp($str1, $str2);
}
/**
* Returns a string or an array with all occurrences of search in subject
* (ignoring case) and replaced with the given replace value. This is a
* UTF8-aware version of [str_ireplace](http://php.net/str_ireplace).
*
* [!!] This function is very slow compared to the native version. Avoid
* using it when possible.
*
* @author Harry Fuecks <hfuecks@gmail.com
* @param string|array text to replace
* @param string|array replacement text
* @param string|array subject text
* @param integer number of matched and replaced needles will be returned via this parameter which is passed by reference
* @return string if the input was a string
* @return array if the input was an array
*/
public static function str_ireplace($search, $replace, $str, & $count = NULL)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _str_ireplace($search, $replace, $str, $count);
}
/**
* Case-insenstive UTF-8 version of strstr. Returns all of input string
* from the first occurrence of needle to the end. This is a UTF8-aware
* version of [stristr](http://php.net/stristr).
*
* $found = UTF8::stristr($str, $search);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string input string
* @param string needle
* @return string matched substring if found
* @return FALSE if the substring was not found
*/
public static function stristr($str, $search)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _stristr($str, $search);
}
/**
* Finds the length of the initial segment matching mask. This is a
* UTF8-aware version of [strspn](http://php.net/strspn).
*
* $found = UTF8::strspn($str, $mask);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string input string
* @param string mask for search
* @param integer start position of the string to examine
* @param integer length of the string to examine
* @return integer length of the initial segment that contains characters in the mask
*/
public static function strspn($str, $mask, $offset = NULL, $length = NULL)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strspn($str, $mask, $offset, $length);
}
/**
* Finds the length of the initial segment not matching mask. This is a
* UTF8-aware version of [strcspn](http://php.net/strcspn).
*
* $found = UTF8::strcspn($str, $mask);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string input string
* @param string mask for search
* @param integer start position of the string to examine
* @param integer length of the string to examine
* @return integer length of the initial segment that contains characters not in the mask
*/
public static function strcspn($str, $mask, $offset = NULL, $length = NULL)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strcspn($str, $mask, $offset, $length);
}
/**
* Pads a UTF-8 string to a certain length with another string. This is a
* UTF8-aware version of [str_pad](http://php.net/str_pad).
*
* $str = UTF8::str_pad($str, $length);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string input string
* @param integer desired string length after padding
* @param string string to use as padding
* @param string padding type: STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH
* @return string
*/
public static function str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _str_pad($str, $final_str_length, $pad_str, $pad_type);
}
/**
* Converts a UTF-8 string to an array. This is a UTF8-aware version of
* [str_split](http://php.net/str_split).
*
* $array = UTF8::str_split($str);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string input string
* @param integer maximum length of each chunk
* @return array
*/
public static function str_split($str, $split_length = 1)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _str_split($str, $split_length);
}
/**
* Reverses a UTF-8 string. This is a UTF8-aware version of [strrev](http://php.net/strrev).
*
* $str = UTF8::strrev($str);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string string to be reversed
* @return string
*/
public static function strrev($str)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _strrev($str);
}
/**
* Strips whitespace (or other UTF-8 characters) from the beginning and
* end of a string. This is a UTF8-aware version of [trim](http://php.net/trim).
*
* $str = UTF8::trim($str);
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param string input string
* @param string string of characters to remove
* @return string
*/
public static function trim($str, $charlist = NULL)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _trim($str, $charlist);
}
/**
* Strips whitespace (or other UTF-8 characters) from the beginning of
* a string. This is a UTF8-aware version of [ltrim](http://php.net/ltrim).
*
* $str = UTF8::ltrim($str);
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param string input string
* @param string string of characters to remove
* @return string
*/
public static function ltrim($str, $charlist = NULL)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _ltrim($str, $charlist);
}
/**
* Strips whitespace (or other UTF-8 characters) from the end of a string.
* This is a UTF8-aware version of [rtrim](http://php.net/rtrim).
*
* $str = UTF8::rtrim($str);
*
* @author Andreas Gohr <andi@splitbrain.org>
* @param string input string
* @param string string of characters to remove
* @return string
*/
public static function rtrim($str, $charlist = NULL)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _rtrim($str, $charlist);
}
/**
* Returns the unicode ordinal for a character. This is a UTF8-aware
* version of [ord](http://php.net/ord).
*
* $digit = UTF8::ord($character);
*
* @author Harry Fuecks <hfuecks@gmail.com>
* @param string UTF-8 encoded character
* @return integer
*/
public static function ord($chr)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _ord($chr);
}
/**
* Takes an UTF-8 string and returns an array of ints representing the Unicode characters.
* Astral planes are supported i.e. the ints in the output can be > 0xFFFF.
* Occurrences of the BOM are ignored. Surrogates are not allowed.
*
* $array = UTF8::to_unicode($str);
*
* The Original Code is Mozilla Communicator client code.
* The Initial Developer of the Original Code is Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer.
* Ported to PHP by Henri Sivonen <hsivonen@iki.fi>, see <http://hsivonen.iki.fi/php-utf8/>
* Slight modifications to fit with phputf8 library by Harry Fuecks <hfuecks@gmail.com>
*
* @param string UTF-8 encoded string
* @return array unicode code points
* @return FALSE if the string is invalid
*/
public static function to_unicode($str)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _to_unicode($str);
}
/**
* Takes an array of ints representing the Unicode characters and returns a UTF-8 string.
* Astral planes are supported i.e. the ints in the input can be > 0xFFFF.
* Occurrances of the BOM are ignored. Surrogates are not allowed.
*
* $str = UTF8::to_unicode($array);
*
* The Original Code is Mozilla Communicator client code.
* The Initial Developer of the Original Code is Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer.
* Ported to PHP by Henri Sivonen <hsivonen@iki.fi>, see http://hsivonen.iki.fi/php-utf8/
* Slight modifications to fit with phputf8 library by Harry Fuecks <hfuecks@gmail.com>.
*
* @param array unicode code points representing a string
* @return string utf8 string of characters
* @return boolean FALSE if a code point cannot be found
*/
public static function from_unicode($arr)
{
if ( ! isset(self::$called[__FUNCTION__]))
{
require SYSPATH.'utf8'.DIRECTORY_SEPARATOR.__FUNCTION__.EXT;
// Function has been called
self::$called[__FUNCTION__] = TRUE;
}
return _from_unicode($arr);
}
} // End UTF8
if (Kohana_UTF8::$server_utf8 === NULL)
{
// Determine if this server supports UTF-8 natively
Kohana_UTF8::$server_utf8 = extension_loaded('mbstring');
}
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/utf8.php | PHP | mit | 21,893 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Abstract Controller class for RESTful controller mapping. Supports GET, PUT,
* POST, and DELETE. By default, these methods will be mapped to these actions:
*
* GET
* : Mapped to the "index" action, lists all objects
*
* POST
* : Mapped to the "create" action, creates a new object
*
* PUT
* : Mapped to the "update" action, update an existing object
*
* DELETE
* : Mapped to the "delete" action, delete an existing object
*
* Additional methods can be supported by adding the method and action to
* the `$_action_map` property.
*
* [!!] Using this class within a website will require heavy modification,
* due to most web browsers only supporting the GET and POST methods.
* Generally, this class should only be used for web services and APIs.
*
* @package Kohana
* @category Controller
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class Kohana_Controller_REST extends Controller {
/**
* @var array REST types
*/
protected $_action_map = array
(
HTTP_Request::GET => 'index',
HTTP_Request::PUT => 'update',
HTTP_Request::POST => 'create',
HTTP_Request::DELETE => 'delete',
);
/**
* @var string requested action
*/
protected $_action_requested = '';
/**
* Checks the requested method against the available methods. If the method
* is supported, sets the request action from the map. If not supported,
* the "invalid" action will be called.
*/
public function before()
{
$this->_action_requested = $this->request->action();
$method = Arr::get($_SERVER, 'HTTP_X_HTTP_METHOD_OVERRIDE', $this->request->method());
if ( ! isset($this->_action_map[$method]))
{
$this->request->action('invalid');
}
else
{
$this->request->action($this->_action_map[$method]);
}
return parent::before();
}
/**
* undocumented function
*/
public function after()
{
if (in_array(Arr::get($_SERVER, 'HTTP_X_HTTP_METHOD_OVERRIDE', $this->request->method()), array(
HTTP_Request::PUT,
HTTP_Request::POST,
HTTP_Request::DELETE)))
{
$this->response->headers('cache-control', 'no-cache, no-store, max-age=0, must-revalidate');
}
}
/**
* Sends a 405 "Method Not Allowed" response and a list of allowed actions.
*/
public function action_invalid()
{
// Send the "Method Not Allowed" response
$this->response->status(405)
->headers('Allow', implode(', ', array_keys($this->_action_map)));
}
} // End REST
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/controller/rest.php | PHP | mit | 2,573 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Abstract controller class for automatic templating.
*
* @package Kohana
* @category Controller
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class Kohana_Controller_Template extends Controller {
/**
* @var View page template
*/
public $template = 'template';
/**
* @var boolean auto render template
**/
public $auto_render = TRUE;
/**
* Loads the template [View] object.
*/
public function before()
{
if ($this->auto_render === TRUE)
{
// Load the template
$this->template = View::factory($this->template);
}
return parent::before();
}
/**
* Assigns the template [View] as the request response.
*/
public function after()
{
if ($this->auto_render === TRUE)
{
$this->response->body($this->template->render());
}
return parent::after();
}
} // End Controller_Template
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/controller/template.php | PHP | mit | 991 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* @package Kohana
* @category Exceptions
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Validation_Exception extends Kohana_Exception {
/**
* @var object Validation instance
*/
public $array;
/**
* @param Validate Validate object
* @param string error message
* @param array translation variables
* @param int the exception code
*/
public function __construct(Validation $array, $message = 'Failed to validate array', array $values = NULL, $code = 0)
{
$this->array = $array;
parent::__construct($message, $values, $code);
}
} // End Kohana_Validation_Exception
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/validation/exception.php | PHP | mit | 770 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Cookie helper.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Cookie {
/**
* @var string Magic salt to add to the cookie
*/
public static $salt = NULL;
/**
* @var integer Number of seconds before the cookie expires
*/
public static $expiration = 0;
/**
* @var string Restrict the path that the cookie is available to
*/
public static $path = '/';
/**
* @var string Restrict the domain that the cookie is available to
*/
public static $domain = NULL;
/**
* @var boolean Only transmit cookies over secure connections
*/
public static $secure = FALSE;
/**
* @var boolean Only transmit cookies over HTTP, disabling Javascript access
*/
public static $httponly = FALSE;
/**
* Gets the value of a signed cookie. Cookies without signatures will not
* be returned. If the cookie signature is present, but invalid, the cookie
* will be deleted.
*
* // Get the "theme" cookie, or use "blue" if the cookie does not exist
* $theme = Cookie::get('theme', 'blue');
*
* @param string cookie name
* @param mixed default value to return
* @return string
*/
public static function get($key, $default = NULL)
{
if ( ! isset($_COOKIE[$key]))
{
// The cookie does not exist
return $default;
}
// Get the cookie value
$cookie = $_COOKIE[$key];
// Find the position of the split between salt and contents
$split = strlen(Cookie::salt($key, NULL));
if (isset($cookie[$split]) AND $cookie[$split] === '~')
{
// Separate the salt and the value
list ($hash, $value) = explode('~', $cookie, 2);
if (Cookie::salt($key, $value) === $hash)
{
// Cookie signature is valid
return $value;
}
// The cookie signature is invalid, delete it
Cookie::delete($key);
}
return $default;
}
/**
* Sets a signed cookie. Note that all cookie values must be strings and no
* automatic serialization will be performed!
*
* // Set the "theme" cookie
* Cookie::set('theme', 'red');
*
* @param string name of cookie
* @param string value of cookie
* @param integer lifetime in seconds
* @return boolean
* @uses Cookie::salt
*/
public static function set($name, $value, $expiration = NULL)
{
if ($expiration === NULL)
{
// Use the default expiration
$expiration = Cookie::$expiration;
}
if ($expiration !== 0)
{
// The expiration is expected to be a UNIX timestamp
$expiration += time();
}
// Add the salt to the cookie value
$value = Cookie::salt($name, $value).'~'.$value;
return setcookie($name, $value, $expiration, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}
/**
* Deletes a cookie by making the value NULL and expiring it.
*
* Cookie::delete('theme');
*
* @param string cookie name
* @return boolean
* @uses Cookie::set
*/
public static function delete($name)
{
// Remove the cookie
unset($_COOKIE[$name]);
// Nullify the cookie and make it expire
return setcookie($name, NULL, -86400, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
}
/**
* Generates a salt string for a cookie based on the name and value.
*
* $salt = Cookie::salt('theme', 'red');
*
* @param string name of cookie
* @param string value of cookie
* @return string
*/
public static function salt($name, $value)
{
// Require a valid salt
if ( ! Cookie::$salt)
{
throw new Kohana_Exception('A valid cookie salt is required. Please set Cookie::$salt.');
}
// Determine the user agent
$agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : 'unknown';
return sha1($agent.$name.$value.Cookie::$salt);
}
} // End cookie
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/cookie.php | PHP | mit | 3,959 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* STDERR log writer. Writes out messages to STDERR.
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Log_StdErr extends Kohana_Log_Writer {
/**
* Writes each of the messages to STDERR.
*
* $writer->write($messages);
*
* @param array messages
* @return void
*/
public function write(array $messages)
{
// Set the log line format
$format = 'time --- type: body';
foreach ($messages as $message)
{
// Writes out each message
fwrite(STDERR, PHP_EOL.strtr($format, $message));
}
}
} // End Kohana_Log_StdErr
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/log/stderr.php | PHP | mit | 741 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* File log writer. Writes out messages and stores them in a YYYY/MM directory.
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Log_File extends Log_Writer {
/**
* @var string Directory to place log files in
*/
protected $_directory;
/**
* Creates a new file logger. Checks that the directory exists and
* is writable.
*
* $writer = new Log_File($directory);
*
* @param string log directory
* @return void
*/
public function __construct($directory)
{
if ( ! is_dir($directory) OR ! is_writable($directory))
{
throw new Kohana_Exception('Directory :dir must be writable',
array(':dir' => Debug::path($directory)));
}
// Determine the directory path
$this->_directory = realpath($directory).DIRECTORY_SEPARATOR;
}
/**
* Writes each of the messages into the log file. The log file will be
* appended to the `YYYY/MM/DD.log.php` file, where YYYY is the current
* year, MM is the current month, and DD is the current day.
*
* $writer->write($messages);
*
* @param array messages
* @return void
*/
public function write(array $messages)
{
// Set the yearly directory name
$directory = $this->_directory.date('Y');
if ( ! is_dir($directory))
{
// Create the yearly directory
mkdir($directory, 02777);
// Set permissions (must be manually set to fix umask issues)
chmod($directory, 02777);
}
// Add the month to the directory
$directory .= DIRECTORY_SEPARATOR.date('m');
if ( ! is_dir($directory))
{
// Create the yearly directory
mkdir($directory, 02777);
// Set permissions (must be manually set to fix umask issues)
chmod($directory, 02777);
}
// Set the name of the log file
$filename = $directory.DIRECTORY_SEPARATOR.date('d').EXT;
if ( ! file_exists($filename))
{
// Create the log file
file_put_contents($filename, Kohana::FILE_SECURITY.' ?>'.PHP_EOL);
// Allow anyone to write to log files
chmod($filename, 0666);
}
foreach ($messages as $message)
{
// Write each message into the log file
// Format: time --- level: body
file_put_contents($filename, PHP_EOL.$message['time'].' --- '.$this->_log_levels[$message['level']].': '.$message['body'], FILE_APPEND);
}
}
} // End Kohana_Log_File | 12-05-2011-wharfland-project | trunk/system/classes/kohana/log/file.php | PHP | mit | 2,466 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Log writer abstract class. All [Log] writers must extend this class.
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class Kohana_Log_Writer {
/**
* Numeric log level to string lookup table.
* @var array
*/
protected $_log_levels = array(
LOG_EMERG => 'EMERGENCY',
LOG_CRIT => 'CRITICAL',
LOG_ERR => 'ERROR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG',
);
/**
* Write an array of messages.
*
* $writer->write($messages);
*
* @param array messages
* @return void
*/
abstract public function write(array $messages);
/**
* Allows the writer to have a unique key when stored.
*
* echo $writer;
*
* @return string
*/
final public function __toString()
{
return spl_object_hash($this);
}
} // End Kohana_Log_Writer
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/log/writer.php | PHP | mit | 1,046 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* STDOUT log writer. Writes out messages to STDOUT.
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaphp.com/license
*/
class Kohana_Log_StdOut extends Kohana_Log_Writer {
/**
* Writes each of the messages to STDOUT.
*
* $writer->write($messages);
*
* @param array messages
* @return void
*/
public function write(array $messages)
{
// Set the log line format
$format = 'time --- type: body';
foreach ($messages as $message)
{
// Writes out each message
fwrite(STDOUT, PHP_EOL.strtr($format, $message));
}
}
} // End Kohana_Log_StdOut
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/log/stdout.php | PHP | mit | 741 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Syslog log writer.
*
* @package Kohana
* @category Logging
* @author Jeremy Bush
* @copyright (c) 2010 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Log_Syslog extends Log_Writer {
/**
* @var string The syslog identifier
*/
protected $_ident;
/**
* @var array log levels
*/
protected $_syslog_levels = array('ERROR' => LOG_ERR,
'CRITICAL' => LOG_CRIT,
'STRACE' => LOG_ALERT,
'ALERT' => LOG_WARNING,
'INFO' => LOG_INFO,
'DEBUG' => LOG_DEBUG);
/**
* Creates a new syslog logger.
*
* @see http://us2.php.net/openlog
*
* @param string syslog identifier
* @param int facility to log to
* @return void
*/
public function __construct($ident = 'KohanaPHP', $facility = LOG_USER)
{
$this->_ident = $ident;
// Open the connection to syslog
openlog($this->_ident, LOG_CONS, $facility);
}
/**
* Writes each of the messages into the syslog.
*
* @param array messages
* @return void
*/
public function write(array $messages)
{
foreach ($messages as $message)
{
syslog($message['level'], $message['body']);
}
}
/**
* Closes the syslog connection
*
* @return void
*/
public function __destruct()
{
// Close connection to syslog
closelog();
}
} // End Kohana_Log_Syslog
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/log/syslog.php | PHP | mit | 1,563 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* URL helper class.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_URL {
/**
* Gets the base URL to the application.
* To specify a protocol, provide the protocol as a string or request object.
* If a protocol is used, a complete URL will be generated using the
* `$_SERVER['HTTP_HOST']` variable.
*
* // Absolute URL path with no host or protocol
* echo URL::base();
*
* // Absolute URL path with host, https protocol and index.php if set
* echo URL::base('https', TRUE);
*
* // Absolute URL path with host and protocol from $request
* echo URL::base($request);
*
* @param mixed $protocol Protocol string, [Request], or boolean
* @param boolean $index Add index file to URL?
* @return string
* @uses Kohana::$index_file
* @uses Request::protocol()
*/
public static function base($protocol = NULL, $index = FALSE)
{
// Start with the configured base URL
$base_url = Kohana::$base_url;
if ($protocol === TRUE)
{
// Use the initial request to get the protocol
$protocol = Request::$initial;
}
if ($protocol instanceof Request)
{
// Use the current protocol
$protocol = $protocol->protocol();
}
if ( ! $protocol)
{
// Use the configured default protocol
$protocol = parse_url($base_url, PHP_URL_SCHEME);
}
if ($index === TRUE AND ! empty(Kohana::$index_file))
{
// Add the index file to the URL
$base_url .= Kohana::$index_file.'/';
}
if (is_string($protocol))
{
if ($port = parse_url($base_url, PHP_URL_PORT))
{
// Found a port, make it usable for the URL
$port = ':'.$port;
}
if ($domain = parse_url($base_url, PHP_URL_HOST))
{
// Remove everything but the path from the URL
$base_url = parse_url($base_url, PHP_URL_PATH);
}
else
{
// Attempt to use HTTP_HOST and fallback to SERVER_NAME
$domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'];
}
// Add the protocol and domain to the base URL
$base_url = $protocol.'://'.$domain.$port.$base_url;
}
return $base_url;
}
/**
* Fetches an absolute site URL based on a URI segment.
*
* echo URL::site('foo/bar');
*
* @param string $uri Site URI to convert
* @param mixed $protocol Protocol string or [Request] class to use protocol from
* @param boolean $index Include the index_page in the URL
* @return string
* @uses URL::base
*/
public static function site($uri = '', $protocol = NULL, $index = TRUE)
{
// Chop off possible scheme, host, port, user and pass parts
$path = preg_replace('~^[-a-z0-9+.]++://[^/]++/?~', '', trim($uri, '/'));
if ( ! UTF8::is_ascii($path))
{
// Encode all non-ASCII characters, as per RFC 1738
$path = preg_replace('~([^/]+)~e', 'rawurlencode("$1")', $path);
}
// Concat the URL
return URL::base($protocol, $index).$path;
}
/**
* Merges the current GET parameters with an array of new or overloaded
* parameters and returns the resulting query string.
*
* // Returns "?sort=title&limit=10" combined with any existing GET values
* $query = URL::query(array('sort' => 'title', 'limit' => 10));
*
* Typically you would use this when you are sorting query results,
* or something similar.
*
* [!!] Parameters with a NULL value are left out.
*
* @param array $params Array of GET parameters
* @param boolean $use_get Include current request GET parameters
* @return string
*/
public static function query(array $params = NULL, $use_get = TRUE)
{
if ($use_get)
{
if ($params === NULL)
{
// Use only the current parameters
$params = $_GET;
}
else
{
// Merge the current and new parameters
$params = array_merge($_GET, $params);
}
}
if (empty($params))
{
// No query parameters
return '';
}
// Note: http_build_query returns an empty string for a params array with only NULL values
$query = http_build_query($params, '', '&');
// Don't prepend '?' to an empty string
return ($query === '') ? '' : ('?'.$query);
}
/**
* Convert a phrase to a URL-safe title.
*
* echo URL::title('My Blog Post'); // "my-blog-post"
*
* @param string $title Phrase to convert
* @param string $separator Word separator (any single character)
* @param boolean $ascii_only Transliterate to ASCII?
* @return string
* @uses UTF8::transliterate_to_ascii
*/
public static function title($title, $separator = '-', $ascii_only = FALSE)
{
if ($ascii_only === TRUE)
{
// Transliterate non-ASCII characters
$title = UTF8::transliterate_to_ascii($title);
// Remove all characters that are not the separator, a-z, 0-9, or whitespace
$title = preg_replace('![^'.preg_quote($separator).'a-z0-9\s]+!', '', strtolower($title));
}
else
{
// Remove all characters that are not the separator, letters, numbers, or whitespace
$title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', UTF8::strtolower($title));
}
// Replace all separator characters and whitespace by a single separator
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
// Trim separators from the beginning and end
return trim($title, $separator);
}
} // End url | 12-05-2011-wharfland-project | trunk/system/classes/kohana/url.php | PHP | mit | 5,533 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Text helper class. Provides simple methods for working with text.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Text {
/**
* @var array number units and text equivalents
*/
public static $units = array(
1000000000 => 'billion',
1000000 => 'million',
1000 => 'thousand',
100 => 'hundred',
90 => 'ninety',
80 => 'eighty',
70 => 'seventy',
60 => 'sixty',
50 => 'fifty',
40 => 'fourty',
30 => 'thirty',
20 => 'twenty',
19 => 'nineteen',
18 => 'eighteen',
17 => 'seventeen',
16 => 'sixteen',
15 => 'fifteen',
14 => 'fourteen',
13 => 'thirteen',
12 => 'twelve',
11 => 'eleven',
10 => 'ten',
9 => 'nine',
8 => 'eight',
7 => 'seven',
6 => 'six',
5 => 'five',
4 => 'four',
3 => 'three',
2 => 'two',
1 => 'one',
);
/**
* Limits a phrase to a given number of words.
*
* $text = Text::limit_words($text);
*
* @param string phrase to limit words of
* @param integer number of words to limit to
* @param string end character or entity
* @return string
*/
public static function limit_words($str, $limit = 100, $end_char = NULL)
{
$limit = (int) $limit;
$end_char = ($end_char === NULL) ? '…' : $end_char;
if (trim($str) === '')
return $str;
if ($limit <= 0)
return $end_char;
preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches);
// Only attach the end character if the matched string is shorter
// than the starting string.
return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
}
/**
* Limits a phrase to a given number of characters.
*
* $text = Text::limit_chars($text);
*
* @param string phrase to limit characters of
* @param integer number of characters to limit to
* @param string end character or entity
* @param boolean enable or disable the preservation of words while limiting
* @return string
* @uses UTF8::strlen
*/
public static function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE)
{
$end_char = ($end_char === NULL) ? '…' : $end_char;
$limit = (int) $limit;
if (trim($str) === '' OR UTF8::strlen($str) <= $limit)
return $str;
if ($limit <= 0)
return $end_char;
if ($preserve_words === FALSE)
return rtrim(UTF8::substr($str, 0, $limit)).$end_char;
// Don't preserve words. The limit is considered the top limit.
// No strings with a length longer than $limit should be returned.
if ( ! preg_match('/^.{0,'.$limit.'}\s/us', $str, $matches))
return $end_char;
return rtrim($matches[0]).((strlen($matches[0]) === strlen($str)) ? '' : $end_char);
}
/**
* Alternates between two or more strings.
*
* echo Text::alternate('one', 'two'); // "one"
* echo Text::alternate('one', 'two'); // "two"
* echo Text::alternate('one', 'two'); // "one"
*
* Note that using multiple iterations of different strings may produce
* unexpected results.
*
* @param string strings to alternate between
* @return string
*/
public static function alternate()
{
static $i;
if (func_num_args() === 0)
{
$i = 0;
return '';
}
$args = func_get_args();
return $args[($i++ % count($args))];
}
/**
* Generates a random string of a given type and length.
*
*
* $str = Text::random(); // 8 character random string
*
* The following types are supported:
*
* alnum
* : Upper and lower case a-z, 0-9 (default)
*
* alpha
* : Upper and lower case a-z
*
* hexdec
* : Hexadecimal characters a-f, 0-9
*
* distinct
* : Uppercase characters and numbers that cannot be confused
*
* You can also create a custom type by providing the "pool" of characters
* as the type.
*
* @param string a type of pool, or a string of characters to use as the pool
* @param integer length of string to return
* @return string
* @uses UTF8::split
*/
public static function random($type = NULL, $length = 8)
{
if ($type === NULL)
{
// Default is to generate an alphanumeric string
$type = 'alnum';
}
$utf8 = FALSE;
switch ($type)
{
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'hexdec':
$pool = '0123456789abcdef';
break;
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
case 'distinct':
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
break;
default:
$pool = (string) $type;
$utf8 = ! UTF8::is_ascii($pool);
break;
}
// Split the pool into an array of characters
$pool = ($utf8 === TRUE) ? UTF8::str_split($pool, 1) : str_split($pool, 1);
// Largest pool key
$max = count($pool) - 1;
$str = '';
for ($i = 0; $i < $length; $i++)
{
// Select a random character from the pool and add it to the string
$str .= $pool[mt_rand(0, $max)];
}
// Make sure alnum strings contain at least one letter and one digit
if ($type === 'alnum' AND $length > 1)
{
if (ctype_alpha($str))
{
// Add a random digit
$str[mt_rand(0, $length - 1)] = chr(mt_rand(48, 57));
}
elseif (ctype_digit($str))
{
// Add a random letter
$str[mt_rand(0, $length - 1)] = chr(mt_rand(65, 90));
}
}
return $str;
}
/**
* Reduces multiple slashes in a string to single slashes.
*
* $str = Text::reduce_slashes('foo//bar/baz'); // "foo/bar/baz"
*
* @param string string to reduce slashes of
* @return string
*/
public static function reduce_slashes($str)
{
return preg_replace('#(?<!:)//+#', '/', $str);
}
/**
* Replaces the given words with a string.
*
* // Displays "What the #####, man!"
* echo Text::censor('What the frick, man!', array(
* 'frick' => '#####',
* ));
*
* @param string phrase to replace words in
* @param array words to replace
* @param string replacement string
* @param boolean replace words across word boundries (space, period, etc)
* @return string
* @uses UTF8::strlen
*/
public static function censor($str, $badwords, $replacement = '#', $replace_partial_words = TRUE)
{
foreach ( (array) $badwords as $key => $badword)
{
$badwords[$key] = str_replace('\*', '\S*?', preg_quote( (string) $badword));
}
$regex = '('.implode('|', $badwords).')';
if ($replace_partial_words === FALSE)
{
// Just using \b isn't sufficient when we need to replace a badword that already contains word boundaries itself
$regex = '(?<=\b|\s|^)'.$regex.'(?=\b|\s|$)';
}
$regex = '!'.$regex.'!ui';
if (UTF8::strlen($replacement) == 1)
{
$regex .= 'e';
return preg_replace($regex, 'str_repeat($replacement, UTF8::strlen(\'$1\'))', $str);
}
return preg_replace($regex, $replacement, $str);
}
/**
* Finds the text that is similar between a set of words.
*
* $match = Text::similar(array('fred', 'fran', 'free'); // "fr"
*
* @param array words to find similar text of
* @return string
*/
public static function similar(array $words)
{
// First word is the word to match against
$word = current($words);
for ($i = 0, $max = strlen($word); $i < $max; ++$i)
{
foreach ($words as $w)
{
// Once a difference is found, break out of the loops
if ( ! isset($w[$i]) OR $w[$i] !== $word[$i])
break 2;
}
}
// Return the similar text
return substr($word, 0, $i);
}
/**
* Converts text email addresses and anchors into links. Existing links
* will not be altered.
*
* echo Text::auto_link($text);
*
* [!!] This method is not foolproof since it uses regex to parse HTML.
*
* @param string text to auto link
* @return string
* @uses Text::auto_link_urls
* @uses Text::auto_link_emails
*/
public static function auto_link($text)
{
// Auto link emails first to prevent problems with "www.domain.com@example.com"
return Text::auto_link_urls(Text::auto_link_emails($text));
}
/**
* Converts text anchors into links. Existing links will not be altered.
*
* echo Text::auto_link_urls($text);
*
* [!!] This method is not foolproof since it uses regex to parse HTML.
*
* @param string text to auto link
* @return string
* @uses HTML::anchor
*/
public static function auto_link_urls($text)
{
// Find and replace all http/https/ftp/ftps links that are not part of an existing html anchor
$text = preg_replace_callback('~\b(?<!href="|">)(?:ht|f)tps?://\S+(?:/|\b)~i', 'Text::_auto_link_urls_callback1', $text);
// Find and replace all naked www.links.com (without http://)
return preg_replace_callback('~\b(?<!://|">)www(?:\.[a-z0-9][-a-z0-9]*+)+\.[a-z]{2,6}\b~i', 'Text::_auto_link_urls_callback2', $text);
}
protected static function _auto_link_urls_callback1($matches)
{
return HTML::anchor($matches[0]);
}
protected static function _auto_link_urls_callback2($matches)
{
return HTML::anchor('http://'.$matches[0], $matches[0]);
}
/**
* Converts text email addresses into links. Existing links will not
* be altered.
*
* echo Text::auto_link_emails($text);
*
* [!!] This method is not foolproof since it uses regex to parse HTML.
*
* @param string text to auto link
* @return string
* @uses HTML::mailto
*/
public static function auto_link_emails($text)
{
// Find and replace all email addresses that are not part of an existing html mailto anchor
// Note: The "58;" negative lookbehind prevents matching of existing encoded html mailto anchors
// The html entity for a colon (:) is : or : or : etc.
return preg_replace_callback('~\b(?<!href="mailto:|58;)(?!\.)[-+_a-z0-9.]++(?<!\.)@(?![-.])[-a-z0-9.]+(?<!\.)\.[a-z]{2,6}\b(?!</a>)~i', 'Text::_auto_link_emails_callback', $text);
}
protected static function _auto_link_emails_callback($matches)
{
return HTML::mailto($matches[0]);
}
/**
* Automatically applies "p" and "br" markup to text.
* Basically [nl2br](http://php.net/nl2br) on steroids.
*
* echo Text::auto_p($text);
*
* [!!] This method is not foolproof since it uses regex to parse HTML.
*
* @param string subject
* @param boolean convert single linebreaks to <br />
* @return string
*/
public static function auto_p($str, $br = TRUE)
{
// Trim whitespace
if (($str = trim($str)) === '')
return '';
// Standardize newlines
$str = str_replace(array("\r\n", "\r"), "\n", $str);
// Trim whitespace on each line
$str = preg_replace('~^[ \t]+~m', '', $str);
$str = preg_replace('~[ \t]+$~m', '', $str);
// The following regexes only need to be executed if the string contains html
if ($html_found = (strpos($str, '<') !== FALSE))
{
// Elements that should not be surrounded by p tags
$no_p = '(?:p|div|h[1-6r]|ul|ol|li|blockquote|d[dlt]|pre|t[dhr]|t(?:able|body|foot|head)|c(?:aption|olgroup)|form|s(?:elect|tyle)|a(?:ddress|rea)|ma(?:p|th))';
// Put at least two linebreaks before and after $no_p elements
$str = preg_replace('~^<'.$no_p.'[^>]*+>~im', "\n$0", $str);
$str = preg_replace('~</'.$no_p.'\s*+>$~im', "$0\n", $str);
}
// Do the <p> magic!
$str = '<p>'.trim($str).'</p>';
$str = preg_replace('~\n{2,}~', "</p>\n\n<p>", $str);
// The following regexes only need to be executed if the string contains html
if ($html_found !== FALSE)
{
// Remove p tags around $no_p elements
$str = preg_replace('~<p>(?=</?'.$no_p.'[^>]*+>)~i', '', $str);
$str = preg_replace('~(</?'.$no_p.'[^>]*+>)</p>~i', '$1', $str);
}
// Convert single linebreaks to <br />
if ($br === TRUE)
{
$str = preg_replace('~(?<!\n)\n(?!\n)~', "<br />\n", $str);
}
return $str;
}
/**
* Returns human readable sizes. Based on original functions written by
* [Aidan Lister](http://aidanlister.com/repos/v/function.size_readable.php)
* and [Quentin Zervaas](http://www.phpriot.com/d/code/strings/filesize-format/).
*
* echo Text::bytes(filesize($file));
*
* @param integer size in bytes
* @param string a definitive unit
* @param string the return string format
* @param boolean whether to use SI prefixes or IEC
* @return string
*/
public static function bytes($bytes, $force_unit = NULL, $format = NULL, $si = TRUE)
{
// Format string
$format = ($format === NULL) ? '%01.2f %s' : (string) $format;
// IEC prefixes (binary)
if ($si == FALSE OR strpos($force_unit, 'i') !== FALSE)
{
$units = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB');
$mod = 1024;
}
// SI prefixes (decimal)
else
{
$units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
$mod = 1000;
}
// Determine unit to use
if (($power = array_search( (string) $force_unit, $units)) === FALSE)
{
$power = ($bytes > 0) ? floor(log($bytes, $mod)) : 0;
}
return sprintf($format, $bytes / pow($mod, $power), $units[$power]);
}
/**
* Format a number to human-readable text.
*
* // Display: one thousand and twenty-four
* echo Text::number(1024);
*
* // Display: five million, six hundred and thirty-two
* echo Text::number(5000632);
*
* @param integer number to format
* @return string
* @since 3.0.8
*/
public static function number($number)
{
// The number must always be an integer
$number = (int) $number;
// Uncompiled text version
$text = array();
// Last matched unit within the loop
$last_unit = NULL;
// The last matched item within the loop
$last_item = '';
foreach (Text::$units as $unit => $name)
{
if ($number / $unit >= 1)
{
// $value = the number of times the number is divisble by unit
$number -= $unit * ($value = (int) floor($number / $unit));
// Temporary var for textifying the current unit
$item = '';
if ($unit < 100)
{
if ($last_unit < 100 AND $last_unit >= 20)
{
$last_item .= '-'.$name;
}
else
{
$item = $name;
}
}
else
{
$item = Text::number($value).' '.$name;
}
// In the situation that we need to make a composite number (i.e. twenty-three)
// then we need to modify the previous entry
if (empty($item))
{
array_pop($text);
$item = $last_item;
}
$last_item = $text[] = $item;
$last_unit = $unit;
}
}
if (count($text) > 1)
{
$and = array_pop($text);
}
$text = implode(', ', $text);
if (isset($and))
{
$text .= ' and '.$and;
}
return $text;
}
/**
* Prevents [widow words](http://www.shauninman.com/archive/2006/08/22/widont_wordpress_plugin)
* by inserting a non-breaking space between the last two words.
*
* echo Text::widont($text);
*
* @param string text to remove widows from
* @return string
*/
public static function widont($str)
{
$str = rtrim($str);
$space = strrpos($str, ' ');
if ($space !== FALSE)
{
$str = substr($str, 0, $space).' '.substr($str, $space + 1);
}
return $str;
}
} // End text
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/text.php | PHP | mit | 15,329 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Provides simple benchmarking and profiling. To display the statistics that
* have been collected, load the `profiler/stats` [View]:
*
* echo View::factory('profiler/stats');
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Profiler {
/**
* @var integer maximium number of application stats to keep
*/
public static $rollover = 1000;
/**
* @var array collected benchmarks
*/
protected static $_marks = array();
/**
* Starts a new benchmark and returns a unique token. The returned token
* _must_ be used when stopping the benchmark.
*
* $token = Profiler::start('test', 'profiler');
*
* @param string group name
* @param string benchmark name
* @return string
*/
public static function start($group, $name)
{
static $counter = 0;
// Create a unique token based on the counter
$token = 'kp/'.base_convert($counter++, 10, 32);
Profiler::$_marks[$token] = array
(
'group' => strtolower($group),
'name' => (string) $name,
// Start the benchmark
'start_time' => microtime(TRUE),
'start_memory' => memory_get_usage(),
// Set the stop keys without values
'stop_time' => FALSE,
'stop_memory' => FALSE,
);
return $token;
}
/**
* Stops a benchmark.
*
* Profiler::stop($token);
*
* @param string token
* @return void
*/
public static function stop($token)
{
// Stop the benchmark
Profiler::$_marks[$token]['stop_time'] = microtime(TRUE);
Profiler::$_marks[$token]['stop_memory'] = memory_get_usage();
}
/**
* Deletes a benchmark. If an error occurs during the benchmark, it is
* recommended to delete the benchmark to prevent statistics from being
* adversely affected.
*
* Profiler::delete($token);
*
* @param string token
* @return void
*/
public static function delete($token)
{
// Remove the benchmark
unset(Profiler::$_marks[$token]);
}
/**
* Returns all the benchmark tokens by group and name as an array.
*
* $groups = Profiler::groups();
*
* @return array
*/
public static function groups()
{
$groups = array();
foreach (Profiler::$_marks as $token => $mark)
{
// Sort the tokens by the group and name
$groups[$mark['group']][$mark['name']][] = $token;
}
return $groups;
}
/**
* Gets the min, max, average and total of a set of tokens as an array.
*
* $stats = Profiler::stats($tokens);
*
* @param array profiler tokens
* @return array min, max, average, total
* @uses Profiler::total
*/
public static function stats(array $tokens)
{
// Min and max are unknown by default
$min = $max = array(
'time' => NULL,
'memory' => NULL);
// Total values are always integers
$total = array(
'time' => 0,
'memory' => 0);
foreach ($tokens as $token)
{
// Get the total time and memory for this benchmark
list($time, $memory) = Profiler::total($token);
if ($max['time'] === NULL OR $time > $max['time'])
{
// Set the maximum time
$max['time'] = $time;
}
if ($min['time'] === NULL OR $time < $min['time'])
{
// Set the minimum time
$min['time'] = $time;
}
// Increase the total time
$total['time'] += $time;
if ($max['memory'] === NULL OR $memory > $max['memory'])
{
// Set the maximum memory
$max['memory'] = $memory;
}
if ($min['memory'] === NULL OR $memory < $min['memory'])
{
// Set the minimum memory
$min['memory'] = $memory;
}
// Increase the total memory
$total['memory'] += $memory;
}
// Determine the number of tokens
$count = count($tokens);
// Determine the averages
$average = array(
'time' => $total['time'] / $count,
'memory' => $total['memory'] / $count);
return array(
'min' => $min,
'max' => $max,
'total' => $total,
'average' => $average);
}
/**
* Gets the min, max, average and total of profiler groups as an array.
*
* $stats = Profiler::group_stats('test');
*
* @param mixed single group name string, or array with group names; all groups by default
* @return array min, max, average, total
* @uses Profiler::groups
* @uses Profiler::stats
*/
public static function group_stats($groups = NULL)
{
// Which groups do we need to calculate stats for?
$groups = ($groups === NULL)
? Profiler::groups()
: array_intersect_key(Profiler::groups(), array_flip( (array) $groups));
// All statistics
$stats = array();
foreach ($groups as $group => $names)
{
foreach ($names as $name => $tokens)
{
// Store the stats for each subgroup.
// We only need the values for "total".
$_stats = Profiler::stats($tokens);
$stats[$group][$name] = $_stats['total'];
}
}
// Group stats
$groups = array();
foreach ($stats as $group => $names)
{
// Min and max are unknown by default
$groups[$group]['min'] = $groups[$group]['max'] = array(
'time' => NULL,
'memory' => NULL);
// Total values are always integers
$groups[$group]['total'] = array(
'time' => 0,
'memory' => 0);
foreach ($names as $total)
{
if ( ! isset($groups[$group]['min']['time']) OR $groups[$group]['min']['time'] > $total['time'])
{
// Set the minimum time
$groups[$group]['min']['time'] = $total['time'];
}
if ( ! isset($groups[$group]['min']['memory']) OR $groups[$group]['min']['memory'] > $total['memory'])
{
// Set the minimum memory
$groups[$group]['min']['memory'] = $total['memory'];
}
if ( ! isset($groups[$group]['max']['time']) OR $groups[$group]['max']['time'] < $total['time'])
{
// Set the maximum time
$groups[$group]['max']['time'] = $total['time'];
}
if ( ! isset($groups[$group]['max']['memory']) OR $groups[$group]['max']['memory'] < $total['memory'])
{
// Set the maximum memory
$groups[$group]['max']['memory'] = $total['memory'];
}
// Increase the total time and memory
$groups[$group]['total']['time'] += $total['time'];
$groups[$group]['total']['memory'] += $total['memory'];
}
// Determine the number of names (subgroups)
$count = count($names);
// Determine the averages
$groups[$group]['average']['time'] = $groups[$group]['total']['time'] / $count;
$groups[$group]['average']['memory'] = $groups[$group]['total']['memory'] / $count;
}
return $groups;
}
/**
* Gets the total execution time and memory usage of a benchmark as a list.
*
* list($time, $memory) = Profiler::total($token);
*
* @param string token
* @return array execution time, memory
*/
public static function total($token)
{
// Import the benchmark data
$mark = Profiler::$_marks[$token];
if ($mark['stop_time'] === FALSE)
{
// The benchmark has not been stopped yet
$mark['stop_time'] = microtime(TRUE);
$mark['stop_memory'] = memory_get_usage();
}
return array
(
// Total time in seconds
$mark['stop_time'] - $mark['start_time'],
// Amount of memory in bytes
$mark['stop_memory'] - $mark['start_memory'],
);
}
/**
* Gets the total application run time and memory usage. Caches the result
* so that it can be compared between requests.
*
* list($time, $memory) = Profiler::application();
*
* @return array execution time, memory
* @uses Kohana::cache
*/
public static function application()
{
// Load the stats from cache, which is valid for 1 day
$stats = Kohana::cache('profiler_application_stats', NULL, 3600 * 24);
if ( ! is_array($stats) OR $stats['count'] > Profiler::$rollover)
{
// Initialize the stats array
$stats = array(
'min' => array(
'time' => NULL,
'memory' => NULL),
'max' => array(
'time' => NULL,
'memory' => NULL),
'total' => array(
'time' => NULL,
'memory' => NULL),
'count' => 0);
}
// Get the application run time
$time = microtime(TRUE) - KOHANA_START_TIME;
// Get the total memory usage
$memory = memory_get_usage() - KOHANA_START_MEMORY;
// Calculate max time
if ($stats['max']['time'] === NULL OR $time > $stats['max']['time'])
{
$stats['max']['time'] = $time;
}
// Calculate min time
if ($stats['min']['time'] === NULL OR $time < $stats['min']['time'])
{
$stats['min']['time'] = $time;
}
// Add to total time
$stats['total']['time'] += $time;
// Calculate max memory
if ($stats['max']['memory'] === NULL OR $memory > $stats['max']['memory'])
{
$stats['max']['memory'] = $memory;
}
// Calculate min memory
if ($stats['min']['memory'] === NULL OR $memory < $stats['min']['memory'])
{
$stats['min']['memory'] = $memory;
}
// Add to total memory
$stats['total']['memory'] += $memory;
// Another mark has been added to the stats
$stats['count']++;
// Determine the averages
$stats['average'] = array(
'time' => $stats['total']['time'] / $stats['count'],
'memory' => $stats['total']['memory'] / $stats['count']);
// Cache the new stats
Kohana::cache('profiler_application_stats', $stats);
// Set the current application execution time and memory
// Do NOT cache these, they are specific to the current request only
$stats['current']['time'] = $time;
$stats['current']['memory'] = $memory;
// Return the total application run time and memory usage
return $stats;
}
} // End Profiler
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/profiler.php | PHP | mit | 9,592 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* The Encrypt library provides two-way encryption of text and binary strings
* using the [Mcrypt](http://php.net/mcrypt) extension, which consists of three
* parts: the key, the cipher, and the mode.
*
* The Key
* : A secret passphrase that is used for encoding and decoding
*
* The Cipher
* : A [cipher](http://php.net/mcrypt.ciphers) determines how the encryption
* is mathematically calculated. By default, the "rijndael-128" cipher
* is used. This is commonly known as "AES-128" and is an industry standard.
*
* The Mode
* : The [mode](http://php.net/mcrypt.constants) determines how the encrypted
* data is written in binary form. By default, the "nofb" mode is used,
* which produces short output with high entropy.
*
* @package Kohana
* @category Security
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Encrypt {
/**
* @var string default instance name
*/
public static $default = 'default';
/**
* @var array Encrypt class instances
*/
public static $instances = array();
/**
* @var string OS-dependent RAND type to use
*/
protected static $_rand;
/**
* Returns a singleton instance of Encrypt. An encryption key must be
* provided in your "encrypt" configuration file.
*
* $encrypt = Encrypt::instance();
*
* @param string configuration group name
* @return Encrypt
*/
public static function instance($name = NULL)
{
if ($name === NULL)
{
// Use the default instance name
$name = Encrypt::$default;
}
if ( ! isset(Encrypt::$instances[$name]))
{
// Load the configuration data
$config = Kohana::config('encrypt')->$name;
if ( ! isset($config['key']))
{
// No default encryption key is provided!
throw new Kohana_Exception('No encryption key is defined in the encryption configuration group: :group',
array(':group' => $name));
}
if ( ! isset($config['mode']))
{
// Add the default mode
$config['mode'] = MCRYPT_MODE_NOFB;
}
if ( ! isset($config['cipher']))
{
// Add the default cipher
$config['cipher'] = MCRYPT_RIJNDAEL_128;
}
// Create a new instance
Encrypt::$instances[$name] = new Encrypt($config['key'], $config['mode'], $config['cipher']);
}
return Encrypt::$instances[$name];
}
/**
* Creates a new mcrypt wrapper.
*
* @param string encryption key
* @param string mcrypt mode
* @param string mcrypt cipher
*/
public function __construct($key, $mode, $cipher)
{
// Find the max length of the key, based on cipher and mode
$size = mcrypt_get_key_size($cipher, $mode);
if (isset($key[$size]))
{
// Shorten the key to the maximum size
$key = substr($key, 0, $size);
}
// Store the key, mode, and cipher
$this->_key = $key;
$this->_mode = $mode;
$this->_cipher = $cipher;
// Store the IV size
$this->_iv_size = mcrypt_get_iv_size($this->_cipher, $this->_mode);
}
/**
* Encrypts a string and returns an encrypted string that can be decoded.
*
* $data = $encrypt->encode($data);
*
* The encrypted binary data is encoded using [base64](http://php.net/base64_encode)
* to convert it to a string. This string can be stored in a database,
* displayed, and passed using most other means without corruption.
*
* @param string data to be encrypted
* @return string
*/
public function encode($data)
{
// Set the rand type if it has not already been set
if (Encrypt::$_rand === NULL)
{
if (Kohana::$is_windows)
{
// Windows only supports the system random number generator
Encrypt::$_rand = MCRYPT_RAND;
}
else
{
if (defined('MCRYPT_DEV_URANDOM'))
{
// Use /dev/urandom
Encrypt::$_rand = MCRYPT_DEV_URANDOM;
}
elseif (defined('MCRYPT_DEV_RANDOM'))
{
// Use /dev/random
Encrypt::$_rand = MCRYPT_DEV_RANDOM;
}
else
{
// Use the system random number generator
Encrypt::$_rand = MCRYPT_RAND;
}
}
}
if (Encrypt::$_rand === MCRYPT_RAND)
{
// The system random number generator must always be seeded each
// time it is used, or it will not produce true random results
mt_srand();
}
// Create a random initialization vector of the proper size for the current cipher
$iv = mcrypt_create_iv($this->_iv_size, Encrypt::$_rand);
// Encrypt the data using the configured options and generated iv
$data = mcrypt_encrypt($this->_cipher, $this->_key, $data, $this->_mode, $iv);
// Use base64 encoding to convert to a string
return base64_encode($iv.$data);
}
/**
* Decrypts an encoded string back to its original value.
*
* $data = $encrypt->decode($data);
*
* @param string encoded string to be decrypted
* @return FALSE if decryption fails
* @return string
*/
public function decode($data)
{
// Convert the data back to binary
$data = base64_decode($data, TRUE);
if ( ! $data)
{
// Invalid base64 data
return FALSE;
}
// Extract the initialization vector from the data
$iv = substr($data, 0, $this->_iv_size);
if ($this->_iv_size !== strlen($iv))
{
// The iv is not the expected size
return FALSE;
}
// Remove the iv from the data
$data = substr($data, $this->_iv_size);
// Return the decrypted data, trimming the \0 padding bytes from the end of the data
return rtrim(mcrypt_decrypt($this->_cipher, $this->_key, $data, $this->_mode, $iv), "\0");
}
} // End Encrypt
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/encrypt.php | PHP | mit | 5,600 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Request and response wrapper. Uses the [Route] class to determine what
* [Controller] to send the request to.
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request implements HTTP_Request {
/**
* @var string client user agent
*/
public static $user_agent = '';
/**
* @var string client IP address
*/
public static $client_ip = '0.0.0.0';
/**
* @var Request main request instance
*/
public static $initial;
/**
* @var Request currently executing request instance
*/
public static $current;
/**
* 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 Kohana_Request_Exception
* @uses Route::all
* @uses Route::matches
*/
public static function factory($uri = TRUE, 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'];
}
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['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
$protocol = 'https';
}
else
{
$protocol = 'http';
}
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']))
{
// 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 ($method !== '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->query($_GET);
$request->post($_POST);
if (isset($protocol))
{
// Set the request protocol
$request->protocol($protocol);
}
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;
}
/**
* Automatically detects the URI of the main request using PATH_INFO,
* REQUEST_URI, PHP_SELF or REDIRECT_URL.
*
* $uri = Request::detect_uri();
*
* @return string URI of the main request
* @throws Kohana_Exception
* @since 3.0.8
*/
public static function detect_uri()
{
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']))
{
/**
* We use REQUEST_URI as the fallback value. The reason
* for this is we might have a malformed URL such as:
*
* http://localhost/http://example.com/judge.php
*
* which parse_url can't handle. So rather than leave empty
* handed, we'll use this.
*/
$uri = $_SERVER['REQUEST_URI'];
if ($request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
{
// Valid URL path found, set it.
$uri = $request_uri;
}
// Decode the request URI
$uri = rawurldecode($uri);
}
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 http://dev.kohanaphp.com/projects/kohana3/issues
// along with any relevant information about your web server setup. Thanks!
throw new Kohana_Exception('Unable to detect the URI using PATH_INFO, REQUEST_URI, PHP_SELF or REDIRECT_URL');
}
// 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 = (string) substr($uri, strlen($base_url));
}
if (Kohana::$index_file AND strpos($uri, Kohana::$index_file) === 0)
{
// Remove the index file from the URI
$uri = (string) substr($uri, strlen(Kohana::$index_file));
}
}
return $uri;
}
/**
* Return the currently executing request. This is changed to the current
* request when [Request::execute] is called and restored when the request
* is completed.
*
* $request = Request::current();
*
* @return Request
* @since 3.0.5
*/
public static function current()
{
return Request::$current;
}
/**
* Returns the first request encountered by this framework. This will should
* only be set once during the first [Request::factory] invocation.
*
* // Get the first request
* $request = Request::initial();
*
* // Test whether the current request is the first request
* if (Request::initial() === Request::current())
* // Do something useful
*
* @return Request
* @since 3.1.0
*/
public static function initial()
{
return Request::$initial;
}
/**
* 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('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('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;
}
/**
* Returns the accepted content types. If a specific type is defined,
* the quality of that type will be returned.
*
* $types = Request::accept_type();
*
* @param string $type Content MIME type
* @return mixed An array of all types or a specific type as a string
* @uses Request::_parse_accept
*/
public static function accept_type($type = NULL)
{
static $accepts;
if ($accepts === NULL)
{
// Parse the HTTP_ACCEPT header
$accepts = Request::_parse_accept($_SERVER['HTTP_ACCEPT'], array('*/*' => 1.0));
}
if (isset($type))
{
// Return the quality setting for this type
return isset($accepts[$type]) ? $accepts[$type] : $accepts['*/*'];
}
return $accepts;
}
/**
* Returns the accepted languages. If a specific language is defined,
* the quality of that language will be returned. If the language is not
* accepted, FALSE will be returned.
*
* $langs = Request::accept_lang();
*
* @param string $lang Language code
* @return mixed An array of all types or a specific type as a string
* @uses Request::_parse_accept
*/
public static function accept_lang($lang = NULL)
{
static $accepts;
if ($accepts === NULL)
{
// Parse the HTTP_ACCEPT_LANGUAGE header
$accepts = Request::_parse_accept($_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
if (isset($lang))
{
// Return the quality setting for this lang
return isset($accepts[$lang]) ? $accepts[$lang] : FALSE;
}
return $accepts;
}
/**
* Returns the accepted encodings. If a specific encoding is defined,
* the quality of that encoding will be returned. If the encoding is not
* accepted, FALSE will be returned.
*
* $encodings = Request::accept_encoding();
*
* @param string $type Encoding type
* @return mixed An array of all types or a specific type as a string
* @uses Request::_parse_accept
*/
public static function accept_encoding($type = NULL)
{
static $accepts;
if ($accepts === NULL)
{
// Parse the HTTP_ACCEPT_LANGUAGE header
$accepts = Request::_parse_accept($_SERVER['HTTP_ACCEPT_ENCODING']);
}
if (isset($type))
{
// Return the quality setting for this type
return isset($accepts[$type]) ? $accepts[$type] : FALSE;
}
return $accepts;
}
/**
* Determines if a file larger than the post_max_size has been uploaded. PHP
* does not handle this situation gracefully on its own, so this method
* helps to solve that problem.
*
* @return boolean
* @uses Num::bytes
* @uses Arr::get
*/
public static function post_max_size_exceeded()
{
// Make sure the request method is POST
if (Request::$initial->method() !== HTTP_Request::POST)
return FALSE;
// Get the post_max_size in bytes
$max_bytes = Num::bytes(ini_get('post_max_size'));
// Error occurred if method is POST, and content length is too long
return (Arr::get($_SERVER, 'CONTENT_LENGTH') > $max_bytes);
}
/**
* Process URI
*
* @param string $uri URI
* @param array $routes Route
* @return array
*/
public static function process_uri($uri, $routes = NULL)
{
// Load routes
$routes = (empty($routes)) ? Route::all() : $routes;
$params = NULL;
foreach ($routes as $name => $route)
{
// We found something suitable
if ($params = $route->matches($uri))
{
return array(
'params' => $params,
'route' => $route,
);
}
}
return NULL;
}
/**
* Parses an accept header and returns an array (type => quality) of the
* accepted types, ordered by quality.
*
* $accept = Request::_parse_accept($header, $defaults);
*
* @param string $header Header to parse
* @param array $accepts Default values
* @return array
*/
protected static function _parse_accept( & $header, array $accepts = NULL)
{
if ( ! empty($header))
{
// Get all of the types
$types = explode(',', $header);
foreach ($types as $type)
{
// Split the type into parts
$parts = explode(';', $type);
// Make the type only the MIME
$type = trim(array_shift($parts));
// Default quality is 1.0
$quality = 1.0;
foreach ($parts as $part)
{
// Prevent undefined $value notice below
if (strpos($part, '=') === FALSE)
continue;
// Separate the key and value
list ($key, $value) = explode('=', trim($part));
if ($key === 'q')
{
// There is a quality for this type
$quality = (float) trim($value);
}
}
// Add the accept type and quality
$accepts[$type] = $quality;
}
}
// Make sure that accepts is an array
$accepts = (array) $accepts;
// Order by quality
arsort($accepts);
return $accepts;
}
/**
* @var string the x-requested-with header which most likely
* will be xmlhttprequest
*/
protected $_requested_with;
/**
* @var string method: GET, POST, PUT, DELETE, HEAD, etc
*/
protected $_method = 'GET';
/**
* @var string protocol: HTTP/1.1, FTP, CLI, etc
*/
protected $_protocol;
/**
* @var string referring URL
*/
protected $_referrer;
/**
* @var Route route matched for this request
*/
protected $_route;
/**
* @var Route array of routes to manually look at instead of the global namespace
*/
protected $_routes;
/**
* @var Kohana_Response response
*/
protected $_response;
/**
* @var Kohana_HTTP_Header headers to sent as part of the request
*/
protected $_header;
/**
* @var string the body
*/
protected $_body;
/**
* @var string controller directory
*/
protected $_directory = '';
/**
* @var string controller to be executed
*/
protected $_controller;
/**
* @var string action to be executed in the controller
*/
protected $_action;
/**
* @var string the URI of the request
*/
protected $_uri;
/**
* @var boolean external request
*/
protected $_external = FALSE;
/**
* @var array parameters from the route
*/
protected $_params = array();
/**
* @var array query parameters
*/
protected $_get = array();
/**
* @var array post parameters
*/
protected $_post = array();
/**
* @var array cookies to send with the request
*/
protected $_cookies = array();
/**
* @var Kohana_Request_Client
*/
protected $_client;
/**
* Creates a new request object for the given URI. New requests should be
* created using the [Request::instance] or [Request::factory] methods.
*
* $request = new Request($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 Kohana_Request_Exception
* @uses Route::all
* @uses Route::matches
*/
public function __construct($uri, Cache $cache = NULL, $injected_routes = array())
{
// Initialise the header
$this->_header = new HTTP_Header(array());
// Assign injected routes
$this->_injected_routes = $injected_routes;
// Detect protocol (if present)
/**
* @todo make this smarter, search for localhost etc
*/
if (strpos($uri, '://') === FALSE)
{
// Remove trailing slashes from the URI
$uri = trim($uri, '/');
$processed_uri = Request::process_uri($uri, $this->_injected_routes);
if ($processed_uri === NULL)
{
throw new HTTP_Exception_404('Unable to find a route to match the URI: :uri', array(
':uri' => $uri,
));
}
// Store the URI
$this->_uri = $uri;
// Store the matching route
$this->_route = $processed_uri['route'];
$params = $processed_uri['params'];
// Is this route external?
$this->_external = $this->_route->is_external();
if (isset($params['directory']))
{
// Controllers are in a sub-directory
$this->_directory = $params['directory'];
}
// Store the controller
$this->_controller = $params['controller'];
if (isset($params['action']))
{
// Store the action
$this->_action = $params['action'];
}
else
{
// Use the default action
$this->_action = Route::$default_action;
}
// These are accessible as public vars and can be overloaded
unset($params['controller'], $params['action'], $params['directory']);
// Params cannot be changed once matched
$this->_params = $params;
// Apply the client
$this->_client = new Request_Client_Internal(array('cache' => $cache));
}
else
{
// Create a route
$this->_route = new Route($uri);
// Store the URI
$this->_uri = $uri;
// Set external state
$this->_external = TRUE;
// Setup the client
$this->_client = new Request_Client_External(array('cache' => $cache));
}
}
/**
* Returns the response as the string representation of a request.
*
* echo $request;
*
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* Generates a relative URI for the current route.
*
* $request->uri($params);
*
* @param array $params Additional route parameters
* @return string
* @uses Route::uri
*/
public function uri(array $params = NULL)
{
if ( ! isset($params['directory']))
{
// Add the current directory
$params['directory'] = $this->_directory;
}
if ( ! isset($params['controller']))
{
// Add the current controller
$params['controller'] = $this->_controller;
}
if ( ! isset($params['action']))
{
// Add the current action
$params['action'] = $this->_action;
}
// Add the current parameters
$params += $this->_params;
return $this->_route->uri($params);
}
/**
* Create a URL from the current request. This is a shortcut for:
*
* echo URL::site($this->request->uri($params), $protocol);
*
* @param array $params URI parameters
* @param mixed $protocol protocol string or Request object
* @return string
* @since 3.0.7
* @uses URL::site
*/
public function url(array $params = NULL, $protocol = NULL)
{
// Create a URI with the current route and convert it to a URL
return URL::site($this->uri($params), $protocol);
}
/**
* Retrieves a value from the route parameters.
*
* $id = $request->param('id');
*
* @param string $key Key of the value
* @param mixed $default Default value if the key is not set
* @return mixed
*/
public function param($key = NULL, $default = NULL)
{
if ($key === NULL)
{
// Return the full array
return $this->_params;
}
return isset($this->_params[$key]) ? $this->_params[$key] : $default;
}
/**
* Sends the response status and all set headers. The current server
* protocol (HTTP/1.0 or HTTP/1.1) will be used when available. If not
* available, HTTP/1.1 will be used.
*
* $request->send_headers();
*
* @return $this
* @uses Request::$messages
*/
public function send_headers()
{
if ( ! headers_sent())
{
if (isset($_SERVER['SERVER_PROTOCOL']))
{
// Use the default server protocol
$protocol = $_SERVER['SERVER_PROTOCOL'];
}
else
{
// Default to using newer protocol
$protocol = 'HTTP/1.1';
}
// HTTP status line
header($protocol.' '.$this->status.' '.Request::$messages[$this->status]);
foreach ($this->headers as $name => $value)
{
if (is_string($name))
{
// Combine the name and value to make a raw header
$value = "{$name}: {$value}";
}
// Send the raw header
header($value, TRUE);
}
foreach (Session::$instances as $session)
{
// Sessions will most likely write cookies, which will be sent
// with the headers
$session->write();
}
}
return $this;
}
/**
* Redirects as the request response. If the URL does not include a
* protocol, it will be converted into a complete URL.
*
* $request->redirect($url);
*
* [!!] No further processing can be done after this method is called!
*
* @param string $url Redirect location
* @param integer $code Status code: 301, 302, etc
* @return void
* @uses URL::site
* @uses Request::send_headers
*/
public function redirect($url = '', $code = 302)
{
if (strpos($url, '://') === FALSE)
{
// Make the URI into a URL
$url = URL::site($url, TRUE);
}
// Redirect
$response = $this->create_response();
// Set the response status
$response->status($code);
// Set the location header
$response->headers('Location', $url);
// Send headers
$response->send_headers();
// Stop execution
exit;
}
/**
* Sets and gets the referrer from the request.
*
* @param string $referrer
* @return mixed
*/
public function referrer($referrer = NULL)
{
if ($referrer === NULL)
{
// Act as a getter
return $this->_referrer;
}
// Act as a setter
$this->_referrer = (string) $referrer;
return $this;
}
/**
* Sets and gets the route from the request.
*
* @param string $route
* @return mixed
*/
public function route(Route $route = NULL)
{
if ($route === NULL)
{
// Act as a getter
return $this->_route;
}
// Act as a setter
$this->_route = $route;
return $this;
}
/**
* Sets and gets the directory for the controller.
*
* @param string $directory Directory to execute the controller from
* @return mixed
*/
public function directory($directory = NULL)
{
if ($directory === NULL)
{
// Act as a getter
return $this->_directory;
}
// Act as a setter
$this->_directory = (string) $directory;
return $this;
}
/**
* Sets and gets the controller for the matched route.
*
* @param string $controller Controller to execute the action
* @return mixed
*/
public function controller($controller = NULL)
{
if ($controller === NULL)
{
// Act as a getter
return $this->_controller;
}
// Act as a setter
$this->_controller = (string) $controller;
return $this;
}
/**
* Sets and gets the action for the controller.
*
* @param string $action Action to execute the controller from
* @return mixed
*/
public function action($action = NULL)
{
if ($action === NULL)
{
// Act as a getter
return $this->_action;
}
// Act as a setter
$this->_action = (string) $action;
return $this;
}
/**
* Provides readonly access to the [Request_Client],
* useful for accessing the caching methods within the
* request client.
*
* @return Request_Client
*/
public function get_client()
{
return $this->_client;
}
/**
* Gets and sets the requested with property, which should
* be relative to the x-requested-with pseudo header.
*
* @param string $requested_with Requested with value
* @return mixed
*/
public function requested_with($requested_with = NULL)
{
if ($requested_with === NULL)
{
// Act as a getter
return $this->_requested_with;
}
// Act as a setter
$this->_requested_with = strtolower($requested_with);
return $this;
}
/**
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
*
* By default, the output from the controller is captured and returned, and
* no headers are sent.
*
* $request->execute();
*
* @return Response
* @throws Kohana_Exception
* @uses [Kohana::$profiling]
* @uses [Profiler]
*/
public function execute()
{
if ( ! $this->_client instanceof Kohana_Request_Client)
{
throw new Kohana_Request_Exception('Unable to execute :uri without a Kohana_Request_Client', array(
':uri' => $this->_uri,
));
}
return $this->_client->execute($this);
}
/**
* Returns whether this request is the initial request Kohana received.
* Can be used to test for sub requests.
*
* if ( ! $request->is_initial())
* // This is a sub request
*
* @return boolean
*/
public function is_initial()
{
return ($this === Request::$initial);
}
/**
* Returns whether this is an ajax request (as used by JS frameworks)
*
* @return boolean
*/
public function is_ajax()
{
return ($this->requested_with() === 'xmlhttprequest');
}
/**
* Generates an [ETag](http://en.wikipedia.org/wiki/HTTP_ETag) from the
* request response.
*
* $etag = $request->generate_etag();
*
* [!!] If the request response is empty when this method is called, an
* exception will be thrown!
*
* @return string
* @throws Kohana_Request_Exception
*/
public function generate_etag()
{
if ($this->_response === NULL)
{
throw new Kohana_Request_Exception('No response yet associated with request - cannot auto generate resource ETag');
}
// Generate a unique hash for the response
return '"'.sha1($this->_response).'"';
}
/**
* Set or get the response for this request
*
* @param Response $response Response to apply to this request
* @return Response
* @return void
*/
public function response(Response $response = NULL)
{
if ($response === NULL)
{
// Act as a getter
return $this->_response;
}
// Act as a setter
$this->_response = $response;
return $this;
}
/**
* Creates a response based on the type of request, i.e. an
* Request_HTTP will produce a Response_HTTP, and the same applies
* to CLI.
*
* // Create a response to the request
* $response = $request->create_response();
*
* @param boolean $bind Bind to this request
* @return Response
* @since 3.1.0
*/
public function create_response($bind = TRUE)
{
$response = new Response(array('_protocol' => $this->protocol()));
if ($bind)
{
// Bind a new response to the request
$this->_response = $response;
}
return $response;
}
/**
* Gets or sets the HTTP method. Usually GET, POST, PUT or DELETE in
* traditional CRUD applications.
*
* @param string $method Method to use for this request
* @return mixed
*/
public function method($method = NULL)
{
if ($method === NULL)
{
// Act as a getter
return $this->_method;
}
// Act as a setter
$this->_method = strtoupper($method);
return $this;
}
/**
* Gets or sets the HTTP protocol. The standard protocol to use
* is `http`.
*
* @param string $protocol Protocol to set to the request/response
* @return mixed
*/
public function protocol($protocol = NULL)
{
if ($protocol === NULL)
{
if ($this->_protocol)
{
// Act as a getter
return $this->_protocol;
}
else
{
// Get the default protocol
return HTTP::$protocol;
}
}
// Act as a setter
$this->_protocol = strtolower($protocol);
return $this;
}
/**
* Gets or sets HTTP headers to the request or response. All headers
* are included immediately after the HTTP protocol definition during
* transmission. This method provides a simple array or key/value
* interface to the headers.
*
* @param mixed $key Key or array of key/value pairs to set
* @param string $value Value to set to the supplied key
* @return mixed
*/
public function headers($key = NULL, $value = NULL)
{
if ($key instanceof HTTP_Header)
{
// Act a setter, replace all headers
$this->_header = $key;
return $this;
}
if (is_array($key))
{
// Act as a setter, replace all headers
$this->_header->exchangeArray($key);
return $this;
}
if ( ! $this->_header AND $this->is_initial())
{
// Lazy load the request headers
$this->_header = HTTP::request_headers();
}
if ($key === NULL)
{
// Act as a getter, return all headers
return $this->_header;
}
elseif ($value === NULL)
{
// Act as a getter, single header
return ($this->_header->offsetExists($key)) ? $this->_header->offsetGet($key) : NULL;
}
// Act as a setter for a single header
$this->_header[$key] = $value;
return $this;
}
/**
* Set and get cookies values for this request.
*
* @param mixed $key Cookie name, or array of cookie values
* @param string $value Value to set to cookie
* @return string
* @return mixed
*/
public function cookie($key = NULL, $value = NULL)
{
if (is_array($key))
{
// Act as a setter, replace all cookies
$this->_cookies = $key;
}
if ($key === NULL)
{
// Act as a getter, all cookies
return $this->_cookies;
}
elseif ($value === NULL)
{
// Act as a getting, single cookie
return isset($this->_cookies[$key]) ? $this->_cookies[$key] : NULL;
}
// Act as a setter for a single cookie
$this->_cookies[$key] = (string) $value;
return $this;
}
/**
* Gets or sets the HTTP body to the request or response. The body is
* included after the header, separated by a single empty new line.
*
* @param string $content Content to set to the object
* @return mixed
*/
public function body($content = NULL)
{
if ($content === NULL)
{
// Act as a getter
return $this->_body;
}
// Act as a setter
$this->_body = $content;
return $this;
}
/**
* Renders the HTTP_Interaction to a string, producing
*
* - Protocol
* - Headers
* - Body
*
* If there are variables set to the `Kohana_Request::$_post`
* they will override any values set to body.
*
* @param boolean $response Return the rendered response, else returns the rendered request
* @return string
*/
public function render($response = TRUE)
{
if ($response)
{
// Act as a getter
return (string) $this->_response;
}
if ( ! $this->_post)
{
$body = $this->_body;
}
else
{
$this->_header['content-type'] = 'application/x-www-form-urlencoded';
$body = HTTP::www_form_urlencode($this->_post);
}
if ( ! $this->_get)
{
$query_string = '';
}
else
{
$query_string = '?'.HTTP::www_form_urlencode($this->query());
}
// Prepare cookies
if ($this->_cookies)
{
$cookie_string = array();
// Parse each
foreach ($this->_cookies as $key => $value)
{
$cookie_string[] = $key.'='.$value;
}
// Create the cookie string
$this->_header['cookie'] = implode('; ', $cookie_string);
}
$output = $this->_method.' '.$this->uri($this->param()).$query_string.' '.$this->protocol()."\n";
$output .= (string) $this->_header;
$output .= $body;
return $output;
}
/**
* Gets or sets HTTP query string.
*
* @param mixed $key Key or key value pairs to set
* @param string $value Value to set to a key
* @return mixed
*/
public function query($key = NULL, $value = NULL)
{
if (is_array($key))
{
// Act as a setter, replace all query strings
$this->_get = $key;
return $this;
}
if ($key === NULL)
{
// Act as a getter, all query strings
return $this->_get;
}
elseif ($value === NULL)
{
// Act as a getter, single query string
return Arr::get($this->_get, $key);
}
// Act as a setter, single query string
$this->_get[$key] = $value;
return $this;
}
/**
* Gets or sets HTTP POST parameters to the request.
*
* @param mixed $key Key or key value pairs to set
* @param string $value Value to set to a key
* @return mixed
*/
public function post($key = NULL, $value = NULL)
{
if (is_array($key))
{
// Act as a setter, replace all fields
$this->_post = $key;
return $this;
}
if ($key === NULL)
{
// Act as a getter, all fields
return $this->_post;
}
elseif ($value === NULL)
{
// Act as a getter, single field
return Arr::get($this->_post, $key);
}
// Act as a setter, single field
$this->_post[$key] = $value;
return $this;
}
} // End Request
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/request.php | PHP | mit | 33,891 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Form helper class. Unless otherwise noted, all generated HTML will be made
* safe using the [HTML::chars] method. This prevents against simple XSS
* attacks that could otherwise be trigged by inserting HTML characters into
* form fields.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Form {
/**
* Generates an opening HTML form tag.
*
* // Form will submit back to the current page using POST
* echo Form::open();
*
* // Form will submit to 'search' using GET
* echo Form::open('search', array('method' => 'get'));
*
* // When "file" inputs are present, you must include the "enctype"
* echo Form::open(NULL, array('enctype' => 'multipart/form-data'));
*
* @param mixed form action, defaults to the current request URI, or [Request] class to use
* @param array html attributes
* @return string
* @uses Request::instance
* @uses URL::site
* @uses HTML::attributes
*/
public static function open($action = NULL, array $attributes = NULL)
{
if ($action instanceof Request)
{
// Use the current URI
$action = $action->uri();
}
if ($action === '')
{
// Use only the base URI
$action = Kohana::$base_url;
}
elseif (strpos($action, '://') === FALSE)
{
// Make the URI absolute
$action = URL::site($action);
}
// Add the form action to the attributes
$attributes['action'] = $action;
// Only accept the default character set
$attributes['accept-charset'] = Kohana::$charset;
if ( ! isset($attributes['method']))
{
// Use POST method
$attributes['method'] = 'post';
}
return '<form'.HTML::attributes($attributes).'>';
}
/**
* Creates the closing form tag.
*
* echo Form::close();
*
* @return string
*/
public static function close()
{
return '</form>';
}
/**
* Creates a form input. If no type is specified, a "text" type input will
* be returned.
*
* echo Form::input('username', $username);
*
* @param string input name
* @param string input value
* @param array html attributes
* @return string
* @uses HTML::attributes
*/
public static function input($name, $value = NULL, array $attributes = NULL)
{
// Set the input name
$attributes['name'] = $name;
// Set the input value
$attributes['value'] = $value;
if ( ! isset($attributes['type']))
{
// Default type is text
$attributes['type'] = 'text';
}
return '<input'.HTML::attributes($attributes).' />';
}
/**
* Creates a hidden form input.
*
* echo Form::hidden('csrf', $token);
*
* @param string input name
* @param string input value
* @param array html attributes
* @return string
* @uses Form::input
*/
public static function hidden($name, $value = NULL, array $attributes = NULL)
{
$attributes['type'] = 'hidden';
return Form::input($name, $value, $attributes);
}
/**
* Creates a password form input.
*
* echo Form::password('password');
*
* @param string input name
* @param string input value
* @param array html attributes
* @return string
* @uses Form::input
*/
public static function password($name, $value = NULL, array $attributes = NULL)
{
$attributes['type'] = 'password';
return Form::input($name, $value, $attributes);
}
/**
* Creates a file upload form input. No input value can be specified.
*
* echo Form::file('image');
*
* @param string input name
* @param array html attributes
* @return string
* @uses Form::input
*/
public static function file($name, array $attributes = NULL)
{
$attributes['type'] = 'file';
return Form::input($name, NULL, $attributes);
}
/**
* Creates a checkbox form input.
*
* echo Form::checkbox('remember_me', 1, (bool) $remember);
*
* @param string input name
* @param string input value
* @param boolean checked status
* @param array html attributes
* @return string
* @uses Form::input
*/
public static function checkbox($name, $value = NULL, $checked = FALSE, array $attributes = NULL)
{
$attributes['type'] = 'checkbox';
if ($checked === TRUE)
{
// Make the checkbox active
$attributes['checked'] = 'checked';
}
return Form::input($name, $value, $attributes);
}
/**
* Creates a radio form input.
*
* echo Form::radio('like_cats', 1, $cats);
* echo Form::radio('like_cats', 0, ! $cats);
*
* @param string input name
* @param string input value
* @param boolean checked status
* @param array html attributes
* @return string
* @uses Form::input
*/
public static function radio($name, $value = NULL, $checked = FALSE, array $attributes = NULL)
{
$attributes['type'] = 'radio';
if ($checked === TRUE)
{
// Make the radio active
$attributes['checked'] = 'checked';
}
return Form::input($name, $value, $attributes);
}
/**
* Creates a textarea form input.
*
* echo Form::textarea('about', $about);
*
* @param string textarea name
* @param string textarea body
* @param array html attributes
* @param boolean encode existing HTML characters
* @return string
* @uses HTML::attributes
* @uses HTML::chars
*/
public static function textarea($name, $body = '', array $attributes = NULL, $double_encode = TRUE)
{
// Set the input name
$attributes['name'] = $name;
// Add default rows and cols attributes (required)
$attributes += array('rows' => 10, 'cols' => 50);
return '<textarea'.HTML::attributes($attributes).'>'.HTML::chars($body, $double_encode).'</textarea>';
}
/**
* Creates a select form input.
*
* echo Form::select('country', $countries, $country);
*
* [!!] Support for multiple selected options was added in v3.0.7.
*
* @param string input name
* @param array available options
* @param mixed selected option string, or an array of selected options
* @param array html attributes
* @return string
* @uses HTML::attributes
*/
public static function select($name, array $options = NULL, $selected = NULL, array $attributes = NULL)
{
// Set the input name
$attributes['name'] = $name;
if (is_array($selected))
{
// This is a multi-select, god save us!
$attributes['multiple'] = 'multiple';
}
if ( ! is_array($selected))
{
if ($selected === NULL)
{
// Use an empty array
$selected = array();
}
else
{
// Convert the selected options to an array
$selected = array( (string) $selected);
}
}
if (empty($options))
{
// There are no options
$options = '';
}
else
{
foreach ($options as $value => $name)
{
if (is_array($name))
{
// Create a new optgroup
$group = array('label' => $value);
// Create a new list of options
$_options = array();
foreach ($name as $_value => $_name)
{
// Force value to be string
$_value = (string) $_value;
// Create a new attribute set for this option
$option = array('value' => $_value);
if (in_array($_value, $selected))
{
// This option is selected
$option['selected'] = 'selected';
}
// Change the option to the HTML string
$_options[] = '<option'.HTML::attributes($option).'>'.HTML::chars($_name, FALSE).'</option>';
}
// Compile the options into a string
$_options = "\n".implode("\n", $_options)."\n";
$options[$value] = '<optgroup'.HTML::attributes($group).'>'.$_options.'</optgroup>';
}
else
{
// Force value to be string
$value = (string) $value;
// Create a new attribute set for this option
$option = array('value' => $value);
if (in_array($value, $selected))
{
// This option is selected
$option['selected'] = 'selected';
}
// Change the option to the HTML string
$options[$value] = '<option'.HTML::attributes($option).'>'.HTML::chars($name, FALSE).'</option>';
}
}
// Compile the options into a single string
$options = "\n".implode("\n", $options)."\n";
}
return '<select'.HTML::attributes($attributes).'>'.$options.'</select>';
}
/**
* Creates a submit form input.
*
* echo Form::submit(NULL, 'Login');
*
* @param string input name
* @param string input value
* @param array html attributes
* @return string
* @uses Form::input
*/
public static function submit($name, $value, array $attributes = NULL)
{
$attributes['type'] = 'submit';
return Form::input($name, $value, $attributes);
}
/**
* Creates a image form input.
*
* echo Form::image(NULL, NULL, array('src' => 'media/img/login.png'));
*
* @param string input name
* @param string input value
* @param array html attributes
* @param boolean add index file to URL?
* @return string
* @uses Form::input
*/
public static function image($name, $value, array $attributes = NULL, $index = FALSE)
{
if ( ! empty($attributes['src']))
{
if (strpos($attributes['src'], '://') === FALSE)
{
// Add the base URL
$attributes['src'] = URL::base($index).$attributes['src'];
}
}
$attributes['type'] = 'image';
return Form::input($name, $value, $attributes);
}
/**
* Creates a button form input. Note that the body of a button is NOT escaped,
* to allow images and other HTML to be used.
*
* echo Form::button('save', 'Save Profile', array('type' => 'submit'));
*
* @param string input name
* @param string input value
* @param array html attributes
* @return string
* @uses HTML::attributes
*/
public static function button($name, $body, array $attributes = NULL)
{
// Set the input name
$attributes['name'] = $name;
return '<button'.HTML::attributes($attributes).'>'.$body.'</button>';
}
/**
* Creates a form label. Label text is not automatically translated.
*
* echo Form::label('username', 'Username');
*
* @param string target input
* @param string label text
* @param array html attributes
* @return string
* @uses HTML::attributes
*/
public static function label($input, $text = NULL, array $attributes = NULL)
{
if ($text === NULL)
{
// Use the input name as the text
$text = ucwords(preg_replace('/[\W_]+/', ' ', $input));
}
// Set the label target
$attributes['for'] = $input;
return '<label'.HTML::attributes($attributes).'>'.$text.'</label>';
}
} // End form
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/form.php | PHP | mit | 10,750 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Internationalization (i18n) class. Provides language loading and translation
* methods without dependencies on [gettext](http://php.net/gettext).
*
* Typically this class would never be used directly, but used via the __()
* function, which loads the message and replaces parameters:
*
* // Display a translated message
* echo __('Hello, world');
*
* // With parameter replacement
* echo __('Hello, :user', array(':user' => $username));
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_I18n {
/**
* @var string target language: en-us, es-es, zh-cn, etc
*/
public static $lang = 'en-us';
/**
* @var string source language: en-us, es-es, zh-cn, etc
*/
public static $source = 'en-us';
/**
* @var array cache of loaded languages
*/
protected static $_cache = array();
/**
* Get and set the target language.
*
* // Get the current language
* $lang = I18n::lang();
*
* // Change the current language to Spanish
* I18n::lang('es-es');
*
* @param string new language setting
* @return string
* @since 3.0.2
*/
public static function lang($lang = NULL)
{
if ($lang)
{
// Normalize the language
I18n::$lang = strtolower(str_replace(array(' ', '_'), '-', $lang));
}
return I18n::$lang;
}
/**
* Returns translation of a string. If no translation exists, the original
* string will be returned. No parameters are replaced.
*
* $hello = I18n::get('Hello friends, my name is :name');
*
* @param string text to translate
* @param string target language
* @return string
*/
public static function get($string, $lang = NULL)
{
if ( ! $lang)
{
// Use the global target language
$lang = I18n::$lang;
}
// Load the translation table for this language
$table = I18n::load($lang);
// Return the translated string if it exists
return isset($table[$string]) ? $table[$string] : $string;
}
/**
* Returns the translation table for a given language.
*
* // Get all defined Spanish messages
* $messages = I18n::load('es-es');
*
* @param string language to load
* @return array
*/
public static function load($lang)
{
if (isset(I18n::$_cache[$lang]))
{
return I18n::$_cache[$lang];
}
// New translation table
$table = array();
// Split the language: language, region, locale, etc
$parts = explode('-', $lang);
do
{
// Create a path for this set of parts
$path = implode(DIRECTORY_SEPARATOR, $parts);
if ($files = Kohana::find_file('i18n', $path, NULL, TRUE))
{
$t = array();
foreach ($files as $file)
{
// Merge the language strings into the sub table
$t = array_merge($t, Kohana::load($file));
}
// Append the sub table, preventing less specific language
// files from overloading more specific files
$table += $t;
}
// Remove the last part
array_pop($parts);
}
while ($parts);
// Cache the translation table locally
return I18n::$_cache[$lang] = $table;
}
} // End I18n
if ( ! function_exists('__'))
{
/**
* Kohana translation/internationalization function. The PHP function
* [strtr](http://php.net/strtr) is used for replacing parameters.
*
* __('Welcome back, :user', array(':user' => $username));
*
* [!!] The target language is defined by [I18n::$lang].
*
* @uses I18n::get
* @param string text to translate
* @param array values to replace in the translated text
* @param string source language
* @return string
*/
function __($string, array $values = NULL, $lang = 'en-us')
{
if ($lang !== I18n::$lang)
{
// The message and target languages are different
// Get the translation for this message
$string = I18n::get($string);
}
return empty($values) ? $string : strtr($string, $values);
}
}
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/i18n.php | PHP | mit | 4,045 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Array helper.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Arr {
/**
* @var string default delimiter for path()
*/
public static $delimiter = '.';
/**
* Tests if an array is associative or not.
*
* // Returns TRUE
* Arr::is_assoc(array('username' => 'john.doe'));
*
* // Returns FALSE
* Arr::is_assoc('foo', 'bar');
*
* @param array array to check
* @return boolean
*/
public static function is_assoc(array $array)
{
// Keys of the array
$keys = array_keys($array);
// If the array keys of the keys match the keys, then the array must
// not be associative (e.g. the keys array looked like {0:0, 1:1...}).
return array_keys($keys) !== $keys;
}
/**
* Test if a value is an array with an additional check for array-like objects.
*
* // Returns TRUE
* Arr::is_array(array());
* Arr::is_array(new ArrayObject);
*
* // Returns FALSE
* Arr::is_array(FALSE);
* Arr::is_array('not an array!');
* Arr::is_array(Database::instance());
*
* @param mixed value to check
* @return boolean
*/
public static function is_array($value)
{
if (is_array($value))
{
// Definitely an array
return TRUE;
}
else
{
// Possibly a Traversable object, functionally the same as an array
return (is_object($value) AND $value instanceof Traversable);
}
}
/**
* Gets a value from an array using a dot separated path.
*
* // Get the value of $array['foo']['bar']
* $value = Arr::path($array, 'foo.bar');
*
* Using a wildcard "*" will search intermediate arrays and return an array.
*
* // Get the values of "color" in theme
* $colors = Arr::path($array, 'theme.*.color');
*
* // Using an array of keys
* $colors = Arr::path($array, array('theme', '*', 'color'));
*
* @param array array to search
* @param mixed key path string (delimiter separated) or array of keys
* @param mixed default value if the path is not set
* @param string key path delimiter
* @return mixed
*/
public static function path($array, $path, $default = NULL, $delimiter = NULL)
{
if ( ! Arr::is_array($array))
{
// This is not an array!
return $default;
}
if (is_array($path))
{
// The path has already been separated into keys
$keys = $path;
}
else
{
if (array_key_exists($path, $array))
{
// No need to do extra processing
return $array[$path];
}
if ($delimiter === NULL)
{
// Use the default delimiter
$delimiter = Arr::$delimiter;
}
// Remove starting delimiters and spaces
$path = ltrim($path, "{$delimiter} ");
// Remove ending delimiters, spaces, and wildcards
$path = rtrim($path, "{$delimiter} *");
// Split the keys by delimiter
$keys = explode($delimiter, $path);
}
do
{
$key = array_shift($keys);
if (ctype_digit($key))
{
// Make the key an integer
$key = (int) $key;
}
if (isset($array[$key]))
{
if ($keys)
{
if (Arr::is_array($array[$key]))
{
// Dig down into the next part of the path
$array = $array[$key];
}
else
{
// Unable to dig deeper
break;
}
}
else
{
// Found the path requested
return $array[$key];
}
}
elseif ($key === '*')
{
// Handle wildcards
$values = array();
foreach ($array as $arr)
{
if ($value = Arr::path($arr, implode('.', $keys)))
{
$values[] = $value;
}
}
if ($values)
{
// Found the values requested
return $values;
}
else
{
// Unable to dig deeper
break;
}
}
else
{
// Unable to dig deeper
break;
}
}
while ($keys);
// Unable to find the value requested
return $default;
}
/**
* Set a value on an array by path.
*
* @see Arr::path()
* @param array $array Array to update
* @param string $path Path
* @param mixed $value Value to set
* @param string $delimiter Path delimiter
*/
public static function set_path( & $array, $path, $value, $delimiter = NULL)
{
if ( ! $delimiter)
{
// Use the default delimiter
$delimiter = Arr::$delimiter;
}
// Split the keys by delimiter
$keys = explode($delimiter, $path);
// Set current $array to inner-most array path
while (count($keys) > 1)
{
$key = array_shift($keys);
if (ctype_digit($key))
{
// Make the key an integer
$key = (int) $key;
}
if ( ! isset($array[$key]))
{
$array[$key] = array();
}
$array = & $array[$key];
}
// Set key on inner-most array
$array[array_shift($keys)] = $value;
}
/**
* Fill an array with a range of numbers.
*
* // Fill an array with values 5, 10, 15, 20
* $values = Arr::range(5, 20);
*
* @param integer stepping
* @param integer ending number
* @return array
*/
public static function range($step = 10, $max = 100)
{
if ($step < 1)
return array();
$array = array();
for ($i = $step; $i <= $max; $i += $step)
{
$array[$i] = $i;
}
return $array;
}
/**
* Retrieve a single key from an array. If the key does not exist in the
* array, the default value will be returned instead.
*
* // Get the value "username" from $_POST, if it exists
* $username = Arr::get($_POST, 'username');
*
* // Get the value "sorting" from $_GET, if it exists
* $sorting = Arr::get($_GET, 'sorting');
*
* @param array array to extract from
* @param string key name
* @param mixed default value
* @return mixed
*/
public static function get($array, $key, $default = NULL)
{
return isset($array[$key]) ? $array[$key] : $default;
}
/**
* Retrieves multiple keys from an array. If the key does not exist in the
* array, the default value will be added instead.
*
* // Get the values "username", "password" from $_POST
* $auth = Arr::extract($_POST, array('username', 'password'));
*
* @param array array to extract keys from
* @param array list of key names
* @param mixed default value
* @return array
*/
public static function extract($array, array $keys, $default = NULL)
{
$found = array();
foreach ($keys as $key)
{
$found[$key] = isset($array[$key]) ? $array[$key] : $default;
}
return $found;
}
/**
* Retrieves muliple single-key values from a list of arrays.
*
* // Get all of the "id" values from a result
* $ids = Arr::pluck($result, 'id');
*
* [!!] A list of arrays is an array that contains arrays, eg: array(array $a, array $b, array $c, ...)
*
* @param array list of arrays to check
* @param string key to pluck
* @return array
*/
public static function pluck($array, $key)
{
$values = array();
foreach ($array as $row)
{
if (isset($row[$key]))
{
// Found a value in this row
$values[] = $row[$key];
}
}
return $values;
}
/**
* Adds a value to the beginning of an associative array.
*
* // Add an empty value to the start of a select list
* Arr::unshift($array, 'none', 'Select a value');
*
* @param array array to modify
* @param string array key name
* @param mixed array value
* @return array
*/
public static function unshift( array & $array, $key, $val)
{
$array = array_reverse($array, TRUE);
$array[$key] = $val;
$array = array_reverse($array, TRUE);
return $array;
}
/**
* Recursive version of [array_map](http://php.net/array_map), applies the
* same callback to all elements in an array, including sub-arrays.
*
* // Apply "strip_tags" to every element in the array
* $array = Arr::map('strip_tags', $array);
*
* [!!] Unlike `array_map`, this method requires a callback and will only map
* a single array.
*
* @param mixed callback applied to every element in the array
* @param array array to map
* @return array
*/
public static function map($callback, $array)
{
foreach ($array as $key => $val)
{
if (is_array($val))
{
$array[$key] = Arr::map($callback, $val);
}
else
{
$array[$key] = call_user_func($callback, $val);
}
}
return $array;
}
/**
* Merges one or more arrays recursively and preserves all keys.
* Note that this does not work the same as [array_merge_recursive](http://php.net/array_merge_recursive)!
*
* $john = array('name' => 'john', 'children' => array('fred', 'paul', 'sally', 'jane'));
* $mary = array('name' => 'mary', 'children' => array('jane'));
*
* // John and Mary are married, merge them together
* $john = Arr::merge($john, $mary);
*
* // The output of $john will now be:
* array('name' => 'mary', 'children' => array('fred', 'paul', 'sally', 'jane'))
*
* @param array initial array
* @param array array to merge
* @param array ...
* @return array
*/
public static function merge(array $a1, array $a2)
{
$result = array();
for ($i = 0, $total = func_num_args(); $i < $total; $i++)
{
// Get the next array
$arr = func_get_arg($i);
// Is the array associative?
$assoc = Arr::is_assoc($arr);
foreach ($arr as $key => $val)
{
if (isset($result[$key]))
{
if (is_array($val) AND is_array($result[$key]))
{
if (Arr::is_assoc($val))
{
// Associative arrays are merged recursively
$result[$key] = Arr::merge($result[$key], $val);
}
else
{
// Find the values that are not already present
$diff = array_diff($val, $result[$key]);
// Indexed arrays are merged to prevent duplicates
$result[$key] = array_merge($result[$key], $diff);
}
}
else
{
if ($assoc)
{
// Associative values are replaced
$result[$key] = $val;
}
elseif ( ! in_array($val, $result, TRUE))
{
// Indexed values are added only if they do not yet exist
$result[] = $val;
}
}
}
else
{
// New values are added
$result[$key] = $val;
}
}
}
return $result;
}
/**
* Overwrites an array with values from input arrays.
* Keys that do not exist in the first array will not be added!
*
* $a1 = array('name' => 'john', 'mood' => 'happy', 'food' => 'bacon');
* $a2 = array('name' => 'jack', 'food' => 'tacos', 'drink' => 'beer');
*
* // Overwrite the values of $a1 with $a2
* $array = Arr::overwrite($a1, $a2);
*
* // The output of $array will now be:
* array('name' => 'jack', 'mood' => 'happy', 'food' => 'tacos')
*
* @param array master array
* @param array input arrays that will overwrite existing values
* @return array
*/
public static function overwrite($array1, $array2)
{
foreach (array_intersect_key($array2, $array1) as $key => $value)
{
$array1[$key] = $value;
}
if (func_num_args() > 2)
{
foreach (array_slice(func_get_args(), 2) as $array2)
{
foreach (array_intersect_key($array2, $array1) as $key => $value)
{
$array1[$key] = $value;
}
}
}
return $array1;
}
/**
* Creates a callable function and parameter list from a string representation.
* Note that this function does not validate the callback string.
*
* // Get the callback function and parameters
* list($func, $params) = Arr::callback('Foo::bar(apple,orange)');
*
* // Get the result of the callback
* $result = call_user_func_array($func, $params);
*
* @param string callback string
* @return array function, params
*/
public static function callback($str)
{
// Overloaded as parts are found
$command = $params = NULL;
// command[param,param]
if (preg_match('/^([^\(]*+)\((.*)\)$/', $str, $match))
{
// command
$command = $match[1];
if ($match[2] !== '')
{
// param,param
$params = preg_split('/(?<!\\\\),/', $match[2]);
$params = str_replace('\,', ',', $params);
}
}
else
{
// command
$command = $str;
}
if (strpos($command, '::') !== FALSE)
{
// Create a static method callable command
$command = explode('::', $command, 2);
}
return array($command, $params);
}
/**
* Convert a multi-dimensional array into a single-dimensional array.
*
* $array = array('set' => array('one' => 'something'), 'two' => 'other');
*
* // Flatten the array
* $array = Arr::flatten($array);
*
* // The array will now be
* array('one' => 'something', 'two' => 'other');
*
* [!!] The keys of array values will be discarded.
*
* @param array array to flatten
* @return array
* @since 3.0.6
*/
public static function flatten($array)
{
$flat = array();
foreach ($array as $key => $value)
{
if (is_array($value))
{
$flat += Arr::flatten($value);
}
else
{
$flat[$key] = $value;
}
}
return $flat;
}
} // End arr
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/arr.php | PHP | mit | 13,199 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* HTML helper class. Provides generic methods for generating various HTML
* tags and making output HTML safe.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_HTML {
/**
* @var array preferred order of attributes
*/
public static $attribute_order = array
(
'action',
'method',
'type',
'id',
'name',
'value',
'href',
'src',
'width',
'height',
'cols',
'rows',
'size',
'maxlength',
'rel',
'media',
'accept-charset',
'accept',
'tabindex',
'accesskey',
'alt',
'title',
'class',
'style',
'selected',
'checked',
'readonly',
'disabled',
);
/**
* @var boolean automatically target external URLs to a new window?
*/
public static $windowed_urls = FALSE;
/**
* Convert special characters to HTML entities. All untrusted content
* should be passed through this method to prevent XSS injections.
*
* echo HTML::chars($username);
*
* @param string string to convert
* @param boolean encode existing entities
* @return string
*/
public static function chars($value, $double_encode = TRUE)
{
return htmlspecialchars( (string) $value, ENT_QUOTES, Kohana::$charset, $double_encode);
}
/**
* Convert all applicable characters to HTML entities. All characters
* that cannot be represented in HTML with the current character set
* will be converted to entities.
*
* echo HTML::entities($username);
*
* @param string string to convert
* @param boolean encode existing entities
* @return string
*/
public static function entities($value, $double_encode = TRUE)
{
return htmlentities( (string) $value, ENT_QUOTES, Kohana::$charset, $double_encode);
}
/**
* Create HTML link anchors. Note that the title is not escaped, to allow
* HTML elements within links (images, etc).
*
* echo HTML::anchor('/user/profile', 'My Profile');
*
* @param string URL or URI string
* @param string link text
* @param array HTML anchor attributes
* @param mixed protocol to pass to URL::base()
* @param boolean include the index page
* @return string
* @uses URL::base
* @uses URL::site
* @uses HTML::attributes
*/
public static function anchor($uri, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
if ($title === NULL)
{
// Use the URI as the title
$title = $uri;
}
if ($uri === '')
{
// Only use the base URL
$uri = URL::base($protocol, $index);
}
else
{
if (strpos($uri, '://') !== FALSE)
{
if (HTML::$windowed_urls === TRUE AND empty($attributes['target']))
{
// Make the link open in a new window
$attributes['target'] = '_blank';
}
}
elseif ($uri[0] !== '#')
{
// Make the URI absolute for non-id anchors
$uri = URL::site($uri, $protocol, $index);
}
}
// Add the sanitized link to the attributes
$attributes['href'] = $uri;
return '<a'.HTML::attributes($attributes).'>'.$title.'</a>';
}
/**
* Creates an HTML anchor to a file. Note that the title is not escaped,
* to allow HTML elements within links (images, etc).
*
* echo HTML::file_anchor('media/doc/user_guide.pdf', 'User Guide');
*
* @param string name of file to link to
* @param string link text
* @param array HTML anchor attributes
* @param mixed protocol to pass to URL::base()
* @param boolean include the index page
* @return string
* @uses URL::base
* @uses HTML::attributes
*/
public static function file_anchor($file, $title = NULL, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
if ($title === NULL)
{
// Use the file name as the title
$title = basename($file);
}
// Add the file link to the attributes
$attributes['href'] = URL::base($protocol, $index).$file;
return '<a'.HTML::attributes($attributes).'>'.$title.'</a>';
}
/**
* Generates an obfuscated version of a string. Text passed through this
* method is less likely to be read by web crawlers and robots, which can
* be helpful for spam prevention, but can prevent legitimate robots from
* reading your content.
*
* echo HTML::obfuscate($text);
*
* @param string string to obfuscate
* @return string
* @since 3.0.3
*/
public static function obfuscate($string)
{
$safe = '';
foreach (str_split($string) as $letter)
{
switch (rand(1, 3))
{
// HTML entity code
case 1:
$safe .= '&#'.ord($letter).';';
break;
// Hex character code
case 2:
$safe .= '&#x'.dechex(ord($letter)).';';
break;
// Raw (no) encoding
case 3:
$safe .= $letter;
}
}
return $safe;
}
/**
* Generates an obfuscated version of an email address. Helps prevent spam
* robots from finding email addresses.
*
* echo HTML::email($address);
*
* @param string email address
* @return string
* @uses HTML::obfuscate
*/
public static function email($email)
{
// Make sure the at sign is always obfuscated
return str_replace('@', '@', HTML::obfuscate($email));
}
/**
* Creates an email (mailto:) anchor. Note that the title is not escaped,
* to allow HTML elements within links (images, etc).
*
* echo HTML::mailto($address);
*
* @param string email address to send to
* @param string link text
* @param array HTML anchor attributes
* @return string
* @uses HTML::email
* @uses HTML::attributes
*/
public static function mailto($email, $title = NULL, array $attributes = NULL)
{
// Obfuscate email address
$email = HTML::email($email);
if ($title === NULL)
{
// Use the email address as the title
$title = $email;
}
return '<a href="mailto:'.$email.'"'.HTML::attributes($attributes).'>'.$title.'</a>';
}
/**
* Creates a style sheet link element.
*
* echo HTML::style('media/css/screen.css');
*
* @param string file name
* @param array default attributes
* @param mixed protocol to pass to URL::base()
* @param boolean include the index page
* @return string
* @uses URL::base
* @uses HTML::attributes
*/
public static function style($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
if (strpos($file, '://') === FALSE)
{
// Add the base URL
$file = URL::base($protocol, $index).$file;
}
// Set the stylesheet link
$attributes['href'] = $file;
// Set the stylesheet rel
$attributes['rel'] = 'stylesheet';
// Set the stylesheet type
$attributes['type'] = 'text/css';
return '<link'.HTML::attributes($attributes).' />';
}
/**
* Creates a script link.
*
* echo HTML::script('media/js/jquery.min.js');
*
* @param string file name
* @param array default attributes
* @param mixed protocol to pass to URL::base()
* @param boolean include the index page
* @return string
* @uses URL::base
* @uses HTML::attributes
*/
public static function script($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
if (strpos($file, '://') === FALSE)
{
// Add the base URL
$file = URL::base($protocol, $index).$file;
}
// Set the script link
$attributes['src'] = $file;
// Set the script type
$attributes['type'] = 'text/javascript';
return '<script'.HTML::attributes($attributes).'></script>';
}
/**
* Creates a image link.
*
* echo HTML::image('media/img/logo.png', array('alt' => 'My Company'));
*
* @param string file name
* @param array default attributes
* @param mixed protocol to pass to URL::base()
* @param boolean include the index page
* @return string
* @uses URL::base
* @uses HTML::attributes
*/
public static function image($file, array $attributes = NULL, $protocol = NULL, $index = FALSE)
{
if (strpos($file, '://') === FALSE)
{
// Add the base URL
$file = URL::base($protocol, $index).$file;
}
// Add the image link
$attributes['src'] = $file;
return '<img'.HTML::attributes($attributes).' />';
}
/**
* Compiles an array of HTML attributes into an attribute string.
* Attributes will be sorted using HTML::$attribute_order for consistency.
*
* echo '<div'.HTML::attributes($attrs).'>'.$content.'</div>';
*
* @param array attribute list
* @return string
*/
public static function attributes(array $attributes = NULL)
{
if (empty($attributes))
return '';
$sorted = array();
foreach (HTML::$attribute_order as $key)
{
if (isset($attributes[$key]))
{
// Add the attribute to the sorted list
$sorted[$key] = $attributes[$key];
}
}
// Combine the sorted attributes
$attributes = $sorted + $attributes;
$compiled = '';
foreach ($attributes as $key => $value)
{
if ($value === NULL)
{
// Skip attributes that have NULL values
continue;
}
if (is_int($key))
{
// Assume non-associative keys are mirrored attributes
$key = $value;
}
// Add the attribute value
$compiled .= ' '.$key.'="'.HTML::chars($value).'"';
}
return $compiled;
}
} // End html
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/html.php | PHP | mit | 9,364 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Message logging with observer-based log writing.
*
* [!!] This class does not support extensions, only additional writers.
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Log {
// Log message levels
const EMERGENCY = LOG_EMERG; // 0
const ALERT = LOG_ALERT; // 1
const CRITICAL = LOG_CRIT; // 2
const ERROR = LOG_ERR; // 3
const WARNING = LOG_WARNING; // 4
const NOTICE = LOG_NOTICE; // 5
const INFO = LOG_INFO; // 6
const DEBUG = LOG_DEBUG; // 7
/**
* @var string timestamp format for log entries
*/
public static $timestamp = 'Y-m-d H:i:s';
/**
* @var string timezone for log entries
*/
public static $timezone;
/**
* @var boolean immediately write when logs are added
*/
public static $write_on_add = FALSE;
/**
* @var Log Singleton instance container
*/
protected static $_instance;
/**
* Get the singleton instance of this class and enable writing at shutdown.
*
* $log = Log::instance();
*
* @return Log
*/
public static function instance()
{
if (Log::$_instance === NULL)
{
// Create a new instance
Log::$_instance = new Log;
// Write the logs at shutdown
register_shutdown_function(array(Log::$_instance, 'write'));
}
return Log::$_instance;
}
/**
* @var array list of added messages
*/
protected $_messages = array();
/**
* @var array list of log writers
*/
protected $_writers = array();
/**
* Attaches a log writer, and optionally limits the levels of messages that
* will be written by the writer.
*
* $log->attach($writer);
*
* @param object Log_Writer instance
* @param mixed array of messages levels to write OR max level to write
* @param integer min level to write IF $levels is not an array
* @return Log
*/
public function attach(Log_Writer $writer, $levels = array(), $min_level = 0)
{
if ( ! is_array($levels))
{
$levels = range($min_level, $levels);
}
$this->_writers["{$writer}"] = array
(
'object' => $writer,
'levels' => $levels
);
return $this;
}
/**
* Detaches a log writer. The same writer object must be used.
*
* $log->detach($writer);
*
* @param object Log_Writer instance
* @return Log
*/
public function detach(Log_Writer $writer)
{
// Remove the writer
unset($this->_writers["{$writer}"]);
return $this;
}
/**
* Adds a message to the log. Replacement values must be passed in to be
* replaced using [strtr](http://php.net/strtr).
*
* $log->add(Log::ERROR, 'Could not locate user: :user', array(
* ':user' => $username,
* ));
*
* @param string level of message
* @param string message body
* @param array values to replace in the message
* @return Log
*/
public function add($level, $message, array $values = NULL)
{
if ($values)
{
// Insert the values into the message
$message = strtr($message, $values);
}
// Create a new message and timestamp it
$this->_messages[] = array
(
'time' => Date::formatted_time('now', Log::$timestamp, Log::$timezone),
'level' => $level,
'body' => $message,
);
if (Log::$write_on_add)
{
// Write logs as they are added
$this->write();
}
return $this;
}
/**
* Write and clear all of the messages.
*
* $log->write();
*
* @return void
*/
public function write()
{
if (empty($this->_messages))
{
// There is nothing to write, move along
return;
}
// Import all messages locally
$messages = $this->_messages;
// Reset the messages array
$this->_messages = array();
foreach ($this->_writers as $writer)
{
if (empty($writer['levels']))
{
// Write all of the messages
$writer['object']->write($messages);
}
else
{
// Filtered messages
$filtered = array();
foreach ($messages as $message)
{
if (in_array($message['level'], $writer['levels']))
{
// Writer accepts this kind of message
$filtered[] = $message;
}
}
// Write the filtered messages
$writer['object']->write($filtered);
}
}
}
} // End Kohana_Log
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/log.php | PHP | mit | 4,365 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Abstract controller class. Controllers should only be created using a [Request].
*
* Controllers methods will be automatically called in the following order by
* the request:
*
* $controller = new Controller_Foo($request);
* $controller->before();
* $controller->action_bar();
* $controller->after();
*
* The controller action should add the output it creates to
* `$this->response->body($output)`, typically in the form of a [View], during the
* "action" part of execution.
*
* @package Kohana
* @category Controller
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
abstract class Kohana_Controller {
/**
* @var Request Request that created the controller
*/
public $request;
/**
* @var Response The response that will be returned from controller
*/
public $response;
/**
* Creates a new controller instance. Each controller must be constructed
* with the request object that created it.
*
* @param Request $request Request that created the controller
* @param Response $response The request's response
* @return void
*/
public function __construct(Request $request, Response $response)
{
// Assign the request to the controller
$this->request = $request;
// Assign a response to the controller
$this->response = $response;
}
/**
* Automatically executed before the controller action. Can be used to set
* class properties, do authorization checks, and execute other custom code.
*
* @return void
*/
public function before()
{
// Nothing by default
}
/**
* Automatically executed after the controller action. Can be used to apply
* transformation to the request response, add extra output, and execute
* other custom code.
*
* @return void
*/
public function after()
{
// Nothing by default
}
} // End Controller
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/controller.php | PHP | mit | 1,984 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Inflector helper class. Inflection is changing the form of a word based on
* the context it is used in. For example, changing a word into a plural form.
*
* [!!] Inflection is only tested with English, and is will not work with other languages.
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Inflector {
/**
* @var array cached inflections
*/
protected static $cache = array();
/**
* @var array uncountable words
*/
protected static $uncountable;
/**
* @var array irregular words
*/
protected static $irregular;
/**
* Checks if a word is defined as uncountable. An uncountable word has a
* single form. For instance, one "fish" and many "fish", not "fishes".
*
* Inflector::uncountable('fish'); // TRUE
* Inflector::uncountable('cat'); // FALSE
*
* If you find a word is being pluralized improperly, it has probably not
* been defined as uncountable in `config/inflector.php`. If this is the
* case, please report [an issue](http://dev.kohanaphp.com/projects/kohana3/issues).
*
* @param string word to check
* @return boolean
*/
public static function uncountable($str)
{
if (Inflector::$uncountable === NULL)
{
// Cache uncountables
Inflector::$uncountable = Kohana::config('inflector')->uncountable;
// Make uncountables mirrored
Inflector::$uncountable = array_combine(Inflector::$uncountable, Inflector::$uncountable);
}
return isset(Inflector::$uncountable[strtolower($str)]);
}
/**
* Makes a plural word singular.
*
* echo Inflector::singular('cats'); // "cat"
* echo Inflector::singular('fish'); // "fish", uncountable
*
* You can also provide the count to make inflection more intelligent.
* In this case, it will only return the singular value if the count is
* greater than one and not zero.
*
* echo Inflector::singular('cats', 2); // "cats"
*
* [!!] Special inflections are defined in `config/inflector.php`.
*
* @param string word to singularize
* @param integer count of thing
* @return string
* @uses Inflector::uncountable
*/
public static function singular($str, $count = NULL)
{
// $count should always be a float
$count = ($count === NULL) ? 1.0 : (float) $count;
// Do nothing when $count is not 1
if ($count != 1)
return $str;
// Remove garbage
$str = strtolower(trim($str));
// Cache key name
$key = 'singular_'.$str.$count;
if (isset(Inflector::$cache[$key]))
return Inflector::$cache[$key];
if (Inflector::uncountable($str))
return Inflector::$cache[$key] = $str;
if (empty(Inflector::$irregular))
{
// Cache irregular words
Inflector::$irregular = Kohana::config('inflector')->irregular;
}
if ($irregular = array_search($str, Inflector::$irregular))
{
$str = $irregular;
}
elseif (preg_match('/us$/', $str))
{
// http://en.wikipedia.org/wiki/Plural_form_of_words_ending_in_-us
// Already singular, do nothing
}
elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str))
{
// Remove "es"
$str = substr($str, 0, -2);
}
elseif (preg_match('/[^aeiou]ies$/', $str))
{
// Replace "ies" with "y"
$str = substr($str, 0, -3).'y';
}
elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss')
{
// Remove singular "s"
$str = substr($str, 0, -1);
}
return Inflector::$cache[$key] = $str;
}
/**
* Makes a singular word plural.
*
* echo Inflector::plural('fish'); // "fish", uncountable
* echo Inflector::plural('cat'); // "cats"
*
* You can also provide the count to make inflection more intelligent.
* In this case, it will only return the plural value if the count is
* not one.
*
* echo Inflector::singular('cats', 3); // "cats"
*
* [!!] Special inflections are defined in `config/inflector.php`.
*
* @param string word to pluralize
* @param integer count of thing
* @return string
* @uses Inflector::uncountable
*/
public static function plural($str, $count = NULL)
{
// $count should always be a float
$count = ($count === NULL) ? 0.0 : (float) $count;
// Do nothing with singular
if ($count == 1)
return $str;
// Remove garbage
$str = trim($str);
// Cache key name
$key = 'plural_'.$str.$count;
// Check uppercase
$is_uppercase = ctype_upper($str);
if (isset(Inflector::$cache[$key]))
return Inflector::$cache[$key];
if (Inflector::uncountable($str))
return Inflector::$cache[$key] = $str;
if (empty(Inflector::$irregular))
{
// Cache irregular words
Inflector::$irregular = Kohana::config('inflector')->irregular;
}
if (isset(Inflector::$irregular[$str]))
{
$str = Inflector::$irregular[$str];
}
elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str))
{
$str .= 'es';
}
elseif (preg_match('/[^aeiou]y$/', $str))
{
// Change "y" to "ies"
$str = substr_replace($str, 'ies', -1);
}
else
{
$str .= 's';
}
// Convert to uppsecase if nessasary
if ($is_uppercase)
{
$str = strtoupper($str);
}
// Set the cache and return
return Inflector::$cache[$key] = $str;
}
/**
* Makes a phrase camel case. Spaces and underscores will be removed.
*
* $str = Inflector::camelize('mother cat'); // "motherCat"
* $str = Inflector::camelize('kittens in bed'); // "kittensInBed"
*
* @param string phrase to camelize
* @return string
*/
public static function camelize($str)
{
$str = 'x'.strtolower(trim($str));
$str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
return substr(str_replace(' ', '', $str), 1);
}
/**
* Converts a camel case phrase into a spaced phrase.
*
* $str = Inflector::decamelize('houseCat'); // "house cat"
* $str = Inflector::decamelize('kingAllyCat'); // "king ally cat"
*
* @param string phrase to camelize
* @param string word separator
* @return string
*/
public static function decamelize($str, $sep = ' ')
{
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1'.$sep.'$2', trim($str)));
}
/**
* Makes a phrase underscored instead of spaced.
*
* $str = Inflector::underscore('five cats'); // "five_cats";
*
* @param string phrase to underscore
* @return string
*/
public static function underscore($str)
{
return preg_replace('/\s+/', '_', trim($str));
}
/**
* Makes an underscored or dashed phrase human-readable.
*
* $str = Inflector::humanize('kittens-are-cats'); // "kittens are cats"
* $str = Inflector::humanize('dogs_as_well'); // "dogs as well"
*
* @param string phrase to make human-readable
* @return string
*/
public static function humanize($str)
{
return preg_replace('/[_-]+/', ' ', trim($str));
}
} // End Inflector
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/inflector.php | PHP | mit | 7,016 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* @package Kohana
* @category Exceptions
* @author Kohana Team
* @copyright (c) 2009-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_Exception extends Kohana_Exception {} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/request/exception.php | PHP | mit | 293 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Request_Client_External extends Request_Client {
/**
* @var array internal header cache for curl processing
* @todo remove in PHP 5.3, use Lambda instead
*/
protected static $_processed_headers = array();
/**
* Parses the returned headers from the remote
* request
*
* @param resource $remote The curl resource
* @param string $header The full header string
* @return int
*/
protected static function _parse_headers($remote, $header)
{
$headers = array();
if (preg_match_all('/(\w[^\s:]*):[ ]*([^\r\n]*(?:\r\n[ \t][^\r\n]*)*)/', $header, $matches))
{
foreach ($matches[0] as $key => $value)
$headers[$matches[1][$key]] = $matches[2][$key];
}
// If there are headers to apply
if ($headers)
{
Request_Client_External::$_processed_headers += $headers;
}
return strlen($header);
}
/**
* @var array additional curl options to use on execution
*/
protected $_options = array();
/**
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
*
* By default, the output from the controller is captured and returned, and
* no headers are sent.
*
* $request->execute();
*
* @param Request $request A request object
* @return Response
* @throws Kohana_Exception
* @uses [Kohana::$profiling]
* @uses [Profiler]
*/
public function execute(Request $request)
{
// Check for cache existance
if ($this->_cache instanceof Cache AND ($response = $this->cache_response($request)) instanceof Response)
return $response;
if (Kohana::$profiling)
{
// Set the benchmark name
$benchmark = '"'.$request->uri().'"';
if ($request !== Request::$initial AND Request::$current)
{
// Add the parent request uri
$benchmark .= ' « "'.Request::$current->uri().'"';
}
// Start benchmarking
$benchmark = Profiler::start('Requests', $benchmark);
}
// Store the current active request and replace current with new request
$previous = Request::$current;
Request::$current = $request;
try
{
// If PECL_HTTP is present, use extension to complete request
if (extension_loaded('http'))
{
$this->_http_execute($request);
}
// Else if CURL is present, use extension to complete request
elseif (extension_loaded('curl'))
{
$this->_curl_execute($request);
}
// Else use the sloooow method
else
{
$this->_native_execute($request);
}
}
catch (Exception $e)
{
// Restore the previous request
Request::$current = $previous;
if (isset($benchmark))
{
// Delete the benchmark, it is invalid
Profiler::delete($benchmark);
}
// Re-throw the exception
throw $e;
}
// Restore the previous request
Request::$current = $previous;
if (isset($benchmark))
{
// Stop the benchmark
Profiler::stop($benchmark);
}
// Cache the response if cache is available
if ($this->_cache instanceof Cache)
{
$this->cache_response($request, $request->response());
}
// Return the response
return $request->response();
}
/**
* Set and get options for this request.
*
* @param mixed $key Option name, or array of options
* @param mixed $value Option value
* @return mixed
* @return Request_Client_External
*/
public function options($key = NULL, $value = NULL)
{
if ($key === NULL)
return $this->_options;
if (is_array($key))
{
$this->_options = $key;
}
elseif ( ! $value)
{
return Arr::get($this->_options, $key);
}
else
{
$this->_options[$key] = $value;
}
return $this;
}
/**
* Execute the request using the PECL HTTP extension. (recommended)
*
* @param Request $request Request to execute
* @return Response
*/
protected function _http_execute(Request $request)
{
$http_method_mapping = array(
HTTP_Request::GET => HTTPRequest::METH_GET,
HTTP_Request::HEAD => HTTPRequest::METH_HEAD,
HTTP_Request::POST => HTTPRequest::METH_POST,
HTTP_Request::PUT => HTTPRequest::METH_PUT,
HTTP_Request::DELETE => HTTPRequest::METH_DELETE,
HTTP_Request::OPTIONS => HTTPRequest::METH_OPTIONS,
HTTP_Request::TRACE => HTTPRequest::METH_TRACE,
HTTP_Request::CONNECT => HTTPRequest::METH_CONNECT,
);
// Create an http request object
$http_request = new HTTPRequest($request->uri(), $http_method_mapping[$request->method()]);
// Set custom options
$http_request->setOptions($this->_options);
// Set headers
$http_request->setHeaders($request->headers()->getArrayCopy());
// Set cookies
$http_request->setCookies($request->cookie());
// Set body
$http_request->setBody($request->body());
try
{
$http_request->send();
}
catch (HTTPRequestException $e)
{
throw new Kohana_Request_Exception($e->getMessage());
}
catch (HTTPMalformedHeaderException $e)
{
throw new Kohana_Request_Exception($e->getMessage());
}
catch (HTTPEncodingException $e)
{
throw new Kohana_Request_Exception($e->getMessage());
}
// Create the response
$response = $request->create_response();
// Build the response
$response->status($http_request->getResponseCode())
->headers($http_request->getResponseHeader())
->cookie($http_request->getResponseCookies())
->body($http_request->getResponseBody());
return $response;
}
/**
* Execute the request using the CURL extension. (recommended)
*
* @param Request $request Request to execute
* @return Response
*/
protected function _curl_execute(Request $request)
{
// Reset the headers
Request_Client_External::$_processed_headers = array();
// Set the request method
$options[CURLOPT_CUSTOMREQUEST] = $request->method();
switch ($request->method())
{
case Request::POST:
// Set the request post fields
$options[CURLOPT_POSTFIELDS] = http_build_query($request->post(), NULL, '&');
break;
case Request::PUT:
// Create a temporary file to hold the body
$body = tmpfile();
// Write the request body into the temp file
fwrite($body, $request->body());
// Get the length of the content
$length = ftell($body);
// Rewind to the beginning of the file
fseek($body, 0);
// Set the request body
$options[CURLOPT_INFILE] = $body;
$options[CURLOPT_INFILESIZE] = $length;
break;
}
// Process headers
if ($headers = $request->headers())
{
$http_headers = array();
foreach ($headers as $key => $value)
{
$http_headers[] = $key.': '.$value;
}
$options[CURLOPT_HTTPHEADER] = $http_headers;
}
// Process cookies
if ($cookies = $request->cookie())
{
$options[CURLOPT_COOKIE] = http_build_query($cookies, NULL, '; ');
}
// The transfer must always be returned
$options[CURLOPT_RETURNTRANSFER] = TRUE;
// Apply any additional options set to Request_Client_External::$_options
$options += $this->_options;
// Open a new remote connection
$curl = curl_init($request->uri());
// Set connection options
if ( ! curl_setopt_array($curl, $options))
{
throw new Kohana_Request_Exception('Failed to set CURL options, check CURL documentation: :url',
array(':url' => 'http://php.net/curl_setopt_array'));
}
// Get the response body
$body = curl_exec($curl);
// Get the response information
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($body === FALSE)
{
$error = curl_error($curl);
}
// Close the connection
curl_close($curl);
if (isset($error))
{
throw new Kohana_Request_Exception('Error fetching remote :url [ status :code ] :error',
array(':url' => $request->url(), ':code' => $code, ':error' => $error));
}
// Create response
$response = $request->create_response();
$response->status($code)
->headers(Request_Client_External::$_processed_headers)
->body($body);
return $response;
}
/**
* Execute the request using PHP stream. (not recommended)
*
* @param Request $request Request to execute
* @return Response
*/
protected function _native_execute(Request $request)
{
// Reset the headers
Request_Client_External::$_processed_headers = array();
// Calculate stream mode
$mode = ($request->method() === HTTP_Request::GET) ? 'r' : 'r+';
// Process cookies
if ($cookies = $request->cookie())
{
$request->headers('cookie', http_build_query($cookies, NULL, '; '));
}
// Create the context
$options = array(
$request->protocol() => array(
'method' => $request->method(),
'header' => (string) $request->headers(),
'content' => $request->body(),
'user-agent' => 'Kohana Framework '.Kohana::VERSION.' ('.Kohana::CODENAME.')'
)
);
// Create the context stream
$context = stream_context_create($options);
stream_context_set_option($context, $this->_options);
$stream = fopen($request->uri(), $mode, FALSE, $context);
$meta_data = stream_get_meta_data($stream);
// Get the HTTP response code
$http_response = array_shift($meta_data['wrapper_data']);
if (preg_match_all('/(\w+\/\d\.\d) (\d{3})/', $http_response, $matches) !== FALSE)
{
$protocol = $matches[1][0];
$status = (int) $matches[2][0];
}
else
{
$protocol = NULL;
$status = NULL;
}
// Process headers
array_map(array('Request_Client_External', '_parse_headers'), array(), $meta_data['wrapper_data']);
// Create a response
$response = $request->create_response();
$response->status($status)
->protocol($protocol)
->headers(Request_Client_External::$_processed_headers)
->body(stream_get_contents($stream));
// Close the stream after use
fclose($stream);
return $response;
}
} // End Kohana_Request_Client_External | 12-05-2011-wharfland-project | trunk/system/classes/kohana/request/client/external.php | PHP | mit | 10,239 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Request Client for internal execution
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
* @since 3.1.0
*/
class Kohana_Request_Client_Internal extends Request_Client {
/**
* @var array
*/
protected $_previous_environment;
/**
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
*
* By default, the output from the controller is captured and returned, and
* no headers are sent.
*
* $request->execute();
*
* @param Request $request
* @return Response
* @throws Kohana_Exception
* @uses [Kohana::$profiling]
* @uses [Profiler]
* @deprecated passing $params to controller methods deprecated since version 3.1
* will be removed in 3.2
*/
public function execute(Request $request)
{
// Check for cache existance
if ($this->_cache instanceof Cache AND ($response = $this->cache_response($request)) instanceof Response)
return $response;
// Create the class prefix
$prefix = 'controller_';
// Directory
$directory = $request->directory();
// Controller
$controller = $request->controller();
if ($directory)
{
// Add the directory name to the class prefix
$prefix .= str_replace(array('\\', '/'), '_', trim($directory, '/')).'_';
}
if (Kohana::$profiling)
{
// Set the benchmark name
$benchmark = '"'.$request->uri().'"';
if ($request !== Request::$initial AND Request::$current)
{
// Add the parent request uri
$benchmark .= ' « "'.Request::$current->uri().'"';
}
// Start benchmarking
$benchmark = Profiler::start('Requests', $benchmark);
}
// Store the currently active request
$previous = Request::$current;
// Change the current request to this request
Request::$current = $request;
// Is this the initial request
$initial_request = ($request === Request::$initial);
try
{
// Initiate response time
$this->_response_time = time();
if ( ! class_exists($prefix.$controller))
{
throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
array(':uri' => $request->uri()));
}
// Load the controller using reflection
$class = new ReflectionClass($prefix.$controller);
if ($class->isAbstract())
{
throw new Kohana_Exception('Cannot create instances of abstract :controller',
array(':controller' => $prefix.$controller));
}
// Create a new instance of the controller
$controller = $class->newInstance($request, $request->response() ? $request->response() : $request->create_response());
$class->getMethod('before')->invoke($controller);
// Determine the action to use
$action = $request->action();
$params = $request->param();
// If the action doesn't exist, it's a 404
if ( ! $class->hasMethod('action_'.$action))
{
throw new HTTP_Exception_404('The requested URL :uri was not found on this server.',
array(':uri' => $request->uri()));
}
$method = $class->getMethod('action_'.$action);
/**
* Execute the main action with the parameters
*
* @deprecated $params passing is deprecated since version 3.1
* will be removed in 3.2.
*/
$method->invokeArgs($controller, $params);
// Execute the "after action" method
$class->getMethod('after')->invoke($controller);
// Stop response time
$this->_response_time = (time() - $this->_response_time);
// Add the default Content-Type header to initial request if not present
if ($initial_request AND ! $request->headers('content-type'))
{
$request->headers('content-type', Kohana::$content_type.'; charset='.Kohana::$charset);
}
}
catch (Exception $e)
{
// Restore the previous request
Request::$current = $previous;
if (isset($benchmark))
{
// Delete the benchmark, it is invalid
Profiler::delete($benchmark);
}
// Re-throw the exception
throw $e;
}
// Restore the previous request
Request::$current = $previous;
if (isset($benchmark))
{
// Stop the benchmark
Profiler::stop($benchmark);
}
// Cache the response if cache is available
if ($this->_cache instanceof Cache)
{
$this->cache_response($request, $request->response());
}
// Return the response
return $request->response();
}
} // End Kohana_Request_Client_Internal
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/request/client/internal.php | PHP | mit | 4,801 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Request Client
*
* @package Kohana
* @category Base
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
* @since 3.1.0
*/
abstract class Kohana_Request_Client {
/**
* @var Cache Caching library for request caching
*/
protected $_cache;
/**
* @var boolean Defines whether this client should cache `private` cache directives
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
*/
protected $_allow_private_cache = FALSE;
/**
* @var int The timestamp of the request
*/
protected $_request_time;
/**
* @var int The timestamp of the response
*/
protected $_response_time;
/**
* Creates a new `Request_Client` object,
* allows for dependency injection.
*
* @param array $params Params
*/
public function __construct(array $params = array())
{
if ($params)
{
foreach ($params as $key => $value)
{
if (method_exists($this, $key))
{
if (property_exists($this, $key) OR property_exists($this, '_'.$key))
{
$method = trim($key, '_');
$this->$method($value);
}
}
}
}
}
/**
* Processes the request, executing the controller action that handles this
* request, determined by the [Route].
*
* 1. Before the controller action is called, the [Controller::before] method
* will be called.
* 2. Next the controller action will be called.
* 3. After the controller action is called, the [Controller::after] method
* will be called.
*
* By default, the output from the controller is captured and returned, and
* no headers are sent.
*
* $request->execute();
*
* @param Request $request
* @return Response
* @throws Kohana_Exception
* @uses [Kohana::$profiling]
* @uses [Profiler]
*/
abstract public function execute(Request $request);
/**
* Invalidate a cached response for the [Request] supplied.
* This has the effect of deleting the response from the
* [Cache] entry.
*
* @param Request $request Response to remove from cache
* @return void
*/
public function invalidate_cache(Request $request)
{
if ( ! $this->_cache instanceof Cache)
return;
$this->_cache->delete($this->_create_cache_key($request));
return;
}
/**
* Getter and setter for the internal caching engine,
* used to cache responses if available and valid.
*
* @param Kohana_Cache cache engine to use for caching
* @return Kohana_Cache
* @return Kohana_Request_Client
*/
public function cache(Cache $cache = NULL)
{
if ($cache === NULL)
return $this->_cache;
$this->_cache = $cache;
return $this;
}
/**
* Gets or sets the [Request_Client::allow_private_cache] setting.
* If set to `TRUE`, the client will also cache cache-control directives
* that have the `private` setting.
*
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
* @param boolean allow caching of privately marked responses
* @return boolean
* @return [Request_Client]
*/
public function allow_private_cache($setting = NULL)
{
if ($setting === NULL)
return $this->_allow_private_cache;
$this->_allow_private_cache = (bool) $setting;
return $this;
}
/**
* Creates a cache key for the request to use for caching
* [Kohana_Response] returned by [Request::execute].
*
* @param Request request
* @return string
* @return boolean
*/
public function create_cache_key(Request $request)
{
return sha1($request->url());
}
/**
* Controls whether the response can be cached. Uses HTTP
* protocol to determine whether the response can be cached.
*
* @see RFC 2616 http://www.w3.org/Protocols/rfc2616/
* @param Response $response The Response
* @return boolean
*/
public function set_cache(Response $response)
{
$headers = (array) $response->headers();
if ($cache_control = arr::get($headers, 'cache-control'))
{
// Parse the cache control
$cache_control = Response::parse_cache_control( (string) $cache_control);
// If the no-cache or no-store directive is set, return
if (array_intersect_key($cache_control, array('no-cache' => NULL, 'no-store' => NULL)))
return FALSE;
// Get the directives
$directives = array_keys($cache_control);
// Check for private cache and get out of here if invalid
if ( ! $this->_allow_private_cache and in_array('private', $directives))
{
if ( ! isset($cache_control['s-maxage']))
return FALSE;
// If there is a s-maxage directive we can use that
$cache_control['max-age'] = $cache_control['s-maxage'];
}
// Check that max-age has been set and if it is valid for caching
if (isset($cache_control['max-age']) and (int) $cache_control['max-age'] < 1)
return FALSE;
}
if ($expires = arr::get($headers, 'expires') and ! isset($cache_control['max-age']))
{
// Can't cache things that have expired already
if (strtotime( (string) $expires) <= time())
return FALSE;
}
return TRUE;
}
/**
* Caches a [Response] using the supplied [Cache]
* and the key generated by [Request_Client::_create_cache_key].
*
* If not response is supplied, the cache will be checked for an existing
* one that is available.
*
* @param Request $request The request
* @param Response $response Response
* @return mixed
*/
public function cache_response(Request $request, Response $response = NULL)
{
if ( ! $this->_cache instanceof Cache)
return FALSE;
// Check for Pragma: no-cache
if ($pragma = $request->headers('pragma'))
{
if ($pragma instanceof HTTP_Header_Value and $pragma->key == 'no-cache')
return FALSE;
elseif (is_array($pragma) and isset($pragma['no-cache']))
return FALSE;
}
if ( ! $response)
{
$response = $this->_cache->get($this->create_cache_key($request));
return ($response !== NULL) ? $response : FALSE;
}
else
{
if (($ttl = $this->cache_lifetime($response)) === FALSE)
return FALSE;
return $this->_cache->set($this->create_cache_key($request), $response, $ttl);
}
}
/**
* Calculates the total Time To Live based on the specification
* RFC 2616 cache lifetime rules.
*
* @param Response $response Response to evaluate
* @return mixed TTL value or false if the response should not be cached
*/
public function cache_lifetime(Response $response)
{
// Get out of here if this cannot be cached
if ( ! $this->set_cache($response))
return FALSE;
// Calculate apparent age
if ($date = $response->headers('date'))
{
$apparent_age = max(0, $this->_response_time - strtotime( (string) $date));
}
else
{
$apparent_age = max(0, $this->_response_time);
}
// Calculate corrected received age
if ($age = $response->headers('age'))
{
$corrected_received_age = max($apparent_age, intval( (string) $age));
}
else
{
$corrected_received_age = $apparent_age;
}
// Corrected initial age
$corrected_initial_age = $corrected_received_age + $this->request_execution_time();
// Resident time
$resident_time = time() - $this->_response_time;
// Current age
$current_age = $corrected_initial_age + $resident_time;
// Prepare the cache freshness lifetime
$ttl = NULL;
// Cache control overrides
if ($cache_control = $response->headers('cache-control'))
{
// Parse the cache control header
$cache_control = Response::parse_cache_control( (string) $cache_control);
if (isset($cache_control['max-age']))
{
$ttl = (int) $cache_control['max-age'];
}
if (isset($cache_control['s-maxage']) AND isset($cache_control['private']) AND $this->_allow_private_cache)
{
$ttl = (int) $cache_control['s-maxage'];
}
if (isset($cache_control['max-stale']) AND ! isset($cache_control['must-revalidate']))
{
$ttl = $current_age + (int) $cache_control['max-stale'];
}
}
// If we have a TTL at this point, return
if ($ttl !== NULL)
return $ttl;
if ($expires = $response->headers('expires'))
return strtotime( (string) $expires) - $current_age;
return FALSE;
}
/**
* Returns the duration of the last request execution.
* Either returns the time of completed requests or
* `FALSE` if the request hasn't finished executing, or
* is yet to be run.
*
* @return mixed
*/
public function request_execution_time()
{
if ($this->_request_time === NULL OR $this->_response_time === NULL)
return FALSE;
return $this->_response_time - $this->_request_time;
}
} | 12-05-2011-wharfland-project | trunk/system/classes/kohana/request/client.php | PHP | mit | 8,630 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Native PHP session class.
*
* @package Kohana
* @category Session
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Session_Native extends Session {
/**
* @return string
*/
public function id()
{
return session_id();
}
/**
* @param string $id session id
* @return null
*/
protected function _read($id = NULL)
{
// Sync up the session cookie with Cookie parameters
session_set_cookie_params($this->_lifetime, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httponly);
// Do not allow PHP to send Cache-Control headers
session_cache_limiter(FALSE);
// Set the session cookie name
session_name($this->_name);
if ($id)
{
// Set the session id
session_id($id);
}
// Start the session
session_start();
// Use the $_SESSION global for storing data
$this->_data =& $_SESSION;
return NULL;
}
/**
* @return string
*/
protected function _regenerate()
{
// Regenerate the session id
session_regenerate_id();
return session_id();
}
/**
* @return bool
*/
protected function _write()
{
// Write and close the session
session_write_close();
return TRUE;
}
/**
* @return bool
*/
protected function _destroy()
{
// Destroy the current session
session_destroy();
// Did destruction work?
$status = ! session_id();
if ($status)
{
// Make sure the session cannot be restarted
Cookie::delete($this->_name);
}
return $status;
}
} // End Session_Native
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/session/native.php | PHP | mit | 1,638 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Cookie-based session class.
*
* @package Kohana
* @category Session
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Session_Cookie extends Session {
/**
* @param string $id session id
* @return string
*/
protected function _read($id = NULL)
{
return Cookie::get($this->_name, NULL);
}
/**
* @return null
*/
protected function _regenerate()
{
// Cookie sessions have no id
return NULL;
}
/**
* @return bool
*/
protected function _write()
{
return Cookie::set($this->_name, $this->__toString(), $this->_lifetime);
}
/**
* @return bool
*/
protected function _destroy()
{
return Cookie::delete($this->_name);
}
} // End Session_Cookie
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/session/cookie.php | PHP | mit | 851 |
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Upload helper class for working with uploaded files and [Validation].
*
* $array = Validation::factory($_FILES);
*
* [!!] Remember to define your form with "enctype=multipart/form-data" or file
* uploading will not work!
*
* The following configuration properties can be set:
*
* - [Upload::$remove_spaces]
* - [Upload::$default_directory]
*
* @package Kohana
* @category Helpers
* @author Kohana Team
* @copyright (c) 2007-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Upload {
/**
* @var boolean remove spaces in uploaded files
*/
public static $remove_spaces = TRUE;
/**
* @var string default upload directory
*/
public static $default_directory = 'upload';
/**
* Save an uploaded file to a new location. If no filename is provided,
* the original filename will be used, with a unique prefix added.
*
* This method should be used after validating the $_FILES array:
*
* if ($array->check())
* {
* // Upload is valid, save it
* Upload::save($_FILES['file']);
* }
*
* @param array uploaded file data
* @param string new filename
* @param string new directory
* @param integer chmod mask
* @return string on success, full path to new file
* @return FALSE on failure
*/
public static function save(array $file, $filename = NULL, $directory = NULL, $chmod = 0644)
{
if ( ! isset($file['tmp_name']) OR ! is_uploaded_file($file['tmp_name']))
{
// Ignore corrupted uploads
return FALSE;
}
if ($filename === NULL)
{
// Use the default filename, with a timestamp pre-pended
$filename = uniqid().$file['name'];
}
if (Upload::$remove_spaces === TRUE)
{
// Remove spaces from the filename
$filename = preg_replace('/\s+/u', '_', $filename);
}
if ($directory === NULL)
{
// Use the pre-configured upload directory
$directory = Upload::$default_directory;
}
if ( ! is_dir($directory) OR ! is_writable(realpath($directory)))
{
throw new Kohana_Exception('Directory :dir must be writable',
array(':dir' => Debug::path($directory)));
}
// Make the filename into a complete path
$filename = realpath($directory).DIRECTORY_SEPARATOR.$filename;
if (move_uploaded_file($file['tmp_name'], $filename))
{
if ($chmod !== FALSE)
{
// Set permissions on filename
chmod($filename, $chmod);
}
// Return new file path
return $filename;
}
return FALSE;
}
/**
* Tests if upload data is valid, even if no file was uploaded. If you
* _do_ require a file to be uploaded, add the [Upload::not_empty] rule
* before this rule.
*
* $array->rule('file', 'Upload::valid')
*
* @param array $_FILES item
* @return bool
*/
public static function valid($file)
{
return (isset($file['error'])
AND isset($file['name'])
AND isset($file['type'])
AND isset($file['tmp_name'])
AND isset($file['size']));
}
/**
* Tests if a successful upload has been made.
*
* $array->rule('file', 'Upload::not_empty');
*
* @param array $_FILES item
* @return bool
*/
public static function not_empty(array $file)
{
return (isset($file['error'])
AND isset($file['tmp_name'])
AND $file['error'] === UPLOAD_ERR_OK
AND is_uploaded_file($file['tmp_name']));
}
/**
* Test if an uploaded file is an allowed file type, by extension.
*
* $array->rule('file', 'Upload::type', array(array('jpg', 'png', 'gif')));
*
* @param array $_FILES item
* @param array allowed file extensions
* @return bool
*/
public static function type(array $file, array $allowed)
{
if ($file['error'] !== UPLOAD_ERR_OK)
return TRUE;
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
return in_array($ext, $allowed);
}
/**
* Validation rule to test if an uploaded file is allowed by file size.
* File sizes are defined as: SB, where S is the size (1, 8.5, 300, etc.)
* and B is the byte unit (K, MiB, GB, etc.). All valid byte units are
* defined in Num::$byte_units
*
* $array->rule('file', 'Upload::size', array('1M'))
* $array->rule('file', 'Upload::size', array('2.5KiB'))
*
* @param array $_FILES item
* @param string maximum file size allowed
* @return bool
*/
public static function size(array $file, $size)
{
if ($file['error'] === UPLOAD_ERR_INI_SIZE)
{
// Upload is larger than PHP allowed size (upload_max_filesize)
return FALSE;
}
if ($file['error'] !== UPLOAD_ERR_OK)
{
// The upload failed, no size to check
return TRUE;
}
// Convert the provided size to bytes for comparison
$size = Num::bytes($size);
// Test that the file is under or equal to the max size
return ($file['size'] <= $size);
}
} // End upload
| 12-05-2011-wharfland-project | trunk/system/classes/kohana/upload.php | PHP | mit | 4,902 |
<?php defined('SYSPATH') or die('No direct script access.');
class Security extends Kohana_Security {} | 12-05-2011-wharfland-project | trunk/system/classes/security.php | PHP | mit | 103 |
<?php defined('SYSPATH') or die('No direct script access.');
class View extends Kohana_View {}
| 12-05-2011-wharfland-project | trunk/system/classes/view.php | PHP | mit | 96 |
<?php defined('SYSPATH') or die('No direct script access.');
abstract class Model extends Kohana_Model {}
| 12-05-2011-wharfland-project | trunk/system/classes/model.php | PHP | mit | 107 |
<?php defined('SYSPATH') or die('No direct script access.');
class Date extends Kohana_Date {}
| 12-05-2011-wharfland-project | trunk/system/classes/date.php | PHP | mit | 96 |
<?php defined('SYSPATH') or die('No direct script access.');
abstract class HTTP extends Kohana_HTTP {} | 12-05-2011-wharfland-project | trunk/system/classes/http.php | PHP | mit | 104 |
<?php defined('SYSPATH') or die('No direct script access.');
class Config extends Kohana_Config {} | 12-05-2011-wharfland-project | trunk/system/classes/config.php | PHP | mit | 99 |
<?php defined('SYSPATH') or die('No direct script access.');
class File extends Kohana_File {} | 12-05-2011-wharfland-project | trunk/system/classes/file.php | PHP | mit | 95 |
<?php defined('SYSPATH') or die('No direct script access.');
class Validation extends Kohana_Validation {}
| 12-05-2011-wharfland-project | trunk/system/classes/validation.php | PHP | mit | 108 |
<?php defined('SYSPATH') or die('No direct script access.');
class Route extends Kohana_Route {}
| 12-05-2011-wharfland-project | trunk/system/classes/route.php | PHP | mit | 98 |
<?php defined('SYSPATH') or die('No direct script access.');
class Debug extends Kohana_Debug {}
| 12-05-2011-wharfland-project | trunk/system/classes/debug.php | PHP | mit | 98 |
<?php defined('SYSPATH') or die('No direct script access.');
class Num extends Kohana_Num {}
| 12-05-2011-wharfland-project | trunk/system/classes/num.php | PHP | mit | 94 |
<?php defined('SYSPATH') or die('No direct script access.');
class Response extends Kohana_Response {} | 12-05-2011-wharfland-project | trunk/system/classes/response.php | PHP | mit | 103 |
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana extends Kohana_Core {}
| 12-05-2011-wharfland-project | trunk/system/classes/kohana.php | PHP | mit | 98 |
<?php defined('SYSPATH') or die('No direct script access.');
class CLI extends Kohana_CLI {} | 12-05-2011-wharfland-project | trunk/system/classes/cli.php | PHP | mit | 93 |
<?php defined('SYSPATH') or die('No direct script access.');
class Fragment extends Kohana_Fragment {}
| 12-05-2011-wharfland-project | trunk/system/classes/fragment.php | PHP | mit | 104 |
<?php defined('SYSPATH') or die('No direct script access.');
abstract class Session extends Kohana_Session {}
| 12-05-2011-wharfland-project | trunk/system/classes/session.php | PHP | mit | 111 |
<?php defined('SYSPATH') or die('No direct script access.');
class HTTP_Exception extends Kohana_HTTP_Exception {} | 12-05-2011-wharfland-project | trunk/system/classes/http/exception.php | PHP | mit | 115 |
<?php defined('SYSPATH') or die('No direct script access.');
class HTTP_Exception_503 extends Kohana_HTTP_Exception_503 {} | 12-05-2011-wharfland-project | trunk/system/classes/http/exception/503.php | PHP | mit | 123 |