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