#!/bin/bash # #-------------------------------------------------------------------------------+ # Copyright (c) 2012-2013 Connect 2 Ltd | #===============================================================================+ # # Script to install vsCMS # #===============================================================================+ #$LastChangedDate:: 2014-12-09 09:05:00 +1300 (Tue, 09 Dec 2014) $| #$LastChangedRevision:: 494 $| #$LastChangedBy:: mlevene $| # ------------------------------------------------------------------------------+ # [ -f /usr/local/bin/functions ] && . /usr/local/bin/functions # # We are assuming that vsCMS has been put into it's own folder under the site home # labelled "vscms" # hereiam=`pwd` hereiam=`basename ${hereiam}` if [ ${hereiam} != "vscms" ] ; then echo "FATAL ERROR: We are expecting to be in our own directory named \"vscms\"" exit 1 fi # # Ok to proceed! # VSCMS_CONFIG=vscmsconfig.php CONFIG=config.php SQL=t.tables.sql DB_PASSWORD=`cat /dev/urandom |tr -cd [:alnum:]|head -c32` ADMIN_PASSWORD=`cat /dev/urandom |tr -cd [:alnum:]|head -c8` WEBSITEURL=`pwd` WEBSITEURL=`dirname ${WEBSITEURL}` WEBSITEURL=`basename ${WEBSITEURL}` TMPPASS=`mktemp` DB_FOUND=1 MYSQL_HOST=192.168.9.10 echo ----------------- echo Looking for mySQL echo ----------------- hostarray=( 1.2.3.4 localhost 192.168.9.10 172.26.1.1 ) for CHECKHOST in "${hostarray[@]}" do retval=`mysql --host ${CHECKHOST} --connect_timeout=2 2>&1 | grep ERROR | cut -d " " -f-2` if [ "${retval}" = "ERROR 1045" ] ; then MYSQL_HOST=${CHECKHOST} echo "Found a host: ${MYSQL_HOST}" break fi done MYSQL="mysql --host=${MYSQL_HOST}" runsql() { # echo Running SQL "$*" retval=`${MYSQL} -u root -p${mysqlpswd} -e "$*"` check_ne $? || exit 1 return ${retval} } echo ----------------- echo Looking for mySQL Password echo ----------------- #MYSQL_PASSWORD=LE2jaEWDJtGeFLr6neyXB6Sk for LOOKIN in `find /sites/*/vscms -name install -exec grep "^MYSQL_PASSWORD" {} \;` do eval ${LOOKIN} sqlcmd="help" ${MYSQL} -u root -p${MYSQL_PASSWORD} -e "${sqlcmd}" 2&>1 > /dev/null if [ $? -eq 0 ] ; then echo Found a password that works: ${MYSQL_PASSWORD} # else # read -sp "Please enter the root mySQL password " mysqlpswd # echo -e "" fi done #MYSQL_PASSWORD=LE2jaEWDJtGeFLr6neyXB6Sk if [ "${MYSQL_PASSWORD}" = "" ] ; then read -sp "Please enter the root mySQL password " mysqlpswd echo -e "" #else # mysqlpswd=${MYSQL_PASSWORD} fi echo ----------------- echo Installing vsCMS for: ${WEBSITEURL} echo ----------------- echo -e "" read -p "Please enter the name of the company (e.g. Acme Importers Ltd) " COMPANYNAME echo -e "" echo The default logo for the Admin screen will be images/adminlogo.png echo -e "" cat << NOTES ------------------------------------------------------------------------------- You will need to include the following code at the top of each PHP page where you need to use vsCMS. You can then use the following function to pull a variable's value into a page: The code will work out what page you are on, and pull in the value assigned to that variable ------------------------------------------------------------------------------- NOTES echo -e "" echo Checking to see if the database we need exists sqlcmd="show databases;" ${MYSQL} -u root -p${mysqlpswd} -e "${sqlcmd}" | grep vscms if [ $? -eq 0 ] ; then DB_FOUND=0 echo The database exists else echo "The database doesn't exist. It will be created" fi if [ ${DB_FOUND} -ne 0 ] ; then # The database has not been found, so make sure the user doesn't exist echo Revoking privileges for User sqlcmd="REVOKE ALL PRIVILEGES, GRANT OPTION FROM 'vscms_user'@'localhost';" ${MYSQL} -u root -p${mysqlpswd} -e "${sqlcmd}" echo Dropping User sqlcmd="DROP USER 'vscms_user'@'localhost';" ${MYSQL} -u root -p${mysqlpswd} -e "${sqlcmd}" cat > ${SQL} << 'SQL_END' -- phpMyAdmin SQL Dump -- version 2.11.11.3 -- http://www.phpmyadmin.net -- -- Host: localhost -- Generation Time: Mar 13, 2012 at 06:17 PM -- Server version: 5.1.61 -- PHP Version: 5.3.3 CREATE DATABASE IF NOT EXISTS `vscms` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; USE `vscms`; SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- Database: `vscms` -- -- -------------------------------------------------------- -- -- Table structure for table `pages` -- CREATE TABLE IF NOT EXISTS `pages` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `locationid` int(10) unsigned NOT NULL, `locationuuid` varchar(255) DEFAULT NULL, `websiteurl` varchar(255) DEFAULT NULL, `pageid` varchar(255) DEFAULT NULL, `variablename` varchar(255) DEFAULT NULL, `contenttype` int(10) unsigned NOT NULL, `content` mediumtext, `createdDate` datetime NOT NULL, `modifiedDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -------------------------------------------------------- -- -- Table structure for table `user` -- CREATE TABLE IF NOT EXISTS `site` ( `id` int(11) NOT NULL AUTO_INCREMENT, `websiteurl` varchar(255) DEFAULT NULL, `locationuuid` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; -- -- Table structure for table `user` -- CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `locationid` int(11) DEFAULT 0, `locationuuid` varchar(255) DEFAULT NULL, `websiteurl` varchar(255) DEFAULT NULL, `username` varchar(255) DEFAULT NULL, `firstname` varchar(255) DEFAULT NULL, `lastname` varchar(255) DEFAULT NULL, `pswrd` varchar(50) DEFAULT NULL, `level` INT(11) DEFAULT 0, `active` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ; SQL_END # Create the tables echo Creating the tables cat ${SQL} | ${MYSQL} -u root -p${mysqlpswd} # Create the database user echo Creating Database User sqlcmd="CREATE USER 'vscms_user'@'localhost' IDENTIFIED BY '${DB_PASSWORD}';" ${MYSQL} -u root -p${mysqlpswd} -e "${sqlcmd}" # Grant the privileges to the database user sqlcmd="GRANT ALL PRIVILEGES ON vscms.* TO 'vscms_user'@'localhost' IDENTIFIED BY '${DB_PASSWORD}' WITH GRANT OPTION;" echo Granting privileges ${MYSQL} -u root -p${mysqlpswd} -e "${sqlcmd}" fi # Create a UUID as the Site Code LOCATIONUUID=`php -r '$uuid=uniqid().md5(microtime(true)*10000);echo substr($uuid,0,8)."-".substr($uuid,8,4)."-".substr($uuid,12,4)."-".substr($uuid,16,4)."-".substr($uuid,20,12);'` sqlcmd="INSERT INTO vscms.site (websiteurl, locationuuid) VALUES (\"${WEBSITEURL}\", \"${LOCATIONUUID}\"); select LAST_INSERT_ID();" LOCATIONID=`${MYSQL} -u root -p${mysqlpswd} -e "${sqlcmd}"|tail -1` if [ ${DB_FOUND} -eq 0 ] ; then echo "Trying to discover existing Database password" echo -e "" rm -rf ${TMPPASS} # Assume we are in SITE_NAME/vscms, therefore we need to go up 2 levels FINDIN=../.. for i in `find ${FINDIN} -iname vscmsconfig.php`; do grep DB_PASSWORD $i; done|sort|uniq|awk '{print $3}'|sed -e 's/^\"//'|sed -e 's/\"$//' > ${TMPPASS} while read DB_PASSWORD do ${MYSQL} -uvscms_user -p${DB_PASSWORD} -e";" 2> /dev/null if [ $? -eq 0 ] ; then echo Found a vaid password break fi done < ${TMPPASS} if [ "${DB_PASSWORD}" = "" ] ; then echo "We could not determine the Database password" echo You will need to manually update \'${VSCMS_CONFIG}\' with the database password echo The variable is called \'DB_PASSWORD\' echo -e "" DB_PASSWORD="" else echo "Successfully retrieved the database password" fi fi rm -rf ${TMPPASS} rm -rf ${VSCMS_CONFIG} cat > ${VSCMS_CONFIG} << VSCMSCONFIG_END_1 * within the tag of every page which needs to access the database * * To pull a variable's value from the database, use the p_var(VARIABLE_NAME) function * * */ /* Set up the system-wide variables */ if ( php_sapi_name() !== 'cli' ) { if ( version_compare(phpversion(), '5.4.0', '>=') ) { if (session_status() !== PHP_SESSION_ACTIVE) {session_start();} } else { if (session_id() === '') { session_start(); } } } ini_set( "display_errors", true ); // define( "CLASS_PATH", "classes" ); // define( "TEMPLATE_PATH", "templates" ); // define( "UPLOAD_PATH", "upload" ); // Image upload path define( "HOMEPAGE_NUM_VARIABLES", 5 ); // define( "ADMIN_PERM", 1 ); // Security bit-wise level to denote ADMINISTRATOR privileges define( "ADD_PERM", 2 ); // Security bit-wise level to allow adding define( "DELETE_PERM", 4 ); // Security bit-wise level to allow deleting define( "EDIT_PERM", 8 ); // Security bit-wise level to allow editing define( "USER_PERM", 16 ); // Security bit-wise level to allow editing define( "CONTENT_HTML", 1); // Content types define( "CONTENT_IMAGE", 2); // define( "CONTENT_HYPERLINK", 3); // define( "CONTENT_PDF", 4); // define( "CONTENT_TEXT", 8); // define( "CONTENT_OTHER", 9); // define( "MAX_IDLE_TIME", 30 * 60); // Idle time before the session expires (minutes * 60 = seconds) define( "SHOW_VAR", False); // Future development - will show the variable positions on-screen define( "LOCATIONID", ${LOCATIONID}); define( "LOCATIONUUID", "${LOCATIONUUID}"); define( "SITENAME", "${WEBSITEURL}"); define( "DB_DSN", "mysql:host=${MYSQL_HOST};dbname=vscms" ); define( "DB_USERNAME", "vscms_user" ); // define( "DB_PASSWORD", "${DB_PASSWORD}" ); require('vscmsboot.php'); ?> VSCMSCONFIG_END_1 cat > ${CONFIG} << CONFIG_END CONFIG_END echo Creating Admin logo field sqlcmd="INSERT INTO vscms.pages (locationid, locationuuid, websiteurl, pageid, variablename, contenttype, content) VALUES (${LOCATIONID}, '${LOCATIONUUID}','${WEBSITEURL}', 'admin', 'logo', 8, 'images/adminlogo.png');" runsql ${sqlcmd} echo Creating Admin user sqlcmd="USE vscms;INSERT INTO user (locationid, locationuuid, websiteurl, username, firstname, lastname, pswrd, level, active) VALUES (${LOCATIONID}, '${LOCATIONUUID}', '${WEBSITEURL}', 'admin', 'Administrator', '', '', 0xFFFFFFFF, 1);" runsql ${sqlcmd} echo Setting Administrator password sqlcmd="USE vscms; UPDATE user SET pswrd=PASSWORD('${ADMIN_PASSWORD}') WHERE locationuuid='${LOCATIONUUID}' and username='admin';" runsql ${sqlcmd} echo "You can login using the following password '${ADMIN_PASSWORD}'" echo ${ADMIN_PASSWORD} > admin.passwd echo "The password is stored in a file (admin.passwd) which you must remember to delete" # # Tidy up # if [ ${DB_FOUND} -ne 0 ] ; then rm ${SQL} fi # And now tidy up anything that didn't rely on there being no database