Factor out functionality into a model so the controllers get more lightweight
[cacert-mgr.git] / manager / application / controllers / MailController.php
1 <?php
2 /**
3 * @author markus
4 * $Id: IndexController.php 6 2009-11-18 14:52:50Z markus $
5 */
6
7 require_once(LIBRARY_PATH . '/imap/imapConnection.php');
8
9 class MailController extends Zend_Controller_Action
10 {
11 /**
12 * list of email addresses associated with that account
13 * @var array
14 */
15 private $addresses = array();
16
17 public function init()
18 {
19 /* Initialize action controller here */
20 $session = Zend_Registry::get('session');
21 $auth = $session->authdata['authed_permissions'];
22
23 $action = $this->getRequest()->getActionName();
24
25 $this->view->leftNav('<a href="' .
26 $this->view->url(array('controller' => 'mail', 'action' => 'index'), 'default', true) .
27 '"' . (($action == 'index')?' class="active"':'') . '>' . I18n::_('View own Mails') . '</a>', Zend_View_Helper_Placeholder_Container_Abstract::SET, 1);
28 if ($session->authdata['authed_role'] == 'Admin') {
29 $this->view->leftNav('<a href="' .
30 $this->view->url(array('controller' => 'mail', 'action' => 'full'), 'default', true) .
31 '"' . (($action == 'full')?' class="active"':'') . '>' . I18n::_('View all Mails') . '</a>', Zend_View_Helper_Placeholder_Container_Abstract::SET, 2);
32 }
33
34 $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
35 $db = Zend_Db::factory($config->ca_mgr->db->auth->pdo, $config->ca_mgr->db->auth);
36 $emails = new CAcert_User_Emails($db);
37
38 $this->addresses = $emails->getEmailAddressesByLogin($session->authdata['authed_username']);
39
40 }
41
42 public function indexAction()
43 {
44 $config = Zend_Registry::get('config');
45 $session = Zend_Registry::get('session');
46
47 $imap_config = $config->imap;
48 $imap = imapConnection::getInstance('cacert', $imap_config);
49 $imap->imapSwitchMbox('INBOX');
50
51 $ck = $imap->imapCheck();
52
53 $headers = array();
54 for ($i=0; $i < $ck->Nmsgs; $i++) {
55 $header = $imap->imapHeader($i+1);
56
57 // skip all emails that do not belong to the user
58 if (!in_array($header->toaddress, $this->addresses))
59 continue;
60
61 $header->uid = $imap->imapUID($i+1);
62 $header->detailslink = $this->view->url(array('controller' => 'mail', 'action' => 'read', 'uid' => $header->uid), 'default', true);
63 $header->deletelink = $this->view->url(array('controller' => 'mail', 'action' => 'delete', 'uid' => $header->uid), 'default', true);
64 $headers[] = $header;
65 }
66
67 $this->view->headers = $headers;
68 }
69
70 public function fullAction()
71 {
72 $config = Zend_Registry::get('config');
73
74 $imap_config = $config->imap;
75 $imap = imapConnection::getInstance('cacert', $imap_config);
76 $imap->imapSwitchMbox('INBOX');
77
78 $ck = $imap->imapCheck();
79
80 $headers = array();
81 for ($i=0; $i < $ck->Nmsgs; $i++) {
82 $header = $imap->imapHeader($i+1);
83 $header->uid = $imap->imapUID($i+1);
84 $header->detailslink = $this->view->url(array('controller' => 'mail', 'action' => 'read', 'uid' => $header->uid), 'default', true);
85 $header->deletelink = $this->view->url(array('controller' => 'mail', 'action' => 'delete', 'uid' => $header->uid), 'default', true);
86 $headers[] = $header;
87 }
88
89 $this->view->headers = $headers;
90 }
91
92 public function readAction()
93 {
94 $config = Zend_Registry::get('config');
95 $imap_config = $config->imap;
96 $imap = imapConnection::getInstance('cacert', $imap_config);
97 $imap->imapSwitchMbox('INBOX');
98
99 $uid = $this->getRequest()->getParam('uid');
100
101 $body = $imap->imapBodyByUID($uid);
102
103 $this->view->mail_body = $body;
104 }
105
106 /**
107 * delete message with unique id
108 */
109 public function deleteAction()
110 {
111 $config = Zend_Registry::get('config');
112 $uid = $this->getRequest()->getParam('uid', -1);
113 $this->view->returnto = $_SERVER['HTTP_REFERER'];
114
115 if ($uid == -1) {
116 $this->view->message = I18n::_('You did not select an email for deletion');
117 }
118 elseif ($this->view->returnto == '') {
119 $this->view->message = I18n::_('Please use the delete icons in the mail inventory to delete mails');
120 }
121 else {
122 $imap_config = $config->imap;
123 $imap = imapConnection::getInstance('cacert', $imap_config);
124 $imap->imapSwitchMbox('INBOX');
125
126 $header = $imap->imapFetchOverview($uid);
127
128 $session = Zend_Registry::get('session');
129
130 if ($session->authdata['authed_role'] != 'Admin' && !in_array($header->to, $this->addresses)) {
131 $this->view->message = I18n::_('This message does not belong to you');
132 }
133 else {
134 $imap->imapDelete($uid);
135 $imap->imapExpunge();
136 $this->view->message = I18n::_('Message deleted');
137 }
138 }
139 }
140 }