]> git.nbdom.net Git - nb.git/commitdiff
Bed
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 20 Sep 2016 22:47:10 +0000 (00:47 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Tue, 20 Sep 2016 22:47:10 +0000 (00:47 +0200)
13 files changed:
etc/dbs.php
etc/dbs/nb.php
etc/dbs/owncloud.php
etc/dbs/postfix.php
etc/dbs/puppetdb.php
etc/dbs/ui.php
etc/dbs/wp.php
etc/vim/templates/daemon.tt.bak [deleted file]
lib/php/db.php
lib/php/db/table.php
lib/php/nb.php
lib/php/oc.php [new file with mode: 0644]
lib/php/out.php

index 8af2fee8fd1b0e01622fdd448bb9b7c4eaabb3ad..799ff5449730ede3f1c0a6c5058c337aade7f6b4 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 require_once dirname(__FILE__).'/../lib/php/nb.php';
-$CONF = [];
+$DB_CONFS = [];
 #$dev = (bool)(stripos(gethostname(),'macbook') !== false);
 $LOCAL_DB = (
   file_exists('/opt/local/var/run/mysql5')
@@ -34,7 +34,7 @@ foreach (nb::ls_dir($dir,'\.php$') as $file) {
 # Create *-sqlite databases
 #
 $host_already_exists = [];
-foreach ($CONF as $id => $db) {
+foreach ($DB_CONFS as $id => $db) {
   if (1
     and !empty($db['type']) and $db['type'] == 'sqlite'
     and !empty($db['host']) 
@@ -45,20 +45,20 @@ if ($DIR_SQLITE) {
   foreach (nb::ls_dir($DIR_SQLITE,'\.db$') as $file) {
 
     if (isset($host_already_exists["$DIR_SQLITE/$file"])) {
-      $CONF[$host_already_exists["$DIR_SQLITE/$file"]]['sql_pre'][] = 'PRAGMA journal_mode=OFF';
+      $DB_CONFS[$host_already_exists["$DIR_SQLITE/$file"]]['sql_pre'][] = 'PRAGMA journal_mode=OFF';
       continue;
     }
 
     $name = basename($file,'.db');
     $id = "$name-sqlite";
 
-    $CONF[$id] = [
+    $DB_CONFS[$id] = [
       'host' => "$DIR_SQLITE/$file",
       'type' => 'sqlite',
       'title' => 'created by dbs.php',
       '_import' => $name
     ];
-    #if (!empty($CONF[$name])) $CONF[$id] = array_merge_recursive($CONF[$name],$CONF[$id]);
+    #if (!empty($DB_CONFS[$name])) $DB_CONFS[$id] = array_merge_recursive($DB_CONFS[$name],$DB_CONFS[$id]);
   }
 }
 
@@ -66,7 +66,7 @@ if ($DIR_SQLITE) {
 # Sqlite all Attach db type=sqlite
 #
 #return;
-$CONF['all'] = [
+$DB_CONFS['all'] = [
   'host' => ':memory:',
   'type' => 'sqlite',
   'pdo' => '',
@@ -82,7 +82,7 @@ $CONF['all'] = [
   ],
 ];
 
-foreach ($CONF as $id => $db) {
+foreach ($DB_CONFS as $id => $db) {
   if (0
     or empty($db['type'] )
     or $db['type']!='sqlite'
@@ -92,11 +92,11 @@ foreach ($CONF as $id => $db) {
     or !is_readable($db['host'])
   ) continue;
   $fname = basename($db['host'],'.db');
-  $CONF['all']['types']['sql_pre'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname."";
-  conf_merge($CONF[$fname],$CONF['all']);
+  $DB_CONFS['all']['types']['sql_pre'][] = "ATTACH DATABASE '".$db['host']."' as ".$fname."";
+  conf_merge($DB_CONFS[$fname],$DB_CONFS['all']);
 
 }
-conf_merge($CONF['nb'],$CONF['all']);
+conf_merge($DB_CONFS['nb'],$DB_CONFS['all']);
 
 return 1;
 
index c2de8764d665d117053dec6eaf5b8872e233c0c4..c6a5a731eaf56b77f10075c34f99e555b72cca56 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-$CONF['nb'] = array(
+$DB_CONFS['nb'] = array(
   'host' => 'admin.izideal.vpn',
   'type' => 'mysql',
   'name' => 'nb',
@@ -64,7 +64,7 @@ $CONF['nb'] = array(
   ),
 );
 
-if (strpos(php_uname("n"),'ovh.net')!==false) $CONF['nb']['order'] = 1;
+if (strpos(php_uname("n"),'ovh.net')!==false) $DB_CONFS['nb']['order'] = 1;
 return 1;
 
 function tenant_doc(&$r) { return rent_doc('tenant',$r); }
index fd39f5730d7e0c1e05505515d5de63eeb35fba29..f1c78170fd9c90f59852ea1cae4a215a1c02eee8 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-$CONF['owncloud'] = [
+$DB_CONFS['owncloud'] = [
   '_import' => '_nico',
   'name' => 'owncloud',
   'type' => 'mysql',
index ee0122d3a7b552ec88b2e6daee38b6ad2070fef0..8c10855bd50c01dd869c7910255102ea2da54df6 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-$CONF['postfix'] = array (
+$DB_CONFS['postfix'] = array (
   'title' => 'Postfix on Izideal',
   'type' => 'mysql',
   'host' => 'admin.izideal.vpn',
index 719497286750c6e8e780daf32c1c69fde027cbe2..cfe407b1102bb7e0a9b47262e4e37758a34768b3 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-$CONF['puppetdb'] = array (
+$DB_CONFS['puppetdb'] = array (
   'title' => 'Puppetdb',
   'host' => 'big.cascais.loc',
   'type' => 'pgsql',
index 70de31d5a17f7aed494584d6c6d9556a0ca368e4..eaae442ce8ca1376a27efd6e64fa96257e05b7c7 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-$CONF['_ui'] = array(
+$DB_CONFS['_ui'] = array(
   'title' => 'Semantico UI',
   'type' => 'sqlite',
   'host' => (file_exists('/opt/www/sem_ui/var/db/semantico.db') ?  '/opt/www/sem_ui/var/db/semantico.db' : "$DIR_SQLITE/semantico.db"),
@@ -153,19 +153,19 @@ $CONF['_ui'] = array(
 
   ),
 );
-$CONF['ui'] = [
+$DB_CONFS['ui'] = [
   '_import' => ['_ui'],
   'order' => (preg_match('/^(dev|isp|ist)/',php_uname('n')) ? 1 : null),
   #'order' => 1,
 ];
-$CONF['ui-pgsql'] = [
+$DB_CONFS['ui-pgsql'] = [
   'host' => 'ui.semantico.net',
   'type' => 'pgsql',
   'name' => 'ui',
   'pdo' => '',
   '_import' => ['_ui','_semadm'],
 ];
-$CONF['ui-mysql'] = [
+$DB_CONFS['ui-mysql'] = [
   'host' => 'ui.semantico.net',
   'type' => 'mysql',
   'name' => 'ui',
index 8940b04b6f6fd121c0c812bbcab413fbe1d6e85e..af776f2ce9acaf8d79b2c6f45fcac9e59f842a74 100644 (file)
@@ -1,5 +1,5 @@
 <?php
-$CONF['wp'] = [
+$DB_CONFS['wp'] = [
   'title' => 'Wordpress',
   'host' => 'admin.izideal.vpn',
   'type' => 'mysql',
@@ -13,11 +13,11 @@ $CONF['wp'] = [
 ];
 
 if (false and !empty($GLOBALS['wpdb'])) {
-  $CONF['wp']['type'] = 'sqlite';
-  $CONF['wp']['host'] = DB_DIR.'/'.DB_FILE;
+  $DB_CONFS['wp']['type'] = 'sqlite';
+  $DB_CONFS['wp']['host'] = DB_DIR.'/'.DB_FILE;
 }
 
-$CONF['wp-sqlite'] = array (
+$DB_CONFS['wp-sqlite'] = array (
   'host' => "$DIR_SQLITE/wp.db",
   'type' => 'sqlite',
   '_import' => 'wp',
diff --git a/etc/vim/templates/daemon.tt.bak b/etc/vim/templates/daemon.tt.bak
deleted file mode 100644 (file)
index e3704c2..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-#!/bin/bash
-[% programname=input("Program name: ")
-   log4perl=choice('with Log::Log4perl: ', 'no', 'yes')
--%]
-fetchmail=`which fetchmail`
-
-case "$1" in
-
-'start')
-       if pidof fetchmail &>/dev/null; then
-               echo "Fetchmail is already running"
-               exit 1
-       fi
-       if $fetchmail -f $fetchmailrc; then
-               echo "Fetchmail is up"
-               exit 0
-               else
-               echo "Fetchmail can not start"
-               exit 2
-       fi
-;;
-
-'stop')
-       if !(pidof fetchmail &>/dev/null); then
-               echo "Fetchmail is already down"
-               exit 1
-       fi
-       if $fetchmail -q; then
-               echo "Fetchmail is down"
-               exit 0
-       else
-               echo "Fetchmail can not stop"
-               exit 2
-       fi
-;;
-
-'restart')
-       $0 stop
-       sleep 2
-       $0 start
-;;
-
-'status')
-       if pidof fetchmail &>/dev/null; then
-               echo "Fetchmail is up"
-               exit 0
-       else
-               echo "Fetchmail is down"
-               exit 1
-       fi
-;;
-
-*)
-       echo "usage $0 start|stop|restart|status"
-       exit -1
-;;
-
-esac
-
-exit
index 8758af679be78788fca87d686d6c4059f177ae9d..b5b6623f7e1d57f611450ea0984e6a7c90528688 100644 (file)
@@ -139,7 +139,7 @@ class Db extends nb {
     if (empty($this->type)) $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo));
     if (!$this->type) return false;
 
-    if ($this->type('use_path')) {
+    if ($this->conf_type('use_path')) {
       if ($p = $this->p('db.host')) $this->host = $p;
       if (empty($this->host)) $this->host = preg_replace('/^\w+:/','',$this->pdo);
       # Add file
@@ -184,8 +184,8 @@ class Db extends nb {
     if (empty($this->title)) $this->title = prettyText($this->name);
 
     # Row parser
-    if (empty($this->row_parse) and $this->type('row_parse')) {
-      $this->row_parse = $this->type('row_parse');
+    if (empty($this->row_parse) and $this->conf_type('row_parse')) {
+      $this->row_parse = $this->conf_type('row_parse');
     }
   }
 
@@ -203,7 +203,7 @@ class Db extends nb {
     if (empty($this->pdo)) return false;
 
     # Type
-    if (!$this->type(null)) $this->bye("Unknow type = `".$this->type."`");
+    $this->conf_type_load();
 
     # Connect
     try {
@@ -223,13 +223,11 @@ class Db extends nb {
     if (empty($this->conn)) return false;
 
     # Create functions
-    if ($this->type('sqliteCreateFunction')) {
-      foreach ($this->type('sqliteCreateFunction') as $name => $fct) {
-        if (is_array($fct)) {
-          $this->conn->sqliteCreateFunction($name,$fct[0],$fct[1]);
-        } else {
-          $this->conn->sqliteCreateFunction($name,$fct);
-        }
+    foreach ((array)$this->conf_type('sqliteCreateFunction') as $name => $fct) {
+      if (is_array($fct)) {
+        $this->conn->sqliteCreateFunction($name,$fct[0],$fct[1]);
+      } else {
+        $this->conn->sqliteCreateFunction($name,$fct);
       }
     }
 
@@ -309,7 +307,7 @@ class Db extends nb {
   }
 
   function quote($v) {
-    static $fct = false; if ($fct === false) $fct = $this->type('quote');
+    static $fct = false; if ($fct === false) $fct = $this->conf_type('quote');
     if ($fct) return $fct($v);
     return $this->conn->quote($v);
   }
@@ -371,25 +369,32 @@ class Db extends nb {
   }
 
   public function method($key=null,$args=null) {
-    $method = $this->type($key);
+    $method = $this->conf_type($key);
     if (is_callable($method)) return $this->unvar($method($this));
     return $this->unvar($method);
   }
 
-  public function type($key=null,$die=false,$type=null) {
+  public function conf_type_load($type=null) {
     global $DB_TYPES;
-    if (!$type) $type = $this->type;
-    if ($key == 'type') return $type;
-
-    # Load php file
     static $require = [];
+
+    if (empty($type)) $type = $this->type;
+    if (empty($type)) self::bye('Db type is required');
+
     if (empty($require[$type])) {
-      if (empty($type)) self::bye('Db->type is required');
-      require_once(dirname(__FILE__).'/db/types/'.$type.'.php');
-      $require[$type] = 1;
+      $require[$type] = dirname(__FILE__).'/db/types/'.$type.'.php';
+      if (!file_exists($require[$type])) self::bye("Unknow type `$type`");
+      $require[$type] = dirname(__FILE__).'/db/types/'.$type.'.php';
+      require_once($require[$type]);
     }
 
-    $db_type = $DB_TYPES[$type];
+    return $DB_TYPES[$type];
+  }
+
+  public function conf_type($key=null,$die_if_empty=false) {
+
+    # Load php file
+    $db_type = $this->conf_type_load();
 
     # Dump all array
     if ($key === null) return $db_type;
@@ -401,23 +406,18 @@ class Db extends nb {
 
     # Check key exists
     if (empty($db_type[$key])) {
-      if ($die) self::bye("db.type(): Unknow key `$key` for type `".$type."`");#.' - '.print_r($DB_TYPES,true));
+      if ($die_if_empty) self::bye("db.type(): Unknow key `$key` for type `".$this->type."`");#.' - '.print_r($db_type,true));
       return;
     }
 
     return $this->unvar($db_type[$key]);
-    $value = $db_type[$key];
-    /*
-    */
-    if (is_scalar($value)) {
-      $value = $this->unvar($value);
-    } elseif (is_scalar($value) and !$this->is_hash($value)) {
-      for ($i=0;$i<count($value);$i++) {
-        if (is_scalar($value[$i])) $value[$i] = $this->unvar($value[$i]);
-      }
-    }
-    return $value;
-    return $db_type[$key];
+  }
+
+  public function type($key=null,$die=false,$type=null) {
+    if (empty($type)) $type = $this->type;
+    if (!empty($key) and $key == 'type') return $type;
+
+    return $this->conf_type($key,$die);
   }
 
   public function tables() {
@@ -453,7 +453,7 @@ class Db extends nb {
     if (strpos($value,'.') !== false) return $value;
 
     # specific quote
-    if ($q=$this->type('quote_name')) return $q.$value.$q;
+    if ($q=$this->conf_type('quote_name')) return $q.$value.$q;
 
     # default quote
     return '"'.$value.'"';
@@ -550,13 +550,14 @@ class Db extends nb {
         $h = array_replace_recursive($h,$yaml);
 
       } elseif (preg_match('/\.php$/i',$file)) {
-        #global $CONF;
-        require($file); if (!empty($CONF)) $h = array_replace_recursive($h,$CONF);
+        #global $DB_CONFS;
+        require($file);
+        if (!empty($DB_CONFS)) $h = array_replace_recursive($h,$DB_CONFS);
 
       }
 
     }
-    unset($yaml,$CONF);
+    unset($yaml,$DB_CONFS);
     if (!$h) return false;
 
     # Emulate hash pointer _import
@@ -800,7 +801,7 @@ class Db extends nb {
         'DROP '.strtoupper($t->type).' IF EXISTS '.$t->sql_name()
       ;
       /*
-      if ($drop = $this->type('sql.drop')) {
+      if ($drop = $this->conf_type('sql.drop')) {
         $drop = str_replace('<NAME>',$t->name,$drop);
         $drop = str_replace('<TYPE>',strtoupper($t->type),$drop);
       } else {
@@ -849,13 +850,13 @@ class Db extends nb {
       #'conf' => count(array_keys($this->conf)),
       'tables' => count($this->tables()),
     ]
-    +($this->type('use_path') ? [] : array(
+    +($this->conf_type('use_path') ? [] : array(
       'port' => $this->port,
       'user' => $this->user,
     ))
     +$new;
 
-    if (($sqls=$this->type('status'))) {
+    if (($sqls=$this->conf_type('status'))) {
 
       foreach ($sqls as $sql => $fct) {
 
@@ -876,7 +877,7 @@ class Db extends nb {
 
     } # << type.status
 
-    if (($call=$this->type('status_callback'))) {
+    if (($call=$this->conf_type('status_callback'))) {
       $call($new);
     }
 
@@ -923,7 +924,7 @@ class Db extends nb {
   public function sql_pre() {
     $return = [];
 
-    foreach ((array)$this->type('sql_pre') as $s) {
+    foreach ((array)$this->conf_type('sql_pre') as $s) {
       if (!empty($s)) $return[] = $s;
     }
 
@@ -945,7 +946,7 @@ class Db extends nb {
 
       $this->databases = [];
       $name = self::p('name','');
-      if ($sql = $this->type('databases')) {
+      if ($sql = $this->conf_type('databases')) {
         $fct = '';
         if (is_array($sql)) list($sql,$fct) = count($sql)>1 ? $sql : [$sql[0],''];
 
index 461759f6b9e7e20c58c609c5b53e884c123c48f9..c92f60337929d54bc58ad5143daafaec6dbad32e 100644 (file)
@@ -137,7 +137,7 @@ Class Table extends nb {
   public function create($from_sql=true) {
 
     // String replace function
-    $sql_replace_fct = $this->db()->type('table.sql.create',false);
+    $sql_replace_fct = $this->db()->conf_type('table.sql.create',false);
     $db_sql_replace_fct = empty($this->db()->sql_replace) ? '' : $this->db()->sql_replace;
     $sql_replace = function($sql) use ($sql_replace_fct,$db_sql_replace_fct) {
       if ($db_sql_replace_fct) $sql = $db_sql_replace_fct($sql);
@@ -151,7 +151,7 @@ Class Table extends nb {
     }
 
     // Specific function for fields if return something use it instead of default
-    $_create_fct = $this->db()->type('field.create',false);
+    $_create_fct = $this->db()->conf_type('field.create',false);
     $_create = function(&$field) use ($_create_fct) {
       if ($_create_fct and ($sql=$_create_fct($field))) return $sql;
 
@@ -177,7 +177,7 @@ Class Table extends nb {
       ;
     }
 
-    $indexes = [];#$this->db()->type('table.sql.index',false);
+    $indexes = [];#$this->db()->conf_type('table.sql.index',false);
     foreach ($this->indexes() as $i) {
       if (!empty($i['unique']) or !empty($i['key'])) continue;
       $indexes[] = 'CREATE INDEX '.$i['name'].' ON '.$this->sql_name().' ('.$i['field'].')';
@@ -193,7 +193,7 @@ Class Table extends nb {
    */
   public function indexes() {
     if (!isset($this->indexes)) {
-      $sql = $this->db()->type('table.sql.index');
+      $sql = $this->db()->conf_type('table.sql.index');
       $fct = '';
 
       if (is_array($sql)) list($sql,$fct) = (count($sql)==1 ? [$sql[0],null] : $sql);
@@ -228,7 +228,7 @@ Class Table extends nb {
       return $this->sql;
     }
 
-    $sql = str_replace('<T.NAME>',$this->name,$this->db()->type('table.sql',true));
+    $sql = str_replace('<T.NAME>',$this->name,$this->db()->conf_type('table.sql',true));
 
     # Noise before CREATE like MySql
     $this->sql = explode('\0',$this->db()->row($sql,'\0'));
@@ -269,7 +269,7 @@ Class Table extends nb {
     if (!isset($this->fields)) {
 
       $this->fields = array();
-      $conf = $this->unvar($this->db()->type('table.fields',true));
+      $conf = $this->unvar($this->db()->conf_type('table.fields',true));
       if (is_scalar($conf)) $conf = array('sql'=>$conf);
       $rows = $this->db()->conn->query($conf['sql']);
       $rows->setFetchMode(PDO::FETCH_ASSOC);
@@ -539,7 +539,7 @@ Class Table extends nb {
     $having = $where = array();
     if (empty($logic)) $logic = 'AND';
 
-    $regexp = $this->db()->type('regexp');
+    $regexp = $this->db()->conf_type('regexp');
     if (empty($regexp)) $regexp = 'REGEXP';
 
     foreach ($this->fields() as $k => $field) {
@@ -605,7 +605,7 @@ Class Table extends nb {
       }
 
       # having, denorm, EMPTY
-      $extra_where = (string)$this->db()->type('extra_where');
+      $extra_where = (string)$this->db()->conf_type('extra_where');
       if ($extra_where == 'having' and $field->extras) {
         $having[] = $this->extras[$k]->sql_name()."$equal$v";
 
@@ -632,7 +632,7 @@ Class Table extends nb {
     #return false;
 
     # Test if type exists because of a bug from shell.php
-# NB 30.08.16     if ($this->db()->type('type')) $this->fields();
+# NB 30.08.16     if ($this->db()->conf_type('type')) $this->fields();
 
     foreach ($extras as $k => $v) {
 
@@ -688,7 +688,7 @@ Class Table extends nb {
     } else {
 
       $where = $this->where_criterias($this->p(),$this->p('op'));
-      $select_count = ( $where ? $this->db()->type('select_count') : null );
+      $select_count = ( $where ? $this->db()->conf_type('select_count') : null );
       if (empty($select_count)) $select_count = array('','');
 
       if (!empty($this->_rows_fields)) {
@@ -1049,7 +1049,7 @@ Class Table extends nb {
   public function rows_begin_xml() {
     return ''
       .'<?xml version="1.0" encoding="utf-8"?>'.NB_EOL #<?
-      .'<rows name="'.$this->name.'" database="'.$this->db()->name.'" database-type="'.$this->db()->type('type').'">'.NB_EOL
+      .'<rows name="'.$this->name.'" database="'.$this->db()->name.'" database-type="'.$this->db()->conf_type('type').'">'.NB_EOL
     ;
   }
 
index 2451ddac16b4c3008a0306c76a42f2f2dc3f01bb..3687550a46eb8022395d6cf70388734a3cb22851 100644 (file)
@@ -750,17 +750,6 @@ class NB {
     exit;
   }
 
-  public static function oc_password_verify($pass) {
-    require_once "/home/owncloud/config/config.php";
-    require_once '/opt/owncloud/lib/base.php';
-    require_once '/opt/owncloud/lib/private/Security/Hasher.php';
-    return \OC::$server->getHasher()->hash($pass);
-    $l = new Hasher($CONFIG);
-    debug($l->legacyHashVerify($pass,"YbS9N9cK8TY6b7oyw3skfcHykXIjlx"));
-    return $CONFIG;
-    debug($CONF);
-  }
-
   /*
    * Function client_content_type
    * Return the head Accept from the client
diff --git a/lib/php/oc.php b/lib/php/oc.php
new file mode 100644 (file)
index 0000000..3e3c5dc
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+function oc_password_verify($pass) {
+  require_once "/home/owncloud/config/config.php";
+  require_once '/opt/owncloud/lib/base.php';
+  require_once '/opt/owncloud/lib/private/Security/Hasher.php';
+  return \OC::$server->getHasher()->hash($pass);
+  $l = new Hasher($CONFIG);
+  debug($l->legacyHashVerify($pass,"YbS9N9cK8TY6b7oyw3skfcHykXIjlx"));
+  return $CONFIG;
+  debug($CONFIG);
+}
+?>
index 0585e17caa496f87abee2500fac03984e9c855d4..e73c974c4b636ec7588c34de1fcd8cba1412aa81 100644 (file)
@@ -37,7 +37,7 @@ Class Out extends Nb {
 
       'php' => array(
         'enclose' => [
-           '<?php'.(NB_EOL ? NB_EOL:' ') . '$CONF = array('.NB_EOL
+           '<?php'.(NB_EOL ? NB_EOL:' ') . '$VAR = array('.NB_EOL
           ,NB_EOL.');' . (NB_EOL ? NB_EOL:' ').'?'.'>'.NB_EOL
         ],
         'eol' => self::p('eol',NB_EOL),