Ejemplo n.º 1
0
 /**
  * Field data
  *
  * Generates an array of objects containing field meta-data
  *
  * @access  public
  * @return  array
  */
 function field_data()
 {
     $retval = array();
     $fieldCount = $this->num_fields();
     for ($c = 1; $c <= $fieldCount; $c++) {
         $F = new stdClass();
         $F->name = ocicolumnname($this->stmt_id, $c);
         $F->type = ocicolumntype($this->stmt_id, $c);
         $F->max_length = ocicolumnsize($this->stmt_id, $c);
         $retval[] = $F;
     }
     return $retval;
 }
Ejemplo n.º 2
0
 function write_data($table_name)
 {
     global $db;
     $ary_type = $ary_name = array();
     // Grab all of the data from current table.
     $sql = "SELECT *\n\t\t\tFROM {$table_name}";
     $result = $db->sql_query($sql);
     $i_num_fields = ocinumcols($result);
     for ($i = 0; $i < $i_num_fields; $i++) {
         $ary_type[$i] = ocicolumntype($result, $i + 1);
         $ary_name[$i] = ocicolumnname($result, $i + 1);
     }
     $sql_data = '';
     while ($row = $db->sql_fetchrow($result)) {
         $schema_vals = $schema_fields = array();
         // Build the SQL statement to recreate the data.
         for ($i = 0; $i < $i_num_fields; $i++) {
             // Oracle uses uppercase - we use lowercase
             $str_val = $row[strtolower($ary_name[$i])];
             if (preg_match('#char|text|bool|raw|clob#i', $ary_type[$i])) {
                 $str_quote = '';
                 $str_empty = "''";
                 $str_val = sanitize_data_oracle($str_val);
             } else {
                 if (preg_match('#date|timestamp#i', $ary_type[$i])) {
                     if (empty($str_val)) {
                         $str_quote = '';
                     } else {
                         $str_quote = "'";
                     }
                 } else {
                     $str_quote = '';
                     $str_empty = 'NULL';
                 }
             }
             if (empty($str_val) && $str_val !== '0') {
                 $str_val = $str_empty;
             }
             $schema_vals[$i] = $str_quote . $str_val . $str_quote;
             $schema_fields[$i] = '"' . $ary_name[$i] . '"';
         }
         // Take the ordered fields and their associated data and build it
         // into a valid sql statement to recreate that field in the data.
         $sql_data = "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES (' . implode(', ', $schema_vals) . ")\n/\n";
         $this->flush($sql_data);
     }
     $db->sql_freeresult($result);
 }
Ejemplo n.º 3
0
function DBfetch(&$cursor)
{
    global $DB;
    $result = false;
    if (isset($DB['DB']) && !empty($DB['DB'])) {
        switch ($DB['TYPE']) {
            case 'MYSQL':
                $result = mysql_fetch_assoc($cursor);
                break;
            case 'POSTGRESQL':
                $result = pg_fetch_assoc($cursor);
                break;
            case 'ORACLE':
                if (ocifetchinto($cursor, $row, OCI_ASSOC + OCI_RETURN_NULLS)) {
                    $result = array();
                    foreach ($row as $key => $value) {
                        $result[strtolower($key)] = str_in_array(strtolower(ocicolumntype($cursor, $key)), array('varchar', 'varchar2', 'blob', 'clob')) && is_null($value) ? '' : $value;
                    }
                }
                break;
            case 'SQLITE3':
                if ($cursor) {
                    $result = array_shift($cursor);
                    if (is_null($result)) {
                        $result = false;
                    }
                }
                break;
        }
    }
    return $result;
}
Ejemplo n.º 4
0
 function FieldType($rsMain, $i)
 {
     return ocicolumntype($rsMain, $i + 1);
 }
Ejemplo n.º 5
0
    function main($id, $mode)
    {
        global $db, $user, $auth, $template, $table_prefix;
        global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
        $user->add_lang('acp/database');
        $this->tpl_name = 'acp_database';
        $this->page_title = 'ACP_DATABASE';
        $action = request_var('action', '');
        $submit = isset($_POST['submit']) ? true : false;
        $template->assign_vars(array('MODE' => $mode));
        switch ($mode) {
            case 'backup':
                switch ($action) {
                    case 'download':
                        $type = request_var('type', '');
                        $table = request_var('table', array(''));
                        $format = request_var('method', '');
                        $where = request_var('where', '');
                        $store = $download = $structure = $schema_data = false;
                        if ($where == 'store_and_download' || $where == 'store') {
                            $store = true;
                        }
                        if ($where == 'store_and_download' || $where == 'download') {
                            $download = true;
                        }
                        if ($type == 'full' || $type == 'structure') {
                            $structure = true;
                        }
                        if ($type == 'full' || $type == 'data') {
                            $schema_data = true;
                        }
                        @set_time_limit(1200);
                        $filename = time();
                        // We set up the info needed for our on-the-fly creation :D
                        switch ($format) {
                            case 'text':
                                $ext = '.sql';
                                $open = 'fopen';
                                $write = 'fwrite';
                                $close = 'fclose';
                                $oper = '';
                                $mimetype = 'text/x-sql';
                                break;
                            case 'bzip2':
                                $ext = '.sql.bz2';
                                $open = 'bzopen';
                                $write = 'bzwrite';
                                $close = 'bzclose';
                                $oper = 'bzcompress';
                                $mimetype = 'application/x-bzip2';
                                break;
                            case 'gzip':
                                $ext = '.sql.gz';
                                $open = 'gzopen';
                                $write = 'gzwrite';
                                $close = 'gzclose';
                                $oper = 'gzencode';
                                $mimetype = 'application/x-gzip';
                                break;
                        }
                        // We write the file to "store" first (and then compress the file) to not use too much
                        // memory. The server process can be easily killed by storing too much data at once.
                        if ($store == true) {
                            $file = $phpbb_root_path . 'store/' . $filename . $ext;
                            $fp = $open($file, 'w');
                            if (!$fp) {
                                trigger_error('Unable to write temporary file to storage folder');
                            }
                        }
                        if ($download == true) {
                            $name = $filename . $ext;
                            header('Pragma: no-cache');
                            header("Content-Type: {$mimetype}; name=\"{$name}\"");
                            header("Content-disposition: attachment; filename={$name}");
                        }
                        // All of the generated queries go here
                        $sql_data = '';
                        $sql_data .= "#\n";
                        $sql_data .= "# phpBB Backup Script\n";
                        $sql_data .= "# Dump of tables for {$table_prefix}\n";
                        $sql_data .= "# DATE : " . gmdate("d-m-Y H:i:s", $filename) . " GMT\n";
                        $sql_data .= "#\n";
                        switch (SQL_LAYER) {
                            case 'sqlite':
                                $sql_data .= "BEGIN TRANSACTION;\n";
                                break;
                            case 'postgres':
                                $sql_data .= "BEGIN;\n";
                                break;
                            case 'mssql':
                            case 'mssql_odbc':
                                $sql_data .= "BEGIN TRANSACTION\nGO\n";
                                break;
                        }
                        foreach ($table as $table_name) {
                            // Get the table structure
                            if ($structure) {
                                switch (SQL_LAYER) {
                                    case 'mysqli':
                                    case 'mysql4':
                                    case 'mysql':
                                    case 'sqlite':
                                        $sql_data .= '# Table: ' . $table_name . "\n";
                                        $sql_data .= "DROP TABLE IF EXISTS {$table_name};\n";
                                        break;
                                    case 'oracle':
                                        $sql_data .= '# Table: ' . $table_name . "\n";
                                        $sql_data .= "DROP TABLE {$table_name};\n\\\n";
                                        break;
                                    case 'postgres':
                                    case 'firebird':
                                        $sql_data .= '# Table: ' . $table_name . "\n";
                                        $sql_data .= "DROP TABLE {$table_name};\n";
                                        break;
                                    case 'mssql':
                                    case 'mssql_odbc':
                                        $sql_data .= '# Table: ' . $table_name . "\n";
                                        $sql_data .= "IF OBJECT_ID(N'{$table_name}', N'U') IS NOT NULL\n";
                                        $sql_data .= "DROP TABLE {$table_name};\nGO\n";
                                        break;
                                }
                                $sql_data .= $this->get_table_structure($table_name);
                            }
                            // Now write the data for the first time. :)
                            if ($store == true) {
                                $write($fp, $sql_data);
                            }
                            if ($download == true) {
                                if (!empty($oper)) {
                                    echo $oper($sql_data);
                                } else {
                                    echo $sql_data;
                                }
                            }
                            $sql_data = '';
                            // Data
                            if ($schema_data) {
                                $sql_data .= "\n";
                                switch (SQL_LAYER) {
                                    case 'mysqli':
                                        $sql = "SELECT * FROM {$table_name}";
                                        $result = mysqli_query($db->db_connect_id, $sql, MYSQLI_USE_RESULT);
                                        if ($result != false) {
                                            $fields_cnt = mysqli_num_fields($result);
                                            // Get field information
                                            $field = mysqli_fetch_fields($result);
                                            $field_set = array();
                                            for ($j = 0; $j < $fields_cnt; $j++) {
                                                $field_set[$j] = $field[$j]->name;
                                            }
                                            $search = array('\\', "'", "", "\n", "\r", "");
                                            $replace = array('\\\\\\\\', "''", '\\0', '\\n', '\\r', '\\Z');
                                            $fields = implode(', ', $field_set);
                                            $values = array();
                                            $schema_insert = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES (';
                                            while ($row = mysqli_fetch_row($result)) {
                                                for ($j = 0; $j < $fields_cnt; $j++) {
                                                    if (!isset($row[$j]) || is_null($row[$j])) {
                                                        $values[$j] = 'NULL';
                                                    } else {
                                                        if ($field[$j]->flags & 32768 && !($field[$j]->flags & 1024)) {
                                                            $values[$j] = $row[$j];
                                                        } else {
                                                            $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
                                                        }
                                                    }
                                                }
                                                $sql_data .= $schema_insert . implode(', ', $values) . ");\n";
                                                if ($store == true) {
                                                    $write($fp, $sql_data);
                                                }
                                                if ($download == true) {
                                                    if (!empty($oper)) {
                                                        echo $oper($sql_data);
                                                    } else {
                                                        echo $sql_data;
                                                    }
                                                }
                                                $sql_data = '';
                                                $values = array();
                                            }
                                            mysqli_free_result($result);
                                        }
                                        break;
                                    case 'mysql4':
                                    case 'mysql':
                                        $sql = "SELECT * FROM {$table_name}";
                                        $result = mysql_unbuffered_query($sql, $db->db_connect_id);
                                        if ($result != false) {
                                            $fields_cnt = mysql_num_fields($result);
                                            // Get field information
                                            $field = array();
                                            for ($i = 0; $i < $fields_cnt; $i++) {
                                                $field[$i] = mysql_fetch_field($result, $i);
                                            }
                                            $field_set = array();
                                            for ($j = 0; $j < $fields_cnt; $j++) {
                                                $field_set[$j] = $field[$j]->name;
                                            }
                                            $search = array('\\', "'", "", "\n", "\r", "");
                                            $replace = array('\\\\\\\\', "''", '\\0', '\\n', '\\r', '\\Z');
                                            $fields = implode(', ', $field_set);
                                            $schema_insert = 'INSERT INTO ' . $table_name . ' (' . $fields . ') VALUES (';
                                            while ($row = mysql_fetch_row($result)) {
                                                $values = array();
                                                for ($j = 0; $j < $fields_cnt; $j++) {
                                                    if (!isset($row[$j]) || is_null($row[$j])) {
                                                        $values[$j] = 'NULL';
                                                    } else {
                                                        if ($field[$j]->numeric && $field[$j]->type !== 'timestamp') {
                                                            $values[$j] = $row[$j];
                                                        } else {
                                                            $values[$j] = "'" . str_replace($search, $replace, $row[$j]) . "'";
                                                        }
                                                    }
                                                }
                                                $sql_data .= $schema_insert . implode(', ', $values) . ");\n";
                                                if ($store == true) {
                                                    $write($fp, $sql_data);
                                                }
                                                if ($download == true) {
                                                    if (!empty($oper)) {
                                                        echo $oper($sql_data);
                                                    } else {
                                                        echo $sql_data;
                                                    }
                                                }
                                                $sql_data = '';
                                            }
                                            mysql_free_result($result);
                                        }
                                        break;
                                    case 'sqlite':
                                        $col_types = sqlite_fetch_column_types($table_name, $db->db_connect_id);
                                        $sql = "SELECT * FROM {$table_name}";
                                        $result = $db->sql_query($sql);
                                        while ($row = $db->sql_fetchrow($result)) {
                                            $names = $data = array();
                                            foreach ($row as $row_name => $row_data) {
                                                $names[] = $row_name;
                                                // Figure out what this data is, escape it properly
                                                if (is_null($row_data)) {
                                                    $row_data = 'NULL';
                                                } else {
                                                    if ($row_data == '') {
                                                        $row_data = "''";
                                                    } else {
                                                        if (strpos($col_types[$row_name], 'text') !== false || strpos($col_types[$row_name], 'char') !== false) {
                                                            $row_data = "'" . $row_data . "'";
                                                        }
                                                    }
                                                }
                                                $data[] = $row_data;
                                            }
                                            $sql_data .= 'INSERT INTO ' . $table_name . ' (' . implode(', ', $names) . ') VALUES (' . implode(', ', $data) . ");\n";
                                            if ($store == true) {
                                                $write($fp, $sql_data);
                                            }
                                            if ($download == true) {
                                                if (!empty($oper)) {
                                                    echo $oper($sql_data);
                                                } else {
                                                    echo $sql_data;
                                                }
                                            }
                                            $sql_data = '';
                                        }
                                        $db->sql_freeresult($result);
                                        break;
                                    case 'postgres':
                                        $ary_type = $ary_name = array();
                                        // Grab all of the data from current table.
                                        $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}";
                                        $result = $db->sql_query($sql);
                                        $i_num_fields = pg_num_fields($result);
                                        $seq = '';
                                        for ($i = 0; $i < $i_num_fields; $i++) {
                                            $ary_type[$i] = pg_field_type($result, $i);
                                            $ary_name[$i] = pg_field_name($result, $i);
                                            $sql = "SELECT pg_get_expr(d.adbin, d.adrelid) as rowdefault\n\t\t\t\t\t\t\t\t\t\t\t\tFROM pg_attrdef d, pg_class c\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE (c.relname = '{$table_name}')\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND (c.oid = d.adrelid)\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND d.adnum = " . strval($i + 1);
                                            $result2 = $db->sql_query($sql);
                                            if ($row = $db->sql_fetchrow($result2)) {
                                                // Determine if we must reset the sequences
                                                if (strpos($row['rowdefault'], 'nextval(\'') === 0) {
                                                    $seq .= "SELECT SETVAL('{$table_name}_seq',(select case when max({$ary_name[$i]})>0 then max({$ary_name[$i]})+1 else 1 end from {$table_name}));\n";
                                                }
                                            }
                                        }
                                        while ($row = $db->sql_fetchrow($result)) {
                                            $schema_vals = $schema_fields = array();
                                            // Build the SQL statement to recreate the data.
                                            for ($i = 0; $i < $i_num_fields; $i++) {
                                                $str_val = $row[$ary_name[$i]];
                                                if (preg_match('#char|text|bool#i', $ary_type[$i])) {
                                                    $str_quote = "'";
                                                    $str_empty = '';
                                                    $str_val = addslashes($str_val);
                                                } else {
                                                    if (preg_match('#date|timestamp#i', $ary_type[$i])) {
                                                        if (empty($str_val)) {
                                                            $str_quote = '';
                                                        } else {
                                                            $str_quote = "'";
                                                        }
                                                    } else {
                                                        $str_quote = '';
                                                        $str_empty = 'NULL';
                                                    }
                                                }
                                                if (empty($str_val) && $str_val !== '0') {
                                                    $str_val = $str_empty;
                                                }
                                                $schema_vals[$i] = $str_quote . $str_val . $str_quote;
                                                $schema_fields[$i] = $ary_name[$i];
                                            }
                                            // Take the ordered fields and their associated data and build it
                                            // into a valid sql statement to recreate that field in the data.
                                            $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n";
                                            if ($store == true) {
                                                $write($fp, $sql_data);
                                            }
                                            if ($download == true) {
                                                if (!empty($oper)) {
                                                    echo $oper($sql_data);
                                                } else {
                                                    echo $sql_data;
                                                }
                                            }
                                            $sql_data = '';
                                        }
                                        $db->sql_freeresult($result);
                                        // Write out the sequence statements
                                        if ($store == true) {
                                            $write($fp, $seq);
                                        }
                                        if ($download == true) {
                                            if (!empty($oper)) {
                                                echo $oper($seq);
                                            } else {
                                                echo $seq;
                                            }
                                        }
                                        $seq = '';
                                        break;
                                    case 'mssql_odbc':
                                        $ary_type = $ary_name = array();
                                        $ident_set = false;
                                        // Grab all of the data from current table.
                                        $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}";
                                        $result = $db->sql_query($sql);
                                        $retrieved_data = odbc_num_rows($result);
                                        if ($retrieved_data) {
                                            $sql = "SELECT 1 as has_identity\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM INFORMATION_SCHEMA.COLUMNS\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE COLUMNPROPERTY(object_id('{$table_name}'), COLUMN_NAME, 'IsIdentity') = 1";
                                            $result2 = $db->sql_query($sql);
                                            $row2 = $db->sql_fetchrow($result2);
                                            if (!empty($row2['has_identity'])) {
                                                $sql_data .= "\nSET IDENTITY_INSERT {$table_name} ON\nGO\n";
                                                $ident_set = true;
                                            }
                                            $db->sql_freeresult($result2);
                                        }
                                        $i_num_fields = odbc_num_fields($result);
                                        for ($i = 0; $i < $i_num_fields; $i++) {
                                            $ary_type[$i] = odbc_field_type($result, $i);
                                            $ary_name[$i] = odbc_field_name($result, $i);
                                        }
                                        while ($row = $db->sql_fetchrow($result)) {
                                            $schema_vals = $schema_fields = array();
                                            // Build the SQL statement to recreate the data.
                                            for ($i = 0; $i < $i_num_fields; $i++) {
                                                $str_val = $row[$ary_name[$i]];
                                                if (preg_match('#char|text|bool#i', $ary_type[$i])) {
                                                    $str_quote = "'";
                                                    $str_empty = '';
                                                    $str_val = addslashes($str_val);
                                                } else {
                                                    if (preg_match('#date|timestamp#i', $ary_type[$i])) {
                                                        if (empty($str_val)) {
                                                            $str_quote = '';
                                                        } else {
                                                            $str_quote = "'";
                                                        }
                                                    } else {
                                                        $str_quote = '';
                                                        $str_empty = 'NULL';
                                                    }
                                                }
                                                if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val))) {
                                                    $str_val = $str_empty;
                                                }
                                                $schema_vals[$i] = $str_quote . $str_val . $str_quote;
                                                $schema_fields[$i] = $ary_name[$i];
                                            }
                                            // Take the ordered fields and their associated data and build it
                                            // into a valid sql statement to recreate that field in the data.
                                            $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n";
                                            if ($store == true) {
                                                $write($fp, $sql_data);
                                            }
                                            if ($download == true) {
                                                if (!empty($oper)) {
                                                    echo $oper($sql_data);
                                                } else {
                                                    echo $sql_data;
                                                }
                                            }
                                            $sql_data = '';
                                        }
                                        $db->sql_freeresult($result);
                                        if ($retrieved_data) {
                                            $sql_data = "\nGO\n";
                                            if ($ident_set) {
                                                $sql_data .= "\nSET IDENTITY_INSERT {$table_name} OFF\nGO\n";
                                            }
                                        }
                                        break;
                                    case 'mssql':
                                        $ary_type = $ary_name = array();
                                        $ident_set = false;
                                        // Grab all of the data from current table.
                                        $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}";
                                        $result = $db->sql_query($sql);
                                        $retrieved_data = mssql_num_rows($result);
                                        $i_num_fields = mssql_num_fields($result);
                                        for ($i = 0; $i < $i_num_fields; $i++) {
                                            $ary_type[$i] = mssql_field_type($result, $i);
                                            $ary_name[$i] = mssql_field_name($result, $i);
                                        }
                                        if ($retrieved_data) {
                                            $sql = "SELECT 1 as has_identity\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM INFORMATION_SCHEMA.COLUMNS\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE COLUMNPROPERTY(object_id('{$table_name}'), COLUMN_NAME, 'IsIdentity') = 1";
                                            $result2 = $db->sql_query($sql);
                                            $row2 = $db->sql_fetchrow($result2);
                                            if (!empty($row2['has_identity'])) {
                                                $sql_data .= "\nSET IDENTITY_INSERT {$table_name} ON\nGO\n";
                                                $ident_set = true;
                                            }
                                            $db->sql_freeresult($result2);
                                        }
                                        while ($row = $db->sql_fetchrow($result)) {
                                            $schema_vals = $schema_fields = array();
                                            // Build the SQL statement to recreate the data.
                                            for ($i = 0; $i < $i_num_fields; $i++) {
                                                $str_val = $row[$ary_name[$i]];
                                                if (preg_match('#char|text|bool#i', $ary_type[$i])) {
                                                    $str_quote = "'";
                                                    $str_empty = '';
                                                    $str_val = addslashes($str_val);
                                                } else {
                                                    if (preg_match('#date|timestamp#i', $ary_type[$i])) {
                                                        if (empty($str_val)) {
                                                            $str_quote = '';
                                                        } else {
                                                            $str_quote = "'";
                                                        }
                                                    } else {
                                                        $str_quote = '';
                                                        $str_empty = 'NULL';
                                                    }
                                                }
                                                if (empty($str_val) && $str_val !== '0' && !(is_int($str_val) || is_float($str_val))) {
                                                    $str_val = $str_empty;
                                                }
                                                $schema_vals[$i] = $str_quote . $str_val . $str_quote;
                                                $schema_fields[$i] = $ary_name[$i];
                                            }
                                            // Take the ordered fields and their associated data and build it
                                            // into a valid sql statement to recreate that field in the data.
                                            $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n";
                                            if ($store == true) {
                                                $write($fp, $sql_data);
                                            }
                                            if ($download == true) {
                                                if (!empty($oper)) {
                                                    echo $oper($sql_data);
                                                } else {
                                                    echo $sql_data;
                                                }
                                            }
                                            $sql_data = '';
                                        }
                                        $db->sql_freeresult($result);
                                        if ($retrieved_data) {
                                            $sql_data = "\nGO\n";
                                            if ($ident_set) {
                                                $sql_data .= "\nSET IDENTITY_INSERT {$table_name} OFF\nGO\n";
                                            }
                                        }
                                        break;
                                    case 'firebird':
                                        $ary_type = $ary_name = array();
                                        // Grab all of the data from current table.
                                        $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}";
                                        $result = $db->sql_query($sql);
                                        $i_num_fields = ibase_num_fields($result);
                                        for ($i = 0; $i < $i_num_fields; $i++) {
                                            $info = ibase_field_info($result, $i);
                                            $ary_type[$i] = $info['type'];
                                            $ary_name[$i] = $info['name'];
                                        }
                                        while ($row = $db->sql_fetchrow($result)) {
                                            $schema_vals = $schema_fields = array();
                                            // Build the SQL statement to recreate the data.
                                            for ($i = 0; $i < $i_num_fields; $i++) {
                                                $str_val = $row[strtolower($ary_name[$i])];
                                                if (preg_match('#char|text|bool#i', $ary_type[$i])) {
                                                    $str_quote = "'";
                                                    $str_empty = '';
                                                    $str_val = addslashes($str_val);
                                                } else {
                                                    if (preg_match('#date|timestamp#i', $ary_type[$i])) {
                                                        if (empty($str_val)) {
                                                            $str_quote = '';
                                                        } else {
                                                            $str_quote = "'";
                                                        }
                                                    } else {
                                                        $str_quote = '';
                                                        $str_empty = 'NULL';
                                                    }
                                                }
                                                if (empty($str_val) && $str_val !== '0') {
                                                    $str_val = $str_empty;
                                                }
                                                $schema_vals[$i] = $str_quote . $str_val . $str_quote;
                                                $schema_fields[$i] = "'" . $ary_name[$i] . "'";
                                            }
                                            // Take the ordered fields and their associated data and build it
                                            // into a valid sql statement to recreate that field in the data.
                                            $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n";
                                            if ($store == true) {
                                                $write($fp, $sql_data);
                                            }
                                            if ($download == true) {
                                                if (!empty($oper)) {
                                                    echo $oper($sql_data);
                                                } else {
                                                    echo $sql_data;
                                                }
                                            }
                                            $sql_data = '';
                                        }
                                        $db->sql_freeresult($result);
                                        break;
                                    case 'oracle':
                                        $ary_type = $ary_name = array();
                                        // Grab all of the data from current table.
                                        $sql = "SELECT *\n\t\t\t\t\t\t\t\t\t\t\tFROM {$table_name}";
                                        $result = $db->sql_query($sql);
                                        $i_num_fields = ocinumcols($result);
                                        for ($i = 0; $i < $i_num_fields; $i++) {
                                            $ary_type[$i] = ocicolumntype($result, $i);
                                            $ary_name[$i] = ocicolumnname($result, $i);
                                        }
                                        while ($row = $db->sql_fetchrow($result)) {
                                            $schema_vals = $schema_fields = array();
                                            // Build the SQL statement to recreate the data.
                                            for ($i = 0; $i < $i_num_fields; $i++) {
                                                $str_val = $row[$ary_name[$i]];
                                                if (preg_match('#char|text|bool#i', $ary_type[$i])) {
                                                    $str_quote = "'";
                                                    $str_empty = '';
                                                    $str_val = addslashes($str_val);
                                                } else {
                                                    if (preg_match('#date|timestamp#i', $ary_type[$i])) {
                                                        if (empty($str_val)) {
                                                            $str_quote = '';
                                                        } else {
                                                            $str_quote = "'";
                                                        }
                                                    } else {
                                                        $str_quote = '';
                                                        $str_empty = 'NULL';
                                                    }
                                                }
                                                if (empty($str_val) && $str_val !== '0') {
                                                    $str_val = $str_empty;
                                                }
                                                $schema_vals[$i] = $str_quote . $str_val . $str_quote;
                                                $schema_fields[$i] = '"' . $ary_name[$i] . "'";
                                            }
                                            // Take the ordered fields and their associated data and build it
                                            // into a valid sql statement to recreate that field in the data.
                                            $sql_data .= "INSERT INTO {$table_name} (" . implode(', ', $schema_fields) . ') VALUES(' . implode(', ', $schema_vals) . ");\n";
                                            if ($store == true) {
                                                $write($fp, $sql_data);
                                            }
                                            if ($download == true) {
                                                if (!empty($oper)) {
                                                    echo $oper($sql_data);
                                                } else {
                                                    echo $sql_data;
                                                }
                                            }
                                            $sql_data = '';
                                        }
                                        $db->sql_freeresult($result);
                                        break;
                                }
                            }
                        }
                        switch (SQL_LAYER) {
                            case 'sqlite':
                            case 'postgres':
                                $sql_data .= "COMMIT;";
                                break;
                            case 'mssql':
                            case 'mssql_odbc':
                                $sql_data .= "COMMIT\nGO";
                                break;
                        }
                        if ($store == true) {
                            $write($fp, $sql_data);
                            $close($fp);
                        }
                        if ($download == true) {
                            if (!empty($oper)) {
                                echo $oper($sql_data);
                            } else {
                                echo $sql_data;
                            }
                            exit;
                        }
                        unset($sql_data);
                        add_log('admin', 'LOG_DB_BACKUP');
                        trigger_error($user->lang['BACKUP_SUCCESS']);
                        break;
                    default:
                        $tables = array();
                        switch (SQL_LAYER) {
                            case 'sqlite':
                                $sql = "SELECT name\n\t\t\t\t\t\t\t\t\tFROM sqlite_master\n\t\t\t\t\t\t\t\t\tWHERE type='table'\n\t\t\t\t\t\t\t\t\tORDER BY name";
                                $result = $db->sql_query($sql);
                                while ($row = $db->sql_fetchrow($result)) {
                                    if (strpos($row['name'], $table_prefix) === 0) {
                                        $tables[] = $row['name'];
                                    }
                                }
                                $db->sql_freeresult($result);
                                break;
                            case 'mysqli':
                            case 'mysql4':
                            case 'mysql':
                                $sql = "SHOW TABLES\n\t\t\t\t\t\t\t\t\tLIKE '{$table_prefix}%'";
                                $result = $db->sql_query($sql);
                                while ($row = $db->sql_fetchrow($result)) {
                                    $tables[] = current($row);
                                }
                                $db->sql_freeresult($result);
                                break;
                            case 'postgres':
                                $sql = "SELECT relname\n\t\t\t\t\t\t\t\t\tFROM pg_stat_user_tables\n\t\t\t\t\t\t\t\t\tORDER BY relname;";
                                $result = $db->sql_query($sql);
                                while ($row = $db->sql_fetchrow($result)) {
                                    if (strpos($row['relname'], $table_prefix) === 0) {
                                        $tables[] = $row['relname'];
                                    }
                                }
                                $db->sql_freeresult($result);
                                break;
                            case 'mssql':
                            case 'mssql_odbc':
                                $sql = "SELECT TABLE_NAME\n\t\t\t\t\t\t\t\t\tFROM INFORMATION_SCHEMA.TABLES\n\t\t\t\t\t\t\t\t\tWHERE TABLE_TYPE = 'BASE TABLE'\n\t\t\t\t\t\t\t\t\tORDER BY TABLE_NAME";
                                $result = $db->sql_query($sql);
                                while ($row = $db->sql_fetchrow($result)) {
                                    if (strpos($row['TABLE_NAME'], $table_prefix) === 0) {
                                        $tables[] = $row['TABLE_NAME'];
                                    }
                                }
                                $db->sql_freeresult($result);
                                break;
                            case 'firebird':
                                $sql = 'SELECT RDB$RELATION_NAME as TABLE_NAME
									FROM RDB$RELATIONS
									WHERE RDB$SYSTEM_FLAG=0
										AND RDB$VIEW_BLR IS NULL';
                                $result = $db->sql_query($sql);
                                while ($row = $db->sql_fetchrow($result)) {
                                    if (stripos($row['table_name'], $table_prefix) === 0) {
                                        $tables[] = $row['table_name'];
                                    }
                                }
                                $db->sql_freeresult($result);
                                break;
                            case 'oracle':
                                $sql = 'SELECT TNAME as table_name
									FROM TAB';
                                $result = $db->sql_query($sql);
                                while ($row = $db->sql_fetchrow($result)) {
                                    if (stripos($row['table_name'], $table_prefix) === 0) {
                                        $tables[] = $row['table_name'];
                                    }
                                }
                                $db->sql_freeresult($result);
                                break;
                        }
                        foreach ($tables as $table) {
                            $template->assign_block_vars('tables', array('TABLE' => $table));
                        }
                        $template->assign_vars(array('U_ACTION' => $this->u_action . '&amp;action=download'));
                        $available_methods = array('gzip' => 'zlib', 'bzip2' => 'bz2');
                        foreach ($available_methods as $type => $module) {
                            if (!@extension_loaded($module)) {
                                continue;
                            }
                            $template->assign_block_vars('methods', array('TYPE' => $type));
                        }
                        $template->assign_block_vars('methods', array('TYPE' => 'text'));
                        break;
                }
                break;
            case 'restore':
                switch ($action) {
                    case 'submit':
                        $delete = request_var('delete', '');
                        $file = request_var('file', '');
                        preg_match('#^(\\d{10})\\.(sql(?:\\.(?:gz|bz2))?)$#', $file, $matches);
                        $file_name = $phpbb_root_path . 'store/' . $matches[0];
                        if (!(file_exists($file_name) && is_readable($file_name))) {
                            trigger_error($user->lang['BACKUP_INVALID']);
                        }
                        if ($delete) {
                            unlink($file_name);
                            trigger_error($user->lang['BACKUP_DELETE']);
                        }
                        $data = file_get_contents($file_name);
                        switch ($matches[2]) {
                            case 'sql.bz2':
                                $data = bzdecompress($data);
                                break;
                            case 'sql.gz':
                                $data = gzinflate(substr($data, 10));
                                break;
                        }
                        $download = request_var('download', '');
                        if ($download) {
                            $name = $matches[0];
                            switch ($matches[2]) {
                                case 'sql':
                                    $mimetype = 'text/x-sql';
                                    break;
                                case 'sql.bz2':
                                    $mimetype = 'application/x-bzip2';
                                    break;
                                case 'sql.gz':
                                    $mimetype = 'application/x-gzip';
                                    break;
                            }
                            header('Pragma: no-cache');
                            header("Content-Type: {$mimetype}; name=\"{$name}\"");
                            header("Content-disposition: attachment; filename={$name}");
                            echo $data;
                            die;
                        }
                        if (!empty($data)) {
                            // Strip out sql comments...
                            remove_remarks($data);
                            switch (SQL_LAYER) {
                                case 'firebird':
                                    $delim = ';;';
                                    break;
                                case 'mysql':
                                case 'mysql4':
                                case 'mysqli':
                                case 'sqlite':
                                case 'postgres':
                                    $delim = ';';
                                    break;
                                case 'oracle':
                                    $delim = '/';
                                    break;
                                case 'mssql':
                                case 'mssql-odbc':
                                    $delim = 'GO';
                                    break;
                            }
                            $pieces = split_sql_file($data, $delim);
                            $sql_count = count($pieces);
                            for ($i = 0; $i < $sql_count; $i++) {
                                $sql = trim($pieces[$i]);
                                if (!empty($sql) && $sql[0] != '#') {
                                    $db->sql_query($sql);
                                }
                            }
                        }
                        add_log('admin', 'LOG_DB_RESTORE');
                        trigger_error($user->lang['RESTORE_SUCCESS']);
                        break;
                    default:
                        $selected = false;
                        $methods = array('sql');
                        $available_methods = array('sql.gz' => 'zlib', 'sql.bz2' => 'bz2');
                        foreach ($available_methods as $type => $module) {
                            if (!@extension_loaded($module)) {
                                continue;
                            }
                            $methods[] = $type;
                        }
                        $dir = $phpbb_root_path . 'store/';
                        $dh = opendir($dir);
                        while (($file = readdir($dh)) !== false) {
                            if (preg_match('#^(\\d{10})\\.(sql(?:\\.(?:gz|bz2))?)$#', $file, $matches)) {
                                $supported = in_array($matches[2], $methods);
                                if ($supported == 'true') {
                                    $template->assign_block_vars('files', array('FILE' => $file, 'NAME' => gmdate("d-m-Y H:i:s", $matches[1]), 'SUPPORTED' => $supported));
                                    $selected = true;
                                }
                            }
                        }
                        closedir($dh);
                        if ($selected === true) {
                            $template->assign_var('EXISTS', true);
                        }
                        $template->assign_vars(array('U_ACTION' => $this->u_action . '&amp;action=submit'));
                        break;
                }
                break;
        }
    }
Ejemplo n.º 6
0
 /**
  * Get column information
  * @param  int
  * @return object
  */
 protected function fetch_field($intOffset)
 {
     // Oracle starts row counting at 1
     ++$intOffset;
     $arrData['name'] = @ocicolumnname($this->resResult, $intOffset);
     $arrData['max_length'] = @ocicolumnsize($this->resResult, $intOffset);
     $arrData['not_null'] = @ocicolumnisnull($this->resResult, $intOffset);
     $arrData['type'] = @ocicolumntype($this->resResult, $intOffset);
     return $arrData;
 }
Ejemplo n.º 7
0
 /**
  * Field data
  *
  * Generates an array of objects containing field meta-data
  *
  * @access  public
  * @return  array
  */
 function field_data()
 {
     $retval = array();
     $fieldCount = $this->num_fields();
     for ($c = 1; $c <= $fieldCount; $c++) {
         $F = new stdClass();
         $F->name = ocicolumnname($this->stmt_id, $c);
         $F->type = ocicolumntype($this->stmt_id, $c);
         $F->max_length = ocicolumnsize($this->stmt_id, $c);
         $F->length = $this->getColumnLength($c);
         $F->longest = $this->getColumnLongest($c);
         $F->primary_key = $this->isColumnPrimaryKey($c);
         $F->unique = $this->isColumnUnique($c);
         $F->part_of_key = $this->isColumnPartOfKey($c);
         $F->autoIncrement = $this->isColumnAutoIncrement($c);
         $retval[] = $F;
     }
     return $retval;
 }
Ejemplo n.º 8
0
 /**
  * Get the column information
  * @param integer
  * @return object
  */
 protected function fetch_field($intOffset)
 {
     ++$intOffset;
     // Oracle starts row counting at 1
     $objData = new stdClass();
     $objData->name = @ocicolumnname($this->resResult, $intOffset);
     $objData->max_length = @ocicolumnsize($this->resResult, $intOffset);
     $objData->not_null = @ocicolumnisnull($this->resResult, $intOffset);
     $objData->type = @ocicolumntype($this->resResult, $intOffset);
     return $objData;
 }
Ejemplo n.º 9
0
 /**
  * Get the rows returned from a SELECT query.
  *
  * @param  resource		The query result pointer
  * @param  ?integer		Whether to start reading from (NULL: irrelevant for this forum driver)
  * @return array			A list of row maps
  */
 function db_get_query_rows($stmt, $start = NULL)
 {
     $out = array();
     $i = 0;
     $num_fields = ocinumcols($stmt);
     $types = array();
     $names = array();
     for ($x = 1; $x <= $num_fields; $x++) {
         $types[$x] = ocicolumntype($stmt, $x);
         $names[$x] = strtolower(ocicolumnname($stmt, $x));
     }
     while (ocifetch($stmt)) {
         if (is_null($start) || $i >= $start) {
             $newrow = array();
             for ($j = 1; $j <= $num_fields; $j++) {
                 $v = ociresult($stmt, $j);
                 if (is_object($v)) {
                     $v = $v->load();
                 }
                 // For CLOB's
                 if ($v === false) {
                     fatal_exit(do_lang_tempcode('QUERY_FAILED', ocierror($stmt)));
                 }
                 $name = $names[$j];
                 $type = $types[$j];
                 if ($type == 'NUMBER') {
                     if (!is_null($v)) {
                         $newrow[$name] = intval($v);
                     } else {
                         $newrow[$name] = NULL;
                     }
                 } else {
                     if ($v == ' ') {
                         $v = '';
                     }
                     $newrow[$name] = $v;
                 }
             }
             $out[] = $newrow;
         }
         $i++;
     }
     return $out;
 }
ocibindbyname();
ocicancel();
ocicloselob();
ocicollappend();
ocicollassign();
ocicollassignelem();
ocicollgetelem();
ocicollmax();
ocicollsize();
ocicolltrim();
ocicolumnisnull();
ocicolumnname();
ocicolumnprecision();
ocicolumnscale();
ocicolumnsize();
ocicolumntype();
ocicolumntyperaw();
ocicommit();
ocidefinebyname();
ocierror();
ociexecute();
ocifetch();
ocifetchinto();
ocifetchstatement();
ocifreecollection();
ocifreecursor();
ocifreedesc();
ocifreestatement();
ociinternaldebug();
ociloadlob();
ocilogoff();