3 * Enable cookieless sessions by including $CFG->usesid=true;
5 * Based on code from php manual by Richard at postamble.co.uk
6 * Attempts to use cookies if cookies not present then uses session ids attached to all urls and forms to pass session id from page to page.
7 * If site is open to google, google is given guest access as usual and there are no sessions. No session ids will be attached to urls for googlebot.
8 * This doesn't require trans_sid to be turned on but this is recommended for better performance
10 * session.use_trans_sid = 1
11 * in your php.ini file and make sure that you don't have a line like this in your php.ini
12 * session.use_only_cookies = 1
13 * @author Richard at postamble.co.uk and Jamie Pratt
14 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
17 * You won't call this function directly. This function is used to process
18 * text buffered by php in an output buffer. All output is run through this function
20 * @param string $buffer is the output sent from php
21 * @return string the output sent to the browser
23 function sid_ob_rewrite($buffer){
24 $replacements = array(
25 '/(<\s*(a|link|script|frame|area)\s[^>]*(href|src)\s*=\s*")([^"]*)(")/i',
26 '/(<\s*(a|link|script|frame|area)\s[^>]*(href|src)\s*=\s*\')([^\']*)(\')/i');
28 $buffer = preg_replace_callback($replacements, "sid_rewrite_link_tag", $buffer);
29 $buffer = preg_replace('/<form\s[^>]*>/i',
30 '\0<input type="hidden" name="' . session_name() . '" value="' . session_id() . '"/>', $buffer);
35 * You won't call this function directly. This function is used to process
36 * text buffered by php in an output buffer. All output is run through this function
38 * This function only processes absolute urls, it is used when we decide that
39 * php is processing other urls itself but needs some help with internal absolute urls still.
40 * @param string $buffer is the output sent from php
41 * @return string the output sent to the browser
43 function sid_ob_rewrite_absolute($buffer){
44 $replacements = array(
45 '/(<\s*(a|link|script|frame|area)\s[^>]*(href|src)\s*=\s*")((?:http|https)[^"]*)(")/i',
46 '/(<\s*(a|link|script|frame|area)\s[^>]*(href|src)\s*=\s*\')((?:http|https)[^\']*)(\')/i');
48 $buffer = preg_replace_callback($replacements, "sid_rewrite_link_tag", $buffer);
49 $buffer = preg_replace('/<form\s[^>]*>/i',
50 '\0<input type="hidden" name="' . session_name() . '" value="' . session_id() . '"/>', $buffer);
54 * A function to process link, a and script tags found
55 * by preg_replace_callback in {@link sid_ob_rewrite($buffer)}.
57 function sid_rewrite_link_tag($matches){
59 $url=sid_process_url($url);
60 return $matches[1]. $url.$matches[5];
63 * You can call this function directly. This function is used to process
64 * urls to add a moodle session id to the url for internal links.
65 * @param string $url is a url
66 * @return string the processed url
68 function sid_process_url($url) {
70 if ((preg_match('/^(http|https):/i', $url)) // absolute url
71 && ((stripos($url, $CFG->wwwroot
)!==0) && stripos($url, $CFG->httpswwwroot
)!==0)) { // and not local one
72 return $url; //don't attach sessid to non local urls
74 if ($url[0]=='#' ||
(stripos($url, 'javascript:')===0)) {
75 return $url; //don't attach sessid to anchors
77 if (strpos($url, session_name())!==FALSE)
79 return $url; //don't attach sessid to url that already has one sessid
81 if (strpos($url, "?")===FALSE){
82 $append="?".strip_tags(session_name() . '=' . session_id() );
84 $append="&".strip_tags(session_name() . '=' . session_id() );
86 //put sessid before any anchor
87 $p = strpos($url, "#");
89 $anch = substr($url, $p);
90 $url = substr($url, 0, $p).$append.$anch ;
99 * Call this function before there has been any output to the browser to
100 * buffer output and add session ids to all internal links.
102 function sid_start_ob(){
104 //don't attach sess id for bots
106 if (!empty($_SERVER['HTTP_USER_AGENT'])) {
107 if (!empty($CFG->opentogoogle
)) {
108 if (strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false ) {
109 @ini_set
('session.use_trans_sid', '0'); // try and turn off trans_sid
113 if (strpos($_SERVER['HTTP_USER_AGENT'], 'google.com') !== false ) {
114 @ini_set
('session.use_trans_sid', '0'); // try and turn off trans_sid
119 if (strpos($_SERVER['HTTP_USER_AGENT'], 'W3C_Validator') !== false ) {
120 @ini_set
('session.use_trans_sid', '0'); // try and turn off trans_sid
125 @ini_set
('session.use_trans_sid', '1'); // try and turn on trans_sid
126 if (ini_get('session.use_trans_sid')!=0 ){
127 // use trans sid as its available
128 ini_set('url_rewriter.tags', 'a=href,area=href,script=src,link=href,'
129 . 'frame=src,form=fakeentry');
130 ob_start('sid_ob_rewrite_absolute');
132 //rewrite all links ourselves
133 ob_start('sid_ob_rewrite');