Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaned up format a little, plus added better error checking of server

variables so that at least people will get feedback if they are missing
  • Loading branch information...
commit c1d57101740f42fdb794d9fb80e3696a347ab281 1 parent 9b21d11
moodler authored
Showing with 81 additions and 63 deletions.
  1. +81 −63 lib/weblib.php
View
144 lib/weblib.php
@@ -8,7 +8,7 @@
/// Constants
-// Define text formatting types ... eventually we can add Wiki, BBcode etc
+/// Define text formatting types ... eventually we can add Wiki, BBcode etc
define("FORMAT_MOODLE", "0");
define("FORMAT_HTML", "1");
@@ -67,27 +67,27 @@
/// Functions
function s($var) {
-// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
-// or if $var is empty, will return an empty string.
+/// returns $var with HTML characters (like "<", ">", etc.) properly quoted,
+/// or if $var is empty, will return an empty string.
return empty($var) ? "" : htmlSpecialChars(stripslashes($var));
}
function p($var) {
-// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
-// or if $var is empty, will print an empty string.
+/// prints $var with HTML characters (like "<", ">", etc.) properly quoted,
+/// or if $var is empty, will print an empty string.
echo empty($var) ? "" : htmlSpecialChars(stripslashes($var));
}
function nvl(&$var, $default="") {
-// if $var is undefined, return $default, otherwise return $var
+/// if $var is undefined, return $default, otherwise return $var
return isset($var) ? $var : $default;
}
function strip_querystring($url) {
-// takes a URL and returns it without the querystring portion
+/// takes a URL and returns it without the querystring portion
if ($commapos = strpos($url, '?')) {
return substr($url, 0, $commapos);
@@ -97,37 +97,45 @@ function strip_querystring($url) {
}
function get_referer() {
-// returns the URL of the HTTP_REFERER, less the querystring portion
+/// returns the URL of the HTTP_REFERER, less the querystring portion
global $HTTP_REFERER;
+
return strip_querystring(nvl($HTTP_REFERER));
}
+
function me() {
-// returns the name of the current script, WITH the querystring portion.
-// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
-// return different things depending on a lot of things like your OS, Web
-// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
+/// returns the name of the current script, WITH the querystring portion.
+/// this function is necessary because PHP_SELF and REQUEST_URI and PATH_INFO
+/// return different things depending on a lot of things like your OS, Web
+/// server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
global $REQUEST_URI, $PATH_INFO, $PHP_SELF;
if ($REQUEST_URI) {
return $REQUEST_URI;
+
} else if ($PATH_INFO) {
return $PATH_INFO;
+
} else if ($PHP_SELF) {
return $PHP_SELF;
+
} else {
- return "";
+ notify("Error: Could not find any of these web server variables: \$REQUEST_URI, \$PATH_INFO or \$PHP_SELF");
}
}
-
function qualified_me() {
-// like me() but returns a full URL
+/// like me() but returns a full URL
- global $HTTPS, $SERVER_PROTOCOL, $HTTP_HOST;
+ global $HTTPS, $HTTP_HOST;
+
+ if (!$HTTP_HOST) {
+ notify("Error: could not find web server variable: \$HTTP_HOST");
+ }
$protocol = (isset($HTTPS) && $HTTPS == "on") ? "https://" : "http://";
$url_prefix = "$protocol$HTTP_HOST";
@@ -136,18 +144,20 @@ function qualified_me() {
function match_referer($good_referer = "") {
-// returns true if the referer is the same as the good_referer. If
-// good_refer is not specified, use qualified_me as the good_referer
+/// returns true if the referer is the same as the good_referer. If
+/// good_refer is not specified, use qualified_me as the good_referer
- if ($good_referer == "") { $good_referer = qualified_me(); }
+ if ($good_referer == "") {
+ $good_referer = qualified_me();
+ }
return $good_referer == get_referer();
}
function stri_replace($find, $replace, $string ) {
-// This does a search and replace, ignoring case
-// This function is only here because one doesn't exist yet in PHP
-// Unlike str_replace(), this only works on single values (not arrays)
+/// This does a search and replace, ignoring case
+/// This function is only here because one doesn't exist yet in PHP
+/// Unlike str_replace(), this only works on single values (not arrays)
$parts = explode(strtolower($find), strtolower($string));
@@ -162,11 +172,11 @@ function stri_replace($find, $replace, $string ) {
}
function read_template($filename, &$var) {
-// return a (big) string containing the contents of a template file with all
-// the variables interpolated. all the variables must be in the $var[] array or
-// object (whatever you decide to use).
-//
-// WARNING: do not use this on big files!!
+/// return a (big) string containing the contents of a template file with all
+/// the variables interpolated. all the variables must be in the $var[] array or
+/// object (whatever you decide to use).
+///
+/// WARNING: do not use this on big files!!
$temp = str_replace("\\", "\\\\", implode(file($filename), ""));
$temp = str_replace('"', '\"', $temp);
@@ -175,9 +185,9 @@ function read_template($filename, &$var) {
}
function checked(&$var, $set_value = 1, $unset_value = 0) {
-// if variable is set, set it to the set_value otherwise set it to the
-// unset_value. used to handle checkboxes when you are expecting them from
-// a form
+/// if variable is set, set it to the set_value otherwise set it to the
+/// unset_value. used to handle checkboxes when you are expecting them from
+/// a form
if (empty($var)) {
$var = $unset_value;
@@ -187,8 +197,8 @@ function checked(&$var, $set_value = 1, $unset_value = 0) {
}
function frmchecked(&$var, $true_value = "checked", $false_value = "") {
-// prints the word "checked" if a variable is true, otherwise prints nothing,
-// used for printing the word "checked" in a checkbox form input
+/// prints the word "checked" if a variable is true, otherwise prints nothing,
+/// used for printing the word "checked" in a checkbox form input
if ($var) {
echo $true_value;
@@ -199,10 +209,10 @@ function frmchecked(&$var, $true_value = "checked", $false_value = "") {
function link_to_popup_window ($url, $name="popup", $linkname="click here", $height=400, $width=500, $title="Popup window") {
-// This will create a HTML link that will work on both
-// Javascript and non-javascript browsers.
-// Relies on the Javascript function openpopup in javascript.php
-// $url must be relative to home page eg /mod/survey/stuff.php
+/// This will create a HTML link that will work on both
+/// Javascript and non-javascript browsers.
+/// Relies on the Javascript function openpopup in javascript.php
+/// $url must be relative to home page eg /mod/survey/stuff.php
global $CFG;
@@ -216,6 +226,8 @@ function link_to_popup_window ($url, $name="popup", $linkname="click here", $hei
}
function close_window_button() {
+/// Prints a simple button to close a window
+
echo "<FORM><CENTER>";
echo "<INPUT TYPE=button onClick=\"self.close();\" VALUE=\"Close this window\">";
echo "</CENTER></FORM>";
@@ -223,7 +235,8 @@ function close_window_button() {
function choose_from_menu ($options, $name, $selected="", $nothing="choose", $script="", $nothingvalue="0", $return=false) {
-// $options["value"]["label"]
+/// Given an array of value, creates a popup menu to be part of a form
+/// $options["value"]["label"]
if ($nothing == "choose") {
$nothing = get_string("choose")."...";
@@ -261,12 +274,12 @@ function choose_from_menu ($options, $name, $selected="", $nothing="choose", $sc
}
function popup_form ($common, $options, $formname, $selected="", $nothing="choose") {
-// Implements a complete little popup form
-// $common = the URL up to the point of the variable that changes
-// $options = A list of value-label pairs for the popup list
-// $formname = name must be unique on the page
-// $selected = the option that is already selected
-// $nothing = The label for the "no choice" option
+/// Implements a complete little popup form
+/// $common = the URL up to the point of the variable that changes
+/// $options = A list of value-label pairs for the popup list
+/// $formname = name must be unique on the page
+/// $selected = the option that is already selected
+/// $nothing = The label for the "no choice" option
if ($nothing == "choose") {
$nothing = get_string("choose")."...";
@@ -296,6 +309,7 @@ function popup_form ($common, $options, $formname, $selected="", $nothing="choos
function formerr($error) {
+/// Prints some red text
if (!empty($error)) {
echo "<font color=#ff0000>$error</font>";
}
@@ -303,7 +317,7 @@ function formerr($error) {
function validate_email ($address) {
-// Validates an email to make it makes sense.
+/// Validates an email to make it makes sense.
return (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.
'@'.
'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
@@ -313,9 +327,9 @@ function validate_email ($address) {
function get_slash_arguments($i=0) {
-// Extracts arguments from "/foo/bar/something"
-// eg http://mysite.com/script.php/foo/bar/something
-// Might only work on Apache
+/// Extracts arguments from "/foo/bar/something"
+/// eg http://mysite.com/script.php/foo/bar/something
+/// Might only work on Apache
global $PATH_INFO;
@@ -339,16 +353,17 @@ function get_slash_arguments($i=0) {
}
function format_text_menu() {
+/// Just returns an array of formats suitable for a popup menu
return array (FORMAT_MOODLE => get_string("formattext"),
FORMAT_HTML => get_string("formathtml") );
}
function format_text($text, $format, $options=NULL) {
-// Given text in a variety of format codings, this function returns
-// the text as safe HTML.
-//
-// $text is raw text (originally from a user)
-// $format is one of the format constants, defined above
+/// Given text in a variety of format codings, this function returns
+/// the text as safe HTML.
+///
+/// $text is raw text (originally from a user)
+/// $format is one of the format constants, defined above
switch ($format) {
case FORMAT_MOODLE:
@@ -370,8 +385,8 @@ function format_text($text, $format, $options=NULL) {
function clean_text($text, $format) {
-// Given raw text (eg typed in by a user), this function cleans it up
-// and removes any nasty tags that could mess up Moodle pages.
+/// Given raw text (eg typed in by a user), this function cleans it up
+/// and removes any nasty tags that could mess up Moodle pages.
global $JAVASCRIPT_TAGS, $ALLOWED_TAGS;
@@ -393,38 +408,41 @@ function clean_text($text, $format) {
}
function replace_smilies($text) {
+/// Replaces all known smileys in the text with image equivalents
+
global $CFG, $SMILEY_TEXT, $SMILEY_IMAGE;
return str_replace($SMILEY_TEXT, $SMILEY_IMAGE, $text);
}
function text_to_html($text, $smiley=true, $para=true) {
-// Given plain text, makes it into HTML as nicely as possible.
-// May contain most HTML tags
+/// Given plain text, makes it into HTML as nicely as possible.
+/// May contain HTML tags already
- // Remove any whitespace that may be between HTML tags
+/// Remove any whitespace that may be between HTML tags
$text = eregi_replace(">([[:space:]]+)<", "><", $text);
- // Remove any returns that precede or follow HTML tags
+/// Remove any returns that precede or follow HTML tags
$text = eregi_replace("([\n\r])<", " <", $text);
$text = eregi_replace(">([\n\r])", "> ", $text);
- // Make lone URLs into links. eg http://moodle.com/
+/// Make lone URLs into links. eg http://moodle.com/
$text = eregi_replace("([\n\r ([])([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
"\\1<A HREF=\"\\2://\\3\\4\" TARGET=\"newpage\">\\2://\\3\\4</A>", $text);
- // eg www.moodle.com
+/// eg www.moodle.com
$text = eregi_replace("([[:space:]])www.([^[:space:]]*)([[:alnum:]#?/&=])",
"\\1<A HREF=\"http://www.\\2\\3\" TARGET=\"newpage\">www.\\2\\3</A>", $text);
- // Make returns into HTML newlines.
+/// Make returns into HTML newlines.
$text = nl2br($text);
- // Turn smileys into images.
+/// Turn smileys into images.
if ($smiley) {
$text = replace_smilies($text);
}
+/// Wrap the whole thing in a paragraph tag if required
if ($para) {
return "<P>".$text."</P>";
} else {
@@ -433,7 +451,7 @@ function text_to_html($text, $smiley=true, $para=true) {
}
function highlight($needle, $haystack) {
-// This function will highlight instances of $needle in $haystack
+/// This function will highlight instances of $needle in $haystack
$parts = explode(strtolower($needle), strtolower($haystack));

0 comments on commit c1d5710

Please sign in to comment.
Something went wrong with that request. Please try again.