addressbook.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. <?php
  2. /**
  3. * functions/addressbook.php - Functions and classes for the addressbook system
  4. *
  5. * Functions require SM_PATH and support of forms.php functions
  6. *
  7. * @copyright 1999-2025 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage addressbook
  12. */
  13. /**
  14. * Create and initialize an addressbook object.
  15. * @param boolean $showerr display any address book init errors. html page header
  16. * must be created before calling addressbook_init() with $showerr enabled.
  17. * @param boolean $onlylocal enable only local address book backends. Should
  18. * be used when code does not need access to remote backends. Backends
  19. * that provide read only address books with limited listing options can be
  20. * tagged as remote.
  21. * @return object address book object.
  22. */
  23. function addressbook_init($showerr = true, $onlylocal = false) {
  24. global $data_dir, $username, $ldap_server, $address_book_global_filename;
  25. global $addrbook_dsn, $addrbook_table;
  26. global $abook_global_file, $abook_global_file_writeable, $abook_global_file_listing;
  27. global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
  28. global $abook_file_line_length;
  29. /* Create a new addressbook object */
  30. $abook = new AddressBook;
  31. /* Create empty error message */
  32. $abook_init_error='';
  33. /*
  34. Always add a local backend. We use *either* file-based *or* a
  35. database addressbook. If $addrbook_dsn is set, the database
  36. backend is used. If not, addressbooks are stores in files.
  37. */
  38. if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
  39. /* Database */
  40. if (!isset($addrbook_table) || empty($addrbook_table)) {
  41. $addrbook_table = 'address';
  42. }
  43. $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
  44. 'owner' => $username,
  45. 'table' => $addrbook_table));
  46. if (!$r && $showerr) {
  47. $abook_init_error.=_("Error initializing address book database.") . "\n" . $abook->error;
  48. }
  49. } else {
  50. /* File */
  51. $filename = getHashedFile($username, $data_dir, "$username.abook");
  52. $r = $abook->add_backend('local_file', Array('filename' => $filename,
  53. 'umask' => 0077,
  54. 'line_length' => $abook_file_line_length,
  55. 'create' => true));
  56. if(!$r && $showerr) {
  57. // no need to use $abook->error, because message explains error.
  58. $abook_init_error.=sprintf( _("Error opening file %s"), $filename );
  59. }
  60. }
  61. /* Global file based addressbook */
  62. if (isset($abook_global_file) &&
  63. isset($abook_global_file_writeable) &&
  64. isset($abook_global_file_listing) &&
  65. trim($abook_global_file)!=''){
  66. // Detect place of address book
  67. if (! preg_match("/[\/\\\]/",$abook_global_file)) {
  68. /* no path chars, address book stored in data directory
  69. * make sure that there is a slash between data directory
  70. * and address book file name
  71. */
  72. $abook_global_filename=$data_dir
  73. . ((substr($data_dir, -1) != '/') ? '/' : '')
  74. . $abook_global_file;
  75. } elseif (preg_match("/^\/|\w:/",$abook_global_file)) {
  76. // full path is set in options (starts with slash or x:)
  77. $abook_global_filename=$abook_global_file;
  78. } else {
  79. $abook_global_filename=SM_PATH . $abook_global_file;
  80. }
  81. $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename,
  82. 'name' => _("Global Address Book"),
  83. 'detect_writeable' => false,
  84. 'line_length' => $abook_file_line_length,
  85. 'writeable'=> $abook_global_file_writeable,
  86. 'listing' => $abook_global_file_listing));
  87. /* global abook init error is not fatal. add error message and continue */
  88. if (!$r && $showerr) {
  89. if ($abook_init_error!='') $abook_init_error.="\n";
  90. $abook_init_error.=_("Error initializing global address book.") . "\n" . $abook->error;
  91. }
  92. }
  93. /* Load global addressbook from SQL if configured */
  94. if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) {
  95. /* Database configured */
  96. if (!isset($addrbook_global_table) || empty($addrbook_global_table)) {
  97. $addrbook_global_table = 'global_abook';
  98. }
  99. $r = $abook->add_backend('database',
  100. Array('dsn' => $addrbook_global_dsn,
  101. 'owner' => 'global',
  102. 'name' => _("Global Address Book"),
  103. 'writeable' => $addrbook_global_writeable,
  104. 'listing' => $addrbook_global_listing,
  105. 'table' => $addrbook_global_table));
  106. /* global abook init error is not fatal. add error message and continue */
  107. if (!$r && $showerr) {
  108. if ($abook_init_error!='') $abook_init_error.="\n";
  109. $abook_init_error.=_("Error initializing global address book.") . "\n" . $abook->error;
  110. }
  111. }
  112. /*
  113. * hook allows to include different address book backends.
  114. * plugins should extract $abook and $r from arguments
  115. * and use same add_backend commands as above functions.
  116. * Since 1.5.2 hook sends third ($onlylocal) argument to address book
  117. * plugins in order to allow detection of local address book init.
  118. * @since 1.5.1 and 1.4.5
  119. * Since 1.5.2, the plugin arguments are passed inside an array
  120. * and by reference, so plugins hooking in here need to accept arguments
  121. * in an array and change those values as needed instead of returning
  122. * the changed values.
  123. */
  124. $temp = array(&$abook, &$r, &$onlylocal);
  125. do_hook('abook_init', $temp);
  126. if (!$r && $showerr) {
  127. if ($abook_init_error!='') $abook_init_error.="\n";
  128. $abook_init_error.=_("Error initializing other address books.") . "\n" . $abook->error;
  129. }
  130. /* Load configured LDAP servers (if PHP has LDAP support) */
  131. if (isset($ldap_server) && is_array($ldap_server)) {
  132. reset($ldap_server);
  133. foreach ($ldap_server as $param) {
  134. if (!is_array($param))
  135. continue;
  136. /* if onlylocal is true, we only add writeable ldap servers */
  137. if ($onlylocal && (!isset($param['writeable']) || $param['writeable'] != true))
  138. continue;
  139. $r = $abook->add_backend('ldap_server', $param);
  140. if (!$r && $showerr) {
  141. if ($abook_init_error!='') $abook_init_error.="\n";
  142. $abook_init_error.=sprintf(_("Error initializing LDAP server %s:"), $param['host'])."\n";
  143. $abook_init_error.= $abook->error;
  144. }
  145. }
  146. } // end of ldap server init
  147. /**
  148. * display address book init errors.
  149. */
  150. if ($abook_init_error!='' && $showerr) {
  151. error_box(nl2br(sm_encode_html_special_chars($abook_init_error)));
  152. }
  153. /* Return the initialized object */
  154. return $abook;
  155. }
  156. /**
  157. * Constructs the "new address" form
  158. *
  159. * NOTE! The form is not closed - the caller
  160. * must add the closing form tag itself.
  161. *
  162. * @since 1.5.1
  163. *
  164. * @param string $form_url Form action url
  165. * @param string $name Form name
  166. * @param string $title Form title
  167. * @param string $button Form button name
  168. * @param int $backend The current backend being displayed
  169. * @param array $defdata Values of form fields
  170. *
  171. * @return string The desired address form display code
  172. *
  173. */
  174. function abook_create_form($form_url, $name, $title, $button,
  175. $backend, $defdata=array()) {
  176. global $oTemplate;
  177. $output = addForm($form_url, 'post', 'f_add', '', '', array(), TRUE);
  178. if ($button == _("Update address")) {
  179. $edit = true;
  180. $backends = NULL;
  181. } else {
  182. $edit = false;
  183. $backends = getWritableBackends();
  184. }
  185. $fields = array (
  186. 'nickname' => 'NickName',
  187. 'firstname' => 'FirstName',
  188. 'lastname' => 'LastName',
  189. 'email' => 'Email',
  190. 'label' => 'Info',
  191. );
  192. $values = array();
  193. foreach ($fields as $sqm=>$template) {
  194. $values[$template] = isset($defdata[$sqm]) ? $defdata[$sqm] : '';
  195. }
  196. $oTemplate->assign('writable_backends', $backends);
  197. $oTemplate->assign('values', $values);
  198. $oTemplate->assign('edit', $edit);
  199. $oTemplate->assign('current_backend', $backend);
  200. $output .= $oTemplate->fetch('addrbook_addedit.tpl');
  201. return $output;
  202. }
  203. /**
  204. * Had to move this function outside of the Addressbook Class
  205. * PHP 4.0.4 Seemed to be having problems with inline functions.
  206. * Note: this can return now since we don't support 4.0.4 anymore.
  207. */
  208. function addressbook_cmp($a,$b) {
  209. if($a['backend'] > $b['backend']) {
  210. return 1;
  211. } else if($a['backend'] < $b['backend']) {
  212. return -1;
  213. }
  214. return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
  215. }
  216. /**
  217. * Retrieve a list of writable backends
  218. * @since 1.5.2
  219. */
  220. function getWritableBackends () {
  221. global $abook;
  222. $write = array();
  223. $backends = $abook->get_backend_list();
  224. foreach ($backends as $v) {
  225. if ($v->writeable) {
  226. $write[$v->bnum]=$v->sname;
  227. }
  228. }
  229. return $write;
  230. }
  231. /**
  232. * Sort array by the key "name"
  233. */
  234. function alistcmp($a,$b) {
  235. $abook_sort_order=get_abook_sort();
  236. switch ($abook_sort_order) {
  237. case 0:
  238. case 1:
  239. $abook_sort='nickname';
  240. break;
  241. case 4:
  242. case 5:
  243. $abook_sort='email';
  244. break;
  245. case 6:
  246. case 7:
  247. $abook_sort='label';
  248. break;
  249. case 2:
  250. case 3:
  251. case 8:
  252. default:
  253. $abook_sort='name';
  254. }
  255. if ($a['backend'] > $b['backend']) {
  256. return 1;
  257. } else {
  258. if ($a['backend'] < $b['backend']) {
  259. return -1;
  260. }
  261. }
  262. if( (($abook_sort_order+2) % 2) == 1) {
  263. return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
  264. } else {
  265. return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
  266. }
  267. }
  268. /**
  269. * Address book sorting options
  270. *
  271. * returns address book sorting order
  272. * @return integer book sorting options order
  273. */
  274. function get_abook_sort() {
  275. global $data_dir, $username;
  276. /* get sorting order */
  277. if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
  278. $abook_sort_order = (int) $temp;
  279. if ($abook_sort_order < 0 or $abook_sort_order > 8)
  280. $abook_sort_order=8;
  281. setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
  282. } else {
  283. /* get previous sorting options. default to unsorted */
  284. $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
  285. }
  286. return $abook_sort_order;
  287. }
  288. /**
  289. * This function shows the address book sort button.
  290. *
  291. * @param integer $abook_sort_order Current sort value
  292. * @param string $alt_tag The alt tag value (string
  293. * visible to text only browsers)
  294. * @param integer $Down Sort value when list is sorted
  295. * ascending
  296. * @param integer $Up Sort value when list is sorted
  297. * descending
  298. * @param array $uri_extra Any additional parameters to add
  299. * to the button's link, as an
  300. * associative array of key/value pairs
  301. * (OPTIONAL; default none)
  302. *
  303. * @return string html code with sorting images and urls
  304. *
  305. */
  306. function show_abook_sort_button($abook_sort_order, $alt_tag,
  307. $Down, $Up, $uri_extra=array() ) {
  308. global $form_url, $icon_theme_path;
  309. /* Figure out which image we want to use. */
  310. if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
  311. $img = 'sort_none.png';
  312. $text_icon = '&#9723;'; // U+25FB WHITE MEDIUM SQUARE
  313. $which = $Up;
  314. } elseif ($abook_sort_order == $Up) {
  315. $img = 'up_pointer.png';
  316. $text_icon = '&#8679;'; // U+21E7 UPWARDS WHITE ARROW
  317. $which = $Down;
  318. } else {
  319. $img = 'down_pointer.png';
  320. $text_icon = '&#8681;'; // U+21E9 DOWNWARDS WHITE ARROW
  321. $which = 8;
  322. }
  323. $uri_extra['abook_sort_order'] = $which;
  324. $uri = set_uri_vars($form_url, $uri_extra, FALSE);
  325. /* Now that we have everything figured out, show the actual button. */
  326. return create_hyperlink($uri,
  327. getIcon($icon_theme_path, $img, $text_icon, $alt_tag),
  328. '', '', '', '', '',
  329. array('style' => 'text-decoration:none',
  330. 'title' => $alt_tag),
  331. FALSE);
  332. }
  333. /**
  334. * This is the main address book class that connect all the
  335. * backends and provide services to the functions above.
  336. * @package squirrelmail
  337. * @subpackage addressbook
  338. */
  339. class AddressBook {
  340. /**
  341. * Enabled address book backends
  342. * @var array
  343. */
  344. var $backends = array();
  345. /**
  346. * Number of enabled backends
  347. * @var integer
  348. */
  349. var $numbackends = 0;
  350. /**
  351. * Error messages
  352. * @var string
  353. */
  354. var $error = '';
  355. /**
  356. * id of backend with personal address book
  357. * @var integer
  358. */
  359. var $localbackend = 0;
  360. /**
  361. * Name of backend with personal address book
  362. * @var string
  363. */
  364. var $localbackendname = '';
  365. /**
  366. * Controls use of 'extra' field
  367. *
  368. * Extra field can be used to add link to form, which allows
  369. * to modify all fields supported by backend. This is the only field
  370. * that is not sanitized with sm_encode_html_special_chars. Backends MUST make
  371. * sure that field data is sanitized and displayed correctly inside
  372. * table cell. Use of html formating in other address book fields is
  373. * not allowed. Backends that don't return 'extra' row in address book
  374. * data should not modify this object property.
  375. * @var boolean
  376. * @since 1.5.1
  377. */
  378. var $add_extra_field = false;
  379. /**
  380. * Constructor (PHP5 style, required in some future version of PHP)
  381. */
  382. function __construct() {
  383. $this->localbackendname = _("Personal Address Book");
  384. }
  385. /**
  386. * Constructor (PHP4 style, kept for compatibility reasons)
  387. */
  388. function AddressBook() {
  389. self::__construct();
  390. }
  391. /**
  392. * Return an array of backends of a given type,
  393. * or all backends if no type is specified.
  394. * @param string $type backend type
  395. * @return array list of backends
  396. */
  397. function get_backend_list($type = '') {
  398. $ret = array();
  399. for ($i = 1 ; $i <= $this->numbackends ; $i++) {
  400. if (empty($type) || $type == $this->backends[$i]->btype) {
  401. $ret[] = &$this->backends[$i];
  402. }
  403. }
  404. return $ret;
  405. }
  406. /* ========================== Public ======================== */
  407. /**
  408. * Add a new backend.
  409. *
  410. * @param string $backend backend name (without the abook_ prefix)
  411. * @param mixed optional variable that is passed to the backend constructor.
  412. * See each of the backend classes for valid parameters
  413. * @return integer number of backends
  414. */
  415. function add_backend($backend, $param = '') {
  416. static $backend_classes;
  417. if (!isset($backend_classes)) {
  418. $backend_classes = array();
  419. }
  420. if (!isset($backend_classes[$backend])) {
  421. /**
  422. * Support backend provided by plugins. Plugin function must
  423. * return an associative array with as key the backend name ($backend)
  424. * and as value the file including the path containing the backend class.
  425. * i.e.: $aBackend = array('backend_template' => SM_PATH . 'plugins/abook_backend_template/functions.php')
  426. *
  427. * NB: Because the backend files are included from within this function they DO NOT have access to
  428. * vars in the global scope. This function is the global scope for the included backend !!!
  429. */
  430. global $null;
  431. $aBackend = do_hook('abook_add_class', $null);
  432. if (isset($aBackend) && is_array($aBackend) && isset($aBackend[$backend])) {
  433. require_once($aBackend[$backend]);
  434. } else {
  435. require_once(SM_PATH . 'functions/abook_'.$backend.'.php');
  436. }
  437. $backend_classes[$backend] = true;
  438. }
  439. $backend_name = 'abook_' . $backend;
  440. $newback = new $backend_name($param);
  441. //eval('$newback = new ' . $backend_name . '($param);');
  442. if(!empty($newback->error)) {
  443. $this->error = $newback->error;
  444. return false;
  445. }
  446. $this->numbackends++;
  447. $newback->bnum = $this->numbackends;
  448. $this->backends[$this->numbackends] = $newback;
  449. /* Store ID of first local backend added */
  450. if ($this->localbackend == 0 && $newback->btype == 'local') {
  451. $this->localbackend = $this->numbackends;
  452. $this->localbackendname = $newback->sname;
  453. }
  454. return $this->numbackends;
  455. }
  456. /**
  457. * create string with name and email address
  458. *
  459. * This function takes a $row array as returned by the addressbook
  460. * search and returns an e-mail address with the full name or
  461. * nickname optionally prepended.
  462. * @param array $row address book entry
  463. * @return string email address with real name prepended
  464. */
  465. static function full_address($row) {
  466. global $data_dir, $username, $addrsrch_fullname;
  467. // allow multiple addresses in one row (poor person's grouping - bah)
  468. // (separate with commas)
  469. //
  470. $return = '';
  471. $addresses = explode(',', $row['email']);
  472. foreach ($addresses as $address) {
  473. if (!empty($return)) $return .= ', ';
  474. if ($addrsrch_fullname == 'fullname')
  475. $return .= '"' . $row['name'] . '" <' . trim($address) . '>';
  476. else if ($addrsrch_fullname == 'nickname')
  477. $return .= '"' . $row['nickname'] . '" <' . trim($address) . '>';
  478. else // "noprefix"
  479. $return .= trim($address);
  480. }
  481. return $return;
  482. }
  483. /**
  484. * Search for entries in address books
  485. *
  486. * Return a list of addresses matching expression in
  487. * all backends of a given type.
  488. * @param string $expression search expression
  489. * @param integer $bnum backend number. default to search in all backends
  490. * @return array search results
  491. */
  492. function search($expression, $bnum = -1) {
  493. $ret = array();
  494. $this->error = '';
  495. /* Search all backends */
  496. if ($bnum == -1) {
  497. $sel = $this->get_backend_list('');
  498. $failed = 0;
  499. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  500. $backend = &$sel[$i];
  501. $backend->error = '';
  502. $res = $backend->search($expression);
  503. if (is_array($res)) {
  504. $ret = array_merge($ret, $res);
  505. } else {
  506. $this->error .= "\n" . $backend->error;
  507. $failed++;
  508. }
  509. }
  510. /* Only fail if all backends failed */
  511. if( $failed >= sizeof( $sel ) ) {
  512. $ret = FALSE;
  513. }
  514. } elseif (! isset($this->backends[$bnum])) {
  515. /* make sure that backend exists */
  516. $this->error = _("Unknown address book backend");
  517. $ret = false;
  518. } else {
  519. /* Search only one backend */
  520. $ret = $this->backends[$bnum]->search($expression);
  521. if (!is_array($ret)) {
  522. $this->error .= "\n" . $this->backends[$bnum]->error;
  523. $ret = FALSE;
  524. }
  525. }
  526. return( $ret );
  527. }
  528. /**
  529. * Sorted search
  530. * @param string $expression search expression
  531. * @param integer $bnum backend number. default to search in all backends
  532. * @return array search results
  533. */
  534. function s_search($expression, $bnum = -1) {
  535. $ret = $this->search($expression, $bnum);
  536. if ( is_array( $ret ) ) {
  537. usort($ret, 'addressbook_cmp');
  538. }
  539. return $ret;
  540. }
  541. /**
  542. * Lookup an address by the indicated field.
  543. *
  544. * Only possible in local backends.
  545. *
  546. * @param string $value The value to look up
  547. * @param integer $bnum The number of the backend to
  548. * look within (OPTIONAL; defaults
  549. * to look in all local backends)
  550. * @param integer $field The field to look in, should be one
  551. * of the SM_ABOOK_FIELD_* constants
  552. * defined in include/constants.php
  553. * (OPTIONAL; defaults to nickname field)
  554. * NOTE: uniqueness is only guaranteed
  555. * when the nickname field is used here;
  556. * otherwise, the first matching address
  557. * is returned.
  558. *
  559. * @return mixed Array with lookup results when the value
  560. * was found, an empty array if the value was
  561. * not found, or false if an error occured.
  562. *
  563. */
  564. function lookup($value, $bnum = -1, $field = SM_ABOOK_FIELD_NICKNAME) {
  565. $ret = array();
  566. if ($bnum > -1) {
  567. if (!isset($this->backends[$bnum])) {
  568. $this->error = _("Unknown address book backend");
  569. return false;
  570. }
  571. $res = $this->backends[$bnum]->lookup($value, $field);
  572. if (is_array($res)) {
  573. return $res;
  574. } else {
  575. $this->error = $this->backends[$bnum]->error;
  576. return false;
  577. }
  578. }
  579. $sel = $this->get_backend_list('local');
  580. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  581. $backend = &$sel[$i];
  582. $backend->error = '';
  583. $res = $backend->lookup($value, $field);
  584. // return an address if one is found
  585. // (empty array means lookup concluded
  586. // but no result found - in this case,
  587. // proceed to next backend)
  588. //
  589. if (is_array($res)) {
  590. if (!empty($res)) return $res;
  591. } else {
  592. $this->error = $backend->error;
  593. return false;
  594. }
  595. }
  596. return $ret;
  597. }
  598. /**
  599. * Return all addresses
  600. * @param integer $bnum backend number
  601. * @return mixed array with search results or boolean false on error.
  602. */
  603. function list_addr($bnum = -1) {
  604. $ret = array();
  605. if ($bnum == -1) {
  606. $sel = $this->get_backend_list('');
  607. } elseif (! isset($this->backends[$bnum])) {
  608. /* make sure that backend exists */
  609. $this->error = _("Unknown address book backend");
  610. $ret = false;
  611. } else {
  612. $sel = array(0 => &$this->backends[$bnum]);
  613. }
  614. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  615. $backend = &$sel[$i];
  616. $backend->error = '';
  617. $res = $backend->list_addr();
  618. if (is_array($res)) {
  619. $ret = array_merge($ret, $res);
  620. } else {
  621. $this->error = $backend->error;
  622. return false;
  623. }
  624. }
  625. return $ret;
  626. }
  627. /**
  628. * Create a new address
  629. * @param array $userdata added address record
  630. * @param integer $bnum backend number
  631. * @return integer the backend number that the/ address was added
  632. * to, or false if it failed.
  633. */
  634. function add($userdata, $bnum) {
  635. /* Validate data */
  636. if (!is_array($userdata)) {
  637. $this->error = _("Invalid input data");
  638. return false;
  639. }
  640. if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
  641. $this->error = _("Name is missing");
  642. return false;
  643. }
  644. if (empty($userdata['email'])) {
  645. $this->error = _("E-mail address is missing");
  646. return false;
  647. }
  648. if (empty($userdata['nickname'])) {
  649. $userdata['nickname'] = $userdata['email'];
  650. }
  651. /* Blocks use of space, :, |, #, " and ! in nickname */
  652. if (preg_match('/[ :|#"!]/', $userdata['nickname'])) {
  653. $this->error = _("Nickname contains illegal characters");
  654. return false;
  655. }
  656. /* make sure that backend exists */
  657. if (! isset($this->backends[$bnum])) {
  658. $this->error = _("Unknown address book backend");
  659. return false;
  660. }
  661. /* Check that specified backend accept new entries */
  662. if (!$this->backends[$bnum]->writeable) {
  663. $this->error = _("Address book is read-only");
  664. return false;
  665. }
  666. /* Add address to backend */
  667. $res = $this->backends[$bnum]->add($userdata);
  668. if ($res) {
  669. return $bnum;
  670. } else {
  671. $this->error = $this->backends[$bnum]->error;
  672. return false;
  673. }
  674. return false; // Not reached
  675. } /* end of add() */
  676. /**
  677. * Remove the entries from address book
  678. * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
  679. * @param integer $bnum backend number
  680. * @return bool true if removed successfully. false if there s an error. $this->error contains error message
  681. */
  682. function remove($alias, $bnum) {
  683. /* Check input */
  684. if (empty($alias)) {
  685. return true;
  686. }
  687. /* Convert string to single element array */
  688. if (!is_array($alias)) {
  689. $alias = array(0 => $alias);
  690. }
  691. /* make sure that backend exists */
  692. if (! isset($this->backends[$bnum])) {
  693. $this->error = _("Unknown address book backend");
  694. return false;
  695. }
  696. /* Check that specified backend is writable */
  697. if (!$this->backends[$bnum]->writeable) {
  698. $this->error = _("Address book is read-only");
  699. return false;
  700. }
  701. /* Remove user from backend */
  702. $res = $this->backends[$bnum]->remove($alias);
  703. if ($res) {
  704. return $bnum;
  705. } else {
  706. $this->error = $this->backends[$bnum]->error;
  707. return false;
  708. }
  709. return FALSE; /* Not reached */
  710. } /* end of remove() */
  711. /**
  712. * Modify entry in address book
  713. * @param string $alias nickname
  714. * @param array $userdata newdata
  715. * @param integer $bnum backend number
  716. */
  717. function modify($alias, $userdata, $bnum) {
  718. /* Check input */
  719. if (empty($alias) || !is_string($alias)) {
  720. return true;
  721. }
  722. /* Validate data */
  723. if(!is_array($userdata)) {
  724. $this->error = _("Invalid input data");
  725. return false;
  726. }
  727. if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
  728. $this->error = _("Name is missing");
  729. return false;
  730. }
  731. if (empty($userdata['email'])) {
  732. $this->error = _("E-mail address is missing");
  733. return false;
  734. }
  735. if (preg_match('/[: |#"!]/', $userdata['nickname'])) {
  736. $this->error = _("Nickname contains illegal characters");
  737. return false;
  738. }
  739. if (empty($userdata['nickname'])) {
  740. $userdata['nickname'] = $userdata['email'];
  741. }
  742. /* make sure that backend exists */
  743. if (! isset($this->backends[$bnum])) {
  744. $this->error = _("Unknown address book backend");
  745. return false;
  746. }
  747. /* Check that specified backend is writable */
  748. if (!$this->backends[$bnum]->writeable) {
  749. $this->error = _("Address book is read-only");;
  750. return false;
  751. }
  752. /* Modify user in backend */
  753. $res = $this->backends[$bnum]->modify($alias, $userdata);
  754. if ($res) {
  755. return $bnum;
  756. } else {
  757. $this->error = $this->backends[$bnum]->error;
  758. return false;
  759. }
  760. return FALSE; /* Not reached */
  761. } /* end of modify() */
  762. } /* End of class Addressbook */
  763. /**
  764. * Generic backend that all other backends extend
  765. * @package squirrelmail
  766. * @subpackage addressbook
  767. */
  768. class addressbook_backend {
  769. /* Variables that all backends must provide. */
  770. /**
  771. * Backend type
  772. *
  773. * Can be 'local' or 'remote'
  774. * @var string backend type
  775. */
  776. var $btype = 'dummy';
  777. /**
  778. * Internal backend name
  779. * @var string
  780. */
  781. var $bname = 'dummy';
  782. /**
  783. * Displayed backend name
  784. * @var string
  785. */
  786. var $sname = 'Dummy backend';
  787. /*
  788. * Variables common for all backends, but that
  789. * should not be changed by the backends.
  790. */
  791. /**
  792. * Backend number
  793. * @var integer
  794. */
  795. var $bnum = -1;
  796. /**
  797. * Error messages
  798. * @var string
  799. */
  800. var $error = '';
  801. /**
  802. * Writeable flag
  803. * @var bool
  804. */
  805. var $writeable = false;
  806. /**
  807. * Set error message
  808. * @param string $string error message
  809. * @return bool
  810. */
  811. function set_error($string) {
  812. $this->error = '[' . $this->sname . '] ' . $string;
  813. return false;
  814. }
  815. /* ========================== Public ======================== */
  816. /**
  817. * Search for entries in backend
  818. *
  819. * Working backend should support use of wildcards. * symbol
  820. * should match one or more symbols. ? symbol should match any
  821. * single symbol.
  822. * @param string $expression
  823. * @return bool
  824. */
  825. function search($expression) {
  826. $this->set_error('search is not implemented');
  827. return false;
  828. }
  829. /**
  830. * Find entry in backend by the indicated field
  831. *
  832. * @param string $value The value to look up
  833. * @param integer $field The field to look in, should be one
  834. * of the SM_ABOOK_FIELD_* constants
  835. * defined in include/constants.php
  836. * NOTE: uniqueness is only guaranteed
  837. * when the nickname field is used here;
  838. * otherwise, the first matching address
  839. * is returned.
  840. *
  841. * @return mixed Array with lookup results when the value
  842. * was found, an empty array if the value was
  843. * not found, or false if an error occured.
  844. *
  845. */
  846. function lookup($value, $field=SM_ABOOK_FIELD_NICKNAME) {
  847. $this->set_error('lookup is not implemented');
  848. return false;
  849. }
  850. /**
  851. * List all entries in backend
  852. *
  853. * Working backend should provide this function or at least
  854. * dummy function that returns empty array.
  855. * @return bool
  856. */
  857. function list_addr() {
  858. $this->set_error('list_addr is not implemented');
  859. return false;
  860. }
  861. /**
  862. * Add entry to backend
  863. * @param array userdata
  864. * @return bool
  865. */
  866. function add($userdata) {
  867. $this->set_error('add is not implemented');
  868. return false;
  869. }
  870. /**
  871. * Remove entry from backend
  872. * @param string $alias name used for id
  873. * @return bool
  874. */
  875. function remove($alias) {
  876. $this->set_error('delete is not implemented');
  877. return false;
  878. }
  879. /**
  880. * Modify entry in backend
  881. * @param string $alias name used for id
  882. * @param array $newuserdata new data
  883. * @return bool
  884. */
  885. function modify($alias, $newuserdata) {
  886. $this->set_error('modify is not implemented');
  887. return false;
  888. }
  889. /**
  890. * Creates full name from given name and surname
  891. *
  892. * Handles name order differences. Function always runs in SquirrelMail gettext domain.
  893. * Plugins don't have to switch domains before calling this function.
  894. * @param string $firstname given name
  895. * @param string $lastname surname
  896. * @return string full name
  897. * @since 1.5.2
  898. */
  899. function fullname($firstname,$lastname) {
  900. // i18n: allows to control fullname layout in address book listing
  901. // first %s is for first name, second %s is for last name.
  902. // Translate it to '%2$s %1$s', if surname must be displayed first in your language.
  903. // Please note that variables can be set to empty string and extra formating
  904. // (for example '%2$s, %1$s' as in 'Smith, John') might break. Use it only for
  905. // setting name and surname order. scripts will remove all prepended and appended
  906. // whitespace.
  907. return trim(sprintf(dgettext('squirrelmail',"%s %s"),$firstname,$lastname));
  908. }
  909. }