ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilMailCronOrphanedMailsDeletionProcessor.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
5 
11 {
15  protected $collector;
16 
20  protected $db;
21 
25  protected $settings;
26 
31  {
32  global $DIC;
33 
34  $this->collector = $collector;
35 
36  $this->settings = $DIC->settings();
37  $this->db = $DIC->database();
38  }
39 
43  private function deleteAttachments()
44  {
45  $attachment_paths = array();
46 
47  $res = $this->db->query('
48  SELECT path, COUNT(mail_id) cnt_mail_ids
49  FROM mail_attachment
50  WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer') . '
51  GROUP BY path');
52 
53  while ($row = $this->db->fetchAssoc($res)) {
54  $usage_res = $this->db->queryF(
55  'SELECT mail_id, path FROM mail_attachment WHERE path = %s',
56  array('text'),
57  array($row['path'])
58  );
59 
60  $num_rows = $this->db->numRows($usage_res);
61  if ($row['cnt_mail_ids'] >= $num_rows) {
62  // collect path to delete attachment file
63  $attachment_paths[$row['mail_id']] = $row['path'];
64  }
65  }
66 
67  foreach ($attachment_paths as $mail_id => $path) {
68  try {
69  $path = CLIENT_DATA_DIR . '/mail/' . $path;
70  $iter = new RecursiveIteratorIterator(
72  RecursiveIteratorIterator::CHILD_FIRST
73  );
74 
75  foreach ($iter as $file) {
80  $path_name = $file->getPathname();
81  if ($file->isDir()) {
82  ilUtil::delDir($path_name);
83  ilLoggerFactory::getLogger('mail')->info(sprintf(
84  'Attachment directory (%s) deleted for mail_id: %s',
85  $path_name,
86  $mail_id
87  ));
88  } else {
89  if (file_exists($path_name) && unlink($path_name)) {
90  ilLoggerFactory::getLogger('mail')->info(sprintf(
91  'Attachment file (%s) deleted for mail_id: %s',
92  $path_name,
93  $mail_id
94  ));
95  } else {
96  ilLoggerFactory::getLogger('mail')->info(sprintf(
97  'Attachment file (%s) for mail_id could not be deleted due to missing file system permissions: %s',
98  $path_name,
99  $mail_id
100  ));
101  }
102  }
103  }
104 
106  ilLoggerFactory::getLogger('mail')->info(sprintf(
107  'Attachment directory (%s) deleted for mail_id: %s',
108  $path,
109  $mail_id
110  ));
111  } catch (Exception $e) {
112  }
113  }
114 
115  $this->db->manipulate('DELETE FROM mail_attachment WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer'));
116  }
117 
121  private function deleteMails()
122  {
123  $this->db->manipulate('DELETE FROM mail WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer'));
124  }
125 
129  private function deleteMarkedAsNotified()
130  {
131  if ((int) $this->settings->get('mail_notify_orphaned') >= 1) {
132  $this->db->manipulate('DELETE FROM mail_cron_orphaned WHERE ' . $this->db->in('mail_id', $this->collector->getMailIdsToDelete(), false, 'integer'));
133  } else {
134  $this->db->manipulate('DELETE FROM mail_cron_orphaned');
135  }
136  }
137 
141  public function processDeletion()
142  {
143  if (count($this->collector->getMailIdsToDelete()) > 0) {
144  // delete possible attachments ...
145  $this->deleteAttachments();
146 
147  $this->deleteMails();
148  require_once './Services/Logging/classes/public/class.ilLoggerFactory.php';
149  ilLoggerFactory::getLogger('mail')->info(sprintf(
150  'Deleted mail_ids: %s',
151  implode(', ', $this->collector->getMailIdsToDelete())
152  ));
153 
154  $this->deleteMarkedAsNotified();
155  ilLoggerFactory::getLogger('mail')->info(sprintf(
156  'Deleted mail_cron_orphaned mail_ids: %s',
157  implode(', ', $this->collector->getMailIdsToDelete())
158  ));
159  }
160  }
161 }
$path
Definition: aliased.php:25
settings()
Definition: settings.php:2
global $DIC
Definition: saml.php:7
__construct(ilMailCronOrphanedMailsDeletionCollector $collector)
foreach($_POST as $key=> $value) $res
$row
static getLogger($a_component_id)
Get component logger.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively