diff options
Diffstat (limited to 'inc')
-rw-r--r-- | inc/auth/ldap.class.php | 16 | ||||
-rw-r--r-- | inc/auth/mysql.class.php | 98 | ||||
-rw-r--r-- | inc/auth/pgsql.class.php | 380 | ||||
-rw-r--r-- | inc/auth/pgsql.php | 135 | ||||
-rw-r--r-- | inc/common.php | 4 |
5 files changed, 455 insertions, 178 deletions
diff --git a/inc/auth/ldap.class.php b/inc/auth/ldap.class.php index 49643d3bf..852634e3d 100644 --- a/inc/auth/ldap.class.php +++ b/inc/auth/ldap.class.php @@ -1,10 +1,9 @@ <?php /** - * auth/basic.class.php - * - * foundation authorisation class - * all auth classes should inherit from this class + * LDAP authentication backend * + * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) + * @author Andreas Gohr <andi@splitbrain.org> * @author Chris Smith <chris@jalakaic.co.uk> */ @@ -19,6 +18,15 @@ class auth_ldap extends auth_basic { function auth_ldap(){ global $conf; $this->cnf = $conf['auth']['ldap']; + + // ldap extension is needed + if(!function_exists('ldap_connect')) { + if ($this->cnf['debug']) + msg("LDAP err: PHP LDAP extension not found.",-1); + $this->success = false; + return; + } + if(empty($this->cnf['groupkey'])) $this->cnf['groupkey'] = 'cn'; // try to connect diff --git a/inc/auth/mysql.class.php b/inc/auth/mysql.class.php index 632a97b5d..849d48646 100644 --- a/inc/auth/mysql.class.php +++ b/inc/auth/mysql.class.php @@ -30,29 +30,34 @@ class auth_mysql extends auth_basic { */ function auth_mysql() { global $conf; + $this->cnf = $conf['auth']['mysql']; if (method_exists($this, 'auth_basic')) parent::auth_basic(); if(!function_exists('mysql_connect')) { if ($this->cnf['debug']) - msg("MySQL err: PHP MySQL extension not found.",-1); + msg("MySQL err: PHP MySQL extension not found.",-1,__LINE__,__FILE__); $this->success = false; return; } - $this->cnf = $conf['auth']['mysql']; $this->defaultgroup = $conf['defaultgroup']; // set capabilities based upon config strings set if (empty($this->cnf['server']) || empty($this->cnf['user']) || - empty($this->cnf['password']) || empty($this->cnf['database'])) + empty($this->cnf['password']) || empty($this->cnf['database'])){ + if ($this->cnf['debug']) + msg("MySQL err: insufficient configuration.",-1,__LINE__,__FILE__); + $this->success = false; return; + } $this->cando['addUser'] = $this->_chkcnf(array('getUserInfo', 'getGroups', 'addUser', 'getUserID', + 'getGroupID', 'addGroup', 'addUserGroup'),true); $this->cando['delUser'] = $this->_chkcnf(array('getUserID', @@ -120,9 +125,9 @@ class auth_mysql extends auth_basic { $rc = false; if($this->_openDB()) { - $sql = str_replace('%{user}',addslashes($user),$this->cnf['checkPass']); - $sql = str_replace('%{pass}',addslashes($pass),$sql); - $sql = str_replace('%{dgroup}',addslashes($this->defaultgroup),$sql); + $sql = str_replace('%{user}',$this->_escape($user),$this->cnf['checkPass']); + $sql = str_replace('%{pass}',$this->_escape($pass),$sql); + $sql = str_replace('%{dgroup}',$this->_escape($this->defaultgroup),$sql); $result = $this->_queryDB($sql); if($result !== false && count($result) == 1) { @@ -334,7 +339,7 @@ class auth_mysql extends auth_basic { if($this->_openDB()) { $this->_lockTables("READ"); $sql = $this->_createSQLFilter($this->cnf['getUsers'], $filter); - $sql .= " ".$this->cnf['SortOrder']." LIMIT $first,$limit"; + $sql .= " ".$this->cnf['SortOrder']." LIMIT $limit OFFSET $first"; $result = $this->_queryDB($sql); foreach ($result as $user) @@ -415,22 +420,22 @@ class auth_mysql extends auth_basic { $gid = $this->_getGroupID($group); if (!$gid) { if ($force) { // create missing groups - $sql = str_replace('%{group}',addslashes($group),$this->cnf['addGroup']); + $sql = str_replace('%{group}',$this->_escape($group),$this->cnf['addGroup']); $gid = $this->_modifyDB($sql); $newgroup = 1; // group newly created } if (!$gid) return false; // group didn't exist and can't be created } - $sql = str_replace('%{uid}', addslashes($uid),$this->cnf['addUserGroup']); - $sql = str_replace('%{user}', addslashes($user),$sql); - $sql = str_replace('%{gid}', addslashes($gid),$sql); - $sql = str_replace('%{group}',addslashes($group),$sql); + $sql = str_replace('%{uid}', $this->_escape($uid),$this->cnf['addUserGroup']); + $sql = str_replace('%{user}', $this->_escape($user),$sql); + $sql = str_replace('%{gid}', $this->_escape($gid),$sql); + $sql = str_replace('%{group}',$this->_escape($group),$sql); if ($this->_modifyDB($sql) !== false) return true; if ($newgroup) { // remove previously created group on error - $sql = str_replace('%{gid}', addslashes($gid),$this->cnf['delGroup']); - $sql = str_replace('%{group}',addslashes($group),$sql); + $sql = str_replace('%{gid}', $this->_escape($gid),$this->cnf['delGroup']); + $sql = str_replace('%{group}',$this->_escape($group),$sql); $this->_modifyDB($sql); } } @@ -452,10 +457,10 @@ class auth_mysql extends auth_basic { if (($this->dbcon) && ($uid)) { $gid = $this->_getGroupID($group); if ($gid) { - $sql = str_replace('%{uid}', addslashes($uid),$this->cnf['delUserGroup']); - $sql = str_replace('%{user}', addslashes($user),$sql); - $sql = str_replace('%{gid}', addslashes($gid),$sql); - $sql = str_replace('%{group}',addslashes($group),$sql); + $sql = str_replace('%{uid}', $this->_escape($uid),$this->cnf['delUserGroup']); + $sql = str_replace('%{user}', $this->_escape($user),$sql); + $sql = str_replace('%{gid}', $this->_escape($gid),$sql); + $sql = str_replace('%{group}',$this->_escape($group),$sql); $rc = $this->_modifyDB($sql) == 0 ? true : false; } } @@ -479,7 +484,7 @@ class auth_mysql extends auth_basic { $groups = array(); if($this->dbcon) { - $sql = str_replace('%{user}',addslashes($user),$this->cnf['getGroups']); + $sql = str_replace('%{user}',$this->_escape($user),$this->cnf['getGroups']); $result = $this->_queryDB($sql); if(count($result)) { @@ -505,7 +510,7 @@ class auth_mysql extends auth_basic { */ function _getUserID($user) { if($this->dbcon) { - $sql = str_replace('%{user}',addslashes($user),$this->cnf['getUserID']); + $sql = str_replace('%{user}',$this->_escape($user),$this->cnf['getUserID']); $result = $this->_queryDB($sql); return $result === false ? false : $result[0]['id']; } @@ -532,15 +537,14 @@ class auth_mysql extends auth_basic { */ function _addUser($user,$pwd,$name,$mail,$grps){ if($this->dbcon && is_array($grps)) { - $sql = str_replace('%{user}', addslashes($user),$this->cnf['addUser']); - $sql = str_replace('%{pass}', addslashes($pwd),$sql); - $sql = str_replace('%{name}', addslashes($name),$sql); - $sql = str_replace('%{email}',addslashes($mail),$sql); + $sql = str_replace('%{user}', $this->_escape($user),$this->cnf['addUser']); + $sql = str_replace('%{pass}', $this->_escape($pwd),$sql); + $sql = str_replace('%{name}', $this->_escape($name),$sql); + $sql = str_replace('%{email}',$this->_escape($mail),$sql); $uid = $this->_modifyDB($sql); if ($uid) { foreach($grps as $group) { - $uid = $this->_getUserID($user); $gid = $this->_addUserToGroup($uid, $group, 1); if ($gid === false) break; } @@ -554,7 +558,7 @@ class auth_mysql extends auth_basic { */ $this->_delUser($user); if ($this->cnf['debug']) - msg ("MySQL err: Adding user '$user' to group '$group' failed.",-1); + msg ("MySQL err: Adding user '$user' to group '$group' failed.",-1,__LINE__,__FILE__); } } } @@ -577,10 +581,10 @@ class auth_mysql extends auth_basic { if($this->dbcon) { $uid = $this->_getUserID($user); if ($uid) { - $sql = str_replace('%{uid}',addslashes($uid),$this->cnf['delUser']); - $sql = str_replace('%{user}', addslashes($user),$sql); + $sql = str_replace('%{uid}',$this->_escape($uid),$this->cnf['delUserRefs']); $this->_modifyDB($sql); - $sql = str_replace('%{uid}',addslashes($uid),$this->cnf['delUserRefs']); + $sql = str_replace('%{uid}',$this->_escape($uid),$this->cnf['delUser']); + $sql = str_replace('%{user}', $this->_escape($user),$sql); $this->_modifyDB($sql); return true; } @@ -602,7 +606,7 @@ class auth_mysql extends auth_basic { * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> */ function _getUserInfo($user){ - $sql = str_replace('%{user}',addslashes($user),$this->cnf['getUserInfo']); + $sql = str_replace('%{user}',$this->_escape($user),$this->cnf['getUserInfo']); $result = $this->_queryDB($sql); if(count($result)) { $info = $result[0]; @@ -662,7 +666,7 @@ class auth_mysql extends auth_basic { if ($err == 0) { if ($cnt > 0) { $sql .= " ".str_replace('%{uid}', $uid, $this->cnf['UpdateTarget']); - $sql .= " LIMIT 1"; + if(get_class($this) == 'auth_mysql') $sql .= " LIMIT 1"; //some PgSQL inheritance comp. $this->_modifyDB($sql); } return true; @@ -685,7 +689,7 @@ class auth_mysql extends auth_basic { */ function _getGroupID($group) { if($this->dbcon) { - $sql = str_replace('%{group}',addslashes($group),$this->cnf['getGroupID']); + $sql = str_replace('%{group}',$this->_escape($group),$this->cnf['getGroupID']); $result = $this->_queryDB($sql); return $result === false ? false : $result[0]['id']; } @@ -716,10 +720,11 @@ class auth_mysql extends auth_basic { } else { mysql_close ($con); if ($this->cnf['debug']) - msg("MySQL err: No access to database {$this->cnf['database']}.", -1); + msg("MySQL err: No access to database {$this->cnf['database']}.",-1,__LINE__,__FILE__); } } else if ($this->cnf['debug']) - msg ("MySQL err: Connection to {$this->cnf['user']}@{$this->cnf['server']} not possible.", -1); + msg ("MySQL err: Connection to {$this->cnf['user']}@{$this->cnf['server']} not possible.", + -1,__LINE__,__FILE__); return false; // connection failed } @@ -760,7 +765,7 @@ class auth_mysql extends auth_basic { return $resultarray; } if ($this->cnf['debug']) - msg('MySQL err: '.mysql_error($this->dbcon), -1); + msg('MySQL err: '.mysql_error($this->dbcon),-1,__LINE__,__FILE__); } return false; } @@ -784,7 +789,7 @@ class auth_mysql extends auth_basic { if ($rc !== false) return $rc; } if ($this->cnf['debug']) - msg('MySQL err: '.mysql_error($this->dbcon), -1); + msg('MySQL err: '.mysql_error($this->dbcon),-1,__LINE__,__FILE__); } return false; } @@ -857,7 +862,7 @@ class auth_mysql extends auth_basic { if ($this->dbcon) { foreach ($filter as $item => $pattern) { - $tmp = addslashes('%'.mysql_real_escape_string($pattern, $this->dbcon).'%'); + $tmp = '%'.$this->_escape($pattern).'%'; if ($item == 'user') { if ($cnt++ > 0) $SQLfilter .= " AND "; $SQLfilter .= str_replace('%{user}',$tmp,$this->cnf['FilterLogin']); @@ -886,7 +891,24 @@ class auth_mysql extends auth_basic { return $sql; } - + /** + * Escape a string for insertion into the database + * + * @author Andreas Gohr <andi@splitbrain.org> + * @param string $string The string to escape + * @param boolean $like Escape wildcard chars as well? + */ + function _escape($string,$like=false){ + if($this->dbcon){ + $string = mysql_real_escape_string($string, $this->dbcon); + }else{ + $string = addslashes($string); + } + if($like){ + $string = addcslashes($string,'%_'); + } + return $string; + } } //Setup VIM: ex: et ts=2 enc=utf-8 : diff --git a/inc/auth/pgsql.class.php b/inc/auth/pgsql.class.php new file mode 100644 index 000000000..96ad8acb7 --- /dev/null +++ b/inc/auth/pgsql.class.php @@ -0,0 +1,380 @@ +<?php +/** + * PgSQL authentication backend + * + * This class inherits much functionality from the MySQL class + * and just reimplements the Postgres specific parts. + * + * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) + * @author Andreas Gohr <andi@splitbrain.org> + * @author Chris Smith <chris@jalakai.co.uk> + * @author Matthias Grimm <matthias.grimmm@sourceforge.net> +*/ + +define('DOKU_AUTH', dirname(__FILE__)); +require_once(DOKU_AUTH.'/mysql.class.php'); + +class auth_pgsql extends auth_mysql { + + /** + * Constructor + * + * checks if the pgsql interface is available, otherwise it will + * set the variable $success of the basis class to FALSE + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + * @author Andreas Gohr <andi@splitbrain.org> + */ + function auth_pgsql() { + global $conf; + $this->cnf = $conf['auth']['pgsql']; + if(!$this->cnf['port']) $this->cnf['port'] = 5432; + + if (method_exists($this, 'auth_basic')) + parent::auth_basic(); + + if(!function_exists('pg_connect')) { + if ($this->cnf['debug']) + msg("PgSQL err: PHP Postgres extension not found.",-1); + $this->success = false; + return; + } + + $this->defaultgroup = $conf['defaultgroup']; + + // set capabilities based upon config strings set + if (empty($this->cnf['server']) || empty($this->cnf['user']) || + empty($this->cnf['password']) || empty($this->cnf['database'])){ + if ($this->cnf['debug']) + msg("PgSQL err: insufficient configuration.",-1,__LINE__,__FILE__); + $this->success = false; + return; + } + + $this->cando['addUser'] = $this->_chkcnf(array('getUserInfo', + 'getGroups', + 'addUser', + 'getUserID', + 'getGroupID', + 'addGroup', + 'addUserGroup')); + $this->cando['delUser'] = $this->_chkcnf(array('getUserID', + 'delUser', + 'delUserRefs')); + $this->cando['modLogin'] = $this->_chkcnf(array('getUserID', + 'updateUser', + 'UpdateTarget')); + $this->cando['modPass'] = $this->cando['modLogin']; + $this->cando['modName'] = $this->cando['modLogin']; + $this->cando['modMail'] = $this->cando['modLogin']; + $this->cando['modGroups'] = $this->_chkcnf(array('getUserID', + 'getGroups', + 'getGroupID', + 'addGroup', + 'addUserGroup', + 'delGroup', + 'getGroupID', + 'delUserGroup')); + /* getGroups is not yet supported + $this->cando['getGroups'] = $this->_chkcnf(array('getGroups', + 'getGroupID')); */ + $this->cando['getUsers'] = $this->_chkcnf(array('getUsers', + 'getUserInfo', + 'getGroups')); + $this->cando['getUserCount'] = $this->_chkcnf(array('getUsers')); + } + + /** + * Check if the given config strings are set + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + * @return bool + */ + function _chkcnf($keys, $wop=false){ + foreach ($keys as $key){ + if (empty($this->cnf[$key])) return false; + } + return true; + } + + // @inherit function checkPass($user,$pass) + // @inherit function getUserData($user) + // @inherit function createUser($user,$pwd,$name,$mail,$grps=null) + // @inherit function modifyUser($user, $changes) + // @inherit function deleteUsers($users) + + + /** + * [public function] + * + * Counts users which meet certain $filter criteria. + * + * @param array $filter filter criteria in item/pattern pairs + * @return count of found users. + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ + function getUserCount($filter=array()) { + $rc = 0; + + if($this->_openDB()) { + $sql = $this->_createSQLFilter($this->cnf['getUsers'], $filter); + + // no equivalent of SQL_CALC_FOUND_ROWS in pgsql? + if (($result = $this->_queryDB($sql))){ + $rc = count($result); + } + $this->_closeDB(); + } + return $rc; + } + + // @inherit function retrieveUsers($first=0,$limit=10,$filter=array()) + // @inherit function joinGroup($user, $group) + // @inherit function leaveGroup($user, $group) { + + /** + * Adds a user to a group. + * + * If $force is set to '1' non existing groups would be created. + * + * The database connection must already be established. Otherwise + * this function does nothing and returns 'false'. + * + * @param $uid user id to add to a group + * @param $group name of the group + * @param $force '1' create missing groups + * @return bool 'true' on success, 'false' on error + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + * @author Andreas Gohr <andi@splitbrain.org> + */ + function _addUserToGroup($uid, $group, $force=0) { + $newgroup = 0; + + if (($this->dbcon) && ($uid)) { + $gid = $this->_getGroupID($group); + if (!$gid) { + if ($force) { // create missing groups + $sql = str_replace('%{group}',addslashes($group),$this->cnf['addGroup']); + $this->_modifyDB($sql); + //group should now exists try again to fetch it + $gid = $this->_getGroupID($group); + $newgroup = 1; // group newly created + } + } + + if (!$gid) return false; // group didn't exist and can't be created + + $sql = str_replace('%{uid}', addslashes($uid),$this->cnf['addUserGroup']); + $sql = str_replace('%{user}', addslashes($user),$sql); + $sql = str_replace('%{gid}', addslashes($gid),$sql); + $sql = str_replace('%{group}',addslashes($group),$sql); + if ($this->_modifyDB($sql) !== false) return true; + + if ($newgroup) { // remove previously created group on error + $sql = str_replace('%{gid}', addslashes($gid),$this->cnf['delGroup']); + $sql = str_replace('%{group}',addslashes($group),$sql); + $this->_modifyDB($sql); + } + } + return false; + } + + // @inherit function _delUserFromGroup($uid, $group) + // @inherit function _getGroups($user) + // @inherit function _getUserID($user) + + /** + * Adds a new User to the database. + * + * The database connection must already be established + * for this function to work. Otherwise it will return + * 'false'. + * + * @param $user login of the user + * @param $pwd encrypted password + * @param $name full name of the user + * @param $mail email address + * @param $grps array of groups the user should become member of + * @return bool + * + * @author Andreas Gohr <andi@splitbrain.org> + * @author Chris Smith <chris@jalakai.co.uk> + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ + function _addUser($user,$pwd,$name,$mail,$grps){ + if($this->dbcon && is_array($grps)) { + $sql = str_replace('%{user}', addslashes($user),$this->cnf['addUser']); + $sql = str_replace('%{pass}', addslashes($pwd),$sql); + $sql = str_replace('%{name}', addslashes($name),$sql); + $sql = str_replace('%{email}',addslashes($mail),$sql); + if($this->_modifyDB($sql)){ + $uid = $this->_getUserID($user); + }else{ + return false; + } + + if ($uid) { + foreach($grps as $group) { + $gid = $this->_addUserToGroup($uid, $group, 1); + if ($gid === false) break; + } + + if ($gid) return true; + else { + /* remove the new user and all group relations if a group can't + * be assigned. Newly created groups will remain in the database + * and won't be removed. This might create orphaned groups but + * is not a big issue so we ignore this problem here. + */ + $this->_delUser($user); + if ($this->cnf['debug']) + msg("PgSQL err: Adding user '$user' to group '$group' failed.",-1,__LINE__,__FILE__); + } + } + } + return false; + } + + // @inherit function _delUser($user) + // @inherit function _getUserInfo($user) + // @inherit function _updateUserInfo($changes, $uid) + // @inherit function _getGroupID($group) + + /** + * Opens a connection to a database and saves the handle for further + * usage in the object. The successful call to this functions is + * essential for most functions in this object. + * + * @return bool + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ + function _openDB() { + if (!$this->dbcon) { + $dsn = 'host='.$this->cnf['server']; + $dsn .= ' port='.$this->cnf['port']; + $dsn .= ' dbname='.$this->cnf['database']; + $dsn .= ' user='.$this->cnf['user']; + $dsn .= ' password='.$this->cnf['password']; + + $con = @pg_connect($dsn); + if ($con) { + $this->dbcon = $con; + return true; // connection and database successfully opened + } else if ($this->cnf['debug']){ + msg ("PgSQL err: Connection to {$this->cnf['user']}@{$this->cnf['server']} not possible.", + -1,__LINE__,__FILE__); + } + return false; // connection failed + } + return true; // connection already open + } + + /** + * Closes a database connection. + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ + function _closeDB() { + if ($this->dbcon) { + pg_close ($this->dbcon); + $this->dbcon = 0; + } + } + + /** + * Sends a SQL query to the database and transforms the result into + * an associative array. + * + * This function is only able to handle queries that returns a + * table such as SELECT. + * + * @param $query SQL string that contains the query + * @return array with the result table + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ + function _queryDB($query) { + if ($this->dbcon) { + $result = @pg_query($this->dbcon,$query); + if ($result) { + while (($t = pg_fetch_assoc($result)) !== false) + $resultarray[]=$t; + pg_free_result ($result); + return $resultarray; + }elseif ($this->cnf['debug']) + msg('PgSQL err: '.pg_last_error($this->dbcon),-1,__LINE__,__FILE__); + } + return false; + } + + /** + * Executes an update or insert query. This differs from the + * MySQL one because it does NOT return the last insertID + * + * @author Andreas Gohr + */ + function _modifyDB($query) { + if ($this->dbcon) { + $result = @pg_query($this->dbcon,$query); + if ($result) { + pg_free_result ($result); + return true; + } + if ($this->cnf['debug']){ + msg('PgSQL err: '.pg_last_error($this->dbcon),-1,__LINE__,__FILE__); + } + } + return false; + } + + /** + * Start a transaction + * + * @param $mode could be 'READ' or 'WRITE' + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ + function _lockTables($mode) { + if ($this->dbcon) { + $this->_modifyDB('BEGIN'); + return true; + } + return false; + } + + /** + * Commit a transaction + * + * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> + */ + function _unlockTables() { + if ($this->dbcon) { + $this->_modifyDB('COMMIT'); + return true; + } + return false; + } + + // @inherit function _createSQLFilter($sql, $filter) + + + /** + * Escape a string for insertion into the database + * + * @author Andreas Gohr <andi@splitbrain.org> + * @param string $string The string to escape + * @param boolean $like Escape wildcard chars as well? + */ + function _escape($string,$like=false){ + $string = pg_escape_string($string); + if($like){ + $string = addcslashes($string,'%_'); + } + return $string; + } + +} + +//Setup VIM: ex: et ts=2 enc=utf-8 : diff --git a/inc/auth/pgsql.php b/inc/auth/pgsql.php deleted file mode 100644 index b063f405e..000000000 --- a/inc/auth/pgsql.php +++ /dev/null @@ -1,135 +0,0 @@ -<?php -/** - * PgSQL authentication backend - * (shamelessly based on the original auth_mysql.php ;-) - * - * PHP's PgSQL extension is needed - * - * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) - * @author Alexander Marx < mad-ml [at] madness [dot] at > - */ - -//check for Postgresql extension on load -if(!function_exists('pg_connect')) - msg("PgSQL extension not found",-1); - -/** - * Execute SQL - * - * Executes SQL statements and returns the results as list - * of hashes. Returns false on error. - * - */ -function auth_pgsql_runsql($sql_string) { - global $conf; - $cnf = $conf['auth']['pgsql']; - - if($cnf['port']) { - $port=" port=".$cnf['port']; - } - - $dsn="host=".$cnf['server']." dbname=".$cnf['database'].$port." user=".$cnf['user']." password=".$cnf['password']; - $link = pg_connect($dsn); - if(!$link){ - msg('PgSQL: Connection to database failed!',-1); - return false; - } - - $result = pg_query($link, $sql_string); - if(!$result){ - msg('PgSQL: '.pg_last_error($link)); - return false; - } - - for($i=0; $i< pg_num_rows($result); $i++) { - $temparray = pg_fetch_assoc($result); - $resultarray[]=$temparray; - } - pg_free_result($result); - pg_close($link); - return $resultarray; -} - -/** - * Check user+password [required auth function] - * - * Checks if the given user exists and the given - * plaintext password is correct - * - * @author Andreas Gohr <andi@splitbrain.org> - * @return bool - */ -function auth_checkPass($user,$pass){ - global $conf; - $cnf = $conf['auth']['pgsql']; - - $sql = str_replace('%u',addslashes($user),$cnf['userinfo']); - $result = auth_pgsql_runsql($sql); - if(count($result)>0) { - $info=$result[0]; - return auth_verifyPassword($pass, $info['pass']); - } else { - return false; - } -} - -/** - * Return user info [required auth function] - * - * Returns info about the given user needs to contain - * at least these fields: - * - * name string full name of the user - * mail string email addres of the user - * grps array list of groups the user is in - * - * @author Andreas Gohr <andi@splitbrain.org> - */ -function auth_getUserData($user){ - global $conf; - $cnf = $conf['auth']['pgsql']; - - $sql = str_replace('%u',addslashes($user),$cnf['userinfo']); - $result = auth_pgsql_runsql($sql); - if(!count($result)) return false; - $info = $result[0]; - - $sql = str_replace('%u',addslashes($user),$cnf['groups']); - $result = auth_pgsql_runsql($sql); - if(!count($result)) return false; - foreach($result as $row){ - $info['grps'][] = $row['group']; - } - - return $info; -} - -/** - * Create a new User [required auth function] - */ -function auth_createUser($user,$pass,$name,$mail) { - global $conf; - $cnf = $conf['auth']['pgsql']; - - if($cnf['createuser']) { - $sql = str_replace('%u',addslashes($user),$cnf['userinfo']); - $result = auth_pgsql_runsql($sql); - if(count($result)>0) return false; - - $sql = str_replace('%u',addslashes($user),$cnf['createuser']); - $sql = str_replace('%p',auth_cryptPassword($pass),$sql); - $sql = str_replace('%f',addslashes($name),$sql); - $sql = str_replace('%e',addslashes($mail),$sql); - $sql = str_replace('%g',addslashes($conf['defaultgroup']),$sql); - - $result=auth_pgsql_runsql($sql); - if(count($result)) - return $pass; - } else { - msg("Sorry. Your PgSQL backend is not configured to create new users.",-1); - } - return null; -} - -//Setup VIM: ex: et ts=2 enc=utf-8 : - diff --git a/inc/common.php b/inc/common.php index c321bce82..636b26f0b 100644 --- a/inc/common.php +++ b/inc/common.php @@ -140,12 +140,14 @@ function buildAttributes($params){ * @author Andreas Gohr <andi@splitbrain.org> * @see html_msgarea */ -function msg($message,$lvl=0){ +function msg($message,$lvl=0,$line='',$file=''){ global $MSG; $errors[-1] = 'error'; $errors[0] = 'info'; $errors[1] = 'success'; + if($line || $file) $message.=' ['.basename($file).':'.$line.']'; + if(!headers_sent()){ if(!isset($MSG)) $MSG = array(); $MSG[]=array('lvl' => $errors[$lvl], 'msg' => $message); |