source: src/webservice/htdocs/SiWeather/Webservice/Setup.php@ 8c356c1

Last change on this file since 8c356c1 was 8c356c1, checked in by Joachim Langenbach <joachim.langenbach@…>, 11 years ago
  • Finished setup
    • Added tests for setup
    • Added translation to setup
  • Property mode set to 100644
File size: 9.5 KB
RevLine 
[8c356c1]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
21namespace SiWeather\Webservice;
22
23/**
24 * @brief Configures the Webservice and creates all needed files and directories (e.g. Databases)
25 *
26 * @note Requires php-posix!
27 */
28class Setup {
29
30 public function __construct(){
31 $this->_webserviceConfig = new WebserviceConfig();
32 $this->_dataDirectory = $this->_webserviceConfig->getConfig("dataDirectory");
33 $this->_databaseHandler = new DatabaseHandler();
34 $this->_databaseFile = $this->_databaseHandler->databaseFile();
35 $this->_databaseDirectory = pathinfo($this->_databaseFile, PATHINFO_DIRNAME);
36 }
37
38 /**
39 * @brief Checks if data directory is present and writable and returns a value from DatabaesChecks
40 *
41 * If an error occurs, the error message is stored into @p $errorMessage.
42 *
43 * @see DataDirectoryChecks
44 * @see dataDirectoryCreate()
45 */
46 public function dataDirectoryCheck(&$errorMessage){
47 if(!is_object($this->_webserviceConfig)){
48 $errorMessage = "Could not load the webservice configuration";
49 return DataDirectoryChecks::NoConfiguration;
50 }
51 if(!$this->_dataDirectory || empty($this->_dataDirectory)){
52 $errorMessage = "Could not determine dataDirectory setting";
53 return DataDirectoryChecks::NoDirectoryPath;
54 }
55 if(!is_dir($this->_dataDirectory)){
56 $errorMessage = "Data directory does not exists";
57 return DataDirectoryChecks::NotExist;
58 }
59 if(!is_writable($this->_dataDirectory)){
60 $errorMessage = "Data directory is not writable for ". $this->getProcessUser() .":". $this->getProcessGroup() ." (". $this->_dataDirectory .")";
61 return DataDirectoryChecks::NotWritable;
62 }
63 return DataDirectoryChecks::Success;
64 }
65
66 /**
67 * @brief Creates the data directory and returns a value from DatabaesChecks
68 *
69 * If an error occurs, the error message is stored into @p $errorMessage.
70 *
71 * @see DataDirectoryChecks
72 * @see dataDirectoryCreate()
73 */
74 public function dataDirectoryCreate(&$errorMessage){
75 $result = $this->dataDirectoryCheck($errorMessage);
76 if($result == DataDirectoryChecks::Success){
77 return DataDirectoryChecks::Success;
78 }
79 if($result == DataDirectoryChecks::NoConfiguration || $result == DataDirectoryChecks::NoDirectoryPath || $result == DataDirectoryChecks::DirectoryNotWritable){
80 return $result;
81 }
82 if(!mkdir($this->_dataDirectory)){
83 $errorMessage = "Could not create data directory (". $this->_dataDirectory .")";
84 return DataDirectoryChecks::Error;
85 }
86 return DataDirectoryChecks::Success;
87 }
88
89 /**
90 * @brief Checks everything related to the database path and returns a value from DatabaesChecks
91 *
92 * If an error occurs, the error message is stored into @p $errorMessage.
93 *
94 * @see DatabaseDirectoryChecks
95 * @see databaseDirectoryCreate()
96 */
97 public function databaseDirectoryCheck(&$errorMessage){
98 if(is_null($this->_databaseDirectory) || empty($this->_databaseDirectory)){
99 $errorMessage = "Failed to extract directory from database file path ("+ $this->_databaseFile +")";
100 return DatabaseChecks::NoDirectoryPath;
101 }
102 if(!is_dir($this->_databaseDirectory)){
103 $errorMessage = "Database directory does not exists (". $this->_databaseDirectory .")";
104 return DatabaseChecks::DirectoryDoesNotExist;
105 }
106 if(!is_writable($this->_databaseDirectory)){
107 $errorMessage = "Database directory is not writable for ". $this->getProcessUser() .":". $this->getProcessGroup() ." (". $this->_databaseDirectory .")";
108 return DatabaseChecks::DirectoryNotWritable;
109 }
110 return DatabaseChecks::Success;
111 }
112
113 /**
114 * @brief Creates the database directory and returns a value from DatabaesChecks
115 *
116 * If an error occurs, the error message is stored into @p $errorMessage.
117 *
118 * @see DatabaseDirectoryChecks
119 * @see databaseDirectoryCheck()
120 */
121 public function databaseDirectoryCreate(&$errorMessage){
122 $result = $this->databaseDirectoryChecks($errorMessage);
123 if($result == DatabaseChecks::Success){
124 return DatabaseChecks::Success;
125 }
126 if($result == DatabaseChecks::NoDatabaseFile || $result == DatabaseChecks::NoDirectoryPath || $result == DatabaseChecks::DirectoryNotWritable){
127 return $result;
128 }
129 if(!mkdir($this->_databaseDirectory)){
130 $errorMessage = "Could not create database directory (". $this->_databaseDirectory .")";
131 return DatabaseChecks::Error;
132 }
133 return DatabaseChecks::Success;
134 }
135
136 /**
137 * @brief Checks, if the database file is given and is in a writable path
138 *
139 * The database file itself is created by the DatabaseHandler. Therefore
140 * this function only checks, if the database directory exists, and it must be
141 * writable.
142 *
143 * @see DatabaseChecks
144 * @see databaseDirectoryCheck()
145 */
146 public function databaseFileCheck(&$errorMessage){
147 if(is_null($this->_databaseFile) || empty($this->_databaseFile)){
148 $errorMessage = "No database file given (". $this->_databaseHandler->lastErrorMessage() .")";
149 return DatabaseChecks::NoDatabaseFile;
150 }
151 if(file_exists($this->_databaseFile)){
152 if(!is_writable($this->_databaseFile)){
153 $errorMessage = "Database file is not writable for ". $this->getProcessUser() .":". $this->getProcessGroup() ." (". $this->_databaseFile .")";
154 return DatabaseChecks::DatabaseFileNotWritable;
155 }
156 return DatabaseChecks::Success;
157 }
158 // if the databaseDirectoryCheck function return something other than success,
159 // we probably have a problem (like no write permission, ...)
160 if($this->databaseDirectoryCheck($errorMessage) != DatabaseChecks::Success){
161 return DatabaseChecks::Error;
162 }
163 return DatabaseChecks::Success;
164 }
165
166 public function databaseConnection(&$errorMessage){
167 if(!is_object($this->_databaseHandler)){
168 $errorMessage = "No database handler";
169 return false;
170 }
171 if(!$this->_databaseHandler->connect()){
172 $errorMessage = "Could not connect to the database (". $db->lastErrorMessage() .")";
173 return false;
174 }
175
176 // prepare Spatalite
177 if(!$this->_databaseHandler->exec("BEGIN")){
178 $errorMessage = "Could not start transaction to init Spatialite";
179 return false;
180 }
181 if(!$this->_databaseHandler->exec("SELECT InitSpatialMetadata()")){
182 $errorMessage = "Could not initiate Spatialite metadata";
183 return false;
184 }
185 if(!$this->_databaseHandler->exec("COMMIT")){
186 $errorMessage = "Could not commit transaction to init Spatialite";
187 return false;
188 }
189
190 return true;
191 }
192
193 public function databaseVersionSqlite(&$errorMessage){
194 if(!is_object($this->_databaseHandler)){
195 return 0;
196 }
197 $result = $this->_databaseHandler->versionSqlite();
198 if($result == 0){
199 $errorMessage = $this->_databaseHandler->lastErrorMessage();
200 }
201 return $result;
202 }
203
204 public function databaseVersionSpatalite(&$errorMessage){
205 if(!is_object($this->_databaseHandler)){
206 return 0;
207 }
208 $result = $this->_databaseHandler->versionSpatialite();
209 if($result == 0){
210 $errorMessage = $this->_databaseHandler->lastErrorMessage();
211 }
212 return $result;
213 }
214
215 public function apiCheckWithoutRewrite(&$errorMessage){
216 return $this->apiCheck("index.php/stations", $errorMessage);
217 }
218
219 public function apiCheckWithRewrite(&$errorMessage){
220 $apiVersion = $this->_webserviceConfig->getConfig("webserviceApiVersion");
221 if(!$apiVersion || empty($apiVersion)){
222 $errorMessage = "Could not determine API version";
223 return false;
224 }
225
226 return $this->apiCheck($apiVersion ."/stations", $errorMessage);
227 }
228
229 private function getProcessUser(){
230 $processUser = posix_getpwuid(posix_getuid());
231 return $processUser["name"];
232 }
233
234 private function getProcessGroup(){
235 $processGroup = posix_getgrgid(posix_getgid());
236 return $processGroup["name"];
237 }
238
239 private function apiCheck($url, &$errorMessage){
240 $baseUrl = $this->_webserviceConfig->getConfig("webserviceBaseUrl");
241 if(!$baseUrl || empty($baseUrl)){
242 $errorMessage = "No webservice base url given!";
243 return false;
244 }
245
246 $client = new \GuzzleHttp\Client
247 (
248 [
249 "base_uri" => $baseUrl,
250 "timeout" => 2.0,
251 "http_errors" => false,
252 ]
253 );
254 $response = $client->get($url);
255 if(is_null($response)){
256 $errorMessage = "Webservice has not answered at url ". $baseUrl ."!";
257 return false;
258 }
259 $result = $response->getStatusCode();
260 if($result != 200){
261 $errorMessage = "Got http error ". $result ." (". $response->getReasonPhrase() .")";
262 return false;
263 }
264 $result = $response->getReasonPhrase();
265 if($result != "OK"){
266 $errorMessage = "Got a http error reason with http code 200: ". $result;
267 return false;
268 }
269
270 $body = $response->getBody();
271 if(empty($body)){
272 $errorMessage = "Got empty return value";
273 return false;
274 }
275
276 $json = json_decode($body);
277 if(is_null($json)){
278 $errorMessage = "Could not decode json message body";
279 return false;
280 }
281 return true;
282 }
283
284 private $_webserviceConfig;
285 private $_dataDirectory;
286 private $_databaseHandler;
287 private $_databaseFile;
288}
289
290?>
Note: See TracBrowser for help on using the repository browser.