__load($a); } public function __load(&$a) { if(!is_array($a) && !is_object($a)) thr('Object passed to O is not iterable: '.var_export($a,1)); foreach($a AS $k => $v)$this->$k = $v; return $this; } public function __get($n) { return null; } public function __toString() { return json_encode($this,JSON_UNESCAPED_UNICODE); } } function g(&$a,$k,$def=null) { if(is_array($a) && isset($a[$k])) return $a[$k]; if(is_object($a) && isset($a->$k)) return $a->$k; return $def; } function post($n) { return g($_POST,$n); } function upart($n,$def='') { global $uparts; if(empty($uparts))$uparts = explode( '/', preg_replace('/(^\/+|\/$|[\?\#].*)/','',trim(g($_GET,'__uri'))) ); return g($uparts,$n,$def); } function o($a=null){ $o = new O(); return $a ? $o->__load($a) : $o; } function thr($e) { throw new Exception($e); } function pr($a1,$a2=null) { if(!is_null($a2)) echo $a1.': '; else $a2 =& $a1; if(is_scalar($a2)) echo $a2.NL; else print_r($a2); } function cl($a1,$a2=null,$color='#d70') { $data = $a1; $caption = ''; if(!is_null($a2)) { $caption = $a1; $data = $a2; } $json = toJSON($data); echo ''; } function err($msg) { cl('ERROR',$msg,'#a00'); } function db() { return DB::$DB; } function &fget($fname) { $s = file_get_contents($fname); return $s; } function toJSON($obj,$pretty=false) { $flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES; if($pretty)$flags |= JSON_PRETTY_PRINT; return json_encode($obj,$flags); } function readJSON($fname) { return o(json_decode(file_get_contents($fname))); } function assign(&$trg,&$src,$onlyKeys=null,$exceptKeys=null) { $tobj = is_object($trg); foreach($src AS $k => $v) { if(($onlyKeys && !in_array($k, $onlyKeys)) || ($exceptKeys && in_array($k, $exceptKeys))) continue; if($tobj)$trg->$k = $v; else $trg[$k] = $v; } return $trg; } function isAssoc($arr) { if(is_object($arr))return true; if (array() === $arr) return false; return array_keys($arr) !== range(0, count($arr) - 1); } function initPath($path,$isFile=false){ if(file_exists($path))return $path; if($isFile) initPath(dirname($path)); else mkdir($path,0777,true); return $path; } function filterKeys($arr,$keys) { normlist($keys); $out = []; foreach($keys AS $k) if(isset($arr[$k])) $out[$k] = $arr[$k]; return $out; } function normlist(&$list) { if(!is_array($list)) $list = explode(',',$list); } function isExternalURL($url) { return $url[0] != '/'; } function getFileByURI($uri) { $file = WWW.$uri; if(!file_exists($file)) thr('Resource not found: '.$uri); return $file; } function signURI($uri) { if(isExternalURL($uri))return $uri; return $uri.'?'.filemtime(getFileByURI($uri)); } function css($uri,$inline=false) { if($inline)return css_inline($uri); echo ''; } function js($uri,$inline=false) { if($inline)return js_inline($uri); echo ''; } function js_inline($uri) { if(isExternalURL($uri))return js($uri); echo ''; } function css_inline($uri) { if(isExternalURL($uri))return js($uri); echo ''; } function jsvar($name,$obj) { echo ''; } function initJsApp($t) { $data = o([ 'pagename' => $t->pagename, 'modname' => $t->modname, 'modPath' => $t->modPath, 'jsPath' => $t->jsPath, 'mainJS' => $t->mainJS, 'release' => $t->release ]); assign($data,$t->jsvars); foreach($t->jsVarNames AS $n) $data->$n = $t->$n; jsvar('App',$data); } function svgSymbol($name) { return ''; } function svgSymbols($dirname) { $sym = []; foreach(scandir($dirname) AS $f) { if(substr($f,-4)!='.svg')continue; $s =& fget($dirname.'/'.$f); if(!preg_match('/]+)>(.+?)<\/svg>/s',$s,$mm)) thr('Missing or incorrect tag "svg" in file '.$f); $viewbox = ''; if(preg_match('/viewbox\=\"([^\"]+)/i',$mm[1],$mm2)) { $viewbox = $mm2[1]; } else { preg_match_all('/(width|height)\=\"([^\"]+)/', $mm[1], $mm2,PREG_SET_ORDER); $dim = []; foreach($mm2 AS $m) $dim[$m[1]=='width' ? 0 : 1] = $m[2]; $viewbox = '0 0 '.implode(' ',$dim); } $sym[] = '' .$mm[2].''; } return ''.implode(NL,$sym).''; } function e404($msg='') { exit('404 not found
'.$msg.'
'); } function redir($url) { header('Location: '.$url); exit; } $loaded_configs = []; function loadConf($src,$alias=null) { global $loaded_configs; $name = trim(substr($src,strlen(WWW)),'\\/'); $name = preg_replace('/\.[a-z\d]+$/i','',$name); $name = preg_replace('%[\\\/]%','__',$name); if(isset($loaded_configs[$name])) return $loaded_configs[$name]; if(!file_exists($src)) thr('Missing conf file '.$src); $cached = initPath(ROOT.'/tmp/conf/').$name.'.json'; if(file_exists($cached) && filemtime($cached) > filemtime($src)) { $data = o(readJSON($cached)); }else{ require_once(WWW.'/lib/hjson.php'); $parser = new HJSONParser(); $data = $parser->parse(fget($src)); //$data = o(Json5Decoder::decode(fget($src))); file_put_contents($cached,toJSON($data)); } $loaded_configs[$name] =& $data; if($alias) $loaded_configs[$alias] =& $data; return $data; } function conf($name,$key=null) { global $loaded_configs; if(!isset($loaded_configs[$name])) thr('Missing config "'.$name.'"'); $conf =& $loaded_configs[$name]; return $key ? $conf->$key : $conf; } function conf_module($key=null){ return conf('module',$key); } function conf_site($key=null){ return conf('site',$key); } function prepareTplData() { $t = o(); $t->conf = loadConf(WWW.'/conf/site.hjson','site'); $t->release = file_get_contents(ROOT.'/.release'); return $t; } function route(&$t,$upartNum=0,$root='') { $mod = upart($upartNum); if(!$mod)$mod='main'; $t->upartNum = $upartNum; $t->modname = $mod; $t->modPath = $root.'/modules/'.$mod; $t->basePath = $t->modPath.'/'.$mod; $t->jsPath = $t->modPath.'/js'; $t->routerPath = $t->basePath.'.route.php'; $t->confPath = $t->basePath.'.hjson'; $modConf = file_exists(WWW.$t->confPath) ? loadConf(WWW.$t->confPath,'module') : o(); assign($t,$modConf); $t->modConf = $modConf; if(upart($upartNum+1)=='ajax') { $file = $t->basePath.'.ajax.php'; if(!file_exists(WWW.$file)) thr('Missing '.$file); try{ require_once WWW.$file; exit; } catch(Exception $e) { sendError($e->getMessage()); } } $t->viewsPath = WWW.$root.'/views'; $t->header = $t->viewsPath.'/header.tpl.php'; $t->footer= $t->viewsPath.'/footer.tpl.php'; $t->jsvars = o(); $t->jsVarNames = []; if(file_exists(WWW.$t->routerPath)) { include WWW.$t->routerPath; return true; } elseif(file_exists(WWW.$t->modPath)) { $id = upart($t->upartNum+1); $pname = (($id || $id==='0') && is_numeric($id)) ? $mod.'-item' : $mod; loadPage($pname,$t); return true; } else return false; } function loadPage($pagename,$t) { $t->pagename = $pagename; if($t->pages && $t->pages->$pagename) assign($t,$t->pages->$pagename); else { $path = WWW.$t->modPath.'/'.$pagename.'.hjson'; if(file_exists($path)) { $pageconf = loadConf($path); assign($t,$pageconf); } } $t->meta_title = $t->meta_title ? $t->conf->meta_title.' | ' : ''; $t->meta_title .= $t->conf->base_title; if(!$t->menu)$t->menu = $t->conf->default_menu; $basepath = $t->modPath.'/'.$pagename; $t->TPLPath = $basepath.'.tpl.php'; if(file_exists(WWW.$t->TPLPath)) include WWW.$t->TPLPath; exit; } function form2email($form,$fields) { $s = ''; foreach ($fields AS $f){ $s .= ''.ucfirst($f).': '.nl2br($form->$f->val()).'
'; } return $s; } function receiveJSON($fields=null) { $s = file_get_contents('php://input'); $d = json_decode($s); $out = null; if($fields){ $out = o(); foreach($fields AS $n) $out->$n = $d->$n; } else $out = $d; return $out; } function sendSuccess($d=null) { if(!$d)$d = o(); $d->success = 1; send($d); } function sendError($error,$d=null) { if(!$d)$d = o(); $d->error = $error; send($d); } function send($d) { $d = o($d); echo json_encode($d,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); exit; }