ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilRoleMailboxAddress.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
13  protected $roleId;
14 
16  protected $localize = true;
17 
19  protected $parserFactory;
20 
22  protected $db;
23 
25  protected $lng;
26 
35  public function __construct(
36  int $roleId,
37  bool $localize = true,
39  ilDBInterface $db = null,
40  ilLanguage $lng = null
41 
42  ) {
43  global $DIC;
44 
45  $this->roleId = $roleId;
46  $this->localize = $localize;
47 
48  if (null === $db) {
49  $db = $DIC->database();
50  }
51  $this->db = $db;
52 
53  if (null === $lng) {
54  $lng = $DIC->language();
55  }
56  $this->lng = $lng;
57 
58  if (null === $parserFactory) {
60  }
61  $this->parserFactory = $parserFactory;
62  }
63 
122  public function value() : string
123  {
124  // Retrieve the role title and the object title.
125  $query = "SELECT rdat.title role_title,odat.title object_title, " .
126  " oref.ref_id object_ref " .
127  "FROM object_data rdat " .
128  "JOIN rbac_fa fa ON fa.rol_id = rdat.obj_id " .
129  "JOIN tree rtree ON rtree.child = fa.parent " .
130  "JOIN object_reference oref ON oref.ref_id = rtree.child " .
131  "JOIN object_data odat ON odat.obj_id = oref.obj_id " .
132  "WHERE rdat.obj_id = " . $this->db->quote($this->roleId, 'integer') . " " .
133  "AND fa.assign = 'y' ";
134  $res = $this->db->query($query);
135  if (!$row = $this->db->fetchObject($res)) {
136  return '';
137  }
138 
139  $object_title = $row->object_title;
140  $object_ref = $row->object_ref;
141  $role_title = $row->role_title;
142 
143  // In a perfect world, we could use the object_title in the
144  // domain part of the mailbox address, and the role title
145  // with prefix '#' in the local part of the mailbox address.
146  $domain = $object_title;
147  $local_part = $role_title;
148 
149  // Determine if the object title is unique
150  $q = "SELECT COUNT(DISTINCT dat.obj_id) count " .
151  "FROM object_data dat " .
152  "JOIN object_reference ref ON ref.obj_id = dat.obj_id " .
153  "JOIN tree ON tree.child = ref.ref_id " .
154  "WHERE title = " . $this->db->quote($object_title, 'text') . " " .
155  "AND tree.tree = 1 ";
156  $res = $this->db->query($q);
157  $row = $this->db->fetchObject($res);
158 
159  // If the object title is not unique, we get rid of the domain.
160  if ($row->count > 1) {
161  $domain = null;
162  }
163 
164  // If the domain contains illegal characters, we get rid of it.
165  //if (domain != null && preg_match('/[\[\]\\]|[\x00-\x1f]/',$domain))
166  // Fix for Mantis Bug: 7429 sending mail fails because of brakets
167  // Fix for Mantis Bug: 9978 sending mail fails because of semicolon
168  if ($domain != null && preg_match('/[\[\]\\]|[\x00-\x1f]|[\x28-\x29]|[;]/', $domain)) {
169  $domain = null;
170  }
171 
172  // If the domain contains special characters, we put square
173  // brackets around it.
174  if ($domain != null &&
175  (preg_match('/[()<>@,;:\\".\[\]]/', $domain) ||
176  preg_match('/[^\x21-\x8f]/', $domain))
177  ) {
178  $domain = '[' . $domain . ']';
179  }
180 
181  // If the role title is one of the ILIAS reserved role titles,
182  // we can use a shorthand version of it for the local part
183  // of the mailbox address.
184  if (strpos($role_title, 'il_') === 0 && $domain != null) {
185  $unambiguous_role_title = $role_title;
186 
187  $pos = strpos($role_title, '_', 3) + 1;
188  $local_part = substr(
189  $role_title,
190  $pos,
191  strrpos($role_title, '_') - $pos
192  );
193  } else {
194  $unambiguous_role_title = 'il_role_' . $this->roleId;
195  }
196 
197  // Determine if the local part is unique. If we don't have a
198  // domain, the local part must be unique within the whole repositry.
199  // If we do have a domain, the local part must be unique for that
200  // domain.
201  if ($domain == null) {
202  $q = "SELECT COUNT(DISTINCT dat.obj_id) count " .
203  "FROM object_data dat " .
204  "JOIN object_reference ref ON ref.obj_id = dat.obj_id " .
205  "JOIN tree ON tree.child = ref.ref_id " .
206  "WHERE title = " . $this->db->quote($local_part, 'text') . " " .
207  "AND tree.tree = 1 ";
208  } else {
209  $q = "SELECT COUNT(rd.obj_id) count " .
210  "FROM object_data rd " .
211  "JOIN rbac_fa fa ON rd.obj_id = fa.rol_id " .
212  "JOIN tree t ON t.child = fa.parent " .
213  "WHERE fa.assign = 'y' " .
214  "AND t.child = " . $this->db->quote($object_ref, 'integer') . " " .
215  "AND rd.title LIKE " . $this->db->quote(
216  '%' . preg_replace('/([_%])/', '\\\\$1', $local_part) . '%',
217  'text'
218  ) . " ";
219  }
220 
221  $res = $this->db->query($q);
222  $row = $this->db->fetchObject($res);
223 
224  // if the local_part is not unique, we use the unambiguous role title
225  // instead for the local part of the mailbox address
226  if ($row->count > 1) {
227  $local_part = $unambiguous_role_title;
228  }
229 
230  $use_phrase = true;
231 
232  // If the local part contains illegal characters, we use
233  // the unambiguous role title instead.
234  if (preg_match('/[\\"\x00-\x1f]/', $local_part)) {
235  $local_part = $unambiguous_role_title;
236  } else {
237  if (!preg_match('/^[\\x00-\\x7E]+$/i', $local_part)) {
238  // 2013-12-05: According to #12283, we do not accept umlauts in the local part
239  $local_part = $unambiguous_role_title;
240  $use_phrase = false;
241  }
242  }
243 
244  // Add a "#" prefix to the local part
245  $local_part = '#' . $local_part;
246 
247  // Put quotes around the role title, if needed
248  if (preg_match('/[()<>@,;:.\[\]\x20]/', $local_part)) {
249  $local_part = '"' . $local_part . '"';
250  }
251 
252  $mailbox = ($domain == null) ?
253  $local_part :
254  $local_part . '@' . $domain;
255 
256  if ($this->localize) {
257  if (substr($role_title, 0, 3) == 'il_') {
258  $phrase = $this->lng->txt(substr($role_title, 0, strrpos($role_title, '_')));
259  } else {
260  $phrase = $role_title;
261  }
262 
263  if ($use_phrase) {
264  // make phrase RFC 822 conformant:
265  // - strip excessive whitespace
266  // - strip special characters
267  $phrase = preg_replace('/\s\s+/', ' ', $phrase);
268  $phrase = preg_replace('/[()<>@,;:\\".\[\]]/', '', $phrase);
269 
270  $mailbox = $phrase . ' <' . $mailbox . '>';
271  }
272  }
273 
274  try {
275  $parser = $this->parserFactory->getParser((string) $mailbox);
276  $parser->parse();
277 
278  return $mailbox;
279  } catch (ilException $e) {
280  $res = $this->db->query("SELECT title FROM object_data WHERE obj_id = " . $this->db->quote(
281  $this->roleId,
282  'integer'
283  ));
284  if ($row = $this->db->fetchObject($res)) {
285  return '#' . $row->title;
286  } else {
287  return '';
288  }
289  }
290  }
291 }
value()
Returns the mailbox address of a role.
Class ilRoleMailboxAddress.
Interface ilDBInterface.
Class ilMailRfc822AddressParserFactory.
foreach($_POST as $key=> $value) $res
$query
$parser
Definition: BPMN2Parser.php:23
$DIC
Definition: xapitoken.php:46
language handling
__construct(int $roleId, bool $localize=true, ilMailRfc822AddressParserFactory $parserFactory=null, ilDBInterface $db=null, ilLanguage $lng=null)
ilRoleMailboxAddress constructor.