From: Hans-Emil Skogh Date: Thu, 12 Jan 2012 20:46:49 +0000 (+0100) Subject: Wrapped the android permissions function in a class holding config variables. X-Git-Tag: 0.1~1063^2~10^2~1 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=5f4a422b622260a00475a4daee37555e314ddd7c;p=fdroidserver.git Wrapped the android permissions function in a class holding config variables. --- diff --git a/wp-fdroid/android-permissions.php b/wp-fdroid/android-permissions.php index 7e1288a9..6b8c2805 100644 --- a/wp-fdroid/android-permissions.php +++ b/wp-fdroid/android-permissions.php @@ -1,103 +1,114 @@ 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($cache_file_path, 'r'); - $cache_file_content = fread($cache_file_handle, filesize($cache_file_path)); - fclose($cache_file_handle); + // 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); + $permissions = unserialize($cache_file_content); - return $permissions; - } + return $permissions; + } - // We are updating the cache, touch the file (note: race condition possible between stating the cache file above and this line...) - touch($cache_file_path); - - // Get permission raw data from XML - $manifestDoc = new DOMDocument; - $manifestDoc->load($android_manifest_file_path); - $manifestXpath = new DOMXPath($manifestDoc); - - $stringsDoc = new DOMDocument; - $stringsDoc->load($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); - } + // 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); - } + // 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)); + $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); - } + 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; + $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 = ''; + // 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($cache_file_path, 'w'); - fwrite($cache_file_handle, serialize($permissions)); - fclose($cache_file_handle); + // Update cache with serialized permissions + $cache_file_handle = fopen($cache_file_path, 'w'); + fwrite($cache_file_handle, serialize($permissions)); + fclose($cache_file_handle); - return $permissions; + return $permissions; + } } -?> \ No newline at end of file +?> diff --git a/wp-fdroid/wp-fdroid.php b/wp-fdroid/wp-fdroid.php index e3ec52d5..48ad80e0 100644 --- a/wp-fdroid/wp-fdroid.php +++ b/wp-fdroid/wp-fdroid.php @@ -103,7 +103,8 @@ class FDroid function get_app($query_vars) { - $permissions_data = get_android_permissions_array($this->site_path.'/repo/AndroidManifest.xml', $this->site_path.'/repo/strings.xml', $this->site_path.'/repo/android-permissions.cache'); + $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'); foreach($xml->children() as $app) {