-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.php
More file actions
801 lines (737 loc) · 23.8 KB
/
functions.php
File metadata and controls
801 lines (737 loc) · 23.8 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
<?PHP
class upload
{
public static function upload_oramap($username, $pre_version="0", $user_id)
{
if(isset($_FILES["map_upload"]["name"]))
{
if (is_uploaded_file($_FILES["map_upload"]["tmp_name"]))
{
$filename = str_replace("'","",$_FILES["map_upload"]["name"]);
$filename = str_replace(" ", "\ ", $filename);
$filename = str_replace("(", "_", $filename);
$filename = str_replace(")", "_", $filename);
$source = $_FILES["map_upload"]["tmp_name"];
$type = $_FILES["map_upload"]["type"];
$name = explode(".", $filename);
$accepted_type = "application/octet-stream";
if ($type != $accepted_type)
{
return "Not supported file type"; // that's not a map file
}
if (strtolower(end($name)) != "oramap")
{
return "Not supported file type"; // that's not a map file (map file must have `oramap` extention)
}
exec("python python/minimap.py -s " . str_replace(" ", "\ ", $source) . " -i " . $user_id . " -u " . $username . " -t " . $filename . " -p " . $pre_version, $output, $return_code);
function code_match($code)
{
$codes = array(
'0' => "0",
'1' => "Error's while uploading map, contact administrator",
'2' => "Incorrect options",
'3' => "Unknown map format",
'4' => "Unknown mod",
'5' => "Map already exists",
'6' => "Could not upload the map",
'7' => "Database error, try again later",
'8' => "You already have a map with the same hash",
'9' => "Uploading Shellmap is NOT allowed! If you count this map playable, please set 'UseAsShellmap: False' in Yaml file",
'10' => "10",
);
return $codes[$code];
}
//return codes:
// 0 - Success
// 1 - Other errors
// 2 - Incorrect options
// 3 - Unknown map format
// 4 - Unknown mod
// 5 - Map exists
// 6 - Could not upload map
// 7 - Database error
// 8 - User already uploaded such a map
// 9 - It's a shellmap which is not allowed
// 10 - Uploading is ok but amount of playable slots equal zero
if ($return_code == 0 or $return_code == 10)
{
misc::increase_experience(10);
$row = db::nextRowFromQuery(db::executeQuery("SELECT uid,maphash FROM maps WHERE user_id = :1 ORDER BY posted DESC LIMIT 1", array($user_id)));
misc::event_log(user::uid(), "add", "maps", $row["uid"]);
if (isset($_POST["additional_desc"]))
db::executeQuery("UPDATE maps SET additional_desc = :1 WHERE user_id = :2 AND maphash = :3", array($_POST["additional_desc"], $user_id, trim($row["maphash"])));
}
//print_r($output); //logging on
return code_match($return_code);
}
else
return "";
}
else
return ""; // file is not choosen
}
public static function upload_unit($username)
{
function insert_unit($dirname,$description,$type,$unit_palette)
{
$query = "INSERT INTO units
(title,description,preview_image,user_id,type,palette)
VALUES
(:1,:2,:3,:4,:5,:6)
";
db::executeQuery($query, array($dirname, $description, "users/".user::username()."/units/".$dirname."/preview.gif", user::uid(), $type, $unit_palette));
misc::increase_experience(50);
$row = db::nextRowFromQuery(db::executeQuery("SELECT uid FROM units WHERE user_id = :1 ORDER BY posted DESC LIMIT 1", array(user::uid())));
misc::event_log(user::uid(), "add", "units", $row["uid"]);
}
$unit_palette = "temperat.pal"; //Needed for shp extractor
if(isset($_POST['unit_palette']))
$unit_palette = $_POST['unit_palette'];
$count = 0;
$messages = "";
while (isset($_FILES["file_".$count]))
{
$run_shp = false;
$filename = $_FILES["file_".$count]["name"];
if ($filename == "")
return $messages;
if (!isset($_POST["unit_name"]) or $_POST["unit_name"] == "")
return "1"; // Name of unit is not set!
$dirname = str_replace(" ", "_" , $_POST["unit_name"]);
$description = "";
if (isset($_POST["unit_description"]))
$description = $_POST["unit_description"];
$unit_type = "other";
if (isset($_POST["unit_type"]))
$unit_type = $_POST["unit_type"];
$source = $_FILES["file_".$count]["tmp_name"];
$type = $_FILES["file_".$count]["type"];
$name = explode(".", $filename); //array
$accepted_types = array("application/octet-stream","application/x-qgis");
$accepted_exts = array("shp","yaml","aud");
if(!in_array($type, $accepted_types) or !in_array(strtolower($name[1]), $accepted_exts))
{
$messages .= $filename . " - upload fail: not supported file type<br>";
continue;
}
$path = WEBSITE_PATH . "users/" . $username . "/units/" . $dirname;
if ($count == 0) //make dir checking at first file recognised
{
if (is_dir($path))
{
return "Unit with such name already exists";
}
else
{
mkdir($path);
insert_unit($dirname, $description, $unit_type, $unit_palette);
}
}
else
{
//directory was not created before this moment - file types were unsupported
if (!is_dir($path))
{
mkdir($path);
insert_unit($dirname, $description, $unit_type, $unit_palette);
}
}
$target_path = $path . "/" . $filename;
if(move_uploaded_file($source, $target_path))
{
$messages .= $filename ." - uploaded<br>";
}
if (strtolower(end($name)) == "shp" and $run_shp == false)
{
exec("python python/shpextractor.py -s ".str_replace(" ", "\ ", $target_path)." -p ".$unit_palette, $output, $return_code);
$run_shp = true;
}
$count++;
}
return $messages;
}
public static function upload_replay($username, $user_id)
{
if(isset($_FILES["replay_upload"]["name"]))
{
if (is_uploaded_file($_FILES["replay_upload"]["tmp_name"]))
{
$filename = $_FILES["replay_upload"]["name"];
$source = $_FILES["replay_upload"]["tmp_name"];
$type = $_FILES["replay_upload"]["type"];
$name = explode(".", $filename);
$accepted_type = "application/octet-stream";
if ($type != $accepted_type)
{
return "Not supported file type"; // that's not a replay file
}
if (strtolower($name[1]) != "rep")
{
return "Not supported file type"; // that's not a replay file (replay file must have `rep` extention)
}
$query = "SELECT COUNT(*) AS count FROM replays WHERE user_id = :1";
$row = db::nextRowFromQuery(db::executeQuery($query, array($user_id)));
if ($row["count"] > 50)
{
return "You have reached your limit!"; // can't upload more then 50 replays
}
exec("python python/replay.py -s " . str_replace(" ", "\ ", $source) . " -i " . $user_id . " -u " . $username . " -t " . str_replace(" ", "\ ", $filename), $output, $return_code);
function code_match($code)
{
$codes = array(
'0' => "0",
'1' => "Error's while uploading replay, contact administrator",
'2' => "Incorrect options",
'3' => "StartGame point is not detected",
'4' => "You already have this replay uploaded",
);
return $codes[$code];
}
//return codes:
// 0 - Success
// 1 - Other errors
// 2 - Incorrect options
// 3 - StartGame point is not detected
// 4 - User already has an identical replay uploaded
if ($return_code == 0)
{
misc::increase_experience(10);
$row = db::nextRowFromQuery(db::executeQuery("SELECT uid FROM replays WHERE user_id = :1 ORDER BY posted DESC LIMIT 1", array($user_id)));
misc::event_log(user::uid(), "add", "replays", $row["uid"]);
if (isset($_POST["description"]))
db::executeQuery("UPDATE replays SET description = :1 WHERE user_id = :2 AND uid = :3", array($_POST["description"], $user_id, $row["uid"]));
}
return code_match($return_code);
}
else
return "";
}
else
return ""; // file is not choosen
}
public static function screenshot()
{
if(isset($_FILES["screenshot_upload"]["name"]))
{
if (!is_uploaded_file($_FILES["screenshot_upload"]["tmp_name"]))
return "";
if ( !(isset($_POST["table_name"]) and isset($_POST["table_id"])) )
return "";
$id = $_POST["table_id"];
$table = $_POST["table_name"];
$query = "SELECT * FROM screenshot_group WHERE table_name = :1 AND table_id = :2 AND user_id = :3";
$res = db::executeQuery($query, array($table, $id, user::uid()));
if (db::num_rows($res) >= 4)
return "";
$filename = $_FILES["screenshot_upload"]["name"];
$name = explode(".", $filename);
$source = $_FILES["screenshot_upload"]["tmp_name"];
$type = $_FILES["screenshot_upload"]["type"];
$accepted_types = array("image/jpeg","image/png","image/gif","image/bmp","image/x-png");
if(!in_array($type, $accepted_types))
return "";
$path = "images/screenshots/".$table."_".$id."_".user::uid()."_".chr(97 + mt_rand(0, 25)).chr(97 + mt_rand(0, 25)).".".$name[1];
move_uploaded_file($source, $path);
$query = "INSERT INTO screenshot_group
(table_id,table_name,user_id,image_path)
VALUES (:1,:2,:3,:4)
";
db::executeQuery($query, array($id,$table,user::uid(),$path));
//create thumbnail
$type = image_type_to_mime_type(exif_imagetype($path));
$new_name = explode(".", $path);
$new_name = $new_name[0] . "_thumbnail." . $new_name[1];
misc::imageresize($new_name,$path,300,300,100, $type);
$query = "SELECT uid FROM screenshot_group WHERE table_id = :1 AND table_name = :2 AND user_id = :3 ORDER BY posted DESC";
$row_sc = db::nextRowFromQuery(db::executeQuery($query, array($id, $table, user::uid())));
misc::event_log(user::uid(), "add", "screenshot", $row_sc["uid"]);
}
return "";
}
public static function avatar()
{
if(isset($_FILES["avatar_upload"]["name"]))
{
if (!is_uploaded_file($_FILES["avatar_upload"]["tmp_name"]))
return "";
$filename = $_FILES["avatar_upload"]["name"];
$source = $_FILES["avatar_upload"]["tmp_name"];
$type = $_FILES["avatar_upload"]["type"];
$accepted_types = array("image/jpeg","image/png","image/gif","image/bmp","image/x-png");
if(!in_array($type, $accepted_types))
{
return "type error";
}
move_uploaded_file($source, "users/".user::username()."/avatar_original.jpg");
misc::imageresize("users/".user::username()."/avatar.jpg","users/".user::username()."/avatar_original.jpg",200,400,100, $type);
unlink("users/".user::username()."/avatar_original.jpg");
$query = "UPDATE users SET avatar = :1 WHERE uid = :2";
db::executeQuery($query, array("Some", user::uid()));
return "done";
}
return "";
}
}
class pages
{
public static function main_page_request()
{
if (isset($_GET['register']) and (!user::online()))
{
user::register_actions();
return;
}
if (isset($_GET['recover']) and (!user::online()))
{
echo "<a href='?recover&recover_pass'>Recover password</a><br>";
echo "<a href='?recover&recover_user'>Recover username</a><br>";
user::recover();
return;
}
if (isset($_GET['action']))
{
// non menu or profile: other pages
content::action($_GET['action']);
return;
}
// other checks should be done before $_GET['p'], because it will override page
if (isset($_GET['profile']))
{
profile::show_profile();
return;
}
if (isset($_GET['p']))
{
content::page($_GET['p']);
return;
}
if (count($_GET) == 0)
{
echo "<h3>Recent Articles</h3>";
$result = db::executeQuery("SELECT * FROM articles ORDER BY posted DESC");
echo content::createArticleItems($result);
return;
}
}
public static function current($page, $request)
{
if ($page == $request)
return "current";
else
return "";
}
public static function allISSet($arr)
{
for($i = 0; $i < count($arr); $i++)
if(isset($_POST[$arr[$i]]) == false)
return false;
return true;
}
public static function serialize_array($arr)
{
return base64_encode(json_encode($arr));
}
public static function deserialize_array($str)
{
return json_decode(base64_decode($str));
}
}
class misc
{
public static function avatar($user_id)
{
$query = "SELECT avatar,login FROM users WHERE uid = :1";
$ava = db::nextRowFromQuery(db::executeQuery($query, array($user_id)));
if ($ava["avatar"] == "None")
{
return "images/noavatar.png";
}
elseif ($ava["avatar"] == "Some")
{
return "users/".$ava["login"]."/avatar.jpg";
}
}
public static function comment_owner($id)
{
if (user::online())
{
if ($id == user::uid())
{
return True;
}
}
return False;
}
public static function delete_comment($id, $user)
{
if ( $user == user::uid() )
{
$query = "DELETE FROM comments WHERE uid = :1";
db::executeQuery($query, array($id));
}
}
public static function delete_item($item_id, $table_name, $user_id)
{
if ($user_id == user::uid() or user::permission() == "1")
{
//remove map directory and it's content from disk
if ($table_name == "maps")
{
$p_ver = 0;
$n_ver = 0;
$query = "SELECT path,p_ver,n_ver FROM maps WHERE uid = :1";
$result = db::executeQuery($query, array($item_id));
while ($db_data = db::nextRowFromQuery($result))
{
$p_ver = $db_data["p_ver"];
$n_ver = $db_data["n_ver"];
$path = WEBSITE_PATH . $db_data['path'];
}
foreach (scandir($path) as $item)
{
if ($item == '.' || $item == '..') continue;
unlink($path.$item);
}
rmdir($path);
$query = "UPDATE maps
SET n_ver = :1
WHERE uid = :2
";
db::executeQuery($query, array($n_ver, $p_ver));
$query = "UPDATE maps
SET p_ver = :1
WHERE uid = :2
";
db::executeQuery($query, array($p_ver, $n_ver));
misc::decrease_experience(10);
}
if ($table_name == "units")
{
$query = "SELECT title FROM units WHERE uid = :1";
$result = db::executeQuery($query, array($item_id));
while ($db_data = db::nextRowFromQuery($result))
{
$title = $db_data['title'];
}
$query = "SELECT login FROM users WHERE uid = :1";
$result = db::executeQuery($query, array($user_id));
while ($db_data = db::nextRowFromQuery($result))
{
$username = $db_data['login'];
}
$path = WEBSITE_PATH . "users/" . $username . "/units/" . $title . "/";
foreach (scandir($path) as $item)
{
if ($item == '.' || $item == '..') continue;
unlink($path.$item);
}
rmdir($path);
misc::decrease_experience(50);
}
if ($table_name == "replays")
{
$query = "SELECT path FROM replays WHERE uid = :1";
$result = db::executeQuery($query, array($item_id));
while ($db_data = db::nextRowFromQuery($result))
{
$path = WEBSITE_PATH . $db_data['path'];
}
unlink($path);
$query = "DELETE FROM replay_players WHERE id_replays = :1";
db::executeQuery($query, array($item_id));
misc::decrease_experience(10);
}
if ($table_name == "screenshot_group")
{
$query = "SELECT image_path FROM screenshot_group WHERE uid = :1";
$result = db::executeQuery($query, array($item_id));
$path = False;
while ($db_data = db::nextRowFromQuery($result))
{
$path = $db_data['image_path'];
}
if ($path)
{
$dir = dirname($path) . "/";
$name = basename($path);
$thumb = explode(".", $name);
$thumb = $dir . $thumb[0] . "_thumbnail." . $thumb[1];
unlink(WEBSITE_PATH . $path);
unlink(WEBSITE_PATH . $thumb);
}
}
if ($table_name == "guides")
misc::decrease_experience(50);
//remove screenshots for item if there are any
if (in_array($table_name, array("maps","units")))
{
$query = "SELECT * FROM screenshot_group WHERE table_id = :1 AND table_name = :2 AND user_id = :3 AND image_path NOT LIKE ('%/fullPreview%')";
$result_screen = db::executeQuery($query, array($item_id, $table_name, $user_id));
while ($row_screen = db::nextRowFromQuery($result_screen))
{
$dir = dirname($row_screen["image_path"]) . "/";
$name = basename($row_screen["image_path"]);
$thumb = explode(".", $name);
$thumb = $dir . $thumb[0] . "_thumbnail." . $thumb[1];
unlink(WEBSITE_PATH . $row_screen["image_path"]);
unlink(WEBSITE_PATH . $thumb);
}
$query = "DELETE FROM screenshot_group WHERE table_id = :1 AND table_name = :2 AND user_id = :3";
db::executeQuery($query, array($item_id, $table_name, $user_id));
}
//remove item from DB
$query = "DELETE FROM $table_name WHERE uid = :1";
db::executeQuery($query, array($item_id));
//remove comments from DB
//remove records from fav_item table related to current item for each user
//remove records in featured and reported tables; remove screenshots for this item
$tables = array("comments", "fav_item", "featured", "reported", "screenshot_group");
foreach($tables as $table)
{
$query = "DELETE FROM $table WHERE table_name = :1 AND table_id = :2";
db::executeQuery($query, array($table_name, $item_id));
}
}
}
public static function imageresize($result_file, $original_file, $new_width, $new_height, $quality, $type)
{
if($type == "image/jpeg")
{
$im=imagecreatefromjpeg($original_file);
}
elseif($type == "image/png" or $type == "image/x-png")
{
$im=imagecreatefrompng($original_file);
}
elseif($type == "image/gif")
{
$im=imagecreatefromgif($original_file);
}
elseif($type == "image/bmp")
{
$im=imagecreatefrombmp($original_file);
}
$k1=$new_width/imagesx($im);
$k2=$new_height/imagesy($im);
$k=$k1>$k2?$k2:$k1;
$w=intval(imagesx($im)*$k);
$h=intval(imagesy($im)*$k);
$im1=imagecreatetruecolor($w,$h);
if($type == "image/png" or $type == "image/x-png")
{
imagealphablending($im1, false);
imagesavealpha($im1, true);
}
imagecopyresampled($im1,$im,0,0,0,0,$w,$h,imagesx($im),imagesy($im));
if($type == "image/png" or $type == "image/x-png")
{
imagepng($im1,$result_file);
}
else
{
imagejpeg($im1,$result_file,$quality);
}
imagedestroy($im);
imagedestroy($im1);
}
public static function increase_experience($points)
{
$query = "SELECT experience FROM users WHERE uid = :1";
$value = db::nextRowFromQuery(db::executeQuery($query, array(user::uid())));
$value = $value["experience"] + $points;
$query = "UPDATE users SET experience = :1 WHERE uid = :2";
db::executeQuery($query, array($value, user::uid()));
}
public static function decrease_experience($points)
{
$query = "SELECT experience FROM users WHERE uid = :1";
$value = db::nextRowFromQuery(db::executeQuery($query, array(user::uid())));
$value = $value["experience"] - $points;
$query = "UPDATE users SET experience = :1 WHERE uid = :2";
db::executeQuery($query, array($value, user::uid()));
}
public static function event_log($user_id, $type, $table_name="", $table_id=0)
{
// types: add,delete_item,delete_comment,report,fav,unfav,edit,login,logout,comment,follow,unfollow
// issue with `delete_item`: we can not show what user removed because it's removed completely (even basic info of item)
$query = "INSERT INTO event_log
(user_id, type, table_name, table_id)
VALUES
(:1,:2,:3,:4)
";
db::executeQuery($query, array($user_id, $type, $table_name, $table_id));
}
public static function amount_rows($result, $value)
{
if (db::num_rows($result) > $value)
return True;
return False;
}
public static function minimap($path)
{
if (file_exists($path . "minimap.bmp"))
return $path . "minimap.bmp";
return "images/nominimap.png";
}
public static function fullPreviewExists($path)
{
if (file_exists($path . "fullPreview.bmp"))
return True;
return False;
}
public static function option_selected($value, $request)
{
if ($value == $request)
return "selected='selected'";
return "";
}
public static function option_selected_bool($value, $request)
{
if ($value == $request)
return "true";
return "false";
}
public static function item_title_by_uid($id, $table)
{
$query = "SELECT title FROM $table WHERE uid = :1";
$result = db::executeQuery($query, array($id));
while ($row = db::nextRowFromQuery($result))
return $row["title"];
return "";
}
public static function item_exists($id, $table)
{
$query = "SELECT uid FROM $table WHERE uid = :1";
$result = db::executeQuery($query, array($id));
while ($row = db::nextRowFromQuery($result))
return True;
return False;
}
public static function item_owner($id, $table, $user_id)
{
$query = "SELECT user_id FROM $table WHERE uid = :1";
$result = db::executeQuery($query, array($id));
while ($row = db::nextRowFromQuery($result))
{
if ($row["user_id"] == $user_id)
return True;
}
return False;
}
public static function paging($nrOfPages, $i, $current, $gets, $table, $type="grid", $pointer = "")
{
if( $i <= 3 || $i >= $nrOfPages - 3 || ( $i < $current + 3 && $i > $current - 3) )
{
if($current == $i)
$pages = "<span id='page_count_none'>" . $i . "</span>";
else
$pages = "<span id='page_count'><a href='?current_".$type."_page_".$table."=".$i.$gets.$pointer."'>" . $i . "</a></span>";
return $pages;
}
return "...";
}
public static function current_map_version($current, $id)
{
if ($current == $id)
return "map_grid_current";
else
return "map_grid";
}
public static function send_mail($email, $subject, $body, Array $headers = array(), $additional_parameters = NULL)
{
// Make sure we set Content-Type and charset
if ( !isset( $headers['Content-Type'] ) )
{
$headers['Content-Type'] = 'text/plain; charset=utf-8';
}
$headers_str = '';
foreach( $headers as $key => $val )
{
$headers_str .= sprintf( "%s: %s\r\n", $key, $val );
}
// Use mb_send_mail() function instead of mail() so that headers, including subject are properly encoded
return mb_send_mail( $email, $subject, $body, $headers_str, $additional_parameters );
}
public static function amount_of_items_option($table, $option, $my_items=false)
{
$my = "";
$my_array = array();
if ($my_items == true)
{
$my = " AND user_id = :1";
$my_array = array(user::uid());
}
$query = "SELECT * FROM $table ".$option.$my;
$result = db::executeQuery($query, $my_array);
return db::num_rows($result);
}
/* creates a compressed zip file */
public static function create_zip($dir, $destination="", $overwrite=false)
{
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if (is_dir($dir))
{
foreach (scandir($dir) as $item)
{
if ($item == '.' || $item == '..') continue;
//make sure the file exists
if(file_exists($dir."/".$item))
{
$valid_files[] = $dir."/".$item;
}
}
}
$dirname = basename($dir);
//if we have good files...
if(count($valid_files))
{
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true)
{
return false;
}
$zip->addEmptyDir($dirname);
//add the files
foreach($valid_files as $file)
{
$zip->addFile($file,$dirname."/".basename($file));
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
public static function ingame_players()
{
$ra = 0;
$cnc = 0;
$d2k = 0;
$string = file_get_contents("http://master.open-ra.org/list_json.php");
$json = json_decode($string, true);
$json_len = count($json);
for ($i=0;$i<$json_len;$i++)
{
$mod = explode("@",$json[$i]["mods"]);
if (strtolower($mod[0]) == "ra")
$ra = $ra + (int)$json[$i]["players"];
else if (strtolower($mod[0]) == "cnc")
$cnc = $cnc + (int)$json[$i]["players"];
else if (strtolower($mod[0]) == "d2k")
$d2k = $d2k + (int)$json[$i]["players"];
}
return array($ra,$cnc,$d2k);
}
}
?>