addressbook.php 32 KB

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