Location: PHPKode > scripts > Impleo Music Collection Script > impleo/install.php
<?php
/* ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <hide@address.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return Christoffer Kjeldgaard Petersen
 * ----------------------------------------------------------------------------
 */

header("content-type:text/html;charset=utf-8");

require_once(dirname(__FILE__) . '/classes/Database.class.php');

$db = Database::getInstance();
$tableStart = Database::getTableStart();

$arrTables = array("detail_types", "image", "info_value", "music_item", "settings", "user");
foreach ($arrTables as $table)
{
    $sqlShow = "SHOW TABLES like '{$tableStart}{$table}'";
    if ($db->show($sqlShow))
    {
        echo "You appear to have already installed Impleo Music Collection. Log in to the admin tool and start adding records to your list :-)";
        exit;
    }
}

function createRandomPassword()
{
    $chars = "abcdefghijkmnopqrstuvwxyz023456789";
    srand((double)microtime()*1000000);
    $i = 0;
    $pass = '' ;
    while ($i <= 7)
	{
        $num = rand() % 33;
        $tmp = substr($chars, $num, 1);
        $pass = $pass . $tmp;
        $i++;
    }
    return $pass;
}

if ($_POST['submit'])
{
    if (ereg("[A-z0-9]+", $_POST['name']))
        $collectionName = $db->escapeString(htmlspecialchars($_POST['name']));
	else
		$collectionName = "Music collection";

    $username = "admin";
	$password = createRandomPassword();
    $md5Password = md5($password);

    $sql1 = "CREATE TABLE IF NOT EXISTS `{$tableStart}detail_types` (
  `name` varchar(50) collate utf8_unicode_ci NOT NULL,
  `key` varchar(50) collate utf8_unicode_ci NOT NULL,
  `required` tinyint(1) NOT NULL default '1',
  `show_in_table` tinyint(1) NOT NULL default '1',
  `order` tinyint(3) unsigned NOT NULL,
  PRIMARY KEY  (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
    $db->query($sql1);

    $sql2 = "CREATE TABLE IF NOT EXISTS `{$tableStart}image` (
  `id` int(11) NOT NULL,
  `img_name` varchar(255) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
    $db->query($sql2);

    $sql3 = "CREATE TABLE IF NOT EXISTS `{$tableStart}info_value` (
  `item` int(11) NOT NULL,
  `detail` varchar(50) collate utf8_unicode_ci NOT NULL,
  `value` varchar(255) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`item`,`detail`),
  KEY `detail` (`detail`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
    $db->query($sql3);

    $sql4 = "CREATE TABLE IF NOT EXISTS `{$tableStart}music_item` (
  `id` int(11) NOT NULL auto_increment,
  `artist` varchar(255) collate utf8_unicode_ci NOT NULL,
  `title` varchar(255) collate utf8_unicode_ci NOT NULL,
  `year` smallint(5) unsigned NOT NULL,
  `date_added` datetime NOT NULL,
  `date_updated` datetime default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;";
    $db->query($sql4);

    $sql5 = "CREATE TABLE IF NOT EXISTS `{$tableStart}settings` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) collate utf8_unicode_ci NOT NULL,
  `records_per_page` smallint(6) NOT NULL,
  `link` tinyint(1) NOT NULL,
  `image_width` smallint(6) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;";
    $db->query($sql5);

    $sql6 = "CREATE TABLE IF NOT EXISTS `{$tableStart}user` (
  `username` varchar(100) collate utf8_unicode_ci NOT NULL,
  `password` varchar(100) collate utf8_unicode_ci NOT NULL,
  PRIMARY KEY  (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;";
    $db->query($sql6);

    $sql7 = "INSERT INTO `{$tableStart}detail_types` (`name`, `key`, `required`, `show_in_table`, `order`) VALUES
('Comment', 'comment', 0, 0, 3),
('Format', 'format', 1, 1, 1),
('Label', 'label', 1, 1, 2);";
    $db->query($sql7);

    $sql8 = "INSERT INTO `{$tableStart}settings` (`id`, `name`, `records_per_page`, `link`, `image_width`) VALUES
(1, '{$collectionName}', 25, 1, 250);";
    $db->query($sql8);

    $sql9 = "INSERT INTO `{$tableStart}user` (`username`, `password`) VALUES
('{$username}', '{$md5Password}');";
    $db->query($sql9);

    $sql10 = "ALTER TABLE `{$tableStart}image`
  ADD CONSTRAINT `{$tableStart}image_ibfk_1` FOREIGN KEY (`id`) REFERENCES `{$tableStart}music_item` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;";
    $db->query($sql10);

    $sql11 = "ALTER TABLE `{$tableStart}info_value`
  ADD CONSTRAINT `{$tableStart}info_value_ibfk_1` FOREIGN KEY (`item`) REFERENCES `{$tableStart}music_item` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
  ADD CONSTRAINT `{$tableStart}info_value_ibfk_2` FOREIGN KEY (`detail`) REFERENCES `{$tableStart}detail_types` (`name`) ON UPDATE CASCADE ON DELETE CASCADE;";
    $db->query($sql11);

    $sqlIndex = "ALTER TABLE `{$tableStart}music_item` ADD INDEX `artist_year` ( `artist` , `year` )";
    $db->query($sqlIndex);

    $posted = true;

}
else
{
    $posted = false;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
  <title>Install Impleo Music Collection</title>
</head>
<body>
<h1>Install Impleo Music Collection</h1>
<?php if ($posted) { ?>
  <h2>Step 2</h2>
  <p><b>Installation finished!</b> You can now use Impleo Music Collection.</p>
  <p>Log in to <a href="./admin/">the admin tool</a> with these user details and start adding records to your list. You can always change the password in the admin tool.</p>
  <dl>
    <dt>Username:</dt>
    <dd><?php echo $username; ?></dd>
    <dt>Password:</dt>
    <dd><?php echo $password; ?></dd>
  </dl>
<?php } else { ?>
  <h2>Step 1</h2>
  <p>This guide installs Impleo Music Collection. Write a name for your collection. Don't worry, you can always change it again later.</p>
  <form method="post" action="install.php">
    <p>
      <label for="name">Name of collection:</label><br>
      <input name="name" type="text" id="name" size="25">
    </p>
    <p><input type="submit" name="submit" value="Continue to Step 2"></p>
  </form>
<?php }?>
</body>
</html>
Return current item: Impleo Music Collection Script