From 9d49b4301613469c6672ca217034492979790016 Mon Sep 17 00:00:00 2001 From: moodler Date: Thu, 12 Feb 2004 08:39:48 +0000 Subject: [PATCH] OK, well this is working for me now! --- filter/tex/pix.php | 68 ++++++++++++++++ filter/tex/tex_filter.php | 162 +++++++++++++++----------------------- 2 files changed, 133 insertions(+), 97 deletions(-) create mode 100644 filter/tex/pix.php diff --git a/filter/tex/pix.php b/filter/tex/pix.php new file mode 100644 index 0000000000..872b94d4d1 --- /dev/null +++ b/filter/tex/pix.php @@ -0,0 +1,68 @@ +texfilterdir = "filter/tex"; + $CFG->teximagedir = "filter/tex"; + + error_reporting(E_ALL); + + $lifetime = 86400; + if (isset($file)) { // workaround for situations where / syntax doesn't work + $pathinfo = '/' . $file; + } else { + $pathinfo = get_slash_arguments("pix.php"); + } + + if (! $args = parse_slash_arguments($pathinfo)) { + error("No valid arguments supplied"); + } + + $numargs = count($args); + + if ($numargs == 1) { + $image = $args[0]; + $pathname = "$CFG->dataroot/$CFG->teximagedir/$image"; + $filetype = "image/gif"; + } else { + error("No valid arguments supplied"); + } + + + if (!file_exists($pathname)) { + $md5 = str_replace('.gif','',$image); + if ($texcache = get_record("cache_filters", "filter", "tex", "md5key", $md5)) { + if (!file_exists("$CFG->dataroot/$CFG->teximagedir")) { + make_upload_directory($CFG->teximagedir); + } + + $texexp = $texcache->rawtext; + $texexp = str_replace('<','<',$texexp); + $texexp = str_replace('>','>',$texexp); + $texexp = preg_replace('!\r\n?!',' ',$texexp); + $texexp = '\Large ' . $texexp; + system("QUERY_STRING=;export QUERY_STRING;$CFG->dirroot/$CFG->texfilterdir/mimetex -d ". escapeshellarg($texexp) . " >$pathname"); + } + } + + if (file_exists($pathname)) { + $lastmodified = filemtime($pathname); + header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT"); + header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT"); + header("Cache-control: max_age = $lifetime"); // a day + header("Pragma: "); + header("Content-disposition: inline; filename=$image"); + header("Content-length: ".filesize($pathname)); + header("Content-type: $filetype"); + readfile("$pathname"); + } else { + echo "Image not found!"; + } + + exit; +?> diff --git a/filter/tex/tex_filter.php b/filter/tex/tex_filter.php index 5cdafe728d..ed0c28e580 100644 --- a/filter/tex/tex_filter.php +++ b/filter/tex/tex_filter.php @@ -32,103 +32,77 @@ // You will then need to edit your moodle/config.php to invoke tex_filter.php //------------------------------------------------------------------------- - -/// Edit these lines to correspond to your installation -// File path to the directory where mathml_filter.php resides - $CFG->filterDirectory = "$CFG->dirroot/filter/tex"; -// File paths to the echo binary executable - $CFG->echo = "/bin/echo"; -// Frequency with which cache cleanup code is called: 119 means once in 119 times -// that the filter is invoked - $CFG->cacheCleanFreq = 119; -// Time in seconds after which image gifs which haven't been viewed are considered stale -// and are scheduled for deletion - $CFG->cacheCleanTime = 14*24*3600; -// Command used to list the oldest cached gif files to be scheduled for deletion, in -// conjunction with the value of cacheCleanTime - $CFG->cleanFiles = "cd ". $CFG->dataroot . "/1/tex_files;/bin/ls -tr | /usr/bin/head -20"; - +$CFG->texfilterdir = "filter/tex"; /// These lines are important - the variable must match the name /// of the actual function below - $textfilter_function='tex_filter'; +$textfilter_function='tex_filter'; + +if (function_exists($textfilter_function)) { + return; +} - if (function_exists($textfilter_function)) { - return; - } +function string_file_picture_tex($imagefile, $tex= "", $height="", $width="") { + // Given the path to a picture file in a course, or a URL, + // this function includes the picture in the page. + global $CFG; -function string_file_picture($path, $courseid=0, $height="", $width="", $link="") { - // Given the path to a picture file in a course, or a URL, - // this function includes the picture in the page. - global $CFG; - $output = ""; - if ($height) { - $height = "height=\"$height\""; - } - if ($width) { - $width = "width=\"$width\""; - } - if ($link) { - $output .= ""; - } - if (substr(strtolower($path), 0, 7) == "http://") { - $output .= ""; - - } else if ($courseid) { - $output .= "slasharguments) { // Use this method if possible for better caching - $output .= "$CFG->wwwroot/file.php/$courseid/$path"; + $output = ""; + if ($tex) { + $tex = str_replace('&','&',$tex); + $tex = str_replace('<','<',$tex); + $tex = str_replace('>','>',$tex); + $tex = str_replace('"','"',$tex); + $tex = str_replace("\'",''',$tex); + $title = "title=\"$tex\""; + } + if ($height) { + $height = "height=\"$height\""; + } + if ($width) { + $width = "width=\"$width\""; + } + if ($imagefile) { + $output .= "slasharguments) { // Use this method if possible for better caching + $output .= "$CFG->wwwroot/$CFG->texfilterdir/pix.php/$imagefile"; + } else { + $output .= "$CFG->wwwroot/$CFG->texfilterdir/pix.php?file=$imagefile"; + } + $output .= "\" />"; } else { - $output .= "$CFG->wwwroot/file.php?file=/$courseid/$path"; + $output .= "Error: must pass URL or course"; } - $output .= "\" />"; - } else { - $output .= "Error: must pass URL or course"; - } - if ($link) { - $output .= ""; - } - return $output; + return $output; } function tex_filter ($courseid, $text) { global $CFG; - $filterDirectory = $CFG->filterDirectory; - - /// Do a quick check using stripos to avoid unnecessary wor - if (!preg_match('/dataroot . "/1/")) { - mkdir($CFG->dataroot . "/1/",0775); - } +# //restrict filtering to forum 130 (Maths Tools on moodle.org) +# $scriptname = $_SERVER['SCRIPT_NAME']; +# if (!strstr($scriptname,'/forum/')) { +# return $text; +# } +# if (strstr($scriptname,'post.php')) { +# $parent = forum_get_post_full($_GET['reply']); +# $discussion = get_record("forum_discussions","id",$parent->discussion); +# } else if (strstr($scriptname,'discuss.php')) { +# $discussion = get_record("forum_discussions","id",$_GET['d'] ); +# } else { +# return $text; +# } +# if ($discussion->forum != 130) { +# return $text; +# } - if (!file_exists($CFG->dataroot . "/1/tex_files/")) { - mkdir($CFG->dataroot . "/1/tex_files/",0775); - } - umask($old_umask); - echo "\n\n"; - if (isadmin()) { error_reporting (E_ALL); }; //for debugging - $timenow = time(); - if (!($timenow % $CFG->cacheCleanFreq)) { - $cleanFiles = explode("\n",`$CFG->cleanFiles`); - foreach ($cleanFiles as $cleanFile) { - $pathname = $CFG->dataroot . "/1/tex_files/" . $cleanFile; - if ($timenow - filemtime($pathname)>$CFG->cacheCleanTime) { - unlink($pathname); - } else { - break; - } - } - } - - $text .= ' '; preg_match_all('/\$(\$\$+?)([^\$])/s',$text,$matches); for ($i=0;$i TeX expression // or $$ TeX expression $$ preg_match_all('/(.+?)<\/tex>|\$\$(.+?)\$\$/is', $text, $matches); for ($i=0; $idataroot . "/1/" . $filename; - - if (file_exists($pathname)) { - touch($pathname); - $text = str_replace( $matches[0][$i], string_file_picture($filename, 1), $text); - } else { - $texexp = str_replace('<','<',$texexp); - $texexp = str_replace('>','>',$texexp); - $texexp = preg_replace('!\r\n?!',' ',$texexp); - - $texexp = '\Large'.$texexp; - - system("QUERY_STRING=;export QUERY_STRING;$filterDirectory/mimetex -d ". escapeshellarg($texexp) . " >$pathname"); - $text = str_replace( $matches[0][$i], string_file_picture($filename, 1), $text); + $md5 = md5($texexp); + if (! $texcache = get_record("cache_filters","filter","tex", "md5key", $md5)) { + $texcache->filter = 'tex'; + $texcache->version = 1; + $texcache->md5key = $md5; + $texcache->rawtext = addslashes($texexp); + $texcache->timemodified = time(); + insert_record("cache_filters",$texcache); } - + $filename = $md5 . ".gif"; + $text = str_replace( $matches[0][$i], string_file_picture_tex($filename, $texexp), $text); } return $text; -}; +} ?> -- 2.39.5