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