Microweber\Utils\Backup::create PHP Method

create() public method

public create ( $filename = false )
    public function create($filename = false)
    {
        if (is_array($filename)) {
            $filename = false;
        }
        ignore_user_abort(true);
        $start = microtime_float();
        if (defined('MW_CRON_EXEC')) {
        } else {
            only_admin_access();
        }
        $table = '*';
        if ($table == '*') {
            $extname = 'all';
        } else {
            $extname = str_replace(',', '_', $table);
            $extname = str_replace(' ', '_', $extname);
        }
        $here = $this->get_bakup_location();
        if (!is_dir($here)) {
            if (!mkdir_recursive($here)) {
                $back_log_action = 'Error the dir is not writable: ' . $here;
                $this->log_action($back_log_action);
            }
        }
        ini_set('memory_limit', '512M');
        set_time_limit(0);
        $index1 = $here . 'index.php';
        if ($filename == false) {
            $engine = mw()->database_manager->get_sql_engine();
            $mwv = MW_VERSION;
            $mwv = str_replace('.', '', $mwv);
            $filename_to_return = 'database_' . date('YMdHis') . '_' . uniqid() . '_' . $mwv . '_' . $engine . '.sql';
        } else {
            $filename_to_return = $filename;
        }
        $filess = $here . $filename_to_return;
        if (is_file($filess)) {
            return false;
        }
        touch($filess);
        touch($index1);
        $sql_bak_file = $filess;
        $hta = $here . '.htaccess';
        if (!is_file($hta)) {
            touch($hta);
            file_put_contents($hta, 'Deny from all');
        }
        $head = '/* Microweber database backup exported on: ' . date('l jS \\of F Y h:i:s A') . " */ \n";
        $head .= '/* get_table_prefix(): ' . get_table_prefix() . " */ \n\n\n";
        file_put_contents($sql_bak_file, $head);
        $return = '';
        $tables = '*';
        // Get all of the tables
        if ($tables == '*') {
            $tables = array();
            $result = mw()->database_manager->get_tables_list();
            if (!empty($result)) {
                foreach ($result as $item) {
                    $tables[] = $item;
                }
            }
        } else {
            if (is_array($tables)) {
                $tables = explode(',', $tables);
            }
        }
        $back_log_action = 'Starting database backup';
        $this->log_action($back_log_action);
        // Cycle through each provided table
        foreach ($tables as $table) {
            $is_cms_table = false;
            if (get_table_prefix() == '') {
                $is_cms_table = 1;
            } elseif (stristr($table, get_table_prefix())) {
                $is_cms_table = 1;
            }
            if (stristr($table, 'sessions')) {
                $is_cms_table = false;
            }
            if ($table != false and $is_cms_table) {
                $back_log_action = "Backing up database table {$table}";
                $this->log_action($back_log_action);
                $qs = 'SELECT * FROM ' . $table;
                $result = mw()->database_manager->query($qs, $cache_id = false, $cache_group = false, $only_query = false);
                $num_fields = count($result[0]);
                $table_without_prefix = $this->prefix_placeholder . str_ireplace(get_table_prefix(), '', $table);
                //                $return = 'DROP TABLE IF EXISTS ' . $table_without_prefix . $this->file_q_sep . "\n\n\n";
                //                $this->append_string_to_file($sql_bak_file, $return);
                $ddl = mw()->database_manager->get_table_ddl($table);
                $ddl = str_ireplace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ', $ddl);
                $create_table_without_prefix = str_ireplace(get_table_prefix(), $this->prefix_placeholder, $ddl);
                $return = "\n\n" . $create_table_without_prefix . $this->file_q_sep . "\n\n\n";
                $this->append_string_to_file($sql_bak_file, $return);
                $this->log_action(false);
                if (!empty($result)) {
                    $table_accos = str_replace(get_table_prefix(), '', $table);
                    $columns = $this->app->database_manager->get_fields($table_accos);
                    foreach ($result as $row) {
                        $row = array_values($row);
                        $columns = array_values($columns);
                        $columns_q = false;
                        $columns_temp = array();
                        foreach ($columns as $column) {
                            $columns_temp[] = $column;
                        }
                        if (!empty($columns_temp)) {
                            $columns_q = implode(',', $columns_temp);
                            $columns_q = '(' . $columns_q . ')';
                        }
                        $return = 'REPLACE INTO ' . $table_without_prefix . ' ' . $columns_q . ' VALUES(';
                        for ($j = 0; $j < $num_fields; ++$j) {
                            $row[$j] = str_replace("'", '&rsquo;', $row[$j]);
                            if (isset($row[$j])) {
                                $return .= "'" . $row[$j] . "'";
                            } else {
                                $return .= "''";
                            }
                            if ($j < $num_fields - 1) {
                                $return .= ',';
                            }
                        }
                        $return .= ')' . $this->file_q_sep . "\n\n\n";
                        $this->append_string_to_file($sql_bak_file, $return);
                    }
                }
                $return = "\n\n\n";
                $this->append_string_to_file($sql_bak_file, $return);
            }
        }
        $this->log_action(false);
        $back_log_action = 'Saving to file ' . basename($filess);
        $this->log_action($back_log_action);
        $end = microtime_float();
        $end = round($end - $start, 3);
        $this->log_action(false);
        return array('success' => "Backup was created for {$end} sec! {$filename_to_return}", 'filename' => $filename_to_return, 'runtime' => $end, 'url' => dir2url($filess));
    }