Skip to content

Commit

Permalink
Leverage framework's ArrayAccess capability
Browse files Browse the repository at this point in the history
  • Loading branch information
bcosca committed Mar 25, 2017
1 parent 65eaf0c commit b11c03e
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 111 deletions.
6 changes: 3 additions & 3 deletions audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function ispublic($addr) {
**/
function isdesktop($agent=NULL) {
if (!isset($agent))
$agent=Base::instance()->get('AGENT');
$agent=Base::instance()->AGENT;
return (bool)preg_match('/('.self::UA_Desktop.')/i',$agent) &&
!$this->ismobile($agent);
}
Expand All @@ -120,7 +120,7 @@ function isdesktop($agent=NULL) {
**/
function ismobile($agent=NULL) {
if (!isset($agent))
$agent=Base::instance()->get('AGENT');
$agent=Base::instance()->AGENT;
return (bool)preg_match('/('.self::UA_Mobile.')/i',$agent);
}

Expand All @@ -131,7 +131,7 @@ function ismobile($agent=NULL) {
**/
function isbot($agent=NULL) {
if (!isset($agent))
$agent=Base::instance()->get('AGENT');
$agent=Base::instance()->AGENT;
return (bool)preg_match('/('.self::UA_Bot.')/i',$agent);
}

Expand Down
6 changes: 3 additions & 3 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ protected function _smtp($id,$pw) {
stream_set_blocking($socket,TRUE);
$dialog();
$fw=Base::instance();
$dialog('EHLO '.$fw->get('HOST'));
$dialog('EHLO '.$fw->HOST);
if (strtolower($this->args['scheme'])=='tls') {
$dialog('STARTTLS');
stream_socket_enable_crypto(
$socket,TRUE,STREAM_CRYPTO_METHOD_TLS_CLIENT);
$dialog('EHLO '.$fw->get('HOST'));
$dialog('EHLO '.$fw->HOST);
}
// Authenticate
$dialog('AUTH LOGIN');
Expand Down Expand Up @@ -196,7 +196,7 @@ function login($id,$pw,$realm=NULL) {
**/
function basic($func=NULL) {
$fw=Base::instance();
$realm=$fw->get('REALM');
$realm=$fw->REALM;
$hdr=NULL;
if (isset($_SERVER['HTTP_AUTHORIZATION']))
$hdr=$_SERVER['HTTP_AUTHORIZATION'];
Expand Down
30 changes: 14 additions & 16 deletions base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1370,11 +1370,9 @@ function reroute($url=NULL,$permanent=FALSE) {
if (is_array($url))
$url=call_user_func_array([$this,'alias'],$url);
elseif (preg_match('/^(?:@?([^\/()?]+)(?:(\(.+?)\))*(\?.+)*)/',
$url,$parts)) {
if (empty($this->hive['ALIASES'][$parts[1]]))
user_error(sprintf(self::E_Named,$parts[1]),E_USER_ERROR);
$url,$parts) &&
isset($this->hive['ALIASES'][$parts[1]]))
$url=$this->hive['ALIASES'][$parts[1]];
}
$url=$this->build($url,isset($parts[2])?$this->parse($parts[2]):[]).
(isset($parts[3])?$parts[3]:'');
if (($handler=$this->hive['ONREROUTE']) &&
Expand Down Expand Up @@ -2602,12 +2600,12 @@ function load($dsn) {
// Auto-detect
current($grep):
// Use filesystem as fallback
('folder='.$fw->get('TEMP').'cache/');
('folder='.$fw->TEMP.'cache/');
if (preg_match('/^folder\h*=\h*(.+)/',$dsn,$parts) &&
!is_dir($parts[1]))
mkdir($parts[1],Base::MODE,TRUE);
}
$this->prefix=$fw->get('SEED');
$this->prefix=$fw->SEED;
return $this->dsn=$dsn;
}

Expand Down Expand Up @@ -2698,11 +2696,11 @@ protected function sandbox(array $hive=NULL,$mime=NULL) {
$hive=$fw->hive();
}
if ($this->level<1 || $implicit) {
if (!$fw->get('CLI') && !headers_sent() &&
if (!$fw->CLI && !headers_sent() &&
!preg_grep ('/^Content-Type:/',headers_list()))
header('Content-Type: '.($mime?:'text/html').'; '.
'charset='.$fw->get('ENCODING'));
if ($fw->get('ESCAPE'))
'charset='.$fw->ENCODING);
if ($fw->ESCAPE)
$hive=$this->esc($hive);
if (isset($hive['ALIASES']))
$hive['ALIASES']=$fw->build($hive['ALIASES']);
Expand Down Expand Up @@ -2731,7 +2729,7 @@ function render($file,$mime=NULL,array $hive=NULL,$ttl=0) {
$cache=Cache::instance();
if ($cache->exists($hash=$fw->hash($file),$data))
return $data;
foreach ($fw->split($fw->get('UI')) as $dir)
foreach ($fw->split($fw->UI) as $dir)
if (is_file($this->file=$fw->fixslashes($dir.$file))) {
if (isset($_COOKIE[session_name()]) &&
!headers_sent() && session_status()!=PHP_SESSION_ACTIVE)
Expand Down Expand Up @@ -2847,10 +2845,10 @@ function resolve($str,array $hive=NULL,$ttl=0,$persist=FALSE) {
if ($cache->exists($hash=$fw->hash($str),$data))
return $data;
if ($persist) {
if (!is_dir($tmp=$fw->get('TEMP')))
if (!is_dir($tmp=$fw->TEMP))
mkdir($tmp,Base::MODE,TRUE);
if (!is_file($this->file=($tmp.
$fw->get('SEED').'.'.$hash.'.php'))) {
$fw->SEED.'.'.$hash.'.php'))) {
// Remove PHP code and comments
$text=preg_replace(
'/\h*<\?(?!xml)(?:php|\s*=)?.+?\?>\h*|'.
Expand All @@ -2866,7 +2864,7 @@ function resolve($str,array $hive=NULL,$ttl=0,$persist=FALSE) {
else {
if (!$hive)
$hive=$fw->hive();
if ($fw->get('ESCAPE'))
if ($fw->ESCAPE)
$hive=$this->esc($hive);
extract($hive);
unset($hive);
Expand All @@ -2890,14 +2888,14 @@ function resolve($str,array $hive=NULL,$ttl=0,$persist=FALSE) {
function render($file,$mime=NULL,array $hive=NULL,$ttl=0) {
$fw=Base::instance();
$cache=Cache::instance();
if (!is_dir($tmp=$fw->get('TEMP')))
if (!is_dir($tmp=$fw->TEMP))
mkdir($tmp,Base::MODE,TRUE);
foreach ($fw->split($fw->get('UI')) as $dir) {
foreach ($fw->split($fw->UI) as $dir) {
if ($cache->exists($hash=$fw->hash($dir.$file),$data))
return $data;
if (is_file($view=$fw->fixslashes($dir.$file))) {
if (!is_file($this->file=($tmp.
$fw->get('SEED').'.'.$fw->hash($view).'.php')) ||
$fw->SEED.'.'.$fw->hash($view).'.php')) ||
filemtime($this->file)<filemtime($view)) {
// Remove PHP code and comments
$text=preg_replace(
Expand Down
2 changes: 1 addition & 1 deletion basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ function drop() {
**/
function copyfrom($var) {
if (is_string($var))
$var=\Base::instance()->get($var);
$var=\Base::instance()->$var;
foreach ($var as $key=>$val)
$this->set($key,$val);
}
Expand Down
6 changes: 3 additions & 3 deletions db/jig/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function find($filter=NULL,array $options=NULL,$ttl=0,$log=TRUE) {
$db=$this->db;
$now=microtime(TRUE);
$data=[];
if (!$fw->get('CACHE') || !$ttl || !($cached=$cache->exists(
if (!$fw->CACHE || !$ttl || !($cached=$cache->exists(
$hash=$fw->hash($this->db->dir().
$fw->stringify([$filter,$options])).'.jig',$data)) ||
$cached[0]+$ttl<microtime(TRUE)) {
Expand Down Expand Up @@ -258,7 +258,7 @@ function($val1,$val2) use($cols) {
}
$data=array_slice($data,
$options['offset'],$options['limit']?:NULL,TRUE);
if ($fw->get('CACHE') && $ttl)
if ($fw->CACHE && $ttl)
// Save to cache backend
$cache->set($hash,$data,$ttl);
}
Expand Down Expand Up @@ -428,7 +428,7 @@ function reset() {
**/
function copyfrom($var,$func=NULL) {
if (is_string($var))
$var=\Base::instance()->get($var);
$var=\Base::instance()->$var;
if ($func)
$var=call_user_func($func,$var);
foreach ($var as $key=>$val)
Expand Down
13 changes: 7 additions & 6 deletions db/jig/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ function read($id) {
$fw=\Base::instance();
if (!isset($this->onsuspect) ||
$fw->call($this->onsuspect,[$this,$id])===FALSE) {
//NB: `session_destroy` can't be called at that stage (`session_start` not completed)
// NB: `session_destroy` can't be called at that stage;
// `session_start` not completed
$this->destroy($id);
$this->close();
$fw->clear('COOKIE.'.session_name());
unset($fw->{'COOKIE.'.session_name()});
$fw->error(403);
}
}
Expand Down Expand Up @@ -178,12 +179,12 @@ function __construct(\DB\Jig $db,$file='sessions',$onsuspect=NULL,$key=NULL) {
);
register_shutdown_function('session_commit');
$fw=\Base::instance();
$headers=$fw->get('HEADERS');
$this->_csrf=$fw->get('SEED').'.'.$fw->hash(mt_rand());
$headers=$fw->HEADERS;
$this->_csrf=$fw->SEED.'.'.$fw->hash(mt_rand());
if ($key)
$fw->set($key,$this->_csrf);
$fw->$key=$this->_csrf;
$this->_agent=isset($headers['User-Agent'])?$headers['User-Agent']:'';
$this->_ip=$fw->get('IP');
$this->_ip=$fw->IP;
}

}
8 changes: 4 additions & 4 deletions db/mongo/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) {
]
);
$tmp=$this->db->selectcollection(
$fw->get('HOST').'.'.$fw->get('BASE').'.'.
$fw->HOST.'.'.$fw->BASE.'.'.
uniqid(NULL,TRUE).'.tmp'
);
$tmp->batchinsert($grp['retval'],['w'=>1]);
Expand Down Expand Up @@ -179,7 +179,7 @@ function select($fields=NULL,$filter=NULL,array $options=NULL,$ttl=0) {
}
if ($options['group'])
$tmp->drop();
if ($fw->get('CACHE') && $ttl)
if ($fw->CACHE && $ttl)
// Save to cache backend
$cache->set($hash,$result,$ttl);
}
Expand Down Expand Up @@ -222,7 +222,7 @@ function count($filter=NULL,array $options=NULL,$ttl=0) {
[$filter])).'.mongo',$result)) || !$ttl ||
$cached[0]+$ttl<microtime(TRUE)) {
$result=$this->collection->count($filter?:[]);
if ($fw->get('CACHE') && $ttl)
if ($fw->CACHE && $ttl)
// Save to cache backend
$cache->set($hash,$result,$ttl);
}
Expand Down Expand Up @@ -339,7 +339,7 @@ function reset() {
**/
function copyfrom($var,$func=NULL) {
if (is_string($var))
$var=\Base::instance()->get($var);
$var=\Base::instance()->$var;
if ($func)
$var=call_user_func($func,$var);
foreach ($var as $key=>$val)
Expand Down
13 changes: 7 additions & 6 deletions db/mongo/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ function read($id) {
$fw=\Base::instance();
if (!isset($this->onsuspect) ||
$fw->call($this->onsuspect,[$this,$id])===FALSE) {
//NB: `session_destroy` can't be called at that stage (`session_start` not completed)
// NB: `session_destroy` can't be called at that stage;
// `session_start` not completed
$this->destroy($id);
$this->close();
$fw->clear('COOKIE.'.session_name());
unset($fw->{'COOKIE.'.session_name()});
$fw->error(403);
}
}
Expand Down Expand Up @@ -178,12 +179,12 @@ function __construct(\DB\Mongo $db,$table='sessions',$onsuspect=NULL,$key=NULL)
);
register_shutdown_function('session_commit');
$fw=\Base::instance();
$headers=$fw->get('HEADERS');
$this->_csrf=$fw->get('SEED').'.'.$fw->hash(mt_rand());
$headers=$fw->HEADERS;
$this->_csrf=$fw->SEED.'.'.$fw->hash(mt_rand());
if ($key)
$fw->set($key,$this->_csrf);
$fw->$key=$this->_csrf;
$this->_agent=isset($headers['User-Agent'])?$headers['User-Agent']:'';
$this->_ip=$fw->get('IP');
$this->_ip=$fw->IP;
}

}
10 changes: 5 additions & 5 deletions db/sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function exec($cmds,$args=NULL,$ttl=0,$log=TRUE,$stamp=FALSE) {
continue;
$now=microtime(TRUE);
$keys=$vals=[];
if ($fw->get('CACHE') && $ttl && ($cached=$cache->exists(
if ($fw->CACHE && $ttl && ($cached=$cache->exists(
$hash=$fw->hash($this->dsn.$cmd.
$fw->stringify($arg)).($tag?'.'.$tag:'').'.sql',$result)) &&
$cached[0]+$ttl>microtime(TRUE)) {
Expand Down Expand Up @@ -246,7 +246,7 @@ function exec($cmds,$args=NULL,$ttl=0,$log=TRUE,$stamp=FALSE) {
$result[$pos][trim($key,'\'"[]`')]=$val;
}
$this->rows=count($result);
if ($fw->get('CACHE') && $ttl)
if ($fw->CACHE && $ttl)
// Save to cache backend
$cache->set($hash,$result,$ttl);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ function log($flag=TRUE) {
function schema($table,$fields=NULL,$ttl=0) {
$fw=\Base::instance();
$cache=\Cache::instance();
if ($fw->get('CACHE') && $ttl &&
if ($fw->CACHE && $ttl &&
($cached=$cache->exists(
$hash=$fw->hash($this->dsn.$table).'.schema',$result)) &&
$cached[0]+$ttl>microtime(TRUE))
Expand Down Expand Up @@ -386,7 +386,7 @@ function schema($table,$fields=NULL,$ttl=0) {
'pkey'=>$row[$val[6]]==$val[7]
];
}
if ($fw->get('CACHE') && $ttl)
if ($fw->CACHE && $ttl)
// Save to cache backend
$cache->set($hash,$rows,$ttl);
return $rows;
Expand Down Expand Up @@ -501,7 +501,7 @@ function __construct($dsn,$user=NULL,$pw=NULL,array $options=NULL) {
$options=[];
if (isset($parts[0]) && strstr($parts[0],':',TRUE)=='mysql')
$options+=[\PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES '.
strtolower(str_replace('-','',$fw->get('ENCODING'))).';'];
strtolower(str_replace('-','',$fw->ENCODING)).';'];
$this->pdo=new \PDO($dsn,$user,$pw,$options);
$this->engine=$this->pdo->getattribute(\PDO::ATTR_DRIVER_NAME);
}
Expand Down
2 changes: 1 addition & 1 deletion db/sql/mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ function reset() {
**/
function copyfrom($var,$func=NULL) {
if (is_string($var))
$var=\Base::instance()->get($var);
$var=\Base::instance()->$var;
if ($func)
$var=call_user_func($func,$var);
foreach ($var as $key=>$val)
Expand Down
10 changes: 5 additions & 5 deletions db/sql/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function read($id) {
//NB: `session_destroy` can't be called at that stage (`session_start` not completed)
$this->destroy($id);
$this->close();
$fw->clear('COOKIE.'.session_name());
unset($fw->{'COOKIE.'.session_name()});
$fw->error(403);
}
}
Expand Down Expand Up @@ -200,12 +200,12 @@ function __construct(\DB\SQL $db,$table='sessions',$force=TRUE,$onsuspect=NULL,$
);
register_shutdown_function('session_commit');
$fw=\Base::instance();
$headers=$fw->get('HEADERS');
$this->_csrf=$fw->get('SEED').'.'.$fw->hash(mt_rand());
$headers=$fw->HEADERS;
$this->_csrf=$fw->SEED.'.'.$fw->hash(mt_rand());
if ($key)
$fw->set($key,$this->_csrf);
$fw->$key=$this->_csrf;
$this->_agent=isset($headers['User-Agent'])?$headers['User-Agent']:'';
$this->_ip=$fw->get('IP');
$this->_ip=$fw->IP;
}

}
Loading

0 comments on commit b11c03e

Please sign in to comment.