addressbook.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. * Manage personal address book.
  9. *
  10. * $Id$
  11. */
  12. require_once('../src/validate.php');
  13. require_once('../functions/array.php');
  14. require_once('../functions/display_messages.php');
  15. require_once('../functions/addressbook.php');
  16. require_once('../functions/strings.php');
  17. require_once('../functions/html.php');
  18. /* Make an input field */
  19. function adressbook_inp_field($label, $field, $name, $size, $values, $add) {
  20. global $color;
  21. $td_str = '<INPUT NAME="' . $name . '[' . $field . ']" SIZE="' . $size . '" VALUE="';
  22. if (isset($values[$field])) {
  23. $td_str .= htmlspecialchars($values[$field]);
  24. }
  25. $td_str .= '">' . $add . '';
  26. return html_tag( 'tr' ,
  27. html_tag( 'td', $label . ':', 'right', $color[4]) .
  28. html_tag( 'td', $td_str, 'left', $color[4])
  29. )
  30. . "\n";
  31. }
  32. /* Output form to add and modify address data */
  33. function address_form($name, $submittext, $values = array()) {
  34. global $color;
  35. echo html_tag( 'table',
  36. adressbook_inp_field(_("Nickname"), 'nickname', $name, 15, $values,
  37. '<SMALL>' . _("Must be unique") . '</SMALL>') .
  38. adressbook_inp_field(_("E-mail address"), 'email', $name, 45, $values, '') .
  39. adressbook_inp_field(_("First name"), 'firstname', $name, 45, $values, '') .
  40. adressbook_inp_field(_("Last name"), 'lastname', $name, 45, $values, '') .
  41. adressbook_inp_field(_("Additional info"), 'label', $name, 45, $values, '') .
  42. html_tag( 'tr',
  43. html_tag( 'td',
  44. '<INPUT TYPE=submit NAME="' . $name . '[SUBMIT]" VALUE="' .
  45. $submittext . '">',
  46. 'center', $color[4], 'colspan="2"')
  47. )
  48. , 'center', '', 'border="0" cellpadding="1" cols="2" width="90%"') ."\n";
  49. }
  50. /* Open addressbook, with error messages on but without LDAP (the *
  51. * second "true"). Don't need LDAP here anyway */
  52. $abook = addressbook_init(true, true);
  53. if($abook->localbackend == 0) {
  54. plain_error_message(
  55. _("No personal address book is defined. Contact administrator."),
  56. $color);
  57. exit();
  58. }
  59. displayPageHeader($color, 'None');
  60. $defdata = array();
  61. $formerror = '';
  62. $abortform = false;
  63. $showaddrlist = true;
  64. $defselected = array();
  65. /* Handle user's actions */
  66. if($REQUEST_METHOD == 'POST') {
  67. /**************************************************
  68. * Add new address *
  69. **************************************************/
  70. if (!empty($addaddr['nickname'])) {
  71. $r = $abook->add($addaddr, $abook->localbackend);
  72. /* Handle error messages */
  73. if (!$r) {
  74. /* Remove backend name from error string */
  75. $errstr = $abook->error;
  76. $errstr = ereg_replace('^\[.*\] *', '', $errstr);
  77. $formerror = $errstr;
  78. $showaddrlist = false;
  79. $defdata = $addaddr;
  80. }
  81. } else {
  82. /************************************************
  83. * Delete address(es) *
  84. ************************************************/
  85. if ((!empty($deladdr)) && sizeof($sel) > 0) {
  86. $orig_sel = $sel;
  87. sort($sel);
  88. /* The selected addresses are identidied by "backend:nickname". *
  89. * Sort the list and process one backend at the time */
  90. $prevback = -1;
  91. $subsel = array();
  92. $delfailed = false;
  93. for ($i = 0 ; (($i < sizeof($sel)) && !$delfailed) ; $i++) {
  94. list($sbackend, $snick) = explode(':', $sel[$i]);
  95. /* When we get to a new backend, process addresses in *
  96. * previous one. */
  97. if ($prevback != $sbackend && $prevback != -1) {
  98. $r = $abook->remove($subsel, $prevback);
  99. if (!$r) {
  100. $formerror = $abook->error;
  101. $i = sizeof($sel);
  102. $delfailed = true;
  103. break;
  104. }
  105. $subsel = array();
  106. }
  107. /* Queue for processing */
  108. array_push($subsel, $snick);
  109. $prevback = $sbackend;
  110. }
  111. if (!$delfailed) {
  112. $r = $abook->remove($subsel, $prevback);
  113. if (!$r) { /* Handle errors */
  114. $formerror = $abook->error;
  115. $delfailed = true;
  116. }
  117. }
  118. if ($delfailed) {
  119. $showaddrlist = true;
  120. $defselected = $orig_sel;
  121. }
  122. } else {
  123. /***********************************************
  124. * Update/modify address *
  125. ***********************************************/
  126. if (!empty($editaddr)) {
  127. /* Stage one: Copy data into form */
  128. if (isset($sel) && sizeof($sel) > 0) {
  129. if(sizeof($sel) > 1) {
  130. $formerror = _("You can only edit one address at the time");
  131. $showaddrlist = true;
  132. $defselected = $sel;
  133. } else {
  134. $abortform = true;
  135. list($ebackend, $enick) = explode(':', $sel[0]);
  136. $olddata = $abook->lookup($enick, $ebackend);
  137. /* Display the "new address" form */
  138. echo '<FORM ACTION="' . $PHP_SELF . '" METHOD="POST">' .
  139. "\n" .
  140. html_tag( 'table',
  141. html_tag( 'tr',
  142. html_tag( 'td',
  143. "\n". '<strong>' . _("Update address") . '</strong>' ."\n",
  144. 'center', $color[0] )
  145. ),
  146. 'center', '', 'width="100%" cols="1"' ) .
  147. address_form("editaddr", _("Update address"), $olddata);
  148. echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
  149. htmlspecialchars($olddata["nickname"]) . "\">\n" .
  150. '<INPUT TYPE=hidden NAME=backend VALUE="' .
  151. htmlspecialchars($olddata["backend"]) . "\">\n" .
  152. '<INPUT TYPE=hidden NAME=doedit VALUE=1>' . "\n" .
  153. '</FORM>';
  154. }
  155. } else {
  156. /* Stage two: Write new data */
  157. if ($doedit = 1) {
  158. $newdata = $editaddr;
  159. $r = $abook->modify($oldnick, $newdata, $backend);
  160. /* Handle error messages */
  161. if (!$r) {
  162. /* Display error */
  163. echo html_tag( 'table',
  164. html_tag( 'tr',
  165. html_tag( 'td',
  166. "\n". '<br><strong><font color="' . $color[2] .
  167. '">' . _("ERROR") . ': ' . $abook->error . '</font></strong>' ."\n",
  168. 'center' )
  169. ),
  170. 'center', '', 'width="100%" cols="1"' );
  171. /* Display the "new address" form again */
  172. echo '<FORM ACTION="' . $PHP_SELF .
  173. '" METHOD="POST">' . "\n" .
  174. html_tag( 'table',
  175. html_tag( 'tr',
  176. html_tag( 'td',
  177. "\n". '<br><strong>' . _("Update address") . '</strong>' ."\n",
  178. 'center', $color[0] )
  179. ),
  180. 'center', '', 'width="100%" cols="1"' ) .
  181. address_form("editaddr", _("Update address"), $newdata);
  182. echo '<INPUT TYPE=hidden NAME=oldnick VALUE="' .
  183. htmlspecialchars($oldnick) . "\">\n" .
  184. '<INPUT TYPE=hidden NAME=backend VALUE="' .
  185. htmlspecialchars($backend) . "\">\n" .
  186. '<INPUT TYPE=hidden NAME=doedit VALUE=1>' .
  187. "\n" . '</FORM>';
  188. $abortform = true;
  189. }
  190. } else {
  191. /* Should not get here... */
  192. plain_error_message(_("Unknown error"), $color);
  193. $abortform = true;
  194. }
  195. }
  196. } /* !empty($editaddr) - Update/modify address */
  197. } /* (!empty($deladdr)) && sizeof($sel) > 0 - Delete address(es) */
  198. } /* !empty($addaddr['nickname']) - Add new address */
  199. // Some times we end output before forms are printed
  200. if($abortform) {
  201. echo "</BODY></HTML>\n";
  202. exit();
  203. }
  204. }
  205. /* =================================================================== *
  206. * The following is only executed on a GET request, or on a POST when *
  207. * a user is added, or when "delete" or "modify" was successful. *
  208. * =================================================================== */
  209. /* Display error messages */
  210. if (!empty($formerror)) {
  211. echo html_tag( 'table',
  212. html_tag( 'tr',
  213. html_tag( 'td',
  214. "\n". '<br><strong><font color="' . $color[2] .
  215. '">' . _("ERROR") . ': ' . $formerror . '</font></strong>' ."\n",
  216. 'center' )
  217. ),
  218. 'center', '', 'width="100%" cols="1"' );
  219. }
  220. /* Display the address management part */
  221. if ($showaddrlist) {
  222. /* Get and sort address list */
  223. $alist = $abook->list_addr();
  224. if(!is_array($alist)) {
  225. plain_error_message($abook->error, $color);
  226. exit;
  227. }
  228. usort($alist,'alistcmp');
  229. $prevbackend = -1;
  230. $headerprinted = false;
  231. echo html_tag( 'p', '<a href="#AddAddress">' . _("Add address") . '</a>', 'center' ) . "\n";
  232. /* List addresses */
  233. if (count($alist) > 0) {
  234. echo '<FORM ACTION="' . $PHP_SELF . '" METHOD="POST">' . "\n";
  235. while(list($undef,$row) = each($alist)) {
  236. /* New table header for each backend */
  237. if($prevbackend != $row['backend']) {
  238. if($prevbackend < 0) {
  239. echo html_tag( 'table',
  240. html_tag( 'tr',
  241. html_tag( 'td',
  242. '<INPUT TYPE=submit NAME=editaddr VALUE="' .
  243. _("Edit selected") . "\">\n" .
  244. '<INPUT TYPE=submit NAME=deladdr VALUE="' .
  245. _("Delete selected") . "\">\n",
  246. 'center', '', 'colspan="5"' )
  247. ) .
  248. html_tag( 'tr',
  249. html_tag( 'td', '&nbsp;<br>', 'center', '', 'colspan="5"' )
  250. ) ,
  251. 'center' );
  252. }
  253. echo html_tag( 'table',
  254. html_tag( 'tr',
  255. html_tag( 'td', "\n" . '<strong>' . $row['source'] . '</strong>' . "\n", 'center', $color[0] )
  256. ) ,
  257. 'center', '', 'width="95%" cols="1"' ) ."\n"
  258. . html_tag( 'table', '', 'center', '', 'cols="5" border="0" cellpadding="1" cellspacing="0" width="90%"' ) .
  259. html_tag( 'tr', "\n" .
  260. html_tag( 'th', '&nbsp;', 'left', '', 'width="1%"' ) .
  261. html_tag( 'th', _("Nickname"), 'left', '', 'width="1%"' ) .
  262. html_tag( 'th', _("Name"), 'left', '', 'width="1%"' ) .
  263. html_tag( 'th', _("E-mail"), 'left', '', 'width="1%"' ) .
  264. html_tag( 'th', _("Info"), 'left', '', 'width="1%"' ) ,
  265. '', $color[9] ) . "\n";
  266. $line = 0;
  267. $headerprinted = true;
  268. } /* End of header */
  269. $prevbackend = $row['backend'];
  270. /* Check if this user is selected */
  271. if(in_array($row['backend'] . ':' . $row['nickname'], $defselected)) {
  272. $selected = 'CHECKED';
  273. } else {
  274. $selected = '';
  275. }
  276. /* Print one row */
  277. $tr_bgcolor = '';
  278. if ($line % 2) { $tr_bgcolor = $color[0]; }
  279. echo html_tag( 'tr', '') .
  280. html_tag( 'td',
  281. '<SMALL>' .
  282. '<INPUT TYPE=checkbox ' . $selected . ' NAME="sel[]" VALUE="' .
  283. $row['backend'] . ':' . $row['nickname'] . '"></SMALL>' ,
  284. 'center', '', 'valign="top" width="1%"' ) .
  285. html_tag( 'td', '&nbsp;' . $row['nickname'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
  286. html_tag( 'td', '&nbsp;' . $row['name'] . '&nbsp;', 'left', '', 'valign="top" width="1%" nowrap' ) .
  287. html_tag( 'td', '', 'left', '', 'valign="top" width="1%" nowrap' ) . '&nbsp;';
  288. if ($compose_new_win == '1') {
  289. echo '<a href="javascript:void(0)" onclick=comp_in_new(false,"compose.php?send_to='.rawurlencode($row['email']).'")>';
  290. }
  291. else {
  292. echo '<A HREF="compose.php?send_to=' . rawurlencode($row['email']).'">';
  293. }
  294. echo $row['email'] . '</A>&nbsp;</td>'."\n".
  295. html_tag( 'td', '&nbsp;' . $row['label'] . '&nbsp;', 'left', '', 'valign="top" width="1%"' ) .
  296. "</tr>\n";
  297. $line++;
  298. }
  299. /* End of list. Close table. */
  300. if ($headerprinted) {
  301. echo html_tag( 'tr',
  302. html_tag( 'td',
  303. '<INPUT TYPE="submit" NAME="editaddr" VALUE="' . _("Edit selected") .
  304. "\">\n" .
  305. '<INPUT TYPE="submit" NAME="deladdr" VALUE="' . _("Delete selected") .
  306. "\">\n",
  307. 'center', '', 'colspan="5"' )
  308. );
  309. }
  310. echo '</table></FORM>';
  311. }
  312. } /* end of addresslist */
  313. /* Display the "new address" form */
  314. echo '<a name="AddAddress"></a>' . "\n" .
  315. '<FORM ACTION="' . $PHP_SELF . '" NAME=f_add METHOD="POST">' . "\n" .
  316. html_tag( 'table',
  317. html_tag( 'tr',
  318. html_tag( 'td', "\n". '<strong>' . sprintf(_("Add to %s"), $abook->localbackendname) . '</strong>' . "\n",
  319. 'center', $color[0]
  320. )
  321. )
  322. , 'center', '', 'width="100%" cols="1"' ) ."\n";
  323. address_form('addaddr', _("Add address"), $defdata);
  324. echo '</FORM>';
  325. /* Add hook for anything that wants on the bottom */
  326. do_hook('addressbook_bottom');
  327. ?>
  328. </BODY></HTML>