ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMailImportParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("classes/class.ilSaxParser.php");
25 require_once("Services/Mail/classes/class.ilMailbox.php");
26 
36 {
37  var $mode; // "check" or "import"
38  var $counter; // counter for building executeMultiple array
39 
40 
48  function ilMailImportParser($a_xml_file,$a_mode)
49  {
50  define('EXPORT_VERSION',4);
51 
52  parent::ilSaxParser($a_xml_file);
53  $this->mode = $a_mode;
54  $this->counter = -1;
55  }
56 
57  function getMode()
58  {
59  return $this->mode;
60  }
61 
62  function getCountImported()
63  {
64  return count($this->mails);
65  }
66 
68  {
69  if(count($this->not_imported))
70  {
71  return implode("<br/>",$this->not_imported);
72  }
73  return "";
74  }
75 
81  function setHandlers($a_xml_parser)
82  {
83  xml_set_object($a_xml_parser,$this);
84  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
85  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
86  }
87 
91  function startParsing()
92  {
94 
95  switch($this->getMode())
96  {
97  case "check":
98  if(count($this->not_imported))
99  {
100  return false;
101  }
102  break;
103 
104  case "import":
105  $this->__insert();
106  break;
107  }
108  return true;
109  }
110 
111 
115  function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
116  {
117  global $ilErr;
118 
119  switch($a_name)
120  {
121  case "users":
122  if($a_attribs["exportVersion"] < EXPORT_VERSION)
123  {
124  $ilErr->raiseError("!!! This export Version isn't supported, update your ILIAS 2 installation"
125  ,$ilErr->WARNING);
126  }
127  break;
128 
129  case "user":
130  if(!($this->i3_id = ilObjUser::_getImportedUserId($a_attribs["id"])))
131  {
132  $this->not_imported[] = $a_attribs["id"];
133  }
134  break;
135 
136  case "mail":
137  if($this->i3_id)
138  {
139  $this->mails[++$this->counter]["usr_id"] = $this->i3_id;
140  $this->mails[$this->counter]["m_email"] = $a_attribs["asEmail"];
141 
142  // SET FOLDER ID = 0 FOR SYSTEM MESSAGES
143  if($a_attribs["systemMessage"])
144  {
145  $this->mails[$this->counter]["folder_id"] = 0;
146  }
147  }
148  break;
149 
150  case "sender":
151  if($this->i3_id)
152  {
153  $sender = ilObjUser::_getImportedUserId($a_attribs["id"]);
154  $this->mails[$this->counter]["sender_id"] = $sender;
155  $this->mails[$this->counter]["import_name"] = $a_attribs["import_name"];
156  }
157  break;
158 
159 
160  default:
161  // Do nothing
162  break;
163  }
164  }
165 
166 
170  function handlerEndTag($a_xml_parser, $a_name)
171  {
172  // STOP IF USER IS NOT ASSIGNABLE
173  if(!$this->i3_id)
174  {
175  $this->cdata = '';
176  return;
177  }
178  switch($a_name)
179  {
180  case "targetFolder":
181  if(!isset($this->mails[$this->counter]["folder_id"]))
182  {
183  $tmp_mailbox =& new ilMailbox($this->i3_id);
184  switch($this->cdata)
185  {
186  case "inbox":
187  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getInboxFolder();
188  $this->mails[$this->counter]["read"] = "unread";
189  break;
190  case "sentbox":
191  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getSentFolder();
192  $this->mails[$this->counter]["read"] = "read";
193  break;
194  case "draft":
195  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getDraftsFolder();
196  $this->mails[$this->counter]["read"] = "read";
197  break;
198  case "trash":
199  $this->mails[$this->counter]["folder_id"] = $tmp_mailbox->getTrashFolder();
200  $this->mails[$this->counter]["read"] = "read";
201  break;
202  }
203  }
204  break;
205 
206  case "sendTime":
207  $this->mails[$this->counter]["send_time"] = date("Y-m-d H:i:s",$this->cdata);
208  $this->mails[$this->counter]["timest"] = date("YmdHis",$this->cdata);
209  break;
210 
211  case "to":
212  $this->mails[$this->counter]["rcp_to"] = $this->cdata;
213  break;
214 
215  case "cc":
216  $this->mails[$this->counter]["rcp_cc"] = $this->cdata;
217  break;
218 
219  case "bcc":
220  $this->mails[$this->counter]["rcp_bcc"] = $this->cdata;
221  break;
222 
223  case "url":
224  if($this->cdata)
225  {
226  $this->mails[$this->counter]["m_message"] = "Url: ".$this->cdata." <br>";
227  }
228  break;
229 
230  case "urlDescription":
231  if($this->cdata)
232  {
233  $this->mails[$this->counter]["m_message"] .= $this->cdata."<br>";
234  }
235  break;
236 
237  case "subject":
238  $this->mails[$this->counter]["m_subject"] = $this->cdata;
239  break;
240 
241  case "message":
242  $this->mails[$this->counter]["m_message"] .= $this->cdata;
243  break;
244  }
245  $this->cdata = '';
246  }
247 
248 
252  function handlerCharacterData($a_xml_parser, $a_data)
253  {
254  // i don't know why this is necessary, but
255  // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
256  // in character data, but we don't want that, because it's the
257  // way we mask user html in our content, so we convert back...
258  $a_data = str_replace("<","&lt;",$a_data);
259  $a_data = str_replace(">","&gt;",$a_data);
260 
261  if(!empty($a_data))
262  {
263  $this->cdata .= $a_data;
264  }
265  }
266 
267  function __insert()
268  {
269  global $ilDB;
270 
271  $sth = $ilDB->prepare("INSERT INTO mail VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
272 
273  $ilDB->executeMultiple($sth,$this->__buildSQLArray());
274 
275  return true;
276  }
277  function __buildSQLArray()
278  {
279  global $ilDB;
280 
281  $sql = array();
282 
283  if(!count($this->mails))
284  {
285  return array();
286  }
287 
288  foreach($this->mails as $mail)
289  {
290  $sql[] = (array('0',
291  addslashes($mail["usr_id"]),
292  addslashes($mail["folder_id"]),
293  addslashes($mail["sender_id"]),
294  addslashes(serialize(array())),
295  addslashes($mail["send_time"]),
296  addslashes($mail["rcp_to"]),
297  addslashes($mail["rcp_cc"]),
298  addslashes($mail["rcp_bcc"]),
299  addslashes($mail["read"]),
300  addslashes(serialize(array("normal"))),
301  addslashes($mail["m_email"]),
302  addslashes($mail["m_subject"]),
303  addslashes($mail["m_message"]),
304  addslashes($mail["import_name"])));
305 
306  }
307  return $sql ? $sql :array();
308  }
309 }
310 ?>