* @package WB
* @subpackage setup
*/
/**
* make wombat site
*
* @author gERD Schaufelberger
* @package WB
* @subpackage setup
*/
class MKWombat
{
const SEVERITY_MSG = "msg";
const SEVERITY_WARN = "warning";
const SEVERITY_ERROR = "error";
/**
* location of base dir
* @var string
*/
private $base = '';
/**
* location of system dir
* @var string
*/
private $system = '';
/**
* session data
* @var array
*/
private $session = array();
/**
* post data
* @var array
*/
private $post = array(
'action' => 'welcome'
);
/**
* list of messages
* @var array
*/
private $msg = array();
public function __construct()
{
$this->system = realpath(dirname(__FILE__));
$this->base = '';
session_start();
error_reporting( E_ALL | E_STRICT );
$this->post = array_merge( $this->post, $_REQUEST);
$this->session =& $_SESSION;
$this->self = $_SERVER['PHP_SELF'] . '?' . SID;
}
public function run()
{
$this->setBaseDir();
$this->displayMenu($this->post['action']);
$this->displayAction($this->post['action']);
}
private function displayAction($action)
{
$m = ucfirst(strtolower($action));
if (empty($m) || !method_exists($this, 'display'. $m)) {
$m = 'Welcome';
}
call_user_func(array($this, 'display' . $m));
}
private function displayWelcome()
{
$self = $this->self;
echo <<Welcome
This tool helps setting up a new web site based on the Wombat framework.
EOF;
if (!isset($this->post['save']) && empty($this->post['save'])) {
echo <<
Flush session data to start from scratch.
EOF;
$this->printMsg();
return;
}
$this->session = array();
$this->addMsg('Restarted');
$this->printMsg();
}
private function displayMkVFS()
{
$base = $this->base;
if (empty($this->base)) {
$this->printMsg();
return;
}
$self = $this->self;
if (!isset($this->post['save']) && empty($this->post['save'])) {
echo <<CMS: Virtual File System (VFS)
In order to insert media files like photos, videos and PDF-documents
into any CMS-page, those file need to be hosted on the web-server. VFS
manages those files, transcode them to other formats and keeps them in order.
All files to use WXML CMS are installed. Use "Wxml" as
processor in page definitions or simply use patTemplate:wxml-tag in
any template file.
EOF;
}
private function displayActivateWebmaster()
{
$base = $this->base;
if (empty($this->base)) {
$this->printMsg();
return;
}
$activate = false;
if (isset($this->post['webmaster']['webmasteractivate']) && !empty($this->post['webmaster']['webmasteractivate'])) {
$activate = true;
}
if (!isset($this->post['save']) && empty($this->post['save'])) {
$activate = false;
}
if ($activate) {
if (empty($this->post['webmaster']['password'])) {
$this->addMsg('Password must not be empty', self::SEVERITY_WARN);
$activate = false;
unset($this->post['save']);
}
else if ($this->post['webmaster']['password'] != $this->post['webmaster']['passwordretype']) {
$this->addMsg('Password does not match retype', self::SEVERITY_WARN);
$activate = false;
unset($this->post['save']);
}
else if (4 > strlen($this->post['webmaster']['password'])) {
$this->addMsg('Password must be at least 4 characters long', self::SEVERITY_WARN);
$activate = false;
unset($this->post['save']);
}
}
if (!isset($this->post['save']) && empty($this->post['save'])) {
$this->printMsg();
echo <<Activate Webmaster Account
Optional: The user "webmaster" is a member of the most important groups and therefore
a good start and allows you to manage the site before you set up your personal user account.
Please deactivate or remove the webmaster's account after your personal account is set up properly.
EOF;
return;
}
if (!$this->connectDb()) {
return;
}
if (!$activate) {
$this->printMsg();
echo <<Activate Webmaster Account
Open BASEDIR/etc-default/config.xml and setup server name and document root
$xml
Those values tell Wombat where the web site is located. Also this allows to setup
different instances of the web page on different environments
(e.g. one live system and one development site on your local machine).
EOF;
}
private function displayMkbasedir()
{
$base = $this->base;
$self = $this->self;
if (empty($this->base)) {
$base = realpath(dirname(__FILE__) . '/../');
$this->printMsg();
echo <<Create and Populate BASEDIR
Create all basic files and folders in BASEDIR to use it as Wombat-based
web-site. A proper set up BASEDIR is essential for any Wombat site.
EOF;
return;
}
echo <<BASEDIR Created and Populated
Please check ownership and permissions of your files in BASEDIR.
It is recommended to set all files to read-only.
Still, the web-server needs write access to "var" and
subfolders. Hence it's probably a good idea to change owner and
permissions:
EOF;
// etc/wbLoader.php
if (file_exists($base . '/etc/wbLoader.php')) {
$this->addMsg('Create wbLoader Helper skipped, file exists', self::SEVERITY_WARN);
}
else {
$this->addMsg('Create wbLoader Helper');
$file = array('system . '\';';
$file[] = '?>';
$file[] = '';
$file = implode("\n", $file);
file_put_contents($base . '/etc/wbLoader.php', $file);
}
// etc/config.php
if (file_exists($base . '/etc/config.php')) {
$this->addMsg('Create optional config.php skipped, file exists', self::SEVERITY_WARN);
}
else {
$this->addMsg('Create optional config.php');
$file = array('';
$file[] = '';
$file = implode("\n", $file);
file_put_contents($base . '/etc/config.php', $file);
}
$this->printMsg();
}
private function setBaseDir()
{
if ('mkbasedir' == $this->post['action']) {
//$this->session['basedir'] = '';
$this->mkBaseDir();
}
if (!isset($this->session['basedir']) || empty($this->session['basedir'])) {
if ('welcome' != $this->post['action']) {
$this->post['action'] = 'mkbasedir';
}
return;
}
$this->base = $this->session['basedir'];
if (!is_dir($this->base) || !is_writable($this->base)) {
$this->addMsg(
'BASEDIR does not exist or is not writable'
, self::SEVERITY_ERROR);
}
}
private function mkBaseDir()
{
$base = realpath( $this->system . '/..');
if (!isset($this->post['basedir']) || empty($this->post['basedir'])) {
$this->post['basedir'] = $base;
return;
}
if (rtrim($this->post['basedir'], '/') == $base) {
$this->addMsg('BASEDIR must not be the parent folder of Wombat\s SYSTEMDIR', self::SEVERITY_ERROR);
return;
}
if ($this->post['basedir'] == $this->system) {
$this->addMsg(
'SYSTEMDIR and BASEDIR must not be the same! The folder that holds the
Wombat framework must not be your BASEDIR. That way, you never have to
touch anything inside SYSTEMDIR and you may reuse SYSTEMDIR for further
web-sites installed in other BASEDIRs.'
, self::SEVERITY_ERROR);
return;
}
// create basedir
if (!is_dir($this->post['basedir'])) {
if (!@mkdir($this->post['basedir'], 0777, true)) {
$this->addMsg(
'Could not create BASEDIR. Maybe there is a problem regarding ownership or permissions.'
, self::SEVERITY_ERROR);
return;
}
}
// writeable
if (!is_writable($this->post['basedir'])) {
$this->addMsg(
'Folder BASEDIR exists, but is not writeable.'
, self::SEVERITY_ERROR);
return;
}
$this->base = realpath($this->post['basedir']);
$this->session['basedir'] = $this->base;
$folders = array(
'etc',
'etc-default',
'etc-default/event',
'etc-default/site',
'etc-default/site/page',
'include',
'include/WB',
'include/patEx',
'htdoc',
'htdoc/s',
'resource',
'resource/css',
'resource/js',
'template',
'template/Composite',
'template/Static',
'template/Mailer',
'var',
'var/log',
'var/cache',
'var/tmp'
);
$this->mkDir($folders);
$files = array(
'wbLoader.php',
'etc-default/config.xml',
'etc-default/site.xml',
'etc-default/site/content.xml',
'etc-default/site/page/__index.xml',
'etc-default/site/page/sitemap.xml',
'etc-default/site/page/robots.txt.xml',
'etc-default/site/page/blank.xml',
'htdoc/dispatcher.php'
);
$this->copy($files);
}
private function addMsg($msg, $severity = self::SEVERITY_MSG)
{
$this->msg[] = array($msg, $severity);
}
/**
* create folders
*
*
* @param array|string $folders
*/
private function mkDir($folders = array())
{
if (!is_array($folders)) {
$folders = array($folders);
}
foreach ($folders as $f) {
if (is_dir($this->base . '/' . $f)) {
continue;
}
mkdir($this->base . '/' . $f);
chmod($this->base . '/' . $f, 0777);
$this->addMsg('Created folder ' . $f, self::SEVERITY_MSG);
}
}
/**
* copy bunch of files
*
*
* @param array|string $files
*/
private function copy($files = array(), $des = null)
{
if (!is_array($files)) {
$files = array($files);
}
foreach ($files as $f) {
if (file_exists($this->base . '/' . $f)) {
continue;
}
if ($des) {
copy($this->system . '/' . $f, $this->base . '/' . $des);
chmod($this->base . '/' . $des, 0666);
} else {
copy($this->system . '/' . $f, $this->base . '/' . $f);
chmod($this->base . '/' . $f, 0666);
}
$this->addMsg('Copied ' . $f, self::SEVERITY_MSG);
}
}
private function connectDb()
{
$dbdriver = $this->session['db']['driver'];
$dbhost = $this->session['db']['host'];
$dbdb = $this->session['db']['db'];
$dbuser = $this->session['db']['user'];
$dbpassword = $this->session['db']['password'];
$this->dbc = @new mysqli($dbhost, $dbuser, $dbpassword, $dbdb);
if (!$this->dbc->connect_error) {
return true;
}
$this->addMsg('Error connection to database: ' . $this->dbc->connect_error, self::SEVERITY_ERROR);
$this->printMsg();
echo <<
You probably want to create a new database and/or user for you new wombat site.
CREATE DATABASE `$dbdb` DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
GRANT USAGE ON *.* TO `$dbuser`@`$dbhost`;
SET PASSWORD FOR `$dbuser`@`$dbhost` = password("$dbpassword");
GRANT ALL PRIVILEGES ON `$dbdb`.* TO `$dbuser`@`$dbhost`;