MDL-12598 use user context instead of personal; merged from MOODLE_19_STABLE
[moodle.git] / filter / tex / filter.php
CommitLineData
c49dede8 1<?PHP
2/////////////////////////////////////////////////////////////////////////////
3// //
4// NOTICE OF COPYRIGHT //
5// //
6// Moodle - Filter for converting TeX expressions to cached gif images //
7// //
8// Copyright (C) 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu //
9// Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca//
10// This program is free software; you can redistribute it and/or modify //
11// it under the terms of the GNU General Public License as published by //
12// the Free Software Foundation; either version 2 of the License, or //
13// (at your option) any later version. //
14// //
15// This program is distributed in the hope that it will be useful, //
16// but WITHOUT ANY WARRANTY; without even the implied warranty of //
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
18// GNU General Public License for more details: //
19// //
20// http://www.gnu.org/copyleft/gpl.html //
21// //
22/////////////////////////////////////////////////////////////////////////////
23//-------------------------------------------------------------------------
24// NOTE: This Moodle text filter converts TeX expressions delimited
25// by either $$...$$ or by <tex...>...</tex> tags to gif images using
26// mimetex.cgi obtained from http://www.forkosh.com/mimetex.html authored by
4da85078 27// John Forkosh john@forkosh.com. Several binaries of this areincluded with
28// this distribution.
c49dede8 29// Note that there may be patent restrictions on the production of gif images
30// in Canada and some parts of Western Europe and Japan until July 2004.
31//-------------------------------------------------------------------------
4da85078 32/////////////////////////////////////////////////////////////////////////////
33// To activate this filter, add a line like this to your //
34// list of filters in your Filter configuration: //
35// //
36// filter/tex/filter.php //
37/////////////////////////////////////////////////////////////////////////////
c49dede8 38
9d49b430 39$CFG->texfilterdir = "filter/tex";
c49dede8 40
f5ceb06a 41function string_file_picture_tex($imagefile, $tex= "", $height="", $width="", $align="middle", $alt='') {
42 if($alt==='') {
43 $alt=s($tex);
44 }
45// Given the path to a picture file in a course, or a URL,
9d49b430 46 // this function includes the picture in the page.
47 global $CFG;
c49dede8 48
9d49b430 49 $output = "";
2bbaf749 50 $origtex = $tex;
0f13ec8f 51 $style = 'style="border:0px; vertical-align:'.$align.';';
9d49b430 52 if ($tex) {
53 $tex = str_replace('&','&amp;',$tex);
54 $tex = str_replace('<','&lt;',$tex);
55 $tex = str_replace('>','&gt;',$tex);
56 $tex = str_replace('"','&quot;',$tex);
57 $tex = str_replace("\'",'&#39;',$tex);
f5ceb06a 58 // Note that we retain the title tag as TeX format rather than using
59 // the alt text, even if supplied. The alt text is intended for blind
60 // users (to provide a text equivalent to the equation) while the title
61 // is there as a convenience for sighted users who want to see the TeX
62 // code.
9d49b430 63 $title = "title=\"$tex\"";
64 }
65 if ($height) {
0f13ec8f 66 $style .= " height:{$height}px;";
9d49b430 67 }
68 if ($width) {
0f13ec8f 69 $style .= " width:{$width}px;";
9d49b430 70 }
0f13ec8f 71 $style .= '"';
9d49b430 72 if ($imagefile) {
51792df0 73 if (!file_exists("$CFG->dataroot/$CFG->texfilterdir/$imagefile") && has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
894ff63f 74 $output .= "<a href=\"$CFG->wwwroot/$CFG->texfilterdir/texdebug.php\">";
d633b4eb 75 } else {
30fc04e4 76 $output .= "<a target=\"popup\" title=\"TeX\" href=";
77 $output .= "\"$CFG->wwwroot/$CFG->texfilterdir/displaytex.php?";
f352814a 78 $output .= urlencode($tex) . "\" onclick=\"return openpopup('/$CFG->texfilterdir/displaytex.php?";
30fc04e4 79 $output .= urlencode($tex) . "', 'popup', 'menubar=0,location=0,scrollbars,";
80 $output .= "resizable,width=300,height=240', 0);\">";
894ff63f 81 }
f5ceb06a 82 $output .= "<img class=\"texrender\" $title alt=\"$alt\" src=\"";
9d49b430 83 if ($CFG->slasharguments) { // Use this method if possible for better caching
84 $output .= "$CFG->wwwroot/$CFG->texfilterdir/pix.php/$imagefile";
85 } else {
86 $output .= "$CFG->wwwroot/$CFG->texfilterdir/pix.php?file=$imagefile";
87 }
0f13ec8f 88 $output .= "\" $style />";
d633b4eb 89 $output .= "</a>";
c49dede8 90 } else {
9d49b430 91 $output .= "Error: must pass URL or course";
c49dede8 92 }
9d49b430 93 return $output;
c49dede8 94}
95
96function tex_filter ($courseid, $text) {
97
98 global $CFG;
c49dede8 99
9d49b430 100 /// Do a quick check using stripos to avoid unnecessary work
82a76d38 101 if (!preg_match('/<tex/i',$text) and !strstr($text,'$$') and !strstr($text,'\\[') and !preg_match('/\[tex/i',$text)) { //added one more tag (dlnsk)
c49dede8 102 return $text;
103 }
c49dede8 104
9d49b430 105# //restrict filtering to forum 130 (Maths Tools on moodle.org)
106# $scriptname = $_SERVER['SCRIPT_NAME'];
107# if (!strstr($scriptname,'/forum/')) {
108# return $text;
109# }
110# if (strstr($scriptname,'post.php')) {
111# $parent = forum_get_post_full($_GET['reply']);
112# $discussion = get_record("forum_discussions","id",$parent->discussion);
113# } else if (strstr($scriptname,'discuss.php')) {
114# $discussion = get_record("forum_discussions","id",$_GET['d'] );
115# } else {
116# return $text;
117# }
118# if ($discussion->forum != 130) {
119# return $text;
120# }
c49dede8 121 $text .= ' ';
122 preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches);
123 for ($i=0;$i<count($matches[0]);$i++) {
124 $replacement = str_replace('$','&#x00024;',$matches[1][$i]).$matches[2][$i];
125 $text = str_replace($matches[0][$i],$replacement,$text);
126 }
127
c49dede8 128 // <tex> TeX expression </tex>
f5ceb06a 129 // or <tex alt="My alternative text to be used instead of the TeX form"> TeX expression </tex>
c49dede8 130 // or $$ TeX expression $$
82a76d38 131 // or \[ TeX expression \] // original tag of MathType and TeXaide (dlnsk)
132 // or [tex] TeX expression [/tex] // somtime it's more comfortable than <tex> (dlnsk)
f5ceb06a 133 preg_match_all('/<tex(?:\s+alt=["\'](.*?)["\'])?>(.+?)<\/tex>|\$\$(.+?)\$\$|\\\\\[(.+?)\\\\\]|\\[tex\\](.+?)\\[\/tex\\]/is', $text, $matches);
c49dede8 134 for ($i=0; $i<count($matches[0]); $i++) {
f5ceb06a 135 $texexp = $matches[2][$i] . $matches[3][$i] . $matches[4][$i] . $matches[5][$i];
136 $alt = $matches[1][$i];
ea3cf781 137 $texexp = str_replace('<nolink>','',$texexp);
138 $texexp = str_replace('</nolink>','',$texexp);
ee9ff7f3 139 $texexp = str_replace('<span class="nolink">','',$texexp);
140 $texexp = str_replace('</span>','',$texexp);
25d7779c 141 $texexp = eregi_replace("<br[[:space:]]*\/?>", '', $texexp); //dlnsk
f352814a 142 $align = "middle";
143 if (preg_match('/^align=bottom /',$texexp)) {
144 $align = "text-bottom";
145 $texexp = preg_replace('/^align=bottom /','',$texexp);
146 } else if (preg_match('/^align=top /',$texexp)) {
147 $align = "text-top";
148 $texexp = preg_replace('/^align=top /','',$texexp);
149 }
9d49b430 150 $md5 = md5($texexp);
151 if (! $texcache = get_record("cache_filters","filter","tex", "md5key", $md5)) {
152 $texcache->filter = 'tex';
153 $texcache->version = 1;
154 $texcache->md5key = $md5;
155 $texcache->rawtext = addslashes($texexp);
156 $texcache->timemodified = time();
a9a9bdba 157 insert_record("cache_filters",$texcache, false);
c49dede8 158 }
9d49b430 159 $filename = $md5 . ".gif";
f5ceb06a 160 $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp, '', '', $align, $alt), $text);
c49dede8 161 }
162 return $text;
9d49b430 163}
c49dede8 164
165
1cdd857c 166?>