addressbook.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <?php
  2. /**
  3. * addressbook.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * Functions and classes for the addressbook system.
  9. *
  10. * $Id$
  11. */
  12. /*
  13. This is the path to the global site-wide addressbook.
  14. It looks and feels just like a user's .abook file
  15. If this is in the data directory, use "$data_dir/global.abook"
  16. If not, specify the path as though it was accessed from the
  17. src/ directory ("../global.abook" -> in main directory)
  18. If you don't want a global site-wide addressbook, comment these
  19. two lines out. (They are disabled by default.)
  20. The global addressbook is unmodifiable by anyone. You must actually
  21. use a shell script or whatnot to modify the contents.
  22. global $data_dir;
  23. $address_book_global_filename = "$data_dir/global.abook";
  24. Include backends here.
  25. */
  26. require_once('../functions/abook_local_file.php');
  27. require_once('../functions/abook_ldap_server.php');
  28. global $addrbook_dsn;
  29. /* Use this if you wanna have a global address book */
  30. if (isset($address_book_global_filename)) {
  31. include_once('../functions/abook_global_file.php');
  32. }
  33. /* Only load database backend if database is configured */
  34. if(isset($addrbook_dsn) && !empty($addrbook_dsn)) {
  35. include_once('../functions/abook_database.php');
  36. }
  37. /*
  38. Create and initialize an addressbook object.
  39. Returns the created object
  40. */
  41. function addressbook_init($showerr = true, $onlylocal = false) {
  42. global $data_dir, $username, $ldap_server, $address_book_global_filename;
  43. global $addrbook_dsn;
  44. /* Create a new addressbook object */
  45. $abook = new AddressBook;
  46. /*
  47. Always add a local backend. We use *either* file-based *or* a
  48. database addressbook. If $addrbook_dsn is set, the database
  49. backend is used. If not, addressbooks are stores in files.
  50. */
  51. if (isset($addrbook_dsn) && !empty($addrbook_dsn)) {
  52. /* Database */
  53. $r = $abook->add_backend('database', Array('dsn' => $addrbook_dsn,
  54. 'owner' => $username,
  55. 'table' => 'address'));
  56. if (!$r && $showerr) {
  57. echo _("Error initializing addressbook database.");
  58. exit;
  59. }
  60. } else {
  61. /* File */
  62. $filename = getHashedFile($username, $data_dir, "$username.abook");
  63. $r = $abook->add_backend('local_file', Array('filename' => $filename,
  64. 'create' => true));
  65. if(!$r && $showerr) {
  66. printf( _("Error opening file %s"), $filename );
  67. exit;
  68. }
  69. }
  70. /* This would be for the global addressbook */
  71. if (isset($address_book_global_filename)) {
  72. $r = $abook->add_backend('global_file');
  73. if (!$r && $showerr) {
  74. echo _("Error initializing global addressbook.");
  75. exit;
  76. }
  77. }
  78. if ($onlylocal) {
  79. return $abook;
  80. }
  81. /* Load configured LDAP servers (if PHP has LDAP support) */
  82. if (isset($ldap_server) && is_array($ldap_server) && function_exists('ldap_connect')) {
  83. reset($ldap_server);
  84. while (list($undef,$param) = each($ldap_server)) {
  85. if (is_array($param)) {
  86. $r = $abook->add_backend('ldap_server', $param);
  87. if (!$r && $showerr) {
  88. printf( '&nbsp;' . _("Error initializing LDAP server %s:") .
  89. "<BR>\n", $param['host']);
  90. echo '&nbsp;' . $abook->error;
  91. exit;
  92. }
  93. }
  94. }
  95. }
  96. /* Return the initialized object */
  97. return $abook;
  98. }
  99. /*
  100. * Had to move this function outside of the Addressbook Class
  101. * PHP 4.0.4 Seemed to be having problems with inline functions.
  102. */
  103. function addressbook_cmp($a,$b) {
  104. if($a['backend'] > $b['backend']) {
  105. return 1;
  106. } else if($a['backend'] < $b['backend']) {
  107. return -1;
  108. }
  109. return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
  110. }
  111. /*
  112. * This is the main address book class that connect all the
  113. * backends and provide services to the functions above.
  114. *
  115. */
  116. class AddressBook {
  117. var $backends = array();
  118. var $numbackends = 0;
  119. var $error = '';
  120. var $localbackend = 0;
  121. var $localbackendname = '';
  122. // Constructor function.
  123. function AddressBook() {
  124. $localbackendname = _("Personal address book");
  125. }
  126. /*
  127. * Return an array of backends of a given type,
  128. * or all backends if no type is specified.
  129. */
  130. function get_backend_list($type = '') {
  131. $ret = array();
  132. for ($i = 1 ; $i <= $this->numbackends ; $i++) {
  133. if (empty($type) || $type == $this->backends[$i]->btype) {
  134. $ret[] = &$this->backends[$i];
  135. }
  136. }
  137. return $ret;
  138. }
  139. /*
  140. ========================== Public ========================
  141. Add a new backend. $backend is the name of a backend
  142. (without the abook_ prefix), and $param is an optional
  143. mixed variable that is passed to the backend constructor.
  144. See each of the backend classes for valid parameters.
  145. */
  146. function add_backend($backend, $param = '') {
  147. $backend_name = 'abook_' . $backend;
  148. eval('$newback = new ' . $backend_name . '($param);');
  149. if(!empty($newback->error)) {
  150. $this->error = $newback->error;
  151. return false;
  152. }
  153. $this->numbackends++;
  154. $newback->bnum = $this->numbackends;
  155. $this->backends[$this->numbackends] = $newback;
  156. /* Store ID of first local backend added */
  157. if ($this->localbackend == 0 && $newback->btype == 'local') {
  158. $this->localbackend = $this->numbackends;
  159. $this->localbackendname = $newback->sname;
  160. }
  161. return $this->numbackends;
  162. }
  163. /*
  164. Return a list of addresses matching expression in
  165. all backends of a given type.
  166. */
  167. function search($expression, $bnum = -1) {
  168. $ret = array();
  169. $this->error = '';
  170. /* Search all backends */
  171. if ($bnum == -1) {
  172. $sel = $this->get_backend_list('');
  173. $failed = 0;
  174. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  175. $backend = &$sel[$i];
  176. $backend->error = '';
  177. $res = $backend->search($expression);
  178. if (is_array($res)) {
  179. $ret = array_merge($ret, $res);
  180. } else {
  181. $this->error .= "<br>\n" . $backend->error;
  182. $failed++;
  183. }
  184. }
  185. /* Only fail if all backends failed */
  186. if( $failed >= sizeof( $sel ) ) {
  187. $ret = FALSE;
  188. }
  189. } else {
  190. /* Search only one backend */
  191. $ret = $this->backends[$bnum]->search($expression);
  192. if (!is_array($ret)) {
  193. $this->error .= "<br>\n" . $this->backends[$bnum]->error;
  194. $ret = FALSE;
  195. }
  196. }
  197. return( $ret );
  198. }
  199. /* Return a sorted search */
  200. function s_search($expression, $bnum = -1) {
  201. $ret = $this->search($expression, $bnum);
  202. if ( is_array( $ret ) ) {
  203. usort($ret, 'addressbook_cmp');
  204. }
  205. return $ret;
  206. }
  207. /*
  208. * Lookup an address by alias. Only possible in
  209. * local backends.
  210. */
  211. function lookup($alias, $bnum = -1) {
  212. $ret = array();
  213. if ($bnum > -1) {
  214. $res = $this->backends[$bnum]->lookup($alias);
  215. if (is_array($res)) {
  216. return $res;
  217. } else {
  218. $this->error = $backend->error;
  219. return false;
  220. }
  221. }
  222. $sel = $this->get_backend_list('local');
  223. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  224. $backend = &$sel[$i];
  225. $backend->error = '';
  226. $res = $backend->lookup($alias);
  227. if (is_array($res)) {
  228. if(!empty($res))
  229. return $res;
  230. } else {
  231. $this->error = $backend->error;
  232. return false;
  233. }
  234. }
  235. return $ret;
  236. }
  237. /* Return all addresses */
  238. function list_addr($bnum = -1) {
  239. $ret = array();
  240. if ($bnum == -1) {
  241. $sel = $this->get_backend_list('local');
  242. } else {
  243. $sel = array(0 => &$this->backends[$bnum]);
  244. }
  245. for ($i = 0 ; $i < sizeof($sel) ; $i++) {
  246. $backend = &$sel[$i];
  247. $backend->error = '';
  248. $res = $backend->list_addr();
  249. if (is_array($res)) {
  250. $ret = array_merge($ret, $res);
  251. } else {
  252. $this->error = $backend->error;
  253. return false;
  254. }
  255. }
  256. return $ret;
  257. }
  258. /*
  259. * Create a new address from $userdata, in backend $bnum.
  260. * Return the backend number that the/ address was added
  261. * to, or false if it failed.
  262. */
  263. function add($userdata, $bnum) {
  264. /* Validate data */
  265. if (!is_array($userdata)) {
  266. $this->error = _("Invalid input data");
  267. return false;
  268. }
  269. if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
  270. $this->error = _("Name is missing");
  271. return false;
  272. }
  273. if (empty($userdata['email'])) {
  274. $this->error = _("E-mail address is missing");
  275. return false;
  276. }
  277. if (empty($userdata['nickname'])) {
  278. $userdata['nickname'] = $userdata['email'];
  279. }
  280. if (eregi('[ \\:\\|\\#\\"\\!]', $userdata['nickname'])) {
  281. $this->error = _("Nickname contains illegal characters");
  282. return false;
  283. }
  284. /* Check that specified backend accept new entries */
  285. if (!$this->backends[$bnum]->writeable) {
  286. $this->error = _("Addressbook is read-only");
  287. return false;
  288. }
  289. /* Add address to backend */
  290. $res = $this->backends[$bnum]->add($userdata);
  291. if ($res) {
  292. return $bnum;
  293. } else {
  294. $this->error = $this->backends[$bnum]->error;
  295. return false;
  296. }
  297. return false; // Not reached
  298. } /* end of add() */
  299. /*
  300. * Remove the user identified by $alias from backend $bnum
  301. * If $alias is an array, all users in the array are removed.
  302. */
  303. function remove($alias, $bnum) {
  304. /* Check input */
  305. if (empty($alias)) {
  306. return true;
  307. }
  308. /* Convert string to single element array */
  309. if (!is_array($alias)) {
  310. $alias = array(0 => $alias);
  311. }
  312. /* Check that specified backend is writable */
  313. if (!$this->backends[$bnum]->writeable) {
  314. $this->error = _("Addressbook is read-only");
  315. return false;
  316. }
  317. /* Remove user from backend */
  318. $res = $this->backends[$bnum]->remove($alias);
  319. if ($res) {
  320. return $bnum;
  321. } else {
  322. $this->error = $this->backends[$bnum]->error;
  323. return false;
  324. }
  325. return FALSE; /* Not reached */
  326. } /* end of remove() */
  327. /*
  328. * Remove the user identified by $alias from backend $bnum
  329. * If $alias is an array, all users in the array are removed.
  330. */
  331. function modify($alias, $userdata, $bnum) {
  332. /* Check input */
  333. if (empty($alias) || !is_string($alias)) {
  334. return true;
  335. }
  336. /* Validate data */
  337. if(!is_array($userdata)) {
  338. $this->error = _("Invalid input data");
  339. return false;
  340. }
  341. if (empty($userdata['firstname']) && empty($userdata['lastname'])) {
  342. $this->error = _("Name is missing");
  343. return false;
  344. }
  345. if (empty($userdata['email'])) {
  346. $this->error = _("E-mail address is missing");
  347. return false;
  348. }
  349. if (eregi('[\\: \\|\\#"\\!]', $userdata['nickname'])) {
  350. $this->error = _("Nickname contains illegal characters");
  351. return false;
  352. }
  353. if (empty($userdata['nickname'])) {
  354. $userdata['nickname'] = $userdata['email'];
  355. }
  356. /* Check that specified backend is writable */
  357. if (!$this->backends[$bnum]->writeable) {
  358. $this->error = _("Addressbook is read-only");;
  359. return false;
  360. }
  361. /* Modify user in backend */
  362. $res = $this->backends[$bnum]->modify($alias, $userdata);
  363. if ($res) {
  364. return $bnum;
  365. } else {
  366. $this->error = $this->backends[$bnum]->error;
  367. return false;
  368. }
  369. return FALSE; /* Not reached */
  370. } /* end of modify() */
  371. } /* End of class Addressbook */
  372. /*
  373. * Generic backend that all other backends extend
  374. */
  375. class addressbook_backend {
  376. /* Variables that all backends must provide. */
  377. var $btype = 'dummy';
  378. var $bname = 'dummy';
  379. var $sname = 'Dummy backend';
  380. /*
  381. * Variables common for all backends, but that
  382. * should not be changed by the backends.
  383. */
  384. var $bnum = -1;
  385. var $error = '';
  386. var $writeable = false;
  387. function set_error($string) {
  388. $this->error = '[' . $this->sname . '] ' . $string;
  389. return false;
  390. }
  391. /* ========================== Public ======================== */
  392. function search($expression) {
  393. $this->set_error('search not implemented');
  394. return false;
  395. }
  396. function lookup($alias) {
  397. $this->set_error('lookup not implemented');
  398. return false;
  399. }
  400. function list_addr() {
  401. $this->set_error('list_addr not implemented');
  402. return false;
  403. }
  404. function add($userdata) {
  405. $this->set_error('add not implemented');
  406. return false;
  407. }
  408. function remove($alias) {
  409. $this->set_error('delete not implemented');
  410. return false;
  411. }
  412. function modify($alias, $newuserdata) {
  413. $this->set_error('modify not implemented');
  414. return false;
  415. }
  416. }
  417. /* Sort array by the key "name" */
  418. function alistcmp($a,$b) {
  419. if ($a['backend'] > $b['backend']) {
  420. return 1;
  421. } else {
  422. if ($a['backend'] < $b['backend']) {
  423. return -1;
  424. }
  425. }
  426. return (strtolower($a['name']) > strtolower($b['name'])) ? 1 : -1;
  427. }
  428. ?>