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