# Includes
$dir = dirname(__FILE__).'/dbs';
+#debug(nb::ls_dir($dir,'\.php$'));
+foreach (nb::ls_dir($dir,'\.php$') as $file) {
+ require_once("$dir/$file");
+ #if (file_exists("$dir/$file.php")) echo("$dir/$file.php\n");
+}
+return;
foreach (array(
'postfix',
'nb',
'wp',
) as $file) {
if (file_exists("$dir/$file.php")) require_once("$dir/$file.php");
+ #if (file_exists("$dir/$file.php")) echo("$dir/$file.php\n");
}
?>
'name' => 'nb',
#'order' => ($LOCAL_DB ? null : 1),
'order' => 1,
- '_import' => array('_rent','_nico'),
+ '_import' => array('_nb','_nico'),
);
$CONF['nb-sqlite'] = array (
'host' => "$DIR_SQLITE/nb.db",
'type' => 'sqlite',
- '_import' => '_rent',
+ '_import' => '_nb',
);
if ($LOCAL_DB and !empty($CONF['nb-sqlite'])) {
$CONF['nb-sqlite']['order'] = isset($CONF['nb']['order']) ? $CONF['nb']['order'] : 1;
unset($CONF['nb']['order']);
}
+/*****************************************************************************
+*****************************************************************************/
+#die(print_r(posix_getpwnam('nico'),true));
+#die(print_r(posix_getpwuid(posix_getuid()),true));
+#die(print_r(pathinfo('~/.gitconfig'),true));
+#die(file_exists(realpath('~/.gitconfig')) ? 'yes' : 'no');
+$CONF['_nb'] = array(
+ 'title' => 'Nb',
+ 'notice' => 'Micro foncier 4BE',
+ 'default_table' => 'rent',
+
+ 'tables' => array(
+
+ 'agency' => [ 'row_parse_post' => function(&$r) { return rent_doc('agency',$r); }, ],
+
+ 'place' => array(
+ #'replace' => [ 'id' => 'addr.addr' ],
+ 'row_parse_post' => 'place_doc',
+ ),
+
+ 'tenant' => array(
+ 'row_parse_post' => 'tenant_doc',
+ ),
+
+ 'test' => array(
+ 'sql' => "SELECT 'Test sql table'",
+ ),
+
+ 'rent' => array(
+ 'orderby' => 'start desc, end desc',
+ #'replace' => [ 'idtenant' => 'tenant.name', 'idplace' => 'addr.addr' ],
+
+ 'row_parse_pre' => function(&$r) {
+ $r['month'] = $r['rent'] + $r['charge'];
+
+ $start_month = date('m',strtotime($r['start']));
+ $end_month = date('m',strtotime($r['end']));
+ if (date('Y',strtotime($r['end'])) > date('Y',strtotime($r['start']))) $end_month = 12;
+ $r['months'] = (1 + $end_month - $start_month);
+
+ $r['total'] = sprintf('%d',$r['rent'] * $r['months']);
+ },
+
+ 'row_parse_post' => ((empty($GLOBALS['Db']) or Db::php_cli()) ? null : function(&$r) {
+ static $templates;
+ if (!isset($templates)) {
+ $templates = $GLOBALS['Db']->rows("SELECT id FROM template",PDO::FETCH_NUM);
+ foreach ($templates as $k=>$t) {
+ $templates[$k] = '<a class="small revision" href="template/?id='.$t[0].'&idplace=:idplace&idtenant=:idtenant&start=:start">'.preg_replace('/\.\w+$/','',$t[0]).'</a>';
+ }
+ }
+
+ tenant_doc($r);
+ $revision = '';
+ foreach ($templates as $t) {
+ $revision .= preg_replace_callback('/:(\w+)/',function($m) use(&$r) {return $r[$m[1]];},$t).' ';
+ }
+ $r['doc'] = preg_replace('@^(?:<div[^>]+>)?(.*?)(?:</div>)?$@','<div class="list">'.trim($revision).'\1</div>',$r['doc']);
+
+ }),
+ ),
+
+ ),
+);
+
+return 1;
+
+function tenant_doc(&$r) { return rent_doc('tenant',$r); }
+function place_doc(&$r) { return rent_doc('place',$r); }
+
+function rent_doc($table,&$r) {
+ if (empty($_SERVER['DOCUMENT_ROOT'])) return;
+
+ #if (!empty($r['end']) and date('Ymd') > $r['end']) return $r['doc'] = isset($r['doc']) ? $r['doc'] : '';
+
+ $url = '';
+ foreach (['id'.$table,'id'] as $k) {
+ if (isset($r[$k])) {
+ $url = '/data/'.$table.'/'.$r[$k];
+ break;
+ }
+ }
+ if (!$url) return;
+
+ if (!isset($r['doc'])) $r['doc'] = '';
+#debug($_SERVER['DOCUMENT_ROOT'].$url);
+ if (
+
+ # Web url
+ (
+ empty($r['doc']) and file_exists( $dir = $_SERVER['DOCUMENT_ROOT'].$url )
+ )
+
+ # Absolute path
+ or (
+ ( $dir = nb::untilde($r['doc']) )
+ and file_exists($dir) and is_dir($dir)
+ and ($url='' or 1)
+ )
+
+ ) {
+
+ foreach (ls_dir($dir,true) as $p) {
+ $r['doc'] .= sprintf('<a href="%s" class="small">%s</a> ',"$url/$p",
+ nb::prettyText(preg_replace('@^.*?([^/\.]+).*?$@','\1',$p))
+ );
+ }
+
+ }
+
+ if (!empty($r['doc'])) $r['doc'] = '<div class="list">'.$r['doc'].'</div>';
+}
?>
<?php
$CONF['owncloud'] = [
- 'host' => '/home/owncloud/data/owncloud.db',
+ 'host' => "$DIR_SQLITE/owncloud.db",
'type' => 'sqlite',
'default_table' => 'oc_users',
+ 'default_table' => 'last_files',
+ 'tables' => [
+ 'last_files' => [
+ 'sql' => "SELECT path,mtime FROM oc_filecache WHERE path NOT LIKE 'files_versions/%' ORDER BY fileid DESC LIMIT 10",
+ 'row_parse_pre' => function(&$r) {
+ $r['mtime'] = date ("Y-m-d H:i:s",$r['mtime']);
+ }
+ ],
+ ],
];
?>
+++ /dev/null
-<?php
-#die(print_r(posix_getpwnam('nico'),true));
-#die(print_r(posix_getpwuid(posix_getuid()),true));
-#die(print_r(pathinfo('~/.gitconfig'),true));
-#die(file_exists(realpath('~/.gitconfig')) ? 'yes' : 'no');
-$CONF['_rent'] = array(
- 'title' => 'Rent',
- 'notice' => 'Micro foncier 4BE',
- 'default_table' => 'rent',
-
- 'tables' => array(
-
- 'agency' => [ 'row_parse_post' => function(&$r) { return rent_doc('agency',$r); }, ],
-
- 'place' => array(
- #'replace' => [ 'id' => 'addr.addr' ],
- 'row_parse_post' => 'place_doc',
- ),
-
- 'tenant' => array(
- 'row_parse_post' => 'tenant_doc',
- ),
-
- 'test' => array(
- 'sql' => "SELECT 'Test sql table'",
- ),
-
- 'rent' => array(
- 'orderby' => 'start desc, end desc',
- #'replace' => [ 'idtenant' => 'tenant.name', 'idplace' => 'addr.addr' ],
-
- 'row_parse_pre' => function(&$r) {
- $r['month'] = $r['rent'] + $r['charge'];
-
- $start_month = date('m',strtotime($r['start']));
- $end_month = date('m',strtotime($r['end']));
- if (date('Y',strtotime($r['end'])) > date('Y',strtotime($r['start']))) $end_month = 12;
- $r['months'] = (1 + $end_month - $start_month);
-
- $r['total'] = sprintf('%d',$r['rent'] * $r['months']);
- },
-
- 'row_parse_post' => ((empty($GLOBALS['Db']) or Db::php_cli()) ? null : function(&$r) {
- static $templates;
- if (!isset($templates)) {
- $templates = $GLOBALS['Db']->rows("SELECT id FROM template",PDO::FETCH_NUM);
- foreach ($templates as $k=>$t) {
- $templates[$k] = '<a class="small revision" href="template/?id='.$t[0].'&idplace=:idplace&idtenant=:idtenant&start=:start">'.preg_replace('/\.\w+$/','',$t[0]).'</a>';
- }
- }
-
- tenant_doc($r);
- $revision = '';
- foreach ($templates as $t) {
- $revision .= preg_replace_callback('/:(\w+)/',function($m) use(&$r) {return $r[$m[1]];},$t).' ';
- }
- $r['doc'] = preg_replace('@^(?:<div[^>]+>)?(.*?)(?:</div>)?$@','<div class="list">'.trim($revision).'\1</div>',$r['doc']);
-
- }),
- ),
-
- ),
-);
-
-return 1;
-
-function tenant_doc(&$r) { return rent_doc('tenant',$r); }
-function place_doc(&$r) { return rent_doc('place',$r); }
-
-function rent_doc($table,&$r) {
- if (empty($_SERVER['DOCUMENT_ROOT'])) return;
-
- #if (!empty($r['end']) and date('Ymd') > $r['end']) return $r['doc'] = isset($r['doc']) ? $r['doc'] : '';
-
- $url = '';
- foreach (['id'.$table,'id'] as $k) {
- if (isset($r[$k])) {
- $url = '/data/'.$table.'/'.$r[$k];
- break;
- }
- }
- if (!$url) return;
-
- if (!isset($r['doc'])) $r['doc'] = '';
-#debug($_SERVER['DOCUMENT_ROOT'].$url);
- if (
-
- # Web url
- (
- empty($r['doc']) and file_exists( $dir = $_SERVER['DOCUMENT_ROOT'].$url )
- )
-
- # Absolute path
- or (
- ( $dir = nb::untilde($r['doc']) )
- and file_exists($dir) and is_dir($dir)
- and ($url='' or 1)
- )
-
- ) {
-
- foreach (ls_dir($dir,true) as $p) {
- $r['doc'] .= sprintf('<a href="%s" class="small">%s</a> ',"$url/$p",
- nb::prettyText(preg_replace('@^.*?([^/\.]+).*?$@','\1',$p))
- );
- }
-
- }
-
- if (!empty($r['doc'])) $r['doc'] = '<div class="list">'.$r['doc'].'</div>';
-}
-
-?>
require_once(realpath(dirname(__FILE__).'/out.php'));
require_once(realpath(dirname(__FILE__).'/db/table.php'));
require_once(realpath(dirname(__FILE__).'/db/field.php'));
-$DB_TYPES = array(); # See db/types/*.php
+$DB_TYPES = []; # See db/types/*.php
#$arr = ['rent'=>'Rent','nb'=>'Nb'];
#if (!empty($argv) and $argv[1] == 'zaza') bye($arr);
function __construct($opt = '') {
- # Args
- if ($opt==='') $opt = array();
- $opt = is_scalar($opt) ? array('pdo' => $opt) : $opt;
+ # Args defaults
+ if ($opt==='') $opt = [];
+ $opt = is_scalar($opt) ? ['pdo' => $opt] : $opt;
- # Tables - We dont want to affect $this
+ # Tables
if (isset($opt['tables'])) {
foreach ($opt['tables'] as $name=>$param) $this->table($name,$param);
unset($opt['tables']);
}
- # Statics
- #$this->type = null;
- if (isset($opt['Ztype'])) {
- $this->type = $opt['type'];
- unset($opt['type']);
- }
- # Args
+ # Args into this
foreach ($opt as $k=>$v) $this->$k = $v;
# id
if (!$this->type(null)) $this->bye("Unknow type = `".$this->type."`");
# Connect
- if ($this->type('use_path') and ($this->host!=':memory:') and !is_readable($this->host)) {
- $this->bye("Can't read database path `".$this->host."` type=`".$this->type."`",false);
- }
-
try {
$this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options);
#$this->conn = new PDO($this->pdo,$this->user,$this->password,$this->pdo_error);
* @author NB 12.08.16
* Return a table instance
*/
- public function table($name,$params=array()) {
- if (!array_key_exists($name,$this->tables)) $this->tables[$name] = new Table($name,['db'=>$this]+$params);
- elseif ($params) $this->tables[$name]->__construct($name,$params);
+ public function table($name,$params=[]) {
+ #if (!array_key_exists($name,$this->tables)) {
+ if (empty($this->tables[$name])) {
+ $this->tables[$name] = new Table($name,['db'=>$this]+$params);
+ } elseif ($params) {
+ $this->tables[$name]->__construct($name,$params);
+ }
+ #if (empty($this->tables[$name]->type)) debug($this->tables[$name]);
return $this->tables[$name];
}
$config[1] = str_replace('<NAME>',($name ? $name : $this->name),$config[1]);
- if (!file_exists($config[0])) return array();
+ if (!file_exists($config[0])) return [];
- $return = array();
+ $return = [];
foreach (explode("\n",file_get_contents($config[0])) as $line) {
#bye($line);
if (!preg_match('/'.($config[1]).'/',$line,$m)) continue;
return str_replace(array_keys($replace),array_values($replace),$value);
}
+ public function info($key=null,$die=false,$type=null) {
+ $info = $this->type($key,$die,$type);
+ if (is_callable($info)) return $info($this);
+ return $this->unvar($info);
+ }
+
public function type($key=null,$die=false,$type=null) {
global $DB_TYPES;
if (!$type) $type = $this->type;
if ($key == 'type') return $type;
# Load php file
- static $require = array();
+ static $require = [];
if (empty($require[$type])) {
if (empty($type)) self::bye('Db::$type is required');
require_once(dirname(__FILE__).'/db/types/'.$type.'.php');
if (is_callable($sql)) $sql = $sql($this);
foreach ($this->conn->query($sql,PDO::FETCH_ASSOC) as $row) {
+ #debug($row);
$name = current($row);
$this->table($name,$row);
}
return '"'.$value.'"';
}
- public function out($rows,$head=array()) {
+ public function out($rows,$head=[]) {
out::rows($this->p('format',out::php_cli() ? 'csv' : 'table'),$rows,$head);
return true;
}
];
$actions = explode(',',$action);
$this->pdef('format',($this->php_cli() ? 'csv' : ''));
- $rows = array();
+ $rows = [];
$return = false;
foreach($actions as $action) {
* @param [FILES] $files Files to load
* @return ARRAY the new/existing value of $this->db
*/
- public static function conf_dbs($files=array(),&$first=false) {
- if (empty($files)) return array();
+ public static function conf_dbs($files=[],&$first=false) {
+ if (empty($files)) return [];
# Load all files into a $h if #files is not a hash
- $h = array();
+ $h = [];
if (is_scalar($files)) $files = array($files);
foreach ($files as $file) {
public function status() {
- $status = $new = array();
+ $status = $new = [];
$new = []
#+( empty($this->id) ? [] : ['id' => $this->id])
#'conf' => count(array_keys($this->conf)),
'tables' => count($this->tables()),
]
- +($this->type('use_path') ? array() : array(
+ +($this->type('use_path') ? [] : array(
'port' => $this->port,
'user' => $this->user,
))
public function fields($st) {
# See: http://php.net/manual/en/pdostatement.getcolumnmeta.php
- $fields = array();
+ $fields = [];
#debug($st->columnCount());
#debug($st->getColumnMeta(0));
if ($from_sql) return $sql_replace($this->sql());
- if ($this->type == 'view') {
+ if ($this->type() == 'view') {
return $sql_replace('CREATE VIEW '.$this->sql_name().' AS SELECT'.preg_replace('/^CREATE\s+.*?\s+AS\s+.*?SELECT/i','',$this->sql()));
}
;
};
- $sql = 'CREATE '.strtoupper($this->type).' '.$this->sql_name()
+ $sql = 'CREATE '.strtoupper($this->type()).' '.$this->sql_name()
.' ('
.join(",",array_map(function($f) use ($_create) {return $_create($f);},array_values($this->fields())))
# done at the end for primary keys .')'
public function url_sort($name) {
- if ($this->p('buttons')==='0' or !($f=$this->fields($name)) or !empty($f->dyn)) return self::prettyText($name);
+ if (!$this->buttons() or !($f=$this->fields($name)) or !empty($f->dyn)) return self::prettyText($name);
#debug($f);
# See: http://dev.w3.org/html5/html-author/charref
// Get results
//
# NB 28.03.16 $this->sql = $sql;
- $this->debug(preg_replace('/(,|FROM|WHERE|HAVING|GROUP|ORDER)/i',"\n\\1",$sql),1);
+ $this->debug(preg_replace('/[\r\n]+[ ]*/',' ',$sql),1);
+# NB 03.09.16 $this->debug(preg_replace('/(,|FROM|WHERE|HAVING|GROUP|ORDER)/i',"\n\\1",$sql),1);
return array($sql,$where,$limit,$select_count);
}
+ public function buttons() {
+ if ($this->type() != 'table') return false;
+ return true;
+ }
+
public function rows(&$opt=array(),$opt_by_val=null) {
$this->create_temporary();
- if (empty($this->type) or $this->type != 'table') $this->pset('buttons','0');;
if ($opt_by_val !== null) $opt = $opt_by_val;
$format = $this->p('format');
self::$params += array_values(out::$types);
if (empty($out_conf['enclose'])) $out_conf['enclose'] = array('','');
- debug('Using out module!',1);
+ debug('Using out module!',3);
}
if ($this->p('header')!=="0") {
$html .= '<thead>'.NB_EOL.'<tr class="head">'.NB_EOL;
- if ($this->p('buttons')!=='0' and DB_HTML_EDIT) $html .= '<th class="edit"></th>'.NB_EOL;
+ if ($this->buttons() and DB_HTML_EDIT) $html .= '<th class="edit"></th>'.NB_EOL;
foreach ($fields as $name => $f) {
- #$html .= '<th class="'.$f.'">'.($this->p('buttons')==='0' ? $f : $this->url_sort($f)).'</th>'.NB_EOL;
$html .= '<th class="'.$name.($f->key?' key':'').'">'.$this->url_sort($name).'</th>'.NB_EOL;
}
- if ($this->p('buttons')!=='0' and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.NB_EOL;
+ if ($this->buttons() and DB_HTML_DELETE) $html .= '<th class="delete"></th>'.NB_EOL;
$html .= '</tr>'.NB_EOL.'</thead>'.NB_EOL;
}
$html = '<tr class="row">'.NB_EOL;
- if ($this->p('buttons')!=='0') $html .= '<td class="action">'.$buttons[0].'</td>'.NB_EOL;
+ if ($this->buttons()) $html .= '<td class="action">'.$buttons[0].'</td>'.NB_EOL;
foreach ($row as $k => $v) {
if (isset($this->extras[$k])) $k .= " extra";
$html .= '<td class="'.$k.'">'.$v.'</td>'.NB_EOL;
}
- if ($this->p('buttons')!=='0') $html .= '<td class="action">'.$buttons[1].'</td>'.NB_EOL;
+ if ($this->buttons()) $html .= '<td class="action">'.$buttons[1].'</td>'.NB_EOL;
$html .= '</tr>'.NB_EOL;
$html .= '<ul class="row">'.NB_EOL;
- if ($this->p('buttons')!=='0') {
+ if ($this->buttons()) {
$html .= '<li class="buttons">'.NB_EOL;
$html .= join('',$this->html_row_buttons($row));
$html .= '</li>'.NB_EOL;
public function out($v,$head=array()) { return $this->db()->out($v,$head); }
public function infos() {
+ $this->type();
return
array(
"name" => $this->name,
return var_export($o,true);
}
+ public function type() {
+ if (isset($this->type)) return $this->type;
+ return $this->type = $this->status('type');
+ }
+
+ public function status($key=null) {
+ if (!isset($this->status)) {
+ $this->status = [];
+
+ $sql = $this->db()->type('tables');
+ if (is_callable($sql)) $sql = $sql($this);
+ #$sql .= ' AND name='.$this->db()->quote($this->name);
+ $sql = "SELECT * FROM ($sql) t WHERE name=".$this->db()->quote($this->name);
+
+ $this->status = $this->db()->conn->query($sql,PDO::FETCH_ASSOC);
+ if (!empty($this->status)) $this->status = $this->status->fetch();
+ }
+ if (!empty($key)) return ( empty($this->status[$key]) ? '' : $this->status[$key] );
+ return $this->status;
+ }
+
} # < Class
?>
,2),
),
-#'tables' => "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'",
-'tables' => function($Db) {
+'tables' => "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'",
+'_tables' => function($Db) {
#debug($Db->databases());
$sql = "SELECT name,type FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type IN('table','view') AND name NOT LIKE 'sqlite_%'";
$dbs = $Db->databases();
}
-function ls_dir($path,$recurse=false,$exp='') {
-
- $ls = array();
- $rep = opendir($path);
- #die(">>>".$exp);
-
- while($file = readdir($rep)) {
-
- if ($file == '.' or $file == '..') continue;
-
- if (!$exp or preg_match('#\.('.$exp.')$#',$file)) {
- $ls[] = $file;
- }
-
- if ($recurse and is_dir("$path/$file")) {
-#print ">>>$path/$file\n";
-
- foreach (ls_dir("$path/$file",$recurse,$exp) as $l) {
-#print ">>>$path/$file << $l\n";
- $ls[] = "$file/$l";
- }
-
- }
-
- #echo "<div>$file</div>\n";
-
- }
-
- closedir($rep);
-
-#print ">>>$path: ".print_r($ls,true)."\n";
- return $ls;
-
-}
-
function ar_first($ar) { return (is_array($ar) and count($ar)) ? $ar[0] : false; }
function ar_map($return,$array,$as_hash=false) {
return strtolower(php_uname('s'));
return strtolower(preg_replace('/\s+.*$/','', php_uname()));
}
+
+ public static function ls_dir($path,$exp='',$recurse=false) {
+
+ $ls = array();
+ $rep = opendir($path);
+ #die(">>>".$exp);
+
+ while($file = readdir($rep)) {
+
+ if ($file == '.' or $file == '..') continue;
+
+ if (!$exp or preg_match('#'.$exp.'#',$file)) {
+ $ls[] = $file;
+ #$ls[] = "$path/$file";
+ }
+
+ if ($recurse and is_dir("$path/$file")) {
+ #print ">>>$path/$file\n";
+
+ foreach (ls_dir("$path/$file",$recurse,$exp) as $l) {
+ #print ">>>$path/$file << $l\n";
+ $ls[] = "$file/$l";
+ #$ls[] = $l;
+ }
+
+ }
+
+ #echo "<div>$file</div>\n";
+
+ }
+
+ closedir($rep);
+
+ #print ">>>$path: ".print_r($ls,true)."\n";
+ return $ls;
+
+ }
+
+ public static function http_user() {
+ if (!empty($_SERVER['REMOTE_USER'])) return $_SERVER['REMOTE_USER']; # empty since jessie
+ if (!empty($_SERVER['PHP_AUTH_USER'])) return $_SERVER['PHP_AUTH_USER'];
+ return '';
+ }
+
+ public static function http_login_header($msg_box='My Realm',$msg_cancel='Please login!') {
+ header('WWW-Authenticate: Basic realm="'.$msg_box.'"');
+ header('HTTP/1.0 401 Unauthorized');
+ echo $msg_cancel;
+ exit;
+ }
+
} # < Class
?>
$new = array(); $i = 0;
foreach ($head as $k => $h) {
- $new[$h] = $row[$i];
+ $new[$h] = isset($row[$i]) ? $row[$i] : null;
$i++;
}
$row = $new;