PageRenderTime 75ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/moodle/lib/weblib.php

https://bitbucket.org/geek745/moodle-db2
PHP | 7102 lines | 5005 code | 725 blank | 1372 comment | 909 complexity | f692db5144ff53443dba58a43eee55fc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php // $Id$
  2. ///////////////////////////////////////////////////////////////////////////
  3. // //
  4. // NOTICE OF COPYRIGHT //
  5. // //
  6. // Moodle - Modular Object-Oriented Dynamic Learning Environment //
  7. // http://moodle.com //
  8. // //
  9. // Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
  10. // //
  11. // This program is free software; you can redistribute it and/or modify //
  12. // it under the terms of the GNU General Public License as published by //
  13. // the Free Software Foundation; either version 2 of the License, or //
  14. // (at your option) any later version. //
  15. // //
  16. // This program is distributed in the hope that it will be useful, //
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  19. // GNU General Public License for more details: //
  20. // //
  21. // http://www.gnu.org/copyleft/gpl.html //
  22. // //
  23. ///////////////////////////////////////////////////////////////////////////
  24. /**
  25. * Library of functions for web output
  26. *
  27. * Library of all general-purpose Moodle PHP functions and constants
  28. * that produce HTML output
  29. *
  30. * Other main libraries:
  31. * - datalib.php - functions that access the database.
  32. * - moodlelib.php - general-purpose Moodle functions.
  33. * @author Martin Dougiamas
  34. * @version $Id$
  35. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  36. * @package moodlecore
  37. */
  38. /// We are going to uses filterlib functions here
  39. require_once("$CFG->libdir/filterlib.php");
  40. require_once("$CFG->libdir/ajax/ajaxlib.php");
  41. /// Constants
  42. /// Define text formatting types ... eventually we can add Wiki, BBcode etc
  43. /**
  44. * Does all sorts of transformations and filtering
  45. */
  46. define('FORMAT_MOODLE', '0'); // Does all sorts of transformations and filtering
  47. /**
  48. * Plain HTML (with some tags stripped)
  49. */
  50. define('FORMAT_HTML', '1'); // Plain HTML (with some tags stripped)
  51. /**
  52. * Plain text (even tags are printed in full)
  53. */
  54. define('FORMAT_PLAIN', '2'); // Plain text (even tags are printed in full)
  55. /**
  56. * Wiki-formatted text
  57. * Deprecated: left here just to note that '3' is not used (at the moment)
  58. * and to catch any latent wiki-like text (which generates an error)
  59. */
  60. define('FORMAT_WIKI', '3'); // Wiki-formatted text
  61. /**
  62. * Markdown-formatted text http://daringfireball.net/projects/markdown/
  63. */
  64. define('FORMAT_MARKDOWN', '4'); // Markdown-formatted text http://daringfireball.net/projects/markdown/
  65. /**
  66. * TRUSTTEXT marker - if present in text, text cleaning should be bypassed
  67. */
  68. define('TRUSTTEXT', '#####TRUSTTEXT#####');
  69. /**
  70. * Javascript related defines
  71. */
  72. define('REQUIREJS_BEFOREHEADER', 0);
  73. define('REQUIREJS_INHEADER', 1);
  74. define('REQUIREJS_AFTERHEADER', 2);
  75. /**
  76. * Allowed tags - string of html tags that can be tested against for safe html tags
  77. * @global string $ALLOWED_TAGS
  78. */
  79. global $ALLOWED_TAGS;
  80. $ALLOWED_TAGS =
  81. '<p><br><b><i><u><font><table><tbody><thead><tfoot><span><div><tr><td><th><ol><ul><dl><li><dt><dd><h1><h2><h3><h4><h5><h6><hr><img><a><strong><emphasis><em><sup><sub><address><cite><blockquote><pre><strike><param><acronym><nolink><lang><tex><algebra><math><mi><mn><mo><mtext><mspace><ms><mrow><mfrac><msqrt><mroot><mstyle><merror><mpadded><mphantom><mfenced><msub><msup><msubsup><munder><mover><munderover><mmultiscripts><mtable><mtr><mtd><maligngroup><malignmark><maction><cn><ci><apply><reln><fn><interval><inverse><sep><condition><declare><lambda><compose><ident><quotient><exp><factorial><divide><max><min><minus><plus><power><rem><times><root><gcd><and><or><xor><not><implies><forall><exists><abs><conjugate><eq><neq><gt><lt><geq><leq><ln><log><int><diff><partialdiff><lowlimit><uplimit><bvar><degree><set><list><union><intersect><in><notin><subset><prsubset><notsubset><notprsubset><setdiff><sum><product><limit><tendsto><mean><sdev><variance><median><mode><moment><vector><matrix><matrixrow><determinant><transpose><selector><annotation><semantics><annotation-xml><tt><code>';
  82. /**
  83. * Allowed protocols - array of protocols that are safe to use in links and so on
  84. * @global string $ALLOWED_PROTOCOLS
  85. */
  86. $ALLOWED_PROTOCOLS = array('http', 'https', 'ftp', 'news', 'mailto', 'rtsp', 'teamspeak', 'gopher', 'mms',
  87. 'color', 'callto', 'cursor', 'text-align', 'font-size', 'font-weight', 'font-style', 'font-family',
  88. 'border', 'margin', 'padding', 'background', 'background-color', 'text-decoration'); // CSS as well to get through kses
  89. /// Functions
  90. /**
  91. * Add quotes to HTML characters
  92. *
  93. * Returns $var with HTML characters (like "<", ">", etc.) properly quoted.
  94. * This function is very similar to {@link p()}
  95. *
  96. * @param string $var the string potentially containing HTML characters
  97. * @param boolean $strip to decide if we want to strip slashes or no. Default to false.
  98. * true should be used to print data from forms and false for data from DB.
  99. * @return string
  100. */
  101. function s($var, $strip=false) {
  102. if ($var == '0') { // for integer 0, boolean false, string '0'
  103. return '0';
  104. }
  105. if ($strip) {
  106. return preg_replace("/&amp;(#\d+);/i", "&$1;", htmlspecialchars(stripslashes_safe($var)));
  107. } else {
  108. return preg_replace("/&amp;(#\d+);/i", "&$1;", htmlspecialchars($var));
  109. }
  110. }
  111. /**
  112. * Add quotes to HTML characters
  113. *
  114. * Prints $var with HTML characters (like "<", ">", etc.) properly quoted.
  115. * This function is very similar to {@link s()}
  116. *
  117. * @param string $var the string potentially containing HTML characters
  118. * @param boolean $strip to decide if we want to strip slashes or no. Default to false.
  119. * true should be used to print data from forms and false for data from DB.
  120. * @return string
  121. */
  122. function p($var, $strip=false) {
  123. echo s($var, $strip);
  124. }
  125. /**
  126. * Does proper javascript quoting.
  127. * Do not use addslashes anymore, because it does not work when magic_quotes_sybase is enabled.
  128. *
  129. * @since 1.8 - 22/02/2007
  130. * @param mixed value
  131. * @return mixed quoted result
  132. */
  133. function addslashes_js($var) {
  134. if (is_string($var)) {
  135. $var = str_replace('\\', '\\\\', $var);
  136. $var = str_replace(array('\'', '"', "\n", "\r", "\0"), array('\\\'', '\\"', '\\n', '\\r', '\\0'), $var);
  137. $var = str_replace('</', '<\/', $var); // XHTML compliance
  138. } else if (is_array($var)) {
  139. $var = array_map('addslashes_js', $var);
  140. } else if (is_object($var)) {
  141. $a = get_object_vars($var);
  142. foreach ($a as $key=>$value) {
  143. $a[$key] = addslashes_js($value);
  144. }
  145. $var = (object)$a;
  146. }
  147. return $var;
  148. }
  149. /**
  150. * Remove query string from url
  151. *
  152. * Takes in a URL and returns it without the querystring portion
  153. *
  154. * @param string $url the url which may have a query string attached
  155. * @return string
  156. */
  157. function strip_querystring($url) {
  158. if ($commapos = strpos($url, '?')) {
  159. return substr($url, 0, $commapos);
  160. } else {
  161. return $url;
  162. }
  163. }
  164. /**
  165. * Returns the URL of the HTTP_REFERER, less the querystring portion if required
  166. * @param boolean $stripquery if true, also removes the query part of the url.
  167. * @return string
  168. */
  169. function get_referer($stripquery=true) {
  170. if (isset($_SERVER['HTTP_REFERER'])) {
  171. if ($stripquery) {
  172. return strip_querystring($_SERVER['HTTP_REFERER']);
  173. } else {
  174. return $_SERVER['HTTP_REFERER'];
  175. }
  176. } else {
  177. return '';
  178. }
  179. }
  180. /**
  181. * Returns the name of the current script, WITH the querystring portion.
  182. * this function is necessary because PHP_SELF and REQUEST_URI and SCRIPT_NAME
  183. * return different things depending on a lot of things like your OS, Web
  184. * server, and the way PHP is compiled (ie. as a CGI, module, ISAPI, etc.)
  185. * <b>NOTE:</b> This function returns false if the global variables needed are not set.
  186. *
  187. * @return string
  188. */
  189. function me() {
  190. if (!empty($_SERVER['REQUEST_URI'])) {
  191. return $_SERVER['REQUEST_URI'];
  192. } else if (!empty($_SERVER['PHP_SELF'])) {
  193. if (!empty($_SERVER['QUERY_STRING'])) {
  194. return $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
  195. }
  196. return $_SERVER['PHP_SELF'];
  197. } else if (!empty($_SERVER['SCRIPT_NAME'])) {
  198. if (!empty($_SERVER['QUERY_STRING'])) {
  199. return $_SERVER['SCRIPT_NAME'] .'?'. $_SERVER['QUERY_STRING'];
  200. }
  201. return $_SERVER['SCRIPT_NAME'];
  202. } else if (!empty($_SERVER['URL'])) { // May help IIS (not well tested)
  203. if (!empty($_SERVER['QUERY_STRING'])) {
  204. return $_SERVER['URL'] .'?'. $_SERVER['QUERY_STRING'];
  205. }
  206. return $_SERVER['URL'];
  207. } else {
  208. notify('Warning: Could not find any of these web server variables: $REQUEST_URI, $PHP_SELF, $SCRIPT_NAME or $URL');
  209. return false;
  210. }
  211. }
  212. /**
  213. * Like {@link me()} but returns a full URL
  214. * @see me()
  215. * @return string
  216. */
  217. function qualified_me() {
  218. global $CFG;
  219. if (!empty($CFG->wwwroot)) {
  220. $url = parse_url($CFG->wwwroot);
  221. }
  222. if (!empty($url['host'])) {
  223. $hostname = $url['host'];
  224. } else if (!empty($_SERVER['SERVER_NAME'])) {
  225. $hostname = $_SERVER['SERVER_NAME'];
  226. } else if (!empty($_ENV['SERVER_NAME'])) {
  227. $hostname = $_ENV['SERVER_NAME'];
  228. } else if (!empty($_SERVER['HTTP_HOST'])) {
  229. $hostname = $_SERVER['HTTP_HOST'];
  230. } else if (!empty($_ENV['HTTP_HOST'])) {
  231. $hostname = $_ENV['HTTP_HOST'];
  232. } else {
  233. notify('Warning: could not find the name of this server!');
  234. return false;
  235. }
  236. if (!empty($url['port'])) {
  237. $hostname .= ':'.$url['port'];
  238. } else if (!empty($_SERVER['SERVER_PORT'])) {
  239. if ($_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 443) {
  240. $hostname .= ':'.$_SERVER['SERVER_PORT'];
  241. }
  242. }
  243. // TODO, this does not work in the situation described in MDL-11061, but
  244. // I don't know how to fix it. Possibly believe $CFG->wwwroot ahead of what
  245. // the server reports.
  246. if (isset($_SERVER['HTTPS'])) {
  247. $protocol = ($_SERVER['HTTPS'] == 'on') ? 'https://' : 'http://';
  248. } else if (isset($_SERVER['SERVER_PORT'])) { # Apache2 does not export $_SERVER['HTTPS']
  249. $protocol = ($_SERVER['SERVER_PORT'] == '443') ? 'https://' : 'http://';
  250. } else {
  251. $protocol = 'http://';
  252. }
  253. $url_prefix = $protocol.$hostname;
  254. return $url_prefix . me();
  255. }
  256. /**
  257. * Class for creating and manipulating urls.
  258. *
  259. * See short write up here http://docs.moodle.org/en/Development:lib/weblib.php_moodle_url
  260. */
  261. class moodle_url {
  262. var $scheme = '';// e.g. http
  263. var $host = '';
  264. var $port = '';
  265. var $user = '';
  266. var $pass = '';
  267. var $path = '';
  268. var $fragment = '';
  269. var $params = array(); //associative array of query string params
  270. /**
  271. * Pass no arguments to create a url that refers to this page. Use empty string to create empty url.
  272. *
  273. * @param string $url url default null means use this page url with no query string
  274. * empty string means empty url.
  275. * if you pass any other type of url it will be parsed into it's bits, including query string
  276. * @param array $params these params override anything in the query string where params have the same name.
  277. */
  278. function moodle_url($url = null, $params = array()){
  279. global $FULLME;
  280. if ($url !== ''){
  281. if ($url === null){
  282. $url = strip_querystring($FULLME);
  283. }
  284. $parts = parse_url($url);
  285. if ($parts === FALSE){
  286. error('invalidurl');
  287. }
  288. if (isset($parts['query'])){
  289. parse_str(str_replace('&amp;', '&', $parts['query']), $this->params);
  290. }
  291. unset($parts['query']);
  292. foreach ($parts as $key => $value){
  293. $this->$key = $value;
  294. }
  295. $this->params($params);
  296. }
  297. }
  298. /**
  299. * Add an array of params to the params for this page.
  300. *
  301. * The added params override existing ones if they have the same name.
  302. *
  303. * @param array $params Defaults to null. If null then return value of param 'name'.
  304. * @return array Array of Params for url.
  305. */
  306. function params($params = null) {
  307. if (!is_null($params)) {
  308. return $this->params = $params + $this->params;
  309. } else {
  310. return $this->params;
  311. }
  312. }
  313. /**
  314. * Remove all params if no arguments passed. Or else remove param $arg1, $arg2, etc.
  315. *
  316. * @param string $arg1
  317. * @param string $arg2
  318. * @param string $arg3
  319. */
  320. function remove_params(){
  321. if ($thisargs = func_get_args()){
  322. foreach ($thisargs as $arg){
  323. if (isset($this->params[$arg])){
  324. unset($this->params[$arg]);
  325. }
  326. }
  327. } else { // no args
  328. $this->params = array();
  329. }
  330. }
  331. /**
  332. * Add a param to the params for this page. The added param overrides existing one if they
  333. * have the same name.
  334. *
  335. * @param string $paramname name
  336. * @param string $param value
  337. */
  338. function param($paramname, $param){
  339. $this->params = array($paramname => $param) + $this->params;
  340. }
  341. function get_query_string($overrideparams = array()){
  342. $arr = array();
  343. $params = $overrideparams + $this->params;
  344. foreach ($params as $key => $val){
  345. $arr[] = urlencode($key)."=".urlencode($val);
  346. }
  347. return implode($arr, "&amp;");
  348. }
  349. /**
  350. * Outputs params as hidden form elements.
  351. *
  352. * @param array $exclude params to ignore
  353. * @param integer $indent indentation
  354. * @param array $overrideparams params to add to the output params, these
  355. * override existing ones with the same name.
  356. * @return string html for form elements.
  357. */
  358. function hidden_params_out($exclude = array(), $indent = 0, $overrideparams=array()){
  359. $tabindent = str_repeat("\t", $indent);
  360. $str = '';
  361. $params = $overrideparams + $this->params;
  362. foreach ($params as $key => $val){
  363. if (FALSE === array_search($key, $exclude)) {
  364. $val = s($val);
  365. $str.= "$tabindent<input type=\"hidden\" name=\"$key\" value=\"$val\" />\n";
  366. }
  367. }
  368. return $str;
  369. }
  370. /**
  371. * Output url
  372. *
  373. * @param boolean $noquerystring whether to output page params as a query string in the url.
  374. * @param array $overrideparams params to add to the output url, these override existing ones with the same name.
  375. * @return string url
  376. */
  377. function out($noquerystring = false, $overrideparams = array()) {
  378. $uri = $this->scheme ? $this->scheme.':'.((strtolower($this->scheme) == 'mailto') ? '':'//'): '';
  379. $uri .= $this->user ? $this->user.($this->pass? ':'.$this->pass:'').'@':'';
  380. $uri .= $this->host ? $this->host : '';
  381. $uri .= $this->port ? ':'.$this->port : '';
  382. $uri .= $this->path ? $this->path : '';
  383. if (!$noquerystring){
  384. $uri .= (count($this->params)||count($overrideparams)) ? '?'.$this->get_query_string($overrideparams) : '';
  385. }
  386. $uri .= $this->fragment ? '#'.$this->fragment : '';
  387. return $uri;
  388. }
  389. /**
  390. * Output action url with sesskey
  391. *
  392. * @param boolean $noquerystring whether to output page params as a query string in the url.
  393. * @return string url
  394. */
  395. function out_action($overrideparams = array()) {
  396. $overrideparams = array('sesskey'=> sesskey()) + $overrideparams;
  397. return $this->out(false, $overrideparams);
  398. }
  399. }
  400. /**
  401. * Determine if there is data waiting to be processed from a form
  402. *
  403. * Used on most forms in Moodle to check for data
  404. * Returns the data as an object, if it's found.
  405. * This object can be used in foreach loops without
  406. * casting because it's cast to (array) automatically
  407. *
  408. * Checks that submitted POST data exists and returns it as object.
  409. *
  410. * @param string $url not used anymore
  411. * @return mixed false or object
  412. */
  413. function data_submitted($url='') {
  414. if (empty($_POST)) {
  415. return false;
  416. } else {
  417. return (object)$_POST;
  418. }
  419. }
  420. /**
  421. * Moodle replacement for php stripslashes() function,
  422. * works also for objects and arrays.
  423. *
  424. * The standard php stripslashes() removes ALL backslashes
  425. * even from strings - so C:\temp becomes C:temp - this isn't good.
  426. * This function should work as a fairly safe replacement
  427. * to be called on quoted AND unquoted strings (to be sure)
  428. *
  429. * @param mixed something to remove unsafe slashes from
  430. * @return mixed
  431. */
  432. function stripslashes_safe($mixed) {
  433. // there is no need to remove slashes from int, float and bool types
  434. if (empty($mixed)) {
  435. //nothing to do...
  436. } else if (is_string($mixed)) {
  437. if (ini_get_bool('magic_quotes_sybase')) { //only unescape single quotes
  438. $mixed = str_replace("''", "'", $mixed);
  439. } else { //the rest, simple and double quotes and backslashes
  440. $mixed = str_replace("\\'", "'", $mixed);
  441. $mixed = str_replace('\\"', '"', $mixed);
  442. $mixed = str_replace('\\\\', '\\', $mixed);
  443. }
  444. } else if (is_array($mixed)) {
  445. foreach ($mixed as $key => $value) {
  446. $mixed[$key] = stripslashes_safe($value);
  447. }
  448. } else if (is_object($mixed)) {
  449. $vars = get_object_vars($mixed);
  450. foreach ($vars as $key => $value) {
  451. $mixed->$key = stripslashes_safe($value);
  452. }
  453. }
  454. return $mixed;
  455. }
  456. /**
  457. * Recursive implementation of stripslashes()
  458. *
  459. * This function will allow you to strip the slashes from a variable.
  460. * If the variable is an array or object, slashes will be stripped
  461. * from the items (or properties) it contains, even if they are arrays
  462. * or objects themselves.
  463. *
  464. * @param mixed the variable to remove slashes from
  465. * @return mixed
  466. */
  467. function stripslashes_recursive($var) {
  468. if (is_object($var)) {
  469. $new_var = new object();
  470. $properties = get_object_vars($var);
  471. foreach($properties as $property => $value) {
  472. $new_var->$property = stripslashes_recursive($value);
  473. }
  474. } else if(is_array($var)) {
  475. $new_var = array();
  476. foreach($var as $property => $value) {
  477. $new_var[$property] = stripslashes_recursive($value);
  478. }
  479. } else if(is_string($var)) {
  480. $new_var = stripslashes($var);
  481. } else {
  482. $new_var = $var;
  483. }
  484. return $new_var;
  485. }
  486. /**
  487. * Recursive implementation of addslashes()
  488. *
  489. * This function will allow you to add the slashes from a variable.
  490. * If the variable is an array or object, slashes will be added
  491. * to the items (or properties) it contains, even if they are arrays
  492. * or objects themselves.
  493. *
  494. * @param mixed the variable to add slashes from
  495. * @return mixed
  496. */
  497. function addslashes_recursive($var) {
  498. if (is_object($var)) {
  499. $new_var = new object();
  500. $properties = get_object_vars($var);
  501. foreach($properties as $property => $value) {
  502. $new_var->$property = addslashes_recursive($value);
  503. }
  504. } else if (is_array($var)) {
  505. $new_var = array();
  506. foreach($var as $property => $value) {
  507. $new_var[$property] = addslashes_recursive($value);
  508. }
  509. } else if (is_string($var)) {
  510. $new_var = addslashes($var);
  511. } else { // nulls, integers, etc.
  512. $new_var = $var;
  513. }
  514. return $new_var;
  515. }
  516. /**
  517. * Given some normal text this function will break up any
  518. * long words to a given size by inserting the given character
  519. *
  520. * It's multibyte savvy and doesn't change anything inside html tags.
  521. *
  522. * @param string $string the string to be modified
  523. * @param int $maxsize maximum length of the string to be returned
  524. * @param string $cutchar the string used to represent word breaks
  525. * @return string
  526. */
  527. function break_up_long_words($string, $maxsize=20, $cutchar=' ') {
  528. /// Loading the textlib singleton instance. We are going to need it.
  529. $textlib = textlib_get_instance();
  530. /// First of all, save all the tags inside the text to skip them
  531. $tags = array();
  532. filter_save_tags($string,$tags);
  533. /// Process the string adding the cut when necessary
  534. $output = '';
  535. $length = $textlib->strlen($string);
  536. $wordlength = 0;
  537. for ($i=0; $i<$length; $i++) {
  538. $char = $textlib->substr($string, $i, 1);
  539. if ($char == ' ' or $char == "\t" or $char == "\n" or $char == "\r" or $char == "<" or $char == ">") {
  540. $wordlength = 0;
  541. } else {
  542. $wordlength++;
  543. if ($wordlength > $maxsize) {
  544. $output .= $cutchar;
  545. $wordlength = 0;
  546. }
  547. }
  548. $output .= $char;
  549. }
  550. /// Finally load the tags back again
  551. if (!empty($tags)) {
  552. $output = str_replace(array_keys($tags), $tags, $output);
  553. }
  554. return $output;
  555. }
  556. /**
  557. * This does a search and replace, ignoring case
  558. * This function is only used for versions of PHP older than version 5
  559. * which do not have a native version of this function.
  560. * Taken from the PHP manual, by bradhuizenga @ softhome.net
  561. *
  562. * @param string $find the string to search for
  563. * @param string $replace the string to replace $find with
  564. * @param string $string the string to search through
  565. * return string
  566. */
  567. if (!function_exists('str_ireplace')) { /// Only exists in PHP 5
  568. function str_ireplace($find, $replace, $string) {
  569. if (!is_array($find)) {
  570. $find = array($find);
  571. }
  572. if(!is_array($replace)) {
  573. if (!is_array($find)) {
  574. $replace = array($replace);
  575. } else {
  576. // this will duplicate the string into an array the size of $find
  577. $c = count($find);
  578. $rString = $replace;
  579. unset($replace);
  580. for ($i = 0; $i < $c; $i++) {
  581. $replace[$i] = $rString;
  582. }
  583. }
  584. }
  585. foreach ($find as $fKey => $fItem) {
  586. $between = explode(strtolower($fItem),strtolower($string));
  587. $pos = 0;
  588. foreach($between as $bKey => $bItem) {
  589. $between[$bKey] = substr($string,$pos,strlen($bItem));
  590. $pos += strlen($bItem) + strlen($fItem);
  591. }
  592. $string = implode($replace[$fKey],$between);
  593. }
  594. return ($string);
  595. }
  596. }
  597. /**
  598. * Locate the position of a string in another string
  599. *
  600. * This function is only used for versions of PHP older than version 5
  601. * which do not have a native version of this function.
  602. * Taken from the PHP manual, by dmarsh @ spscc.ctc.edu
  603. *
  604. * @param string $haystack The string to be searched
  605. * @param string $needle The string to search for
  606. * @param int $offset The position in $haystack where the search should begin.
  607. */
  608. if (!function_exists('stripos')) { /// Only exists in PHP 5
  609. function stripos($haystack, $needle, $offset=0) {
  610. return strpos(strtoupper($haystack), strtoupper($needle), $offset);
  611. }
  612. }
  613. /**
  614. * This function will print a button/link/etc. form element
  615. * that will work on both Javascript and non-javascript browsers.
  616. * Relies on the Javascript function openpopup in javascript.php
  617. *
  618. * All parameters default to null, only $type and $url are mandatory.
  619. *
  620. * $url must be relative to home page eg /mod/survey/stuff.php
  621. * @param string $url Web link relative to home page
  622. * @param string $name Name to be assigned to the popup window (this is used by
  623. * client-side scripts to "talk" to the popup window)
  624. * @param string $linkname Text to be displayed as web link
  625. * @param int $height Height to assign to popup window
  626. * @param int $width Height to assign to popup window
  627. * @param string $title Text to be displayed as popup page title
  628. * @param string $options List of additional options for popup window
  629. * @param string $return If true, return as a string, otherwise print
  630. * @param string $id id added to the element
  631. * @param string $class class added to the element
  632. * @return string
  633. * @uses $CFG
  634. */
  635. function element_to_popup_window ($type=null, $url=null, $name=null, $linkname=null,
  636. $height=400, $width=500, $title=null,
  637. $options=null, $return=false, $id=null, $class=null) {
  638. if (is_null($url)) {
  639. debugging('You must give the url to display in the popup. URL is missing - can\'t create popup window.', DEBUG_DEVELOPER);
  640. }
  641. global $CFG;
  642. if ($options == 'none') { // 'none' is legacy, should be removed in v2.0
  643. $options = null;
  644. }
  645. // add some sane default options for popup windows
  646. if (!$options) {
  647. $options = 'menubar=0,location=0,scrollbars,resizable';
  648. }
  649. if ($width) {
  650. $options .= ',width='. $width;
  651. }
  652. if ($height) {
  653. $options .= ',height='. $height;
  654. }
  655. if ($id) {
  656. $id = ' id="'.$id.'" ';
  657. }
  658. if ($class) {
  659. $class = ' class="'.$class.'" ';
  660. }
  661. if ($name) {
  662. $_name = $name;
  663. if (($name = preg_replace("/\s/", '_', $name)) != $_name) {
  664. debugging('The $name of a popup window shouldn\'t contain spaces - string modified. '. $_name .' changed to '. $name, DEBUG_DEVELOPER);
  665. }
  666. } else {
  667. $name = 'popup';
  668. }
  669. // get some default string, using the localized version of legacy defaults
  670. if (is_null($linkname) || $linkname === '') {
  671. $linkname = get_string('clickhere');
  672. }
  673. if (!$title) {
  674. $title = get_string('popupwindowname');
  675. }
  676. $fullscreen = 0; // must be passed to openpopup
  677. $element = '';
  678. switch ($type) {
  679. case 'button' :
  680. $element = '<input type="button" name="'. $name .'" title="'. $title .'" value="'. $linkname .'" '. $id . $class .
  681. "onclick=\"return openpopup('$url', '$name', '$options', $fullscreen);\" />\n";
  682. break;
  683. case 'link' :
  684. // some log url entries contain _SERVER[HTTP_REFERRER] in which case wwwroot is already there.
  685. if (!(strpos($url,$CFG->wwwroot) === false)) {
  686. $url = substr($url, strlen($CFG->wwwroot));
  687. }
  688. $element = '<a title="'. s(strip_tags($title)) .'" href="'. $CFG->wwwroot . $url .'" '.
  689. "$CFG->frametarget onclick=\"this.target='$name'; return openpopup('$url', '$name', '$options', $fullscreen);\">$linkname</a>";
  690. break;
  691. default :
  692. error('Undefined element - can\'t create popup window.');
  693. break;
  694. }
  695. if ($return) {
  696. return $element;
  697. } else {
  698. echo $element;
  699. }
  700. }
  701. /**
  702. * Creates and displays (or returns) a link to a popup window, using element_to_popup_window function.
  703. *
  704. * @return string html code to display a link to a popup window.
  705. * @see element_to_popup_window()
  706. */
  707. function link_to_popup_window ($url, $name=null, $linkname=null,
  708. $height=400, $width=500, $title=null,
  709. $options=null, $return=false) {
  710. return element_to_popup_window('link', $url, $name, $linkname, $height, $width, $title, $options, $return, null, null);
  711. }
  712. /**
  713. * Creates and displays (or returns) a buttons to a popup window, using element_to_popup_window function.
  714. *
  715. * @return string html code to display a button to a popup window.
  716. * @see element_to_popup_window()
  717. */
  718. function button_to_popup_window ($url, $name=null, $linkname=null,
  719. $height=400, $width=500, $title=null, $options=null, $return=false,
  720. $id=null, $class=null) {
  721. return element_to_popup_window('button', $url, $name, $linkname, $height, $width, $title, $options, $return, $id, $class);
  722. }
  723. /**
  724. * Prints a simple button to close a window
  725. * @param string $name name of the window to close
  726. * @param boolean $return whether this function should return a string or output it
  727. * @return string if $return is true, nothing otherwise
  728. */
  729. function close_window_button($name='closewindow', $return=false) {
  730. global $CFG;
  731. $output = '';
  732. $output .= '<div class="closewindow">' . "\n";
  733. $output .= '<form action="#"><div>';
  734. $output .= '<input type="button" onclick="self.close();" value="'.get_string($name).'" />';
  735. $output .= '</div></form>';
  736. $output .= '</div>' . "\n";
  737. if ($return) {
  738. return $output;
  739. } else {
  740. echo $output;
  741. }
  742. }
  743. /*
  744. * Try and close the current window immediately using Javascript
  745. * @param int $delay the delay in seconds before closing the window
  746. */
  747. function close_window($delay=0) {
  748. ?>
  749. <script type="text/javascript">
  750. //<![CDATA[
  751. function close_this_window() {
  752. self.close();
  753. }
  754. setTimeout("close_this_window()", <?php echo $delay * 1000 ?>);
  755. //]]>
  756. </script>
  757. <noscript><center>
  758. <?php print_string('pleaseclose') ?>
  759. </center></noscript>
  760. <?php
  761. die;
  762. }
  763. /**
  764. * Given an array of values, output the HTML for a select element with those options.
  765. * Normally, you only need to use the first few parameters.
  766. *
  767. * @param array $options The options to offer. An array of the form
  768. * $options[{value}] = {text displayed for that option};
  769. * @param string $name the name of this form control, as in &lt;select name="..." ...
  770. * @param string $selected the option to select initially, default none.
  771. * @param string $nothing The label for the 'nothing is selected' option. Defaults to get_string('choose').
  772. * Set this to '' if you don't want a 'nothing is selected' option.
  773. * @param string $script in not '', then this is added to the &lt;select> element as an onchange handler.
  774. * @param string $nothingvalue The value corresponding to the $nothing option. Defaults to 0.
  775. * @param boolean $return if false (the default) the the output is printed directly, If true, the
  776. * generated HTML is returned as a string.
  777. * @param boolean $disabled if true, the select is generated in a disabled state. Default, false.
  778. * @param int $tabindex if give, sets the tabindex attribute on the &lt;select> element. Default none.
  779. * @param string $id value to use for the id attribute of the &lt;select> element. If none is given,
  780. * then a suitable one is constructed.
  781. * @param mixed $listbox if false, display as a dropdown menu. If true, display as a list box.
  782. * By default, the list box will have a number of rows equal to min(10, count($options)), but if
  783. * $listbox is an integer, that number is used for size instead.
  784. * @param boolean $multiple if true, enable multiple selections, else only 1 item can be selected. Used
  785. * when $listbox display is enabled
  786. * @param string $class value to use for the class attribute of the &lt;select> element. If none is given,
  787. * then a suitable one is constructed.
  788. */
  789. function choose_from_menu ($options, $name, $selected='', $nothing='choose', $script='',
  790. $nothingvalue='0', $return=false, $disabled=false, $tabindex=0,
  791. $id='', $listbox=false, $multiple=false, $class='') {
  792. if ($nothing == 'choose') {
  793. $nothing = get_string('choose') .'...';
  794. }
  795. $attributes = ($script) ? 'onchange="'. $script .'"' : '';
  796. if ($disabled) {
  797. $attributes .= ' disabled="disabled"';
  798. }
  799. if ($tabindex) {
  800. $attributes .= ' tabindex="'.$tabindex.'"';
  801. }
  802. if ($id ==='') {
  803. $id = 'menu'.$name;
  804. // name may contaion [], which would make an invalid id. e.g. numeric question type editing form, assignment quickgrading
  805. $id = str_replace('[', '', $id);
  806. $id = str_replace(']', '', $id);
  807. }
  808. if ($class ==='') {
  809. $class = 'menu'.$name;
  810. // name may contaion [], which would make an invalid class. e.g. numeric question type editing form, assignment quickgrading
  811. $class = str_replace('[', '', $class);
  812. $class = str_replace(']', '', $class);
  813. }
  814. $class = 'select ' . $class; /// Add 'select' selector always
  815. if ($listbox) {
  816. if (is_integer($listbox)) {
  817. $size = $listbox;
  818. } else {
  819. $numchoices = count($options);
  820. if ($nothing) {
  821. $numchoices += 1;
  822. }
  823. $size = min(10, $numchoices);
  824. }
  825. $attributes .= ' size="' . $size . '"';
  826. if ($multiple) {
  827. $attributes .= ' multiple="multiple"';
  828. }
  829. }
  830. $output = '<select id="'. $id .'" class="'. $class .'" name="'. $name .'" '. $attributes .'>' . "\n";
  831. if ($nothing) {
  832. $output .= ' <option value="'. s($nothingvalue) .'"'. "\n";
  833. if ($nothingvalue === $selected) {
  834. $output .= ' selected="selected"';
  835. }
  836. $output .= '>'. $nothing .'</option>' . "\n";
  837. }
  838. if (!empty($options)) {
  839. foreach ($options as $value => $label) {
  840. $output .= ' <option value="'. s($value) .'"';
  841. if ((string)$value == (string)$selected ||
  842. (is_array($selected) && in_array($value, $selected))) {
  843. $output .= ' selected="selected"';
  844. }
  845. if ($label === '') {
  846. $output .= '>'. $value .'</option>' . "\n";
  847. } else {
  848. $output .= '>'. $label .'</option>' . "\n";
  849. }
  850. }
  851. }
  852. $output .= '</select>' . "\n";
  853. if ($return) {
  854. return $output;
  855. } else {
  856. echo $output;
  857. }
  858. }
  859. /**
  860. * Choose value 0 or 1 from a menu with options 'No' and 'Yes'.
  861. * Other options like choose_from_menu.
  862. * @param string $name
  863. * @param string $selected
  864. * @param string $string (defaults to '')
  865. * @param boolean $return whether this function should return a string or output it (defaults to false)
  866. * @param boolean $disabled (defaults to false)
  867. * @param int $tabindex
  868. */
  869. function choose_from_menu_yesno($name, $selected, $script = '',
  870. $return = false, $disabled = false, $tabindex = 0) {
  871. return choose_from_menu(array(get_string('no'), get_string('yes')), $name,
  872. $selected, '', $script, '0', $return, $disabled, $tabindex);
  873. }
  874. /**
  875. * Just like choose_from_menu, but takes a nested array (2 levels) and makes a dropdown menu
  876. * including option headings with the first level.
  877. */
  878. function choose_from_menu_nested($options,$name,$selected='',$nothing='choose',$script = '',
  879. $nothingvalue=0,$return=false,$disabled=false,$tabindex=0) {
  880. if ($nothing == 'choose') {
  881. $nothing = get_string('choose') .'...';
  882. }
  883. $attributes = ($script) ? 'onchange="'. $script .'"' : '';
  884. if ($disabled) {
  885. $attributes .= ' disabled="disabled"';
  886. }
  887. if ($tabindex) {
  888. $attributes .= ' tabindex="'.$tabindex.'"';
  889. }
  890. $output = '<select id="menu'.$name.'" name="'. $name .'" '. $attributes .'>' . "\n";
  891. if ($nothing) {
  892. $output .= ' <option value="'. $nothingvalue .'"'. "\n";
  893. if ($nothingvalue === $selected) {
  894. $output .= ' selected="selected"';
  895. }
  896. $output .= '>'. $nothing .'</option>' . "\n";
  897. }
  898. if (!empty($options)) {
  899. foreach ($options as $section => $values) {
  900. $output .= ' <optgroup label="'. s(format_string($section)) .'">'."\n";
  901. foreach ($values as $value => $label) {
  902. $output .= ' <option value="'. format_string($value) .'"';
  903. if ((string)$value == (string)$selected) {
  904. $output .= ' selected="selected"';
  905. }
  906. if ($label === '') {
  907. $output .= '>'. $value .'</option>' . "\n";
  908. } else {
  909. $output .= '>'. $label .'</option>' . "\n";
  910. }
  911. }
  912. $output .= ' </optgroup>'."\n";
  913. }
  914. }
  915. $output .= '</select>' . "\n";
  916. if ($return) {
  917. return $output;
  918. } else {
  919. echo $output;
  920. }
  921. }
  922. /**
  923. * Given an array of values, creates a group of radio buttons to be part of a form
  924. *
  925. * @param array $options An array of value-label pairs for the radio group (values as keys)
  926. * @param string $name Name of the radiogroup (unique in the form)
  927. * @param string $checked The value that is already checked
  928. */
  929. function choose_from_radio ($options, $name, $checked='', $return=false) {
  930. static $idcounter = 0;
  931. if (!$name) {
  932. $name = 'unnamed';
  933. }
  934. $output = '<span class="radiogroup '.$name."\">\n";
  935. if (!empty($options)) {
  936. $currentradio = 0;
  937. foreach ($options as $value => $label) {
  938. $htmlid = 'auto-rb'.sprintf('%04d', ++$idcounter);
  939. $output .= ' <span class="radioelement '.$name.' rb'.$currentradio."\">";
  940. $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="radio" value="'.$value.'"';
  941. if ($value == $checked) {
  942. $output .= ' checked="checked"';
  943. }
  944. if ($label === '') {
  945. $output .= ' /> <label for="'.$htmlid.'">'. $value .'</label></span>' . "\n";
  946. } else {
  947. $output .= ' /> <label for="'.$htmlid.'">'. $label .'</label></span>' . "\n";
  948. }
  949. $currentradio = ($currentradio + 1) % 2;
  950. }
  951. }
  952. $output .= '</span>' . "\n";
  953. if ($return) {
  954. return $output;
  955. } else {
  956. echo $output;
  957. }
  958. }
  959. /** Display an standard html checkbox with an optional label
  960. *
  961. * @param string $name The name of the checkbox
  962. * @param string $value The valus that the checkbox will pass when checked
  963. * @param boolean $checked The flag to tell the checkbox initial state
  964. * @param string $label The label to be showed near the checkbox
  965. * @param string $alt The info to be inserted in the alt tag
  966. */
  967. function print_checkbox ($name, $value, $checked = true, $label = '', $alt = '', $script='',$return=false) {
  968. static $idcounter = 0;
  969. if (!$name) {
  970. $name = 'unnamed';
  971. }
  972. if ($alt) {
  973. $alt = strip_tags($alt);
  974. } else {
  975. $alt = 'checkbox';
  976. }
  977. if ($checked) {
  978. $strchecked = ' checked="checked"';
  979. } else {
  980. $strchecked = '';
  981. }
  982. $htmlid = 'auto-cb'.sprintf('%04d', ++$idcounter);
  983. $output = '<span class="checkbox '.$name."\">";
  984. $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="checkbox" value="'.$value.'" alt="'.$alt.'"'.$strchecked.' '.((!empty($script)) ? ' onclick="'.$script.'" ' : '').' />';
  985. if(!empty($label)) {
  986. $output .= ' <label for="'.$htmlid.'">'.$label.'</label>';
  987. }
  988. $output .= '</span>'."\n";
  989. if (empty($return)) {
  990. echo $output;
  991. } else {
  992. return $output;
  993. }
  994. }
  995. /** Display an standard html text field with an optional label
  996. *
  997. * @param string $name The name of the text field
  998. * @param string $value The value of the text field
  999. * @param string $label The label to be showed near the text field
  1000. * @param string $alt The info to be inserted in the alt tag
  1001. */
  1002. function print_textfield ($name, $value, $alt = '',$size=50,$maxlength=0, $return=false) {
  1003. static $idcounter = 0;
  1004. if (empty($name)) {
  1005. $name = 'unnamed';
  1006. }
  1007. if (empty($alt)) {
  1008. $alt = 'textfield';
  1009. }
  1010. if (!empty($maxlength)) {
  1011. $maxlength = ' maxlength="'.$maxlength.'" ';
  1012. }
  1013. $htmlid = 'auto-tf'.sprintf('%04d', ++$idcounter);
  1014. $output = '<span class="textfield '.$name."\">";
  1015. $output .= '<input name="'.$name.'" id="'.$htmlid.'" type="text" value="'.$value.'" size="'.$size.'" '.$maxlength.' alt="'.$alt.'" />';
  1016. $output .= '</span>'."\n";
  1017. if (empty($return)) {
  1018. echo $output;
  1019. } else {
  1020. return $output;
  1021. }
  1022. }
  1023. /**
  1024. * Implements a complete little popup form
  1025. *
  1026. * @uses $CFG
  1027. * @param string $common The URL up to the point of the variable that changes
  1028. * @param array $options Alist of value-label pairs for the popup list
  1029. * @param string $formid Id must be unique on the page (originaly $formname)
  1030. * @param string $selected The option that is already selected
  1031. * @param string $nothing The label for the "no choice" option
  1032. * @param string $help The name of a help page if help is required
  1033. * @param string $helptext The name of the label for the help button
  1034. * @param boolean $return Indicates whether the function should return the text
  1035. * as a string or echo it directly to the page being rendered
  1036. * @param string $targetwindow The name of the target page to open the linked page in.
  1037. * @param string $selectlabel Text to place in a [label] element - preferred for accessibility.
  1038. * @param array $optionsextra TODO, an array?
  1039. * @param mixed $gobutton If set, this turns off the JavaScript and uses a 'go'
  1040. * button instead (as is always included for JS-disabled users). Set to true
  1041. * for a literal 'Go' button, or to a string to change the name of the button.
  1042. * @return string If $return is true then the entire form is returned as a string.
  1043. * @todo Finish documenting this function<br>
  1044. */
  1045. function popup_form($common, $options, $formid, $selected='', $nothing='choose', $help='', $helptext='', $return=false,
  1046. $targetwindow='self', $selectlabel='', $optionsextra=NULL, $gobutton=NULL) {
  1047. global $CFG;
  1048. static $go, $choose; /// Locally cached, in case there's lots on a page
  1049. if (empty($options)) {
  1050. return '';
  1051. }
  1052. if (!isset($go)) {
  1053. $go = get_string('go');
  1054. }
  1055. if ($nothing == 'choose') {
  1056. if (!isset($choose)) {
  1057. $choose = get_string('choose');
  1058. }
  1059. $nothing = $choose.'...';
  1060. }
  1061. // changed reference to document.getElementById('id_abc') instead of document.abc
  1062. // MDL-7861
  1063. $output = '<form action="'.$CFG->wwwroot.'/course/jumpto.php"'.
  1064. ' method="get" '.
  1065. $CFG->frametarget.
  1066. ' id="'.$formid.'"'.
  1067. ' class="popupform">';
  1068. if ($help) {
  1069. $button = helpbutton($help, $helptext, 'moodle', true, false, '', true);
  1070. } else {
  1071. $button = '';
  1072. }
  1073. if ($selectlabel) {
  1074. $selectlabel = '<label for="'.$formid.'_jump">'.$selectlabel.'</label>';
  1075. }
  1076. if ($gobutton) {
  1077. // Using the no-JavaScript version
  1078. $javascript = '';
  1079. } else if (check_browser_version('MSIE') || (check_browser_version('Opera') && !check_browser_operating_system("Linux"))) {
  1080. //IE and Opera fire the onchange when ever you move into a dropdown list with the keyboard.
  1081. //onfocus will call a function inside dropdown.js. It fixes this IE/Opera behavior.
  1082. //Note: There is a bug on Opera+Linux with the javascript code (first mouse selection is inactive),
  1083. //so we do not fix the Opera behavior on Linux
  1084. $javascript = ' onfocus="initSelect(\''.$formid.'\','.$targetwindow.')"';
  1085. } else {
  1086. //Other browser
  1087. $javascript = ' onchange="'.$targetwindow.
  1088. '.location=document.getElementById(\''.$formid.
  1089. '\').jump.options[document.getElementById(\''.
  1090. $formid.'\').jump.selectedIndex].value;"';
  1091. }
  1092. $output .= '<div>'.$selectlabel.$button.'<select id="'.$formid.'_jump" name="jump"'.$javascript.'>'."\n";
  1093. if ($nothing != '') {
  1094. $output .= " <option value=\"javascript:void(0)\">$nothing</option>\n";
  1095. }
  1096. $inoptgroup = false;
  1097. foreach ($options as $value => $label) {
  1098. if ($label == '--') { /// we are ending previous optgroup
  1099. /// Check to see if we already have a valid open optgroup
  1100. /// XHTML demands that there be at least 1 option within an optgroup
  1101. if ($inoptgroup and (count($optgr) > 1) ) {
  1102. $output .= implode('', $optgr);
  1103. $output .= ' </optgroup>';
  1104. }
  1105. $optgr = array();
  1106. $inoptgroup = false;
  1107. continue;
  1108. } else if (substr($label,0,2) == '--') { /// we are starting a new optgroup
  1109. /// Check to see if we already have a valid open optgroup
  1110. /// XHTML demands that there be at least 1 option within an optgroup
  1111. if ($inoptgroup and (count($optgr) > 1) ) {
  1112. $output .= implode('', $optgr);
  1113. $output .= ' </optgroup>';
  1114. }
  1115. unset($optgr);
  1116. $optgr = array();
  1117. $optgr[] = ' <optgroup label="'. s(format_string(substr($label,2))) .'">'; // Plain labels
  1118. $inoptgroup = true; /// everything following will be in an optgroup
  1119. continue;
  1120. } else {
  1121. if (!empty($CFG->usesid) && !isset($_COOKIE[session_name()]))
  1122. {
  1123. $url=sid_process_url( $common . $value );
  1124. } else
  1125. {
  1126. $url=$common . $value;
  1127. }
  1128. $optstr = ' <option value="' . $url . '"';
  1129. if ($value == $selected) {
  1130. $optstr .= ' selected="selected"';
  1131. }
  1132. if (!empty($optionsextra[$value])) {
  1133. $optstr .= ' '.$optionsextra[$value];
  1134. }
  1135. if ($label) {
  1136. $optstr .= '>'. $label .'</option>' . "\n";
  1137. } else {
  1138. $optstr .= '>'. $value .'</option>' . "\n";
  1139. }
  1140. if ($inoptgroup) {
  1141. $optgr[] = $optstr;
  1142. } else {
  1143. $output .= $optstr;
  1144. }
  1145. }
  1146. }
  1147. /// catch the final group if not closed
  1148. if ($inoptgroup and count($optgr) > 1) {
  1149. $output .= implode('', $optgr);
  1150. $output .= ' </optgroup>';
  1151. }
  1152. $output .= '</select>';
  1153. $output .= '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
  1154. if ($gobutton) {
  1155. $output .= '<input type="submit" value="'.
  1156. ($gobutton===true ? $go : $gobutton).'" />';
  1157. } else {
  1158. $output .= '<div id="noscript'.$formid.'" style="display: inline;">';
  1159. $output .= '<input type="submit" value="'.$go.'" /></div>';
  1160. $output .= '<script type="text/javascript">'.
  1161. "\n//<![CDATA[\n".
  1162. 'document.getElementById("noscript'.$formid.'").style.display = "none";'.
  1163. "\n//]]>\n".'</script>';
  1164. }
  1165. $output .= '</div></form>';
  1166. if ($return) {
  1167. return $output;
  1168. } else {
  1169. echo $output;
  1170. }
  1171. }
  1172. /**
  1173. * Prints some red text
  1174. *
  1175. * @param string $error The text to be displayed in red
  1176. */
  1177. function formerr($error) {
  1178. if (!empty($error)) {
  1179. echo '<span class="error">'. $error .'</span>';
  1180. }
  1181. }
  1182. /**
  1183. * Validates an email to make sure it makes sense.
  1184. *
  1185. * @param string $address The email address to validate.
  1186. * @return boolean
  1187. */
  1188. function validate_email($address) {
  1189. return (ereg('^[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+'.
  1190. '(\.[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+)*'.
  1191. '@'.
  1192. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
  1193. '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$',
  1194. $address));
  1195. }
  1196. /**
  1197. * Extracts file argument either from file parameter or PATH_INFO
  1198. *
  1199. * @param string $scriptname name of the calling script
  1200. * @return string file path (only safe characters)
  1201. */
  1202. function get_file_argument($scriptname) {
  1203. global $_SERVER;
  1204. $relativepath = FALSE;
  1205. // first try normal parameter (compatible method == no relative links!)
  1206. $relativepath = optional_param('file', FALSE, PARAM_PATH);
  1207. if ($relativepath === '/testslasharguments') {
  1208. echo 'test -1 : Incorrect use - try "file.php/testslasharguments" instead'; //indicate fopen/fread works for health center
  1209. die;
  1210. }
  1211. // then try extract file from PATH_INFO (slasharguments method)
  1212. if (!$relativepath and !empty($_SERVER['PATH_INFO'])) {
  1213. $path_info = $_SERVER['PATH_INFO'];
  1214. // check that PATH_INFO works == must not contain the script name
  1215. if (!strpos($path_info, $scriptname)) {
  1216. $relativepath = clean_param(rawurldecode($path_info), PARAM_PATH);
  1217. if ($relativepath === '/testslasharguments') {
  1218. echo 'test 1 : Slasharguments test passed. Server confguration is compatible with file.php/1/pic.jpg slashargument setting.'; //indicate ok for health center
  1219. die;
  1220. }
  1221. }
  1222. }
  1223. // now if both fail try the old way
  1224. // (for compatibility with misconfigured or older buggy php implementations)
  1225. if (!$relativepath) {
  1226. $arr = explode($scriptname, me());
  1227. if (!empty($arr[1])) {
  1228. $path_info = strip_querystring($arr[1]);
  1229. $relativepath = clean_param(rawurldecode($path_info), PARAM_PATH);
  1230. if ($relativepath === '/testslasharguments') {
  1231. echo 'test 2 : Slasharguments test passed (compatibility hack). Server confguration may be compatible with file.php/1/pic.jpg slashargument setting'; //indicate ok for health center
  1232. die;
  1233. }
  1234. }
  1235. }
  1236. return $relativepath;
  1237. }
  1238. /**
  1239. * Searches the current environment variables for some slash arguments
  1240. *
  1241. * @param string $file ?
  1242. * @todo Finish documenting this function
  1243. */
  1244. function get_slash_arguments($

Large files files are truncated, but you can click here to view the full file