forked from oeuvres/teinte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web.php
373 lines (367 loc) · 14.6 KB
/
Web.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
<?php
/**
* © 2010 frederic.glorieux@fictif.org et École nationale des chartes
* © 2012 frederic.glorieux@fictif.org
* © 2014 frederic.glorieux@fictif.org et LABEX OBVIL
* LGPL http://www.gnu.org/licenses/lgpl.html
*
* This program is a free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License
* http://www.gnu.org/licenses/lgpl.html
* This program 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.
*
*/
/**
* Tools to deal with PHP Http oddities
*/
if ( get_magic_quotes_gpc() ) {
function stripslashes_gpc( &$value ) {
$value = stripslashes( $value );
}
array_walk_recursive( $_GET, 'stripslashes_gpc' );
array_walk_recursive( $_POST, 'stripslashes_gpc' );
array_walk_recursive( $_COOKIE, 'stripslashes_gpc' );
array_walk_recursive( $_REQUEST, 'stripslashes_gpc' );
}
class Teinte_Web {
/** web parameters */
static $pars;
/** pathinfo, relative to base application */
static $pathinfo;
/** relative path to base application, calculated with pathinfo */
static $basehref;
/** Content-Type header */
static $mime = array(
"css" => 'text/css; charset=UTF-8',
"epub" => 'application/epub+zip',
"html" => 'text/html; charset=UTF-8',
"iramuteq" => "text/plain; charset=UTF-8",
"jpg" => 'image/jpeg',
"js" => 'text/javascript; charset=UTF-8',
"md" => "text/plain; charset=UTF-8",
"markdown" => "text/plain; charset=UTF-8",
"naked" => "text/plain; charset=UTF-8",
"png" => 'image/png',
"xml" => 'text/xml',
"xhtml" => 'text/html; charset=UTF-8',
);
/** Langs */
static $langs=array(
"en" => "English",
"fr" => "Français",
);
/** lang found */
static $lang;
/**
* Give pathinfo with priority order of different values.
* The possible variables are not equally robust
*
* http://localhost/~user/teipot/doc/install&sons?a=1&a=2#ancre
*
* — $_SERVER['REQUEST_URI'] OK /~user/teipot/doc/install&sons?a=1&a=2
* — $_SERVER['SCRIPT_NAME'] OK /~user/teipot/index.php
* — $_SERVER['PHP_SELF'] /~user/teipot/index.php/doc/install&sons (not always given by mod_rewrite)
* — $_SERVER['PATH_INFO'] sometimes unavailable, ex: through mod_rewrite /doc/install&sons
* — $_SERVER['SCRIPT_URI'] sometimes, ex : http://teipot.x10.mx/install&bon
* — $_SERVER['PATH_ORIG_INFO'] found on the web
*
*/
public static function pathinfo()
{
if (self::$pathinfo) return self::$pathinfo;
$pathinfo = "";
if (!isset($_SERVER['REQUEST_URI'])) return $pathinfo; // command line
list($request) = explode('?', $_SERVER['REQUEST_URI']);
if(strpos($request, '%') !== false) $request = urldecode($request);
if (strpos($request, $_SERVER['SCRIPT_NAME']) === 0)
$pathinfo = substr($request, strlen($_SERVER['SCRIPT_NAME']));
else if (strpos($request, dirname($_SERVER['SCRIPT_NAME'])) === 0)
$pathinfo = substr($request, strlen(dirname($_SERVER['SCRIPT_NAME'])));
// if nothing found, try other variables
if ($pathinfo); // something found, keep it
else if (isset($_SERVER['PATH_ORIG_INFO'])) $pathinfo = $_SERVER['PATH_ORIG_INFO'];
else if (isset($_SERVER['PATH_INFO'])) $pathinfo = $_SERVER['PATH_INFO'];
else if (isset($_REQUEST['id'])) $pathinfo = $_REQUEST['id'];
// should I trim last / ?
$pathinfo = ltrim($pathinfo, '/');
$pathinfo = preg_replace('@/+@', '/', $pathinfo);
// html injection ?
$pathinfo = strip_tags($pathinfo);
self::$pathinfo = $pathinfo;
return self::$pathinfo;
}
/**
* Relative path to context
*/
public static function basehref( $path=null )
{
if ($path) { // return a result, no store
$path = preg_replace('@/+@', '/', ltrim($path, '/'));
$path = str_repeat("../", substr_count($path, '/'));
if (!$path) $path="./"; // with /toto, go up with ./
return $path;
}
if (isset(self::$basehref)) return self::$basehref;
$pathinfo = self::pathinfo();
self::$basehref = str_repeat("../", substr_count($pathinfo, '/'));
if (!self::$basehref) self::$basehref="./"; // with /toto, go up with ./
return self::$basehref;
}
/**
* Handle repeated parameters values, especially in multiple select.
* $_REQUEST propose a strange PHP centric interpretation of http protocol, with the bracket keys
* <select name="var[]">
*
* $query : optional, a "query string" ?cl%C3%A9=%C3%A9%C3%A9¶m=valeur1¶m=¶m=valeur2
* return : Array (
* "clé" => array("éé"),
* "param" => array("valeur1", "", "valeur2")
* )
*/
public static function pars( $name = FALSE, $expire = 0, $pattern = null, $default = null, $query = FALSE )
{
// store params array extracted from query
if (!self::$pars) {
if (!$query) $query = Teinte_Web::query();
// populate an array
self::$pars = array();
$a = explode('&', $query);
foreach ($a as $p) {
if (!$p) continue;
if (!strpos($p,'=')) continue;
list($k, $v) = preg_split('/=/', $p);
$k = urldecode($k);
$v = urldecode($v);
// seems ISO, translate accents
if (preg_match('/[\xC0-\xFD]/', $k+$v)) {
$k = utf8_encode ($k);
$v = utf8_encode ($v);
}
self::$pars[$k][] = $v;
}
}
// no key requested, return all params, do not store cookies
if (!$name) return self::$pars;
// a param is requested, values found
else if (isset(self::$pars[$name])) $pars = self::$pars[$name];
// no param for this name
else $pars = array();
// no cookie store requested
if(!$expire);
// if empty ?, delete cookie
else if (count($pars)==1 && !$pars[0]) {
setcookie($name);
}
// if a value, set cookie, do not $_COOKIE[$name] = $value
else if (count($pars)) {
// if a number
if ($expire > 60) setcookie($name, serialize($pars), time()+ $expire);
// session time
else setcookie($name, serialize($pars));
}
// if cookie stored, load it
else if(isset($_COOKIE[$name])) $pars = unserialize($_COOKIE[$name]);
// validate
if ($pattern) {
$newPars = array();
foreach($pars as $value) if (preg_match($pattern, $value)) $newPars[] = $value;
$pars = $newPars;
}
// default
if (count($pars));
else if (!$default);
else if (is_array($default)) $pars = $default;
else $pars = array($default);
return $pars;
}
/**
* Search for a lang in an accpted list
*/
public static function lang( $langs=null )
{
if (!$langs || !is_array($langs) || !count($langs)) $langs = self::$langs;
// check browser request
$lang=false;
// http param, set a lang
if (isset($_GET['lang'])) {
// empty value, reset cookie
if(!$_GET['lang']) setcookie("lang", "", time() - 3600);
// lang not available, do nothing
else if (!isset($langs[$_GET['lang']]));
// language requested should be available
else {
$lang=$_GET['lang'];
setcookie ( "lang", $lang);
}
}
// coookie persistancy
if (!$lang && isset($_COOKIE['lang'])) {
// language in cookie is not available, maybe setted from elsewhere in the site, do nothing
if(!isset($langs[$_COOKIE['lang']]));
else $lang=$_COOKIE['lang'];
}
// browser request
if(!$lang) {
$http_accept_language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
preg_match_all("/(\w\w)(-\w+)*/", $http_accept_language, $matches);
// array_values() reindex the keys starting at 0
$accepted=array_values(array_intersect(array_keys(array_flip($matches[1])), array_keys($langs)));
if(isset($accepted[0])) $lang=$accepted[0];
}
// no lang found, take the first lang available
if(!$lang) {
reset($langs);
$lang=key($langs);
}
self::$lang = $lang;
return self::$lang;
}
/**
* build a clean query string from get or post, especially
* to get multiple params from select
*
* query: ?A=1&A=2&A=&B=3
* return: ?A=1&A=2&B=3
* $keep=true : keep empty params -> ?A=1&A=2&A=&B=3
* $exclude=array() : exclude some parameters
*/
public static function query( $keep = false, $exclude = array(), $query = null )
{
// query given as param
if ($query) $query = preg_replace( '/&/', '&', $p1);
// POST
else if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($HTTP_RAW_POST_DATA)) $query = $HTTP_RAW_POST_DATA;
else $query = file_get_contents("php://input");
}
// GET
else $query = $_SERVER['QUERY_STRING'];
// exclude some params
if (count($exclude)) $query = preg_replace( '/&('.implode('|',$exclude).')=[^&]*/', '', '&'.$query);
// delete empty params
if (!$keep) $query = preg_replace( array('/[^&=]+=&/', '/&$/'), array('', ''), $query.'&');
return $query;
}
/**
* Send the best headers for cache, according to the request and a timestamp
*/
public static function notModified( $file, $expires = null, $force = false )
{
if (!$file) return false;
$filemtime = false;
// seems already a filemtime
if (is_int($file)) $filemtime = $file;
// if array of file, get the newest
else if (is_array($file)) foreach($file as $f) {
// if not file exists, no error
if (!file_exists($f)) continue;
$i = filemtime($f);
if ($i && $i > $filemtime) $filemtime = $i;
}
else $filemtime = filemtime($file);
if(!$filemtime) return $filemtime;
// Default expires
if (filemtime($_SERVER['SCRIPT_FILENAME']) > $filemtime) {
$filemtime = filemtime($_SERVER['SCRIPT_FILENAME']);
}
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :false;
// $if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']) : false; // etag
$modification = gmdate('D, d M Y H:i:s', $filemtime).' GMT';
// tests for 304
if($force);
else if (self::noCache());
// ($if_none_match && $if_none_match == $etag) ||
else if ( $if_modified_since == $modification) {
header('HTTP/1.x 304 Not Modified');
exit;
}
// header("X-Date: ". substr(gmdate('r'), 0, -5).'GMT');
/*
// According to google, https://developers.google.com/speed/docs/best-practices/caching
// exclude etag if last-Modified, and last-Modified is better
$etag = '"'.md5($modification).'"';
header("ETag: $etag");
*/
// it seems there is something to send
header("Cache control: public"); // for FireFox over https
header("Last-Modified: $modification");
// it's good to
if ($expires) header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
}
/**
* If client ask a forced reload.
*/
public static function noCache()
{
// pas de cache en POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') return 'POST';
if (isset ($_SERVER['HTTP_PRAGMA']) && stripos($_SERVER['HTTP_PRAGMA'], "no-cache") !== false) return "Pragma: no-cache";
if (isset ($_SERVER['HTTP_CACHE_CONTROL']) && stripos($_SERVER['HTTP_CACHE_CONTROL'], "no-cache") !== false) return "Cache-Control: no-cache";
if (isset($_REQUEST['no-cache'])) return '?no-cache=';
if (isset($_REQUEST['force'])) return '?force=';
return false;
}
/**
* Get link to un upload file, by key or first one if no key
* return a file record like ine $_FILES
* http://php.net/manual/features.file-upload.post-method.php
*/
public static function upload( $key=null )
{
// no post, return nothing
if ( $_SERVER['REQUEST_METHOD'] != 'POST' ) return false;
$lang = self::lang(array('en'=>'', 'fr'=>''));
$mess = array(
UPLOAD_ERR_INI_SIZE => array(
'en' => 'The uploaded file exceeds a directive in php.ini; upload_max_filesize='.ini_get('upload_max_filesize').', post_max_size='.ini_get('post_max_size'),
"fr" => 'Le fichier téléchargé dépasse la limite acceptée par la configuration du serveur (php.ini) ; upload_max_filesize='.ini_get('upload_max_filesize').', post_max_size='.ini_get('post_max_size'),
),
UPLOAD_ERR_FORM_SIZE => array(
'en' => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
'fr' => 'Le fichier téléchargé dépasse la directive MAX_FILE_SIZE spécifiée dans le formulaire.',
),
UPLOAD_ERR_PARTIAL => array(
'en' => 'The uploaded file was only partially uploaded. ',
'fr' => 'Le fichier téléchargé est incomplet',
),
UPLOAD_ERR_NO_FILE => array(
'en' => 'No file was uploaded.',
'fr' => 'Pas de fichier téléchargé.',
),
UPLOAD_ERR_NO_TMP_DIR => array(
'en' => 'Server configuration error, missing a temporary folder.',
'fr' => 'Erreur de configuration serveur, pas de dossier temporaire.',
),
UPLOAD_ERR_CANT_WRITE => array(
'en' => 'Server system error, failed to write file to disk.',
'fr' => 'Erreur système sur le serveur, impossible d’écrire le fichier sur le disque.',
),
UPLOAD_ERR_EXTENSION => array(
'en' => 'PHP server problem, a PHP extension stopped the file upload.',
'fr' => 'Erreur de configuration PHP, une extension a arrêté le téléchargement du fichier.',
),
'nokey' => array(
'en' => "Teinte_Web::upload(), no field $key in submitted form.",
'fr' => "Teinte_Web::upload(), pas de champ $key dans le formulaire soumis.",
),
'nofile' => array(
'en' => 'Teinte_Web::upload(), no file found. Too big ? Directives in php.ini: upload_max_filesize='.ini_get('upload_max_filesize').', post_max_size='.ini_get('post_max_size'),
'fr' => 'Teinte_Web::upload(), pas de fichier trouvé. Trop gros ? Directives php.ini: upload_max_filesize='.ini_get('upload_max_filesize').', post_max_size='.ini_get('post_max_size'),
),
);
if ($key && !isset($_FILES[$key])) throw new Exception($mess['nokey'][$lang]);
if ($key) $file = $_FILES[$key];
else $file = reset($_FILES);
if (!$file || !is_array($file) || !isset($file['error'])) throw new Exception($mess['nofile'][$lang]);
// validation, no matter for an exception
if ($file['error'] == UPLOAD_ERR_NO_FILE) return false;
if ($file['error']) throw new Exception($mess[$file['error']][$lang]);
// return the array to have the tmp link, and the original name of the file, and some more useful fields
$file["filename"] = pathinfo($file['name'], PATHINFO_FILENAME);
$file["extension"] = pathinfo($file['name'], PATHINFO_EXTENSION);
return $file;
}
}
?>