ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilNICKeyRegisteredObjective.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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  new ilInstIdDefaultStoredObjective($this->config)
52  ];
53  }
54 
55  public function achieve(Setup\Environment $environment): Setup\Environment
56  {
57  $factory = $environment->getResource(Setup\Environment::RESOURCE_SETTINGS_FACTORY);
58  $settings = $factory->settingsFor("common");
59 
60  $systemfolder_config = $environment->getConfigFor("systemfolder");
61  $http_config = $environment->getConfigFor("http");
62 
64  throw new Setup\UnachievableException(
65  "CURL extension is required to register NIC."
66  );
67  }
68 
69  //ATTENTION: This makes ilProxySettings work. It uses global ilSetting...
70  $old_settings = $GLOBALS["ilSetting"] ?? null;
71  $GLOBALS["ilSetting"] = $settings;
72 
73  $old_DIC = $GLOBALS["DIC"];
74  $GLOBALS["DIC"] = new ILIAS\DI\Container();
75  $GLOBALS["DIC"]["ilSetting"] = $GLOBALS["ilSetting"];
76 
77  $url = $this->getURLStringForNIC($settings, $systemfolder_config, $http_config);
78  $req = $this->getCurlConnection($url);
79  $response = $req->exec();
80  $req->parseResponse($response);
81 
82  if ($req->getInfo()["http_code"] != "200") {
83  $settings->set("nic_enabled", "-1");
84  throw new Setup\UnachievableException(
85  "Could not connect to NIC server at \"" . self::ILIAS_NIC_SERVER . "\""
86  );
87  }
88 
89  $status = explode("\n", $req->getResponseBody());
90 
91  $nic_id = ($status[2] ?? '');
92  if ($nic_id === '') {
93  $settings->set("nic_enabled", "-1");
94  throw new Setup\UnachievableException(
95  "Did not receive valid installation id from " .
96  "NIC server (\"" . self::ILIAS_NIC_SERVER . "\") for URL: $url" .
97  $this->getRegistrationProblem($status)
98  );
99  }
100 
101  $settings->set("nic_enabled", "1");
102  $settings->set("inst_id", $status[2]);
103 
104  $GLOBALS["DIC"] = $old_DIC;
105  $GLOBALS["ilSetting"] = $old_settings;
106 
107  return $environment;
108  }
109 
113  public function isApplicable(Setup\Environment $environment): bool
114  {
115  $factory = $environment->getResource(Setup\Environment::RESOURCE_SETTINGS_FACTORY);
116  $settings = $factory->settingsFor("common");
117  return ($settings->get("inst_id") === '0') && $this->config->getRegisterNIC();
118  }
119 
120  protected function getRegistrationProblem(array $nic_response_parts): string
121  {
122  $error_code = trim((string) ($nic_response_parts[1] ?? ''));
123  $message = 'Unknown reason';
124 
125  switch ($error_code) {
126  case 'INIC-F-01':
127  $message = "NIC server could not connect to database";
128  break;
129 
130  case 'INIC-F-04':
131  $message = "NIC server could not execute query";
132  break;
133 
134  case 'INIC-E-04':
135  $message = "The installation name was missing in request, please check your " .
136  "configuration (systemfolder.client.name)";
137  break;
138 
139  case 'INIC-E-08':
140  $message = "The http path or contact's lastname was missing in request, please check " .
141  "your configuration (http.path or systemfolder.contact.lastname)";
142  break;
143 
144  case 'INIC-E-09':
145  $message = "The contact's firstname was missing in request, please check your " .
146  "configuration (systemfolder.contact.firstname)";
147  break;
148 
149  case 'INIC-E-15':
150  $message = "The contact's email address was missing in request, please check your " .
151  "configuration (systemfolder.contact.email)";
152  break;
153  }
154 
155  return 'Reason: ' . $message;
156  }
157 
158  protected function getURLStringForNIC($settings, \ilSystemFolderSetupConfig $systemfolder_config, \ilHttpSetupConfig $http_config): string
159  {
160  $inst_id = (string) $settings->get('inst_id', '0');
161  $http_path = $http_config->getHttpPath();
162  $host_name = parse_url($http_path)["host"];
163 
164  $url = self::ILIAS_NIC_SERVER .
165  "?cmd=getid" .
166  "&inst_id=" . rawurlencode($inst_id) .
167  "&hostname=" . rawurlencode($host_name) .
168  "&inst_name=" . rawurlencode($systemfolder_config->getClientName() ?? '') .
169  "&inst_info=" . rawurlencode($systemfolder_config->getClientDescription() ?? '') .
170  "&http_path=" . rawurlencode($http_path) .
171  "&contact_firstname=" . rawurlencode($systemfolder_config->getContactFirstname()) .
172  "&contact_lastname=" . rawurlencode($systemfolder_config->getContactLastname()) .
173  "&contact_email=" . rawurlencode($systemfolder_config->getContactEMail()) .
174  "&nic_key=" . rawurlencode($settings->get("nic_key"));
175 
176  return $url;
177  }
178 
179  protected function getCurlConnection(string $url): \ilCurlConnection
180  {
181  $req = new \ilCurlConnection($url);
182  $req->init();
183 
184  $req->setOpt(CURLOPT_HEADER, 1);
185  $req->setOpt(CURLOPT_RETURNTRANSFER, 1);
186  $req->setOpt(CURLOPT_CONNECTTIMEOUT, self::SOCKET_TIMEOUT);
187  $req->setOpt(CURLOPT_MAXREDIRS, self::MAX_REDIRECTS);
188 
189  return $req;
190  }
191 }
$response
Definition: xapitoken.php:93
$url
Definition: shib_logout.php:66
getPreconditions(Setup\Environment $environment)
getRegistrationProblem(array $nic_response_parts)
catch(\Exception $e) $req
Definition: xapiproxy.php:91
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$GLOBALS["DIC"]
Definition: wac.php:53
getResource(string $id)
Consumers of this method should check if the result is what they expect, e.g.
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:31
achieve(Setup\Environment $environment)
getURLStringForNIC($settings, \ilSystemFolderSetupConfig $systemfolder_config, \ilHttpSetupConfig $http_config)