[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <?php 2 /**************************************************************************\ 3 * eGroupWare API - Accounts manager for SQL * 4 * Written by Joseph Engo <jengo@phpgroupware.org> * 5 * and Dan Kuykendall <seek3r@phpgroupware.org> * 6 * and Bettina Gille [ceb@phpgroupware.org] * 7 * View and manipulate account records using SQL * 8 * Copyright (C) 2000 - 2002 Joseph Engo * 9 * Copyright (C) 2003 Joseph Engo, Bettina Gille * 10 * ------------------------------------------------------------------------ * 11 * This library is part of the eGroupWare API * 12 * http://www.egroupware.org * 13 * ------------------------------------------------------------------------ * 14 * This library is free software; you can redistribute it and/or modify it * 15 * under the terms of the GNU Lesser General Public License as published by * 16 * the Free Software Foundation; either version 2.1 of the License, * 17 * or any later version. * 18 * This library is distributed in the hope that it will be useful, but * 19 * WITHOUT ANY WARRANTY; without even the implied warranty of * 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * 21 * See the GNU Lesser General Public License for more details. * 22 * You should have received a copy of the GNU Lesser General Public License * 23 * along with this library; if not, write to the Free Software Foundation, * 24 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * 25 \**************************************************************************/ 26 /* $Id: class.accounts_sql.inc.php 21491 2006-04-30 12:26:59Z ralfbecker $ */ 27 28 /** 29 * Class for handling user and group accounts in SQL 30 */ 31 class accounts_ 32 { 33 var $db; 34 var $account_id; 35 var $data; 36 var $total; 37 var $table = 'egw_accounts'; 38 39 function accounts_() 40 { 41 } 42 43 function list_methods($_type='xmlrpc') 44 { 45 if (is_array($_type)) 46 { 47 $_type = $_type['type'] ? $_type['type'] : $_type[0]; 48 } 49 50 switch($_type) 51 { 52 case 'xmlrpc': 53 $xml_functions = array( 54 'get_list' => array( 55 'function' => 'get_list', 56 'signature' => array(array(xmlrpcStruct)), 57 'docstring' => lang('Returns a full list of accounts on the system. Warning: This is return can be quite large') 58 ), 59 'list_methods' => array( 60 'function' => 'list_methods', 61 'signature' => array(array(xmlrpcStruct,xmlrpcString)), 62 'docstring' => lang('Read this list of methods.') 63 ) 64 ); 65 return $xml_functions; 66 break; 67 case 'soap': 68 return $this->soap_functions; 69 break; 70 default: 71 return array(); 72 break; 73 } 74 } 75 76 /** 77 * grabs the records from the data store 78 * 79 */ 80 function read_repository() 81 { 82 $this->db->select($this->table,'*',array('account_id'=>abs($this->account_id)),__LINE__,__FILE__); 83 84 $this->data['account_id'] = $this->db->next_record() ? $this->account_id : null; 85 $this->data['account_lid'] = $this->data['userid'] = $this->db->f('account_lid'); 86 $this->data['firstname'] = $this->db->f('account_firstname'); 87 $this->data['lastname'] = $this->db->f('account_lastname'); 88 $this->data['fullname'] = $this->db->f('account_firstname') . ' ' . $this->db->f('account_lastname'); 89 $this->data['lastlogin'] = $this->db->f('account_lastlogin'); 90 $this->data['lastloginfrom'] = $this->db->f('account_lastloginfrom'); 91 $this->data['lastpasswd_change'] = $this->db->f('account_lastpwd_change'); 92 $this->data['status'] = $this->db->f('account_status'); 93 $this->data['expires'] = $this->db->f('account_expires'); 94 $this->data['person_id'] = $this->db->f('person_id'); 95 $this->data['account_primary_group'] = $this->db->f('account_primary_group'); 96 $this->data['email'] = $this->db->f('account_email'); 97 98 return $this->data; 99 } 100 101 /** 102 * saves the records to the data store 103 * 104 */ 105 function save_repository() 106 { 107 $this->db->update($this->table,array( 108 'account_firstname' => $this->data['firstname'], 109 'account_lastname' => $this->data['lastname'], 110 'account_status' => $this->data['status'], 111 'account_expires' => $this->data['expires'], 112 'account_lid' => $this->data['account_lid'], 113 'person_id' => $this->data['person_id'], 114 'account_primary_group' => $this->data['account_primary_group'], 115 'account_email' => $this->data['email'], 116 ),array( 117 'account_id' => abs($this->account_id) 118 ),__LINE__,__FILE__); 119 } 120 121 function delete($accountid = '') 122 { 123 $account_id = get_account_id($accountid); 124 125 /* Do this last since we are depending upon this record to get the account_lid above */ 126 $this->db->lock(Array($this->table)); 127 $this->db->delete($this->table,array('account_id'=>abs($account_id)),__LINE__,__FILE__); 128 $this->db->unlock(); 129 } 130 131 function get_list($_type='both',$start = '',$sort = '', $order = '', $query = '', $offset = '',$query_type='') 132 { 133 if (! $sort) 134 { 135 $sort = "DESC"; 136 } 137 138 if (!empty($order) && preg_match('/^[a-zA-Z_0-9, ]+$/',$order) && (empty($sort) || preg_match('/^(DESC|ASC|desc|asc)$/',$sort))) 139 { 140 $orderclause = "ORDER BY $order $sort"; 141 } 142 else 143 { 144 $orderclause = "ORDER BY account_lid ASC"; 145 } 146 147 switch($_type) 148 { 149 case 'accounts': 150 $whereclause = "WHERE account_type = 'u'"; 151 break; 152 case 'groups': 153 $whereclause = "WHERE account_type = 'g'"; 154 break; 155 default: 156 $whereclause = ''; 157 } 158 159 if ($query) 160 { 161 if ($whereclause) 162 { 163 $whereclause .= ' AND ( '; 164 } 165 else 166 { 167 $whereclause = ' WHERE ( '; 168 } 169 switch($query_type) 170 { 171 case 'all': 172 default: 173 $query = '%'.$query; 174 // fall-through 175 case 'start': 176 $query .= '%'; 177 // fall-through 178 case 'exact': 179 $query = $this->db->quote($query); 180 $whereclause .= " account_firstname LIKE $query OR account_lastname LIKE $query OR account_lid LIKE $query )"; 181 break; 182 case 'firstname': 183 case 'lastname': 184 case 'lid': 185 case 'email': 186 $query = $this->db->quote('%'.$query.'%'); 187 $whereclause .= " account_$query_type LIKE $query )"; 188 break; 189 } 190 } 191 192 $sql = "SELECT * FROM $this->table $whereclause $orderclause"; 193 if ($offset) 194 { 195 $this->db->limit_query($sql,$start,__LINE__,__FILE__,$offset); 196 } 197 elseif (is_numeric($start)) 198 { 199 $this->db->limit_query($sql,$start,__LINE__,__FILE__); 200 } 201 else 202 { 203 $this->db->query($sql,__LINE__,__FILE__); 204 } 205 while ($this->db->next_record()) 206 { 207 $accounts[] = Array( 208 'account_id' => ($this->db->f('account_type') == 'g' ? -1 : 1) * $this->db->f('account_id'), 209 'account_lid' => $this->db->f('account_lid'), 210 'account_type' => $this->db->f('account_type'), 211 'account_firstname' => $this->db->f('account_firstname'), 212 'account_lastname' => $this->db->f('account_lastname'), 213 'account_status' => $this->db->f('account_status'), 214 'account_expires' => $this->db->f('account_expires'), 215 'person_id' => $this->db->f('person_id'), 216 'account_primary_group' => $this->db->f('account_primary_group'), 217 'account_email' => $this->db->f('account_email'), 218 ); 219 } 220 $this->db->query("SELECT count(*) FROM $this->table $whereclause"); 221 $this->total = $this->db->next_record() ? $this->db->f(0) : 0; 222 223 return $accounts; 224 } 225 226 /** 227 * converts a name / unique value from the accounts-table (account_lid,account_email) to an id 228 */ 229 function name2id($name,$which='account_lid') 230 { 231 $this->db->select($this->table,'account_id,account_type',array($which=>$name),__LINE__,__FILE__); 232 if($this->db->next_record()) 233 { 234 return ($this->db->f('account_type') == 'g' ? -1 : 1) * $this->db->f('account_id'); 235 } 236 return False; 237 } 238 239 /** 240 * converts an id to the corresponding value of the accounts-table (account_lid,account_email,account_firstname,...) 241 */ 242 function id2name($account_id,$which='account_lid') 243 { 244 $this->db->select($this->table,$this->db->name_quote($which),array('account_id'=>abs($account_id)),__LINE__,__FILE__); 245 if($this->db->next_record()) 246 { 247 return $this->db->f(0); 248 } 249 return False; 250 } 251 252 function exists($account_lid) 253 { 254 static $by_id, $by_lid; 255 256 $where = array(); 257 if(is_numeric($account_lid)) 258 { 259 if(@isset($by_id[$account_lid]) && $by_id[$account_lid] != '') 260 { 261 return $by_id[$account_lid]; 262 } 263 $where['account_id'] = abs($account_lid); 264 } 265 else 266 { 267 if(@isset($by_lid[$account_lid]) && $by_lid[$account_lid] != '') 268 { 269 return $by_lid[$account_lid]; 270 } 271 $where['account_lid'] = $account_lid; 272 } 273 274 $this->db->select($this->table,'count(*)',$where,__LINE__,__FILE__); 275 $this->db->next_record(); 276 $ret_val = $this->db->f(0) > 0; 277 if(is_numeric($account_lid)) 278 { 279 $by_id[$account_lid] = $ret_val; 280 $by_lid[$this->id2name($account_lid)] = $ret_val; 281 } 282 else 283 { 284 $by_lid[$account_lid] = $ret_val; 285 $by_id[$this->name2id($account_lid)] = $ret_val; 286 } 287 return $ret_val; 288 } 289 290 function create($account_info) 291 { 292 $account_data = array( 293 'account_lid' => $account_info['account_lid'], 294 'account_pwd' => $GLOBALS['egw']->common->encrypt_password($account_info['account_passwd'],True), 295 'account_firstname' => $account_info['account_firstname'], 296 'account_lastname' => $account_info['account_lastname'], 297 'account_status' => $account_info['account_status'], 298 'account_expires' => $account_info['account_expires'], 299 'account_type' => $account_info['account_type'], 300 'person_id' => $account_info['person_id'], 301 'account_primary_group' => $account_info['account_primary_group'], 302 'account_email' => $account_info['account_email'], 303 ); 304 if (isset($account_info['account_id']) && (int)$account_info['account_id'] && !$this->id2name($account_info['account_id'])) 305 { 306 // only use account_id, if it's not already used 307 $account_data['account_id'] = abs($account_info['account_id']); 308 } 309 if (!$this->db->insert($this->table,$account_data,False,__LINE__,__FILE__)) 310 { 311 return false; 312 } 313 $id = $account_data['account_id'] ? $account_data['account_id'] : $this->db->get_last_insert_id($this->table,'account_id'); 314 315 if ($account_info['account_type'] == 'g' && $id > 0) // create negative id for groups 316 { 317 $id = -$id; 318 } 319 return $id; 320 } 321 322 function auto_add($accountname, $passwd, $default_prefs = False, $default_acls = False, $expiredate = 0, $account_status = 'A') 323 { 324 if ($expiredate == 0) 325 { 326 if(isset($GLOBALS['egw_info']['server']['auto_create_expire']) == True) 327 { 328 if($GLOBALS['egw_info']['server']['auto_create_expire'] == 'never') 329 { 330 $expires = -1; 331 } 332 else 333 { 334 $expiredate = time() + $GLOBALS['egw_info']['server']['auto_create_expire']; 335 } 336 } 337 } 338 else 339 { 340 /* expire in 30 days by default */ 341 $expiredate = time() + ((60 * 60) * (30 * 24)); 342 } 343 344 if ($expires != -1) 345 { 346 $expires = mktime(2,0,0,date('n',$expiredate), (int)date('d',$expiredate), date('Y',$expiredate)); 347 } 348 349 $default_group_id = $this->name2id($GLOBALS['egw_info']['server']['default_group_lid']); 350 if (!$default_group_id) 351 { 352 $default_group_id = (int) $this->name2id('Default'); 353 } 354 $primary_group = $GLOBALS['auto_create_acct']['primary_group'] && 355 $this->get_type((int)$GLOBALS['auto_create_acct']['primary_group']) == 'g' ? 356 (int) $GLOBALS['auto_create_acct']['primary_group'] : $default_group_id; 357 358 $acct_info = array( 359 'account_id' => (int) $GLOBALS['auto_create_acct']['id'], 360 'account_lid' => $accountname, 361 'account_type' => 'u', 362 'account_passwd' => $passwd, 363 'account_firstname' => $GLOBALS['auto_create_acct']['firstname'] ? $GLOBALS['auto_create_acct']['firstname'] : 'New', 364 'account_lastname' => $GLOBALS['auto_create_acct']['lastname'] ? $GLOBALS['auto_create_acct']['lastname'] : 'User', 365 'account_status' => $account_status, 366 'account_expires' => $expires, 367 'account_primary_group' => $primary_group, 368 ); 369 370 /* attempt to set an email address */ 371 if (isset($GLOBALS['auto_create_acct']['email']) == True && $GLOBALS['auto_create_acct']['email'] != '') 372 { 373 $acct_info['account_email'] = $GLOBALS['auto_create_acct']['email']; 374 } 375 elseif(isset($GLOBALS['egw_info']['server']['mail_suffix']) == True && $GLOBALS['egw_info']['server']['mail_suffix'] != '') 376 { 377 $acct_info['account_email'] = $accountname . '@' . $GLOBALS['egw_info']['server']['mail_suffix']; 378 } 379 380 $this->db->transaction_begin(); 381 382 $accountid = $this->create($acct_info); /* create the account */ 383 384 if ($accountid) /* begin account setup */ 385 { 386 /* If we have a primary_group, add it as "regular" eGW group (via ACL) too. */ 387 if ($primary_group) 388 { 389 $GLOBALS['egw']->acl->add_repository('phpgw_group', $primary_group,$accountid,1); 390 } 391 392 /* if we have an mail address set it in the users' email preference */ 393 if (isset($GLOBALS['auto_create_acct']['email']) && $GLOBALS['auto_create_acct']['email'] != '') 394 { 395 $GLOBALS['egw']->acl->acl($accountid); /* needed als preferences::save_repository calls acl */ 396 $GLOBALS['egw']->preferences->preferences($accountid); 397 $GLOBALS['egw']->preferences->read_repository(); 398 $GLOBALS['egw']->preferences->add('email','address',$GLOBALS['auto_create_acct']['email']); 399 $GLOBALS['egw']->preferences->save_repository(); 400 } 401 /* use the default mail domain to set the uesrs' email preference */ 402 elseif(isset($GLOBALS['egw_info']['server']['mail_suffix']) && $GLOBALS['egw_info']['server']['mail_suffix'] != '') 403 { 404 $GLOBALS['egw']->acl->acl($accountid); /* needed als preferences::save_repository calls acl */ 405 $GLOBALS['egw']->preferences->preferences($accountid); 406 $GLOBALS['egw']->preferences->read_repository(); 407 $GLOBALS['egw']->preferences->add('email','address', $accountname . '@' . $GLOBALS['egw_info']['server']['mail_suffix']); 408 $GLOBALS['egw']->preferences->save_repository(); 409 } 410 411 /* commit the new account transaction */ 412 $this->db->transaction_commit(); 413 414 // call hook to notify interested apps about the new account 415 $GLOBALS['hook_values']['account_lid'] = $acct_info['account_lid']; 416 $GLOBALS['hook_values']['account_id'] = $accountid; 417 $GLOBALS['hook_values']['new_passwd'] = $acct_info['account_passwd']; 418 $GLOBALS['hook_values']['account_status'] = $acct_info['account_status']; 419 $GLOBALS['hook_values']['account_firstname'] = $acct_info['account_firstname']; 420 $GLOBALS['hook_values']['account_lastname'] = $acct_info['account_lastname']; 421 $GLOBALS['egw']->hooks->process($GLOBALS['hook_values']+array( 422 'location' => 'addaccount', 423 // at login-time only the hooks from the following apps will be called 424 'order' => array('felamimail','fudforum'), 425 ),False,True); /* called for every app now, not only enabled ones */ 426 427 } /* end account setup */ 428 else /* if no account id abort the account creation */ 429 { 430 $this->db->transaction_abort(); 431 } 432 433 /* 434 * If we succeeded in creating the account (above), return the accountid, else, 435 * return the error value from $this->name2id($accountname) 436 */ 437 return $accountid; 438 439 } /* end auto_add() */ 440 441 function get_account_name($accountid,&$lid,&$fname,&$lname) 442 { 443 $this->db->select($this->table,'account_lid,account_firstname,account_lastname',array('account_id'=>abs($accountid)),__LINE__,__FILE__); 444 if (!$this->db->next_record()) 445 { 446 return False; 447 } 448 $lid = $this->db->f('account_lid'); 449 $fname = $this->db->f('account_firstname'); 450 $lname = $this->db->f('account_lastname'); 451 452 return True; 453 } 454 455 /** 456 * Update the last login timestamps and the IP 457 * 458 * @param int $account_id 459 * @param string $ip 460 * @return int lastlogin time 461 */ 462 function update_lastlogin($account_id, $ip) 463 { 464 $this->db->select($this->table,'account_lastlogin',array('account_id'=>abs($account_id)),__LINE__,__FILE__); 465 $previous_login = $this->db->next_record() ? $this->db->f('account_lastlogin') : false; 466 467 $this->db->update($this->table,array( 468 'account_lastloginfrom' => $ip, 469 'account_lastlogin' => time(), 470 ),array( 471 'account_id' => abs($account_id), 472 ),__LINE__,__FILE__); 473 474 return $previous_login; 475 } 476 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |