ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilMailbox.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
13{
15 protected $lng;
16
18 protected $db;
19
21 protected $mtree;
22
24 protected $usrId;
25
27 protected $actions = [];
28
30 protected $defaultFolders = [];
31
34
36 protected $table_tree;
37
42 public function __construct($a_user_id = 0)
43 {
44 global $DIC;
45
46 $this->lng = $DIC->language();
47 $this->db = $DIC->database();
48
49 $this->usrId = (int) $a_user_id;
50 $this->table_mail_obj_data = 'mail_obj_data';
51 $this->table_tree = 'mail_tree';
52
53 if ($this->usrId) {
54 $this->mtree = new ilTree($this->usrId);
55 $this->mtree->setTableNames($this->table_tree, $this->table_mail_obj_data);
56 }
57
58 // i added this, becaus if i create a new user automatically during
59 // CAS authentication, we have no $lng variable (alex, 16.6.2006)
60 // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
61 if (is_object($this->lng)) {
62 $this->lng->loadLanguageModule("mail");
63
64 $this->actions = [
65 'moveMails' => $this->lng->txt('mail_move_to'),
66 'markMailsRead' => $this->lng->txt('mail_mark_read'),
67 'markMailsUnread' => $this->lng->txt('mail_mark_unread'),
68 'deleteMails' => $this->lng->txt('delete')
69 ];
70 }
71
72 // array contains basic folders and there lng translation for every new user
73 $this->defaultFolders = [
74 'b_inbox' => 'inbox',
75 'c_trash' => 'trash',
76 'd_drafts' => 'drafts',
77 'e_sent' => 'sent',
78 'z_local' => 'local'
79 ];
80 }
81
85 public function getInboxFolder() : int
86 {
87 $res = $this->db->queryF(
88 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
89 ['integer', 'text'],
90 [$this->usrId, 'inbox']
91 );
92
93 $row = $this->db->fetchAssoc($res);
94
95 return (int) $row['obj_id'];
96 }
97
101 public function getDraftsFolder() : int
102 {
103 $res = $this->db->queryF(
104 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
105 ['integer', 'text'],
106 [$this->usrId, 'drafts']
107 );
108
109 $row = $this->db->fetchAssoc($res);
110
111 return (int) $row['obj_id'];
112 }
113
117 public function getTrashFolder() : int
118 {
119 $res = $this->db->queryF(
120 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
121 ['integer', 'text'],
122 [$this->usrId, 'trash']
123 );
124
125 $row = $this->db->fetchAssoc($res);
126
127 return (int) $row['obj_id'];
128 }
129
133 public function getSentFolder() : int
134 {
135 $res = $this->db->queryF(
136 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
137 ['integer', 'text'],
138 [$this->usrId, 'sent']
139 );
140
141 $row = $this->db->fetchAssoc($res);
142
143 return (int) $row['obj_id'];
144 }
145
149 private function getRootFolderId() : int
150 {
151 return (int) $this->mtree->getRootId();
152 }
153
158 public function getActions($folderId) : array
159 {
160 if ($folderId) {
161 $folder_data = $this->getFolderData($folderId);
162 if ($folder_data['type'] === 'user_folder' || $folder_data['type'] === 'local') {
163 return $this->actions;
164 }
165 }
166
167 return $this->actions;
168 }
169
173 public function createDefaultFolder() : void
174 {
175 $rootFolderId = (int) $this->db->nextId($this->table_mail_obj_data);
176 $this->db->manipulateF(
177 'INSERT INTO ' . $this->table_mail_obj_data . ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
178 ['integer', 'integer', 'text', 'text'],
179 [$rootFolderId, $this->usrId, 'a_root', 'root']
180 );
181 $this->mtree->addTree($this->usrId, $rootFolderId);
182
183 foreach ($this->defaultFolders as $key => $folder) {
184 $last_id = $this->db->nextId($this->table_mail_obj_data);
185 $this->db->manipulateF(
186 'INSERT INTO ' . $this->table_mail_obj_data . ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
187 ['integer', 'integer', 'text', 'text'],
188 [$last_id, $this->usrId, $key, $folder]
189 );
190 $this->mtree->insertNode($last_id, $rootFolderId);
191 }
192 }
193
199 public function addFolder(int $parentFolderId, string $name) : int
200 {
201 if ($this->folderNameExists($name)) {
202 return 0;
203 }
204
205 $nextId = (int) $this->db->nextId($this->table_mail_obj_data);
206 $this->db->manipulateF(
207 'INSERT INTO ' . $this->table_mail_obj_data . ' (obj_id, user_id, title, m_type) VALUES(%s,%s,%s,%s)',
208 ['integer', 'integer', 'text', 'text'],
209 [$nextId, $this->usrId, $name, 'user_folder']
210 );
211 $this->mtree->insertNode($nextId, $parentFolderId);
212
213 return $nextId;
214 }
215
221 public function renameFolder(int $folderId, string $name) : bool
222 {
223 if ($this->folderNameExists($name)) {
224 return false;
225 }
226
227 $this->db->manipulateF(
228 'UPDATE ' . $this->table_mail_obj_data . ' SET title = %s WHERE obj_id = %s AND user_id = %s',
229 ['text', 'integer', 'integer'],
230 [$name, $folderId, $this->usrId]
231 );
232
233 return true;
234 }
235
240 protected function folderNameExists(string $name) : bool
241 {
242 $res = $this->db->queryF(
243 'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND title = %s',
244 ['integer', 'text'],
245 [$this->usrId, $name]
246 );
247 $row = $this->db->fetchAssoc($res);
248
249 return is_array($row) && $row['obj_id'] > 0 ? true : false;
250 }
251
257 public function deleteFolder($folderId) : bool
258 {
259 $query = $this->db->queryF(
260 'SELECT obj_id, title FROM ' . $this->table_mail_obj_data . ' WHERE obj_id = %s AND user_id = %s',
261 ['integer', 'integer'],
262 [$folderId, $this->usrId]
263 );
264 $row = $this->db->fetchAssoc($query);
265
266 if (!is_array($row) || array_key_exists($row['title'], $this->defaultFolders)) {
267 return false;
268 }
269
270 $mailer = new ilMail($this->usrId);
271
272 $subtree = $this->mtree->getSubtree($this->mtree->getNodeData($folderId));
273 $this->mtree->deleteTree($this->mtree->getNodeData($folderId));
274
275 foreach ($subtree as $node) {
276 $nodeId = (int) $node['obj_id'];
277
278 $mails = $mailer->getMailsOfFolder($nodeId);
279
280 $mailIds = [];
281 foreach ($mails as $mail) {
282 $mailIds[] = $mail['mail_id'];
283 }
284
285 $mailer->deleteMails($mailIds);
286
287 $this->db->manipulateF(
288 'DELETE FROM ' . $this->table_mail_obj_data . ' WHERE obj_id = %s AND user_id = %s',
289 ['integer', 'integer'],
290 [$nodeId, $this->usrId]
291 );
292 }
293
294 return true;
295 }
296
301 public function getFolderData($folderId) : array
302 {
303 $res = $this->db->queryF(
304 'SELECT * FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND obj_id = %s',
305 ['integer', 'integer'],
306 [$this->usrId, $folderId]
307 );
308 $row = $this->db->fetchAssoc($res);
309
310 return [
311 'obj_id' => (int) $row['obj_id'],
312 'title' => (string) $row['title'],
313 'type' => (string) $row['m_type'],
314 ];
315 }
316
321 public function getParentFolderId(int $folderId) : int
322 {
323 $res = $this->db->queryF(
324 'SELECT * FROM ' . $this->table_tree . ' WHERE child = %s AND tree = %s',
325 ['integer', 'integer'],
326 [$folderId, $this->usrId]
327 );
328 $row = $this->db->fetchAssoc($res);
329
330 return is_array($row) ? (int) $row['parent'] : 0;
331 }
332
336 public function getSubFolders() : array
337 {
338 $userFolders = [];
339
340 foreach ($this->defaultFolders as $key => $value) {
341 $res = $this->db->queryF(
342 'SELECT obj_id, m_type FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND title = %s',
343 ['integer', 'text'],
344 [$this->usrId, $key]
345 );
346 $row = $this->db->fetchAssoc($res);
347
348 $userFolders[] = [
349 'title' => (string) $key,
350 'type' => (string) $row['m_type'],
351 'obj_id' => (int) $row['obj_id'],
352 ];
353 }
354
355 $query = implode(' ', [
356 'SELECT * FROM ' . $this->table_tree . ', ' . $this->table_mail_obj_data,
357 'WHERE ' . $this->table_mail_obj_data . '.obj_id = ' . $this->table_tree . '.child',
358 'AND ' . $this->table_tree . '.depth > %s',
359 'AND ' . $this->table_tree . '.tree = %s',
360 'ORDER BY ' . $this->table_tree . '.lft, ' . $this->table_mail_obj_data . '.title'
361 ]);
362 $res = $this->db->queryF(
363 $query,
364 ['integer', 'integer'],
365 [2, $this->usrId]
366 );
367 while ($row = $this->db->fetchAssoc($res)) {
368 $userFolders[] = [
369 'title' => (string) $row['title'],
370 'type' => (string) $row['m_type'],
371 'obj_id' => (int) $row['child'],
372 ];
373 }
374
375 return $userFolders;
376 }
377
381 public function setUsrId(int $usrId) : void
382 {
383 $this->usrId = $usrId;
384 }
385
389 public function getUsrId() : int
390 {
391 return (int) $this->usrId;
392 }
393
397 public function delete() : void
398 {
399 $this->db->manipulateF(
400 'DELETE FROM mail_obj_data WHERE user_id = %s',
401 ['integer'],
402 [$this->usrId]
403 );
404
405 $this->db->manipulateF(
406 'DELETE FROM mail_options WHERE user_id = %s',
407 ['integer'],
408 [$this->usrId]
409 );
410
411 $this->db->manipulateF(
412 'DELETE FROM mail_saved WHERE user_id = %s',
413 ['integer'],
414 [$this->usrId]
415 );
416
417 $this->db->manipulateF(
418 'DELETE FROM mail_tree WHERE tree = %s',
419 ['integer'],
420 [$this->usrId]
421 );
422
423 // Delete the user's files from filesystem: This has to be done before deleting the database entries in table 'mail'
424 $fdm = new ilFileDataMail($this->usrId);
425 $fdm->onUserDelete();
426
427 // Delete mails of deleted user
428 $this->db->manipulateF(
429 'DELETE FROM mail WHERE user_id = %s',
430 ['integer'],
431 [$this->usrId]
432 );
433 }
434
440 public function updateMailsOfDeletedUser(string $nameToShow) : void
441 {
442 $this->db->manipulateF(
443 'UPDATE mail SET sender_id = %s, import_name = %s WHERE sender_id = %s',
444 ['integer', 'text', 'integer'],
445 [0, $nameToShow, $this->usrId]
446 );
447 }
448
453 public function isOwnedFolder(int $folderId) : bool
454 {
455 $folderData = $this->getFolderData($folderId);
456
457 return (int) $folderData['obj_id'] === $folderId;
458 }
459}
An exception for terminatinating execution or to throw for unit testing.
return true
Flag indicating whether or not HTTP headers will be sent when outputting captcha image/audio.
Class ilFileDataMail.
Mail Box class Base class for creating and handling mail boxes.
getFolderData($folderId)
isOwnedFolder(int $folderId)
__construct($a_user_id=0)
ilMailbox constructor.
addFolder(int $parentFolderId, string $name)
createDefaultFolder()
Creates all default folders for a user.
deleteFolder($folderId)
updateMailsOfDeletedUser(string $nameToShow)
Update existing mails.
renameFolder(int $folderId, string $name)
setUsrId(int $usrId)
getActions($folderId)
folderNameExists(string $name)
getParentFolderId(int $folderId)
Tree class data representation in hierachical trees using the Nested Set Model with Gaps by Joe Celco...
global $DIC
Definition: goto.php:24
if($format !==null) $name
Definition: metadata.php:230
$query
foreach($_POST as $key=> $value) $res