Source for file addressbook.php

Documentation is available at addressbook.php

  1. <?php
  2.  
  3. /**
  4.  * addressbook.php
  5.  *
  6.  * Manage personal address book.
  7.  *
  8.  * @copyright 1999-2014 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: addressbook.php 14420 2014-01-01 20:33:20Z pdontthink $
  11.  * @package squirrelmail
  12.  * @subpackage addressbook
  13.  */
  14.  
  15. /** This is the addressbook page */
  16. define('PAGE_NAME''addressbook');
  17.  
  18. /**
  19.  * Include the SquirrelMail initialization file.
  20.  */
  21. include('../include/init.php');
  22.  
  23. /** SquirrelMail required files. */
  24. /* address book functions */
  25. require_once(SM_PATH 'functions/addressbook.php');
  26. include_once(SM_PATH 'templates/util_addressbook.php');
  27.  
  28. /* form functions */
  29. require_once(SM_PATH 'functions/forms.php');
  30.  
  31. /** lets get the global vars we may need */
  32.  
  33. /* From the address form */
  34. sqgetGlobalVar('smtoken',       $submitted_tokenSQ_POST'');
  35. sqgetGlobalVar('addaddr',       $addaddr,       SQ_POST);
  36. sqgetGlobalVar('editaddr',      $editaddr,      SQ_POST);
  37. sqgetGlobalVar('deladdr',       $deladdr,       SQ_POST);
  38. sqgetGlobalVar('compose_to',    $compose_to,    SQ_POST);
  39. sqgetGlobalVar('sel',           $sel,           SQ_POST);
  40. sqgetGlobalVar('oldnick',       $oldnick,       SQ_POST);
  41. sqgetGlobalVar('backend',       $backend,       SQ_POST);
  42. sqgetGlobalVar('doedit',        $doedit,        SQ_POST);
  43. $page_size $abook_show_num;
  44. if (!sqGetGlobalVar('page_number'$page_numberSQ_FORM))
  45.     if (!sqGetGlobalVar('current_page_number'$page_numberSQ_FORM))
  46.         $page_number 1;
  47. if (!sqGetGlobalVar('show_all'$show_allSQ_FORM))
  48.     $show_all 0;
  49.  
  50. /* Get sorting order */
  51. $abook_sort_order get_abook_sort();
  52.  
  53. // Create page header before addressbook_init in order to
  54. // display error messages correctly, unless we might be
  55. // redirecting the browser to the compose page.
  56. //
  57. if ((empty($compose_to)) || sizeof($sel1)
  58.     displayPageHeader($color);
  59.  
  60. /* Open addressbook with error messages on.
  61.  remote backends (LDAP) are enabled because they can be used. (list_addr function)
  62. */
  63. $abook addressbook_init(truefalse);
  64.  
  65. // FIXME: do we really have to stop use of address book when localbackend is not present?
  66. if($abook->localbackend == 0{
  67.     plain_error_message(_("No personal address book is defined. Contact administrator."));
  68.     exit();
  69. }
  70.  
  71. $current_backend $abook->localbackend;
  72. if (sqgetGlobalVar('new_bnum'$new_backendSQ_FORM)
  73.  && array_key_exists($new_backend$abook->backends)) {
  74.     $current_backend = (int) $new_backend;
  75. }
  76.  
  77. $abook_selection '&nbsp;';
  78. $list_backends array();
  79. if (count($abook->backends1{
  80.     foreach($abook->get_backend_list(as $oBackend{
  81.         if ($oBackend->listing{
  82.             $list_backends[$oBackend->bnum]=$oBackend->sname;
  83.         }
  84.     }
  85.     if (count($list_backends)>1{
  86.         $abook_selection addSelect('new_bnum',$list_backends,$current_backend,true)
  87.             .addSubmit(_("Change"),'change_abook');
  88.     }
  89. }
  90.  
  91. $defdata   array();
  92. $formerror '';
  93. $abortform false;
  94. $showaddrlist true;
  95. $defselected  array();
  96. $form_url 'addressbook.php';
  97.  
  98. /* Handle user's actions */
  99. if(sqgetGlobalVar('REQUEST_METHOD'$req_methodSQ_SERVER&& $req_method == 'POST'{
  100.  
  101.     // first, validate security token
  102.     sm_validate_security_token($submitted_token-1TRUE);
  103.  
  104.     /**************************************************
  105.      * Add new address                                *
  106.      **************************************************/
  107.     if (isset($addaddr)) {
  108.         if (isset($backend)) {
  109.             $r $abook->add($addaddr$backend);
  110.         else {
  111.             $r $abook->add($addaddr$abook->localbackend);
  112.         }
  113.  
  114.         /* Handle error messages */
  115.         if (!$r{
  116.             /* Remove backend name from error string */
  117.             $errstr $abook->error;
  118.             $errstr preg_replace('/^\[.*\] */'''$errstr);
  119.  
  120.             $formerror $errstr;
  121.             $showaddrlist false;
  122.             $defdata $addaddr;
  123.         }
  124.     else {
  125.  
  126.         /************************************************
  127.          * Delete address(es)                           *
  128.          ************************************************/
  129.         if ((!empty($deladdr)) && sizeof($sel0{
  130.             $orig_sel $sel;
  131.             sort($sel);
  132.  
  133.             /* The selected addresses are identified by "backend_nickname". *
  134.              * Sort the list and process one backend at the time            */
  135.             $prevback  = -1;
  136.             $subsel    array();
  137.             $delfailed false;
  138.  
  139.             for ($i (($i sizeof($sel)) && !$delfailed$i++{
  140.                 list($sbackend$snickexplode('_'$sel[$i]2);
  141.  
  142.                 /* When we get to a new backend, process addresses in *
  143.                  * previous one.                                      */
  144.                 if ($prevback != $sbackend && $prevback != -1{
  145.  
  146.                     $r $abook->remove($subsel$prevback);
  147.                     if (!$r{
  148.                         $formerror $abook->error;
  149.                         $i sizeof($sel);
  150.                         $delfailed true;
  151.                         break;
  152.                     }
  153.                     $subsel   array();
  154.                 }
  155.  
  156.                 /* Queue for processing */
  157.                 array_push($subsel$snick);
  158.                 $prevback $sbackend;
  159.             }
  160.  
  161.             if (!$delfailed{
  162.                 $r $abook->remove($subsel$prevback);
  163.                 if (!$r/* Handle errors */
  164.                     $formerror $abook->error;
  165.                     $delfailed true;
  166.                 }
  167.             }
  168.  
  169.             if ($delfailed{
  170.                 $showaddrlist true;
  171.                 $defselected  $orig_sel;
  172.             }
  173.  
  174.         /************************************************
  175.          * Compose to selected address(es)              *
  176.          ************************************************/
  177.         else if ((!empty($compose_to)) && sizeof($sel0{
  178.             $orig_sel $sel;
  179.             sort($sel);
  180.  
  181.             // The selected addresses are identified by "backend_nickname"
  182.             $lookup_failed false;
  183.             $send_to '';
  184.  
  185.             for ($i (($i sizeof($sel)) && !$lookup_failed$i++{
  186.                 list($sbackend$snickexplode('_'$sel[$i]2);
  187.  
  188.                 $data $abook->lookup($snick$sbackend);
  189.  
  190.                 if (!$data{
  191.                     $formerror $abook->error;
  192.                     $lookup_failed true;
  193.                     break;
  194.                 else {
  195.                     $addr $abook->full_address($data);
  196.                     if (!empty($addr))
  197.                         $send_to .= $addr ', ';
  198.                 }
  199.             }
  200.  
  201.  
  202.             if ($lookup_failed || empty($send_to)) {
  203.                 $showaddrlist true;
  204.                 $defselected  $sel;
  205.  
  206.                 // we skipped the page header above for this functionality, so add it here
  207.                 displayPageHeader($color);
  208.             }
  209.  
  210.  
  211.             // send off to compose screen
  212.             else {
  213.                 $send_to trim($send_to', ');
  214.                 header('Location: ' $base_uri 'src/compose.php?send_to=' rawurlencode($send_to));
  215.                 exit;
  216.             }
  217.  
  218.         else {
  219.  
  220.             /***********************************************
  221.              * Update/modify address                       *
  222.              ***********************************************/
  223.             if (!empty($editaddr)) {
  224.                 /* Stage one: Copy data into form */
  225.                 if (isset($sel&& sizeof($sel0{
  226.                     if(sizeof($sel1{
  227.                         $formerror _("You can only edit one address at the time");
  228.                         $showaddrlist true;
  229.                         $defselected $sel;
  230.                     else {
  231.                         $abortform true;
  232.                         list($ebackend$enickexplode('_'current($sel)2);
  233.                         $olddata $abook->lookup($enick$ebackend);
  234.                         // Test if $olddata really contains anything and return an error message if it doesn't
  235.                         if (!$olddata{
  236.                             error_box(nl2br(sm_encode_html_special_chars($abook->error)));
  237.                         else {
  238.                             /* Display the "new address" form */
  239.                             echo abook_create_form($form_url'editaddr',
  240.                                                    _("Update address"),
  241.                                                    _("Update address"),
  242.                                                    $current_backend,
  243.                                                    $olddata);
  244.                             echo addHidden('oldnick'$olddata['nickname']).
  245.                                 addHidden('backend'$olddata['backend']).
  246.                                 addHidden('doedit''1').
  247.                                 '</form>';
  248.                         }
  249.                     }
  250.                 elseif ($doedit == 1{
  251.                     /* Stage two: Write new data */
  252.                     $newdata $editaddr;
  253.                     $r $abook->modify($oldnick$newdata$backend);
  254.  
  255.                     /* Handle error messages */
  256.                     if (!$r{
  257.                         /* Display error */
  258.                         plain_error_messagenl2br(sm_encode_html_special_chars($abook->error)));
  259.  
  260.                         /* Display the "new address" form again */
  261.                         echo abook_create_form($form_url'editaddr',
  262.                                                _("Update address"),
  263.                                                _("Update address"),
  264.                                                $current_backend,
  265.                                                $newdata);
  266.                         echo addHidden('oldnick'$oldnick).
  267.                             addHidden('backend'$backend).
  268.                             addHidden('doedit',  '1').
  269.                             "\n" '</form>';
  270.                         $abortform true;
  271.                     }
  272.                 else {
  273.                     /**
  274.                      * $editaddr is set, but $sel (address selection in address listing)
  275.                      * and $doedit (address edit form) are not set.
  276.                      * Assume that user clicked on "Edit address" without selecting any address.
  277.                      */
  278.                     $formerror _("Please select address that you want to edit");
  279.                     $showaddrlist true;
  280.                 /* end of edit stage detection */
  281.             /* !empty($editaddr)                     - Update/modify address */
  282.         /* (!empty($deladdr)) && sizeof($sel) > 0    - Delete address(es) 
  283.           or (!empty($compose_to)) && sizeof($sel) > 0 - Compose to address(es) */
  284.     /* !empty($addaddr['nickname'])                  - Add new address */
  285.  
  286.     // Some times we end output before forms are printed
  287.     if($abortform{
  288. //FIXME: use footer.tpl; remove HTML from core
  289.         echo "</body></html>\n";
  290.         exit();
  291.     }
  292. }
  293.  
  294.  
  295. /* =================================================================== *
  296.  * The following is only executed on a GET request, or on a POST when  *
  297.  * a user is added, or when "delete" or "modify" was successful.       *
  298.  * =================================================================== */
  299.  
  300. /* Display error messages */
  301. if (!empty($formerror)) {
  302. }
  303.  
  304.  
  305. /* Display the address management part */
  306. $addresses array();
  307. while (list($k$backendeach ($abook->backends)) {
  308.     $a array();
  309.     $a['BackendID'$backend->bnum;
  310.     $a['BackendSource'$backend->sname;
  311.     $a['BackendWritable'$backend->writeable;
  312.     $a['Addresses'array();
  313.  
  314.     // don't do address lookup if we are not viewing that backend
  315.     //
  316.     if ($backend->bnum == $current_backend{
  317.         $alist $abook->list_addr($backend->bnum);
  318.  
  319.         /* check return (array with data or boolean false) */
  320.         if (is_array($alist)) {
  321.             usort($alist,'alistcmp');
  322.     
  323.             $a['Addresses'formatAddressList($alist);
  324.   
  325.             $addresses[$backend->bnum$a;
  326.         else {
  327.             // list_addr() returns boolean
  328.             plain_error_message(nl2br(sm_encode_html_special_chars($abook->error)));
  329.         }
  330.     else {
  331.         $addresses[$backend->bnum$a;
  332.     }
  333. }
  334.  
  335.  
  336. $current_page_args array(
  337.                            'abook_sort_order' => $abook_sort_order,
  338.                            'new_bnum'         => $current_backend,
  339.                            'page_number'      => $page_number,
  340.                           );
  341.  
  342.  
  343. // note that plugins can add to $current_page_args as well as
  344. // filter the address list
  345. //
  346. $temp array(&$addresses&$current_backend&$page_number&$current_page_args);
  347. do_hook('abook_list_filter'$temp);
  348.  
  349.  
  350. // NOTE to address book backend authors and plugin authors: if a backend does
  351. //      pagination (which might be more efficient), it needs to place a key
  352. //      in every address listing it returns called "paginated", whose value
  353. //      should evaluate to boolean TRUE.  However, if a plugin will also be
  354. //      used on the hook above to filter the addresses (perhaps by group), then
  355. //      the backend should be made compatible with the filtering plugin and
  356. //      should do the actual filtering too.  Otherwise, the backend will paginate
  357. //      before filtering has taken place, the output of which is clearly wrong.
  358. //      It is proposed that filtering be based on a GET/POST variable called
  359. //      "abook_groups_X" where X is the current backend number.  The value of
  360. //      this varaible would be an array of possible filter names, which the
  361. //      plugin and the backend would both know about.  The plugin would only
  362. //      filter based on that value if the backend didn't already do it.  The
  363. //      backend can insert a "grouped" key into all address listings, whose
  364. //      value evaluates to boolean TRUE, telling the plugin not to do any
  365. //      filtering itself.  For an example of this implementation, see the
  366. //      Address Book Grouping and Pagination plugin.
  367.  
  368.  
  369. // if no pagination was done by a plugin or the abook
  370. // backend (which is indicated by the presence of a
  371. // "paginated" key within all of the address entries
  372. // in the list of addresses for the backend currently
  373. // being viewed), then we provide default pagination
  374. //
  375. $total_addresses 0;
  376. if (!$show_all
  377.  && is_array($addresses[$current_backend]['Addresses'])
  378.  && empty($addresses[$current_backend]['Addresses'][0]['paginated'])) {
  379.  
  380.     // at this point, we assume the current list is
  381.     // the *full* list
  382.     //
  383.     $total_addresses sizeof($addresses[$current_backend]['Addresses']);
  384.  
  385.     // iterate through all the entries, building list of addresses
  386.     // to keep based on current page
  387.     //
  388.     $new_address_list array();
  389.     $total_pages ceil($total_addresses $page_size);
  390.     if ($page_number $total_pages$page_number $total_pages;
  391.     $page_count 1;
  392.     $page_item_count 0;
  393.     foreach ($addresses[$current_backend]['Addresses'as $addr{
  394.         $page_item_count++;
  395.         if ($page_item_count $page_size{
  396.             $page_count++;
  397.             $page_item_count 1;
  398.         }
  399.         if ($page_count == $page_number)
  400.             $new_address_list[$addr;
  401.     }
  402.     $addresses[$current_backend]['Addresses'$new_address_list;
  403.  
  404. }
  405.  
  406.  
  407. if ($showaddrlist{
  408.     
  409.     $oTemplate->assign('show_all'$show_all);
  410.     $oTemplate->assign('page_number'$page_number);
  411.     $oTemplate->assign('page_size'$page_size);
  412.     $oTemplate->assign('total_addresses'$total_addresses);
  413.     $oTemplate->assign('abook_compact_paginator'$abook_compact_paginator);
  414.     $oTemplate->assign('abook_page_selector'$abook_page_selector);
  415.     $oTemplate->assign('current_page_args'$current_page_args);
  416.     $oTemplate->assign('abook_page_selector_max'$abook_page_selector_max);
  417.     $oTemplate->assign('addresses'$addresses);
  418.     $oTemplate->assign('current_backend'$current_backend);
  419.     $oTemplate->assign('backends'$list_backends);
  420.     $oTemplate->assign('abook_has_extra_field'$abook->add_extra_field);
  421.     $oTemplate->assign('compose_new_win'$compose_new_win);
  422.     $oTemplate->assign('compose_height'$compose_height);
  423.     $oTemplate->assign('compose_width'$compose_width);
  424.     $oTemplate->assign('form_action'$form_url);
  425.         
  426.     $oTemplate->display('addressbook_list.tpl');
  427.     
  428. }
  429.  
  430. /* Display the "new address" form */
  431. //FIXME: Remove HTML from here! (echo abook_create_form() is OK, since it is all template based output
  432. echo '<a name="AddAddress"></a>' "\n";
  433. echo abook_create_form($form_url'addaddr',
  434.                        _("Add to address book"),
  435.                        _("Add address"),
  436.                        $current_backend,
  437.                        $defdata);
  438. echo "</form>\n";
  439.  
  440. /* Hook for extra address book blocks */
  441. do_hook('addressbook_bottom'$null);
  442.  
  443. $oTemplate->display('footer.tpl');

Documentation generated on Sat, 26 Jul 2014 04:17:15 +0200 by phpDocumentor 1.4.3