]> git.nbdom.net Git - nb.git/commitdiff
www/dbq/dbq.php
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 25 Jan 2018 02:39:33 +0000 (02:39 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 25 Jan 2018 02:39:33 +0000 (02:39 +0000)
etc/profile.d/functions
lib/php/ldap.php
www/dbq/dbq.php

index 51051191282705ef6cd5e04678155a823fa0c66a..267bb79af3fa237dadb02306e31f1edc95f98dec 100644 (file)
@@ -794,6 +794,7 @@ http_get() {
 
        else
                echo "Can't find web client" 1>&2
+               return 1
        fi
 }
 
@@ -809,6 +810,7 @@ http_head() {
 
        else
                echo "Can't find web client" 1>&2
+               return 1
        fi
 }
 
index 0318f7872af924470323eb45a0f1f765b82f7738..632b3f493c59fd1d44fcb9434685f8ad98b70ac3 100644 (file)
@@ -4,7 +4,7 @@ require_once(NB_ROOT.'/lib/php/http.php');
 
 class Ldap {
 
-       public static function connect(&$o=[]) {
+       public static function _connect(&$o=[]) {
                $host = '127.0.0.1';
                if (preg_match('/^.*?([^\.]+\.[^\.]+)$/',$_SERVER['HTTP_HOST'],$m)) {
                        $host = 'ldap.'.$m[1];
@@ -45,9 +45,9 @@ class Ldap {
                return $connect;
        }
 
-       public static function search($o=[]) {
+       public static function _search($o=[]) {
 
-               $connect = self::connect($o);
+               $connect = self::_connect($o);
 
                if (empty($o['filter'])) {
                        $o['filter'] = "(&(userpassword=*))";
@@ -112,6 +112,12 @@ class Ldap {
 
        } # < ldap_search
 
+       private $conn;
+       private $host = '127.0.0.1';
+       private $base;
+       private $user;
+       private $password;
+
        private static function ar_filter_keys($ar,$keys) {
                $new = [];
                foreach ($keys as $k) {
@@ -120,14 +126,6 @@ class Ldap {
                return $new;
        }
 
-       public $host = '127.0.0.1';
-       public $base;
-       public $user;
-       public $password;
-       #public $search = false;
-
-       private $conn;
-
   public function __construct($opt = []) {
                foreach ($opt as $k => $v) if ((string)$v !== '') $this->$k = $v;
        }
@@ -154,7 +152,7 @@ class Ldap {
                ldap_close($this->conn);
        }
 
-       public function _search($o=[]) {
+       public function search($o=[]) {
 
                $conn = $this->conn();
 
@@ -193,14 +191,17 @@ class Ldap {
 
        } # < ldap_search
 
-       public static function add($dn,$data) {
+       public function add($dn,$data) {
                $conn = $this->conn();
                return ldap_add($conn, $dn, $data); # or self::bye("Unable to add entry to ldap server");
        }
 
-       public static function update($dn,$data) {
-               $conn = $this->conn();
-               return ldap_mod_add($conn, $dn, $data); # or self::bye("Unable to add entry to ldap server");
+       public function update($dn,$data) {
+               return ldap_mod_add($this->conn(), $dn, $data); # or self::bye("Unable to add entry to ldap server");
+       }
+
+       public function delete($dn) {
+               return ldap_delete($this->conn(), $dn); # or self::bye("Unable to add entry to ldap server");
        }
 
 }
index 54ef072fc355f4e329f82444871f5ddf34413a4f..c6faf4c855a4a2f1836870613f4eea4d4eda54ab 100644 (file)
@@ -170,8 +170,28 @@ class DbQ extends nb {
        }
 
        public function page_ldap() {
-               require_once(NB_ROOT.'/lib/php/ldap.php');
                $this->deniedUnless($this->perm >= self::READ);
+
+               require_once(NB_ROOT.'/lib/php/ldap.php');
+               #debug($GLOBALS['DBQ_LDAP']);
+               $ldap = new Ldap($GLOBALS['DBQ_LDAP']);
+
+       if ($this->params['table'] == 'add') {
+               debug($this->params);
+               $rows = (int)$ldap->add($this->params['action'],$_POST);
+               bye($_POST);
+               $this->page($rows);
+
+       } elseif ($this->params['table'] == 'update') {
+               $rows = (int)$ldap->update($this->params['action'],$_POST);
+               $this->page($rows);
+
+       } elseif ($this->params['table'] == 'rm') {
+               $rows = (int)$ldap->delete($this->params['action'],$_POST);
+               $this->page($rows);
+
+       }
+
                $filter = ($this->params['table'] ? $this->params['table'] : '');
                $attrs = ($this->params['action'] ? explode(',',$this->params['action']) : []);
 
@@ -185,24 +205,15 @@ class DbQ extends nb {
                        'attrs' => $attrs,
                ];
 
-               if (true and !empty($GLOBALS['DBQ_LDAP'])) {
-                       #debug($GLOBALS['DBQ_LDAP']);
-                       $ldap = new Ldap($GLOBALS['DBQ_LDAP']);
-                       if (!$search['filter']) {
-                               if (preg_match('/^([^,]+),(.*?)$/',$GLOBALS['DBQ_LDAP']['user'],$m)) {
-                                       $search['filter'] = $m[1];
-                                       $search['base'] = $m[2];
-                               } else {
-                                       $search['filter'] = 'cn='.Http::user();
-                               }
+               if (!$search['filter']) {
+                       if (preg_match('/^([^,]+),(.*?)$/',$GLOBALS['DBQ_LDAP']['user'],$m)) {
+                               $search['filter'] = $m[1];
+                               $search['base'] = $m[2];
+                       } else {
+                               $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);
                }
+               $rows = $ldap->search($GLOBALS['DBQ_LDAP'] + $search);
 
                #echo $rows[0]['jpegPhoto']; exit;
                #$rows = [ 'jpegPhoto' => $rows[0]['jpegPhoto'] ];
@@ -210,6 +221,7 @@ class DbQ extends nb {
                                or ($this->params['format'] == $this->format_cli)
                                or ($this->params['format'] == 'csv')
                ) $rows = $this->array_fill_assoc($rows);
+
                $this->page($rows);
        }
 
@@ -855,11 +867,10 @@ EOF;
                        'value' => $this->params['format'],
                ];
 
-               $user = '';
-               if (true or $user = $this->http->user()) {
-                       if ($user) $rows[] = [ 'name' => 'dbq.user.name', 'value' => $user ];
-                       $rows[] = [ 'name' => 'dbq.user.perm', 'value' => $this->perm2h() ];
+               if ($user = $this->http->user()) {
+                       $rows[] = [ 'name' => 'dbq.user.name', 'value' => $user ];
                }
+               $rows[] = [ 'name' => 'dbq.user.perm', 'value' => $this->perm2h() ];
 
                // Machine
                if ($this->perm >= self::ADMIN and function_exists('posix_uname')) {