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

/Xtra/ToolScanner/functions.php

http://xrnx.googlecode.com/
PHP | 124 lines | 108 code | 11 blank | 5 comment | 12 complexity | 152012e3a7f300035e02416eb74aeb87 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, GPL-2.0
  1. <?php
  2. // ----------------------------------------------------------------------------
  3. // Generic
  4. // ----------------------------------------------------------------------------
  5. function unzip($file, $dir)
  6. {
  7. if (class_exists('ZipArchive')) {
  8. $zip = new ZipArchive();
  9. if ($zip->open($file) === true) {
  10. $_ret = $zip->extractTo($dir);
  11. $zip->close();
  12. return $_ret;
  13. }
  14. else return false;
  15. }
  16. else {
  17. // Escape
  18. $file = escapeshellarg($file);
  19. $dir = escapeshellarg($dir);
  20. $cmd = "unzip {$file} -d {$dir}"; // Info-zip assumed to be in path
  21. $res = -1; // any nonzero value
  22. $unused = array();
  23. $unused2 = exec($cmd, $unused, $res);
  24. if ($res != 0) trigger_error("Warning: unzip return value is $res ", E_USER_WARNING);
  25. return ($res == 0 || $res == 1); // http://www.info-zip.org/FAQ.html#error-codes
  26. }
  27. }
  28. function obliterate_dir($dirname)
  29. {
  30. if (!is_dir($dirname)) return false;
  31. if (isset($_ENV['OS']) && strripos($_ENV['OS'], "windows", 0) !== FALSE) {
  32. // Windows patch for buggy perimssions on some machines
  33. $command = 'cmd /C "rmdir /S /Q "'.str_replace('//', '\\', $dirname).'\\""';
  34. $wsh = new COM("WScript.Shell");
  35. $wsh->Run($command, 7, false);
  36. $wsh = null;
  37. return true;
  38. }
  39. else {
  40. $dscan = array(realpath($dirname));
  41. $darr = array();
  42. while (!empty($dscan)) {
  43. $dcur = array_pop($dscan);
  44. $darr[] = $dcur;
  45. if ($d = opendir($dcur)) {
  46. while ($f=readdir($d)) {
  47. if ($f == '.' || $f == '..') continue;
  48. $f = $dcur . '/' . $f;
  49. if (is_dir($f)) $dscan[] = $f;
  50. else unlink($f);
  51. }
  52. closedir($d);
  53. }
  54. }
  55. for ($i=count($darr)-1; $i >= 0 ; $i--) {
  56. if (!rmdir($darr[$i]))
  57. trigger_error("Warning: There was a problem deleting a temporary file in $dirname ", E_USER_WARNING);
  58. }
  59. return (!is_dir($dirname));
  60. }
  61. }
  62. function get_temp_dir() {
  63. // Try to get from environment variable
  64. if (!empty($_ENV['TMP'])) {
  65. return realpath($_ENV['TMP']);
  66. }
  67. else if (!empty($_ENV['TMPDIR'])) {
  68. return realpath($_ENV['TMPDIR']);
  69. }
  70. else if (!empty($_ENV['TEMP'])) {
  71. return realpath($_ENV['TEMP']);
  72. }
  73. else {
  74. // Detect by creating a temporary file
  75. $temp_file = tempnam(md5(uniqid(mt_rand(), true)), '');
  76. if ($temp_file) {
  77. $temp_dir = realpath(dirname($temp_file));
  78. unlink($temp_file);
  79. return $temp_dir;
  80. }
  81. else {
  82. return false;
  83. }
  84. }
  85. }
  86. function is_binary($file)
  87. {
  88. if (file_exists($file)) {
  89. if (!is_file($file)) return false;
  90. $fh = fopen($file, "r");
  91. $blk = fread($fh, 512);
  92. fclose($fh);
  93. clearstatcache();
  94. return (
  95. false ||
  96. substr_count($blk, "^\r\n")/512 > 0.3 ||
  97. substr_count($blk, "^ -~")/512 > 0.3 ||
  98. substr_count($blk, "\x00") > 0
  99. );
  100. }
  101. return false;
  102. }
  103. ?>