diff --git a/backend/php-mysql+file/data/default b/backend/php-mysql+file/data/default new file mode 100644 index 0000000..a6de8b9 --- /dev/null +++ b/backend/php-mysql+file/data/default @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +INTEGERVARCHAR(100)NULLid
INTEGERVARCHAR(100)NULLid
INTEGERINTEGERVARCHAR(100)id
INTEGERINTEGERINTEGERTIMESTAMPid
\ No newline at end of file diff --git a/backend/php-mysql+file/index.php b/backend/php-mysql+file/index.php new file mode 100644 index 0000000..4528c22 --- /dev/null +++ b/backend/php-mysql+file/index.php @@ -0,0 +1,115 @@ + + * @since 10-July-2009 + */ + + // Define these constants for importing from your MySQL database. MySQL + // usually stores your metadata inside the `information_schema` database + // on the `TABLES` table + define('HOST', 'localhost'); + define('USER', 'root'); + define('PASS', 'password'); + define('DB', 'information_schema'); + + // This constant is the regular expression used to validate the filename + define('FILENAME_REGEX', '^[A-Za-z0-9_]{1,100}$'); + + // Pull the action variable from the GET and validate it + $action = isset($_GET['action']) ? $_GET['action'] : ''; + + if(!in_array($action, array('list','save','load','import'))) { + header('HTTP/1.0 501 Not Implemented'); + exit; + } + + // Save and Load actions must have the 'keyword' GET variable to work, + // I've used a regular expression to keep filenames simple and valid + // so that they don't do anything unintended. + if($action == 'save' || $action == 'load') { + if(!isset($_GET['keyword']) || !ereg(FILENAME_REGEX, $_GET['keyword'])) { + header('HTTP/1.0 400 Bad Request'); + exit; + } + } + + // The import action requires the database variable to be set + if($action == 'import' && !isset($_GET['database'])) { + header('HTTP/1.0 400 Bad Request'); + exit; + } + + // Based on the action variable return appropriate data + switch($action) { + case 'list': + // List all files in the data directory + foreach (glob('data/*') as $file) + echo basename($file)."\n"; + break; + + case 'save': + // Open the file for writing + $f = fopen('data/'.$_GET['keyword'], 'w'); + + // Read in the contents of the XML file from input + $data = file_get_contents('php://input'); + if (get_magic_quotes_gpc() || get_magic_quotes_runtime()) { + $data = stripslashes($data); + } + + // Write the data to the file and close the file + fwrite($f, $data); + fclose($f); + + // Write the response HTTP code + header('HTTP/1.0 201 Created'); + break; + + case 'load': + $keyword = 'data/' . $_GET['keyword']; + if (!file_exists($keyword)) { + header('HTTP/1.0 404 Not Found'); + } else { + header('Content-type: text/xml'); + echo file_get_contents($keyword); + } + break; + + case 'import': + // The import command may take some time, so make sure that PHP won't timeout + set_time_limit(0); + + // I've included the mysql_import function from the php_mysql plugin as its + // own file for future compatibility and to take so much code out of this one + // file. + require_once 'mysql_import.php'; + + // Connect to the MySQL Database, short circuiting ensures that this code will + // execute without error + if (!mysql_connect(HOST,USER,PASS) || !mysql_select_db(DB)) { + header("HTTP/1.0 503 Service Unavailable"); + exit; + } + + header("Content-type: text/xml"); + echo import(); + + break; + } +?> diff --git a/backend/php-mysql+file/mysql_import.php b/backend/php-mysql+file/mysql_import.php new file mode 100644 index 0000000..8d399e0 --- /dev/null +++ b/backend/php-mysql+file/mysql_import.php @@ -0,0 +1,95 @@ +'; + for ($i=1;$i'; + $comment = (isset($row["TABLE_COMMENT"]) ? $row["TABLE_COMMENT"] : ""); + if ($comment) { $xml .= ''.$comment.''; } + + $q = "SELECT * FROM COLUMNS WHERE TABLE_NAME = '".$table."' AND TABLE_SCHEMA = '".$db."'"; + $result2 = mysql_query($q); + while ($row = mysql_fetch_array($result2)) { + $name = $row["COLUMN_NAME"]; + $type = $row["COLUMN_TYPE"]; + $comment = (isset($row["COLUMN_COMMENT"]) ? $row["COLUMN_COMMENT"] : ""); + $null = ($row["IS_NULLABLE"] == "YES" ? "0" : "1"); + $def = $row["COLUMN_DEFAULT"]; + $ai = (preg_match("/auto_increment/i",$row["EXTRA"]) ? "1" : "0"); + if ($def == "NULL") { $def = ""; } + $xml .= ''; + $xml .= ''.strtoupper($type).''; + $xml .= ''.$def.''; + if ($comment) { $xml .= ''.$comment.''; } + + /* fk constraints */ + $q = "SELECT + REFERENCED_TABLE_NAME AS 'table', REFERENCED_COLUMN_NAME AS 'column' + FROM KEY_COLUMN_USAGE k + LEFT JOIN TABLE_CONSTRAINTS c + ON k.CONSTRAINT_NAME = c.CONSTRAINT_NAME + WHERE CONSTRAINT_TYPE = 'FOREIGN KEY' + AND c.TABLE_SCHEMA = '".$db."' AND c.TABLE_NAME = '".$table."' + AND k.COLUMN_NAME = '".$name."'"; + $result3 = mysql_query($q); + + while ($row = mysql_fetch_array($result3)) { + $xml .= ''; + } + + $xml .= ''; + } + + /* keys */ + $q = "SELECT * FROM STATISTICS WHERE TABLE_NAME = '".$table."' AND TABLE_SCHEMA = '".$db."' ORDER BY SEQ_IN_INDEX ASC"; + $result2 = mysql_query($q); + $idx = array(); + + while ($row = mysql_fetch_array($result2)) { + $name = $row["INDEX_NAME"]; + if (array_key_exists($name, $idx)) { + $obj = $idx[$name]; + } else { + $type = $row["INDEX_TYPE"]; + $t = "INDEX"; + if ($type == "FULLTEXT") { $t = $type; } + if ($row["NON_UNIQUE"] == "0") { $t = "UNIQUE"; } + if ($name == "PRIMARY") { $t = "PRIMARY"; } + + $obj = array( + "columns" => array(), + "type" => $t + ); + } + + $obj["columns"][] = $row["COLUMN_NAME"]; + $idx[$name] = $obj; + } + + foreach ($idx as $name=>$obj) { + $xml .= ''; + for ($i=0;$i'; + } + $xml .= ''; + } + $xml .= ""; + } + $arr[] = $xml; + $arr[] = ''; + return implode("\n",$arr); + } +?> \ No newline at end of file