ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilNICKeyRegisteredObjective.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2019 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
5 use ILIAS\Setup;
6 
8 {
9  const MAX_REDIRECTS = 5;
10  const SOCKET_TIMEOUT = 5;
11  const ILIAS_NIC_SERVER = "https://nic.ilias.de/index.php";
12 
13  public function getHash() : string
14  {
15  return hash("sha256", self::class);
16  }
17 
18  public function getLabel() : string
19  {
20  return "The NIC key is registered at the ILIAS Open Source society";
21  }
22 
23  public function isNotable() : bool
24  {
25  return true;
26  }
27 
28  public function getPreconditions(Setup\Environment $environment) : array
29  {
30  $http_config = $environment->getConfigFor("http");
31  return [
32  new \ilNICKeyStoredObjective($this->config),
33  new \ilSettingsFactoryExistsObjective(),
34  new \ilHttpConfigStoredObjective($http_config)
35  ];
36  }
37 
38  public function achieve(Setup\Environment $environment) : Setup\Environment
39  {
40  $factory = $environment->getResource(Setup\Environment::RESOURCE_SETTINGS_FACTORY);
41  $settings = $factory->settingsFor("common");
42  $systemfolder_config = $environment->getConfigFor("systemfolder");
43  $http_config = $environment->getConfigFor("http");
44 
46  throw new Setup\UnachievableException(
47  "CURL extension is required to register NIC."
48  );
49  }
50 
51  //ATTENTION: This makes ilProxySettings work. It uses global ilSetting...
52  $old_settings = $GLOBALS["ilSetting"] ?? null;
53  $GLOBALS["ilSetting"] = $settings;
54 
55  $url = $this->getURLStringForNIC($settings, $systemfolder_config, $http_config);
56  $req = $this->getCurlConnection($url);
57  $response = $req->exec();
58  $req->parseResponse($response);
59 
60  if ($req->getInfo()["http_code"] != "200") {
61  $settings->set("nic_enabled", "-1");
62  throw new Setup\UnachievableException(
63  "Could not connect to NIC server at \"" . self::ILIAS_NIC_SERVER . "\""
64  );
65  }
66 
67  $status = explode("\n", $req->getResponseBody());
68 
69  $nic_id = (string) ($status[2] ?? '');
70  if ($nic_id === '') {
71  $settings->set("nic_enabled", "-1");
72  throw new Setup\UnachievableException(
73  "Did not receive valid installation id from " .
74  "NIC server (\"" . self::ILIAS_NIC_SERVER . "\") for URL: $url" .
75  $this->getRegistrationProblem($status)
76  );
77  }
78 
79  $settings->set("nic_enabled", "1");
80  $settings->set("inst_id", $status[2]);
81 
82  $GLOBALS["ilSetting"] = $old_settings;
83 
84  return $environment;
85  }
86 
90  public function isApplicable(Setup\Environment $environment) : bool
91  {
92  return true;
93  }
94 
95  protected function getRegistrationProblem(array $nic_response_parts) : string
96  {
97  $error_code = trim((string) ($nic_response_parts[1] ?? ''));
98  $message = 'Unknown reason';
99 
100  switch ($error_code) {
101  case 'INIC-F-01':
102  $message = "NIC server could not connect to database";
103  break;
104 
105  case 'INIC-F-04':
106  $message = "NIC server could not execute query";
107  break;
108 
109  case 'INIC-E-04':
110  $message = "The installation name was missing in request, please check your " .
111  "configuration (systemfolder.client.name)";
112  break;
113 
114  case 'INIC-E-08':
115  $message = "The http path or contact's lastname was missing in request, please check " .
116  "your configuration (http.path or systemfolder.contact.lastname)";
117  break;
118 
119  case 'INIC-E-09':
120  $message = "The contact's firstname was missing in request, please check your " .
121  "configuration (systemfolder.contact.firstname)";
122  break;
123 
124  case 'INIC-E-15':
125  $message = "The contact's email address was missing in request, please check your " .
126  "configuration (systemfolder.contact.email)";
127  break;
128  }
129 
130  return 'Reason: ' . $message;
131  }
132 
133  protected function getURLStringForNIC($settings, \ilSystemFolderSetupConfig $systemfolder_config, \ilHttpSetupConfig $http_config) : string
134  {
135  $inst_id = (string) $settings->get('inst_id', '0');
136  $http_path = $http_config->getHttpPath();
137  $host_name = parse_url($http_path)["host"];
138 
139  $url = self::ILIAS_NIC_SERVER .
140  "?cmd=getid" .
141  "&inst_id=" . rawurlencode($inst_id) .
142  "&hostname=" . rawurlencode($host_name) .
143  "&inst_name=" . rawurlencode($systemfolder_config->getClientName() ?? '') .
144  "&inst_info=" . rawurlencode($systemfolder_config->getClientDescription() ?? '') .
145  "&http_path=" . rawurlencode($http_path) .
146  "&contact_firstname=" . rawurlencode($systemfolder_config->getContactFirstname()) .
147  "&contact_lastname=" . rawurlencode($systemfolder_config->getContactLastname()) .
148  "&contact_email=" . rawurlencode($systemfolder_config->getContactEMail()) .
149  "&nic_key=" . rawurlencode($settings->get("nic_key"));
150 
151  return $url;
152  }
153 
154  protected function getCurlConnection(string $url)
155  {
156  $req = new \ilCurlConnection($url);
157  $req->init();
158 
159  $req->setOpt(CURLOPT_HEADER, 1);
160  $req->setOpt(CURLOPT_RETURNTRANSFER, 1);
161  $req->setOpt(CURLOPT_CONNECTTIMEOUT, self::SOCKET_TIMEOUT);
162  $req->setOpt(CURLOPT_MAXREDIRS, self::MAX_REDIRECTS);
163 
164  return $req;
165  }
166 }
getPreconditions(Setup\Environment $environment)
getRegistrationProblem(array $nic_response_parts)
static _isCurlExtensionLoaded()
Check if curl extension is loaded.
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:11
isApplicable(Setup\Environment $environment)
$message
Definition: xapiexit.php:14
$url
$response
achieve(Setup\Environment $environment)
$factory
Definition: metadata.php:58
getURLStringForNIC($settings, \ilSystemFolderSetupConfig $systemfolder_config, \ilHttpSetupConfig $http_config)