text.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php // $Id: text.lib.php 14637 2008-03-17 22:44:52Z juliomontoya $
  2. /*
  3. ==============================================================================
  4. Dokeos - elearning and course management software
  5. Copyright (c) 2004-2008 Dokeos S.A.
  6. Copyright (c) 2003 Ghent University (UGent)
  7. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  8. Copyright (c) various contributors
  9. For a full list of contributors, see "credits.txt".
  10. The full license can be read in "license.txt".
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15. See the GNU General Public License for more details.
  16. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  17. ==============================================================================
  18. */
  19. /**
  20. ==============================================================================
  21. * This is the text library for Dokeos.
  22. * Include/require it in your code to use its functionality.
  23. *
  24. * @package dokeos.library
  25. ==============================================================================
  26. */
  27. /*
  28. ==============================================================================
  29. FUNCTIONS
  30. ==============================================================================
  31. */
  32. /**
  33. * function make_clickable($string)
  34. *
  35. * @desc completes url contained in the text with "<a href ...".
  36. * However the function simply returns the submitted text without any
  37. * transformation if it already contains some "<a href:" or "<img src=".
  38. * @param string $text text to be converted
  39. * @return text after conversion
  40. * @author Rewritten by Nathan Codding - Feb 6, 2001.
  41. * completed by Hugues Peeters - July 22, 2002
  42. *
  43. * Actually this function is taken from the PHP BB 1.4 script
  44. * - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
  45. * to that URL
  46. * - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
  47. * to http://www.xxxx.yyyy[/zzzz]
  48. * - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
  49. * to that email address
  50. * - Only matches these 2 patterns either after a space, or at the beginning of a line
  51. *
  52. * Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
  53. * have it require something like xxxx@yyyy.zzzz or such. We'll see.
  54. */
  55. function make_clickable($string)
  56. {
  57. if(!stristr($string,' src=') && !stristr($string,' href='))
  58. {
  59. $string=eregi_replace("(https?|ftp)://([a-z0-9#?/&=._+:~%-]+)","<a href=\"\\1://\\2\" target=\"_blank\">\\1://\\2</a>",$string);
  60. $string=eregi_replace("([a-z0-9_.-]+@[a-z0-9.-]+)","<a href=\"mailto:\\1\">\\1</a>",$string);
  61. }
  62. return $string;
  63. }
  64. /**
  65. * formats the date according to the locale settings
  66. *
  67. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  68. * @author Christophe Gesch� <gesche@ipm.ucl.ac.be>
  69. * originally inspired from from PhpMyAdmin
  70. * @param string $formatOfDate date pattern
  71. * @param integer $timestamp, default is NOW.
  72. * @return the formatted date
  73. */
  74. function format_locale_date( $dateFormat, $timeStamp = -1)
  75. {
  76. // Defining the shorts for the days
  77. $DaysShort = array (get_lang("SundayShort"), get_lang("MondayShort"), get_lang("TuesdayShort"), get_lang("WednesdayShort"), get_lang("ThursdayShort"), get_lang("FridayShort"), get_lang("SaturdayShort"));
  78. // Defining the days of the week to allow translation of the days
  79. $DaysLong = array (get_lang("SundayLong"), get_lang("MondayLong"), get_lang("TuesdayLong"), get_lang("WednesdayLong"), get_lang("ThursdayLong"), get_lang("FridayLong"), get_lang("SaturdayLong"));
  80. // Defining the shorts for the months
  81. $MonthsShort = array (get_lang("JanuaryShort"), get_lang("FebruaryShort"), get_lang("MarchShort"), get_lang("AprilShort"), get_lang("MayShort"), get_lang("JuneShort"), get_lang("JulyShort"), get_lang("AugustShort"), get_lang("SeptemberShort"), get_lang("OctoberShort"), get_lang("NovemberShort"), get_lang("DecemberShort"));
  82. // Defining the months of the year to allow translation of the months
  83. $MonthsLong = array (get_lang("JanuaryLong"), get_lang("FebruaryLong"), get_lang("MarchLong"), get_lang("AprilLong"), get_lang("MayLong"), get_lang("JuneLong"), get_lang("JulyLong"), get_lang("AugustLong"), get_lang("SeptemberLong"), get_lang("OctoberLong"), get_lang("NovemberLong"), get_lang("DecemberLong"));
  84. if ($timeStamp == -1) $timeStamp = time();
  85. // with the ereg we replace %aAbB of date format
  86. //(they can be done by the system when locale date aren't aivailable
  87. $date = ereg_replace('%[A]', $DaysLong[(int)strftime('%w', $timeStamp)], $dateFormat);
  88. $date = ereg_replace('%[a]', $DaysShort[(int)strftime('%w', $timeStamp)], $date);
  89. $date = ereg_replace('%[B]', $MonthsLong[(int)strftime('%m', $timeStamp)-1], $date);
  90. $date = ereg_replace('%[b]', $MonthsShort[(int)strftime('%m', $timeStamp)-1], $date);
  91. return strftime($date, $timeStamp);
  92. } // end function format_locale_date
  93. /**
  94. * @desc this function does some parsing on the text that gets inputted. This parsing can be of any kind
  95. * LaTeX notation, Word Censoring, Glossary Terminology (extension will available soon), Musical Notations, ...
  96. * The inspiration for this filter function came from Moodle an phpBB who both use a similar approach
  97. * @param $input string. some text
  98. * @return $output string. some text that contains the parsed elements.
  99. * @example [tex]\sqrt(2)[/tex]
  100. * @author Patrick Cool <patrick.cool@UGent.be>
  101. * @version March 2OO6
  102. */
  103. function text_filter($input, $filter=true)
  104. {
  105. //$input=stripslashes($input);
  106. if ($filter==true)
  107. {
  108. // *** parse [tex]...[/tex] tags *** //
  109. // which will return techexplorer or image html depending on the capabilities of the
  110. // browser of the user (using some javascript that checks if the browser has the TechExplorer plugin installed or not)
  111. $input=_text_parse_tex($input);
  112. // *** parse [teximage]...[/teximage] tags *** //
  113. // these force the gif rendering of LaTeX using the mimetex gif renderer
  114. //$input=_text_parse_tex_image($input);
  115. // *** parse [texexplorer]...[/texexplorer] tags *** //
  116. // these force the texeplorer LaTeX notation
  117. $input=_text_parse_texexplorer($input);
  118. // *** Censor Words *** //
  119. // censor words. This function removes certain words by [censored]
  120. // this can be usefull when the campus is open to the world.
  121. // $input=text_censor_words($input);
  122. // *** parse [?]...[/?] tags *** //
  123. // for the glossary tool (see http://www.dokeos.com/extensions)
  124. $input=_text_parse_glossary($input);
  125. // parse [wiki]...[/wiki] tags
  126. // this is for the coolwiki plugin.
  127. // $input=text_parse_wiki($input);
  128. // parse [tool]...[/tool] tags
  129. // this parse function adds a link to a certain tool
  130. // $input=text_parse_tool($input);
  131. // parse [user]...[/user] tags
  132. // parse [email]...[/email] tags
  133. // parse [code]...[/code] tags
  134. }
  135. return $input;
  136. }
  137. /**
  138. * Apply parsing to content to parse tex commandos that are seperated by [tex]
  139. * [/tex] to make it readable for techexplorer plugin.
  140. * This function should not be accessed directly but should be accesse through the text_filter function
  141. * @param string $text The text to parse
  142. * @return string The text after parsing.
  143. * @author Patrick Cool <patrick.cool@UGent.be>
  144. * @version June 2004
  145. */
  146. function _text_parse_tex($textext)
  147. {
  148. //$textext = str_replace(array ("[tex]", "[/tex]"), array ('[*****]', '[/*****]'), $textext);
  149. //$textext=stripslashes($texttext);
  150. $input_array=preg_split("/(\[tex]|\[\/tex])/",$textext,-1, PREG_SPLIT_DELIM_CAPTURE);
  151. foreach ($input_array as $key=>$value)
  152. {
  153. if ($input_array[$key-1]=='[tex]' AND $input_array[$key+1]=='[/tex]')
  154. {
  155. $input_array[$key]=latex_gif_renderer($value);
  156. unset($input_array[$key-1]);
  157. unset($input_array[$key+1]);
  158. //echo 'LaTeX: <embed type="application/x-techexplorer" texdata="'.stripslashes($value).'" autosize="true" pluginspage="http://www.integretechpub.com/techexplorer/"><br />';
  159. }
  160. }
  161. $output=implode('',$input_array);
  162. return $output;
  163. }
  164. /**
  165. * Apply parsing to content to parse tex commandos that are seperated by [tex]
  166. * [/tex] to make it readable for techexplorer plugin.
  167. * This function should not be accessed directly but should be accesse through the text_filter function
  168. * @param string $text The text to parse
  169. * @return string The text after parsing.
  170. * @author Patrick Cool <patrick.cool@UGent.be>
  171. * @version June 2004
  172. */
  173. function _text_parse_texexplorer($textext)
  174. {
  175. if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
  176. {
  177. $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);
  178. }
  179. else
  180. {
  181. $textext = str_replace(array ("[texexplorer]", "[/texexplorer]"), array ("<embed type=\"application/x-techexplorer\" texdata=\"", "\" autosize=\"true\" pluginspage=\"http://www.integretechpub.com/techexplorer/\">"), $textext);
  182. }
  183. return $textext;
  184. }
  185. /**
  186. * This function should not be accessed directly but should be accesse through the text_filter function
  187. * @author Patrick Cool <patrick.cool@UGent.be>
  188. */
  189. function _text_parse_glossary($input)
  190. {
  191. return $input;
  192. }
  193. /**
  194. * @desc this function makes a valid link to a different tool
  195. * This function should not be accessed directly but should be accesse through the text_filter function
  196. * @author Patrick Cool <patrick.cool@UGent.be>
  197. */
  198. function _text_parse_tool($input)
  199. {
  200. // an array with all the valid tools
  201. $tools[]=array(TOOL_ANNOUNCEMENT, 'announcements/announcements.php');
  202. $tools[]=array(TOOL_CALENDAR_EVENT, 'calendar/agenda.php');
  203. // check if the name between the [tool] [/tool] tags is a valid one
  204. }
  205. /**
  206. * render LaTeX code into a gif or retrieve a cached version of the gif
  207. * @author Patrick Cool <patrick.cool@UGent.be> Ghent University
  208. */
  209. function latex_gif_renderer($latex_code)
  210. {
  211. global $_course;
  212. // setting the paths and filenames
  213. $mimetex_path=api_get_path(LIBRARY_PATH).'mimetex/';
  214. $temp_path=api_get_path(SYS_COURSE_PATH).$_course['path'].'/temp/';
  215. $latex_filename=md5($latex_code).'.gif';
  216. if(!file_exists($temp_path.$latex_filename) OR isset($_GET['render']))
  217. {
  218. if ((PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows"))
  219. {
  220. $mimetex_command=$mimetex_path.'mimetex.exe -e "'.$temp_path.md5($latex_code).'.gif" '.escapeshellarg($latex_code).'';
  221. }
  222. else
  223. {
  224. $mimetex_command=$mimetex_path.'mimetex.linux -e "'.$temp_path.md5($latex_code).'.gif" '.escapeshellarg($latex_code);
  225. }
  226. exec($mimetex_command);
  227. //echo 'volgende shell commando werd uitgevoerd:<br /><pre>'.$mimetex_command.'</pre><hr>';
  228. }
  229. $return = "<a href=\"\" onclick=\"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');\">";
  230. $return .= '<img src="'.api_get_path(WEB_COURSE_PATH).$_course['path'].'/temp/'.$latex_filename.'" alt="'.$latex_code.'" border="0" /></a>';
  231. return $return;
  232. }
  233. /**
  234. * This function returns the difference between the current date (date(now)) with the parameter $date in a string format like "2 days, 1 hour"
  235. * Example: $date="2008-03-07 15:44:08";
  236. * date_to_str($date) it will return 3 days, 20 hours
  237. *
  238. * @param string This string has to be the result of a date function in this format -> date("Y-m-d H:i:s",time());
  239. * @return string The difference between the current date and the parameter in a literal way "3 days, 2 hour" *
  240. * @author Julio Montoya
  241. */
  242. function date_to_str_ago($date)
  243. {
  244. $dst_date=strtotime($date);
  245. //for not call date several times
  246. $date_array=date("s/i/G/j/n/Y",$dst_date);
  247. $date_split=explode("/",$date_array);
  248. $dst_s=$date_split[0];
  249. $dst_m=$date_split[1];
  250. $dst_h=$date_split[2];
  251. $dst_day=$date_split[3];
  252. $dst_mth=$date_split[4];
  253. $dst_yr=$date_split[5];
  254. $dst_date = mktime($dst_h,$dst_m,$dst_s,$dst_mth,$dst_day,$dst_yr);
  255. $time=$offset = time()-$dst_date; //seconds between current days and today
  256. //------------ Here start the functions sec_to_str
  257. $act_day=date('d');
  258. $act_mth=date('n');
  259. $act_yr = date('Y');
  260. if ($dst_day==$act_day && $dst_mth==$act_mth && $dst_yr == $act_yr )
  261. {
  262. return ucfirst(get_lang('Today'));
  263. }
  264. if ($dst_day==$act_day-1 && $dst_mth==$act_mth && $dst_yr == $act_yr )
  265. {
  266. return ucfirst(get_lang('Yesterday'));
  267. }
  268. // original 1
  269. //$sec_time=array("century"=>3.1556926*pow(10,9),"decade"=>315569260,"year"=>31556926,"month"=>2629743.83,"week"=>604800,"day"=>86400,"hour"=>3600,"minute"=>60,"second"=>1);
  270. //$sec_time=array(get_lang('MinDecade')=>315569260,get_lang('MinYear')=>31556926,get_lang('MinMonth')=>2629743.83,get_lang('MinWeek')=>604800,get_lang('MinDay')=>86400,get_lang('MinHour')=>3600,get_lang('MinMinute')=>60);
  271. $MinDecade=get_lang('MinDecade');
  272. $MinYear=get_lang('MinYear');
  273. $MinMonth=get_lang('MinMonth');
  274. $MinWeek=get_lang('MinWeek');
  275. $MinDay=get_lang('MinDay');
  276. $MinHour=get_lang('MinHour');
  277. $MinMinute=get_lang('MinMinute');
  278. $MinDecades=get_lang('MinDecades');
  279. $MinYears=get_lang('MinYears');
  280. $MinMonths=get_lang('MinMonths');
  281. $MinWeeks=get_lang('MinWeeks');
  282. $MinDays=get_lang('MinDays');
  283. $MinHours=get_lang('MinHours');
  284. $MinMinutes=get_lang('MinMinutes');
  285. $sec_time_time=array(315569260,31556926,2629743.83,604800,86400,3600,60);
  286. $sec_time_sing=array($MinDecade,$MinYear,$MinMonth,$MinWeek,$MinDay,$MinHour,$MinMinute);
  287. $sec_time_plu =array($MinDecades,$MinYears,$MinMonths,$MinWeeks,$MinDays,$MinHours,$MinMinutes);
  288. $str_result=array();
  289. $time_result=array();
  290. $key_result=array();
  291. $str='';
  292. $i=0;
  293. for ($i=0;$i<count($sec_time_time);$i++)
  294. {
  295. $seconds=$sec_time_time[$i];
  296. if($seconds > $time) {
  297. continue;
  298. }
  299. $current_value=intval($time/$seconds);
  300. if ($current_value!='1')
  301. {
  302. $date_str= $sec_time_plu[$i];
  303. }
  304. else
  305. {
  306. $date_str= $sec_time_sing[$i];
  307. }
  308. $key_result[]=$sec_time_sing[$i];
  309. $str_result[]=$current_value.' '.$date_str;
  310. $time_result[]= $current_value;
  311. $str.=$current_value.$date_str;
  312. $time%=$seconds;
  313. }
  314. if ($key_result[0]== $MinDay && $key_result[1]== $MinMinute)
  315. {
  316. $key_result[1]=' 0 '.$MinHours;
  317. $str_result[0]=$time_result[0].' '.$key_result[0];
  318. $str_result[1]=$key_result[1];
  319. }
  320. if ($key_result[0]== $MinYear && ($key_result[1]== $MinDay || $key_result[1]== $MinWeek))
  321. {
  322. $key_result[1]=' 0 '.$MinMonths;
  323. $str_result[0]=$time_result[0].' '.$key_result[0];
  324. $str_result[1]=$key_result[1];
  325. }
  326. if (!empty($str_result[1]))
  327. {
  328. $str=$str_result[0].', '.$str_result[1];
  329. }
  330. else
  331. {
  332. $str=$str_result[0];
  333. }
  334. return $str;
  335. }
  336. ?>