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