1<?php
2
3class GoBaseBackendDiff extends \BackendDiff {
4
5	protected $_server_delimiter;
6	private static $_account=false;
7
8	/**
9	 * Get the imap account of the current user
10	 *
11	 * @return \GO\Email\Model\Account
12	 */
13	public function getImapAccount(){
14		if(!\GO::modules()->email){
15			ZLog::Write(LOGLEVEL_INFO, 'GoBaseBackendDiff->getImapAccount() ~~ EMAIL MODULE IS NOT INSTALLED!');
16			return false;
17		}
18
19		if(!self::$_account){
20			$settings  = GoSyncUtils::getUserSettings();
21			if(!empty($settings->account_id)) {
22				try{
23					self::$_account = \GO\Email\Model\Account::model()->findByPk($settings->account_id);
24					if(!self::$_account){
25						ZLog::Write(LOGLEVEL_FATAL, 'E-mail account not found!');
26					}
27				}catch(\GO\Base\Exception\AccessDenied $e){
28					ZLog::Write(LOGLEVEL_FATAL, 'GoBaseBackendDiff->getImapAccount() ~~ ACCESS DENIED to e-mail account configured in sync settings('.(string)$e->getMessage().')');
29				}
30			}else
31			{
32				ZLog::Write(LOGLEVEL_FATAL, 'No e-mail account. Settings: '.var_export($settings->getAttributes(), true));
33			}
34		}
35		return self::$_account;
36	}
37
38	/**
39	 * Get the folder ID of the imap trash folder
40	 *
41	 * @return StringHelper
42	 */
43	public function getImapTrashFolderId(){
44		$imapAccount = $this->getImapAccount();
45		return $imapAccount ? $imapAccount->trash : false;
46	}
47
48	/**
49	 * Get the folder ID of the imap sent items folder
50	 *
51	 * @return StringHelper
52	 */
53	public function getImapSentFolderId(){
54		$imapAccount = $this->getImapAccount();
55		return $imapAccount ? $imapAccount->sent : false;
56	}
57
58	/**
59	 * Connect to the imap server
60	 *
61	 * @param type $mailbox
62	 * @return \GO\Base\Mail\Imap
63	 */
64	protected function _imapLogon($mailbox = 'INBOX') {
65
66		if ($mailbox != 'INBOX')
67			$mailbox = $this->_replaceDotWithServerDelimiter($mailbox);
68
69		$imapAccount = $this->getImapAccount();
70		if(!$imapAccount)
71			return false;
72
73		$conn = false;
74
75		try {
76			$conn = $imapAccount->openImapConnection($mailbox);
77
78			if(!$conn)
79				ZLog::Write(LOGLEVEL_FATAL, 'GoBaseBackendDiff->_imapLogon('.$mailbox.') ~~ IMAP LOGIN FAILED (account: '.$imapAccount->username.')');
80			else
81				ZLog::Write(LOGLEVEL_INFO, 'GoBaseBackendDiff->_imapLogon('.$mailbox.') ~~ IMAP LOGIN SUCCESS (account: '.$imapAccount->username.')');
82
83		} catch (\Exception $e) {
84			ZLog::Write(LOGLEVEL_FATAL, 'GoBaseBackendDiff->_imapLogon('.$mailbox.') ~~ OPEN IMAP CONNECTION FAILED ('.(string)$e->getMessage().')');
85		}
86
87		return $conn;
88	}
89
90	/**
91	 * Modify the folderid string so it will use the correct server delimiter
92	 *
93	 * @param StringHelper $folderid
94	 * @return StringHelper
95	 */
96	protected function _replaceDotWithServerDelimiter($folderid) {
97		return str_replace('.', $this->_getServerDelimiter(), $folderid);
98	}
99
100	/**
101	 * Get the server delimiter of the imap server
102	 *
103	 * @return StringHelper
104	 */
105	protected function _getServerDelimiter() {
106		if (!$this->_server_delimiter) {
107			$imap = $this->_imapLogon();
108			if(!$imap)
109				throw new StatusException("Could not logon to IMAP server");
110
111			$this->_server_delimiter = $imap->get_mailbox_delimiter();
112		}
113		return $this->_server_delimiter;
114	}
115
116	/**
117	 * Get the stat of the folder with the given id
118	 *
119	 * @param StringHelper $id
120	 * @return array
121	 */
122	public function StatFolder($id) {
123		$folder = $this->GetFolder($id);
124		if(!$folder) {
125			return false;
126		}
127		$stat = array();
128		$stat['id'] = $id;
129		$stat['parent'] = $folder->parentid;
130		$stat['mod'] = $folder->displayname;
131		ZLog::Write(LOGLEVEL_DEBUG, 'ZPUSH2::StatFolder'.$id);
132		return $stat;
133	}
134
135	/**
136	 * Get the folder
137	 *
138	 * ** THIS FUNCTION NEEDS TO BE OVERRIDDEN BY BACKENDS THAT EXTEND FROM THIS BACKEND **
139	 *
140	 * @param StringHelper $id
141	 * @return boolean
142	 */
143	public function GetFolder($id) {
144		return false;
145	}
146
147
148
149	public function GetMessage($folderid, $id, $contentparameters) {
150		return false;
151	}
152
153	public function ChangeMessage($folderid, $id, $message, $contentParameters) {
154		return false;
155	}
156
157	public function StatMessage($folderid, $id) {
158		return false;
159	}
160
161	public function GetMessageList($folderid, $cutoffdate) {
162		return false;
163	}
164
165	public function GetFolderList() {
166		return false;
167	}
168
169	/**
170	 * Login function
171	 *
172	 * This function is a dummy one,
173	 * the login process is already been handled in the go.php file.
174	 *
175	 * @param StringHelper $username
176	 * @param StringHelper $domain
177	 * @param StringHelper $password
178	 * @return boolean
179	 */
180	public function Logon($username, $domain, $password){
181		return true;
182	}
183
184	/**
185	 * Logout function
186	 *
187	 * This function is a dummy one,
188	 * the logout process is already been handled in the go.php file.
189	 *
190	 * @return boolean
191	 */
192	public function Logoff(){
193		return true;
194	}
195
196	public function ChangeFolder($folderid, $oldid, $displayname, $type) {
197		return false;
198	}
199
200	public function DeleteFolder($id, $parentid) {
201		return false;
202	}
203
204	public function SetReadFlag($folderid, $id, $flags, $contentParameters) {
205		return false;
206	}
207
208	public function DeleteMessage($folderid, $id, $contentParameters) {
209		return false;
210	}
211
212	public function MoveMessage($folderid, $id, $newfolderid, $contentParameters) {
213		return false;
214	}
215
216	public function SendMail($sm) {
217		return false;
218	}
219
220	public function GetWasteBasket() {
221		return false;
222	}
223
224	public function GetAttachmentData($attname) {
225		return false;
226	}
227}
228