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