Changeset 522 in subversion


Ignore:
Timestamp:
Mar 27, 2007 10:06:49 AM (6 years ago)
Author:
thomasb
Message:

devel-addressbook: first implementation of the new LDAP address book; made JS work with Firebug

Location:
branches/devel-addressbook/program
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/devel-addressbook/program/include/rcube_ldap.inc

    r499 r522  
    2424  var $conn; 
    2525  var $prop = array(); 
    26    
    27   var $filter = '1'; 
     26  var $fieldmap = array(); 
     27   
     28  var $filter = ''; 
     29  var $idmap = array(); 
    2830  var $result = null; 
    29   var $search_fields; 
    30   var $search_string; 
     31  var $ldap_result = null; 
     32  var $sort_col = ''; 
    3133   
    3234  /** public properties */ 
     
    4749  { 
    4850    $this->prop = $p; 
     51     
     52    foreach ($p as $prop => $value) 
     53      if (preg_match('/^(.+)_field$/', $prop, $matches)) 
     54        $this->fieldmap[$matches[1]] = $value; 
     55     
     56    // $this->filter = "(dn=*)"; 
    4957    $this->connect(); 
    5058  } 
     
    5563   * @see  rcube_ldap::__construct 
    5664   */ 
    57   function rcube_contacts($p) 
     65  function rcube_ldap($p) 
    5866  { 
    5967    $this->__construct($p); 
     
    6775  { 
    6876    if (!function_exists('ldap_connect')) 
    69       raise_error(array('type' => "ldap", 
    70                         'message' => "No ldap support in this installation of PHP"), 
    71                   true); 
     77      raise_error(array('type' => 'ldap', 'message' => "No ldap support in this installation of PHP"), true); 
    7278 
    7379    if (is_resource($this->conn)) 
     
    8187      if ($lc = @ldap_connect($host, $this->prop['port'])) 
    8288      { 
    83         @ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['port']); 
     89        ldap_set_option($lc, LDAP_OPT_PROTOCOL_VERSION, $this->prop['port']); 
    8490        $this->prop['host'] = $host; 
    8591        $this->conn = $lc; 
     
    9197      $this->ready = true; 
    9298    else 
    93       raise_error(array('type' => "ldap", 
    94                         'message' => "Could not connect to any LDAP server, tried $host:{$this->prop[port]} last"), 
    95                   true); 
    96   } 
     99      raise_error(array('type' => 'ldap', 'message' => "Could not connect to any LDAP server, tried $host:{$this->prop[port]} last"), true); 
     100  } 
     101 
     102 
     103  /** 
     104   * Merge with connect()? 
     105   */ 
     106  function bind($dn=null, $pass=null) 
     107  { 
     108    if ($this->conn) 
     109    { 
     110      if ($dn) 
     111      { 
     112        if (@ldap_bind($this->conn, $dn, $pass)) 
     113          return true; 
     114        else 
     115          raise_error(array('code' => ldap_errno($this->conn), 
     116                            'type' => 'ldap', 
     117                            'message' => "Bind failed for dn=$dn: ".ldap_error($this->conn)), 
     118                      true); 
     119      } 
     120      else 
     121      { 
     122        if (@ldap_bind($this->conn)) 
     123          return true; 
     124        else 
     125          raise_error(array('code' => ldap_errno($this->conn), 
     126                            'type' => 'ldap', 
     127                            'message' => "Anonymous bind failed: ".ldap_error($this->conn)), 
     128                      true); 
     129        } 
     130    } 
     131    else 
     132      raise_error(array('type' => 'ldap', 'message' => "Attempted bind on nonexistent connection"), true); 
     133       
     134    return false; 
     135    } 
    97136 
    98137 
     
    136175   * @param string ?? 
    137176   */ 
    138   function set_search_set($filter) 
    139   { 
    140     // TODO 
     177  function set_search_set($p) 
     178  { 
     179    $this->filter = $p['filter']; 
     180    $this->idmap = $p['ids']; 
    141181  } 
    142182   
     
    149189  function get_search_set() 
    150190  { 
    151     // TODO 
     191    return array('filter' => $this->filter, 'ids' => $this->idmap); 
    152192  } 
    153193 
     
    158198  function reset() 
    159199  { 
    160     // TODO 
    161200    $this->result = null; 
    162     $this->filter = '1'; 
    163     $this->search_fields = null; 
    164     $this->search_string = null; 
     201    $this->ldap_result = null; 
     202    $this->filter = ''; 
     203    $this->idmap = array(); 
    165204  } 
    166205   
     
    174213  function list_records($cols=null, $subset=0) 
    175214  { 
     215    // exec LDAP search if no result resource is stored 
     216    if ($this->conn && !$this->ldap_result) 
     217      $this->_exec_search(); 
     218     
    176219    // count contacts for this user 
    177220    $this->result = $this->count(); 
    178  
    179     // TODO 
     221     
     222    // we have a search result resource 
     223    if ($this->ldap_result && $this->result->count > 0) 
     224    { 
     225      if ($this->sort_col && $this->prop['scope'] !== "base") 
     226        @ldap_sort($this->conn, $this->ldap_result, $this->sort_col); 
     227         
     228      $entries = ldap_get_entries($this->conn, $this->ldap_result); 
     229      for ($i = $this->result->first; $i < min($entries['count'], $this->result->first + $this->page_size); $i++) 
     230        $this->result->add($this->_ldap2result($entries[$i])); 
     231    } 
    180232 
    181233    return $this->result; 
     
    193245  function search($fields, $value, $select=true) 
    194246  { 
    195     // TODO 
    196      
    197     //$this->set_search_set(); 
     247    // special treatment for ID-based search 
     248    if ($fields == 'ID' || $fields == $this->primary_key) 
     249    { 
     250      $ids = explode(',', $value); 
     251      $this->result = new rcube_result_set(); 
     252      foreach ($ids as $id) 
     253        if ($rec = $this->get_record($id, true)) 
     254        { 
     255          $this->result->add($rec); 
     256          $this->result->count++; 
     257        } 
     258       
     259      return $this->result; 
     260    } 
     261     
     262    $filter = '(|'; 
     263    foreach ((array)$fields as $field) 
     264      if ($f = $this->_map_field($field)) 
     265        $filter .= "($f=*" . rcube_ldap::quote_string($value) . "*)"; 
     266    $filter .= ')'; 
     267 
     268    // set filter string and execute search 
     269    $this->filter = $filter; 
     270    $this->_exec_search(); 
     271     
    198272    if ($select) 
    199273      $this->list_records(); 
     
    213287  { 
    214288    $count = 0; 
    215     // TODO 
    216     return new rcube_result_set($count, ($this->list_page-1) * $this->page_size);; 
     289    if ($this->conn && $this->ldap_result) 
     290      $count = ldap_count_entries($this->conn, $this->ldap_result); 
     291 
     292    return new rcube_result_set($count, ($this->list_page-1) * $this->page_size); 
    217293  } 
    218294 
     
    237313  function get_record($id, $assoc=false) 
    238314  { 
    239     // TODO 
    240  
    241     return $assoc && $sql_arr ? $sql_arr : $this->result; 
     315    $res = null; 
     316    if ($this->conn && ($dn = $this->idmap[$id])) 
     317    { 
     318      $this->ldap_result = @ldap_read($this->conn, $dn, "(objectclass=*)", array_values($this->fieldmap)); 
     319      $entry = @ldap_first_entry($this->conn, $this->ldap_result); 
     320       
     321      if ($entry && ($rec = ldap_get_attributes($this->conn, $entry))) 
     322      { 
     323        $res = $this->_ldap2result($rec); 
     324        $this->result = new rcube_result_set(1); 
     325        $this->result->add($res); 
     326      } 
     327    } 
     328 
     329    return $assoc && $res ? $res : $this->result; 
    242330  } 
    243331   
     
    281369  } 
    282370 
     371 
     372  /** 
     373   * Execute the LDAP search based on the stored credentials 
     374   * 
     375   * @private 
     376   */ 
     377  function _exec_search() 
     378  { 
     379    if ($this->conn && $this->filter) 
     380    { 
     381      $limit = $this->page_size * $this->list_page; 
     382      $function = $this->prop['scope'] == 'sub' ? 'ldap_search' : ($this->prop['scope'] == 'base' ? 'ldap_read' : 'ldap_list'); 
     383 
     384      $this->ldap_result = @$function($this->conn, $this->prop['base_dn'], $this->filter, array_values($this->fieldmap), 0, $limit); 
     385      return true; 
     386    } 
     387    else 
     388      return false; 
     389  } 
     390   
     391   
     392  /** 
     393   * @private 
     394   */ 
     395  function _ldap2result($rec) 
     396  { 
     397    $out = array(); 
     398    $id = count($this->idmap) + 1; 
     399     
     400    if ($rec['dn']) 
     401    { 
     402      $dn_hash = $id++; // md5($rec['dn']); 
     403      $out[$this->primary_key] = $dn_hash; 
     404      $this->idmap[$dn_hash] = $rec['dn']; 
     405    } 
     406     
     407    foreach ($this->fieldmap as $rf => $lf) 
     408    { 
     409      if ($rec[$lf]['count']) 
     410      { 
     411        unset($rec[$lf]['count']); 
     412        $out[$rf] = join(' ', $rec[$lf]); 
     413      } 
     414    } 
     415     
     416    return $out; 
     417  } 
     418   
     419   
     420  /** 
     421   * @private 
     422   */ 
     423  function _map_field($field) 
     424  { 
     425    return $this->fieldmap[$field]; 
     426  } 
     427   
     428   
     429  /** 
     430   * @static 
     431   */ 
     432  function quote_string($str) 
     433  { 
     434    return strtr($str, array('*'=>'\2a', '('=>'\28', ')'=>'\29', '\\'=>'\5c')); 
     435  } 
     436 
     437 
    283438} 
    284439 
  • branches/devel-addressbook/program/js/app.js

    r499 r522  
    12391239      } 
    12401240     
     1241    this.select_folder(mbox, this.env.mailbox); 
     1242    this.env.mailbox = mbox; 
     1243 
     1244    // load message list remotely 
     1245    if (this.gui_objects.messagelist) 
     1246      { 
     1247      this.list_mailbox_remote(mbox, page, add_url); 
     1248      return; 
     1249      } 
     1250     
     1251    if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) 
     1252      { 
     1253      target = window.frames[this.env.contentframe]; 
     1254      add_url += '&_framed=1'; 
     1255      } 
     1256 
    12411257    // also send search request to get the right messages 
    12421258    if (this.env.search_request) 
    12431259      add_url += '&_search='+this.env.search_request; 
    1244        
    1245     this.select_folder(mbox, this.env.mailbox); 
    1246     this.env.mailbox = mbox; 
    1247  
    1248     // load message list remotely 
    1249     if (this.gui_objects.messagelist) 
    1250       { 
    1251       this.list_mailbox_remote(mbox, page, add_url); 
    1252       return; 
    1253       } 
    1254      
    1255     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) 
    1256       { 
    1257       target = window.frames[this.env.contentframe]; 
    1258       add_url += '&_framed=1'; 
    1259       } 
    12601260 
    12611261    // load message list to target frame/window 
     
    14201420      this.message_list.select_next(); 
    14211421      } 
    1422  
    1423     // also send search request to get the right messages 
    1424     if (this.env.search_request) 
    1425       add_url += '&_search='+this.env.search_request; 
    14261422 
    14271423    // send request to server 
     
    22552251    this.env.source = src; 
    22562252 
    2257     // also send search request to get the correct listing 
    2258     if (this.env.search_request) 
    2259       url += '&_search='+this.env.search_request; 
    2260  
    22612253    this.set_busy(true, 'loading'); 
    22622254    this.http_request('list', url, true); 
     
    22772269    else if (framed) 
    22782270      return false; 
     2271       
     2272    if (this.env.search_request) 
     2273      add_url += '&_search='+this.env.search_request; 
    22792274 
    22802275    if (action && (cid || action=='add') && !this.drag_active) 
     
    31673162      querystring += '&_ts='+(new Date().getTime()); 
    31683163 
     3164    // also send search request to get the right messages 
     3165    if (this.env.search_request) 
     3166      querystring += '&_search='+this.env.search_request; 
     3167 
    31693168    // send request 
    31703169    if (request_obj) 
    31713170      { 
    3172       console('HTTP request: '+this.env.comm_path+'&_action='+action+'&'+querystring); 
     3171      console.log('HTTP request: '+this.env.comm_path+'&_action='+action+'&'+querystring); 
    31733172 
    31743173      if (lock) 
     
    31933192        postdata += (postdata ? '&' : '') + '_remote=1'; 
    31943193 
     3194      if (this.env.search_request) 
     3195        postdata += '&_search='+this.env.search_request; 
     3196 
    31953197      // send request 
    31963198      if (request_obj = this.get_request_obj()) 
    31973199        { 
    3198         console('HTTP POST: '+this.env.comm_path+'&_action='+action); 
     3200        console.log('HTTP POST: '+this.env.comm_path+'&_action='+action); 
    31993201 
    32003202        if (lock) 
     
    32223224    this.set_busy(false); 
    32233225 
    3224   console(request_obj.get_text()); 
     3226    console.log(request_obj.get_text()); 
    32253227 
    32263228    // if we get javascript code from server -> execute it 
     
    35283530  } 
    35293531 
    3530 function console(str) 
     3532 
     3533function rcube_console() 
     3534{ 
     3535  this.box = rcube_find_object('console'); 
     3536   
     3537  this.log = function(msg) 
    35313538  { 
    3532   if (document.debugform && document.debugform.console) 
    3533     document.debugform.console.value += str+'\n--------------------------------------\n'; 
    3534   } 
    3535  
     3539    if (this.box) 
     3540      this.box.value += str+'\n--------------------------------------\n'; 
     3541  }; 
     3542   
     3543  this.reset = function() 
     3544  { 
     3545    if (this.box) 
     3546      this.box.value = ''; 
     3547  }; 
     3548} 
     3549 
     3550if (!window.console) 
     3551  console = new rcube_console(); 
  • branches/devel-addressbook/program/js/editor.js

    r458 r522  
    101101   http_request.oncomplete = function(o) { rcmail_set_text_value(o); }; 
    102102   var url = rcmail.env.bin_path+'html2text.php'; 
    103    console('HTTP request: ' + url); 
     103   console.log('HTTP request: ' + url); 
    104104   http_request.POST(url, htmlText, 'application/octet-stream'); 
    105105} 
  • branches/devel-addressbook/program/steps/addressbook/func.inc

    r499 r522  
    4141 
    4242// set message set for search result 
    43 if (!empty($_GET['_search']) && isset($_SESSION['search'][$_GET['_search']])) 
    44   $CONTACTS->set_search_set($_SESSION['search'][$_GET['_search']]); 
     43if (!empty($_REQUEST['_search']) && isset($_SESSION['search'][$_REQUEST['_search']])) 
     44  $CONTACTS->set_search_set($_SESSION['search'][$_REQUEST['_search']]); 
    4545 
    4646// set data source env 
Note: See TracChangeset for help on using the changeset viewer.