function setup_account_object()
 {
     if (!is_object($GLOBALS['phpgw']->accounts)) {
         if (!is_object($this->db)) {
             $this->loaddb();
         }
         /* Load up some configured values */
         $this->db->query("SELECT config_name,config_value FROM phpgw_config " . "WHERE config_name LIKE 'ldap%' OR config_name LIKE 'account_%' OR config_name LIKE '%encryption%'", __LINE__, __FILE__);
         while ($this->db->next_record()) {
             $GLOBALS['phpgw_info']['server'][$this->db->f('config_name')] = $this->db->f('config_value');
         }
         if (!is_object($GLOBALS['phpgw'])) {
             $GLOBALS['phpgw'] = CreateObject('phpgwapi.phpgw');
         }
         copyobj($this->db, $GLOBALS['phpgw']->db);
         $GLOBALS['phpgw']->common = CreateObject('phpgwapi.common');
         $GLOBALS['phpgw']->accounts = CreateObject('phpgwapi.accounts');
         if ($GLOBALS['phpgw_info']['server']['account_repository'] == 'ldap' && !$GLOBALS['phpgw']->accounts->ds) {
             printf("<b>Error: Error connecting to LDAP server %s!</b><br>", $GLOBALS['phpgw_info']['server']['ldap_host']);
             exit;
         }
     }
 }
 function read($start = 0, $limit = 0, $fields = '', $query = '', $filter = '', $sort = '', $order = '', $lastmod = -1, $cquery = '')
 {
     if (!$start) {
         $start = 0;
     }
     if (!$limit) {
         $limit = 0;
     }
     if (!$filter) {
         $filter = 'tid=n';
     }
     if (!$fields || empty($fields)) {
         $fields = $this->stock_contact_fields;
     }
     $DEBUG = 0;
     list($stock_fields, $stock_fieldnames, $extra_fields) = $this->split_stock_and_extras($fields);
     if (count($stock_fieldnames)) {
         $t_fields = ',' . implode(',', $stock_fieldnames);
         if ($t_fields == ',') {
             unset($t_fields);
         }
     }
     /* turn filter's a=b,c=d OR a=b into an array */
     if ($filter) {
         $check_stock = $this->stock_contact_fields + $this->non_contact_fields;
         if ($DEBUG) {
             echo 'DEBUG - Inbound filter is: #' . $filter . '#';
         }
         $filterlist = array();
         foreach (explode(',', $filter) as $pair) {
             list($name, $value) = explode('=', $pair, 2);
             if (!$name || !isset($check_stock[$name])) {
                 continue;
             }
             if ($DEBUG) {
                 echo '<br>DEBUG - Filter intermediate strings 2: #' . $name . '# => #' . $value . '#';
             }
             if (empty($value)) {
                 if ($DEBUG) {
                     echo '<br>DEBUG - filter field "' . $name . '" is empty (NULL)';
                 }
                 $filterlist[] = $name . ' is NULL';
             } else {
                 if ($name == 'cat_id') {
                     if (!(int) $value) {
                         continue;
                     }
                     // nothing to filter
                     //$filterlist[] = "(" . $name . " LIKE '%," . (int)$value . ",%' OR " . $name."='".(int)$value."')";
                     if (!is_object($GLOBALS['phpgw']->categories)) {
                         $GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories');
                     }
                     $cats = $GLOBALS['phpgw']->categories->return_all_children((int) $value);
                     $cat_filter = '(cat_id IN (' . implode(',', $cats) . ')';
                     foreach ($cats as $cat) {
                         $cat_filter .= " OR cat_id LIKE '%,{$cat},%'";
                     }
                     $cat_filter .= ')';
                     $filterlist[] = $cat_filter;
                 } elseif (@is_int($value)) {
                     $filterlist[] = $name . '=' . $value;
                 } elseif ($value == "!''") {
                     $filterlist[] = $name . "!=''";
                 } else {
                     $filterlist[] = $name . "='" . $this->db->db_addslashes($value) . "'";
                 }
             }
         }
         $filterlist = implode(' AND ', $filterlist);
         if ($DEBUG) {
             echo '<br>DEBUG - Filter output string: #' . $filterlist . '#';
         }
         if ($filterlist) {
             $filtermethod = '(' . $filterlist . ') ';
             $fwhere = ' WHERE ';
             $fand = ' AND ';
         }
     } else {
         $filtermethod = " AND (tid='n' OR tid is null)";
     }
     if (!$filtermethod) {
         if ($this->account_id) {
             $fwhere .= ' (owner=' . $this->account_id;
             $fand .= ' (owner=' . $this->account_id;
         }
     } else {
         if ($this->account_id) {
             $fwhere .= $filtermethod . ' AND (owner=' . $this->account_id;
             $fand .= $filtermethod . ' AND (owner=' . $this->account_id;
         } else {
             $filtermethod = substr($filtermethod, 0, -2);
             $fwhere .= $filtermethod;
             $fand .= $filtermethod;
         }
     }
     if (@is_array($this->grants)) {
         $grants = $this->grants;
         foreach ($grants as $user => $_right) {
             $public_user_list[] = $user;
         }
         $fwhere .= " OR (access='public' AND owner in(" . implode(',', $public_user_list) . "))) ";
         $fand .= " OR (access='public' AND owner in(" . implode(',', $public_user_list) . "))) ";
     } else {
         $fwhere .= ') ';
         $fand .= ') ';
     }
     if ($DEBUG && $filtermethod) {
         echo '<br>DEBUG - Filtering with: #' . $filtermethod . '#';
     }
     if (!$sort) {
         $sort = 'ASC';
     }
     if (!empty($order) && preg_match('/^[a-zA-Z_0-9, ]+$/', $order) && (empty($sort) || preg_match('/^(DESC|ASC|desc|asc)$/', $sort))) {
         $ordermethod = "ORDER BY {$order} {$sort} ";
     } else {
         $ordermethod = "ORDER BY n_family,n_given,email ASC";
     }
     if ($DEBUG && $ordermethod) {
         echo "<br>DEBUG - {$ordermethod}";
     }
     if ($lastmod >= 0 && $fwhere) {
         $fwhere .= " AND last_mod > " . (int) $lastmod . ' ';
     } elseif ($lastmod >= 0) {
         $fwhere = " WHERE last_mod > " . (int) $lastmod . ' ';
     }
     if ($DEBUG && $last_mod_filter && $fwhere) {
         echo "<br>DEBUG - last_mod_filter added to fwhere: {$fwhere}";
     }
     $filtermethod = '';
     if ($cquery) {
         $sql = 'SELECT * FROM ' . $this->std_table . ' WHERE (';
         $sqlcount = 'SELECT COUNT(id) FROM ' . $this->std_table . ' WHERE (';
         foreach (array('fn' => 'cn', 'n_family' => 'sn', 'org_name' => 'o') as $f => $x) {
             $cquery = strtoupper($this->db->db_addslashes($cquery));
             $sql .= " UPPER({$f}) LIKE '{$cquery}%' OR ";
             $sqlcount .= " UPPER({$f}) LIKE '{$cquery}%' OR ";
         }
         $sql = substr($sql, 0, -3) . ') ' . $fand . $filtermethod . $ordermethod;
         $sqlcount = substr($sqlcount, 0, -3) . ') ' . $fand . $filtermethod;
         unset($f);
         unset($x);
     } elseif ($query) {
         if (is_array($query)) {
             $sql = "SELECT * FROM {$this->std_table} WHERE (";
             $sqlcount = "SELECT COUNT(id) FROM {$this->std_table} WHERE (";
             foreach ($query as $queryKey => $queryValue) {
                 if (!preg_match('/^[a-zA-Z0-9_]+$/', $queryKey)) {
                     continue;
                     // this can be something nasty
                 }
                 // special handling of text columns for certain db's;
                 if (in_array($f, array('note', 'pubkey', 'label'))) {
                     switch ($this->db->Type) {
                         case 'mssql':
                             $queryKey = "CAST({$queryKey} AS varchar)";
                             // mssql cant use UPPER on text columns
                             break;
                     }
                 }
                 $queryValue = strtoupper($this->db->db_addslashes($queryValue));
                 $sql .= " UPPER({$queryKey}) LIKE '{$queryValue}' AND ";
                 $sqlcount .= " UPPER({$queryKey}) LIKE '{$queryValue}' AND ";
             }
             $sql = substr($sql, 0, -5) . ') ' . $fand . $filtermethod . $ordermethod;
             $sqlcount = substr($sqlcount, 0, -5) . ') ' . $fand . $filtermethod;
             unset($queryKey);
             unset($queryValue);
         } else {
             $query = strtoupper($this->db->db_addslashes($query));
             $sql = "SELECT * FROM {$this->std_table} WHERE (";
             $sqlcount = "SELECT COUNT(id) FROM {$this->std_table} WHERE (";
             foreach ($this->stock_contact_fields as $f => $x) {
                 // special handling of text columns for certain db's;
                 if (in_array($f, array('note', 'pubkey', 'label'))) {
                     switch ($this->db->Type) {
                         case 'mssql':
                             $f = "CAST({$f} AS varchar)";
                             // mssql cant use UPPER on text columns
                             break;
                     }
                 }
                 $sql .= " UPPER({$f}) LIKE '%{$query}%' OR ";
                 $sqlcount .= " UPPER({$f}) LIKE '%{$query}%' OR ";
             }
             $sql = substr($sql, 0, -3) . ') ' . $fand . $filtermethod . $ordermethod;
             $sqlcount = substr($sqlcount, 0, -3) . ') ' . $fand . $filtermethod;
             unset($f);
             unset($x);
         }
     } else {
         $sql = "SELECT id,lid,tid,owner,access,cat_id,last_mod {$t_fields} FROM {$this->std_table} " . $fwhere . $filtermethod . ' ' . $ordermethod;
         $sqlcount = "SELECT COUNT(id) FROM {$this->std_table} " . $fwhere . $filtermethod;
     }
     if ($DEBUG) {
         echo '<br>COUNT QUERY' . $sqlcount;
         echo '<br>FULL  QUERY' . $sql;
     }
     //			$db2 = $this->db;
     copyobj($this->db, $db2);
     /* Perhaps it is more efficient to count records for this query, which is all we need here */
     $this->db->query($sqlcount, __LINE__, __FILE__);
     $this->db->next_record();
     unset($sqlcount);
     $this->total_records = $this->db->f(0);
     if ($start && $limit) {
         if ($this->total_records <= $limit) {
             $this->db->query($sql, __LINE__, __FILE__);
         } else {
             $this->db->limit_query($sql, $start, __LINE__, __FILE__, $limit);
         }
     } elseif (!$limit) {
         $this->db->query($sql, __LINE__, __FILE__);
     } else {
         $this->db->limit_query($sql, $start, __LINE__, __FILE__);
     }
     $i = 0;
     while ($this->db->next_record()) {
         $return_fields[$i]['id'] = $this->db->f('id');
         $return_fields[$i]['lid'] = $this->db->f('lid');
         $return_fields[$i]['tid'] = $this->db->f('tid');
         $return_fields[$i]['owner'] = $this->db->f('owner');
         $return_fields[$i]['access'] = $this->db->f('access');
         $return_fields[$i]['cat_id'] = $this->db->f('cat_id');
         $return_fields[$i]['last_mod'] = $this->db->f('last_mod');
         $return_fields[$i]['rights'] = (int) $this->grants[$this->db->f('owner')];
         if (@is_array($stock_fieldnames)) {
             foreach ($stock_fieldnames as $f_name) {
                 $return_fields[$i][$f_name] = $this->db->f($f_name);
             }
             reset($stock_fieldnames);
         }
         $db2->query("SELECT contact_name,contact_value FROM {$this->ext_table} WHERE contact_id=" . (int) $this->db->f('id') . $filterextra, __LINE__, __FILE__);
         while ($db2->next_record()) {
             if ($extra_fields[$db2->f('contact_name')]) {
                 $return_fields[$i][$db2->f('contact_name')] = $db2->f('contact_value');
             }
         }
         $i++;
     }
     return $return_fields;
 }
 function sonews()
 {
     copyobj($GLOBALS['phpgw']->db, $this->db);
 }
             }
         }
     }
 }
 if ($users) {
     while (list($key, $accountid) = each($users)) {
         $id_exist = 0;
         $acct_exist = 0;
         $thisacctid = $account_info[$accountid]['account_id'];
         $thisacctlid = $account_info[$accountid]['account_lid'];
         $thisfirstname = $account_info[$accountid]['account_firstname'];
         $thislastname = $account_info[$accountid]['account_lastname'];
         // Do some checks before we try to import the data.
         if (!empty($thisacctid) && !empty($thisacctlid)) {
             $accounts = CreateObject('phpgwapi.accounts', (int) $thisacctid);
             copyobj($GLOBALS['phpgw_setup']->db, $accounts->db);
             // Check if the account is already there.
             // If so, we won't try to create it again.
             $acct_exist = $acct->name2id($thisacctlid);
             if ($acct_exist) {
                 $thisacctid = $acct_exist;
             }
             $id_exist = $accounts->exists((int) $thisacctid);
             // If not, create it now.
             if (!$id_exist) {
                 echo '<br>Adding' . $thisacctid;
                 $thisaccount_info = array('account_type' => 'u', 'account_id' => $thisacctid, 'account_lid' => $thisacctlid, 'account_passwd' => 'x', 'account_firstname' => $thisfirstname, 'account_lastname' => $thislastname, 'account_status' => 'A', 'account_expires' => -1, 'homedirectory' => $config['ldap_account_home'] . '/' . $thisacctlid, 'loginshell' => $config['ldap_account_shell']);
                 $accounts->create($thisaccount_info);
             }
         }
     }
 function auth_()
 {
     copyobj($GLOBALS['phpgw']->db, $this->db);
 }
            // Check if the group account is already there.
            // If so, set our group_id to that account's id for use below.
            $acct_exist = $groups->name2id('Default');
            if ($acct_exist) {
                $defaultgroupid = $acct_exist;
            }
            $id_exist = $groups->exists((int) $defaultgroupid);
            // if not, create it, using our original groupid.
            if ($id_exist) {
                $groups->delete($defaultgroupid);
            }
            $thisgroup_info = array('account_type' => 'g', 'account_lid' => 'Default', 'account_passwd' => $passwd, 'account_firstname' => 'Default', 'account_lastname' => 'Group', 'account_status' => 'A', 'account_expires' => -1);
            $acct->create($thisgroup_info);
            $defaultgroupid = $acct->name2id('Default');
            $acl = CreateObject('phpgwapi.acl', $defaultgroupid);
            copyobj($GLOBALS['phpgw_setup']->db, $acl->db);
            $acl->account_id = (int) $defaultgroupid;
            $acl->read_repository();
            @reset($s_apps);
            while (list($key, $app) = each($s_apps)) {
                $acl->delete($app, 'run', 1);
                $acl->add($app, 'run', 1);
            }
            $acl->save_repository();
        }
        //end default group creation
    }
    $setup_complete = True;
}
$GLOBALS['phpgw_setup']->html->show_header(lang('LDAP Import'), False, 'config', $GLOBALS['phpgw_setup']->ConfigDomain . '(' . $phpgw_domain[$GLOBALS['phpgw_setup']->ConfigDomain]['db_type'] . ')');
if ($error) {