Displaying of human readable Android permissions for each package in the repo.

Conflicts:
	wp-fdroid/wp-fdroid.php
This commit is contained in:
Hans-Emil Skogh 2012-01-14 12:22:19 +01:00
commit 9355640475
4 changed files with 5227 additions and 4 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,114 @@
<?php
// Class that provides PHP-friendly android permissions information from the raw Andoid source XML files that describes the permissions.
class AndroidPermissions
{
// Path to the AndroidManifest.xml-file from the Android source. Get it from https://raw.github.com/android/platform_frameworks_base/master/core/res/AndroidManifest.xml for example.
private $android_manifest_file_path;
// Path to the strings.xml-file from the Android source. Get it from https://raw.github.com/android/platform_frameworks_base/master/core/res/res/values/strings.xml for example.
private $android_strings_file_path;
// Path to the file where the resulting permissions data will be cached. NOTE: Must be writable by PHP!
private $cache_file_path;
public function __construct($android_manifest_file_path_in = 'AndroidManifest.xml', $android_strings_file_path_in = 'strings.xml', $cache_file_path_in = 'android-permissions.cache') {
$this->android_manifest_file_path = $android_manifest_file_path_in;
$this->android_strings_file_path = $android_strings_file_path_in;
$this->cache_file_path = $cache_file_path_in;
}
// Returns an associative array with android permissions and data about them
function get_permissions_array() {
// Check status of cache
$android_manifest_file_stat = stat($this->android_manifest_file_path);
$android_manifest_file_mtime = $android_manifest_file_stat['mtime'];
$android_strings_file_stat = stat($this->android_strings_file_path);
$android_strings_file_mtime = $android_strings_file_stat['mtime'];
$cache_file_mtime = 0;
if(file_exists($this->cache_file_path)) {
$cache_file_stat = stat($this->cache_file_path);
$cache_file_mtime = $cache_file_stat['mtime'];
}
// If the cache is fresh, use it instead
if($android_manifest_file_mtime < $cache_file_mtime && $android_strings_file_mtime < $cache_file_mtime ) {
$cache_file_handle = fopen($this->cache_file_path, 'r');
$cache_file_content = fread($cache_file_handle, filesize($this->cache_file_path));
fclose($cache_file_handle);
$permissions = unserialize($cache_file_content);
return $permissions;
}
// We are updating the cache, touch the file (note: race condition possible between stating the cache file above and this line...)
touch($this->cache_file_path);
// Get permission raw data from XML
$manifestDoc = new DOMDocument;
$manifestDoc->load($this->android_manifest_file_path);
$manifestXpath = new DOMXPath($manifestDoc);
$stringsDoc = new DOMDocument;
$stringsDoc->load($this->android_strings_file_path);
$stringsXpath = new DOMXPath($stringsDoc);
$comment = '';
foreach ($manifestXpath->query('node()') as $node) {
// Save permissions and permission groups from tags
if($node->nodeName == 'permission-group' || $node->nodeName == 'permission') {
$name = $node->attributes->getNamedItem('name')->value;
$name = substr(strrchr($name,'.'), 1);
// Lookup the human readable title
$labelObject = $node->attributes->getNamedItem('label');
$labelString = $name;
if( $labelObject !== NULL ) {
$labelName = substr(strrchr($labelObject->value,'/'),1);
$labelStringObject = $stringsXpath->query('//string[@name="'.$labelName.'"]');
$labelString = ucfirst($labelStringObject->item(0)->nodeValue);
}
// Lookup the human readable description
$descriptionObject = $node->attributes->getNamedItem('description');
$descriptionString = '(Description missing)';
if($descriptionObject !== NULL) {
$descriptionName = substr(strrchr($descriptionObject->value,'/'),1);
$descriptionStringObject = $stringsXpath->query('//string[@name="'.$descriptionName.'"]');
$descriptionString = ucfirst($descriptionStringObject->item(0)->nodeValue);
}
$permissions[$node->nodeName][$name]['label'] = stripslashes($labelString);
$permissions[$node->nodeName][$name]['description'] = stripslashes($descriptionString);
$permissions[$node->nodeName][$name]['comment'] = stripslashes(str_replace(array("\r\n", "\r", "\n", "\t", ' '), '', $comment));
if($node->nodeName == 'permission') {
$permissionGroupObject = $node->attributes->getNamedItem('permissionGroup');
$permissionGroup = 'none';
if($permissionGroupObject !== NULL) {
$permissionGroup = substr(strrchr($permissionGroupObject->value,'.'), 1);
}
$permissions[$node->nodeName][$name]['permissionGroup'] = $permissionGroup;
$permissions[$node->nodeName][$name]['protectionLevel'] = $node->attributes->getNamedItem('protectionLevel')->value;
}
}
// Cache descriptions from comments preceding the tags
if($node->nodeName == '#comment') {
$comment .= $node->textContent;
}
elseif($node->nodeName != '#text') {
$comment = '';
}
}
// Update cache with serialized permissions
$cache_file_handle = fopen($this->cache_file_path, 'w');
fwrite($cache_file_handle, serialize($permissions));
fclose($cache_file_handle);
return $permissions;
}
}
?>

3389
wp-fdroid/strings.xml Normal file

File diff suppressed because it is too large Load diff

View file

@ -12,6 +12,8 @@ Revision history
*/ */
include('android-permissions.php');
class FDroid class FDroid
{ {
@ -100,8 +102,10 @@ class FDroid
function get_app($query_vars) { function get_app($query_vars) {
$permissions_object = new AndroidPermissions($this->site_path.'/repo/AndroidManifest.xml', $this->site_path.'/repo/strings.xml', $this->site_path.'/repo/android-permissions.cache');
$permissions_data = $permissions_object->get_permissions_array();
$xml = simplexml_load_file($this->site_path."/repo/index.xml"); $xml = simplexml_load_file($this->site_path.'/repo/index.xml');
foreach($xml->children() as $app) { foreach($xml->children() as $app) {
$attrs=$app->attributes(); $attrs=$app->attributes();
@ -192,14 +196,77 @@ class FDroid
$out.='<b>Donate:</b> <a href="'.$donate.'">'.$donate.'</a><br />'; $out.='<b>Donate:</b> <a href="'.$donate.'">'.$donate.'</a><br />';
$out.="</p>"; $out.="</p>";
$out.='<script type="text/javascript">';
$out.='function showHidePermissions(id) {';
$out.=' if(document.getElementById(id).style.display==\'none\')';
$out.=' document.getElementById(id).style.display=\'block\';';
$out.=' else';
$out.=' document.getElementById(id).style.display=\'none\';';
$out.=' return false;';
$out.='}';
$out.='</script>';
$out.="<h3>Packages</h3>"; $out.="<h3>Packages</h3>";
$i=0;
foreach($apks as $apk) { foreach($apks as $apk) {
$out.="<p><b>Version ".$apk['version']."</b> - "; $out.="<p><b>Version ".$apk['version']."</b><br />";
$out.='<a href="http://f-droid.org/repo/'.$apk['apkname'].'">download</a> '; $out.='<a href="http://f-droid.org/repo/'.$apk['apkname'].'">download apk</a> ';
$out.=$apk['size']." bytes"; $out.=$apk['size']." bytes";
if($apk['srcname']) if($apk['srcname'])
$out.='<br><a href="http://f-droid.org/repo/'.$apk['srcname'].'">source tarball</a>'; $out.='<br><a href="http://f-droid.org/repo/'.$apk['srcname'].'">source tarball</a>';
$out.="</p>";
/*if($i==0)
$divStyleDisplay='block';
else*/
$divStyleDisplay='none';
$divId='permissions'.$i;
$out.='<br /><a href="javascript:void(0);" onClick="showHidePermissions(\''.$divId.'\');">view permissions</a><br/>';
$out.='<div style="display:'.$divStyleDisplay.';" id="'.$divId.'">';
$permissions = explode(',',$apk['permissions']);
usort($permissions,
function ($a, $b) use (&$permissions_data) {
$aProtectionLevel = $permissions_data['permission'][$a]['protectionLevel'];
$bProtectionLevel = $permissions_data['permission'][$b]['protectionLevel'];
if($aProtectionLevel != $bProtectionLevel) {
if(strlen($aProtectionLevel)==0) return 1;
if(strlen($bProtectionLevel)==0) return -1;
return strcmp($aProtectionLevel, $bProtectionLevel);
}
$aGroup = $permissions_data['permission'][$a]['permissionGroup'];
$bGroup = $permissions_data['permission'][$b]['permissionGroup'];
if($aGroup != $bGroup) {
return strcmp($aGroup, $bGroup);
}
return strcmp($a, $b);
}
);
$permission_group_last = '';
foreach($permissions as $permission) {
$permission_group = $permissions_data['permission'][$permission]['permissionGroup'];
if($permission_group != $permission_group_last) {
$permission_group_label = $permissions_data['permission-group'][$permission_group]['label'];
if($permission_group_label=='') $permission_group_label = 'Extra/Custom';
$out.='<strong>'.strtoupper($permission_group_label).'</strong><br/>';
$permission_group_last = $permission_group;
}
$out.=$this->get_permission_protection_level_icon($permissions_data['permission'][$permission]['protectionLevel']).' ';
$out.='<strong>'.$permissions_data['permission'][$permission]['label'].'</strong> [<code>'.$permission.'</code>]<br/>';
if($permissions_data['permission'][$permission]['description']) $out.=$permissions_data['permission'][$permission]['description'].'<br/>';
//$out.=$permissions_data['permission'][$permission]['comment'].'<br/>';
$out.='<br/>';
}
$out.='</div>';
$out.='</p>';
$i++;
} }
$out.='<hr><p><a href="'.makelink($query_vars,array('fdid'=>null)).'">Index</a></p>'; $out.='<hr><p><a href="'.makelink($query_vars,array('fdid'=>null)).'">Index</a></p>';
@ -210,6 +277,20 @@ class FDroid
return "<p>Application not found</p>"; return "<p>Application not found</p>";
} }
private function get_permission_protection_level_icon($protection_level) {
if($protection_level=='dangerous')
{
return '<span style="color:#DD9900;font-size:150%;">&#x26a0;</span>';
}
elseif($protection_level=='normal')
{
return '<span style="color:#6666FF;font-size:110%;">&#x24D8;</span>';
}
else
{
return '<span style="color:#33AA33;font-size:130%;">&#x2699;</span>';
}
}
function get_apps($query_vars) { function get_apps($query_vars) {