/* Enjoy!!! Dino Ciuffetti - dam2k@users.sourceforge.net
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (COPYING); if not, go to http://www.fsf.org/ or write
* to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/* USERS HANDLING */
/**
* Here you can handle all cmsdAm users operations, for example creation, parameters reading,
* elimination, login, logout, ecc.
* Useful, for example, to authenticate a user for each section access.
* Class instance: $cms->users
*/
class cmsdam_users
{
/**
* You should not use this variable from your section.
*/
var $dbms;
/**
* You should not use this variable from your section.
*/
var $logs;
/**
* You should not use this variable from your section.
*/
var $debug;
/**
* You should not use this variable from your section.
*/
var $sections;
/**
* You should not use this variable from your section.
*/
var $ldap;
/**
* You should not use this variable from your section.
*/
var $plugins;
/**
* The name of the logged in user.
* @type str
*/
var $logged_user; // name of the logged user or null if user not logged
/**
* This api method returns an array with ALL the cmsdAm user names.
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @return array ALL user names
*/
function user_names() { // This api method returns an array with ALL the user names!
$_user_names = $this->memcache->get('user_names');
if ($_user_names != false) {
return $_user_names;
}
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$rows_arr = $this->ldap->ldap_user_names();
@sort($rows_arr);
if (count ($rows_arr) > 0) { // LDAP returned something
$this->memcache->add('user_names', $rows_arr, time() + 500);
return $rows_arr;
} else {
return false;
}
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_name from " . $this->dbms->DB_tbl_prefix . "users order by user_name";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
$this->memcache->add('user_names', $rows_arr, time() + 500);
return $rows_arr;
}
return null;
}
}
return false;
}
/**
* This api method returns the DB id of the user. It can be used, for example, to reference other tables.
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The name of the user
* @return int cmsdAm username DB id
*/
function user_id($username) { // Returns the username id (can be useful to reference other tables!)
// WARNING: This method doesn't work with LDAP
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$this->ldap->ldap_not_implemented("user_id");
return false;
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select id from " . $this->dbms->DB_tbl_prefix . "users where user_name = \"$username\"";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
return $rows_arr[0];
}
return null;
}
}
return false;
}
/**
* This api method returns the username by its DB id (can be useful to reference other tables)
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param int user_id The user DB id of the user
* @return str cmsdAm user name
*/
function user_namebyid($user_id) { // Returns the username by its id (can be useful to reference other tables!)
// WARNING: This method doesn't work with LDAP
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$this->ldap->ldap_not_implemented("user_namebyid");
return false;
}
if (($user_id == "") || strlen($user_id) == 0) {
return false;
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
return false;
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_name from " . $this->dbms->DB_tbl_prefix . "users where id=$user_id";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
return $rows_arr[0];
}
return null;
}
}
return false;
}
/**
* This api method returns the user type by its name
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @return int cmsdAm user type
*/
function user_type($username) { // Returns the username type
// WARNING: This method doesn't work with LDAP
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$this->ldap->ldap_not_implemented("user_type");
return false;
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_type from " . $this->dbms->DB_tbl_prefix . "users where user_name = \"$username\"";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
return $rows_arr[0];
}
return null;
}
}
return false;
}
/**
* This api method returns the user email by its name
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @return str cmsdAm user email
*/
function user_email($username) { // Returns the username email
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$rows_arr = $this->ldap->ldap_user_email($username);
if (strlen($rows_arr) > 0) { // LDAP returned something
return $rows_arr;
} else {
return false;
}
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_email from " . $this->dbms->DB_tbl_prefix . "users where user_name = \"$username\"";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
return $rows_arr[0];
}
return null;
}
}
return false;
}
/**
* This api method returns the user password by its name
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @return str cmsdAm user password
*/
function user_password($username, $force_db="") { // Returns the username password
// WARNING: This method doesn't work with LDAP
if ($force_db == "") { // Use DB
$useldap = $this->ldap->ldap_ldapconnect();
} else {
$useldap = false;
}
if ($useldap != false) { // We can use LDAP, ready to begin
$this->ldap->ldap_not_implemented("user_password");
return false;
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_password from " . $this->dbms->DB_tbl_prefix . "users where user_name = \"$username\"";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
return $rows_arr[0];
}
return null;
}
}
return false;
}
/**
* This api method returns true if username exists
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @return bool True or false
*/
function user_exist($username, $force_db="") { // Returns true if username exist
if ($force_db == "") { // Use DB
$useldap = $this->ldap->ldap_ldapconnect();
} else {
$useldap = false;
}
if ($useldap != false) { // We can use LDAP, ready to begin
return $this->ldap->ldap_user_exist($username);
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_name from " . $this->dbms->DB_tbl_prefix . "users where user_name = \"$username\"";
if ($this->dbms->dbquery($query) == true) { // DB error
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
$this->logs->log_note(8, "User \"" . $username . "\" exists");
return true;
}
}
}
$this->logs->log_note(8, "User \"" . $username . "\" does not exists");
return false;
}
/**
* This api method adds a new user
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @param int usertype The user type
* @param int useremail The user email
* @param int userpassword The user password
* @param int userip The user ip, auto if null
* @return bool True on successm false false on error
*/
function user_add($username, $usertype = "", $useremail, $userpassword, $userip = "") { // Returns false if username exist
$username=trim(htmlspecialchars($username));
$useremail=trim(htmlspecialchars($useremail));
$basedn = $this->ldap->conf->LDAP_basedn;
// include ("etc/ldap_user.php");
//echo $ldapadd["quota"];
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$rcode = $this->ldap->ldap_user_add($username, $useremail, $userpassword);
if ($rcode == true) {
$this->logs->log_note(6, "User \"" . $username . "\" successfully added to LDAP");
return true;
} else {
$this->logs->log_note(6, "Problems adding user \"" . $username . "\" to LDAP");
return false;
}
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_name from " . $this->dbms->DB_tbl_prefix . "users where user_name = \"$username\"";
if ($this->dbms->dbquery($query) == true) { // ! DB error
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
$this->logs->log_note(6, "Problems adding user \"" . $username . "\"");
return false;
} else { // Good! No users with this name found!! We can proceed!
if (($username != "") and ($usertype != "") and ($useremail != "")
and ($userpassword != "") and ($userip != "")) { // All parameters passed
$query = "insert into " . $this->dbms->DB_tbl_prefix . "users (id, user_name, user_type, user_email, user_password, user_date, ip) VALUES (NULL, \"$username\", $usertype, \"$useremail\", \"$userpassword\", now(), \"$userip\")";
if ($this->dbms->dbquery($query) == true) { // ! DB error
$this->logs->log_note(6, "User \"" . $username . "\" successfully added");
return true;
}
}
}
}
}
$this->logs->log_note(6, "Problems adding user \"" . $username . "\"");
return false;
}
/**
* This api method returns the user date by its name
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @return str The user date
*/
function user_date($username) { // Returns the username subscription date
// WARNING: This method doesn't work with LDAP
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$this->ldap->ldap_not_implemented("user_date");
return false;
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select user_date from " . $this->dbms->DB_tbl_prefix . "users where user_name = \"$username\"";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
return $rows_arr[0];
}
return null;
}
}
return false;
}
/**
* This api method returns the total number of users
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @return int Number of users
*/
function users_count() { // Returns the count of the users
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
return $this->ldap->ldap_user_count();
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
// Try to get data from the users table
$query = "select count(id) from " . $this->dbms->DB_tbl_prefix . "users";
if ($this->dbms->dbquery($query) == false) { // DB error
return false;
} else { // Users returned
if ($this->dbms->dbnum_rows() > 0) { // There are users in the table
for ($rows = 0; $rows < $this->dbms->dbnum_rows(); $rows++)
{
// Getting data from the database
$data = $this->dbms->dbfetch_row();
// Building the array
$rows_arr[$rows] = $data[0];
}
return count ($rows_arr[0]);
}
return 0;
}
}
return false;
}
/**
* This api method returns true if user and credentials are valid, else false
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @param str password The password
* @return bool True or false
*/
function user_check($username, $password) { // Returns true if user and credentials are valid
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
return $this->ldap->ldap_user_check($username, $password);
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
if (! $this->user_exist($username)) { // The user does not exists
return false;
}
if ($this->user_password($username) != $password) { // The password of the user
return false;
}
return true;
}
}
/**
* This api method delete a cmsdam user
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @return bool True on success, else false
*/
function user_delete($username) { // Delete a user
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
$rcode = $this->ldap->ldap_user_delete($username);
if ($recode == true) {
$this->logs->log_note(6, "User \"" . $username . "\" successfully deleted from LDAP");
return true;
} else {
$this->logs->log_note(6, "Problems deleting user \"" . $username . "\" from LDAP");
return false;
}
}
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
} else { // We are connected to the DB
if (! $this->user_exist($username)) { // The user does not exists
return false;
}
// TODO: Write code to delete a user from the DB
return false;
}
}
/**
* This api method can be used to login a cmsdam user
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param str username The username
* @param str password The user password
* @param int reload If reload is 1 the page is automatic reloaded on login
* @return bool True on success, else false
*/
function user_login($username, $password, $reload=0) { // Login user. Returns true if user and credentials are valid
$this->logs->log_note(7, "NOTE: Login requested for user \"$username\"...");
// Checking if this is the cmsdAm Super Administrator
if (($username == $this->ldap->conf->Administrator) && ($password == $this->ldap->conf->Administrator_Password)) {
// Welcome to the cmsdAm Administrator!!! ;-)
$this->logs->log_note(4, "NOTE: Administrator \"$username\" logged in");
$this->debug->debug_write(7, "NOTE: Administrator \"$username\" logged in");
// Register the session
$_SESSION['cmsdamUser'] = $username;
$this->logged_user = $username;
if ($reload == 1) { // The user want to reload the page on login
// We use cmsdam HTTP RELOCATOR plugin to send the user on the same template
// he was few milliseconds ago.
$this->logs->log_note(7, "NOTE: Redirecting to section " . $this->sections->section_requested);
$this->debug->debug_write(7, "NOTE: Redirecting to section " . $this->sections->section_requested);
$this->plugins->plugin_include("http_relocator");
//$this->plugins->installed->http_relocator->api->CmsDam_HTTP_relocator_SendToSectionNOCache($this->sections->section_requested);
}
return true;
}
// Checking user login
$err = 0;
$useldap = $this->ldap->ldap_ldapconnect();
if ($useldap != false) { // We can use LDAP, ready to begin
if (! $this->ldap->ldap_user_exist($username)) { // The user does not exists
$this->logs->log_note(3, "WARNING: Login failed for LDAP user \"$username\": user does not exist");
$this->debug->debug_write(5, "WARNING: Login failed for LDAP user \"$username\": user does not exist");
// Checking if LDAP login failure should failback to DB
if ($this->ldap->conf->LDAP_exclusive != "2") { // No, an error here should terminate the login sequence
return false;
} else { // Yes, errors on LDAP, failback to DB
$err++;
}
}
if ($err == 0) { // No errors from previous functions
if ($this->ldap->ldap_user_check($username, $password) == true) { // User credentials valid in LDAP
$this->logs->log_note(6, "NOTE: LDAP user \"$username\" logged in");
$this->debug->debug_write(7, "NOTE: LDAP user \"$username\" logged in");
// Register the session
$_SESSION['cmsdamUser'] = $username;
} else { // User credentials NOT valid in LDAP
$this->logs->log_note(3, "WARNING: Login failed for LDAP user \"$username\": password \"$password\" is not valid");
$this->debug->debug_write(5, "WARNING: Login failed for LDAP user \"$username\": password \"$password\" is not valid");
// Checking if LDAP login failure should failback to DB
if ($this->ldap->conf->LDAP_exclusive != "2") { // No, an error here should terminate the login sequence
return false;
} else {
$err++;
}
}
}
if ($err == 0) { // No error from previous functions
$this->logged_user = $username;
if ($reload == 1) { // The user want to reload the page on login
// We use cmsdam HTTP RELOCATOR plugin to send the user on the same template
// he was few milliseconds ago.
$this->logs->log_note(7, "NOTE: Redirecting to section " . $this->sections->section_requested);
$this->debug->debug_write(7, "NOTE: Redirecting to section " . $this->sections->section_requested);
$this->plugins->plugin_include("http_relocator");
//$this->plugins->installed->http_relocator->api->CmsDam_HTTP_relocator_SendToSectionNOCache($this->sections->section_requested);
}
return true;
} else { // Error from previous functions
$this->logs->log_note(3, "WARNING: Errors with LDAP login sequence, failback to DB");
$this->debug->debug_write(3, "WARNING: Login failed for LDAP user \"$username\": user does not exist");
}
}
// TODO: DEBUG THIS: DINO
if ($this->dbms->db_connected != 1) { // DB not connected
$this->debug->debug_write(4, "DB not connected: " . $this->db_connected);
$this->logs->log_note(4, "DB not connected: " . $this->db_connected);
return false;
}
if (! $this->user_exist($username, "1")) { // The user does not exists
$this->logs->log_note(3, "WARNING: Login failed for user \"$username\": user does not exist");
$this->debug->debug_write(3, "WARNING: Login failed for user \"$username\": user does not exist");
return false;
}
$dbuserpwd = $this->user_password($username, "1");
if ($dbuserpwd == $password) { // The password is valid
$this->logs->log_note(6, "NOTE: User \"$username\" logged in");
$this->debug->debug_write(6, "NOTE: User \"$username\" logged in");
// Register the session
$_SESSION['cmsdamUser'] = $username;
} else { // The password is not valid
$this->logs->log_note(3, "WARNING: Login failed for user \"$username\": password \"$password\" is not valid");
$this->debug->debug_write(3, "WARNING: Login failed for user \"$username\": password \"$password\" is not valid");
return false;
}
$this->logged_user = $username;
if ($reload == 1) { // The user want to reload the page on login
// We use cmsdam HTTP RELOCATOR plugin to send the user on the same template
// he was few seconds ago.
$this->logs->log_note(7, "NOTE: Redirecting to section " . $this->sections->section_requested);
$this->debug->debug_write(7, "NOTE: Redirecting to section " . $this->sections->section_requested);
$this->plugins->plugin_include("http_relocator");
$this->plugins->included->http_relocator->api->CmsDam_HTTP_relocator_SendToSection($this->sections->section_requested);
}
return true;
}
/**
* This api method can be used to logout a cmsdam user
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
* @param int reload If reload = 1 the page is automatic reloaded on logout
* @return bool True on success, else false
*/
function user_logout($reload=0) { // This method is used to logout a user
$cmsdamUser = $_SESSION['cmsdamUser'];
$this->logs->log_note(6, "NOTE: Logout requested for user \"$cmsdamUser\"...");
$this->logs->log_note(5, "NOTE: User \"" . $cmsdamUser . "\" logged out");
$this->debug->debug_write(6, "NOTE: User \"" . $cmsdamUser . "\" logged out");
unset ($_SESSION['cmsdamUser']);
$this->logged_user = "";
if ($reload == 1) { // The user want to reload the page on logout
// We use cmsdam HTTP RELOCATOR plugin to send the user on the same template
// he was few seconds ago.
$this->logs->log_note(7, "NOTE: Redirecting to section " . $this->sections->section_requested);
$this->plugins->plugin_include("http_relocator");
//$this->plugins->included->http_relocator->api->CmsDam_HTTP_relocator_SendToSectionNOCache($this->sections->section_requested);
$this->plugins->included->http_relocator->api->CmsDam_HTTP_relocator_SendToSection($this->sections->section_requested);
}
}
}
?>