--- /dev/null
+<?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']);
+?>
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() {
<?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']);
* 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);
}
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];
},$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 = '';
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;
}
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']) : []);
$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'] ];