home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / classes / users.class.inc < prev    next >
Text File  |  2004-03-08  |  24KB  |  722 lines

  1. <?php
  2. /*
  3.    Copyright Intermesh 2003
  4.    Author: Merijn Schering <mschering@intermesh.nl>
  5.    Version: 1.0 Release date: 08 July 2003
  6.  
  7.    This program is free software; you can redistribute it and/or modify it
  8.    under the terms of the GNU General Public License as published by the
  9.    Free Software Foundation; either version 2 of the License, or (at your
  10.    option) any later version.
  11.  */
  12.  
  13. require_once($GO_CONFIG->class_path.'profiles.class.inc');
  14.  
  15. class users extends db
  16. {
  17.   var $user_id;
  18.   var $ldap;
  19.   var $ldap_um = false;
  20.   var $profile;
  21.   var $userlist;
  22.   var $userlist_index;
  23.  
  24.   function users()
  25.   {
  26.     global $GO_CONFIG;
  27.     $this->db();
  28.  
  29.     if ($GO_CONFIG->auth_db_type == "ldap" )
  30.     {
  31.       $this->ldap = new ldap();
  32.       $this->ldap->connect();
  33.       // TODO: Probably we could connect with our own ldap-uid and not as admin
  34.       // (or whatever is configured in GO as rootdn), so that we definitely can
  35.       // only see what we are allowed by LDAP access rights. So we cannot change
  36.       // attributes of other users. Probably administrator should bind with
  37.       // rootdn.
  38.       $this->ldap->bind($_SESSION['GO_SESSION']['user_id'],
  39.     $_SESSION['GO_SESSION']['user_auth_id']);
  40.       if ( $GO_CONFIG->auth_db_ldap_um )
  41.     $this->ldap_um = true;
  42.  
  43.     }
  44.  
  45.     if (isset($_SESSION['GO_SESSION']['user_id']))
  46.     {
  47.       $this->logged_in=true;
  48.       $this->user_id=$_SESSION['GO_SESSION']['user_id'];
  49.     }
  50.   }
  51.  
  52.   function search($query, $field, $user_id, $start=0, $offset=0)
  53.   {
  54.     if ( $this->ldap_um )
  55.     {
  56.       $query = substr( $query, 1, strlen( $query ) - 2 );
  57.       switch( $field )
  58.       {
  59.     default:
  60.         case "name":
  61.           $filter="(&(cn=*".utf8_encode($query)."*)(mail=*))";
  62.         break;
  63.       }
  64.       $this->ldap->search($filter, $this->ldap->PeopleDN );
  65.     //, array( "uidNumber", "uid", "cn"));
  66.       $this->ldap->sort( "sn" );
  67.       $ldapentries = $this->ldap->num_entries();
  68.       $entries = $this->ldap->get_entries();
  69.       $profile = new profiles();
  70.       for ( $i=0; $i<$entries["count"]; $i++ )
  71.     $this->userlist[] = $profile->convert_profile_ldap( $entries[$i] );
  72.       
  73.       $this->userlist_index = 0;
  74.       return count($this->userlist);
  75.       
  76.     } else
  77.     {
  78.       $sql = "SELECT DISTINCT users.* FROM users, users_groups INNER ".
  79.     "JOIN acl ON users.acl_id= acl.acl_id WHERE ".
  80.     "((acl.group_id = users_groups.group_id AND users_groups.user_id = ".
  81.     $user_id.") OR (acl.user_id = ".$user_id." )) AND $field LIKE '".
  82.     smart_addslashes($query)."' ORDER BY first_name ASC, last_name ASC";
  83.       if ($offset != 0)
  84.     $sql .= " LIMIT $start, $offset";
  85.     
  86.       $this->query($sql);
  87.       return $this->num_rows();
  88.     }
  89.   }
  90.  
  91.   function authorize($requesting_user_id, $authcode, $accepting_user_id)
  92.   {
  93.     global $GO_SECURITY;
  94.       
  95.       $this->query("SELECT acl_id, id FROM users WHERE authcode='".smart_addslashes($authcode)."' AND id='$requesting_user_id'");
  96.       if ($this->next_record())
  97.       {
  98.     $acl_id = $this->f("acl_id");
  99.       if (!$GO_SECURITY->user_in_acl($accepting_user_id, $acl_id))
  100.       {
  101.         if (!$GO_SECURITY->add_user_to_acl($accepting_user_id,$acl_id))
  102.         {
  103.           return false;
  104.         }
  105.       }
  106.     $this->query("SELECT acl_id FROM users WHERE id='$accepting_user_id'");
  107.       if ($this->next_record())
  108.       {
  109.         $acl_id = $this->f("acl_id");
  110.           if (!$GO_SECURITY->user_in_acl($requesting_user_id, $acl_id))
  111.           {
  112.         if (!$GO_SECURITY->add_user_to_acl($requesting_user_id,$acl_id))
  113.         {
  114.           return false;
  115.         }
  116.           }
  117.       }
  118.     return true;
  119.       }else
  120.       {
  121.     return false;
  122.       }
  123.   }
  124.  
  125.   function delete_samba_account($user_id)
  126.   {
  127.     global $GO_CONFIG;
  128.  
  129.     $sql = "UPDATE users SET samba_user='0' WHERE id='$user_id'";
  130.     if ($user = $this->get_user($user_id))
  131.     {
  132.       exec($GO_CONFIG->sudo." ".$GO_CONFIG->smbdeluser." \"".$user['username']."\"");
  133.       $this->query($sql);
  134.     }
  135.     if ($this->ldap)
  136.     {
  137.       $this->ldap->search("uidNumber=$user_id", $this->ldap->PeopleDN);
  138.       if ($this->ldap->num_entries() > 0)
  139.       {
  140.     $this->ldap->next_entry();
  141.     if (($this->ldap->in_values("objectClass", "sambaSamAccount")) ||
  142.         ($this->ldap->in_values("objectClass", "sambaAccount"   )))
  143.     {
  144.       // TODO: delete the corresponding attributes if they are
  145.       // not present in any other objectClass...
  146.     }
  147.       }
  148.     }
  149.   }
  150.  
  151.   function create_samba_account($user_id)
  152.   {
  153.     global $GO_CONFIG;
  154.  
  155.     $sql = "UPDATE users SET samba_user='1' WHERE id='$user_id'";
  156.     if ($user = $this->get_user($user_id))
  157.     {
  158.       exec($GO_CONFIG->sudo.' '.$GO_CONFIG->auto_smbadduser.' "'.$user['username'].'" "'.$user['password'].'"');
  159.       $this->query($sql);
  160.     }
  161.     if ($this->ldap)
  162.     {
  163.       $this->ldap->search("uidNumber=$user_id", $this->ldap->PeopleDN);
  164.       if ($this->ldap->num_entries() > 0)
  165.       {
  166.     $this->ldap->next_entry();
  167.     // TODO: add objectClass sambaAccount or sambaSamAccount (if present)
  168.     // and corresponding attributes
  169.       }
  170.     }
  171.   }
  172.  
  173.   function get_users($sort="name",$direction="ASC", $start=0, $offset=0)
  174.   {
  175.     if ($sort == 'name')
  176.     {
  177.       $sort = 'first_name '.$direction.', last_name';
  178.     }
  179.     $sqlrows=0;
  180.       $this->query("SELECT COUNT(*) FROM users");
  181.       if ($this->next_record())
  182.       {
  183.     $sqlrows = $this->f(0);
  184.       }
  185.     
  186.       if ($sqlrows > 0)
  187.       {
  188.     $sql = "SELECT * FROM users ORDER BY ".$sort." ".$direction;
  189.     
  190.       if ($offset != 0)
  191.       {
  192.         $sql .= " LIMIT $start,$offset";
  193.       }
  194.     $this->query($sql);
  195.       }
  196.     
  197.       // No need to use LDAP here, since each LDAP user is automatically created in
  198.       // SQL after his first login. If you do further work with the result of this
  199.       // search you will NOT have LDAP attributes in the records. I'm not sure if this
  200.       // could be a problem at the moment...
  201.       if ( $this->ldap_um ) {
  202.     // Since we user LDAP User Management there is no need to store User Accounts in SQL.
  203.     //          while ( parent::next_record() ) {
  204.     //            $this->userlist[] = $this->Record;
  205.     //          }
  206.     $this->ldap->search("(&(uid=*)(mail=*))", $this->ldap->PeopleDN ); //, array( "uidNumber", "uid", "cn"));
  207.     $this->ldap->sort( "sn" );
  208.     $ldapentries = $this->ldap->num_entries();
  209.     $entries = $this->ldap->get_entries();
  210.     $profile = new profiles();
  211.     if ( $offset == 0 ) { $offset = $entries["count"]; }
  212.     for ( $i=$start; ( $i<$entries["count"] ) && ( $i<$start+$offset ); $i++ ) {
  213.       $this->userlist[] = $profile->convert_profile_ldap( $entries[$i] );
  214.     }
  215.  
  216.     //          sort( $this->userlist );
  217.     $this->userlist_index = 0;
  218.     return $entries["count"];
  219.       }
  220.     return $sqlrows;
  221.   }
  222.   
  223.     function get_authorized_users($user_id, $sort="name",$direction="ASC")
  224.     {
  225.       if ($sort == 'users.name' || $sort=='name')
  226.       {
  227.     $sort = 'users.first_name AND users.last_name';
  228.       }
  229.       $sql = "SELECT DISTINCT users.* FROM users, users_groups INNER JOIN acl ON users.acl_id= acl.acl_id WHERE ".
  230.     "((acl.group_id = users_groups.group_id AND users_groups.user_id = ".$user_id.") OR (".
  231.     "acl.user_id = ".$user_id." )) ORDER BY ".$sort." ".$direction;
  232.     
  233.     $this->query($sql);
  234.     return $this->num_rows();
  235.     }
  236.  
  237.   function next_record() {
  238.     if ( $this->ldap_um ) {
  239.       if ( count( $this->userlist ) > $this->userlist_index ) {
  240.     $this->Record = $this->userlist[$this->userlist_index++];
  241.     return $this->Record;
  242.       } else {
  243.     return false;
  244.       }
  245.     } else {
  246.       return parent::next_record();
  247.     }
  248.   }
  249.  
  250.   function get_profile_by_email($email)
  251.   {
  252.     if ( $uid = $this->get_user_id_by_email( $email ) ) {
  253.       $profile = new profiles();
  254.       //MS: update $this->Record to so it won't break when function f() is called
  255.       // from the db class
  256.       $this->Record = $profile->get_profile( $uid );
  257.       return $this->Record;
  258.     }
  259.     return false;
  260.   }
  261.  
  262.   function get_user_id_by_email($email)
  263.   {
  264.     $sql = "SELECT id FROM users WHERE email='".smart_addslashes($email)."'";
  265.     $this->query($sql);
  266.  
  267.     if ($this->next_record())
  268.     {
  269.       return $this->f("id");
  270.     }else if ($this->ldap)
  271.     {
  272.       // I'm not sure if we really need this, because each LDAP user should be in
  273.       // SQL too. But in LDAP you have the possibility to specify more than one
  274.       // email address.
  275.       $this->ldap->search("mail=$email", $this->ldap->PeopleDN);
  276.       if ( $this->ldap->num_entries() > 0 )
  277.       {
  278.     $this->ldap->next_entry();
  279.     return $this->ldap->first_value("uidnumber");
  280.       }
  281.     }
  282.     return false;
  283.   }
  284.  
  285.   function check_password($password)
  286.   {
  287.     if ($_SESSION['GO_SESSION']['auth_src']=="ldap")
  288.     {
  289.       if ($this->ldap)
  290.       {
  291.     // rebinding is not an optimal solution. hints for doing better are welcome...
  292.     $ok = false;
  293.     if ($this->ldap->bind("uid=".$_SESSION['GO_SESSION']['username'].",".$this->ldap->PeopleDN, $password))
  294.     {
  295.       $ok = true;
  296.     }
  297.     $this->ldap->bind();
  298.     return $ok;
  299.       }
  300.     }else
  301.     {
  302.       $this->query("SELECT id FROM users WHERE password='".md5($password)."' AND id='$this->user_id'");
  303.       if ($this->num_rows() > 0)
  304.     return true;
  305.     }
  306.     return false;
  307.   }
  308.  
  309.   function get_user($user_id)
  310.   {
  311.     $profile = new profiles();
  312.     //MS: update $this->Record to so it won't break when function f() is called
  313.     // from the db class
  314.     $this->Record = $profile->get_profile( $user_id );
  315.       return $this->Record;
  316.  
  317.   }
  318.  
  319.   function update_profile($user_id, $first_name, $middle_name, $last_name, $initials, $title, $sex, $birthday, $email,$work_phone, $home_phone, $fax, $cellular, $country, $state, $city, $zip, $address, $company, $work_country, $work_state, $work_city, $work_zip, $work_address, $work_fax, $homepage, $department, $function)
  320.   {
  321.     $middle_name = trim($middle_name);
  322.       
  323.       $sql = "UPDATE users SET first_name='".smart_addslashes($first_name)."', middle_name='".smart_addslashes($middle_name)."', last_name='".smart_addslashes($last_name)."', initials='".smart_addslashes($initials)."', title='".smart_addslashes($title)."', sex='$sex', birthday='".smart_addslashes($birthday)."', email='".smart_addslashes($email)."', work_phone='".smart_addslashes($work_phone)."', home_phone='".smart_addslashes($home_phone)."', fax='".smart_addslashes($fax)."', cellular='".smart_addslashes($cellular)."', country='".smart_addslashes($country)."', state='".smart_addslashes($state)."'";
  324.     $sql .= ", city='".smart_addslashes($city)."', zip='".smart_addslashes($zip)."', address='".smart_addslashes($address)."',company='".smart_addslashes($company)."', department='".smart_addslashes($department)."', function='".smart_addslashes($function)."', work_country='".smart_addslashes($work_country)."', work_state='".smart_addslashes($work_state)."', work_city='".smart_addslashes($work_city)."', work_zip='".smart_addslashes($work_zip)."', work_address='".smart_addslashes($work_address)."', work_fax='".smart_addslashes($work_fax)."', homepage='".smart_addslashes($homepage)."' WHERE id='$user_id'";
  325.     if ($this->query($sql))
  326.     {
  327.       if ($user_id == $this->user_id)
  328.       {
  329.     $middle_name = $middle_name == '' ? '' : $middle_name.' ';
  330.       
  331.       $_SESSION['GO_SESSION']['name']  = $first_name.' '.$middle_name.$last_name;
  332.     $_SESSION['GO_SESSION']['first_name']  = $first_name;
  333.       $_SESSION['GO_SESSION']['middle_name']  = $middle_name;
  334.       $_SESSION['GO_SESSION']['last_name']  = $last_name;
  335.       $_SESSION['GO_SESSION']['email'] = $email;
  336.       }
  337.       if ($this->ldap)
  338.       {
  339.     $this->ldap->search("uidNumber=".$this->user_id, $this->ldap->PeopleDN);
  340.     if ($this->ldap->num_entries() > 0)
  341.     {
  342.       $this->ldap->next_entry();
  343.       // TODO: update ldap attributes if we are able to write. This needs
  344.       // to be intelligent code because LDAP structure is mostly different.
  345.     }
  346.       }
  347.       return true;
  348.     }
  349.     return false;
  350.   }
  351.  
  352.   function update_password($user_id, $password,$old_password=null)
  353.   {
  354.     global $GO_CONFIG,$GO_CRYPTO,$GO_SESSION;
  355.     if($profile = $this->get_user($user_id))
  356.     {
  357.       $sql = "UPDATE users SET password='".md5($password)."' WHERE id='$user_id'";
  358.       if ($this->query($sql))
  359.       {
  360.     if (!$old_password) $old_password=$_SESSION['GO_SESSION']['old_password'];
  361. #echo "changing passwd: $old_password,$password  <br>";
  362.     $GO_CRYPTO->re_encrypt_email($user_id,$old_password,$password);
  363.     if ($GO_CONFIG->enable_system_accounts)
  364.     {
  365.       if ($profile["samba_user"] == '1' && $GO_CONFIG->enable_samba_accounts)
  366.       {
  367.         exec($GO_CONFIG->sudo.' '.$GO_CONFIG->auto_smbpasswd.' "'.$profile["username"].'" "'.$password.'"');
  368.       }
  369.       exec("echo '".$profile["username"].":".$password."' | ".$GO_CONFIG->sudo." ".$GO_CONFIG->chpasswd);
  370.       $sql = "UPDATE emAccounts SET password='".$GO_CRYPTO->encrypt($password,$password)."' WHERE host='".$GO_CONFIG->local_email_host."' AND username='".smart_addslashes($profile["username"])."'";
  371.       $this->query($sql);
  372.     }
  373.     $_SESSION['GO_SESSION']['old_password']=$_SESSION['GO_SESSION']['password']=$password;
  374.     return true;
  375.       }
  376.  
  377.     }
  378.     return false;
  379.   }
  380.  
  381.   function update_authcode($authcode)
  382.   {
  383.     $sql = "UPDATE users SET authcode='$authcode' WHERE id='$this->user_id'";
  384.       if ($this->query($sql))
  385.       {
  386.     return true;
  387.       }else
  388.       {
  389.     return false;
  390.       }
  391.   }
  392.   
  393.  
  394.     function get_profile_by_username($username)
  395.     {
  396.       $uid = -1;
  397.       if ($this->ldap) {
  398.     $this->ldap->search("uid=$username", $this->ldap->PeopleDN);
  399.     if ( $this->ldap->num_entries() > 0 ) {
  400.       $this->ldap->next_entry();
  401.       $uid = $this->ldap->get_first_value( "uidNumber" );
  402.     }
  403.       }
  404.  
  405.       if ( $uid < 0 ) {
  406.     $sql = "SELECT * FROM users WHERE username='$username'";
  407.     $this->query($sql);
  408.     $this->next_record();
  409.     $uid = $this->f( 'id' );
  410.       }
  411.  
  412.       $profile = new profiles();
  413.       //MS: update $this->Record to so it won't break when function f() is called
  414.       // from the db class
  415.       $this->record = $profile->get_profile( $uid );
  416.       return $this->Record;
  417.     }
  418.  
  419.   function email_exists($email)
  420.   {
  421.     $sql = "SELECT id FROM users WHERE email='".smart_addslashes($email)."'";
  422.     $this->query($sql);
  423.     if ($this->num_rows() > 0)
  424.     {
  425.       return true;
  426.     } else if ($this->ldap)
  427.     {
  428.       $this->ldap->search("mail=$email", $this->ldap->PeopleDN);
  429.       if ($this->ldap->num_entries() > 0)
  430.       {
  431.     return true;
  432.       }
  433.     }
  434.     return false;
  435.   }
  436.  
  437.   function add_user($username, $password, $first_name, $middle_name, $last_name, $initials, $title, $sex, $birthday, $email, $authcode, $work_phone, $home_phone, $fax, $cellular, $country, $state, $city, $zip, $address, $company, $work_country, $work_state, $work_city, $work_zip, $work_address, $work_fax, $homepage, $department, $function, $language='', $theme='', $acl_id, $create_samba_user=false, $user_id=-1)
  438.   {
  439.     global $GO_CONFIG;
  440.     
  441.       if ($language == '')
  442.       {
  443.     $language=$GO_CONFIG->language;
  444.       }
  445.     
  446.       if ($theme == '')
  447.       {
  448.     $theme=$GO_CONFIG->theme;
  449.       }
  450.  
  451.     if ($create_samba_user)
  452.     {
  453.       $smb = '1';
  454.     }else
  455.     {
  456.       $smb = '0';
  457.     }
  458.     
  459.       if ($user_id < 0)
  460.       {
  461.     $user_id = $this->nextid("users");
  462.       }
  463.     if ($user_id > 0)
  464.     {
  465.       $sql = "INSERT INTO users (id, username, password, first_name, middle_name, last_name, initials, title, sex, birthday, email, authcode, work_phone, home_phone, fax, cellular, country, state, city, zip, address, company, work_country, work_state, work_city, work_zip, work_address, work_fax, homepage, department, function, acl_id, registration_time, samba_user, currency, max_rows_list, timezone, date_format, time_format, language, theme, first_weekday)";
  466.     $sql .= " VALUES ('$user_id', '".smart_addslashes($username)."','".md5($password)."', '".smart_addslashes($first_name)."', '".smart_addslashes($middle_name)."', '".smart_addslashes($last_name)."', '".smart_addslashes($initials)."', '".smart_addslashes($title)."', '$sex', '".smart_addslashes($birthday)."', '".smart_addslashes($email)."', '".smart_addslashes($authcode)."', '".smart_addslashes($work_phone)."', '".smart_addslashes($home_phone)."', '".smart_addslashes($fax)."', '".smart_addslashes($cellular)."', '".smart_addslashes($country)."','".smart_addslashes($state)."', '".smart_addslashes($city)."', '".smart_addslashes($zip)."', '".smart_addslashes($address)."', '".smart_addslashes($company)."', '".smart_addslashes($work_country)."', '".smart_addslashes($work_state)."', '".smart_addslashes($work_city)."', '".smart_addslashes($work_zip)."', '".smart_addslashes($work_address)."', '".smart_addslashes($work_fax)."', '".smart_addslashes($homepage)."', '".smart_addslashes($department)."', '".smart_addslashes($function)."', '$acl_id', '".get_gmt_time()."','$smb', 'Ç', '15', '$GO_CONFIG->timezone_offset', '".$GO_CONFIG->date_formats[0]."', '".$GO_CONFIG->time_formats[0]."', '$language', '$theme', '".$GO_CONFIG->first_weekday."')";
  467.     if ($this->query($sql))
  468.     {
  469.       if ($GO_CONFIG->enable_system_accounts)
  470.       {
  471.         system($GO_CONFIG->sudo." ".$GO_CONFIG->useradd." \"".$username."\" -s ".$GO_CONFIG->shell." -p ".crypt($password,substr($password,0,2)));
  472.           if ($GO_CONFIG->enable_samba_accounts && $create_samba_user)
  473.           {
  474.         exec($GO_CONFIG->sudo.' '.$GO_CONFIG->auto_smbadduser.' "'.$username.'" "'.$password.'"');
  475.           }
  476.       }
  477.       
  478.         return $user_id;
  479.     }else
  480.     {
  481.       return -1;
  482.     }
  483.     }
  484.   }
  485.   
  486.     function max_users_reached()
  487.     {
  488.       global $GO_CONFIG;
  489.     
  490.     if($this->get_users() < $GO_CONFIG->max_users || $GO_CONFIG->max_users == 0)
  491.     {
  492.       return false;
  493.     }else
  494.     {
  495.       return true;
  496.     }
  497.     }
  498.   
  499.     function set_preferences($user_id, $date_format, $time_format, $thousands_seperator, $decimal_seperator, $currency, $mail_client, $max_rows_list, $timezone_offset, $start_module, $language, $theme, $first_weekday)
  500.     {
  501.       if($this->query("UPDATE users SET time_format='".smart_addslashes($time_format)."', ".
  502.         "date_format='".smart_addslashes($date_format)."', ".
  503.         "thousands_seperator='".smart_addslashes($thousands_seperator)."', ".
  504.         "decimal_seperator='".smart_addslashes($decimal_seperator)."', ".
  505.         "currency='".smart_addslashes($currency)."', ".
  506.         "mail_client='$mail_client', max_rows_list='$max_rows_list', ".
  507.         "timezone='$timezone_offset', ".
  508.         "start_module='$start_module', ".
  509.         "theme='$theme', ".
  510.         "language='$language', ".
  511.         "first_weekday='$first_weekday' ".
  512.         
  513.         "WHERE id='$user_id'"))
  514.       {
  515.     $_SESSION['GO_SESSION']['thousands_seperator'] = $thousands_seperator;
  516.       $_SESSION['GO_SESSION']['decimal_seperator'] = $decimal_seperator;
  517.       $_SESSION['GO_SESSION']['date_format']= $date_format;
  518.     $_SESSION['GO_SESSION']['time_format']= $time_format;
  519.       $_SESSION['GO_SESSION']['currency'] = $currency;
  520.       $_SESSION['GO_SESSION']['mail_client'] = $mail_client;
  521.       $_SESSION['GO_SESSION']['max_rows_list'] = $max_rows_list;
  522.     $_SESSION['GO_SESSION']['timezone'] = $timezone_offset;
  523.       $_SESSION['GO_SESSION']['start_module'] = $start_module;
  524.       $_SESSION['GO_SESSION']['theme'] = $theme;
  525.       $_SESSION['GO_SESSION']['language'] = $language;
  526.       $_SESSION['GO_SESSION']['first_weekday'] = $first_weekday;
  527.       }
  528.     }
  529.   
  530.     function delete_user($user_id)
  531.     {
  532.       global $GO_CONFIG,$GO_SECURITY, $GO_MODULES;
  533.     
  534.     if($user = $this->get_user($user_id))
  535.     {
  536.       $acl_id = $this->f("acl_id");
  537.         $username = $this->f("username");
  538.         if ($this->f("samba_user") == '1')
  539.         {
  540.           $samba_user = true;
  541.         }else
  542.         {
  543.           $samba_user = false;
  544.         }
  545.         
  546.           $sql = "DELETE FROM users WHERE id='$user_id'";
  547.           if ($this->query($sql))
  548.           {
  549.         if ($GO_CONFIG->enable_system_accounts)
  550.         {
  551.           system($GO_CONFIG->sudo." ".$GO_CONFIG->userdel." -r \"".$username."\"");
  552.             if ($samba_user)
  553.             {
  554.               exec($GO_CONFIG->sudo." ".$GO_CONFIG->smbdeluser." \"".$username."\"");
  555.             }
  556.         }
  557.         
  558.           $GO_SECURITY->delete_acl($acl_id);
  559.           $GO_SECURITY->delete_user($acl_id);
  560.           
  561.           if ($GO_MODULES->get_module('email'))
  562.           {
  563.             require_once($GO_CONFIG->class_path."email.class.inc");
  564.               $email = new email();
  565.               $email->delete_user($user_id);
  566.           }
  567.         
  568.           if ($GO_MODULES->get_module('addressbook'))
  569.           {
  570.             require_once($GO_CONFIG->class_path."addressbook.class.inc");
  571.               $ab = new addressbook();
  572.               $ab->delete_user($user_id);
  573.           }
  574.         
  575.           if ($GO_MODULES->get_module('scheduler'))
  576.           {
  577.             require_once($GO_CONFIG->class_path."scheduler.class.inc");
  578.               $scheduler = new scheduler();
  579.               $scheduler->delete_user($user_id);
  580.           }
  581.         
  582.           if ($GO_MODULES->get_module('calendar'))
  583.           {
  584.             require_once($GO_CONFIG->class_path."calendar.class.inc");
  585.               $calendar = new calendar();
  586.               $calendar->delete_user($user_id);
  587.           }
  588.         
  589.           if ($GO_MODULES->get_module('filesystem'))
  590.           {
  591.             require_once($GO_CONFIG->class_path."filesystem.class.inc");
  592.               $filesystem = new filesystem();
  593.               $filesystem->delete_user($user_id);
  594.           }
  595.         
  596.           if ($GO_MODULES->get_module('projects'))
  597.           {
  598.             require_once($GO_CONFIG->class_path."projects.class.inc");
  599.               $projects = new projects();
  600.               $projects->delete_user($user_id);
  601.           }
  602.         
  603.           if ($GO_MODULES->get_module('cms'))
  604.           {
  605.             require_once($GO_CONFIG->class_path."cms.class.inc");
  606.               $cms = new cms();
  607.               $cms->delete_user($user_id);
  608.           }
  609.         
  610.           if ($GO_MODULES->get_module('notes'))
  611.           {
  612.             require_once($GO_CONFIG->class_path."notes.class.inc");
  613.               $notes = new notes();
  614.               $notes->delete_user($user_id);
  615.           }
  616.         
  617.           require_once($GO_CONFIG->class_path."bookmarks.class.inc");
  618.           $bookmarks = new bookmarks();
  619.           $bookmarks->delete_user($user_id);
  620.           
  621.           require_once($GO_CONFIG->class_path."groups.class.inc");
  622.           $groups = new groups();
  623.           $groups->delete_user($user_id);
  624.           
  625.           $sql = "SELECT * FROM acl_items WHERE user_id='$user_id'";
  626.           $this->query($sql);
  627.           while($this->next_record())
  628.           {
  629.             $GO_SECURITY->delete_acl($this->f('id'));
  630.           }
  631.         
  632.           system('rm -Rf '.$GO_CONFIG->file_storage_path.$username);
  633.           return true;
  634.           }
  635.     }
  636.       return false;
  637.     }
  638.   
  639.     function random_password($characters_allow = 'a-z,1-9',$characters_disallow = 'i,o',$password_length = 8,$repeat = 0)
  640.     {
  641.       // Generate array of allowable characters.
  642.       
  643.     $characters_allow = explode( ',', $characters_allow );
  644.     
  645.     for ( $i = 0; $i < count( $characters_allow ); $i++ )
  646.     {
  647.       if ( substr_count( $characters_allow[$i], '-' ) > 0 )
  648.       {
  649.         $character_range = explode( '-', $characters_allow[$i] );
  650.           
  651.           for ($j=ord($character_range[0]);$j <= ord( $character_range[1] ); $j++)
  652.           {
  653.         $array_allow[] = chr( $j );
  654.           }
  655.       }else
  656.       {
  657.         $array_allow[] = $characters_allow[$i];
  658.       }
  659.     }
  660.       
  661.     // Generate array of disallowed characters.
  662.     
  663.     $characters_disallow = explode( ',', $characters_disallow );
  664.     
  665.     for ( $i = 0; $i < count( $characters_disallow ); $i++ )
  666.     {
  667.       if ( substr_count( $characters_disallow[$i], '-' ) > 0 )
  668.       {
  669.         $character_range = explode( '-', $characters_disallow[$i] );
  670.           
  671.           for ( $j = ord( $character_range[0] );
  672.           $j <= ord( $character_range[1] ); $j++ )
  673.           {
  674.         $array_disallow[] = chr( $j );
  675.           }
  676.       }else
  677.       {
  678.         $array_disallow[] = $characters_disallow[$i];
  679.       }
  680.     }
  681.       
  682.     mt_srand( ( double ) microtime() * 1000000 );
  683.     
  684.     // Generate array of allowed characters by removing disallowed
  685.     // characters from array.
  686.     $array_allow = array_diff( $array_allow, $array_disallow );
  687.     
  688.     // Resets the keys since they won't be consecutive after
  689.     // removing the disallowed characters.
  690.     reset( $array_allow );
  691.     $new_key = 0;
  692.     while( list( $key, $val ) = each( $array_allow ) )
  693.     {
  694.       $array_allow_tmp[$new_key] = $val;
  695.         $new_key++;
  696.     }
  697.       
  698.     $array_allow = $array_allow_tmp;
  699.     $password = '';
  700.     while ( strlen( $password ) < $password_length )
  701.     {
  702.       $character = mt_rand( 0, count( $array_allow ) - 1 );
  703.         
  704.         // If characters are not allowed to repeat,
  705.         // only add character if not found in partial password string.
  706.         if ( $repeat == 0 )
  707.         {
  708.           if (substr_count($password, $array_allow[$character])== 0)
  709.           {
  710.         $password .= $array_allow[$character];
  711.           }
  712.         }else
  713.         {
  714.           $password .= $array_allow[$character];
  715.         }
  716.     }
  717.       
  718.     return $password;
  719.     }
  720. }
  721. ?>
  722.