[2] | 1 | <?php |
---|
| 2 | /**************************************************************************\ |
---|
| 3 | * eGroupWare API - Contacts manager for SQL * |
---|
| 4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
| 5 | * and Miles Lott <milosch@groupwhere.org> * |
---|
| 6 | * View and manipulate contact records using SQL * |
---|
| 7 | * Copyright (C) 2001 Joseph Engo * |
---|
| 8 | * ------------------------------------------------------------------------ * |
---|
| 9 | * This library is part of the eGroupWare API * |
---|
| 10 | * http://www.egroupware.org/api * |
---|
| 11 | * ------------------------------------------------------------------------ * |
---|
| 12 | * This library is free software; you can redistribute it and/or modify it * |
---|
| 13 | * under the terms of the GNU Lesser General Public License as published by * |
---|
| 14 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
| 15 | * or any later version. * |
---|
| 16 | * This library is distributed in the hope that it will be useful, but * |
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
| 19 | * See the GNU Lesser General Public License for more details. * |
---|
| 20 | * You should have received a copy of the GNU Lesser General Public License * |
---|
| 21 | * along with this library; if not, write to the Free Software Foundation, * |
---|
| 22 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
| 23 | \**************************************************************************/ |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * This class provides a contact database scheme. |
---|
| 27 | * It attempts to be based on the vcard 2.1 standard, with mods as needed |
---|
| 28 | * to make for more reasonable sql storage. |
---|
| 29 | * Note that changes here must also work in the LDAP version. |
---|
| 30 | * Syntax: CreateObject('phpgwapi.contacts'); |
---|
| 31 | * Example1: $contacts = CreateObject('phpgwapi.contacts'); |
---|
| 32 | * |
---|
| 33 | * @class contacts_ |
---|
| 34 | * @abstract Contact Management System |
---|
| 35 | * @author jengo/Milosch |
---|
| 36 | * @license LGPL |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | class contacts_ |
---|
| 41 | { |
---|
| 42 | var $db = ''; |
---|
| 43 | var $std_table='phpgw_addressbook'; |
---|
| 44 | var $ext_table='phpgw_addressbook_extra'; |
---|
| 45 | |
---|
| 46 | var $account_id = 0; |
---|
| 47 | var $total_records = 0; |
---|
| 48 | var $grants = ''; |
---|
| 49 | |
---|
| 50 | /* The left side are the array elements used throughout phpgw, right side are the db field names. */ |
---|
| 51 | var $stock_contact_fields = array( |
---|
| 52 | 'fn' => 'fn', |
---|
| 53 | 'n_given' => 'n_given', |
---|
| 54 | 'n_family' => 'n_family', |
---|
| 55 | 'n_middle' => 'n_middle', |
---|
| 56 | 'n_prefix' => 'n_prefix', |
---|
| 57 | 'n_suffix' => 'n_suffix', |
---|
| 58 | 'sound' => 'sound', |
---|
| 59 | 'bday' => 'bday', |
---|
| 60 | 'note' => 'note', |
---|
| 61 | 'tz' => 'tz', |
---|
| 62 | 'geo' => 'geo', |
---|
| 63 | 'url' => 'url', |
---|
| 64 | 'pubkey' => 'pubkey', |
---|
| 65 | 'org_name' => 'org_name', |
---|
| 66 | 'org_unit' => 'org_unit', |
---|
| 67 | 'title' => 'title', |
---|
| 68 | 'adr_one_street' => 'adr_one_street', |
---|
| 69 | 'adr_one_locality' => 'adr_one_locality', |
---|
| 70 | 'adr_one_region' => 'adr_one_region', |
---|
| 71 | 'adr_one_postalcode' => 'adr_one_postalcode', |
---|
| 72 | 'adr_one_countryname' => 'adr_one_countryname', |
---|
| 73 | 'adr_one_type' => 'adr_one_type', |
---|
| 74 | 'label' => 'label', |
---|
| 75 | 'adr_two_street' => 'adr_two_street', |
---|
| 76 | 'adr_two_locality' => 'adr_two_locality', |
---|
| 77 | 'adr_two_region' => 'adr_two_region', |
---|
| 78 | 'adr_two_postalcode' => 'adr_two_postalcode', |
---|
| 79 | 'adr_two_countryname' => 'adr_two_countryname', |
---|
| 80 | 'adr_two_type' => 'adr_two_type', |
---|
| 81 | 'tel_work' => 'tel_work', |
---|
| 82 | 'tel_home' => 'tel_home', |
---|
| 83 | 'tel_voice' => 'tel_voice', |
---|
| 84 | 'tel_fax' => 'tel_fax', |
---|
| 85 | 'tel_msg' => 'tel_msg', |
---|
| 86 | 'tel_cell' => 'tel_cell', |
---|
| 87 | 'tel_pager' => 'tel_pager', |
---|
| 88 | 'tel_bbs' => 'tel_bbs', |
---|
| 89 | 'tel_modem' => 'tel_modem', |
---|
| 90 | 'tel_car' => 'tel_car', |
---|
| 91 | 'tel_isdn' => 'tel_isdn', |
---|
| 92 | 'tel_video' => 'tel_video', |
---|
| 93 | 'tel_prefer' => 'tel_prefer', |
---|
| 94 | 'email' => 'email', |
---|
| 95 | 'email_type' => 'email_type', |
---|
| 96 | 'email_home' => 'email_home', |
---|
| 97 | 'email_home_type' => 'email_home_type' |
---|
| 98 | ); |
---|
| 99 | |
---|
| 100 | var $non_contact_fields = array( |
---|
| 101 | 'id' => 'id', |
---|
| 102 | 'lid' => 'lid', |
---|
| 103 | 'tid' => 'tid', |
---|
| 104 | 'cat_id' => 'cat_id', |
---|
| 105 | 'access' => 'access', |
---|
| 106 | 'owner' => 'owner' |
---|
| 107 | ); |
---|
| 108 | |
---|
| 109 | var $adr_types = array(); |
---|
| 110 | |
---|
| 111 | /* Used to set preferred number field */ |
---|
| 112 | var $tel_types = array( |
---|
| 113 | 'work' => 'work', |
---|
| 114 | 'home' => 'home', |
---|
| 115 | 'voice' => 'voice', |
---|
| 116 | 'fax' => 'fax', |
---|
| 117 | 'msg' => 'msg', |
---|
| 118 | 'cell' => 'cell', |
---|
| 119 | 'pager' => 'pager', |
---|
| 120 | 'bbs' => 'bbs', |
---|
| 121 | 'modem' => 'modem', |
---|
| 122 | 'car' => 'car', |
---|
| 123 | 'isdn' => 'isdn', |
---|
| 124 | 'video' => 'video' |
---|
| 125 | ); |
---|
| 126 | |
---|
| 127 | /* Used to set email_type fields */ |
---|
| 128 | var $email_types = array( |
---|
| 129 | 'INTERNET' => 'INTERNET', |
---|
| 130 | 'CompuServe' => 'CompuServe', |
---|
| 131 | 'AOL' => 'AOL', |
---|
| 132 | 'Prodigy' => 'Prodigy', |
---|
| 133 | 'eWorld' => 'eWorld', |
---|
| 134 | 'AppleLink' => 'AppleLink', |
---|
| 135 | 'AppleTalk' => 'AppleTalk', |
---|
| 136 | 'PowerShare' => 'PowerShare', |
---|
| 137 | 'IBMMail' => 'IBMMail', |
---|
| 138 | 'ATTMail' => 'ATTMail', |
---|
| 139 | 'MCIMail' => 'MCIMail', |
---|
| 140 | 'X.400' => 'X.400', |
---|
| 141 | 'TLX' => 'TLX' |
---|
| 142 | ); |
---|
| 143 | |
---|
| 144 | function contacts_($useacl=True) |
---|
| 145 | { |
---|
| 146 | // $this->db = $GLOBALS['phpgw']->db; |
---|
| 147 | copyobj($GLOBALS['phpgw']->db,$this->db); |
---|
| 148 | if($useacl) |
---|
| 149 | { |
---|
| 150 | $this->grants = $GLOBALS['phpgw']->acl->get_grants('addressbook'); |
---|
| 151 | } |
---|
| 152 | $this->account_id = $GLOBALS['phpgw_info']['user']['account_id']; |
---|
| 153 | |
---|
| 154 | /* Used to flag an address as being: |
---|
| 155 | domestic AND/OR international(default) |
---|
| 156 | parcel(default) |
---|
| 157 | postal(default) |
---|
| 158 | */ |
---|
| 159 | $this->adr_types = array( |
---|
| 160 | 'dom' => lang('Domestic'), |
---|
| 161 | 'intl' => lang('International'), |
---|
| 162 | 'parcel' => lang('Parcel'), |
---|
| 163 | 'postal' => lang('Postal') |
---|
| 164 | ); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* send this the id and whatever fields you want to see */ |
---|
| 168 | function read_single_entry($id,$fields='') |
---|
| 169 | { |
---|
| 170 | if (!$fields || empty($fields)) |
---|
| 171 | { |
---|
| 172 | $fields = $this->stock_contact_fields; |
---|
| 173 | } |
---|
| 174 | list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields); |
---|
| 175 | |
---|
| 176 | if (count($stock_fieldnames)) |
---|
| 177 | { |
---|
| 178 | $t_fields = ',' . implode(',',$stock_fieldnames); |
---|
| 179 | if ($t_fields == ',') |
---|
| 180 | { |
---|
| 181 | unset($t_fields); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | $this->db->query("SELECT id,lid,tid,owner,access,cat_id $t_fields FROM $this->std_table WHERE id=" . (int)$id); |
---|
| 186 | $this->db->next_record(); |
---|
| 187 | |
---|
| 188 | $return_fields[0]['id'] = $this->db->f('id'); |
---|
| 189 | $return_fields[0]['lid'] = $this->db->f('lid'); |
---|
| 190 | $return_fields[0]['tid'] = $this->db->f('tid'); |
---|
| 191 | $return_fields[0]['owner'] = $this->db->f('owner'); |
---|
| 192 | $return_fields[0]['access'] = $this->db->f('access'); |
---|
| 193 | $return_fields[0]['cat_id'] = $this->db->f('cat_id'); |
---|
| 194 | $return_fields[0]['rights'] = (int)$this->grants[$this->db->f('owner')]; |
---|
| 195 | |
---|
| 196 | if(@is_array($stock_fieldnames)) |
---|
| 197 | { |
---|
| 198 | foreach($stock_fieldnames as $f_name) |
---|
| 199 | { |
---|
| 200 | $return_fields[0][$f_name] = $this->db->f($f_name); |
---|
| 201 | } |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | /* Setup address type fields for ui forms display */ |
---|
| 205 | if ($this->db->f('adr_one_type')) |
---|
| 206 | { |
---|
| 207 | $one_type = $this->db->f('adr_one_type'); |
---|
| 208 | foreach($this->adr_types as $name => $val) |
---|
| 209 | { |
---|
| 210 | eval("if (strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }"); |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | if ($this->db->f('adr_two_type')) |
---|
| 214 | { |
---|
| 215 | $two_type = $this->db->f('adr_two_type'); |
---|
| 216 | foreach($this->adr_types as $name => $val) |
---|
| 217 | { |
---|
| 218 | eval("if (strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }"); |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | $this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id=" . (int)$this->db->f('id'),__LINE__,__FILE__); |
---|
| 223 | while ($this->db->next_record()) |
---|
| 224 | { |
---|
| 225 | if ($extra_fields[$this->db->f('contact_name')]) |
---|
| 226 | { |
---|
| 227 | $return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value'); |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | return $return_fields; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | function read_last_entry($fields='') |
---|
| 234 | { |
---|
| 235 | if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; } |
---|
| 236 | list($stock_fields,$stock_fieldnames,$extra_fields) = |
---|
| 237 | $this->split_stock_and_extras($fields); |
---|
| 238 | |
---|
| 239 | if (count($stock_fieldnames)) |
---|
| 240 | { |
---|
| 241 | $t_fields = ',' . implode(',',$stock_fieldnames); |
---|
| 242 | if ($t_fields == ',') |
---|
| 243 | { |
---|
| 244 | unset($t_fields); |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | $this->db->query('SELECT max(id) FROM '.$this->std_table,__LINE__,__FILE__); |
---|
| 249 | $this->db->next_record(); |
---|
| 250 | |
---|
| 251 | $id = $this->db->f(0); |
---|
| 252 | |
---|
| 253 | $this->db->query("SELECT id,lid,tid,owner,access,cat_id $t_fields FROM $this->std_table WHERE id=" . (int)$id,__LINE__,__FILE__); |
---|
| 254 | $this->db->next_record(); |
---|
| 255 | |
---|
| 256 | $return_fields[0]['id'] = $this->db->f('id'); |
---|
| 257 | $return_fields[0]['lid'] = $this->db->f('lid'); |
---|
| 258 | $return_fields[0]['tid'] = $this->db->f('tid'); |
---|
| 259 | $return_fields[0]['owner'] = $this->db->f('owner'); |
---|
| 260 | $return_fields[0]['access'] = $this->db->f('access'); |
---|
| 261 | $return_fields[0]['cat_id'] = $this->db->f('cat_id'); |
---|
| 262 | $return_fields[0]['rights'] = (int)$this->grants[$this->db->f('owner')]; |
---|
| 263 | |
---|
| 264 | if (@is_array($stock_fieldnames)) |
---|
| 265 | { |
---|
| 266 | foreach($stock_fieldnames as $f_name) |
---|
| 267 | { |
---|
| 268 | $return_fields[0][$f_name] = $this->db->f($f_name); |
---|
| 269 | } |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | /* Setup address type fields for ui forms display */ |
---|
| 273 | if($this->db->f('adr_one_type')) |
---|
| 274 | { |
---|
| 275 | $one_type = $this->db->f('adr_one_type'); |
---|
| 276 | foreach($this->adr_types as $name => $val) |
---|
| 277 | { |
---|
| 278 | eval("if (strstr(\$one_type,\$name)) { \$return_fields[0][\"one_\$name\"] = \"on\"; }"); |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | if($this->db->f('adr_two_type')) |
---|
| 282 | { |
---|
| 283 | $two_type = $this->db->f('adr_two_type'); |
---|
| 284 | foreach($this->adr_types as $name => $val) |
---|
| 285 | { |
---|
| 286 | eval("if (strstr(\$two_type,\$name)) { \$return_fields[0][\"two_\$name\"] = \"on\"; }"); |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | $this->db->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id=" . (int)$this->db->f('id'),__LINE__,__FILE__); |
---|
| 291 | while ($this->db->next_record()) |
---|
| 292 | { |
---|
| 293 | if ($extra_fields[$this->db->f('contact_name')]) |
---|
| 294 | { |
---|
| 295 | $return_fields[0][$this->db->f('contact_name')] = $this->db->f('contact_value'); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | return $return_fields; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | /* send this the range, query, sort, order and whatever fields you want to see */ |
---|
| 302 | function read($start=0,$limit=0,$fields='',$query='',$filter='',$sort='',$order='', $lastmod=-1,$cquery='') |
---|
| 303 | { |
---|
| 304 | if(!$start) { $start = 0; } |
---|
| 305 | if(!$limit) { $limit = 0; } |
---|
| 306 | if(!$filter) { $filter = 'tid=n'; } |
---|
| 307 | |
---|
| 308 | if (!$fields || empty($fields)) { $fields = $this->stock_contact_fields; } |
---|
| 309 | $DEBUG = 0; |
---|
| 310 | |
---|
| 311 | list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields); |
---|
| 312 | if (count($stock_fieldnames)) |
---|
| 313 | { |
---|
| 314 | $t_fields = ',' . implode(',',$stock_fieldnames); |
---|
| 315 | if ($t_fields == ',') |
---|
| 316 | { |
---|
| 317 | unset($t_fields); |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | /* turn filter's a=b,c=d OR a=b into an array */ |
---|
| 322 | if ($filter) |
---|
| 323 | { |
---|
| 324 | $check_stock = $this->stock_contact_fields + $this->non_contact_fields; |
---|
| 325 | |
---|
| 326 | if ($DEBUG) { echo 'DEBUG - Inbound filter is: #'.$filter.'#'; } |
---|
| 327 | |
---|
| 328 | $filterlist = array(); |
---|
| 329 | foreach(explode(',',$filter) as $pair) |
---|
| 330 | { |
---|
| 331 | list($name,$value) = explode('=',$pair,2); |
---|
| 332 | if (!$name || !isset($check_stock[$name])) // only use valid column-names |
---|
| 333 | { |
---|
| 334 | continue; |
---|
| 335 | } |
---|
| 336 | if ($DEBUG) { echo '<br>DEBUG - Filter intermediate strings 2: #'.$name.'# => #'.$value.'#'; } |
---|
| 337 | |
---|
| 338 | if (empty($value)) |
---|
| 339 | { |
---|
| 340 | if ($DEBUG) { echo '<br>DEBUG - filter field "'.$name.'" is empty (NULL)'; } |
---|
| 341 | |
---|
| 342 | $filterlist[] = $name.' is NULL'; |
---|
| 343 | } |
---|
| 344 | else |
---|
| 345 | { |
---|
| 346 | if($name == 'cat_id') |
---|
| 347 | { |
---|
| 348 | if (!(int)$value) continue; // nothing to filter |
---|
| 349 | |
---|
| 350 | //$filterlist[] = "(" . $name . " LIKE '%," . (int)$value . ",%' OR " . $name."='".(int)$value."')"; |
---|
| 351 | if (!is_object($GLOBALS['phpgw']->categories)) |
---|
| 352 | { |
---|
| 353 | $GLOBALS['phpgw']->categories = CreateObject('phpgwapi.categories'); |
---|
| 354 | } |
---|
| 355 | $cats = $GLOBALS['phpgw']->categories->return_all_children((int)$value); |
---|
| 356 | $cat_filter = '(cat_id IN ('.implode(',',$cats).')'; |
---|
| 357 | foreach($cats as $cat) |
---|
| 358 | { |
---|
| 359 | $cat_filter .= " OR cat_id LIKE '%,$cat,%'"; |
---|
| 360 | } |
---|
| 361 | $cat_filter .= ')'; |
---|
| 362 | $filterlist[] = $cat_filter; |
---|
| 363 | } |
---|
| 364 | elseif(@is_int($value)) |
---|
| 365 | { |
---|
| 366 | $filterlist[] = $name . '=' . $value; |
---|
| 367 | } |
---|
| 368 | elseif ($value == "!''") // check for not empty |
---|
| 369 | { |
---|
| 370 | $filterlist[] = $name . "!=''"; |
---|
| 371 | } |
---|
| 372 | else |
---|
| 373 | { |
---|
| 374 | $filterlist[] = $name . "='" . $this->db->db_addslashes($value) . "'"; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | } |
---|
| 378 | $filterlist = implode(' AND ',$filterlist); |
---|
| 379 | |
---|
| 380 | if ($DEBUG) |
---|
| 381 | { |
---|
| 382 | echo '<br>DEBUG - Filter output string: #'.$filterlist.'#'; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | if ($filterlist) |
---|
| 386 | { |
---|
| 387 | $filtermethod = '('.$filterlist.') '; |
---|
| 388 | $fwhere = ' WHERE '; $fand = ' AND '; |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | else |
---|
| 392 | { |
---|
| 393 | $filtermethod = " AND (tid='n' OR tid is null)"; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | if (!$filtermethod) |
---|
| 397 | { |
---|
| 398 | if($this->account_id) |
---|
| 399 | { |
---|
| 400 | $fwhere .= ' (owner=' . $this->account_id; |
---|
| 401 | $fand .= ' (owner=' . $this->account_id; |
---|
| 402 | } |
---|
| 403 | } |
---|
| 404 | else |
---|
| 405 | { |
---|
| 406 | if($this->account_id) |
---|
| 407 | { |
---|
| 408 | $fwhere .= $filtermethod . ' AND (owner=' . $this->account_id; |
---|
| 409 | $fand .= $filtermethod . ' AND (owner=' . $this->account_id; |
---|
| 410 | } |
---|
| 411 | else |
---|
| 412 | { |
---|
| 413 | $filtermethod = substr($filtermethod,0,-2); |
---|
| 414 | $fwhere .= $filtermethod; |
---|
| 415 | $fand .= $filtermethod; |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | if(@is_array($this->grants)) |
---|
| 420 | { |
---|
| 421 | $grants = $this->grants; |
---|
| 422 | foreach($grants as $user => $_right) |
---|
| 423 | { |
---|
| 424 | $public_user_list[] = $user; |
---|
| 425 | } |
---|
| 426 | $fwhere .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) "; |
---|
| 427 | $fand .= " OR (access='public' AND owner in(" . implode(',',$public_user_list) . "))) "; |
---|
| 428 | } |
---|
| 429 | else |
---|
| 430 | { |
---|
| 431 | $fwhere .= ') '; $fand .= ') '; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | if ($DEBUG && $filtermethod) |
---|
| 435 | { |
---|
| 436 | echo '<br>DEBUG - Filtering with: #' . $filtermethod . '#'; |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | if (!$sort) { $sort = 'ASC'; } |
---|
| 440 | |
---|
| 441 | if (!empty($order) && preg_match('/^[a-zA-Z_0-9, ]+$/',$order) && (empty($sort) || preg_match('/^(DESC|ASC|desc|asc)$/',$sort))) |
---|
| 442 | { |
---|
| 443 | $ordermethod = "ORDER BY $order $sort "; |
---|
| 444 | } |
---|
| 445 | else |
---|
| 446 | { |
---|
| 447 | $ordermethod = "ORDER BY n_family,n_given,email ASC"; |
---|
| 448 | } |
---|
| 449 | |
---|
| 450 | if ($DEBUG && $ordermethod) |
---|
| 451 | { |
---|
| 452 | echo "<br>DEBUG - $ordermethod"; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | if($lastmod >= 0 && $fwhere) |
---|
| 456 | { |
---|
| 457 | $fwhere .= " AND last_mod > ".(int)$lastmod.' '; |
---|
| 458 | } |
---|
| 459 | elseif($lastmod >= 0) |
---|
| 460 | { |
---|
| 461 | $fwhere = " WHERE last_mod > ".(int)$lastmod.' '; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | if ($DEBUG && $last_mod_filter && $fwhere) |
---|
| 465 | { |
---|
| 466 | echo "<br>DEBUG - last_mod_filter added to fwhere: $fwhere"; |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | $filtermethod = ''; |
---|
| 470 | |
---|
| 471 | if($cquery) |
---|
| 472 | { |
---|
| 473 | $sql = 'SELECT * FROM ' . $this->std_table . ' WHERE ('; |
---|
| 474 | $sqlcount = 'SELECT COUNT(id) FROM ' . $this->std_table . ' WHERE ('; |
---|
| 475 | foreach(array( |
---|
| 476 | 'fn' => 'cn', |
---|
| 477 | 'n_family' => 'sn', |
---|
| 478 | 'org_name' => 'o' |
---|
| 479 | ) as $f => $x) |
---|
| 480 | { |
---|
| 481 | $cquery = strtoupper($this->db->db_addslashes($cquery)); |
---|
| 482 | $sql .= " UPPER($f) LIKE '$cquery%' OR "; |
---|
| 483 | $sqlcount .= " UPPER($f) LIKE '$cquery%' OR "; |
---|
| 484 | } |
---|
| 485 | $sql = substr($sql,0,-3) . ') ' . $fand . $filtermethod . $ordermethod; |
---|
| 486 | $sqlcount = substr($sqlcount,0,-3) . ') ' . $fand . $filtermethod; |
---|
| 487 | unset($f); unset($x); |
---|
| 488 | } |
---|
| 489 | elseif($query) |
---|
| 490 | { |
---|
| 491 | if(is_array($query)) |
---|
| 492 | { |
---|
| 493 | $sql = "SELECT * FROM $this->std_table WHERE ("; |
---|
| 494 | $sqlcount = "SELECT COUNT(id) FROM $this->std_table WHERE ("; |
---|
| 495 | foreach($query as $queryKey => $queryValue) |
---|
| 496 | { |
---|
| 497 | if (!preg_match('/^[a-zA-Z0-9_]+$/',$queryKey)) |
---|
| 498 | { |
---|
| 499 | continue; // this can be something nasty |
---|
| 500 | } |
---|
| 501 | // special handling of text columns for certain db's; |
---|
| 502 | if (in_array($f,array('note','pubkey','label'))) |
---|
| 503 | { |
---|
| 504 | switch($this->db->Type) |
---|
| 505 | { |
---|
| 506 | case 'mssql': |
---|
| 507 | $queryKey = "CAST($queryKey AS varchar)"; // mssql cant use UPPER on text columns |
---|
| 508 | break; |
---|
| 509 | } |
---|
| 510 | } |
---|
| 511 | $queryValue = strtoupper($this->db->db_addslashes($queryValue)); |
---|
| 512 | $sql .= " UPPER($queryKey) LIKE '$queryValue' AND "; |
---|
| 513 | $sqlcount .= " UPPER($queryKey) LIKE '$queryValue' AND "; |
---|
| 514 | } |
---|
| 515 | $sql = substr($sql,0,-5) . ') ' . $fand . $filtermethod . $ordermethod; |
---|
| 516 | $sqlcount = substr($sqlcount,0,-5) . ') ' . $fand . $filtermethod; |
---|
| 517 | unset($queryKey); unset($queryValue); |
---|
| 518 | } |
---|
| 519 | else |
---|
| 520 | { |
---|
| 521 | $query = strtoupper($this->db->db_addslashes($query)); |
---|
| 522 | |
---|
| 523 | $sql = "SELECT * FROM $this->std_table WHERE ("; |
---|
| 524 | $sqlcount = "SELECT COUNT(id) FROM $this->std_table WHERE ("; |
---|
| 525 | foreach($this->stock_contact_fields as $f => $x) |
---|
| 526 | { |
---|
| 527 | // special handling of text columns for certain db's; |
---|
| 528 | if (in_array($f,array('note','pubkey','label'))) |
---|
| 529 | { |
---|
| 530 | switch($this->db->Type) |
---|
| 531 | { |
---|
| 532 | case 'mssql': |
---|
| 533 | $f = "CAST($f AS varchar)"; // mssql cant use UPPER on text columns |
---|
| 534 | break; |
---|
| 535 | } |
---|
| 536 | } |
---|
| 537 | $sql .= " UPPER($f) LIKE '%$query%' OR "; |
---|
| 538 | $sqlcount .= " UPPER($f) LIKE '%$query%' OR "; |
---|
| 539 | } |
---|
| 540 | $sql = substr($sql,0,-3) . ') ' . $fand . $filtermethod . $ordermethod; |
---|
| 541 | $sqlcount = substr($sqlcount,0,-3) . ') ' . $fand . $filtermethod; |
---|
| 542 | unset($f); unset($x); |
---|
| 543 | } |
---|
| 544 | } |
---|
| 545 | else |
---|
| 546 | { |
---|
| 547 | $sql = "SELECT id,lid,tid,owner,access,cat_id,last_mod $t_fields FROM $this->std_table " . $fwhere |
---|
| 548 | . $filtermethod . ' ' . $ordermethod; |
---|
| 549 | $sqlcount = "SELECT COUNT(id) FROM $this->std_table " . $fwhere |
---|
| 550 | . $filtermethod; |
---|
| 551 | } |
---|
| 552 | if($DEBUG) |
---|
| 553 | { |
---|
| 554 | echo '<br>COUNT QUERY' . $sqlcount; |
---|
| 555 | echo '<br>FULL QUERY' . $sql; |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | // $db2 = $this->db; |
---|
| 559 | copyobj($this->db,$db2); |
---|
| 560 | |
---|
| 561 | /* Perhaps it is more efficient to count records for this query, which is all we need here */ |
---|
| 562 | $this->db->query($sqlcount,__LINE__,__FILE__); |
---|
| 563 | $this->db->next_record(); |
---|
| 564 | unset($sqlcount); |
---|
| 565 | $this->total_records = $this->db->f(0); |
---|
| 566 | |
---|
| 567 | if($start && $limit) |
---|
| 568 | { |
---|
| 569 | if($this->total_records <= $limit) |
---|
| 570 | { |
---|
| 571 | $this->db->query($sql,__LINE__,__FILE__); |
---|
| 572 | } |
---|
| 573 | else |
---|
| 574 | { |
---|
| 575 | $this->db->limit_query($sql,$start,__LINE__,__FILE__,$limit); |
---|
| 576 | } |
---|
| 577 | } |
---|
| 578 | elseif(!$limit) |
---|
| 579 | { |
---|
| 580 | $this->db->query($sql,__LINE__,__FILE__); |
---|
| 581 | } |
---|
| 582 | else |
---|
| 583 | { |
---|
| 584 | $this->db->limit_query($sql,$start,__LINE__,__FILE__); |
---|
| 585 | } |
---|
| 586 | |
---|
| 587 | $i = 0; |
---|
| 588 | while($this->db->next_record()) |
---|
| 589 | { |
---|
| 590 | $return_fields[$i]['id'] = $this->db->f('id'); |
---|
| 591 | $return_fields[$i]['lid'] = $this->db->f('lid'); |
---|
| 592 | $return_fields[$i]['tid'] = $this->db->f('tid'); |
---|
| 593 | $return_fields[$i]['owner'] = $this->db->f('owner'); |
---|
| 594 | $return_fields[$i]['access'] = $this->db->f('access'); |
---|
| 595 | $return_fields[$i]['cat_id'] = $this->db->f('cat_id'); |
---|
| 596 | $return_fields[$i]['last_mod'] = $this->db->f('last_mod'); |
---|
| 597 | $return_fields[$i]['rights'] = (int)$this->grants[$this->db->f('owner')]; |
---|
| 598 | |
---|
| 599 | if(@is_array($stock_fieldnames)) |
---|
| 600 | { |
---|
| 601 | foreach($stock_fieldnames as $f_name) |
---|
| 602 | { |
---|
| 603 | $return_fields[$i][$f_name] = $this->db->f($f_name); |
---|
| 604 | } |
---|
| 605 | reset($stock_fieldnames); |
---|
| 606 | } |
---|
| 607 | $db2->query("SELECT contact_name,contact_value FROM $this->ext_table WHERE contact_id=" |
---|
| 608 | . (int)$this->db->f('id') . $filterextra,__LINE__,__FILE__); |
---|
| 609 | while($db2->next_record()) |
---|
| 610 | { |
---|
| 611 | if($extra_fields[$db2->f('contact_name')]) |
---|
| 612 | { |
---|
| 613 | $return_fields[$i][$db2->f('contact_name')] = $db2->f('contact_value'); |
---|
| 614 | } |
---|
| 615 | } |
---|
| 616 | $i++; |
---|
| 617 | } |
---|
| 618 | return $return_fields; |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | function add($owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL) |
---|
| 622 | { |
---|
| 623 | $owner = (int)$owner; |
---|
| 624 | $lid = array(); |
---|
| 625 | // access, cat_id and tid can be in $fields now or as extra params |
---|
| 626 | foreach(array('access','cat_id','tid') as $extra) |
---|
| 627 | { |
---|
| 628 | if (!is_null($$extra)) |
---|
| 629 | { |
---|
| 630 | $fields[$extra] = $$extra; |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | if(empty($fields['tid'])) |
---|
| 634 | { |
---|
| 635 | $fields['tid'] = 'n'; |
---|
| 636 | } |
---|
| 637 | if(isset($fields['lid'])) |
---|
| 638 | { |
---|
| 639 | //fix by pim |
---|
| 640 | //$lid = array('lid,' => $fields['lid']."','"); |
---|
| 641 | $lid = array('lid,', $fields['lid'] . "','"); |
---|
| 642 | } |
---|
| 643 | list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields); |
---|
| 644 | |
---|
| 645 | //this is added here so it is never tainted |
---|
| 646 | $this->stock_contact_fields['last_mod'] = 'last_mod'; |
---|
| 647 | $stock_fields['last_mod'] = $GLOBALS['phpgw']->datetime->gmtnow; |
---|
| 648 | |
---|
| 649 | $sql = 'INSERT INTO ' . $this->std_table . " (owner,access,cat_id,tid," . $lid[0] |
---|
| 650 | . implode(',',$this->stock_contact_fields) |
---|
| 651 | . ') VALUES (' . $owner . ",'" . $fields['access'] . "','" . $fields['cat_id'] |
---|
| 652 | . "','" . $fields['tid'] . "','" . $lid[1] |
---|
| 653 | . implode("','",$this->loop_addslashes($stock_fields)) . "')"; |
---|
| 654 | $this->db->query($sql,__LINE__,__FILE__); |
---|
| 655 | |
---|
| 656 | $id = $this->db->get_last_insert_id($this->std_table, 'id'); |
---|
| 657 | |
---|
| 658 | if(count($extra_fields)) |
---|
| 659 | { |
---|
| 660 | foreach($extra_fields as $name => $value) |
---|
| 661 | { |
---|
| 662 | $this->db->query("INSERT INTO $this->ext_table VALUES (" . (int)$id . ",'" . $owner . "','" |
---|
| 663 | . $this->db->db_addslashes($name) . "','" . $this->db->db_addslashes($value) . "')",__LINE__,__FILE__); |
---|
| 664 | } |
---|
| 665 | } |
---|
| 666 | return ($id ? $id : False); |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | function field_exists($id,$field_name) |
---|
| 670 | { |
---|
| 671 | $this->db->query("SELECT COUNT(*) FROM $this->ext_table WHERE contact_id=" . (int)$id . " AND contact_name='" |
---|
| 672 | . $this->db->db_addslashes($field_name) . "'",__LINE__,__FILE__); |
---|
| 673 | $this->db->next_record(); |
---|
| 674 | return $this->db->f(0); |
---|
| 675 | } |
---|
| 676 | |
---|
| 677 | function add_single_extra_field($id,$owner,$field_name,$field_value) |
---|
| 678 | { |
---|
| 679 | $this->db->query("INSERT INTO $this->ext_table VALUES (" . (int)$id . ",'".(int)$owner."','" . $this->db->db_addslashes($field_name) |
---|
| 680 | . "','" . $this->db->db_addslashes($field_value) . "')",__LINE__,__FILE__); |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | function delete_single_extra_field($id,$field_name) |
---|
| 684 | { |
---|
| 685 | $this->db->query("DELETE FROM $this->ext_table WHERE contact_id=" . (int)$id . " AND contact_name='" |
---|
| 686 | . $this->db->db_addslashes($field_name) . "'",__LINE__,__FILE__); |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | function update($id,$owner,$fields,$access=NULL,$cat_id=NULL,$tid=NULL) |
---|
| 690 | { |
---|
| 691 | $owner = (int)$owner; |
---|
| 692 | $id = (int)$id; |
---|
| 693 | /* First make sure that id number exists */ |
---|
| 694 | $this->db->query("SELECT COUNT(*) FROM $this->std_table WHERE id=$id",__LINE__,__FILE__); |
---|
| 695 | $this->db->next_record(); |
---|
| 696 | if (!$this->db->f(0)) |
---|
| 697 | { |
---|
| 698 | return False; |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | list($stock_fields,,$extra_fields) = $this->split_stock_and_extras($fields); |
---|
| 702 | // access, cat_id and tid can be in $fields now or as extra params |
---|
| 703 | foreach(array('access','cat_id','tid') as $extra) |
---|
| 704 | { |
---|
| 705 | if (!is_null($$extra)) |
---|
| 706 | { |
---|
| 707 | $fields[$extra] = $$extra; |
---|
| 708 | } |
---|
| 709 | if (isset($fields[$extra])) |
---|
| 710 | { |
---|
| 711 | $stock_fields[$extra] = $fields[$extra]; |
---|
| 712 | } |
---|
| 713 | } |
---|
| 714 | |
---|
| 715 | if (count($stock_fields)) |
---|
| 716 | { |
---|
| 717 | foreach($stock_fields as $name => $value) |
---|
| 718 | { |
---|
| 719 | $ta[] = $name . "='" . $this->db->db_addslashes($value) . "'"; |
---|
| 720 | } |
---|
| 721 | $ta[] = 'last_mod=' . $GLOBALS['phpgw']->datetime->gmtnow; |
---|
| 722 | $fields_s = implode(',',$ta); |
---|
| 723 | if ($field_s == ',') |
---|
| 724 | { |
---|
| 725 | unset($field_s); |
---|
| 726 | } |
---|
| 727 | $this->db->query($sql="UPDATE $this->std_table SET $fields_s WHERE " |
---|
| 728 | . "id=$id",__LINE__,__FILE__); |
---|
| 729 | } |
---|
| 730 | if (is_array($extra_fields)) |
---|
| 731 | { |
---|
| 732 | foreach($extra_fields as $x_name => $x_value) |
---|
| 733 | { |
---|
| 734 | if ($this->field_exists($id,$x_name)) |
---|
| 735 | { |
---|
| 736 | if (!$x_value) |
---|
| 737 | { |
---|
| 738 | $this->delete_single_extra_field($id,$x_name); |
---|
| 739 | } |
---|
| 740 | else |
---|
| 741 | { |
---|
| 742 | $this->db->query("UPDATE $this->ext_table SET contact_value='" . $this->db->db_addslashes($x_value) |
---|
| 743 | . "',contact_owner=$owner WHERE contact_name='" . $this->db->db_addslashes($x_name) |
---|
| 744 | . "' AND contact_id=$id",__LINE__,__FILE__); |
---|
| 745 | } |
---|
| 746 | } |
---|
| 747 | elseif($x_value) // dont write emtpy extra-fields |
---|
| 748 | { |
---|
| 749 | $this->add_single_extra_field($id,$owner,$x_name,$x_value); |
---|
| 750 | } |
---|
| 751 | } |
---|
| 752 | } |
---|
| 753 | return True; |
---|
| 754 | } |
---|
| 755 | |
---|
| 756 | /* Used by admin to change ownership on account delete */ |
---|
| 757 | function change_owner($old_owner,$new_owner) |
---|
| 758 | { |
---|
| 759 | $old_owner = (int) $old_owner; |
---|
| 760 | $new_owner = (int) $new_owner; |
---|
| 761 | if (!$new_owner || !$old_owner) |
---|
| 762 | { |
---|
| 763 | return False; |
---|
| 764 | } |
---|
| 765 | $this->db->query("UPDATE $this->std_table SET owner='$new_owner' WHERE owner=$old_owner",__LINE__,__FILE__); |
---|
| 766 | $this->db->query("UPDATE $this->ext_table SET contact_owner='$new_owner' WHERE contact_owner=$old_owner",__LINE__,__FILE__); |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | /* This is where the real work of delete() is done, shared class file contains calling function */ |
---|
| 770 | function delete_($id) |
---|
| 771 | { |
---|
| 772 | $this->db->query("DELETE FROM $this->std_table WHERE id=" . (int)$id,__LINE__,__FILE__); |
---|
| 773 | $this->db->query("DELETE FROM $this->ext_table WHERE contact_id=" . (int)$id,__LINE__,__FILE__); |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | /* This is for the admin script deleteaccount.php */ |
---|
| 777 | function delete_all($owner=0) |
---|
| 778 | { |
---|
| 779 | $owner = (int) $owner; |
---|
| 780 | if ($owner) |
---|
| 781 | { |
---|
| 782 | $this->db->query("DELETE FROM $this->std_table WHERE owner=$owner",__LINE__,__FILE__); |
---|
| 783 | $this->db->query("DELETE FROM $this->ext_table WHERE contact_owner=$owner",__LINE__,__FILE__); |
---|
| 784 | } |
---|
| 785 | } |
---|
| 786 | } |
---|
| 787 | ?> |
---|