-
Notifications
You must be signed in to change notification settings - Fork 4
/
wc3.php
1319 lines (1228 loc) · 39.7 KB
/
wc3.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
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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// WarezCoders DDL Script 3
// Coded by JmZ
// Thanks to my partner & friend since 2004, Sickness, for coding with me all these years
// Thanks to the following people for being around:
// Whoo, Costa, Phaze, Kurupt, Corrup, Katz, Ultra, Wau, Ultima, Tippie
// the list goes on.
// I coded this in widescreen so expect long lines
define('WCDDL_GUTS', 1337);
require "./wcfg.php";
foreach(Core::load()->getModules() as $module)
include $module['path'];
Core::load()->executeHook('init');
class DatabaseRaw {
protected $val;
public function __construct($val) {
$this->val = $val;
}
public function __toString() {
return $this->val;
}
}
class Database {
private static $db;
private static $log = array();
private static $logTime = 0;
public static function load() {
if(empty(self::$db))
try {
self::$db = new PDO(WCDDL_DB_DSN, WCDDL_DB_USER, WCDDL_DB_PASS);
} catch(PDOException $e) {
die('Database problems, try again in a few minutes.');
}
return self::$db;
}
public static function quickColumn($query, $args=array()) {
Core::load()->executeHook('DatabaseColumn', array(&$query, &$args));
if(!$c = self::query($query, $args))
return false;
return $c->fetchColumn();
}
public static function quickRowObject($class, $query, $args=array()) {
Core::load()->executeHook('DatabaseRowObject', array(&$class, &$query, &$args));
if(!$c = self::query($query, $args))
return false;
return $c->fetchObject($class);
}
public static function quickRowObjects($class, $query, $args=array()) {
Core::load()->executeHook('DatabaseRowObjects', array(&$class, &$query, &$args));
if(!$c = self::query($query, $args))
return false;
return $c->fetchAll(PDO::FETCH_CLASS, $class);
}
public static function quickExecute($query, $args=array(), $id = false) {
Core::load()->executeHook('DatabaseExecute', array(&$query, &$args));
if(!$c = self::query($query, $args))
return false;
return $id ? self::load()->lastInsertId() : $c->rowCount();
}
public static function quickRow($query, $args=array()) {
Core::load()->executeHook('DatabaseRow', array(&$query, &$args));
if(!$c = self::query($query, $args))
return false;
return $c->fetch();
}
public static function quickRows($query, $args=array()) {
Core::load()->executeHook('DatabaseRows', array(&$query, &$args));
if(!$c = self::query($query, $args))
return false;
return $c->fetchAll();
}
public static function query($query, $args=array()) {
$d = self::load();
$start = microtime(true);
if(!$q = $d->prepare($query))
return false;
if(!$q->execute($args))
return false;
$end = microtime(true)-$start;
self::$log[] = array($query, $args, $end);
self::$logTime += $end;
return $q;
}
public static function queryLog() {
return self::$log;
}
public static function queryLogCount() {
return count(self::$log);
}
public static function queryLogTime() {
return self::$logTime;
}
public static function insert($table, $map, $id = false) {
// Paranoia mode again
$table = preg_replace('#[^\w\-]+#', '', $table);
Core::load()->executeHook('DatabaseInsert', array(&$table, &$map));
$query = 'INSERT INTO ' . $table . ' (' . implode(', ', array_keys($map)) . ') VALUES (';
$params = array();
foreach($map as $field => $value) {
if($value instanceof DatabaseRaw) {
$params[] = (string) $value;
unset($map[$field]);
continue;
}
$params[] = ':' . $field;
}
$query .= implode(', ', $params) . ')';
if(!$c = self::query($query, $map))
return false;
return $id ? self::load()->lastInsertId() : $c->rowCount();
}
public static function delete($table, $map = array()) {
$table = preg_replace('#[^\w\-]+#', '', $table);
Core::load()->executeHook('DatabaseDelete', array(&$table, &$map));
$query = 'DELETE FROM ' . $table;
if(!empty($map)) {
$query .= ' WHERE ';
$where = array();
foreach($map as $field => $value) {
if($value instanceof DatabaseRaw) {
$where[] = $field . ' = ' . ((string) $value);
unset($map[$field]);
continue;
}
$where[] = $field . ' = :' . $field;
}
$query .= implode(' AND ', $where);
}
if(!$c = self::query($query, $map))
return false;
return $c->rowCount();
}
public static function update($table, $map, $criteria = array()) {
$table = preg_replace('#[^\w\-]+#', '', $table);
Core::load()->executeHook('DatabaseUpdate', array(&$table, &$map));
$query = 'UPDATE ' . $table . ' SET ';
$params = array();
foreach($map as $field => $value) {
if($value instanceof DatabaseRaw) {
$params[] = $field . ' = ' . ((string) $value);
unset($map[$field]);
continue;
}
$params[] = $field . ' = :' . $field;
}
$query .= implode(', ', $params);
if(!empty($criteria)) {
$query .= ' WHERE ';
$params = array();
foreach($criteria as $field => $value) {
if($value instanceof DatabaseRaw) {
$params[] = $field . ' = ' . ((string) $value);
unset($criteria[$field]);
continue;
}
$params[] = $field . ' = :' . $field;
}
$query .= implode(' AND ', $params);
}
$map = array_merge($map, $criteria);
if(!$c = self::query($query, $map))
return false;
return $c->rowCount();
}
}
class Core {
private static $instance;
private $templateVariables = array();
private $configCache = array();
protected $hookList = array();
public static function load() {
if(empty(self::$instance))
self::$instance = new Core;
return self::$instance;
}
// If this function causes a vuln, it's your fault
// never pass properties which shouldn't be
// altered by the user
public function mapRequest($object, $props) {
if(!class_exists($object))
return false;
if(!is_array($props))
return false;
$return = new $object;
foreach($props as $prop) {
if(!empty($_GET[$prop]))
$return->$prop = $_GET[$prop];
elseif(!empty($_POST[$prop]))
$return->$prop = $_POST[$prop];
}
return $return;
}
public function templateVar($name, $val=null) {
if(strpos($name, ' ') !== false)
return false;
if(!is_null($val))
$this->templateVariables[$name] = $val;
return array_key_exists($name, $this->templateVariables) ? $this->templateVariables[$name] : false;
}
public function config($name, $val=null, $group=null) {
if(empty($val)) {
if(in_array($name, $this->configCache))
$d = $this->configCache[$name];
elseif(!$d = Database::quickColumn('SELECT config_val FROM ' . WCDDL_DB_PREFIX . 'config WHERE config_name = ?', array($name)))
return false;
return $d;
}
if(!is_string($val)) $val = serialize($val);
if(!$d = Database::quickColumn('SELECT config_name FROM ' . WCDDL_DB_PREFIX . 'config WHERE config_name = ?', array($name)))
$save = Database::quickExecute('INSERT INTO ' . WCDDL_DB_PREFIX . 'config (config_name, config_val, config_group) VALUES (?, ?, ?)', array(
$name, $val, empty($group) ? 'misc' : $group));
else {
if(empty($group))
$save = Database::quickExecute('UPDATE ' . WCDDL_DB_PREFIX . 'config SET config_val = ? WHERE config_name = ?', array($val, $d));
else
$save = Database::quickExecute('UPDATE ' . WCDDL_DB_PREFIX . 'config SET config_val = ?, config_group = ? WHERE config_name = ?', array($val, $group, $d));
}
$this->configCache[$name] = $val;
return $save;
}
public function parseConfig($name) {
if(!$c = $this->config($name))
return false;
return unserialize($c);
}
public function configGroup($group) {
return Database::quickRow('SELECT config_name, config_val FROM ' . WCDDL_DB_PREFIX . 'config WHERE config_group = ?', array($group));
}
public function configDelete($name) {
unset($this->configCache[$name]);
return Database::quickExecute('DELETE FROM ' . WCDDL_DB_PREFIX . 'config WHERE config_name = ?', array($name));
}
public function executeHook($name, $data=null) {
if(empty($this->hookList[$name]))
return is_null($data) ? true : $data;
foreach($this->hookList[$name] as $h) {
if(!is_array($h) && is_callable($h))
call_user_func_array($h, !is_array($data) ? array() : $data);
elseif(is_array($h) && (is_object($h[0]) || class_exists((string) $h[0]))) {
$c = is_object($h[0]) ? $h[0] : new $h[0];
if(method_exists($c, $h[1]))
call_user_func_array(array($c, $h[1]), !is_array($data) ? array() : $data);
}
}
return is_null($data) ? true : $data;
}
public function hook($name, $func) {
if(!is_array($func) && !is_callable($func) && str_word_count($func) != 1)
return false;
if(!array_key_exists($name, $this->hookList))
$this->hookList[$name] = array();
if(!is_array($func))
$this->hookList[$name][] = $func;
elseif(is_array($func) && count($func) == 2)
$this->hookList[$name][] = $func;
else
return false;
return true;
}
public function getModules() {
$return = array();
if(!defined('WCDDL_PATH_MODULES'))
return $return;
$path = WCDDL_PATH_MODULES;
if(!file_exists($path) || !is_dir($path))
return $return;
if($dir = opendir($path)) {
while(($file = readdir($dir)) !== false) {
if(substr($file, 0, 6) == "wcddl_" && strrchr($file, ".") == ".php")
$return[] = array(
'file' => basename($file),
'path' => $path . basename($file)
);
}
closedir($dir);
}
Core::load()->executeHook('CoreGetModules', array(&$return));
return $return;
}
}
class Downloads {
public $page = 1;
public $perPage = 30;
public $query;
public $type;
public $numRows = 0;
public $maxPages = 0;
public $order = 'id DESC';
public $siteInfo = false;
public $optimisePagination = 0;
public $queue = false;
public function get() {
// Im paranoid like this
// OTT security but whatever
if(!empty($this->order)) $this->order = preg_replace('#[^\w ]+#', '', $this->order);
$this->perPage = intval($this->perPage);
$this->page = intval($this->page);
if(!empty($this->type) && defined('WCDDL_TYPES') && $allowableTypes = explode(',', WCDDL_TYPES))
if(is_array($allowableTypes) && !in_array($this->type, $allowableTypes))
$this->type = null;
Core::load()->executeHook('DownloadsPreGet', array(&$this));
// End paranoia
$return = array();
if($this->queue) {
if(!$this->siteInfo)
$sql = 'SELECT d.id, d.title, d.type, d.url FROM ' . WCDDL_DB_PREFIX . 'queue d';
else
$sql = 'SELECT d.id, d.title, d.type, d.url, s.url AS site_url, s.name AS site_name
FROM ' . WCDDL_DB_PREFIX . 'queue d LEFT JOIN ' . WCDDL_DB_PREFIX . 'sites s ON (s.id = d.sid)';
} else {
if(!$this->siteInfo)
$sql = 'SELECT d.id, d.title, d.type, d.url, d.time_added, d.views FROM ' . WCDDL_DB_PREFIX . 'downloads d';
else
$sql = 'SELECT d.id, d.title, d.type, d.url, d.time_added, d.views, s.url as site_url, s.name as site_name
FROM ' . WCDDL_DB_PREFIX . 'downloads d LEFT JOIN ' . WCDDL_DB_PREFIX . 'sites s ON (s.id = d.sid)';
}
Core::load()->executeHook('DownloadsGetQuery', array(&$sql));
$where = '';
$whereParams = array();
if(!empty($this->query) && !$this->queue) {
$this->logQuery();
$where .= (empty($where) ? ' WHERE' : ' AND') . ' MATCH(d.title) AGAINST(:query)';
$whereParams['query'] = $this->query;
}
if(!empty($this->type)) {
$where .= (empty($where) ? ' WHERE' : ' AND') . ' d.type = :type';
$whereParams['type'] = $this->type;
}
Core::load()->executeHook('DownloadsGetWhere', array(&$where, &$whereParams));
if(!$this->optimisePagination) {
$this->numRows = Database::quickColumn('SELECT COUNT(*) FROM ' . WCDDL_DB_PREFIX . ($this->queue ? 'queue' : 'downloads') . ' d' . $where, $whereParams);
$this->maxPages = ceil($this->numRows/$this->perPage);
}
$rows = $sql . $where . (!empty($this->order) ? ' ORDER BY d.' . $this->order : '');
$rows .= ' LIMIT ' . (($this->page-1)*$this->perPage) . ', ' . ($this->optimisePagination ? $this->perPage+1 : $this->perPage);
Core::load()->executeHook('DownloadsGetFullQuery', array(&$rows));
if(!$rows = Database::quickRowObjects('Download', $rows, $whereParams))
return $return;
if($this->optimisePagination) {
$this->numRows = count($rows);
$this->maxPages = count($rows) == ($this->perPage+1) ? $this->page+1 : $this->page;
array_pop($rows);
}
$return = array_merge($return, $rows);
Core::load()->executeHook('DownloadsGetRows', array(&$rows));
Core::load()->executeHook('DownloadsPostGet', array(&$this));
return $rows;
}
public function logQuery() {
if(empty($this->query))
return false;
Core::load()->executeHook('LogQueryPre', array(&$this->query));
if($i = Database::quickColumn('SELECT id FROM ' . WCDDL_DB_PREFIX . 'recents WHERE query = ?', array($this->query)))
return Database::quickExecute('UPDATE ' . WCDDL_DB_PREFIX . 'recents SET searches=searches+1 WHERE id = ?', array($i));
return Database::quickExecute('INSERT INTO ' . WCDDL_DB_PREFIX . 'recents (query) VALUES (?)', array($this->query));
}
public function pages($map, $html=true) {
return Common::pages($this, $map, $html);
}
// Lets make this one static
public static function showQueries($i=50) {
$q = Database::quickRowObjects('DownloadQuery', 'SELECT query, searches FROM ' . WCDDL_DB_PREFIX . 'recents ORDER BY id DESC LIMIT ' . intval($i));
Core::load()->executeHook('ShowQueriesPost', array(&$q));
return is_array($q) ? $q : array();
}
}
class DownloadQuery {
public $query;
public $searches = 1;
public static $outputPattern = 'index.php?q=#queryurl#';
public function url() {
return Common::formatURL($this->query);
}
public function increment() {
return Database::quickExecute('UPDATE ' . WCDDL_DB_PREFIX . 'recents SET searches=searches+1 WHERE query = ?', array($this->query));
}
public function __toString() {
if(empty(self::$outputPattern))
return htmlspecialchars($this->query);
return str_replace(array('#query#', '#queryurl#'), array(htmlspecialchars($this->query), $this->url()), self::$outputPattern);
}
}
class Common {
public static function formatURL($s, $sep='-') {
$s = preg_replace('#[\W]+#', $sep, $s);
$s = trim($s, $sep);
$s = strtolower($s);
Core::load()->executeHook('CommonFormatUrl', array(&$s));
return $s;
}
public static function isEmail($e) {
Core::load()->executeHook('CommonIsEmail', array(&$e));
if(empty($e)) return false;
return filter_var($e, FILTER_VALIDATE_EMAIL);
}
public static function isUrl($u) {
Core::load()->executeHook('CommonIsUrl', array(&$u));
if(empty($u)) return false;
return filter_var($u, FILTER_VALIDATE_URL);
}
public static function urlHost($u) {
if(!self::isUrl($u))
return false;
$p = strtolower(parse_url($u, PHP_URL_HOST));
if(substr($p, 0, 4) == 'www.')
$p = substr($p, 4);
Core::load()->executeHook('CommonUrlHost', array(&$p));
return $p;
}
public static function displayStr($s) {
Core::load()->executeHook('CommonDisplayStr', array(&$s));
return htmlspecialchars($s);
}
public static function isAjaxRequest() {
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
return true;
return false;
}
public static function arrayMergeUnique($a, $b) {
foreach($b as $entry)
if(!in_array($entry, $a))
$a[] = $entry;
return $a;
}
public static function pages($instance, $map, $html=true) {
if(!is_array($map))
return false;
Core::load()->executeHook('PagesPre', array(&$map));
foreach($map as $m) {
if(count($m) !== 2)
continue;
if(!is_array($m[0]))
$m[0] = array($m[0]);
if($m[0] == array('default'))
$defaultURL = $m;
$found = 0;
foreach($m[0] as $prop) {
if(empty($instance->$prop))
continue;
$found++;
}
if($found !== count($m[0]))
continue;
$finalURL = $m;
break;
}
if(empty($finalURL) && !empty($defaultURL))
$finalURL = $defaultURL;
if(!empty($finalURL)) {
foreach($finalURL[0] as $prop)
if($prop != 'default')
$finalURL[1] = str_replace('#' . $prop . '#', $instance->$prop, $finalURL[1]);
$result = array();
for($i = ($instance->page-10); $i <= ($instance->page+10); $i++) {
if($i > 0 && $i <= $instance->maxPages)
$result[] = str_replace('#page#', $i, $finalURL[1]);
}
Core::load()->executeHook('PagesPost', array(&$result));
return $html ? implode(", ", $result) : $result;
}
return false;
}
}
class Download {
public $id;
// Updateable ones
public $title, $type, $url, $sid;
// Static ones
public $time_added, $views;
// Site stuff
public $site_name, $site_url;
public function __construct() {
}
public function queue() {
Core::load()->executeHook('DownloadQueuePre', array(&$this));
$id = array(
'sid' => $this->sid,
'title' => $this->title,
'type' => $this->type,
'url' => $this->url,
);
Core::load()->executeHook('DownloadQueueInsert', array(&$id));
$this->id = Database::insert(WCDDL_DB_PREFIX . 'queue', $id, true);
return $this->id;
}
public function deQueue() {
if(empty($this->id))
return false;
Core::load()->executeHook('DownloadDeQueuePre', array(&$this));
$dq = array(
'id' => $this->id,
);
Core::load()->executeHook('DownloadDeQueueDelete', array(&$id));
$dq = Database::delete(WCDDL_DB_PREFIX . 'queue', $dq);
$this->id = null;
return $dq;
}
public function delete() {
if(empty($this->id))
return false;
Core::load()->executeHook('DownloadDeletePre', array(&$this));
$d = array(
'id' => $this->id,
);
Core::load()->executeHook('DownloadDeleteDelete', array(&$d));
$d = Database::delete(WCDDL_DB_PREFIX . 'downloads', $d);
$this->id = null;
return $d;
}
public function save() {
$params = array(
'sid' => $this->sid,
'title' => $this->title,
'type' => $this->type,
'url' => $this->url,
'time_added' => new DatabaseRaw('NOW()'),
);
if(!empty($this->id)) {
unset($params['time_added']);
$criteria = array(
'id' => $this->id,
);
Core::load()->executeHook('DownloadSaveUpdate', array(&$params, &$criteria));
Database::update(WCDDL_DB_PREFIX . 'downloads', $params, $criteria);
} else {
Core::load()->executeHook('DownloadSaveInsert', array(&$params));
$this->id = Database::insert(WCDDL_DB_PREFIX . 'downloads', $params, true);
}
return $this->id;
}
public function addView() {
if(empty($this->id))
return false;
Core::load()->executeHook('DownloadAddView', array(&$this));
return Database::quickExecute('UPDATE ' . WCDDL_DB_PREFIX . 'downloads SET views=views+1 WHERE id = ?', array($this->id));
}
public function showTitle() {
if(empty($this->title))
return '';
$t = $this->title;
Core::load()->executeHook('DownloadShowTitle', array(&$t));
$t = Common::displayStr($t);
Core::load()->executeHook('DownloadShowTitlePost', array(&$t));
return $t;
}
// Static methods
public static function getQueue($i, $siteInfo=false) {
if(!self::existsByID($i, true))
return false;
if(!$siteInfo)
$query = 'SELECT * FROM ' . WCDDL_DB_PREFIX . 'queue WHERE id = ?';
else
$query = 'SELECT q.*, s.name AS site_name, s.url AS site_url FROM ' . WCDDL_DB_PREFIX . 'queue q
LEFT JOIN ' . WCDDL_DB_PREFIX . 'sites s ON (s.id = q.sid) WHERE q.id = ?';
return Database::quickRowObject('Download', $query, array($i));
}
public static function get($i, $siteInfo=false) {
if(!self::existsByID($i))
return false;
if(!$siteInfo)
$query = 'SELECT * FROM ' . WCDDL_DB_PREFIX . 'downloads WHERE id = ?';
else
$query = 'SELECT d.*, s.name AS site_name, s.url AS site_url FROM ' . WCDDL_DB_PREFIX . 'downloads d
LEFT JOIN ' . WCDDL_DB_PREFIX . 'sites s ON (s.id = d.sid) WHERE d.id = ?';
return Database::quickRowObject('Download', $query, array($i));
}
public static function existsByID($i, $queue=false) {
if(Database::quickColumn('SELECT id FROM ' . WCDDL_DB_PREFIX . ($queue ? 'queue' : 'downloads') . ' WHERE id = ?', array($i)))
return true;
return false;
}
}
class Submit {
public $title = array();
public $url = array();
public $type = array();
public $sname, $surl, $email;
public $error = '';
public $whitelist = false;
public $blacklist = true;
public $sid;
public function __construct() {
# The constants should really be kept apart from
# this class but ill use them here to reduce
# code in the submit page header and for
# simplicity
if(defined('WCDDL_WHITELIST') && WCDDL_WHITELIST == 1)
$this->whitelist = true;
if(defined('WCDDL_BLACKLIST') && WCDDL_BLACKLIST == 1)
$this->blacklist = true;
Core::load()->executeHook('SubmitConstruct', array(&$this));
}
public function submit() {
// Better to code this to allow multiple errors
// but im in a rush
Core::load()->executeHook('SubmitPre', array(&$this));
if(empty($this->title))
$this->error = 'No titles were set.';
elseif(empty($this->url))
$this->error = 'No URLs were provided.';
elseif(empty($this->type))
$this->error = 'No types were chosen.';
elseif(empty($this->sname))
$this->error = 'Site name was empty.';
elseif(empty($this->surl))
$this->error = 'Site URL was empty.';
elseif(empty($this->email))
$this->error = 'Email was empty.';
elseif(!common::isEmail($this->email))
$this->error = 'Invalid email provided.';
elseif(!common::isUrl($this->surl))
$this->error = 'Invalid site URL.';
else {
$down = $this->filterDownloads();
if(empty($down))
$this->error = 'No valid downloads were entered.';
}
Core::load()->executeHook('SubmitValidation', array(&$this));
if(!empty($this->error)) return false;
$shost = common::urlHost($this->surl);
if($this->whitelist && !Site::isWhitelisted($shost)) {
$this->error = 'Site URL is not whitelisted.';
return false;
}
if($this->blacklist && Site::isBlacklisted($shost)) {
$this->error = 'Site URL is blacklisted.';
return false;
}
if(!$siteID = Site::existsByURL($shost)) {
$siteID = new Site;
$siteID->name = $this->sname;
$siteID->url = $shost;
$siteID->email = $this->email;
$siteID = $siteID->save();
}
$this->sid = $siteID;
foreach($down as $d) {
Core::load()->executeHook('SubmitDownload', array(&$d));
$d->sid = $this->sid;
$d->queue();
}
Core::load()->executeHook('SubmitDownloadPost', array(&$this));
return true;
}
public function filterDownloads() {
$pass = array();
Core::load()->executeHook('SubmitFilterPre', array(&$this));
if(empty($this->title) || empty($this->url) || empty($this->type))
return $pass;
foreach($this->title as $i => $t) {
if(empty($this->url[$i]) || !common::isUrl($this->url[$i]))
continue;
if(common::urlHost($this->url[$i]) != common::urlHost($this->surl))
continue;
if(empty($this->type[$i]))
continue;
if(defined('WCDDL_TYPES') && $allowableTypes = explode(',', WCDDL_TYPES))
if(is_array($allowableTypes) && !in_array($this->type[$i], $allowableTypes))
continue;
$valid = true;
Core::load()->executeHook('SubmitFilterValidate', array(&$valid));
if(!$valid) continue;
$d = new Download;
$d->sid = $this->sid;
$d->title = $this->title[$i];
$d->url = $this->url[$i];
$d->type = $this->type[$i];
$pass[] = $d;
}
return $pass;
}
}
class Site {
public $url, $name, $email;
public $id;
// List stuff
public $page = 1;
public $maxPages = 1;
public $perPage = 20;
public function save() {
Core::load()->executeHook('SiteSavePre', array(&$this));
$query = 'INSERT INTO ' . WCDDL_DB_PREFIX . 'sites (url, name, email) VALUES (:url, :name, :email)';
$params = array(
'url' => $this->url,
'name' => $this->name,
'email' => $this->email,
);
if(!empty($this->id)) {
$criteria = array('id' => $this->id);
Core::load()->executeHook('SiteSaveUpdate', array(&$params, &$criteria));
Database::update(WCDDL_DB_PREFIX . 'sites', $params, $criteria);
} else {
Core::load()->executeHook('SiteSaveInsert', array(&$params));
$this->id = Database::insert(WCDDL_DB_PREFIX . 'sites', $params, true);
}
return $this->id;
}
public function remove() {
if(empty($this->id))
return false;
Database::quickExecute('DELETE FROM ' . WCDDL_DB_PREFIX . 'sites WHERE id = ?', array($this->id));
Database::quickExecute('DELETE FROM ' . WCDDL_DB_PREFIX . 'downloads WHERE sid = ?', array($this->id));
return true;
}
public function getMany() {
if(empty($this->page))
$this->page = 1;
Core::load()->executeHook('SitesGetPre', array(&$this));
$query = 'SELECT id, url, name, email FROM ' . WCDDL_DB_PREFIX . 'sites ORDER BY name ASC';
$query .= ' LIMIT ' . (($this->page-1)*$this->perPage) . ', ' . ($this->perPage+1);
Core::load()->executeHook('SitesGet', array(&$query));
$rows = Database::quickRowObjects('Site', $query);
$this->maxPages = $this->page;
if(is_array($rows) && count($rows) == ($this->perPage+1)) {
$this->maxPages++;
array_pop($rows);
}
return $rows;
}
public function getList($wb='white') {
if(empty($this->page))
$this->page = 1;
Core::load()->executeHook('SiteGetListPre', array(&$this));
if($wb == 'white')
$query = 'SELECT url FROM ' . WCDDL_DB_PREFIX . 'whitelist';
else
$query = 'SELECT url, reason FROM ' . WCDDL_DB_PREFIX . 'blacklist';
$query .= ' LIMIT ' . (($this->page-1)*$this->perPage) . ', ' . ($this->perPage+1);
Core::load()->executeHook('SiteGetList', array(&$query));
$rows = Database::quickRows($query);
$this->maxPages = $this->page;
if(is_array($rows) && count($rows) == ($this->perPage+1)) {
$this->maxPages++;
array_pop($rows);
}
return $rows;
}
public static function whitelist($url) {
if(Common::isUrl($url))
$url = Common::urlHost($url);
Core::load()->executeHook('SiteWhitelist', array(&$url));
return Database::quickExecute('INSERT IGNORE INTO ' . WCDDL_DB_PREFIX . 'whitelist (url) VALUES (?)', array($url));
}
public static function whitelistRemove($url) {
if(Common::isUrl($url))
$url = Common::urlHost($url);
Core::load()->executeHook('SiteWhitelistRemove', array(&$url));
return Database::quickExecute('DELETE FROM ' . WCDDL_DB_PREFIX . 'whitelist WHERE url = ?', array($url));
}
public static function blacklistRemove($url) {
if(Common::isUrl($url))
$url = Common::urlHost($url);
Core::load()->executeHook('SiteBlacklistRemove', array(&$url));
return Database::quickExecute('DELETE FROM ' . WCDDL_DB_PREFIX . 'blacklist WHERE url = ?', array($url));
}
public static function blacklist($url, $reason=null, $removeDownloads=false) {
if(Common::isUrl($url))
$url = Common::urlHost($url);
Core::load()->executeHook('SiteBlacklist', array(&$url));
if($removeDownloads && $sid = self::existsByUrl($url))
Database::quickExecute('DELETE FROM ' . WCDDL_DB_PREFIX . 'downloads WHERE sid = ?', array($sid));
return Database::quickExecute('INSERT IGNORE INTO ' . WCDDL_DB_PREFIX . 'blacklist (url, reason) VALUES (?, ?)', array($url, $reason));
}
public static function isWhitelisted($url) {
if(Common::isUrl($url))
$url = Common::urlHost($url);
Core::load()->executeHook('SiteIsWhitelisted', array(&$url));
return Database::quickColumn('SELECT url FROM ' . WCDDL_DB_PREFIX . 'whitelist WHERE url = ?', array($url));
}
public static function isBlacklisted($url) {
if(Common::isUrl($url))
$url = Common::urlHost($url);
Core::load()->executeHook('SiteIsBlacklisted', array(&$url));
return Database::quickColumn('SELECT url FROM ' . WCDDL_DB_PREFIX . 'blacklist WHERE url = ?', array($url));
}
public static function existsByID($i) {
return Database::quickColumn('SELECT id FROM ' . WCDDL_DB_PREFIX . 'sites WHERE id = ?', array($i));
}
public static function existsByURL($u) {
if(Common::isUrl($u))
$u = Common::urlHost($u);
return Database::quickColumn('SELECT id FROM ' . WCDDL_DB_PREFIX . 'sites WHERE url = ?', array($u));
}
public static function get($i) {
if(!self::existsByID($i))
return false;
return Database::quickRowObject('Site', 'SELECT * FROM ' . WCDDL_DB_PREFIX . 'sites WHERE id = ?', array($i));
}
}
// This class outputs HTML in some methods
// Goes against my personal style of coding but
// was needed to keep the admin page as a single file
// while keeping it clean.
class Admin {
private static $instance;
public $goMethods = array(
'queue',
'downloads',
'downloadsAdd',
'downloadsEdit',
'whitelist',
'blacklist',
'config',
'configAdd',
'configEdit',
'modules',
'sites',
'sitesAdd'
);
public static function load() {
if(empty(self::$instance))
self::$instance = new Admin;
return self::$instance;
}
public function init() {
Core::load()->executeHook('AdminInit', array(&$this));
}
public function handleContent() {
if(empty($_REQUEST['go']))
return false;
$go = $_REQUEST['go'];
Core::load()->executeHook('AdminHandleContent', array(&$go));
if(in_array($go, $this->goMethods))
call_user_func(array($this, $go));
}
private function queue() {
$downloads = Core::load()->mapRequest('Downloads', array('page', 'type'));
$downloads->siteInfo = true;
$downloads->queue = true;
if(!empty($_POST['rows'])) {
if(isset($_POST['decline']))
$this->queueDecline($_POST['rows']);
elseif(isset($_POST['accept']))
$this->queueAccept($_POST['rows']);
}
echo '<form action="?go=queue" method="post">';
echo '<table width="100%">
<tr>
<th> </th>
<th>Download</th>
<th>Type</th>
<th>Provider</th>
</tr>';
foreach($downloads->get() as $d)
echo '<tr>
<td><input type="checkbox" name="rows[]" value="' . $d->id . '" /></td>
<td><a href="' . $d->url . '" target="_blank">' . $d->showTitle() . '</a></td>
<td>' . Common::displayStr($d->type) . '</td>
<td><a href="http://' . urlencode($d->site_url) . '/" target="_blank">' . Common::displayStr($d->site_name) . '</a></td>
</tr>';
echo '</table>';
echo $downloads->pages(array(
array('default', '<a href="?go=queue&page=#page#">#page#</a>')
));
echo '<br /><input type="submit" name="decline" value="Decline" />
<input type="submit" name="accept" value="Accept" />';
echo '</form>';
}
private function queueDecline($rows) {
if(empty($rows))
return false;
elseif(!is_array($rows))
return false;
else {
foreach($rows as $row) {
$download = new Download;
$download->id = $row;
$download->deQueue();
}
}
return true;
}
private function queueAccept($rows) {
if(empty($rows) || !is_array($rows))
return false;
else {
foreach($rows as $row) {
if(!$download = Download::getQueue($row))
continue;
$download->deQueue();
$download->save();
}
}
return true;
}
private function downloads() {
$downloads = Core::load()->mapRequest('Downloads', array('page', 'type'));
$downloads->siteInfo = true;
if(!empty($_POST['rows'])) {
if(isset($_POST['delete']))
$this->downloadsDelete($_POST['rows']);
}
echo '<form action="?go=downloads" method="post">';
echo '<table width="100%">
<tr>
<th> </th>
<th>Download</th>
<th>Type</th>
<th>Provider</th>
</tr>';
foreach($downloads->get() as $d)
echo '<tr>
<td>
<input type="checkbox" name="rows[]" value="' . $d->id . '" />
(<a href="?go=downloadsEdit&id=' . $d->id . '">edit</a>)
</td>
<td><a href="' . $d->url . '" target="_blank">' . $d->showTitle() . '</a></td>
<td>' . Common::displayStr($d->type) . '</td>
<td><a href="http://' . urlencode($d->site_url) . '/" target="_blank">' . Common::displayStr($d->site_name) . '</a></td>
</tr>';
echo '</table>';
echo $downloads->pages(array(
array('default', '<a href="?go=downloads&page=#page#">#page#</a>')
));
echo '<br /><input type="submit" name="delete" value="Delete" />';
echo '</form>';
}