Fakoli Framework
functions.inc
Go to the documentation of this file.
1 <?php
2 /**************************************************************
3 
4  Copyright (c) 2007-2010 Sonjara, Inc
5 
6  Permission is hereby granted, free of charge, to any person
7  obtaining a copy of this software and associated documentation
8  files (the "Software"), to deal in the Software without
9  restriction, including without limitation the rights to use,
10  copy, modify, merge, publish, distribute, sublicense, and/or sell
11  copies of the Software, and to permit persons to whom the
12  Software is furnished to do so, subject to the following
13  conditions:
14 
15  The above copyright notice and this permission notice shall be
16  included in all copies or substantial portions of the Software.
17 
18  Except as contained in this notice, the name(s) of the above
19  copyright holders shall not be used in advertising or otherwise
20  to promote the sale, use or other dealings in this Software
21  without prior written authorization.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
25  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
27  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
28  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30  OTHER DEALINGS IN THE SOFTWARE.
31 
32 *****************************************************************/
33 
34 require_once realpath(dirname(__FILE__)."/document_handler.inc");
35 require_once realpath(dirname(__FILE__)."/date_utils.inc");
36 
52 function checkRole($role, $account = null)
53 {
54  global $user;
55 
56  if (!$account) $account = $user;
57 
58  if ($role == "" || $account->role == "super" || $account->role == "admin") return true;
59  if (!$account) return false;
60 
61  if ($role != "" && $account->role == "") return false;
62 
63  $userRoles = explode(",", $account->role);
64  foreach($userRoles as $ur)
65  {
66  if ($ur == "super" || $ur == "admin") return true;
67  $expr = "/\\b{$ur}\\b/";
68  $allowed = (preg_match($expr, $role) > 0) ? true : false;
69  if ($allowed) break;
70  }
71 
72  return $allowed;
73 }
74 
82 function hasRole($role, $account = null)
83 {
84  global $user;
85 
86  if (!$account) $account = $user;
87  if (!$role) return false;
88 
89  $expr = "/\\b{$role}\\b/";
90  return (preg_match($expr, $account->role) > 0) ? true : false;
91 }
92 
101 function filterByRole($items, $field)
102 {
103  $ret = array();
104  foreach($items as $item)
105  {
106  if (checkRole($item->get($field)))
107  {
108  $ret[] = $item;
109  }
110  }
111 
112  return $ret;
113 }
114 
118 $image_types = array( "image/jpeg", "image/gif", "image/x-png", "image/pjpeg" );
119 
126 function formatAddress()
127 {
128  $numargs = func_num_args();
129  $arg_list = func_get_args();
130  for ($i = 0; $i < $numargs; $i++)
131  {
132  if ($arg_list[$i]) echo $arg_list[$i]."<br>\n";
133  }
134 }
135 
136 
142 function formatCurrency($amount)
143 {
144  return "$".number_format($amount, 2, '.', ',');
145 }
146 
154 function formatDate($date)
155 {
156 
157  if ($date !='Jan 1 1900 12:00AM')
158  {
159  return date("F d, Y", strtotime($date));
160  }
161 }
162 
169 function formatMonthYear($date)
170 {
171 
172  if ($date !='Jan 1 1900 12:00AM')
173  {
174  return date("F Y", strtotime($date));
175  }
176 }
177 
183 function formatDateShort($date)
184 {
185  $text = "";
186 
187  if ($date)
188  {
189  $text = date("m/d/Y", strtotime($date));
190  }
191 
192  if ($text == "01/01/1900") $text = "";
193 
194  return $text;
195 }
196 
203 function formatDateLong($date)
204 {
205  $text = "";
206 
207  if ($date)
208  {
209  $text = date("l F j, Y", strtotime($date));
210  }
211 
212  return $text;
213 }
214 
223 function formatTimestamp($ts)
224 {
225  if (!$ts) return "N/A";
226 
227 
228  $yyyy = substr($ts,0,4);
229  $mm = substr($ts,4,2);
230  $dd = substr($ts,6,2);
231  $hh = substr($ts,8,2);
232  $mi = substr($ts,10,2);
233  $ss = substr($ts,12,2);
234 
235  if ($yyyy == '0000') return "N/A";
236 
237  return date("F d, Y", mktime($hh,$mi,$ss,$mm,$dd,$yyyy));
238 }
239 
240 /*
241  * Format timestamp with date in short format
242  * e.g., 1/2/2011 and include time in 12 hour format
243  * e.g., 10:15am
244  *
245  * Deprecated
246  */
247 function formatTimestampShort($ts)
248 {
249  if (!$ts) return "N/A";
250 
251  $yyyy = substr($ts,0,4);
252  $mm = substr($ts,4,2);
253  $dd = substr($ts,6,2);
254  $hh = substr($ts,8,2);
255  $mi = substr($ts,10,2);
256  $ss = substr($ts,12,2);
257 
258  if ($yyyy == '0000') return "N/A";
259 
260  return date("m/d/Y g:ia", mktime($hh,$mi,$ss,$mm,$dd,$yyyy));
261 
262 }
263 
272 function getMonthDays($m, $y)
273 {
274  $monthDays = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
275  $d = $monthDays[$m - 1];
276  if ($m == 2 && ($y % 4) == 0 && ($y % 100) != 0) $d++;
277 
278  return $d;
279 }
280 
289 function getMonthWeekDays($month, $year)
290 {
291  $weekDaysInMonth = array
292  (
293  28 => array(20, 20, 20, 20, 20, 20, 20),
294  29 => array(21, 21, 21, 21, 21, 20, 20),
295  30 => array(22, 22, 22, 22, 21, 20, 21),
296  31 => array(23, 23, 23, 22, 21, 21, 22)
297  );
298 
299  $days = getMonthDays($month, $year);
300  $date = strtotime("$year-$month-01");
301 
302  $first = date("N", $date) - 1;
303  return $weekDaysInMonth[$days][$first];
304 }
305 
311 function formatTime12($time)
312 {
313  return date("g:ia", $time);
314 }
315 
321 function formatDateTime12($time)
322 {
323  return date("m/d/Y g:ia", $time);
324 }
325 
326 /*
327  * Formats a datetime in date short format
328  * with hours on 12 hour clock, no seconds.
329  * If time component is 00:00:00, then
330  * don't return time.
331  *
332  * Deprecated
333  */
334 function formatDateTime12Hr($datetime)
335 {
336  $yyyy = substr($datetime,0,4);
337  $mm = substr($datetime,5,2);
338  $dd = substr($datetime,8,2);
339  $hh = substr($datetime,11,2);
340  $mi = substr($datetime,14,2);
341  $ss = "00";
342 
343  if ($yyyy == '0000') return "N/A";
344  if($hh == '00')
345  return date("m/d/Y", mktime($hh,$mi,$ss,$mm,$dd,$yyyy));
346  else
347  return date("m/d/Y g:ia", mktime($hh,$mi,$ss,$mm,$dd,$yyyy));
348 }
349 
350 /*
351  * If start and end dates are the same date
352  * then show the date followed by start and end time.
353  * Otherwise, show start date and time to end date
354  * and time.
355  *
356  * $start - datetime
357  * $end - datetime
358  *
359  * Deprecated
360  */
361 function formatStartDateToEndDate($start, $end)
362 {
363  $yyyy = substr($start,0,4);
364  $yyyy_2 = substr($end,0,4);
365  $mm = substr($start,5,2);
366  $mm_2 = substr($end,5,2);
367  $dd = substr($start,8,2);
368  $dd_2 = substr($end,8,2);
369 
370  if($yyyy != $yyyy_2 || $mm != $mm_2 || $dd != $dd_2)
371  {
372  return formatDateTime12Hr($start) ." &ndash; ". formatDateTime12Hr($end);
373  }
374  else
375  {
376  $hh = substr($start,11,2);
377  $mi = substr($start,14,2);
378  $hh_2 = substr($end,11,2);
379  $mi_2 = substr($end,14,2);
380  $s_time = date("g:ia", mktime($hh,$mi));
381  $e_time = date("g:ia", mktime($hh_2,$mi_2));
382  return formatDateShort($start) . " $s_time &ndash; $e_time";
383  }
384 }
385 
386 /*
387  * Given a phone number containing various
388  * separator characters between the numbers,
389  * clear out the separators and standardize
390  * the format as
391  * (xxx) xxx-xxxx
392  *
393  * tylerhall on 11/30/-1
394  * http://snipplr.com/view/25/format-phone-number/
395  *
396  * Deprecated
397  */
398 function format_phone($phone)
399 {
400  $phone = preg_replace("/[^0-9]/", "", $phone);
401 
402  if(strlen($phone) == 7)
403  return preg_replace("/([0-9]{3})([0-9]{4})/", "$1-$2", $phone);
404  elseif(strlen($phone) == 10)
405  return preg_replace("/([0-9]{3})([0-9]{3})([0-9]{4})/", "($1) $2-$3", $phone);
406  else
407  return $phone;
408 }
409 
410 
411 
416 function now()
417 {
418  return date('Y/m/d H:i:s');
419 }
420 
424 function today()
425 {
426  return date('Y-m-d');
427 }
428 
434 function jsSafe($str, $escapeEntities = false)
435 {
436  $str = str_replace("\\", "\\\\", $str);
437  $str = preg_replace("/'/","\'",$str);
438  $str = preg_replace('/"/','\"',$str);
439  $str = str_replace("\r", "", $str);
440  $str = str_replace("\n", "\\n", $str);
441  $str = str_replace("&#39;", "\'", $str);
442  $str = str_ireplace("</script>", "\\x3C/script\\x3E", $str);
443 
444  if ($escapeEntities)
445  {
446  $str = str_replace("&", "&amp;", $str);
447  }
448  return $str;
449 }
450 
451 function htmlsafe($str)
452 {
453  return htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
454 }
455 
456 function cleanHTMLTags($str)
457 {
458  return str_replace(array("<", ">"), array("&lt;", "&gt;"), $str);
459 }
460 
461 function HTMLToText($html)
462 {
463  $patterns = array("/<br>/", "/&nbsp;/", "/<p>/", "/<BR>/", "/<P>/");
464  $replacements = array("\r\n", " ", "\r\n", "\r\n", "\r\n");
465  $text = stripHTML(preg_replace($patterns, $replacements, $html));
466 
467  return $text;
468 }
469 
470 function cleanQuotes($str)
471 {
472  $chr_map = array(
473  // Windows codepage 1252
474  "\xC2\x82" => "'", // U+0082 U+201A single low-9 quotation mark
475  "\xC2\x84" => '"', // U+0084 U+201E double low-9 quotation mark
476  "\xC2\x8B" => "'", // U+008B U+2039 single left-pointing angle quotation mark
477  "\xC2\x91" => "'", // U+0091 U+2018 left single quotation mark
478  "\xC2\x92" => "'", // U+0092 U+2019 right single quotation mark
479  "\xC2\x93" => '"', // U+0093 U+201C left double quotation mark
480  "\xC2\x94" => '"', // U+0094 U+201D right double quotation mark
481  "\xC2\x9B" => "'", // U+009B U+203A single right-pointing angle quotation mark
482 
483  // Regular Unicode // U+0022 quotation mark (")
484  // U+0027 apostrophe (')
485  "\xC2\xAB" => '"', // U+00AB left-pointing double angle quotation mark
486  "\xC2\xBB" => '"', // U+00BB right-pointing double angle quotation mark
487  "\xE2\x80\x98" => "'", // U+2018 left single quotation mark
488  "\xE2\x80\x99" => "'", // U+2019 right single quotation mark
489  "\xE2\x80\x9A" => "'", // U+201A single low-9 quotation mark
490  "\xE2\x80\x9B" => "'", // U+201B single high-reversed-9 quotation mark
491  "\xE2\x80\x9C" => '"', // U+201C left double quotation mark
492  "\xE2\x80\x9D" => '"', // U+201D right double quotation mark
493  "\xE2\x80\x9E" => '"', // U+201E double low-9 quotation mark
494  "\xE2\x80\x9F" => '"', // U+201F double high-reversed-9 quotation mark
495  "\xE2\x80\xB9" => "'", // U+2039 single left-pointing angle quotation mark
496  "\xE2\x80\xBA" => "'", // U+203A single right-pointing angle quotation mark
497  );
498  $chr = array_keys ($chr_map); // but: for efficiency you should
499  $rpl = array_values($chr_map); // pre-calculate these two arrays
500  $str = str_replace($chr, $rpl, html_entity_decode($str, ENT_QUOTES, "UTF-8"));
501  return $str;
502 }
503 
511 function printMIMETypeList($types)
512 {
513  print "*";
514  //foreach ($types as $t)
515  //{
516  // if ($out) $out .= ",";
517  // $out .= $t;
518  //}
519 
520  //print $out;
521 }
522 
531 function getDocType($doc)
532 {
533  global $doc_extensions;
534 
535  $d = strtolower($doc);
536 
537  $ext = substr($d, strrpos($d, "."));
538 
539  $doc_type = $doc_extensions[$ext];
540 
541  if (!$doc_type) $doc_type = "Unknown";
542 
543  return $doc_type;
544 
545 }
546 
554 function getDocSize($doc)
555 {
556  global $config;
557 
558  // Find the file name for the file on disk
559 
560  $doc_file = $doc;
561 
562  $doc_file = str_replace($config["uploadurl"], $config["uploaddir"], $doc_file);
563 
564  // If the file exists, return its size
565 
566  if (file_exists($doc_file))
567  {
568  $doc_size = (int)(filesize($doc_file)/1024);
569  }
570  else
571  {
572  $doc_size = 0;
573  }
574 
575  return $doc_size;
576 }
577 
578 // ?Is getMIMEType supposed to be used instead of getMimeType??
579 // Doesn't seem to work.
586 function getDocMimeType($name)
587 {
588  global $doc_mime_types;
589 
590  $n = strtolower($name);
591  $extension = substr($n, strrpos($n, "."));
592  $type = $doc_mime_types[$extension];
593  if (!$type) $type="application/octet-stream";
594  return $type;
595 
596 }
597 
598 /*function getMimeType($name)
599 {
600  global $doc_mime_types;
601 
602  $n = strtolower($name);
603  $extension = substr($n, strrpos($n, "."));
604  $type = $doc_mime_types[$extension];
605  if (!$type) $type="application/octet-stream";
606  return $type;
607 }*/
608 
615 function getDocIcon($doc)
616 {
617  global $doc_icons;
618  $n = strtolower($doc);
619  $extension = substr($n, strrpos($n, "."));
620  $icon = $doc_icons[$extension];
621  if (!$icon) $icon = "/fakoli/images/file_icon.png";
622  return $icon;
623 }
624 
630 function checkNumeric($p)
631 {
632  if (is_array($p))
633  {
634  foreach($p as $v)
635  {
636  if ($v!="" && !is_numeric($v)) die("Invalid Parameter");
637  }
638  }
639  else if ($p!="" && !is_numeric($p)) die("Invalid Parameter");
640  return $p;
641 }
642 
649 function checkNumericList($p, $separator = ",")
650 {
651  if (is_array($p))
652  {
653  foreach($p as $v)
654  {
655  if ($v != "" & !preg_match("/^[0-9\\.{$separator}]*$/", $v)) die("Invalid Parameter");
656  }
657  }
658  else if ($p!="" && !preg_match("/^[0-9\\.{$separator}]*$/", $p)) die("Invalid Parameter");
659  return $p;
660 }
661 
666 function checkIdentifier($p)
667 {
668  return preg_match("/^[A-Za-z0-9_\\-\\.]*$/", $p) ? $p : die("Invalid Parameter");
669 }
670 
676 function checkValidEmail($e)
677 {
678  return (filter_var($e, FILTER_VALIDATE_EMAIL)) ? $e : die("Invalid Parameter");
679 }
680 
689 function color($count)
690 {
691 
692 $color = "#FFFFFF";
693 if ($count%2==0) $color = "#EEEEEE";
694 return $color;
695 }
696 
706 function ellipsis($txt, $max, $wholeWord = false)
707 {
708  if (strlen($txt) > $max)
709  {
710  if (!$wholeWord)
711  {
712  $txt = substr($txt, 0, $max)."...";
713  }
714  else
715  {
716  $txt = substr($txt, 0, $max);
717  $pos = strrpos($txt, " ");
718  if ($pos)
719  {
720  while($pos && $txt[$pos - 1] == '.')
721  {
722  $pos--;
723  }
724  }
725  $txt = substr($txt, 0, $pos)."...";
726  }
727  }
728 
729  return $txt;
730 }
731 
738 function rteSafe($strText) {
739  //returns safe code for preloading in the RTE
740  $tmpString = $strText;
741 
742  //convert all types of single quotes
743  $tmpString = str_replace(chr(145), chr(39), $tmpString);
744  $tmpString = str_replace(chr(146), chr(39), $tmpString);
745  $tmpString = str_replace("'", "&#39;", $tmpString);
746 
747  //convert all types of double quotes
748  $tmpString = str_replace(chr(147), chr(34), $tmpString);
749  $tmpString = str_replace(chr(148), chr(34), $tmpString);
750 // $tmpString = str_replace("\"", "\"", $tmpString);
751 
752  //replace carriage returns & line feeds
753  $tmpString = str_replace(chr(10), " ", $tmpString);
754  $tmpString = str_replace(chr(13), " ", $tmpString);
755 
756  return $tmpString;
757 }
758 
766 function rtePrepare($strText, $css)
767 {
768  global $_SERVER;
769  return rteSafe($strText);
770 }
771 
778 function rteClean($strText)
779 {
780  $patterns = array( "|<\\!\\-\\-\\s+HEADERS\\s+\\-\\->.*<\\!\\-\\-\\s+/HEADERS\\s+\\-\\->|i",
781  "/_base_href=\\\?\".*?\"/i",
782  "|href=\\\?\"http://www.sonjara.com/ictinedtoolkit|i",
783  "|src=\\\?\"http://www.sonjara.com\\/ictinedtoolkit|i",
784  "|href=\\\?\"http://www.ictinedtoolkit.org|i",
785  "|src=\\\?\"http://www.ictinedtoolkit.org|i");
786 
787  $subs = array( "", "", "href=\\\"..", "src=\\\"..", "href=\\\"..", "src=\\\"..");
788 
789  $strText = preg_replace($patterns, $subs, $strText);
790 
791  return $strText;
792 }
793 
801 function firstSentence($text)
802 {
803  $text = preg_replace("/<br>/i", " ", $text);
804  $text = preg_replace("/<.*?>/", "", $text);
805  $pos = strpos($text, ". ");
806  return ($pos) ? substr($text, 0, $pos + 1) : $text;
807 }
808 
809 function stripHTML($text)
810 {
811  /*$text = preg_replace("/<.*?>/", "", $text);*/
812  $text = strip_tags($text);
813 
814  // JDG 5/19/2011 - remove &ndash, &nbsp;
815  $text = html_entity_decode($text);
816  $text = trim($text);
817  return $text;
818 }
819 
827 function parseMultiParam($collection, $param)
828 {
829  $len = strlen($param);
830  $values = array();
831 
832  foreach($collection as $field => $value)
833  {
834  if (!strncmp($field, $param, $len))
835  {
836  $values[] = $value;
837  }
838  }
839 
840  return $values;
841 }
842 
850 function option($value, $text, $sel = "")
851 {
852  echo "<option value='$value'";
853  if ($sel === $value)
854  {
855  echo " selected";
856  }
857  echo ">$text</option>";
858 }
859 
860 
868 function str_option($value, $text, $sel = "")
869 {
870  $opt = "<option value='$value'";
871  if ($sel == $value)
872  {
873  $opt .= " selected";
874  }
875  $opt .= ">$text</option>";
876 
877  return $opt;
878 }
879 
884 function redirect($page)
885 {
886  global $_SERVER;
887  global $config;
888 
889  if ($config['prettyURLs'] === false)
890  {
891  $page = preg_replace("/^\\/?([\\w\\d_]+?)\\?/", "/page.php?identifier=$1&", $page);
892  $page = preg_replace("/^\\/?([\\w\\d_]+)$/", "/page.php?identifier=$1", $page);
893  }
894 
895  trace("redirect:: page is $page", 3);
896  $server = $config['http_host'];
897  if (!$server) $server="localhost:8080";
898 
899  //AJG: 3/9/11 - maintain current protocol by default
900 
901  $https = $_SERVER['HTTPS'];
902  if ($https == "off") $https = false;
903 
904  $protocol = $https ? "https" : "http";
905 
906  // JDG 9/27/10 - handle https
907  if (!strncmp($page, "http://", 7) OR (!strncmp($page, "https://", 8)))
908  {
909  $location = $page;
910  }
911  else if ($page[0] == '/')
912  {
913  $location = "$protocol://$server$page";
914  }
915 // else if (preg_match( "/^[\\w\\d_\\-]+$/", $page))
916 // {
917 // $location = "http://$server/$page";
918 // }
919  else
920  {
921  $directory = str_replace(DIRECTORY_SEPARATOR, '/', dirname($_SERVER['REQUEST_URI']));
922  if ($directory == "/") $directory = "";
923  $location = "$protocol://".$server.$directory."/".$page;
924  }
925 
926  header("Location: $location");
927  session_write_close();
928  exit;
929 }
930 
936 {
937  $qs = $_SERVER['QUERY_STRING'];
938 
939  $params = array();
940 
941  foreach($_GET as $key => $value)
942  {
943  if ($key != "identifier" && $key != "section") $params[$key] = $value;
944  }
945 
946  return http_build_query($params);
947 }
948 
949 $traceLevels = array(1 => "FATAL",
950  2 => "ERROR",
951  3 => "TRACE",
952  4 => "DEBUG",
953  5 => "NOTE*");
959 function trace($msg, $lvl, $callStack = null)
960 {
961  global $config;
962  global $traceLevels;
963 
964  if ($lvl <= $config["trace_level"])
965  {
966  switch($config["trace_detail"])
967  {
968  case TraceDetailFull:
969 
970  $callStack = $callStack ? $callStack : debug_backtrace();
971  if (count($callStack) > 1)
972  {
973  $callingFn = $callStack[1];
974  }
975  else
976  {
977  $callingFn = $callStack[0];
978  }
979  $file = str_replace($config["homedir"], "", $callingFn["file"]);
980  $ref = $file.":".$callingFn["line"]." ";
981  if ($callingFn["class"])
982  {
983  $ref .= $callingFn["class"].$callingFn["type"];
984  }
985 
986  $ref .= $callingFn["function"]."(): ";
987 
988  error_log("[".$traceLevels[$lvl]."] ".date("Y-m-d H:i:s")." : ".$_SERVER['REQUEST_URI']." : ".$ref." ".$msg."\r\n", 3, $config["trace_path"]);
989  break;
990 
991  case TraceDetailHigh:
992 
993  $callStack = $callStack ? $callStack : debug_backtrace();
994 
995  if (count($callStack) > 1)
996  {
997  $callingFn = $callStack[1];
998  }
999  else
1000  {
1001  $callingFn = $callStack[0];
1002  }
1003 
1004  $file = str_replace($config["homedir"], "", $callingFn["file"]);
1005  $ref = $file.":".$callingFn["line"]." ";
1006  if ($callingFn["class"])
1007  {
1008  $ref .= $callingFn["class"].$callingFn["type"];
1009  }
1010 
1011  $ref .= $callingFn["function"]."(): ";
1012 
1013  error_log("[".$traceLevels[$lvl]."] ".date("Y-m-d H:i:s")." : ".$ref." ".$msg."\r\n", 3, $config["trace_path"]);
1014  break;
1015 
1016 
1017  case TraceDetailMedium:
1018  error_log("[".$traceLevels[$lvl]."] ".date("Y-m-d H:i:s : ").$msg."\r\n", 3, $config["trace_path"]);
1019  break;
1020 
1021  case TraceDetailNormal:
1022  default:
1023 
1024  error_log("[".$traceLevels[$lvl]."] ".$msg."\r\n", 3, $config["trace_path"]);
1025  break;
1026  }
1027  }
1028 }
1029 
1030 
1037 function getScaledSize($size, $decimal_places = 1)
1038 {
1039  if (!$size) return "";
1040 
1041  $result = "";
1042  if ($size > 1073741824)
1043  {
1044  $result = sprintf("%.{$decimal_places}fGB", ($size / 1073741824));
1045  }
1046  else if ($size > 1048576)
1047  {
1048  $result = sprintf("%.{$decimal_places}fMB", ($size / 1048576));
1049  }
1050  else if ($size > 1024)
1051  {
1052  $result = intval($size / 1024)."KB";
1053  }
1054  else
1055  {
1056  $result = "$size bytes";
1057  }
1058 
1059  return $result;
1060 }
1061 
1062 function roundFormat($num,$abbrev = false)
1063 {
1064  if (!$num) return "";
1065 
1066  $result = "";
1067  if ($abbrev == false) {
1068  if ($num >= 1000000000) {
1069  $result = sprintf("%.1f billion", ($num / 1000000000));
1070  } elseif ($num >= 1000000) {
1071  $result = sprintf("%.1f million", ($num / 1000000));;
1072  } elseif ($num >= 1000) {
1073  $result = sprintf("%.0f,000", ($num / 1000));;
1074  }
1075  } else {
1076  if ($num >= 1000000000) {
1077  $result = sprintf("%.1f bn", ($num / 1000000000));
1078  } elseif ($num >= 1000000) {
1079  $result = sprintf("%.1f m", ($num / 1000000));;
1080  } elseif ($num >= 1000) {
1081  $result = sprintf("%.0f,000", ($num / 1000));
1082  }
1083  }
1084  return $result;
1085 }
1086 
1087 $_icons = array(".doc" => "/images/msword_icon.png",
1088  ".xls" => "/images/msexcel_icon.png",
1089  ".pdf" => "/images/pdf_icon.png",
1090  ".ppt" => "/images/ppt_icon.png",
1091  "default" => "/images/file_icon.png");
1092 
1098 function getIcon($file)
1099 {
1100  global $_icons;
1101  global $auto_form_defaults;
1102 
1103  $ext = substr($file, strrpos($file, "."));
1104  $icon = $_icons[$ext];
1105  trace("getIcon($file) $ext $icon", 3);
1106 
1107  if (!isset($icon))
1108  {
1109  $icon = $_icons["default"];
1110  }
1111 
1112  return $auto_form_defaults["componentPath"].$icon;
1113 }
1114 
1122 function includeRemote($host, $file)
1123 {
1124  $ch = curl_init("http://$host/$file");
1125  curl_setopt($ch, CURLOPT_HEADER, 0);
1126  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1127  $html = curl_exec($ch);
1128  curl_close($ch);
1129 
1130  $html = preg_replace("/(src|href)(=['\"])\//", "$1$2http://$host/", $html);
1131  echo $html;
1132 }
1133 
1141 function getRemote($url, $username = "", $password = "")
1142 {
1143  $url = canonicalizeURL($url);
1144  trace("Retrieving $url", 3);
1145  $ch = curl_init("$url");
1146  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1147  curl_setopt($ch, CURLOPT_HEADER, 0);
1148  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1149 
1150  if ($username)
1151  {
1152  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
1153  curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
1154  }
1155 
1156  $html = curl_exec($ch);
1157 
1158  if ($html === FALSE) throw new Exception(curl_error($ch));
1159 
1160  curl_close($ch);
1161 
1162  return $html;
1163 }
1164 
1170 function jsonRequest($url, $params = array())
1171 {
1172  $qs = array();
1173  foreach($params as $key => $value)
1174  {
1175  $qs[] = "$key=".urlencode($value);
1176  }
1177 
1178  $qs = implode("&", $qs);
1179 
1180  $api = "{$url}?{$qs}";
1181  trace($api);
1182  $response = getRemote($api);
1183  return json_decode($response);
1184 }
1185 
1195 function saveRemote($url, $saveAs)
1196 {
1197  trace("Downloading $url to $saveAs", 3);
1198 
1199  $ch = curl_init ($url);
1200  curl_setopt($ch, CURLOPT_HEADER, 0);
1201  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1202  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1203  curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
1204  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1205 
1206  $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
1207  $bytes = curl_exec($ch);
1208  curl_close ($ch);
1209 
1210  trace(count($bytes)." bytes recieved", 3);
1211 
1212  if(file_exists($saveAs))
1213  {
1214  unlink($saveAs);
1215  }
1216 
1217  file_put_contents($saveAs, $bytes);
1218 
1219  return $contentType;
1220 }
1221 
1227 function canonicalizeURL($url)
1228 {
1229  global $config;
1230 
1231  if (startsWith($url, "http://") || startsWith($url, "https://")) return $url;
1232 
1233 
1234  if (!startsWith($url, "/"))
1235  {
1236  $out = realpath($config['homedir']."/".$url);
1237  }
1238  else
1239  {
1240  $out = realpath($config['homedir'].$url);
1241  }
1242 
1243  $out = str_replace('\\', '/', $out);
1244 
1245  if (!startsWith($out, $config['homedir'])) return null;
1246  $out = str_replace($config['homedir'], "http://{$config['http_host']}", $out);
1247 
1248  trace("$url -> $out", 4);
1249  return $out;
1250 }
1251 
1258 function sanitizePath($path)
1259 {
1260  $path = str_replace("/", DIRECTORY_SEPARATOR, $path);
1261  $path = str_replace("\\", DIRECTORY_SEPARATOR, $path);
1262  $path = str_replace("..".DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path);
1263  $path = str_replace(DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, $path);
1264  return $path;
1265 }
1266 
1274 function postRemote($url, $data = null, $timeout = 30)
1275 {
1276  $ch = curl_init();
1277  curl_setopt($ch, CURLOPT_URL, $url);
1278  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1279  curl_setopt($ch, CURLOPT_HEADER, 0);
1280  curl_setopt($ch, CURLOPT_POST, 1);
1281  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
1282  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
1283  $result = trim(curl_exec($ch));
1284  curl_close($ch);
1285  return $result;
1286 }
1287 
1288 function postRemoteXML($url, $xml, $timeout = 30)
1289 {
1290  $ch = curl_init($url);
1291  curl_setopt($ch, CURLOPT_MUTE, 1);
1292  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
1293  curl_setopt($ch, CURLOPT_POST, 1);
1294  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
1295  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
1296  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1297  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
1298  $result = trim(curl_exec($ch));
1299  curl_close($ch);
1300  return $result;
1301 }
1302 
1308 function getRemoteHeaders($url)
1309 {
1310  $ch = curl_init();
1311  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1312  curl_setopt($ch, CURLOPT_HEADER, 1);
1313  curl_setopt($ch, CURLOPT_NOBODY, 1);
1314  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1315 
1316  $html = curl_exec($ch);
1317 
1318  curl_close($ch);
1319 
1320  return $html;
1321 }
1322 
1330 function wrapURL($url, $width = 120)
1331 {
1332  $text = str_replace(" ", "%20", $url);
1333  $text = str_replace("/", " ", $text);
1334  $text = wordwrap($text, $width, "/<br>", true);
1335  $text = str_replace(" ", "/", $text);
1336 
1337  return $text;
1338 }
1339 
1348 function codify($name)
1349 {
1350  $name = preg_replace("/[\\s\\W]+/", "_", $name);
1351  $name = str_replace("&", "and", $name);
1352  return $name;
1353 }
1354 
1362 function prettify($name)
1363 {
1364  $name = preg_replace("/([a-z])([A-Z0-9])/", "$1 $2", $name);
1365  $name = str_replace("_", " ", $name);
1366  $name = ucwords($name);
1367 
1368  return $name;
1369 }
1370 
1377 function pluralize($text, $count = 0)
1378 {
1379  if ($count == 1) return $text;
1380 
1381  $c = $text;
1382 
1383  if (preg_match("/[^aeiouAEIOU]y$/", $c))
1384  {
1385  $c = substr($c, 0, -1)."ies";
1386  }
1387  else if (preg_match("/(?:ss|[^aeiou]o|x|s|ch)$/", $c))
1388  {
1389  $c .= "es";
1390  }
1391  else
1392  {
1393  $c .= "s";
1394  }
1395 
1396  return $c;
1397 }
1398 
1405 function formatAsHTML($text)
1406 {
1407  if (preg_match("/<.*?>/", $text)) return $text;
1408  $text = str_replace("\n", "<br/>", $text);
1409 
1410  return $text;
1411 }
1412 
1419 function startsWith($text, $start)
1420 {
1421  return (strncmp($text, $start, strlen($start)) == 0);
1422 }
1423 
1430 function endsWith($text, $end)
1431 {
1432  return (substr($text, -strlen($end)) == $end);
1433 }
1434 
1440 function stripHTMLTags($text)
1441 {
1442  return preg_replace("/<.*?>/", "", $text);
1443 }
1444 
1452 function ordinalSuffix($n, $sup = false)
1453 {
1454  $suffix = "";
1455  if (is_numeric($n))
1456  {
1457  $n = trim($n);
1458 
1459  $lastTwo = substr($n, -2, 2);
1460  $lastOne = substr($n, -1, 1);
1461 
1462  if ($lastTwo == "11" || $lastTwo == "12" || $lastTwo == "13") $suffix = "th";
1463  else
1464  {
1465  switch($lastOne)
1466  {
1467  case "1": $suffix = "st"; break;
1468  case "2": $suffix = "nd"; break;
1469  case "3": $suffix = "rd"; break;
1470  default: $suffix = "th";
1471  }
1472  }
1473  }
1474  else return $n;
1475 
1476  // JDG 2/10/12 - fix was not returning value if $sup false
1477  if ($sup)
1478  return "$n<sup>$suffix</sup>";
1479  else
1480  return "$n$suffix";
1481 }
1482 
1488 function makeURL($uri)
1489 {
1490  if (!startsWith($uri, "http://")) $uri = "http://$uri";
1491  return $uri;
1492 }
1493 
1504 function formatCheckListItemsforView($options, $items)
1505 {
1506  if(count($options) == 0) return;
1507 
1508  $values = explode(",", $items);
1509 
1510  foreach($values as $value)
1511  {
1512  if(array_key_exists($value, $options))
1513  {
1514  $list .= $options[$value] . ", ";
1515  }
1516  else
1517  $list .= $value . ", ";
1518  }
1519 
1520  if(strlen($list) > 0)
1521  $list = substr($list, 0, strlen($list) - 2);
1522 
1523  return $list;
1524 }
1525 
1531 function baseURI($uri = null)
1532 {
1533  if (!$uri) $uri = $_SERVER['REQUEST_URI'];
1534  return preg_replace("/\\?.*$/", "", $uri);
1535 }
1536 
1543 function appendToQueryString($qs, $params)
1544 {
1545  if (!$qs)
1546  {
1547  $qs = "?$params";
1548  }
1549  else
1550  {
1551  $qs .= "&$params";
1552  }
1553 
1554  if ($qs[0] != "?") $qs = "?$qs";
1555  $qs = preg_replace("/&&+/", "&", $qs);
1556 
1557  return $qs;
1558 }
1559 
1561 
1567 {
1568  global $__urlRewriteParams;
1569 
1570  for($i = 0; $i < func_num_args(); ++$i)
1571  {
1572  $__urlRewriteParams[func_get_arg($i)] = true;
1573  }
1574 }
1575 
1584 function getFullQueryString($includePOST = true)
1585 {
1586  global $__urlRewriteParams;
1587 
1588  $qs = "";
1589  $sep = "?";
1590 
1591  foreach($_GET as $name => $value)
1592  {
1593  if (array_key_exists($name, $__urlRewriteParams)) continue;
1594 
1595  if (is_array($value))
1596  {
1597  foreach($value as $key => $val)
1598  {
1599  $qs .= $sep.urlencode($name)."[".urlencode($key)."]=".urlencode($val);
1600  $sep = "&";
1601  }
1602  }
1603  else
1604  {
1605  $qs .= $sep.urlencode($name)."=".urlencode($value);
1606  $sep = "&";
1607  }
1608  }
1609 
1610  if ($includePOST)
1611  {
1612  foreach($_POST as $name => $value)
1613  {
1614  if (is_array($value))
1615  {
1616  foreach($value as $key => $val)
1617  {
1618  $qs .= $sep.urlencode($name)."[".urlencode($key)."]=".urlencode($val);
1619  $sep = "&";
1620  }
1621  }
1622  else
1623  {
1624  $qs .= $sep.urlencode($name)."=".urlencode($value);
1625  $sep = "&";
1626  }
1627  }
1628  }
1629 
1630  return $qs;
1631 }
1632 
1633 /*
1634  * Convert a php array to a javascript array.
1635  *
1636  * @items - array of objects or simple array
1637  *
1638  * @field - if items is array of objects, the field
1639  * in the object to use to create the javascript array
1640  *
1641  * returns javascript array in the format "[cat, dog, spider]";
1642  */
1643 function toJSArray($items, $field = "")
1644 {
1645  $js_arr = "[";
1646  $first = true;
1647 
1648  if(count($items) == 0)
1649  return "";
1650 
1651  if(is_object($items[0]) && !$field)
1652  {
1653  $field = $items[0]->getPrimaryKey();
1654  }
1655 
1656  foreach($items as $item)
1657  {
1658  if (!$first) $js_arr .=", ";
1659  $js_arr .= (is_object($item) && $field) ? $item->$field : $item;
1660  $first = false;
1661  }
1662 
1663  $js_arr .= "]";
1664 
1665  return $js_arr;
1666 }
1667 
1675 function makeRandomString($len)
1676 {
1677  $str = "";
1678  for($i = 0; $i < $len; ++$i)
1679  {
1680  $str .= chr(ord('a') + rand(0, 25));
1681  }
1682 
1683  return $str;
1684 }
1685 
1692 function arrayFilterKey( $input, $callback )
1693 {
1694  if ( !is_array( $input ) )
1695  {
1696  return null;
1697  }
1698 
1699  if ( empty( $input ) )
1700  {
1701  return $input;
1702  }
1703 
1704  $filteredKeys = array_filter( array_keys( $input ), $callback );
1705 
1706  if ( empty( $filteredKeys ) )
1707  {
1708  return array();
1709  }
1710 
1711  $input = array_intersect_key( array_flip( $filteredKeys ), $input );
1712 
1713  return $input;
1714 }
1715 
1723 function ajaxReturn($msg = null)
1724 {
1725  if (class_exists(FakoliEarlyExit))
1726  {
1727  throw new FakoliEarlyExit($msg);
1728  }
1729  else
1730  {
1731  session_write_close();
1732  die($msg);
1733  }
1734 }
1735 
1742 function getMIMEType($filename)
1743 {
1744  $mime_types = array(
1745 
1746  'txt' => 'text/plain',
1747  'htm' => 'text/html',
1748  'html' => 'text/html',
1749  'css' => 'text/css',
1750  'js' => 'application/javascript',
1751  'json' => 'application/json',
1752  'xml' => 'application/xml',
1753  'swf' => 'application/x-shockwave-flash',
1754  'flv' => 'video/x-flv',
1755 
1756  // images
1757  'png' => 'image/png',
1758  'jpe' => 'image/jpeg',
1759  'jpeg' => 'image/jpeg',
1760  'jpg' => 'image/jpeg',
1761  'gif' => 'image/gif',
1762  'bmp' => 'image/bmp',
1763  'ico' => 'image/vnd.microsoft.icon',
1764  'tiff' => 'image/tiff',
1765  'tif' => 'image/tiff',
1766  'svg' => 'image/svg+xml',
1767  'svgz' => 'image/svg+xml',
1768 
1769  // archives
1770  'zip' => 'application/zip',
1771  'rar' => 'application/x-rar-compressed',
1772  'exe' => 'application/x-msdownload',
1773  'msi' => 'application/x-msdownload',
1774  'cab' => 'application/vnd.ms-cab-compressed',
1775 
1776  // audio/video
1777  'mp3' => 'audio/mpeg',
1778  'qt' => 'video/quicktime',
1779  'mov' => 'video/quicktime',
1780 
1781  // adobe
1782  'pdf' => 'application/pdf',
1783  'psd' => 'image/vnd.adobe.photoshop',
1784  'ai' => 'application/postscript',
1785  'eps' => 'application/postscript',
1786  'ps' => 'application/postscript',
1787 
1788  // ms office
1789  'doc' => 'application/msword',
1790  'rtf' => 'application/rtf',
1791  'xls' => 'application/vnd.ms-excel',
1792  'ppt' => 'application/vnd.ms-powerpoint',
1793 
1794  // open office
1795  'odt' => 'application/vnd.oasis.opendocument.text',
1796  'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
1797  );
1798 
1799  $ext = strtolower(array_pop(explode('.',$filename)));
1800 
1801  if (array_key_exists($ext, $mime_types))
1802  {
1803  return $mime_types[$ext];
1804  }
1805  elseif (function_exists('finfo_open'))
1806  {
1807  $finfo = finfo_open(FILEINFO_MIME);
1808  $mimetype = finfo_file($finfo, $filename);
1809  finfo_close($finfo);
1810  return $mimetype;
1811  }
1812  else
1813  {
1814  // Prevent other file types (such as source code or configuration files) from being served
1815  die("");
1816  }
1817 }
1818 
1824 function array_remove_keys($array)
1825 {
1826  for($i = 1; $i < func_num_args(); ++$i)
1827  {
1828  unset($array[func_get_arg($i)]);
1829  }
1830 
1831  return $array;
1832 }
1833 
1842 function xmlEntities($string)
1843 {
1844  if (defined(ENT_XML1)) return htmlentities($value, ENT_XML1);
1845 
1846  $result = '';
1847  foreach (str_split(utf8_decode(htmlspecialchars($string))) as $char)
1848  {
1849  $num = ord($char);
1850  if ($num > 127) {
1851  $result .= '&#' . $num . ';';
1852  } else {
1853  $result .= $char;
1854  }
1855  }
1856  return $result;
1857 }
1858 
1864 function encode7bit($text)
1865 {
1866  return mb_encode_mimeheader($text, "iso-8859-1", "Q");
1867 }
1868 
1873 function GUID()
1874 {
1875  if (function_exists('com_create_guid') === true)
1876  {
1877  return trim(com_create_guid(), '{}');
1878  }
1879 
1880  return '{'.sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)).'}';
1881 }
1882 
1887 function plainGUID()
1888 {
1889  return str_replace(array("{", "}", "-"), array("", "", ""), GUID());
1890 }
1891 
1895 function getBacktrace($ignore = 2)
1896 {
1897  $trace = '';
1898  foreach (debug_backtrace() as $k => $v) {
1899  if ($k < $ignore) {
1900  continue;
1901  }
1902 
1903  array_walk($v['args'], function (&$item, $key) {
1904  $item = var_export($item, true);
1905  });
1906 
1907  $trace .= '#' . ($k - $ignore) . ' ' . $v['file'] . '(' . $v['line'] . '): ' . (isset($v['class']) ? $v['class'] . '->' : '') . $v['function'] . '(' . implode(', ', $v['args']) . ')' . "\n";
1908  }
1909 
1910  return $trace;
1911 }
1912 
1918 function luhnTest($num)
1919 {
1920  $len = strlen($num);
1921  for ($i = $len-1; $i >= 0; $i--)
1922  {
1923  $ord = ord($num[$i]);
1924  if (($len - 1) & $i)
1925  {
1926  $sum += $ord;
1927  }
1928  else
1929  {
1930  $sum += $ord / 5 + (2 * $ord) % 10;
1931  }
1932  }
1933  return $sum % 10 == 0;
1934 }
1935 
1936 
1944 function countMatchingElements($a, $b)
1945 {
1946  $arr1 = explode(",", $a);
1947  $arr2 = explode(",", $b);
1948  return count(array_intersect($arr1, $arr2));
1949 }
1950 
1956 function printIncludes($html = true, $return = false)
1957 {
1958  if ($return)
1959  {
1960  ob_start();
1961  }
1962 
1963  $includes = get_included_files();
1964  if ($html) echo "<ul>\n";
1965  foreach($includes as $include)
1966  {
1967  if ($html)
1968  {
1969  echo "<li>$include</li>";
1970  }
1971  else
1972  {
1973  echo $include."\n";
1974  }
1975  }
1976  if ($html) echo "</ul>";
1977 
1978  if ($return)
1979  {
1980  $out = ob_get_contents();
1981  ob_end_clean();
1982  return $out;
1983  }
1984 }
1988 ?>
$__urlRewriteParams
Definition: functions.inc:1560
formatCheckListItemsforView($options, $items)
For dataitems that are rendered as CheckListFieldRenderer, this function converts the comma-delimited...
Definition: functions.inc:1504
pluralize($text, $count=0)
Takes a singular string and makes it plural.
Definition: functions.inc:1377
plainGUID()
Generates a version 4 GUID with no punctutation.
Definition: functions.inc:1887
formatTimestampShort($ts)
Definition: functions.inc:247
getRemote($url, $username="", $password="")
Retrieve the contents of the remote file at the given URL.
Definition: functions.inc:1141
canonicalizeURL($url)
Takes a URL and converts relative URLs to absolute URLs for the current site.
Definition: functions.inc:1227
$doc_mime_types
$image_types
MIME types allowable for image uploads.
Definition: functions.inc:118
getMIMEType($filename)
Definition: functions.inc:1742
baseURI($uri=null)
Returns the base URI for the current script, with the query string removed.
Definition: functions.inc:1531
getMonthWeekDays($month, $year)
Get the number of week days in the specified month.
Definition: functions.inc:289
getDocSize($doc)
Returns the file size in Kb for a document.
Definition: functions.inc:554
color($count)
Utility function to nicely format addresses, etc.
Definition: functions.inc:689
$doc_icons
getMonthDays($m, $y)
Returns the number of days in the specified month.
Definition: functions.inc:272
formatDateLong($date)
Definition: functions.inc:203
printIncludes($html=true, $return=false)
Outputs a list of all included files.
Definition: functions.inc:1956
parseMultiParam($collection, $param)
Parse out multiple values from the provided collection that have the same root name, and return the results in an array.
Definition: functions.inc:827
ellipsis($txt, $max, $wholeWord=false)
Truncate the supplied text at the given maximum length.
Definition: functions.inc:706
formatTime12($time)
Format a time in 12 hour clock, no seconds.
Definition: functions.inc:311
startsWith($text, $start)
Tests whether a string starts with a given sub-string.
Definition: functions.inc:1419
filterByRole($items, $field)
Filter the given list, retaining only the objects that match the current user&#39;s role profile in the g...
Definition: functions.inc:101
redirect($page)
Simplified redirect.
Definition: functions.inc:884
formatAddress()
Utility function to nicely format addresses, etc.
Definition: functions.inc:126
getFullQueryString($includePOST=true)
Generates a query string containing the values passed to this page.
Definition: functions.inc:1584
rteClean($strText)
Definition: functions.inc:778
option($value, $text, $sel="")
Write out an option tag, marking as selected if applicable.
Definition: functions.inc:850
endsWith($text, $end)
Tests whether a string ends with the given sub-string.
Definition: functions.inc:1430
$doc_extensions
Mapping of common file extensions to human-readable document type.
countMatchingElements($a, $b)
Determines the number of items that two comma-separated lists have in common.
Definition: functions.inc:1944
stripHTMLTags($text)
Removes all HTML tags from the specified string.
Definition: functions.inc:1440
getScaledSize($size, $decimal_places=1)
Get human-readable file size from raw number of bytes.
Definition: functions.inc:1037
printMIMETypeList($types)
Outputs the MIME type list based on an array of MIME types.
Definition: functions.inc:511
$_icons
Definition: functions.inc:1087
formatStartDateToEndDate($start, $end)
Definition: functions.inc:361
cleanHTMLTags($str)
Definition: functions.inc:456
now()
Returns the current time and date in a database compatible format.
Definition: functions.inc:416
encode7bit($text)
Encode text to 7-bit clean format, suitable for mail headers.
Definition: functions.inc:1864
rteSafe($strText)
Definition: functions.inc:738
getDocIcon($doc)
Definition: functions.inc:615
formatDateTime12Hr($datetime)
Definition: functions.inc:334
ajaxReturn($msg=null)
Returns a string output and exits the script cleanly.
Definition: functions.inc:1723
formatAsHTML($text)
Takes a string and formats it for display as HTML, removing any HTML tags it contains, and converting carriage returns to br tags.
Definition: functions.inc:1405
trace($msg, $lvl, $callStack=null)
Send output to the trace log.
Definition: functions.inc:959
formatDateShort($date)
Utility function to format date in short form (MM/DD/YYYY), with no time component.
Definition: functions.inc:183
formatMonthYear($date)
Definition: functions.inc:169
xmlEntities($string)
Function to provide html to XML entity renaming.
Definition: functions.inc:1842
makeRandomString($len)
Creates a randomized string of characters.
Definition: functions.inc:1675
firstSentence($text)
Returns the first sentence of the supplied text.
Definition: functions.inc:801
toJSArray($items, $field="")
Definition: functions.inc:1643
getIcon($file)
Retrieves a graphic icon appropriate for the given file.
Definition: functions.inc:1098
postRemote($url, $data=null, $timeout=30)
Make a POST request to a remote URL, returning the results.
Definition: functions.inc:1274
checkValidEmail($e)
Security helper function.
Definition: functions.inc:676
getDocType($doc)
Returns a human-readable type name for a document, based on the file extension of the supplied file n...
Definition: functions.inc:531
array_remove_keys($array)
Remove specified keys from the given array.
Definition: functions.inc:1824
checkIdentifier($p)
Security helper function.
Definition: functions.inc:666
postRemoteXML($url, $xml, $timeout=30)
Definition: functions.inc:1288
formatTimestamp($ts)
Utility function to format readable date from a timestamp (no time component).
Definition: functions.inc:223
makeURL($uri)
Ensures that the http:// protocol specifier prefix is present in the supplied string.
Definition: functions.inc:1488
getRemoteHeaders($url)
Retrieve the HTTP headers for the specified remote URL (excluding the body).
Definition: functions.inc:1308
formatCurrency($amount)
Utility function to format currency using $ and , and stuff.
Definition: functions.inc:142
getDocMimeType($name)
Definition: functions.inc:586
saveRemote($url, $saveAs)
Retreive the contents of the remote file at the given URL and save it to the specified location on th...
Definition: functions.inc:1195
prettify($name)
Takes a variable or field name and converts it into a human-readable version (assuming that the origi...
Definition: functions.inc:1362
codify($name)
Takes a text string and converts it into a code-compliant format, suitable for use as a variable name...
Definition: functions.inc:1348
formatDateTime12($time)
Format a date and time in 12 hour clock, no seconds.
Definition: functions.inc:321
today()
Returns today as a string.
Definition: functions.inc:424
rtePrepare($strText, $css)
Definition: functions.inc:766
checkNumeric($p)
Security helper function.
Definition: functions.inc:630
ordinalSuffix($n, $sup=false)
Adds the english ordinal suffix to a number.
Definition: functions.inc:1452
format_phone($phone)
Definition: functions.inc:398
stripHTML($text)
Definition: functions.inc:809
includeRemote($host, $file)
Includes the contents of the given remote file, with any relative src or href references converted to...
Definition: functions.inc:1122
checkNumericList($p, $separator=",")
Security helper function.
Definition: functions.inc:649
registerURLRewriteParameters()
Register any URL rewrite parameters that should be excluded from query string reformatting.
Definition: functions.inc:1566
GUID()
Generates a version 4 GUID.
Definition: functions.inc:1873
getBacktrace($ignore=2)
Returns a formatted backtrace suitable for debugging output.
Definition: functions.inc:1895
str_option($value, $text, $sel="")
Write out an option tag to a string, marking as selected if applicable.
Definition: functions.inc:868
checkRole($role, $account=null)
Check whether the user has one of a specified set of roles.
Definition: functions.inc:52
jsonRequest($url, $params=array())
Make a simple GET request to a JSON-return RESTful API, passing the supplied parameters.
Definition: functions.inc:1170
arrayFilterKey( $input, $callback)
Filter an array based on applying the specified callback to the keys.
Definition: functions.inc:1692
HTMLToText($html)
Definition: functions.inc:461
getCleanQueryString()
Returns the query string for the current page, cleaned of any Fakoli-related navigation parameters...
Definition: functions.inc:935
jsSafe($str, $escapeEntities=false)
Utility function to escape a string correctly for use in a Javascript client-side call...
Definition: functions.inc:434
htmlsafe($str)
Definition: functions.inc:451
luhnTest($num)
Performs a Luhn validity test for credit card or IMEI numbers.
Definition: functions.inc:1918
$traceLevels
Definition: functions.inc:949
hasRole($role, $account=null)
Determine whether a user has a specific role explicitly set.
Definition: functions.inc:82
cleanQuotes($str)
Definition: functions.inc:470
wrapURL($url, $width=120)
Break a URL at a given string position.
Definition: functions.inc:1330
formatDate($date)
Utility function to format date (no time component).
Definition: functions.inc:154
appendToQueryString($qs, $params)
Appends the specified parameters to the supplied query string.
Definition: functions.inc:1543
sanitizePath($path)
Sanitize a file path, removing relative path sections and ensuring the correct directory separator is...
Definition: functions.inc:1258
roundFormat($num, $abbrev=false)
Definition: functions.inc:1062