<?php
/*
* phpMyEdit - instant MySQL table editor and code generator
*
* phpMyEditSetup.php - interactive table configuration utility (setup)
* ____________________________________________________________________
*
* Copyright (c) 1999-2002 John McCreesh <jpmcc@users.sourceforge.net>
* Copyright (c) 2001-2002 Jim Kraai <jkraai@users.sourceforge.net>
* Versions 5.0 and higher developed by Ondrej Jombik <nepto@php.net>
* Versions 5.7.2 and higher developed by Yves De Billoëz
* Copyright (c) 2002-2006 Platon Group, http://platon.sk/
* All rights reserved.
*
* See README.md file for more information about this software.
* See COPYING file for license information.
*
* Download the latest version from
* https://sourceforge.net/projects/phpmariaedit/
*/
/* $Platon: phpMyEdit/phpMyEditSetup.php,v 5.7.5 2024-01-14 12:57:07 Yves $ */
//declare(strict_types=1);
//ini_set('display_errors', '1');
//ini_set('display_startup_errors', '1');
//error_reporting(E_ALL);
//global $debug_query; $debug_query = true;
//mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>phpMyEdit Setup</title>
<link rel="stylesheet" href="css/phpMyEditSetup.css" />
</head>
<body>
<?php
// --------------------------------------------------------------------
// setup environment
// --------------------------------------------------------------------
if (! defined('PHP_EOL')) {
define('PHP_EOL', strtoupper(substr(PHP_OS, 0, 3) == 'WIN') ? "\r\n"
: (strtoupper(substr(PHP_OS, 0, 3) == 'MAC') ? "\r" : "\n"));
}
if (!isset($_POST['un']) && file_exists(dirname(__FILE__).'/lib/phpMyEditDB.php')) {
include(dirname(__FILE__).'/lib/phpMyEditDB.php');
$hn = $opts['hn'];
$pt = $opts['pt'];
$un = $opts['un'];
$pw = $opts['pw'];
$db = $opts['db'];
} else {
$hn = @$_POST['hn']; // host (default localhost)
$pt = @$_POST['pt']; // port (default 3306)
$un = @$_POST['un']; // user name
$pw = @$_POST['pw']; // password
}
if(isset($_POST['db']) && strlen($_POST['db'])>0)
$db = @$_POST['db']; // database (optional)
if(isset($_POST['tb']) && strlen($_POST['tb'])>0)
$tb = @$_POST['tb']; // table name (optional)
$id = @$_POST['id']; // table unique key
$submit = @$_POST['submit']; // submit button
$options = @$_POST['options'];
$multifile = @$_POST['multifile'];
$baseFilename = @$_POST['baseFilename'];
$pageTitle = @$_POST['pageTitle'];
$pageHeader = @$_POST['pageHeader'];
$HTMLissues = @$_POST['HTMLissues'];
$CSSstylesheet = @$_POST['CSSstylesheet'];
// --------------------------------------------------------------------
// Support functions start here
// --------------------------------------------------------------------
function build_buffer($x)
{
global $buffer;
$buffer .= $x . PHP_EOL;
}
#:#####################################:#
#:# Function: check_constraints #:#
#:# Parameters: tb=table name #:#
#:# field=field name #:#
#:# db=database name #:#
#:# db1=database handle #:#
#:# return: lookup default for #:#
#:# said constraint #:#
#:# or null if no #:#
#:# constraint is found. #:#
#:# Contributed by Wade Ryan, #:#
#:# 20060906 #:#
#:#####################################:#
function check_constraints($db, $tb, $field, $db1)
{
$constraint_arg="";
mysqli_select_db($db1, $db);
$sql = "show create table $tb";
$result = mysqli_query($db1, $sql);
$row = mysqli_fetch_assoc($result);
$tableDef = preg_split('/\n/',$row['Create Table']);
foreach($tableDef as $key => $val) {
$words=preg_split("/[\s'`()]+/", $val);
if ($words[1] == "CONSTRAINT" && $words[6]=="REFERENCES") {
if ($words[5]==$field) {
$constraint_arg=" 'values' => array(\n".
" 'table' => '$words[7]',\n".
" 'column' => '$words[8]'\n".
" ),";
}
}
}
mysqli_free_result($result);
return $constraint_arg;
}
function get_versions()
{
$ret_ar = array();
$dirname = dirname(__FILE__);
foreach (array(
'current' => __FILE__,
'setup' => "$dirname/phpMyEditSetup.php",
'core' => "$dirname/lib/phpMyEdit.class.php",
'version' => "$dirname/doc/VERSION")
as $type => $file) {
if (@file_exists($file) && @is_readable($file)) {
if (($f = fopen($file, 'r')) == false) {
continue;
}
$str = trim(fread($f, 4096));
if (strpos($str, ' ') === false && strlen($str) < 10) {
$ret_ar[$type] = $str;
} else if (preg_match('|.*\$'.'Platon.*,v\s(\d*\.\S*).*|', $str, $matches)) {
$ret_ar[$type] = $matches[1];
}
fclose($f);
}
}
return $ret_ar;
}
function EchoFormHiddenInput ($self, $hn, $pt, $un, $pw, $db, $tb, $id, $opt)
{
echo '<form action="'.htmlspecialchars($self).'" method="POST">
<input type="hidden" name="hn" value = "' . htmlspecialchars($hn) . '">
<input type="hidden" name="pt" value = "' . htmlspecialchars($pt) . '">
<input type="hidden" name="un" value = "' . htmlspecialchars($un) . '">
<input type="hidden" name="pw" value= "' . htmlspecialchars($pw) . '">' . PHP_EOL;
if ($db != "")
echo '<input type="hidden" name="db" value = "' . htmlspecialchars($db) . '">' . PHP_EOL;
if ($tb != "")
echo '<input type="hidden" name="tb" value = "' . htmlspecialchars($tb) . '">' . PHP_EOL;
if ($id != "")
echo '<input type="hidden" name="id" value = "' . htmlspecialchars($id) . '">' . PHP_EOL;
if ($opt != "")
echo '<input type="hidden" name="options" value="1">' . PHP_EOL;
}
function ShowLogin ($self, $hn, $pt, $un, $pw, $db, $tb)
{
echo '
<form action="' . htmlspecialchars($self) . '" method="POST">
<table summary="Login form">
<tr>
<td>Hostname:</td>
<td><input type="text" name="hn" value="' . htmlspecialchars($hn) . '"></td>
</tr><tr>
<tr>
<td>Port:</td>
<td><input type="text" name="pt" value="' . htmlspecialchars($pt) . '"></td>
</tr><tr>
<td>Username:</td>
<td><input type="text" name="un" value="'.htmlspecialchars($un).'"></td>
</tr><tr>
<td>Password:</td>
<td><input type="password" name="pw" value="'.htmlspecialchars($pw).'"></td>
</tr><tr>
<td>Database:</td>
<td><input type="text" name="db" value="'.htmlspecialchars($db).'"></td>
</tr><tr>
<td>Table:</td>
<td><input type="text" name="tb" value="'.htmlspecialchars($tb).'"></td>
</tr>
</table><br/>
<input type="submit" name="submit" value="Submit">
</form>'.PHP_EOL;
}
function ShowSelectDatabase ($self, $hn, $pt, $un, $pw, $db1)
{
$error = false;
$checked = '';
$sql = "show databases";
$result = mysqli_query($db1, $sql);
if ($error == false) {
echo '<h1>Please select a database</h1>' . PHP_EOL;
EchoFormHiddenInput ($self, $hn, $pt, $un, $pw, "", "", "", "");
echo '
<table class="selectdb" summary="Database selection">' . PHP_EOL;
while ($row = mysqli_fetch_assoc($result)) {
$db = $row['Database'];
echo '<tr><td><input'.$checked.' type="radio" name="db" value="'.$db.'"></td><td>'.$db.'</td></tr>'.PHP_EOL;
}
echo '</table><br/>
<input type="submit" name="submit" value="Submit">
<input type="submit" name="cancel" value="Cancel">
</form>' . PHP_EOL;
mysqli_free_result ($result);
}
}
function CheckForDatabase ($self, $db, $db1)
{
$havedb = false;
$sql = "show databases";
$result = mysqli_query($db1, $sql);
while ($row = mysqli_fetch_array($result)) {
$thisdb = $row [0];
if ($thisdb == $db) {
$havedb = true;
break;
}
}
mysqli_free_result ($result);
return $havedb;
}
function CheckForTable ($self, $db, $tb, $db1)
{
$havetb = false;
$sql = "show columns from $tb in $db";
$result = mysqli_query($db1, $sql);
if ($row = mysqli_fetch_assoc($result)) {
$havetb = true;
}
mysqli_free_result ($result);
return $havetb;
}
function ShowSelectTable ($self, $hn, $pt, $un, $pw, $db, $db1)
{
echo '<h1>Please select a table from database: '.htmlspecialchars($db).'</h1>' . PHP_EOL;
EchoFormHiddenInput ($self, $hn, $pt, $un, $pw, $db, "", "", "");
echo '
<table class="selecttable" summary="Table selection">' . PHP_EOL;
$sql = "show tables from $db";
$result = mysqli_query($db1, $sql);
while ($row = mysqli_fetch_array($result)) {
$tb = $row[0];
$exists = '';
if (file_exists($tb.'.php')) $exists = 'Test <a href="'.$tb.'.php'.'">'.$tb.'</a>';
echo '<tr><td><input type="radio" name="tb" value="' . $tb . '"></td><td>' . $tb . '</td><td>' . $exists . '</td></tr>' . PHP_EOL;
}
echo '</table><br/>
<input type="submit" name="submit" value="Submit">
<input type="submit" name="cancel" value="Cancel">
</form>'.PHP_EOL;
mysqli_free_result ($result);
}
function ShowSelectId ($self, $hn, $pt, $un, $pw, $db, $tb, $db1)
{
echo ' <h1>Please select an identifier from table: ' . htmlspecialchars($tb) . '</h1>
<p>
This field will be used in change, view, copy and delete operations.<br/>
The field should be numeric and must uniquely identify a record.
</p>
<p>
Please note, that there were problems reported by phpMyEdit users regarding using MySQL reserved word as unique key name (the example for this is "key" name). Thus we recommend you to use another name of unique key. Usage of "id" or "ID" should be safe and good idea.
</p>' . PHP_EOL;
EchoFormHiddenInput ($self, $hn, $pt, $un, $pw, $db, $tb, "", "");
echo ' <table class="selectid" summary="Key selection">' . PHP_EOL;
$sql = "show columns from $tb in $db";
$result = mysqli_query($db1, $sql);
while ($row = mysqli_fetch_array($result)) {
$field = $row[0];
$type = $row[1];
$ff = '';
$key = $row[3];
if ($key == "PRI") {
$checked = "checked";
$ff .= $key . ", ";
// get primary key type
} else {
$checked = "";
}
if ($row[2] != 'NO') $checked = "disabled";
if (isset($row[4])) {
$ff .= $row[4] . ', ' . $row[5];
} else {
$ff .= $row[5];
}
$ff = rtrim($ff,', ');
echo '<tr><td><input ' . $checked . ' type="radio" name="id" value="',htmlspecialchars($field),'"></td>';
echo '<td>',htmlspecialchars($field),'</td>';
echo '<td>',htmlspecialchars($ff),'</td></tr>' . PHP_EOL;;
}
mysqli_free_result ($result);
echo '</table><br/>
<input type="submit" name="submit" value="Submit">
<input type="submit" name="cancel" value="Cancel">
</form>' . PHP_EOL;
}
function GetPrimaryKeyType ($self, $hn, $pt, $un, $pw, $db, $tb, $db1)
{
$primarykeytype = "";
$sql = "show columns from $tb in $db";
$result = mysqli_query($db1, $sql);
while ($row = mysqli_fetch_array($result)) {
$field = $row [0];
$type = $row [1];
$key = $row [3];
if ($key == "PRI") {
// what if UNI(que) and no PRI
// now decode the type
$decodeType = DecodeType ($type);
$primarykeytype = $decodeType[0]; // array: type, length
}
}
mysqli_free_result ($result);
return $primarykeytype;
}
function DecodeType ($type)
{
$decodeType = $type;
$length = 0;
$i = strpos ($type, "(");
if ($i > 0) {
$decodeType = substr ($type, 0, $i);
}
$length = GetLength ($type);
return array ("$decodeType", "$length");
}
function GetLongestValue ($type)
{
$len = 0;
$i = strpos ($type, "(");
$j = strlen ($type);
$mytype = substr ($type, $i + 1, ($j - $i) - 2);
// split $mytype at ","
$values = explode(",", $mytype);
$i = count ($values);
for ($j = 0; $j < $i; $j++) {
$k = strlen ($values [$j]);
if ($k > $len) $len = $k;
}
if ($len > 0) $len -= 2;
return $len;
}
function ShowSelectOptions ($self, $hn, $pt, $un, $pw, $db, $tb, $db1, $id)
{
echo "Database settings: $hn, $pt, $un, ***, $db, $tb, $id<br/>";
echo '<h1>Please select additional options</h1>' . PHP_EOL;
EchoFormHiddenInput ($self, $hn, $pt, $un, $pw, $db, $tb, $id, "");
$multifilecheck = '';
if (file_exists('phpMyEditDefaults.php')) $multifilecheck = 'checked onclick="return false;" onkeydown="return false;"';
$showheadercheck = '';
if (file_exists('phpMyEditHeader.php')) $showheadercheck = 'checked onclick="return false;" onkeydown="return false;"';
echo ' <table class="options" summary="Additional options">
<tr><td>Split result files</td><td><input type="checkbox" '.$multifilecheck.' name=multifile value ="multifile"></td><td>Create multiple files (DB, Header, Defaults)</td></tr>
<tr><td>Base filename</td><td><input type="text" name=baseFilename value ="'.htmlspecialchars($tb).'"></td><td>Contains table information</td></tr>
<tr><td>HTML header & footer</td><td><input type="checkbox" '.$showheadercheck.' name=HTMLissues></td><td></td></tr>
<tr><td>Show page title</td><td><input type="checkbox" name=pageHeader></td><td></td></tr>
<tr><td>Page title</td><td><input type="text" name=pageTitle value ="'.ucfirst(htmlspecialchars($tb)).'"></td><td></td></tr>
<tr><td>CSS basic stylesheet</td><td><input checked type="checkbox" name=CSSstylesheet></td><td>If set, stylesheet will be used</td></tr>
</table><br/>
<input type="submit" name="submit" value="Submit">
<input type="submit" name="cancel" value="Cancel">
<input type="hidden" name="options" value="1">
</form>' . PHP_EOL;
}
function GetLength($type)
{
$len = 0;
$mytype = $type;
$i = strpos ($mytype, "(");
if ($i > 0) {
$mytype = substr ($mytype, 0, $i);
}
switch ($mytype) {
case "blob":
$len = 60;
break;
case "date":
$len = 10;
break;
case "datetime":
$len = 22;
break;
case "enum":
$len = 30;
// now get the longest enum value
$len = GetLongestValue ($type);
break;
case "set":
$len = 30;
// now get the longest set value
$len = GetLongestValue ($type);
break;
case "text":
$len = 60;
break;
case "time":
$len = 11;
break;
case "timestamp":
$len = 22;
break;
default:
$i = strpos ($type, "(");
if ($i > 0) {
$j = strpos ($type, ",");
if ($j == 0) $j = strpos ($type, ")" );
$len = substr ($type, $i + 1, $j - $i - 1);
}
}
return $len;
}
function createPHPFileHeader($fileName) {
$text = <<<END
<?php
/*
* $fileName, part of phpMyEdit - instant MySQL table editor and code generator
*/
END;
return $text;
}
// --------------------------------------------------------------------
function GenerateCode ($self, $hn, $pt, $un, $pw, $db, $tb, $db1, $id)
{
global $buffer;
global $DBbuffer;
global $DefaultsBuffer;
global $HeaderBuffer;
global $pageHeader;
global $pageTitle;
global $contentFile;
global $CSSstylesheet;
global $multifile;
global $HTMLissues;
echo '<h1>Here is your phpMyEdit calling program</h1>'.PHP_EOL;
$css_directive = '';
if ($CSSstylesheet) {
$css_directive = '<link rel="stylesheet" href="css/phpMyEdit.css" />';
}
if ($HTMLissues) {
$HeaderBuffer = <<<END
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>$pageTitle</title>
$css_directive
<script type="text/javascript" src="js/phpMyEdit.js"></script>
</head>
<body>
END;
} else {
$HeaderBuffer = '';
}
if (!$multifile) {
$buffer = $HeaderBuffer;
$HeaderBuffer = '';
} else {
$buffer = '';
$text = <<<END
<?php
include(dirname(__FILE__).'/phpMyEditHeader.php');
?>
END;
build_buffer ($text);
}
if (!$HTMLissues && $CSSstylesheet) {
$buffer = $css_directive.PHP_EOL;
}
if ($pageHeader) {
$buffer .= '<h3>'.$pageTitle.'</h3>'.PHP_EOL;
}
$versions = '';
$versions_ar = get_versions();
foreach (array(
'version' => 'phpMyEdit version:',
'core' => 'lib/phpMyEdit.class.php class:',
'setup' => 'phpMyEditSetup.php script:',
'current' => 'generated script:')
as $type => $desc) {
$version = isset($versions_ar[$type]) ? $versions_ar[$type] : 'unknown';
$versions .= sprintf("\n * %36s %s", $desc, $version);
}
$text = <<<END
<?php
/*
* IMPORTANT NOTE: This generated file contains only a subset of huge amount
* of options that can be used with phpMyEdit. To get information about all
* features offered by phpMyEdit, check the official documentation. It is available
* online and also for download on phpMyEdit project management page:
*
* http://platon.sk/projects/main_page.php?project_id=5
*
* This file was generated by:
*$versions
*/
require_once(dirname(__FILE__).'/lib/phpMyEdit.class.php');
END;
build_buffer ($text);
$text = <<<END
// MySQL host name, port, user name, password, database, and table
\$opts['hn'] = '$hn';
\$opts['pt'] = '$pt';
\$opts['un'] = '$un';
\$opts['pw'] = '$pw';
\$opts['db'] = '$db';
END;
if (!$multifile) {
build_buffer ($text);
$DBbuffer = '';
} else {
$DBbuffer = createPHPFileHeader('lib/phpMyEditDB.php') . $text;
$text = <<<END
require_once(dirname(__FILE__).'/lib/phpMyEditDB.php');
require_once(dirname(__FILE__).'/phpMyEditDefaults.php');
END;
build_buffer ($text);
}
$text = <<<END
\$opts['tb'] = '$tb';
// Name of field which is the unique key
\$opts['key'] = '$id';
// Type of key field (int/real/string/date etc.)
END;
build_buffer ($text);
if ($id == '') {
build_buffer("\$opts['key_type'] = '';");
} else {
$keyType = GetPrimaryKeyType ($self, $hn, $pt, $un, $pw, $db, $tb, $db1);
if ($keyType != "") {
build_buffer("\$opts['key_type'] = '".$keyType."';");
}
}
$text = <<<END
// Sorting field(s)
\$opts['sort_field'] = array('$id');
END;
build_buffer ($text);
$text = <<<END
// Number of records to display on the screen
// Value of -1 lists all records in a table
\$opts['inc'] = 15;
// Options you wish to give the users
// A - add, C - change, P - copy, V - view, D - delete,
// F - filter, I - initial sort suppressed
\$opts['options'] = 'ACPVDF';
// Number of lines to display on multiple selection filters
\$opts['multiple'] = '4';
// Navigation style: B - buttons (default), T - text links, G - graphic links
// Buttons position: U - up, D - down (default)
\$opts['navigation'] = 'DB';
// Display special page elements
\$opts['display'] = array(
'form' => true,
'query' => true,
'sort' => true,
'time' => false,
'tabs' => true
);
// Set default prefixes for variables
\$opts['js']['prefix'] = 'PME_js_';
\$opts['dhtml']['prefix'] = 'PME_dhtml_';
\$opts['cgi']['prefix']['operation'] = 'PME_op_';
\$opts['cgi']['prefix']['sys'] = 'PME_sys_';
\$opts['cgi']['prefix']['data'] = 'PME_data_';
/* Get the user's default language and use it if possible or you can
specify particular one you want to use. Refer to official documentation
for list of available languages. */
\$opts['language'] = \$_SERVER['HTTP_ACCEPT_LANGUAGE'] . '-UTF8';
END;
if (!$multifile) {
build_buffer ($text);
$DefaultsBuffer = '';
} else {
$DefaultsBuffer = createPHPFileHeader('phpMyEditDefaults.php') . $text;
}
$text = <<<END
/* please refer to lib/phpMyEditInfo.php for additional options
that can be added in this file
*/
END;
build_buffer ($text);
$ts_cnt = 0;
$sql = "show columns from $tb in $db";
$result = mysqli_query($db1, $sql);
while ($row = mysqli_fetch_array ($result)) {
$field = $row[0];
$type = $row[1];
$null = $row[2];
$key = $row[3];
$default = $row[4];
$extra = $row[5];
$len = 0;
$fm = $field;
$fn = strtr($field, '_-.', ' ');
$fn = preg_replace('/(^| +)id( +|$)/', '\\1ID\\2', $fn); // uppercase IDs
$fn = ucfirst($fn);
build_buffer( '$opts[\'fdd\'][\'' . $field . '\'] = array(');
build_buffer(" 'name' => '" . str_replace('\'','\\\'',$fn) . "',");
if ((substr($type,0,3) == 'set') or (substr($type,0,4) == 'enum')) {
build_buffer(" 'select' => 'M',");
} else {
build_buffer(" 'select' => 'T',");
}
if ($extra == 'auto_increment') {
build_buffer(" 'options' => 'AVCPDR', // auto increment");
} else if ($type == 'timestamp') {
if ($ts_cnt > 0) {
build_buffer(" 'options' => 'AVCPD',");
} else {
// first timestamp
build_buffer(" 'options' => 'AVCPDR', // updated automatically (MySQL feature)");
}
$ts_cnt++;
}
$length = DecodeType ($type);
$len = $length [1];
if (($type == 'blob') or ($type == 'text')) {
$DoNothing = true;
} else {
build_buffer(" 'maxlen' => $len,");
}
// blobs -> textarea
if (($type == 'blob') or ($type == 'text')) {
build_buffer(" 'textarea' => array(");
build_buffer(" 'rows' => 5,");
build_buffer(" 'cols' => 80),");
}
// SETs and ENUMs get special treatment
if ((substr($type,0,3) == 'set' || substr($type,0,4) == 'enum') && ! (($pos = strpos($type, '(')) === false)) {
$indent = str_repeat(' ', 18);
$outstr = substr($type, $pos + 2, -2);
$outstr = explode("','", $outstr);
$outstr = str_replace("''", "'", $outstr);
$outstr = str_replace('"', '\\"', $outstr);
$outstr = implode('",' . PHP_EOL . $indent . '"', $outstr);
build_buffer(" 'values' => array(".PHP_EOL.$indent.'"'.$outstr.'"),');
}
// automatic support for Default values
if ($default != '' && $default != 'NULL') {
build_buffer(" 'default' => '".$default."',");
} else if ($key == 'PRI') {
// ok if auto increment
// but not if a text type key
build_buffer(" 'default' => '0',");
}
// check for table constraints
$outstr = check_constraints($db, $tb, $field, $db1);
if ($outstr != '') {
build_buffer($outstr);
}
build_buffer(" 'sort' => true");
build_buffer(');');
}
mysqli_free_result ($result);
build_buffer("
// possibly initialise page further before going to main function
if (function_exists('phpMyEditHeaderInit')) { phpMyEditHeaderInit(\$opts); }
// Now important call to phpMyEdit
new phpMyEdit(\$opts);
//eof
");
// write the content include file
echo 'Trying to write content file to: <b>'.$contentFile.'</b><br/>'.PHP_EOL;
$filehandle = @fopen($contentFile, 'w+');
if ($filehandle) {
fwrite($filehandle, $buffer);
ob_flush(); flush();
fclose($filehandle);
echo '<h2>phpMyEdit content file written successfully</h2>';
echo 'You may also copy and paste it into your PHP editor ';
echo 'or <a href="'.$contentFile.'">test it out</a> immediately.<br/>'.PHP_EOL;
echo '<a href="'.$_SERVER['PHP_SELF'].'">Click</a> to continue creation of files.<br/>'.PHP_EOL;
} else {
echo 'phpMyEdit content file was NOT written due to inssufficient privileges.<br/>';
echo '<h2>Please copy and paste content listed below to <i>'.$contentFile.'</i> file.</h2>'.PHP_EOL;
echo '<a href="'.$_SERVER['PHP_SELF'].'">Click</a> to retry creation of files.<br/>'.PHP_EOL;
}
echo '<br/><hr/><pre>';
echo htmlspecialchars($buffer);
echo '</pre><hr/>';
writeBuffer('lib/phpMyEditDB.php', $DBbuffer, 'library');
writeBuffer('phpMyEditDefaults.php', $DefaultsBuffer, 'application');
writeBuffer('phpMyEditHeader.php', $HeaderBuffer, 'application');
}
function writeBuffer($fileName, $theBuffer, $title) {
if (!empty($theBuffer)) {
echo "<h2>This is the $fileName file</h2>";
if (file_exists($fileName)) {
echo "<h2>Already exists</h2>".PHP_EOL;
echo "It has NOT been written your $title folder, please check it's content for possible changes".PHP_EOL;
} else {
$filehandle = @fopen($fileName, 'w+');
if ($filehandle) {
fwrite($filehandle, $theBuffer);
ob_flush(); flush();
fclose($filehandle);
echo "It has been written your $title folder".PHP_EOL;
} else {
echo "<h2>It has not been written, please save it in your $title folder</h2>".PHP_EOL;
}
}
echo '<br/><br/><hr/><pre>'.PHP_EOL;
echo htmlspecialchars($theBuffer);
echo '</pre><hr/>'.PHP_EOL;
}
}
// --------------------------------------------------------------------
// Mainline starts here
// --------------------------------------------------------------------
$phpExtension = '.php';
if (isset($baseFilename) && $baseFilename != '') {
$phpFile = $baseFilename.$phpExtension;
$contentFile = './'.$baseFilename.'.php';
} else if (isset($tb)) {
$phpFile = $tb.$phpExtension;
$contentFile = './'.$tb.'.php';
} else {
$phpFile = 'index'.$phpExtension;
$contentFile = './phpMyEdit-content.php';
}
$self = basename($_SERVER['PHP_SELF']);
$buffer = '';
// now do the main lifting
if (empty($submit)) {
// first time or cancel button
echo "<h1>Please log in to your MySQL database</h1>";
if (! isset($hn)) $hn = 'localhost';
if (! isset($pt)) $pt = '3306';
if (! isset($un)) $un = '';
if (! isset($pw)) $pw = '';
if (! isset($db)) $db = '';
if (! isset($tb)) $tb = '';
ShowLogin ($self, $hn, $pt, $un, $pw, $db, $tb);
} else {
// process the user input
$havehost = true; // assume we have host info
$havedb = false; // no database info
$havetb = false; // no table info
if ($hn == "") // host present?
$haveuost = false;
if ($un == "") // user present?
$havehost = false;
if ($pw == "") // password present?
$havehost = false;
if ($havehost == true) {
/* Connect to a MySQL database */
$host = $hn;
if ($pt != "") $host .= ":$pt";
$db1 = @mysqli_connect($host, $un, $pw);
if ($db1) {
// check for database
if (!empty($db))
$havedb = CheckForDatabase ($self, $db, $db1);
if (!empty($tb))
$havetb = CheckForTable ($self, $db, $tb, $db1);
}
}
if ($havehost == false) {
ShowLogin ($self, $hn, $pt, $un, $pw, $db, $tb);
} else if (!$db1) {
echo '<h2>Sorry - mysql login failed - please try again</h2>' . PHP_EOL;
if (!isset($db)) $db = '';
if (!isset($tb)) $tb = '';
ShowLogin ($self, $hn, $pt, $un, $pw, $db, $tb);
} else if ((! isset($db)) or ($havedb == false)) {
if (isset($db))
echo "<h2>Sorry - unknown database $db - please try again</h2>" . PHP_EOL;
ShowSelectDatabase ($self, $hn, $pt, $un, $pw, $db1);
} else if (!isset($tb)) {
ShowSelectTable ($self, $hn, $pt, $un, $pw, $db, $db1);
} else if ($havetb == false) {
echo "<h2>Sorry - unknown table $tb - please try again</h2><br/>" . PHP_EOL;
ShowSelectTable ($self, $hn, $pt, $un, $pw, $db, $db1);
} else if (!isset($id)) {
ShowSelectId ($self, $hn, $pt, $un, $pw, $db, $tb, $db1);
} else if (!isset($options)) {
ShowSelectOptions ($self, $hn, $pt, $un, $pw, $db, $tb, $db1, $id);
} else {
GenerateCode ($self, $hn, $pt, $un, $pw, $db, $tb, $db1, $id);
}
}
if (isset($db1) && $db1)
mysqli_close ($db1); // close any open connection
?>
</body>
</html>