summaryrefslogtreecommitdiff
path: root/manager/application/controllers/MailController.php
blob: 1ba73e24a095f04ca8f547ff0bab8ec722a51e8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
 * @author markus
 * $Id: IndexController.php 6 2009-11-18 14:52:50Z markus $
 */

require_once(LIBRARY_PATH . '/imap/imapConnection.php');

class MailController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
		$session = Zend_Registry::get('session');
    	$auth = $session->authdata['authed_permissions'];

    	$action = $this->getRequest()->getActionName();

    	$this->view->leftNav('<a href="' .
    		$this->view->url(array('controller' => 'mail', 'action' => 'index'), 'default', true) .
    		'"' . (($action == 'index')?' class="active"':'') . '>' . I18n::_('View own Mails') . '</a>', Zend_View_Helper_Placeholder_Container_Abstract::SET, 1);
    	if ($session->authdata['authed_role'] == 'Admin') {
	   		$this->view->leftNav('<a href="' .
	    		$this->view->url(array('controller' => 'mail', 'action' => 'full'), 'default', true) .
	    		'"' . (($action == 'full')?' class="active"':'') . '>' . I18n::_('View all Mails') . '</a>', Zend_View_Helper_Placeholder_Container_Abstract::SET, 2);
    	}
    }

    public function indexAction()
    {
    	$config = Zend_Registry::get('config');
    	$session = Zend_Registry::get('session');

		$imap_config = $config->imap;
        $imap = imapConnection::getInstance('cacert', $imap_config);
		$imap->imapSwitchMbox('INBOX');

        $ck = $imap->imapCheck();

        $headers = array();
        for ($i=0; $i < $ck->Nmsgs; $i++) {
        	$header = $imap->imapHeader($i+1);

        	// skip all emails that do not belong to the user
        	if ($header->toaddress != $session->authdata['authed_username'])
				continue;

        	$header->uid = $imap->imapUID($i+1);
        	$header->detailslink = $this->view->url(array('controller' => 'mail', 'action' => 'read', 'uid' => $header->uid), 'default', true);
        	$header->deletelink = $this->view->url(array('controller' => 'mail', 'action' => 'delete', 'uid' => $header->uid), 'default', true);
        	$headers[] = $header;
        }

        $this->view->headers = $headers;
    }

    public function fullAction()
    {
    	$config = Zend_Registry::get('config');

		$imap_config = $config->imap;
        $imap = imapConnection::getInstance('cacert', $imap_config);
		$imap->imapSwitchMbox('INBOX');

        $ck = $imap->imapCheck();

        $headers = array();
        for ($i=0; $i < $ck->Nmsgs; $i++) {
        	$header = $imap->imapHeader($i+1);
        	$header->uid = $imap->imapUID($i+1);
        	$header->detailslink = $this->view->url(array('controller' => 'mail', 'action' => 'read', 'uid' => $header->uid), 'default', true);
        	$header->deletelink = $this->view->url(array('controller' => 'mail', 'action' => 'delete', 'uid' => $header->uid), 'default', true);
        	$headers[] = $header;
        }

        $this->view->headers = $headers;
    }

    public function readAction()
    {
    	$config = Zend_Registry::get('config');
		$imap_config = $config->imap;
        $imap = imapConnection::getInstance('cacert', $imap_config);
		$imap->imapSwitchMbox('INBOX');

		$uid = $this->getRequest()->getParam('uid');

		$body = $imap->imapBodyByUID($uid);

		$this->view->mail_body = $body;
    }

    /**
     * delete message with unique id
     */
	public function deleteAction()
    {
    	$config = Zend_Registry::get('config');
		$uid = $this->getRequest()->getParam('uid', -1);
		$this->view->returnto = $_SERVER['HTTP_REFERER'];

		if ($uid == -1) {
			$this->view->message = I18n::_('You did not select an email for deletion');
		}
		elseif ($this->view->returnto == '') {
			$this->view->message = I18n::_('Please use the delete icons in the mail inventory to delete mails');
		}
		else {
			$imap_config = $config->imap;
	        $imap = imapConnection::getInstance('cacert', $imap_config);
			$imap->imapSwitchMbox('INBOX');

			$header = $imap->imapFetchOverview($uid);

			$session = Zend_Registry::get('session');
			if ($session->authdata['authed_role'] != 'Admin' && $header->to != $session->authdata['authed_username']) {
				$this->view->message = I18n::_('This message does not belong to you');
			}
			else {
	        	$imap->imapDelete($uid);
	        	$imap->imapExpunge();
	        	$this->view->message = I18n::_('Message deleted');
			}
		}
    }
}