Hi
I had problems with this snippet and PHP5 - and found that a plugin was the more reliable solution.
It’s pretty simple. Use this custom placeholder in your template head:
<base href="((base.href))" />
Prepend this placeholder to links that must always point to https regardless if the page the link appears on is encrypted or not:
((secure.server))
Prepend this placeholder to links that must always point to http regardless if the page the link appears on is encrypted or not:
((insecure.server))
(Note that using these last two placeholders should only be necessary for forms that POST data to another page, eg login forms and search forms where you don’t want the page to redirect thus losing your POSTed data).
Create a TV called ’encryption’, make it a checkbox and set the input option value: Yes==1
Assign the TV to all your templates and check the checkbox for pages that will you want SSL
Create a plugin called SSL, copy the code below into it and and tick the OnWebPageInit event.
(Change the $secureserver and $insecureserver values to your server)
/*
Title: SSL
Author: Mark Croxton, [email protected]
Desc: Plugin triggered OnWebPageInit
Manages switching between secure and insecure pages
Sets sitewide custom placeholders for secure/insecure server paths and base href
Updated: May 29 2009
*/
global $modx;
// server paths - no trailing slash!
$secureserver = "https://www.mywebsite.com";
$insecureserver = "http://www.mywebsite.com";
$id = $modx->documentIdentifier;
$tv = $modx->getTemplateVar('encryption', "", $id);
$encryption = $tv['value']; // will be set to false if not set
// What's the URI of the page we're checking
$url = $_SERVER['REQUEST_URI'];
// check for SSL
$ssl = isSSL();
// switch between http / https if necessary
// if $secureserver === $insecureserver (eg you are testing locally)
// then comment out this code block to avoid infinite recursion
if($encryption && !$ssl) {
// if SSL off and we are about to access a secure page then redirect
$modx->sendRedirect($secureserver.$url);
} else if ($ssl && !$encryption) {
// if SSL is on and we are about to acccess an unsecure page then redirect
$modx->sendRedirect($insecureserver.$url);
}
// set the some globals for use in custom non-caching ((placeholders))
if ($ssl) {
$GLOBALS['basehref'] = $secureserver.'/';
} else {
$GLOBALS['basehref'] = $insecureserver.'/';
}
$GLOBALS['secureserver'] = $secureserver.'/';
$GLOBALS['insecureserver'] = $insecureserver.'/';
function isSSL(){
if($_SERVER['HTTPS'] == 1) {
return TRUE;
} elseif ($_SERVER['HTTPS'] == 'on'){
return TRUE;
} elseif ($_SERVER['SERVER_PORT'] == 443) {
return TRUE;
} else {
return FALSE;
}
}
Now create a second plugin called SSL-PH with the code below and check the OnWebPagePrerender event.
/*
Title: SSL PH
Author: Mark Croxton, [email protected]
Desc: Plugin triggered OnWebPagePrerender
Replaces custom SSL placeholders in the document output
Updated: May 29 2009
*/
global $basehref, $secureserver, $insecureserver;
$fields = array('((base.href))','((secure.server))','((insecure.server))');
$values = array($basehref, $secureserver, $insecureserver);
$modx->documentOutput = str_replace($fields,$values,$modx->documentOutput);
Edited: 7th August 2009 - revised version sets global variables $GLOBALS[’secureserver’] and $GLOBALS[’insecureserver’] which can also be accessed in snippets