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