For those interested, this is how I have implemented it using two files:
/root/includes/session.php: this is a where the authentication logic resides.
There is an array $attributes which is ALWAYS set. If the user is authenticated, then it contains the user data. If the user isn't authenticated, then it is an empty array.
Code: Select all
$attributes = array();
if (...) {
$attributes = array('id' => $userid);
}
/root/includes/update.php: this script it used to prepend the madcap output with require_once, if it's not yet there.
Code: Select all
function prepend($filename) {
$filecontent = file_get_contents($filename);
if(strpos($filecontent, "<?php require_once '/root/includes/session.php'; ?>") !== 0) {
file_put_contents($filename, "<?php require_once '/root/includes/session.php'; ?>\r\n" . $filecontent);
return true;
}
return false;
}
function processdirectory($dir) {
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle)))
{
if ($file == ".") {
}
elseif ($file == "..") {
}
elseif (is_dir($dir . '/' . $file)) {
$list .= processdirectory($dir . '/' . $file, $list);
}
elseif (strtolower(substr($file, strrpos($file, '.') + 1)) == 'php') {
if (prepend($dir . '/' . $file)) {
$list .= '<li>updated ' . $dir . '/' . $file . '</li>';
}
}
}
}
closedir($handle);
return $list;
}
processdirectory('/root/content/madcap');
Finally, in the master page, I use the following logic:
- If the $attributes array doesn't exists, run the update script and show "maintenance in progress"
- If the $attributes array exists, but not the id, then the user isn't logged in, and show "login"
- If the $attributes array exists, and also the id, then show the content
Code: Select all
<?php if (!isset($attributes)) : require_once '/root/includes/update.php'; echo('<div class="MCBreadcrumbsBox_0">Maintenance in progress... Please refresh the page.</div>'); ?>
<?php elseif (!isset($attributes['id'])) : ?>
<div class="MCBreadcrumbsBox_0">
Hello guest. You need to <a href="?action=login">login</a> or <a href="?action=register">register</a> to view the content of the page.
</div>
<?php else : ?>
<div class="row collapse">
<div class="sideContent">
<div class="clearfix">
<MadCap:topicToolbarProxy />
</div>
</div>
<MadCap:breadcrumbsProxy />
<MadCap:bodyProxy />
</div>
<?php endif; ?>