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