-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.php
363 lines (325 loc) · 13.8 KB
/
app.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
<?php
namespace Tools;
header('Content-Type: text/html;charset=UTF-8');
@session_start();
require_once "config.php";
require_once "route.php";
//error_reporting(0);
$subdomaine = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], "/"));
define('SUBDOMAINE', $subdomaine);
$currenturl=$_SERVER['REQUEST_URI'];
if(strpos( $currenturl, SUBDOMAINE."/")!==NULL) $currenturl=substr( $currenturl,strlen(SUBDOMAINE));
if(strrpos( $currenturl, "?")) $currenturl=substr( $currenturl, 0,strrpos( $currenturl, "?")-strlen( $currenturl));
if(strrpos( $currenturl, "/")==strlen( $currenturl)-1) $currenturl=substr( $currenturl,0,strlen( $currenturl)-1);
define('currenturl',$currenturl);
function check($path=""){
return (currenturl===$path);
}
function has($path=""){
return str_starts_with(currenturl, $path);
}
class DB{
var $defaultDebug = false;
var $mtStart;
var $nbQueries;
var $lastResult;
var $link;
function __construct($server=DBHOST, $user=DBUSER, $pass=DBPWD, $base=DBNAME, $error=1){
$this->mtStart = $this->getMicroTime();
$this->nbQueries = 0;
$this->lastResult = NULL;
if($error){
$this->link = @mysqli_connect($server, $user, $pass) or die("error connect BDD");
@mysqli_select_db($this->link, $base) or die("error connect datatable");
}else{
$this->link = @mysqli_connect($server, $user, $pass) or function(){return false;};
@mysqli_select_db($this->link, $base) or function(){return false;};
}
}
function query($query, $debug = -1,$error=0){
$this->nbQueries++;
$this->lastResult = mysqli_query($this->link, $query) or ($this->debugAndDie($query,$error));
$this->debug($debug, $query, $this->lastResult);
return $this->lastResult;
}
function execute($query, $debug = -1,$error=0){
$this->nbQueries++;
mysqli_query($this->link, $query) or ($this->debugAndDie($query,$error));
$this->debug($debug, $query);
}
function fetchNextObject($result = NULL){
if ($result == NULL)
$result = $this->lastResult;
if ($result == NULL || mysqli_num_rows($result) < 1)
return NULL;
else
return mysqli_fetch_object($result);
}
function numRows($result = NULL){
if ($result == NULL) return mysqli_num_rows($this->lastResult);
else return mysqli_num_rows($result);
}
function queryUniqueObject($query, $debug = -1){
$query = "$query LIMIT 1";
$this->nbQueries++;
$result = mysqli_query($this->link, $query) or $this->debugAndDie($query);
$this->debug($debug, $query, $result);
return mysqli_fetch_object($result);
}
function queryUniqueValue($query, $debug = -1){
$query = "$query LIMIT 1";
$this->nbQueries++;
$result = mysqli_query($this->link, $query) or $this->debugAndDie($query);
$line = mysqli_fetch_row($result);
$this->debug($debug, $query, $result);
return $line[0];
}
function maxOf($column, $table, $where){
return $this->queryUniqueValue("SELECT MAX(`".$column."`) FROM `".$table."` WHERE ".$where);
}
function maxOfAll($column, $table){
return $this->queryUniqueValue("SELECT MAX(`".$column."`) FROM `".$table."`");
}
function countOf($table, $where){
return $this->queryUniqueValue("SELECT COUNT(*) FROM ".$table." WHERE ".$where);
}
function countOfAll($table){
return $this->queryUniqueValue("SELECT COUNT(*) FROM ".$table);
}
function debugAndDie($query,$error=1){
if($error){
$this->debugQuery($query, "Error");
$GLOBALS["ERRORSQL"] = mysqli_error($this->link);
}
return true;
}
function debug($debug, $query, $result = NULL){
if ($debug === -1 && $this->defaultDebug === false) return;
if ($debug === false) return;
$reason = ($debug === -1 ? "Default Debug" : "Debug");
$this->debugQuery($query, $reason);
if ($result == NULL) echo "<p style=\"margin: 2px;\">Number of affected rows: " . mysqli_affected_rows($this->link) . "</p></div>";
else $this->debugResult($result);
}
function debugQuery($query, $reason = "Debug"){
$color = ($reason == "Error" ? "red" : "orange");
echo "<div style=\"border: solid $color 1px; margin: 2px;\">" .
"<p style=\"margin: 0 0 2px 0; padding: 0; background-color: #DDF;\">" .
"<strong style=\"padding: 0 3px; background-color: $color; color: white;\">$reason:</strong> " .
"<span style=\"font-family: monospace;\">" . htmlentities($query) . "</span></p>";
}
function debugResult($result){
echo "<table border=\"1\" style=\"margin: 2px;\">" .
"<thead style=\"font-size: 80%\">";
$numFields = mysqli_num_fields($result);
// BEGIN HEADER
$tables = array();
$nbTables = -1;
$lastTable = "";
$fields = array();
$nbFields = -1;
while ($column = mysqli_fetch_field($result)) {
if ($column->table != $lastTable) {
$nbTables++;
$tables[$nbTables] = array("name" => $column->table, "count" => 1);
} else
$tables[$nbTables]["count"]++;
$lastTable = $column->table;
$nbFields++;
$fields[$nbFields] = $column->name;
}
for ($i = 0; $i <= $nbTables; $i++)
echo "<th colspan=" . $tables[$i]["count"] . ">" . $tables[$i]["name"] . "</th>";
echo "</thead>";
echo "<thead style=\"font-size: 80%\">";
for ($i = 0; $i <= $nbFields; $i++)
echo "<th>" . $fields[$i] . "</th>";
echo "</thead>";
// END HEADER
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
for ($i = 0; $i < $numFields; $i++)
echo "<td>" . htmlentities($row[$i]) . "</td>";
echo "</tr>";
}
echo "</table></div>";
$this->resetFetch($result);
}
function getExecTime(){
return round(($this->getMicroTime() - $this->mtStart) * 1000) / 1000;
}
function getQueriesCount(){
return $this->nbQueries;
}
function resetFetch($result){
if (mysqli_num_rows($result) > 0)
mysqli_data_seek($result, 0);
}
function lastInsertedId(){
return mysqli_insert_id($this->link);
}
function close(){
mysqli_close($this->link);
}
function getMicroTime(){
list($msec, $sec) = explode(' ', microtime());
return floor($sec / 1000) + $msec;
}
}
class Tools {
public $SITE;
protected $CONFIG;
protected $PARAMS;
protected $SQL;
public function __construct()
{
$this->CONFIG=static::get_CONFIG();
$this->SITE=static::get_SITE();
//$this->SQL = new DB();
if(isset($_GET["logout"])) $this->redirect_logout($this->CONFIG['logout_link']);
if(!(isset($_SESSION["log"]) && $_SESSION["log"])){
$this->SITE["log"]=false;
}
else{
$this->SITE["log"]=true;
$this->SITE["classBody"].=" log";
}
if(!($this->SITE["log"] && $_SESSION["token"]==PASS && $_SESSION["statut"]>=200)){$this->SITE["log_admin"]=false;}
else{$this->SITE["log_admin"]=true;}
$this->PARAMS['meta']=(($this->CONFIG['favicon'])?"<link rel='icon' href='".SUBDOMAINE."/content/assets/uploads/".$this->CONFIG['favicon']."' type=\"image/x-icon\">":"");
$this->PARAMS['meta'].='<link rel="preload" href="'.SUBDOMAINE.'/content/assets/webfonts/fa-solid-900.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="'.SUBDOMAINE.'/content/assets/webfonts/fa-regular-400.woff2" as="font" type="font/woff2" crossorigin="anonymous">
';//<link rel="preload" href="'.SUBDOMAINE.'/content/assets/webfonts/fa-brands-400.woff2" as="font" type="font/woff2" crossorigin="anonymous">';
if(!$this->SITE["log_admin"] && $this->SITE['in_Editor']===false){
if($this->CONFIG['gtagID']) $this->PARAMS['meta'].="<script async src=\"https://www.googletagmanager.com/gtag/js?id=".$this->CONFIG['gtagID']."\"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '".$this->CONFIG['gtagID']."');
</script>";
}
if($this->CONFIG['AStat']) $this->PARAMS['meta'].='<script id="stateAnalytics" log="'.((@$this->SITE["log"])?"1":"0").'" src="'.SUBDOMAINE.'/content/assets/lib/state.js"></script>';
}
public static function run(){
$route=\route::route();
$tools=new Tools();
if($route['template'])$tools->SITE['template']=$route['template'];
if($route['title'])$tools->SITE['title']=$route['title'];
if($route['page'])$tools->SITE['page']=$route['page'];
$tools->showConvertSc('content/templates/'.$tools->SITE['template'].'/template.php');
}
public static function get_SITE($key=''){
global $SITE;
if($key==''){
$SITE['in_Editor'] = (isset($SITE['in_Editor'])? $SITE['in_Editor'] : false);
$SITE['log_admin']=false;
$SITE['log']=false;
return $SITE;
}else{
if(isset($SITE[$key])){
return $SITE[$key];
}else{
return false;
}
}
}
public static function get_CONFIG($key=''){
global $CONFIG;
if($key==''){
return $CONFIG;
}else{
if(isset($CONFIG[$key])){
return $CONFIG[$key];
}else{
return false;
}
}
}
public static function get_SHORTCODE($key=''){
global $SHORTCODE;
if($key==''){
return $SHORTCODE;
}else{
if(isset($SHORTCODE[$key])){
return $SHORTCODE[$key];
}else{
return false;
}
}
}
/**
* @param string $logout_link
*/
public function redirect_logout($logout_link=""){//Redirection après decconexion
if (intval($_GET["logout"])>1000&&(intval($_GET["logout"]) % 2)==1) {
session_destroy();
if($logout_link!=""){
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
|| $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
header('Location: ' . $protocol . $_SERVER['HTTP_HOST'] . SUBDOMAINE.$logout_link);exit;
}else{
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
|| $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
header('Location: ' . $protocol . $_SERVER['HTTP_HOST'] . strtok($_SERVER["REQUEST_URI"], '?'));exit;
}
}}
public static function noSpecialCharacter($string){//Verifie si ne text ne presente aucun caractere special
if(preg_match('/[\'\"^£%&*()}{#~?><>,|=+¬\[\]]/', $string)) return false;
else return true;
}
function cache($strcache){//Ajoute le systeme de cache a tout les fichiers liés
if(cacheTAG!=''){$lc=strlen(cacheTAG);
preg_match_all('/[^(\.min\.css)](\.css)/im',$strcache, $matches, PREG_OFFSET_CAPTURE);
for($i=0,$size=sizeof($matches[0]);$i<$size;$i++){$strcache = substr_replace($strcache, "?v=".cacheTAG,($matches[0][$i][1])+((3+$lc)*($i+1)-$lc+2), 0);}
preg_match_all('/[^(\.min\.js)](\.js)/im',$strcache, $matches, PREG_OFFSET_CAPTURE);
for($i=0,$size=sizeof($matches[0]);$i<$size;$i++){$strcache = substr_replace($strcache, "?v=".cacheTAG,($matches[0][$i][1])+((3+$lc)*($i+1)-$lc+1), 0);}
}return $strcache;
}
function convertSc($str){
if(str_ends_with($str,'.php')) $str=file_get_contents($str);
if($str!=null) {
$str = str_replace('src="/', 'src="' . SUBDOMAINE . '/', $str);
$str = str_replace('href="/', 'href="' . SUBDOMAINE . '/', $str);
preg_match_all('/\[shortcode [^.+$\]\[()]*\]/mi', $str, $matches);
if($size=sizeof($matches[0])){
$shortcode=static::get_SHORTCODE();
for($i = 0, $size; $i < $size; $i++) {
$code = substr($matches[0][$i], 11, -1);
if(isset($shortcode[$code])){
$str = str_replace($matches[0][$i],$shortcode[$code],$str);
}
}
}
$shortcode=array(
'[template]' => $this->SITE["template"],
'[page]' => $this->SITE["page"],
'[title]' => $this->SITE["title"],
'[href-logout]' => '"?logout=<?= 1001+(2*rand(1, 2000))?>"',
'[class-body]' => $this->SITE["classBody"],
'[meta]' => $this->PARAMS['meta'],
'[header]' => '<?php $this->showConvertSc("./content/templates/'.$this->SITE["template"].'/header.php") ?>',
'[footer]' => '<?php $this->showConvertSc("./content/templates/'.$this->SITE["template"].'/footer.php") ?>',
'[content]' => '<?php $this->showConvertSc("./content/pages/'.$this->SITE["page"].'.php") ?>',
);
foreach ($shortcode as $k => $v) {
$str = str_replace($k, $v, $str);
}
return $str;
}else{
return false;
}
}
public function showConvertSc($str){
$content= $this->convertSc($str);
$content= $this->cache($content);
if($content){
eval(sprintf("?>%s<?php ", $content));
return true;
}else{
return false;
}
}
}
define('version','1.0.1');
?>