ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilUserXMLWriter.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 include_once "./Services/Xml/classes/class.ilXmlWriter.php";
6 include_once './Services/User/classes/class.ilObjUserFolder.php';
7 
21 {
22  public $ilias;
23  public $xml;
24  public $users;
25  public $user_id = 0;
26  public $attachRoles = false;
27  public $attachPreferences = false;
28  private static $exportablePrefs;
29 
35  private $settings;
36 
44  public function __construct()
45  {
46  global $DIC;
47 
48  $ilias = $DIC['ilias'];
49  $ilUser = $DIC['ilUser'];
50 
51  parent::__construct();
52 
53  $this->ilias = $ilias;
54  $this->user_id = $ilUser->getId();
55  $this->attachRoles = false;
56 
57  /* $this->exportablePrefs = array(
58  "priv_feed_pass", "language", "style", "skin", 'ilPageEditor_HTMLMode',
59  'ilPageEditor_JavaScript', 'ilPageEditor_MediaMode', 'tst_javascript',
60  'tst_lastquestiontype', 'tst_multiline_answers', 'tst_use_previous_answers',
61  'graphicalAnswerSetting', "weekstart"
62  );*/
63  }
64 
65  public function setAttachRoles($value)
66  {
67  $this->attachRoles = $value == 1? true : false;
68  }
69 
70  public function setObjects(&$users)
71  {
72  $this->users = &$users;
73  }
74 
75 
76  public function start()
77  {
78  if (!is_array($this->users)) {
79  return false;
80  }
81 
82  $this->__buildHeader();
83 
84 
85  include_once("./Services/User/classes/class.ilUserDefinedFields.php");
86  $udf_data = &ilUserDefinedFields::_getInstance();
87  $udf_data->addToXML($this);
88 
89  foreach ($this->users as $user) {
90  $this->__handleUser($user);
91  }
92 
93  $this->__buildFooter();
94 
95  return true;
96  }
97 
98  public function getXML()
99  {
100  return $this->xmlDumpMem(false);
101  }
102 
103 
104  public function __buildHeader()
105  {
106  $this->xmlSetDtdDef("<!DOCTYPE Users PUBLIC \"-//ILIAS//DTD UserImport//EN\" \"" . ILIAS_HTTP_PATH . "/xml/ilias_user_5_1.dtd\">");
107  $this->xmlSetGenCmt("User of ilias system");
108  $this->xmlHeader();
109 
110  $this->xmlStartTag('Users');
111 
112  return true;
113  }
114 
115  public function __buildFooter()
116  {
117  $this->xmlEndTag('Users');
118  }
119 
120  public function __handleUser($row)
121  {
122  global $DIC;
123 
124  $ilDB = $DIC['ilDB'];
125  $lng = $DIC['lng'];
126  if (!is_array($this->settings)) {
127  include_once('./Services/User/classes/class.ilObjUserFolder.php');
129  }
130 
131  $prefs = ilObjUser::_getPreferences($row["usr_id"]);
132 
133  if (strlen($row["language"]) == 0) {
134  $row["language"] = $lng->getDefaultLanguage();
135  }
136 
137  $attrs = array(
138  'Id' => "il_" . IL_INST_ID . "_usr_" . $row["usr_id"],
139  'Language' => $row["language"],
140  'Action' => "Update"
141  );
142 
143  $this->xmlStartTag("User", $attrs);
144 
145  $this->xmlElement("Login", null, $row["login"]);
146 
147  if ($this->attachRoles == true) {
148  include_once './Services/AccessControl/classes/class.ilObjRole.php';
149 
150  $query = sprintf(
151  "SELECT object_data.title, object_data.description, rbac_fa.* " .
152  "FROM object_data, rbac_ua, rbac_fa WHERE rbac_ua.usr_id = %s " .
153  "AND rbac_ua.rol_id = rbac_fa.rol_id AND object_data.obj_id = rbac_fa.rol_id",
154  $ilDB->quote($row["usr_id"], 'integer')
155  );
156  $rbacresult = $ilDB->query($query);
157 
158  while ($rbacrow = $rbacresult->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) {
159  if ($rbacrow["assign"] != "y") {
160  continue;
161  }
162 
163  $type = "";
164 
165  if ($rbacrow["parent"] == ROLE_FOLDER_ID) {
166  $type = "Global";
167  } else {
168  $type = "Local";
169  }
170  if (strlen($type)) {
171  $this->xmlElement(
172  "Role",
173  array("Id" =>
174  "il_" . IL_INST_ID . "_role_" . $rbacrow["rol_id"], "Type" => $type),
175  $rbacrow["title"]
176  );
177  }
178  }
179  }
180 
181  $this->__addElement("Firstname", $row["firstname"]);
182  $this->__addElement("Lastname", $row["lastname"]);
183  $this->__addElement("Title", $row["title"]);
184 
185  if ($this->canExport("PersonalPicture", "upload")) {
186  $imageData = $this->getPictureValue($row["usr_id"]);
187  if ($imageData) {
188  $value = array_shift($imageData); //$imageData["value"];
189  $this->__addElement("PersonalPicture", $value, $imageData, "upload");
190  }
191  }
192 
193 
194  $this->__addElement("Gender", $row["gender"]);
195  $this->__addElement("Email", $row["email"]);
196  $this->__addElement("SecondEmail", $row["second_email"], null, "second_email");
197  $this->__addElement("Birthday", $row["birthday"]);
198  $this->__addElement("Institution", $row["institution"]);
199  $this->__addElement("Street", $row["street"]);
200  $this->__addElement("City", $row["city"]);
201  $this->__addElement("PostalCode", $row["zipcode"], null, "zipcode");
202  $this->__addElement("Country", $row["country"]);
203  $this->__addElement("SelCountry", $row["sel_country"], null, "sel_country");
204  $this->__addElement("PhoneOffice", $row["phone_office"], null, "phone_office");
205  $this->__addElement("PhoneHome", $row["phone_home"], null, "phone_home");
206  $this->__addElement("PhoneMobile", $row["phone_mobile"], null, "phone_mobile");
207  $this->__addElement("Fax", $row["fax"]);
208  $this->__addElement("Hobby", $row["hobby"]);
209 
210  $this->__addElementMulti("GeneralInterest", $row["interests_general"], null, "interests_general");
211  $this->__addElementMulti("OfferingHelp", $row["interests_help_offered"], null, "interests_help_offered");
212  $this->__addElementMulti("LookingForHelp", $row["interests_help_looking"], null, "interests_help_looking");
213 
214  $this->__addElement("Department", $row["department"]);
215  $this->__addElement("Comment", $row["referral_comment"], null, "referral_comment");
216  $this->__addElement("Matriculation", $row["matriculation"]);
217  $this->__addElement("Active", $row["active"] ? "true":"false");
218  $this->__addElement("ClientIP", $row["client_ip"], null, "client_ip");
219  $this->__addElement("TimeLimitOwner", $row["time_limit_owner"], null, "time_limit_owner");
220  $this->__addElement("TimeLimitUnlimited", $row["time_limit_unlimited"], null, "time_limit_unlimited");
221  $this->__addElement("TimeLimitFrom", $row["time_limit_from"], null, "time_limit_from");
222  $this->__addElement("TimeLimitUntil", $row["time_limit_until"], null, "time_limit_until");
223  $this->__addElement("TimeLimitMessage", $row["time_limit_message"], null, "time_limit_message");
224  $this->__addElement("ApproveDate", $row["approve_date"], null, "approve_date");
225  $this->__addElement("AgreeDate", $row["agree_date"], null, "agree_date");
226 
227  if (strlen($row["auth_mode"]) > 0) {
228  $this->__addElement("AuthMode", null, array("type" => $row["auth_mode"]), "auth_mode", true);
229  }
230 
231  if (strlen($row["ext_account"]) > 0) {
232  $this->__addElement("ExternalAccount", $row["ext_account"], null, "ext_account", true);
233  }
234 
235  if ($this->canExport("Look", "skin_style")) {
236  $this->__addElement("Look", null, array(
237  "Skin" => $prefs["skin"], "Style" => $prefs["style"]
238  ), "skin_style", true);
239  }
240 
241 
242  $this->__addElement("LastUpdate", $row["last_update"], null, "last_update");
243  $this->__addElement("LastLogin", $row["last_login"], null, "last_login");
244 
245  include_once("./Services/User/classes/class.ilUserDefinedData.php");
246  $udf_data = new ilUserDefinedData($row['usr_id']);
247  $udf_data->addToXML($this, $this->settings);
248 
249  $this->__addElement("AccountInfo", $row["ext_account"], array("Type" => "external"));
250 
251  $this->__addElement("GMapsInfo", null, array(
252  "longitude" => $row["longitude"],
253  "latitude" => $row["latitude"],
254  "zoom" => $row["loc_zoom"]));
255 
256  $this->__addElement("Feedhash", $row["feed_hash"]);
257 
258  if ($this->attachPreferences || $this->canExport("prefs", "preferences")) {
259  $this->__handlePreferences($prefs, $row);
260  }
261 
262  $this->xmlEndTag('User');
263  }
264 
265 
266  private function __handlePreferences($prefs, $row)
267  {
268  //todo nadia: test mail_address_option
269  include_once("Services/Mail/classes/class.ilMailOptions.php");
270  $mailOptions = new ilMailOptions($row["usr_id"]);
271  $prefs["mail_incoming_type"] = $mailOptions->getIncomingType();
272  $prefs["mail_address_option"] = $mailOptions->getMailAddressOption();
273  $prefs["mail_signature"] = $mailOptions->getSignature();
274  $prefs["mail_linebreak"] = $mailOptions->getLinebreak();
275  if (count($prefs)) {
276  $this->xmlStartTag("Prefs");
277  foreach ($prefs as $key => $value) {
279  $this->xmlElement("Pref", array("key" => $key), $value);
280  }
281  }
282  $this->xmlEndTag("Prefs");
283  }
284  }
285 
286  public function __addElementMulti($tagname, $value, $attrs = null, $settingsname = null, $requiredTag = false)
287  {
288  if (is_array($value) && sizeof($value)) {
289  foreach ($value as $idx => $item) {
290  $this->__addElement($tagname, $item, $attrs, $settingsname, $requiredTag);
291  }
292  }
293  }
294 
295  public function __addElement($tagname, $value, $attrs = null, $settingsname = null, $requiredTag = false)
296  {
297  if ($this->canExport($tagname, $settingsname)) {
298  if (strlen($value) > 0 || $requiredTag || (is_array($attrs) && count($attrs) > 0)) {
299  $this->xmlElement($tagname, $attrs, $value);
300  }
301  }
302  }
303 
304  private function canExport($tagname, $settingsname = null)
305  {
306  return !is_array($this->settings) ||
307  in_array(strtolower($tagname), $this->settings) !== false ||
308  in_array($settingsname, $this->settings) !== false;
309  }
310 
316  public function setSettings($settings)
317  {
318  $this->settings = $settings;
319  }
320 
326  private function getPictureValue($usr_id)
327  {
328  global $DIC;
329 
330  $ilDB = $DIC['ilDB'];
331  // personal picture
332  $q = sprintf(
333  "SELECT value FROM usr_pref WHERE usr_id = %s AND keyword = %s",
334  $ilDB->quote($usr_id, "integer"),
335  $ilDB->quote('profile_image', "text")
336  );
337  $r = $ilDB->query($q);
338  if ($ilDB->numRows($r) == 1) {
339  $personal_picture_data = $r->fetchRow(ilDBConstants::FETCHMODE_ASSOC);
340  $personal_picture = $personal_picture_data["value"];
341  $webspace_dir = ilUtil::getWebspaceDir();
342  $image_file = $webspace_dir . "/usr_images/" . $personal_picture;
343  if (@is_file($image_file)) {
344  $fh = fopen($image_file, "rb");
345  if ($fh) {
346  $image_data = fread($fh, filesize($image_file));
347  fclose($fh);
348  $base64 = base64_encode($image_data);
349  $imagetype = "image/jpeg";
350  if (preg_match("/.*\.(png|gif)$/", $personal_picture, $matches)) {
351  $imagetype = "image/" . $matches[1];
352  }
353  return array(
354  "value" => $base64,
355  "encoding" => "Base64",
356  "imagetype" => $imagetype
357  );
358  }
359  }
360  }
361  return false;
362  }
363 
364 
371  public function setAttachPreferences($attachPrefs)
372  {
373  $this->attachPreferences = $attachPrefs;
374  }
375 
381  public static function getExportablePreferences()
382  {
383  return array(
384  'hits_per_page',
385  'public_city',
386  'public_country',
387  'public_department',
388  'public_email',
389  'public_second_email',
390  'public_fax',
391  'public_hobby',
392  'public_institution',
393  'public_matriculation',
394  'public_phone',
395  'public_phone_home',
396  'public_phone_mobile',
397  'public_phone_office',
398  'public_profile',
399  'public_street',
400  'public_upload',
401  'public_zip',
402  'send_info_mails',
403  /*'show_users_online',*/
404  'hide_own_online_status',
405  'bs_allow_to_contact_me',
406  'chat_osc_accept_msg',
407  'user_tz',
408  'weekstart',
409  'mail_incoming_type',
410  'mail_signature',
411  'mail_linebreak',
412  'public_interests_general',
413  'public_interests_help_offered',
414  'public_interests_help_looking'
415  );
416  }
417 
424  public static function isPrefExportable($key)
425  {
427  }
428 }
getPictureValue($usr_id)
return array with baseencoded picture data as key value, encoding type as encoding, and image type as key type.
Class ilMailOptions this class handles user mails.
xmlStartTag($tag, $attrs=null, $empty=false, $encode=true, $escape=true)
Writes a starttag.
xmlSetGenCmt($genCmt)
Sets generated comment.
settings()
Definition: settings.php:2
static _getInstance()
Get instance.
Class ilUserDefinedData.
__handlePreferences($prefs, $row)
__addElement($tagname, $value, $attrs=null, $settingsname=null, $requiredTag=false)
xmlSetDtdDef($dtdDef)
Sets dtd definition.
$type
global $DIC
Definition: saml.php:7
xmlDumpMem($format=true)
Returns xml document from memory.
setSettings($settings)
write access to settings
XML writer class.
static isPrefExportable($key)
returns wether a key from db is exportable or not
canExport($tagname, $settingsname=null)
xmlEndTag($tag)
Writes an endtag.
static getExportSettings()
getExport Settings
$r
Definition: example_031.php:79
setAttachPreferences($attachPrefs)
if set to true, all preferences of a user will be set
$lng
XML writer class.
$ilUser
Definition: imgupload.php:18
redirection script todo: (a better solution should control the processing via a xml file) ...
xmlHeader()
Writes xml header public.
$query
$user
Definition: migrateto20.php:57
$row
__addElementMulti($tagname, $value, $attrs=null, $settingsname=null, $requiredTag=false)
xmlElement($tag, $attrs=null, $data=null, $encode=true, $escape=true)
Writes a basic element (no children, just textual content)
global $ilDB
static getExportablePreferences()
return exportable preference keys as found in db
static _getPreferences($user_id)
get preferences for user
static getWebspaceDir($mode="filesystem")
get webspace directory
$key
Definition: croninfo.php:18
__construct()
constructor