Files
pathfinder/app/lib/db/jig/mapper.php
Mark Friedrich ecd505a202 v1.0.0 (#183)
* #84 test data dump from CREST login

* updated "credits" dialog (Google+ link)
fixed login form layout

* updated Cortex Data-Mapper

* - #84 CREST Login (WIP)
- New CREST controller
- Database restructuring
- improved type-casting for some controller functions
- New login process
- Fixed some bugs during the setup process (/setup root)
- Added CREST request caching by response headers

* pathfinder-84 [Feature Request] CREST Pilot Tracking, many smaller Bugfixes

* pathfinder-84 [Feature Request] added develop JS files

* closed #121 fixed wormhole signature type caching

* closed #120 removed map-loading animation for larger maps (same behaviour as IGB)

* closed #119 fixed wormhole signature id count

* closed #114 Added check for already existing system when adding a new one. (fixed PDO 'duplicate entry' error)

* closed #112 fixed DataTables error for missing "status" data (signature table)

* closed #111 fixed convertDataToUTC(); client side date transformation

* closed #109 fixed system TrueSec rounding

* closed #103 fixed system updated timestamp in getData()

* fixed CSS class for secStatus in Routes module

* closed #121 fixed wormhole signature type caching

* changed dateTime format from German to US format
fixed some minor bugs in signatureTable module

* closed #81 fixed "signature type" overwriting by "signature reader" update

* closed #106 added new signature_types form C5/6 wormholes (gas/ore)

* closed #129 fixed parameter hinting

* closed #131 new "route search" algorithm, added current map systems to live search, added refresh/update functionality for each found route, added bulk route refresh function, added "meta map" route search (search on multiple maps), added route "filters" (restrict search on "stargates", "wormholes", "jumpbridges"), added route "filter" for wormholes (reduced/critical wormholes)
closed #89 fixed "loop connections" on same system
#84 added error messages for "invalid" CREST "Client ID"
added "bootboxjs" (customized styled checkboxes/radio buttons) CSS only
"Font Awesome" version upgrade 4.4.0 -> 4.61
"Bootbox.js" version upgrade 4.3.0 -> 4.4.0
fixed "system dialog" (added responsive layout)

* closed #134  fixed db column type DT_INT (8 bytes) to DT_BIGINT

* closed #138 added new cookie based login

* closed #137 fixed javascript errors on trying to establish an "invalid" connection

* - #84, #138 improved "character selection" on login page (expired cookies are deleted, character panel layout improvements)
- added new "Server info panel" to the login page
- added new cronjob to delete expired cookie authentication data

* #138 enables character switching between characters which have same user

* - PHP Framework upgrade 3.5.0 -> 3.5.1 (fixes some issues with CREST cURL caching, and SESSION management)
- #138 added "cookie logout" to "logout" menu entry

* - updated "feature page" with new feature descriptions and label
- added some new images to the "feature gallery"
- removed "beta" status from "magnetizing" feature on map menu
- hide "server status" panel on "mobile" breakpoint

* - #138 clear character authentication data on sold characters

* closed #142 added custom "onsuspect()" session handler

* #142 do not log suspect if no file is defined in pathfinder.ini

* #142 added NullSec Data/Relic sites to C1/2/3 wormholes as signature option

* #144 fixed "Character not found" warning

* #144 fixed "Character not found" warning

* closed #144 fixed broken routes panel in IGB

* updated README.md for upcoming release

* #147 response header validation

* #149 changed comment for 'BASE' framework var

* fixed map  import

* - added minimal SDE dump (EVE Online: Citadel)
- #147 improved CREST API error logging (WIP)
- improved SSO controller (removed access_token from public endpoints)

* closed #154 added alliance maps to CREST API

* - updated Gulp build dependencies
- increased CREST timeout from 3s -> 4s
- added "Accept" Headers for some CREST endpoints

* cloased #147

* - closed #153 added character verification check for getAll(); Signatures Ajax endpoint

* - updated README.md (added Slack developer chat information)

* Bugfix frig holes (#159)

* added missing frigate wormholes and fixed Q003 destination in shattered wormholes

* changed C7 to 0.0 for Q003

* - fixed broken "graph" data for system

* added a  "failover" system  for bad crest requests (HTTP status 5xx,.. )

* Red Gaint => Red Giant (#161)

* closed #163 added CREST endpoint support for "waypoints"

* fixed typo

* closed #160 fixed tooltip container

* - added new features to login page

* closes #154 added alliance map support

* fixed XML path for cronjobs

* fixed a bug with inactive "private" maps

* closes #175 added alternative environment configuration

* - v1.0.0  build
2016-06-03 23:05:34 +02:00

477 lines
11 KiB
PHP

<?php
/*
Copyright (c) 2009-2015 F3::Factory/Bong Cosca, All rights reserved.
This file is part of the Fat-Free Framework (http://fatfreeframework.com).
This is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or later.
Fat-Free Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
*/
namespace DB\Jig;
//! Flat-file DB mapper
class Mapper extends \DB\Cursor {
protected
//! Flat-file DB wrapper
$db,
//! Data file
$file,
//! Document identifier
$id,
//! Document contents
$document=array();
/**
* Return database type
* @return string
**/
function dbtype() {
return 'Jig';
}
/**
* Return TRUE if field is defined
* @return bool
* @param $key string
**/
function exists($key) {
return array_key_exists($key,$this->document);
}
/**
* Assign value to field
* @return scalar|FALSE
* @param $key string
* @param $val scalar
**/
function set($key,$val) {
return ($key=='_id')?FALSE:($this->document[$key]=$val);
}
/**
* Retrieve value of field
* @return scalar|FALSE
* @param $key string
**/
function &get($key) {
if ($key=='_id')
return $this->id;
if (array_key_exists($key,$this->document))
return $this->document[$key];
user_error(sprintf(self::E_Field,$key),E_USER_ERROR);
}
/**
* Delete field
* @return NULL
* @param $key string
**/
function clear($key) {
if ($key!='_id')
unset($this->document[$key]);
}
/**
* Convert array to mapper object
* @return object
* @param $id string
* @param $row array
**/
protected function factory($id,$row) {
$mapper=clone($this);
$mapper->reset();
$mapper->id=$id;
foreach ($row as $field=>$val)
$mapper->document[$field]=$val;
$mapper->query=array(clone($mapper));
if (isset($mapper->trigger['load']))
\Base::instance()->call($mapper->trigger['load'],$mapper);
return $mapper;
}
/**
* Return fields of mapper object as an associative array
* @return array
* @param $obj object
**/
function cast($obj=NULL) {
if (!$obj)
$obj=$this;
return $obj->document+array('_id'=>$this->id);
}
/**
* Convert tokens in string expression to variable names
* @return string
* @param $str string
**/
function token($str) {
$self=$this;
$str=preg_replace_callback(
'/(?<!\w)@(\w(?:[\w\.\[\]])*)/',
function($token) use($self) {
// Convert from JS dot notation to PHP array notation
return '$'.preg_replace_callback(
'/(\.\w+)|\[((?:[^\[\]]*|(?R))*)\]/',
function($expr) use($self) {
$fw=\Base::instance();
return
'['.
($expr[1]?
$fw->stringify(substr($expr[1],1)):
(preg_match('/^\w+/',
$mix=$self->token($expr[2]))?
$fw->stringify($mix):
$mix)).
']';
},
$token[1]
);
},
$str
);
return trim($str);
}
/**
* Return records that match criteria
* @return static[]|FALSE
* @param $filter array
* @param $options array
* @param $ttl int
* @param $log bool
**/
function find($filter=NULL,array $options=NULL,$ttl=0,$log=TRUE) {
if (!$options)
$options=array();
$options+=array(
'order'=>NULL,
'limit'=>0,
'offset'=>0
);
$fw=\Base::instance();
$cache=\Cache::instance();
$db=$this->db;
$now=microtime(TRUE);
$data=array();
if (!$fw->get('CACHE') || !$ttl || !($cached=$cache->exists(
$hash=$fw->hash($this->db->dir().
$fw->stringify(array($filter,$options))).'.jig',$data)) ||
$cached[0]+$ttl<microtime(TRUE)) {
$data=$db->read($this->file);
if (is_null($data))
return FALSE;
foreach ($data as $id=>&$doc) {
$doc['_id']=$id;
unset($doc);
}
if ($filter) {
if (!is_array($filter))
return FALSE;
// Normalize equality operator
$expr=preg_replace('/(?<=[^<>!=])=(?!=)/','==',$filter[0]);
// Prepare query arguments
$args=isset($filter[1]) && is_array($filter[1])?
$filter[1]:
array_slice($filter,1,NULL,TRUE);
$args=is_array($args)?$args:array(1=>$args);
$keys=$vals=array();
$tokens=array_slice(
token_get_all('<?php '.$this->token($expr)),1);
$data=array_filter($data,
function($_row) use($fw,$args,$tokens) {
$_expr='';
$ctr=0;
$named=FALSE;
foreach ($tokens as $token) {
if (is_string($token))
if ($token=='?') {
// Positional
$ctr++;
$key=$ctr;
}
else {
if ($token==':')
$named=TRUE;
else
$_expr.=$token;
continue;
}
elseif ($named &&
token_name($token[0])=='T_STRING') {
$key=':'.$token[1];
$named=FALSE;
}
else {
$_expr.=$token[1];
continue;
}
$_expr.=$fw->stringify(
is_string($args[$key])?
addcslashes($args[$key],'\''):
$args[$key]);
}
// Avoid conflict with user code
unset($fw,$tokens,$args,$ctr,$token,$key,$named);
extract($_row);
// Evaluate pseudo-SQL expression
return eval('return '.$_expr.';');
}
);
}
if (isset($options['order'])) {
$cols=$fw->split($options['order']);
uasort(
$data,
function($val1,$val2) use($cols) {
foreach ($cols as $col) {
$parts=explode(' ',$col,2);
$order=empty($parts[1])?
SORT_ASC:
constant($parts[1]);
$col=$parts[0];
if (!array_key_exists($col,$val1))
$val1[$col]=NULL;
if (!array_key_exists($col,$val2))
$val2[$col]=NULL;
list($v1,$v2)=array($val1[$col],$val2[$col]);
if ($out=strnatcmp($v1,$v2)*
(($order==SORT_ASC)*2-1))
return $out;
}
return 0;
}
);
}
$data=array_slice($data,
$options['offset'],$options['limit']?:NULL,TRUE);
if ($fw->get('CACHE') && $ttl)
// Save to cache backend
$cache->set($hash,$data,$ttl);
}
$out=array();
foreach ($data as $id=>&$doc) {
unset($doc['_id']);
$out[]=$this->factory($id,$doc);
unset($doc);
}
if ($log && isset($args)) {
if ($filter)
foreach ($args as $key=>$val) {
$vals[]=$fw->stringify(is_array($val)?$val[0]:$val);
$keys[]='/'.(is_numeric($key)?'\?':preg_quote($key)).'/';
}
$db->jot('('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
$this->file.' [find] '.
($filter?preg_replace($keys,$vals,$filter[0],1):''));
}
return $out;
}
/**
* Count records that match criteria
* @return int
* @param $filter array
* @param $ttl int
**/
function count($filter=NULL,$ttl=0) {
$now=microtime(TRUE);
$out=count($this->find($filter,NULL,$ttl,FALSE));
$this->db->jot('('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
$this->file.' [count] '.($filter?json_encode($filter):''));
return $out;
}
/**
* Return record at specified offset using criteria of previous
* load() call and make it active
* @return array
* @param $ofs int
**/
function skip($ofs=1) {
$this->document=($out=parent::skip($ofs))?$out->document:array();
$this->id=$out?$out->id:NULL;
if ($this->document && isset($this->trigger['load']))
\Base::instance()->call($this->trigger['load'],$this);
return $out;
}
/**
* Insert new record
* @return array
**/
function insert() {
if ($this->id)
return $this->update();
$db=$this->db;
$now=microtime(TRUE);
while (($id=uniqid(NULL,TRUE)) &&
($data=&$db->read($this->file)) && isset($data[$id]) &&
!connection_aborted())
usleep(mt_rand(0,100));
$this->id=$id;
$pkey=array('_id'=>$this->id);
if (isset($this->trigger['beforeinsert']) &&
\Base::instance()->call($this->trigger['beforeinsert'],
array($this,$pkey))===FALSE)
return $this->document;
$data[$id]=$this->document;
$db->write($this->file,$data);
$db->jot('('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
$this->file.' [insert] '.json_encode($this->document));
if (isset($this->trigger['afterinsert']))
\Base::instance()->call($this->trigger['afterinsert'],
array($this,$pkey));
$this->load(array('@_id=?',$this->id));
return $this->document;
}
/**
* Update current record
* @return array
**/
function update() {
$db=$this->db;
$now=microtime(TRUE);
$data=&$db->read($this->file);
if (isset($this->trigger['beforeupdate']) &&
\Base::instance()->call($this->trigger['beforeupdate'],
array($this,array('_id'=>$this->id)))===FALSE)
return $this->document;
$data[$this->id]=$this->document;
$db->write($this->file,$data);
$db->jot('('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
$this->file.' [update] '.json_encode($this->document));
if (isset($this->trigger['afterupdate']))
\Base::instance()->call($this->trigger['afterupdate'],
array($this,array('_id'=>$this->id)));
return $this->document;
}
/**
* Delete current record
* @return bool
* @param $filter array
**/
function erase($filter=NULL) {
$db=$this->db;
$now=microtime(TRUE);
$data=&$db->read($this->file);
$pkey=array('_id'=>$this->id);
if ($filter) {
foreach ($this->find($filter,NULL,FALSE) as $mapper)
if (!$mapper->erase())
return FALSE;
return TRUE;
}
elseif (isset($this->id)) {
unset($data[$this->id]);
parent::erase();
}
else
return FALSE;
if (isset($this->trigger['beforeerase']) &&
\Base::instance()->call($this->trigger['beforeerase'],
array($this,$pkey))===FALSE)
return FALSE;
$db->write($this->file,$data);
if ($filter) {
$args=isset($filter[1]) && is_array($filter[1])?
$filter[1]:
array_slice($filter,1,NULL,TRUE);
$args=is_array($args)?$args:array(1=>$args);
foreach ($args as $key=>$val) {
$vals[]=\Base::instance()->
stringify(is_array($val)?$val[0]:$val);
$keys[]='/'.(is_numeric($key)?'\?':preg_quote($key)).'/';
}
}
$db->jot('('.sprintf('%.1f',1e3*(microtime(TRUE)-$now)).'ms) '.
$this->file.' [erase] '.
($filter?preg_replace($keys,$vals,$filter[0],1):''));
if (isset($this->trigger['aftererase']))
\Base::instance()->call($this->trigger['aftererase'],
array($this,$pkey));
return TRUE;
}
/**
* Reset cursor
* @return NULL
**/
function reset() {
$this->id=NULL;
$this->document=array();
parent::reset();
}
/**
* Hydrate mapper object using hive array variable
* @return NULL
* @param $var array|string
* @param $func callback
**/
function copyfrom($var,$func=NULL) {
if (is_string($var))
$var=\Base::instance()->get($var);
if ($func)
$var=call_user_func($func,$var);
foreach ($var as $key=>$val)
$this->set($key,$val);
}
/**
* Populate hive array variable with mapper fields
* @return NULL
* @param $key string
**/
function copyto($key) {
$var=&\Base::instance()->ref($key);
foreach ($this->document as $key=>$field)
$var[$key]=$field;
}
/**
* Return field names
* @return array
**/
function fields() {
return array_keys($this->document);
}
/**
* Retrieve external iterator for fields
* @return object
**/
function getiterator() {
return new \ArrayIterator($this->cast());
}
/**
* Instantiate class
* @return void
* @param $db object
* @param $file string
**/
function __construct(\DB\Jig $db,$file) {
$this->db=$db;
$this->file=$file;
$this->reset();
}
}