Skip to content

Instantly share code, notes, and snippets.

@jbroadway
Created May 24, 2012 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbroadway/2783140 to your computer and use it in GitHub Desktop.
Save jbroadway/2783140 to your computer and use it in GitHub Desktop.
Elefant demo site setup

The demo site is controlleed by two handlers in the apps/demo app:

  • demo/index - Creates a new user when you visit /demo and logs them in
  • demo/reset - Resets the database and files via cron

The files that are modified are:

  • apps/demo - Demo handlers
  • apps/admin/css/admin.css - Added styles for demo reset notice
  • css/mobile.css - Added styles for demo reset notice
  • css/style.css - Added styles for demo reset notice
  • layouts/admin.html - Added code for demo reset notice
  • layouts/default.html - Added code for demo reset notice
  • layouts/index.html - Added code for demo reset notice

In addition, the css, files and layouts folders are duplicated as css2, files2, and layouts2, as well as conf/navigation.json to conf/navigation2.json. These are used to return those folders to their default state on each reset.

And the cron entry looks like this:

0 * * * * cd /path/to/demo/site; php index.php demo/reset
<?php
// apps/demo/handlers/index.php
if (User::require_admin ()) {
$this->redirect ('/');
}
// returns random uppercase letters
function az ($len = 1) {
$o = '';
for ($i = 0; $i < $len; $i++) {
$o .= chr (rand (65, 90));
}
return $o;
}
// create new admin user with email/password like
// xpasdfov@demo.elefantcms.com:demo
$date = gmdate ('Y-m-d H:i:s');
$session_id = md5 (uniqid (mt_rand (), 1));
if (! db_execute (
'insert into user (id, email, password, session_id, expires, name, type, signed_up, updated, userdata) values (null, ?, ?, ?, ?, ?, "admin", ?, ?, ?)',
strtolower (az (8)) . '@demo.elefantcms.com',
User::encrypt_pass ('demo'),
$session_id,
gmdate ('Y-m-d H:i:s', time () + 2592000),
'Demo Admin ' . az (),
$date,
$date,
json_encode (array ())
)) {
$page->title = 'Unknown Error';
echo '<p>Please try again later.</p>';
return;
}
// log them in automatically
@session_start ();
$_SESSION['session_id'] = $session_id;
$this->redirect ('/');
?>
<?php
// apps/demo/handlers/reset.php
// add this to cron:
// 0 * * * * cd /path/to/site; php index.php demo/reset
$page->template = false;
if (! $this->cli) {
die ('Must be run from cli');
}
$sqldata = sql_split (file_get_contents ('conf/install_sqlite.sql'));
// drop all tables and recreate all data
foreach ($sqldata as $sql) {
if (preg_match ('/^create table ([`a-z0-9_]+)/i', $sql, $regs)) {
db_execute ('drop table ' . $regs[1]);
}
if (! db_execute ($sql)) {
echo 'Error: ' . db_error () . "\n";
}
}
// create an initial admin user
$date = gmdate ('Y-m-d H:i:s');
if (! db_execute ('insert into user (id, email, password, session_id, expires, name, type, signed_up, updated, userdata) values (1, ?, ?, null, ?, "Admin User", "admin", ?, ?, ?)',
'you@example.com',
User::encrypt_pass ('test'),
$date,
$date,
$date,
json_encode (array ())
)) {
echo 'Error: ' . db_error () . "\n";
}
// initialize versions of default content
$wp = new Webpage ('index');
Versions::add ($wp);
$b = new Block ('members');
Versions::add ($b);
// reset potentially modified files
system ('rm -Rf css/* files/* layouts/*');
system ('cp -R css2/* css/');
system ('cp -R files2/* files/');
system ('cp -R layouts2/* layouts/');
system ('cp -R conf/navigation2.json conf/navigation.json');
system ('chmod -R 777 css files layouts conf/navigation.json');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment