PageRenderTime 26ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/duplicator/inc/functions.php

https://bitbucket.org/zachisit/zachis.it-m
PHP | 367 lines | 237 code | 54 blank | 76 comment | 42 complexity | f7b5b440531072d4bb2e0af7bda240e5 MD5 | raw file
  1. <?php
  2. /**
  3. * DUPLICATOR_CREATE_DBSCRIPT
  4. * Create the SQL DataDump File
  5. * @param string $destination The full path and filname where the sql script will be written
  6. */
  7. function duplicator_create_dbscript($destination) {
  8. try {
  9. global $wpdb;
  10. $dbiconv = ($GLOBALS['duplicator_opts']['dbiconv'] == "0" && function_exists("iconv")) ? false : true;
  11. $handle = fopen($destination,'w+');
  12. $tables = $wpdb->get_col('SHOW TABLES');
  13. duplicator_log("log:fun__create_dbscript=>started");
  14. if ($dbiconv) {
  15. duplicator_log("log:fun__create_dbscript=>dbiconv enabled");
  16. }
  17. foreach ($tables as $table) {
  18. //Generate Drop Statement
  19. //$sql_del = ($GLOBALS['duplicator_opts']['dbadd_drop']) ? "DROP TABLE IF EXISTS {$table};\n\n" : "";
  20. //@fwrite($handle, $sql_del);
  21. //Generate Create Statement
  22. $row_count = $wpdb->get_var("SELECT Count(*) FROM `{$table}`");
  23. duplicator_log("start: {$table} ({$row_count})");
  24. $create = $wpdb->get_row("SHOW CREATE TABLE `{$table}`", ARRAY_N);
  25. $sql_crt = "{$create[1]};\n\n";
  26. @fwrite($handle, $sql_crt);
  27. if ($row_count > 100) {
  28. $row_count = ceil($row_count / 100);
  29. } else if ($row_count > 0) {
  30. $row_count = 1;
  31. }
  32. //PERFORM ICONV ROUTINE
  33. //Chunck the query results to avoid memory issues
  34. if ($dbiconv) {
  35. for ($i = 0; $i < $row_count; $i++) {
  36. $sql = "";
  37. $limit = $i * 100;
  38. $query = "SELECT * FROM `{$table}` LIMIT {$limit}, 100";
  39. $rows = $wpdb->get_results($query, ARRAY_A);
  40. if (is_array($rows)) {
  41. foreach ($rows as $row) {
  42. $sql .= "INSERT INTO `{$table}` VALUES(";
  43. $num_values = count($row);
  44. $num_counter = 1;
  45. foreach ($row as $value) {
  46. $value = @iconv(DUPLICATOR_DB_ICONV_IN, DUPLICATOR_DB_ICONV_OUT, $value);
  47. ($num_values == $num_counter)
  48. ? $sql .= '"' . @mysql_real_escape_string($value) . '"'
  49. : $sql .= '"' . @mysql_real_escape_string($value) . '", ';
  50. $num_counter++;
  51. }
  52. $sql .= ");\n";
  53. }
  54. @fwrite($handle, $sql);
  55. duplicator_fcgi_flush();
  56. }
  57. }
  58. //DO NOT PERFORM ICONV
  59. } else {
  60. for ($i = 0; $i < $row_count; $i++) {
  61. $sql = "";
  62. $limit = $i * 100;
  63. $query = "SELECT * FROM `{$table}` LIMIT {$limit}, 100";
  64. $rows = $wpdb->get_results($query, ARRAY_A);
  65. if (is_array($rows)) {
  66. foreach ($rows as $row) {
  67. $sql .= "INSERT INTO `{$table}` VALUES(";
  68. $num_values = count($row);
  69. $num_counter = 1;
  70. foreach ($row as $value) {
  71. ($num_values == $num_counter)
  72. ? $sql .= '"' . @mysql_real_escape_string($value) . '"'
  73. : $sql .= '"' . @mysql_real_escape_string($value) . '", ';
  74. $num_counter++;
  75. }
  76. $sql .= ");\n";
  77. }
  78. @fwrite($handle, $sql);
  79. duplicator_fcgi_flush();
  80. }
  81. }
  82. }
  83. @fwrite($handle, "\n\n");
  84. duplicator_log("done: {$table}");
  85. }
  86. duplicator_log("log:fun__create_dbscript=>sql file written to {$destination}");
  87. fclose($handle);
  88. $wpdb->flush();
  89. duplicator_log("log:fun__create_dbscript=>ended");
  90. } catch(Exception $e) {
  91. duplicator_log("log:fun__create_dbscript=>runtime error: " . $e);
  92. }
  93. }
  94. /**
  95. * DUPLICATOR_CREATE_INSTALLERFILE
  96. * Prep the Installer file for use. use %string% token for replacing
  97. * @param string $uniquename The unique name this installer file will be associated with
  98. */
  99. function duplicator_create_installerFile($uniquename) {
  100. duplicator_log("log:fun__create_installerFile=>started");
  101. global $wpdb;
  102. $template = duplicator_safe_path(DUPLICATOR_PLUGIN_PATH . 'files/installer.template.php');
  103. $installerRescue = duplicator_safe_path(DUPLICATOR_PLUGIN_PATH . 'files/installer.rescue.php');
  104. $installerCore = duplicator_safe_path(DUPLICATOR_SSDIR_PATH) . "/{$uniquename}_installer.php";
  105. $err_msg = "\n!!!WARNING!!! unable to read/write installer\nSee file:{$installerCore} \nPlease check permission and owner on file and parent folder.";
  106. get_option('duplicator_options') == "" ? "" : $duplicator_opts = unserialize(get_option('duplicator_options'));
  107. $replace_items = Array(
  108. "fwrite_current_url" => get_option('siteurl'),
  109. "fwrite_package_name" => "{$uniquename}_package.zip",
  110. "fwrite_secure_name" => "{$uniquename}",
  111. "fwrite_nurl" => $duplicator_opts['nurl'],
  112. "fwrite_dbhost" => $duplicator_opts['dbhost'],
  113. "fwrite_dbname" => $duplicator_opts['dbname'],
  114. "fwrite_dbuser" => $duplicator_opts['dbuser'],
  115. "fwrite_wp_tableprefix" => $wpdb->prefix,
  116. "fwrite_site_title" => get_option('blogname'),
  117. "fwrite_rescue_flag" => "");
  118. if( file_exists($template) && is_readable($template)) {
  119. $install_str = duplicator_parse_template($template, $replace_items);
  120. if (empty($install_str)) {
  121. die(duplicator_log("log:fun__create_installerFile=>file-empty-read" . $err_msg));
  122. }
  123. //RESCUE FILE
  124. $replace_items["fwrite_rescue_flag"] = '(rescue file)';
  125. $rescue_str = duplicator_parse_template($template, $replace_items);
  126. $fp = fopen($installerRescue, (!file_exists($installerRescue)) ? 'x+' : 'w');
  127. @fwrite($fp, $rescue_str, strlen($rescue_str));
  128. @fclose($fp);
  129. $rescue_str = null;
  130. //INSTALLER FILE
  131. if (!file_exists($installerCore)) {
  132. $fp2 = fopen($installerCore, 'x+') or die(duplicator_log("log:fun__create_installerFile=>file-open-error-x" . $err_msg));
  133. } else {
  134. $fp2 = fopen($installerCore, 'w') or die(duplicator_log("log:fun__create_installerFile=>file-open-error-w" . $err_msg));
  135. }
  136. if (fwrite($fp2, $install_str, strlen($install_str))) {
  137. duplicator_log("log:fun__create_installerFile=>installer.php updated at: {$installerCore}");
  138. } else {
  139. duplicator_log("log:fun__create_installerFile=>file-create-error" . $err_msg);
  140. }
  141. @fclose($fp2);
  142. }
  143. else
  144. {
  145. die(duplicator_log("log:fun__create_installerFile=>Template missing or unreadable: '$template'"));
  146. }
  147. duplicator_log("log:fun__create_installerFile=>ended");
  148. }
  149. /**
  150. * DUPLICATOR_PARSE_TEMPLATE
  151. * Tokenize a file based on an array key
  152. *
  153. * @param string $filename The filename to tokenize
  154. * @param array $data The array of key value items to tokenize
  155. */
  156. function duplicator_parse_template($filename, $data) {
  157. $q = file_get_contents($filename);
  158. foreach ($data as $key => $value) {
  159. $q = str_replace('%'.$key.'%', $value, $q);
  160. }
  161. return $q;
  162. }
  163. /**
  164. * DUPLICATOR_BYTESIZE
  165. * Display human readable byte sizes
  166. * @param string $size The size in bytes
  167. */
  168. function duplicator_bytesize($size) {
  169. try {
  170. $units = array('B', 'KB', 'MB', 'GB', 'TB');
  171. for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
  172. return round($size, 2).$units[$i];
  173. } catch (Exception $e) {
  174. return "n/a";
  175. }
  176. }
  177. /**
  178. * DUPLICATOR_DIRSIZE
  179. * Get the directory size recursively, but don't calc the snapshot directory, exclusion diretories
  180. * @param string $directory The directory to calculate
  181. */
  182. function duplicator_dirInfo($directory) {
  183. try {
  184. $size = 0;
  185. $count = 0;
  186. $folders = 0;
  187. $flag = false;
  188. //EXCLUDE: Snapshot directory
  189. $directory = duplicator_safe_path($directory);
  190. if( strstr($directory, DUPLICATOR_SSDIR_PATH)) {
  191. return;
  192. }
  193. //EXCLUDE: Directory Exclusions List
  194. if ($GLOBALS['duplicator_bypass-array'] != null) {
  195. foreach ($GLOBALS['duplicator_bypass-array'] as $val) {
  196. if (duplicator_safe_path($val) == $directory) {
  197. return;
  198. }
  199. }
  200. }
  201. if ($handle = @opendir($directory)) {
  202. while (false !== ($file = @readdir($handle))) {
  203. if ($file != '.' && $file != '..') {
  204. $nextpath = $directory . '/' . $file;
  205. if (is_dir($nextpath)) {
  206. $folders++;
  207. $result = duplicator_dirInfo($nextpath);
  208. $size += $result['size'];
  209. $count += $result['count'];
  210. $folders += $result['folders'];
  211. }
  212. else if (is_file($nextpath) && is_readable($nextpath)) {
  213. if(!in_array(@pathinfo($nextpath, PATHINFO_EXTENSION), $GLOBALS['duplicator_skip_ext-array'])) {
  214. $fmod = @filesize($nextpath);
  215. if ($fmod === false) {
  216. $flag = true;
  217. } else {
  218. $size += @filesize($nextpath);
  219. }
  220. $count++;
  221. }
  222. }
  223. }
  224. }
  225. }
  226. closedir($handle);
  227. $total['size'] = $size;
  228. $total['count'] = $count;
  229. $total['folders'] = $folders;
  230. $total['flag'] = $flag;
  231. return $total;
  232. } catch(Exception $e) {
  233. duplicator_log("log:fun__dirInfo=>runtime error: " . $e . "\nNOTE: Try excluding the stat failed to the Duplicators directory exclusion list or change the permissions.");
  234. }
  235. }
  236. /**
  237. * DUPLICATOR_CREATE_SNAPSHOTPATH
  238. * Creates the snapshot directory if it doesn't already exisit
  239. */
  240. function duplicator_init_snapshotpath() {
  241. $path_wproot = duplicator_safe_path(DUPLICATOR_WPROOTPATH);
  242. $path_ssdir = duplicator_safe_path(DUPLICATOR_SSDIR_PATH);
  243. $path_plugin = duplicator_safe_path(DUPLICATOR_PLUGIN_PATH);
  244. //--------------------------------
  245. //CHMOD DIRECTORY ACCESS
  246. //wordpress root directory
  247. @chmod($path_wproot , 0755);
  248. //snapshot directory
  249. @mkdir($path_ssdir, 0755);
  250. @chmod($path_ssdir, 0755);
  251. //plugins dir/files
  252. @chmod($path_plugin . 'files', 0755);
  253. @chmod(duplicator_safe_path($path_plugin . 'files/installer.rescue.php'), 0644);
  254. //--------------------------------
  255. //FILE CREATION
  256. //SSDIR: Create Index File
  257. $ssfile = @fopen($path_ssdir .'/index.php', 'w');
  258. @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(); ?>');
  259. @fclose($ssfile);
  260. //SSDIR: Create token file in snapshot
  261. $tokenfile = @fopen($path_ssdir .'/dtoken.php', 'w');
  262. @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(); ?>');
  263. @fclose($tokenfile);
  264. //SSDIR: Create .htaccess
  265. $htfile = @fopen($path_ssdir .'/.htaccess', 'w');
  266. @fwrite($htfile, "Options -Indexes");
  267. @fclose($htfile);
  268. //SSDIR: Robots.txt file
  269. $robotfile = @fopen($path_ssdir .'/robots.txt', 'w');
  270. @fwrite($robotfile, "User-agent: * \nDisallow: /" . DUPLICATOR_SSDIR_NAME . '/');
  271. @fclose($robotfile);
  272. //PLUG DIR: Create token file in plugin
  273. $tokenfile2 = @fopen($path_plugin .'files/dtoken.php', 'w');
  274. @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"); ?>');
  275. @fclose($tokenfile2);
  276. }
  277. /**
  278. * DUPLICATOR_SAFE_PATH
  279. * Makes path safe for any OS
  280. * Paths should ALWAYS READ be "/"
  281. * uni: /home/path/file.xt
  282. * win: D:/home/path/file.txt
  283. * @param string $path The path to make safe
  284. */
  285. function duplicator_safe_path($path) {
  286. return str_replace("\\", "/", $path);
  287. }
  288. /**
  289. * DUPLICATOR_FCGI_FLUSH
  290. * PHP_SAPI for fcgi requires a data flush of at least 256
  291. * bytes every 40 seconds or else it forces a script hault
  292. */
  293. function duplicator_fcgi_flush() {
  294. echo(str_repeat(' ',256));
  295. @flush();
  296. }
  297. /**
  298. * DUPLICATOR_SNAPSHOT_URLPATH
  299. * returns the snapshot url
  300. */
  301. function duplicator_snapshot_urlpath() {
  302. return get_site_url(null, '', is_ssl() ? 'https' : 'http') . '/' . DUPLICATOR_SSDIR_NAME . '/' ;
  303. }
  304. /**
  305. * DUPLICATOR_LOG
  306. * Centralized logging method
  307. * @param string $msg The message to log
  308. */
  309. function duplicator_log($msg, $level = 0) {
  310. $stamp = date('h:i:s');
  311. @fwrite($GLOBALS['duplicator_package_log_handle'], "{$stamp} {$msg} \n");
  312. }
  313. ?>