PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/wp-content/plugins/duplicator/classes/utility.php

https://github.com/Fishgate/privatecollectionswp
PHP | 230 lines | 100 code | 25 blank | 105 comment | 14 complexity | e29573ef897aa177dd0d07fc2059f9ef MD5 | raw file
  1. <?php
  2. if ( ! defined( 'DUPLICATOR_VERSION' ) ) exit; // Exit if accessed directly
  3. class DUP_Util {
  4. /**
  5. * PHP_SAPI for fcgi requires a data flush of at least 256
  6. * bytes every 40 seconds or else it forces a script hault
  7. */
  8. static public function FcgiFlush() {
  9. echo(str_repeat(' ', 300));
  10. @flush();
  11. }
  12. /**
  13. * returns the snapshot url
  14. */
  15. static public function SSDirURL() {
  16. return get_site_url(null, '', is_ssl() ? 'https' : 'http') . '/' . DUPLICATOR_SSDIR_NAME . '/';
  17. }
  18. /**
  19. * Runs the APC cache to pre-cache the php files
  20. * returns true if all files where cached
  21. */
  22. static public function RunAPC() {
  23. if(function_exists('apc_compile_file')){
  24. $file01 = @apc_compile_file(DUPLICATOR_PLUGIN_PATH . "duplicator.php");
  25. return ($file01);
  26. } else {
  27. return false;
  28. }
  29. }
  30. /**
  31. * Display human readable byte sizes
  32. * @param string $size The size in bytes
  33. */
  34. static public function ByteSize($size) {
  35. try {
  36. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  37. for ($i = 0; $size >= 1024 && $i < 4; $i++)
  38. $size /= 1024;
  39. return round($size, 2) . $units[$i];
  40. } catch (Exception $e) {
  41. return "n/a";
  42. }
  43. }
  44. /**
  45. * Makes path safe for any OS
  46. * Paths should ALWAYS READ be "/"
  47. * uni: /home/path/file.xt
  48. * win: D:/home/path/file.txt
  49. * @param string $path The path to make safe
  50. */
  51. static public function SafePath($path) {
  52. return str_replace("\\", "/", $path);
  53. }
  54. /**
  55. * Get current microtime as a float. Can be used for simple profiling.
  56. */
  57. static public function GetMicrotime() {
  58. return microtime(true);
  59. }
  60. /**
  61. * Append the value to the string if it doesn't already exist
  62. */
  63. static public function StringAppend($string, $value ) {
  64. return $string . (substr($string, -1) == $value ? '' : $value);
  65. }
  66. /**
  67. * Return a string with the elapsed time.
  68. * Order of $end and $start can be switched.
  69. */
  70. static public function ElapsedTime($end, $start) {
  71. return sprintf("%.2f sec.", abs($end - $start));
  72. }
  73. /**
  74. * Get the MySQL system variables
  75. * @param conn $dbh Database connection handle
  76. * @return string the server variable to query for
  77. */
  78. static public function MysqlVariableValue($variable) {
  79. global $wpdb;
  80. $row = $wpdb->get_row("SHOW VARIABLES LIKE '{$variable}'", ARRAY_N);
  81. return isset($row[1]) ? $row[1] : null;
  82. }
  83. /**
  84. * List all of the directories of a path
  85. * @path path to a system directory
  86. * @return array of all directories in that path
  87. */
  88. static public function ListDirs($path = '.') {
  89. $dirs = array();
  90. foreach (new DirectoryIterator($path) as $file) {
  91. if ($file->isDir() && !$file->isDot()) {
  92. $dirs[] = DUP_Util::SafePath($file->getPathname());
  93. }
  94. }
  95. return $dirs;
  96. }
  97. /**
  98. * Does the directory have content
  99. */
  100. static public function IsDirectoryEmpty($dir) {
  101. if (!is_readable($dir)) return NULL;
  102. return (count(scandir($dir)) == 2);
  103. }
  104. /**
  105. * Size of the directory recuresivly in bytes
  106. */
  107. static public function GetDirectorySize($dir) {
  108. if(!file_exists($dir))
  109. return 0;
  110. if(is_file($dir))
  111. return filesize($dir);
  112. $size = 0;
  113. $list = glob($dir."/*");
  114. if (! empty($list)) {
  115. foreach($list as $file)
  116. $size += self::GetDirectorySize($file);
  117. }
  118. return $size;
  119. }
  120. public static function IsShellExecAvailable() {
  121. if (array_intersect(array('shell_exec', 'escapeshellarg', 'escapeshellcmd', 'extension_loaded'), array_map('trim', explode(',', @ini_get('disable_functions')))))
  122. return false;
  123. //Suhosin: http://www.hardened-php.net/suhosin/
  124. //Will cause PHP to silently fail.
  125. if (extension_loaded('suhosin'))
  126. return false;
  127. // Can we issue a simple echo command?
  128. if (!@shell_exec('echo duplicator'))
  129. return false;
  130. return true;
  131. }
  132. public static function IsOSWindows() {
  133. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  134. return true;
  135. }
  136. return false;
  137. }
  138. public static function GetCurrentUser() {
  139. $unreadable = '(Undetectable)';
  140. if (@function_exists('get_current_user') && @is_callable('get_current_user')) {
  141. $user = @get_current_user();
  142. return strlen($user) ? $user : $unreadable;
  143. }
  144. return $unreadable;
  145. }
  146. /**
  147. * Creates the snapshot directory if it doesn't already exisit
  148. */
  149. static public function InitSnapshotDirectory() {
  150. $path_wproot = DUP_Util::SafePath(DUPLICATOR_WPROOTPATH);
  151. $path_ssdir = DUP_Util::SafePath(DUPLICATOR_SSDIR_PATH);
  152. $path_plugin = DUP_Util::SafePath(DUPLICATOR_PLUGIN_PATH);
  153. //--------------------------------
  154. //CHMOD DIRECTORY ACCESS
  155. //wordpress root directory
  156. @chmod($path_wproot, 0755);
  157. //snapshot directory
  158. @mkdir($path_ssdir, 0755);
  159. @chmod($path_ssdir, 0755);
  160. //snapshot tmp directory
  161. $path_ssdir_tmp = $path_ssdir . '/tmp';
  162. @mkdir($path_ssdir_tmp, 0755);
  163. @chmod($path_ssdir_tmp, 0755);
  164. //plugins dir/files
  165. @chmod($path_plugin . 'files', 0755);
  166. //--------------------------------
  167. //FILE CREATION
  168. //SSDIR: Create Index File
  169. $ssfile = @fopen($path_ssdir . '/index.php', 'w');
  170. @fwrite($ssfile, '<?php error_reporting(0); if (stristr(php_sapi_name(), "fcgi")) { $url = "http://" . $_SERVER["HTTP_HOST"]; header("Location: {$url}/404.html");} else { header("HTML/1.1 404 Not Found", true, 404);} exit(); ?>');
  171. @fclose($ssfile);
  172. //SSDIR: Create token file in snapshot
  173. $tokenfile = @fopen($path_ssdir . '/dtoken.php', 'w');
  174. @fwrite($tokenfile, '<?php error_reporting(0); if (stristr(php_sapi_name(), "fcgi")) { $url = "http://" . $_SERVER["HTTP_HOST"]; header("Location: {$url}/404.html");} else { header("HTML/1.1 404 Not Found", true, 404);} exit(); ?>');
  175. @fclose($tokenfile);
  176. //SSDIR: Create .htaccess
  177. $storage_htaccess_off = DUP_Settings::Get('storage_htaccess_off');
  178. if ($storage_htaccess_off) {
  179. @unlink($path_ssdir . '/.htaccess');
  180. } else {
  181. $htfile = @fopen($path_ssdir . '/.htaccess', 'w');
  182. $htoutput = "Options -Indexes" ;
  183. @fwrite($htfile, $htoutput);
  184. @fclose($htfile);
  185. }
  186. //SSDIR: Robots.txt file
  187. $robotfile = @fopen($path_ssdir . '/robots.txt', 'w');
  188. @fwrite($robotfile, "User-agent: * \nDisallow: /" . DUPLICATOR_SSDIR_NAME . '/');
  189. @fclose($robotfile);
  190. //PLUG DIR: Create token file in plugin
  191. $tokenfile2 = @fopen($path_plugin . 'installer/dtoken.php', 'w');
  192. @fwrite($tokenfile2, '<?php @error_reporting(0); @require_once("../../../../wp-admin/admin.php"); global $wp_query; $wp_query->set_404(); header("HTML/1.1 404 Not Found", true, 404); header("Status: 404 Not Found"); @include(get_template_directory () . "/404.php"); ?>');
  193. @fclose($tokenfile2);
  194. }
  195. }
  196. ?>