From 8edb9c2723b2cdecc761c2f465e75e5dabef98b6 Mon Sep 17 00:00:00 2001 From: Igor Feghali Date: Sat, 1 Nov 2008 13:08:38 +0000 Subject: [PATCH] - mdb2_schema tool moved from "bin" to "scripts" folder. now installs to pear_bin dir - introducing www/mdb2_schematool that is a rewrite of docs/examples/example.php and should be installed to web root - package.xml reflects new files changes git-svn-id: http://svn.php.net/repository/pear/packages/MDB2_Schema/trunk@268111 c90b9560-bf6c-de11-be94-00142212c4b1 --- docs/examples/example.php | 419 ------------------------------ package.xml | 20 +- {bin => scripts}/mdb2_schematool | 4 +- www/mdb2_schematool/action.php | 207 +++++++++++++++ www/mdb2_schematool/class.inc.php | 191 ++++++++++++++ www/mdb2_schematool/index.php | 211 +++++++++++++++ www/mdb2_schematool/result.php | 92 +++++++ 7 files changed, 717 insertions(+), 427 deletions(-) delete mode 100644 docs/examples/example.php rename {bin => scripts}/mdb2_schematool (80%) create mode 100644 www/mdb2_schematool/action.php create mode 100644 www/mdb2_schematool/class.inc.php create mode 100644 www/mdb2_schematool/index.php create mode 100644 www/mdb2_schematool/result.php diff --git a/docs/examples/example.php b/docs/examples/example.php deleted file mode 100644 index 78bd689..0000000 --- a/docs/examples/example.php +++ /dev/null @@ -1,419 +0,0 @@ - | -// | Author: Igor Feghali | -// +----------------------------------------------------------------------+ -// -// $Id$ -// - -/** - * MDB2 reverse engineering of xml schemas script. - * - * This is all rather ugly code, thats probably very much XSS exploitable etc. - * However the idea was to keep the magic and dependencies low, to just - * illustrate the MDB2_Schema API a bit. - * - * @package MDB2 - * @category Database - * @author Lukas Smith - */ - -?> - - - MDB2_Schema example - -getOption('log_line_break'); - } - MDB2_defaultDebugOutput($db, $scope, $message); -} - -$databases = array( - 'mysql' => 'MySQL', - 'mysqli' => 'MySQLi', - 'pgsql' => 'PostGreSQL', - 'sqlite' => 'SQLite' -); - -$options = array( - 'use_transactions' => false, - 'log_line_break' => '
', - 'idxname_format' => '%s', - 'DBA_username' => '', - 'DBA_password' => '', - 'debug' => true, - 'quote_identifier' => true, - 'force_defaults' => false, - 'portability' => false -); - -if (isset($_REQUEST['submit'])) { - require_once 'MDB2/Schema.php'; - - foreach ($options as $k => $v) { - if ((isset($_REQUEST[$k])) && (!empty($_REQUEST[$k]))) { - $options[$k] = $_REQUEST[$k]; - } elseif ($v) { - $options[$k] = false; - } - } - - if (isset($_REQUEST['disable_query']) && $_REQUEST['disable_query']) { - $disable_query = true; - } else { - $disable_query = false; - } - - $dsn = $_REQUEST['type'].'://'.$_REQUEST['user'].':'.$_REQUEST['pass'].'@'.$_REQUEST['host'].'/'.$_REQUEST['name']; - - $schema =& MDB2_Schema::factory($dsn, $options); - $schema->db->supported['transactions'] = false; - if (PEAR::isError($schema)) { - $error = $schema->getMessage() . ' ' . $schema->getUserInfo(); - } elseif (array_key_exists('action', $_REQUEST)) { - set_time_limit(0); - - /* DUMP DATABASE */ - if ($_REQUEST['action'] == 'dump' && array_key_exists('dumptype', $_REQUEST)) { - switch ($_REQUEST['dumptype']) { - case 'structure': - $dump_what = MDB2_SCHEMA_DUMP_STRUCTURE; - break; - case 'content': - $dump_what = MDB2_SCHEMA_DUMP_CONTENT; - break; - default: - $dump_what = MDB2_SCHEMA_DUMP_ALL; - break; - } - $dump_config = array( - 'output_mode' => 'file', - 'output' => $_REQUEST['file'] - ); - - $definition = $schema->getDefinitionFromDatabase(); - if (PEAR::isError($definition)) { - $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); - } else { - $operation = $schema->dumpDatabase($definition, $dump_config, $dump_what); - if (PEAR::isError($operation)) { - $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); - } else { - call_user_func($var_dump, $operation); - } - } - - /* UPDATE DATABASE */ - } elseif ($_REQUEST['action'] == 'update') { - if ($disable_query) { - $debug_tmp = $schema->db->getOption('debug'); - $schema->db->setOption('debug', true); - $debug_handler_tmp = $schema->db->getOption('debug_handler'); - $schema->db->setOption('debug_handler', 'printQueries'); - } - - $dump_config = array( - 'output_mode' => 'file', - 'output' => $_REQUEST['file'].'.old' - ); - $definition = $schema->getDefinitionFromDatabase(); - if (PEAR::isError($definition)) { - $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); - } else { - $operation = $schema->dumpDatabase($definition, $dump_config, MDB2_SCHEMA_DUMP_ALL); - if (PEAR::isError($operation)) { - $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); - } else { - call_user_func($var_dump, $operation); - - $operation = $schema->updateDatabase($_REQUEST['file'] - , $_REQUEST['file'].'.old', array(), $disable_query - ); - if (PEAR::isError($operation)) { - $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); - } else { - call_user_func($var_dump, $operation); - } - } - } - - if ($disable_query) { - $schema->db->setOption('debug', $debug_tmp); - $schema->db->setOption('debug_handler', $debug_handler_tmp); - } - - /* CREATE DATABASE */ - } elseif ($_REQUEST['action'] == 'create') { - if ($disable_query) { - $debug_tmp = $schema->db->getOption('debug'); - $schema->db->setOption('debug', true); - $debug_handler_tmp = $schema->db->getOption('debug_handler'); - $schema->db->setOption('debug_handler', 'printQueries'); - } - - $definition = $schema->parseDatabaseDefinition( - $_REQUEST['file'], false, array(), $schema->options['fail_on_invalid_names'] - ); - if (PEAR::isError($definition)) { - $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); - } else { - $schema->db->setOption('disable_query', $disable_query); - $operation = $schema->createDatabase($definition); - $schema->db->setOption('disable_query', false); - - if (PEAR::isError($operation)) { - $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); - } else { - call_user_func($var_dump, $operation); - } - } - - if ($disable_query) { - $schema->db->setOption('debug', $debug_tmp); - $schema->db->setOption('debug_handler', $debug_handler_tmp); - } - } - - /* INITIALIZE DATABASE - * - * To be used when we split initialization and - * definition into two diferent structures - * - */ - /*} elseif ($_REQUEST['action'] == 'initialize') { - if ($disable_query) { - $debug_tmp = $schema->db->getOption('debug'); - $schema->db->setOption('debug', true); - $debug_handler_tmp = $schema->db->getOption('debug_handler'); - $schema->db->setOption('debug_handler', 'printQueries'); - } - - $definition = $schema->parseDatabaseDefinition( - $_REQUEST['file'], false, array(), $schema->options['fail_on_invalid_names'] - ); - - $schema->db->setOption('disable_query', $disable_query); - if (isset($definition['tables']) - && is_array($definition['tables']) - ) { - foreach ($definition['tables'] as $table_name => $table) { - $operation = $schema->initializeTable($table_name, $table); - if (PEAR::isError($operation)) { - echo $operation->getMessage() . ' ' . $operation->getUserInfo(); - } else { - call_user_func($var_dump, $operation); - } - } - } - $schema->db->setOption('disable_query', false); - - if ($disable_query) { - $schema->db->setOption('debug', $debug_tmp); - $schema->db->setOption('debug_handler', $debug_handler_tmp); - }*/ - - /* NO ACTION */ - } else { - $error = 'Script Error: no action selected'; - } - - $warnings = $schema->getWarnings(); - if (count($warnings) > 0) { - echo('

Warnings

'); - call_user_func($var_dump, $warnings); - } - - if ($schema->db->getOption('debug')) { - echo('

Debug messages

'); - echo($schema->db->getDebugOutput().'
'); - } - - if (isset($_REQUEST['show_structure']) - && $_REQUEST['show_structure'] - && isset($definition) - && is_array($definition) - ) { - echo('

Database structure

'); - call_user_func($var_dump, $definition); - } - - $schema->disconnect(); -} - -if (!isset($_REQUEST['submit']) || isset($error)) { - if (isset($error) && $error) { - echo '
    '; - echo '
  • ' . $error . '
  • '; - echo '
'; - } -?> -
-
- Database information - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
/> - -
/>
/>
-
- -
- Options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/>
/>
/>
/>
/>
/>
-
- -

- -
- - diff --git a/package.xml b/package.xml index fb61033..5c703e7 100644 --- a/package.xml +++ b/package.xml @@ -92,14 +92,22 @@ open todo items: - Fulltext index support - - - + + + + + + + + + + + + + + - - - diff --git a/bin/mdb2_schematool b/scripts/mdb2_schematool similarity index 80% rename from bin/mdb2_schematool rename to scripts/mdb2_schematool index e295333..01c5868 100755 --- a/bin/mdb2_schematool +++ b/scripts/mdb2_schematool @@ -1,5 +1,5 @@ -#!/bin/env php +#!@php_bin@ \ No newline at end of file +?> diff --git a/www/mdb2_schematool/action.php b/www/mdb2_schematool/action.php new file mode 100644 index 0000000..a7ca8d7 --- /dev/null +++ b/www/mdb2_schematool/action.php @@ -0,0 +1,207 @@ + | +// | Author: Igor Feghali | +// +----------------------------------------------------------------------+ +// +// $Id$ +// + +/** + * This is all rather ugly code, thats probably very much XSS exploitable etc. + * However the idea was to keep the magic and dependencies low, to just + * illustrate the MDB2_Schema API a bit. + */ + +require_once 'MDB2/Schema.php'; +require_once 'class.inc.php'; + +$data =& MDB2_Schema_Example::factory($_GET); +if (PEAR::isError($data)) { + setcookie('error', $data->getMessage()); + header('location: index.php'); + exit; +} + +$schema =& MDB2_Schema::factory($data->dsn, $data->options); +if (PEAR::isError($schema)) { + $error = $schema->getMessage() . ' ' . $schema->getUserInfo(); + setcookie('error', $error); + header('location: index.php'); + exit; +} + +switch ($data->action) { +/* DUMP DATABASE */ +case 'dump': + switch ($data->dumptype) { + case 'structure': + $dump_what = MDB2_SCHEMA_DUMP_STRUCTURE; + break; + case 'content': + $dump_what = MDB2_SCHEMA_DUMP_CONTENT; + break; + default: + $dump_what = MDB2_SCHEMA_DUMP_ALL; + break; + } + $dump_config = array( + 'output_mode' => 'file', + 'output' => $data->file + ); + + $definition = $schema->getDefinitionFromDatabase(); + if (PEAR::isError($definition)) { + $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); + } else { + $operation = $schema->dumpDatabase($definition, $dump_config, $dump_what); + if (PEAR::isError($operation)) { + $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); + } + } + break; + +/* UPDATE DATABASE */ +case 'update': + if ($data->disable_query) { + $debug_tmp = $schema->db->getOption('debug'); + $schema->db->setOption('debug', true); + $debug_handler_tmp = $schema->db->getOption('debug_handler'); + $schema->db->setOption('debug_handler', 'printQueries'); + } + + $dump_config = array( + 'output_mode' => 'file', + 'output' => $data->file.'.old' + ); + $definition = $schema->getDefinitionFromDatabase(); + if (PEAR::isError($definition)) { + $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); + } else { + $operation = $schema->dumpDatabase($definition, $dump_config, MDB2_SCHEMA_DUMP_ALL); + if (PEAR::isError($operation)) { + $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); + } else { + $operation = $schema->updateDatabase($data->file + , $data->file.'.old', array(), $data->disable_query + ); + if (PEAR::isError($operation)) { + $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); + } + } + } + + if ($data->disable_query) { + $schema->db->setOption('debug', $debug_tmp); + $schema->db->setOption('debug_handler', $debug_handler_tmp); + } + break; + +/* CREATE DATABASE */ +case 'create': + if ($data->disable_query) { + $debug_tmp = $schema->db->getOption('debug'); + $schema->db->setOption('debug', true); + $debug_handler_tmp = $schema->db->getOption('debug_handler'); + $schema->db->setOption('debug_handler', 'printQueries'); + } + + $definition = $schema->parseDatabaseDefinition( + $data->file, false, array(), $schema->options['fail_on_invalid_names'] + ); + if (PEAR::isError($definition)) { + $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); + } else { + $schema->db->setOption('disable_query', $data->disable_query); + $operation = $schema->createDatabase($definition); + $schema->db->setOption('disable_query', false); + + if (PEAR::isError($operation)) { + $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); + } + } + + if ($data->disable_query) { + $schema->db->setOption('debug', $debug_tmp); + $schema->db->setOption('debug_handler', $debug_handler_tmp); + } + break; + +/* INITIALIZE DATABASE */ +case 'initialize': + if ($data->disable_query) { + $debug_tmp = $schema->db->getOption('debug'); + $schema->db->setOption('debug', true); + $debug_handler_tmp = $schema->db->getOption('debug_handler'); + $schema->db->setOption('debug_handler', 'printQueries'); + } + + $definition = $schema->parseDatabaseDefinition( + $data->file, false, array(), $schema->options['fail_on_invalid_names'] + ); + if (PEAR::isError($definition)) { + $error = $definition->getMessage() . ' ' . $definition->getUserInfo(); + } else { + $schema->db->setOption('disable_query', $data->disable_query); + if (isset($definition['tables']) + && is_array($definition['tables']) + ) { + foreach ($definition['tables'] as $table_name => $table) { + $operation = $schema->initializeTable($table_name, $table); + if (PEAR::isError($operation)) { + $error = $operation->getMessage() . ' ' . $operation->getUserInfo(); + } + } + } + $schema->db->setOption('disable_query', false); + } + + if ($data->disable_query) { + $schema->db->setOption('debug', $debug_tmp); + $schema->db->setOption('debug_handler', $debug_handler_tmp); + } + break; +} + +include 'result.php'; +$schema->disconnect(); +?> diff --git a/www/mdb2_schematool/class.inc.php b/www/mdb2_schematool/class.inc.php new file mode 100644 index 0000000..a4ac4c2 --- /dev/null +++ b/www/mdb2_schematool/class.inc.php @@ -0,0 +1,191 @@ + | +// +----------------------------------------------------------------------+ +// +// $Id$ +// + +/** + * This is all rather ugly code, thats probably very much XSS exploitable etc. + * However the idea was to keep the magic and dependencies low, to just + * illustrate the MDB2_Schema API a bit. + */ + +class MDB2_Schema_Example +{ + var $options = array( + 'log_line_break' => '
', + 'idxname_format' => '%s', + 'DBA_username' => '', + 'DBA_password' => '', + 'default_table_type' => 'MyISAM', + 'debug' => true, + 'use_transactions' => true, + 'quote_identifier' => true, + 'force_defaults' => false, + 'portability' => false, + ); + + var $dsn = array( + 'phptype' => '', + 'username' => 'root', + 'password' => '', + 'hostspec' => 'localhost', + 'database' => 'MDB2Example', + 'charset' => 'utf8' + ); + + var $show_structure = false; + var $disable_query = false; + var $action = ''; + var $dumptype = ''; + var $file = ''; + + function factory($input) { + $obj = new MDB2_Schema_Example($input); + if ($error = $obj->validateInput($input)) { + return PEAR::raiseError($error); + } else { + $obj->saveCookies(); + $obj->setOptions($input); + return $obj; + } + } + + function printQueries(&$db, $scope, $message) + { + if ($scope == 'query') { + echo $message.$db->getOption('log_line_break'); + } + MDB2_defaultDebugOutput($db, $scope, $message); + } + + function setOptions($options) + { + foreach ($this->options as $k => $v) { + if (is_string($v)) { + if (isset($options[$k])) { + $this->options[$k] = $options[$k]; + } else { + $this->options[$k] = ''; + } + } else { + if ((isset($options[$k])) && (!empty($options[$k]))) { + $this->options[$k] = true; + } else { + $this->options[$k] = false; + } + } + } + + $this->dsn = array( + 'phptype' => $options['type'], + 'username' => $options['user'], + 'password' => $options['pass'], + 'hostspec' => $options['host'], + 'database' => $options['name'], + 'charset' => $options['char'] + ); + } + + function validateInput($input) + { + if (!array_key_exists('action', $input)) { + return 'Script Error: no action selected'; + } + switch ($input['action']) { + case 'dump': + if (!array_key_exists('dumptype', $input)) { + return 'no dump type specified'; + } + $this->dumptype = $input['dumptype']; + if (!array_key_exists('file', $input)) { + return 'no output file specified'; + } + $this->file = $input['file']; + break; + + case 'update': + case 'create': + case 'initialize': + break; + + default: + return 'Script Error: invalid action'; + } + + $this->action = $input['action']; + if (isset($input['show_structure'])) { + $this->show_structure = $input['show_structure']; + } + return false; + } + + function saveCookies() { + setcookie('use_transactions', $this->options['use_transactions']); + setcookie('default_table_type', $this->options['default_table_type']); + setcookie('log_line_break', $this->options['log_line_break']); + setcookie('idxname_format', $this->options['idxname_format']); + setcookie('DBA_username', $this->options['DBA_username']); + setcookie('DBA_password', $this->options['DBA_password']); + setcookie('debug', $this->options['debug']); + setcookie('quote_identifier', $this->options['quote_identifier']); + setcookie('force_defaults', $this->options['force_defaults']); + setcookie('portability', $this->options['portability']); + + setcookie('disable_query', $this->disable_query); + setcookie('action', $this->action); + setcookie('dumptype', $this->dumptype); + setcookie('file', $this->file); + setcookie('show_structure', $this->show_structure); + + setcookie('username', $this->dsn['username']); + setcookie('password', $this->dsn['password']); + setcookie('hostspec', $this->dsn['hostspec']); + setcookie('database', $this->dsn['database']); + setcookie('charset', $this->dsn['charset']); + + setcookie('loaded', '1'); + } +} +?> diff --git a/www/mdb2_schematool/index.php b/www/mdb2_schematool/index.php new file mode 100644 index 0000000..8f6fce2 --- /dev/null +++ b/www/mdb2_schematool/index.php @@ -0,0 +1,211 @@ + | +// | Author: Igor Feghali | +// +----------------------------------------------------------------------+ +// +// $Id$ +// + +/** + * This is all rather ugly code, thats probably very much XSS exploitable etc. + * However the idea was to keep the magic and dependencies low, to just + * illustrate the MDB2_Schema API a bit. + */ + +if (!isset($_REQUEST['loaded'])) { + require_once 'class.inc.php'; + $defaults = new MDB2_Schema_Example(); + $defaults->saveCookies(); + header('location: index.php?loaded=1'); + exit; +} + +$databases = array( + 'mysql' => 'MySQL', + 'mysqli' => 'MySQLi', + 'pgsql' => 'PostGreSQL', + 'sqlite' => 'SQLite' +); +?> + + + MDB2_Schema Web Frontend + +Error'; + echo '
    '; + echo '
  • ' . $_COOKIE['error'] . '
  • '; + echo '
'; + setcookie('error',''); +} +?> +
+
+ Database information + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
/> + +
/>
/>
/>
+
+ +
+ Options + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
/>
/>
/>
/>
/>
/>
/>
+
+ +

+
+ + diff --git a/www/mdb2_schematool/result.php b/www/mdb2_schematool/result.php new file mode 100644 index 0000000..0ca5772 --- /dev/null +++ b/www/mdb2_schematool/result.php @@ -0,0 +1,92 @@ + | +// | Author: Igor Feghali | +// +----------------------------------------------------------------------+ +// +// $Id$ +// + +/** + * This is all rather ugly code, thats probably very much XSS exploitable etc. + * However the idea was to keep the magic and dependencies low, to just + * illustrate the MDB2_Schema API a bit. + */ + +?> + + + MDB2_Schema Web Frontend + +getWarnings(); +if (count($warnings) > 0) { + echo '

Warnings

'; + echo '
';
+    var_dump($warnings);
+    echo '
'; +} + +if ($schema->db->getOption('debug')) { + echo '

Debug messages

'; + echo $schema->db->getDebugOutput().'
'; +} + +if ($data->show_structure + && isset($definition) + && is_array($definition) +) { + echo '

Database structure

'; + $var_dump($definition); +} + +if (isset($error) && $error) { + echo '

Error

'; + echo '
    '; + echo '
  • ' . $error . '
  • '; + echo '
'; + setcookie('error',''); +} +?> + +