piler/webui/model/user/user.php

560 lines
16 KiB
PHP
Raw Normal View History

2012-02-08 23:14:28 +01:00
<?php
class ModelUserUser extends Model {
2012-06-22 15:22:02 +02:00
public function check_uid($uid) {
2012-02-08 23:14:28 +01:00
if($uid == "") { return 0; }
if(!is_numeric($uid)) { return 0; }
if($uid < 1) { return 0; }
return 1;
}
2012-06-22 15:22:02 +02:00
public function get_uid_by_name($username = '') {
2012-02-08 23:14:28 +01:00
if($username == ""){ return -1; }
$query = $this->db->query("SELECT uid FROM " . TABLE_USER . " WHERE username=?", array($username));
if(isset($query->row['uid'])){
return $query->row['uid'];
}
return -1;
}
public function get_uid_by_email($email = '') {
$query = $this->db->query("SELECT uid FROM " . TABLE_EMAIL . " WHERE email=?", array($email));
if(isset($query->row['uid'])){ return $query->row['uid']; }
return -1;
}
public function get_username_by_email($email = '') {
$query = $this->db->query("SELECT username FROM " . TABLE_USER . ", " . TABLE_EMAIL . " WHERE " . TABLE_USER . ".uid=" . TABLE_EMAIL . ".uid AND email=?", array($email));
if(isset($query->row['username'])){ return $query->row['username']; }
return "";
}
2012-07-06 15:02:23 +02:00
public function get_users_all_email_addresses($uid = 0) {
2012-02-08 23:14:28 +01:00
$data = array();
$uids = $uid;
if($uid > 0) {
$query = $this->db->query("SELECT gid FROM " . TABLE_EMAIL_LIST . " WHERE uid=?", array((int)$uid));
if(isset($query->rows)) {
foreach ($query->rows as $q) {
if(is_numeric($q['gid']) && $q['gid'] > 0) {
$uids .= "," . $q['gid'];
}
}
}
$query = $this->db->query("SELECT email FROM " . TABLE_EMAIL . " WHERE uid IN ($uids)");
2012-02-08 23:14:28 +01:00
foreach ($query->rows as $q) {
array_push($data, $q['email']);
}
}
2012-06-22 12:30:55 +02:00
$query = $this->db->query("SELECT `" . TABLE_GROUP_EMAIL . "`.email FROM `" . TABLE_GROUP_EMAIL . "`, `" . TABLE_GROUP_USER . "` WHERE `" . TABLE_GROUP_EMAIL . "`.id=`" . TABLE_GROUP_USER . "`.id and `" . TABLE_GROUP_USER . "`.uid=?", array($uid) );
2012-06-22 12:30:55 +02:00
if(isset($query->rows)) {
foreach ($query->rows as $q) {
2012-07-06 15:02:23 +02:00
if(!in_array($q['email'], $data)) { array_push($data, $q['email']); }
2012-06-22 12:30:55 +02:00
}
}
2012-02-08 23:14:28 +01:00
return $data;
}
2013-01-05 16:42:36 +01:00
public function get_users_all_domains($uid = 0) {
$data = array();
if($uid > 0) {
$query = $this->db->query("SELECT domain FROM " . TABLE_DOMAIN_USER . " WHERE uid=?", array((int)$uid));
if(isset($query->rows)) {
foreach ($query->rows as $q) {
array_push($data, $q['domain']);
}
}
}
return $data;
}
2012-02-08 23:14:28 +01:00
public function get_additional_uids($uid = 0) {
$data = array();
if($uid > 0) {
$query = $this->db->query("SELECT gid FROM " . TABLE_EMAIL_LIST . " WHERE uid=?", array((int)$uid));
if(isset($query->rows)) {
foreach ($query->rows as $q) {
array_push($data, $q['gid']);
}
}
}
return $data;
}
2012-06-22 15:22:02 +02:00
public function get_emails($username = '') {
2012-02-08 23:14:28 +01:00
$emails = "";
$query = $this->db->query("SELECT " . TABLE_EMAIL . ".email AS email FROM " . TABLE_EMAIL . "," . TABLE_USER . " WHERE " . TABLE_EMAIL . ".uid=" . TABLE_USER . ".uid AND " . TABLE_USER . ".username=?", array($username));
foreach ($query->rows as $q) {
$emails .= $q['email'] . "\n";
}
return preg_replace("/\n$/", "", $emails);
}
2012-06-22 15:22:02 +02:00
public function get_emails_by_uid($uid = 0) {
2012-02-08 23:14:28 +01:00
$emails = "";
$query = $this->db->query("SELECT email FROM " . TABLE_EMAIL . " WHERE uid=?", array((int)$uid));
foreach ($query->rows as $q) {
$emails .= $q['email'] . "\n";
}
return preg_replace("/\n$/", "", $emails);
}
2013-01-05 16:42:36 +01:00
public function get_domains_by_uid($uid = 0) {
$domains = "";
$query = $this->db->query("SELECT domain FROM " . TABLE_DOMAIN_USER . " WHERE uid=?", array((int)$uid));
foreach ($query->rows as $q) {
$domains .= $q['domain'] . "\n";
}
return preg_replace("/\n$/", "", $domains);
}
2012-11-26 23:15:56 +01:00
public function get_primary_email_by_domain($uid = 0, $domain = '') {
$email = "";
$query = $this->db->query("SELECT email FROM " . TABLE_EMAIL . " WHERE uid=?", array((int)$uid));
if(isset($query->row)) { $email = $query->row['email']; }
foreach ($query->rows as $q) {
if(preg_match("/\@$domain$/", $q['email'])) { return $q['email']; }
}
return $email;
}
2012-06-22 15:22:02 +02:00
public function get_user_by_dn($dn = '') {
2012-02-08 23:14:28 +01:00
if($dn == '') { return array(); }
$query = $this->db->query("SELECT * FROM " . TABLE_USER . " WHERE dn=?", array($dn));
if($query->num_rows == 1) {
return $query->row;
}
return array();
}
2012-06-22 12:30:55 +02:00
public function get_user_by_uid($uid = 0) {
2012-02-08 23:14:28 +01:00
if(!is_numeric($uid) || (int)$uid < 0){
return array();
}
$query = $this->db->query("SELECT * FROM " . TABLE_USER . " WHERE uid=?", array((int)$uid));
return $query->row;
}
2012-06-22 15:22:02 +02:00
public function get_user_by_email($email = '') {
2012-02-08 23:14:28 +01:00
if($email == '') {
return array();
}
$query = $this->db->query("SELECT * FROM " . TABLE_USER . "," . TABLE_EMAIL . " WHERE " . TABLE_USER . ".uid=" . TABLE_EMAIL . ".uid AND email=?", array($email));
return $query->row;
}
2012-06-22 15:22:02 +02:00
public function get_users($search = '', $page = 0, $page_len = 0, $sort = 'username', $order = 0) {
2012-02-08 23:14:28 +01:00
$where_cond = " WHERE " . TABLE_USER . ".uid=" . TABLE_EMAIL . ".uid ";
$_order = "";
$users = array();
$my_domain = array();
$limit = "";
2012-07-06 11:02:21 +02:00
$q = array();
2012-02-08 23:14:28 +01:00
$from = (int)$page * (int)$page_len;
$search = preg_replace("/\s{1,}/", "", $search);
if($search){
2012-07-06 11:02:21 +02:00
$where_cond .= " AND email like ? ";
array_push($q, '%' . $search . '%');
2012-02-08 23:14:28 +01:00
}
/* sort order */
if($order == 0) { $order = "ASC"; }
else { $order = "DESC"; }
$_order = "ORDER BY $sort $order";
if($page_len > 0) { $limit = " LIMIT " . (int)$from . ", " . (int)$page_len; }
2012-07-06 11:02:21 +02:00
$query = $this->db->query("SELECT " . TABLE_USER . ".uid, isadmin, username, realname, domain, email FROM " . TABLE_USER . "," . TABLE_EMAIL . " $where_cond group by " . TABLE_USER . ".uid $_order $limit", $q);
2012-02-08 23:14:28 +01:00
foreach ($query->rows as $q) {
if(Registry::get('admin_user') == 1 || (isset($q['domain']) && $q['domain'] == $my_domain[0]) ) {
$users[] = array(
'uid' => $q['uid'],
'username' => $q['username'],
'realname' => $q['realname'],
'domain' => isset($q['domain']) ? $q['domain'] : "",
'email' => $q['email'],
'isadmin' => $q['isadmin']
);
}
}
return $users;
}
2012-06-22 15:22:02 +02:00
public function count_users($search = '') {
2012-02-08 23:14:28 +01:00
$where_cond = "";
2012-07-06 11:02:21 +02:00
$q = array();
2012-02-08 23:14:28 +01:00
if($search){
2012-07-06 11:02:21 +02:00
$where_cond .= " WHERE email like ? ";
array_push($q, '%' . $search . '%');
2012-02-08 23:14:28 +01:00
}
2012-07-06 11:02:21 +02:00
$query = $this->db->query("SELECT COUNT(*) AS num, uid FROM " . TABLE_EMAIL . " $where_cond group by uid", $q);
2012-02-08 23:14:28 +01:00
return $query->num_rows;
}
public function get_domains() {
$data = array();
$query = $this->db->query("SELECT DISTINCT mapped AS domain FROM " . TABLE_DOMAIN);
foreach ($query->rows as $q) {
array_push($data, $q['domain']);
}
return $data;
}
public function get_email_domains() {
$data = array();
$query = $this->db->query("SELECT domain FROM " . TABLE_DOMAIN);
foreach ($query->rows as $q) {
array_push($data, $q['domain']);
}
return $data;
}
2012-06-22 15:22:02 +02:00
public function get_next_uid() {
2012-02-08 23:14:28 +01:00
$query = $this->db->query("SELECT MAX(uid) AS last_id FROM " . TABLE_USER);
if(isset($query->row['last_id']) && $query->row['last_id'] > 0) {
return (int)$query->row['last_id'] + 1;
}
return 1;
}
2012-06-22 15:22:02 +02:00
public function add_user($user) {
2012-02-08 23:14:28 +01:00
LOGGER("add user: " . $user['username'] . ", uid=" . (int)$user['uid']);
if(!isset($user['domain']) || $user['domain'] == "") { return -1; }
2012-06-22 15:22:02 +02:00
if(!isset($user['username']) || $user['username'] == "" || $this->get_uid_by_name($user['username']) > 0) { return -1; }
2012-02-08 23:14:28 +01:00
$emails = explode("\n", $user['email']);
foreach ($emails as $email) {
$email = rtrim($email);
if(validemail($email) == 0) { continue; }
2012-02-08 23:14:28 +01:00
$query = $this->db->query("SELECT COUNT(*) AS count FROM " . TABLE_EMAIL . " WHERE email=?", array($email));
/* remove from memcached */
if(MEMCACHED_ENABLED) {
$memcache = Registry::get('memcache');
2012-02-10 14:35:07 +01:00
$memcache->delete(MEMCACHED_PREFIX . $email);
2012-02-08 23:14:28 +01:00
}
if($query->row['count'] > 0) {
return $email;
}
}
$query = $this->db->query("SELECT COUNT(*) AS count FROM " . TABLE_USER . " WHERE username=?", array($user['username']));
if($query->row['count'] > 0) {
return $user['username'];
}
$encrypted_password = crypt($user['password']);
$samaccountname = '';
if(isset($user['samaccountname'])) { $samaccountname = $user['samaccountname']; }
$query = $this->db->query("INSERT INTO " . TABLE_USER . " (uid, username, realname, password, domain, dn, isadmin, samaccountname) VALUES(?,?,?,?,?,?,?,?)", array((int)$user['uid'], $user['username'], $user['realname'], $encrypted_password, $user['domain'], @$user['dn'], (int)$user['isadmin'], $samaccountname));
2012-02-08 23:14:28 +01:00
if($query->error == 1 || $this->db->countAffected() == 0){ return $user['username']; }
foreach ($emails as $email) {
$email = rtrim($email);
if(validemail($email) == 0) { continue; }
2012-06-22 15:22:02 +02:00
$ret = $this->add_email((int)$user['uid'], $email);
2012-02-08 23:14:28 +01:00
if($ret == 0) { return -2; }
}
2013-01-05 16:42:36 +01:00
$this->update_domains_settings((int)$user['uid'], $user['domains']);
$this->update_group_settings((int)$user['uid'], $user['group']);
2012-09-06 15:27:20 +02:00
$this->update_folder_settings((int)$user['uid'], $user['folder']);
2012-02-08 23:14:28 +01:00
return 1;
}
2012-06-22 15:22:02 +02:00
public function add_email($uid = 0, $email = '') {
2012-02-08 23:14:28 +01:00
if($uid < 1 || $email == ""){ return 0; }
$query = $this->db->query("INSERT INTO " . TABLE_EMAIL . " (uid, email) VALUES(?,?)", array((int)$uid, $email));
$rc = $this->db->countAffected();
LOGGER("add email: $email, uid=$uid (rc=$rc)");
return $rc;
}
2012-06-22 15:22:02 +02:00
public function remove_email($uid = 0, $email = '') {
2012-02-08 23:14:28 +01:00
if((int)$uid < 1 || $email == ""){ return 0; }
$query = $this->db->query("DELETE FROM " . TABLE_EMAIL . " WHERE uid=? AND email=?", array((int)$uid, $email));
$rc = $this->db->countAffected();
LOGGER("remove email: $email, uid=$uid (rc=$rc)");
return $rc;
}
2012-06-22 15:22:02 +02:00
public function update_user($user) {
2012-02-08 23:14:28 +01:00
LOGGER("update user: " . $user['username'] . ", uid=" . (int)$user['uid']);
$emails = explode("\n", $user['email']);
foreach ($emails as $email) {
$email = rtrim($email);
if(validemail($email) == 0) { continue; }
2012-02-08 23:14:28 +01:00
$query = $this->db->query("SELECT COUNT(*) AS count FROM " . TABLE_EMAIL . " WHERE uid!=? AND email=?", array((int)$user['uid'], $email));
if($query->row['count'] > 0) {
return $email;
}
}
/* update password field if we have to */
if(strlen($user['password']) >= MIN_PASSWORD_LENGTH) {
$query = $this->db->query("UPDATE " . TABLE_USER . " SET password=? WHERE uid=?", array(crypt($user['password']), (int)$user['uid']));
if($this->db->countAffected() != 1) { return 0; }
}
$query = $this->db->query("UPDATE " . TABLE_USER . " SET username=?, realname=?, domain=?, dn=?, isadmin=? WHERE uid=?", array($user['username'], $user['realname'], $user['domain'], @$user['dn'], $user['isadmin'], (int)$user['uid']));
2012-02-08 23:14:28 +01:00
/* first, remove all his email addresses */
$query = $this->db->query("DELETE FROM " . TABLE_EMAIL . " WHERE uid=?", array((int)$user['uid']));
/* then add all the emails we have from the CGI post input */
foreach ($emails as $email) {
$email = rtrim($email);
if(validemail($email) == 0) { continue; }
2012-02-08 23:14:28 +01:00
$query = $this->db->query("INSERT INTO " . TABLE_EMAIL . " (uid, email) VALUES(?,?)", array((int)$user['uid'], $email));
/* remove from memcached */
if(MEMCACHED_ENABLED) {
$memcache = Registry::get('memcache');
2012-02-10 14:35:07 +01:00
$memcache->delete(MEMCACHED_PREFIX . $email);
2012-02-08 23:14:28 +01:00
}
}
2013-01-05 16:42:36 +01:00
$this->update_domains_settings((int)$user['uid'], $user['domains']);
$this->update_group_settings((int)$user['uid'], $user['group']);
2012-09-06 15:27:20 +02:00
$this->update_folder_settings((int)$user['uid'], $user['folder']);
return 1;
}
2013-01-05 16:42:36 +01:00
private function update_domains_settings($uid = -1, $domains = '') {
$__d = array();
if($uid <= 0) { return 0; }
$query = $this->db->query("DELETE FROM `" . TABLE_DOMAIN_USER . "` WHERE uid=?", array($uid));
$all_domains = $this->get_email_domains();
$submitted_domains = explode("\n", $domains);
foreach($submitted_domains as $d) {
$d = trim($d);
if($d && checkdomain($d, $all_domains) > 0) {
$query = $this->db->query("INSERT INTO `" . TABLE_DOMAIN_USER . "` (domain, uid) VALUES(?,?)", array($d, (int)$uid));
}
}
return 1;
}
private function update_group_settings($uid = -1, $group = '') {
2012-06-27 11:17:23 +02:00
$__g = array();
if($uid <= 0) { return 0; }
$query = $this->db->query("DELETE FROM `" . TABLE_GROUP_USER . "` WHERE uid=?", array($uid));
$query = $this->db->query("SELECT id, groupname FROM `" . TABLE_GROUP . "`");
$groups = array();
foreach ($query->rows as $q) {
$groups[$q['groupname']] = $q['id'];
}
$group = explode("\n", $group);
foreach($group as $g) {
$g = rtrim($g);
2012-07-06 21:58:52 +02:00
if($g && !isset($__g[$groups[$g]])) {
2012-06-27 11:17:23 +02:00
$query = $this->db->query("INSERT INTO `" . TABLE_GROUP_USER . "` (id, uid) VALUES(?,?)", array($groups[$g], (int)$uid));
$__g[$groups[$g]] = 1;
}
}
2012-02-08 23:14:28 +01:00
return 1;
}
2012-09-06 15:27:20 +02:00
private function update_folder_settings($uid = -1, $folder = '') {
$__g = array();
if($uid <= 0) { return 0; }
$query = $this->db->query("DELETE FROM `" . TABLE_FOLDER_USER . "` WHERE uid=?", array($uid));
$query = $this->db->query("SELECT id, name FROM `" . TABLE_FOLDER . "`");
$folders = array();
foreach ($query->rows as $q) {
$folders[$q['name']] = $q['id'];
}
$folder = explode("\n", $folder);
foreach($folder as $g) {
$g = rtrim($g);
if($g && !isset($__g[$folders[$g]])) {
$query = $this->db->query("INSERT INTO `" . TABLE_FOLDER_USER . "` (id, uid) VALUES(?,?)", array($folders[$g], (int)$uid));
$__g[$folders[$g]] = 1;
}
}
return 1;
}
public function update_dn_by_uid($uid = 0, $new_dn = '') {
if(!$this->check_uid($uid) || $new_dn == ''){ return 0; }
$user = $this->get_user_by_uid($uid);
if(isset($user['uid']) && $user['dn'] != '' && $user['dn'] != '*') {
$query = $this->db->query("UPDATE " . TABLE_USER . " SET dn=? WHERE uid=?", array($new_dn, $uid));
return 1;
}
return 0;
}
2012-06-22 15:22:02 +02:00
public function delete_user($uid) {
if(!$this->check_uid($uid)){ return 0; }
2012-02-08 23:14:28 +01:00
$query = $this->db->query("DELETE FROM " . TABLE_EMAIL . " WHERE uid=?", array((int)$uid));
$query = $this->db->query("DELETE FROM " . TABLE_USER . " WHERE uid=?", array((int)$uid));
LOGGER("remove user: uid=$uid");
return 1;
}
}
?>