File: install.php

Recommend this page to a friend!
  Classes of Martin Latter  >  PHP Secure Chat  >  install.php  >  Download  
File: install.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Secure Chat
Chat box between users using encrypted messages
Author: By
Last change: revise install.php
- allow for existence of previously-created CChat user
- default password: slightly increase complexity to avoid MySQL complaints
- MySQL 8: bypass default password module authentication requirements
Date: 7 months ago
Size: 5,353 bytes
 

Contents

Class file image Download
<?php

/**
    * Simple install script to set-up the MySQL database and database user for CChat.
    * Edit the constants in the configuration section below, then load this file via a web server
    * (or via the command-line: php install.php).
    *
    * @author Martin Latter
    * @copyright 29/06/2014
    * @version 0.06
    * @license GNU GPL version 3.0 (GPL v3); http://www.gnu.org/licenses/gpl.html
    * @link https://github.com/Tinram/CChat.git
*/


/* CONFIGURATION */

define('APP_USERNAME', 'messenger');
define('APP_PASSWORD', 'P@55w0rd');
define('APP_NAME', 'CChat');

define('SUPER_USER', 'root');
define('SUPER_USER_PASSWORD', '**root_password**');

define('HOST', 'localhost');
define('DATABASE', 'cchat');
define('TABLE', 'chatbox');

define('CHARSET', 'latin1');
define('COLLATION', 'latin1_general_ci');

/* END CONFIGURATION */


# command-line or server line break output
define('LINE_BREAK', (PHP_SAPI !== 'cli') ? '<br>' : "\n");


$oConnection = new mysqli(HOST, SUPER_USER, SUPER_USER_PASSWORD);

if (
$oConnection->connect_errno) {
    die(
'Database connection failed: ' . $oConnection->connect_errno . ') ' . $oConnection->connect_error . LINE_BREAK);
}
else {

   
$sTitle = APP_NAME . ' Database Setup';

    if (
PHP_SAPI !== 'cli') {
        echo
'<h2>' . $sTitle . '</h2>';
    }
    else {
        echo
$sTitle . LINE_BREAK . LINE_BREAK;
    }

   
# create database
   
$sQuery = 'CREATE DATABASE IF NOT EXISTS ' . DATABASE . ' CHARACTER SET ' . CHARSET . ' COLLATE ' . COLLATION;
   
$rResults = $oConnection->query($sQuery);

    if (
$rResults) {
        echo
'Created database ' . DATABASE . '.' . LINE_BREAK;
    }
    else {
        die(
'ERROR: could not create the ' . DATABASE . ' database.' . LINE_BREAK);
    }

   
# select database
   
$sQuery = 'USE ' . DATABASE;
   
$rResults = $oConnection->query($sQuery);

   
# create table
   
$sQuery = '
        CREATE TABLE IF NOT EXISTS `'
. TABLE . '` (
            `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
            `name` CHAR(15) NOT NULL,
            `message` VARCHAR(384) NOT NULL,
            `date` INT(10) UNSIGNED NOT NULL,
            KEY `kdate` (`date`),
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET='
. CHARSET;

   
$rResults = $oConnection->query($sQuery);

    if (
$rResults) {
        echo
'Created table ' . TABLE . '.' . LINE_BREAK;
    }
    else {
        die(
'ERROR: could not create the ' . TABLE . ' table.' . LINE_BREAK);
    }

   
# create first (unencrypted) test message
   
$sQuery = 'INSERT INTO ' . DATABASE . '.' . TABLE . ' (name, message, date) VALUES("init", "test", UNIX_TIMESTAMP())';
   
$rResults = $oConnection->query($sQuery);

    if (
$rResults) {
        echo
'Created first ' . APP_NAME . ' message.' . LINE_BREAK;
    }
    else {
        die(
'ERROR: could not create the first message.' . LINE_BREAK);
    }

   
# create CChat user, avoiding errors if user was previously created
   
$sQuery = 'CREATE USER IF NOT EXISTS ' . APP_USERNAME . '@localhost';
   
$rResults = $oConnection->query($sQuery);

    if (
$rResults) {

        echo
'Created ' . APP_NAME . ' database user (' . APP_USERNAME . ').' . LINE_BREAK;

       
# create CChat user password
       
$sQuery = 'SET PASSWORD FOR ' . APP_USERNAME . '@localhost = ' . '"' . APP_PASSWORD . '"';
       
$rResults = $oConnection->query($sQuery);

        if (
$rResults) {
            echo
'Created ' . APP_NAME . ' database user password.' . LINE_BREAK;
        }
        else {
            die(
'ERROR: could not create database user password (check password complexity requirements).' . LINE_BREAK);
        }

       
# create CChat user permissions
       
$sQuery = 'GRANT SELECT, INSERT ON ' . DATABASE . '.* TO ' . APP_USERNAME . '@localhost';
       
$rResults = $oConnection->query($sQuery);

        if (
$rResults) {
            echo
'Created database user permissions.' . LINE_BREAK;
        }
        else {
            die(
'ERROR: could not create ' . APP_NAME . ' database user (' . APP_USERNAME . ') permissions.' . LINE_BREAK);
        }
    }
    else {

        echo
'Could not create ' . APP_NAME . ' database user (' . APP_USERNAME . ') with method 1' . LINE_BREAK;
        echo
'trying method 2 ...' . LINE_BREAK;

       
# for password requirements blocking
       
$sQuery = 'GRANT SELECT, INSERT ON ' . DATABASE . '.* TO ' . APP_USERNAME . '@localhost IDENTIFIED BY "' . APP_PASSWORD . '"';
       
$rResults = $oConnection->query($sQuery);

        if (
$rResults) {
            echo
'Created ' . APP_NAME . ' database user (' . APP_USERNAME . ') and permissions.' . LINE_BREAK;
        }
        else {
            die(
'ERROR: could not create the ' . APP_NAME . ' database user.' . LINE_BREAK);
        }
    }

   
# circumvent MySQL 8's sha256_password default authentication
    # this is just to get CChat operational on MySQL 8, and is best replaced with caching_sha2_password authentication ASAP
   
$sQuery = 'SELECT VERSION()';
   
$rResults = $oConnection->query($sQuery);
   
$aVersion = $rResults->fetch_row()[0];

    if (
substr($aVersion, 0, 1) === '8') {

       
$sQuery = 'ALTER USER ' . APP_USERNAME . '@localhost IDENTIFIED WITH mysql_native_password BY "' . APP_PASSWORD . '"';
       
$rResults = $oConnection->query($sQuery);
        if (
$rResults) {
            echo
'Bypassed MySQL 8 sha256_password authentication.' . LINE_BREAK;
        }
        else {
            die(
'ERROR: could not bypass MySQL 8 sha256_password authentication.' . LINE_BREAK);
        }
    }

   
# flush
   
$sQuery = 'FLUSH PRIVILEGES';
   
$rResults = $oConnection->query($sQuery);

   
# if run in browser, display link to CChat
   
if (PHP_SAPI !== 'cli') {
        echo
LINE_BREAK . '<a href="./index.php">' . APP_NAME . '</a>';
    }

   
# close connection
   
$oConnection->close();
}

?>


For more information send a message to info at phpclasses dot org.