Exemple #1
0
 /**
  * Returns the option with the given name.	You can also give the option
  * number.
  *
  * Named options must be in the following formats:
  * php index.php user -v --v -name=John --name=John
  *
  * @param	string|int	$name	the name of the option (int if unnamed)
  * @return	string
  */
 public static function option($name, $default = null)
 {
     if (!isset(static::$args[$name])) {
         return \Fuel::value($default);
     }
     return static::$args[$name];
 }
Exemple #2
0
 /**
  * Gets a dot-notated key from an array, with a default value if it does
  * not exist.
  *
  * @param   array   $array    The search array
  * @param   mixed   $key      The dot-notated key or array of keys
  * @param   string  $default  The default value
  * @return  mixed
  */
 public static function get($array, $key, $default = null)
 {
     if (!is_array($array) and !$array instanceof \ArrayAccess) {
         throw new \InvalidArgumentException('First parameter must be an array or ArrayAccess object.');
     }
     if (is_null($key)) {
         return $array;
     }
     if (is_array($key)) {
         $return = array();
         foreach ($key as $k) {
             $return[$k] = static::get($array, $k, $default);
         }
         return $return;
     }
     foreach (explode('.', $key) as $key_part) {
         if (($array instanceof \ArrayAccess and isset($array[$key_part])) === false) {
             if (!is_array($array) or !array_key_exists($key_part, $array)) {
                 return \Fuel::value($default);
             }
         }
         $array = $array[$key_part];
     }
     return $array;
 }
Exemple #3
0
 /**
  * {@inheritdoc}
  */
 public static function forge($instance = 'default', array $handlers = array())
 {
     $logger = new Monolog\Logger($instance);
     $handlers = \Arr::merge(\Config::get('logger.' . $instance, array()), $handlers);
     foreach ($handlers as $handler) {
         $handler = \Fuel::value($handler);
         $logger->pushHandler($handler);
     }
     return static::newInstance($instance, $logger);
 }
Exemple #4
0
 public static function get($line, array $params = array(), $default = null, $language = null)
 {
     $output = parent::get($line, $params, '__NOT__FOUND__', $language);
     if (!empty($output) && $output != '__NOT__FOUND__') {
         return $output;
     }
     if ($output == '__NOT__FOUND__') {
         $output = $default;
     }
     if (!static::$autosave || !\CMF::$lang_enabled) {
         return $output;
     }
     $language === null and $language = static::get_lang();
     $pos = strpos($line, '.');
     $group = 'common';
     $basename = $line;
     if ($pos === false) {
         if (empty($default)) {
             $default = $line;
         }
         $line = "{$group}.{$line}";
     } else {
         $basename = substr($line, $pos + 1);
         if (empty($default)) {
             $default = $basename;
         }
         $group = substr($line, 0, $pos);
     }
     // Try and load from the DB...
     if (!in_array($group . '_' . $language, static::$loaded)) {
         static::load("{$group}.db", $group, $language, true, true);
         static::$loaded[] = $group . '_' . $language;
     }
     // Don't continue if it's not the 'common' group
     if ($group != 'common') {
         return $output != null ? \Str::tr(\Fuel::value($output), $params) : $default;
     }
     $output = \Arr::get(static::$lines, "{$language}.{$line}");
     if ($output == null) {
         // First try and get from the fallback...
         $output = \Arr::get(static::$lines, static::$fallback[0] . ".{$line}");
         if (!in_array($group, static::$to_save)) {
             static::$to_save[] = $group;
         }
         //if (!empty($default) && $default != $line)
         static::set($line, $default);
         static::set($line, $default, null, static::$fallback[0]);
         if ($output == null) {
             $output = $default;
         }
     }
     return $output != null ? \Str::tr(\Fuel::value($output), $params) : $default;
 }
Exemple #5
0
 /**
  * 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
  * @param   string    path of the cookie
  * @param   string    domain of the cookie
  * @param   boolean   if true, the cookie should only be transmitted over a secure HTTPS connection
  * @param   boolean   if true, the cookie will be made accessible only through the HTTP protocol
  * @return  boolean
  */
 public static function set($name, $value, $expiration = null, $path = null, $domain = null, $secure = null, $http_only = null)
 {
     $value = \Fuel::value($value);
     // use the class defaults for the other parameters if not provided
     is_null($expiration) and $expiration = static::$config['expiration'];
     is_null($path) and $path = static::$config['path'];
     is_null($domain) and $domain = static::$config['domain'];
     is_null($secure) and $secure = static::$config['secure'];
     is_null($http_only) and $http_only = static::$config['http_only'];
     // add the current time so we have an offset
     $expiration = $expiration > 0 ? $expiration + time() : 0;
     return setcookie($name, $value, $expiration, $path, $domain, $secure, $http_only);
 }
Exemple #6
0
 /**
  * Return the named column from the current row.
  *
  *     // Get the "id" value
  *     $id = $result->get('id');
  *
  * @param   string $name    column to get
  * @param   mixed  $default default value if the column does not exist
  *
  * @return  mixed
  */
 public function get($name, $default = null)
 {
     $row = $this->current();
     if ($this->_as_object) {
         if (isset($row->{$name})) {
             // sanitize the data if needed
             if (!$this->_sanitization_enabled) {
                 $result = $row->{$name};
             } else {
                 $result = \Security::clean($row->{$name}, null, 'security.output_filter');
             }
             return $result;
         }
     } else {
         if (isset($row[$name])) {
             // sanitize the data if needed
             if (!$this->_sanitization_enabled) {
                 $result = $row[$name];
             } else {
                 $result = \Security::clean($row[$name], null, 'security.output_filter');
             }
             return $result;
         }
     }
     return \Fuel::value($default);
 }
Exemple #7
0
 /**
  * Sets a (dot notated) config item
  *
  * @param
  *        	string a (dot notated) config key
  * @param
  *        	mixed the config value
  * @return void the \Arr::set result
  */
 public static function set($item, $value)
 {
     strpos($item, '.') === false or static::$itemcache[$item] = $value;
     return \Arr::set(static::$items, $item, \Fuel::value($value));
 }
Exemple #8
0
 /**
  * Front for writing the cache, ensures interchangebility of storage drivers. Actual writing
  * is being done by the _set() method which needs to be extended.
  *
  * @param   mixed  The identifier of the cache, can be anything but empty
  * @param   mixed  The content to be cached
  * @param   int    The time in seconds until the cache will expire, =< 0 or null means no expiration
  * @param   array  Contains the identifiers of caches this one will depend on (not supported by all drivers!)
  * @return  Cache_Storage_Driver  The new Cache object
  */
 public static function set($identifier, $contents = null, $expiration = false, $dependencies = array())
 {
     $contents = \Fuel::value($contents);
     $cache = static::forge($identifier);
     return $cache->set($contents, $expiration, $dependencies);
 }
Exemple #9
0
 /**
  * Return the named column from the current row.
  *
  *     // Get the "id" value
  *     $id = $result->get('id');
  *
  * @param   string  column to get
  * @param   mixed   default value if the column does not exist
  * @return  mixed
  */
 public function get($name, $default = null)
 {
     $row = $this->current();
     if ($this->_as_object) {
         if (isset($row->{$name})) {
             return $row->{$name};
         }
     } else {
         if (isset($row[$name])) {
             return $row[$name];
         }
     }
     return \Fuel::value($default);
 }
Exemple #10
0
 /**
  * Searches for the given variable and returns its value.
  * Local variables will be returned before global variables.
  *
  *     $value = $view->get('foo', 'bar');
  *
  * If the key is not given or null, the entire data array is returned.
  *
  * If a default parameter is not given and the variable does not
  * exist, it will throw an OutOfBoundsException.
  *
  * @param   string  The variable name
  * @param   mixed   The default value to return
  * @return  mixed
  * @throws  OutOfBoundsException
  */
 public function &get($key = null, $default = null)
 {
     if (func_num_args() === 0 or $key === null) {
         return $this->data;
     } elseif (array_key_exists($key, $this->data)) {
         return $this->data[$key];
     } elseif (array_key_exists($key, static::$global_data)) {
         return static::$global_data[$key];
     }
     if (is_null($default) and func_num_args() === 1) {
         throw new \OutOfBoundsException('View variable is not set: ' . $key);
     } else {
         // assign it first, you can't return a return value by reference directly!
         $default = \Fuel::value($default);
         return $default;
     }
 }
Exemple #11
0
 /**
  * Gets a specific named parameter
  *
  * @param   string  $param    Name of the parameter
  * @param   mixed   $default  Default value
  * @return  mixed
  */
 public function param($param, $default = null)
 {
     if (!isset($this->named_params[$param])) {
         return \Fuel::value($default);
     }
     return $this->named_params[$param];
 }
Exemple #12
0
 /**
  * Sets a (dot notated) language string
  *
  * @param    string    a (dot notated) language key
  * @param    mixed     the language string
  * @param    string    group
  * @return   void      the \Arr::set result
  */
 public static function set($line, $value, $group = null)
 {
     $group === null or $line = $group . '.' . $line;
     return \Arr::set(static::$lines, $line, \Fuel::value($value));
 }
Exemple #13
0
 /**
  * Get the specified URI segment, return default if it doesn't exist.
  *
  * Segment index is 1 based, not 0 based
  *
  * @param   string  $segment  The 1-based segment index
  * @param   mixed   $default  The default value
  * @return  mixed
  */
 public function get_segment($segment, $default = null)
 {
     if (isset($this->segments[$segment - 1])) {
         return $this->segments[$segment - 1];
     }
     return \Fuel::value($default);
 }
Exemple #14
0
 /**
  * Front for writing the cache, ensures interchangeability of storage engines.
  * Actual writing
  * is being done by the _set() method which needs to be extended.
  *
  * @param
  *        	mixed The content to be cached
  * @param
  *        	int The time in seconds until the cache will expire, =< 0 or null means no expiration
  * @param
  *        	array array of names on which this cache depends for
  * @return Cache_Storage_Driver The new request
  */
 public final function set($contents = null, $expiration = false, $dependencies = array())
 {
     $contents = \Fuel::value($contents);
     // save the current expiration
     $current_expiration = $this->expiration;
     // Use either the given value or the class property
     if (!is_null($contents)) {
         $this->set_contents($contents);
     }
     $this->expiration = $expiration !== false ? $expiration : $this->expiration;
     $this->dependencies = !empty($dependencies) ? $dependencies : $this->dependencies;
     $this->created = time();
     // Create expiration timestamp when other then null
     if (!is_null($this->expiration)) {
         if (!is_numeric($this->expiration)) {
             throw new \InvalidArgumentException('Expiration must be a valid number.');
         }
         $this->expiration = $this->created + intval($this->expiration);
     }
     // Convert dependency identifiers to string when set
     $this->dependencies = !is_array($this->dependencies) ? array($this->dependencies) : $this->dependencies;
     if (!empty($this->dependencies)) {
         foreach ($this->dependencies as $key => $id) {
             $this->dependencies[$key] = $this->stringify_identifier($id);
         }
     }
     // Turn everything over to the storage specific method
     $this->_set();
     // restore the expiration
     $this->expiration = $current_expiration;
 }
Exemple #15
0
 /**
  * Searches for the given variable and returns its value.
  * Local variables will be returned before global variables.
  *
  *     $value = $view->get('foo', 'bar');
  *
  * If a default parameter is not given and the variable does not
  * exist, it will throw an OutOfBoundsException.
  *
  * @param   string  The variable name
  * @param   mixed   The default value to return
  * @return  mixed
  * @throws  OutOfBoundsException
  */
 public function &get($key, $default = null)
 {
     if (array_key_exists($key, $this->data)) {
         return $this->data[$key];
     } elseif (array_key_exists($key, static::$global_data)) {
         return static::$global_data[$key];
     }
     if (is_null($default) and func_num_args() === 1) {
         throw new \OutOfBoundsException('View variable is not set: ' . $key);
     } else {
         return \Fuel::value($default);
     }
 }
Exemple #16
0
 /**
  * Get the real ip address of the user.  Even if they are using a proxy.
  *
  * @param	string	the default to return on failure
  * @param	bool	exclude private and reserved IPs
  * @return  string  the real ip address of the user
  */
 public static function real_ip($default = '0.0.0.0', $exclude_reserved = false)
 {
     $server_keys = array('HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_CLIENT_IP', 'REMOTE_ADDR');
     foreach ($server_keys as $key) {
         if (!static::server($key)) {
             continue;
         }
         $ips = explode(',', static::server($key));
         array_walk($ips, function (&$ip) {
             $ip = trim($ip);
         });
         if ($exclude_reserved) {
             $ips = array_filter($ips, function ($ip) {
                 return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
             });
         }
         if ($ips) {
             return reset($ips);
         }
     }
     return \Fuel::value($default);
 }
Exemple #17
0
 /**
  * Sets a (dot notated) language string
  *
  * @param    string       $line      a (dot notated) language key
  * @param    mixed        $value     the language string
  * @param    string       $group     group
  * @param    string|null  $language  name of the language to set, null for the configurated language
  * @return   void                    the \Arr::set result
  */
 public static function set($line, $value, $group = null, $language = null)
 {
     $group === null or $line = $group . '.' . $line;
     $language === null and $language = static::get_lang();
     isset(static::$lines[$language]) or static::$lines[$language] = array();
     return \Arr::set(static::$lines[$language], $line, \Fuel::value($value));
 }
Exemple #18
0
 /**
  * Sets a (dot notated) config item
  *
  * @param    string    a (dot notated) config key
  * @param    mixed     the config value
  * @return   void      the \Arr::set result
  */
 public static function set($item, $value)
 {
     return \Arr::set(static::$items, $item, \Fuel::value($value));
 }
Exemple #19
0
 /**
  * Searches for the given variable and returns its value.
  * Local variables will be returned before global variables.
  *
  *     $value = $view->get('foo', 'bar');
  *
  * If the key is not given or null, the entire data array is returned.
  *
  * If a default parameter is not given and the variable does not
  * exist, it will throw an OutOfBoundsException.
  *
  * @param   string  The variable name
  * @param   mixed   The default value to return
  * @return  mixed
  * @throws  OutOfBoundsException
  */
 public function &get($key = null, $default = null)
 {
     if (func_num_args() === 0 or $key === null) {
         return $this->data;
     } elseif (strpos($key, '.') === false) {
         if (array_key_exists($key, $this->data)) {
             return $this->data[$key];
         } elseif (array_key_exists($key, static::$global_data)) {
             return static::$global_data[$key];
         }
     } else {
         if (($result = \Arr::get($this->data, $key, \Arr::get(static::$global_data, $key, '__KEY__LOOKUP__MISS__'))) !== '__KEY__LOOKUP__MISS__') {
             return $result;
         }
     }
     if (is_null($default) and func_num_args() === 1) {
         throw new \OutOfBoundsException('View variable is not set: ' . $key);
     } else {
         // assign it first, you can't return a return value by reference directly!
         $default = \Fuel::value($default);
         return $default;
     }
 }
Exemple #20
0
 /**
  * Get the real ip address of the user.  Even if they are using a proxy.
  *
  * @return  string  the real ip address of the user
  */
 public static function real_ip($default = '0.0.0.0')
 {
     if (static::server('HTTP_X_CLUSTER_CLIENT_IP') !== null) {
         return static::server('HTTP_X_CLUSTER_CLIENT_IP');
     }
     if (static::server('HTTP_X_FORWARDED_FOR') !== null) {
         return static::server('HTTP_X_FORWARDED_FOR');
     }
     if (static::server('HTTP_CLIENT_IP') !== null) {
         return static::server('HTTP_CLIENT_IP');
     }
     if (static::server('REMOTE_ADDR') !== null) {
         return static::server('REMOTE_ADDR');
     }
     // detection failed, return the default
     return \Fuel::value($default);
 }
Exemple #21
0
 /**
  * Get the real ip address of the user.  Even if they are using a proxy.
  *
  * @param	string	the default to return on failure
  * @param	bool	exclude private and reserved IPs
  * @return  string  the real ip address of the user
  */
 public static function real_ip($default = '0.0.0.0', $exclude_reserved = false)
 {
     static $server_keys = null;
     if (empty($server_keys)) {
         $server_keys = array('HTTP_CLIENT_IP', 'REMOTE_ADDR');
         if (\Config::get('security.allow_x_headers', false)) {
             $server_keys = array_merge(array('HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_X_FORWARDED_FOR'), $server_keys);
         }
     }
     foreach ($server_keys as $key) {
         if (!static::server($key)) {
             continue;
         }
         $ips = explode(',', static::server($key));
         array_walk($ips, function (&$ip) {
             $ip = trim($ip);
         });
         $ips = array_filter($ips, function ($ip) use($exclude_reserved) {
             return filter_var($ip, FILTER_VALIDATE_IP, $exclude_reserved ? FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE : null);
         });
         if ($ips) {
             return reset($ips);
         }
     }
     return \Fuel::value($default);
 }
Exemple #22
0
 /**
  * Sets a (dot notated) language string
  *
  * @param    string       $line      a (dot notated) language key
  * @param    mixed        $value     the language string
  * @param    string       $group     group
  * @param    string|null  $language  name of the language to set, null for the configurated language
  * @return   void                    the \Arr::set result
  */
 public static function set($line, $value, $group = null, $language = null)
 {
     $group === null or $line = $group . '.' . $line;
     if ($language === null) {
         $languages = static::$fallback;
         array_unshift($languages, $language ?: \Config::get('language'));
         $language = reset($languages);
     }
     isset(static::$lines[$language]) or static::$lines[$language] = array();
     return \Arr::set(static::$lines[$language], $line, \Fuel::value($value));
 }