ILIAS  Release_4_1_x_branch Revision 61804
 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('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  {
203  include_once("./Services/Init/classes/class.ilInitialisation.php");
204  $init = new ilInitialisation();
205  return $init->initILIAS("soap");
206  }
207 
208 
209  function __initAuthenticationObject($a_auth_mode = AUTH_LOCAL)
210  {
211  include_once './Services/Authentication/classes/class.ilAuthFactory.php';
213  }
214 
215 
216  function __raiseError($a_message,$a_code)
217  {
218  #echo $a_message, $a_code;
219  switch($this->error_method)
220  {
221  case NUSOAP:
222  return new soap_fault($a_code,'',$a_message);
223  case PHP5:
224  return new SoapFault($a_code, $a_message);
225  }
226  }
227 
235  function getNIC($sid)
236  {
237  $this->initAuth($sid);
238  $this->initIlias();
239 
240  if(!$this->__checkSession($sid))
241  {
242  return $this->__raiseError($this->__getMessage(),$this->__getMessageCode());
243  }
244 
245  global $rbacsystem, $rbacreview, $ilLog, $rbacadmin,$ilSetting, $ilClientIniFile;
246 
247  if (!is_object($ilClientIniFile)) {
248  return $this->__raiseError("Client ini is not initialized","Server");
249  }
250 
251  $auth_modes = ilAuthUtils::_getActiveAuthModes();
252  $auth_mode_default = strtoupper(ilAuthUtils::_getAuthModeName(array_shift($auth_modes)));
253  $auth_mode_names = array();
254  foreach ($auth_modes as $mode) {
255  $auth_mode_names[] = strtoupper(ilAuthUtils::_getAuthModeName($mode));
256  }
257 
258  include_once 'Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php';
259  include_once 'Services/AdvancedMetaData/classes/class.ilAdvancedMDRecordXMLWriter.php';
260 
261  // create advanced meta data record xml
262  $record_ids = array();
264  foreach($record_types as $type) {
266  foreach ($records as $record){
267  $record_ids [] = $record->getRecordId();
268  }
269  }
270  $record_ids = array_unique($record_ids);
271  $advmwriter = new ilAdvancedMDRecordXMLWriter($record_ids);
272  $advmwriter->write();
273 
274  // create user defined fields record xml, simulate empty user records
275  include_once ("./Services/User/classes/class.ilUserXMLWriter.php");
276  $udfWriter = new ilUserXMLWriter();
277  $users = array();
278  $udfWriter->setObjects($users);
279  $udfWriter->start();
280 
281  // todo: get information from client id, read from ini file specificied
282  $client_details[] = array ("installation_id" => IL_INST_ID,
283  "installation_version" => ILIAS_VERSION,
284  "installation_url" => ILIAS_HTTP_PATH,
285  "installation_description" => $ilClientIniFile->readVariable("client","description"),
286  "installation_language_default" => $ilClientIniFile->readVariable("language","default"),
287  "installation_session_expire" => $ilClientIniFile->readVariable("session","expire"),
288  "installation_php_postmaxsize" => $this->return_bytes(ini_get("post_max_size")),
289  "authentication_methods" => join(",", $auth_mode_names),
290  "authentication_default_method" => $auth_mode_default,
291  "installation_udf_xml" => $udfWriter ->getXML(),
292  "installation_advmd_xml" => $advmwriter->xmlDumpMem(false)
293 
294  );
295 
296  // store into xml result set
297  include_once './webservice/soap/classes/class.ilXMLResultSet.php';
298 
299 
300  $xmlResult = new ilXMLResultSet();
301  $xmlResult->addArray($client_details, true);
302 
303  // create writer and return xml
304  include_once './webservice/soap/classes/class.ilXMLResultSetWriter.php';
305  $xmlResultWriter = new ilXMLResultSetWriter($xmlResult);
306  $xmlResultWriter->start();
307  return $xmlResultWriter->getXML();
308  }
309 
314  public static function return_bytes($val) {
315  $val = trim($val);
316  $last = strtolower($val{strlen($val)-1});
317  switch($last) {
318  // The 'G' modifier is available since PHP 5.1.0
319  case 'g':
320  $val *= 1024;
321  case 'm':
322  $val *= 1024;
323  case 'k':
324  $val *= 1024;
325  }
326  return $val;
327  }
328 
329  public function isFault($object) {
330  switch($this->error_method)
331  {
332  case NUSOAP:
333  return $object instanceof soap_fault;
334  case PHP5:
335  return $object instanceof SoapFault;
336  }
337  return true;
338  }
339 
349  public function checkObjectAccess($ref_id, $expected_type, $permission, $returnObject = false) {
350  global $rbacsystem;
351  if(!is_numeric($ref_id))
352  {
353  return $this->__raiseError('No valid id given.',
354  'Client');
355  }
356  if (!ilObject::_exists($ref_id, true)) {
357  return $this->__raiseError('No object for id.',
358  'CLIENT_OBJECT_NOT_FOUND');
359  }
360 
362  return $this->__raiseError('Object is already trashed.',
363  'CLIENT_OBJECT_DELETED');
364  }
365 
367  if ((is_array($expected_type) && !in_array($type, $expected_type))
368  ||
369  (!is_array($expected_type) && $type != $expected_type)
370  )
371  {
372  return $this->__raiseError("Wrong type $type for id. Expected: ".(is_array($expected_type) ? join (",",$expected_type) : $expected_type), 'CLIENT_OBJECT_WRONG_TYPE');
373  }
374 
375  if (!$rbacsystem->checkAccess($permission, $ref_id, $type))
376  {
377  return $this->__raiseError('Missing permission $permission for type $type.', 'CLIENT_OBJECT_WRONG_PERMISSION');
378  }
379 
380  if ($returnObject) {
382  }
383 
384  return $type;
385  }
386 
387  public function getInstallationInfoXML()
388  {
389  require_once("Services/Init/classes/class.ilInitialisation.php");
390 
391  $init = new ilInitialisation();
392  $init->requireCommonIncludes();
393  $init->initIliasIniFile();
394 
395  $ilias = & new ILIAS();
396  $GLOBALS['ilias'] =& $ilias;
397 
398 
399  $settings = array();
400  $clientdirs = glob(ILIAS_WEB_DIR."/*",GLOB_ONLYDIR);
401  require_once ("webservice/soap/classes/class.ilSoapInstallationInfoXMLWriter.php");
402  $writer = new ilSoapInstallationInfoXMLWriter ();
403  $writer->start();
404  if (is_array($clientdirs))
405  {
406  foreach ($clientdirs as $clientdir)
407  {
408  if (is_object($clientInfo= $this->getClientInfo($init, $clientdir)))
409  {
410  $writer->addClient ($clientInfo);
411  }
412  }
413  }
414  $writer->end();
415 
416  return $writer->getXML();
417  }
418 
419  public function getClientInfoXML($clientid)
420  {
421  require_once("Services/Init/classes/class.ilInitialisation.php");
422 
423  $init = new ilInitialisation();
424  $init->requireCommonIncludes();
425  $init->initIliasIniFile();
426 
427  $ilias = & new ILIAS();
428  $GLOBALS['ilias'] =& $ilias;
429 
430 
431  $settings = array();
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("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  $ilDB->initFromIniFile($ilClientIniFile);
474  if ($ilDB->connect(true))
475  {
476  $GLOBALS['ilDB'] = $ilDB;
477 
478  require_once("Services/Administration/classes/class.ilSetting.php");
479  $settings = new ilSetting();
480  $GLOBALS["ilSetting"] = $settings;
481  // workaround to determine http path of client
482  define ("IL_INST_ID", $settings->get("inst_id",0));
483  $settings->access = $ilClientIniFile->readVariable("client", "access");
484  $settings->description = $ilClientIniFile->readVariable("client","description");
485  $settings->session = min((int) ini_get("session.gc_maxlifetime"), (int) $ilClientIniFile->readVariable("session","expire"));
486  $settings->language = $ilClientIniFile->readVariable("language","default");
487  $settings->clientid = basename($client_dir); //pathinfo($client_dir, PATHINFO_FILENAME);
488  $settings->default_show_users_online = $settings->get("show_users_online");
489  $settings->default_hits_per_page = $settings->get("hits_per_page");
490  $skin = $ilClientIniFile->readVariable("layout","skin");
491  $style = $ilClientIniFile->readVariable("layout","style");
492  $settings->default_skin_style = $skin.":".$style;
493  return $settings;
494  }
495  return null;
496  }
497 
498 
499 }
500 ?>