ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMailbox.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 class ilMailbox
30 {
31  protected ilLanguage $lng;
32  protected ilDBInterface $db;
33  protected ilTree $mtree;
35  protected array $actions = [
36  'moveMails' => '',
37  'markMailsRead' => '',
38  'markMailsUnread' => '',
39  'deleteMails' => '',
40  ];
42  protected array $defaultFolders = [
43  'b_inbox' => 'inbox',
44  'c_trash' => 'trash',
45  'd_drafts' => 'drafts',
46  'e_sent' => 'sent',
47  'z_local' => 'local',
48  ];
49  protected string $table_mail_obj_data;
50  protected string $table_tree;
51 
52  public function __construct(protected int $usrId)
53  {
54  global $DIC;
55 
56  if ($usrId < 1) {
57  throw new InvalidArgumentException("Cannot create mailbox without user id");
58  }
59 
60  $this->lng = $DIC->language();
61  $this->db = $DIC->database();
62  $this->table_mail_obj_data = 'mail_obj_data';
63  $this->table_tree = 'mail_tree';
64 
65  $this->mtree = new ilTree($this->usrId);
66  $this->mtree->setTableNames($this->table_tree, $this->table_mail_obj_data);
67 
68  // i added this, becaus if i create a new user automatically during
69  // CAS authentication, we have no $lng variable (alex, 16.6.2006)
70  // (alternative: make createDefaultFolder call static in ilObjUser->saveAsNew())
71  if (is_object($this->lng)) {
72  $this->lng->loadLanguageModule("mail");
73 
74  $this->actions = [
75  'moveMails' => $this->lng->txt('mail_move_to'),
76  'markMailsRead' => $this->lng->txt('mail_mark_read'),
77  'markMailsUnread' => $this->lng->txt('mail_mark_unread'),
78  'deleteMails' => $this->lng->txt('delete'),
79  ];
80  }
81  }
82 
83  public function getRooFolder(): int
84  {
85  $res = $this->db->queryF(
86  'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
87  ['integer', 'text'],
88  [$this->usrId, 'root']
89  );
90 
91  $row = $this->db->fetchAssoc($res);
92 
93  return (int) $row['obj_id'];
94  }
95 
96  public function getInboxFolder(): int
97  {
98  $res = $this->db->queryF(
99  'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
100  ['integer', 'text'],
101  [$this->usrId, 'inbox']
102  );
103 
104  $row = $this->db->fetchAssoc($res);
105 
106  return (int) $row['obj_id'];
107  }
108 
109  public function getDraftsFolder(): int
110  {
111  $res = $this->db->queryF(
112  'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
113  ['integer', 'text'],
114  [$this->usrId, 'drafts']
115  );
116 
117  $row = $this->db->fetchAssoc($res);
118 
119  return (int) $row['obj_id'];
120  }
121 
122  public function getTrashFolder(): int
123  {
124  $res = $this->db->queryF(
125  'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
126  ['integer', 'text'],
127  [$this->usrId, 'trash']
128  );
129 
130  $row = $this->db->fetchAssoc($res);
131 
132  return (int) $row['obj_id'];
133  }
134 
135  public function getSentFolder(): int
136  {
137  $res = $this->db->queryF(
138  'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND m_type = %s',
139  ['integer', 'text'],
140  [$this->usrId, 'sent']
141  );
142 
143  $row = $this->db->fetchAssoc($res);
144 
145  return (int) $row['obj_id'];
146  }
147 
151  public function getActions(int $folderId): array
152  {
153  return $this->actions;
154  }
155 
159  public function createDefaultFolder(): void
160  {
161  $rootFolderId = $this->db->nextId($this->table_mail_obj_data);
162  $this->db->manipulateF(
163  'INSERT INTO ' . $this->table_mail_obj_data .
164  ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
165  ['integer', 'integer', 'text', 'text'],
166  [$rootFolderId, $this->usrId, 'a_root', 'root']
167  );
168  $this->mtree->addTree($this->usrId, $rootFolderId);
169 
170  foreach ($this->defaultFolders as $key => $folder) {
171  $last_id = $this->db->nextId($this->table_mail_obj_data);
172  $this->db->manipulateF(
173  'INSERT INTO ' . $this->table_mail_obj_data .
174  ' (obj_id, user_id, title, m_type) VALUES(%s, %s, %s, %s)',
175  ['integer', 'integer', 'text', 'text'],
176  [$last_id, $this->usrId, $key, $folder]
177  );
178  $this->mtree->insertNode($last_id, $rootFolderId);
179  }
180  }
181 
182  public function addFolder(int $parentFolderId, string $name): int
183  {
184  if ($this->folderNameExists($name)) {
185  return 0;
186  }
187 
188  $nextId = $this->db->nextId($this->table_mail_obj_data);
189  $this->db->manipulateF(
190  'INSERT INTO ' . $this->table_mail_obj_data .
191  ' (obj_id, user_id, title, m_type) VALUES(%s,%s,%s,%s)',
192  ['integer', 'integer', 'text', 'text'],
193  [$nextId, $this->usrId, $name, 'user_folder']
194  );
195  $this->mtree->insertNode($nextId, $parentFolderId);
196 
197  return $nextId;
198  }
199 
200  public function renameFolder(int $folderId, string $name): bool
201  {
202  if ($this->folderNameExists($name)) {
203  return false;
204  }
205 
206  $this->db->manipulateF(
207  'UPDATE ' . $this->table_mail_obj_data . ' SET title = %s WHERE obj_id = %s AND user_id = %s',
208  ['text', 'integer', 'integer'],
209  [$name, $folderId, $this->usrId]
210  );
211 
212  return true;
213  }
214 
215  protected function folderNameExists(string $name): bool
216  {
217  $res = $this->db->queryF(
218  'SELECT obj_id FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND title = %s',
219  ['integer', 'text'],
220  [$this->usrId, $name]
221  );
222  $row = $this->db->fetchAssoc($res);
223 
224  return is_array($row) && $row['obj_id'] > 0;
225  }
226 
230  public function deleteFolder(int $folderId): bool
231  {
232  $query = $this->db->queryF(
233  'SELECT obj_id, title FROM ' . $this->table_mail_obj_data . ' WHERE obj_id = %s AND user_id = %s',
234  ['integer', 'integer'],
235  [$folderId, $this->usrId]
236  );
237  $row = $this->db->fetchAssoc($query);
238 
239  if (!is_array($row) || array_key_exists($row['title'], $this->defaultFolders)) {
240  return false;
241  }
242 
243  $mailer = new ilMail($this->usrId);
244 
245  $subtree = $this->mtree->getSubTree($this->mtree->getNodeData($folderId));
246  $this->mtree->deleteTree($this->mtree->getNodeData($folderId));
247 
248  foreach ($subtree as $node) {
249  $nodeId = (int) $node['obj_id'];
250 
251  $mails = $mailer->getMailsOfFolder($nodeId);
252 
253  $mailIds = [];
254  foreach ($mails as $mail) {
255  $mailIds[] = (int) $mail['mail_id'];
256  }
257 
258  $mailer->deleteMails($mailIds);
259 
260  $this->db->manipulateF(
261  'DELETE FROM ' . $this->table_mail_obj_data . ' WHERE obj_id = %s AND user_id = %s',
262  ['integer', 'integer'],
263  [$nodeId, $this->usrId]
264  );
265  }
266 
267  return true;
268  }
269 
273  public function getFolderData(int $folderId): ?array
274  {
275  $res = $this->db->queryF(
276  'SELECT * FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND obj_id = %s',
277  ['integer', 'integer'],
278  [$this->usrId, $folderId]
279  );
280  $row = $this->db->fetchAssoc($res);
281 
282  if (is_array($row)) {
283  return [
284  'obj_id' => (int) $row['obj_id'],
285  'title' => (string) $row['title'],
286  'type' => (string) $row['m_type'],
287  ];
288  }
289 
290  return null;
291  }
292 
293  public function getParentFolderId(int $folderId): int
294  {
295  $res = $this->db->queryF(
296  'SELECT * FROM ' . $this->table_tree . ' WHERE child = %s AND tree = %s',
297  ['integer', 'integer'],
298  [$folderId, $this->usrId]
299  );
300  $row = $this->db->fetchAssoc($res);
301 
302  return is_array($row) ? (int) $row['parent'] : 0;
303  }
304 
305  public function getSubFolders(): array
306  {
307  $userFolders = [];
308 
309  foreach (array_keys($this->defaultFolders) as $key) {
310  $res = $this->db->queryF(
311  'SELECT obj_id, m_type FROM ' . $this->table_mail_obj_data . ' WHERE user_id = %s AND title = %s',
312  ['integer', 'text'],
313  [$this->usrId, $key]
314  );
315  $row = $this->db->fetchAssoc($res);
316 
317  $userFolders[] = [
318  'title' => $key,
319  'type' => (string) $row['m_type'],
320  'obj_id' => (int) $row['obj_id'],
321  ];
322  }
323 
324  $query = implode(' ', [
325  'SELECT * FROM ' . $this->table_tree . ', ' . $this->table_mail_obj_data,
326  'WHERE ' . $this->table_mail_obj_data . '.obj_id = ' . $this->table_tree . '.child',
327  'AND ' . $this->table_tree . '.depth > %s',
328  'AND ' . $this->table_tree . '.tree = %s',
329  'ORDER BY ' . $this->table_tree . '.lft, ' . $this->table_mail_obj_data . '.title',
330  ]);
331  $res = $this->db->queryF(
332  $query,
333  ['integer', 'integer'],
334  [2, $this->usrId]
335  );
336  while ($row = $this->db->fetchAssoc($res)) {
337  $userFolders[] = [
338  'title' => (string) $row['title'],
339  'type' => (string) $row['m_type'],
340  'obj_id' => (int) $row['child'],
341  ];
342  }
343 
344  return $userFolders;
345  }
346 
347  public function setUsrId(int $usrId): void
348  {
349  $this->usrId = $usrId;
350  }
351 
352  public function getUsrId(): int
353  {
354  return $this->usrId;
355  }
356 
357  public function delete(): void
358  {
359  $this->db->manipulateF(
360  'DELETE FROM mail_obj_data WHERE user_id = %s',
361  ['integer'],
362  [$this->usrId]
363  );
364 
365  $this->db->manipulateF(
366  'DELETE FROM mail_options WHERE user_id = %s',
367  ['integer'],
368  [$this->usrId]
369  );
370 
371  $this->db->manipulateF(
372  'DELETE FROM mail_saved WHERE user_id = %s',
373  ['integer'],
374  [$this->usrId]
375  );
376 
377  $this->db->manipulateF(
378  'DELETE FROM mail_tree WHERE tree = %s',
379  ['integer'],
380  [$this->usrId]
381  );
382 
383  $this->db->manipulateF(
384  'DELETE FROM mail_auto_responder WHERE sender_id = %s OR receiver_id = %s',
385  ['integer', 'integer'],
386  [$this->usrId, $this->usrId]
387  );
388 
389  // Delete the user's files from filesystem:
390  // This has to be done before deleting the database entries in table 'mail'
391  $fdm = new ilFileDataMail($this->usrId);
392  $fdm->onUserDelete();
393 
394  // Delete mails of deleted user
395  $this->db->manipulateF(
396  'DELETE FROM mail WHERE user_id = %s',
397  ['integer'],
398  [$this->usrId]
399  );
400  }
401 
406  public function updateMailsOfDeletedUser(string $nameToShow): void
407  {
408  $this->db->manipulateF(
409  'UPDATE mail SET sender_id = %s, import_name = %s WHERE sender_id = %s',
410  ['integer', 'text', 'integer'],
411  [0, $nameToShow, $this->usrId]
412  );
413  }
414 
415  public function isOwnedFolder(int $folderId): bool
416  {
417  $folderData = $this->getFolderData($folderId);
418 
419  return $folderData !== null && (int) $folderData['obj_id'] === $folderId;
420  }
421 }
$res
Definition: ltiservices.php:69
getActions(int $folderId)
addFolder(int $parentFolderId, string $name)
This class handles all operations on files (attachments) in directory ilias_data/mail.
string $table_tree
updateMailsOfDeletedUser(string $nameToShow)
Update existing mails.
array $defaultFolders
ilDBInterface $db
setUsrId(int $usrId)
global $DIC
Definition: feed.php:28
Mail Box class Base class for creating and handling mail boxes.
createDefaultFolder()
Creates all default folders for a user.
getParentFolderId(int $folderId)
string $key
Consumer key/client ID value.
Definition: System.php:193
ilLanguage $lng
renameFolder(int $folderId, string $name)
getFolderData(int $folderId)
__construct(protected int $usrId)
deleteFolder(int $folderId)
array string $table_mail_obj_data
folderNameExists(string $name)
isOwnedFolder(int $folderId)