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