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