ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSoapMailXmlParser.php
Go to the documentation of this file.
1 <?php
2 /******************************************************************************
3  * This file is part of ILIAS, a powerful learning management system.
4  * ILIAS is licensed with the GPL-3.0, you should have received a copy
5  * of said license along with the source code.
6  * If this is not the case or you just want to try ILIAS, you'll find
7  * us at:
8  * https://www.ilias.de
9  * https://github.com/ILIAS-eLearning
10  *****************************************************************************/
11 
18 {
19  protected array $mails = [];
20  protected array $mail = [];
21  protected array $attachment = [];
22  protected array $lines = [];
23  protected string $cdata = '';
24 
25  public function __construct(string $a_xml)
26  {
27  parent::__construct('', true);
28  $this->setThrowException(true);
29  $this->setXMLContent($a_xml);
30  }
31 
32  public function getMails(): array
33  {
34  return $this->mails;
35  }
36 
37  public function start(): bool
38  {
39  $this->startParsing();
40  return true;
41  }
42 
47  public function setHandlers($a_xml_parser): void
48  {
49  xml_set_object($a_xml_parser, $this);
50  xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
51  xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
52  }
53 
60  public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
61  {
62  switch ($a_name) {
63  case 'Mail':
64  $this->mail = array();
65  $this->mail['usePlaceholders'] = (bool) $a_attribs['usePlaceholders'];
66  $this->mail['type'] = $a_attribs['type'] === 'System' ? 'system' : 'normal';
67  break;
68 
69  case 'To':
70  $this->mail['to'] = $this->parseName($a_attribs);
71  break;
72 
73  case 'Cc':
74  $this->mail['cc'] = $this->parseName($a_attribs);
75  break;
76 
77  case 'Bcc':
78  $this->mail['bcc'] = $this->parseName($a_attribs);
79  break;
80 
81  case 'Subject':
82  break;
83 
84  case 'Message':
85  $this->lines = array();
86  break;
87 
88  case 'Attachment':
89  $this->attachment = array();
90  $this->attachment['name'] = $a_attribs['name'];
91  break;
92  }
93  }
94 
100  public function handlerEndTag($a_xml_parser, string $a_name): void
101  {
102  switch ($a_name) {
103  case 'Mail':
104  $this->mails[] = $this->mail;
105  break;
106 
107  case 'Subject':
108  $this->mail['subject'] = $this->cdata;
109  break;
110 
111  case 'Message':
112  $this->mail['body'] = $this->lines;
113  break;
114 
115  case 'P':
116  $this->lines[] = trim($this->cdata);
117  break;
118 
119  case 'Attachment':
120  $this->attachment['content'] = base64_decode(trim($this->cdata));
121  $this->mail['attachments'][] = $this->attachment;
122  break;
123  }
124 
125  $this->cdata = '';
126  }
127 
133  public function handlerCharacterData($a_xml_parser, string $a_data): void
134  {
135  if ($a_data !== "\n") {
136  // Replace multiple tabs with one space
137  $a_data = preg_replace("/\t+/", " ", $a_data);
138  $this->cdata .= $a_data;
139  }
140  }
141 
142  protected function parseName(array $a_attribs): string
143  {
144  if ($a_attribs['obj_id']) {
145  $il_id = explode('_', $a_attribs['obj_id']);
146  if (!$user = ilObjectFactory::getInstanceByObjId($il_id[3], false)) {
147  throw new InvalidArgumentException("Invalid user id given: obj_id => " . $a_attribs['obj_id']);
148  }
149  return $user->getLogin();
150  }
151  return (string) ($a_attribs['name'] ?? '');
152  }
153 }
setThrowException(bool $throw_exception)
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...
__construct(VocabulariesInterface $vocabularies)
XML parser for soap mails.
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
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)