chiark / gitweb /
12cc02e6601d8009756617433a43f2df79014e30
[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="'.esc_attr($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                 switch ($sdkLevel) {
225                         case 23: return "6.0";
226                         case 22: return "5.1";
227                         case 21: return "5.0";
228                         case 20: return "4.4W";
229                         case 19: return "4.4";
230                         case 18: return "4.3";
231                         case 17: return "4.2";
232                         case 16: return "4.1";
233                         case 15: return "4.0.3";
234                         case 14: return "4.0";
235                         case 13: return "3.2";
236                         case 12: return "3.1";
237                         case 11: return "3.0";
238                         case 10: return "2.3.3";
239                         case 9: return "2.3";
240                         case 8: return "2.2";
241                         case 7: return "2.1";
242                         case 6: return "2.0.1";
243                         case 5: return "2.0";
244                         case 4: return "1.6";
245                         case 3: return "1.5";
246                         case 2: return "1.1";
247                         case 1: return "1.0";
248                         default: return "?";
249                 }
250         }
251
252         function get_app($query_vars) {
253                 global $permissions_data;
254                 $permissions_object = new AndroidPermissions($this->site_path.'/wp-content/plugins/wp-fdroid/AndroidManifest.xml',
255                         $this->site_path.'/wp-content/plugins/wp-fdroid/strings.xml',
256                         sys_get_temp_dir().'/android-permissions.cache');
257                 $permissions_data = $permissions_object->get_permissions_array();
258
259                 // Get app data
260                 $xml = simplexml_load_file($this->site_path.'/repo/index.xml');
261                 foreach($xml->children() as $app) {
262
263                         $attrs=$app->attributes();
264                         if($attrs['id']==$query_vars['fdid']) {
265                                 $apks=array();;
266                                 foreach($app->children() as $el) {
267                                         switch($el->getName()) {
268                                         case "name":
269                                                 $name=$el;
270                                                 break;
271                                         case "icon":
272                                                 $icon=$el;
273                                                 break;
274                                         case "summary":
275                                                 $summary=$el;
276                                                 break;
277                                         case "desc":
278                                                 $desc=$el;
279                                                 break;
280                                         case "license":
281                                                 $license=$el;
282                                                 break;
283                                         case "author":
284                                                 $author=$el;
285                                                 break;
286                                         case "email":
287                                                 $email=$el;
288                                                 break;
289                                         case "source":
290                                                 $source=$el;
291                                                 break;
292                                         case "tracker":
293                                                 $issues=$el;
294                                                 break;
295                                         case "changelog":
296                                                 $changelog=$el;
297                                                 break;
298                                         case "donate":
299                                                 $donate=$el;
300                                                 break;
301                                         case "flattr":
302                                                 $flattr=$el;
303                                                 break;
304                                         case "web":
305                                                 $web=$el;
306                                                 break;
307                                         case "antifeatures":
308                                                 $antifeatures=$el;
309                                                 break;
310                                         case "requirements":
311                                                 $requirements=$el;
312                                                 break;
313                                         case "package":
314                                                 $thisapk=array();
315                                                 foreach($el->children() as $pel) {
316                                                         switch($pel->getName()) {
317                                                         case "version":
318                                                                 $thisapk['version']=$pel;
319                                                                 break;
320                                                         case "vercode":
321                                                                 $thisapk['vercode']=$pel;
322                                                                 break;
323                                                         case "added":
324                                                                 $thisapk['added']=$pel;
325                                                                 break;
326                                                         case "apkname":
327                                                                 $thisapk['apkname']=$pel;
328                                                                 break;
329                                                         case "srcname":
330                                                                 $thisapk['srcname']=$pel;
331                                                                 break;
332                                                         case "hash":
333                                                                 $thisapk['hash']=$pel;
334                                                                 break;
335                                                         case "size":
336                                                                 $thisapk['size']=$pel;
337                                                                 break;
338                                                         case "sdkver":
339                                                                 $thisapk['sdkver']=$pel;
340                                                                 break;
341                                                         case "maxsdkver":
342                                                                 $thisapk['maxsdkver']=$pel;
343                                                                 break;
344                                                         case "nativecode":
345                                                                 $thisapk['nativecode']=$pel;
346                                                                 break;
347                                                         case "permissions":
348                                                                 $thisapk['permissions']=$pel;
349                                                                 break;
350                                                         }
351                                                 }
352                                                 $apks[]=$thisapk;
353
354                                         }
355                                 }
356
357                                 // Generate app diff data
358                                 foreach(array_reverse($apks, true) as $key=>$apk) {
359                                         if(isset($previous)) {
360                                                 // Apk size
361                                                 $apks[$key]['diff']['size'] = $apk['size']-$previous['size'];
362                                         }
363
364                                         // Permissions
365                                         $permissions = explode(',',$apk['permissions']);
366                                         $permissionsPrevious = isset($previous['permissions'])?explode(',',$previous['permissions']):array();
367                                         $apks[$key]['diff']['permissions']['added'] = array_diff($permissions, $permissionsPrevious);
368                                         $apks[$key]['diff']['permissions']['removed'] = array_diff($permissionsPrevious, $permissions);
369
370                                         $previous = $apk;
371                                 }
372
373                                 // Output app information
374                                 $out='<div id="appheader">';
375                                 $out.='<div style="float:left;padding-right:10px;"><img src="' . site_url() . '/repo/icons/'.$icon.'" width=48></div>';
376                                 $out.='<p><span style="font-size:20px">'.$name."</span>";
377                                 $out.="<br>".$summary."</p>";
378                                 $out.="</div>";
379
380                                 $out.=str_replace('href="fdroid.app:', 'href="/repository/browse/?fdid=', $desc);
381
382                                 if(isset($antifeatures)) {
383                                         $antifeaturesArray = explode(',',$antifeatures);
384                                         foreach($antifeaturesArray as $antifeature) {
385                                                 $antifeatureDescription = $this->get_antifeature_description($antifeature);
386                                                 $out.='<p style="border:3px solid #CC0000;background-color:#FFDDDD;padding:5px;"><strong>'.$antifeatureDescription['name'].'</strong><br />';
387                                                 $out.=$antifeatureDescription['description'].' <a href="/wiki/page/Antifeature:'.$antifeature.'">more...</a></p>';
388                                         }
389                                 }
390
391                                 $out.="<p>";
392                                 $licenseurl=$this->getlicenseurl($license);
393                                 $out.="<b>License:</b> ";
394                                 if($licenseurl)
395                                         $out.='<a href="'.$licenseurl.'" target="_blank">';
396                                 $out.=$license;
397                                 if($licenseurl)
398                                         $out.='</a>';
399
400                                 if(isset($requirements)) {
401                                         $out.='<br /><b>Additional requirements:</b> '.$requirements;
402                                 }
403                                 $out.="</p>";
404
405                                 $out.="<p>";
406                                 if(strlen($web)>0)
407                                         $out.='<b>Website:</b> <a href="'.$web.'">'.$web.'</a><br />';
408                                 if(isset($author) && strlen($author)>0)
409                                         if(isset($email) && strlen($email)>0)
410                                                 $out.='<b>Author(s):</b> <a href="mailto:'.$email.'">'.$author.'</a><br />';
411                                         else
412                                                 $out.='<b>Author(s):</b> '.$author.'<br />';
413                                 if(strlen($issues)>0)
414                                         $out.='<b>Issue Tracker:</b> <a href="'.$issues.'">'.$issues.'</a><br />';
415                                 if(strlen($source)>0)
416                                         $out.='<b>Source Code:</b> <a href="'.$source.'">'.$source.'</a><br />';
417                                 if(strlen($changelog)>0)
418                                         $out.='<b>Changelog:</b> <a href="'.$changelog.'">'.$changelog.'</a><br />';
419                                 if(isset($donate) && strlen($donate)>0)
420                                         $out.='<b>Donate:</b> <a href="'.$donate.'">'.$donate.'</a><br />';
421                                 if(isset($flattr) && strlen($flattr)>0)
422                                         $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 />';
423                                 $out.="</p>";
424
425                                 $out.="<p>For full details and additional technical information, see ";
426                                 $out.="<a href=\"/wiki/page/".$query_vars['fdid']."\">this application's page</a> on the F-Droid wiki.</p>";
427
428                                 $out.='<script type="text/javascript">';
429                                 $out.='function showHidePermissions(id) {';
430                                 $out.='  if(document.getElementById(id).style.display==\'none\')';
431                                 $out.=' document.getElementById(id).style.display=\'block\';';
432                                 $out.='  else';
433                                 $out.=' document.getElementById(id).style.display=\'none\';';
434                                 $out.='  return false;';
435                                 $out.='}';
436                                 $out.='</script>';
437
438                                 $out.="<h3>Packages</h3>";
439
440                                 $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>';
441                                 $out.="<p>Although APK downloads are available below to give ";
442                                 $out.="you the choice, you should be aware that by installing that way you ";
443                                 $out.="will not receive update notifications, and it's a less secure way ";
444                                 $out.="to download. ";
445                                 $out.="We recommend that you install the F-Droid client and use that.</p>";
446
447                                 $i=0;
448                                 foreach($apks as $apk) {
449                                         $first = $i+1==count($apks);
450                                         $out.="<p><b>Version ".$apk['version']."</b>";
451                                         $out.=" - Added on ".$apk['added']."<br />";
452
453                                         $hasminsdk = isset($apk['sdkver']);
454                                         $hasmaxsdk = isset($apk['maxsdkver']);
455                                         if($hasminsdk && $hasmaxsdk) {
456                                                 $out.="<p>This version requires Android ".$this->androidversion($apk['sdkver'])." up to ".$this->androidversion($apk['maxsdkver'])."</p>";
457                                         } elseif($hasminsdk) {
458                                                 $out.="<p>This version requires Android ".$this->androidversion($apk['sdkver'])." or newer.</p>";
459                                         } elseif($hasmaxsdk) {
460                                                 $out.="<p>This version requires Android ".$this->androidversion($apk['maxsdkver'])." or old.</p>";
461                                         }
462
463                                         $hasabis = isset($apk['nativecode']);
464                                         if($hasabis) {
465                                                 $abis = str_replace(',', ' ', $apk['nativecode']);
466                                                 $out.="<p>This version uses native code and is built for: ".$abis."</p>";
467                                         }
468
469                                         // Is this source or binary?
470                                         $srcbuild = isset($apk['srcname']) && file_exists($this->site_path.'/repo/'.$apk['srcname']);
471
472                                         $out.="<p>This version is built and signed by ";
473                                         if($srcbuild) {
474                                                 $out.="F-Droid, and guaranteed to correspond to the source tarball below.</p>";
475                                         } else {
476                                                 $out.="the original developer.</p>";
477                                         }
478                                         $out.='<a href="https://f-droid.org/repo/'.$apk['apkname'].'">download apk</a> ';
479                                         $out.=$this->human_readable_size($apk['size']);
480                                         $diffSize = $apk['diff']['size'];
481                                         if(abs($diffSize) > 500) {
482                                                 $out.=' <span style="color:#AAAAAA;">(';
483                                                 $out.=$diffSize>0?'+':'';
484                                                 $out.=$this->human_readable_size($diffSize, 1).')</span>';
485                                         }
486                                         if(file_exists($this->site_path.'/repo/'.$apk['apkname'].'.asc')) {
487                                                 $out.=' <a href="https://f-droid.org/repo/'.$apk['apkname'].'.asc">GPG Signature</a> ';
488                                         }
489                                         if($srcbuild) {
490                                                 $out.='<br /><a href="https://f-droid.org/repo/'.$apk['srcname'].'">source tarball</a> ';
491                                                 $out.=$this->human_readable_size(filesize($this->site_path.'/repo/'.$apk['srcname']));
492                                         }
493
494                                         if(isset($apk['permissions'])) {
495                                                 // Permissions diff link
496                                                 if($first == false) {
497                                                         $permissionsAddedCount = count($apk['diff']['permissions']['added']);
498                                                         $permissionsRemovedCount = count($apk['diff']['permissions']['removed']);
499                                                         $divIdDiff='permissionsDiff'.$i;
500                                                         if($permissionsAddedCount || $permissionsRemovedCount) {
501                                                                 $out.='<br /><a href="javascript:void(0);" onClick="showHidePermissions(\''.$divIdDiff.'\');">permissions diff</a>';
502                                                                 $out.=' <span style="color:#AAAAAA;">(';
503                                                                 if($permissionsAddedCount)
504                                                                         $out.='+'.$permissionsAddedCount;
505                                                                 if($permissionsAddedCount && $permissionsRemovedCount)
506                                                                         $out.='/';
507                                                                 if($permissionsRemovedCount)
508                                                                         $out.='-'.$permissionsRemovedCount;
509                                                                 $out.=')</span>';
510                                                         }
511                                                         else
512                                                         {
513                                                                 $out.='<br /><span style="color:#999999;">no permission changes</span>';
514                                                         }
515                                                 }
516
517                                                 // Permissions list link
518                                                 $permissionsListString = $this->get_permission_list_string(explode(',',$apk['permissions']), $permissions_data, $summary);
519                                                 /*if($i==0)
520                                                         $divStyleDisplay='block';
521                                                 else*/
522                                                 $divStyleDisplay='none';
523                                                 $divId='permissions'.$i;
524                                                 $out.='<br /><a href="javascript:void(0);" onClick="showHidePermissions(\''.$divId.'\');">view permissions</a>';
525                                                 $out.=' <span style="color:#AAAAAA;">['.$summary.']</span>';
526                                                 $out.='<br/>';
527
528                                                 // Permissions list
529                                                 $out.='<div style="display:'.$divStyleDisplay.';" id="'.$divId.'">';
530                                                 $out.=$permissionsListString;
531                                                 $out.='</div>';
532
533                                                 // Permissions diff
534                                                 {
535                                                         $out.='<div style="display:'.$divStyleDisplay.';" id="'.$divIdDiff.'">';
536                                                         $permissionsRemoved = $apk['diff']['permissions']['removed'];
537                                                         usort($permissionsRemoved, "permissions_cmp");
538
539                                                         // Added permissions
540                                                         if($permissionsAddedCount) {
541                                                                 $out.='<h5>ADDED</h5><br />';
542                                                                 $out.=$this->get_permission_list_string($apk['diff']['permissions']['added'], $permissions_data, $summary);
543                                                         }
544
545                                                         // Removed permissions
546                                                         if($permissionsRemovedCount) {
547                                                                 $out.='<h5>REMOVED</h5><br />';
548                                                                 $out.=$this->get_permission_list_string($apk['diff']['permissions']['removed'], $permissions_data, $summary);
549                                                         }
550
551                                                         $out.='</div>';
552                                                 }
553                                         }
554                                         else {
555                                                 $out.='<br /><span style="color:#999999;">no extra permissions needed</span><br />';
556                                         }
557
558                                         $out.='</p>';
559                                         $i++;
560                                 }
561
562                                 $out.='<hr><p><a href="'.makelink($query_vars,array('fdid'=>null)).'">Index</a></p>';
563
564                                 return $out;
565                         }
566                 }
567                 return "<p>Application not found</p>";
568         }
569
570         private function get_permission_list_string($permissions, $permissions_data, &$summary) {
571                 $out='';
572                 usort($permissions, "permissions_cmp");
573                 $permission_group_last = '';
574                 foreach($permissions as $permission) {
575                         $permission_group = $permissions_data['permission'][$permission]['permissionGroup'];
576                         if($permission_group != $permission_group_last) {
577                                 $permission_group_label = $permissions_data['permission-group'][$permission_group]['label'];
578                                 if($permission_group_label=='') $permission_group_label = 'Extra/Custom';
579                                 $out.='<strong>'.strtoupper($permission_group_label).'</strong><br/>';
580                                 $permission_group_last = $permission_group;
581                         }
582
583                         $out.=$this->get_permission_protection_level_icon($permissions_data['permission'][$permission]['protectionLevel']).' ';
584                         $out.='<strong>'.$permissions_data['permission'][$permission]['label'].'</strong> [<code>'.$permission.'</code>]<br/>';
585                         if($permissions_data['permission'][$permission]['description']) $out.=$permissions_data['permission'][$permission]['description'].'<br/>';
586                         //$out.=$permissions_data['permission'][$permission]['comment'].'<br/>';
587                         $out.='<br/>';
588
589                         if(!isset($summaryCount[$permissions_data['permission'][$permission]['protectionLevel']]))
590                                 $summaryCount[$permissions_data['permission'][$permission]['protectionLevel']] = 0;
591                         $summaryCount[$permissions_data['permission'][$permission]['protectionLevel']]++;
592                 }
593
594                 $summary = '';
595                 if(isset($summaryCount)) {
596                         foreach($summaryCount as $protectionLevel => $count) {
597                                 $summary .= $this->get_permission_protection_level_icon($protectionLevel, 'regular').' '.$count;
598                                 $summary .= ', ';
599                         }
600                 }
601                 $summary = substr($summary,0,-2);
602
603                 return $out;
604         }
605
606         private function get_permission_protection_level_icon($protection_level, $size='adjusted') {
607                 $iconString = '';
608                 if($protection_level=='dangerous') {
609                         $iconString .= '<span style="color:#DD9900;';
610                         if($size=='adjusted')
611                                 $iconString .= 'font-size:150%;';
612                         $iconString .= '">&#x26a0;</span>';     // WARNING SIGN
613                 }
614                 elseif($protection_level=='normal') {
615                         $iconString .= '<span style="color:#6666FF;';
616                         if($size=='adjusted')
617                                 $iconString .= 'font-size:110%;';
618                         $iconString .= '">&#x24D8;</span>';     // CIRCLED LATIN SMALL LETTER I
619                 }
620                 elseif($protection_level=='signature') {
621                         $iconString .= '<span style="color:#33AAAA;';
622                         if($size=='adjusted')
623                                 $iconString .= 'font-size:140%;';
624                         $iconString .= '">&#x273D;</span>';     // HEAVY TEARDROP-SPOKED ASTERISK
625                 }
626                 elseif($protection_level=='signatureOrSystem') {
627                         $iconString .= '<span style="color:#DD66DD;';
628                         if($size=='adjusted')
629                                 $iconString .= 'font-size:140%;';
630                         $iconString .= '">&#x269B;</span>';     // ATOM SYMBOL
631                 }
632                 else {
633                         $iconString .= '<span style="color:#33AA33';
634                         if($size=='adjusted')
635                                 $iconString .= ';font-size:130%;';
636                         $iconString .= '">&#x2699;</span>';     // GEAR
637                 }
638
639                 return $iconString;
640         }
641
642         private function human_readable_size($size, $minDiv=0) {
643                 $si_prefix = array('bytes','kB','MB');
644                 $div = 1024;
645
646                 for($i=0;(abs($size) > $div && $i < count($si_prefix)) || $i<$minDiv;$i++) {
647                         $size /= $div;
648                 }
649
650                 return round($size,max(0,$i-1)).' '.$si_prefix[$i];
651         }
652
653         private function get_antifeature_description($antifeature) {
654                 // Anti feature names and descriptions
655                 $antifeatureDescription['Ads']['name'] = 'Advertising';
656                 $antifeatureDescription['Ads']['description'] = 'This application contains advertising.';
657                 $antifeatureDescription['Tracking']['name'] = 'Tracks You';
658                 $antifeatureDescription['Tracking']['description'] = 'This application tracks and reports your activity to somewhere.';
659                 $antifeatureDescription['NonFreeNet']['name'] = 'Non-Free Network Services';
660                 $antifeatureDescription['NonFreeNet']['description'] = 'This application promotes a non-Free network service.';
661                 $antifeatureDescription['NonFreeAdd']['name'] = 'Non-Free Addons';
662                 $antifeatureDescription['NonFreeAdd']['description'] = 'This application promotes non-Free add-ons.';
663                 $antifeatureDescription['NonFreeDep']['name'] = 'Non-Free Dependencies';
664                 $antifeatureDescription['NonFreeDep']['description'] = 'This application depends on another non-Free application.';
665                 $antifeatureDescription['UpstreamNonFree']['name'] = 'Upstream Non-Free';
666                 $antifeatureDescription['UpstreamNonFree']['description'] = 'The upstream source code is non-free.';
667                 $antifeatureDescription['NonFreeAssets']['name'] = 'Non-Free Assets';
668                 $antifeatureDescription['NonFreeAssets']['description'] = 'This application contains non-free assets.';
669
670                 if(isset($antifeatureDescription[$antifeature])) {
671                         return $antifeatureDescription[$antifeature];
672                 }
673                 return array('name'=>$antifeature);
674         }
675
676
677         function get_apps($query_vars) {
678
679                 $xml = simplexml_load_file($this->site_path."/repo/index.xml");
680                 $matches = $this->show_apps($xml,$query_vars,$numpages);
681
682                 $out='';
683
684                 if(($query_vars['fdfilter']===null || $query_vars['fdfilter']!='') && $numpages>0)
685                 {
686                         $out.='<div style="float:left;">';
687                         if($query_vars['fdfilter']===null) {
688
689                                 $categories = array('All categories');
690                                 $handle = fopen(getenv('DOCUMENT_ROOT').'/repo/categories.txt', 'r');
691                                 if ($handle) {
692                                         while (($buffer = fgets($handle, 4096)) !== false) {
693                                                 $categories[] = rtrim($buffer);
694                                         }
695                                         fclose($handle);
696                                 }
697
698                                 $out.='<form name="categoryform" action="" method="get">';
699                                 $out.=$this->makeformdata($query_vars);
700
701                                 $out.='<select name="fdcategory" style="color:#333333;" onChange="document.categoryform.submit();">';
702                                 foreach($categories as $category) {
703                                         $out.='<option';
704                                         if(isset($query_vars['fdcategory']) && $category==$query_vars['fdcategory'])
705                                                 $out.=' selected';
706                                         $out.='>'.$category.'</option>';
707                                 }
708                                 $out.='</select>';
709
710                                 $out.='</form>'."\n";
711                         }
712                         else {
713                                 $out.='Applications matching "'.esc_attr($query_vars['fdfilter']).'"';
714                         }
715                         $out.="</div>";
716
717                         $out.='<div style="float:right;">';
718                         $out.='<a href="'.makelink($query_vars, array('fdstyle'=>'list', 'fdpage'=>1)).'">List</a> | ';
719                         $out.='<a href="'.makelink($query_vars, array('fdstyle'=>'grid', 'fdpage'=>1)).'">Grid</a>';
720                         $out.='</div>';
721
722                         $out.='<br break="all"/>';
723                 }
724
725                 if($numpages>0) {
726                         $out.=$matches;
727
728                         $out.='<hr><p>';
729
730                         $out.='<div style="width:20%; float:left; text-align:left;">';
731                         $out.=' Page '.$query_vars['fdpage'].' of '.$numpages.' ';
732                         $out.='</div>';
733
734                         $out.='<div style="width:60%; float:left; text-align:center;">';
735                         if($numpages>1) {
736                                 for($i=1;$i<=$numpages;$i++) {
737                                         if($i == $query_vars['fdpage']) {
738                                                 $out.='<b>'.$i.'</b>';
739                                         } else {
740                                                 $out.='<a href="'.makelink($query_vars, array('fdpage'=>$i)).'">';
741                                                 $out.=$i;
742                                                 $out.='</a>';
743                                         }
744                                         $out.=' ';
745                                 }
746                                 $out.=' ';
747                         }
748                         $out.='</div>';
749
750                         $out.='<div style="width:20%; float:left; text-align:right;">';
751                         if($query_vars['fdpage']!=$numpages) {
752                                 $out.='<a href="'.makelink($query_vars, array('fdpage'=>($query_vars['fdpage']+1))).'">next&gt;</a> ';
753                         }
754                         $out.='</div>';
755
756                         $out.='</p>';
757                 } else if($query_vars['fdfilter']!='') {
758                         $out.='<p>No matches</p>';
759                 }
760
761                 return $out;
762         }
763
764
765         function makeformdata($query_vars) {
766
767                 $out='';
768
769                 $out.='<input type="hidden" name="page_id" value="'.(int)get_query_var('page_id').'">';
770                 foreach($query_vars as $name => $value) {
771                         if($value !== null && $name != 'fdfilter' && $name != 'fdpage')
772                                 $out.='<input type="hidden" name="'.esc_attr($name).'" value="'.esc_attr($value).'">';
773                 }
774
775                 return $out;
776         }
777
778
779         function show_apps($xml,$query_vars,&$numpages) {
780
781                 $skipped=0;
782                 $got=0;
783                 $total=0;
784
785                 if($query_vars['fdstyle']=='grid') {
786                         $outputter = new FDOutGrid();
787                 } else {
788                         $outputter = new FDOutList();
789                 }
790
791                 $out = "";
792
793                 $out.=$outputter->outputStart();
794
795                 foreach($xml->children() as $app) {
796
797                         if($app->getName() == 'repo') continue;
798                         $appinfo['attrs']=$app->attributes();
799                         $appinfo['id']=$appinfo['attrs']['id'];
800                         foreach($app->children() as $el) {
801                                 switch($el->getName()) {
802                                 case "name":
803                                         $appinfo['name']=$el;
804                                         break;
805                                 case "icon":
806                                         $appinfo['icon']=$el;
807                                         break;
808                                 case "summary":
809                                         $appinfo['summary']=$el;
810                                         break;
811                                 case "desc":
812                                         $appinfo['description']=$el;
813                                         break;
814                                 case "license":
815                                         $appinfo['license']=$el;
816                                         break;
817                                 case "category":
818                                         $appinfo['category']=$el;
819                                         break;
820                                 }
821                         }
822
823                         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'])) {
824                                 if($skipped<($query_vars['fdpage']-1)*$outputter->perpage) {
825                                         $skipped++;
826                                 } else if($got<$outputter->perpage) {
827                                         $out.=$outputter->outputEntry($query_vars, $appinfo);
828                                         $got++;
829                                 }
830                                 $total++;
831                         }
832
833                 }
834
835                 $out.=$outputter->outputEnd();
836
837                 $numpages = ceil((float)$total/$outputter->perpage);
838
839                 return $out;
840         }
841 }
842
843 // Class to output app entries in a detailed list format
844 class FDOutList
845 {
846         var $perpage=30;
847
848         function FDOutList() {
849         }
850
851         function outputStart() {
852                 return '';
853         }
854
855         function outputEntry($query_vars, $appinfo) {
856                 $out="";
857                 $out.='<hr style="clear:both;" />'."\n";
858                 $out.='<a href="'.makelink($query_vars, array('fdid'=>$appinfo['id'])).'">';
859                 $out.='<div id="appheader">';
860
861                 $out.='<div style="float:left;padding-right:10px;"><img src="' . site_url() . '/repo/icons/'.$appinfo['icon'].'" style="width:48px;border:none;"></div>';
862
863                 $out.='<div style="float:right;">';
864                 $out.='<p>Details...</p>';
865                 $out.="</div>\n";
866
867                 $out.='<p style="color:#000000;"><span style="font-size:20px;">'.$appinfo['name']."</span>";
868                 $out.="<br>".$appinfo['summary']."</p>\n";
869
870                 $out.="</div>\n";
871                 $out.='</a>';
872
873                 return $out;
874         }
875
876         function outputEnd() {
877                 return '';
878         }
879 }
880
881 // Class to output app entries in a compact grid format
882 class FDOutGrid
883 {
884         var $perpage=80;
885
886         var $itemCount = 0;
887
888         function FDOutGrid() {
889         }
890
891         function outputStart() {
892                 return "\n".'<table border="0" width="100%"><tr>'."\n";
893         }
894
895         function outputEntry($query_vars, $appinfo) {
896                 $link=makelink($query_vars, array('fdid'=>$appinfo['id']));
897
898                 $out='';
899
900                 if($this->itemCount%4 == 0 && $this->itemCount > 0)
901                 {
902                         $out.='</tr><tr>'."\n";
903                 }
904
905                 $out.='<td align="center" valign="top" style="background-color:#F8F8F8;">';
906                 $out.='<p>';
907                 $out.='<div id="appheader" style="text-align:center;width:110px;">';
908
909                 $out.='<a href="'.$link.'" style="border-bottom-style:none;">';
910                 $out.='<img src="' . site_url() . '/repo/icons/'.$appinfo['icon'].'" style="width:48px;border-width:0;padding-top:5px;padding-bottom:5px;"><br/>';
911                 $out.=$appinfo['name'].'<br/>';
912                 $out.='</a>';
913
914                 $out.="</div>";
915                 $out.='</p>';
916                 $out.="</td>\n";
917
918                 $this->itemCount++;
919                 return $out;
920         }
921
922         function outputEnd() {
923                 return '</tr></table>'."\n";
924         }
925 }
926
927 function permissions_cmp($a, $b) {
928         global $permissions_data;
929
930         $aProtectionLevel = $permissions_data['permission'][$a]['protectionLevel'];
931         $bProtectionLevel = $permissions_data['permission'][$b]['protectionLevel'];
932
933         if($aProtectionLevel != $bProtectionLevel) {
934                 if(strlen($aProtectionLevel)==0) return 1;
935                 if(strlen($bProtectionLevel)==0) return -1;
936
937                 return strcmp($aProtectionLevel, $bProtectionLevel);
938         }
939
940         $aGroup = $permissions_data['permission'][$a]['permissionGroup'];
941         $bGroup = $permissions_data['permission'][$b]['permissionGroup'];
942
943         if($aGroup != $bGroup) {
944                 return strcmp($aGroup, $bGroup);
945         }
946
947         return strcmp($a, $b);
948 }
949
950 // Make a link to this page, with the current query vars attached and desired params added/modified
951 function makelink($query_vars, $params=array()) {
952         $link=get_permalink();
953
954         $p = array_merge($query_vars, $params);
955
956         // Page 1 is the default, don't clutter urls with it...
957         if($p['fdpage'] == 1)
958                 unset($p['fdpage']);
959         // Likewise for list style...
960         if($p['fdstyle'] == 'list')
961                 unset($p['fdstyle']);
962
963         $vars=linkify($p);
964         if(strlen($vars)==0)
965                 return $link;
966         if(strpos($link,'?')===false)
967                 $link.='?';
968         else
969                 $link.='&';
970         return $link.$vars;
971 }
972
973 // Return the key value pairs in http-get-parameter format as a string
974 function linkify($vars) {
975         $retvar = '';
976         foreach($vars as $k => $v) {
977                 if($k!==null && $v!==null && $v!='')
978                         $retvar .= $k.'='.urlencode($v).'&';
979         }
980         return substr($retvar,0,-1);
981 }
982
983 $wp_fdroid = new FDroid();
984
985
986 ?>