ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSoapAdministration.php
Go to the documentation of this file.
1 <?php
2  /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2009 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 
35 include_once './webservice/soap/lib/nusoap.php';
36 include_once ("./Services/Authentication/classes/class.ilAuthUtils.php"); // to get auth mode constants
37 
38 define ('SOAP_CLIENT_ERROR', 1);
39 define ('SOAP_SERVER_ERROR', 2);
40 
42 {
43  protected $soap_check = true;
44 
45 
46  /*
47  * object which handles php's authentication
48  * @var object
49  */
50  var $sauth = null;
51 
52  /*
53  * Defines type of error handling (PHP5 || NUSOAP)
54  * @var object
55  */
56  var $error_method = null;
57 
58 
59  function ilSoapAdministration($use_nusoap = true)
60  {
61  define('USER_FOLDER_ID',7);
62  define('NUSOAP',1);
63  define('PHP5',2);
64 
65  if(IL_SOAPMODE == IL_SOAPMODE_NUSOAP)
66  {
67  $this->error_method = NUSOAP;
68  }
69  else
70  {
71  $this->error_method = PHP5;
72  }
73  #echo ("SOAP: using soap mode ".IL_SOAPMODE == IL_SOAPMODE_NUSOAP ? "NUSOAP": "PHP5");
75  }
76 
77  // PROTECTED
78  function __checkSession($sid)
79  {
80  global $ilAuth;
81 
82  list($sid,$client) = $this->__explodeSid($sid);
83 
84  if(!strlen($sid))
85  {
86  $this->__setMessage('No session id given');
87  $this->__setMessageCode('Client');
88  return false;
89  }
90  if(!$client)
91  {
92  $this->__setMessage('No client given');
93  $this->__setMessageCode('Client');
94  return false;
95  }
96  if(!$ilAuth->getAuth())
97  {
98  switch($ilAuth->getStatus())
99  {
100  case AUTH_EXPIRED:
101  $this->__setMessage('Session expired');
102  $this->__setMessageCode('Server');
103  return false;
104 
105  case AUTH_IDLED:
106  $this->__setMessage('Session idled');
107  $this->__setMessageCode('Server');
108  return false;
109 
110  case AUTH_WRONG_LOGIN:
111  $this->__setMessage('Wrong Login or Password');
112  $this->__setMessageCode('Client');
113  return false;
114 
115  default:
116  $this->__setMessage('Session invalid');
117  $this->__setMessageCode('Client');
118  return false;
119  }
120  }
121 
122  global $ilUser;
123 
124  if(!$ilUser->hasAcceptedUserAgreement() and $ilUser->getId() != ANONYMOUS_USER_ID)
125  {
126  $this->__setMessage('User agreement no accepted.');
127  $this->__setMessageCode('Server');
128  return false;
129  }
130 
131  global $ilSetting;
132 
133  if($this->soap_check)
134  {
135  $set = new ilSetting();
136  $this->__setMessage('SOAP is not enabled in ILIAS administration for this client');
137  $this->__setMessageCode('Server');
138  return ($set->get("soap_user_administration") == 1);
139  }
140 
141  return true;
142  }
143 
151  public function initErrorWriter()
152  {
153  include_once('./Services/Init/classes/class.ilErrorHandling.php');
154 
155  set_error_handler(array('ilErrorHandling','_ilErrorWriter'),E_ALL);
156  }
157 
158 
159  function __explodeSid($sid)
160  {
161  $exploded = explode('::',$sid);
162 
163  return is_array($exploded) ? $exploded : array('sid' => '','client' => '');
164  }
165 
166 
167  function __setMessage($a_str)
168  {
169  $this->message = $a_str;
170  }
171  function __getMessage()
172  {
173  return $this->message;
174  }
175  function __appendMessage($a_str)
176  {
177  $this->message .= isset($this->message) ? ' ' : '';
178  $this->message .= $a_str;
179  }
180 
181  public function __setMessageCode($a_code)
182  {
183  $this->message_code = $a_code;
184  }
185 
186  public function __getMessageCode()
187  {
188  return $this->message_code;
189  }
190 
191  public function initAuth($sid)
192  {
193  list($sid,$client) = $this->__explodeSid($sid);
194  define('CLIENT_ID',$client);
195  $_COOKIE['ilClientId'] = $client;
196  $_COOKIE['PHPSESSID'] = $sid;
197  #$_SESSION['_authhttp'.md5(CLIENT_ID)] = true;
198  #$_SESSION['PHPSESSID'] = $sid;
199  }
200 
201  public function initIlias()
202  {
204  {
205  try
206  {
207  require_once("Services/Init/classes/class.ilInitialisation.php");
209  }
210  catch(Exception $e)
211  {
212  // #10608
213  // no need to do anything here, see __checkSession() below
214  }
215  }
216  }
217 
218 
219  function __initAuthenticationObject($a_auth_mode = AUTH_LOCAL)
220  {
221  include_once './Services/Authentication/classes/class.ilAuthFactory.php';
223  }
224 
225 
226  function __raiseError($a_message,$a_code)
227  {
228  #echo $a_message, $a_code;
229  switch($this->error_method)
230  {
231  case NUSOAP:
232  return new soap_fault($a_code,'',$a_message);
233  case PHP5:
234  return new SoapFault($a_code, $a_message);
235  }
236  }
237 
245  function getNIC($sid)
246  {
247  $this->initAuth($sid);
248  $this->initIlias();
249 
250  if(!$this->__checkSession($sid))
251  {
252  return $this->__raiseError($this->__getMessage(),$this->__getMessageCode());
253  }
254 
255  global $rbacsystem, $rbacreview, $ilLog, $rbacadmin,$ilSetting, $ilClientIniFile;
256 
257  if (!is_object($ilClientIniFile)) {
258  return $this->__raiseError("Client ini is not initialized","Server");
259  }
260 
261  $auth_modes = ilAuthUtils::_getActiveAuthModes();
262  $auth_mode_default = strtoupper(ilAuthUtils::_getAuthModeName(array_shift($auth_modes)));
263  $auth_mode_names = array();
264  foreach ($auth_modes as $mode) {
265  $auth_mode_names[] = strtoupper(ilAuthUtils::_getAuthModeName($mode));
266  }
267 
268  include_once 'Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php';
269  include_once 'Services/AdvancedMetaData/classes/class.ilAdvancedMDRecordXMLWriter.php';
270 
271  // create advanced meta data record xml
272  $record_ids = array();
274  foreach($record_types as $type) {
276  foreach ($records as $record){
277  $record_ids [] = $record->getRecordId();
278  }
279  }
280  $record_ids = array_unique($record_ids);
281  $advmwriter = new ilAdvancedMDRecordXMLWriter($record_ids);
282  $advmwriter->write();
283 
284  // create user defined fields record xml, simulate empty user records
285  include_once ("./Services/User/classes/class.ilUserXMLWriter.php");
286  $udfWriter = new ilUserXMLWriter();
287  $users = array();
288  $udfWriter->setObjects($users);
289  $udfWriter->start();
290 
291  // todo: get information from client id, read from ini file specificied
292  $client_details[] = array ("installation_id" => IL_INST_ID,
293  "installation_version" => ILIAS_VERSION,
294  "installation_url" => ILIAS_HTTP_PATH,
295  "installation_description" => $ilClientIniFile->readVariable("client","description"),
296  "installation_language_default" => $ilClientIniFile->readVariable("language","default"),
297  "installation_session_expire" => $ilClientIniFile->readVariable("session","expire"),
298  "installation_php_postmaxsize" => $this->return_bytes(ini_get("post_max_size")),
299  "authentication_methods" => join(",", $auth_mode_names),
300  "authentication_default_method" => $auth_mode_default,
301  "installation_udf_xml" => $udfWriter ->getXML(),
302  "installation_advmd_xml" => $advmwriter->xmlDumpMem(false)
303 
304  );
305 
306  // store into xml result set
307  include_once './webservice/soap/classes/class.ilXMLResultSet.php';
308 
309 
310  $xmlResult = new ilXMLResultSet();
311  $xmlResult->addArray($client_details, true);
312 
313  // create writer and return xml
314  include_once './webservice/soap/classes/class.ilXMLResultSetWriter.php';
315  $xmlResultWriter = new ilXMLResultSetWriter($xmlResult);
316  $xmlResultWriter->start();
317  return $xmlResultWriter->getXML();
318  }
319 
324  public static function return_bytes($val) {
325  $val = trim($val);
326  $last = strtolower($val{strlen($val)-1});
327  switch($last) {
328  // The 'G' modifier is available since PHP 5.1.0
329  case 'g':
330  $val *= 1024;
331  case 'm':
332  $val *= 1024;
333  case 'k':
334  $val *= 1024;
335  }
336  return $val;
337  }
338 
339  public function isFault($object) {
340  switch($this->error_method)
341  {
342  case NUSOAP:
343  return $object instanceof soap_fault;
344  case PHP5:
345  return $object instanceof SoapFault;
346  }
347  return true;
348  }
349 
359  public function checkObjectAccess($ref_id, $expected_type, $permission, $returnObject = false) {
360  global $rbacsystem;
361  if(!is_numeric($ref_id))
362  {
363  return $this->__raiseError('No valid id given.',
364  'Client');
365  }
366  if (!ilObject::_exists($ref_id, true)) {
367  return $this->__raiseError('No object for id.',
368  'CLIENT_OBJECT_NOT_FOUND');
369  }
370 
372  return $this->__raiseError('Object is already trashed.',
373  'CLIENT_OBJECT_DELETED');
374  }
375 
377  if ((is_array($expected_type) && !in_array($type, $expected_type))
378  ||
379  (!is_array($expected_type) && $type != $expected_type)
380  )
381  {
382  return $this->__raiseError("Wrong type $type for id. Expected: ".(is_array($expected_type) ? join (",",$expected_type) : $expected_type), 'CLIENT_OBJECT_WRONG_TYPE');
383  }
384 
385  if (!$rbacsystem->checkAccess($permission, $ref_id, $type))
386  {
387  return $this->__raiseError('Missing permission $permission for type $type.', 'CLIENT_OBJECT_WRONG_PERMISSION');
388  }
389 
390  if ($returnObject) {
392  }
393 
394  return $type;
395  }
396 
397  public function getInstallationInfoXML()
398  {
399  include_once "Services/Context/classes/class.ilContext.php";
401 
402  require_once("Services/Init/classes/class.ilInitialisation.php");
404 
405  $clientdirs = glob(ILIAS_WEB_DIR."/*",GLOB_ONLYDIR);
406  require_once ("webservice/soap/classes/class.ilSoapInstallationInfoXMLWriter.php");
407  $writer = new ilSoapInstallationInfoXMLWriter ();
408  $writer->start();
409  if (is_array($clientdirs))
410  {
411  foreach ($clientdirs as $clientdir)
412  {
413  if (is_object($clientInfo= $this->getClientInfo($init, $clientdir)))
414  {
415  // only export soap enabled clients
416  if ($clientInfo->access == 1)
417  $writer->addClient ($clientInfo, false);
418  }
419  }
420  }
421  $writer->end();
422 
423  return $writer->getXML();
424  }
425 
426  public function getClientInfoXML($clientid)
427  {
428  include_once "Services/Context/classes/class.ilContext.php";
430 
431  require_once("Services/Init/classes/class.ilInitialisation.php");
433 
434  $clientdir = ILIAS_WEB_DIR."/".$clientid;
435  require_once ("webservice/soap/classes/class.ilSoapInstallationInfoXMLWriter.php");
436  $writer = new ilSoapInstallationInfoXMLWriter ();
437  $writer->setExportAdvancedMetaDataDefinitions (true);
438  $writer->setExportUDFDefinitions (true);
439  $writer->start();
440  if (is_object($client = $this->getClientInfo($init, $clientdir)))
441  {
442  $writer->addClient($client);
443  }
444  else
445  return $this->__raiseError("Client ID $clientid does not exist!", 'Client');
446  $writer->end();
447  return $writer->getXML();
448  }
449 
450  public function getClientDetailsXML($sid, $clientid)
451  {
452  $this->initAuth($sid);
453  $this->initIlias();
454 
455  if(!$this->__checkSession($sid))
456  {
457  return $this->__raiseError($this->__getMessage(),$this->__getMessageCode());
458  }
459 
460  global $ilClientIniFile;
461 
462  if (!is_object($ilClientIniFile)) {
463  return $this->__raiseError("Client ini is not initialized","Server");
464  }
465 
466  /* include_once "Services/Context/classes/class.ilContext.php";
467  ilContext::init(ilContext::CONTEXT_SOAP_WITHOUT_CLIENT);
468 
469  require_once("Services/Init/classes/class.ilInitialisation.php");
470  ilInitialisation::initILIAS();*/
471 
472  $clientdir = ILIAS_WEB_DIR."/".$clientid;
473  require_once ("webservice/soap/classes/class.ilSoapInstallationInfoXMLWriter.php");
474  $writer = new ilSoapInstallationInfoXMLWriter ();
475  $writer->setExportAdvancedMetaDataDefinitions (true);
476  $writer->setExportUDFDefinitions (true);
477  $writer->start();
478  if (is_object($client = $this->getClientInfo($init, $clientdir)))
479  {
480  $writer->addClient($client, true);
481  }
482  else
483  return $this->__raiseError("Client ID $clientid does not exist!", 'Client');
484  $writer->end();
485  return $writer->getXML();
486  }
487 
488  private function getClientInfo ($init, $client_dir)
489  {
490  global $ilDB;
491  $ini_file = "./".$client_dir."/client.ini.php";
492 
493  // get settings from ini file
494  require_once("./Services/Init/classes/class.ilIniFile.php");
495 
496  $ilClientIniFile = new ilIniFile($ini_file);
497  $ilClientIniFile->read();
498  if ($ilClientIniFile->ERROR != "")
499  {
500  return false;
501  }
502  $client_id = $ilClientIniFile->readVariable('client','name');
503  if ($ilClientIniFile->variableExists('client', 'expose'))
504  {
505  $client_expose = $ilClientIniFile->readVariable('client','expose');
506  if ($client_expose == "0")
507  return false;
508  }
509 
510  // build dsn of database connection and connect
511  require_once("./Services/Database/classes/class.ilDBWrapperFactory.php");
512  $ilDB = ilDBWrapperFactory::getWrapper($ilClientIniFile->readVariable("db","type"),
513  $ilClientIniFile->readVariable("db","inactive_mysqli"));
514  $ilDB->initFromIniFile($ilClientIniFile);
515  if ($ilDB->connect(true))
516  {
517  $GLOBALS['ilDB'] = $ilDB;
518 
519  require_once("Services/Administration/classes/class.ilSetting.php");
520  $settings = new ilSetting();
521  $GLOBALS["ilSetting"] = $settings;
522  // workaround to determine http path of client
523  define ("IL_INST_ID", $settings->get("inst_id",0));
524  $settings->access = $ilClientIniFile->readVariable("client", "access");
525  $settings->description = $ilClientIniFile->readVariable("client","description");
526  $settings->session = min((int) ini_get("session.gc_maxlifetime"), (int) $ilClientIniFile->readVariable("session","expire"));
527  $settings->language = $ilClientIniFile->readVariable("language","default");
528  $settings->clientid = basename($client_dir); //pathinfo($client_dir, PATHINFO_FILENAME);
529  $settings->default_show_users_online = $settings->get("show_users_online");
530  $settings->default_hits_per_page = $settings->get("hits_per_page");
531  $skin = $ilClientIniFile->readVariable("layout","skin");
532  $style = $ilClientIniFile->readVariable("layout","style");
533  $settings->default_skin_style = $skin.":".$style;
534  return $settings;
535  }
536  return null;
537  }
538 
539 
540 }
541 ?>