chiark / gitweb /
Fix browser search: switch back to page 1 when submitting new query
[fdroidserver.git] / wp-fdroid / wp-fdroid.php
1 <?php
2 /*
3 Plugin Name: WP FDroid
4 Plugin URI: http://f-droid.org/repository
5 Description: An FDroid repository browser
6 Author: Ciaran Gultnieks
7 Version: 0.01
8 Author URI: http://ciarang.com
9
10 Revision history
11 0.01 - 2010-12-04: Initial development version
12
13 */
14
15 include('android-permissions.php');
16
17 class FDroid
18 {
19
20         // Our text domain, for internationalisation
21         private $textdom='wp-fdroid';
22
23         private $site_path;
24
25         // Constructor
26         function FDroid() {
27                 // Add filters etc here!
28                 add_shortcode('fdroidrepo',array($this, 'do_shortcode'));
29                 add_filter('query_vars',array($this, 'queryvars'));
30                 $this->inited=false;
31                 $this->site_path=getenv('DOCUMENT_ROOT');
32                 wp_register_sidebar_widget('fdroid_latest', 'FDroid Latest', 'widget_fdroidlatest');
33         }
34
35
36         // Register additional query variables. (Handler for the 'query_vars' filter)
37         function queryvars($qvars) {
38                 $qvars[]='fdfilter';
39                 $qvars[]='fdcategory';
40                 $qvars[]='fdid';
41                 $qvars[]='fdpage';
42                 $qvars[]='fdstyle';
43                 return $qvars;
44         }
45
46
47         // Lazy initialise. All non-trivial members should call this before doing anything else.
48         function lazyinit() {
49                 if(!$this->inited) {
50                         load_plugin_textdomain($this->textdom, PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__)));
51
52                         $this->inited=true;
53                 }
54         }
55
56         // Gets a required query parameter by name.
57         function getrequiredparam($name) {
58                 global $wp_query;
59                 if(!isset($wp_query->query_vars[$name]))
60                         wp_die("Missing parameter ".$name,"Error");
61                 return $wp_query->query_vars[$name];
62         }
63
64         // Handler for the 'fdroidrepo' shortcode.
65         //  $attribs - shortcode attributes
66         //  $content - optional content enclosed between the starting and
67         //                       ending shortcode
68         // Returns the generated content.
69         function do_shortcode($attribs,$content=null) {
70                 global $wp_query,$wp_rewrite;
71                 $this->lazyinit();
72
73                 // Init local query vars
74                 foreach($this->queryvars(array()) as $qv) {
75                         if(array_key_exists($qv,$wp_query->query_vars)) {
76                                 $query_vars[$qv] = $wp_query->query_vars[$qv];
77                         } else {
78                                 $query_vars[$qv] = null;
79                         }
80                 }
81
82                 // Santiy check query vars
83                 if(!isset($query_vars['fdpage']) || !is_numeric($query_vars['fdpage']) || $query_vars['fdpage'] <= 0) {
84                         $query_vars['fdpage'] = 1;
85                 }
86
87                 $out = '';
88
89                 if(isset($attribs['search']) && $query_vars['fdfilter']===null) {
90                         $query_vars['fdfilter'] = '';
91                 }
92
93                 if($query_vars['fdcategory'] == 'All categories') {
94                         unset($query_vars['fdcategory']);
95                 }
96
97                 if($query_vars['fdid']!==null) {
98                         $out.=$this->get_app($query_vars);
99                 } else {
100                         $out.='<form name="searchform" action="" method="get">';
101                         $out.='<p><input name="fdfilter" type="text" value="'.sanitize_text_field($query_vars['fdfilter']).'" size="30"> ';
102                         $out.='<input type="hidden" name="fdpage" value="1">';
103                         $out.='<input type="submit" value="Search"></p>';
104                         $out.=$this->makeformdata($query_vars);
105                         $out.='</form>'."\n";
106
107                         $out.=$this->get_apps($query_vars);
108                 }
109
110                 return $out;
111         }
112
113
114         // Get a URL for a full description of a license, as given by one of our
115         // pre-defined license abbreviations. This is a temporary function, as this
116         // needs to be data-driven so the same information can be used by the client,
117         // the web site and the documentation.
118         function getlicenseurl($license) {
119             switch($license) {
120                     case 'MIT':
121                         return 'http://www.gnu.org/licenses/license-list.html#X11License';
122                     case 'NewBSD':
123                         return 'http://www.gnu.org/licenses/license-list.html#ModifiedBSD';
124                     case 'BSD':
125                         return 'http://www.gnu.org/licenses/license-list.html#OriginalBSD';
126                     case 'GPLv3':
127                     case 'GPLv3+':
128                         return 'http://www.gnu.org/licenses/license-list.html#GNUGPL';
129                     case 'GPLv2':
130                     case 'GPLv2+':
131                         return 'http://www.gnu.org/licenses/license-list.html#GPLv2';
132                     case 'LGPL':
133                         return 'http://www.gnu.org/licenses/license-list.html#LGPL';
134                     case 'Apache2':
135                         return 'http://www.gnu.org/licenses/license-list.html#apache2';
136                     default:
137                         return null;
138             }
139         }
140
141         function get_app($query_vars) {
142                 global $permissions_data;
143                 $permissions_object = new AndroidPermissions($this->site_path.'/wp-content/plugins/wp-fdroid/AndroidManifest.xml',
144                         $this->site_path.'/wp-content/plugins/wp-fdroid/strings.xml',
145                         sys_get_temp_dir().'/android-permissions.cache');
146                 $permissions_data = $permissions_object->get_permissions_array();
147
148                 // Get app data
149                 $xml = simplexml_load_file($this->site_path.'/repo/index.xml');
150                 foreach($xml->children() as $app) {
151
152                         $attrs=$app->attributes();
153                         if($attrs['id']==$query_vars['fdid']) {
154                                 $apks=array();;
155                                 foreach($app->children() as $el) {
156                                         switch($el->getName()) {
157                                                 case "name":
158                                                         $name=$el;
159                                                         break;
160                                                 case "added":
161                                                         $added=$el;
162                                                         break;
163                                                 case "icon":
164                                                         $icon=$el;
165                                                         break;
166                                                 case "summary":
167                                                         $summary=$el;
168                                                         break;
169                                                 case "desc":
170                                                         $desc=$el;
171                                                         break;
172                                                 case "license":
173                                                         $license=$el;
174                                                         break;
175                                                 case "source":
176                                                         $source=$el;
177                                                         break;
178                                                 case "tracker":
179                                                         $issues=$el;
180                                                         break;
181                                                 case "donate":
182                                                         $donate=$el;
183                                                         break;
184                                                 case "web":
185                                                         $web=$el;
186                                                         break;
187                                                 case "antifeatures";
188                                                         $antifeatures=$el;
189                                                         break;
190                                                 case "requirements";
191                                                         $requirements=$el;
192                                                         break;
193                                                 case "package":
194                                                         $thisapk=array();
195                                                         foreach($el->children() as $pel) {
196                                                                 switch($pel->getName()) {
197                                                                 case "version":
198                                                                         $thisapk['version']=$pel;
199                                                                         break;
200                                                                 case "vercode":
201                                                                         $thisapk['vercode']=$pel;
202                                                                         break;
203                                                                 case "apkname":
204                                                                         $thisapk['apkname']=$pel;
205                                                                         break;
206                                                                 case "srcname":
207                                                                         $thisapk['srcname']=$pel;
208                                                                         break;
209                                                                 case "hash":
210                                                                         $thisapk['hash']=$pel;
211                                                                         break;
212                                                                 case "size":
213                                                                         $thisapk['size']=$pel;
214                                                                         break;
215                                                                 case "sdkver":
216                                                                         $thisapk['sdkver']=$pel;
217                                                                         break;
218                                                                 case "permissions":
219                                                                         $thisapk['permissions']=$pel;
220                                                                         break;
221                                                                 }
222                                                         }
223                                                         $apks[]=$thisapk;
224
225                                         }
226                                 }
227
228                                 // Generate app diff data
229                                 foreach(array_reverse($apks, true) as $key=>$apk) {
230                                         if(isset($previous)) {
231                                                 // Apk size
232                                                 $apks[$key]['diff']['size'] = $apk['size']-$previous['size'];
233                                         }
234
235                                         // Permissions
236                                         $permissions = explode(',',$apk['permissions']);
237                                         $permissionsPrevious = isset($previous['permissions'])?explode(',',$previous['permissions']):array();
238                                         $apks[$key]['diff']['permissions']['added'] = array_diff($permissions, $permissionsPrevious);
239                                         $apks[$key]['diff']['permissions']['removed'] = array_diff($permissionsPrevious, $permissions);
240
241                                         $previous = $apk;
242                                 }
243
244                                 // Output app information
245                                 $out='<div id="appheader">';
246                                 $out.='<div style="float:left;padding-right:10px;"><img src="' . site_url() . '/repo/icons/'.$icon.'" width=48></div>';
247                                 $out.='<p><span style="font-size:20px">'.$name."</span>";
248                                 $out.="<br>".$summary."</p>";
249                                 $out.="</div>";
250
251                                 $out.=str_replace('href="fdroid.app:', 'href="/repository/browse/?fdid=', $desc);
252
253                                 if(isset($antifeatures)) {
254                                         $antifeaturesArray = explode(',',$antifeatures);
255                                         foreach($antifeaturesArray as $antifeature) {
256                                                 $antifeatureDescription = $this->get_antifeature_description($antifeature);
257                                                 $out.='<p style="border:3px solid #CC0000;background-color:#FFDDDD;padding:5px;"><strong>'.$antifeatureDescription['name'].'</strong><br />';
258                                                 $out.=$antifeatureDescription['description'].' <a href="/wiki/page/Antifeature:'.$antifeature.'">more...</a></p>';
259                                         }
260                                 }
261
262                                 $out.="<p>";
263                                 $licenseurl=$this->getlicenseurl($license);
264                                 $out.="<b>License:</b> ";
265                                 if($licenseurl)
266                                     $out.='<a href="'.$licenseurl.'" target="_blank">';
267                                 $out.=$license;
268                                 if($licenseurl)
269                                     $out.='</a>';
270
271                                 if(isset($requirements)) {
272                                         $out.='<br /><b>Additional requirements:</b> '.$requirements;
273                                 }
274                                 $out.="</p>";
275
276                                 $out.="<p>";
277                                 if(strlen($web)>0)
278                                         $out.='<b>Website:</b> <a href="'.$web.'">'.$web.'</a><br />';
279                                 if(strlen($issues)>0)
280                                         $out.='<b>Issue Tracker:</b> <a href="'.$issues.'">'.$issues.'</a><br />';
281                                 if(strlen($source)>0)
282                                         $out.='<b>Source Code:</b> <a href="'.$source.'">'.$source.'</a><br />';
283                                 if($donate && strlen($donate)>0)
284                                         $out.='<b>Donate:</b> <a href="'.$donate.'">'.$donate.'</a><br />';
285                                 $out.="</p>";
286
287                                 $out.="<p>For full details and additional technical information, see ";
288                                 $out.="<a href=\"/wiki/page/".$query_vars['fdid']."\">this application's page</a> on the F-Droid wiki.</p>";
289
290                                 $out.='<script type="text/javascript">';
291                                 $out.='function showHidePermissions(id) {';
292                                 $out.='  if(document.getElementById(id).style.display==\'none\')';
293                                 $out.=' document.getElementById(id).style.display=\'block\';';
294                                 $out.='  else';
295                                 $out.=' document.getElementById(id).style.display=\'none\';';
296                                 $out.='  return false;';
297                                 $out.='}';
298                                 $out.='</script>';
299
300                                 $out.="<h3>Packages</h3>";
301
302                                 $out.='<div style="float:right; margin-left:10px;"><a id="downloadbutton" href="https://f-droid.org/FDroid.apk"><span>Download F-Droid</span></a></div>';
303                                 $out.="<p>Although APK downloads are available below to give ";
304                                 $out.="you the choice, you should be aware that by installing that way you ";
305                                 $out.="will not receive update notifications, and it's a less secure way ";
306                                 $out.="to download. ";
307                                 $out.="We recommend that you install the F-Droid client and use that.</p>";
308
309                                 $i=0;
310                                 foreach($apks as $apk) {
311                                         $first = $i+1==count($apks);
312                                         $out.="<p><b>Version ".$apk['version']."</b><br />";
313                                         $out.="Added on ".$apk['added']."<br />";
314
315                                         // Is this source or binary?
316                                         $srcbuild = isset($apk['srcname']) && file_exists($this->site_path.'/repo/'.$apk['srcname']);
317
318                                         $out.="<p>This version is built and signed by ";
319                                         if($srcbuild) {
320                                             $out.="F-Droid, and guaranteed to correspond to the source tarball below.</p>";
321                                         } else {
322                                             $out.="the original developer.</p>";
323                                         }
324                                         $out.='<a href="https://f-droid.org/repo/'.$apk['apkname'].'">download apk</a> ';
325                                         $out.=$this->human_readable_size($apk['size']);
326                                         $diffSize = $apk['diff']['size'];
327                                         if(abs($diffSize) > 500) {
328                                                 $out.=' <span style="color:#AAAAAA;">(';
329                                                 $out.=$diffSize>0?'+':'';
330                                                 $out.=$this->human_readable_size($diffSize, 1).')</span>';
331                                         }
332                                         if($srcbuild) {
333                                                 $out.='<br /><a href="https://f-droid.org/repo/'.$apk['srcname'].'">source tarball</a> ';
334                                                 $out.=$this->human_readable_size(filesize($this->site_path.'/repo/'.$apk['srcname']));
335                                         }
336
337                                         if(isset($apk['permissions'])) {
338                                                 // Permissions diff link
339                                                 if($first == false) {
340                                                         $permissionsAddedCount = count($apk['diff']['permissions']['added']);
341                                                         $permissionsRemovedCount = count($apk['diff']['permissions']['removed']);
342                                                         $divIdDiff='permissionsDiff'.$i;
343                                                         if($permissionsAddedCount || $permissionsRemovedCount) {
344                                                                 $out.='<br /><a href="javascript:void(0);" onClick="showHidePermissions(\''.$divIdDiff.'\');">permissions diff</a>';
345                                                                 $out.=' <span style="color:#AAAAAA;">(';
346                                                                 if($permissionsAddedCount)
347                                                                         $out.='+'.$permissionsAddedCount;
348                                                                 if($permissionsAddedCount && $permissionsRemovedCount)
349                                                                         $out.='/';
350                                                                 if($permissionsRemovedCount)
351                                                                         $out.='-'.$permissionsRemovedCount;
352                                                                 $out.=')</span>';
353                                                         }
354                                                         else
355                                                         {
356                                                                 $out.='<br /><span style="color:#999999;">no permission changes</span>';
357                                                         }
358                                                 }
359
360                                                 // Permissions list link
361                                                 $permissionsListString = $this->get_permission_list_string(explode(',',$apk['permissions']), $permissions_data, $summary);
362                                                 /*if($i==0)
363                                                         $divStyleDisplay='block';
364                                                 else*/
365                                                         $divStyleDisplay='none';
366                                                 $divId='permissions'.$i;
367                                                 $out.='<br /><a href="javascript:void(0);" onClick="showHidePermissions(\''.$divId.'\');">view permissions</a>';
368                                                 $out.=' <span style="color:#AAAAAA;">['.$summary.']</span>';
369                                                 $out.='<br/>';
370
371                                                 // Permissions list
372                                                 $out.='<div style="display:'.$divStyleDisplay.';" id="'.$divId.'">';
373                                                 $out.=$permissionsListString;
374                                                 $out.='</div>';
375
376                                                 // Permissions diff
377                                                 {
378                                                         $out.='<div style="display:'.$divStyleDisplay.';" id="'.$divIdDiff.'">';
379                                                         $permissionsRemoved = $apk['diff']['permissions']['removed'];
380                                                         usort($permissionsRemoved, "permissions_cmp");
381
382                                                         // Added permissions
383                                                         if($permissionsAddedCount) {
384                                                                 $out.='<h5>ADDED</h5><br />';
385                                                                 $out.=$this->get_permission_list_string($apk['diff']['permissions']['added'], $permissions_data, $summary);
386                                                         }
387
388                                                         // Removed permissions
389                                                         if($permissionsRemovedCount) {
390                                                                 $out.='<h5>REMOVED</h5><br />';
391                                                                 $out.=$this->get_permission_list_string($apk['diff']['permissions']['removed'], $permissions_data, $summary);
392                                                         }
393
394                                                         $out.='</div>';
395                                                 }
396                                         }
397                                         else {
398                                                 $out.='<br /><span style="color:#999999;">no extra permissions needed</span><br />';
399                                         }
400
401                                         $out.='</p>';
402                                         $i++;
403                                 }
404
405                                 $out.='<hr><p><a href="'.makelink($query_vars,array('fdid'=>null)).'">Index</a></p>';
406
407                                 return $out;
408                         }
409                 }
410                 return "<p>Application not found</p>";
411         }
412
413         private function get_permission_list_string($permissions, $permissions_data, &$summary) {
414                 $out='';
415                 usort($permissions, "permissions_cmp");
416                 $permission_group_last = '';
417                 foreach($permissions as $permission) {
418                         $permission_group = $permissions_data['permission'][$permission]['permissionGroup'];
419                         if($permission_group != $permission_group_last) {
420                                 $permission_group_label = $permissions_data['permission-group'][$permission_group]['label'];
421                                 if($permission_group_label=='') $permission_group_label = 'Extra/Custom';
422                                 $out.='<strong>'.strtoupper($permission_group_label).'</strong><br/>';
423                                 $permission_group_last = $permission_group;
424                         }
425
426                         $out.=$this->get_permission_protection_level_icon($permissions_data['permission'][$permission]['protectionLevel']).' ';
427                         $out.='<strong>'.$permissions_data['permission'][$permission]['label'].'</strong> [<code>'.$permission.'</code>]<br/>';
428                         if($permissions_data['permission'][$permission]['description']) $out.=$permissions_data['permission'][$permission]['description'].'<br/>';
429                         //$out.=$permissions_data['permission'][$permission]['comment'].'<br/>';
430                         $out.='<br/>';
431
432                         if(!isset($summaryCount[$permissions_data['permission'][$permission]['protectionLevel']]))
433                                 $summaryCount[$permissions_data['permission'][$permission]['protectionLevel']] = 0;
434                         $summaryCount[$permissions_data['permission'][$permission]['protectionLevel']]++;
435                 }
436
437                 $summary = '';
438                 if(isset($summaryCount)) {
439                         foreach($summaryCount as $protectionLevel => $count) {
440                                 $summary .= $this->get_permission_protection_level_icon($protectionLevel, 'regular').' '.$count;
441                                 $summary .= ', ';
442                         }
443                 }
444                 $summary = substr($summary,0,-2);
445
446                 return $out;
447         }
448
449         private function get_permission_protection_level_icon($protection_level, $size='adjusted') {
450                 $iconString = '';
451                 if($protection_level=='dangerous') {
452                         $iconString .= '<span style="color:#DD9900;';
453                         if($size=='adjusted')
454                                 $iconString .= 'font-size:150%;';
455                         $iconString .= '">&#x26a0;</span>';     // WARNING SIGN
456                 }
457                 elseif($protection_level=='normal') {
458                         $iconString .= '<span style="color:#6666FF;';
459                         if($size=='adjusted')
460                                 $iconString .= 'font-size:110%;';
461                         $iconString .= '">&#x24D8;</span>';     // CIRCLED LATIN SMALL LETTER I
462                 }
463                 elseif($protection_level=='signature') {
464                         $iconString .= '<span style="color:#33AAAA;';
465                         if($size=='adjusted')
466                                 $iconString .= 'font-size:140%;';
467                         $iconString .= '">&#x273D;</span>';     // HEAVY TEARDROP-SPOKED ASTERISK
468                 }
469                 elseif($protection_level=='signatureOrSystem') {
470                         $iconString .= '<span style="color:#DD66DD;';
471                         if($size=='adjusted')
472                                 $iconString .= 'font-size:140%;';
473                         $iconString .= '">&#x269B;</span>';     // ATOM SYMBOL
474                 }
475                 else {
476                         $iconString .= '<span style="color:#33AA33';
477                         if($size=='adjusted')
478                                 $iconString .= ';font-size:130%;';
479                         $iconString .= '">&#x2699;</span>';     // GEAR
480                 }
481
482                 return $iconString;
483         }
484
485         private function human_readable_size($size, $minDiv=0) {
486                 $si_prefix = array('bytes','kB','MB');
487                 $div = 1024;
488
489                 for($i=0;(abs($size) > $div && $i < count($si_prefix)) || $i<$minDiv;$i++) {
490                         $size /= $div;
491                 }
492
493                 return round($size,max(0,$i-1)).' '.$si_prefix[$i];
494         }
495
496         private function get_antifeature_description($antifeature) {
497                 // Anti feature names and descriptions
498                 $antifeatureDescription['Ads']['name'] = 'Advertising';
499                 $antifeatureDescription['Ads']['description'] = 'This application contains advertising.';
500                 $antifeatureDescription['Tracking']['name'] = 'Tracks You';
501                 $antifeatureDescription['Tracking']['description'] = 'This application tracks and reports your activity to somewhere.';
502                 $antifeatureDescription['NonFreeNet']['name'] = 'Non-Free Network Services';
503                 $antifeatureDescription['NonFreeNet']['description'] = 'This application promotes a non-Free network service.';
504                 $antifeatureDescription['NonFreeAdd']['name'] = 'Non-Free Addons';
505                 $antifeatureDescription['NonFreeAdd']['description'] = 'This application promotes non-Free add-ons.';
506                 $antifeatureDescription['NonFreeDep']['name'] = 'Non-Free Dependencies';
507                 $antifeatureDescription['NonFreeDep']['description'] = 'This application depends on another non-Free application.';
508                 $antifeatureDescription['UpstreamNonFree']['name'] = 'Upstream Non-Free';
509                 $antifeatureDescription['UpstreamNonFree']['description'] = 'The upstream source code is non-free.';
510
511                 if(isset($antifeatureDescription[$antifeature])) {
512                         return $antifeatureDescription[$antifeature];
513                 }
514                 return array('name'=>$antifeature);
515         }
516
517
518         function get_apps($query_vars) {
519
520                 $xml = simplexml_load_file($this->site_path."/repo/index.xml");
521                 $matches = $this->show_apps($xml,$query_vars,$numpages);
522
523                 $out='';
524
525                 if(($query_vars['fdfilter']===null || $query_vars['fdfilter']!='') && $numpages>0)
526                 {
527                         $out.='<div style="float:left;">';
528                         if($query_vars['fdfilter']===null) {
529
530                                 $categories = array('All categories');
531                                 $handle = fopen(getenv('DOCUMENT_ROOT').'/repo/categories.txt', 'r');
532                                 if ($handle) {
533                                         while (($buffer = fgets($handle, 4096)) !== false) {
534                                             $categories[] = rtrim($buffer);
535                                         }
536                                     fclose($handle);
537                                 }
538
539                                 $out.='<form name="categoryform" action="" method="get">';
540                                 $out.=$this->makeformdata($query_vars);
541
542                                 $out.='<select name="fdcategory" style="color:#333333;" onChange="document.categoryform.submit();">';
543                                 foreach($categories as $category) {
544                                         $out.='<option';
545                                         if(isset($query_vars['fdcategory']) && $category==$query_vars['fdcategory'])
546                                                 $out.=' selected';
547                                         $out.='>'.$category.'</option>';
548                                 }
549                                 $out.='</select>';
550
551                                 $out.='</form>'."\n";
552                         }
553                         else {
554                                 $out.='Applications matching "'.sanitize_text_field($query_vars['fdfilter']).'"';
555                         }
556                         $out.="</div>";
557
558                         $out.='<div style="float:right;">';
559                         $out.='<a href="'.makelink($query_vars, array('fdstyle'=>'list')).'">List</a> | ';
560                         $out.='<a href="'.makelink($query_vars, array('fdstyle'=>'grid')).'">Grid</a>';
561                         $out.='</div>';
562
563                         $out.='<br break="all"/>';
564                 }
565
566                 if($numpages>0) {
567                         $out.=$matches;
568
569                         $out.='<hr><p>';
570
571                         $out.='<div style="width:20%; float:left; text-align:left;">';
572                         $out.=' Page '.$query_vars['fdpage'].' of '.$numpages.' ';
573                         $out.='</div>';
574
575                         $out.='<div style="width:60%; float:left; text-align:center;">';
576                         if($numpages>1) {
577                                 for($i=1;$i<=$numpages;$i++) {
578                                         if($i == $query_vars['fdpage']) {
579                                                 $out.='<b>'.$i.'</b>';
580                                         } else {
581                                                 $out.='<a href="'.makelink($query_vars, array('fdpage'=>$i)).'">';
582                                                 $out.=$i;
583                                                 $out.='</a>';
584                                         }
585                                         $out.=' ';
586                                 }
587                                 $out.=' ';
588                         }
589                         $out.='</div>';
590
591                         $out.='<div style="width:20%; float:left; text-align:right;">';
592                         if($query_vars['fdpage']!=$numpages) {
593                                 $out.='<a href="'.makelink($query_vars, array('fdpage'=>($query_vars['fdpage']+1))).'">next&gt;</a> ';
594                         }
595                         $out.='</div>';
596
597                         $out.='</p>';
598                 } else if($query_vars['fdfilter']!='') {
599                         $out.='<p>No matches</p>';
600                 }
601
602                 return $out;
603         }
604
605
606         function makeformdata($query_vars) {
607
608                 $out='';
609
610                 $out.='<input type="hidden" name="page_id" value="'.(int)get_query_var('page_id').'">';
611                 foreach($query_vars as $name => $value) {
612                         if($value !== null && $name != 'fdfilter' && !($name == 'fdpage' && (int)$value ==1))
613                                 $out.='<input type="hidden" name="'.$name.'" value="'.sanitize_text_field($value).'">';
614                 }
615
616                 return $out;
617         }
618
619
620         function show_apps($xml,$query_vars,&$numpages) {
621
622                 $skipped=0;
623                 $got=0;
624                 $total=0;
625
626                 if($query_vars['fdstyle']=='grid') {
627                         $outputter = new FDOutGrid();
628                 } else {
629                         $outputter = new FDOutList();
630                 }
631
632                 $out = "";
633
634                 $out.=$outputter->outputStart();
635
636                 foreach($xml->children() as $app) {
637
638                         if($app->getName() == 'repo') continue;
639                         $appinfo['attrs']=$app->attributes();
640                         $appinfo['id']=$appinfo['attrs']['id'];
641                         foreach($app->children() as $el) {
642                                 switch($el->getName()) {
643                                         case "name":
644                                                 $appinfo['name']=$el;
645                                                 break;
646                                         case "icon":
647                                                 $appinfo['icon']=$el;
648                                                 break;
649                                         case "summary":
650                                                 $appinfo['summary']=$el;
651                                                 break;
652                                         case "desc":
653                                                 $appinfo['description']=$el;
654                                                 break;
655                                         case "license":
656                                                 $appinfo['license']=$el;
657                                                 break;
658                                         case "category":
659                                                 $appinfo['category']=$el;
660                                                 break;
661                                 }
662                         }
663
664                         if(($query_vars['fdfilter']===null || $query_vars['fdfilter']!='' && (stristr($appinfo['name'],$query_vars['fdfilter']) || stristr($appinfo['summary'],$query_vars['fdfilter']) || stristr($appinfo['description'],$query_vars['fdfilter']))) && (!isset($query_vars['fdcategory']) || $query_vars['fdcategory'] && $query_vars['fdcategory']==$appinfo['category'])) {
665                                 if($skipped<($query_vars['fdpage']-1)*$outputter->perpage) {
666                                         $skipped++;
667                                 } else if($got<$outputter->perpage) {
668                                         $out.=$outputter->outputEntry($query_vars, $appinfo);
669                                         $got++;
670                                 }
671                                 $total++;
672                         }
673
674                 }
675
676                 $out.=$outputter->outputEnd();
677
678                 $numpages = ceil((float)$total/$outputter->perpage);
679
680                 return $out;
681         }
682 }
683
684 // Class to output app entries in a detailed list format
685 class FDOutList
686 {
687         var $perpage=30;
688
689         function FDOutList() {
690         }
691
692         function outputStart() {
693                 return '';
694         }
695
696         function outputEntry($query_vars, $appinfo) {
697                 $out="";
698                 $out.='<hr style="clear:both;" />'."\n";
699                 $out.='<a href="'.makelink($query_vars, array('fdid'=>$appinfo['id'])).'">';
700                 $out.='<div id="appheader">';
701
702                 $out.='<div style="float:left;padding-right:10px;"><img src="' . site_url() . '/repo/icons/'.$appinfo['icon'].'" style="width:48px;border:none;"></div>';
703
704                 $out.='<div style="float:right;">';
705                 $out.='<p>Details...</p>';
706                 $out.="</div>\n";
707
708                 $out.='<p style="color:#000000;"><span style="font-size:20px;">'.$appinfo['name']."</span>";
709                 $out.="<br>".$appinfo['summary']."</p>\n";
710
711                 $out.="</div>\n";
712                 $out.='</a>';
713
714                 return $out;
715         }
716
717         function outputEnd() {
718                 return '';
719         }
720 }
721
722 // Class to output app entries in a compact grid format
723 class FDOutGrid
724 {
725         var $perpage=80;
726
727         var $itemCount = 0;
728
729         function FDOutGrid() {
730         }
731
732         function outputStart() {
733                 return "\n".'<table border="0" width="100%"><tr>'."\n";
734         }
735
736         function outputEntry($query_vars, $appinfo) {
737                 $link=makelink($query_vars, array('fdid'=>$appinfo['id']));
738
739                 $out='';
740
741                 if($this->itemCount%4 == 0 && $this->itemCount > 0)
742                 {
743                         $out.='</tr><tr>'."\n";
744                 }
745
746                 $out.='<td align="center" valign="top" style="background-color:#F8F8F8;">';
747                 $out.='<p>';
748                 $out.='<div id="appheader" style="text-align:center;width:110px;">';
749
750                 $out.='<a href="'.$link.'" style="border-bottom-style:none;">';
751                 $out.='<img src="' . site_url() . '/repo/icons/'.$appinfo['icon'].'" style="width:48px;border-width:0;padding-top:5px;padding-bottom:5px;"><br/>';
752                 $out.=$appinfo['name'].'<br/>';
753                 $out.='</a>';
754
755                 $out.="</div>";
756                 $out.='</p>';
757                 $out.="</td>\n";
758
759                 $this->itemCount++;
760                 return $out;
761         }
762
763         function outputEnd() {
764                 return '</tr></table>'."\n";
765         }
766 }
767
768 function permissions_cmp($a, $b) {
769         global $permissions_data;
770
771         $aProtectionLevel = $permissions_data['permission'][$a]['protectionLevel'];
772         $bProtectionLevel = $permissions_data['permission'][$b]['protectionLevel'];
773
774         if($aProtectionLevel != $bProtectionLevel) {
775                 if(strlen($aProtectionLevel)==0) return 1;
776                 if(strlen($bProtectionLevel)==0) return -1;
777
778                 return strcmp($aProtectionLevel, $bProtectionLevel);
779         }
780
781         $aGroup = $permissions_data['permission'][$a]['permissionGroup'];
782         $bGroup = $permissions_data['permission'][$b]['permissionGroup'];
783
784         if($aGroup != $bGroup) {
785                 return strcmp($aGroup, $bGroup);
786         }
787
788         return strcmp($a, $b);
789 }
790
791 // Make a link to this page, with the current query vars attached and desired params added/modified
792 function makelink($query_vars, $params=array()) {
793         $link=get_permalink();
794
795         $p = array_merge($query_vars, $params);
796
797         // Page 1 is the default, don't clutter urls with it...
798         if($p['fdpage'] == 1)
799                 unset($p['fdpage']);
800         // Likewise for list style...
801         if($p['fdstyle'] == 'list')
802                 unset($p['fdstyle']);
803
804         $vars=linkify($p);
805         if(strlen($vars)==0)
806                 return $link;
807         if(strpos($link,'?')===false)
808                 $link.='?';
809         else
810                 $link.='&';
811         return $link.$vars;
812 }
813
814 // Return the key value pairs in http-get-parameter format as a string
815 function linkify($vars) {
816         $retvar = '';
817         foreach($vars as $k => $v) {
818                 if($k!==null && $v!==null && $v!='')
819                         $retvar .= $k.'='.$v.'&';
820         }
821         return substr($retvar,0,-1);
822 }
823
824 function widget_fdroidlatest($args) {
825         extract($args);
826         echo $before_widget;
827         echo $before_title . 'Latest Apps' . $after_title;
828
829         $handle = fopen(getenv('DOCUMENT_ROOT').'/repo/latestapps.dat', 'r');
830         if ($handle) {
831                 while (($buffer = fgets($handle, 4096)) !== false) {
832                         $app = explode("\t", $buffer);
833                         echo '<a href="/repository/browse/?fdid='.$app[0].'">';
834                         if(isset($app[2]) && trim($app[2])) {
835                                 echo '<img src="' . site_url() . '/repo/icons/'.$app[2].'" style="width:32px;border:none;float:right;" />';
836                         }
837                         echo $app[1].'<br />';
838                         if(isset($app[3]) && trim($app[3])) {
839                                 echo '<span style="color:#BBBBBB;">'.$app[3].'</span>';
840                         }
841                         echo '</a><br style="clear:both;" />';
842                 }
843                 fclose($handle);
844         }
845
846         echo $after_widget;
847 }
848
849 $wp_fdroid = new FDroid();
850
851
852 ?>