/
Storage.php
executable file
·1326 lines (1212 loc) · 39.8 KB
/
Storage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* EGroupware API - Contacts storage object
*
* @link http://www.egroupware.org
* @author Cornelius Weiss <egw-AT-von-und-zu-weiss.de>
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @package addressbook
* @copyright (c) 2005-16 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @copyright (c) 2005/6 by Cornelius Weiss <egw@von-und-zu-weiss.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
namespace EGroupware\Api\Contacts;
use EGroupware\Api;
/**
* Contacts storage object
*
* The contact storage has 3 operation modi (contact_repository):
* - sql: contacts are stored in the SQL table egw_addressbook & egw_addressbook_extra (custom fields)
* - ldap: contacts are stored in LDAP (accounts have to be stored in LDAP too!!!).
* Custom fields are not available in that case!
* - sql-ldap: contacts are read and searched in SQL, but saved to both SQL and LDAP.
* Other clients (Thunderbird, ...) can use LDAP readonly. The get maintained via eGroupWare only.
*
* The accounts can be stored in SQL or LDAP too (account_repository):
* If the account-repository is different from the contacts-repository, the filter all (no owner set)
* will only search the contacts and NOT the accounts! Only the filter accounts (owner=0) shows accounts.
*
* If sql-ldap is used as contact-storage (LDAP is managed from eGroupWare) the filter all, searches
* the accounts in the SQL contacts-table too. Change in made in LDAP, are not detected in that case!
*/
class Storage
{
/**
* name of customfields table
*
* @var string
*/
var $extra_table = 'egw_addressbook_extra';
/**
* @var string
*/
var $extra_id = 'contact_id';
/**
* @var string
*/
var $extra_owner = 'contact_owner';
/**
* @var string
*/
var $extra_key = 'contact_name';
/**
* @var string
*/
var $extra_value = 'contact_value';
/**
* view for distributionlistsmembership
*
* @var string
*/
var $distributionlist_view ='(SELECT contact_id, egw_addressbook_lists.list_id as list_id, egw_addressbook_lists.list_name as list_name, egw_addressbook_lists.list_owner as list_owner FROM egw_addressbook_lists, egw_addressbook2list where egw_addressbook_lists.list_id=egw_addressbook2list.list_id) d_view ';
var $distributionlist_tabledef = array();
/**
* @var string
*/
var $distri_id = 'contact_id';
/**
* @var string
*/
var $distri_owner = 'list_owner';
/**
* @var string
*/
var $distri_key = 'list_id';
/**
* @var string
*/
var $distri_value = 'list_name';
/**
* Contact repository in 'sql' or 'ldap'
*
* @var string
*/
var $contact_repository = 'sql';
/**
* Grants as account_id => rights pairs
*
* @var array
*/
var $grants = array();
/**
* userid of current user
*
* @var int
*/
var $user;
/**
* memberships of the current user
*
* @var array
*/
var $memberships;
/**
* In SQL we can search all columns, though a view make on real sense
*/
var $sql_cols_not_to_search = array(
'jpegphoto','owner','tid','private','cat_id','etag',
'modified','modifier','creator','created','tz','account_id',
'uid','carddav_name','freebusy_uri','calendar_uri',
'geo','pubkey',
);
/**
* columns to search, if we search for a single pattern
*
* @var array
*/
var $columns_to_search = array();
/**
* extra columns to search if accounts are included, eg. account_lid
*
* @var array
*/
var $account_extra_search = array();
/**
* columns to search for accounts, if stored in different repository
*
* @var array
*/
var $account_cols_to_search = array();
/**
* customfields name => array(...) pairs
*
* @var array
*/
var $customfields = array();
/**
* content-types as name => array(...) pairs
*
* @var array
*/
var $content_types = array();
/**
* Directory to store striped photo or public keys in VFS directory of entry
*/
const FILES_DIRECTORY = '.files';
const FILES_PHOTO = '.files/photo.jpeg';
const FILES_PGP_PUBKEY = '.files/pgp-pubkey.asc';
const FILES_SMIME_PUBKEY = '.files/smime-pubkey.crt';
/**
* Constant for bit-field "contact_files" storing what files are available
*/
const FILES_BIT_PHOTO = 1;
const FILES_BIT_PGP_PUBKEY = 2;
const FILES_BIT_SMIME_PUBKEY = 4;
/**
* These fields are options for checking for duplicate contacts
*
* @var array
*/
public static $duplicate_fields = array(
'n_given' => 'first name',
'n_middle' => 'middle name',
'n_family' => 'last name',
'contact_bday' => 'birthday',
'org_name' => 'Organisation',
'org_unit' => 'Department',
'adr_one_locality' => 'Location',
'contact_title' => 'title',
'contact_email' => 'business email',
'contact_email_home'=> 'email (private)',
);
/**
* Special content type to indicate a deleted addressbook
*
* @var String;
*/
const DELETED_TYPE = 'D';
/**
* total number of matches of last search
*
* @var int
*/
var $total;
/**
* storage object: sql (Sql) or ldap (addressbook_ldap) backend class
*
* @var Sql
*/
var $somain;
/**
* storage object for accounts, if not identical to somain (eg. accounts in ldap, contacts in sql)
*
* @var Ldap
*/
var $so_accounts;
/**
* account repository sql or ldap
*
* @var string
*/
var $account_repository = 'sql';
/**
* custom fields backend
*
* @var Sql
*/
var $soextra;
var $sodistrib_list;
/**
* @var Api\Db
*/
var $db;
/**
* Constructor
*
* @param string $contact_app ='addressbook' used for acl->get_grants()
* @param Api\Db $db =null
*/
function __construct($contact_app='addressbook',Api\Db $db=null)
{
$this->db = is_null($db) ? $GLOBALS['egw']->db : $db;
$this->user = $GLOBALS['egw_info']['user']['account_id'];
$this->memberships = $GLOBALS['egw']->accounts->memberships($this->user,true);
// account backend used
if ($GLOBALS['egw_info']['server']['account_repository'])
{
$this->account_repository = $GLOBALS['egw_info']['server']['account_repository'];
}
elseif ($GLOBALS['egw_info']['server']['auth_type'])
{
$this->account_repository = $GLOBALS['egw_info']['server']['auth_type'];
}
$this->customfields = Api\Storage\Customfields::get('addressbook');
// contacts backend (contacts in LDAP require accounts in LDAP!)
if (($GLOBALS['egw_info']['server']['contact_repository']??null) === 'ldap' && $this->account_repository === 'ldap')
{
$this->contact_repository = 'ldap';
$this->somain = new Ldap();
$this->columns_to_search = $this->somain->search_attributes;
}
else // sql or sql->ldap
{
if (($GLOBALS['egw_info']['server']['contact_repository']??null) === 'sql-ldap')
{
$this->contact_repository = 'sql-ldap';
}
$this->somain = new Sql($db);
// remove some columns, absolutly not necessary to search in sql
$this->columns_to_search = array_diff(array_values($this->somain->db_cols),$this->sql_cols_not_to_search);
}
$this->grants = $this->get_grants($this->user,$contact_app);
if ($this->account_repository != 'sql' && $this->contact_repository == 'sql')
{
if ($this->account_repository != $this->contact_repository)
{
$class = 'EGroupware\\Api\\Contacts\\'.ucfirst($this->account_repository);
$this->so_accounts = new $class();
$this->account_cols_to_search = $this->so_accounts->search_attributes;
}
else
{
$this->account_extra_search = array('uid');
}
}
if ($this->contact_repository == 'sql' || $this->contact_repository == 'sql-ldap')
{
$tda2list = $this->db->get_table_definitions('api','egw_addressbook2list');
$tdlists = $this->db->get_table_definitions('api','egw_addressbook_lists');
$this->distributionlist_tabledef = array('fd' => array(
$this->distri_id => $tda2list['fd'][$this->distri_id],
$this->distri_owner => $tdlists['fd'][$this->distri_owner],
$this->distri_key => $tdlists['fd'][$this->distri_key],
$this->distri_value => $tdlists['fd'][$this->distri_value],
), 'pk' => array(), 'fk' => array(), 'ix' => array(), 'uc' => array(),
);
}
// ToDo: it should be the other way arround, the backend should set the grants it uses
$this->somain->grants =& $this->grants;
if($this->somain instanceof Sql)
{
$this->soextra =& $this->somain;
}
else
{
$this->soextra = new Sql($db);
}
$this->content_types = Api\Config::get_content_types('addressbook');
if (!$this->content_types)
{
$this->content_types = array('n' => array(
'name' => 'contact',
'options' => array(
'template' => 'addressbook.edit',
'icon' => 'navbar.png'
)));
}
// Always add in deleted type, to show deleted contacts
$this->content_types[self::DELETED_TYPE] = array(
'name' => lang('Deleted'),
'options' => array(
'template' => 'addressbook.edit',
'icon' => 'deleted.png'
)
);
}
/**
* Get grants for a given user, taking into account static LDAP ACL
*
* @param int $user
* @param string $contact_app ='addressbook'
* @return array
*/
function get_grants($user, $contact_app='addressbook', $preferences=null)
{
if (!isset($preferences)) $preferences = $GLOBALS['egw_info']['user']['preferences'];
if ($user)
{
// contacts backend (contacts in LDAP require accounts in LDAP!)
if(($GLOBALS['egw_info']['server']['contact_repository']??null) === 'ldap' && $this->account_repository === 'ldap')
{
// static grants from ldap: all rights for the own personal addressbook and the group ones of the memberships
$grants = array($user => ~0);
foreach($GLOBALS['egw']->accounts->memberships($user,true) as $gid)
{
$grants[$gid] = ~0;
}
}
else // sql or sql->ldap
{
// group grants are now grants for the group addressbook and NOT grants for all its members,
// therefor the param false!
$grants = $GLOBALS['egw']->acl->get_grants($contact_app,false,$user);
}
// add grants for accounts: if account_selection not in ('none','groupmembers'): everyone has read access,
// if he has not set the hide_accounts preference
// ToDo: be more specific for 'groupmembers', they should be able to see the groupmembers
if (!in_array($preferences['common']['account_selection'], array('none','groupmembers')))
{
$grants[0] = Api\Acl::READ;
}
// add account grants for admins (only for current user!)
if ($user == $this->user && $this->is_admin()) // admin rights can be limited by ACL!
{
$grants[0] = Api\Acl::READ; // admins always have read-access
if (!$GLOBALS['egw']->acl->check('account_access',16,'admin')) $grants[0] |= Api\Acl::EDIT;
if (!$GLOBALS['egw']->acl->check('account_access',4,'admin')) $grants[0] |= Api\Acl::ADD;
if (!$GLOBALS['egw']->acl->check('account_access',32,'admin')) $grants[0] |= Api\Acl::DELETE;
}
// allow certain groups to edit contact-data of accounts
if (self::allow_account_edit($user))
{
$grants[0] |= Api\Acl::READ|Api\Acl::EDIT;
}
}
// no user, eg. setup or not logged in, allow read access to accounts
else
{
$grants = [0 => Api\Acl::READ];
}
//error_log(__METHOD__."($user, '$contact_app') returning ".array2string($grants));
return $grants;
}
/**
* Check if the user is an admin (can unconditionally edit accounts)
*
* We check now the admin ACL for edit users, as the admin app does it for editing accounts.
*
* @param array $contact =null for future use, where admins might not be admins for all accounts
* @return boolean
*/
function is_admin($contact=null)
{
unset($contact); // not (yet) used
return isset($GLOBALS['egw_info']['user']['apps']['admin']) && !$GLOBALS['egw']->acl->check('account_access',16,'admin');
}
/**
* Check if current user is in a group, which is allowed to edit accounts
*
* @param int $user =null default $this->user
* @return boolean
*/
function allow_account_edit($user=null)
{
return !empty($GLOBALS['egw_info']['server']['allow_account_edit']) &&
array_intersect($GLOBALS['egw_info']['server']['allow_account_edit'],
$GLOBALS['egw']->accounts->memberships($user ?: $this->user, true));
}
/**
* Read all customfields of the given id's
*
* @param int|array $ids
* @param array $field_names =null custom fields to read, default all
* @return array id => name => value
*/
function read_customfields($ids,$field_names=null)
{
return $this->soextra->read_customfields($ids,$field_names);
}
/**
* Read all distributionlists of the given id's
*
* @param int|array $ids
* @return array id => name => value
*/
function read_distributionlist($ids, $dl_allowed=array())
{
if ($this->contact_repository == 'ldap')
{
return array(); // ldap does not support distributionlists
}
foreach($ids as $key => $id)
{
if (!is_numeric($id)) unset($ids[$key]);
}
if (!$ids) return array(); // nothing to do, eg. all these contacts are in ldap
$fields = array();
$filter[$this->distri_id]=$ids;
if (count($dl_allowed)) $filter[$this->distri_key]=$dl_allowed;
$distri_view = str_replace(') d_view',' and '.$this->distri_id.' in ('.implode(',',$ids).')) d_view',$this->distributionlist_view);
#_debug_array($this->distributionlist_tabledef);
foreach($this->db->select($distri_view, '*', $filter, __LINE__, __FILE__,
false, 'ORDER BY '.$this->distri_id, false, 0, '', $this->distributionlist_tabledef) as $row)
{
if ((isset($row[$this->distri_id])&&strlen($row[$this->distri_value])>0))
{
$fields[$row[$this->distri_id]][$row[$this->distri_key]] = $row[$this->distri_value].' ('.
Api\Accounts::username($row[$this->distri_owner]).')';
}
}
return $fields;
}
/**
* changes the data from the db-format to your work-format
*
* it gets called everytime when data is read from the db
* This function needs to be reimplemented in the derived class
*
* @param array $data
*/
function db2data($data)
{
return $data;
}
/**
* changes the data from your work-format to the db-format
*
* It gets called everytime when data gets writen into db or on keys for db-searches
* this needs to be reimplemented in the derived class
*
* @param array $data
*/
function data2db($data)
{
return $data;
}
/**
* deletes contact entry including custom fields
*
* @param mixed $contact array with id or just the id
* @param int $check_etag =null
* @return boolean|int true on success or false on failiure, 0 if etag does not match
*/
function delete($contact,$check_etag=null)
{
if (is_array($contact)) $contact = $contact['id'];
$where = array('id' => $contact);
if ($check_etag) $where['etag'] = $check_etag;
// delete mainfields
if ($this->somain->delete($where))
{
// delete customfields, can return 0 if there are no customfields
if(!($this->somain instanceof Sql))
{
$this->soextra->delete_customfields(array($this->extra_id => $contact));
}
// delete from distribution list(s)
$this->remove_from_list($contact);
if ($this->contact_repository == 'sql-ldap')
{
if ($contact['account_id'])
{
// LDAP uses the uid attributes for the contact-id (dn),
// which need to be the account_lid for accounts!
$contact['id'] = $GLOBALS['egw']->accounts->id2name($contact['account_id']);
}
(new Ldap())->delete($contact);
}
return true;
}
return $check_etag ? 0 : false; // if etag given, we return 0 on failure, thought it could also mean the whole contact does not exist
}
/**
* saves contact data including custom fields
*
* @param array &$contact contact data from etemplate::exec
* @return bool false on success, errornumber on failure
*/
function save(&$contact)
{
// save mainfields
if (!empty($contact['id']) && $this->contact_repository != $this->account_repository && is_object($this->so_accounts) &&
($this->contact_repository == 'sql' && !is_numeric($contact['id']) ||
$this->contact_repository == 'ldap' && is_numeric($contact['id'])))
{
$this->so_accounts->data = $this->data2db($contact);
$error_nr = $this->so_accounts->save();
$contact['id'] = $this->so_accounts->data['id'];
}
else
{
// contact_repository sql-ldap (accounts in ldap) the person_id is the uid (account_lid)
// for the sql write here we need to find out the existing contact_id
if ($this->contact_repository == 'sql-ldap' && $contact['id'] && !is_numeric($contact['id']) &&
$contact['account_id'] && ($old = $this->somain->read(array('account_id' => $contact['account_id']))))
{
$contact['id'] = $old['id'];
}
$this->somain->data = $this->data2db($contact);
if (!($error_nr = $this->somain->save()))
{
$contact['id'] = $this->somain->data['id'];
$contact['uid'] = $this->somain->data['uid'];
$contact['etag'] = $this->somain->data['etag'];
$contact['files'] = $this->somain->data['files'] ?? null;
if ($this->contact_repository == 'sql-ldap')
{
$data = $this->somain->data;
if ($contact['account_id'])
{
// LDAP uses the uid attributes for the contact-id (dn),
// which need to be the account_lid for accounts!
$data['id'] = $GLOBALS['egw']->accounts->id2name($contact['account_id']);
}
$error_nr = (new Ldap())->save($data);
}
}
}
if($error_nr) return $error_nr;
return false; // no error
}
/**
* reads contact data including custom fields
*
* @param int|string $contact_id contact_id or 'a'.account_id
* @return array|boolean data if row could be retrived else False
*/
function read($contact_id)
{
if (empty($contact_id))
{
return false; // no need to pass to backend, will fail anyway
}
if (!is_array($contact_id) && substr($contact_id,0,8) == 'account:')
{
$contact_id = array('account_id' => (int) substr($contact_id,8));
}
// read main data
$backend = $this->get_backend($contact_id);
if (!($contact = $backend->read($contact_id)))
{
return $contact;
}
$dl_list=$this->read_distributionlist(array($contact['id']));
if (count($dl_list)) $contact['distrib_lists']=implode("\n",$dl_list[$contact['id']]);
return $this->db2data($contact);
}
/**
* @param array $ids
* @param ?boolean $deleted false: no deleted, true: only deleted, null: both
* @return array contact_id => array of array with sharing info
*/
function read_shared(array $ids, $deleted=false)
{
$contacts = [];
if (method_exists($backend = $this->get_backend($ids[0]), 'read_shared'))
{
foreach($backend->read_shared($ids, $deleted) as $shared)
{
$contacts[$shared['contact_id']][] = $shared;
}
}
return $contacts;
}
/**
* searches db for rows matching searchcriteria
*
* '*' and '?' are replaced with sql-wildcards '%' and '_'
*
* @param array|string $criteria array of key and data cols, OR string to search over all standard search fields
* @param boolean|string $only_keys =true True returns only keys, False returns all cols. comma seperated list of keys to return
* @param string $order_by ='' fieldnames + {ASC|DESC} separated by colons ',', can also contain a GROUP BY (if it contains ORDER BY)
* @param string|array $extra_cols ='' string or array of strings to be added to the SELECT, eg. "count(*) as num"
* @param string $wildcard ='' appended befor and after each criteria
* @param boolean $empty =false False=empty criteria are ignored in query, True=empty have to be empty in row
* @param string $op ='AND' defaults to 'AND', can be set to 'OR' too, then criteria's are OR'ed together
* @param mixed $start =false if != false, return only maxmatch rows begining with start, or array($start,$num)
* @param array $filter =null if set (!=null) col-data pairs, to be and-ed (!) into the query without wildcards
* $filter['cols_to_search'] limit search columns to given columns, otherwise $this->columns_to_search is used
* @param string $join ='' sql to do a join (only used by sql backend!), eg. " RIGHT JOIN egw_accounts USING(account_id)"
* @param boolean $ignore_acl =false true: no acl check
* @return array of matching rows (the row is an array of the cols) or False
*/
function &search($criteria,$only_keys=True,$order_by='',$extra_cols='',$wildcard='',$empty=False,$op='AND',$start=false,$filter=null,$join='', $ignore_acl=false)
{
//error_log(__METHOD__.'('.array2string($criteria,true).','.array2string($only_keys).",'$order_by','$extra_cols','$wildcard','$empty','$op',".array2string($start).','.array2string($filter,true).",'$join')");
// Handle 'None' country option
if(is_array($filter) && isset($filter['adr_one_countrycode']) && $filter['adr_one_countrycode'] === '-custom-')
{
$filter[] = 'adr_one_countrycode IS NULL';
unset($filter['adr_one_countrycode']);
}
// Hide deleted items unless type is specifically deleted
if(!is_array($filter)) $filter = $filter ? (array) $filter : array();
if (isset($filter['cols_to_search']))
{
$cols_to_search = $filter['cols_to_search'];
unset($filter['cols_to_search']);
}
// if no tid set or tid==='' do NOT return deleted entries ($tid === null returns all entries incl. deleted)
if(!array_key_exists('tid', $filter) || $filter['tid'] === '')
{
if ($join && strpos($join,'RIGHT JOIN') !== false) // used eg. to search for groups
{
$filter[] = '(contact_tid != \'' . self::DELETED_TYPE . '\' OR contact_tid IS NULL)';
}
else
{
$filter[] = 'contact_tid != \'' . self::DELETED_TYPE . '\'';
}
}
elseif(is_null($filter['tid']))
{
unset($filter['tid']); // return all entries incl. deleted
}
$backend = $this->get_backend(null, isset($filter['list']) && $filter['list'] < 0 ? 0 : $filter['owner'] ?? null);
// single string to search for --> create so_sql conformant search criteria for the standard search columns
if ($criteria && !is_array($criteria))
{
$op = 'OR';
$wildcard = '%';
$search = $criteria;
$criteria = array();
if (isset($cols_to_search))
{
$cols = $cols_to_search;
}
elseif ($backend === $this->somain)
{
$cols = $this->columns_to_search;
}
else
{
$cols = $this->account_cols_to_search;
}
if($backend instanceof Sql)
{
// Keep a string, let the parent handle it
$criteria = $search;
foreach($cols as $key => &$col)
{
if($col != Sql::EXTRA_VALUE &&
$col != Sql::EXTRA_TABLE.'.'.Sql::EXTRA_VALUE &&
!array_key_exists($col, $backend->db_cols) && $key !== 'search_cfs')
{
if(!($col = array_search($col, $backend->db_cols)))
{
// Can't search this column, it will error if we try
unset($cols[$key]);
}
}
if ($col=='contact_id') $col='egw_addressbook.contact_id';
}
$backend->columns_to_search = $cols;
}
else
{
foreach($cols as $col)
{
// remove from LDAP backend not understood use-AND-syntax
$criteria[$col] = str_replace(' +',' ',$search);
}
}
}
if (is_array($criteria) && count($criteria))
{
$criteria = $this->data2db($criteria);
}
if (is_array($filter) && count($filter))
{
$filter = $this->data2db($filter);
}
else
{
$filter = $filter ? array($filter) : array();
}
// get the used backend for the search and call it's search method
$rows = $backend->search($criteria, $only_keys, $order_by, $extra_cols,
$wildcard, $empty, $op, $start, $filter, $join, false, $ignore_acl);
$this->total = $backend->total;
if ($rows)
{
foreach($rows as $n => $row)
{
$rows[$n] = $this->db2data($row);
}
// allow other apps to hook into search
Api\Hooks::process(array(
'hook_location' => 'contacts_search',
'criteria' => $criteria,
'filter' => $filter,
'ignore_acl' => $ignore_acl,
'obj' => $this,
'rows' => &$rows,
'total' => &$this->total,
), array(), true); // true = no permission check
}
return $rows;
}
/**
* Query organisations by given parameters
*
* @var array $param
* @var string $param[org_view] 'org_name', 'org_name,adr_one_location', 'org_name,org_unit' how to group
* @var int $param[owner] addressbook to search
* @var string $param[search] search pattern for org_name
* @var string $param[searchletter] letter the org_name need to start with
* @var int $param[start]
* @var int $param[num_rows]
* @var string $param[sort] ASC or DESC
* @return array or arrays with keys org_name,count and evtl. adr_one_location or org_unit
*/
function organisations($param)
{
if (!method_exists($this->somain,'organisations'))
{
$this->total = 0;
return false;
}
if ($param['search'] && !is_array($param['search']))
{
$search = $param['search'];
$param['search'] = array();
if($this->somain instanceof Sql)
{
// Keep the string, let the parent deal with it
$param['search'] = $search;
}
else
{
foreach($this->columns_to_search as $col)
{
if ($col != 'contact_value') $param['search'][$col] = $search; // we dont search the customfields
}
}
}
if (is_array($param['search']) && count($param['search']))
{
$param['search'] = $this->data2db($param['search']);
}
if(!array_key_exists('tid', $param['col_filter']) || $param['col_filter']['tid'] === '')
{
$param['col_filter'][] = 'contact_tid != \'' . self::DELETED_TYPE . '\'';
}
elseif(is_null($param['col_filter']['tid']))
{
unset($param['col_filter']['tid']); // return all entries incl. deleted
}
$rows = $this->somain->organisations($param);
$this->total = $this->somain->total;
if (!$rows) return array();
foreach($rows as $n => $row)
{
if (strpos($row['org_name'],'&')!==false) $row['org_name'] = str_replace('&','*AND*',$row['org_name']);
$rows[$n]['id'] = 'org_name:'.$row['org_name'];
foreach(array(
'org_unit' => lang('departments'),
'adr_one_locality' => lang('locations'),
) as $by => $by_label)
{
if ($row[$by.'_count'] > 1)
{
$rows[$n][$by] = $row[$by.'_count'].' '.$by_label;
}
else
{
if (strpos($row[$by],'&')!==false) $row[$by] = str_replace('&','*AND*',$row[$by]);
$rows[$n]['id'] .= '|||'.$by.':'.$row[$by];
}
}
}
return $rows;
}
/**
* Find contacts that appear to be duplicates
*
* @param Array $param
* @param string $param[org_view] 'org_name', 'org_name,adr_one_location', 'org_name,org_unit' how to group
* @param int $param[owner] addressbook to search
* @param string $param[search] search pattern for org_name
* @param string $param[searchletter] letter the org_name need to start with
* @param int $param[start]
* @param int $param[num_rows]
* @param string $param[sort] ASC or DESC
*
* @return array of arrays
*/
public function duplicates($param)
{
if (!method_exists($this->somain,'duplicates'))
{
$this->total = 0;
return false;
}
if ($param['search'] && !is_array($param['search']))
{
$search = $param['search'];
$param['search'] = array();
if($this->somain instanceof Sql)
{
// Keep the string, let the parent deal with it
$param['search'] = $search;
}
else
{
foreach($this->columns_to_search as $col)
{
// we don't search the customfields
if ($col != 'contact_value') $param['search'][$col] = $search;
}
}
}
if (is_array($param['search']) && count($param['search']))
{
$param['search'] = $this->data2db($param['search']);
}
if(!array_key_exists('tid', $param['col_filter']) || $param['col_filter']['tid'] === '')
{
$param['col_filter'][] = $this->somain->table_name.'.contact_tid != \'' . self::DELETED_TYPE . '\'';
}
elseif(is_null($param['col_filter']['tid']))
{
// return all entries including deleted
unset($param['col_filter']['tid']);
}
if(array_key_exists('filter', $param) && $param['filter'] != '')
{
$param['owner'] = $param['filter'];
unset($param['filter']);
}
if(array_key_exists('owner', $param['col_filter']) && $param['col_filter']['owner'] != '')
{
$param['owner'] = $param['col_filter']['owner'];
unset($param['col_filter']['owner']);
}
$rows = $this->somain->duplicates($param);
$this->total = $this->somain->total;
if (!$rows) return array();
foreach($rows as $n => $row)
{
$rows[$n]['id'] = 'duplicate:';
foreach(array_keys(static::$duplicate_fields) as $by)
{
if (strpos($row[$by],'&')!==false) $row[$by] = str_replace('&','*AND*',$row[$by]);
if($row[$by])
{
$rows[$n]['id'] .= '|||'.$by.':'.$row[$by];
}
}
}
return $rows;
}
/**
* gets all contact fields from database
*
* @return array of (internal) field-names
*/
function get_contact_columns()
{
$fields = $this->get_fields('all');
foreach (array_keys((array)$this->customfields) as $cfield)
{
$fields[] = '#'.$cfield;
}
return $fields;
}
/**
* delete / move all contacts of an addressbook
*
* @param array $data
* @param int $data['account_id'] owner to change
* @param int $data['new_owner'] new owner or 0 for delete
*/
function deleteaccount($data)
{
$account_id = $data['account_id'];
$new_owner = $data['new_owner'];
if (!$new_owner)
{
$this->somain->delete(array('owner' => $account_id)); // so_sql_cf::delete() takes care of cfs too
if (method_exists($this->somain, 'get_lists') &&
($lists = $this->somain->get_lists($account_id)))
{
$this->somain->delete_list(array_keys($lists));
}
}
else
{
$this->somain->change_owner($account_id,$new_owner);
}
}
/**
* return the backend, to be used for the given $contact_id
*
* @param array|string|int $keys =null
* @param int $owner =null account_id of owner or 0 for accounts
* @return Sql|Ldap|Ads|Univention
*/
function get_backend($keys=null,$owner=null)
{
if ($owner === '') $owner = null;
$contact_id = !is_array($keys) ? $keys :
(isset($keys['id']) ? $keys['id'] : $keys['contact_id']);