addressbook.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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-2006 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. * If SM_PATH isn't defined, define it. Required to include files.
  15. * @ignore
  16. */
  17. if (!defined('SM_PATH')) {
  18. define('SM_PATH','../');
  19. }
  20. /* make sure that display_messages.php is loaded */
  21. include_once(SM_PATH . 'functions/display_messages.php');
  22. global $addrbook_dsn, $addrbook_global_dsn;
  23. /**
  24. * Create and initialize an addressbook object.
  25. * @param boolean $showerr display any address book init errors. html page header
  26. * must be created before calling addressbook_init() with $showerr enabled.
  27. * @param boolean $onlylocal enable only local address book backends
  28. * @return object address book object.
  29. */
  30. function addressbook_init($showerr = true, $onlylocal = false) {
  31. global $data_dir, $username, $color, $ldap_server, $address_book_global_filename;
  32. global $addrbook_dsn, $addrbook_table;
  33. global $abook_global_file, $abook_global_file_writeable, $abook_global_file_listing;
  34. global $addrbook_global_dsn, $addrbook_global_table, $addrbook_global_writeable, $addrbook_global_listing;
  35. /* Create a new addressbook object */
  36. $abook = new AddressBook;
  37. /* Create empty error message */
  38. $abook_init_error='';
  39. /*
  40. Always add a local backend. We use *either* file-based *or* a
  41. database addressbook. If $addrbook_dsn is set, the database
  42. backend is used. If not, addressbooks are stores in files.
  43. */
  44. if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
  45. /* Database */
  46. if (!isset($addrbook_table) || empty($addrbook_table)) {
  47. $addrbook_table = 'address';
  48. }
  49. $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
  50. 'owner' => $username,
  51. 'table' => $addrbook_table));
  52. if (!$r && $showerr) {
  53. $abook_init_error.=_("Error initializing addressbook database.") . "<br />\n" . $abook->error;
  54. }
  55. } else {
  56. /* File */
  57. $filename = getHashedFile($username, $data_dir, "$username.abook");
  58. $r = $abook->add_backend('local_file', Array('filename' => $filename,
  59. 'create' => true));
  60. if(!$r && $showerr) {
  61. // no need to use $abook->error, because message explains error.
  62. $abook_init_error.=sprintf( _("Error opening file %s"), $filename );
  63. }
  64. }
  65. /* Global file based addressbook */
  66. if (isset($abook_global_file) &&
  67. isset($abook_global_file_writeable) &&
  68. isset($abook_global_file_listing) &&
  69. trim($abook_global_file)!=''){
  70. // Detect place of address book
  71. if (! preg_match("/[\/\\\]/",$abook_global_file)) {
  72. /* no path chars, address book stored in data directory
  73. * make sure that there is a slash between data directory
  74. * and address book file name
  75. */
  76. $abook_global_filename=$data_dir
  77. . ((substr($data_dir, -1) != '/') ? '/' : '')
  78. . $abook_global_file;
  79. } elseif (preg_match("/^\/|\w:/",$abook_global_file)) {
  80. // full path is set in options (starts with slash or x:)
  81. $abook_global_filename=$abook_global_file;
  82. } else {
  83. $abook_global_filename=SM_PATH . $abook_global_file;
  84. }
  85. $r = $abook->add_backend('local_file',array('filename'=>$abook_global_filename,
  86. 'name' => _("Global address book"),
  87. 'detect_writeable' => false,
  88. 'writeable'=> $abook_global_file_writeable,
  89. 'listing' => $abook_global_file_listing));
  90. /* global abook init error is not fatal. add error message and continue */
  91. if (!$r && $showerr) {
  92. if ($abook_init_error!='') $abook_init_error.="<br />\n";
  93. $abook_init_error.=_("Error initializing global addressbook.") . "<br />\n" . $abook->error;
  94. }
  95. }
  96. /* Load global addressbook from SQL if configured */
  97. if (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn)) {
  98. /* Database configured */
  99. if (!isset($addrbook_global_table) || empty($addrbook_global_table)) {
  100. $addrbook_global_table = 'global_abook';
  101. }
  102. $r = $abook->add_backend('database',
  103. Array('dsn' => $addrbook_global_dsn,
  104. 'owner' => 'global',
  105. 'name' => _("Global address book"),
  106. 'writeable' => $addrbook_global_writeable,
  107. 'listing' => $addrbook_global_listing,
  108. 'table' => $addrbook_global_table));
  109. /* global abook init error is not fatal. add error message and continue */
  110. if (!$r && $showerr) {
  111. if ($abook_init_error!='') $abook_init_error.="<br />\n";
  112. $abook_init_error.=_("Error initializing global addressbook.") . "<br />\n" . $abook->error;
  113. }
  114. }
  115. /*
  116. * hook allows to include different address book backends.
  117. * plugins should extract $abook and $r from arguments
  118. * and use same add_backend commands as above functions.
  119. * @since 1.5.1 and 1.4.5
  120. */
  121. $hookReturn = do_hook('abook_init', $abook, $r);
  122. $abook = $hookReturn[1];
  123. $r = $hookReturn[2];
  124. if (! $onlylocal) {
  125. /* Load configured LDAP servers (if PHP has LDAP support) */
  126. if (isset($ldap_server) && is_array($ldap_server)) {
  127. reset($ldap_server);
  128. while (list($undef,$param) = each($ldap_server)) {
  129. if (is_array($param)) {
  130. $r = $abook->add_backend('ldap_server', $param);
  131. if (!$r && $showerr) {
  132. if ($abook_init_error!='') $abook_init_error.="<br />\n";
  133. $abook_init_error.=sprintf(_("Error initializing LDAP server %s:") .
  134. "<br />\n", $param['host']);
  135. $abook_init_error.= $abook->error;
  136. }
  137. }
  138. }
  139. } // end of ldap server init
  140. } // end of remote abook backend init
  141. /**
  142. * display address book init errors.
  143. */
  144. if ($abook_init_error!='' && $showerr) {
  145. error_box($abook_init_error,$color);
  146. }
  147. /* Return the initialized object */
  148. return $abook;
  149. }
  150. /**
  151. * Display the "new address" form
  152. *
  153. * Form is not closed and you must add closing form tag.
  154. * @since 1.5.1
  155. * @param string $form_url form action url
  156. * @param string $name form name
  157. * @param string $title form title
  158. * @param string $button form button name
  159. * @param array $defdata values of form fields
  160. */
  161. function abook_create_form($form_url,$name,$title,$button,$defdata=array()) {
  162. global $color;
  163. echo addForm($form_url, 'post', 'f_add').
  164. html_tag( 'table',
  165. html_tag( 'tr',
  166. html_tag( 'td', "\n". '<strong>' . $title . '</strong>' . "\n",
  167. 'center', $color[0]
  168. )
  169. )
  170. , 'center', '', 'width="90%"' ) ."\n";
  171. address_form($name, $button, $defdata);
  172. }
  173. /**
  174. * Had to move this function outside of the Addressbook Class
  175. * PHP 4.0.4 Seemed to be having problems with inline functions.
  176. * Note: this can return now since we don't support 4.0.4 anymore.
  177. */
  178. function addressbook_cmp($a,$b) {
  179. if($a['backend'] > $b['backend']) {
  180. return 1;
  181. } else if($a['backend'] < $b['backend']) {
  182. return -1;
  183. }
  184. return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
  185. }
  186. /**
  187. * Make an input field
  188. * @param string $label
  189. * @param string $field
  190. * @param string $name
  191. * @param string $size
  192. * @param array $values
  193. * @param string $add
  194. */
  195. function addressbook_inp_field($label, $field, $name, $size, $values, $add='') {
  196. global $color;
  197. $value = ( isset($values[$field]) ? $values[$field] : '');
  198. if (is_array($value)) {
  199. $td_str = addSelect($name.'['.$field.']', $value);
  200. } else {
  201. $td_str = addInput($name.'['.$field.']', $value, $size);
  202. }
  203. $td_str .= $add ;
  204. return html_tag( 'tr' ,
  205. html_tag( 'td', $label . ':', 'right', $color[4]) .
  206. html_tag( 'td', $td_str, 'left', $color[4])
  207. )
  208. . "\n";
  209. }
  210. /**
  211. * Output form to add and modify address data
  212. */
  213. function address_form($name, $submittext, $values = array()) {
  214. global $color, $squirrelmail_language;
  215. if ($squirrelmail_language == 'ja_JP') {
  216. echo html_tag( 'table',
  217. addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
  218. ' <small>' . _("Must be unique") . '</small>') .
  219. addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
  220. addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
  221. addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
  222. addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
  223. list_writable_backends($name) .
  224. html_tag( 'tr',
  225. html_tag( 'td',
  226. addSubmit($submittext, $name.'[SUBMIT]'),
  227. 'center', $color[4], 'colspan="2"')
  228. )
  229. , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
  230. } else {
  231. echo html_tag( 'table',
  232. addressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
  233. ' <small>' . _("Must be unique") . '</small>') .
  234. addressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
  235. addressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
  236. addressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
  237. addressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
  238. list_writable_backends($name) .
  239. html_tag( 'tr',
  240. html_tag( 'td',
  241. addSubmit($submittext, $name.'[SUBMIT]') ,
  242. 'center', $color[4], 'colspan="2"')
  243. )
  244. , 'center', '', 'border="0" cellpadding="1" width="90%"') ."\n";
  245. }
  246. }
  247. /**
  248. * Provides list of writeable backends.
  249. * Works only when address is added ($name='addaddr')
  250. * @param string $name name of form
  251. * @return string html formated backend field (select or hidden)
  252. */
  253. function list_writable_backends($name) {
  254. global $color, $abook;
  255. if ( $name != 'addaddr' ) { return; }
  256. $writeable_abook = 1;
  257. if ( $abook->numbackends > 1 ) {
  258. $backends = $abook->get_backend_list();
  259. $writeable_abooks=array();
  260. while (list($undef,$v) = each($backends)) {
  261. if ($v->writeable) {
  262. // add each backend to array
  263. $writeable_abooks[$v->bnum]=$v->sname;
  264. // save backend number
  265. $writeable_abook=$v->bnum;
  266. }
  267. }
  268. if (count($writeable_abooks)>1) {
  269. // we have more than one writeable backend
  270. $ret=addSelect('backend',$writeable_abooks,null,true);
  271. return html_tag( 'tr',
  272. html_tag( 'td', _("Add to:"),'right', $color[4] ) .
  273. html_tag( 'td', $ret, 'left', $color[4] )) . "\n";
  274. }
  275. }
  276. // Only one backend exists or is writeable.
  277. return html_tag( 'tr',
  278. html_tag( 'td',
  279. addHidden('backend', $writeable_abook),
  280. 'center', $color[4], 'colspan="2"')) . "\n";
  281. }
  282. /**
  283. * Sort array by the key "name"
  284. */
  285. function alistcmp($a,$b) {
  286. $abook_sort_order=get_abook_sort();
  287. switch ($abook_sort_order) {
  288. case 0:
  289. case 1:
  290. $abook_sort='nickname';
  291. break;
  292. case 4:
  293. case 5:
  294. $abook_sort='email';
  295. break;
  296. case 6:
  297. case 7:
  298. $abook_sort='label';
  299. break;
  300. case 2:
  301. case 3:
  302. case 8:
  303. default:
  304. $abook_sort='name';
  305. }
  306. if ($a['backend'] > $b['backend']) {
  307. return 1;
  308. } else {
  309. if ($a['backend'] < $b['backend']) {
  310. return -1;
  311. }
  312. }
  313. if( (($abook_sort_order+2) % 2) == 1) {
  314. return (strtolower($a[$abook_sort]) < strtolower($b[$abook_sort])) ? 1 : -1;
  315. } else {
  316. return (strtolower($a[$abook_sort]) > strtolower($b[$abook_sort])) ? 1 : -1;
  317. }
  318. }
  319. /**
  320. * Address book sorting options
  321. *
  322. * returns address book sorting order
  323. * @return integer book sorting options order
  324. */
  325. function get_abook_sort() {
  326. global $data_dir, $username;
  327. /* get sorting order */
  328. if(sqgetGlobalVar('abook_sort_order', $temp, SQ_GET)) {
  329. $abook_sort_order = (int) $temp;
  330. if ($abook_sort_order < 0 or $abook_sort_order > 8)
  331. $abook_sort_order=8;
  332. setPref($data_dir, $username, 'abook_sort_order', $abook_sort_order);
  333. } else {
  334. /* get previous sorting options. default to unsorted */
  335. $abook_sort_order = getPref($data_dir, $username, 'abook_sort_order', 8);
  336. }
  337. return $abook_sort_order;
  338. }
  339. /**
  340. * This function shows the address book sort button.
  341. *
  342. * @param integer $abook_sort_order current sort value
  343. * @param string $alt_tag alt tag value (string visible to text only browsers)
  344. * @param integer $Down sort value when list is sorted ascending
  345. * @param integer $Up sort value when list is sorted descending
  346. * @return string html code with sorting images and urls
  347. */
  348. function show_abook_sort_button($abook_sort_order, $alt_tag, $Down, $Up ) {
  349. global $form_url;
  350. /* Figure out which image we want to use. */
  351. if ($abook_sort_order != $Up && $abook_sort_order != $Down) {
  352. $img = 'sort_none.png';
  353. $which = $Up;
  354. } elseif ($abook_sort_order == $Up) {
  355. $img = 'up_pointer.png';
  356. $which = $Down;
  357. } else {
  358. $img = 'down_pointer.png';
  359. $which = 8;
  360. }
  361. /* Now that we have everything figured out, show the actual button. */
  362. return ' <a href="' . $form_url .'?abook_sort_order=' . $which
  363. . '"><img src="../images/' . $img
  364. . '" border="0" width="12" height="10" alt="' . $alt_tag . '" title="'
  365. . _("Click here to change the sorting of the address list") .'" /></a>';
  366. }
  367. /**
  368. * This is the main address book class that connect all the
  369. * backends and provide services to the functions above.
  370. * @package squirrelmail
  371. * @subpackage addressbook
  372. */
  373. class AddressBook {
  374. /*
  375. Cleaning errors from html with htmlspecialchars:
  376. Errors from the backend are cleaned up in this class because we not always
  377. have control over it when error output is generated in the backend.
  378. If this appears to be wrong place then clean it up at the source (the backend)
  379. */
  380. /**
  381. * Enabled address book backends
  382. * @var array
  383. */
  384. var $backends = array();
  385. /**
  386. * Number of enabled backends
  387. * @var integer
  388. */
  389. var $numbackends = 0;
  390. /**
  391. * Error messages
  392. * @var string
  393. */
  394. var $error = '';
  395. /**
  396. * id of backend with personal address book
  397. * @var integer
  398. */
  399. var $localbackend = 0;
  400. /**
  401. * Name of backend with personal address book
  402. * @var string
  403. */
  404. var $localbackendname = '';
  405. /**
  406. * Controls use of 'extra' field
  407. *
  408. * Extra field can be used to add link to form, which allows
  409. * to modify all fields supported by backend. This is the only field
  410. * that is not sanitized with htmlspecialchars. Backends MUST make
  411. * sure that field data is sanitized and displayed correctly inside
  412. * table cell. Use of html formating in other address book fields is
  413. * not allowed. Backends that don't return 'extra' row in address book
  414. * data should not modify this object property.
  415. * @var boolean
  416. * @since 1.5.1
  417. */
  418. var $add_extra_field = false;
  419. /**
  420. * Constructor function.
  421. */
  422. function AddressBook() {
  423. $this->localbackendname = _("Personal address book");
  424. }
  425. /**
  426. * Return an array of backends of a given type,
  427. * or all backends if no type is specified.
  428. * @param string $type backend type
  429. * @return array list of backends
  430. */
  431. function get_backend_list($type = '') {
  432. $ret = array();
  433. for ($i = 1 ; $i <= $this->numbackends ; $i++) {
  434. if (empty($type) || $type == $this->backends[$i]->btype) {
  435. $ret[] = &$this->backends[$i];
  436. }
  437. }
  438. return $ret;
  439. }
  440. /* ========================== Public ======================== */
  441. /**
  442. * Add a new backend.
  443. *
  444. * @param string $backend backend name (without the abook_ prefix)
  445. * @param mixed optional variable that is passed to the backend constructor.
  446. * See each of the backend classes for valid parameters
  447. * @return integer number of backends
  448. */
  449. function add_backend($backend, $param = '') {
  450. $backend_name = 'abook_' . $backend;
  451. eval('$newback = new ' . $backend_name . '($param);');
  452. if(!empty($newback->error)) {
  453. $this->error = $newback->error;
  454. return false;
  455. }
  456. $this->numbackends++;
  457. $newback->bnum = $this->numbackends;
  458. $this->backends[$this->numbackends] = $newback;
  459. /* Store ID of first local backend added */
  460. if ($this->localbackend == 0 && $newback->btype == 'local') {
  461. $this->localbackend = $this->numbackends;
  462. $this->localbackendname = $newback->sname;
  463. }
  464. return $this->numbackends;
  465. }
  466. /**
  467. * create string with name and email address
  468. *
  469. * This function takes a $row array as returned by the addressbook
  470. * search and returns an e-mail address with the full name or
  471. * nickname optionally prepended.
  472. * @param array $row address book entry
  473. * @return string email address with real name prepended
  474. */
  475. function full_address($row) {
  476. global $addrsrch_fullname, $data_dir, $username;
  477. $prefix = getPref($data_dir, $username, 'addrsrch_fullname');
  478. if (($prefix != "" || (isset($addrsrch_fullname) &&
  479. $prefix == $addrsrch_fullname)) && $prefix != 'noprefix') {
  480. $name = ($prefix == 'nickname' ? $row['nickname'] : $row['name']);
  481. return $name . ' <' . trim($row['email']) . '>';
  482. } else {
  483. return trim($row['email']);
  484. }
  485. }
  486. /**
  487. * Search for entries in address books
  488. *
  489. * Return a list of addresses matching expression in
  490. * all backends of a given type.
  491. * @param string $expression search expression
  492. * @param integer $bnum backend number. default to search in all backends
  493. * @return array search results
  494. */
  495. function search($expression, $bnum = -1) {
  496. $ret = array();
  497. $this->error = '';
  498. /* Search all backends */
  499. if ($bnum == -1) {
  500. $sel = $this->get_backend_list('');
  501. $failed = 0;
  502. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  503. $backend = &$sel[$i];
  504. $backend->error = '';
  505. $res = $backend->search($expression);
  506. if (is_array($res)) {
  507. $ret = array_merge($ret, $res);
  508. } else {
  509. $this->error .= "<br />\n" . htmlspecialchars($backend->error);
  510. $failed++;
  511. }
  512. }
  513. /* Only fail if all backends failed */
  514. if( $failed >= sizeof( $sel ) ) {
  515. $ret = FALSE;
  516. }
  517. } else {
  518. /* Search only one backend */
  519. $ret = $this->backends[$bnum]->search($expression);
  520. if (!is_array($ret)) {
  521. $this->error .= "<br />\n" . htmlspecialchars($this->backends[$bnum]->error);
  522. $ret = FALSE;
  523. }
  524. }
  525. return( $ret );
  526. }
  527. /**
  528. * Sorted search
  529. * @param string $expression search expression
  530. * @param integer $bnum backend number. default to search in all backends
  531. * @return array search results
  532. */
  533. function s_search($expression, $bnum = -1) {
  534. $ret = $this->search($expression, $bnum);
  535. if ( is_array( $ret ) ) {
  536. usort($ret, 'addressbook_cmp');
  537. }
  538. return $ret;
  539. }
  540. /**
  541. * Lookup an address by alias.
  542. * Only possible in local backends.
  543. * @param string $alias
  544. * @param integer backend number
  545. * @return array lookup results. False, if not found.
  546. */
  547. function lookup($alias, $bnum = -1) {
  548. $ret = array();
  549. if ($bnum > -1) {
  550. $res = $this->backends[$bnum]->lookup($alias);
  551. if (is_array($res)) {
  552. return $res;
  553. } else {
  554. $this->error = htmlspecialchars($this->backends[$bnum]->error);
  555. return false;
  556. }
  557. }
  558. $sel = $this->get_backend_list('local');
  559. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  560. $backend = &$sel[$i];
  561. $backend->error = '';
  562. $res = $backend->lookup($alias);
  563. if (is_array($res)) {
  564. if(!empty($res))
  565. return $res;
  566. } else {
  567. $this->error = htmlspecialchars($backend->error);
  568. return false;
  569. }
  570. }
  571. return $ret;
  572. }
  573. /**
  574. * Return all addresses
  575. * @param integer $bnum backend number
  576. * @return array search results
  577. */
  578. function list_addr($bnum = -1) {
  579. $ret = array();
  580. if ($bnum == -1) {
  581. $sel = $this->get_backend_list('');
  582. } else {
  583. $sel = array(0 => &$this->backends[$bnum]);
  584. }
  585. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  586. $backend = &$sel[$i];
  587. $backend->error = '';
  588. $res = $backend->list_addr();
  589. if (is_array($res)) {
  590. $ret = array_merge($ret, $res);
  591. } else {
  592. $this->error = htmlspecialchars($backend->error);
  593. return false;
  594. }
  595. }
  596. return $ret;
  597. }
  598. /**
  599. * Create a new address
  600. * @param array $userdata added address record
  601. * @param integer $bnum backend number
  602. * @return integer the backend number that the/ address was added
  603. * to, or false if it failed.
  604. */
  605. function add($userdata, $bnum) {
  606. /* Validate data */
  607. if (!is_array($userdata)) {
  608. $this->error = _("Invalid input data");
  609. return false;
  610. }
  611. if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
  612. $this->error = _("Name is missing");
  613. return false;
  614. }
  615. if (empty($userdata['email'])) {
  616. $this->error = _("E-mail address is missing");
  617. return false;
  618. }
  619. if (empty($userdata['nickname'])) {
  620. $userdata['nickname'] = $userdata['email'];
  621. }
  622. if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
  623. $this->error = _("Nickname contains illegal characters");
  624. return false;
  625. }
  626. /* Check that specified backend accept new entries */
  627. if (!$this->backends[$bnum]->writeable) {
  628. $this->error = _("Addressbook is read-only");
  629. return false;
  630. }
  631. /* Add address to backend */
  632. $res = $this->backends[$bnum]->add($userdata);
  633. if ($res) {
  634. return $bnum;
  635. } else {
  636. $this->error = htmlspecialchars($this->backends[$bnum]->error);
  637. return false;
  638. }
  639. return false; // Not reached
  640. } /* end of add() */
  641. /**
  642. * Remove the entries from address book
  643. * @param mixed $alias entries that have to be removed. Can be string with nickname or array with list of nicknames
  644. * @param integer $bnum backend number
  645. * @return bool true if removed successfully. false if there s an error. $this->error contains error message
  646. */
  647. function remove($alias, $bnum) {
  648. /* Check input */
  649. if (empty($alias)) {
  650. return true;
  651. }
  652. /* Convert string to single element array */
  653. if (!is_array($alias)) {
  654. $alias = array(0 => $alias);
  655. }
  656. /* Check that specified backend is writable */
  657. if (!$this->backends[$bnum]->writeable) {
  658. $this->error = _("Addressbook is read-only");
  659. return false;
  660. }
  661. /* Remove user from backend */
  662. $res = $this->backends[$bnum]->remove($alias);
  663. if ($res) {
  664. return $bnum;
  665. } else {
  666. $this->error = htmlspecialchars($this->backends[$bnum]->error);
  667. return false;
  668. }
  669. return FALSE; /* Not reached */
  670. } /* end of remove() */
  671. /**
  672. * Modify entry in address book
  673. * @param string $alias nickname
  674. * @param array $userdata newdata
  675. * @param integer $bnum backend number
  676. */
  677. function modify($alias, $userdata, $bnum) {
  678. /* Check input */
  679. if (empty($alias) || !is_string($alias)) {
  680. return true;
  681. }
  682. /* Validate data */
  683. if(!is_array($userdata)) {
  684. $this->error = _("Invalid input data");
  685. return false;
  686. }
  687. if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
  688. $this->error = _("Name is missing");
  689. return false;
  690. }
  691. if (empty($userdata['email'])) {
  692. $this->error = _("E-mail address is missing");
  693. return false;
  694. }
  695. if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
  696. $this->error = _("Nickname contains illegal characters");
  697. return false;
  698. }
  699. if (empty($userdata['nickname'])) {
  700. $userdata['nickname'] = $userdata['email'];
  701. }
  702. /* Check that specified backend is writable */
  703. if (!$this->backends[$bnum]->writeable) {
  704. $this->error = _("Addressbook is read-only");;
  705. return false;
  706. }
  707. /* Modify user in backend */
  708. $res = $this->backends[$bnum]->modify($alias, $userdata);
  709. if ($res) {
  710. return $bnum;
  711. } else {
  712. $this->error = htmlspecialchars($this->backends[$bnum]->error);
  713. return false;
  714. }
  715. return FALSE; /* Not reached */
  716. } /* end of modify() */
  717. } /* End of class Addressbook */
  718. /**
  719. * Generic backend that all other backends extend
  720. * @package squirrelmail
  721. * @subpackage addressbook
  722. */
  723. class addressbook_backend {
  724. /* Variables that all backends must provide. */
  725. /**
  726. * Backend type
  727. *
  728. * Can be 'local' or 'remote'
  729. * @var string backend type
  730. */
  731. var $btype = 'dummy';
  732. /**
  733. * Internal backend name
  734. * @var string
  735. */
  736. var $bname = 'dummy';
  737. /**
  738. * Displayed backend name
  739. * @var string
  740. */
  741. var $sname = 'Dummy backend';
  742. /*
  743. * Variables common for all backends, but that
  744. * should not be changed by the backends.
  745. */
  746. /**
  747. * Backend number
  748. * @var integer
  749. */
  750. var $bnum = -1;
  751. /**
  752. * Error messages
  753. * @var string
  754. */
  755. var $error = '';
  756. /**
  757. * Writeable flag
  758. * @var bool
  759. */
  760. var $writeable = false;
  761. /**
  762. * Set error message
  763. * @param string $string error message
  764. * @return bool
  765. */
  766. function set_error($string) {
  767. $this->error = '[' . $this->sname . '] ' . $string;
  768. return false;
  769. }
  770. /* ========================== Public ======================== */
  771. /**
  772. * Search for entries in backend
  773. *
  774. * Working backend should support use of wildcards. * symbol
  775. * should match one or more symbols. ? symbol should match any
  776. * single symbol.
  777. * @param string $expression
  778. * @return bool
  779. */
  780. function search($expression) {
  781. $this->set_error('search not implemented');
  782. return false;
  783. }
  784. /**
  785. * Find entry in backend by alias
  786. * @param string $alias name used for id
  787. * @return bool
  788. */
  789. function lookup($alias) {
  790. $this->set_error('lookup not implemented');
  791. return false;
  792. }
  793. /**
  794. * List all entries in backend
  795. *
  796. * Working backend should provide this function or at least
  797. * dummy function that returns empty array.
  798. * @return bool
  799. */
  800. function list_addr() {
  801. $this->set_error('list_addr not implemented');
  802. return false;
  803. }
  804. /**
  805. * Add entry to backend
  806. * @param array userdata
  807. * @return bool
  808. */
  809. function add($userdata) {
  810. $this->set_error('add not implemented');
  811. return false;
  812. }
  813. /**
  814. * Remove entry from backend
  815. * @param string $alias name used for id
  816. * @return bool
  817. */
  818. function remove($alias) {
  819. $this->set_error('delete not implemented');
  820. return false;
  821. }
  822. /**
  823. * Modify entry in backend
  824. * @param string $alias name used for id
  825. * @param array $newuserdata new data
  826. * @return bool
  827. */
  828. function modify($alias, $newuserdata) {
  829. $this->set_error('modify not implemented');
  830. return false;
  831. }
  832. }
  833. /*
  834. PHP 5 requires that the class be made first, which seems rather
  835. logical, and should have been the way it was generated the first time.
  836. */
  837. require_once(SM_PATH . 'functions/abook_local_file.php');
  838. require_once(SM_PATH . 'functions/abook_ldap_server.php');
  839. /* Only load database backend if database is configured */
  840. if((isset($addrbook_dsn) && !empty($addrbook_dsn)) ||
  841. (isset($addrbook_global_dsn) && !empty($addrbook_global_dsn))) {
  842. include_once(SM_PATH . 'functions/abook_database.php');
  843. }
  844. /*
  845. * hook allows adding different address book classes.
  846. * class must follow address book class coding standards.
  847. *
  848. * see addressbook_backend class and functions/abook_*.php files.
  849. * @since 1.5.1 and 1.4.5
  850. */
  851. do_hook('abook_add_class');
  852. ?>