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