Warning: chmod(): Operation not permitted in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [0] in function chmod in /var/www/hopeinstoughton_www/wp-content/plugins/simple-universal-google-analytics/main.php on line 8 [1] in function include_once in /var/www/hopeinstoughton_www/wp-settings.php on line 560 [2] in function require_once in /var/www/hopeinstoughton_www/wp-config.php on line 85 [3] in function require_once in /var/www/hopeinstoughton_www/wp-load.php on line 50 [4] in function require_once in /var/www/hopeinstoughton_www/wp-blog-header.php on line 13 [5] in function require in /var/www/hopeinstoughton_www/index.php on line 17
| Server IP : 94.177.8.99 / Your IP : 216.73.217.165 Web Server : Apache/2.4.58 (Ubuntu) System : Linux aries 6.8.0-134-generic #134-Ubuntu SMP PREEMPT_DYNAMIC Fri Jun 26 18:43:11 UTC 2026 x86_64 User : www-data ( 33) PHP Version : 8.1.34 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/patientapps_support/modules/cms/model/ |
Upload File : |
<?php
use LightnCandy\LightnCandy;
class CMS_Model extends Model {
public $edit_auth = '';
public $INFO;
private $FIELDS;
private $model_menuedit = NULL;
private $preview;
private $pages = NULL;
private $pageshistory = NULL;
function __construct() {
parent::__construct();
$this->preview = '';
$this->level = isset($_SESSION['current_user']) ? $_SESSION['current_user']['u_level'] : 0;
$this->user_id = isset($_SESSION['current_user']) ? $_SESSION['current_user']['u_id'] : '';
$settings = $this->SettingGet('config.module_info', NULL);
$this->salt = isset($settings['key']) ? $settings['secret'] : '';
}
public function SetPages($value) {
$this->pages = $value;
}
public function SetPagesHistory($value) {
$this->pageshistory = $value;
}
public function SetMenuEditModel($model) {
$this->model_menuedit = $model;
}
private function _loadPage($page, $emailhash = FALSE) {
$this->INFO = $this->pages->query('select * from pages')->where('pg_uri=%s', $page)->first();
if ($this->INFO === NULL) return TRUE; // sql error
$this->INFO->tablename('pages');
if ($emailhash !== FALSE && $this->INFO->empty()) {
$this->loadDefaultEmailTemplate($emailhash);
}
return FALSE;
}
// return a value from 0 to 3 based on uri, editmode and user permissions
public function IsCmsPage($uri, $edithash) {
$query = ($p = strpos($uri, '?')) !== FALSE ? substr($uri, $p) : '';
if ($query != '') $uri = substr($uri, 0, $p);
$action = 0; // not a CMS page
// load from database
if ($this->_loadPage($uri, FALSE)) return 0;
if ($edithash != '') {
if($this->validateAuth($uri, $edithash)) {
$this->INFO->pg_uri = $uri;
$action = 2; // edit new or exiting page
}
}
if (!$action) {
if (!$this->INFO->empty()) $action = 1;
// check if have permission to access it
}
return $action;
}
public function _old_isCMSpage($page, $requested_auth, $previewid = '', $preview = '') {
$this->preview = $preview;
if (!is_object($GLOBALS['DATABASE'])) return 0; // no db.
if (!$GLOBALS['DATABASE']->TableExists('pages')) return 0;
$valid_auth = $this->validateAuth($page, $requested_auth);
if ($previewid != '') { // redirect to appropriate page, with preview
$this->RedirectToPreview($previewid);
exit;
}
if ($preview != '') {
$ok = $this->GeneratePreview($preview);
if ($ok) return 1;
}
$ok = $this->TemplateGet($page);
if ($valid_auth && ($this->level > 9 || ($ok && $this->INFO['pg_owner'] == $this->user_id)) ) {
if (!$ok) $this->INFO['pg_uri'] = $page;
return 2; // exists or not: can be edited
}
if ($ok) {
$document_level = empty($this->INFO['__extra']['documentlevel']) ? 0 : $this->INFO['__extra']['documentlevel'];
if ($this->level < $document_level) return 3; // don't have permission to access doc
return 1; // page exists - can view
}
return 0; // page not found
}
/* public function TemplateGet($page) {
$sql = <<<BLOCK
select pages.*, u_username as __username
from pages
left join user on pg_owner=u_id
where pg_uri=%s
BLOCK;
$this->INFO = $this->Lookup($sql, $page, 'pages');
$this->DbErr();
$ok = !empty($this->INFO['__existing']);
if ($ok && !empty($this->INFO['pg_extra'])) {
$this->INFO['__extra'] = unserialize($this->INFO['pg_extra']);
} else {
$this->INFO['__extra'] = [];
}
return $ok;
}
public function TemplateSet($page, $title, $body) {
$data = [ '__table' => 'pages',
'__existing' => 0,
'pg_uri' => $page,
'pg_title' => $title,
'pg_owner' => '',
'pg_text' => $body];
$this->SaveRecord($data);
} */
public function AddLink_CanEdit(&$navigation) {
if (!empty($this->edit_auth)) {
$navigation['edit'] = ['caption' => 'Edit Page', 'menuclass' => 'nav-edit', 'href' => "?editpage={$this->edit_auth}"];
}
}
public function ViewPage() {
// var_dump($this->pages);
$page_class = basename(rtrim($this->INFO->pg_uri, '/'));
if ($page_class == '') $page_class = 'page-index';
// $page_class = cleanslug($page_class);
$body = $this->GetBody();
if (hook_exists('cms.body')) {
$body = hook_execute('cms.body', $body, $this);
} else {
$body = "<div class=\"cms-container\">{$body}</div>";
}
$this->controller->Title($title);
/* $uri = $this->controller->uri();
$authentication = $this->GenerateAuth($uri);
$uri = $uri . '?editpage=' . $authentication;
$body .= "<p><a href=\"$uri\">edit</a></p>"; // */
$this->controller->Body($body);
}
public function GetBody() {
$body = ExpandMacro($this->INFO->pg_text);
$this->edit_auth = $this->GenerateAuth($this->INFO->pg_uri); // only if authorized
/* // if level > 9 or owner == logged in user, display edit link (if not displaying a preview)
if ($this->INFO['pg_owner'] == $this->user_id || $this->level > 9 && $this->preview == '') {
$this->edit_auth = $this->GenerateAuth($this->INFO['pg_uri']);
} else {
unset($this->edit_auth);
} */
return $body;
}
public function __html_footer(&$html) {
$params = [
'selector' => 'textarea.htmledit',
'branding' => false,
'convert_urls' => false,
'browser_spellcheck' => true,
'menubar' => 'edit insert view format table tools',
'toolbar' => 'undo redo | fontselect styleselect fontsizeselect forecolor backcolor removeformat | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | fullscreen code' ,
'plugins' => [
'advlist autolink lists link image charmap preview anchor',
'searchreplace visualblocks code fullscreen textcolor',
'insertdatetime media table contextmenu paste code'
],
'font_formats' => 'Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Courier New=courier new,courier;Helvetica=helvetica;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Verdana=verdana,geneva'
];
$params = hook_execute('tinymce.init', $params); // this is done very late so that theme-based hooks can be run
$params = json_encode($params);
$javascript = <<<BLOCK
tinymce.init($params);
BLOCK;
$id = $this->controller->IncludeFile('');
$id->JavascriptCommand($javascript);
}
private function _activetemplates() {
if (empty($this->active_templates))
$this->active_templates = $GLOBALS['loader']->EmailTemplates_List();
return $this->active_templates;
}
public function loadDefaultEmailTemplate($hash) {
$active_templates = $this->_activetemplates();
if (isset($active_templates[$hash])) {
$view_info = $this->_loadEmailTemplate($hash, $active_templates[$hash]);
$this->INFO->pg_title = $view_info['subject'];
$this->INFO->pg_text = $view_info['body'];
}
}
//to do: remove old previews
//to do: if email template, change fieldname from Title to Subject, and remove Permissions, & Preview button; maybe replace with Send TestEmail button;
public function EditPage() {
$uri = $this->controller->uri();
$this->isEmail = substr($uri, 0, 13) == '/admin/email/';
$auth = $this->GenerateAuth($uri);
if ($this->isEmail && $this->INFO->empty()) {
$elements = explode('/', $uri);
$this->loadDefaultEmailTemplate($elements[3]); // also do this if loading a [missing] template when about to email
}
$id = $this->controller->IncludeFile("/static/tinymce/tinymce.min.js");
$id->Version('');
hook_add('html.footer', [$this, '__html_footer']); // add tinymce loader
$duri = htmlspecialchars($this->INFO->pg_uri);
$dtitle = htmlspecialchars(empty($this->INFO->pg_title) ? '' : $this->INFO->pg_title);
$dbody = htmlspecialchars(empty($this->INFO->pg_text) ? '' : $this->INFO->pg_text);
/*
<div class="control-group cf-uri">
<label for="uri">URI</label>
<div class="controls"><input class="required" id="uri" name="uri" size="30" type="text" value="{$duri}"></div>
<div class="status-message" for="uri"></div></div>
*/
$caption = $this->isEmail ? 'Subject' : 'Page Title';
$this->TABS = [];
$body = <<<BLOCK
<input name="uri" type="hidden" value="{$duri}">
<div class="control-group cf-title">
<label for="title">$caption</label>
<div class="controls"><input class="required" id="title" name="title" size="30" type="text" value="{$dtitle}"></div>
<div class="status-message" for="title"></div></div>
<div class="control-group cf-body">
<label for="body">Body</label>
<div class="controls"><textarea name="body" class="htmledit pagebody">$dbody</textarea></div>
</div>
BLOCK;
hook_execute('cms.fields', $this); // add extra input fields
list($tabs, $content) = $this->_GenerateTabHTML();
$body = hook_execute('cms.prepare', $body, $this);
$previewhash = md5(time() . ':' . mt_rand(0, 999999999));
$document_level = empty($this->INFO->__extra['documentlevel']) ? 0 : $this->INFO->__extra['documentlevel'];
$groups = hook_execute('group.enum', FALSE);
$options = '';
foreach($groups as $grouplevel => $groupname) {
$sel = $document_level == $grouplevel ? ' selected' : '';
$options .= "<option value=\"$grouplevel\"$sel>$groupname</option>";
}
// button generation is part of the theme - theme should be set asap
$preview = hook_execute_late('html.button', '', 'Preview', 'fa-file-o', 'cc-light');
$button = hook_execute_late('html.button', '', 'Update', 'far fa-check-circle', 'cc-blue');
$body = <<<BLOCK
<form method="post" class="frm json">
<div id="cms_panel" class="vertical-panel">
<ul>
<li><a href="#tabs-1">General</a></li>
$tabs
<div class="action-group">
<div class="cms-perms">
<div>Permissions</div>
<div><select class="cms-perm-select" name="perms" id="perms">$options</select></div>
</div>
<div class="action-item"><a class="submit" for="submit_action" data-value="preview" href="/?previewid={$previewhash}" target="_blank">$preview</a></div>
<div class="action-item"><a class="submit" for="submit_action" data-value="update" href="#">$button</a></div>
</div>
</ul>
<div class="cms-tab" id="tabs-1">
<input id="submit_action" type="hidden" name="s" value="">
<input type="hidden" name="previewid" value="$previewhash">
$body
</div>
$content
</div>
</form>
BLOCK;
if (class_exists('BreadCrumb_model', FALSE)) {
$model_crumb = BreadCrumb_model::GetHandle();
$body = $model_crumb . $body;
}
$this->controller->Body($body);
}
private function _GenerateTabHTML() {
$tabs = $content = '';
foreach($this->TABS as $index => $tab_item) {
$name = cleanslug($tab_item['caption']);
$tabs .= "<li><a href=\"#{$name}\">{$tab_item['caption']}</a></li>";
$fields = $this->_GenerateFieldHTML($tab_item['fields']);
$content .= <<<BLOCK
<div class="cms-tab" id="{$name}">
<h3>{$tab_item['caption']}</h3>
$fields
</div>
BLOCK;
}
return array($tabs, $content);
}
public function AddTab($tabname /* field definitions follow */) {
$field_definitions = func_get_args();
array_shift($field_definitions);
$this->TABS[] = [ 'caption' => $tabname,
'fields' => $field_definitions ];
}
public function AddField_TextArea($fieldname, $caption) {
$node = [ 'type' => 'textarea',
'name' => $fieldname,
'caption' => $caption ]; // include sizes?
return $node;
}
private function _fieldHtml_TextArea($field_info, $current_value) {
$name = $field_info['name'];
$dcurrent = htmlspecialchars($current_value);
return $this->_label($field_info, "<textarea class=\"cms-textarea\" id=\"{$name}\" name=\"{$name}\">{$dcurrent}</textarea>");
}
public function AddField_Select($fieldname, $caption, $selectlist) {
$node = [ 'type' => 'select',
'name' => $fieldname,
'caption' => $caption,
'list' => $selectlist ]; //to do: should be 'options'
return $node;
}
private function _fieldHtml_Select($field_info, $current_value) {
$name = $field_info['name'];
$options = '';
foreach($field_info['list'] as $key => $value) {
$sel = $key === $current_value ? ' selected' : '';
$dvalue = htmlspecialchars($value);
$options .= "<option value=\"$key\"$sel>$dvalue</option>";
}
return $this->_label($field_info, "<select id=\"{$name}\" name=\"{$name}\">{$options}</select>");
}
public function AddField_Comment($caption) {
$node = [ 'type' => 'comment',
'caption' => $caption ];
return $node;
}
private function _fieldHtml_comment($field_info) {
return "<div class=\"cms-comment\">{$field_info['caption']}</div>";
}
public function AddField_checkbox($fieldname, $caption) {
$node = [ 'type' => 'checkbox',
'name' => $fieldname,
'caption' => $caption ];
return $node;
}
// input-frame
private function _fieldHtml_Checkbox($field_info, $current_value) {
$name = $field_info['name'];
$sel = empty($current_value) ? '' : ' checked';
// return $this->_label($field_info, "<input type=\"checkbox\" id=\"{$name}\" name=\"{$name}\" value=\"1\"$sel>");
$dcaption = htmlspecialchars($field_info['caption']);
return <<<BLOCK
<div class="control-group cf-{$name}">
<div class="controls input-frame">
<input type="checkbox" id="{$name}" name="{$name}" value="1"$sel>
<label for="$name">$dcaption</label>
</div>
<div class="status-message" for="{$name}"></div>
</div>
BLOCK;
}
public function AddField_Hidden($fieldname, $value = NULL) {
$node = [ 'type' => 'hidden',
'name' => $fieldname,
'value' => $value ];
return $node;
}
private function _fieldHtml_Hidden($field_info, $current_value) {
$dvalue = htmlspecialchars($current_value);
return "<input type=\"hidden\" name=\"{$field_info['name']}\" value=\"{$dvalue}\">";
}
public function AddField_Dimension($fieldname, $caption, $d1, $d2) {
$node = [ 'type' => 'dimension',
'name' => $fieldname,
'label' => [$d1, $d2],
'caption' => $caption];
return $node;
}
private function _fieldHtml_Dimension($field_info, $value1, $value2) {
$name = $field_info['name'];
$dim1 = $name . '_' . strtolower($field_info['label'][0]);
$dim2 = $name . '_' . strtolower($field_info['label'][1]);
$dvalue1 = htmlspecialchars($value1);
$dvalue2 = htmlspecialchars($value2);
$dcaption = htmlspecialchars($field_info['caption']);
return <<<BLOCK
<div class="control-group cf-{$name}">
<label for="$name">$dcaption</label>
<div class="controls">
{$field_info['label'][0]}: <input class="tiny" id="{$dim1}" name="{$dim1}" size="15" type="text" value="{$dvalue1}">
{$field_info['label'][1]}: <input class="tiny" id="{$dim2}" name="{$dim2}" size="15" type="text" value="{$dvalue2}">
</div>
<div class="status-message" for="{$name}"></div>
</div>
BLOCK;
}
public function AddField_input($fieldname, $caption) {
$node = [ 'type' => 'input',
'name' => $fieldname,
'caption' => $caption ]; // include sizes?
return $node;
}
private function _fieldHtml_Input($field_info, $current_value) {
$dvalue = htmlspecialchars($current_value);
return $this->_label($field_info, "<input type=\"text\" id=\"{$field_info['name']}\" name=\"{$field_info['name']}\" value=\"{$dvalue}\">");
}
public function AddField_input_small($fieldname, $caption) {
$node = [ 'type' => 'small',
'name' => $fieldname,
'caption' => $caption ]; // include sizes?
return $node;
}
private function _fieldHtml_Small($field_info, $current_value) {
$dvalue = htmlspecialchars($current_value);
return $this->_label($field_info, "<input type=\"text\" class=\"tiny\" id=\"{$field_info['name']}\" name=\"{$field_info['name']}\" value=\"{$dvalue}\">");
}
public function AddField_Sortable($fieldname, $caption) {
$node = [ 'type' => 'sortable',
'name' => $fieldname,
'caption' => $caption ];
return $node;
}
private function _fieldHtml_Sortable($field_info, $current_value) {
$current_value = trim($current_value);
// $current_value = '/our-solution=Our Solution;/about-us=About Us;/contact-us=Contact Us';
if ($current_value == '') {
$items = [];
} else {
$items = explode(';', $current_value);
}
$output = '';
foreach($items as $item) {
list($k, $v) = explode('=', $item, 2);
$output .= "<li data-value=\"$k=$v\"><span class=\"cms-action\"><i class=\"cms-icon cms-trash\"></i></span>$v</li>";
}
$dcurrent_value = htmlspecialchars($current_value);
$output = <<<BLOCK
<ul class="cms-sortable" for="{$field_info['name']}">{$output}</ul>
<a href="/~cms/page-list" class="cms-sortable-add" for="{$field_info['name']}">Add new item...</a>
<input type="hidden" id="{$field_info['name']}" name="{$field_info['name']}" value="{$dcurrent_value}">
BLOCK;
return $this->_label($field_info, $output);
}
private function _label($field_info, $input_html) {
$name = $field_info['name'];
$dcaption = htmlspecialchars($field_info['caption']);
return <<<BLOCK
<div class="control-group cf-{$name}">
<label for="{$name}">{$dcaption}</label>
<div class="controls">{$input_html}</div>
<div class="status-message" for="{$name}"></div>
</div>
BLOCK;
}
private function _GenerateFieldHTML($fieldlist) {
$output = '';
foreach($fieldlist as $field_info) {
$current_value = isset($field_info['name']) ? $this->_getCurrentValue($field_info['name']) : '';
switch($field_info['type']) {
case 'textarea':
$output .= $this->_fieldHtml_TextArea($field_info, $current_value);
break;
case 'select':
$output .= $this->_fieldHtml_Select($field_info, $current_value);
break;
case 'comment':
$output .= $this->_fieldHtml_comment($field_info);
break;
case 'checkbox':
$output .= $this->_fieldHtml_CheckBox($field_info, $current_value);
break;
case 'hidden':
$output .= $this->_fieldHtml_Hidden($field_info, $current_value);
break;
case 'dimension':
$dim1 = $field_info['name'] . '_' . strtolower($field_info['label'][0]);
$dim1 = $this->_getCurrentValue($dim1);
$dim2 = $field_info['name'] . '_' . strtolower($field_info['label'][1]);
$dim2 = $this->_getCurrentValue($dim2);
$output .= $this->_fieldHtml_Dimension($field_info, $dim1, $dim2);
break;
case 'input':
$output .= $this->_fieldHtml_Input($field_info, $current_value);
break;
case 'small':
$output .= $this->_fieldHtml_Small($field_info, $current_value);
break;
case 'sortable':
$output .= $this->_fieldHtml_Sortable($field_info, $current_value);
break;
}
}
return $output;
}
private function _getCurrentValue($name) {
switch($name) {
case 'title':
return empty($this->INFO->pg_title) ? '' : $this->INFO->pg_title;
case 'body':
return empty($this->INFO->pg_text) ? '' : $this->INFO->pg_text;
}
if (!isset($this->INFO->__extra[$name])) return '';
return $this->INFO__extra[$name];
}
public function pageSave($post) {
$results = [];
//$path = '';
$path = $this->INFO->pg_uri;
$isPreview = $post['s'] == 'preview';
// check for errors - all fields required
foreach(['title', 'body'] as $fieldname) {
$val = trim($post[$fieldname]);
$post[$fieldname] = $val;
$key = $fieldname;
if ($val == '') {
if (!isset($results['field'][$key])) {
if (!isset($results['field'])) $results['field'] = array();
$results['field'][$key] = array('state' => 1, 'message' => '?'); // '?' will be replaced by 'xxx must have a value'
}
}
}
if (isset($results['field'])) return $results;
// process uri
if (substr($path, 0, 1) != '/') $path = '/' . $path;
$old = [];
foreach($this->INFO as $key => $value) {
$old[$key] = $value;
}
$old = $this->INFO;
// $this->INFO->pg_uri = $path;
$this->INFO->pg_title = $post['title'];
$this->INFO->pg_text = $post['body'];
$this->INFO->__extra['documentlevel'] = empty($post['perms']) ? 0 : intval($post['perms']); // who has permission to view?
// save any extra fields
hook_execute('cms.save', $post, $this); // update $this->INFO and $this->INFO->__extra
$this->INFO->pg_owner = $this->user_id;
$this->INFO->pg_modified = gmdate('Y-m-d H:i:s');
$this->INFO->pg_extra = serialize($this->INFO->__extra);
if ($isPreview) {
$id = $this->SavePreview($this->INFO);
$_SESSION['preview'][$post['previewid']] = $id;
return $results;
} else {
if ($this->pageshistory !== NULL) $this->pageshistory->SaveHistory($old);
}
//** to do: remove old /tmp file, if present
$this->INFO->SaveRecord(['pg_uri' => $path]);
$err = $this->INFO->err();
$results['result'] = $err['errnum'];
$results['message'] = $err['errmsg'];
$title_changed = empty($old->pg_title) ? '' : $old->pg_title;
$title_changed = $this->INFO->pg_title != $title_changed;
/* if ($title_changed && is_object($this->model_menuedit) && !$DATABASE->errnum) {
$this->model_menuedit->MenuItem_UpdateTitle($path, $this->INFO['pg_title']);
} */
hook_execute('cms.saved', $this);
unset($this->INFO); // assume $this->INFO is destroyed at this point anyway (probably by a hook function)
if (!$results['result']) {
$results['result'] = -9999;
$results['message'] = 'Page has been updated. <a href="' . $path . '">View Page</a>';
}
if ($old->pg_uri != $path) { // name has changed - redirect with new auth
$auth = $this->GenerateAuth($path);
$results['redirect'] = $path . '?editpage=' . $auth;
}
return $results;
}
function toFilename($classname) {
$classname = str_replace('\\', '/', $classname);
return HOME . '/' . $classname . '.php';
}
private function _loadEmailTemplate($key, $item) {
list($classname, $entrypoint) = explode('::', $item['entrypoint'] . '::'); // classname is the file in which it appears
$entrypoint = strtolower($entrypoint);
$filename = $this->toFilename($classname);
if (!file_exists($filename)) {
die("unable to locate file specified by email template '{$item['entrypoint']}'");
}
$info = $GLOBALS['loader']->GetFileInformation($filename);
$view_info = NULL;
require_once $filename;
foreach($info as $class_id => $publics) {
foreach($publics as $public => $unused) {
if ($entrypoint == strtolower($public)) {
$view = new $class_id;
$view_info = $view->$entrypoint();
break;
}
}
if ($view_info !== NULL) break;
}
return $view_info;
}
public function getEmailLinks($active_templates) {
$output = [];
foreach($active_templates as $key => $item) {
$view_info = $this->_loadEmailTemplate($key, $item);
$value = ($view_info === NULL) ? 'email template could not be located' : $view_info['title'];
$path = '/admin/email/' . $key;
$auth = $this->GenerateAuth($path);
$node = ['key' => $key,
'value' => $value,
'link' => $path . '?editpage=' . $auth];
$output[] = $node;
}
return $output;
}
private function GetEmailTemplateHash($mailobject) {
$template = $mailobject->template();
if (strpos($template, '::') !== FALSE) {
list($module, $template) = explode('::', $template);
} else {
$module = '*';
}
$hashes = $this->_activetemplates();
foreach($hashes as $hash => $email_info) {
if ($email_info['internal'] == $template) {
if ($module == '*' || $module == $email_info['module']) return $hash;
}
}
return NULL;
}
public function ApplyEmailTemplate(&$mailobject) {
$hash = $this->GetEmailTemplateHash($mailobject);
$page = '/admin/email/' . $hash;
// get page from cms - if missing then get default email page
if (!$this->_loadPage($page, $hash)) {
$data = $mailobject->data();
$subject = $mailobject->subject();
if (empty($subject)) {
$mailobject->subject($this->_ProcessHandlebars($data, $this->INFO->pg_title));
}
$mailobject->body($this->_ProcessHandlebars($data, $this->INFO->pg_text));
}
}
private function _ProcessHandlebars($data, $templatetext) {
// md5 is used so we only compile when the source template has changed
$dir = HOME . '/storage/handlebars';
$workfile = $dir . '/hb' . md5($this->salt . ':' . $templatetext) . '.php';
if (!file_exists($dir)) mkdir($dir);
if (!file_exists($workfile)) { // do we have a compiled copy?
$php = LightnCandy::compile($templatetext, [
'flags' => LightnCandy::FLAG_HANDLEBARSJS | LightnCandy::FLAG_BESTPERFORMANCE
]);
file_put_contents($workfile, '<?php ' . $php);
}
$renderer = include($workfile); // get a handle to the compiled function
return $renderer($data);
}
/*private function LoadPreview($ph_id) {
$sql = <<<BLOCK
select pages_history.*, u_username as __username
from pages_history
left join user on pg_owner=u_id
where ph_id=%d and ph_ispreview=1
BLOCK;
$data = $this->Lookup($sql, $ph_id);
$this->DbErr();
$ok = !empty($data['__existing']);
if ($ok && !empty($data['pg_extra'])) {
$data['__extra'] = unserialize($data['pg_extra']);
} else {
$data['__extra'] = [];
}
return $data;
} */
/* private function GetPreviewAuth($data) {
$auth = md5($data['ph_timestamp'] . ':' . $data['pg_title'] . ':' . $data['ph_id']);
return $auth;
} */
//to do: fix this. $config is not a global
private function RedirectToPreview($previewid) {
global $config;
if (isset($_SESSION['preview'][$previewid])) {
$id = $_SESSION['preview'][$previewid];
$data = $this->LoadPreview($id);
} else {
$data = NULL;
}
if (empty($data['__existing'])) {
die("Unable to preview. Please try reloading the page.\n");
}
$auth = $this->GetPreviewAuth($data);
header('Location: ' . $config->base_url . $data['pg_uri'] . '?preview=' . $data['ph_id'] . '-' . $auth);
}
private function GeneratePreview($preview) {
list($id, $requested_auth) = explode('-', $preview . '-');
$this->INFO = $this->LoadPreview($id);
$auth = $this->GetPreviewAuth($this->INFO);
return ($auth == $requested_auth);
}
public function LoadDynamicPages() {
$sql = <<<BLOCK
select pg_title as `c`,pg_uri as `l`, 0 as `s`
from pages
where not (pg_uri like '/admin/email/%')
order by pg_modified desc
BLOCK;
$data = $this->Query($sql, NULL, NULL); // c = caption, l = link, s = isSeparator
$this->DbErr();
return $data;
}
public function GenerateAuth($page) {
$key = md5($page);
$auth = hook_execute('nonce.create', FALSE, 'cmspage', $key);
return $auth;
}
public function validateAuth($page, $requested_auth) {
$key = md5($page);
return hook_execute('nonce.verify', FALSE, 'cmspage', $key, $requested_auth);
}
// remove pages that already appear in $order
public function GeneratePageList($order, $page) {
$data = $this->LoadDynamicPages();
foreach($data as &$data_item) {
unset($data_item['s']);
}
return [
'popup' => [
'pages' => $data,
'width' => 430,
'template' => 'cms_selectpage',
'title' => 'Select CMS Page(s)',
'modal' => true,
]
];
}
public function GetPages($pages) {
foreach($pages as &$page) {
$page = $this->escapeString($page);
}
$list = join(',', $pages);
$output = [];
if ($list != '') {
$sql = <<<BLOCK
select pages.* from pages
where pg_uri in ($list)
BLOCK;
$data = $this->Query($sql, NULL, NULL);
foreach($data as $data_item) {
$key = $data_item['pg_uri'];
$data_item['__extra'] = unserialize($data_item['pg_extra']);
unset($data_item['pg_uri'], $data_item['pg_extra']);
$output[$key] = $data_item;
}
}
return $output;
}
public function ErrorPage(&$data, $uri) {
if ($this->level >= 9) {
$authentication = $this->GenerateAuth($uri);
$uri = $uri . '?editpage=' . $authentication;
$data['extra'] = "<p><a href=\"$uri\">create content for this page</a></p>" . $data['extra'];
return $uri;
}
return FALSE;
}
public function getEmailTemplate($template_name) {
}
}