chiark / gitweb /
A basic 'latest apps' widget'
[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                 register_sidebar_widget('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[]='fdid';
40                 $qvars[]='fdpage';
41                 $qvars[]='fdstyle';
42                 return $qvars;
43         }
44
45
46         // Lazy initialise. All non-trivial members should call this before doing anything else.
47         function lazyinit() {
48                 if(!$this->inited) {
49                         load_plugin_textdomain($this->textdom, PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__)));
50
51                         $this->inited=true;
52                 }
53         }
54
55         // Gets a required query parameter by name.
56         function getrequiredparam($name) {
57                 global $wp_query;
58                 if(!isset($wp_query->query_vars[$name]))
59                         wp_die("Missing parameter ".$name,"Error");
60                 return $wp_query->query_vars[$name];
61         }
62
63         // Handler for the 'fdroidrepo' shortcode.
64         //  $attribs - shortcode attributes
65         //  $content - optional content enclosed between the starting and
66         //                       ending shortcode
67         // Returns the generated content.
68         function do_shortcode($attribs,$content=null) {
69                 global $wp_query,$wp_rewrite;
70                 $this->lazyinit();
71
72                 // Init local query vars
73                 foreach($this->queryvars(array()) as $qv) {
74                         if(array_key_exists($qv,$wp_query->query_vars)) {
75                                 $query_vars[$qv] = $wp_query->query_vars[$qv];
76                         } else {
77                                 $query_vars[$qv] = null;
78                         }
79                 }
80
81                 // Santiy check query vars
82                 if(!isset($query_vars['fdpage']) || !is_numeric($query_vars['fdpage']) || $query_vars['fdpage'] <= 0) {
83                         $query_vars['fdpage'] = 1;
84                 }
85
86                 $out = '';
87
88                 if(isset($attribs['search']) && $query_vars['fdfilter']===null) {
89                         $query_vars['fdfilter'] = '';
90                 }
91
92                 if($query_vars['fdid']!==null) {
93                         $out.=$this->get_app($query_vars);
94                 } else {
95                         if($query_vars['fdfilter'] !== null)
96                                 $out.=$this->show_search($query_vars);
97
98                         $out.=$this->get_apps($query_vars);
99                 }
100
101                 return $out;
102         }
103
104
105         function get_app($query_vars) {
106                 global $permissions_data;
107                 $permissions_object = new AndroidPermissions($this->site_path.'/wp-content/plugins/wp-fdroid/AndroidManifest.xml',
108                         $this->site_path.'/wp-content/plugins/wp-fdroid/strings.xml',
109                         sys_get_temp_dir().'/android-permissions.cache');
110                 $permissions_data = $permissions_object->get_permissions_array();
111
112                 // Get app data
113                 $xml = simplexml_load_file($this->site_path.'/repo/index.xml');
114                 foreach($xml->children() as $app) {
115
116                         $attrs=$app->attributes();
117                         if($attrs['id']==$query_vars['fdid']) {
118                                 $apks=array();;
119                                 foreach($app->children() as $el) {
120                                         switch($el->getName()) {
121                                                 case "name":
122                                                         $name=$el;
123                                                         break;
124                                                 case "icon":
125                                                         $icon=$el;
126                                                         break;
127                                                 case "summary":
128                                                         $summary=$el;
129                                                         break;
130                                                 case "description":
131                                                         $desc=$el;
132                                                         break;
133                                                 case "license":
134                                                         $license=$el;
135                                                         break;
136                                                 case "source":
137                                                         $source=$el;
138                                                         break;
139                                                 case "tracker":
140                                                         $issues=$el;
141                                                         break;
142                                                 case "donate":
143                                                         $donate=$el;
144                                                         break;
145                                                 case "web":
146                                                         $web=$el;
147                                                         break;
148                                                 case "antifeatures";
149                                                         $antifeatures=$el;
150                                                         break;
151                                                 case "requirements";
152                                                         $requirements=$el;
153                                                         break;
154                                                 case "package":
155                                                         $thisapk=array();
156                                                         foreach($el->children() as $pel) {
157                                                                 switch($pel->getName()) {
158                                                                 case "version":
159                                                                         $thisapk['version']=$pel;
160                                                                         break;
161                                                                 case "vercode":
162                                                                         $thisapk['vercode']=$pel;
163                                                                         break;
164                                                                 case "apkname":
165                                                                         $thisapk['apkname']=$pel;
166                                                                         break;
167                                                                 case "srcname":
168                                                                         $thisapk['srcname']=$pel;
169                                                                         break;
170                                                                 case "hash":
171                                                                         $thisapk['hash']=$pel;
172                                                                         break;
173                                                                 case "size":
174                                                                         $thisapk['size']=$pel;
175                                                                         break;
176                                                                 case "sdkver":
177                                                                         $thisapk['sdkver']=$pel;
178                                                                         break;
179                                                                 case "permissions":
180                                                                         $thisapk['permissions']=$pel;
181                                                                         break;
182                                                                 }
183                                                         }
184                                                         $apks[]=$thisapk;
185
186                                         }
187                                 }
188
189                                 // Generate app diff data
190                                 foreach(array_reverse($apks, true) as $key=>$apk) {
191                                         if(isset($previous)) {
192                                                 // Apk size
193                                                 $apks[$key]['diff']['size'] = $apk['size']-$previous['size'];
194                                         }
195
196                                         // Permissions
197                                         $permissions = explode(',',$apk['permissions']);
198                                         $permissionsPrevious = isset($previous['permissions'])?explode(',',$previous['permissions']):array();
199                                         $apks[$key]['diff']['permissions']['added'] = array_diff($permissions, $permissionsPrevious);
200                                         $apks[$key]['diff']['permissions']['removed'] = array_diff($permissionsPrevious, $permissions);
201
202                                         $previous = $apk;
203                                 }
204
205                                 // Output app information
206                                 $out='<div id="appheader">';
207                                 $out.='<div style="float:left;padding-right:10px;"><img src="http://f-droid.org/repo/icons/'.$icon.'" width=48></div>';
208                                 $out.='<p><span style="font-size:20px">'.$name."</span>";
209                                 $out.="<br>".$summary."</p>";
210                                 $out.="</div>";
211
212                                 $out.="<p>".$desc."</p>";
213
214                                 if(isset($antifeatures)) {
215                                         $antifeaturesArray = explode(',',$antifeatures);
216                                         foreach($antifeaturesArray as $antifeature) {
217                                                 $antifeatureDesctiption = $this->get_antifeature_description($antifeature);
218                                                 $out.='<p style="border:3px solid #CC0000;background-color:#FFDDDD;padding:5px;"><strong>'.$antifeatureDesctiption['name'].'</strong><br />';
219                                                 $out.=$antifeatureDesctiption['description'].'</p>';
220                                         }
221                                 }
222
223                                 $out.="<p>";
224                                 $out.="<b>License:</b> ".$license;
225                                 if(isset($requirements)) {
226                                         $out.='<br /><b>Additional requirements:</b> '.$requirements;
227                                 }
228                                 $out.="</p>";
229
230                                 $out.="<p>";
231                                 if(strlen($web)>0)
232                                         $out.='<b>Website:</b> <a href="'.$web.'">'.$web.'</a><br />';
233                                 if(strlen($issues)>0)
234                                         $out.='<b>Issue Tracker:</b> <a href="'.$issues.'">'.$issues.'</a><br />';
235                                 if(strlen($source)>0)
236                                         $out.='<b>Source Code:</b> <a href="'.$source.'">'.$source.'</a><br />';
237                                 if($donate && strlen($donate)>0)
238                                         $out.='<b>Donate:</b> <a href="'.$donate.'">'.$donate.'</a><br />';
239                                 $out.="</p>";
240
241                                 $out.='<script type="text/javascript">';
242                                 $out.='function showHidePermissions(id) {';
243                                 $out.='  if(document.getElementById(id).style.display==\'none\')';
244                                 $out.=' document.getElementById(id).style.display=\'block\';';
245                                 $out.='  else';
246                                 $out.=' document.getElementById(id).style.display=\'none\';';
247                                 $out.='  return false;';
248                                 $out.='}';
249                                 $out.='</script>';
250
251                                 $out.="<h3>Packages</h3>";
252                                 $i=0;
253                                 foreach($apks as $apk) {
254                                         $first = $i+1==count($apks);
255                                         $out.="<p><b>Version ".$apk['version']."</b><br />";
256                                         $out.='<a href="http://f-droid.org/repo/'.$apk['apkname'].'">download apk</a> ';
257                                         $out.=$this->human_readable_size($apk['size']);
258                                         $diffSize = $apk['diff']['size'];
259                                         if(abs($diffSize) > 500) {
260                                                 $out.=' <span style="color:#AAAAAA;">(';
261                                                 $out.=$diffSize>0?'+':'';
262                                                 $out.=$this->human_readable_size($diffSize, 1).')</span>';
263                                         }
264                                         if(isset($apk['srcname']) && file_exists($this->site_path.'/repo/'.$apk['srcname'])) {
265                                                 $out.='<br /><a href="http://f-droid.org/repo/'.$apk['srcname'].'">source tarball</a> ';
266                                                 $out.=$this->human_readable_size(filesize($this->site_path.'/repo/'.$apk['srcname']));
267                                         }
268
269                                         if(isset($apk['permissions'])) {
270                                                 // Permissions diff link
271                                                 if($first == false) {
272                                                         $permissionsAddedCount = count($apk['diff']['permissions']['added']);
273                                                         $permissionsRemovedCount = count($apk['diff']['permissions']['removed']);
274                                                         $divIdDiff='permissionsDiff'.$i;
275                                                         if($permissionsAddedCount || $permissionsRemovedCount) {
276                                                                 $out.='<br /><a href="javascript:void(0);" onClick="showHidePermissions(\''.$divIdDiff.'\');">permissions diff</a>';
277                                                                 $out.=' <span style="color:#AAAAAA;">(';
278                                                                 if($permissionsAddedCount)
279                                                                         $out.='+'.$permissionsAddedCount;
280                                                                 if($permissionsAddedCount && $permissionsRemovedCount)
281                                                                         $out.='/';
282                                                                 if($permissionsRemovedCount)
283                                                                         $out.='-'.$permissionsRemovedCount;
284                                                                 $out.=')</span>';
285                                                         }
286                                                         else
287                                                         {
288                                                                 $out.='<br /><span style="color:#999999;">no permission changes</span>';
289                                                         }
290                                                 }
291
292                                                 // Permissions list link
293                                                 $permissionsListString = $this->get_permission_list_string(explode(',',$apk['permissions']), $permissions_data, $summary);
294                                                 /*if($i==0)
295                                                         $divStyleDisplay='block';
296                                                 else*/
297                                                         $divStyleDisplay='none';
298                                                 $divId='permissions'.$i;
299                                                 $out.='<br /><a href="javascript:void(0);" onClick="showHidePermissions(\''.$divId.'\');">view permissions</a>';
300                                                 $out.=' <span style="color:#AAAAAA;">['.$summary.']</span>';
301                                                 $out.='<br/>';
302
303                                                 // Permissions list
304                                                 $out.='<div style="display:'.$divStyleDisplay.';" id="'.$divId.'">';
305                                                 $out.=$permissionsListString;
306                                                 $out.='</div>';
307
308                                                 // Permissions diff
309                                                 {
310                                                         $out.='<div style="display:'.$divStyleDisplay.';" id="'.$divIdDiff.'">';
311                                                         $permissionsRemoved = $apk['diff']['permissions']['removed'];
312                                                         usort($permissionsRemoved, "permissions_cmp");
313
314                                                         // Added permissions
315                                                         if($permissionsAddedCount) {
316                                                                 $out.='<h5>ADDED</h5><br />';
317                                                                 $out.=$this->get_permission_list_string($apk['diff']['permissions']['added'], $permissions_data, $summary);
318                                                         }
319
320                                                         // Removed permissions
321                                                         if($permissionsRemovedCount) {
322                                                                 $out.='<h5>REMOVED</h5><br />';
323                                                                 $out.=$this->get_permission_list_string($apk['diff']['permissions']['removed'], $permissions_data, $summary);
324                                                         }
325
326                                                         $out.='</div>';
327                                                 }
328                                         }
329                                         else {
330                                                 $out.='<br /><span style="color:#999999;">no extra permissions needed</span><br />';
331                                         }
332
333                                         $out.='</p>';
334                                         $i++;
335                                 }
336
337                                 $out.='<hr><p><a href="'.makelink($query_vars,array('fdid'=>null)).'">Index</a></p>';
338
339                                 return $out;
340                         }
341                 }
342                 return "<p>Application not found</p>";
343         }
344
345         private function get_permission_list_string($permissions, $permissions_data, &$summary) {
346                 $out='';
347                 usort($permissions, "permissions_cmp");
348                 $permission_group_last = '';
349                 foreach($permissions as $permission) {
350                         $permission_group = $permissions_data['permission'][$permission]['permissionGroup'];
351                         if($permission_group != $permission_group_last) {
352                                 $permission_group_label = $permissions_data['permission-group'][$permission_group]['label'];
353                                 if($permission_group_label=='') $permission_group_label = 'Extra/Custom';
354                                 $out.='<strong>'.strtoupper($permission_group_label).'</strong><br/>';
355                                 $permission_group_last = $permission_group;
356                         }
357
358                         $out.=$this->get_permission_protection_level_icon($permissions_data['permission'][$permission]['protectionLevel']).' ';
359                         $out.='<strong>'.$permissions_data['permission'][$permission]['label'].'</strong> [<code>'.$permission.'</code>]<br/>';
360                         if($permissions_data['permission'][$permission]['description']) $out.=$permissions_data['permission'][$permission]['description'].'<br/>';
361                         //$out.=$permissions_data['permission'][$permission]['comment'].'<br/>';
362                         $out.='<br/>';
363
364                         if(!isset($summaryCount[$permissions_data['permission'][$permission]['protectionLevel']]))
365                                 $summaryCount[$permissions_data['permission'][$permission]['protectionLevel']] = 0;
366                         $summaryCount[$permissions_data['permission'][$permission]['protectionLevel']]++;
367                 }
368
369                 $summary = '';
370                 foreach($summaryCount as $protectionLevel => $count) {
371                         $summary .= $this->get_permission_protection_level_icon($protectionLevel, 'regular').' '.$count;
372                         $summary .= ', ';
373                 }
374                 $summary = substr($summary,0,-2);
375
376                 return $out;
377         }
378
379         private function get_permission_protection_level_icon($protection_level, $size='adjusted') {
380                 $iconString = '';
381                 if($protection_level=='dangerous') {
382                         $iconString .= '<span style="color:#DD9900;';
383                         if($size=='adjusted')
384                                 $iconString .= 'font-size:150%;';
385                         $iconString .= '">&#x26a0;</span>';
386                 }
387                 elseif($protection_level=='normal') {
388                         $iconString .= '<span style="color:#6666FF;';
389                         if($size=='adjusted')
390                                 $iconString .= 'font-size:110%;';
391                         $iconString .= '">&#x24D8;</span>';
392                 }
393                 else {
394                         $iconString .= '<span style="color:#33AA33';
395                         if($size=='adjusted')
396                                 $iconString .= ';font-size:130%;';
397                         $iconString .= '">&#x2699;</span>';
398                 }
399
400                 return $iconString;
401         }
402
403         private function human_readable_size($size, $minDiv=0) {
404                 $si_prefix = array('bytes','kB','MB');
405                 $div = 1000;
406
407                 for($i=0;(abs($size) > $div && $i < count($si_prefix)) || $i<$minDiv;$i++) {
408                         $size /= $div;
409                 }
410
411                 return round($size,max(0,$i-1)).' '.$si_prefix[$i];
412         }
413
414         private function get_antifeature_description($antifeature) {
415                 // Anti feature names and descriptions
416                 $antifeatureDesctiption['ads']['name'] = 'Advertising';
417                 $antifeatureDesctiption['ads']['description'] = 'This application contains advertising';
418                 $antifeatureDesctiption['tracking']['name'] = 'Tracks You';
419                 $antifeatureDesctiption['tracking']['description'] = 'This application tracks and reports your activity to somewhere';
420                 $antifeatureDesctiption['nonfreenet']['name'] = 'Non-Free Network Services';
421                 $antifeatureDesctiption['nonfreenet']['description'] = 'This application promotes a non-Free network service';
422                 $antifeatureDesctiption['nonfreeadd']['name'] = 'Non-Free Addons';
423                 $antifeatureDesctiption['nonfreeadd']['description'] = 'This application promotes non-Free add-ons';
424                 $antifeatureDesctiption['nonfreedep']['name'] = 'Non-Free Dependencies';
425                 $antifeatureDesctiption['nonfreedep']['description'] = 'This application depends on another non-Free application';
426
427                 $antifeatureLower = strtolower($antifeature);
428                 if(isset($antifeatureDesctiption[$antifeatureLower])) {
429                         return $antifeatureDesctiption[$antifeatureLower];
430                 }
431                 return array('name'=>$antifeature);
432         }
433
434
435         function get_apps($query_vars) {
436
437                 $xml = simplexml_load_file($this->site_path."/repo/index.xml");
438                 $matches = $this->show_apps($xml,$query_vars,$numpages);
439
440                 $out='';
441
442                 if(($query_vars['fdfilter']===null || $query_vars['fdfilter']!='') && $numpages>0)
443                 {
444                         $out.='<div style="float:left;">';
445                         if($query_vars['fdfilter']===null)
446                                 $out.="All applications";
447                         else
448                                 $out.='Applications matching "'.$query_vars['fdfilter'].'"';
449                         $out.="</div>";
450
451                         $out.='<div style="float:right;">';
452                                 $out.='<a href="'.makelink($query_vars, array('fdstyle'=>'list','fdpage'=>'1')).'">List</a> | ';
453                                 $out.='<a href="'.makelink($query_vars, array('fdstyle'=>'grid','fdpage'=>'1')).'">Grid</a>';
454                         $out.='</div>';
455
456                         $out.='<br break="all"/>';
457                 }
458
459                 if($numpages>0) {
460                         $out.=$matches;
461
462                         $out.='<hr><p>';
463
464                         $out.='<div style="width:20%; float:left; text-align:left;">';
465                         $out.=' Page '.$query_vars['fdpage'].' of '.$numpages.' ';
466                         $out.='</div>';
467
468                         $out.='<div style="width:60%; float:left; text-align:center;">';
469                         if($numpages>1) {
470                                 for($i=1;$i<=$numpages;$i++) {
471                                         if($i == $query_vars['fdpage']) {
472                                                 $out.='<b>'.$i.'</b>';
473                                         } else {
474                                                 $out.='<a href="'.makelink($query_vars, array('fdpage'=>$i)).'">';
475                                                 $out.=$i;
476                                                 $out.='</a>';
477                                         }
478                                         $out.=' ';
479                                 }
480                                 $out.=' ';
481                         }
482                         $out.='</div>';
483
484                         $out.='<div style="width:20%; float:left; text-align:right;">';
485                         if($query_vars['fdpage']!=$numpages) {
486                                 $out.='<a href="'.makelink($query_vars, array('fdpage'=>($query_vars['fdpage']+1))).'">next&gt;</a> ';
487                         }
488                         $out.='</div>';
489
490                         $out.='</p>';
491                 } else if($query_vars['fdfilter']!='') {
492                         $out.='<p>No matches</p>';
493                 }
494
495                 return $out;
496         }
497
498
499         function show_search($query_vars) {
500
501                 $out='';
502                 $out.='<form name="searchform" action="" method="get">';
503                 $out.='<p><input name="fdfilter" type="text" value="'.$query_vars['fdfilter'].'" size="30"> ';
504                 $out.='<input type="submit" value="Search"></p>';
505
506                 $out.='<input type="hidden" name="page_id" value="'.get_query_var('page_id').'">';
507                 foreach($query_vars as $name => $value) {
508                         if($value !== null && $name != 'fdfilter')
509                                 $out.='<input type="hidden" name="'.$name.'" value="'.$value.'">';
510                 }
511
512                 $out.='</form>'."\n";
513
514                 return $out;
515         }
516
517
518         function show_apps($xml,$query_vars,&$numpages) {
519
520                 $skipped=0;
521                 $got=0;
522                 $total=0;
523
524                 if($query_vars['fdstyle']=='grid') {
525                         $outputter = new FDOutGrid();
526                 } else {
527                         $outputter = new FDOutList();
528                 }
529
530                 $out = "";
531
532                 $out.=$outputter->outputStart();
533
534                 foreach($xml->children() as $app) {
535
536                         if($app->getName() == 'repo') continue;
537                         $appinfo['attrs']=$app->attributes();
538                         $appinfo['id']=$appinfo['attrs']['id'];
539                         foreach($app->children() as $el) {
540                                 switch($el->getName()) {
541                                         case "name":
542                                                 $appinfo['name']=$el;
543                                                 break;
544                                         case "icon":
545                                                 $appinfo['icon']=$el;
546                                                 break;
547                                         case "summary":
548                                                 $appinfo['summary']=$el;
549                                                 break;
550                                         case "description":
551                                                 $appinfo['description']=$el;
552                                                 break;
553                                         case "license":
554                                                 $appinfo['license']=$el;
555                                                 break;
556                                 }
557                         }
558
559                         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']))) {
560                                 if($skipped<($query_vars['fdpage']-1)*$outputter->perpage) {
561                                         $skipped++;
562                                 } else if($got<$outputter->perpage) {
563                                         $out.=$outputter->outputEntry($query_vars, $appinfo);
564                                         $got++;
565                                 }
566                                 $total++;
567                         }
568
569                 }
570
571                 $out.=$outputter->outputEnd();
572
573                 $numpages = ceil((float)$total/$outputter->perpage);
574
575                 return $out;
576         }
577 }
578
579 // Class to output app entries in a detailed list format
580 class FDOutList
581 {
582         var $perpage=30;
583
584         function FDOutList() {
585         }
586
587         function outputStart() {
588                 return '';
589         }
590
591         function outputEntry($query_vars, $appinfo) {
592                 $out="";
593                 $out.='<hr style="clear:both;" />'."\n";
594                 $out.='<a href="'.makelink($query_vars, array('fdid'=>$appinfo['id'])).'">';
595                 $out.='<div id="appheader">';
596
597                 $out.='<div style="float:left;padding-right:10px;"><img src="http://f-droid.org/repo/icons/'.$appinfo['icon'].'" style="width:48px;border:none;"></div>';
598
599                 $out.='<div style="float:right;">';
600                 $out.='<p>Details...</p>';
601                 $out.="</div>\n";
602
603                 $out.='<p style="color:#000000;"><span style="font-size:20px;">'.$appinfo['name']."</span>";
604                 $out.="<br>".$appinfo['summary']."</p>\n";
605
606                 $out.="</div>\n";
607                 $out.='</a>';
608
609                 return $out;
610         }
611
612         function outputEnd() {
613                 return '';
614         }
615 }
616
617 // Class to output app entries in a compact grid format
618 class FDOutGrid
619 {
620         var $perpage=80;
621
622         var $itemCount = 0;
623
624         function FDOutGrid() {
625         }
626
627         function outputStart() {
628                 return "\n".'<table border="0" width="100%"><tr>'."\n";
629         }
630
631         function outputEntry($query_vars, $appinfo) {
632                 $link=makelink($query_vars, array('fdid'=>$appinfo['id']));
633
634                 $out='';
635
636                 if($this->itemCount%4 == 0 && $this->itemCount > 0)
637                 {
638                         $out.='</tr><tr>'."\n";
639                 }
640
641                 $out.='<td align="center" valign="top" style="background-color:#F8F8F8;">';
642                 $out.='<p>';
643                 $out.='<div id="appheader" style="text-align:center;width:110px;">';
644
645                 $out.='<a href="'.$link.'" style="border-bottom-style:none;">';
646                 $out.='<img src="http://f-droid.org/repo/icons/'.$appinfo['icon'].'" style="width:48px;border-width:0;padding-top:5px;padding-bottom:5px;"><br/>';
647                 $out.=$appinfo['name'].'<br/>';
648                 $out.='</a>';
649
650                 $out.="</div>";
651                 $out.='</p>';
652                 $out.="</td>\n";
653
654                 $this->itemCount++;
655                 return $out;
656         }
657
658         function outputEnd() {
659                 return '</tr></table>'."\n";
660         }
661 }
662
663 function permissions_cmp($a, $b) {
664         global $permissions_data;
665
666         $aProtectionLevel = $permissions_data['permission'][$a]['protectionLevel'];
667         $bProtectionLevel = $permissions_data['permission'][$b]['protectionLevel'];
668
669         if($aProtectionLevel != $bProtectionLevel) {
670                 if(strlen($aProtectionLevel)==0) return 1;
671                 if(strlen($bProtectionLevel)==0) return -1;
672
673                 return strcmp($aProtectionLevel, $bProtectionLevel);
674         }
675
676         $aGroup = $permissions_data['permission'][$a]['permissionGroup'];
677         $bGroup = $permissions_data['permission'][$b]['permissionGroup'];
678
679         if($aGroup != $bGroup) {
680                 return strcmp($aGroup, $bGroup);
681         }
682
683         return strcmp($a, $b);
684 }
685
686 // Make a link to this page, with the current query vars attached and desired params added/modified
687 function makelink($query_vars, $params=array()) {
688         $link=get_permalink();
689         $vars=linkify(array_merge($query_vars, $params));
690         if(strlen($vars)==0)
691                 return $link;
692         if(strpos($link,'?')===false)
693                 $link.='?';
694         else
695                 $link.='&';
696         return $link.$vars;
697 }
698
699 // Return the key value pairs in http-get-parameter format as a string
700 function linkify($vars) {
701         $retvar = '';
702         foreach($vars as $k => $v) {
703                 if($k!==null && $v!==null && $v!='')
704                         $retvar .= $k.'='.$v.'&';
705         }
706         return substr($retvar,0,-1);
707 }
708
709 function widget_fdroidlatest($args) {
710         extract($args);
711         echo $before_widget;
712         echo $before_title . 'Latest Apps' . $after_title;
713         readfile(getenv('DOCUMENT_ROOT').'/repo/latestapps.html');
714         echo $after_widget;
715 }
716
717 $wp_fdroid = new FDroid();
718
719
720 ?>