-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunctions.php
More file actions
443 lines (401 loc) · 10.9 KB
/
functions.php
File metadata and controls
443 lines (401 loc) · 10.9 KB
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
// Informations generales
const VERSION = '1.0.26';
include("config.php");
$plugins_home = array();
$plugins_home_connected = array();
$plugins_edit = array();
$plugins_manage = array();
function _e( $text ) {
echo gettext($text);
}
function __( $text ) {
return gettext($text);
}
function noBreakLines($str){
return str_replace(array("\r", "\n"), "", $str);
}
function noQuotes($str){
return str_replace(array("\""), "", $str);
}
function formName($str){
return str_replace(array("\"", "'", " "), "", $str);
}
function onlyAlphaNum($str){
return $chaine = preg_replace('#[^a-zA-Z0-9-]#u', "", $str);
}
function onlyOneLetter($str){
return substr(onlyAlphaNum($str),0,1);
}
function onlyTwoChars($str){
return substr(onlyAlphaNum($str),0,2);
}
function str2bdd($str){
return $str;
}
function str2html($str){
return htmlentities($str);
}
function sql_select($table, $where, $order) {
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$where[1] = $link->real_escape_string($where[1]);
$sql = "SELECT * FROM `".DB_PREF.$table."` WHERE ".$where[0]." = '".$where[1]."' ".$order.";";
$result = $link->query($sql);
$results = array();
while($r = $result->fetch_array()){
$results[] = $r;
}
mysqli_close($link);
return $results;
}
function sql_select_unique($table, $where) {
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$where[1] = $link->real_escape_string($where[1]);
$sql = "SELECT * FROM `".DB_PREF.$table."` WHERE ".$where[0]." = '".$where[1]."';";
$result = $link->query($sql);
if($result->num_rows){
$r = $result->fetch_array();
mysqli_close($link);
return $r;
} else {
mysqli_close($link);
return false;
}
}
function sql_exec($sql) {
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if($result = $link->query($sql)){
mysqli_close($link);
return true;
} else {
mysqli_close($link);
return false;
}
}
function sql_insert($table, $vars){
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
foreach($vars as $k => $v){
$vars[$k] = $link->real_escape_string($vars[$k]);
}
echo $sql = "INSERT INTO `".DB_PREF.$table."` (".implode(", ", array_keys($vars)).") VALUES ('".implode("','", array_values($vars))."');";
if($result = $link->query($sql)){
mysqli_close($link);
return true;
} else {
mysqli_close($link);
return false;
}
}
function sql_update($table, $vars, $where){
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$changes = array();
foreach($vars as $k => $v){
$changes[] = $k." = '".$link->real_escape_string($vars[$k])."'";
}
$where[1] = $link->real_escape_string($where[1]);
$sql = "UPDATE `".DB_PREF.$table."` SET ".implode(",", array_values($changes))." WHERE ".$where[0]." = '".$where[1]."';";
if($result = $link->query($sql)){
mysqli_close($link);
return true;
} else {
mysqli_close($link);
return false;
}
}
function sql_delete($table, $where){
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$changes = array();
$where[1] = $link->real_escape_string($where[1]);
$sql = "DELETE FROM `".DB_PREF.$table."` WHERE ".$where[0]." = '".$where[1]."';";
if($result = $link->query($sql)){
mysqli_close($link);
return true;
} else {
mysqli_close($link);
return false;
}
}
function sql_count($table, $where) {
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$where[1] = $link->real_escape_string($where[1]);
$sql = "SELECT * FROM `".DB_PREF.$table."` WHERE ".$where[0]." = '".$where[1]."';";
$result = $link->query($sql);
mysqli_close($link);
return $result->num_rows;
}
function connect($mail, $pass) {
global $error;
$mail = addslashes(strtolower($mail));
if($user = sql_select_unique("users", array("mail", $mail))){
$id = $user['id'];
$hash = $user['password'];
if(password_verify(PREF.$pass.SUFF, $hash)){
$_SESSION['user'] = $user['id'];
$_SESSION['name'] = $user['mail'];
return true;
} else {
$error = __("Identifiants inconnus.");
$_SESSION['user'] = "";
$_SESSION['name'] = "";
return false;
}
}
}
function connected() {
if(!empty($_SESSION['user'])) {
return true;
} else {
$_SESSION['user'] = "";
$_SESSION['name'] = "";
return false;
}
}
function userId(){
return (int)$_SESSION['user'];
}
function userInfo($id) {
if($user = sql_select_unique("users", array("id", $id))){
return $user;
} else {
return false;
}
}
function userLinks($id = false, $order = "time") {
if($id == false){
if(!empty($_SESSION['user'])) {
$id = $_SESSION['user'];
}
}
if($id != false) {
$links = sql_select("links", array("owner", $id), "ORDER BY ".$order." DESC;");
$linksId = array();
foreach($links as $l){
$linksId[] = $l['id'];
}
return $linksId;
} else {
return false;
}
}
function linkInfos($id = false){
if($qr = sql_select_unique("links", array("id", $id))){
$links = explode("\r\n", $qr['links']);
$newlinks = array();
foreach($links as $link){
$l = explode("|", $link);
if(!empty($l[1])){
$l[0] = str2html(utf8_encode($l[0]));
$l[1] = str2html($l[1]);
$newlinks[] = $l;
}
}
$qr['title'] = str2html(utf8_encode($qr['title']));
$qr['description'] = str2html(utf8_encode($qr['description']));
$qr['links'] = $newlinks;
return $qr;
} else {
return false;
}
}
function removeLink($id){
global $uploaddir;
return sql_delete("links", array("id", $id));
}
function ajoututilisateur($mail, $passwd){
$password = password_hash(PREF.$passwd.SUFF, PASSWORD_BCRYPT);
$sql = "INSERT INTO `".DB_PREF."users` (`mail`, `password`) VALUES ('".$mail."', '".$password."')";
sql_exec($sql);
}
function removeAccount($id = false){
global $error;
if($id == false){
if(!empty($_SESSION['user'])) {
$id = $_SESSION['user'];
}
}
if($id != false){
$links = userLinks($id);
$ok = true;
foreach($links as $lid){
if(!removeLink($lid)){
$ok = false;
}
}
if($ok){
if(!sql_delete("users", array("id", $id))){
$ok = false;
}
}
if($ok){
$error = __("Compte supprimé.");
if($id == $_SESSION['user']){
$_SESSION = array();
}
} else {
$error = __("Problème lors de la suppression.");
}
} else {
$error = __("Problème lors de la suppression.");
return false;
}
}
function newId(){
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$string = '';
for($i=0; $i<6; $i++){
$string .= $chars[rand(0, strlen($chars)-1)];
}
return $string;
}
function uniqueId($id){
$idExists = sql_count("links", array("id", $id));
if($idExists == 0){
return true;
} else {
return false;
}
}
function isLockee($url){
$vid = "";
if( strpos($url, 'lockee.fr/o/') !== false ){
$vid = $url;
}
return $vid;
}
function isvideo($url){
$vid = "";
// Youtube
if( (strpos($url, 'youtube') !== false) || (strpos($url, 'youtu.be') !== false) ){
$regex = '/^https?:\/\/(?:(?:www|m)\.)?(?:youtube\.com\/watch(?:\?v=|\?.+?&v=)|youtu\.be\/)([a-z0-9_-]+)$/i';
if ( preg_match($regex, $url, $results) ) {
$id = $results[1];
$vid = 'https://www.youtube.com/embed/'.$id.'?rel=0&showinfo=0&theme=light';
}
// Vimeo
} else if( strpos($url, 'vimeo.com') !== false ){
$regex = '/^https?:\/\/(?:www\.)?vimeo\.com.+?(\d+).*$/i';
if ( preg_match($regex, $url, $results) ) {
$id = $results[1];
$vid = 'https://player.vimeo.com/video/'.$id;
}
// Dailymotion
} else if( (strpos($url, 'dai.ly') !== false) || (strpos($url, 'dailymotion') !== false) ){
$regex = '/^https?:\/\/(?:www\.)?(?:dai\.ly\/|dailymotion\.com\/(?:.+?video=|(?:video|hub)\/))([a-z0-9]+)$/i';
if ( preg_match($regex, $url, $results) ) {
$id = $results[1];
$vid = 'https://www.dailymotion.com/embed/video/'.$id.'?queue-autoplay-next=false&queue-enable=false&sharing-enable=false&ui-theme=false';
}
// Mediacad Ac-Nantes
} else if( strpos($url, 'mediacad.ac-nantes.fr') !== false ) {
$regex = '/^https?:\/\/mediacad.ac-nantes.fr\/m\/([0-9]+)$/i';
if ( preg_match($regex, $url, $results) ) {
$id = $results[1];
$vid = 'https://mediacad.ac-nantes.fr/m/'.$id.'/d/i';
}
}
return $vid;
}
function isMathalea($url){
$vid = "";
if( strpos($url, 'coopmaths.fr/') !== false ){
$vid = $url;
}
return $vid;
}
function isExerciseur($titre){
$vid = "";
if( strpos($titre, 'Exerciseur') !== false ){
$vid = $titre;
}
return $vid;
}
function displayControls($cid, $exclude = "", $txt = 0){
$controls = "";
if($txt == 1){
$ca = " ".__("Aller");
$cv = " ".__("Voir");
$cm = " ".__("Modifier");
$cs = " ".__("Supprimer");
$ce = " ".__("Exporter");
} else {
$ca = "";
$cv = "";
$cm = "";
$cs = "";
$ce = "";
}
$controls .= "<a class='controls' href='".SITE_URL."$cid' target='_blank' title='".__("Aller")."'><div class='img-controls o'></div><div class='l'>$ca</div></a>";
if($exclude != "v"){
$controls .= "<a class='controls' href='".SITE_URL."v/$cid' title='".__("Voir")."'><div class='img-controls v'></div><div class='l'>$cv</div></a>";
}
if($exclude != "m"){
$controls .= "<a class='controls' href='".SITE_URL."m/$cid' title='".__("Modifier")."'><div class='img-controls m'></div><div class='l'>$cm</div></a>";
}
/* if($exclude != "e"){
$controls .= "<a class='controls' href='".SITE_URL."e/$cid' title='".__("Exporter")."'><div class='img-controls e'></div><div class='l'>$ce</div></a>";
}*/
if($exclude != "s"){
$controls .= "<a class='controls' href='".SITE_URL."s/$cid' title='".__("Supprimer")."'><div class='img-controls s'></div><div class='l'>$cs</div></a>";
}
return $controls;
}
function tobase62($number){
$tobase = 62;
$map = implode('',array_merge(range(0,9),range('a','z'),range('A','Z')));
$map_base = substr($map,0,$tobase);
$tobase = strlen($map_base);
$result = '';
while ($number >= $tobase) {
$result = $map_base[$number%$tobase].$result;
$number /= $tobase;
}
return $map_base[$number].$result;
}
function creation_token(){
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$hash = $_SESSION['user'].time();
$token = tobase62($hash);
while(strlen($token)<100){
$token .= $chars[rand(0, strlen($chars)-1)];
}
return $token;
}
function connect_token($token) {
if(!empty($token)){
if($user = sql_select_unique("users", array("token", $token))){
$_SESSION['user'] = $user['id'];
$_SESSION['name'] = $user['mail'];
return true;
} else {
$error = __("Token introuvable.");
$_SESSION['user'] = "";
$_SESSION['name'] = "";
return false;
}
} else {
return false;
}
}
function add_to_home($html, $connected = true, $order = 0){
global $plugins_home_connected, $plugins_home;
$plugins_home_connected[$order][] = $html;
if(!$connected){
$plugins_home[$order][] = $html;
}
}
function add_to_edit($html, $order = 0){
global $plugins_edit;
$plugins_edit[$order][] = $html;
if(!$connected){
$plugins_edit[$order][] = $html;
}
}
function add_to_manage($html, $order = 0){
global $plugins_manage;
$plugins_manage[$order][] = $html;
if(!$connected){
$plugins_manage[$order][] = $html;
}
}
//add_to_home("<a href='test/' class='menu-button'>Test menu 5</a>", false, 5);
?>