forked from pierreboudes/botjobs
/
utils.php
204 lines (188 loc) · 5.21 KB
/
utils.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
<?php
/* Pain - outil de gestion des services d'enseignement
*
* Copyright 2009-2012 Pierre Boudes,
* département d'informatique de l'institut Galilée.
*
* This file is part of Pain.
*
* Pain is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pain is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pain. If not, see <http://www.gnu.org/licenses/>.
*/
function microsecoffset() {
static $us = 0;
if (!$us) $us = microtime(true);
return (microtime(true) - $us);
}
microsecoffset(); /* armee */
/** recupere une chaine passee en HTTP/GET ou POST
*/
function getclean($s) {
global $link;
if (isset($_GET[$s])) {
$source = $_GET[$s];
} else if (isset($_POST[$s])) {
$source = $_POST[$s];
} else {
return NULL;
}
if(get_magic_quotes_gpc()) {
return trim(htmlspecialchars($link->real_escape_string(stripslashes($source)), ENT_QUOTES));
}
else {
return trim(htmlspecialchars($link->real_escape_string($source), ENT_QUOTES));
}
}
function postclean($s) {/* obsolete remplacer par getclean */
return getclean($s);
}
/** recupere un nombre passee en HTTP/GET ou POST (ou une liste de nombres separes par des X)
*/
function getnumeric($s) {
global $link;
if (isset($_GET[$s])) {
$source = $_GET[$s];
} else if (isset($_POST[$s])) {
$source = $_POST[$s];
} else {
return NULL;
}
$a = explode('X',$source);
foreach ($a as $id) {
if (!is_numeric($id)) return NULL;
}
return $source;
}
/** recupere une liste de nombres csv ou tableau passee en HTTP/GET ou POST et la retourne en csv.
*/
function getlistnumeric($s) {
global $link;
if (isset($_GET[$s])) {
$source = $_GET[$s];
} else if (isset($_POST[$s])) {
$source = $_POST[$s];
} else {
return NULL;
}
if (!is_array($source)) {
$a = explode(',',$source);
} else {
$a = $source;
}
foreach ($a as $id) {
if (!is_numeric(trim($id))) return NULL;
}
return implode(',',$a);
}
/** recupere un tableau de tableau associatif passé par HTTP/GET ou POST en json
et échappe les chaînes pour php, mysql et html.
*/
function getjsonaofaa($s) {
global $link;
if (isset($_GET[$s])) {
$source = $_GET[$s];
} else if (isset($_POST[$s])) {
$source = $_POST[$s];
} else {
return NULL;
}
$o = json_decode($source, true); /* o tableau de tableaux associatifs */
foreach ($o as $i => $ligne) {
foreach ($ligne as $name => $cell) {
if (!is_numeric(trim($cell))) {
if (!is_string($cell)) {
$o[$i][$name] = "DEFAULT";
} else {
$o[$i][$name] = "'".trim(htmlspecialchars($link->real_escape_string(stripslashes($cell)), ENT_QUOTES))."'";
}
}
}
}
return $o;
}
function cookieclean($s) {
global $link;
if (isset($_COOKIE[$s])) {
if(get_magic_quotes_gpc()) {
return trim(htmlspecialchars($link->real_escape_string(stripslashes(($_COOKIE[$s]))), ENT_QUOTES));
}
else {
return trim(htmlspecialchars($link->real_escape_string($_COOKIE[$s]), ENT_QUOTES));
}
}
else return NULL;
}
function ip_client() {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
$IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
elseif(isset($_SERVER['HTTP_CLIENT_IP']))
$IP = $_SERVER['HTTP_CLIENT_IP'];
else
$IP = $_SERVER['REMOTE_ADDR'];
return $IP;
}
function postnumclean($s) {
return str_replace(',','.',str_replace(' ', '',postclean($s)));
}
function rob_log($message, $logname='rob') {
global $user;
/* vu qu'on écrit un fichier on ne veut pas de process appelant qui boucle... */
static $compteur = 0;
if ($compteur++ > 10) return;
// $pid = '(pid '.@getmypid().')';
$message = preg_replace("/\n+/", " ", $message);
// $message = preg_replace("/\n*$/", "\n", $message);
$message .= ' -- '.date("M d H:i:s").' '.$user['login']. "\n";
$logfile = dirname($_SERVER['SCRIPT_FILENAME'])."/roblogs/".$logname .'.log';
/* echo $logfile;
if (@is_readable($logfile)) echo "readable";
*/
if (@is_readable($logfile)
AND (!$s = @filesize($logfile) OR $s > 100*1024)) {
$rotate = true;
$message .= "-- [ rotate ]\n";
} else $rotate = '';
$f = @fopen($logfile, "ab");
if ($f) {
fputs($f, htmlspecialchars($message));
fclose($f);
}
if ($rotate) {
$nb = 50;
@unlink($logfile.".$nb");
for ($i = $nb; $i > 1; --$i) {
@rename($logfile.".".($i - 1),$logfile.".$i");
}
@rename($logfile,$logfile.'.1');
}
}
function ig_statsmysql() {
global $link;
echo '<div><pre>';
$status = implode("\n",explode(' ', $link->stat()));
echo "MYSQL\n";
echo $status;
$us = round(microsecoffset() * 1000);
echo "\nPage servie en : ".$us."ms ";
echo '</pre></div>';
}
Class Error {
public $error = "Erreur";
}
function errmsg($s) {
$err = new Error;
$err->error = "Erreur: ".$s;
print json_encode($err);
die();
}
?>