]> git.nbdom.net Git - nb.git/commitdiff
www/dbq/dbq.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 24 Jan 2018 03:34:51 +0000 (03:34 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Wed, 24 Jan 2018 03:34:51 +0000 (03:34 +0000)
etc/dbq/ldap.php [new file with mode: 0644]
lib/php/ldap.php
lib/php/nb.php
www/dbq/dbq.php

diff --git a/etc/dbq/ldap.php b/etc/dbq/ldap.php
new file mode 100644 (file)
index 0000000..d57ae0e
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+require_once(realpath(dirname(__FILE__).'/../../lib/php/nb.php'));
+require_once(NB_ROOT.'/lib/php/http.php');
+if (isset($_SERVER['HTTP_HOST'])) {
+       $user = Http::user();
+       $password = Http::password();
+
+} else {
+       $user = Nb::user_infos('name');
+       $password = '';
+
+}
+$base = '';
+$host = '';
+foreach (array_unique(glob('~/.ldap.conf')+glob('/etc/ldap/ldap.conf')) as $f) {
+       if (!is_readable($f)) continue;
+       foreach (file($f,FILE_SKIP_EMPTY_LINES + FILE_IGNORE_NEW_LINES) as $line) {
+               $line = trim($line);
+               if (preg_match('/^BASE\s+([^\s#]+)/i',$line,$m)) $base = $m[1];
+               if (preg_match('/^HOST\s+([^\s#]+)/i',$line,$m)) $host = $m[1];
+       }
+       break;
+}
+
+$suff = empty($_SERVER['DBQ_LDAP_USER_SUFF']) ? '' : $_SERVER['DBQ_LDAP_USER_SUFF'];
+#debug($_SERVER);
+#$base = $suff . $base;
+$GLOBALS['DBQ_LDAP'] = [
+       'user' => 'cn=' . $user . ',' . $suff . $base,
+       'password' => $password,
+       'host' => $host,
+       'base' => $base,
+];
+#debug($GLOBALS['DBQ_LDAP']);
+?>
index b03d7737335eab351e79980b1810f9b73f05fbe9..0318f7872af924470323eb45a0f1f765b82f7738 100644 (file)
@@ -129,8 +129,7 @@ class Ldap {
        private $conn;
 
   public function __construct($opt = []) {
-               foreach ($opt as $k => $v) $this->$k = $v;
-               #if (isset($this->search)) return $this->_search($this->search);
+               foreach ($opt as $k => $v) if ((string)$v !== '') $this->$k = $v;
        }
 
   public function __destruct() {
index 9dd3ede0cabab35741cc3b30ddd45f133efd42d3..f7ce4fcc882e63162e5c2217d086d30bf4804e4c 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 if (!defined('NB_ROOT')) define('NB_ROOT',realpath(dirname(__FILE__).'/../..'));
 if (!defined('NB_PROD') and defined('PRODUCTION')) define('NB_PROD',PRODUCTION);
+if (!defined('NB_PROD')) define('NB_PROD',false);
 if (!defined('NB_EOL')) define('NB_EOL',(defined('NB_PROD') and NB_PROD) ? '' : "\n");
 if (NB_PROD) unset ($_GET['debug']);
 
@@ -149,18 +150,12 @@ class NB {
         * Function: bye
         * Set a value for param, delete it if null
         */
-# NB 30.10.17   public static function bye($msg='',$backtrace_deep=0) {
-# NB 30.10.17     #throw new Exception($msg);
-# NB 30.10.17     return bye($msg,$backtrace_deep+1);
-# NB 30.10.17   }
        # # NB 30.10.17: From function.php 
        public static function bye($msg='__bye__',$backtrace_deep=0) {
-               #if ($msg) err($msg,'bye',$backtrace_deep === 0 ? 1 : $backtrace_deep);
 
                if ($msg!=='__bye__') {
                        #throw new Exception($msg);
-                       header("HTTP/1.0 500 Internal Server Error");
-# NB 15.11.17       err($msg,'bye',( $backtrace_deep !== false ? (1+-1+$backtrace_deep) : $backtrace_deep ));
+                       if (!self::php_cli()) header("HTTP/1.0 500 Internal Server Error");
                        err($msg,'bye',1+$backtrace_deep);
                        exit(1);
                }
@@ -805,7 +800,7 @@ class NB {
                return preg_replace_callback('/^(~)([\w_-]+)?/',function($m){
 
                        $user = empty($m[2]) ? '' : $m[2];
-                       $infos = self::user_infos($user);
+                       $infos = self::user_infos(null,$user);
 
                        if (empty($infos)) return $m[0];
 
@@ -813,7 +808,7 @@ class NB {
                },$path);
        }
 
-       public static function user_infos($user=null,$key=''){
+       public static function user_infos($key=null,$user=null){
 
                if ($user === null) $user = '';
                if ($key === null) $key = '';
index dab4ac3bc05900ec0c9925c3e4f4b2114bae5c9f..f8b883a0d8ba34658044a9082c19634d8f1e48e1 100644 (file)
@@ -95,7 +95,10 @@ class DbQ extends nb {
 
                if (!empty($_SERVER['DBQ_ENVS'])) {
                        foreach(explode(' ',trim($_SERVER['DBQ_ENVS'])) as $e) {
-                               list($env,$val) = explode('=',trim($e));
+                               $val = explode('=',trim($e));
+                               $env = array_shift($val);
+                               $val = join('=',$val);
+# NB 24.01.18                          list($env,$val) = explode('=',trim($e));
                                #debug([$env,$val]);
                                $_SERVER['DBQ_'.strtoupper($env)] = $val;
                        }
@@ -168,7 +171,6 @@ class DbQ extends nb {
 
        public function page_ldap() {
                require_once(NB_ROOT.'/lib/php/ldap.php');
-               #debug($this->params);
                $this->deniedUnless($this->perm >= self::READ);
                $filter = ($this->params['table'] ? $this->params['table'] : '');
                $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : []);
@@ -178,36 +180,24 @@ class DbQ extends nb {
                        $attrs = explode(',',$this->params['table']);
                }
 
-               # Get server and base from host or ldap.conf
-               # NB 23.01.18: TODO 
-               if (isset($_SERVER['HTTP_HOST']) and preg_match('/^.*?([^\.]+\.[^\.]+)$/',$_SERVER['HTTP_HOST'],$m)) {
-                       $host = 'ldap.'.$m[1];
-                       $base = 'dc=' . str_replace('.',',dc=',preg_replace('/^ldap[^.]*./','',$host));
-               } else {
-                       $host = '127.0.0.1';
-                       $base = '';
-                       foreach (array_unique(glob('~/.ldap.conf')+glob('/etc/ldap/ldap.conf')) as $f) {
-                               if (!is_readable($f)) continue;
-                               foreach (file($f,FILE_SKIP_EMPTY_LINES + FILE_IGNORE_NEW_LINES) as $line) {
-                                       $line = trim($line);
-                                       if (preg_match('/^BASE\s+([^\s#]+)/i',$line,$m)) $base = $m[1];
-                                       if (preg_match('/^HOST\s+([^\s#]+)/i',$line,$m)) $host = $m[1];
-                               }
-                               break;
-                       }
-               }
-               $preff = (!empty($_SERVER['DBQ_LDAP_BIND_DN_PREFF']) ? 'ou='.$_SERVER['DBQ_LDAP_BIND_DN_PREFF'] : '');
-               #$base = $preff . $base;
-               #$ldap = new Ldap([ ]);
 
                $search = [
                        'filter' => $filter,
                        'attrs' => $attrs,
                ];
-               $rows = Ldap::search([
-                       'bind_dn_preff' => (!empty($_SERVER['DBQ_LDAP_BIND_DN_PREFF']) ? 'ou='.$_SERVER['DBQ_LDAP_BIND_DN_PREFF'] : 'ou=auth'),
-                       'exit_on_err' => $this->p('err',true),
-               ]+$search);
+               if (false and !empty($GLOBALS['DBQ_LDAP'])) {
+                       #debug($GLOBALS['DBQ_LDAP']);
+                       $ldap = new Ldap($GLOBALS['DBQ_LDAP']);
+                       if (!$search['filter']) {
+                               $search['filter'] = 'cn='.Http::user();
+                       }
+                       $rows = $ldap->_search($GLOBALS['DBQ_LDAP'] + $search);
+               } else {
+                       $rows = Ldap::search([
+                               'bind_dn_preff' => (!empty($_SERVER['DBQ_LDAP_BIND_DN_PREFF']) ? 'ou='.$_SERVER['DBQ_LDAP_BIND_DN_PREFF'] : 'ou=auth'),
+                               'exit_on_err' => $this->p('err',true),
+                       ]+$search);
+               }
 
                #echo $rows[0]['jpegPhoto']; exit;
                #$rows = [ 'jpegPhoto' => $rows[0]['jpegPhoto'] ];