Simple Groupware PHP Cross Reference Groupware Applications

Source: /src/modules/lib/ldif_contacts.php - 126 lines - 5502 bytes - Summary - Text - Print

   1  <?php
   2      /**************************************************************************\
   3      * Simple Groupware 0.743                                                   *
   4      * http://www.simple-groupware.de                                           *
   5      * Copyright (C) 2002-2012 by Thomas Bley                                   *
   6      * ------------------------------------------------------------------------ *
   7      *  This program is free software; you can redistribute it and/or           *
   8      *  modify it under the terms of the GNU General Public License Version 2   *
   9      *  as published by the Free Software Foundation; only version 2            *
  10      *  of the License, no later version.                                       *
  11      *                                                                          *
  12      *  This program is distributed in the hope that it will be useful,         *
  13      *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
  14      *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
  15      *  GNU General Public License for more details.                            *
  16      *                                                                          *
  17      *  You should have received a copy of the GNU General Public License       *
  18      *  Version 2 along with this program; if not, write to the Free Software   *
  19      *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,                  *
  20      *  MA  02111-1307, USA.                                                    *
  21      \**************************************************************************/
  22  
  23  class lib_ldif_contacts extends lib_default {
  24  
  25  static function count($path,$where,$vars,$mfolder) {
  26    return count(self::_parse($path));
  27  }
  28  
  29  static function select($path,$fields,$where,$order,$limit,$vars,$mfolder) {
  30    $path = rtrim($path,"/");
  31    $datas = self::_parse($path);
  32    $rows = array();
  33    $ids = array();
  34    $mapping = array(
  35      "c"=>"country",    "cn"=>"contactid", "company"=>"company", "department"=>"department", "dn"=>"id",
  36      "description"=>"description", "facsimiletelephonenumber"=>"fax", "fax" => "fax",
  37      "sn"=>"lastname", "homephone"=>"phoneprivate", "ipphone"=>"skype", "l"=>"city",
  38      "mail"=>"email", "mobile"=>"mobile", "mozillasecondemail"=>"emailprivate",
  39      "mozillanickname"=>"nickname", "pager"=>"pager", "postalcode"=>"zipcode", "givenname"=>"firstname",
  40      "st"=>"state", "streetaddress"=>"street", "street"=>"street", "telephonenumber"=>"phone",
  41      "title" => "title"
  42    );
  43    foreach ($datas as $key=>$data) {
  44      $id = $key;
  45      if (!empty($data["id"])) $id = md5($data["id"]);
  46      $row = array();
  47      foreach ($fields as $field) {
  48        $row[$field] = "";
  49        switch ($field) {
  50          case "id": $row[$field] = $path."/?".$id; break;
  51          case "folder": $row[$field] = $path; break;
  52          case "created": $row[$field] = 0; break;
  53          case "lastmodified": $row[$field] = 0; break;
  54          case "lastmodifiedby": $row[$field] = ""; break;
  55          case "searchcontent": $row[$field] = implode(" ",$data); break;
  56          case "contactid": 
  57            if (empty($data["cn"])) {
  58              if (!empty($data["sn"])) $row[$field] = $data["sn"];
  59              if (!empty($data["givenname"])) $row[$field] .= " ".$data["givenname"];
  60              if ($row[$field]=="" and !empty($data["mail"])) $row[$field] = $data["mail"];
  61            } else $row[$field] = $data["cn"];
  62              
  63            $row[$field] = str_replace(array(" ",".",",","@","\"","'"),array("_","_","","_","",""),$row[$field]);
  64            $row[$field] = substr(trim($row[$field]," _-."),0,15);
  65            while (isset($ids[$row[$field]])) $row[$field] .= "_2";
  66            $ids[$row[$field]] = "";
  67            break;
  68          case "lastname":
  69            if (!empty($data["sn"])) $row[$field] = $data["sn"];
  70            if ($row[$field]=="" and !empty($data["mail"])) {
  71              preg_match("/[.-_]?([^.-_@]+)@/i",$data["mail"],$match);
  72              if (!empty($match[1])) $row[$field] = ucfirst(strtolower($match[1]));
  73            }
  74            $row[$field] = trim($row[$field]," ,");
  75            break;
  76          case "firstname":
  77            if (!empty($data["givenname"])) $row[$field] = $data["givenname"];
  78            if ($row[$field]=="" and !empty($data["mail"])) {
  79              preg_match("/([^._@]+)[._][^._@]*@/i",$data["mail"],$match);
  80              if (!empty($match[1])) $row[$field] = ucfirst(strtolower($match[1]));
  81            }
  82            $row[$field] = trim($row[$field]," ,");
  83            break;
  84          default:
  85            if ($field_key = array_search($field,$mapping) and !empty($data[$field_key])) {
  86              $row[$field] = str_replace(array("\"","'"),"",$data[$field_key]);
  87            }
  88            break;
  89        }
  90      }
  91      if (sys_select_where($row,$where,$vars)) $rows[] = $row;
  92    }
  93    $rows = sys_select($rows,$order,$limit,$fields);
  94    return $rows;
  95  }
  96  
  97  private static function _parse($file) {
  98    if (($data = sys_cache_get("ldif_".sha1($file)))) return $data;
  99    if (($message = sys_allowedpath(dirname($file)))) {
 100      sys_warning(sprintf("{t}Cannot read the file %s.{/t} %s",$file,$message));
 101      return array();
 102    }
 103    $rows = array();
 104    $i = 0;
 105    if (($handle = fopen($file, "r"))) {
 106      while (!feof($handle)) {
 107        $data = utf8_encode(trim(fgets($handle, 8192)));
 108        if ($data!="" and $pos = strpos($data,":")) {
 109          $data_key = strtolower(substr($data,0,$pos));
 110          $data_val = substr($data,$pos+1);
 111          if ($data_val!="" and $data_val[0]==":") {
 112            $data_val = base64_decode(trim(substr($data_val,1)));
 113          }
 114          if ($data_key=="" or $data_val=="") continue;
 115          $rows[$i][$data_key] = trim($data_val);
 116        } else if ($data=="") $i++;
 117      }
 118      fclose($handle);
 119    } else {
 120      sys_warning(sprintf("{t}Cannot read the file %s.{/t}",$file));
 121      return array();
 122    }
 123    sys_cache_set("ldif_".sha1($file),$rows,LDIF_CACHE);
 124    return $rows;
 125  }
 126  }

title

Description

title

Description

title

Description

title

title

Body