-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_datasource.php
232 lines (207 loc) · 9.71 KB
/
class_datasource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?
/* ------------------------------------------------------------------------------------------
PHP Class "datasource"
Purpose: Provide simple functionality for gathering & storing data about service availability
That is, the class provides a simple interface for handling URL-based availability checks.
It wraps some PHP CURL functionalities & combines it with a SQL table for storing status data.
// --------------------------------------------------------------------------------------- */
// ------------------------------------------------------------------------------------------
// Dependencies: The class requires a configuration file
// ------------------------------------------------------------------------------------------
require_once 'config_database_server.php'; // File contains information about the database
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Class definition: Variables, constructor and methods
// ------------------------------------------------------------------------------------------
final class datasource
{
// ----------------------------------------------------------------------------------------
// Variables needed for constructing datasource objects
// ----------------------------------------------------------------------------------------
private $id; // String: Datasource ID used for identifying it in the database
private $name; // String: Name of datasource
private $base_url; // String: URL of datasource
private $data_format; // String: Data format (e.g. XML, HTML, ...)
private $license; // String: Type of the license applied by the datasource
private $owner; // String: Owner of the datasource
private $available_status; // Integer: Availability of datasource (can be 1 or 0)
private $available_datetime; // DateTime: Point in time of availbility
private $status; // Integer: Status code of the data record (and not the URL)
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Constructor method
// ----------------------------------------------------------------------------------------
public function __construct($data)
{
$this->id = $data['id'];
$this->name = $data['name'];
$this->base_url = $data['base_url'];
$this->data_format = $data['data_format'];
$this->license = $data['license'];
$this->owner = $data['owner'];
$this->available_status = $data['available_status'];
$this->available_datetime = $data['available_datetime'];
$this->source_status = $data['status'];
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get ID from respective datasource object
// ----------------------------------------------------------------------------------------
public function getId()
{
return $this->id;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get name from respective datasource object
// ----------------------------------------------------------------------------------------
public function getName()
{
return $this->name;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get URL from respective datasource object
// ----------------------------------------------------------------------------------------
public function getBaseUrl()
{
return $this->base_url;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get data format from respective datasource object
// ----------------------------------------------------------------------------------------
public function getDataFormat()
{
return $this->data_format;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get license from respective datasource object
// ----------------------------------------------------------------------------------------
public function getLicense()
{
return $this->license;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get owner from respective datasource object
// ----------------------------------------------------------------------------------------
public function getOwner()
{
return $this->owner;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get availability status from respective datasource object
// ----------------------------------------------------------------------------------------
public function getAvailableStatus()
{
return $this->available_status;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get availability timestamp from respective datasource object
// ----------------------------------------------------------------------------------------
public function getAvailableDateTime()
{
return $this->available_datetime;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Get status from respective datasource object
// ----------------------------------------------------------------------------------------
public function getStatus()
{
return $this->source_status;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Check, if a given URL responds to a call, and return the result
// ----------------------------------------------------------------------------------------
public function checkAvailabilityOfUrl($url)
{
// Check, if a valid url is provided via the method parameter
if(!filter_var($url, FILTER_VALIDATE_URL))
{
return false;
}
// Initialize CURL based on the URL provided
$curl_init = curl_init($url);
curl_setopt($curl_init,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl_init,CURLOPT_HEADER,true);
curl_setopt($curl_init,CURLOPT_NOBODY,true);
curl_setopt($curl_init,CURLOPT_RETURNTRANSFER,true);
// Call URL and save the response
$response = curl_exec($curl_init);
// Close CURL connection
curl_close($curl_init);
// Return result regarding availability
if ($response)
{
return true;
}
else
{
return false;
}
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// Write availability status to database
// ----------------------------------------------------------------------------------------
public function updateAvailabilityInDatabase ($status)
{
if (isset($status) AND !empty($status) AND is_bool($status))
{
try
{
global $database; // variable is declared in config file imported above
// Turn boolean value of $status into numeric/ integer value
$status_for_db = "";
if ($status === true)
{
$status_for_db = 1;
}
elseif ($status === false)
{
$status_for_db = 0;
}
// Create new PDO instance that represents a database connection
$db_internal_handle = "db_number_one";
$pdo = new PDO($database[$db_internal_handle]['server'],
$database[$db_internal_handle]['username'],
$database[$db_internal_handle]['password']);
// Build SQL command for updating status information
$tablename = "datasource";
$current_datetime = date_format(date_create(), 'Y-m-d H:i:s');
$sql = "
UPDATE
".$tablename."
SET
".$tablename.".available_status = '".$status_for_db."',
".$tablename.".available_datetime = '".$current_datetime."',
".$tablename.".update_datetime = '".$current_datetime."'
WHERE
".$tablename.".id = '".$this->getId()."'
LIMIT 1
";
// Execute SQL command and close connection to database
$pdo->exec($sql);
$pdo = null;
return true;
}
catch (PDOException $e)
{
die();
}
}
else
{
return null;
}
}
// ----------------------------------------------------------------------------------------
}
// ------------------------------------------------------------------------------------------
?>