Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
change session management to use mysql, added simple benchmarking, ad…
Browse files Browse the repository at this point in the history
…ded helper functions for benchmarking and page types
  • Loading branch information
rwarasaurus committed Feb 14, 2012
1 parent b3d6d5b commit 8f9988a
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 15 deletions.
26 changes: 26 additions & 0 deletions index.php
Expand Up @@ -6,6 +6,16 @@
* Originally built by @visualidiot, with thanks to @kieronwilson, @spenserj and a bunch of other contributors.
* You're all great.
*/

/*
* XH Profiling
*/
if(function_exists('xhprof_enable')) {
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
}

// benchmark
define('ANCHOR_START', microtime(true));

// Set the include path
define('PATH', pathinfo(__FILE__, PATHINFO_DIRNAME) . '/');
Expand All @@ -18,3 +28,19 @@

// Lets bootstrap our application and get it ready to run
require PATH . 'system/bootstrap.php';

/*
* XH Profiling
*/
if(function_exists('xhprof_enable')) {
$xhprof_data = xhprof_disable();

$path = '../../xh/xhprof_lib/';

include $path . 'config.php';
include $path . 'utils/xhprof_lib.php';
include $path . 'utils/xhprof_runs.php';

$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
}
25 changes: 17 additions & 8 deletions install/anchor.sql
Expand Up @@ -22,6 +22,8 @@ CREATE TABLE `meta` (
PRIMARY KEY (`key`)
) ENGINE=MyISAM CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `meta` (`key`, `value`) VALUES ('posts_page', '1'), ('home_page', '1'), ('twitter', ''), ('date_format', 'jS M, Y');

DROP TABLE IF EXISTS `pages`;

CREATE TABLE `pages` (
Expand All @@ -36,6 +38,10 @@ CREATE TABLE `pages` (
KEY `slug` (`slug`)
) ENGINE=MyISAM CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `pages` (`slug`, `name`, `title`, `content`, `status`) VALUES
('posts', 'Posts', 'My posts and thoughts', '<p>Welcome!</p>', 'published'),
('about', 'About', 'A little bit about me', '<p>This is a little bit of text about me.</p>', 'published');

DROP TABLE IF EXISTS `posts`;

CREATE TABLE `posts` (
Expand All @@ -56,6 +62,9 @@ CREATE TABLE `posts` (
KEY `slug` (`slug`)
) ENGINE=MyISAM CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `posts` (`title`, `slug`, `description`, `html`, `css`, `js`, `created`, `author`, `status`) VALUES
('Hello World', 'hello', 'Hello World.', '<p>My first post.</p>', '', '', '[[now]]', 1, 'published');

DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (
Expand All @@ -70,15 +79,15 @@ CREATE TABLE `users` (
PRIMARY KEY (`id`)
) ENGINE=MyISAM CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `pages` (`slug`, `name`, `title`, `content`, `status`) VALUES
('posts', 'Posts', 'My posts and thoughts', '<p>Welcome!</p>', 'published'),
('about', 'About', 'A little bit about me', '<p>This is a little bit of text about me.</p>', 'published');

INSERT INTO `posts` (`title`, `slug`, `description`, `html`, `css`, `js`, `created`, `author`, `status`) VALUES
('Hello World', 'hello', 'Hello World.', '<p>My first post.</p>', '', '', '[[now]]', 1, 'published');

INSERT INTO `users` (`username`, `password`, `email`, `real_name`, `bio`, `status`, `role`) VALUES
('admin', '[[password]]', '[[email]]', 'Administrator', 'Default account for Anchor.', 'active', 'administrator');

INSERT INTO `meta` (`key`, `value`) VALUES ('posts_page', '1'), ('home_page', '1'), ('twitter', ''), ('date_format', 'jS M, Y');
DROP TABLE IF EXISTS `sessions`;

CREATE TABLE IF NOT EXISTS `sessions` (
`id` CHAR( 32 ) NOT NULL ,
`date` DATETIME NOT NULL ,
`ip` VARCHAR( 15 ) NOT NULL ,
`ua` TEXT NOT NULL ,
`data` TEXT NOT NULL
) ENGINE = InnoDB CHARSET=utf8 COLLATE=utf8_general_ci;
23 changes: 23 additions & 0 deletions system/classes/cookie.php
@@ -0,0 +1,23 @@
<?php defined('IN_CMS') or die('No direct access allowed.');

class Cookie {

public static function has($key) {
return isset($_COOKIE[$key]);
}

public static function get($key, $default = false) {
if(static::has($key)) {
return $_COOKIE[$key];
}

return ($default instanceof \Closure) ? call_user_func($default) : $default;
}

public static function write($name, $data, $expire, $path, $domain) {
if(headers_sent() === false) {
setcookie($name, $data, $expire, $path, $domain, false);
}
}

}
74 changes: 68 additions & 6 deletions system/classes/session.php
Expand Up @@ -9,25 +9,87 @@

class Session {

private static $id, $data = array();

private static function generate($length = 32) {
$pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1);
$value = '';

for ($i = 0; $i < $length; $i++) {
$value .= $pool[mt_rand(0, 61)];
}

return $value;
}

private static function gc() {
$sql = 'delete from sessions where date < ?';
$expire = time() - Config::get('session.expire', 86400);

Db::query($sql, array(date(DATE_ISO8601, $expire)));
}

public static function start() {
session_start();
// run gc
static::gc();

// get session id
$name = Config::get('session.name', 'anchorcms');
static::$id = Cookie::get($name);

if(static::$id === false) {
Log::info('Session cookie not found: ' . $name);
static::$id = static::generate();
}

// load session data
$sql = "select data from sessions where id = ? and ip = ? and ua = ? limit 1";
$args = array(static::$id, Input::ip_address(), Input::user_agent());

if($session = Db::row($sql, $args)) {
static::$data = unserialize($session->data);
} else {
Db::insert('sessions', array(
'id' => static::$id,
'date' => date(DATE_ISO8601),
'ip' => Input::ip_address(),
'ua' => Input::user_agent(),
'data' => serialize(static::$data)
));
}
}

public static function end() {
session_write_close();
// cookie details
$name = Config::get('session.name', 'anchorcms');
$expire = time() + Config::get('session.expire', 86400);
$path = Config::get('session.path', '/');
$domain = Config::get('session.domain', '');

// set cookie
Cookie::write($name, static::$id, $expire, $path, $domain);

return Db::update('sessions', array(
'date' => date(DATE_ISO8601),
'ip' => Input::ip_address(),
'ua' => Input::user_agent(),
'data' => serialize(static::$data)
), array(
'id' => static::$id
));
}

public static function get($key, $default = false) {
return isset($_SESSION[$key]) ? $_SESSION[$key] : $default;
return isset(static::$data[$key]) ? static::$data[$key] : $default;
}

public static function set($key, $value) {
$_SESSION[$key] = $value;
static::$data[$key] = $value;
}

public static function forget($key) {
if(isset($_SESSION[$key])) {
unset($_SESSION[$key]);
if(isset(static::$data[$key])) {
unset(static::$data[$key]);
}
}

Expand Down
15 changes: 15 additions & 0 deletions system/functions/helpers.php
Expand Up @@ -54,4 +54,19 @@ function is_postspage() {
}

return false;
}

function is_debug() {
return Config::get('debug', false);
}

// benchmarking
function execution_time() {
$miliseconds = microtime(true) - ANCHOR_START;
return round($miliseconds, 2);
}

// return in mb
function memory_usage() {
return memory_get_peak_usage(true) / 1024 / 1024;
}
7 changes: 6 additions & 1 deletion themes/default/includes/footer.php
@@ -1,6 +1,11 @@
<div class="wrap">
<footer id="bottom">
<small>&copy; <?php echo date('Y'); ?> <?php echo site_name(); ?>. All rights reserved.</small>
<small>
&copy; <?php echo date('Y'); ?> <?php echo site_name(); ?>. All rights reserved.
<?php if(is_debug()): ?>
<br><em>Anchor took <?php echo execution_time(); ?> Secs to run and used <?php echo memory_usage(); ?>Mib of your memory.</em>
<?php endif; ?>
</small>

<ul role="navigation">
<li><a href="<?php echo rss_url(); ?>">RSS</a></li>
Expand Down

0 comments on commit 8f9988a

Please sign in to comment.