Widespread changes throughout Moodle to make sure it works on

servers that have register_globals turned off (this is the
default setting on newer version of PHP).

In fact it's partly a hack that globalises all GET, POST, FILES
AND COOKIE variables.  Unfortunately though the SESSION and
USER global session variables are only available as $_SESSION["USER"]
and $_SESSION["SESSION"], which is cumbersome to use.

So, for every request I now make a copy of these two session variables
into $USER and $SESSION.  Whenever I update them thoughout Moodle I
now have to call save_session("USER") which copies them back to the
session variable.  This seems to be working well now.

Because I'm using $_SESSION etc now this will raise
the required minimum version of PHP to 4.1.0
This commit is contained in:
martin
2002-08-06 17:23:45 +00:00
parent 515b9f306e
commit 8223d27193
27 changed files with 100 additions and 25 deletions
+2 -1
View File
@@ -44,7 +44,7 @@
$USER->loggedin = true;
$USER->admin = true;
$USER->teacher["$course->id"] = true;
save_session("USER");
}
require_login();
@@ -158,6 +158,7 @@
if ($usernew->id == $USER->id) { // Reload admin
$USER = get_user_info_from_db("id", $usernew->id);
$USER->loggedin = true;
save_session("USER");
set_moodle_cookie($USER->username);
}
redirect("index.php", "Changes saved");
+4
View File
@@ -35,10 +35,12 @@
}
$USER->student["$id"] = true;
save_session("USER");
if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl;
unset($SESSION->wantsurl);
save_session("SESSION");
} else {
$destination = "$CFG->wwwroot/course/view.php?id=$id";
}
@@ -68,10 +70,12 @@
}
$USER->student["$id"] = true;
save_session("USER");
if ($SESSION->wantsurl) {
$destination = $SESSION->wantsurl;
unset($SESSION->wantsurl);
save_session("SESSION");
} else {
$destination = "$CFG->wwwroot/course/view.php?id=$id";
}
+2
View File
@@ -36,4 +36,6 @@
notice("You are now logged in as $student_name", "$CFG->wwwroot/course/view.php?id=$course->id");
save_session("USER");
?>
+2
View File
@@ -70,6 +70,7 @@
if ($SESSION->returnpage) {
$return = $SESSION->returnpage;
unset($SESSION->returnpage);
save_session("SESSION");
redirect($return);
} else {
redirect("view.php?id=$mod->course");
@@ -79,6 +80,7 @@
if (isset($return)) {
$SESSION->returnpage = $HTTP_REFERER;
save_session("SESSION");
}
if (isset($move)) {
+2 -1
View File
@@ -48,7 +48,7 @@
echo "</DIV>";
// Print Admin links for teachers and admin.
if (isteacher($USER->id) || isadmin()) {
if (isteacher($course->id) || isadmin()) {
print_simple_box("Admin", $align="CENTER", $width="100%", $color="$THEME->cellheading");
$adminicon[]="<IMG SRC=\"../pix/i/edit.gif\" HEIGHT=16 WIDTH=16 ALT=\"Edit\">";
if (isediting($course->id)) {
@@ -82,6 +82,7 @@
forum_print_latest_discussions($social->id, 10, "plain", "DESC", false);
$SESSION->fromdiscussion = "$CFG->wwwroot/course/view.php?id=$course->id";
save_session("SESSION");
} else {
notify("Could not find or create a social forum here");
+1
View File
@@ -23,6 +23,7 @@
} else {
$USER->topic = $topic;
}
save_session("USER");
}
if (isteacher($course->id) and isset($marker)) {
+1
View File
@@ -37,6 +37,7 @@
if ($user->id == $USER->id) {
unset($USER->student["$id"]);
save_session("USER");
redirect("$CFG->wwwroot");
}
+2
View File
@@ -39,6 +39,8 @@
$USER->help = false;
}
save_session("USER");
if (! $course->category) { // This course is not a real course.
redirect("$CFG->wwwroot");
}
+1
View File
@@ -19,6 +19,7 @@
} else {
$USER->section = $week;
}
save_session("USER");
}
+7 -3
View File
@@ -26,7 +26,8 @@
<UL>
<LI>a working installation of <A HREF="http://www.php.net/">PHP</A> (version
4.0.6 or better), including the <A HREF="http://www.boutell.com/gd/">GD</A>
library for manipulating images.</LI>
library for manipulating images and with the "register_globals" variable
turned ON (for now).</LI>
<LI>a working database server (<A HREF="http://www.mysql.com/">MySQL</A>, PostgreSQL,
MSSQL, Oracle, Interbase, Foxpro, Access, ADO, Sybase, DB2 or ODBC).</LI>
</UL>
@@ -56,6 +57,7 @@
<LI>course/ - code to display and manage courses </LI>
<LI>doc/ - help documentation for Moodle (eg this page)</LI>
<LI>files/ - code to display and manage uploaded files</LI>
<LI>lang/ - texts in different languages, one directory per language </LI>
<LI>lib/ - libraries of core Moodle code </LI>
<LI>login/ - code to handle login and account creation </LI>
<LI>mod/ - all Moodle course modules</LI>
@@ -122,8 +124,10 @@
</P>
</BLOCKQUOTE>
<P>If you don't see this, then there must have been some problem with the database
or the configuration settings you defined in config.php. Check these and try
this page again.</P>
or the configuration settings you defined in config.php. Check also that your
PHP installation has "register_globals" turned on (recent versions have this
off by default). You can check PHP variables by creating a little file containing
&lt? phpinfo ?&gt and looking at it through a browser. Check all these and try this page again.</P>
<P>Press the &quot;Continue&quot; link at the bottom of the page.</P>
<P>Next you will see a similar page that sets up all the tables required by each
Moodle module. As before, they should all be green, otherwise you may be a problem
+1
View File
@@ -91,6 +91,7 @@
if (isset($USER->id)) {
$SESSION->fromdiscussion = "$CFG->wwwroot";
save_session($SESSION);
if (forum_is_subscribed($USER->id, $newsforum->id)) {
$subtext = get_string("unsubscribe", "forum");
} else {
+13 -1
View File
@@ -393,6 +393,7 @@ function error ($message, $link="") {
if ( !empty($SESSION->fromurl) ) {
$link = "$SESSION->fromurl";
unset($SESSION->fromurl);
save_session("SESSION");
} else {
$link = "$CFG->wwwroot";
}
@@ -455,7 +456,6 @@ function notice_yesno ($message, $linkyes, $linkno) {
function redirect($url, $message="", $delay=0) {
// Uses META tags to redirect the user, after printing a notice
global $THEME;
echo "<META HTTP-EQUIV='Refresh' CONTENT='$delay; URL=$url'>";
@@ -895,6 +895,7 @@ function require_login($courseid=0) {
if (! (isset( $USER->loggedin ) && $USER->confirmed) ) {
$SESSION->wantsurl = $FULLME;
$SESSION->fromurl = $HTTP_REFERER;
save_session("SESSION");
if ($PHPSESSID) { // Cookies not enabled.
redirect("$CFG->wwwroot/login/?PHPSESSID=$PHPSESSID");
} else {
@@ -920,6 +921,7 @@ function require_login($courseid=0) {
// Not allowed in the course, so see if they want to enrol
$SESSION->wantsurl = $FULLME;
save_session("SESSION");
redirect("$CFG->wwwroot/course/enrol.php?id=$courseid");
die;
}
@@ -937,9 +939,11 @@ function update_login_count() {
} else {
$SESSION->logincount++;
}
save_session("SESSION");
if ($SESSION->logincount > $max_logins) {
unset($SESSION->wantsurl);
save_session("SESSION");
error("Sorry, you have exceeded the allowed number of login attempts. Restart your browser.");
}
}
@@ -1004,6 +1008,7 @@ function reset_login_count() {
global $SESSION;
$SESSION->logincount = 0;
save_session("SESSION");
}
@@ -1023,6 +1028,13 @@ function get_moodle_cookie() {
}
function save_session($VAR) {
// Copies temporary session variable to permanent sesson variable
// eg $_SESSION["USER"] = $USER;
global $$VAR;
$_SESSION[$VAR] = $$VAR;
}
function verify_login($username, $password) {
+15 -5
View File
@@ -38,6 +38,17 @@
setlocale ("LC_TIME", $CFG->lang);
}
// The following is a big hack to get around the problem of PHP installations
// that have "register_globals" turned off (default since PHP 4.1.0).
// Eventually I'll go through and upgrade all the code to make this unnecessary
if (isset($_REQUEST)) {
extract($_REQUEST);
}
if (isset($_SERVER)) {
extract($_SERVER);
}
// Load up theme variables (colours etc)
require("$CFG->dirroot/theme/$CFG->theme/config.php");
@@ -49,16 +60,16 @@
require("$CFG->libdir/adodb/adodb.inc.php"); // Database access functions
require("$CFG->libdir/adodb/tohtml.inc.php");// Database display functions
require("$CFG->libdir/moodlelib.php"); // Various Moodle functions
// Load up global environment variables
class object {};
session_start();
session_register("SESSION"); // Current session info
session_register("USER"); // Current user info
if (! isset($SESSION)) $SESSION = new object;
if (! isset($USER)) $USER = new object;
if (! isset($_SESSION["SESSION"])) { $_SESSION["SESSION"] = new object; }
if (! isset($_SESSION["USER"])) { $_SESSION["USER"] = new object; }
extract($_SESSION); // Makes $SESSION and $USER available for read-only access
$FULLME = qualified_me();
$ME = strip_querystring($FULLME);
@@ -70,5 +81,4 @@
$db->PConnect($CFG->dbhost,$CFG->dbuser,$CFG->dbpass,$CFG->dbname);
?>
+1 -1
View File
@@ -27,9 +27,9 @@
error("Could not set the new password");
}
unset($USER);
$USER = $user;
$USER->loggedin = true;
save_session("USER");
set_moodle_cookie($USER->username);
+6 -2
View File
@@ -34,8 +34,12 @@
$USER->loggedin = true;
$USER->confirmed = 1;
if ( ! empty($SESSION["wantsurl"]) ) {
$goto = $SESSION["wantsurl"];
save_session("USER");
if ( ! empty($SESSION->wantsurl) ) {
$goto = $SESSION->wantsurl;
unset($SESSION->wantsurl);
save_session("SESSION");
redirect("$goto");
}
+7 -2
View File
@@ -36,6 +36,7 @@
$USER = $user;
$USER->loggedin = true;
save_session("USER");
if (!update_user_in_db()) {
error("Weird error: User not found");
@@ -44,7 +45,7 @@
if (!update_user_login_times()) {
error("Wierd error: could not update login records");
}
set_moodle_cookie($USER->username);
@@ -53,6 +54,7 @@
} else {
header("Location: $SESSION->wantsurl");
unset($SESSION->wantsurl);
save_session("SESSION");
}
reset_login_count();
@@ -63,9 +65,11 @@
$errormsg = get_string("invalidlogin");
}
}
if (empty($SESSION->wantsurl)) {
$SESSION->wantsurl = $HTTP_REFERER;
$SESSION->wantsurl = $HTTP_REFERER;
save_session("SESSION");
}
if (!$frm->username)
@@ -92,6 +96,7 @@ function update_user_login_times() {
$USER->lastlogin = $USER->currentlogin;
$USER->currentlogin = time();
save_session("USER");
return $db->Execute("UPDATE user
SET lastlogin='$USER->lastlogin', currentlogin='$USER->currentlogin'
+1
View File
@@ -4,6 +4,7 @@
require("../config.php");
$USER = NULL;
save_session("USER");
redirect($HTTP_REFERER);
exit;
+1
View File
@@ -29,6 +29,7 @@
add_to_log($course->id, "forum", "view discussion", "discuss.php?".$_SERVER["QUERY_STRING"], "$discussion->id");
unset($SESSION->fromdiscussion);
save_session("SESSION");
forum_set_display_mode($mode);
+1
View File
@@ -20,6 +20,7 @@
}
unset($SESSION->fromdiscussion);
save_session("SESSION");
add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
+4
View File
@@ -348,6 +348,7 @@ function forum_set_return() {
if (! $SESSION->fromdiscussion) {
$SESSION->fromdiscussion = $HTTP_REFERER;
save_session("SESSION");
}
}
@@ -358,6 +359,7 @@ function forum_go_back_to($default) {
if ($SESSION->fromdiscussion) {
$returnto = $SESSION->fromdiscussion;
unset($SESSION->fromdiscussion);
save_session("SESSION");
return $returnto;
} else {
return $default;
@@ -1026,8 +1028,10 @@ function forum_set_display_mode($mode=0) {
if ($mode) {
$USER->mode = $mode;
save_session("USER");
} else if (!$USER->mode) {
$USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
save_session("USER");
}
}
+1
View File
@@ -61,6 +61,7 @@
if (isset($forum)) { // User is starting a new discussion in a forum
$SESSION->fromurl = $HTTP_REFERER;
save_session("SESSION");
if (! $forum = get_record("forum", "id", $forum)) {
error("The forum number was incorrect ($forum)");
-2
View File
@@ -1,8 +1,6 @@
<?PHP // $Id$
// Collect ratings, store them, then return to where we came from
// Need to do some tricky business and store variables in the
// SESSION variable, just in case
require("../../config.php");
+9 -3
View File
@@ -59,10 +59,16 @@
if (! $forum = get_record("forum", "id", "$discussion->forum")) {
error("Could not find forum $discussion->forum");
}
$post->subject = "<A HREF=\"index.php?id=$course->id&forum=$forum->id\">$forum->name</A> -> ".
"<A HREF=\"discuss.php?d=$discussion->id\">$discussion->name</A> -> ".
"<A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A>";
$fullsubject = "<A HREF=\"view.php?f=$forum->id\">$forum->name</A>";
if ($forum->type != "single") {
$fullsubject .= " -> <A HREF=\"discuss.php?d=$discussion->id\">$discussion->name</A>";
if ($post->parent != 0) {
$fullsubject .= " -> <A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A>";
}
}
$post->subject = $fullsubject;
$post->message = highlight("$search", $post->message);
$fulllink = "<P ALIGN=right><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">See this post in context</A></P>";
+1
View File
@@ -20,6 +20,7 @@
}
unset($SESSION->fromdiscussion);
save_session("SESSION");
add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", "");
+1
View File
@@ -58,6 +58,7 @@
if ($USER) {
$SESSION->fromdiscussion = "$FULLME";
save_session("SESSION");
if (forum_is_forcesubscribed($forum->id)) {
$subtext = "Everyone is subscribed to this forum";
if (isteacher($course->id)) {
+5 -4
View File
@@ -36,15 +36,15 @@
$timenow = time();
if ($imagefile && $imagefile!="none") {
$imageinfo = GetImageSize($imagefile);
if ($filename = valid_uploaded_file($imagefile)) {
$imageinfo = GetImageSize($filename);
$image->width = $imageinfo[0];
$image->height = $imageinfo[1];
$image->type = $imageinfo[2];
switch ($image->type) {
case 2: $im = ImageCreateFromJPEG($imagefile); break;
case 3: $im = ImageCreateFromPNG($imagefile); break;
case 2: $im = ImageCreateFromJPEG($filename); break;
case 3: $im = ImageCreateFromPNG($filename); break;
default: error("Image must be in JPG or PNG format");
}
if (function_exists("ImageCreateTrueColor") and $CFG->gdversion >= 2) {
@@ -114,6 +114,7 @@
foreach ($usernew as $variable => $value) {
$USER->$variable = $value;
}
save_session("USER");
redirect("view.php?id=$user->id&course=$course->id", "Changes saved");
} else {
error("Could not update the user record ($user->id)");
+9
View File
@@ -84,4 +84,13 @@ function print_user($user, $course, $string) {
echo "</TD></TR></TABLE></TD></TR></TABLE>";
}
function valid_uploaded_file($newfile) {
// Returns current name of file on disk if true
if (is_uploaded_file($newfile['tmp_name']) and $newfile['size'] > 0) {
return $newfile['tmp_name'];
} else {
return "";
}
}
?>