ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailImportParser.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once("./Services/Xml/classes/class.ilSaxParser.php");
5 require_once("Services/Mail/classes/class.ilMailbox.php");
6 
16 {
17  var $mode; // "check" or "import"
18  var $counter; // counter for building executeMultiple array
19 
20 
28  public function __construct($a_xml_file,$a_mode)
29  {
30  define('EXPORT_VERSION',4);
31 
32  parent::__construct($a_xml_file);
33  $this->mode = $a_mode;
34  $this->counter = -1;
35  }
36 
37  function getMode()
38  {
39  return $this->mode;
40  }
41 
42  function getCountImported()
43  {
44  return count($this->mails);
45  }
46 
48  {
49  if(count($this->not_imported))
50  {
51  return implode("<br/>",$this->not_imported);
52  }
53  return "";
54  }
55 
61  function setHandlers($a_xml_parser)
62  {
63  xml_set_object($a_xml_parser,$this);
64  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
65  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
66  }
67 
71  function startParsing()
72  {
74 
75  switch($this->getMode())
76  {
77  case "check":
78  if(count($this->not_imported))
79  {
80  return false;
81  }
82  break;
83 
84  case "import":
85  $this->__insert();
86  break;
87  }
88  return true;
89  }
90 
91 
95  function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
96  {
97  global $ilErr;
98 
99  switch($a_name)
100  {
101  case "users":
102  if($a_attribs["exportVersion"] < EXPORT_VERSION)
103  {
104  $ilErr->raiseError("!!! This export Version isn't supported, update your ILIAS 2 installation"
105  ,$ilErr->WARNING);
106  }
107  break;
108 
109  case "user":
110  if(!($this->i3_id = ilObjUser::_getImportedUserId($a_attribs["id"])))
111  {
112  $this->not_imported[] = $a_attribs["id"];
113  }
114  break;
115 
116  case "mail":
117  if($this->i3_id)
118  {
119  $this->mails[++$this->counter]["usr_id"] = $this->i3_id;
120  $this->mails[$this->counter]["m_email"] = $a_attribs["asEmail"];
121 
122  // SET FOLDER ID = 0 FOR SYSTEM MESSAGES
123  if($a_attribs["systemMessage"])
124  {
125  $this->mails[$this->counter]["folder_id"] = 0;
126  }
127  }
128  break;
129 
130  case "sender":
131  if($this->i3_id)
132  {
133  $sender = ilObjUser::_getImportedUserId($a_attribs["id"]);
134  $this->mails[$this->counter]["sender_id"] = $sender;
135  $this->mails[$this->counter]["import_name"] = $a_attribs["import_name"];
136  }
137  break;
138 
139 
140  default:
141  // Do nothing
142  break;
143  }
144  }
145 
146 
150  function handlerEndTag($a_xml_parser, $a_name)
151  {
152  // STOP IF USER IS NOT ASSIGNABLE
153  if(!$this->i3_id)
154  {
155  $this->cdata = '';
156  return;
157  }
158  switch($a_name)
159  {
160  case "targetFolder":
161  if(!isset($this->mails[$this->counter]["folder_id"]))
162  {
163  $tmp_mailbox =& new ilMailbox($this->i3_id);
164  switch($this->cdata)
165  {
166  case "inbox":
167  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getInboxFolder();
168  $this->mails[$this->counter]["read"] = "unread";
169  break;
170  case "sentbox":
171  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getSentFolder();
172  $this->mails[$this->counter]["read"] = "read";
173  break;
174  case "draft":
175  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getDraftsFolder();
176  $this->mails[$this->counter]["read"] = "read";
177  break;
178  case "trash":
179  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getTrashFolder();
180  $this->mails[$this->counter]["read"] = "read";
181  break;
182  }
183  }
184  break;
185 
186  case "sendTime":
187  $this->mails[$this->counter]["send_time"] = date("Y-m-d H:i:s",$this->cdata);
188  $this->mails[$this->counter]["timest"] = date("YmdHis",$this->cdata);
189  break;
190 
191  case "to":
192  $this->mails[$this->counter]["rcp_to"] = $this->cdata;
193  break;
194 
195  case "cc":
196  $this->mails[$this->counter]["rcp_cc"] = $this->cdata;
197  break;
198 
199  case "bcc":
200  $this->mails[$this->counter]["rcp_bcc"] = $this->cdata;
201  break;
202 
203  case "url":
204  if($this->cdata)
205  {
206  $this->mails[$this->counter]["m_message"] = "Url: ".$this->cdata." <br>";
207  }
208  break;
209 
210  case "urlDescription":
211  if($this->cdata)
212  {
213  $this->mails[$this->counter]["m_message"] .= $this->cdata."<br>";
214  }
215  break;
216 
217  case "subject":
218  $this->mails[$this->counter]["m_subject"] = $this->cdata;
219  break;
220 
221  case "message":
222  $this->mails[$this->counter]["m_message"] .= $this->cdata;
223  break;
224  }
225  $this->cdata = '';
226  }
227 
228 
232  function handlerCharacterData($a_xml_parser, $a_data)
233  {
234  // i don't know why this is necessary, but
235  // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
236  // in character data, but we don't want that, because it's the
237  // way we mask user html in our content, so we convert back...
238  $a_data = str_replace("<","&lt;",$a_data);
239  $a_data = str_replace(">","&gt;",$a_data);
240 
241  if(!empty($a_data))
242  {
243  $this->cdata .= $a_data;
244  }
245  }
246 
247  function __insert()
248  {
249  global $ilDB;
250 
251  $sth = $ilDB->prepare("INSERT INTO mail VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
252 
253  $ilDB->executeMultiple($sth,$this->__buildSQLArray());
254 
255  return true;
256  }
257  function __buildSQLArray()
258  {
259  global $ilDB;
260 
261  $sql = array();
262 
263  if(!count($this->mails))
264  {
265  return array();
266  }
267 
268  foreach($this->mails as $mail)
269  {
270  $sql[] = (array('0',
271  addslashes($mail["usr_id"]),
272  addslashes($mail["folder_id"]),
273  addslashes($mail["sender_id"]),
274  addslashes(serialize(array())),
275  addslashes($mail["send_time"]),
276  addslashes($mail["rcp_to"]),
277  addslashes($mail["rcp_cc"]),
278  addslashes($mail["rcp_bcc"]),
279  addslashes($mail["read"]),
280  addslashes(serialize(array("normal"))),
281  addslashes($mail["m_email"]),
282  addslashes($mail["m_subject"]),
283  addslashes($mail["m_message"]),
284  addslashes($mail["import_name"])));
285 
286  }
287  return $sql ? $sql :array();
288  }
289 }
290 ?>