/** * Dumps the database. * * What it does: * - It writes all of the database to standard output. * - It uses gzip compression if compress is set in the URL/post data. * - It may possibly time out, and mess up badly if you were relying on it. :P * - The data dumped depends on whether "struct" and "data" are passed. * - It is called from ManageMaintenance.controller.php. */ function DumpDatabase2() { global $db_name, $scripturl, $modSettings, $db_prefix, $db_show_debug; // We'll need a db to dump :P $database = database(); // We don't need debug when dumping the database $modSettings['disableQueryCheck'] = true; $db_show_debug = false; // You can't dump nothing! if (!isset($_REQUEST['struct']) && !isset($_REQUEST['data'])) { $_REQUEST['data'] = true; } // Attempt to stop from dying... @set_time_limit(600); $time_limit = ini_get('max_execution_time'); $start_time = time(); // @todo ... fail on not getting the requested memory? setMemoryLimit('256M'); $memory_limit = memoryReturnBytes(ini_get('memory_limit')) / 4; $current_used_memory = 0; $db_backup = ''; $output_function = 'un_compressed'; @ob_end_clean(); // Start saving the output... (don't do it otherwise for memory reasons.) if (isset($_REQUEST['compress']) && function_exists('gzencode')) { $output_function = 'gzencode'; // Send faked headers so it will just save the compressed output as a gzip. header('Content-Type: application/x-gzip'); header('Accept-Ranges: bytes'); header('Content-Encoding: none'); // Gecko browsers... don't like this. (Mozilla, Firefox, etc.) if (!isBrowser('gecko')) { header('Content-Transfer-Encoding: binary'); } // The file extension will include .gz... $extension = '.sql.gz'; } else { // Get rid of the gzipping alreading being done. if (!empty($modSettings['enableCompressedOutput'])) { @ob_end_clean(); } elseif (ob_get_length() != 0) { ob_clean(); } // Tell the client to save this file, even though it's text. header('Content-Type: ' . (isBrowser('ie') || isBrowser('opera') ? 'application/octetstream' : 'application/octet-stream')); header('Content-Encoding: none'); // This time the extension should just be .sql. $extension = '.sql'; } // This should turn off the session URL parser. $scripturl = ''; // Send the proper headers to let them download this file. header('Content-Disposition: attachment; filename="' . $db_name . '-' . (empty($_REQUEST['struct']) ? 'data' : (empty($_REQUEST['data']) ? 'structure' : 'complete')) . '_' . strftime('%Y-%m-%d') . $extension . '"'); header('Cache-Control: private'); header('Connection: close'); // This makes things simpler when using it so very very often. $crlf = "\r\n"; // SQL Dump Header. $db_chunks = '-- ==========================================================' . $crlf . '--' . $crlf . '-- Database dump of tables in `' . $db_name . '`' . $crlf . '-- ' . standardTime(time(), false) . $crlf . '--' . $crlf . '-- ==========================================================' . $crlf . $crlf; // Get all tables in the database....for our installation $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix; $tables = $database->db_list_tables(false, $real_prefix . '%'); // Dump each table. foreach ($tables as $tableName) { // Are we dumping the structures? if (isset($_REQUEST['struct'])) { $db_chunks .= $crlf . '--' . $crlf . '-- Table structure for table `' . $tableName . '`' . $crlf . '--' . $crlf . $crlf . $database->db_table_sql($tableName) . ';' . $crlf; } else { // This is needed to speedup things later $database->db_table_sql($tableName); } // How about the data? if (!isset($_REQUEST['data']) || substr($tableName, -10) == 'log_errors') { continue; } $first_round = true; $close_table = false; // Are there any rows in this table? while ($get_rows = $database->insert_sql($tableName, $first_round)) { if (empty($get_rows)) { break; } // Time is what we need here! if (function_exists('apache_reset_timeout')) { @apache_reset_timeout(); } elseif (!empty($time_limit) && $start_time + $time_limit - 20 > time()) { $start_time = time(); @set_time_limit(150); } // for the first pass, start the output with a custom line... if ($first_round) { $db_chunks .= $crlf . '--' . $crlf . '-- Dumping data in `' . $tableName . '`' . $crlf . '--' . $crlf . $crlf; $first_round = false; } $db_chunks .= $get_rows; $current_used_memory += Util::strlen($db_chunks); $db_backup .= $db_chunks; unset($db_chunks); $db_chunks = ''; if ($current_used_memory > $memory_limit) { echo $output_function($db_backup); $current_used_memory = 0; // This is probably redundant unset($db_backup); $db_backup = ''; } $close_table = true; } // No rows to get - skip it. if ($close_table) { $db_backup .= '-- --------------------------------------------------------' . $crlf; } } // write the last line $db_backup .= $crlf . '-- Done' . $crlf; echo $output_function($db_backup); exit; }
/** * Retrieve and return the administration settings for attachments. */ private function _settings() { global $modSettings, $txt, $scripturl, $context; require_once SUBSDIR . '/Attachments.subs.php'; // Get the current attachment directory. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']); $context['attachmentUploadDir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']]; // First time here? if (empty($modSettings['attachment_basedirectories']) && $modSettings['currentAttachmentUploadDir'] == 1 && count($modSettings['attachmentUploadDir']) == 1) { $modSettings['attachmentUploadDir'] = $modSettings['attachmentUploadDir'][1]; } // If not set, show a default path for the base directory if (!isset($_GET['save']) && empty($modSettings['basedirectory_for_attachments'])) { $modSettings['basedirectory_for_attachments'] = $context['attachmentUploadDir']; } $context['valid_upload_dir'] = is_dir($context['attachmentUploadDir']) && is_writable($context['attachmentUploadDir']); if (!empty($modSettings['automanage_attachments'])) { $context['valid_basedirectory'] = !empty($modSettings['basedirectory_for_attachments']) && is_writable($modSettings['basedirectory_for_attachments']); } else { $context['valid_basedirectory'] = true; } // A bit of razzle dazzle with the $txt strings. :) $txt['basedirectory_for_attachments_warning'] = str_replace('{attach_repair_url}', $scripturl . '?action=admin;area=manageattachments;sa=attachpaths', $txt['basedirectory_for_attachments_warning']); $txt['attach_current_dir_warning'] = str_replace('{attach_repair_url}', $scripturl . '?action=admin;area=manageattachments;sa=attachpaths', $txt['attach_current_dir_warning']); $txt['attachment_path'] = $context['attachmentUploadDir']; $txt['basedirectory_for_attachments_path'] = isset($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : ''; $txt['use_subdirectories_for_attachments_note'] = empty($modSettings['attachment_basedirectories']) || empty($modSettings['use_subdirectories_for_attachments']) ? $txt['use_subdirectories_for_attachments_note'] : ''; $txt['attachmentUploadDir_multiple_configure'] = '<a href="' . $scripturl . '?action=admin;area=manageattachments;sa=attachpaths">[' . $txt['attachmentUploadDir_multiple_configure'] . ']</a>'; $txt['attach_current_dir'] = empty($modSettings['automanage_attachments']) ? $txt['attach_current_dir'] : $txt['attach_last_dir']; $txt['attach_current_dir_warning'] = $txt['attach_current_dir'] . $txt['attach_current_dir_warning']; $txt['basedirectory_for_attachments_warning'] = $txt['basedirectory_for_attachments_current'] . $txt['basedirectory_for_attachments_warning']; // Perform a test to see if the GD module or ImageMagick are installed. $testImg = get_extension_funcs('gd') || class_exists('Imagick'); // See if we can find if the server is set up to support the attacment limits $post_max_size = ini_get('post_max_size'); $upload_max_filesize = ini_get('upload_max_filesize'); $testPM = !empty($post_max_size) ? memoryReturnBytes($post_max_size) >= (isset($modSettings['attachmentPostLimit']) ? $modSettings['attachmentPostLimit'] * 1024 : 0) : true; $testUM = !empty($upload_max_filesize) ? memoryReturnBytes($upload_max_filesize) >= (isset($modSettings['attachmentSizeLimit']) ? $modSettings['attachmentSizeLimit'] * 1024 : 0) : true; $config_vars = array(array('title', 'attachment_manager_settings'), array('select', 'attachmentEnable', array($txt['attachmentEnable_deactivate'], $txt['attachmentEnable_enable_all'], $txt['attachmentEnable_disable_new'])), '', array('check', 'attachmentRecodeLineEndings'), '', array('select', 'automanage_attachments', array(0 => $txt['attachments_normal'], 1 => $txt['attachments_auto_space'], 2 => $txt['attachments_auto_years'], 3 => $txt['attachments_auto_months'], 4 => $txt['attachments_auto_16'])), array('check', 'use_subdirectories_for_attachments', 'subtext' => $txt['use_subdirectories_for_attachments_note']), empty($modSettings['attachment_basedirectories']) ? array('text', 'basedirectory_for_attachments', 40) : array('var_message', 'basedirectory_for_attachments', 'message' => 'basedirectory_for_attachments_path', 'invalid' => empty($context['valid_basedirectory']), 'text_label' => !empty($context['valid_basedirectory']) ? $txt['basedirectory_for_attachments_current'] : $txt['basedirectory_for_attachments_warning']), empty($modSettings['attachment_basedirectories']) && $modSettings['currentAttachmentUploadDir'] == 1 && count($modSettings['attachmentUploadDir']) == 1 ? array('text', 'attachmentUploadDir', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 40, 'invalid' => !$context['valid_upload_dir']) : array('var_message', 'attach_current_directory', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 'message' => 'attachment_path', 'invalid' => empty($context['valid_upload_dir']), 'text_label' => !empty($context['valid_upload_dir']) ? $txt['attach_current_dir'] : $txt['attach_current_dir_warning']), array('int', 'attachmentDirFileLimit', 'subtext' => $txt['zero_for_no_limit'], 6), array('int', 'attachmentDirSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']), '', array('warning', empty($testPM) ? 'attachment_postsize_warning' : ''), array('int', 'attachmentPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']), array('warning', empty($testUM) ? 'attachment_filesize_warning' : ''), array('int', 'attachmentSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']), array('int', 'attachmentNumPerPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6), array('title', 'attachment_security_settings'), array('check', 'attachmentCheckExtensions'), array('text', 'attachmentExtensions', 40), '', array('warning', empty($testImg) ? 'attachment_img_enc_warning' : ''), array('check', 'attachment_image_reencode'), '', array('warning', 'attachment_image_paranoid_warning'), array('check', 'attachment_image_paranoid'), array('title', 'attachment_thumbnail_settings'), array('check', 'attachmentShowImages'), array('check', 'attachmentThumbnails'), array('check', 'attachment_thumb_png'), array('check', 'attachment_thumb_memory', 'subtext' => $txt['attachment_thumb_memory_note1'], 'postinput' => $txt['attachment_thumb_memory_note2']), array('warning', 'attachment_thumb_memory_note'), array('text', 'attachmentThumbWidth', 6), array('text', 'attachmentThumbHeight', 6), '', array('int', 'max_image_width', 'subtext' => $txt['zero_for_no_limit']), array('int', 'max_image_height', 'subtext' => $txt['zero_for_no_limit'])); // Add new settings with a nice hook, makes them available for admin settings search as well call_integration_hook('integrate_modify_attachment_settings', array(&$config_vars)); return $config_vars; }
/** * Allows to show/change attachment settings. * This is the default sub-action of the 'Attachments and Avatars' center. * Called by index.php?action=admin;area=manageattachments;sa=attachments. * * @param bool $return_config = false * @uses 'attachments' sub template. */ function ManageAttachmentSettings($return_config = false) { global $txt, $modSettings, $scripturl, $context, $options, $sourcedir, $boarddir; require_once $sourcedir . '/Attachments.php'; // Get the current attachment directory. $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']); $context['attachmentUploadDir'] = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']]; // If not set, show a default path for the base directory if (!isset($_GET['save']) && empty($modSettings['basedirectory_for_attachments'])) { if (!empty($modSettings['currentAttachmentUploadDir'])) { $modSettings['basedirectory_for_attachments'] = $modSettings['attachmentUploadDir'][1]; } else { $modSettings['basedirectory_for_attachments'] = $context['attachmentUploadDir']; } } $context['valid_upload_dir'] = is_dir($context['attachmentUploadDir']) && is_writable($context['attachmentUploadDir']); if (!empty($modSettings['automanage_attachments'])) { $context['valid_basedirectory'] = !empty($modSettings['basedirectory_for_attachments']) && is_writable($modSettings['basedirectory_for_attachments']); } else { $context['valid_basedirectory'] = true; } // A bit of razzle dazzle with the $txt strings. :) $txt['attachment_path'] = $context['attachmentUploadDir']; $txt['basedirectory_for_attachments_path'] = isset($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : ''; $txt['use_subdirectories_for_attachments_note'] = empty($modSettings['attachment_basedirectories']) || empty($modSettings['use_subdirectories_for_attachments']) ? $txt['use_subdirectories_for_attachments_note'] : ''; $txt['attachmentUploadDir_multiple_configure'] = '<a href="' . $scripturl . '?action=admin;area=manageattachments;sa=attachpaths">[' . $txt['attachmentUploadDir_multiple_configure'] . ']</a>'; $txt['attach_current_dir'] = empty($modSettings['automanage_attachments']) ? $txt['attach_current_dir'] : $txt['attach_last_dir']; $txt['attach_current_dir_warning'] = $txt['attach_current_dir'] . $txt['attach_current_dir_warning']; $txt['basedirectory_for_attachments_warning'] = $txt['basedirectory_for_attachments_current'] . $txt['basedirectory_for_attachments_warning']; // Perform a test to see if the GD module or ImageMagick are installed. $testImg = get_extension_funcs('gd') || class_exists('Imagick'); // See if we can find if the server is set up to support the attacment limits $post_max_size = ini_get('post_max_size'); $upload_max_filesize = ini_get('upload_max_filesize'); $testPM = !empty($post_max_size) ? memoryReturnBytes($post_max_size) >= (isset($modSettings['attachmentPostLimit']) ? $modSettings['attachmentPostLimit'] * 1024 : 0) : true; $testUM = !empty($upload_max_filesize) ? memoryReturnBytes($upload_max_filesize) >= (isset($modSettings['attachmentSizeLimit']) ? $modSettings['attachmentSizeLimit'] * 1024 : 0) : true; $config_vars = array(array('title', 'attachment_manager_settings'), array('select', 'attachmentEnable', array($txt['attachmentEnable_deactivate'], $txt['attachmentEnable_enable_all'], $txt['attachmentEnable_disable_new'])), '', array('check', 'attachmentRecodeLineEndings'), '', array('select', 'automanage_attachments', array(0 => $txt['attachments_normal'], 1 => $txt['attachments_auto_space'], 2 => $txt['attachments_auto_years'], 3 => $txt['attachments_auto_months'], 4 => $txt['attachments_auto_16'])), array('check', 'use_subdirectories_for_attachments', 'subtext' => $txt['use_subdirectories_for_attachments_note']), empty($modSettings['attachment_basedirectories']) ? array('text', 'basedirectory_for_attachments', 40) : array('var_message', 'basedirectory_for_attachments', 'message' => 'basedirectory_for_attachments_path', 'invalid' => empty($context['valid_basedirectory']), 'text_label' => !empty($context['valid_basedirectory']) ? $txt['basedirectory_for_attachments_current'] : $txt['basedirectory_for_attachments_warning']), array('var_message', 'attach_current_directory', 'subtext' => $txt['attachmentUploadDir_multiple_configure'], 'message' => 'attachment_path', 'invalid' => empty($context['valid_upload_dir']), 'text_label' => !empty($context['valid_upload_dir']) ? $txt['attach_current_dir'] : $txt['attach_current_dir_warning']), array('int', 'attachmentDirFileLimit', 'subtext' => $txt['zero_for_no_limit'], 6), array('int', 'attachmentDirSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']), '', array('int', 'attachmentPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']), array('warning', empty($testPM) ? 'attachment_postsize_warning' : ''), array('int', 'attachmentSizeLimit', 'subtext' => $txt['zero_for_no_limit'], 6, 'postinput' => $txt['kilobyte']), array('warning', empty($testUM) ? 'attachment_filesize_warning' : ''), array('int', 'attachmentNumPerPostLimit', 'subtext' => $txt['zero_for_no_limit'], 6), array('title', 'attachment_security_settings'), array('check', 'attachmentCheckExtensions'), array('text', 'attachmentExtensions', 40), '', array('warning', empty($testImg) ? 'attachment_img_enc_warning' : ''), array('check', 'attachment_image_reencode'), '', array('warning', 'attachment_image_paranoid_warning'), array('check', 'attachment_image_paranoid'), array('title', 'attachment_thumbnail_settings'), array('check', 'attachmentShowImages'), array('check', 'attachmentThumbnails'), array('check', 'attachment_thumb_png'), array('check', 'attachment_thumb_memory', 'subtext' => $txt['attachment_thumb_memory_note1'], 'postinput' => $txt['attachment_thumb_memory_note2']), array('warning', 'attachment_thumb_memory_note'), array('text', 'attachmentThumbWidth', 6), array('text', 'attachmentThumbHeight', 6), '', array('int', 'max_image_width', 'subtext' => $txt['zero_for_no_limit']), array('int', 'max_image_height', 'subtext' => $txt['zero_for_no_limit'])); $context['settings_post_javascript'] = ' var storing_type = document.getElementById(\'automanage_attachments\'); var base_dir = document.getElementById(\'use_subdirectories_for_attachments\'); createEventListener(storing_type) storing_type.addEventListener("change", toggleSubDir, false); createEventListener(base_dir) base_dir.addEventListener("change", toggleSubDir, false); toggleSubDir();'; call_integration_hook('integrate_modify_attachment_settings', array(&$config_vars)); if ($return_config) { return $config_vars; } // These are very likely to come in handy! (i.e. without them we're doomed!) require_once $sourcedir . '/ManagePermissions.php'; require_once $sourcedir . '/ManageServer.php'; // Saving settings? if (isset($_GET['save'])) { checkSession(); if (!empty($_POST['use_subdirectories_for_attachments'])) { if (isset($_POST['use_subdirectories_for_attachments']) && empty($_POST['basedirectory_for_attachments'])) { $_POST['basedirectory_for_attachments'] = !empty($modSettings['basedirectory_for_attachments']) ? $modSettings['basedirectory_for_attachments'] : $boarddir; } if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($modSettings['attachment_basedirectories'])) { if (!is_array($modSettings['attachment_basedirectories'])) { $modSettings['attachment_basedirectories'] = unserialize($modSettings['attachment_basedirectories']); } } else { $modSettings['attachment_basedirectories'] = array(); } if (!empty($_POST['use_subdirectories_for_attachments']) && !empty($_POST['basedirectory_for_attachments']) && !in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories'])) { $currentAttachmentUploadDir = $modSettings['currentAttachmentUploadDir']; if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachmentUploadDir'])) { if (!automanage_attachments_create_directory($_POST['basedirectory_for_attachments'])) { $_POST['basedirectory_for_attachments'] = $modSettings['basedirectory_for_attachments']; } } if (!in_array($_POST['basedirectory_for_attachments'], $modSettings['attachment_basedirectories'])) { $modSettings['attachment_basedirectories'][$modSettings['currentAttachmentUploadDir']] = $_POST['basedirectory_for_attachments']; updateSettings(array('attachment_basedirectories' => serialize($modSettings['attachment_basedirectories']), 'currentAttachmentUploadDir' => $currentAttachmentUploadDir)); } } } call_integration_hook('integrate_save_attachment_settings'); saveDBSettings($config_vars); redirectexit('action=admin;area=manageattachments;sa=attachments'); } $context['post_url'] = $scripturl . '?action=admin;area=manageattachments;save;sa=attachments'; prepareDBSettingContext($config_vars); $context['sub_template'] = 'show_settings'; }
/** * Supporting function for the database maintenance area. */ public function action_database() { global $context, $modSettings, $maintenance, $iknowitmaybeunsafe; // We need this, really.. require_once SUBSDIR . '/Maintenance.subs.php'; // Set up the sub-template $context['sub_template'] = 'maintain_database'; if (DB_TYPE == 'MySQL') { $body_type = fetchBodyType(); $context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text'; $context['convert_to_suggest'] = $body_type != 'text' && !empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] < 65536; } // Check few things to give advices before make a backup // If safe mod is enable the external tool is *always* the best (and probably the only) solution $context['safe_mode_enable'] = @ini_get('safe_mode'); // This is just a...guess $messages = countMessages(); // 256 is what we use in the backup script setMemoryLimit('256M'); $memory_limit = memoryReturnBytes(ini_get('memory_limit')) / (1024 * 1024); // Zip limit is set to more or less 1/4th the size of the available memory * 1500 // 1500 is an estimate of the number of messages that generates a database of 1 MB (yeah I know IT'S AN ESTIMATION!!!) // Why that? Because the only reliable zip package is the one sent out the first time, // so when the backup takes 1/5th (just to stay on the safe side) of the memory available $zip_limit = $memory_limit * 1500 / 5; // Here is more tricky: it depends on many factors, but the main idea is that // if it takes "too long" the backup is not reliable. So, I know that on my computer it take // 20 minutes to backup 2.5 GB, of course my computer is not representative, so I'll multiply by 4 the time. // I would consider "too long" 5 minutes (I know it can be a long time, but let's start with that): // 80 minutes for a 2.5 GB and a 5 minutes limit means 160 MB approx $plain_limit = 240000; // Last thing: are we able to gain time? $current_time_limit = ini_get('max_execution_time'); @set_time_limit(159); //something strange just to be sure $new_time_limit = ini_get('max_execution_time'); @set_time_limit($current_time_limit); $context['use_maintenance'] = 0; // External tool if: // * safe_mode enable OR // * cannot change the execution time OR // * cannot reset timeout if ($context['safe_mode_enable'] || empty($new_time_limit) || $current_time_limit == $new_time_limit && !function_exists('apache_reset_timeout')) { $context['suggested_method'] = 'use_external_tool'; } elseif ($zip_limit < $plain_limit && $messages < $zip_limit) { $context['suggested_method'] = 'zipped_file'; } elseif ($zip_limit > $plain_limit || $zip_limit < $plain_limit && $plain_limit < $messages) { $context['suggested_method'] = 'use_external_tool'; $context['use_maintenance'] = empty($maintenance) ? 2 : 0; } else { $context['use_maintenance'] = 1; $context['suggested_method'] = 'plain_text'; } loadTemplate('Packages'); loadLanguage('Packages'); // $context['package_ftp'] may be set action_backup_display when an error occur if (!isset($context['package_ftp'])) { $context['package_ftp'] = array('form_elements_only' => true, 'server' => '', 'port' => '', 'username' => '', 'path' => '', 'error' => ''); } $context['skip_security'] = !empty($iknowitmaybeunsafe); }
/** * Helper function to set the system memory to a needed value * * What it does: * - If the needed memory is greater than current, will attempt to get more * - if in_use is set to true, will also try to take the current memory usage in to account * * @param string $needed The amount of memory to request, if needed, like 256M * @param bool $in_use Set to true to account for current memory usage of the script * @return boolean true if we have at least the needed memory */ function setMemoryLimit($needed, $in_use = false) { // Everything in bytes $memory_current = memoryReturnBytes(ini_get('memory_limit')); $memory_needed = memoryReturnBytes($needed); // Should we account for how much is currently being used? if ($in_use) { $memory_needed += memory_get_usage(); } // If more is needed, request it if ($memory_current < $memory_needed) { @ini_set('memory_limit', ceil($memory_needed / 1048576) . 'M'); $memory_current = memoryReturnBytes(ini_get('memory_limit')); } $memory_current = max($memory_current, memoryReturnBytes(get_cfg_var('memory_limit'))); // Return success or not return (bool) ($memory_current >= $memory_needed); }
/** * Supporting function for the database maintenance area. */ function MaintainDatabase() { global $context, $db_type, $db_character_set, $modSettings, $smcFunc, $txt, $maintenance; // Show some conversion options? $context['convert_utf8'] = $db_type == 'mysql' && (!isset($db_character_set) || $db_character_set !== 'utf8' || empty($modSettings['global_character_set']) || $modSettings['global_character_set'] !== 'UTF-8') && version_compare('4.1.2', preg_replace('~\\-.+?$~', '', $smcFunc['db_server_info']()), '<='); $context['convert_entities'] = $db_type == 'mysql' && isset($db_character_set, $modSettings['global_character_set']) && $db_character_set === 'utf8' && $modSettings['global_character_set'] === 'UTF-8'; if ($db_type == 'mysql') { db_extend('packages'); $colData = $smcFunc['db_list_columns']('{db_prefix}messages', true); foreach ($colData as $column) { if ($column['name'] == 'body') { $body_type = $column['type']; } } $context['convert_to'] = $body_type == 'text' ? 'mediumtext' : 'text'; $context['convert_to_suggest'] = $body_type != 'text' && !empty($modSettings['max_messageLength']) && $modSettings['max_messageLength'] < 65536; } // Check few things to give advices before make a backup // If safe mod is enable the external tool is *always* the best (and probably the only) solution $context['safe_mode_enable'] = @ini_get('safe_mode'); // This is just a...guess $result = $smcFunc['db_query']('', ' SELECT COUNT(*) FROM {db_prefix}messages', array()); list($messages) = $smcFunc['db_fetch_row']($result); $smcFunc['db_free_result']($result); // 256 is what we use in the backup script setMemoryLimit('256M'); $memory_limit = memoryReturnBytes(ini_get('memory_limit')) / (1024 * 1024); // Zip limit is set to more or less 1/4th the size of the available memory * 1500 // 1500 is an estimate of the number of messages that generates a database of 1 MB (yeah I know IT'S AN ESTIMATION!!!) // Why that? Because the only reliable zip package is the one sent out the first time, // so when the backup takes 1/5th (just to stay on the safe side) of the memory available $zip_limit = $memory_limit * 1500 / 5; // Here is more tricky: it depends on many factors, but the main idea is that // if it takes "too long" the backup is not reliable. So, I know that on my computer it take // 20 minutes to backup 2.5 GB, of course my computer is not representative, so I'll multiply by 4 the time. // I would consider "too long" 5 minutes (I know it can be a long time, but let's start with that): // 80 minutes for a 2.5 GB and a 5 minutes limit means 160 MB approx $plain_limit = 240000; // Last thing: are we able to gain time? $current_time_limit = ini_get('max_execution_time'); @set_time_limit(159); //something strange just to be sure $new_time_limit = ini_get('max_execution_time'); $context['use_maintenance'] = 0; // External tool if: // * safe_mode enable OR // * cannot change the execution time OR // * cannot reset timeout if ($context['safe_mode_enable'] || empty($new_time_limit) || $current_time_limit == $new_time_limit && !function_exists('apache_reset_timeout')) { $context['suggested_method'] = 'use_external_tool'; } elseif ($zip_limit < $plain_limit && $messages < $zip_limit) { $context['suggested_method'] = 'zipped_file'; } elseif ($zip_limit > $plain_limit || $zip_limit < $plain_limit && $plain_limit < $messages) { $context['suggested_method'] = 'use_external_tool'; $context['use_maintenance'] = empty($maintenance) ? 2 : 0; } else { $context['use_maintenance'] = 1; $context['suggested_method'] = 'plain_text'; } if (isset($_GET['done']) && $_GET['done'] == 'convertutf8') { $context['maintenance_finished'] = $txt['utf8_title']; } if (isset($_GET['done']) && $_GET['done'] == 'convertentities') { $context['maintenance_finished'] = $txt['entity_convert_title']; } }