Go to the documentation of this file.00001 <?php
00002 if(!defined('__PRAGYAN_CMS'))
00003 {
00004 header($_SERVER['SERVER_PROTOCOL'].' 403 Forbidden');
00005 echo "<h1>403 Forbidden<h1><h4>You are not authorized to access the page.</h4>";
00006 echo '<hr/>'.$_SERVER['SERVER_SIGNATURE'];
00007 exit(1);
00008 }
00016 class latexrender {
00017
00018 var $LATEX_PATH = "/usr/bin/latex";
00019 var $DVIPS_PATH = "/usr/bin/dvips";
00020 var $CONVERT_PATH = "/usr/bin/convert";
00022 var $TMP_DIR = "./cache";
00023 var $CACHE_DIR = "/var/www/html/workspace2/pragyan_v2/cms/modules/article/latex/demo/pictures";
00024 var $URL_PATH = "http://pragyan.org/workspace2/pragyan_v2/cms/modules/article/latex/demo/pictures";
00025
00026 function wrap($thunk) {
00027 return<<<EOS
00028 \documentclass[10pt]{article}
00029
00030 % add additional packages here
00031 \usepackage{amsmath}
00032 \usepackage{amsfonts}
00033 \usepackage{amssymb}
00034 \usepackage{pst-plot}
00035 \usepackage{color}
00036 \pagestyle{empty}
00037 \begin{document}
00038 \pagecolor{white}
00039 $thunk
00040 \end{document}
00041 EOS;
00042 }
00043 function render_latex($thunk, $hash) {
00044 $thunk = $this->wrap($thunk);
00045
00046 $current_dir = getcwd();
00047 chdir($this->TMP_DIR);
00048
00049 $fp = fopen("$hash.tex", "w+");
00050 fputs($fp, $thunk);
00051 fclose($fp);
00052
00053 $command = $this->LATEX_PATH .
00054 " --interaction=nonstopmode " .
00055 $hash . ".tex";
00056 exec($command);
00057
00058 $command = $this->DVIPS_PATH .
00059 " -E $hash" .
00060 ".dvi -o " . "$hash.ps";
00061 exec($command);
00062
00063
00064 $command = $this->CONVERT_PATH .
00065 " -density 120 $hash.ps $hash.png";
00066 exec($command);
00067
00068 copy($this->TMP_DIR."/$hash.png", $this->CACHE_DIR ."/$hash.png");
00069 chdir($current_dir);
00070
00071 }
00072 function cleanup($hash) {
00073 $current_dir = getcwd();
00074 chdir($this->TMP_DIR);
00075 unlink($this->TMP_DIR . "/$hash.tex");
00076 unlink($this->TMP_DIR . "/$hash.aux");
00077 unlink($this->TMP_DIR . "/$hash.log");
00078 unlink($this->TMP_DIR . "/$hash.dvi");
00079 unlink($this->TMP_DIR . "/$hash.ps");
00080 unlink($this->TMP_DIR . "/$hash.png");
00081 chdir($current_dir);
00082 }
00083 function transform($text) {
00084 global $sourceFolder;
00085 global $uploadFolder;
00086 global $urlRequestRoot, $cmsFolder;
00087 $uploadDir = $sourceFolder . "/" . $uploadFolder;
00088 if (!file_exists($uploadDir . "/temp"))
00089 mkdir($uploadDir . "/temp", 0755);
00090 if (!file_exists($uploadDir . "/cache"))
00091 mkdir($uploadDir . "/cache", 0755);
00092 $this->TMP_DIR = $uploadDir . "/temp";
00093 $this->CACHE_DIR = $uploadDir . "/cache";
00094 $this->URL_PATH = $urlRequestRoot . "/" . $cmsFolder ."/" .$uploadFolder . "/cache";
00095 preg_match_all("/\[tex\](.*?)\[\/tex\]/si", $text, $matches);
00096 for ($i = 0; $i < count($matches[0]); $i++) {
00097 $position = strpos($text, $matches[0][$i]);
00098 $thunk = $matches[1][$i];
00099 $hash = md5($thunk);
00100 $full_name = $this->CACHE_DIR . "/" .
00101 $hash . ".png";
00102 $url = $this->URL_PATH . "/" .
00103 $hash . ".png";
00104 if (!is_file($full_name)) {
00105 $this->render_latex($thunk, $hash);
00106 $this->cleanup($hash);
00107 } else
00108 touch($full_name);
00109 $text = substr_replace($text, "<img src=\"$url\" alt=\"Formula: $i\" />", $position, strlen($matches[0][$i]));
00110 }
00111 exec("find " . $uploadDir . "/cache -type f -mtime +14 | xargs rm -f");
00112 return $text;
00113 }
00114
00115 }