PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/zachisit/zachis.it-m
PHP | 339 lines | 221 code | 54 blank | 64 comment | 23 complexity | 8027bde35cd55ebcdefc095042580e37 MD5 | raw file
  1. <?php
  2. /**
  3. * DUPLICATOR_CREATE
  4. * Creates the zip file, database entry, and installer file for the
  5. * new culmination of a 'Package Set'
  6. *
  7. * @return string A message about the action
  8. * - log:act__create=>done
  9. */
  10. function duplicator_create() {
  11. global $wp_version;
  12. global $wpdb;
  13. global $current_user;
  14. $packname = isset($_POST['package_name']) ? trim($_POST['package_name']) : null;
  15. $secure_token = uniqid() . mt_rand(1000, 9999);
  16. $uniquename = "{$secure_token}_{$packname}";
  17. foreach(glob(DUPLICATOR_SSDIR_PATH . '/*.log') as $log_file){
  18. @unlink($log_file);
  19. }
  20. $logfilename = "{$uniquename}.log";
  21. $GLOBALS['duplicator_package_log_handle'] = @fopen(DUPLICATOR_SSDIR_PATH . "/{$logfilename}", "c+");
  22. duplicator_log("*********************************************************");
  23. duplicator_log("PACKAGE-LOG: ");
  24. duplicator_log("*********************************************************");
  25. duplicator_log("duplicator: " . DUPLICATOR_VERSION);
  26. duplicator_log("wordpress: {$wp_version}");
  27. duplicator_log("php: " . phpversion());
  28. duplicator_log("php sapi: " . php_sapi_name());
  29. duplicator_log("server: {$_SERVER['SERVER_SOFTWARE']}");
  30. duplicator_log("browser: {$_SERVER['HTTP_USER_AGENT']}");
  31. duplicator_log("package name: {$packname}");
  32. if($packname) {
  33. $max_time = @ini_set("max_execution_time", $GLOBALS['duplicator_opts']['max_time']);
  34. $max_memory = @ini_set('memory_limit', $GLOBALS['duplicator_opts']['max_memory']);
  35. $max_time = ($max_time === false) ? "Unabled to set max_execution_time" : "from={$max_time} to={$GLOBALS['duplicator_opts']['max_time']}";
  36. $max_memory = ($max_memory === false) ? "Unabled to set memory_limit" : "from={$max_memory} to={$GLOBALS['duplicator_opts']['max_memory']}";
  37. @set_time_limit(0);
  38. duplicator_log("max_time: {$max_time}");
  39. duplicator_log("max_memory: {$max_memory}");
  40. $zipfilename = "{$uniquename}_package.zip";
  41. $sqlfilename = "{$uniquename}_database.sql";
  42. $exefilename = "{$uniquename}_installer.php";
  43. $zipfilepath = DUPLICATOR_SSDIR_PATH . "/{$zipfilename}";
  44. $sqlfilepath = DUPLICATOR_SSDIR_PATH . "/{$sqlfilename}";
  45. $exefilepath = DUPLICATOR_SSDIR_PATH . "/{$exefilename}";
  46. $zipsize = 0;
  47. duplicator_log("mysql wait_timeout: {$GLOBALS['duplicator_opts']['max_time']}");
  48. $wpdb->query("SET session wait_timeout = {$GLOBALS['duplicator_opts']['max_time']}");
  49. duplicator_log("*********************************************************");
  50. duplicator_log("SQL SCRIPT");
  51. duplicator_log("*********************************************************");
  52. duplicator_create_dbscript($sqlfilepath);
  53. //CREATE ZIP ARCHIVE
  54. duplicator_log("*********************************************************");
  55. duplicator_log("ZIP ARCHIVE");
  56. duplicator_log("*********************************************************");
  57. $zip = new Duplicator_Zip($zipfilepath, rtrim(DUPLICATOR_WPROOTPATH, '/'), $sqlfilepath);
  58. $zipsize = filesize($zipfilepath);
  59. ($zipsize == false)
  60. ? duplicator_log("log:act__create=>warning: zipsize is unknown.")
  61. : duplicator_log("log:act__create=>zip file size is: " . duplicator_bytesize($zipsize));
  62. duplicator_log("log:act__create=>zip archive complete.", 2);
  63. //Serlized settings
  64. $settings = array('plugin_version' => DUPLICATOR_VERSION,
  65. 'type' => 'Manual');
  66. $serialized_settings = serialize($settings);
  67. //Record archive info to database
  68. $results = $wpdb->insert($wpdb->prefix . "duplicator",
  69. array(
  70. 'token' => $secure_token,
  71. 'packname' => $packname,
  72. 'zipname' => $zipfilename,
  73. 'zipsize' => $zipsize,
  74. 'created' => current_time('mysql', get_option('gmt_offset')),
  75. 'owner' => $current_user->user_login,
  76. 'settings' => "{$serialized_settings}")
  77. );
  78. if ($wpdb->insert_id) {
  79. duplicator_log("log:act__create=>recorded archieve id: " . $wpdb->insert_id);
  80. } else {
  81. duplicator_log("log:act__create=>unable to record to database.");
  82. }
  83. $wpdb->flush();
  84. //UPDATE INSTALL FILE
  85. duplicator_log("*********************************************************");
  86. duplicator_log("UPDATE INSTALLER FILE");
  87. duplicator_log("*********************************************************");
  88. duplicator_create_installerFile($uniquename);
  89. //SEND EMAIL
  90. //TODO: Send only SQL File via mail. Zip files can get too large
  91. if( $GLOBALS['duplicator_opts']['email-me'] == "1" ) {
  92. duplicator_log("log:act__create=>email started");
  93. $status = ($zipsize) ? 'Success' : 'Failure';
  94. $attachments = ""; //array(DUPLICATOR_SSDIR_PATH . '/' . $packname .'.zip');
  95. $headers = 'From: Duplicator Plugin <no-reply@lifeinthegrid.com>' ."\r\n";
  96. $subject = "Package '{$packname}' completed";
  97. $message = "Run Status: {$status}\n\rSite Name: " . get_bloginfo('name') . "\n\rPackage Name: {$packname} \n\rCompleted at: " .current_time('mysql',get_option('gmt_offset')) ;
  98. wp_mail($current_user->user_email, $subject, $message, $headers, $attachments);
  99. duplicator_log("log:act__create=>sent email to: {$current_user->user_email}");
  100. $other_emails = @preg_split("/[,;]/", $GLOBALS['duplicator_opts']['email_others']);
  101. if (count($other_emails)) {
  102. wp_mail($other_emails, $subject, $message, $headers, $attachments);
  103. duplicator_log("log:act__create=>other emails sent: {$GLOBALS['duplicator_opts']['email_others']}");
  104. }
  105. duplicator_log("log:act__create=>email finished");
  106. }
  107. }
  108. duplicator_log("*********************************************************");
  109. duplicator_log("DONE PROCESSING => {$packname}");
  110. duplicator_log("*********************************************************");
  111. @fclose($GLOBALS['duplicator_package_log_handle']);
  112. die();
  113. }
  114. /**
  115. * DUPLICATOR_DELETE
  116. * Deletes the zip file and database record entries for the
  117. * selected ids. Supports 1 to many deletes
  118. *
  119. * @return string A message about the action.
  120. * - see: duplicator_unlink
  121. */
  122. function duplicator_delete() {
  123. try
  124. {
  125. $uniqueid = isset($_POST['duplicator_delid']) ? trim($_POST['duplicator_delid']) : null;
  126. if ($uniqueid != null) {
  127. $unique_list = explode(",", $uniqueid);
  128. foreach ($unique_list as $id) {
  129. $msg = duplicator_unlink($id);
  130. }
  131. }
  132. die($msg);
  133. }
  134. catch(Exception $e)
  135. {
  136. die("log:fun__delete=>runtime error: " . $e);
  137. }
  138. }
  139. /**
  140. * DUPLICATOR_SYSTEM_CHECK
  141. * Check to see if the package already exsits or required files
  142. * are installed. Also check for package size
  143. *
  144. * @return string A message about the action
  145. * - log:act__system_check=>create new package
  146. * - log:act__system_check=>overwrite
  147. */
  148. function duplicator_system_check() {
  149. global $wpdb;
  150. @set_time_limit($GLOBALS['duplicator_opts']['max_time']);
  151. duplicator_init_snapshotpath();
  152. $json = array();
  153. //SYS-100: FILE PERMS
  154. $test = is_readable(DUPLICATOR_WPROOTPATH)
  155. && is_writeable(DUPLICATOR_SSDIR_PATH)
  156. && is_writeable(DUPLICATOR_PLUGIN_PATH . 'files/')
  157. && is_writeable(DUPLICATOR_PLUGIN_PATH . 'files/installer.rescue.php');
  158. $json['SYS-100'] = ($test) ? 'Pass' : 'Fail';
  159. //SYS-101 RESERVED FILE
  160. $phpFile = file_exists(DUPLICATOR_WPROOTPATH . DUPLICATOR_INSTALL_PHP) ? DUPLICATOR_INSTALL_PHP : "";
  161. $sqlFile = file_exists(DUPLICATOR_WPROOTPATH . DUPLICATOR_INSTALL_SQL) ? DUPLICATOR_INSTALL_SQL : "";
  162. $logFile = file_exists(DUPLICATOR_WPROOTPATH . DUPLICATOR_INSTALL_LOG) ? DUPLICATOR_INSTALL_LOG : "";
  163. $test = ! (strlen($phpFile) || strlen($sqlFile ) || strlen($logFile));
  164. $json['SYS-101'] = ($test) ? 'Pass' : 'Fail';
  165. //SYS-102 ZIP-ARCHIVE
  166. $test = class_exists('ZipArchive');
  167. $json['SYS-102'] = ($test) ? 'Pass' : 'Fail';
  168. //SYS-103 SAFE MODE
  169. $test = ini_get('safe_mode');;
  170. $json['SYS-103'] = ! ($test) ? 'Pass' : 'Fail';
  171. //SYS-104 MYSQLI SUPPORT
  172. $test = function_exists('mysqli_connect');
  173. $json['SYS-104'] = ($test) ? 'Pass' : 'Fail';
  174. $result = in_array('Fail', $json);
  175. $json['Success'] = ! $result;
  176. die(json_encode($json));
  177. }
  178. /**
  179. * DUPLICATOR_SYSTEM_DIRECTORY
  180. * Returns the directory size and file count for the root directory minus
  181. * any of the filters
  182. *
  183. * @return json size and file count of directory
  184. */
  185. function duplicator_system_directory() {
  186. $json = array();
  187. $dirInfo = duplicator_dirInfo(rtrim(duplicator_safe_path(DUPLICATOR_WPROOTPATH), '/'));
  188. $dirSizeFormat = duplicator_bytesize($dirInfo['size']) or "0";
  189. $dirCountFormat = number_format($dirInfo['count']) or "unknown";
  190. $dirFolderFormat = number_format($dirInfo['folders']) or "unknown";
  191. $json['size'] = $dirSizeFormat;
  192. $json['count'] = $dirCountFormat;
  193. $json['folders'] = $dirFolderFormat;
  194. $json['flag'] = $dirInfo['flag'];
  195. die(json_encode($json));
  196. }
  197. /**
  198. * DUPLICATOR_UNLINK
  199. * Removes the package
  200. *
  201. * @param string $file The file name of the file to delete.
  202. * @param string $path The full path and file name of the file to delete
  203. *
  204. * @return string A message about the action
  205. * - log:act__unlink=>removed
  206. * - log:act__unlink=>file not found
  207. */
  208. function duplicator_unlink($uniqueid) {
  209. try
  210. {
  211. $msg = "log:act__unlink=>file not found";
  212. if($uniqueid) {
  213. global $wpdb;
  214. $table_name = $wpdb->prefix . 'duplicator';
  215. if($wpdb->query("DELETE FROM {$table_name} WHERE zipname= '{$uniqueid}_package.zip'" ) != 0) {
  216. $msg = "log:act__unlink=>removed";
  217. try {
  218. @chmod(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_package.zip"), 0644);
  219. @chmod(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_database.sql"), 0644);
  220. @unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_package.zip"));
  221. @unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_database.sql"));
  222. @unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}_installer.php"));
  223. }
  224. catch(Exception $e) {
  225. error_log(var_dump($e->getMessage()));
  226. }
  227. //Check for Legacy pre 0.3.1
  228. } else if($wpdb->query("DELETE FROM {$table_name} WHERE zipname= '{$uniqueid}'" ) != 0) {
  229. try {
  230. @chmod(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}"), 0644);
  231. @unlink(duplicator_safe_path(DUPLICATOR_SSDIR_PATH . "/{$uniqueid}"));
  232. } catch(Exception $e) {
  233. error_log(var_dump($e->getMessage()));
  234. }
  235. }
  236. }
  237. return $msg;
  238. }
  239. catch(Exception $e)
  240. {
  241. die("log:fun__unlink=>runtime error: " . $e);
  242. }
  243. }
  244. /**
  245. * DUPLICATOR_SETTINGS
  246. * Saves plugin settings
  247. *
  248. * @return string A message about the action
  249. * - log:act__settings=>saved
  250. */
  251. function duplicator_settings(){
  252. //defaults
  253. add_option('duplicator_options', '');
  254. $skip_ext = str_replace(array(' ', '.'), "", $_POST['skip_ext']);
  255. $by_pass_array = explode(";", $_POST['dir_bypass']);
  256. $by_pass_clean = "";
  257. foreach ($by_pass_array as $val) {
  258. if (strlen($val) >= 2) {
  259. $by_pass_clean .= duplicator_safe_path(trim(rtrim($val, "/\\"))) . ";";
  260. }
  261. }
  262. if (is_numeric($_POST['max_memory'])) {
  263. $maxmem = $_POST['max_memory'] < 256 ? 256 : $_POST['max_memory'];
  264. } else {
  265. $maxmem = 256;
  266. }
  267. $duplicator_opts = array(
  268. 'dbhost' =>$_POST['dbhost'],
  269. 'dbname' =>$_POST['dbname'],
  270. 'dbuser' =>$_POST['dbuser'],
  271. 'dbiconv' =>$_POST['dbiconv'],
  272. 'nurl' =>rtrim($_POST['nurl'], '/'),
  273. 'email-me' =>$_POST['email-me'],
  274. 'email_others' =>$_POST['email_others'],
  275. 'max_time' =>$_POST['max_time'],
  276. 'max_memory' =>preg_replace('/\D/', '', $maxmem) . 'M',
  277. 'skip_ext' =>str_replace(",", ";", $skip_ext),
  278. 'dir_bypass' =>$by_pass_clean,
  279. 'log_level' =>$_POST['log_level'],
  280. 'rm_snapshot' =>$_POST['rm_snapshot']);
  281. update_option('duplicator_options', serialize($duplicator_opts));
  282. die("log:act__settings=>saved");
  283. }
  284. //DO NOT ADD A CARRIAGE RETURN BEYOND THIS POINT (headers issue)!!
  285. ?>