/
common.php
426 lines (382 loc) · 12.1 KB
/
common.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?php
class Report {
private $ID;
private $author;
private $post;
private $report;
function __construct($author,$post,$report){
$this->author = $author;
$this->post = $post;
$this->report = $report;
}
function getAuthor() {
return $this->author;
}
function getPost() {
return $this->post;
}
function getReport() {
return $this->report;
}
function getID() {
return $this->ID;
}
function setID($id) {
$this->ID = $id;
return $this;
}
/**
* Salva il report nel database.
*
* se INSERT: crea una nuova tupla in Report.
* se UPDATE: non fa nulla. Non si può modificare un report.
*/
function save() {
require_once("query.php");
$db = new DBManager();
if(!$db->connect_errno()) {
$table = Query::getDBSchema()->getTable(TABLE_REPORT);
$data = array(REPORT_TEXT => $this->getReport(),
REPORT_POST => $this->getPost(),
REPORT_USER => $this->getAuthor());
$rs = $db->execute($s = Query::generateInsertStm($table,$data), $table->getName(), $this);
//echo "<br />" . $s; //DEBUG
//echo "<br />" . serialize($rs); //DEBUG
if($db->affected_rows() == 1) {
$this->ID = $db->last_inserted_id();
//echo "<br />" . $this; //DEBUG
return $this->getID();
} else $db->display_error("Report::save()");
} else $db->display_connect_error("Report::save()");
return false;
}
function delete() {
require_once("query.php");
$db = new DBManager();
if(!$db->connect_errno()) {
$table = Query::getDBSchema()->getTable(TABLE_REPORT);
$rs = $db->execute($s = Query::generateDeleteStm($table,
array(new WhereConstraint($table->getColumn(REPORT_ID),Operator::EQUAL,$this->getID()))),
$table->getName(), $this);
//echo "<br />" . $s; //DEBUG
if($db->affected_rows() == 1) {
return $this;
} else $db->display_error("Report::delete()");
} else $db->display_connect_error("Report::delete()");
return false;
}
/**
* @Override
*/
function __toString() {
$s = "Report (author = " . $this->getAuthor() .
" | post = " . $this->getPost() .
" | report = " . $this->getReport() .
")";
return $s;
}
}
class Resource {
private $ID;
private $owner;
private $path;
private $type;
private $description;
static $VIDEO = "video";
static $PHOTO = "photo";
function __construct($owner,$path,$type){
$this->owner = $owner;
$this->path = $path;
$this->type = $type;
}
function getDescription(){
return $this->description;
}
function getOwner() {
return $this->owner;
}
function getPath() {
return $this->path;
}
function getType() {
return $this->type;
}
function getID() {
return $this->ID;
}
function setDescritpion($desc){
$this->description = $desc;
return $this;
}
function setID($id) {
$this->ID = $id;
return $this;
}
/**
* Salva il report nel database.
*
* se INSERT: crea una nuova tupla in Report.
* se UPDATE: non fa nulla. Non si può modificare un report.
*/
function save() {
require_once("query.php");
$db = new DBManager();
if(!$db->connect_errno()) {
$table = Query::getDBSchema()->getTable(TABLE_RESOURCE);
$data = array(RESOURCE_OWNER => $this->getOwner(),
RESOURCE_PATH => $this->getPath(),
RESOURCE_TYPE => $this->getType());
$rs = $db->execute($s = Query::generateInsertStm($table,$data), $table->getName(), $this);
//echo "<br />" . $s; //DEBUG
//echo "<br />" . serialize($rs); //DEBUG
if($db->affected_rows() == 1) {
$this->setID($db->last_inserted_id());
//echo "<br />" . $this; //DEBUG
return $this->getID();
} else $db->display_error("Resource::save()");
} else $db->display_connect_error("Resource::save()");
return false;
}
function delete() {
require_once("query.php");
$db = new DBManager();
if(!$db->connect_errno()) {
$table = Query::getDBSchema()->getTable(TABLE_RESOURCE);
$rs = $db->execute($s = Query::generateDeleteStm($table,
array(new WhereConstraint($table->getColumn(RESOURCE_ID),Operator::EQUAL,$this->getID()))),
$table->getName(), $this);
//echo "<br />" . $s; //DEBUG
if($db->affected_rows() == 1) {
return $this;
} else $db->display_error("Resource::delete()");
} else $db->display_connect_error("Resource::delete()");
return false;
}
/**
* @Override
*/
function __toString() {
$s = "Resource (ID = " . $this->getID() .
" | owner = " . $this->getOwner() .
" | path = " . $this->getPath() .
" | type = " . $this->getType() .
")";
return $s;
}
}
class LogManager {
static $INSERT = "INSERT";
static $DELETE = "DELETE";
static $UPDATE = "UPDATE";
/**
* Recupera il contenuto del Log da $from a $to.
*
* @param $from: data TimeStamp da cui selezionare le entry del Log. Se 0 parte dall'inizio.
* @param $to: data TimeStamp in cui finire la selezione delle entry del Log. Se 0 arriva fino alla fine.
* @return: array contenente tutte le entry.
*/
static function getLog($from, $to) {
require_once("query.php");
$db = new DBManager();
if(!$db->connect_errno()) {
define_tables(); defineLogColumns();
$table = Query::getDBSchema()->getTable(TABLE_LOG);
$s = "";
if(is_numeric($from) && $from != 0) {
$s1 = Query::generateSelectStm(array($table), array(),
array(new WhereConstraint($table->getColumn(LOG_TIMESTAMP),Operator::GREATEROREQUAL,$from)),
array());
}
if(is_numeric($to) && $to != 0) {
$s2 = Query::generateSelectStm(array($table), array(),
array(new WhereConstraint($table->getColumn(LOG_TIMESTAMP),Operator::LESSEROREQUAL,$to)),
array("order" => 1, "by" => LOG_TIMESTAMP));
}
if(is_numeric($from) && $from != 0 && is_numeric($to) && $to != 0) {
$s = Query::generateComplexSelectStm(array($s1, $s2), array(SelectOperator::INTERSECT));
} else if(is_numeric($from) && $from != 0) {
$s = $s1;
} else if(is_numeric($to) && $to != 0) {
$s = $s2;
} else {
return array();
}
//echo "<br />" . $s; //DEBUG
$rs = $db->execute($s, $table->getName(), LOGMANAGER);
$log_result = array();
if($db->num_rows() > 0) {
while($row = mysql_fetch_row) {
$log_result[] = $row;
}
//echo "<br />" . serialize($log_result); //DEBUG
return $log_result;
} else $db->display_error("LogManager::getLog()");
} else $db->display_connect_error("LogManager::getLog()");
return array();
}
/**
* Aggiunge una entry al Log.
*
* @param $user: l'utente che ha fatto l'azione.
* @param $action: l'azione eseguita dall'utente, fa parte delle chiavi di LogManager::$actions.
* @param $object: l'oggetto che subisce l'azione (prima che venga eseguita).
*
* @return: l'id della entry inserita, false se non c'è riuscito.
*/
static function addLogEntry($user, $action, $tablename, $object) {
if($object == LOGMANAGER) return;
//echo $user. $action. $tablename . serialize(is_object($object)); //DEBUG
if(!isset($user) || !is_numeric($user) ||
!isset($action) || ($action != self::$DELETE && $action != self::$INSERT && $action != self::$UPDATE) ||
!isset($object) || is_null($object) || !is_object($object))
return false;
require_once("query.php");
$db = new DBManager();
if(!$db->connect_errno()) {
define_tables(); defineLogColumns();
$table = Query::getDBSchema()->getTable(TABLE_LOG);
//echo "<br />" . $tablename; //DEBUG
$sha1ed = sha1(serialize($object));
$id = sha1($sha1ed . $action . $table . $user . time());
$data = array(LOG_ID => $id,
LOG_ACTION => $action,
LOG_TABLE => $tablename,
LOG_SUBJECT => $user,
LOG_OBJECT => $sha1ed);
$s = Query::generateInsertStm($table, $data);
//echo "<br />" . $s; //DEBUG
$rs = mysql_query($s, $db->dblink); //devo fare così è non usare DBManager::execute() perché non avrei affected_rows.
if(mysql_affected_rows($db->dblink)) {
return $id;
} else $db->display_error("LogManager::addLogEntry()");
} else $db->display_connect_error("LogManager::addLogEntry()");
return false;
}
static function getAccessCount($type, $id) {
if($type == null || $type == "" || $id == null)
return 0;
require_once("query.php");
$db = new DBManager();
if(!$db->connect_errno()) {
define_tables(); defineLogColumns();
$table = Query::getDBSchema()->getTable("AccessLog");
$exists = false;
if($type == "Post") {
require_once 'post/PostManager.php';
return 0;
$exists = PostManager::postExists($id); //TODO
} else if ($type == "User") {
require_once 'user/UserManager.php';
return 0;
$exists = UserManager::userExists($id); //TODO
} elseif ($type == "Partner") {
//TODO: implementa Partner
// require_once 'post/PostManager.php';
// $exists = PostManager::postExists($id);
}
if($exists) {
$wheres = array(new WhereConstraint($table->getColumn("alog_type"), Operator::EQUAL, $type),
new WhereConstraint($table->getColumn("alog_id"), Operator::EQUAL, $id));
$db->execute($s = Query::generateSelectStm(array($table), array(), $wheres, array()));
if($db->num_rows() == 1) {
$row = $db->fetch_result();
$data = array("alog_count" => ++$row["alog_count"]);
$db->execute($s = Query::generateUpdateStm($table, $data, $wheres), null, LOGMANAGER);
if($db->affected_rows() == 1)
return $row["alog_count"];
} else {
$data = array("alog_type" => $type, "alog_id" => $id);
$db->execute($s = Query::generateInsertStm($table, $data));
if($db->affected_rows() == 1);
return 1;
}
}
return 0;
}
}
}
class Filter {
static function filterText($text) {
//return self::clean(htmlentities(addslashes(self::escape_utf8($text))));
return htmlspecialchars(htmlentities(self::clean($text)));
}
// TODO controlla il funzionamento
static function textToHyperlink($text) {
return preg_replace("#http://([A-z0-9./-]+)#", '<a href="$1">$0</a>', $text);
}
static function filterArray($array) {
$newarray = array();
foreach($array as $key => $value) {
if(is_string($value))
$value = self::filterText($value);
$newarray[$key] = $value;
}
return $newarray;
}
private static function escape_utf8($text) {
$text = str_replace("&", "&", $text);
$text = str_replace("à", "à", $text);
$text = str_replace("è", "è", $text);
$text = str_replace("é", "é", $text);
$text = str_replace("ì", "ì", $text);
$text = str_replace("ò", "ò", $text);
$text = str_replace("ù", "ù", $text);
return $text;
}
static function decodeFilteredText($text) {
//return html_entity_decode(stripslashes($text));
return stripslashes(html_entity_decode(htmlspecialchars_decode($text)));
}
static function decodeFilteredArray($array) {
$newarray = array();
foreach($array as $key => $value) {
if(is_string($value))
$value = self::decodeFilteredText($value);
$newarray[$key] = $value;
}
return $newarray;
}
static function textToPermalink($text) {
$permalink = iconv('UTF-8', 'ASCII//TRANSLIT', $text);
$permalink = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $permalink);
$permalink = strtolower(trim($permalink, '-'));
$permalink = preg_replace("/[\/_|+ -]+/", '_', $permalink);
return $permalink;
}
static function clean($value) {
// Stripslashes
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
// Quote if not a number or a numeric string
if (!is_numeric($value) && !empty($value)) {
require_once 'query.php';
$db = new DBManager();
$value = mysql_real_escape_string($value, $db->dblink);
}
return $value;
}
/**
* Converte un array di stringhe in una unica stringa dove le varie stringhe sono separate da virgole.
* @param array $array un array di stringhe.
* @param string $separator una stringa contenente un separatore. (Default ",")
* @return una stringa con tutti i dati dell'array.
*/
static function arrayToText($array, $separator = ", ") {
$s = "";
$first = true;
foreach ($array as $value) {
if($first) $first = false;
else $s.= $separator;
$s.= $value;
}
return $s;
}
static function hash($text) {
return sha1($text);
}
static function encodePassword($password) {
return self::hash($password); //si può decidere di cambiare questo encoding
}
}
?>