PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-fdroid/android-permissions.php

https://gitlab.com/fdroid/fdroidserver
PHP | 114 lines | 81 code | 19 blank | 14 comment | 15 complexity | dc3e72c5bbeb2ddb8829fd7908108c1f MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. // Class that provides PHP-friendly android permissions information from the raw Andoid source XML files that describes the permissions.
  3. class AndroidPermissions
  4. {
  5. // 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.
  6. private $android_manifest_file_path;
  7. // 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.
  8. private $android_strings_file_path;
  9. // Path to the file where the resulting permissions data will be cached. NOTE: Must be writable by PHP!
  10. private $cache_file_path;
  11. public function __construct($android_manifest_file_path_in = 'AndroidManifest.xml', $android_strings_file_path_in = 'strings.xml', $cache_file_path_in = 'android-permissions.cache') {
  12. $this->android_manifest_file_path = $android_manifest_file_path_in;
  13. $this->android_strings_file_path = $android_strings_file_path_in;
  14. $this->cache_file_path = $cache_file_path_in;
  15. }
  16. // Returns an associative array with android permissions and data about them
  17. function get_permissions_array() {
  18. // Check status of cache
  19. $android_manifest_file_stat = stat($this->android_manifest_file_path);
  20. $android_manifest_file_mtime = $android_manifest_file_stat['mtime'];
  21. $android_strings_file_stat = stat($this->android_strings_file_path);
  22. $android_strings_file_mtime = $android_strings_file_stat['mtime'];
  23. $cache_file_mtime = 0;
  24. if(file_exists($this->cache_file_path)) {
  25. $cache_file_stat = stat($this->cache_file_path);
  26. $cache_file_mtime = $cache_file_stat['mtime'];
  27. }
  28. // If the cache is fresh, use it instead
  29. if($android_manifest_file_mtime < $cache_file_mtime && $android_strings_file_mtime < $cache_file_mtime ) {
  30. $cache_file_handle = fopen($this->cache_file_path, 'r');
  31. $cache_file_content = fread($cache_file_handle, filesize($this->cache_file_path));
  32. fclose($cache_file_handle);
  33. $permissions = unserialize($cache_file_content);
  34. return $permissions;
  35. }
  36. // We are updating the cache, touch the file (note: race condition possible between stating the cache file above and this line...)
  37. touch($this->cache_file_path);
  38. // Get permission raw data from XML
  39. $manifestDoc = new DOMDocument;
  40. $manifestDoc->load($this->android_manifest_file_path);
  41. $manifestXpath = new DOMXPath($manifestDoc);
  42. $stringsDoc = new DOMDocument;
  43. $stringsDoc->load($this->android_strings_file_path);
  44. $stringsXpath = new DOMXPath($stringsDoc);
  45. $comment = '';
  46. foreach ($manifestXpath->query('node()') as $node) {
  47. // Save permissions and permission groups from tags
  48. if($node->nodeName == 'permission-group' || $node->nodeName == 'permission') {
  49. $name = $node->attributes->getNamedItem('name')->value;
  50. $name = substr(strrchr($name,'.'), 1);
  51. // Lookup the human readable title
  52. $labelObject = $node->attributes->getNamedItem('label');
  53. $labelString = $name;
  54. if( $labelObject !== NULL ) {
  55. $labelName = substr(strrchr($labelObject->value,'/'),1);
  56. $labelStringObject = $stringsXpath->query('//string[@name="'.$labelName.'"]');
  57. $labelString = ucfirst($labelStringObject->item(0)->nodeValue);
  58. }
  59. // Lookup the human readable description
  60. $descriptionObject = $node->attributes->getNamedItem('description');
  61. $descriptionString = '(Description missing)';
  62. if($descriptionObject !== NULL) {
  63. $descriptionName = substr(strrchr($descriptionObject->value,'/'),1);
  64. $descriptionStringObject = $stringsXpath->query('//string[@name="'.$descriptionName.'"]');
  65. $descriptionString = ucfirst($descriptionStringObject->item(0)->nodeValue);
  66. }
  67. $permissions[$node->nodeName][$name]['label'] = stripslashes($labelString);
  68. $permissions[$node->nodeName][$name]['description'] = stripslashes($descriptionString);
  69. $permissions[$node->nodeName][$name]['comment'] = stripslashes(str_replace(array("\r\n", "\r", "\n", "\t", ' '), '', $comment));
  70. if($node->nodeName == 'permission') {
  71. $permissionGroupObject = $node->attributes->getNamedItem('permissionGroup');
  72. $permissionGroup = 'none';
  73. if($permissionGroupObject !== NULL) {
  74. $permissionGroup = substr(strrchr($permissionGroupObject->value,'.'), 1);
  75. }
  76. $permissions[$node->nodeName][$name]['permissionGroup'] = $permissionGroup;
  77. $permissions[$node->nodeName][$name]['protectionLevel'] = $node->attributes->getNamedItem('protectionLevel')->value;
  78. }
  79. }
  80. // Cache descriptions from comments preceding the tags
  81. if($node->nodeName == '#comment') {
  82. $comment .= $node->textContent;
  83. }
  84. elseif($node->nodeName != '#text') {
  85. $comment = '';
  86. }
  87. }
  88. // Update cache with serialized permissions
  89. $cache_file_handle = fopen($this->cache_file_path, 'w');
  90. fwrite($cache_file_handle, serialize($permissions));
  91. fclose($cache_file_handle);
  92. return $permissions;
  93. }
  94. }
  95. ?>