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