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