text.lib.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the text library for Chamilo.
  5. * Include/require it in your code to use its functionality.
  6. *
  7. * @package chamilo.library
  8. */
  9. /* FUNCTIONS */
  10. /**
  11. * function make_clickable($string)
  12. *
  13. * @desc completes url contained in the text with "<a href ...".
  14. * However the function simply returns the submitted text without any
  15. * transformation if it already contains some "<a href:" or "<img src=".
  16. * @param string $text text to be converted
  17. * @return text after conversion
  18. * @author Rewritten by Nathan Codding - Feb 6, 2001.
  19. * completed by Hugues Peeters - July 22, 2002
  20. *
  21. * Actually this function is taken from the PHP BB 1.4 script
  22. * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
  23. * to that URL
  24. * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
  25. * to http://www.xxxx.yyyy[/zzzz]
  26. * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
  27. * to that email address
  28. * - Only matches these 2 patterns either after a space, or at the beginning of a line
  29. *
  30. * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
  31. * have it require something like xxxx@yyyy.zzzz or such. We'll see.
  32. */
  33. function make_clickable($string) {
  34. // TODO: eregi_replace() is deprecated as of PHP 5.3
  35. if (!stristr($string, ' src=') && !stristr($string, ' href=')) {
  36. $string = eregi_replace("(https?|ftp)://([a-z0-9#?/&=._+:~%-]+)", "<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>", $string);
  37. $string = eregi_replace("([a-z0-9_.-]+@[a-z0-9.-]+)", "<a href=\"mailto:\\1\">\\1</a>", $string);
  38. }
  39. return $string;
  40. }
  41. /**
  42. * Formats the date according to the locale settings
  43. *
  44. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  45. * @author Christophe Gesché <gesche@ipm.ucl.ac.be>
  46. * originally inspired from from PhpMyAdmin
  47. * @param string $date_format date pattern
  48. * @param integer $time_stamp, default is NOW.
  49. * @return the formatted date
  50. */
  51. function format_locale_date($date_format, $time_stamp = -1, $language = null) {
  52. if ($time_stamp == -1) {
  53. $time_stamp = time();
  54. }
  55. return api_format_date($time_stamp, $date_format, $language);
  56. }
  57. /**
  58. * @desc This function does some parsing on the text that gets inputted. This parsing can be of any kind
  59. * LaTeX notation, Word Censoring, Glossary Terminology (extension will available soon), Musical Notations, ...
  60. * The inspiration for this filter function came from Moodle an phpBB who both use a similar approach
  61. * @param $input string. some text
  62. * @return $output string. some text that contains the parsed elements.
  63. * @example [tex]\sqrt(2)[/tex]
  64. * @author Patrick Cool <patrick.cool@UGent.be>
  65. * @version March 2OO6
  66. */
  67. function text_filter($input, $filter = true) {
  68. //$input = stripslashes($input);
  69. if ($filter) {
  70. // *** parse [tex]...[/tex] tags *** //
  71. // which will return techexplorer or image html depending on the capabilities of the
  72. // browser of the user (using some javascript that checks if the browser has the TechExplorer plugin installed or not)
  73. $input = _text_parse_tex($input);
  74. // *** parse [teximage]...[/teximage] tags *** //
  75. // these force the gif rendering of LaTeX using the mimetex gif renderer
  76. //$input=_text_parse_tex_image($input);
  77. // *** parse [texexplorer]...[/texexplorer] tags *** //
  78. // these force the texeplorer LaTeX notation
  79. $input = _text_parse_texexplorer($input);
  80. // *** Censor Words *** //
  81. // censor words. This function removes certain words by [censored]
  82. // this can be usefull when the campus is open to the world.
  83. // $input=text_censor_words($input);
  84. // *** parse [?]...[/?] tags *** //
  85. // for the glossary tool
  86. $input = _text_parse_glossary($input);
  87. // parse [wiki]...[/wiki] tags
  88. // this is for the coolwiki plugin.
  89. // $input=text_parse_wiki($input);
  90. // parse [tool]...[/tool] tags
  91. // this parse function adds a link to a certain tool
  92. // $input=text_parse_tool($input);
  93. // parse [user]...[/user] tags
  94. // parse [email]...[/email] tags
  95. // parse [code]...[/code] tags
  96. }
  97. return $input;
  98. }
  99. /**
  100. * Applies parsing for tex commandos that are seperated by [tex]
  101. * [/tex] to make it readable for techexplorer plugin.
  102. * This function should not be accessed directly but should be accesse through the text_filter function
  103. * @param string $text The text to parse
  104. * @return string The text after parsing.
  105. * @author Patrick Cool <patrick.cool@UGent.be>
  106. * @version June 2004
  107. */
  108. function _text_parse_tex($textext) {
  109. //$textext = str_replace(array ("[tex]", "[/tex]"), array ('[*****]', '[/*****]'), $textext);
  110. //$textext=stripslashes($texttext);
  111. $input_array = preg_split("/(\[tex]|\[\/tex])/", $textext, -1, PREG_SPLIT_DELIM_CAPTURE);
  112. foreach ($input_array as $key => $value) {
  113. if ($key > 0 && $input_array[$key - 1] == '[tex]' AND $input_array[$key + 1] == '[/tex]') {
  114. $input_array[$key] = latex_gif_renderer($value);
  115. unset($input_array[$key - 1]);
  116. unset($input_array[$key + 1]);
  117. //echo 'LaTeX: <embed type="application/x-techexplorer" texdata="'.stripslashes($value).'" autosize="true" pluginspage="http://www.integretechpub.com/techexplorer/"><br />';
  118. }
  119. }
  120. $output=implode('',$input_array);
  121. return $output;
  122. }
  123. /**
  124. * Applies parsing for tex commandos that are seperated by [tex]
  125. * [/tex] to make it readable for techexplorer plugin.
  126. * This function should not be accessed directly but should be accesse through the text_filter function
  127. * @param string $text The text to parse
  128. * @return string The text after parsing.
  129. * @author Patrick Cool <patrick.cool@UGent.be>
  130. * @version June 2004
  131. */
  132. function _text_parse_texexplorer($textext) {
  133. if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  134. $textext = str_replace(array("[texexplorer]", "[/texexplorer]"), array("<object classid=\"clsid:5AFAB315-AD87-11D3-98BB-002035EFB1A4\"><param name=\"autosize\" value=\"true\" /><param name=\"DataType\" value=\"0\" /><param name=\"Data\" value=\"", "\" /></object>"), $textext);
  135. } else {
  136. $textext = str_replace(array("[texexplorer]", "[/texexplorer]"), array("<embed type=\"application/x-techexplorer\" texdata=\"", "\" autosize=\"true\" pluginspage=\"http://www.integretechpub.com/techexplorer/\">"), $textext);
  137. }
  138. return $textext;
  139. }
  140. /**
  141. * This function should not be accessed directly but should be accesse through the text_filter function
  142. * @author Patrick Cool <patrick.cool@UGent.be>
  143. */
  144. function _text_parse_glossary($input) {
  145. return $input;
  146. }
  147. /**
  148. * @desc this function makes a valid link to a different tool
  149. * This function should not be accessed directly but should be accesse through the text_filter function
  150. * @author Patrick Cool <patrick.cool@UGent.be>
  151. */
  152. function _text_parse_tool($input) {
  153. // an array with all the valid tools
  154. $tools[] = array(TOOL_ANNOUNCEMENT, 'announcements/announcements.php');
  155. $tools[] = array(TOOL_CALENDAR_EVENT, 'calendar/agenda.php');
  156. // check if the name between the [tool] [/tool] tags is a valid one
  157. }
  158. /**
  159. * render LaTeX code into a gif or retrieve a cached version of the gif
  160. * @author Patrick Cool <patrick.cool@UGent.be> Ghent University
  161. */
  162. function latex_gif_renderer($latex_code) {
  163. global $_course;
  164. // setting the paths and filenames
  165. $mimetex_path = api_get_path(LIBRARY_PATH).'mimetex/';
  166. $temp_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/temp/';
  167. $latex_filename = md5($latex_code).'.gif';
  168. if (!file_exists($temp_path.$latex_filename) OR isset($_GET['render'])) {
  169. if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
  170. $mimetex_command = $mimetex_path.'mimetex.exe -e "'.$temp_path.md5($latex_code).'.gif" '.escapeshellarg($latex_code).'';
  171. } else {
  172. $mimetex_command = $mimetex_path.'mimetex.linux -e "'.$temp_path.md5($latex_code).'.gif" '.escapeshellarg($latex_code);
  173. }
  174. exec($mimetex_command);
  175. //echo 'volgende shell commando werd uitgevoerd:<br /><pre>'.$mimetex_command.'</pre><hr>';
  176. }
  177. $return = "<a href=\"\" onclick=\"javascript: newWindow=window.open('".api_get_path(WEB_CODE_PATH)."inc/latex.php?code=".urlencode($latex_code)."&amp;filename=$latex_filename','latexCode','toolbar=no,location=no,scrollbars=yes,resizable=yes,status=yes,width=375,height=250,left=200,top=100');\">";
  178. $return .= '<img src="'.api_get_path(WEB_COURSE_PATH).$_course['path'].'/temp/'.$latex_filename.'" alt="'.$latex_code.'" border="0" /></a>';
  179. return $return;
  180. }
  181. /**
  182. * This functions cuts a paragraph
  183. * i.e cut('Merry Xmas from Lima',13) = "Merry Xmas fr..."
  184. * @param string the text to "cut"
  185. * @param int count of chars
  186. * @param bool Whether to embed in a <span title="...">...</span>
  187. * @return string
  188. * */
  189. function cut($text, $maxchar, $embed = false) {
  190. if (api_strlen($text) > $maxchar) {
  191. if ($embed) {
  192. return '<span title="'.$text.'">'.api_substr($text, 0, $maxchar).'...</span>';
  193. }
  194. return api_substr($text, 0, $maxchar).'...' ;
  195. }
  196. return $text;
  197. }
  198. /**
  199. * Show a number as only integers if no decimals, but will show 2 decimals if exist.
  200. *
  201. * @param mixed number to convert
  202. * @param int decimal points 0=never, 1=if needed, 2=always
  203. * @return mixed an integer or a float depends on the parameter
  204. */
  205. function float_format($number, $flag = 1) {
  206. if (is_numeric($number)) { // a number
  207. if (!$number) { // zero
  208. $result = ($flag == 2 ? '0.00' : '0'); // output zero
  209. } else { // value
  210. if (floor($number) == $number) { // whole number
  211. $result = number_format($number, ($flag == 2 ? 2 : 0)); // format
  212. } else { // cents
  213. $result = number_format(round($number, 2), ($flag == 0 ? 0 : 2)); // format
  214. } // integer or decimal
  215. } // value
  216. return $result;
  217. }
  218. }
  219. /**
  220. * Function to obtain last week timestamps
  221. * @return array times for every day inside week
  222. */
  223. function get_last_week() {
  224. $week = date('W');
  225. $year = date('Y');
  226. $lastweek = $week - 1;
  227. if ($lastweek == 0) {
  228. $week = 52;
  229. $year--;
  230. }
  231. $lastweek = sprintf("%02d", $lastweek);
  232. for ($i=1; $i<=7; $i++) {
  233. $arrdays[] = strtotime("$year"."W$lastweek"."$i");
  234. }
  235. return $arrdays;
  236. }