| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Copyright (C) 2015 EngSaS - Engineering Solutions and Services Langenbach. All rights reserved.
|
|---|
| 4 |
|
|---|
| 5 | This library is free software; you can redistribute it and/or
|
|---|
| 6 | modify it under the terms of the GNU Lesser General Public
|
|---|
| 7 | License as published by the Free Software Foundation; either
|
|---|
| 8 | version 2.1 of the License, or (at your option) any later version.
|
|---|
| 9 |
|
|---|
| 10 | This library is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 13 | Lesser General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU Lesser General Public
|
|---|
| 16 | License along with this library; if not, write to the Free Software
|
|---|
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|---|
| 18 | MA 02110-1301 USA.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | namespace SiWeather\Webservice;
|
|---|
| 22 |
|
|---|
| 23 | require_once(__DIR__ ."/../../inc/envsetup.php");
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * @brief Configures the Webservice and creates all needed files and directories (e.g. Databases)
|
|---|
| 27 | *
|
|---|
| 28 | * @note Requires php-posix!
|
|---|
| 29 | */
|
|---|
| 30 | class Setup {
|
|---|
| 31 |
|
|---|
| 32 | public function __construct(){
|
|---|
| 33 | $this->_webserviceConfig = new WebserviceConfig();
|
|---|
| 34 | $this->_dataDirectory = $this->_webserviceConfig->getConfig("dataDirectory");
|
|---|
| 35 | $this->_databaseHandler = new DatabaseHandler();
|
|---|
| 36 | $this->_databaseFile = $this->_databaseHandler->databaseFile();
|
|---|
| 37 | $this->_databaseDirectory = pathinfo($this->_databaseFile, PATHINFO_DIRNAME);
|
|---|
| 38 | $this->_databaseFirstConnection = !file_exists($this->_databaseFile);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * @brief Checks if data directory is present and writable and returns a value from DatabaesChecks
|
|---|
| 43 | *
|
|---|
| 44 | * If an error occurs, the error message is stored into @p $errorMessage.
|
|---|
| 45 | *
|
|---|
| 46 | * @see DataDirectoryChecks
|
|---|
| 47 | * @see dataDirectoryCreate()
|
|---|
| 48 | */
|
|---|
| 49 | public function dataDirectoryCheck(&$errorMessage){
|
|---|
| 50 | if(!is_object($this->_webserviceConfig)){
|
|---|
| 51 | $errorMessage = _("Could not load the webservice configuration");
|
|---|
| 52 | return DataDirectoryChecks::NoConfiguration;
|
|---|
| 53 | }
|
|---|
| 54 | if(!$this->_dataDirectory || empty($this->_dataDirectory)){
|
|---|
| 55 | $errorMessage = _("Could not determine dataDirectory setting");
|
|---|
| 56 | return DataDirectoryChecks::NoDirectoryPath;
|
|---|
| 57 | }
|
|---|
| 58 | if(!is_dir($this->_dataDirectory)){
|
|---|
| 59 | $errorMessage = _("Data directory does not exists");
|
|---|
| 60 | return DataDirectoryChecks::NotExist;
|
|---|
| 61 | }
|
|---|
| 62 | if(!is_writable($this->_dataDirectory)){
|
|---|
| 63 | $errorMessage = \Translator::swGettext("Data directory is not writable for %s:%s (%s)",
|
|---|
| 64 | $this->getProcessUser(), $this->getProcessGroup(), $this->_dataDirectory);
|
|---|
| 65 | return DataDirectoryChecks::NotWritable;
|
|---|
| 66 | }
|
|---|
| 67 | return DataDirectoryChecks::Success;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * @brief Creates the data directory and returns a value from DatabaesChecks
|
|---|
| 72 | *
|
|---|
| 73 | * If an error occurs, the error message is stored into @p $errorMessage.
|
|---|
| 74 | *
|
|---|
| 75 | * @see DataDirectoryChecks
|
|---|
| 76 | * @see dataDirectoryCreate()
|
|---|
| 77 | */
|
|---|
| 78 | public function dataDirectoryCreate(&$errorMessage){
|
|---|
| 79 | $result = $this->dataDirectoryCheck($errorMessage);
|
|---|
| 80 | if($result == DataDirectoryChecks::Success){
|
|---|
| 81 | // directory already exists, therefore there is no need to create it
|
|---|
| 82 | return DataDirectoryChecks::Success;
|
|---|
| 83 | }
|
|---|
| 84 | if($result != DataDirectoryChecks::NotExist){
|
|---|
| 85 | return $result;
|
|---|
| 86 | }
|
|---|
| 87 | // clear $errorMessage, because it contains the "Data directory does not exist" entry
|
|---|
| 88 | // from dataDirectoryCheck(), which is not an error at this point!
|
|---|
| 89 | $errorMessage = "";
|
|---|
| 90 | if(!mkdir($this->_dataDirectory)){
|
|---|
| 91 | $errorMessage = \Translator::swGettext("Could not create data directory (%s)", $this->_dataDirectory);
|
|---|
| 92 | return DataDirectoryChecks::Error;
|
|---|
| 93 | }
|
|---|
| 94 | return DataDirectoryChecks::Success;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * @brief Checks everything related to the database path and returns a value from DatabaesChecks
|
|---|
| 99 | *
|
|---|
| 100 | * If an error occurs, the error message is stored into @p $errorMessage.
|
|---|
| 101 | *
|
|---|
| 102 | * @see DatabaseDirectoryChecks
|
|---|
| 103 | * @see databaseDirectoryCreate()
|
|---|
| 104 | */
|
|---|
| 105 | public function databaseDirectoryCheck(&$errorMessage){
|
|---|
| 106 | if(is_null($this->_databaseDirectory) || empty($this->_databaseDirectory)){
|
|---|
| 107 | $errorMessage = \Translator::swGettext("Failed to extract directory from database file path (%s)", $this->_databaseFile);
|
|---|
| 108 | return DatabaseChecks::NoDirectoryPath;
|
|---|
| 109 | }
|
|---|
| 110 | if(!is_dir($this->_databaseDirectory)){
|
|---|
| 111 | $errorMessage = \Translator::swGettext("Database directory does not exists (%s)", $this->_databaseDirectory);
|
|---|
| 112 | return DatabaseChecks::DirectoryDoesNotExist;
|
|---|
| 113 | }
|
|---|
| 114 | if(!is_writable($this->_databaseDirectory)){
|
|---|
| 115 | $errorMessage = \Translator::swGettext("Database directory is not writable for %s:%s (%s)",
|
|---|
| 116 | $this->getProcessUser(), $this->getProcessGroup(),
|
|---|
| 117 | $this->_databaseDirectory);
|
|---|
| 118 | return DatabaseChecks::DirectoryNotWritable;
|
|---|
| 119 | }
|
|---|
| 120 | return DatabaseChecks::Success;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /**
|
|---|
| 124 | * @brief Creates the database directory and returns a value from DatabaesChecks
|
|---|
| 125 | *
|
|---|
| 126 | * If an error occurs, the error message is stored into @p $errorMessage.
|
|---|
| 127 | *
|
|---|
| 128 | * @see DatabaseDirectoryChecks
|
|---|
| 129 | * @see databaseDirectoryCheck()
|
|---|
| 130 | */
|
|---|
| 131 | public function databaseDirectoryCreate(&$errorMessage){
|
|---|
| 132 | $result = $this->databaseDirectoryChecks($errorMessage);
|
|---|
| 133 | if($result == DatabaseChecks::Success){
|
|---|
| 134 | $errorMessage = "";
|
|---|
| 135 | return DatabaseChecks::Success;
|
|---|
| 136 | }
|
|---|
| 137 | if($result == DatabaseChecks::NoDatabaseFile || $result == DatabaseChecks::NoDirectoryPath || $result == DatabaseChecks::DirectoryNotWritable){
|
|---|
| 138 | return $result;
|
|---|
| 139 | }
|
|---|
| 140 | if(!mkdir($this->_databaseDirectory)){
|
|---|
| 141 | $errorMessage = \Translator::swGettext("Could not create database directory (%s)", $this->_databaseDirectory);
|
|---|
| 142 | return DatabaseChecks::Error;
|
|---|
| 143 | }
|
|---|
| 144 | return DatabaseChecks::Success;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | /**
|
|---|
| 148 | * @brief Checks, if the database file is given and is in a writable path
|
|---|
| 149 | *
|
|---|
| 150 | * The database file itself is created by the DatabaseHandler. Therefore
|
|---|
| 151 | * this function only checks, if the database directory exists, and it must be
|
|---|
| 152 | * writable.
|
|---|
| 153 | *
|
|---|
| 154 | * @see DatabaseChecks
|
|---|
| 155 | * @see databaseDirectoryCheck()
|
|---|
| 156 | */
|
|---|
| 157 | public function databaseFileCheck(&$errorMessage){
|
|---|
| 158 | if(is_null($this->_databaseFile) || empty($this->_databaseFile)){
|
|---|
| 159 | $errorMessage = \Translator::swGettext("No database file given (%s)", $this->_databaseHandler->lastErrorMessage());
|
|---|
| 160 | return DatabaseChecks::NoDatabaseFile;
|
|---|
| 161 | }
|
|---|
| 162 | if(file_exists($this->_databaseFile)){
|
|---|
| 163 | if(!is_writable($this->_databaseFile)){
|
|---|
| 164 | $errorMessage = \Translator::swGettext("Database file is not writable for %s:%s (%s)",
|
|---|
| 165 | $this->getProcessUser(), $this->getProcessGroup(),
|
|---|
| 166 | $this->_databaseFile);
|
|---|
| 167 | return DatabaseChecks::DatabaseFileNotWritable;
|
|---|
| 168 | }
|
|---|
| 169 | return DatabaseChecks::Success;
|
|---|
| 170 | }
|
|---|
| 171 | // if the databaseDirectoryCheck function return something other than success,
|
|---|
| 172 | // we probably have a problem (like no write permission, ...)
|
|---|
| 173 | if($this->databaseDirectoryCheck($errorMessage) != DatabaseChecks::Success){
|
|---|
| 174 | return DatabaseChecks::Error;
|
|---|
| 175 | }
|
|---|
| 176 | return DatabaseChecks::Success;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | public function databaseConnection(&$errorMessage){
|
|---|
| 180 | if(!is_object($this->_databaseHandler)){
|
|---|
| 181 | $errorMessage = _("No database handler");
|
|---|
| 182 | return false;
|
|---|
| 183 | }
|
|---|
| 184 | if(!$this->_databaseHandler->connect(true)){
|
|---|
| 185 | $errorMessage = \Translator::swGettext("Could not connect to the database (%s)", $this->_databaseHandler->lastErrorMessage());
|
|---|
| 186 | return false;
|
|---|
| 187 | }
|
|---|
| 188 | // check version, if an schema already exists
|
|---|
| 189 | if($this->_databaseHandler->databaseTableExists("schema")){
|
|---|
| 190 | if(!$this->_databaseHandler->checkVersions()){
|
|---|
| 191 | $errorMessage = \Translator::swGettext("Database schema versions check failed (%s)", $this->_databaseHandler->lastErrorMessage());
|
|---|
| 192 | return false;
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | // prepare Spatalite
|
|---|
| 197 | if(!$this->_databaseHandler->databaseTableExists("spatial_ref_sys")){
|
|---|
| 198 | if(!$this->_databaseHandler->exec("BEGIN")){
|
|---|
| 199 | $errorMessage = _("Could not start transaction to init Spatialite");
|
|---|
| 200 | return false;
|
|---|
| 201 | }
|
|---|
| 202 | if(!$this->_databaseHandler->exec("SELECT InitSpatialMetadata()")){
|
|---|
| 203 | $errorMessage = _("Could not initiate Spatialite metadata");
|
|---|
| 204 | return false;
|
|---|
| 205 | }
|
|---|
| 206 | if(!$this->_databaseHandler->exec("COMMIT")){
|
|---|
| 207 | $errorMessage = _("Could not commit transaction to init Spatialite");
|
|---|
| 208 | return false;
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | // // prepare countries table
|
|---|
| 213 | // $table = "countries";
|
|---|
| 214 | // if(!$this->databaseTableExists($table)){
|
|---|
| 215 | // $sql = "CREATE TABLE ". $table ." (";
|
|---|
| 216 | // $sql .= " id INTEGER NOT NULL PRIMARY KEY,";
|
|---|
| 217 | // $sql .= " name TEXT NOT NULL,";
|
|---|
| 218 | // $sql .= " code VARCHAR(2)";
|
|---|
| 219 | // $sql .= ")";
|
|---|
| 220 | // if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
|
|---|
| 221 | // return false;
|
|---|
| 222 | // }
|
|---|
| 223 | // }
|
|---|
| 224 | //
|
|---|
| 225 | // // prepare regions table
|
|---|
| 226 | // $table = "regions";
|
|---|
| 227 | // if(!$this->databaseTableExists($table)){
|
|---|
| 228 | // $sql = "CREATE TABLE ". $table ." (";
|
|---|
| 229 | // $sql .= " id INTEGER NOT NULL PRIMARY KEY,";
|
|---|
| 230 | // $sql .= " name TEXT NOT NULL,";
|
|---|
| 231 | // $sql .= " code VARCHAR(2),";
|
|---|
| 232 | // $sql .= " country INTEGER,";
|
|---|
| 233 | // $sql .= " FOREIGN KEY(country) REFERENCES countries(id)";
|
|---|
| 234 | // $sql .= ")";
|
|---|
| 235 | // if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
|
|---|
| 236 | // return false;
|
|---|
| 237 | // }
|
|---|
| 238 | // }
|
|---|
| 239 |
|
|---|
| 240 | // prepare schema table
|
|---|
| 241 | $table = "schema";
|
|---|
| 242 | if(!$this->_databaseHandler->databaseTableExists($table)){
|
|---|
| 243 | $sql = "CREATE TABLE ". $table ." (";
|
|---|
| 244 | $sql .= " id INTEGER NOT NULL PRIMARY KEY,";
|
|---|
| 245 | $sql .= " name TEXT NOT NULL,";
|
|---|
| 246 | $sql .= " version INTEGER NOT NULL";
|
|---|
| 247 | $sql .= ")";
|
|---|
| 248 | if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
|
|---|
| 249 | return false;
|
|---|
| 250 | }
|
|---|
| 251 | $sql = "INSERT INTO ". $table ." (name, version) VALUES(:name, :version)";
|
|---|
| 252 | $sqlStatement = $this->_databaseHandler->prepare($sql);
|
|---|
| 253 | $sqlStatement->clear();
|
|---|
| 254 | $sqlStatement->bindValue(":name", "core");
|
|---|
| 255 | $sqlStatement->bindValue(":version", $this->_webserviceConfig->getConfig("databaseVersionCore"));
|
|---|
| 256 | if(!$sqlStatement->execute()){
|
|---|
| 257 | $errorMessage = Translation::swGettext("Could not add schema version %s to the database", "core");
|
|---|
| 258 | error_log($errorMessage);
|
|---|
| 259 | return false;
|
|---|
| 260 | }
|
|---|
| 261 | $sqlStatement->reset();
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | // prepare cities table
|
|---|
| 265 | $table = "cities";
|
|---|
| 266 | if(!$this->_databaseHandler->databaseTableExists($table)){
|
|---|
| 267 | $sql = "CREATE TABLE ". $table ." (";
|
|---|
| 268 | $sql .= " id INTEGER NOT NULL PRIMARY KEY,";
|
|---|
| 269 | $sql .= " name TEXT NOT NULL";
|
|---|
| 270 | $sql .= ")";
|
|---|
| 271 | if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
|
|---|
| 272 | return false;
|
|---|
| 273 | }
|
|---|
| 274 | // creating a POINT Geometry column named center,
|
|---|
| 275 | // taking two coordinates in SRID 4326 (WGS84) format
|
|---|
| 276 | $sql = "SELECT AddGeometryColumn(\"". $table ."\", ";
|
|---|
| 277 | $sql .= "\"location\", 4326, \"POINT\", \"XY\")";
|
|---|
| 278 | if(!$this->_databaseHandler->exec($sql)){
|
|---|
| 279 | $errorMessage = Translation::swGettext("Could not create location field for %s table", $table);
|
|---|
| 280 | return false;
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | // prepare status table
|
|---|
| 285 | $table = "status";
|
|---|
| 286 | if(!$this->_databaseHandler->databaseTableExists($table)){
|
|---|
| 287 | $sql = "CREATE TABLE ". $table ." (";
|
|---|
| 288 | $sql .= " id INTEGER NOT NULL PRIMARY KEY,";
|
|---|
| 289 | $sql .= " name TEXT NOT NULL";
|
|---|
| 290 | $sql .= ")";
|
|---|
| 291 | if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
|
|---|
| 292 | return false;
|
|---|
| 293 | }
|
|---|
| 294 | $status = array("confirming", "active", "deletionrequested");
|
|---|
| 295 | $sql = "INSERT INTO ". $table ." (name) VALUES(:name)";
|
|---|
| 296 | $sqlStatement = $this->_databaseHandler->prepare($sql);
|
|---|
| 297 | foreach ($status as $name){
|
|---|
| 298 | $sqlStatement->clear();
|
|---|
| 299 | $sqlStatement->bindValue(":name", $name);
|
|---|
| 300 | if(!$sqlStatement->execute()){
|
|---|
| 301 | $errorMessage = Translation::swGettext("Could not add status %s to the database", $name);
|
|---|
| 302 | error_log($errorMessage);
|
|---|
| 303 | return false;
|
|---|
| 304 | }
|
|---|
| 305 | $sqlStatement->reset();
|
|---|
| 306 | }
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | // prepare stations table
|
|---|
| 310 | $table = "stations";
|
|---|
| 311 | if(!$this->_databaseHandler->databaseTableExists($table)){
|
|---|
| 312 | $sql = "CREATE TABLE ". $table ." (";
|
|---|
| 313 | $sql .= " id INTEGER NOT NULL PRIMARY KEY,";
|
|---|
| 314 | $sql .= " name TEXT NOT NULL,";
|
|---|
| 315 | $sql .= " city INTEGER NOT NULL,";
|
|---|
| 316 | $sql .= " uuid TEXT NOT NULL,";
|
|---|
| 317 | $sql .= " ownerMail TEXT NOT NULL,";
|
|---|
| 318 | $sql .= " type TEXT NOT NULL,";
|
|---|
| 319 | $sql .= " typeUrl TEXT,";
|
|---|
| 320 | $sql .= " creationDate TIMESTAMP DEFAULT (strftime('%s', 'now')),";
|
|---|
| 321 | // $sql .= " creationDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,";
|
|---|
| 322 | $sql .= " status INTEGER DEFAULT 1,";
|
|---|
| 323 | $sql .= " FOREIGN KEY(city) REFERENCES cities(id)";
|
|---|
| 324 | $sql .= " FOREIGN KEY(status) REFERENCES status(id)";
|
|---|
| 325 | $sql .= ")";
|
|---|
| 326 | if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
|
|---|
| 327 | return false;
|
|---|
| 328 | }
|
|---|
| 329 | // creating a POINT Geometry column named center,
|
|---|
| 330 | // taking two coordinates in SRID 4326 (WGS84) format
|
|---|
| 331 | $sql = "SELECT AddGeometryColumn(\"". $table ."\", ";
|
|---|
| 332 | $sql .= "\"location\", 4326, \"POINT\", \"XY\")";
|
|---|
| 333 | if(!$this->_databaseHandler->exec($sql)){
|
|---|
| 334 | $errorMessage = Translation::swGettext("Could not create location field for %s table", $table);
|
|---|
| 335 | return false;
|
|---|
| 336 | }
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | return true;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | public function databaseVersionSqlite(&$errorMessage){
|
|---|
| 343 | if(!is_object($this->_databaseHandler)){
|
|---|
| 344 | return 0;
|
|---|
| 345 | }
|
|---|
| 346 | $result = $this->_databaseHandler->versionSqlite();
|
|---|
| 347 | if($result == 0){
|
|---|
| 348 | $errorMessage = $this->_databaseHandler->lastErrorMessage();
|
|---|
| 349 | }
|
|---|
| 350 | return $result;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | public function databaseVersionSpatalite(&$errorMessage){
|
|---|
| 354 | if(!is_object($this->_databaseHandler)){
|
|---|
| 355 | return 0;
|
|---|
| 356 | }
|
|---|
| 357 | $result = $this->_databaseHandler->versionSpatialite();
|
|---|
| 358 | if($result == 0){
|
|---|
| 359 | $errorMessage = $this->_databaseHandler->lastErrorMessage();
|
|---|
| 360 | }
|
|---|
| 361 | return $result;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | public function apiCheckWithoutRewrite(&$errorMessage){
|
|---|
| 365 | return $this->apiCheck("index.php/stations", $errorMessage);
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | public function apiCheckWithRewrite(&$errorMessage){
|
|---|
| 369 | return $this->apiCheck("stations", $errorMessage);
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | private function getProcessUser(){
|
|---|
| 373 | $processUser = posix_getpwuid(posix_getuid());
|
|---|
| 374 | return $processUser["name"];
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | private function getProcessGroup(){
|
|---|
| 378 | $processGroup = posix_getgrgid(posix_getgid());
|
|---|
| 379 | return $processGroup["name"];
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | private function apiCheck($url, &$errorMessage){
|
|---|
| 383 | $baseUrl = $this->_webserviceConfig->webserviceBaseUrl();
|
|---|
| 384 | if(!$baseUrl || empty($baseUrl)){
|
|---|
| 385 | $errorMessage = _("No webservice base url given!");
|
|---|
| 386 | return false;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | # base_url needs to end with a /, otherwise the last element is chopped
|
|---|
| 390 | try{
|
|---|
| 391 | $client = new \GuzzleHttp\Client
|
|---|
| 392 | (
|
|---|
| 393 | [
|
|---|
| 394 | "base_uri" => $baseUrl,
|
|---|
| 395 | "timeout" => 2.0,
|
|---|
| 396 | "http_errors" => false,
|
|---|
| 397 | ]
|
|---|
| 398 | );
|
|---|
| 399 | $response = $client->get($url);
|
|---|
| 400 | }
|
|---|
| 401 | catch(\GuzzleHttp\Exception\ConnectException $exception){
|
|---|
| 402 | $errorMessage = \Translator::swGettext("Webservce has not answered at url %s! Error: %s", $baseUrl, $exception->getMessage());
|
|---|
| 403 | return false;
|
|---|
| 404 | }
|
|---|
| 405 | if(is_null($response)){
|
|---|
| 406 | $errorMessage = \Translator::swGettext("Webservice has not answered at url %s!", $baseUrl);
|
|---|
| 407 | return false;
|
|---|
| 408 | }
|
|---|
| 409 | $result = $response->getStatusCode();
|
|---|
| 410 | if($result != 200){
|
|---|
| 411 | $errorMessage = \Translator::swGettext("Got http error %s (%s) at url %s", $result, $response->getReasonPhrase(), $baseUrl . $url);
|
|---|
| 412 | return false;
|
|---|
| 413 | }
|
|---|
| 414 | $result = $response->getReasonPhrase();
|
|---|
| 415 | if($result != "OK"){
|
|---|
| 416 | $errorMessage = \Translator::swGettext("Got a http error reason with http code 200: %s", $result);
|
|---|
| 417 | return false;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | $body = $response->getBody();
|
|---|
| 421 | if(empty($body)){
|
|---|
| 422 | $errorMessage = _("Got empty return value");
|
|---|
| 423 | return false;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | $json = json_decode($body);
|
|---|
| 427 | if(is_null($json)){
|
|---|
| 428 | $errorMessage = _("Could not decode json message body");
|
|---|
| 429 | return false;
|
|---|
| 430 | }
|
|---|
| 431 | return true;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | private function databaseTableCreate($table, $sqlStatement, &$errorMessage){
|
|---|
| 435 | if(!$this->_databaseHandler || !is_object($this->_databaseHandler)){
|
|---|
| 436 | return false;
|
|---|
| 437 | }
|
|---|
| 438 | if(!$this->_databaseHandler->exec("BEGIN")){
|
|---|
| 439 | $errorMessage = \Translator::swGettext("Could not start transaction to create table %s", $table);
|
|---|
| 440 | return false;
|
|---|
| 441 | }
|
|---|
| 442 | if(!$this->_databaseHandler->exec($sqlStatement)){
|
|---|
| 443 | $errorMessage = \Translator::swGettext("Could not execute sql statement to create table %s:\n%s", $table, $sqlStatement);
|
|---|
| 444 | return false;
|
|---|
| 445 | }
|
|---|
| 446 | if(!$this->_databaseHandler->exec("COMMIT")){
|
|---|
| 447 | $errorMessage = \Translator::swGettext("Could not commit transaction to create table %s", $table);
|
|---|
| 448 | return false;
|
|---|
| 449 | }
|
|---|
| 450 | return true;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | private $_webserviceConfig;
|
|---|
| 454 | private $_dataDirectory;
|
|---|
| 455 | private $_databaseHandler;
|
|---|
| 456 | private $_databaseFile;
|
|---|
| 457 | private $_databaseFirstConnection;
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | ?>
|
|---|