ILIAS  Release_5_0_x_branch Revision 61816
 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  {
84  global $ilAuth, $ilUser;
85 
86  list($sid,$client) = $this->__explodeSid($sid);
87 
88  if(!strlen($sid))
89  {
90  $this->__setMessage('No session id given');
91  $this->__setMessageCode('Client');
92  return false;
93  }
94  if(!$client)
95  {
96  $this->__setMessage('No client given');
97  $this->__setMessageCode('Client');
98  return false;
99  }
100  if(!$ilAuth->getAuth())
101  {
102  switch($ilAuth->getStatus())
103  {
104  case AUTH_EXPIRED:
105  $this->__setMessage('Session expired');
106  $this->__setMessageCode('Server');
107  return false;
108 
109  case AUTH_IDLED:
110  $this->__setMessage('Session idled');
111  $this->__setMessageCode('Server');
112  return false;
113 
114  case AUTH_WRONG_LOGIN:
115  $this->__setMessage('Wrong Login or Password');
116  $this->__setMessageCode('Client');
117  return false;
118 
119  default:
120  $this->__setMessage('Session invalid');
121  $this->__setMessageCode('Client');
122  return false;
123  }
124  }
125 
126  if($ilUser->hasToAcceptTermsOfService())
127  {
128  $this->__setMessage('User agreement no accepted.');
129  $this->__setMessageCode('Server');
130  return false;
131  }
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  $writer->addClient ($clientInfo);
416  }
417  }
418  }
419  $writer->end();
420 
421  return $writer->getXML();
422  }
423 
424  public function getClientInfoXML($clientid)
425  {
426  include_once "Services/Context/classes/class.ilContext.php";
428 
429  require_once("Services/Init/classes/class.ilInitialisation.php");
431 
432  $clientdir = ILIAS_WEB_DIR."/".$clientid;
433  require_once ("webservice/soap/classes/class.ilSoapInstallationInfoXMLWriter.php");
434  $writer = new ilSoapInstallationInfoXMLWriter ();
435  $writer->setExportAdvancedMetaDataDefinitions (true);
436  $writer->setExportUDFDefinitions (true);
437  $writer->start();
438  if (is_object($client = $this->getClientInfo($init, $clientdir)))
439  {
440  $writer->addClient($client);
441  }
442  else
443  return $this->__raiseError("Client ID $clientid does not exist!", 'Client');
444  $writer->end();
445  return $writer->getXML();
446  }
447 
448  private function getClientInfo ($init, $client_dir)
449  {
450  global $ilDB;
451  $ini_file = "./".$client_dir."/client.ini.php";
452 
453  // get settings from ini file
454  require_once("./Services/Init/classes/class.ilIniFile.php");
455 
456  $ilClientIniFile = new ilIniFile($ini_file);
457  $ilClientIniFile->read();
458  if ($ilClientIniFile->ERROR != "")
459  {
460  return false;
461  }
462  $client_id = $ilClientIniFile->readVariable('client','name');
463  if ($ilClientIniFile->variableExists('client', 'expose'))
464  {
465  $client_expose = $ilClientIniFile->readVariable('client','expose');
466  if ($client_expose == "0")
467  return false;
468  }
469 
470  // build dsn of database connection and connect
471  require_once("./Services/Database/classes/class.ilDBWrapperFactory.php");
472  $ilDB = ilDBWrapperFactory::getWrapper($ilClientIniFile->readVariable("db","type"),
473  $ilClientIniFile->readVariable("db","inactive_mysqli"));
474  $ilDB->initFromIniFile($ilClientIniFile);
475  if ($ilDB->connect(true))
476  {
477  $GLOBALS['ilDB'] = $ilDB;
478 
479  require_once("Services/Administration/classes/class.ilSetting.php");
480  $settings = new ilSetting();
481  $GLOBALS["ilSetting"] = $settings;
482  // workaround to determine http path of client
483  define ("IL_INST_ID", $settings->get("inst_id",0));
484  $settings->access = $ilClientIniFile->readVariable("client", "access");
485  $settings->description = $ilClientIniFile->readVariable("client","description");
486  $settings->session = min((int) ini_get("session.gc_maxlifetime"), (int) $ilClientIniFile->readVariable("session","expire"));
487  $settings->language = $ilClientIniFile->readVariable("language","default");
488  $settings->clientid = basename($client_dir); //pathinfo($client_dir, PATHINFO_FILENAME);
489  $settings->default_show_users_online = $settings->get("show_users_online");
490  $settings->default_hits_per_page = $settings->get("hits_per_page");
491  $skin = $ilClientIniFile->readVariable("layout","skin");
492  $style = $ilClientIniFile->readVariable("layout","style");
493  $settings->default_skin_style = $skin.":".$style;
494  return $settings;
495  }
496  return null;
497  }
498 
499 
500 }
501 ?>