text.lib.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * Truncates a string.
  12. *
  13. * @author Brouckaert Olivier
  14. * @param string $text The text to truncate.
  15. * @param integer $length The approximate desired length. The length of the suffix below is to be added to have the total length of the result string.
  16. * @param string $suffix A suffix to be added as a replacement.
  17. * @param string $encoding (optional) The encoding to be used. If it is omitted, the platform character set will be used by default.
  18. * @param boolean $middle If this parameter is true, truncation is done in the middle of the string.
  19. * @return string Truncated string, decorated with the given suffix (replacement).
  20. */
  21. function api_trunc_str($text, $length = 30, $suffix = '...', $middle = false, $encoding = null) {
  22. if (empty($encoding)) {
  23. $encoding = api_get_system_encoding();
  24. }
  25. $text_length = api_strlen($text, $encoding);
  26. if ($text_length <= $length) {
  27. return $text;
  28. }
  29. if ($middle) {
  30. return rtrim(api_substr($text, 0, round($length / 2), $encoding)).$suffix.ltrim(api_substr($text, - round($length / 2), $text_length, $encoding));
  31. }
  32. return rtrim(api_substr($text, 0, $length, $encoding)).$suffix;
  33. }
  34. /**
  35. * Handling simple and double apostrofe in order that strings be stored properly in database
  36. *
  37. * @author Denes Nagy
  38. * @param string variable - the variable to be revised
  39. */
  40. function domesticate($input) {
  41. $input = stripslashes($input);
  42. $input = str_replace("'", "''", $input);
  43. $input = str_replace('"', "''", $input);
  44. return ($input);
  45. }
  46. /**
  47. * function make_clickable($string)
  48. *
  49. * @desc Completes url contained in the text with "<a href ...".
  50. * However the function simply returns the submitted text without any
  51. * transformation if it already contains some "<a href:" or "<img src=".
  52. * @param string $text text to be converted
  53. * @return text after conversion
  54. * @author Rewritten by Nathan Codding - Feb 6, 2001.
  55. * completed by Hugues Peeters - July 22, 2002
  56. *
  57. * Actually this function is taken from the PHP BB 1.4 script
  58. * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
  59. * to that URL
  60. * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
  61. * to http://www.xxxx.yyyy[/zzzz]
  62. * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
  63. * to that email address
  64. * - Only matches these 2 patterns either after a space, or at the beginning of a line
  65. *
  66. * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
  67. * have it require something like xxxx@yyyy.zzzz or such. We'll see.
  68. */
  69. function make_clickable($string) {
  70. // TODO: eregi_replace() is deprecated as of PHP 5.3
  71. if (!stristr($string, ' src=') && !stristr($string, ' href=')) {
  72. $string = eregi_replace("(https?|ftp)://([a-z0-9#?/&=._+:~%-]+)", "<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>", $string);
  73. $string = eregi_replace("([a-z0-9_.-]+@[a-z0-9.-]+)", "<a href=\"mailto:\\1\">\\1</a>", $string);
  74. }
  75. return $string;
  76. }
  77. /**
  78. * Applies parsing the content for tex commands that are separated by [tex]
  79. * [/tex] to make it readable for techexplorer plugin.
  80. * @param string $text The text to parse
  81. * @return string The text after parsing.
  82. * @author Patrick Cool <patrick.cool@UGent.be>
  83. * @version June 2004
  84. */
  85. function api_parse_tex($textext) {
  86. if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
  87. return str_replace(array('[tex]', '[/tex]'), 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);
  88. }
  89. return str_replace(array('[tex]', '[/tex]'), array("<embed type=\"application/x-techexplorer\" texdata=\"", "\" autosize=\"true\" pluginspage=\"http://www.integretechpub.com/techexplorer/\">"), $textext);
  90. }
  91. /**
  92. * @desc This function does some parsing on the text that gets inputted. This parsing can be of any kind
  93. * LaTeX notation, Word Censoring, Glossary Terminology (extension will available soon), Musical Notations, ...
  94. * The inspiration for this filter function came from Moodle an phpBB who both use a similar approach
  95. * @param $input string. some text
  96. * @return $output string. some text that contains the parsed elements.
  97. * @example [tex]\sqrt(2)[/tex]
  98. * @author Patrick Cool <patrick.cool@UGent.be>
  99. * @version March 2OO6
  100. */
  101. function text_filter($input, $filter = true) {
  102. //$input = stripslashes($input);
  103. if ($filter) {
  104. // *** parse [tex]...[/tex] tags *** //
  105. // which will return techexplorer or image html depending on the capabilities of the
  106. // browser of the user (using some javascript that checks if the browser has the TechExplorer plugin installed or not)
  107. $input = _text_parse_tex($input);
  108. // *** parse [teximage]...[/teximage] tags *** //
  109. // these force the gif rendering of LaTeX using the mimetex gif renderer
  110. //$input=_text_parse_tex_image($input);
  111. // *** parse [texexplorer]...[/texexplorer] tags *** //
  112. // these force the texeplorer LaTeX notation
  113. $input = _text_parse_texexplorer($input);
  114. // *** Censor Words *** //
  115. // censor words. This function removes certain words by [censored]
  116. // this can be usefull when the campus is open to the world.
  117. // $input=text_censor_words($input);
  118. // *** parse [?]...[/?] tags *** //
  119. // for the glossary tool
  120. $input = _text_parse_glossary($input);
  121. // parse [wiki]...[/wiki] tags
  122. // this is for the coolwiki plugin.
  123. // $input=text_parse_wiki($input);
  124. // parse [tool]...[/tool] tags
  125. // this parse function adds a link to a certain tool
  126. // $input=text_parse_tool($input);
  127. // parse [user]...[/user] tags
  128. // parse [email]...[/email] tags
  129. // parse [code]...[/code] tags
  130. }
  131. return $input;
  132. }
  133. /**
  134. * Applies parsing for tex commands that are separated by [tex]
  135. * [/tex] to make it readable for techexplorer plugin.
  136. * This function should not be accessed directly but should be accesse through the text_filter function
  137. * @param string $text The text to parse
  138. * @return string The text after parsing.
  139. * @author Patrick Cool <patrick.cool@UGent.be>
  140. * @version June 2004
  141. */
  142. function _text_parse_tex($textext) {
  143. //$textext = str_replace(array ("[tex]", "[/tex]"), array ('[*****]', '[/*****]'), $textext);
  144. //$textext = stripslashes($texttext);
  145. $input_array = preg_split("/(\[tex]|\[\/tex])/", $textext, -1, PREG_SPLIT_DELIM_CAPTURE);
  146. foreach ($input_array as $key => $value) {
  147. if ($key > 0 && $input_array[$key - 1] == '[tex]' AND $input_array[$key + 1] == '[/tex]') {
  148. $input_array[$key] = latex_gif_renderer($value);
  149. unset($input_array[$key - 1]);
  150. unset($input_array[$key + 1]);
  151. //echo 'LaTeX: <embed type="application/x-techexplorer" texdata="'.stripslashes($value).'" autosize="true" pluginspage="http://www.integretechpub.com/techexplorer/"><br />';
  152. }
  153. }
  154. $output = implode('',$input_array);
  155. return $output;
  156. }
  157. /**
  158. * Applies parsing for tex commandos that are seperated by [tex]
  159. * [/tex] to make it readable for techexplorer plugin.
  160. * This function should not be accessed directly but should be accesse through the text_filter function
  161. * @param string $text The text to parse
  162. * @return string The text after parsing.
  163. * @author Patrick Cool <patrick.cool@UGent.be>
  164. * @version June 2004
  165. */
  166. function _text_parse_texexplorer($textext) {
  167. if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) {
  168. $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);
  169. } else {
  170. $textext = str_replace(array("[texexplorer]", "[/texexplorer]"), array("<embed type=\"application/x-techexplorer\" texdata=\"", "\" autosize=\"true\" pluginspage=\"http://www.integretechpub.com/techexplorer/\">"), $textext);
  171. }
  172. return $textext;
  173. }
  174. /**
  175. * This function should not be accessed directly but should be accesse through the text_filter function
  176. * @author Patrick Cool <patrick.cool@UGent.be>
  177. */
  178. function _text_parse_glossary($input) {
  179. return $input;
  180. }
  181. /**
  182. * @desc this function makes a valid link to a different tool
  183. * This function should not be accessed directly but should be accesse through the text_filter function
  184. * @author Patrick Cool <patrick.cool@UGent.be>
  185. */
  186. function _text_parse_tool($input) {
  187. // An array with all the valid tools
  188. $tools[] = array(TOOL_ANNOUNCEMENT, 'announcements/announcements.php');
  189. $tools[] = array(TOOL_CALENDAR_EVENT, 'calendar/agenda.php');
  190. // Check if the name between the [tool] [/tool] tags is a valid one
  191. }
  192. /**
  193. * render LaTeX code into a gif or retrieve a cached version of the gif
  194. * @author Patrick Cool <patrick.cool@UGent.be> Ghent University
  195. */
  196. function latex_gif_renderer($latex_code) {
  197. global $_course;
  198. // Setting the paths and filenames
  199. $mimetex_path = api_get_path(LIBRARY_PATH).'mimetex/';
  200. $temp_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/temp/';
  201. $latex_filename = md5($latex_code).'.gif';
  202. if (!file_exists($temp_path.$latex_filename) OR isset($_GET['render'])) {
  203. if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
  204. $mimetex_command = $mimetex_path.'mimetex.exe -e "'.$temp_path.md5($latex_code).'.gif" '.escapeshellarg($latex_code).'';
  205. } else {
  206. $mimetex_command = $mimetex_path.'mimetex.linux -e "'.$temp_path.md5($latex_code).'.gif" '.escapeshellarg($latex_code);
  207. }
  208. exec($mimetex_command);
  209. //echo 'volgende shell commando werd uitgevoerd:<br /><pre>'.$mimetex_command.'</pre><hr>';
  210. }
  211. $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');\">";
  212. $return .= '<img src="'.api_get_path(WEB_COURSE_PATH).$_course['path'].'/temp/'.$latex_filename.'" alt="'.$latex_code.'" border="0" /></a>';
  213. return $return;
  214. }
  215. /**
  216. * This functions cuts a paragraph
  217. * i.e cut('Merry Xmas from Lima',13) = "Merry Xmas fr..."
  218. * @param string the text to "cut"
  219. * @param int count of chars
  220. * @param bool Whether to embed in a <span title="...">...</span>
  221. * @return string
  222. * */
  223. function cut($text, $maxchar, $embed = false) {
  224. if (api_strlen($text) > $maxchar) {
  225. if ($embed) {
  226. return '<span title="'.$text.'">'.api_substr($text, 0, $maxchar).'...</span>';
  227. }
  228. return api_substr($text, 0, $maxchar).'...' ;
  229. }
  230. return $text;
  231. }
  232. /**
  233. * Show a number as only integers if no decimals, but will show 2 decimals if exist.
  234. *
  235. * @param mixed number to convert
  236. * @param int decimal points 0=never, 1=if needed, 2=always
  237. * @return mixed an integer or a float depends on the parameter
  238. */
  239. function float_format($number, $flag = 1) {
  240. if (is_numeric($number)) {
  241. if (!$number) {
  242. $result = ($flag == 2 ? '0.00' : '0');
  243. } else {
  244. if (floor($number) == $number) {
  245. $result = number_format($number, ($flag == 2 ? 2 : 0));
  246. } else {
  247. $result = number_format(round($number, 2), ($flag == 0 ? 0 : 2));
  248. }
  249. }
  250. return $result;
  251. }
  252. }
  253. // TODO: To be checked for correct timezone management.
  254. /**
  255. * Function to obtain last week timestamps
  256. * @return array times for every day inside week
  257. */
  258. function get_last_week() {
  259. $week = date('W');
  260. $year = date('Y');
  261. $lastweek = $week - 1;
  262. if ($lastweek == 0) {
  263. $week = 52;
  264. $year--;
  265. }
  266. $lastweek = sprintf("%02d", $lastweek);
  267. for ($i = 1; $i <= 7; $i++) {
  268. $arrdays[] = strtotime("$year"."W$lastweek"."$i");
  269. }
  270. return $arrdays;
  271. }