PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/weblib.php

https://bitbucket.org/ceu/moodle_demo
PHP | 7269 lines | 5119 code | 743 blank | 1407 comment | 932 complexity | bbefc14d9291c58240eb05d7ec3c25a9 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1

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

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