This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Web.php
1375 lines (1243 loc) · 47.4 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
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
/**
* Chiara PEAR Channel Web frontend. This is a public web interface
* to a Chiara_PEAR_Server channel.
*
* PHP Version 5
*
* @category PEAR
* @package Chiara_PEAR_Server_Web
* @author Davey Shafik <[email protected]>
* @author Brett Bieber <[email protected]>
* @license New BSD
* @link http://pear.chiaraquartet.net/index.php?package=Chiara_PEAR_Server_Web
*/
/**
* Chiara::Chiara_PEAR_Server DB_DataObject Backend
*/
require_once 'Chiara/PEAR/Server/Backend/DBDataObject.php';
/**
* PEAR::HTML_QuickForm
*/
require_once 'HTML/QuickForm.php';
/**
* PEAR::Pager
*/
require_once 'Pager/Pager.php';
/**
* Chiara PEAR Channel Web frontend.
*
* Provides a frontend for PEAR channel servers akin to pearweb
*
* Code originally from Davey Shafik's Crtx_PEAR_Server_Frontend
*
* @category Web
* @package Chiara_PEAR_Server_Web
* @author Davey Shafik <[email protected]>
* @author Brett Bieber <[email protected]>
* @copyright 2004 David Shafik and Synaptic Media. All rights reserved.
* @license New BSD
* @link http://pear.chiaraquartet.net/index.php?package=Chiara_PEAR_Server_Web
*/
class Chiara_PEAR_Server_Web extends Chiara_PEAR_Server_Backend_DBDataObject
{
/**
* Filename for the file using this package
*
* @var string
*/
protected $index = '';
/**
* Filename for the file to serve RSS feeds from
*
* @var string
*/
protected $rss = '';
/**
* Filename for the Admin Interface
*
* @var string
*/
protected $admin = '';
/**
* A Quickform Instance for use in Search/E-Mail forms
*
* @var HTML_QuickForm
*/
protected $quickForm = null;
/**
* Username for admin user
*/
protected $user = false;
/**
* How many items to show per page
*
* This is used in all listings of packages, searches
* category views and the list of maintainers
*
* @var int
*/
public $per_page = 15;
/**
* Number of items to limit the RSS feed too
*
* @var int
*/
public $rss_limit = 5;
/**
* E-Mail Address from which e-mails to maintainers should be sent
*
* @var string
*/
public $mailfrom;
/**
* Chiara_PEAR_Server_Web Constructor
*
* @param string $channel Channel URI
* @param array $options An array of options.
* <code>array('database' => $DSN,
* 'index' => 'index.php',
* 'admin' => 'admin.php');</code>
*/
public function __construct($channel, $options)
{
$this->index = (isset($options['index'])) ? $options['index'] : 'index.php';
$this->admin = (isset($options['admin'])) ? $options['admin'] : 'admin.php';
$this->rss = (isset($options['rss'])) ? $options['rss'] : $this->index;
$this->quickForm = new HTML_QuickForm('channel_frontend');
parent::__construct($channel, false, $options);
$this->functions = array(
'category' => 'showCategory',
'categories' => 'showCategoryList',
'package' => 'showPackage',
'search' => 'showPackageSearchForm',
'install' => 'showInstallPage',
'faq' => 'showFaqPage',
'rss' => 'showRSS',
'maintainers' => 'showAccountList',
'user' => 'showMaintainerInfo',
'email' => 'showMaintainerEmailForm',
);
/*
Special case, catch RSS feed requests and be sure to not continue with
regular output.
*/
if (isset($_GET['rss'])) {
// header('Content-Type: application/rdf+xml');
$this->showRSS();
exit;
}
session_start();
if (isset($_SESSION['_currentUser'])) {
$this->user = $_SESSION['_currentUser'];
}
}
/**
* Run the Frontend. This handles all requests automatically
*
* @return boolean Whether or not the request is for a valid function.
*/
public function run()
{
foreach ($this->functions as $get_var => $function) {
if (isset($_REQUEST[$get_var])) {
call_user_func(array($this, $function));
return true;
}
}
return false;
}
/**
* Show Category List
*
* @return void
*/
public function showCategoryList()
{
if (defined('CHIARA_PEAR_SERVER_WEB_SHOW_EMPTY_CAT')) {
$show_empty = true;
} else {
$show_empty = false;
}
$categories = $this->listCategories();
echo "<h2>Packages</h2>";
if (sizeof($categories) > 1) {
echo '<dl>';
foreach ($categories as $cat) {
$packages = $this->getCategoryPackages($cat['id'], 4);
if ((sizeof($packages) == 0) && !$show_empty) {
continue;
}
$id = (strlen($cat['alias']) == 0) ? $cat['id'] : $cat['alias'];
echo '<dt><a href="' .$this->index. '?category=' .$id. '&page=1">' .$cat['name']. '</a></dt>';
foreach ($packages as $pkg) {
echo '<dd><a href="' .$this->index. '?package=' .$pkg['package']. '">' .$pkg['package']. '</a></dd>';
}
}
echo '</dl>';
} else {
if (sizeof($categories) == 1) {
echo $this->showPackagesNoCategory();
}
}
}
/**
* Display a Categorys Own Page with a list of its packages.
*
* @return void
*/
public function showCategory()
{
$per_page = $this->per_page;
if (!isset($_GET['category'])) {
echo '<strong>No Category to display</strong>';
return;
}
if ($_GET['category'] == "Default") {
$_GET['category'] == 0;
$cat = new stdClass();
$cat->name = "Packages";
$cat->id = 0;
} else {
$cat = DB_DataObject::factory('categories');
if (is_numeric($_GET['category'])) {
$cat->id = $_GET['category'];
} else {
$cat->alias = $_GET['category'];
}
$cat->channel = $this->_channel;
if (!$cat->find(true)) {
echo '<strong>No Category to display</strong>';
return;
}
}
$packages = DB_DataObject::factory('packages');
$packages->query('SELECT COUNT(package) AS count FROM packages WHERE category_id=' . $cat->id);
$packages->fetch();
$count = $packages->count;
$packages = DB_DataObject::factory('packages');
if ($count == 0) {
echo '<h2>' .$cat->name. '</h2>';
echo '<p><strong class="error">No Packages Found</strong></p>';
echo '<p><a href="' .$this->index. '?categories">Back to Package List</a></p>';
return;
}
if (!isset($_GET['page']) || $_GET['page'] == 1 || !is_numeric($_GET['page'])) {
$limit = $per_page;
} else {
$limit = $per_page * $_GET['page'];
}
$start = $limit - $per_page;
$packages->limit($start, $limit);
$packages->category_id = $cat->id;
$packages->channel = $this->_channel;
$packages->find();
echo '<h2>' .$cat->name. '</h2>';
echo '<table><tr><th colspan="3">';
$last_on_page = $start + $per_page;
if ($last_on_page > $count) {
$last_on_page = $count;
}
echo 'Packages ' .($start + 1). ' - ' .($last_on_page). ' of ' .$count;
echo '</th></tr>';
$pager =& Pager::factory(array(
'totalItems' => $count,
'perPage' => $per_page,
'urlVar' => 'page',
'clearIfVoid' => true,
'extraVars' => array('category' => $_GET['category']),
));
$pager_links = $pager->getLinks();
if (!empty($pager_links['all'])) {
echo '<tr>
<td colspan="3" class="pager"><p style="text-align: center;">' .$pager_links['all'].'</p></td>
</tr>';
}
echo '<tr><th>#</th><th>Package Name</th><th>Description</th></tr>';
$i = 0;
$classes = array("dark", "light");
while ($packages->fetch()) {
$class = $i % 2;
$i += 1;
echo '<tr class="'.$classes[$class].'">'
. '<td>'.$i.'</td>'
. '<td>'
. '<a href="'.$this->index.'?package='.$packages->package.'">'
.$packages->package.'</a></td>'
. '<td>'.nl2br($packages->summary).'</td>'
.'</tr>';
}
if (!empty($pager_links['all'])) {
echo '<tr>
<td colspan="3" class="pager">
<p style="text-align: center;">' .$pager_links['all'].'</p>
</td>
</tr>';
}
echo '</table>';
}
/**
* Show Packages that are not in a Category
*
* @return void
*/
public function showPackagesNoCategory()
{
$per_page = $this->per_page;
$packages = DB_DataObject::factory('packages');
$packages->query('SELECT COUNT(package) AS count FROM packages WHERE category_id=0');
$packages->fetch();
$count = $packages->count;
$packages = DB_DataObject::factory('packages');
if ($count == 0) {
echo '<p><strong class="error">No Packages Found.</strong></p>';
return;
}
if (!isset($_GET['page']) || $_GET['page'] == 1 || !is_numeric($_GET['page'])) {
$limit = $per_page;
} else {
$limit = $per_page * $_GET['page'];
}
$start = $limit - $per_page;
$packages->limit($start, $limit);
$packages->channel = $this->_channel;
$packages->category_id = 0;
$packages->find();
echo '<table><tr><th colspan="3">';
$last_on_page = $start + $per_page;
if ($last_on_page > $count) {
$last_on_page = $count;
}
echo 'Packages ' .($start + 1). ' - ' .($last_on_page). ' of ' .$count;
echo '</th></tr>';
$pager =& Pager::factory(array(
'totalItems' => $count,
'perPage' => $per_page,
'urlVar' => 'page',
'clearIfVoid' => true,
));
$pager_links = $pager->getLinks();
if (!empty($pager_links['all'])) {
echo '<tr>
<td colspan="3"><p style="text-align: center;">' .$pager_links['all'].'</p></td>
</tr>';
}
echo '<tr><th>#</th><th>Package Name</th><th>Description</th></tr>';
$i = 0;
$classes = array('dark', 'light');
while ($packages->fetch()) {
$class = $i % 2;
$i += 1;
echo '<tr class="' .$classes[$class]. '">'
. '<td>' .$i. '</td>'
. '<td>
<a href="' .$this->index. '?package=' .$packages->package. '">' .$packages->package. '</a>'
. '</td>'
. '<td>' .nl2br($packages->summary). '</td>'
.'</tr>';
}
if (!empty($pager_links['all'])) {
echo '<tr>
<td colspan="3"><p style="text-align: center;">' .$pager_links['all'].'</p></td>
</tr>';
}
echo '</table>';
}
/**
* Get packages within a single category, optionally limit to $limit packages
*
* @param string $category Category ID
* @param int $limit Amount of packages to query for
*
* @return array An array of package rows
*/
protected function getCategoryPackages($category, $limit = null)
{
$pkg = DB_DataObject::factory('packages');
$pkg->category_id = $category;
if (!is_null($limit)) {
$pkg->limit(0, $limit);
}
$pkg->channel = $this->_channel;
$pkg->orderBy('package ASC');
if (!$pkg->find()) {
return array();
}
while ($pkg->fetch()) {
$packages[] = $pkg->toArray();
}
return $packages;
}
/**
* Show a Packages page
*
* @return void
*/
public function showPackage()
{
$pkg = $this->packageInfo($_GET['package']);
$subpkg = DB_DataObject::factory('packages');
$subpkg->channel = $this->_channel;
$subpkg->parent = $_GET['package'];
$has_sub = $subpkg->find(false);
if (!$pkg) {
echo '<strong>No Package to display</strong>';
return;
}
echo '<h2>' .$pkg['package']. '</h2>';
echo '<ul id="package">';
echo '<li><a href="' .$this->index. '?package=' .$pkg['package'].'">Main</a></li>';
if (sizeof($pkg['releases']) > 0) {
echo '<li><a href="' .$this->index. '?package=' .$pkg['package']. '&downloads">Download</a></li>';
echo '<li><a href="' .$this->index. '?rss&package=' .$pkg['package']. '">RSS Feed</a></li>';
}
$this->showPackageExtras();
echo '</ul>';
if (isset($_REQUEST['downloads'])) {
$this->showPackageDownloads();
return;
}
echo '<h3>Summary</h3>';
echo '<p>' .nl2br($pkg['summary']). '</p>';
echo '<h3>License</h3>';
if (strlen($pkg['licenseuri']) != 0) {
echo '<p><a href="' .$pkg['licenseuri']. '">' .$pkg['license']. '</a></p>';
} else {
echo '<p>' .$pkg['license']. '</p>';
}
echo '<h3>Current Release</h3>';
if (sizeof($pkg['releases']) == 0) {
echo '<ul><li>No releases have been made yet</li></ul>';
} else {
echo '<ul>';
foreach ($this->getPackageLatestReleases($pkg['package']) as $state => $release) {
echo '<li><a href="http://' .$this->_channel. '/get/' .$pkg['package']. '-'. $release['version'] .'.tgz">' .$release['version']. '</a> (' .$state. ') was released on ' .$release['date']. '</li>';
}
echo '</ul>';
}
if ($pkg['summary'] != $pkg['description']) {
echo '<h3>Description</h3>';
echo '<p>' .nl2br($pkg['description']). '</p>';
}
$devs = $this->listPackageMaintainers($_GET['package']);
if (sizeof($devs) != 0) {
echo '<h3>Maintainers</h3>';
echo '<ul>';
foreach ($devs as $dev) {
$dev = $dev->toArray();
$dev['role'] = ucfirst($dev['role']);
echo "<li><a href='{$this->index}?user={$dev['handle']}'>";
echo (!empty($dev['name'])) ? $dev['name'] : $dev['handle'];
echo "</a> ({$dev['role']})</li>";
}
echo '</ul>';
}
if ($pkg['parent'] != null) {
echo '<h3>Parent Package</h3>';
echo "<p><a href='{$this->index}?package={$pkg['parent']}'>{$pkg['parent']}</a></p>";
}
if ($has_sub) {
echo '<h3>Sub-Packages</h3>';
echo '<ul>';
while ($subpkg->fetch()) {
echo "<li><a href='{$this->index}?package={$subpkg->package}'>{$subpkg->package}</a></li>";
}
echo '</ul>';
}
}
/**
* Get the latest releases for a package
*
* This function will find the highest stability release
* as well as the *latest* release (i.e. it will find a 1.0.0-stable
* release and a 1.0.1-snapshot)
*
* @param string $pkg Packaage name
*
* @return array An array of releases
*/
protected function getPackageLatestReleases($pkg)
{
$package = DB_DataObject::factory('releases');
$states = array('snapshot', 'devel', 'alpha', 'beta', 'stable');
foreach ($states as $state) {
// $package->query("SELECT version, UNIX_TIMESTAMP(releasedate) AS epoch, DATE_FORMAT(releasedate, '%M %D %Y') AS date, MAX(releasedate) FROM releases WHERE package='$pkg' AND channel='{$this->_channel}' AND state='$state' GROUP BY package");
$package->query("SELECT version, UNIX_TIMESTAMP( releasedate ) AS epoch, DATE_FORMAT( releasedate, '%M %D %Y' ) AS date, MAX( releasedate ) FROM releases WHERE package='$pkg' AND channel='{$this->_channel}' AND state='$state' GROUP BY releasedate ORDER BY releasedate DESC LIMIT 1");
while ($package->fetch()) {
$release[$state] = array('version' => $package->version, 'date' => $package->date, 'epoch' => $package->epoch);
}
}
if (sizeof($release) == 1) {
return $release;
}
$states = array_keys($release);
for ($i = 0; $i < sizeof($states); $i++) {
if (isset($states[$i+1]) && $release[$states[$i]]['epoch'] < $release[$states[$i+1]]['epoch']) {
unset($release[$states[$i]]);
}
}
return array_reverse($release);
}
/**
* Show Package Extras (CVS/Doc/Bugs Links)
*
* @return void
*/
public function showPackageExtras()
{
$package = DB_DataObject::factory('package_extras');
$package->package = $_GET['package'];
if (!$package->find(true)) {
return;
} else {
if (strlen($package->docs_uri) > 0) {
echo '<li><a href="'.htmlspecialchars($package->docs_uri).'">Documentation</a></li>';
}
if (strlen($package->bugs_uri) > 0) {
echo '<li><a href="'.htmlspecialchars($package->bugs_uri).'">Bugs</a></li>';
}
if (strlen($package->cvs_uri) > 0) {
echo '<li><a href="'.htmlspecialchars($package->cvs_uri).'">CVS</a></li>';
}
}
}
/**
* Show Package Download/Changelog page
*
* @return void
*/
public function showPackageDownloads()
{
$releases = $this->packageInfo($_GET['package']);
echo "<h3>Downloads</h3>";
if (!$releases) {
echo "<p><strong class='error'>No downloads found for '{$_GET['package']}'</strong></p>";
return;
}
echo "<table><tr><th>Version</th><th>Information</th></tr>";
foreach ($releases['releases'] as $version => $release) {
$release['releasedate'] = date('F jS Y', strtotime($release['releasedate']));
if (!isset($_GET['release'])) {
$_GET['release'] = $version;
}
if ($_GET['release'] == $version) {
echo "<tr>";
echo "<td valign='top'>$version</td>";
echo "<td>
<h4><a href='http://{$this->_channel}/get/{$_GET['package']}-{$release['version']}.tgz'>Download</a></h4>
<p>
<span style='font-weight: bold'>Release Date:</span> {$release['releasedate']}
<br />
<span style='font-weight: bold'>Release State:</span> {$release['state']}
</p>
<h4>Changelog:</h4>
<p>
" .nl2br($release['releasenotes']). "
</p>
<h4>Dependencies</h4>
<ul>";
foreach ($release['deps'] as $dep) {
$rel_trans = array('lt' => 'older than %s',
'le' => '%s or older',
'eq' => 'version %s',
'ne' => 'any version but %s',
'gt' => 'newer than %s',
'ge' => '%s or newer',
);
$dep_type_desc = array('pkg' => 'Package',
'ext' => 'PHP Extension',
'php' => 'PHP',
'prog' => 'Program',
'ldlib' => 'Development Library',
'rtlib' => 'Runtime Library',
'os' => 'Operating System',
'websrv' => 'Web Server',
'sapi' => 'SAPI Backend',
);
if (!isset($dep['name'])) {
$dep['name'] = '';
}
if ((!isset($dep['channel'])) || ($dep['type'] == 'php') || ($dep['channel'] == $this->_channel)) {
$dep['channel'] = '';
} else {
$dep['channel'] = 'from ' .$dep['channel'];
}
if (isset($rel_trans[$dep['rel']])) {
if ($dep['type'] == 'php' || $dep['type'] == 'ext') {
$dep['version'] = 'version ' .$dep['version'];
}
$rel = sprintf($rel_trans[$dep['rel']], $dep['version']);
printf("<li>%s: %s %s %s</li>", $dep_type_desc[$dep['type']], $dep['name'], $rel, $dep['channel']);
} else {
printf("<li>%s: %s %s</li>", $dep_type_desc[$dep['type']], $dep['name'], $dep['channel']);
}
if ($dep['optional'] == 1) {
echo ' (optional)';
}
}
echo " </ul>
</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td><a href='{$this->index}?package={$_GET['package']}&release=$version&downloads'>$version-{$release['state']}</a></td>";
echo "<td>{$release['releasedate']}</td>";
echo "</tr>";
}
}
echo '</table>';
}
/**
* Show the Package Search Form
*
* @return void
*/
public function showPackageSearchForm()
{
$category = DB_DataObject::factory('categories');
$handle = DB_DataObject::factory('handles');
$category->channel = $this->_channel;
$category->find();
$categories = array('-1' => '');
$categories[0] = 'No Category';
while ($category->fetch()) {
$categories[$category->id] = $category->name;
}
$handle->channel = $this->_channel;
$handle->find();
$handles = array('-1' => '');
while ($handle->fetch()) {
$handles[$handle->handle] = "{$handle->name} ({$handle->handle})";
}
echo '<h2>Search Packages</h2>';
$defaults['match'] = (!isset($_REQUEST['match'])) ? 'all' : $_REQUEST['match'];
$defaults['name'] = (!isset($_REQUEST['name'])) ? '' : $_REQUEST['name'];
$defaults['search'] = '1';
$defaults['category_id'] = (!isset($_REQUEST['category_id'])) ? '-1' : $_REQUEST['category_id'];
$defaults['maintainer'] = (!isset($_REQUEST['maintainer'])) ? '-1' : $_REQUEST['maintainer'];
$this->quickForm->setDefaults($defaults);
$this->quickForm->addElement('header', '', 'Search by Package Name');
$this->quickForm->addElement('text', 'name', 'Package Name');
$this->quickForm->addElement('hidden', 'search', 'search');
$radio[] = HTML_QuickForm::createElement('radio', 'match', '', 'Any Words', 'any');
$radio[] = HTML_QuickForm::createElement('radio', 'match', '', 'All Words', 'all');
$this->quickForm->addGroup($radio, 'match_group', 'Match', '', false);
if (sizeof($categories) > 1) {
$this->quickForm->addElement('header', '', 'Search by Category');
$this->quickForm->addElement('select', 'category_id', 'Category', $categories);
}
$this->quickForm->addElement('header', '', 'Search by Maintainer');
$this->quickForm->addElement('select', 'maintainer', 'Maintainer', $handles);
$this->quickForm->addElement('submit', 'submit', 'Search');
echo $this->quickForm->toHtml();
if (isset($_REQUEST['search']) && (strlen($this->quickForm->getSubmitValue('name')) > 0)) {
if ($this->quickForm->validate()) {
$this->showPackageSearchResults($this->quickForm->getSubmitValues(), 'package');
}
} elseif (isset($_REQUEST['search']) && (isset($_REQUEST['name'])) && (strlen($_REQUEST['name']) != 0)) {
$this->showPackageSearchResults(array('name' => $_REQUEST['name'], 'match' => $_REQUEST['match']), 'package');
} elseif (isset($_REQUEST['search']) && ($this->quickForm->getSubmitValue('maintainer') != -1)) {
if ($this->quickForm->validate()) {
$this->showPackageSearchResults($this->quickForm->getSubmitValues(), 'maintainer');
}
} elseif (isset($_REQUEST['search']) && (isset($_REQUEST['maintainer'])) && ($_REQUEST['maintainer'] != -1)) {
$this->showPackageSearchResults(array('maintainer' => $_REQUEST['maintainer']), 'maintainer');
} elseif (isset($_REQUEST['search']) && ($this->quickForm->getSubmitValue('category_id') != "-1")) {
if ($this->quickForm->validate()) {
$this->showPackageSearchResults($this->quickForm->getSubmitValues(), 'category');
}
} elseif (isset($_REQUEST['search']) && (isset($_REQUEST['category']))) {
$this->showPackageSearchResults(array('category_id' => $_REQUEST['category']), 'category');
}
}
/**
* Show Search Results
*
* @param array $search Result of the search form
* @param string $type Type of search to perform
*
* @return void
*/
public function showPackageSearchResults($search, $type)
{
$per_page = $this->per_page;
switch ($type) {
case 'package':
$package = $this->packageSearchByName($search);
$extraVars['name'] = $search['name'];
$extraVars['match'] = $search['match'];
break;
case 'maintainer':
$package = $this->packageSearchByMaintainer($search);
$extraVars['maintainer'] = $search['maintainer'];
break;
case 'category':
$package = $this->packageSearchByCategory($search);
$extraVars['category'] = $search['category_id'];
break;
default:
return;
}
$extraVars['search'] = 1;
if (!$package) {
echo '<p><strong class="error">No Results Found for ' .ucfirst($type). ' search</strong></p>';
return;
}
$package->fetch();
$count = $package->count();
if (!isset($_GET['page']) || $_GET['page'] == 1 || !is_numeric($_GET['page'])) {
$limit = $per_page;
} else {
$limit = $per_page * $_GET['page'];
}
$start = $limit - $per_page;
$package->limit($start, $limit);
if (!$package->find()) {
echo '<p><strong class="error">No Results Found for ' .ucfirst($type). ' search</strong></p>';
return;
}
while ($package->fetch()) {
$packages[$package->package] = $package->summary;
}
$last_on_page = $start + $per_page;
if ($last_on_page > $count) {
$last_on_page = $count;
}
$pager =& Pager::factory(array(
'totalItems' => $count,
'perPage' => $per_page,
'urlVar' => 'page',
'clearIfVoid' => true,
'extraVars' => $extraVars,
));
$pager_links = $pager->getLinks();
?>
<table id="search-results">
<tr><th colspan="2">Search Results (<?php echo ($start + 1). ' - ' .($last_on_page). ' of ' .$count; ?> results found)</th></tr>
<tr><th>Package</th><th>Description</th></tr>
<?php
if (!empty($pager_links['all'])) {
echo '<tr>
<td class="pager" style="text-align: center;" colspan="2">' .$pager_links['all'].'</td>
</tr>';
}
$i = 0;
$classes = array("dark", "light");
foreach ($packages as $pkg => $summary) {
$class = $i % 2;
$i += 1;
?>
<tr class="<?php echo $classes[$class]; ?>">
<td><a href="<?php echo $this->index; ?>?package=<?php echo $pkg ?>"><?php echo $pkg; ?></a></td>
<td><?php echo $summary; ?></td>
</tr>
<?php
}
?>
</table>
<?php
}
/**
* Search Packages by Name
*
* @param array $search The result array from the search forms HTML_QuickForm
*
* @return object DB_DataObject with which to perform the search
*/
protected function &packageSearchByName($search)
{
$package = DB_DataObject::factory('packages');
$package->channel = $this->_channel;
$seperator = (!isset($search['match']) || $search['match'] == 'all') ? 'AND' : 'OR';
$terms = explode(' ', $search['name']);
foreach ($terms as $key => $term) {
$terms[$key] = "package LIKE '%" .$package->escape($term). "%'";
}
$where = implode(' ' .$seperator. ' ', $terms);
$package->whereAdd($where);
$package->selectAdd();
$package->selectAdd('package, summary, COUNT(package) AS count');
$package->groupBy('package');
$package->orderBy('package');
return $package;
}
/**
* Search Packages by Category
*
* @param array $search The result array from the search forms HTML_QuickForm
*
* @return object DB_DataObject with which to perform the search
*/
protected function &packageSearchByCategory($search)
{
$package = DB_DataObject::factory('packages');
$package->channel = $this->_channel;
$package->category_id = $search['category_id'];
$package->selectAdd();
$package->selectAdd('package, summary, COUNT(package) AS count');
$package->groupBy('package');
$package->orderBy('package');
return $package;
}
/**
* Search Packages by Maintainer
*
* @param array $search The result array from the search forms HTML_QuickForm
*
* @return object DB_DataObject with which to perform the search
*/
protected function &packageSearchByMaintainer($search)
{
$maintainer = DB_DataObject::factory('maintainers');
$package = DB_DataObject::factory('packages');
$maintainer->channel = $this->_channel;
$maintainer->handle = $search['maintainer'];
if (!$maintainer->find()) {
return false;
}
$packages = array();
while ($maintainer->fetch()) {
$packages[] = "package='{$maintainer->package}'";
}
$package->channel = $this->_channel;
$where = implode(' OR ', $packages);
$package->whereAdd($where);
return $package;
}
/**
* Show list of Maintainers
*
* @return void
*/
public function showAccountList()
{
echo "<h2>Maintainers</h2>";
$maintainers = $this->listMaintainers();
if (!$maintainers) {
echo "<p><strong class='error'>No maintainers found</strong></p>";
}
?>
<table>
<tr>
<th>Name</th>
<th></th>
<th></th>
<th></th>
</tr>
<?php
$i = 0;
$classes = array("dark", "light");
foreach ($maintainers as $maintainer) {
$class = $i % 2;
$i += 1;
?>
<tr class="<?php echo $classes[$class]; ?>">
<td>
<?php echo $maintainer->name; ?>
</td>
<td>
<a href="<?php echo $this->index; ?>?user=<?php echo $maintainer->handle; ?>">More Info</a>
</td>
<td>
<a href="<?php echo $this->index; ?>?email&handle=<?php echo $maintainer->handle; ?>&list">E-Mail</a>
</td>
<td>
<?php
if (is_string($maintainer->uri)) {
if (!empty($maintainer->uri)) {
?>
<a href="<?php echo $maintainer->uri;?>">Website</a>
<?php
}
}
?>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
/**
* Show Maintainer Profile Page
*
* @return false
*/
public function showMaintainerInfo()
{
$handle = DB_DataObject::factory('handles');
$maintainer = DB_DataObject::factory('maintainers');
$handle->handle = $_GET['user'];
$handle->channel = $this->_channel;
$maintainer->handle = $_GET['user'];
$maintainer->channel = $this->_channel;
$maintainer->active = 1;
if (!$handle->find(true)) {
echo "<p><strong class='error'>Maintainer Not Found</strong></p>";
return;
}
$packages = array();
if ($maintainer->find()) {
while ($maintainer->fetch()) {
$packages[] = $maintainer->toArray();
}
}
echo "<h2>" .$handle->name. "</h2>";
?>
<ul>
<li><a href="<?php echo $this->index; ?>?email&handle=<?php echo $maintainer->handle; ?>">E-Mail</a></li>
<?php
if (is_string($handle->uri) && strlen($handle->description) > 0) {
?>
<li><a href="<?php echo $handle->uri;?>">Website</a></li>
<?php
}
if (is_string($handle->wishlist) && strlen($handle->description) > 0) {
?>