ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilSoapMailXmlParser.php
Go to the documentation of this file.
1 <?php
2 
25 {
26  protected array $mails = [];
27  protected array $mail = [];
28  protected array $attachment = [];
29  protected array $lines = [];
30  protected string $cdata = '';
31 
32  public function __construct(string $a_xml)
33  {
34  parent::__construct('', true);
35  $this->setThrowException(true);
36  $this->setXMLContent($a_xml);
37  }
38 
39  public function getMails(): array
40  {
41  return $this->mails;
42  }
43 
44  public function start(): bool
45  {
46  $this->startParsing();
47  return true;
48  }
49 
54  public function setHandlers($a_xml_parser): void
55  {
56  xml_set_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
57  xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
58  }
59 
66  public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
67  {
68  switch ($a_name) {
69  case 'Mail':
70  $this->mail = array();
71  $this->mail['usePlaceholders'] = (bool) $a_attribs['usePlaceholders'];
72  $this->mail['type'] = $a_attribs['type'] === 'System' ? 'system' : 'normal';
73  break;
74 
75  case 'To':
76  $this->mail['to'] = $this->parseName($a_attribs);
77  break;
78 
79  case 'Cc':
80  $this->mail['cc'] = $this->parseName($a_attribs);
81  break;
82 
83  case 'Bcc':
84  $this->mail['bcc'] = $this->parseName($a_attribs);
85  break;
86 
87  case 'Subject':
88  break;
89 
90  case 'Message':
91  $this->lines = array();
92  break;
93 
94  case 'Attachment':
95  $this->attachment = array();
96  $this->attachment['name'] = $a_attribs['name'];
97  break;
98  }
99  }
100 
106  public function handlerEndTag($a_xml_parser, string $a_name): void
107  {
108  switch ($a_name) {
109  case 'Mail':
110  $this->mails[] = $this->mail;
111  break;
112 
113  case 'Subject':
114  $this->mail['subject'] = $this->cdata;
115  break;
116 
117  case 'Message':
118  $this->mail['body'] = $this->lines;
119  break;
120 
121  case 'P':
122  $this->lines[] = trim($this->cdata);
123  break;
124 
125  case 'Attachment':
126  $this->attachment['content'] = base64_decode(trim($this->cdata));
127  $this->mail['attachments'][] = $this->attachment;
128  break;
129  }
130 
131  $this->cdata = '';
132  }
133 
139  public function handlerCharacterData($a_xml_parser, string $a_data): void
140  {
141  if ($a_data !== "\n") {
142  // Replace multiple tabs with one space
143  $a_data = preg_replace("/\t+/", " ", $a_data);
144  $this->cdata .= $a_data;
145  }
146  }
147 
148  protected function parseName(array $a_attribs): string
149  {
150  if ($a_attribs['obj_id']) {
151  $il_id = explode('_', $a_attribs['obj_id']);
152  if (!$user = ilObjectFactory::getInstanceByObjId($il_id[3], false)) {
153  throw new InvalidArgumentException("Invalid user id given: obj_id => " . $a_attribs['obj_id']);
154  }
155  return $user->getLogin();
156  }
157  return (string) ($a_attribs['name'] ?? '');
158  }
159 }
setThrowException(bool $throw_exception)
attachment()
description: > Example for rendring a attachment glyph.
Definition: attachment.php:41
handlerCharacterData($a_xml_parser, string $a_data)
startParsing()
stores xml data in array
handlerEndTag($a_xml_parser, string $a_name)
Handler for end of element.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
__construct(Container $dic, ilPlugin $plugin)
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
handler for begin of element
setHandlers($a_xml_parser)
Set event handlers.
setXMLContent(string $a_xml_content)