misc.inc

  1. 7.x includes/misc.inc
  2. 6.x includes/misc.inc
  3. 4.x includes/misc.inc
  4. 5.x includes/misc.inc

This file contains misc functions for FlightPath

File

includes/misc.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * This file contains misc functions for FlightPath
  5. */
  6. /**
  7. * This is our custom error handler, which will intercept PHP warnings, notices, etc, and let us
  8. * display them, log them, etc.
  9. *
  10. * See https://www.php.net/manual/en/function.set-error-handler.php
  11. */
  12. function _fp_error_handler($error_level, $message, $filename, $line, $context = array()) {
  13. global $user;
  14. if (0 === error_reporting()) { return false;} // suppressed with @-operator
  15. $err_name = _fp_map_php_error_code($error_level);
  16. if (stristr($err_name, 'notice')) return FALSE; // don't care about Notices.
  17. // fpm() only displays for privileged users
  18. fpm($err_name . ": $message<br>... ($line) $filename");
  19. if (@$user->id !== '1' && @$user->id !== 1) {
  20. // We are NOT the admin user.
  21. // Should we email someone, as with mysql errors?
  22. $tomail = trim(variable_get("notify_php_error_email_address", ""));
  23. if ($tomail != "") {
  24. $hostname = php_uname('n') . ' - ' . $GLOBALS['fp_system_settings']['base_url'];
  25. $msg = "";
  26. $msg .= "SERVER: $hostname \n";
  27. $msg .= "DATE: " . format_date() . "\n";
  28. $msg .= "SEVERITY: $err_name \n";
  29. $msg .= "--------------------------\n\n";
  30. $msg .= "$err_name: $message \n\n";
  31. $msg .= "... ($line) $filename";
  32. fp_mail($tomail, "PHP Error in FlightPath", $msg);
  33. }
  34. }
  35. } // _fp_error_handler
  36. /**
  37. * Map an error code into an Error word
  38. *
  39. * @param int $code Error code to map
  40. * @return array Array of error word, and log location.
  41. */
  42. function _fp_map_php_error_code($code) {
  43. $error = '';
  44. switch ($code) {
  45. case E_PARSE:
  46. case E_ERROR:
  47. case E_CORE_ERROR:
  48. case E_COMPILE_ERROR:
  49. case E_USER_ERROR:
  50. $error = 'Fatal Error';
  51. break;
  52. case E_WARNING:
  53. case E_USER_WARNING:
  54. case E_COMPILE_WARNING:
  55. case E_RECOVERABLE_ERROR:
  56. $error = 'Warning';
  57. break;
  58. case E_NOTICE:
  59. case E_USER_NOTICE:
  60. $error = 'Notice';
  61. break;
  62. case E_STRICT:
  63. $error = 'Strict';
  64. break;
  65. case E_DEPRECATED:
  66. case E_USER_DEPRECATED:
  67. $error = 'Deprecated';
  68. break;
  69. default :
  70. break;
  71. }
  72. return $error;
  73. }
  74. /**
  75. * Send an email. Drop-in replacement for PHP's mail() command,
  76. * but can use SMTP protocol if enabled.
  77. */
  78. function fp_mail($to,$subject,$msg) {
  79. // TODO: In the future, check to see if there are any other modules which invoke a hook to intercept mail.
  80. if (function_exists('smtp_mail')) {
  81. smtp_mail($to, $subject, $msg);
  82. return;
  83. }
  84. else {
  85. mail($to, $subject, $msg);
  86. }
  87. } // fp_mail
  88. /**
  89. * Returns the component of the page's path.
  90. *
  91. * When viewing a page at the path "admin/structure/types", for example, arg(0) returns "admin", arg(1) returns "structure", and arg(2) returns "types".
  92. *
  93. */
  94. function arg($index) {
  95. $q = $_REQUEST["q"];
  96. $temp = explode("/", $q);
  97. $rtn = @trim($temp[$index]);
  98. return $rtn;
  99. }
  100. /**
  101. * Send a request through the Internet and return the result as an object.
  102. *
  103. * This is a modified copy of the Drupal 6 function drupal_http_request(),
  104. * taken from here: http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_http_request/6
  105. */
  106. function fp_http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3, $timeout = 30.0) {
  107. global $db_prefix;
  108. $result = new stdClass();
  109. // Parse the URL and make sure we can handle the schema.
  110. $uri = parse_url($url);
  111. if ($uri == FALSE) {
  112. $result->error = 'unable to parse URL';
  113. $result->code = -1001;
  114. return $result;
  115. }
  116. if (!isset($uri['scheme'])) {
  117. $result->error = 'missing schema';
  118. $result->code = -1002;
  119. return $result;
  120. }
  121. timer_start(__FUNCTION__);
  122. switch ($uri['scheme']) {
  123. case 'http':
  124. case 'feed':
  125. $port = isset($uri['port']) ? $uri['port'] : 80;
  126. $host = $uri['host'] . ($port != 80 ? ':' . $port : '');
  127. $fp = @fsockopen($uri['host'], $port, $errno, $errstr, $timeout);
  128. break;
  129. case 'https':
  130. // Note: Only works for PHP 4.3 compiled with OpenSSL.
  131. $port = isset($uri['port']) ? $uri['port'] : 443;
  132. $host = $uri['host'] . ($port != 443 ? ':' . $port : '');
  133. $fp = @fsockopen('ssl://' . $uri['host'], $port, $errno, $errstr, $timeout);
  134. break;
  135. default:
  136. $result->error = 'invalid schema ' . $uri['scheme'];
  137. $result->code = -1003;
  138. return $result;
  139. }
  140. // Make sure the socket opened properly.
  141. if (!$fp) {
  142. // When a network error occurs, we use a negative number so it does not
  143. // clash with the HTTP status codes.
  144. $result->code = -$errno;
  145. $result->error = trim($errstr);
  146. // Log that this failed.
  147. watchdog("http_request", "fp_http_request failed! Perhaps the server cannot make requests?", array(), WATCHDOG_ERROR);
  148. return $result;
  149. }
  150. // Construct the path to act on.
  151. $path = isset($uri['path']) ? $uri['path'] : '/';
  152. if (isset($uri['query'])) {
  153. $path .= '?' . $uri['query'];
  154. }
  155. // Create HTTP request.
  156. $defaults = array(
  157. // RFC 2616: "non-standard ports MUST, default ports MAY be included".
  158. // We don't add the port to prevent from breaking rewrite rules checking the
  159. // host that do not take into account the port number.
  160. 'Host' => "Host: $host",
  161. 'User-Agent' => 'User-Agent: FlightPath (+http://getflightpath.com/)',
  162. );
  163. // Only add Content-Length if we actually have any content or if it is a POST
  164. // or PUT request. Some non-standard servers get confused by Content-Length in
  165. // at least HEAD/GET requests, and Squid always requires Content-Length in
  166. // POST/PUT requests.
  167. $content_length = strlen($data);
  168. if ($content_length > 0 || $method == 'POST' || $method == 'PUT') {
  169. $defaults['Content-Length'] = 'Content-Length: ' . $content_length;
  170. }
  171. // If the server url has a user then attempt to use basic authentication
  172. if (isset($uri['user'])) {
  173. $defaults['Authorization'] = 'Authorization: Basic ' . base64_encode($uri['user'] . (!empty($uri['pass']) ? ":" . $uri['pass'] : ''));
  174. }
  175. foreach ($headers as $header => $value) {
  176. $defaults[$header] = $header . ': ' . $value;
  177. }
  178. $request = $method . ' ' . $path . " HTTP/1.0\r\n";
  179. $request .= implode("\r\n", $defaults);
  180. $request .= "\r\n\r\n";
  181. $request .= $data;
  182. $result->request = $request;
  183. // Calculate how much time is left of the original timeout value.
  184. $time_left = $timeout - timer_read(__FUNCTION__) / 1000;
  185. if ($time_left > 0) {
  186. stream_set_timeout($fp, floor($time_left), floor(1000000 * fmod($time_left, 1)));
  187. fwrite($fp, $request);
  188. }
  189. // Fetch response.
  190. $response = '';
  191. while (!feof($fp)) {
  192. // Calculate how much time is left of the original timeout value.
  193. $time_left = $timeout - timer_read(__FUNCTION__) / 1000;
  194. if ($time_left <= 0) {
  195. $result->code = HTTP_REQUEST_TIMEOUT;
  196. $result->error = 'request timed out';
  197. return $result;
  198. }
  199. stream_set_timeout($fp, floor($time_left), floor(1000000 * fmod($time_left, 1)));
  200. $chunk = fread($fp, 1024);
  201. $response .= $chunk;
  202. }
  203. fclose($fp);
  204. // Parse response headers from the response body.
  205. // Be tolerant of malformed HTTP responses that separate header and body with
  206. // \n\n or \r\r instead of \r\n\r\n. See http://drupal.org/node/183435
  207. list($split, $result->data) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
  208. $split = preg_split("/\r\n|\n|\r/", $split);
  209. list($protocol, $code, $status_message) = explode(' ', trim(array_shift($split)), 3);
  210. $result->protocol = $protocol;
  211. $result->status_message = $status_message;
  212. $result->headers = array();
  213. // Parse headers.
  214. while ($line = trim(array_shift($split))) {
  215. list($header, $value) = explode(':', $line, 2);
  216. if (isset($result->headers[$header]) && $header == 'Set-Cookie') {
  217. // RFC 2109: the Set-Cookie response header comprises the token Set-
  218. // Cookie:, followed by a comma-separated list of one or more cookies.
  219. $result->headers[$header] .= ',' . trim($value);
  220. }
  221. else {
  222. $result->headers[$header] = trim($value);
  223. }
  224. }
  225. $responses = array(
  226. 100 => 'Continue',
  227. 101 => 'Switching Protocols',
  228. 200 => 'OK',
  229. 201 => 'Created',
  230. 202 => 'Accepted',
  231. 203 => 'Non-Authoritative Information',
  232. 204 => 'No Content',
  233. 205 => 'Reset Content',
  234. 206 => 'Partial Content',
  235. 300 => 'Multiple Choices',
  236. 301 => 'Moved Permanently',
  237. 302 => 'Found',
  238. 303 => 'See Other',
  239. 304 => 'Not Modified',
  240. 305 => 'Use Proxy',
  241. 307 => 'Temporary Redirect',
  242. 400 => 'Bad Request',
  243. 401 => 'Unauthorized',
  244. 402 => 'Payment Required',
  245. 403 => 'Forbidden',
  246. 404 => 'Not Found',
  247. 405 => 'Method Not Allowed',
  248. 406 => 'Not Acceptable',
  249. 407 => 'Proxy Authentication Required',
  250. 408 => 'Request Time-out',
  251. 409 => 'Conflict',
  252. 410 => 'Gone',
  253. 411 => 'Length Required',
  254. 412 => 'Precondition Failed',
  255. 413 => 'Request Entity Too Large',
  256. 414 => 'Request-URI Too Large',
  257. 415 => 'Unsupported Media Type',
  258. 416 => 'Requested range not satisfiable',
  259. 417 => 'Expectation Failed',
  260. 500 => 'Internal Server Error',
  261. 501 => 'Not Implemented',
  262. 502 => 'Bad Gateway',
  263. 503 => 'Service Unavailable',
  264. 504 => 'Gateway Time-out',
  265. 505 => 'HTTP Version not supported',
  266. );
  267. // RFC 2616 states that all unknown HTTP codes must be treated the same as the
  268. // base code in their class.
  269. if (!isset($responses[$code])) {
  270. $code = floor($code / 100) * 100;
  271. }
  272. switch ($code) {
  273. case 200: // OK
  274. case 304: // Not modified
  275. break;
  276. case 301: // Moved permanently
  277. case 302: // Moved temporarily
  278. case 307: // Moved temporarily
  279. $location = $result->headers['Location'];
  280. $timeout -= timer_read(__FUNCTION__) / 1000;
  281. if ($timeout <= 0) {
  282. $result->code = HTTP_REQUEST_TIMEOUT;
  283. $result->error = 'request timed out';
  284. }
  285. elseif ($retry) {
  286. $result = fp_http_request($result->headers['Location'], $headers, $method, $data, --$retry, $timeout);
  287. $result->redirect_code = $result->code;
  288. }
  289. $result->redirect_url = $location;
  290. break;
  291. default:
  292. $result->error = $status_message;
  293. }
  294. $result->code = $code;
  295. return $result;
  296. }
  297. /**
  298. * Begin a microtime timer for later use.
  299. */
  300. function timer_start($name) {
  301. global $timers;
  302. list($usec, $sec) = explode(' ', microtime());
  303. $timers[$name]['start'] = (float) $usec + (float) $sec;
  304. $timers[$name]['count'] = isset($timers[$name]['count']) ? ++$timers[$name]['count'] : 1;
  305. }
  306. /**
  307. * Works with the timer_start() function to return how long
  308. * it has been since the start.
  309. */
  310. function timer_read($name) {
  311. global $timers;
  312. if (isset($timers[$name]['start'])) {
  313. list($usec, $sec) = explode(' ', microtime());
  314. $stop = (float) $usec + (float) $sec;
  315. $diff = round(($stop - $timers[$name]['start']) * 1000, 2);
  316. if (isset($timers[$name]['time'])) {
  317. $diff += $timers[$name]['time'];
  318. }
  319. return $diff;
  320. }
  321. }
  322. /**
  323. * Returns a random string of length len.
  324. */
  325. function fp_get_random_string($len = 7, $alpha = TRUE, $numeric = TRUE, $symbols = FALSE) {
  326. $base = "";
  327. if ($alpha) {
  328. $base .= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  329. }
  330. if ($numeric) {
  331. $base .= "12345678901234567890";
  332. }
  333. if ($symbols) {
  334. $base .= "!@#$%^&*()_+!@#$%^&*()-=";
  335. }
  336. $str = "";
  337. for ($t = 0; $t < $len; $t++) {
  338. $base = str_shuffle($base);
  339. $str .= $base[0];
  340. }
  341. return $str;
  342. }
  343. /**
  344. * Call all modules which implement hook_clear_cache
  345. */
  346. function fp_clear_cache() {
  347. // Find modules which implement hook_clear_cache
  348. $modules = modules_implement_hook("clear_cache");
  349. foreach ($modules as $module) {
  350. call_user_func($module . '_clear_cache');
  351. }
  352. }
  353. /**
  354. Remove any possiblilty of a malicious attacker trying to inject
  355. nonsense.
  356. From: https://paragonie.com/blog/2015/06/preventing-xss-vulnerabilities-in-php-everything-you-need-know
  357. */
  358. function fp_no_html_xss($string) {
  359. return htmlentities($string, ENT_QUOTES, 'UTF-8');
  360. //return htmlentities($string, ENT_QUOTES | ENT_HTML5, 'UTF-8'); // ENT_HTML5 requires PGP 5.4+
  361. }
  362. /**
  363. * Filter HTML, allowing only certain tags, and removing dangerous attributes.
  364. *
  365. * $type can be:
  366. * - "basic" - Only certain tags allowed, no attributes. Safest. New lines = <br>
  367. * - "full" - All HTML is allowed through.
  368. *
  369. */
  370. function filter_markup($str, $type = "basic") {
  371. // Use the DOM functions to repair any mismatched HTML.
  372. $doc = new DOMDocument();
  373. @$doc->loadHTML($str);
  374. $str = $doc->saveHTML();
  375. if ($type == "basic") {
  376. // To reduce extra newlines, remove any newline which is at the END of an existing <br> tag.
  377. $str = str_ireplace("<br>\n", "<br />", $str);
  378. $str = str_ireplace("<br />\n", "<br />", $str);
  379. $allowed_tags = array('a', 'em', 'strong', 'cite',
  380. 'blockquote', 'code', 'ul', 'ol', 'li',
  381. 'dl', 'dt', 'dd', 'span', 'div',
  382. 'b', 'i', 'u', 'br', 'p', 'table', 'tr',
  383. 'td', 'th', 'tbody', );
  384. $str = filter_xss($str, $allowed_tags);
  385. $str = trim($str);
  386. $str = nl2br($str);
  387. }
  388. return $str;
  389. }
  390. /**
  391. * This function is taken almost directly from Drupal 7's core code. It is used to help us filter out
  392. * dangerous HTML which the user might type.
  393. * From the D7 documentation:
  394. *
  395. * Filters HTML to prevent cross-site-scripting (XSS) vulnerabilities.
  396. * Based on kses by Ulf Harnhammar, see http://sourceforge.net/projects/kses. For examples of various XSS attacks, see: http://ha.ckers.org/xss.html.
  397. * This code does four things:
  398. * Removes characters and constructs that can trick browsers.
  399. * Makes sure all HTML entities are well-formed.
  400. * Makes sure all HTML tags and attributes are well-formed.
  401. * Makes sure no HTML tags contain URLs with a disallowed protocol (e.g. javascript:).
  402. *
  403. */
  404. function filter_xss($string, $allowed_tags = array('a', 'em', 'strong', 'cite', 'blockquote', 'code', 'ul', 'ol', 'li', 'dl', 'dt', 'dd', 'span', 'div')) {
  405. // Only operate on valid UTF-8 strings. This is necessary to prevent cross
  406. // site scripting issues on Internet Explorer 6.
  407. if (!fp_validate_utf8($string)) {
  408. return '';
  409. }
  410. // Store the text format.
  411. filter_xss_split($allowed_tags, TRUE);
  412. // Remove NULL characters (ignored by some browsers).
  413. $string = str_replace(chr(0), '', $string);
  414. // Remove Netscape 4 JS entities.
  415. $string = preg_replace('%&\s*\{[^}]*(\}\s*;?|$)%', '', $string);
  416. // Defuse all HTML entities.
  417. $string = str_replace('&', '&amp;', $string);
  418. // Change back only well-formed entities in our whitelist:
  419. // Decimal numeric entities.
  420. $string = preg_replace('/&amp;#([0-9]+;)/', '&#\1', $string);
  421. // Hexadecimal numeric entities.
  422. $string = preg_replace('/&amp;#[Xx]0*((?:[0-9A-Fa-f]{2})+;)/', '&#x\1', $string);
  423. // Named entities.
  424. $string = preg_replace('/&amp;([A-Za-z][A-Za-z0-9]*;)/', '&\1', $string);
  425. return preg_replace_callback('%
  426. (
  427. <(?=[^a-zA-Z!/]) # a lone <
  428. | # or
  429. <!--.*?--> # a comment
  430. | # or
  431. <[^>]*(>|$) # a string that starts with a <, up until the > or the end of the string
  432. | # or
  433. > # just a >
  434. )%x', 'filter_xss_split', $string);
  435. }
  436. /**
  437. * Like the filter_xss function, this is taken from D7's
  438. * _filter_xss_split function
  439. */
  440. function filter_xss_split($m, $store = FALSE) {
  441. static $allowed_html;
  442. if ($store) {
  443. $allowed_html = array_flip($m);
  444. return;
  445. }
  446. $string = $m[1];
  447. if (substr($string, 0, 1) != '<') {
  448. // We matched a lone ">" character.
  449. return '&gt;';
  450. }
  451. elseif (strlen($string) == 1) {
  452. // We matched a lone "<" character.
  453. return '&lt;';
  454. }
  455. if (!preg_match('%^<\s*(/\s*)?([a-zA-Z0-9]+)([^>]*)>?|(<!--.*?-->)$%', $string, $matches)) {
  456. // Seriously malformed.
  457. return '';
  458. }
  459. $slash = trim($matches[1]);
  460. $elem = &$matches[2];
  461. $attrlist = &$matches[3];
  462. $comment = &$matches[4];
  463. if ($comment) {
  464. $elem = '!--';
  465. }
  466. if (!isset($allowed_html[strtolower($elem)])) {
  467. // Disallowed HTML element.
  468. return '';
  469. }
  470. if ($comment) {
  471. return $comment;
  472. }
  473. if ($slash != '') {
  474. return "</$elem>";
  475. }
  476. // Is there a closing XHTML slash at the end of the attributes?
  477. $attrlist = preg_replace('%(\s?)/\s*$%', '\1', $attrlist, -1, $count);
  478. $xhtml_slash = $count ? ' /' : '';
  479. // Clean up attributes.
  480. $attr2 = implode(' ', filter_xss_attributes($attrlist));
  481. $attr2 = preg_replace('/[<>]/', '', $attr2);
  482. $attr2 = strlen($attr2) ? ' ' . $attr2 : '';
  483. return "<$elem$attr2$xhtml_slash>";
  484. }
  485. function filter_xss_attributes($attr) {
  486. $attrarr = array();
  487. $mode = 0;
  488. $attrname = '';
  489. while (strlen($attr) != 0) {
  490. // Was the last operation successful?
  491. $working = 0;
  492. switch ($mode) {
  493. case 0:
  494. // Attribute name, href for instance.
  495. if (preg_match('/^([-a-zA-Z]+)/', $attr, $match)) {
  496. $attrname = strtolower($match[1]);
  497. $skip = ($attrname == 'style' || substr($attrname, 0, 2) == 'on');
  498. $working = $mode = 1;
  499. $attr = preg_replace('/^[-a-zA-Z]+/', '', $attr);
  500. }
  501. break;
  502. case 1:
  503. // Equals sign or valueless ("selected").
  504. if (preg_match('/^\s*=\s*/', $attr)) {
  505. $working = 1;
  506. $mode = 2;
  507. $attr = preg_replace('/^\s*=\s*/', '', $attr);
  508. break;
  509. }
  510. if (preg_match('/^\s+/', $attr)) {
  511. $working = 1;
  512. $mode = 0;
  513. if (!$skip) {
  514. $attrarr[] = $attrname;
  515. }
  516. $attr = preg_replace('/^\s+/', '', $attr);
  517. }
  518. break;
  519. case 2:
  520. // Attribute value, a URL after href= for instance.
  521. if (preg_match('/^"([^"]*)"(\s+|$)/', $attr, $match)) {
  522. $thisval = filter_xss_bad_protocol($match[1]);
  523. if (!$skip) {
  524. $attrarr[] = "$attrname=\"$thisval\"";
  525. }
  526. $working = 1;
  527. $mode = 0;
  528. $attr = preg_replace('/^"[^"]*"(\s+|$)/', '', $attr);
  529. break;
  530. }
  531. if (preg_match("/^'([^']*)'(\s+|$)/", $attr, $match)) {
  532. $thisval = filter_xss_bad_protocol($match[1]);
  533. if (!$skip) {
  534. $attrarr[] = "$attrname='$thisval'";
  535. }
  536. $working = 1;
  537. $mode = 0;
  538. $attr = preg_replace("/^'[^']*'(\s+|$)/", '', $attr);
  539. break;
  540. }
  541. if (preg_match("%^([^\s\"']+)(\s+|$)%", $attr, $match)) {
  542. $thisval = filter_xss_bad_protocol($match[1]);
  543. if (!$skip) {
  544. $attrarr[] = "$attrname=\"$thisval\"";
  545. }
  546. $working = 1;
  547. $mode = 0;
  548. $attr = preg_replace("%^[^\s\"']+(\s+|$)%", '', $attr);
  549. }
  550. break;
  551. }
  552. if ($working == 0) {
  553. // Not well formed; remove and try again.
  554. $attr = preg_replace('/
  555. ^
  556. (
  557. "[^"]*("|$) # - a string that starts with a double quote, up until the next double quote or the end of the string
  558. | # or
  559. \'[^\']*(\'|$)| # - a string that starts with a quote, up until the next quote or the end of the string
  560. | # or
  561. \S # - a non-whitespace character
  562. )* # any number of the above three
  563. \s* # any number of whitespaces
  564. /x', '', $attr);
  565. $mode = 0;
  566. }
  567. }
  568. // The attribute list ends with a valueless attribute like "selected".
  569. if ($mode == 1 && !$skip) {
  570. $attrarr[] = $attrname;
  571. }
  572. return $attrarr;
  573. }
  574. function filter_xss_bad_protocol($string) {
  575. // Get the plain text representation of the attribute value (i.e. its meaning).
  576. $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
  577. return htmlspecialchars(fp_strip_dangerous_protocols($string), ENT_QUOTES, 'UTF-8');
  578. }
  579. function fp_strip_dangerous_protocols($uri) {
  580. static $allowed_protocols;
  581. if (!isset($allowed_protocols)) {
  582. $allowed_protocols = array_flip(array('ftp', 'http', 'https', 'irc', 'mailto', 'news', 'nntp', 'rtsp', 'sftp', 'ssh', 'tel', 'telnet', 'webcal'));
  583. }
  584. // Iteratively remove any invalid protocol found.
  585. do {
  586. $before = $uri;
  587. $colonpos = strpos($uri, ':');
  588. if ($colonpos > 0) {
  589. // We found a colon, possibly a protocol. Verify.
  590. $protocol = substr($uri, 0, $colonpos);
  591. // If a colon is preceded by a slash, question mark or hash, it cannot
  592. // possibly be part of the URL scheme. This must be a relative URL, which
  593. // inherits the (safe) protocol of the base document.
  594. if (preg_match('![/?#]!', $protocol)) {
  595. break;
  596. }
  597. // Check if this is a disallowed protocol. Per RFC2616, section 3.2.3
  598. // (URI Comparison) scheme comparison must be case-insensitive.
  599. if (!isset($allowed_protocols[strtolower($protocol)])) {
  600. $uri = substr($uri, $colonpos + 1);
  601. }
  602. }
  603. } while ($before != $uri);
  604. return $uri;
  605. }
  606. function fp_validate_utf8($text) {
  607. if (strlen($text) == 0) {
  608. return TRUE;
  609. }
  610. // With the PCRE_UTF8 modifier 'u', preg_match() fails silently on strings
  611. // containing invalid UTF-8 byte sequences. It does not reject character
  612. // codes above U+10FFFF (represented by 4 or more octets), though.
  613. return (preg_match('/^./us', $text) == 1);
  614. }
  615. /**
  616. * Simple function to convert a string into a machine-readable string.
  617. *
  618. * Useful for making possibly unsafe text work as an array index, a CSS class, etc. Replaces
  619. * "bad" characters, or characters which might not be allowed for variables, for example,
  620. * into underscores (_).
  621. *
  622. * @param unknown_type $str
  623. * @return unknown
  624. */
  625. function fp_get_machine_readable($str) {
  626. return preg_replace('@[^a-zA-Z0-9_]+@','_',$str);
  627. }
  628. /////////////////////////////////////////////////////////////////////
  629. /**
  630. * Return back an assoc array of our set degree classifications, separated by "level"
  631. */
  632. function fp_get_degree_classifications() {
  633. $rtn = array();
  634. // Level 1
  635. $temp = explode("\n", variable_get("degree_classifications_level_1", "MAJOR ~ Major"));
  636. foreach ($temp as $line) {
  637. $temp2 = explode("~", $line);
  638. $machine_name = trim($temp2[0]);
  639. $title = trim($temp2[1]);
  640. if ($machine_name != "") {
  641. $rtn["levels"][1][$machine_name] = $title;
  642. $rtn["machine_names"][$machine_name] = $title;
  643. }
  644. }
  645. // Level 2
  646. $temp = explode("\n", variable_get("degree_classifications_level_2", "MINOR ~ Minor"));
  647. foreach ($temp as $line) {
  648. $temp2 = explode("~", $line);
  649. $machine_name = trim($temp2[0]);
  650. $title = trim($temp2[1]);
  651. if ($machine_name != "") {
  652. $rtn["levels"][2][$machine_name] = $title;
  653. $rtn["machine_names"][$machine_name] = $title;
  654. }
  655. }
  656. // Level 3
  657. $temp = explode("\n", variable_get("degree_classifications_level_3", "CONC ~ Concentration"));
  658. foreach ($temp as $line) {
  659. $temp2 = explode("~", $line);
  660. $machine_name = trim($temp2[0]);
  661. $title = trim($temp2[1]);
  662. if ($machine_name != "") {
  663. $rtn["levels"][3][$machine_name] = $title;
  664. $rtn["machine_names"][$machine_name] = $title;
  665. }
  666. }
  667. return $rtn;
  668. }
  669. /**
  670. * Returns back an assoc array for the supplied code. Looks like:
  671. * $arr["level_num"] = number
  672. * $arr["title"] = the title
  673. *
  674. *
  675. */
  676. function fp_get_degree_classification_details($degree_class = "MAJOR", $bool_return_class_code_as_title_if_not_found = TRUE) {
  677. $rtn = array();
  678. if ($bool_return_class_code_as_title_if_not_found) {
  679. // Use the degree_class as title for default, if we can't find it otherwise.
  680. $rtn["level_num"] = 0;
  681. $rtn["title"] = $degree_class;
  682. $rtn["degree_class"] = $degree_class;
  683. }
  684. $degree_classifications = fp_get_degree_classifications();
  685. foreach ($degree_classifications["levels"] as $num => $details) {
  686. if (isset($details[$degree_class])) {
  687. $rtn["level_num"] = $num;
  688. $rtn["title"] = $details[$degree_class];
  689. $rtn["degree_class"] = $degree_class;
  690. break;
  691. }
  692. }
  693. return $rtn;
  694. }
  695. /**
  696. * Return an array version of the term_id_structure field from the admin settings
  697. *
  698. */
  699. function get_term_structures() {
  700. $rtn = array();
  701. $temp = $GLOBALS["fp_system_settings"]["term_id_structure"];
  702. $structures = explode("\n", $temp);
  703. foreach ($structures as $structure) {
  704. $tokens = explode(",", $structure);
  705. $term_def = trim($tokens[0]);
  706. // Get rid of the replacement pattern.
  707. // Looks like: [Y4]40. We want the 40.
  708. // Simply explode on "]"
  709. $temp = explode("]", $term_def);
  710. $rtn[trim($temp[1])] = array(
  711. "term_def" => $term_def,
  712. "short" => trim(@$tokens[1]),
  713. "full" => trim(@$tokens[2]),
  714. "abbr" => trim(@$tokens[3]),
  715. "disp_adjust" => trim(@$tokens[4]),
  716. );
  717. }
  718. return $rtn;
  719. }
  720. /**
  721. * Returns back an array of all the available requirement types (by code) that
  722. * have been defined.
  723. *
  724. */
  725. function fp_get_requirement_types() {
  726. $rtn = array();
  727. if (isset($GLOBALS['fp_temp_cache']['fp_get_requirement_types'])) {
  728. return $GLOBALS['fp_temp_cache']['fp_get_requirement_types'];
  729. }
  730. $temp = explode("\n", variable_get("requirement_types", "g ~ General Requirements\nc ~ Core Requirements\ne ~ Electives\nm ~ Major Requirements\ns ~ Supporting Requirements\nx ~ Additional Requirements"));
  731. foreach ($temp as $line) {
  732. $line = trim($line);
  733. if ($line == "") continue;
  734. $temp = explode("~", $line);
  735. $code = trim(strtolower($temp[0]));
  736. $desc = trim($temp[1]);
  737. $rtn[$code] = $desc;
  738. }
  739. // Make sure that code 'x' is set.
  740. if (!isset($rtn["x"])) {
  741. $rtn["x"] = t("Additional Requirements");
  742. }
  743. // Make sure code 'e' for Electives is set.
  744. if (!isset($rtn["e"])) {
  745. $rtn["e"] = t("Electives");
  746. }
  747. // Make sure code 'm' is set, for Major Requirements, our default type.
  748. if (!isset($rtn["m"])) {
  749. $rtn["m"] = t("Major Requirements");
  750. }
  751. $GLOBALS['fp_temp_cache']['fp_get_requirement_types'] = $rtn;
  752. return $rtn;
  753. }
  754. /**
  755. * This function provides a pass-thru to $d = new DegreePlan(args).
  756. * However, it allows for quick caching look-up, so it should be used when possible instead of $x = new DegreePlan.
  757. */
  758. function fp_load_degree($degree_id = "", DatabaseHandler $db = NULL, $bool_load_minimal = false, $array_significant_courses = false, $bool_use_draft = false) {
  759. // Create a "cache key" based on the arguments, so we can look this degree up faster later.
  760. $cache_key = md5(serialize(func_get_args()));
  761. //fpm("degree_id: $degree_id . cache key:" . $cache_key . "+++++++++++++");
  762. if (isset($GLOBALS['fp_temp_cache']['fp_load_degree'][$cache_key])) {
  763. $degree = $GLOBALS['fp_temp_cache']['fp_load_degree'][$cache_key];
  764. //fpm(" ... returning cache");
  765. return $degree;
  766. }
  767. $degree = new DegreePlan($degree_id, $db, $bool_load_minimal, $array_significant_courses, $bool_use_draft);
  768. // Save to our cache
  769. $GLOBALS['fp_temp_cache']['fp_load_degree'][$cache_key] = $degree;
  770. return $degree;
  771. }
  772. /**
  773. * If this function is called, it will override any other page tabs
  774. * which might be getting constructed. This lets the programmer,
  775. * at run-time, completely control what tabs are at the top of the page.
  776. */
  777. function fp_set_page_tabs($tab_array) {
  778. $GLOBALS["fp_set_page_tabs"] = $tab_array;
  779. }
  780. /**
  781. * Allows the programmer to define subtabs at the top of the page.
  782. *
  783. * @param unknown_type $tab_array
  784. */
  785. function fp_set_page_sub_tabs($tab_array) {
  786. $GLOBALS["fp_set_page_sub_tabs"] = $tab_array;
  787. }
  788. /**
  789. * Allows the programmer to set the title of the page, overwriting any default title.
  790. *
  791. * @param unknown_type $title
  792. */
  793. function fp_set_title($title) {
  794. $GLOBALS["fp_set_title"] = $title;
  795. if ($title == "") {
  796. fp_show_title(FALSE); // No title to show!
  797. }
  798. else {
  799. fp_show_title(TRUE); // If we are calling this function, we clearly want to display the title.
  800. }
  801. }
  802. /**
  803. * Add a CSS class to the body tag of the page. Useful for themeing later on.
  804. *
  805. * @param unknown_type $class
  806. */
  807. function fp_add_body_class($class) {
  808. // Let's sanitize the "class" to make sure it doesn't contain any trouble characters.
  809. $class = str_replace("'", '', $class);
  810. $class = str_replace('"', '', $class);
  811. $class = str_replace('(', '', $class);
  812. $class = str_replace(')', '', $class);
  813. $class = str_replace(';', '', $class);
  814. $class = str_replace('.', '', $class);
  815. $class = str_replace('<', '', $class);
  816. $class = str_replace('>', '', $class);
  817. $class = str_replace('/', '', $class);
  818. $class = str_replace('\\', '', $class);
  819. $class = str_replace('#', '', $class);
  820. $class = str_replace('&', '', $class);
  821. @$GLOBALS["fp_add_body_classes"] .= " " . $class;
  822. }
  823. /**
  824. * Returns back the site's "token", which is a simply md5 of some randomness.
  825. * It is used primarily with forms, to ensure against cross-site forgeries.
  826. * The site's token gets saved to the variables table, for later use. The idea
  827. * is that every installation of FlightPath has a semi-unique token.
  828. */
  829. function fp_token() {
  830. $site_token = variable_get("site_token", "");
  831. if ($site_token == "") {
  832. $site_token = md5("" . time() . rand(1,9999));
  833. variable_set("site_token", $site_token);
  834. }
  835. return $site_token;
  836. }
  837. /**
  838. * Simple function to split a basic CSV string, trim all elements, then return
  839. * the resulting array.
  840. */
  841. function csv_to_array($csv_string) {
  842. $temp = explode(",", $csv_string);
  843. $temp = array_map("trim", $temp);
  844. return $temp;
  845. }
  846. /**
  847. * Add a "message" to the top of the screen. Useful for short messages like "You have been logged out"
  848. * or "Form submitted successfully."
  849. *
  850. * @param String $msg
  851. * This is the string message itself.
  852. * @param String $type
  853. * The "type" of message. This string is added as a CSS class to the message, for theming later.
  854. * @param boolean $bool_no_repeat
  855. * Boolean. Should the message show more than once per page view? Set to TRUE if it should NOT.
  856. */
  857. function fp_add_message($msg, $type = "status", $bool_no_repeat = FALSE) {
  858. $md5 = md5($type . $msg);
  859. if ($bool_no_repeat && isset($_SESSION["fp_messages"]) && is_array($_SESSION["fp_messages"])) {
  860. // Make sure this message isn't already in the session.
  861. foreach($_SESSION["fp_messages"] as $s) {
  862. if ($s["md5"] == $md5) return;
  863. }
  864. }
  865. $_SESSION["fp_messages"][] = array("type" => $type, "msg" => $msg, "md5" => $md5);
  866. }
  867. /**
  868. * Add an extra CSS file to the page with this function.
  869. * Ex: fp_add_css(fp_get_module_path("admin") . '/css/admin.css');
  870. *
  871. * @param String $path_to_css
  872. */
  873. function fp_add_css($path_to_css) {
  874. // Init if needed
  875. if (!isset($GLOBALS['fp_extra_css'])) $GLOBALS['fp_extra_css'] = array();
  876. if (!in_array($path_to_css, $GLOBALS['fp_extra_css'])) {
  877. $GLOBALS["fp_extra_css"][] = $path_to_css;
  878. }
  879. }
  880. /**
  881. * Add extra javascript to the page.
  882. *
  883. * - type = file... $js is expected to be the path to a javascript file.
  884. * - type = setting... $js is expected to be an associative array of settings.
  885. * For example: array("my_path" => "blah", "my_color" => "red").
  886. * They will be available in javascript in the object FlightPath like so:
  887. * FlightPath.settings.my_color;
  888. *
  889. * Ex: fp_add_js(fp_get_module_path("admin") . '/js/admin.js');
  890. *
  891. * @see fp_add_css()
  892. *
  893. */
  894. function fp_add_js($js, $type = "file") {
  895. // Init if needed
  896. if (!isset($GLOBALS['fp_extra_js'])) $GLOBALS['fp_extra_js'] = array();
  897. if ($type == "file") {
  898. if (!in_array($js, $GLOBALS['fp_extra_js'])) {
  899. $GLOBALS["fp_extra_js"][] = $js;
  900. }
  901. }
  902. if ($type == "setting") {
  903. if (!isset($GLOBALS["fp_extra_js_settings"])) $GLOBALS["fp_extra_js_settings"] = array();
  904. $GLOBALS["fp_extra_js_settings"] = array_merge($GLOBALS["fp_extra_js_settings"], $js);
  905. }
  906. }
  907. /**
  908. * This function will create a string from a 1 dimensional assoc array.
  909. * Ex: arr = array("pet" => "dog", "name" => "Rex")
  910. * will return: pet-dog,name-Rex under the default settings.
  911. *
  912. * Use the fp_explode_assoc function to piece it back together.
  913. * @see fp_explode_assoc
  914. */
  915. function fp_join_assoc($arr, $glue = ",", $assign_sep = "-") {
  916. $rtn = "";
  917. foreach ($arr as $key => $val) {
  918. $rtn .= $key . $assign_sep . $val . $glue;
  919. }
  920. // Should be an extra glue character at the end we need to trim off.
  921. $rtn = rtrim($rtn, $glue);
  922. return $rtn;
  923. }
  924. /**
  925. * Takes a string (created by fp_join_assoc()) and re-creates the 1 dimensional assoc array.
  926. *
  927. * @see fp_join_assoc()
  928. */
  929. function fp_explode_assoc($string, $delim = ",", $assign_sep = "-") {
  930. $rtn = array();
  931. $temp = explode($delim, $string);
  932. foreach($temp as $line) {
  933. $line = trim($line);
  934. if ($line == "") continue;
  935. $temp2 = explode($assign_sep, $line);
  936. if (is_numeric($temp2[1])) {
  937. $temp2[1] = $temp2[1] * 1; // if its numeric anyway, make it have a numeric type.
  938. }
  939. $rtn[$temp2[0]] = $temp2[1];
  940. }
  941. return $rtn;
  942. }
  943. /**
  944. * Return the filepath to the module
  945. *
  946. * @param unknown_type $module
  947. * @param unknown_type $bool_include_file_system_path
  948. * @param unknown_type $bool_include_base_path
  949. * @return unknown
  950. */
  951. function fp_get_module_path($module, $bool_include_file_system_path = FALSE, $bool_include_base_path = TRUE) {
  952. $p = menu_get_module_path($module, $bool_include_file_system_path);
  953. if ($bool_include_file_system_path == FALSE && $bool_include_base_path == TRUE) {
  954. $p = base_path() . "/" . $p;
  955. }
  956. return $p;
  957. }
  958. /**
  959. * Convenience function to return the /files system path. Does NOT end with a trailing slash.
  960. *
  961. */
  962. function fp_get_files_path() {
  963. return $GLOBALS["fp_system_settings"]["file_system_path"] . "/custom/files";
  964. }
  965. /**
  966. * Simply returns the module's row from the modules table, if it exists.
  967. *
  968. * @param unknown_type $module
  969. */
  970. function fp_get_module_details($module) {
  971. // Special case if we are looking up flightpath itself
  972. if ($module == "flightpath") {
  973. $rtn = array(
  974. "info" => array("name" => t("FlightPath (Core)")),
  975. "version" => FLIGHTPATH_VERSION,
  976. );
  977. return $rtn;
  978. }
  979. $res = db_query("SELECT * FROM modules WHERE name = '?' ", $module);
  980. $cur = db_fetch_array($res);
  981. if ($test = unserialize($cur["info"])) {
  982. $cur["info"] = $test;
  983. }
  984. return $cur;
  985. }
  986. /**
  987. * This function will facilitate translations by using hook_translate()
  988. *
  989. * Allows variable replacements. Use like this:
  990. * t("@name's blob", array("@name" => "Richard"));
  991. * or simply
  992. * t("My blob"); if you don't need replacements.
  993. *
  994. * Not implimented yet.
  995. */
  996. function t($str, $vars = array()) {
  997. // Note: at the moment, this isn't being used, but should one day be set.
  998. @$langcode = isset($langcode) ? $langcode : $GLOBALS["fp_system_settings"]["language"];
  999. // First, change $str if any other modules implement hook_translate().
  1000. invoke_hook("translate", array(&$str, $langcode)); // str is passed by ref, so no return needed.
  1001. if (is_array($vars) && count($vars) > 0) {
  1002. foreach ($vars as $var => $val) {
  1003. // If var begins with %, it means we want to italicize the val.
  1004. if (strstr($var, "%")) {
  1005. $val = "<em>$val</em>";
  1006. }
  1007. $str = str_replace($var, $val, $str);
  1008. }
  1009. }
  1010. return $str;
  1011. }
  1012. /**
  1013. * Provides translation functionality when database is not available.
  1014. *
  1015. * Not implemented yet
  1016. */
  1017. function st($str, $vars = array()) {
  1018. // Not implemented yet. For now, just replicate t().
  1019. if (is_array($vars) && count($vars) > 0) {
  1020. foreach ($vars as $var => $val) {
  1021. // If var begins with %, it means we want to italicize the val.
  1022. if (strstr($var, "%")) {
  1023. $val = "<em>$val</em>";
  1024. }
  1025. $str = str_replace($var, $val, $str);
  1026. }
  1027. }
  1028. return $str;
  1029. }
  1030. /**
  1031. * Shortcut for getting the base_path variable from the global system settings.
  1032. */
  1033. function base_path() {
  1034. $p = $GLOBALS["fp_system_settings"]["base_path"];
  1035. // the base_path setting isn't set, so just use '.', meaning, start
  1036. // at the currect directory by default.
  1037. if ($p == "") {
  1038. $p = ".";
  1039. }
  1040. // if our base_path is simply "/" (meaning, we are hosted on a bare domain), then we should
  1041. // actually return nothing, so as not to cause errors with other systems.
  1042. if ($p == "/") {
  1043. $p = "";
  1044. }
  1045. return $p;
  1046. }
  1047. /**
  1048. * Convert a term ID into a description. Ex: 20095 = Spring of 2009.
  1049. */
  1050. function get_term_description($term_id, $bool_abbreviate = false) {
  1051. // Describe the term in plain english, for displays.
  1052. // Ex: "Fall of 2002."
  1053. $rtn = "";
  1054. // See if any modules would like to act on the term_id before we proceed.
  1055. invoke_hook("alter_term_id_prior_to_description", array(&$term_id, &$bool_abbreviate));
  1056. if (strstr($term_id, "1111"))
  1057. {
  1058. return "(data unavailable at this time)";
  1059. }
  1060. $year4 = intval(trim(substr($term_id, 0, 4)));
  1061. $year2 = intval(trim(substr($term_id, 2, 2)));
  1062. $ss = trim(substr($term_id, 4, strlen($term_id) - 4));
  1063. $year4p1 = $year4 + 1;
  1064. $year4m1 = $year4 - 1;
  1065. // left-pad these with 0's if needed.
  1066. $year2p1 = @fp_number_pad($year2 + 1, 2);
  1067. $year2m1 = @fp_number_pad($year2 - 1, 2);
  1068. // Let's look at the term_idStructure setting and attempt to match
  1069. // what we have been supplied.
  1070. // We expect this structure to look something like:
  1071. // [Y4]60, Spring, Spring of [Y4], Spr '[Y2]
  1072. // [Y4]40, Fall, Fall of [Y4-1], Fall '[Y2-1]
  1073. $temp = @$GLOBALS["fp_system_settings"]["term_id_structure"];
  1074. $structures = explode("\n", $temp);
  1075. foreach ($structures as $structure) {
  1076. // Perform the necessary replacement patterns on the structure.
  1077. $structure = str_replace("[Y4]", $year4, $structure);
  1078. $structure = str_replace("[Y2]", $year2, $structure);
  1079. $structure = str_replace("[Y4-1]", $year4m1, $structure);
  1080. $structure = str_replace("[Y2-1]", $year2m1, $structure);
  1081. $structure = str_replace("[Y4+1]", $year4p1, $structure);
  1082. $structure = str_replace("[Y2+1]", $year2p1, $structure);
  1083. // Now, break up the structure to make it easier to work with.
  1084. $tokens = explode(",", $structure);
  1085. $term_def = @trim($tokens[0]);
  1086. $full_description = @trim($tokens[2]);
  1087. $abbr_description = @trim($tokens[3]);
  1088. // Does our term_id match the termDef?
  1089. if ($term_def == $term_id) {
  1090. if ($bool_abbreviate) {
  1091. return $abbr_description;
  1092. }
  1093. else {
  1094. return $full_description;
  1095. }
  1096. }
  1097. }
  1098. // No descr could be found, so just display the term_id itself.
  1099. if (trim($rtn) == "") {
  1100. $rtn = $term_id;
  1101. }
  1102. return $rtn;
  1103. }
  1104. /**
  1105. * Redirect the user's browser to the specified internal path + query.
  1106. *
  1107. * We will automatically add the current_student_id variable, if it is not present
  1108. * in the query.
  1109. *
  1110. * Example uses:
  1111. * - fp_goto("admin");
  1112. * - fp_goto("test/1234");
  1113. * - fp_goto("test/123", "selection=yes&fruit=apple");
  1114. */
  1115. function fp_goto($path, $query = "") {
  1116. global $current_student_id;
  1117. if ($current_student_id != "" && !strstr($query, "current_student_id=")) {
  1118. // If the query doesn't contain the current_student_id, then add it in.
  1119. $query .= "&current_student_id=$current_student_id";
  1120. }
  1121. // Close the seesion before we try to redirect.
  1122. session_write_close();
  1123. if ($path == "<front>") {
  1124. $path = variable_get("front_page", "main");
  1125. }
  1126. header('Location: ' . fp_url($path, $query));
  1127. exit();
  1128. }
  1129. /**
  1130. * This works like Drupal's l() function for creating links.
  1131. * Ex: l("Click here for course search!", "tools/course-search", "abc=xyz&hello=goodbye", array("class" => "my-class"));
  1132. * Do not include preceeding or trailing slashes.
  1133. */
  1134. function l($text, $path, $query = "", $attributes = array()) {
  1135. $rtn = "";
  1136. if ($path == "<front>") {
  1137. $path = variable_get("front_page", "main");
  1138. }
  1139. // Does the path contain possible replacement patterns? (look for %)
  1140. if (strpos($path, "%") !== 0) {
  1141. $path = menu_convert_replacement_pattern($path);
  1142. }
  1143. // Does the query contain possible replacement patterns? (look for %)
  1144. if (strpos($query, "%") !== 0) {
  1145. $query = menu_convert_replacement_pattern($query);
  1146. }
  1147. $rtn .= '<a href="' . fp_url($path, $query) . '" ';
  1148. foreach ($attributes as $key => $value) {
  1149. $rtn .= $key . '="' . $value . '" ';
  1150. }
  1151. $rtn .= ">$text</a>";
  1152. return $rtn;
  1153. }
  1154. /**
  1155. * This function will take a path, ex: "admin/config/module"
  1156. * and a query, ex: "nid=5&whatever=yes"
  1157. * And join them together, respecting whether or not clean URL's are enabled.
  1158. */
  1159. function fp_url($path, $query = "", $include_base_path = TRUE) {
  1160. // If clean URLs are enabled, we should begin with a ?, if not, use an &
  1161. $rtn = "";
  1162. if ($include_base_path) {
  1163. $rtn .= base_path() . "/";
  1164. }
  1165. // Make sure that $rtn isn't now "//". This can happen if our
  1166. // site is hosted on a bare domain. Ex: http://fp.example.com
  1167. // And we have set the base_path to simply "/"
  1168. if ($rtn == "//") $rtn = "/";
  1169. $bool_clean_urls = variable_get("clean_urls", FALSE);
  1170. if (!$bool_clean_urls) {
  1171. // Clean URLs are NOT enabled! Let's make sure the URL contains "index.php?q="
  1172. $rtn .= "index.php?q=";
  1173. }
  1174. $rtn .= $path;
  1175. if ($query != "") {
  1176. // Is there a ? already in the $rtn? If not, add a ?. If so, use a &.
  1177. if (!strstr($rtn, "?")) {
  1178. $rtn .= "?";
  1179. }
  1180. else {
  1181. $rtn .= "&";
  1182. }
  1183. $rtn .= $query;
  1184. }
  1185. return $rtn;
  1186. }
  1187. /**
  1188. * This function will attempt to determine automatically
  1189. * if we are on a mobile device, and should therefor use the mobile
  1190. * theme and layout settings.
  1191. *
  1192. */
  1193. function fp_screen_is_mobile(){
  1194. if (isset($GLOBALS["fp_page_is_mobile"])) {
  1195. return $GLOBALS["fp_page_is_mobile"];
  1196. }
  1197. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  1198. $look_for = array(
  1199. "ipod",
  1200. "iphone",
  1201. "android",
  1202. "opera mini",
  1203. "blackberry",
  1204. "(pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)",
  1205. "(iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile)",
  1206. "(smartphone|iemobile)",
  1207. );
  1208. $is_mobile = FALSE;
  1209. foreach ($look_for as $test_agent) {
  1210. if (preg_match('/' . $test_agent . '/i',$user_agent)) {
  1211. $is_mobile = TRUE;
  1212. break;
  1213. }
  1214. }
  1215. $GLOBALS["fp_page_is_mobile"] = $is_mobile;
  1216. return $is_mobile;
  1217. } // ends function mobile_device_detect
  1218. ////////////////////////////////////////////////////////////////////
  1219. /////////////////////////////////////////////////////////////////////////////////////////////
  1220. /////////////////////////////////////////////////////////////////////////////////////////////
  1221. /////////////////////////////////////////////////////////////////////////////////////////////
  1222. /**
  1223. * Return an array of enabled modules which implement the provided hook.
  1224. * Do not include the preceeding "_" on the hook name!
  1225. */
  1226. function modules_implement_hook($hook = "example_hook_here") {
  1227. // Going to use a global array to keep track of what hooks exist.
  1228. if (!isset($GLOBALS['hook_cache'])) $GLOBALS['hook_cache'] = array();
  1229. // Have we already cached this list previously?
  1230. if (isset($GLOBALS['hook_cache'][$hook])) return $GLOBALS['hook_cache'][$hook];
  1231. // We have not already cached this, so let's look for it fresh...
  1232. $rtn = array();
  1233. // If we are in the install script, the GLOBALS array won't be set up, since there is no
  1234. // settings file yet. If that's the case, create a blank array so we don't have an issue.
  1235. if (!isset($GLOBALS['fp_system_settings'])) $GLOBALS['fp_system_settings'] = array();
  1236. if (!isset($GLOBALS['fp_system_settings']['modules'])) $GLOBALS['fp_system_settings']['modules'] = array();
  1237. foreach ($GLOBALS["fp_system_settings"]["modules"] as $module => $value) {
  1238. if (isset($value["enabled"]) && $value["enabled"] != "1") {
  1239. // Module is not enabled. Skip it.
  1240. continue;
  1241. }
  1242. if (function_exists($module . '_' . $hook)) {
  1243. $rtn[] = $module;
  1244. }
  1245. }
  1246. $GLOBALS['hook_cache'][$hook] = $rtn;
  1247. return $rtn;
  1248. }
  1249. /**
  1250. * Invoke all module hooks for the supplied hook.
  1251. */
  1252. function invoke_hook($hook = "example_hook_here", $params = array()) {
  1253. $rtn = array();
  1254. $modules = modules_implement_hook($hook);
  1255. foreach($modules as $module) {
  1256. $rtn[$module] = call_user_func_array($module . "_" . $hook, $params);
  1257. }
  1258. return $rtn;
  1259. }
  1260. /**
  1261. * This method will return a globally-set DatabaseHandler object,
  1262. * creating it if it does not already exist. This is for efficiency
  1263. * reasons, so every module or method does not need to keep creating
  1264. * databasehandler objects (and re-connecting to the database).
  1265. *
  1266. */
  1267. function get_global_database_handler() {
  1268. $db = @$GLOBALS["fp_global_database_handler"];
  1269. if (!is_object($db) || !is_object($db->pdo)) {
  1270. // Something isn't right, or it wasn't set correctly. Create a new connection.
  1271. $GLOBALS["fp_global_database_handler"] = new DatabaseHandler();
  1272. }
  1273. return $GLOBALS["fp_global_database_handler"];
  1274. }
  1275. /**
  1276. * Uses fp_add_message, but in this case, it also adds in the filename and line number
  1277. * which the message came from!
  1278. *
  1279. * Most useful for developers, tracking down issues. It's also only visible to administrators
  1280. * with the "view_fpm_debug" permission. So if you need to display a message only to admins,
  1281. * you can use fpm() as a shortcut.
  1282. *
  1283. * Note: If you attempt to fpm() an array
  1284. * or object with too many levels of nesting, it may run out of memory and your script will die.
  1285. */
  1286. function fpm($var, $max_levels = 20) {
  1287. if (!user_has_permission("view_fpm_debug")) {
  1288. return;
  1289. }
  1290. // Complex variable? Change it to print_r.
  1291. $str = $var;
  1292. if (is_array($str) || is_object($str)) {
  1293. $str = "<div class='fp-html-print-r-wrapper'>" . fp_html_print_r($str, "", 0, $max_levels) . "</div>";
  1294. }
  1295. $arr = debug_backtrace();
  1296. //pretty_print($arr);
  1297. $t = 0;
  1298. if (@$arr[1]['function'] == 'fpmct') {
  1299. $t = 1;
  1300. }
  1301. $file = $arr[$t]["file"];
  1302. if (strlen($file) > 70) {
  1303. $file = "..." . substr($file, strlen($file) - 70);
  1304. }
  1305. $str .= "<div class='fp-message-backtrace'>line {$arr[$t]["line"]}: $file</div>";
  1306. fp_add_message("&bull; " . $str);
  1307. }
  1308. /**
  1309. * Displays a depricated message on screen. Useful for tracking down
  1310. * when depricated functions are being used.
  1311. */
  1312. function depricated_message($str = "A depricated function has been called.") {
  1313. fpm($str);
  1314. fpm(debug_backtrace());
  1315. }
  1316. /**
  1317. * Convenience function, will use fp_debug_ct() to display
  1318. * a message, and the number of miliseconds since its last call.
  1319. */
  1320. function fpmct($val, $var = "") {
  1321. fpm(fp_debug_ct($val, $var));
  1322. }
  1323. /**
  1324. * Similar to print_r, this will return an HTML-friendly
  1325. * click-to-open system similar in design to Krumo.
  1326. */
  1327. function fp_html_print_r($var, $name = "", $cnt = 0, $max_levels = 20) {
  1328. $rtn = "";
  1329. if ($cnt > $max_levels) {
  1330. // Max levels deep. Deeper, and PHP might run
  1331. // out of memory or complain.
  1332. $rtn .= "<div class='fp-html-print-r-too-deep'>
  1333. " . t("Depth too great. To view deeper,
  1334. rephrase your fpm() call, starting at this depth.") . "
  1335. </div>";
  1336. return $rtn;
  1337. }
  1338. $type = gettype($var);
  1339. $rnd = md5(mt_rand(0, 999999) . microtime() . $type . $name);
  1340. if ($type == "boolean") {
  1341. $var = ($var == TRUE) ? "TRUE" : "FALSE";
  1342. }
  1343. $count = "";
  1344. if ($type == "string") {
  1345. $count = " - " . strlen($var) . " " . t("chars");
  1346. }
  1347. if ($type == "array" || $type == "object") {
  1348. if ($type == "array") {
  1349. $count = " - " . count($var) . " " . t("elements");
  1350. }
  1351. if ($type == "object") {
  1352. $count = " - " . get_class($var);
  1353. }
  1354. $rtn .= "<div class='fp-html-print-r-multi-row'>
  1355. <div class='fp-html-print-r-selector'
  1356. onClick='\$(\"#fp-html-print-r-var-value-$rnd\").toggle(\"medium\");'
  1357. >
  1358. <span class='fp-html-print-r-var-name'>$name</span>
  1359. <span class='fp-html-print-r-var-type'>($type$count)</span>
  1360. </div>
  1361. <div class='fp-html-print-r-var-value' id='fp-html-print-r-var-value-$rnd' style='display: none;'>";
  1362. foreach ($var as $key => $value) {
  1363. $rtn .= fp_html_print_r($value, $key, ($cnt + 1), $max_levels);
  1364. }
  1365. $rtn .= "</div>
  1366. </div>";
  1367. }
  1368. else if ($type == "string" && strlen($var) > 50) {
  1369. // If the variable is fairly long, we want to also make it a hide-to-show type field.
  1370. $rtn .= "<div class='fp-html-print-r-multi-row'>
  1371. <div
  1372. onClick='\$(\"#fp-html-print-r-var-value-$rnd\").toggle(\"medium\");'
  1373. >
  1374. <span class='fp-html-print-r-var-name'>$name</span>
  1375. <span class='fp-html-print-r-var-type'>($type$count)</span>
  1376. <span class='fp-html-print-r-var-value-abbr'>" . htmlentities(substr($var, 0, 50)) . "...</span>
  1377. </div>
  1378. <div class='fp-html-print-r-var-value' id='fp-html-print-r-var-value-$rnd' style='display: none;'>
  1379. ";
  1380. $rtn .= htmlentities($var);
  1381. $rtn .= "</div></div>";
  1382. }
  1383. else {
  1384. $html_val = $var;
  1385. if ($type != "resource") {
  1386. $html_val = htmlentities("" . $var);
  1387. }
  1388. $rtn .= "<div class='fp-html-print-r-single-row'>
  1389. <span class='fp-html-print-r-var-name'>$name</span>
  1390. <span class='fp-html-print-r-var-type'>($type$count)</span>
  1391. <span class='fp-html-print-r-var-value'>$html_val</span>
  1392. </div>";
  1393. }
  1394. return $rtn;
  1395. }
  1396. /**
  1397. * This is used usually when being viewed by a mobile device.
  1398. * It will shorten a catalog year range of 2008-2009 to just
  1399. * "08-09" or "2008-09" or even "09-2009".
  1400. *
  1401. * @param unknown_type $cat_range
  1402. */
  1403. function get_shorter_catalog_year_range($cat_range, $abbr_first = true, $abbr_second = true) {
  1404. $temp = explode("-", $cat_range);
  1405. $first = $temp[0];
  1406. $second = $temp[1];
  1407. if ($abbr_first) {
  1408. $first = substr($first, 2, 2);
  1409. }
  1410. if ($abbr_second) {
  1411. $second = substr($second, 2, 2);
  1412. }
  1413. return "$first-$second";
  1414. }
  1415. /**
  1416. * This will find and include the module in question, calling
  1417. * it's hook_init() function if it has one.
  1418. *
  1419. * Will return TRUE or FALSE for success or failure to include
  1420. * the module.
  1421. *
  1422. * If the use_module_path is set to some value, we will not attempt to use
  1423. * the setting for this module's path. Useful if we do not have the module in our
  1424. * modules table yet.
  1425. *
  1426. * Example use: include_module("course_search");
  1427. *
  1428. * @param string $module
  1429. */
  1430. function include_module($module, $bool_call_init = TRUE, $use_module_path = "") {
  1431. $system_path = trim($GLOBALS["fp_system_settings"]["file_system_path"]);
  1432. $module_path = $GLOBALS["fp_system_settings"]["modules"][$module]["path"];
  1433. if ($use_module_path != "") {
  1434. $module_path = $use_module_path;
  1435. }
  1436. if ($module_path != "") {
  1437. $path = $module_path . "/$module.module";
  1438. if (file_exists($system_path . "/" . $path)) {
  1439. require_once($system_path . "/" . $path);
  1440. }
  1441. else {
  1442. print "<br><b>Could not find module '$module' at '$system_path/$path'</b><br>";
  1443. }
  1444. // Now that we have included it, call the module's hook_init() method.
  1445. if ($bool_call_init) {
  1446. if (function_exists($module . "_init")) {
  1447. call_user_func($module . "_init");
  1448. }
  1449. }
  1450. return TRUE;
  1451. }
  1452. return FALSE;
  1453. }
  1454. /**
  1455. * Find and include the module's .install file, if it exists.
  1456. * Returns TRUE or FALSE if it was able to find & include the file.
  1457. */
  1458. function include_module_install($module, $path) {
  1459. $system_path = trim($GLOBALS["fp_system_settings"]["file_system_path"]);
  1460. $install_path = $path . "/$module.install";
  1461. if (file_exists($system_path . "/" . $install_path)) {
  1462. require_once($system_path . "/" . $install_path);
  1463. return TRUE;
  1464. }
  1465. return FALSE;
  1466. }
  1467. /**
  1468. * Creates a javascript "confirm" link, so when clicked it asks the user a question, then proceeds
  1469. * if they select OK. The main reason I want to do this is so I can pass the $question through
  1470. * my t() function. (do it when you call this function)
  1471. */
  1472. function fp_get_js_confirm_link($question, $action_if_yes, $link_text) {
  1473. $rtn = "";
  1474. $question = fp_reduce_whitespace($question);
  1475. $question = htmlentities($question, ENT_QUOTES);
  1476. $question = str_replace("\n", "\\n", $question);
  1477. $rtn .= "<a href='javascript: if(confirm(\"$question\")) { $action_if_yes; }'>$link_text</a>";
  1478. return $rtn;
  1479. }
  1480. /**
  1481. * Creates a javascript "prompt" link, which will ask the user a question.
  1482. *
  1483. * Similar to the fp_get_js_confirm_link function, but this is a prompt box
  1484. * which lets the user type in a response.
  1485. *
  1486. * @see fp_get_js_confirm_link
  1487. */
  1488. function fp_get_js_prompt_link($question, $default, $action_if_yes, $link_text) {
  1489. $rtn = "";
  1490. $question = fp_reduce_whitespace($question);
  1491. $question = htmlentities($question, ENT_QUOTES);
  1492. $question = str_replace("\n", "\\n", $question);
  1493. $rtn .= "<a href='javascript: var response = prompt(\"$question\", \"$default\");
  1494. if (response != null)
  1495. {
  1496. $action_if_yes ;
  1497. }
  1498. '>$link_text</a>";
  1499. return $rtn;
  1500. }
  1501. /**
  1502. * Creates a javascript "alert" link, which tells the user some message with javascript alert().
  1503. *
  1504. * Similar to the fp_get_js_confirm_link function, but this is a simple alert message,
  1505. * with no user input.
  1506. *
  1507. * @see fp_get_js_confirm_link
  1508. */
  1509. function fp_get_js_alert_link($message, $link_text = "", $extra_css_class = "") {
  1510. $rtn = "";
  1511. if ($link_text == "") {
  1512. $link_text = "[?]";
  1513. }
  1514. $message = fp_reduce_whitespace($message);
  1515. $message = str_replace("\n", "[NL]", $message);
  1516. $message = htmlentities($message, ENT_QUOTES);
  1517. $message = str_replace("&quot;", "\&quot;", $message);
  1518. $message = str_replace("[NL]", "\\n", $message);
  1519. $rtn .= "<a href='javascript: alert(\"" . $message . "\");' class='fp-alert-link $extra_css_class'>$link_text</a>";
  1520. return $rtn;
  1521. }
  1522. /**
  1523. * Simple helper function to reduce whitespace (like double-spaces)
  1524. *
  1525. * @param unknown_type $str
  1526. */
  1527. function fp_reduce_whitespace($str) {
  1528. // Cheap hack to get rid of whitespace
  1529. for ($t = 0; $t < 5; $t++) {
  1530. $str = str_replace(" ", " ", $str);
  1531. $str = str_replace("\n ", "\n", $str);
  1532. }
  1533. return $str;
  1534. }
  1535. /**
  1536. * Does the user have the specified role?
  1537. */
  1538. function user_has_role($role, $account = NULL) {
  1539. global $user;
  1540. if ($account == NULL) $account = $user;
  1541. // Admin always = TRUE
  1542. if ($account->id == 1) return TRUE;
  1543. // Check for other users...
  1544. if (in_array($role, $account->roles)) return TRUE;
  1545. return FALSE;
  1546. }
  1547. /**
  1548. * Returns TRUE or FALSE if the logged in user has access based on the
  1549. * permission supplied.
  1550. *
  1551. * @param String $permission
  1552. */
  1553. function user_has_permission($permission, $account = NULL) {
  1554. global $user;
  1555. if ($account == NULL) $account = $user;
  1556. //fpm("checking permission $permission");
  1557. // If the user is admin (id == 1) then they always have access.
  1558. if ($account->id == 1) return TRUE;
  1559. // Otherwise, simply check their permissions array.
  1560. if (in_array($permission, $account->permissions)) {
  1561. return TRUE;
  1562. }
  1563. return FALSE;
  1564. }
  1565. /**
  1566. * This looks at the global termIDStructure setting and returns back
  1567. * an array of only term suffixes (like 40, 60, mm, etc).
  1568. *
  1569. */
  1570. function get_term_id_suffixes() {
  1571. $rtn = array();
  1572. $temp = $GLOBALS["fp_system_settings"]["term_id_structure"];
  1573. $structures = explode("\n", $temp);
  1574. foreach ($structures as $structure) {
  1575. $tokens = explode(",", $structure);
  1576. $term_def = trim($tokens[0]);
  1577. // Get rid of the replacement pattern.
  1578. // Looks like: [Y4]40. We want the 40.
  1579. // Simply explode on "]"
  1580. $temp = explode("]", $term_def);
  1581. $rtn[] = trim($temp[1]);
  1582. }
  1583. return $rtn;
  1584. }
  1585. /**
  1586. * This function will read through all the modules' permissions and
  1587. * return back an array. Specifically, it retrieves arrays from each
  1588. * modules' hook_perm() function.
  1589. *
  1590. */
  1591. function get_modules_permissions() {
  1592. $rtn = array();
  1593. foreach ($GLOBALS["fp_system_settings"]["modules"] as $module => $value) {
  1594. if (isset($value["disabled"]) && $value["disabled"] == "yes") {
  1595. // Module is not enabled. Skip it.
  1596. continue;
  1597. }
  1598. if (function_exists($module . "_perm")) {
  1599. $rtn[$module][] = call_user_func($module . "_perm");
  1600. }
  1601. }
  1602. return $rtn;
  1603. }
  1604. /**
  1605. * Similar to get_modules_permissions, this will scan through all installed
  1606. * modules' hook_menu() functions, and assemble an array which is sorted
  1607. * by "location" and then by "weight".
  1608. *
  1609. */
  1610. function get_modules_menus() {
  1611. $menus = array();
  1612. foreach ($GLOBALS["fp_system_settings"]["modules"] as $module => $value) {
  1613. if (isset($value["disabled"]) && $value["disabled"] == "yes") {
  1614. // Module is not enabled. Skip it.
  1615. continue;
  1616. }
  1617. if (function_exists($module . "_menu")) {
  1618. $menus[] = call_user_func($module . "_menu");
  1619. }
  1620. }
  1621. // Let's re-order based on weight...
  1622. // Convert to a single dimensional array for easier sorting.
  1623. $temp = array();
  1624. foreach ($menus as $c => $value) {
  1625. foreach ($menus[$c] as $d => $menu_data) {
  1626. $w = $menu_data["weight"];
  1627. if ($w == "") $w = "0";
  1628. // We need to front-pad $w with zeros, so it is the same length
  1629. // for every entry. Otherwise it will not sort correctly.
  1630. $w = fp_number_pad($w, 10);
  1631. $temp[] = "$w~~$c~~$d";
  1632. }
  1633. }
  1634. //var_dump($temp);
  1635. // Now, sort $temp...
  1636. sort($temp);
  1637. //var_dump($temp);
  1638. // Now, go back through $temp and get our new array...
  1639. $new_array = array();
  1640. foreach ($temp as $t) {
  1641. $vals = explode("~~", $t);
  1642. $c = $vals[1];
  1643. $d = $vals[2];
  1644. // Place them into subarrays indexed by location
  1645. $new_array[$menus[$c][$d]["location"]][] = $menus[$c][$d];
  1646. }
  1647. return $new_array;
  1648. }
  1649. /**
  1650. * Simple function to left padd numbers with 0's.
  1651. * 1 becomes 001
  1652. * 20 becomes 020
  1653. * and so on.
  1654. *
  1655. * @param int $number
  1656. * @param int $n
  1657. * @return String
  1658. */
  1659. function fp_number_pad($number, $len) {
  1660. return str_pad((int) $number, $len, "0", STR_PAD_LEFT);
  1661. }
  1662. /**
  1663. * This simple function will take a number and truncate the number of decimals
  1664. * to the requested places. This can be used in place of number_format(), which *rounds*
  1665. * numbers.
  1666. *
  1667. * For example, number_format(1.99999, 2) gives you 2.00.
  1668. * But THIS function gives:
  1669. * fp_truncate_decimals(1.999999, 2) = 1.99
  1670. *
  1671. *
  1672. * @param unknown_type $places
  1673. */
  1674. function fp_truncate_decimals($num, $places = 2) {
  1675. // does $num contain a .? If not, add it on.
  1676. if (!strstr("" . $num, ".")) {
  1677. $num .= ".0";
  1678. }
  1679. // Break it by .
  1680. $temp = explode (".", "" . $num);
  1681. // Get just the decimals and trim 'em
  1682. $decimals = trim(substr($temp[1], 0, $places));
  1683. if (strlen($decimals) < $places) {
  1684. // Padd with zeros on the right!
  1685. $decimals = str_pad($decimals, $places, "0", STR_PAD_RIGHT);
  1686. }
  1687. $new_num = $temp[0] . "." . $decimals;
  1688. return $new_num;
  1689. }
  1690. /**
  1691. * Shortcut to fp_debug_current_time_millis()
  1692. *
  1693. * @see fp_debug_current_time_millis()
  1694. *
  1695. * @param unknown_type $debug_val
  1696. * @param unknown_type $var
  1697. * @return unknown
  1698. */
  1699. function fp_debug_ct($debug_val = "", $var = "")
  1700. { // Shortcut to the other function.
  1701. return fp_debug_current_time_millis($debug_val, false, $var);
  1702. }
  1703. /**
  1704. * When called repeatedly, this function will display a message along with a milisecond count
  1705. * out to the side. Very useful for developers to time function calls or queries, to see how long they
  1706. * are taking.
  1707. *
  1708. * For example:
  1709. * fp_debug_ct("starting query");
  1710. * db_query(".........") // whatever
  1711. * fp_debug_ct("finished query");
  1712. *
  1713. * On screen, that would display our messages, with time values, so we can see how many milliseconds
  1714. * it took to execute between calls of fp_debug_ct().
  1715. *
  1716. * @param String $debug_val
  1717. * The message to display on screen.
  1718. * @param boolean $show_current_time
  1719. * Should we display the current time as well?
  1720. * @param String $var
  1721. * Optional. Include a variable name so you can have more than one timer running
  1722. * at the same time.
  1723. * @return unknown
  1724. */
  1725. function fp_debug_current_time_millis($debug_val = "", $show_current_time = true, $var = "")
  1726. {
  1727. // Display the current time in milliseconds, and, if available,
  1728. // show how many milliseconds its been since the last time
  1729. // this function was called. This helps programmers tell how
  1730. // long a particular function takes to run. Just place a call
  1731. // to this function before and after the function call.
  1732. $rtn = "";
  1733. $debug_string = $debug_val;
  1734. if (is_array($debug_val) || is_object($debug_val)) {
  1735. $debug_string = "<pre>" . print_r($debug_val, true) . "</pre>";
  1736. }
  1737. $last_time = @($GLOBALS["current_time_millis" . $var]) * 1; //*1 forces numeric
  1738. $cur_time = microtime(true) * 1000;
  1739. $debug_string = "<span style='color:red;'>DEBUG:</span>
  1740. <span style='color:green;'>$debug_string</span>";
  1741. $rtn .= "<div style='background-color: white;'>$debug_string";
  1742. if ($last_time > 1)
  1743. {
  1744. $diff = round($cur_time - $last_time,2);
  1745. $rtn .= "<span style='color: blue;'> ($diff" . t("ms since last call") . "</span>";
  1746. } else {
  1747. // Start of clock...
  1748. $rtn .= "<span style='color: blue;'> --- </span>";
  1749. }
  1750. $rtn .= "</div>";
  1751. $GLOBALS["current_time_millis" . $var] = $cur_time;
  1752. $GLOBALS["current_time_millis"] = $cur_time;
  1753. return $rtn;
  1754. }

Functions

Namesort descending Description
arg Returns the component of the page's path.
base_path Shortcut for getting the base_path variable from the global system settings.
csv_to_array Simple function to split a basic CSV string, trim all elements, then return the resulting array.
depricated_message Displays a depricated message on screen. Useful for tracking down when depricated functions are being used.
filter_markup Filter HTML, allowing only certain tags, and removing dangerous attributes.
filter_xss This function is taken almost directly from Drupal 7's core code. It is used to help us filter out dangerous HTML which the user might type. From the D7 documentation:
filter_xss_attributes
filter_xss_bad_protocol
filter_xss_split Like the filter_xss function, this is taken from D7's _filter_xss_split function
fpm Uses fp_add_message, but in this case, it also adds in the filename and line number which the message came from!
fpmct Convenience function, will use fp_debug_ct() to display a message, and the number of miliseconds since its last call.
fp_add_body_class Add a CSS class to the body tag of the page. Useful for themeing later on.
fp_add_css Add an extra CSS file to the page with this function. Ex: fp_add_css(fp_get_module_path("admin") . '/css/admin.css');
fp_add_js Add extra javascript to the page.
fp_add_message Add a "message" to the top of the screen. Useful for short messages like "You have been logged out" or "Form submitted successfully."
fp_clear_cache Call all modules which implement hook_clear_cache
fp_debug_ct Shortcut to fp_debug_current_time_millis()
fp_debug_current_time_millis When called repeatedly, this function will display a message along with a milisecond count out to the side. Very useful for developers to time function calls or queries, to see how long they are taking.
fp_explode_assoc Takes a string (created by fp_join_assoc()) and re-creates the 1 dimensional assoc array.
fp_get_degree_classifications Return back an assoc array of our set degree classifications, separated by "level"
fp_get_degree_classification_details Returns back an assoc array for the supplied code. Looks like: $arr["level_num"] = number $arr["title"] = the title
fp_get_files_path Convenience function to return the /files system path. Does NOT end with a trailing slash.
fp_get_js_alert_link Creates a javascript "alert" link, which tells the user some message with javascript alert().
fp_get_js_confirm_link Creates a javascript "confirm" link, so when clicked it asks the user a question, then proceeds if they select OK. The main reason I want to do this is so I can pass the $question through my t() function. (do it when you call this function)
fp_get_js_prompt_link Creates a javascript "prompt" link, which will ask the user a question.
fp_get_machine_readable Simple function to convert a string into a machine-readable string.
fp_get_module_details Simply returns the module's row from the modules table, if it exists.
fp_get_module_path Return the filepath to the module
fp_get_random_string Returns a random string of length len.
fp_get_requirement_types Returns back an array of all the available requirement types (by code) that have been defined.
fp_goto Redirect the user's browser to the specified internal path + query.
fp_html_print_r Similar to print_r, this will return an HTML-friendly click-to-open system similar in design to Krumo.
fp_http_request Send a request through the Internet and return the result as an object.
fp_join_assoc This function will create a string from a 1 dimensional assoc array. Ex: arr = array("pet" => "dog", "name" => "Rex") will return: pet-dog,name-Rex under the default settings.
fp_load_degree This function provides a pass-thru to $d = new DegreePlan(args). However, it allows for quick caching look-up, so it should be used when possible instead of $x = new DegreePlan.
fp_mail Send an email. Drop-in replacement for PHP's mail() command, but can use SMTP protocol if enabled.
fp_no_html_xss Remove any possiblilty of a malicious attacker trying to inject nonsense. From: https://paragonie.com/blog/2015/06/preventing-xss-vulnerabilities-in-php...
fp_number_pad Simple function to left padd numbers with 0's. 1 becomes 001 20 becomes 020 and so on.
fp_reduce_whitespace Simple helper function to reduce whitespace (like double-spaces)
fp_screen_is_mobile This function will attempt to determine automatically if we are on a mobile device, and should therefor use the mobile theme and layout settings.
fp_set_page_sub_tabs Allows the programmer to define subtabs at the top of the page.
fp_set_page_tabs If this function is called, it will override any other page tabs which might be getting constructed. This lets the programmer, at run-time, completely control what tabs are at the top of the page.
fp_set_title Allows the programmer to set the title of the page, overwriting any default title.
fp_strip_dangerous_protocols
fp_token Returns back the site's "token", which is a simply md5 of some randomness. It is used primarily with forms, to ensure against cross-site forgeries. The site's token gets saved to the variables table, for later use. The idea is…
fp_truncate_decimals This simple function will take a number and truncate the number of decimals to the requested places. This can be used in place of number_format(), which *rounds* numbers.
fp_url This function will take a path, ex: "admin/config/module" and a query, ex: "nid=5&whatever=yes" And join them together, respecting whether or not clean URL's are enabled.
fp_validate_utf8
get_global_database_handler This method will return a globally-set DatabaseHandler object, creating it if it does not already exist. This is for efficiency reasons, so every module or method does not need to keep creating databasehandler objects (and re-connecting to the database).
get_modules_menus Similar to get_modules_permissions, this will scan through all installed modules' hook_menu() functions, and assemble an array which is sorted by "location" and then by "weight".
get_modules_permissions This function will read through all the modules' permissions and return back an array. Specifically, it retrieves arrays from each modules' hook_perm() function.
get_shorter_catalog_year_range This is used usually when being viewed by a mobile device. It will shorten a catalog year range of 2008-2009 to just "08-09" or "2008-09" or even "09-2009".
get_term_description Convert a term ID into a description. Ex: 20095 = Spring of 2009.
get_term_id_suffixes This looks at the global termIDStructure setting and returns back an array of only term suffixes (like 40, 60, mm, etc).
get_term_structures Return an array version of the term_id_structure field from the admin settings
include_module This will find and include the module in question, calling it's hook_init() function if it has one.
include_module_install Find and include the module's .install file, if it exists. Returns TRUE or FALSE if it was able to find & include the file.
invoke_hook Invoke all module hooks for the supplied hook.
l This works like Drupal's l() function for creating links. Ex: l("Click here for course search!", "tools/course-search", "abc=xyz&hello=goodbye", array("class" => "my-class")); Do not…
modules_implement_hook Return an array of enabled modules which implement the provided hook. Do not include the preceeding "_" on the hook name!
st Provides translation functionality when database is not available.
t This function will facilitate translations by using hook_translate()
timer_read Works with the timer_start() function to return how long it has been since the start.
timer_start Begin a microtime timer for later use.
user_has_permission Returns TRUE or FALSE if the logged in user has access based on the permission supplied.
user_has_role Does the user have the specified role?
_fp_error_handler This is our custom error handler, which will intercept PHP warnings, notices, etc, and let us display them, log them, etc.
_fp_map_php_error_code Map an error code into an Error word *