ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilNICKeyRegisteredObjective.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 use ILIAS\Setup;
22 
24 {
25  public const MAX_REDIRECTS = 5;
26  public const SOCKET_TIMEOUT = 5;
27  public const ILIAS_NIC_SERVER = "https://nic.ilias.de/index.php";
28 
29  public function getHash(): string
30  {
31  return hash("sha256", self::class);
32  }
33 
34  public function getLabel(): string
35  {
36  return "The NIC key is registered at the ILIAS Open Source society";
37  }
38 
39  public function isNotable(): bool
40  {
41  return true;
42  }
43 
44  public function getPreconditions(Setup\Environment $environment): array
45  {
46  $http_config = $environment->getConfigFor("http");
47  return [
48  new \ilNICKeyStoredObjective($this->config),
49  new \ilSettingsFactoryExistsObjective(),
50  new \ilHttpConfigStoredObjective($http_config)
51  ];
52  }
53 
54  public function achieve(Setup\Environment $environment): Setup\Environment
55  {
56  $factory = $environment->getResource(Setup\Environment::RESOURCE_SETTINGS_FACTORY);
57  $settings = $factory->settingsFor("common");
58  $systemfolder_config = $environment->getConfigFor("systemfolder");
59  $http_config = $environment->getConfigFor("http");
60 
62  throw new Setup\UnachievableException(
63  "CURL extension is required to register NIC."
64  );
65  }
66 
67  //ATTENTION: This makes ilProxySettings work. It uses global ilSetting...
68  $old_settings = $GLOBALS["ilSetting"] ?? null;
69  $GLOBALS["ilSetting"] = $settings;
70 
71  $old_DIC = $GLOBALS["DIC"];
72  $GLOBALS["DIC"] = new ILIAS\DI\Container();
73  $GLOBALS["DIC"]["ilSetting"] = $GLOBALS["ilSetting"];
74 
75  $url = $this->getURLStringForNIC($settings, $systemfolder_config, $http_config);
76  $req = $this->getCurlConnection($url);
77  $response = $req->exec();
78  $req->parseResponse($response);
79 
80 
81  if ($req->getInfo()["http_code"] != "200") {
82  $settings->set("nic_enabled", "-1");
83  throw new Setup\UnachievableException(
84  "Could not connect to NIC server at \"" . self::ILIAS_NIC_SERVER . "\""
85  );
86  }
87 
88  $status = explode("\n", $req->getResponseBody());
89 
90  $nic_id = (string) ($status[2] ?? '');
91  if ($nic_id === '') {
92  $settings->set("nic_enabled", "-1");
93  throw new Setup\UnachievableException(
94  "Did not receive valid installation id from " .
95  "NIC server (\"" . self::ILIAS_NIC_SERVER . "\") for URL: $url" .
96  $this->getRegistrationProblem($status)
97  );
98  }
99 
100  $settings->set("nic_enabled", "1");
101  $settings->set("inst_id", $status[2]);
102 
103  $GLOBALS["DIC"] = $old_DIC;
104  $GLOBALS["ilSetting"] = $old_settings;
105 
106 
107  return $environment;
108  }
109 
113  public function isApplicable(Setup\Environment $environment): bool
114  {
115  return true;
116  }
117 
118  protected function getRegistrationProblem(array $nic_response_parts): string
119  {
120  $error_code = trim((string) ($nic_response_parts[1] ?? ''));
121  $message = 'Unknown reason';
122 
123  switch ($error_code) {
124  case 'INIC-F-01':
125  $message = "NIC server could not connect to database";
126  break;
127 
128  case 'INIC-F-04':
129  $message = "NIC server could not execute query";
130  break;
131 
132  case 'INIC-E-04':
133  $message = "The installation name was missing in request, please check your " .
134  "configuration (systemfolder.client.name)";
135  break;
136 
137  case 'INIC-E-08':
138  $message = "The http path or contact's lastname was missing in request, please check " .
139  "your configuration (http.path or systemfolder.contact.lastname)";
140  break;
141 
142  case 'INIC-E-09':
143  $message = "The contact's firstname was missing in request, please check your " .
144  "configuration (systemfolder.contact.firstname)";
145  break;
146 
147  case 'INIC-E-15':
148  $message = "The contact's email address was missing in request, please check your " .
149  "configuration (systemfolder.contact.email)";
150  break;
151  }
152 
153  return 'Reason: ' . $message;
154  }
155 
156  protected function getURLStringForNIC($settings, \ilSystemFolderSetupConfig $systemfolder_config, \ilHttpSetupConfig $http_config): string
157  {
158  $inst_id = (string) $settings->get('inst_id', '0');
159  $http_path = $http_config->getHttpPath();
160  $host_name = parse_url($http_path)["host"];
161 
162  $url = self::ILIAS_NIC_SERVER .
163  "?cmd=getid" .
164  "&inst_id=" . rawurlencode($inst_id) .
165  "&hostname=" . rawurlencode($host_name) .
166  "&inst_name=" . rawurlencode($systemfolder_config->getClientName() ?? '') .
167  "&inst_info=" . rawurlencode($systemfolder_config->getClientDescription() ?? '') .
168  "&http_path=" . rawurlencode($http_path) .
169  "&contact_firstname=" . rawurlencode($systemfolder_config->getContactFirstname()) .
170  "&contact_lastname=" . rawurlencode($systemfolder_config->getContactLastname()) .
171  "&contact_email=" . rawurlencode($systemfolder_config->getContactEMail()) .
172  "&nic_key=" . rawurlencode($settings->get("nic_key"));
173 
174  return $url;
175  }
176 
177  protected function getCurlConnection(string $url): \ilCurlConnection
178  {
179  $req = new \ilCurlConnection($url);
180  $req->init();
181 
182  $req->setOpt(CURLOPT_HEADER, 1);
183  $req->setOpt(CURLOPT_RETURNTRANSFER, 1);
184  $req->setOpt(CURLOPT_CONNECTTIMEOUT, self::SOCKET_TIMEOUT);
185  $req->setOpt(CURLOPT_MAXREDIRS, self::MAX_REDIRECTS);
186 
187  return $req;
188  }
189 }
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
getPreconditions(Setup\Environment $environment)
getRegistrationProblem(array $nic_response_parts)
catch(\Exception $e) $req
Definition: xapiproxy.php:93
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An environment holds resources to be used in the setup process.
Definition: Environment.php:27
isApplicable(Setup\Environment $environment)
$message
Definition: xapiexit.php:32
$url
$response
achieve(Setup\Environment $environment)
$factory
Definition: metadata.php:75
getURLStringForNIC($settings, \ilSystemFolderSetupConfig $systemfolder_config, \ilHttpSetupConfig $http_config)