/** * Called automatically during init for template initalization. * * @param string $template_spot Where object's output goes * @param string|array $template_branch Where objects gets it's template * * @return AbstractView $this * * @internal */ public function initializeTemplate($template_spot = null, $template_branch = null) { if ($template_spot === null) { $template_spot = $this->defaultSpot(); } $this->spot = $template_spot; if (@$this->owner->template && !$this->owner->template->is_set($this->spot)) { throw $this->owner->template->exception('Spot is not found in owner\'s template')->addMoreInfo('spot', $this->spot); } if (!isset($template_branch)) { $template_branch = $this->defaultTemplate(); } if (isset($template_branch)) { // template branch would tell us what kind of template we have to // use. Let's look at several cases: if (is_object($template_branch)) { // it might be already template instance (object) $this->template = $template_branch; } elseif (is_array($template_branch)) { // it might be array with [0]=template, [1]=tag if (is_object($template_branch[0])) { // if [0] is object, we'll use that $this->template = $template_branch[0]; } else { $this->template = $this->app->add('Template'); /** @type Template $this->template */ $this->template->loadTemplate($template_branch[0]); } // Now that we loaded it, let's see which tag we need to cut out $this->template = $this->template->cloneRegion(isset($template_branch[1]) ? $template_branch[1] : '_top'); } else { // brach could be just a string - a region to clone off parent if (isset($this->owner->template)) { $this->template = $this->owner->template->cloneRegion($template_branch); } else { $this->template = $this->add('Template'); } } /** @type Template $this->template */ $this->template->owner = $this; } // Now that the template is loaded, let's take care of parent's template if ($this->owner && isset($this->owner->template) && !empty($this->owner->template)) { $this->owner->template->del($this->spot); } // Cool, now let's set _name of this template if ($this->template) { $this->template->trySet('_name', $this->getJSID()); } }
/** * this is the main feature of the view, in the MVC paradigm the controller sends updates to the view, this is * the method which captures the updates. * * The uri is essentially the part of the system which we are updating so different output will be negotiated * depending on the value of the uri. * * The data are the things which have changed due to the controller. * * The message is optional, it is for notes, debug information or with json sending messages back alongside the data * * @param string $uri * @param array $data * @return void * @author Craig Ulliott */ public static function update($uri, $data = NULL) { // extract the base from the url, we use this to determine the type of output $uri_r = explode('/', trim($uri, '/'), 2); $base = array_val($uri_r, 0); $path = array_val($uri_r, 1); // for an error we try and determine the best format to send back the message if ($base == 'error') { // if the original request came from AJAX if (array_val($_SERVER, 'HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest') { // rewrite and use the json handler for this error $base = 'json'; $path = 'fail'; $data = array_val($data, 'message', 'Unknown Error'); } else { // pass back the appropriate http code for this error $code = array_val($data, 'code'); switch ($code) { case '404': header("HTTP/1.0 404 Not Found"); break; case '500': header("HTTP/1.0 500 Internal Server Error"); break; default: die('unknown error code "' . $code . '"'); } // use the page handler to display this error $base = 'page'; $path = 'error/' . $code; } } // for an error, we try to determine if we are // we handle the update differently depending on the base of the uri switch ($base) { // these are the different layout files, we are loading a whole page template and passing the result into these layouts case 'page': // we are preparing a full html page $tpl_vars = array(); // the part of the page being updated from teh controller (aka the page contents) $tpl_vars['page_content'] = Template::loadTemplate($path, $data); // script and css clien side includes $tpl_vars['css_url'] = ENV == 'dev' ? '/css/generate' : STATIC_BASE_URL . 'css/style.css'; $tpl_vars['js_url'] = ENV == 'dev' ? '/js/generate' : STATIC_BASE_URL . 'js/script.js'; // todo:: $tpl_vars['css_url'] = '/css/generate'; $tpl_vars['js_url'] = '/js/generate'; // the facebook API key $tpl_vars['fb_api_key'] = getConfiguration('facebook', 'api_key'); // user values $tpl_vars['current_uid'] = CURRENT_UID; $tpl_vars['current_session_key'] = CURRENT_SESSION_KEY; // the parts of the path $path_r = explode('/', $path); // the active section is the first part of the path $active_section = reset($path_r); // used to set an active class on the main tab $tpl_vars['active'] = $active_section; // we build body classes to target css more accurately, one whole class for each parent section $body_classes = array(); while ($path_r) { $body_classes[] = implode('-', $path_r); array_pop($path_r); } // the current login state if (CURRENT_UID) { $body_classes[] = 'logged-in'; } // the current browser (TODO:) if (true == false) { $body_classes[] = 'ie-7'; } // the body classes, used to determine the browser and login state $tpl_vars['body_class'] = implode(' ', $body_classes); // render the full page in either the base or admin layout file $output = Template::loadLayout($base, $tpl_vars); // complete the translations Translator::translate('en'); $output = Translator::parse($output); // useful headers for debugging self::outputDebugHeaders(); // output to the browser die($output); // partial means we are rendering a template (usualy html) but not passing it back into the page template // this is usually for partial page updates preformed by javascript // partial means we are rendering a template (usualy html) but not passing it back into the page template // this is usually for partial page updates preformed by javascript case 'partial': // render the template and output to the browser $output = Template::loadTemplate($path, $data); // complete the translations Translator::translate('en'); $translated_output = Translator::parse($output); // useful headers for debugging self::outputDebugHeaders(); // to hold the output $r = array(); // the rest of the params go into the data key $r['page'] = $translated_output; // the correct content type header('Content-type: application/json'); // build and send the json back to the browser $encoded_output = json_encode($r); die($encoded_output); // json is used by javascript for various AJAX functionality // json is used by javascript for various AJAX functionality case 'json': $r = array(); switch ($path) { // ouput raw json data case 'data': // the content type header('Content-type: application/json'); // build and send the json back to the browser $encoded_output = json_encode($data); die($encoded_output); // success means we simply set the success key to 1, javascript will capture this // success means we simply set the success key to 1, javascript will capture this case 'success': $r['success'] = 1; break; // fail means we simply set the success key to 0, javascript will capture this and handle is as a fail // fail means we simply set the success key to 0, javascript will capture this and handle is as a fail case 'fail': $r['success'] = 0; break; default: throw new exception($path . ' is not a valid path for json output'); } // the data variable is used for sending back a message // it is sent as a blank string if one wasnt provided $r['message'] = (string) $data; // the correct content type header('Content-type: application/json'); // build and send the json back to the browser $encoded_output = json_encode($r); die($encoded_output); // content pass through, with the uri as a content type // content pass through, with the uri as a content type case 'content': // the different content types we accept switch ($path) { // common image types case 'image/png': case 'image/gif': case 'image/jpeg': // css and js // css and js case 'text/css': case 'text/javascript': case 'text/html': // data // data case 'text/csv': // the content type header('Content-type: ' . $path); // other useful headers for debugging self::outputDebugHeaders(); // send to the browser die($data); default: throw new exception($path . ' is not a known safe content type'); } default: throw new exception($base . ' is not a valid base for updating this view'); } }
if (isset($_POST['register-captcha'])) { $inputValue[4] = ""; /* we need to always clear the captcha field, because it will regenerate after a reresh*/ if ($Error->captcha($_POST['register-captcha'])) { $errorClass[4] = 'success'; } else { $errorClass[4] = 'error'; } } else { if (isset($_POST['action'])) { $Error->add('error', 'Captcha cannot be left empty'); $errorClass[4] = 'error'; } } // Instantiontiate the erroring before we need to refresh the page $msg = $Error->alert(); // Check if the form was submitted without any errors. if (isset($_POST['register-username']) && isset($_POST['register-password']) && isset($_POST['register-confirm']) && isset($_POST['register-email']) && isset($_POST['register-captcha']) && !$Error->ok()) { $complete = true; // Create the actual user Auth::createNewUser($_POST['register-username'], $_POST['register-password'], $_POST['register-email']); $userId = Auth::userId($_POST['register-username']); $link = full_url_to_script('activate.php') . "?action=activate&code=" . Activation::get($userId) . "&id=" . $userId; //echo $link; Emailtemplate::setBaseDir('./assets/email_templates'); $html = Emailtemplate::loadTemplate('activation', array('title' => 'Activation Email', 'prettyName' => Options::get('prettyName'), 'name' => $_POST['register-username'], 'siteName' => Options::get('emailName'), 'activationLink' => $link, 'footerLink' => Options::get('siteName'), 'footerEmail' => Options::get('emailInfo'))); send_html_mail(array($_POST['register-username'] => $_POST['register-email']), 'Activation Email', $html, array(Options::get('siteName') => Options::get('emailAdmin'))); } Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg, 'meta' => $meta, 'selected' => 'register')), 'content' => Template::loadTemplate('register', array('errorClass' => $errorClass, 'inputValue' => $inputValue, 'complete' => $complete, 'callback' => $callback)), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
<?php //Bootstrap SPF require 'includes/master.inc.php'; //$mustauth=true; //This loads up $user - $isadmin - $js require 'includes/user.inc.php'; $title = "Settings, <small>control this online world</small>"; Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg)), 'content' => Template::loadTemplate('settings'), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
<?php //Bootstrap SPF require 'includes/master.inc.php'; require 'includes/user.inc.php'; $mustauth = true; $Auth->requireUser(); $Auth->requireAdmin(); $changes = false; // Check if the settings have changed, the input name needs to be the same as the option name if (isset($_POST)) { foreach ($_POST as $key => $value) { if (Options::get($key)) { if (htmlspecialchars(Options::get($key)) !== htmlspecialchars($value)) { Options::set($key, $value); $changes = true; } } } } $title = "Admin <small>take control</small>"; Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg, 'selected' => 'admin')), 'content' => Template::loadTemplate('admin', array('changes' => $changes)), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
if (htmlspecialchars(Options::userGet($userId, $dateOption)) !== htmlspecialchars($timestamp)) { // Set the option to the new value Options::userSet($userId, $dateOption, $timestamp); $changes = true; } } } if (isset($_POST['action']) && $_POST['action'] == 'Save Changes') { // These are the 2 static things in your account a password and a email address if (isset($_POST['user-email']) && $Error->email($_POST['user-email'], false) && $_POST['user-email'] !== $u->email) { $u->email = $_POST['user-email']; $u->update(); Activation::remove($userId); $link = full_url_to_script('activate.php') . "?action=activate&code=" . Activation::generate($userId) . "&id=" . $userId; Emailtemplate::setBaseDir('./assets/email_templates'); $html = Emailtemplate::loadTemplate('reactivate', array('title' => 'Reactivation Email', 'prettyName' => Options::get('prettyName'), 'name' => $u->username, 'siteName' => Options::get('emailName'), 'activationLink' => $link, 'footerLink' => Options::get('siteName'), 'footerEmail' => Options::get('emailInfo'))); send_html_mail(array($u->username => $u->email), 'Reactivation Email', $html, array(Options::get('siteName') => Options::get('emailAdmin'))); $Error->add('info', '<strong>Logged Out</strong><br />We have sent you a reactivation email to the new email address in order to verify it. Please check your email and follow the link within.'); $Auth->logout(); } // These are the 2 static things in your account a password and a email address if (isset($_POST['user-password']) && $_POST['user-password'] !== $inputValue[1] && $_POST['user-password'] !== '') { Auth::changePassword($u->id, $_POST['user-password']); $Error->add('info', '<strong>Logged Out</strong><br />Password updated, you may login with your new password'); $Auth->logout(); } } } Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg)), 'content' => Template::loadTemplate('settings', array('inputValue' => $inputValue, 'userId' => $userId, 'changes' => $changes)), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
<?php //Bootstrap SPF require 'includes/master.inc.php'; //This loads up $user - $isadmin - $js require 'includes/user.inc.php'; $content = ''; $fb = array(); $title = 'Welcome <small>one and all</small>'; Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg, 'selected' => 'home', 'fb' => $fb)), 'content' => Template::loadTemplate('index', array('update' => $optionsUpdate, 'install' => $installRan)), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
// Instantiontiate the erroring before we need to refresh the page $msg = $Error->alert(); // Check if the form was submitted without any errors. if (isset($detail) && Auth::resetPasswordCheck($detail) !== false) { $userId = Auth::resetPasswordCheck($detail); $activationCode = Activation::get($userId); $complete = true; $u = new User($userId); $link = full_url_to_script('forgot.php') . "?action=resetpassword&code=" . Activation::get($userId) . "&uid=" . $userId; // Select the Email tempalte and replace the relevant values Emailtemplate::setBaseDir('./assets/email_templates'); $html = Emailtemplate::loadTemplate('forgot', array('title' => 'Reset Password Email', 'prettyName' => Options::get('prettyName'), 'name' => $u->username, 'siteName' => Options::get('emailName'), 'link' => $link, 'footerLink' => Options::get('siteName'), 'footerEmail' => Options::get('emailInfo'))); // Replace the relevant values and send the HTML email send_html_mail(array($u->username => $u->email), 'Reset Password Email', $html, array(Options::get('siteName') => Options::get('emailAdmin'))); } // Otherwise if the email link is followed lets reset the password and email it to the user. if (isset($_GET['action']) && $_GET['action'] == 'resetpassword' && isset($_GET['uid']) && isset($_GET['code']) and Activation::get($_GET['uid']) == $_GET['code']) { $u = new User($_GET['uid']); $userId = $u->id; $newPassword = Auth::generateStrongPassword(6, false, 'ld'); Auth::changePassword($userId, $newPassword); $reset = true; // Select the Email tempalte and replace the relevant values Emailtemplate::setBaseDir('./assets/email_templates'); $html = Emailtemplate::loadTemplate('reset', array('title' => 'Password Successfully Reset', 'prettyName' => Options::get('prettyName'), 'name' => $u->username, 'siteName' => Options::get('emailName'), 'password' => $newPassword, 'footerLink' => Options::get('siteName'), 'footerEmail' => Options::get('emailInfo'))); // Replace the relevant values and send the HTML email send_html_mail(array($u->username => $u->email), 'New Password', $html, array(Options::get('siteName') => Options::get('emailAdmin'))); } Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg, 'meta' => $meta, 'selected' => 'forgot')), 'content' => Template::loadTemplate('forgot', array('inputValue' => $inputValue, 'complete' => $complete, 'reset' => $reset, 'password' => $newPassword)), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
} if (isset($_REQUEST['code']) && $_REQUEST['code'] !== $inputValue[1]) { $code = $_REQUEST['code']; $inputValue[1] = $code; } else { $errorClass[1] = 'error'; $Error->add('error', 'Invalid activation code'); } } if ($uid and $code) { // First check the client's username and get the id if it's not one $userId = Auth::userId($uid); $activationCode = Activation::get($userId); /*echo $uid . "<br />"; echo $userId . "<br />"; echo $activationCode . "<br />"; echo $code . "<br />";*/ if ($activationCode !== $code) { $errorClass[0] = 'error'; $errorClass[1] = 'error'; $Error->add('error', 'Activation unsuccessfull, please confirm that the details are correct or follow the link in the activation email sent to you.'); } } $msg = $Error->alert(); if (!$Error->ok() && ($uid and $code)) { $complete = true; echo Activation::activate($userId); } Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg, 'selected' => 'activate')), 'content' => Template::loadTemplate('activate', array('complete' => $complete, 'errorClass' => $errorClass, 'inputValue' => $inputValue)), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
$padding = 3; for ($i = 1; $i <= $Pager->numPages; $i++) { $min = $Pager->page - $padding; $max = $Pager->page + $padding; if ($i == 1) { $paging .= '<li><a href="?page=' . $i . $searchAppend . '">«</a></li>' . nl(); } if ($i == $Pager->page) { $paging .= '<li class="active"><a href="#">' . $i . '</a></li>' . nl(); } else { if ($i >= $max xor $i > $min) { $paging .= '<li><a href="?page=' . $i . $searchAppend . '">' . $i . '</a></li>' . nl(); } } if ($i == $Pager->numPages) { $paging .= '<li><a href="?page=' . $Pager->numPages . $searchAppend . '">»</a></li>' . nl(); } } $paging .= '</ul></div>' . nl(); // Build the paging // Build the user list array and pass it into the template $Users = DBObject::glob('Users', 'SELECT * FROM users ' . $search . ' ORDER By username ASC' . $Pager->limits); foreach ($Users as $User) { $userList[$User->id]['email'] = $User->email; $userList[$User->id]['username'] = $User->username; $userList[$User->id]['group'] = $User->level; } // Build the user list Template::setBaseDir('./assets/tmpl'); $html = Template::loadTemplate('layout', array('header' => Template::loadTemplate('header', array('title' => $title, 'user' => $user, 'admin' => $isadmin, 'msg' => $msg, 'selected' => 'users')), 'content' => Template::loadTemplate('users', array('users' => $userList, 'pager' => $paging, 'search' => $searchValue, 'total' => $Pager->numRecords, 'user' => $user, 'admin' => $isadmin, 'page' => $Pager->page, 'update' => $update)), 'footer' => Template::loadTemplate('footer', array('time_start' => $time_start)))); echo $html;
<?php // includes & requires require_once __DIR__ . '/includes/classes/template.php'; // our template class // define our vars (fixed or via calculations) $title = 'testpage'; $userName = '******'; $weatherToday = 'cloudy'; // load template $tpl = new Template(); $tpl->loadTemplate(__DIR__ . '/templates/main.tpl'); // render template with our data // @note The template class with automatically prevent XSS for us :-) echo $tpl->render(array('title' => $title, 'user' => $userName, 'weather' => $weatherToday)); // EOF
/** * wrapper function to load and parse a simple template * * @param string $uri * @param string $tpl_vars * @return string * @author Craig Ulliott */ function template($uri, $tpl_vars) { return Template::loadTemplate($uri, $tpl_vars); }