Changeset 219

Show
Ignore:
Timestamp:
12/09/07 00:12:27 (1 year ago)
Author:
shupp
Message:

add Framework_Module_Accounts_Modify, clean up accounts, modify Domains and Home to handle new class

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Framework/Module/Accounts.php

    r208 r219  
    11<?php 
    2  
    3 /** 
    4  * 
    5  * Accounts Module 
    6  * 
    7  * This module is for viewing and editing vpopmail accounts 
    8  * 
    9  * @author Bill Shupp <hostmaster@shupp.org> 
    10  * @package ToasterAdmin 
    11  * @version 1.0 
    12  * 
    13  */ 
    14  
    152 
    163/** 
    174 * Framework_Module_Accounts  
    185 *  
    19  * @package ToasterAdmin 
    20  * @copyright 2005-2006 Bill Shupp 
    21  * @author Bill Shupp <hostmaster@shupp.org>  
    22  * @license GPL 2.0  {@link http://www.gnu.org/licenses/gpl.txt} 
     6 * This module is for viewing and editing vpopmail accounts 
     7 *  
     8 * @package   ToasterAdmin 
     9 * @uses      ToasterAdmin_Auth_Domain 
     10 * @author    Bill Shupp <hostmaster@shupp.org>  
     11 * @copyright 2007 Bill Shupp 
     12 * @license   GPL 2.0  {@link http://www.gnu.org/licenses/gpl.txt} 
     13 * @link      http://trac.merchbox.com/trac/toasteradmin 
    2314 */ 
    24 class Framework_Module_Accounts extends ToasterAdmin_Common 
     15 
     16/** 
     17 * Framework_Module_Accounts  
     18 *  
     19 * This module is for viewing and editing vpopmail accounts 
     20 *  
     21 * @package   ToasterAdmin 
     22 * @uses      ToasterAdmin_Auth_Domain 
     23 * @author    Bill Shupp <hostmaster@shupp.org>  
     24 * @copyright 2007 Bill Shupp 
     25 * @license   GPL 2.0  {@link http://www.gnu.org/licenses/gpl.txt} 
     26 * @link      http://trac.merchbox.com/trac/toasteradmin 
     27 */ 
     28class Framework_Module_Accounts extends ToasterAdmin_Auth_Domain 
    2529{ 
    2630 
     
    2832     * __construct  
    2933     *  
    30      * class constructor 
     34     * Throw an exception if no domain was supplied 
    3135     *  
    3236     * @access protected 
     
    3539    function __construct() { 
    3640        parent::__construct(); 
    37         if (($result = $this->noDomainSupplied())) { 
    38             return $result; 
    39         } 
     41        $this->noDomainSupplied(); 
    4042    } 
    4143 
     
    4648     * @return void 
    4749     */ 
    48     function __default() { 
     50    public function __default() { 
    4951        $this->listAccounts(); 
    5052    } 
    5153 
    52     function listAccounts() { 
    53  
    54         if (($result = $this->noDomainPrivs())) { 
    55             return $result; 
    56         } 
    57  
    58         // Pagintation setup 
     54    public function listAccounts() { 
     55        // Pagintation setup - let exception bubble up 
    5956        $total = $this->user->userCount($this->domain); 
    60         if (PEAR::isError($total)) { 
    61             return $total; 
    62         } 
    6357        $this->paginate($total); 
    6458 
     
    10296     * @return void 
    10397     */ 
    104     function addAccount() { 
    105  
    106         if (($result = $this->noDomainPrivs())) { 
    107             return $result; 
    108         } 
    109  
     98    public function addAccount() { 
    11099        $form = $this->addAccountForm(); 
    111100        $renderer =& new HTML_QuickForm_Renderer_AssocArray(); 
     
    116105    } 
    117106 
    118     function addAccountNow() { 
    119  
    120         if (($result = $this->noDomainPrivs())) { 
    121             return $result; 
    122         } 
    123  
     107    public function addAccountNow() { 
    124108        $form = $this->addAccountForm(); 
    125109        if (!$form->validate()) { 
     
    132116 
    133117        $emailArray = explode('@', $_REQUEST['account']); 
    134         $result = $this->user->addUser($this->domain, $emailArray[0], $_REQUEST['password']); 
    135         if(!PEAR::isError($result)) { 
    136             // Update gecos 
    137             $result = $this->user->modUser($this->domain, $emailArray[0], array('comment' => $_REQUEST['comment'])); 
    138         } 
    139         if (PEAR::isError($result)) { 
    140             $this->setData('message', _("Error: ") . $result->getMessage()); 
     118        try { 
     119            $result = $this->user->addUser($this->domain, $emailArray[0], $_REQUEST['password']); 
     120        } catch (Net_Vpopmaild_Exception $e) { 
     121            $this->setData('message', _("Error: ") . $e->getMessage()); 
    141122            $renderer =& new HTML_QuickForm_Renderer_AssocArray(); 
    142123            $form->accept($renderer); 
     
    145126            return; 
    146127        } 
     128        // Update gecos 
     129        $result = $this->user->modUser($this->domain, $emailArray[0], array('comment' => $_REQUEST['comment'])); 
    147130 
    148131        $this->setData('message', _("Account Added Successfully")); 
     
    152135 
    153136    private function addAccountForm() { 
    154  
    155137        $this->setData('LANG_Add_Account_to_domain', _("Add Account to domain")); 
    156138        $this->setData('LANG_Domain_Menu', _("Domain Menu")); 
     
    174156    } 
    175157 
    176     function delete() { 
    177  
    178         if (($result = $this->noDomainPrivs())) { 
    179             return $result; 
    180         } 
    181  
     158    public function delete() { 
    182159        if (!isset($_REQUEST['account'])) { 
    183             return PEAR::raiseError(_("Error: no account supplied")); 
     160            throw new Framework_Exception (_("Error: no account supplied")); 
    184161        } 
    185162 
     
    195172 
    196173    function deleteNow() { 
    197  
    198         if (($result = $this->noDomainPrivs())) { 
    199             return $result; 
     174        if (!isset($_REQUEST['account'])) { 
     175            throw new Framework_Exception (_("Error: no account supplied")); 
    200176        } 
    201  
    202         if (!isset($_REQUEST['account'])) { 
    203             return PEAR::raiseError(_("Error: no account supplied")); 
    204         } 
    205  
    206177        if (!isset($_REQUEST['domain'])) { 
    207             return PEAR::raiseError(_("Error: no domain supplied")); 
     178            throw new Framework_Exception (_("Error: no domain supplied")); 
    208179        } 
    209180 
    210181        $result = $this->user->delUser($this->domain, $_REQUEST['account']); 
    211         if (PEAR::isError($result)) { 
    212             return $result; 
    213         } 
    214182 
    215183        $this->setData('message', _("Account Deleted Successfully")); 
     
    219187 
    220188    function cancelDelete() { 
    221  
    222         if (($result = $this->noDomainPrivs())) { 
    223             return $result; 
    224         } 
    225  
    226189        $this->setData('message', _("Delete Canceled")); 
    227190        $this->listAccounts(); 
     
    229192    } 
    230193 
    231     function modifyAccount() { 
    232  
    233         // Make sure account was supplied 
    234         if (!isset($_REQUEST['account'])) { 
    235             return PEAR::raiseError(_("Error: no account supplied")); 
    236         } 
    237         $account = $_REQUEST['account']; 
    238  
    239         // Check privs 
    240         if (!$this->user->isUserAdmin($account, $this->domain)) { 
    241             return PEAR::raiseError(_('Error: you do not have edit privileges on domain ') . $this->domain); 
    242         } 
    243  
    244         // See what user_info to use 
    245         if ($this->user->isDomainAdmin($this->domain)) { 
    246             $account_info = $this->user->userInfo($this->domain, $_REQUEST['account']); 
    247             if (PEAR::isError($account_info)) return $account_info; 
    248         } else { 
    249             $account_info = $this->user->loginUser; 
    250         } 
    251  
    252         // Get .qmail info if it exists 
    253         try { 
    254             $dot_qmail = $this->user->readFile($this->domain, $_REQUEST['account'], '.qmail'); 
    255         } catch (Net_Vpopmaild_Exception $e) { 
    256         } 
    257         $defaults = $this->parseHomeDotqmail($dot_qmail, $account_info); 
    258         $this->user->recordio(print_r($defaults, 1)); 
    259         $form = $this->modifyAccountForm($account, $defaults); 
    260         $renderer =& new HTML_QuickForm_Renderer_AssocArray(); 
    261         $form->accept($renderer); 
    262         if(isset($_REQUEST['modified'])) { 
    263             $this->setData('message', _('Account Modified Successfully')); 
    264         } 
    265         $this->setData('form', $renderer->toAssocArray()); 
    266         $this->tplFile = 'modifyAccount.tpl'; 
    267         return; 
    268     } 
    269  
    270     private function modifyAccountForm($account, $defaults) { 
    271  
    272         // Language stuff 
    273         $this->setData('LANG_Modify_Account', _("Modify Account")); 
    274         $this->setData('LANG_Domain_Menu', _("Domain Menu")); 
    275         if ($this->user->isDomainAdmin($this->domain)) { 
    276             $this->setData('isDomainAdmin', 1); 
    277             $this->setData('LANG_Main_Menu', _("Main Menu")); 
    278         } 
    279         $this->setData('account', $account); 
    280  
    281         $form = new HTML_QuickForm('formModifyAccount', 'post', "./?module=Accounts&event=modifyAccountNow&domain={$this->domain}&account=$account"); 
    282  
    283         $form->setDefaults($defaults); 
    284  
    285         $form->addElement('text', 'comment', _("Real Name/Comment")); 
    286         $form->addElement('password', 'password', _("Password")); 
    287         $form->addElement('password', 'password2', _("Re-Type Password")); 
    288         $form->addElement('radio', 'routing', 'Mail Routing', _('Standard (No Forwarding)'), 'routing_standard'); 
    289         $form->addElement('radio', 'routing', '', _('All Mail Deleted'), 'routing_deleted'); 
    290         $form->addElement('radio', 'routing', '', _('Forward to:'), 'routing_forwarded'); 
    291         $form->addElement('text', 'forward'); 
    292         $form->addElement('checkbox', 'save_a_copy', _('Save A Copy')); 
    293  
    294         $form->addElement('checkbox', 'vacation', _('Send a Vacation Auto-Response')); 
    295         $form->addElement('text', 'vacation_subject', _('Vacation Subject:')); 
    296         $form->addElement('textarea', 'vacation_body', _('Vacation Message:'), 'rows="10" cols="40"'); 
    297         $form->addElement('submit', 'submit', _('Modify Account')); 
    298  
    299         $form->addRule(array('password', 'password2'), _('The passwords do not match'), 'compare', null, 'client'); 
    300         $form->addRule('routing', _('Please select a mail routing type'), 'required', null, 'client'); 
    301         $form->addRule('forward', _('"Forward to" must be a valid email address'), 'email', null, 'client'); 
    302  
    303         return $form; 
    304     } 
    305  
    306  
    307     function modifyAccountNow() { 
    308  
    309         // Make sure account was supplied 
    310         if (!isset($_REQUEST['account'])) { 
    311             return PEAR::raiseError(_("Error: no account supplied")); 
    312         } 
    313         $account = $_REQUEST['account']; 
    314  
    315         // Check privs 
    316         if (!$this->user->isUserAdmin($account, $this->domain)) { 
    317             return PEAR::raiseError(_('Error: you do not have edit privileges on domain ') . $this->domain); 
    318         } 
    319  
    320         // See what user_info to use 
    321         $account_info = $this->user->userInfo($this->domain, $_REQUEST['account']); 
    322         if (PEAR::isError($account_info)) { 
    323             return $account_info; 
    324         } 
    325  
    326         // Get .qmail info if it exists 
    327         try { 
    328             $dot_qmail = $this->user->readFile($this->domain, $_REQUEST['account'], '.qmail'); 
    329         } catch (Net_Vpopmaild_Exception $e) { 
    330         } 
    331         $defaultsOrig = $this->parseHomeDotqmail($dot_qmail, $account_info); 
    332         $form = $this->modifyAccountForm($account, $defaultsOrig); 
    333         if (!$form->validate()) { 
    334             $this->setData('message', _("Error Modifying Account")); 
    335             $renderer =& new HTML_QuickForm_Renderer_AssocArray(); 
    336             $form->accept($renderer); 
    337             $this->setData('form', $renderer->toAssocArray()); 
    338             $this->tplFile = 'modifyAccount.tpl'; 
    339             return; 
    340         } 
    341  
    342         // Determine new routing 
    343         $routing = ''; 
    344         $save_a_copy = 0; 
    345         if ($_REQUEST['routing'] == 'routing_standard') { 
    346             $routing = 'standard'; 
    347         } else if ($_REQUEST['routing'] == 'routing_deleted') { 
    348             $routing = 'deleted'; 
    349         } else if ($_REQUEST['routing'] == 'routing_forwarded') { 
    350             if (empty($_REQUEST['forward'])) { 
    351                 $this->setData('message', _('Error: you must supply a forward address')); 
    352                 return $this->modifyAccount(); 
    353             } else { 
    354                 $forward = $_REQUEST['forward']; 
    355             } 
    356             $routing = 'forwarded'; 
    357             if (isset($_REQUEST['save_a_copy'])) $save_a_copy = 1; 
    358         } else { 
    359                 $this->setData('message', _('Error: unsupported routing selection')); 
    360                 return $this->modifyAccount(); 
    361         } 
    362  
    363         // Check for vacation 
    364         $vacation = 0; 
    365         if (isset($_REQUEST['vacation']) && $_REQUEST['vacation'] == 1) { 
    366             $vacation = 1; 
    367             $vacation_subject = $_REQUEST['vacation_subject']; 
    368             $vacation_body = $_REQUEST['vacation_body']; 
    369         } 
    370  
    371         // Are we deleting a vacation message? 
    372         if ($vacation == 0 && $defaultsOrig['vacation'] == ' checked') { 
    373             // Kill old message 
    374             $this->user->rmDir($this->domain, $account_info['name'], 'vacation'); 
    375         } 
    376  
    377         // Build .qmail contents 
    378         $dot_qmail_contents = ''; 
    379         if ($routing == 'deleted') { 
    380             $dot_qmail_contents = "# delete"; 
    381         } else if ($routing == 'forwarded') { 
    382             $dot_qmail_contents = "&$forward"; 
    383             if ($save_a_copy == 1) $dot_qmail_contents .= "\n./Maildir/"; 
    384         } 
    385  
    386         if ($vacation == 1) { 
    387             if (strlen($dot_qmail_contents) > 0) $dot_qmail_contents .= "\n"; 
    388             $vacation_dir = $account_info['user_dir'] . '/vacation'; 
    389             $dot_qmail_contents .= '| ' . $this->user->vpopmailRobotProgram; 
    390             $dot_qmail_contents .= ' ' . $this->user->vpopmailRobotTime; 
    391             $dot_qmail_contents .= ' ' . $this->user->vpopmailRobotNumber; 
    392             $dot_qmail_contents .= " $vacation_dir/message $vacation_dir"; 
    393         } 
    394  
    395         $dot_qmail_file = '.qmail'; 
    396         if (strlen($dot_qmail_contents) > 0) { 
    397             $contents = explode("\n", $dot_qmail_contents); 
    398             // Write .qmail file 
    399             $result = $this->user->writeFile($contents, $this->domain, $account_info['name'], $dot_qmail_file); 
    400  
    401             // Add vacation files 
    402             if ($vacation == 1) { 
    403                 $vcontents = "From: " . $account_info['name'] . "@{$this->domain}\n"; 
    404                 $vcontents .= "Subject: $vacation_subject\n\n"; 
    405                 $vcontents .= $vacation_body; 
    406                 $contents = explode("\n", $vcontents); 
    407                 $vdir = 'vacation'; 
    408                 $message = 'vacation/message'; 
    409                 // Delete existing file 
    410                 try { 
    411                     $this->user->rmDir($this->domain, $account_info['name'], $vdir); 
    412                 } catch (Net_Vpopmaild_Exception $e) { 
    413                 } 
    414                 // Make vacation directory 
    415                 $result = $this->user->mkDir($this->domain, $account_info['name'], $vdir); 
    416                 // Write vacation message 
    417                 $result = $this->user->writeFile($contents, $this->domain, $account_info['name'], $message); 
    418             } 
    419         } else { 
    420             try { 
    421                 $this->user->rmFile($this->domain, $account_info['name'], $dot_qmail_file); 
    422             } catch (Net_Vpopmaild_Exception $e) { 
    423             } 
    424         } 
    425  
    426         $url = "./?module=Accounts&event=modifyAccount&domain={$this->domain}&account={$account_info['name']}&modified=1"; 
    427         header("Location: $url"); 
    428         return $this->modifyAccount(); 
    429     } 
    430  
    431     /** 
    432      * Parse Home dot-qmail 
    433      * 
    434      * Evaluate contents of a .qmail file in a user's home directory. 
    435      * Looking for routing types standard, delete, or forward, with optional 
    436      * saving of messages, as well as vacation messages. 
    437      * 
    438      * @param mixed $contents     .qmail contents 
    439      * @param mixed $account_info user account info 
    440      * 
    441      * @access protected 
    442      * @return array $defaults 
    443      */ 
    444     protected function parseHomeDotqmail($contents, $account_info) 
    445     { 
    446         $is_standard  = false; 
    447         $is_deleted   = false; 
    448         $is_forwarded = false; 
    449         // Set default template settings 
    450         $defaults['comment']          = $account_info['comment']; 
    451         $defaults['forward']          = ''; 
    452         $defaults['save_a_copy']      = ''; 
    453         $defaults['vacation']         = ''; 
    454         $defaults['vacation_subject'] = ''; 
    455         $defaults['vacation_body']    = ''; 
    456         if (empty($contents)) { 
    457             $is_standard = true; 
    458         } 
    459         if ((is_array($contents) 
    460             && count($contents) == 1 
    461             && $contents[0] == '# delete')) { 
    462             $is_deleted = true; 
    463         } 
    464         if ($is_standard) { 
    465             $defaults['routing'] = 'routing_standard'; 
    466         } else if ($is_deleted) { 
    467             $defaults['routing'] = 'routing_deleted'; 
    468         } else { 
    469             // now let's parse it 
    470             while (list($key, $val) = each($contents)) { 
    471                 if ($val == $account_info['user_dir'].'/Maildir/' 
    472                     || $val == './Maildir/') { 
    473  
    474                     $defaults['save_a_copy'] = ' checked'; 
    475                     continue; 
    476                 } 
    477                 if (preg_match("({$this->user->vpopmailRobotProgram})", $val)) { 
    478                     $vacation_array = $this->user->getVacation($account_info, $val); 
    479  
    480                     while (list($vacKey, $vacVal) = each($vacation_array)) { 
    481                         $defaults[$vacKey] = $vacVal; 
    482                     } 
    483                     continue; 
    484                 } else { 
    485                     if (Validate::email(preg_replace('/^&/', '', $val), 
    486                         array('use_rfc822' => 1))) { 
    487  
    488                         $is_forwarded        = true; 
    489                         $defaults['routing'] = 'routing_forwarded'; 
    490                         $defaults['forward'] = preg_replace('/^&/', '', $val); 
    491                     } 
    492                 } 
    493             } 
    494             // See if default routing select applies 
    495             if (!$is_standard && !$is_deleted && !$is_forwarded) { 
    496                 $defaults['routing'] = 'routing_standard'; 
    497             } 
    498         } 
    499         return $defaults; 
    500     } 
    501  
    502194} 
    503195 
  • trunk/Framework/Module/Domains.php

    r215 r219  
    6464        while (list($key,$val) = each($domainArray)) { 
    6565            $domains[$count]['name'] = $key; 
    66             $domains[$count]['edit_url'] = htmlspecialchars('./?module=Domains&class=domainMenu&domain=' . $key); 
     66            $domains[$count]['edit_url'] = htmlspecialchars('./?module=Domains&class=Menu&domain=' . $key); 
    6767            $domains[$count]['delete_url'] = htmlspecialchars('./?module=Domains&event=delDomain&domain=' . $key); 
    6868            $count++; 
     
    116116 
    117117        // Add domain 
    118         $result = $this->user->AddDomain($_REQUEST['domain'], $_REQUEST['password']); 
    119         if (PEAR::isError($result)) { 
    120             $this->setData('message', _("Error: ") . $result->getMessage()); 
     118        try { 
     119            $result = $this->user->addDomain($form->getElementValue('domain'), $form->getElementValue('password')); 
     120        } catch (Net_Vpopmaild_Exception $e) { 
     121            $this->setData('message', _("Error: ") . $e->getMessage()); 
    121122            return $this->addDomain(); 
    122123        } 
    123         header('./?module=Domains&class=Menu&success'); 
     124        header('Location: ./?module=Domains&class=Menu&success&domain=' . $form->getElementValue('domain')); 
    124125        return; 
    125126    } 
  • trunk/Framework/Module/Home.php

    r215 r219  
    4040            return; 
    4141        } else { 
    42             header("Location: ./?module=Accounts&domain=" . urlencode($domain) . '&account=' . urlencode($this->session->user) . '&event=modifyAccount'); 
     42            header("Location: ./?module=Accounts&class=Modify&domain=" . urlencode($domain) . '&account=' . urlencode($this->session->user) . '&event=modifyAccount'); 
    4343            return; 
    4444        }