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