Commit 33d89c01 authored by Alex Ne's avatar Alex Ne

Дополнение с управлению сессиями

parent 04eb9d45
<?php
namespace X\DB;
class Table
{
private $driver;
function __construct($driver) { $this->driver = $driver; }
public function __call($name, $arguments)
{
return new TableItem($name, $this->driver);
}
}
?>
\ No newline at end of file
<?php
namespace X\DB;
class TableItem
{
protected $table_name;
protected $sql = "";
protected $sql_where = "";
protected $sql_type;
protected $driver = null;
function __construct($name, $driver)
{
$this->driver = $driver;
$this->table_name = $name;
}
public function insert($Data)
{
$this->sql = "INSERT INTO ".$this->build_insert($Data);
$this->sql_type = "insert";
return $this;
}
public function replace($Data)
{
$this->sql = "REPLACE INTO ".$this->build_insert($Data);
$this->sql_type = "insert";
return $this;
}
private function build_insert($Data)
{
$keys = "`".implode('`,`', array_keys($Data))."`";
$_values = array_values($Data);
for ($i=0; $i < count($_values); $i++)
{
$_values[$i] = $this->escape($_values[$i]);
}
$values = "'".implode("','", $_values)."'";
return "`{$this->table_name}` ({$keys}) VALUES ({$values})";
}
public function update($Data)
{
if( !is_array($Data) || count($Data) == 0 ) return false;
$params = "";
foreach ($Data as $key => $value) {
$params .= strlen($params)==0?"":", ";
$params .= "`".$key."` = '".$this->escape($value)."'";
}
$this->sql = "UPDATE `{$this->table_name}` SET {$params}";
$this->sql_type = "update";
return $this;
}
public function select($columns = [])
{
if(count($columns)>0)
$columns = "`".implode("`,`", $columns)."`";
else $columns = "*";
$this->sql_type = "select";
$this->sql = "SELECT {$columns} FROM `{$this->table_name}`";
return $this;
}
public function where($data)
{
$this->sql_where = " WHERE " . $this->where_clause($data);
return $this;
}
private function where_clause($data, $operator = false, $gr=false)
{
$where = "";
if(is_array($data))
{
if($operator == false && count($data)==2)
{
$where .= $this->where_operation($data[0], $data[1])." ";
}
else
{
$operations = [];
foreach ($data as $key => $value)
{
if( strtolower($key) == "and" || strtolower($key) == "or" )
$operations[] = (($gr)?"(":"").
$this->where_clause($value, strtolower($key),true).
(($gr)?") ":" ");
else $operations[] = $this->where_operation($key, $value);
}
$where .= implode(" ".strtoupper($operator)." ", $operations);
}
return $where;
}
if(is_string($data)) return $data;
}
private function array_quote($array)
{
$temp = array();
foreach ($array as $value)
{
$temp[] = is_int($value) ? $value : $this->escape($value);
}
return implode($temp, ',');
}
private function where_operation($key, $value)
{
$wheres = [];
$type = gettype($value);
preg_match('/(#?)([\w\.\-]+)(\[(\>|\>\=|\<|\<\=|\!|\<\>|\>\<|\!?~)\])?/i', $key, $match);
$column = "`".$match[ 2 ]."`";
if (isset($match[ 4 ]))
{
$operator = $match[ 4 ];
if ($operator == '!')
{
switch ($type)
{
case 'NULL':
$wheres[] = $column . ' IS NOT NULL';
break;
case 'array':
$wheres[] = $column . ' NOT IN (' . $this->array_quote($value) . ')';
break;
case 'integer':
case 'double':
$wheres[] = $column . ' != ' . $value;
break;
case 'boolean':
$wheres[] = $column . ' != ' . ($value ? '1' : '0');
break;
case 'string':
$wheres[] = $column . ' != ' . "'".$this->escape($value)."'";
break;
}
}
if ($operator == '<>' || $operator == '><')
{
if ($type == 'array')
{
if ($operator == '><')
{
$column .= ' NOT';
}
if (is_numeric($value[ 0 ]) && is_numeric($value[ 1 ]))
{
$wheres[] = '(' . $column . ' BETWEEN ' . $value[ 0 ] . ' AND ' . $value[ 1 ] . ')';
}
else
{
$wheres[] = '(' . $column . ' BETWEEN ' . "'".$this->escape($value[ 0 ])."'" . ' AND ' . "'".$this->escape($value[ 1 ])."'" . ')';
}
}
}
if ($operator == '~' || $operator == '!~')
{
if ($type != 'array')
{
$value = array($value);
}
$like_clauses = array();
foreach ($value as $item)
{
$item = strval($item);
$suffix = mb_substr($item, -1, 1);
if ($suffix === '_')
{
$item = substr_replace($item, '%', -1);
}
elseif ($suffix === '%')
{
$item = '%' . substr_replace($item, '', -1, 1);
}
elseif (preg_match('/^(?!%).+(?<!%)$/', $item))
{
$item = '%' . $item . '%';
}
$like_clauses[] = $column . ($operator === '!~' ? ' NOT' : '') . ' LIKE ' . "'".$this->escape($item)."'";
}
$wheres[] = implode(' OR ', $like_clauses);
}
if (in_array($operator, array('>', '>=', '<', '<=')))
{
if (is_numeric($value))
{
$wheres[] = $column . ' ' . $operator . ' ' . $value;
}
else
{
$wheres[] = $column . ' ' . $operator . ' ' . "'".$this->escape($value)."'";
}
}
}
else
{
switch ($type)
{
case 'NULL':
$wheres[] = $column . ' IS NULL';
break;
case 'array':
$wheres[] = $column . ' IN (' . $this->array_quote($value) . ')';
break;
case 'integer':
case 'double':
$wheres[] = $column . ' = ' . $value;
break;
case 'boolean':
$wheres[] = $column . ' = ' . ($value ? '1' : '0');
break;
case 'string':
$wheres[] = $column . ' = ' . "'".$this->escape($value)."'";
break;
}
}
return implode(' ', $wheres);
}
private function escape($data)
{
if($this->driver instanceof \X_DB_MySQLi) return $this->driver->esc($data);
}
public function getSQL()
{
return $this->sql.$this->sql_where;
}
public function exec($op1=false,$op2=false,$op3=false)
{
if($this->sql_type == "update" && $this->driver instanceof \X_DB_MySQLi)
return $this->driver->rq($this->sql.$this->sql_where);
if($this->sql_type == "insert" && $this->driver instanceof \X_DB_MySQLi)
return $this->driver->insert($this->sql);
if($this->sql_type == "select" && $this->driver instanceof \X_DB_MySQLi)
return $this->driver->get($this->sql.$this->sql_where,$op1,$op2,$op3);
else throw new \Exception("Internal error", 0);
}
}
?>
\ No newline at end of file
<?php
class X_Session
{
protected $key, $algo;
use X\Tool\urlSafe;
protected $key, $algo, $session = false, $IV = null;
protected $collName = "s";
function __construct()
{
......@@ -13,6 +16,11 @@ class X_Session
{
$this->key = $this->strToHex($Key);
}
public function setIV($IV)
{
$this->IV = $this->strToHex($IV);
}
public function setAlgo($Algo)
{
if(in_array($Algo, openssl_get_cipher_methods()))
......@@ -21,14 +29,65 @@ class X_Session
}
protected function crypt(Array $Data) // : string // HEX
protected function make_session($Data, $algo = "b64")
{
switch ($algo)
{
case 'b64':
return $this->session = $this->crypt_b64($Data);
break;
case 'hex':
return $this->session = $this->crypt_hex($Data);
break;
case 'bin':
return $this->session = $this->crypt_bin($Data);
break;
default:
return $this->session = $this->crypt_bin($Data);
break;
}
}
public function set_cookie(Array $Data, $algo = "b64") // : void // php7
{
setcookie($this->collName,
$this->make_session($Data, $algo),
time()+(60*60*24*30*12*10), ////////////////////////////////// TIME LIVE COOKIE 10 years
"/"
);
}
protected function crypt_bin(Array $Data) // : string // BIN
{
return bin2hex(openssl_encrypt(implode(":", $Data), $this->algo, $this->key, OPENSSL_RAW_DATA ));
return openssl_encrypt(implode(":", $Data), $this->algo, $this->key, OPENSSL_RAW_DATA, $this->IV );
}
protected function decrypt($HEX) // : Array
protected function decrypt_bin($BIN) // : Array
{
return explode(":", openssl_decrypt(hex2bin($HEX), $this->algo, $this->key, OPENSSL_RAW_DATA ));
return explode(":", openssl_decrypt($BIN, $this->algo, $this->key, OPENSSL_RAW_DATA, $this->IV ));
}
protected function crypt_hex(Array $Data) // : string // HEX
{
return bin2hex( $this->crypt_bin($Data) );
}
protected function decrypt_hex($HEX) // : Array
{
return $this->decrypt_bin( hex2bin($HEX) );
}
protected function crypt_b64(Array $Data) // : string // Base64
{
return $this->urlSafeB64Encode( $this->crypt_bin($Data) );
}
protected function decrypt_b64($B64) // : Array
{
return $this->decrypt_bin( $this->urlSafeB64Decode($B64) );
}
......@@ -38,6 +97,7 @@ class X_Session
for ($i=0; $i < strlen($string); $i++){ $hex .= dechex(ord($string[$i])); }
return $hex;
}
private function hexToStr($hex)
{
$string = "";
......
<?php
namespace X\Tool;
trait IP
{
protected function get_ip()
{
if(isset($_SERVER["REMOTE_ADDR"])) return $_SERVER["REMOTE_ADDR"];
else return false;
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment