PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/filter/tex/texdebug.php

https://bitbucket.org/moodle/moodle
PHP | 386 lines | 307 code | 39 blank | 40 comment | 63 complexity | 8e0584d11aaf0fadac9430b6c22fbb6e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * This function fetches math. images from the data directory
  18. * If not, it obtains the corresponding TeX expression from the cache_tex db table
  19. * and uses mimeTeX to create the image file
  20. *
  21. * @package filter
  22. * @subpackage tex
  23. * @copyright 2004 Zbigniew Fiedorowicz fiedorow@math.ohio-state.edu
  24. * Originally based on code provided by Bruno Vernier bruno@vsbeducation.ca
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. require_once("../../config.php");
  28. if (!filter_is_enabled('tex')) {
  29. print_error('filternotenabled');
  30. }
  31. require_once($CFG->libdir.'/filelib.php');
  32. require_once($CFG->dirroot.'/filter/tex/lib.php');
  33. require_once($CFG->dirroot.'/filter/tex/latex.php');
  34. $action = optional_param('action', '', PARAM_ALPHA);
  35. $texexp = optional_param('tex', '', PARAM_RAW);
  36. require_login();
  37. require_capability('moodle/site:config', context_system::instance(), $USER->id); /// Required cap to run this. MDL-18552
  38. if ($action || $texexp) {
  39. require_sesskey();
  40. }
  41. $output = '';
  42. // look up in cache if required
  43. if ($action=='ShowDB' or $action=='DeleteDB') {
  44. $md5 = md5($texexp);
  45. $texcache = $DB->get_record("cache_filters", array("filter"=>"tex", "md5key"=>$md5));
  46. }
  47. // Action: Show DB Entry
  48. if ($action=='ShowDB') {
  49. if ($texcache) {
  50. $output = "DB cache_filters entry for $texexp\n";
  51. $output .= "id = $texcache->id\n";
  52. $output .= "filter = $texcache->filter\n";
  53. $output .= "version = $texcache->version\n";
  54. $output .= "md5key = $texcache->md5key\n";
  55. $output .= "rawtext = $texcache->rawtext\n";
  56. $output .= "timemodified = $texcache->timemodified\n";
  57. } else {
  58. $output = "DB cache_filters entry for $texexp not found\n";
  59. }
  60. }
  61. // Action: Delete DB Entry
  62. if ($action=='DeleteDB') {
  63. if ($texcache) {
  64. $output = "Deleting DB cache_filters entry for $texexp\n";
  65. $result = $DB->delete_records("cache_filters", array("id"=>$texcache->id));
  66. if ($result) {
  67. $result = 1;
  68. } else {
  69. $result = 0;
  70. }
  71. $output .= "Number of records deleted = $result\n";
  72. } else {
  73. $output = "Could not delete DB cache_filters entry for $texexp\nbecause it could not be found.\n";
  74. }
  75. }
  76. // Action: Show Image
  77. if ($action=='ShowImageMimetex') {
  78. tex2image($texexp);
  79. }
  80. // Action: Check Slasharguments
  81. if ($action=='SlashArguments') {
  82. slasharguments($texexp);
  83. exit;
  84. }
  85. // Action: Show Tex command line output
  86. if ($action=='ShowImageTex') {
  87. TexOutput($texexp, true);
  88. exit;
  89. }
  90. // Action: Show Tex command line output
  91. if ($action=='ShowOutputTex') {
  92. if (debugging()) {
  93. TexOutput($texexp);
  94. } else {
  95. echo "Can not output detailed information due to security concerns, please turn on debug mode first.";
  96. }
  97. exit;
  98. }
  99. if (!empty($action)) {
  100. outputText($output);
  101. }
  102. // nothing more to do if there was any action
  103. if (!empty($action)) {
  104. exit;
  105. }
  106. function outputText($texexp) {
  107. header("Content-type: text/html; charset=utf-8");
  108. echo "<html><body><pre>\n";
  109. if ($texexp) {
  110. echo s($texexp)."\n\n";
  111. } else {
  112. echo "No text output available\n\n";
  113. }
  114. echo "</pre></body></html>\n";
  115. }
  116. function tex2image($texexp, $return=false) {
  117. global $CFG;
  118. if (!$texexp) {
  119. echo 'No tex expresion specified';
  120. return;
  121. }
  122. $image = md5($texexp) . ".gif";
  123. $filetype = 'image/gif';
  124. if (!file_exists("$CFG->dataroot/filter/tex")) {
  125. make_upload_directory("filter/tex");
  126. }
  127. $pathname = "$CFG->dataroot/filter/tex/$image";
  128. if (file_exists($pathname)) {
  129. unlink($pathname);
  130. }
  131. $texexp = '\Large '.$texexp;
  132. $commandpath = filter_tex_get_executable(true);
  133. $cmd = filter_tex_get_cmd($pathname, $texexp);
  134. system($cmd, $status);
  135. if ($return) {
  136. return $image;
  137. }
  138. if (file_exists($pathname)) {
  139. send_file($pathname, $image);
  140. } else if (debugging()) {
  141. $ecmd = "$cmd 2>&1";
  142. echo `$ecmd` . "<br />\n";
  143. echo "The shell command<br />$cmd<br />returned status = $status<br />\n";
  144. if ($status == 4) {
  145. echo "Status corresponds to illegal instruction<br />\n";
  146. } else if ($status == 11) {
  147. echo "Status corresponds to bus error<br />\n";
  148. } else if ($status == 22) {
  149. echo "Status corresponds to abnormal termination<br />\n";
  150. }
  151. if (file_exists($commandpath)) {
  152. echo "File size of mimetex executable $commandpath is " . filesize($commandpath) . "<br />";
  153. echo "The file permissions are: " . decoct(fileperms($commandpath)) . "<br />";
  154. if (function_exists("md5_file")) {
  155. echo "The md5 checksum of the file is " . md5_file($commandpath) . "<br />";
  156. } else {
  157. $handle = fopen($commandpath,"rb");
  158. $contents = fread($handle,16384);
  159. fclose($handle);
  160. echo "The md5 checksum of the first 16384 bytes is " . md5($contents) . "<br />";
  161. }
  162. } else {
  163. echo "mimetex executable $commandpath not found!<br />";
  164. }
  165. echo "Image not found!";
  166. } else {
  167. echo "Can not output detailed information due to security concerns, please turn on debug mode first.";
  168. }
  169. }
  170. // test Tex/Ghostscript output - command execution only
  171. function TexOutput($expression, $graphic=false) {
  172. global $CFG;
  173. $output = '';
  174. $latex = new latex();
  175. // first check if it is likely to work at all
  176. $output .= "<h3>Checking executables</h3>\n";
  177. $executablesexist = true;
  178. $pathlatex = trim(get_config('filter_tex', 'pathlatex'), " '\"");
  179. if (is_file($pathlatex)) {
  180. $output .= "latex executable ($pathlatex) is readable<br />\n";
  181. } else {
  182. $executablesexist = false;
  183. $output .= "<b>Error:</b> latex executable ($pathlatex) is not readable<br />\n";
  184. }
  185. $pathdvips = trim(get_config('filter_tex', 'pathdvips'), " '\"");
  186. if (is_file($pathdvips)) {
  187. $output .= "dvips executable ($pathdvips) is readable<br />\n";
  188. } else {
  189. $executablesexist = false;
  190. $output .= "<b>Error:</b> dvips executable ($pathdvips) is not readable<br />\n";
  191. }
  192. $pathconvert = trim(get_config('filter_tex', 'pathconvert'), " '\"");
  193. if (is_file($pathconvert)) {
  194. $output .= "convert executable ($pathconvert) is readable<br />\n";
  195. } else {
  196. $executablesexist = false;
  197. $output .= "<b>Error:</b> convert executable ($pathconvert) is not readable<br />\n";
  198. }
  199. $pathdvisvgm = trim(get_config('filter_tex', 'pathdvisvgm'), " '\"");
  200. if (is_file($pathdvisvgm)) {
  201. $output .= "dvisvgm executable ($pathdvisvgm) is readable<br />\n";
  202. } else {
  203. $executablesexist = false;
  204. $output .= "<b>Error:</b> dvisvgm executable ($pathdvisvgm) is not readable<br />\n";
  205. }
  206. // knowing that it might work..
  207. $md5 = md5($expression);
  208. $output .= "<p>base filename for expression is '$md5'</p>\n";
  209. // temporary paths
  210. $tex = "$md5.tex"; // Absolute paths won't work with openin_any = p setting.
  211. $dvi = "$latex->temp_dir/$md5.dvi";
  212. $ps = "$latex->temp_dir/$md5.ps";
  213. $convertformat = get_config('filter_tex', 'convertformat');
  214. $img = "$latex->temp_dir/$md5.{$convertformat}";
  215. // Change directory to temp dir so that we can work with relative paths.
  216. chdir($latex->temp_dir);
  217. // put the expression as a file into the temp area
  218. $expression = html_entity_decode($expression);
  219. $output .= "<p>Processing TeX expression:</p><pre>$expression</pre>\n";
  220. $doc = $latex->construct_latex_document($expression);
  221. $fh = fopen($tex, 'w');
  222. fputs($fh, $doc);
  223. fclose($fh);
  224. // step 1: latex command
  225. $pathlatex = escapeshellarg($pathlatex);
  226. $cmd = "$pathlatex --interaction=nonstopmode --halt-on-error $tex";
  227. $output .= execute($cmd);
  228. // step 2: dvips command
  229. $pathdvips = escapeshellarg($pathdvips);
  230. $cmd = "$pathdvips -E $dvi -o $ps";
  231. $output .= execute($cmd);
  232. // Step 3: Set convert or dvisvgm command.
  233. if ($convertformat == 'svg') {
  234. $pathdvisvgm = escapeshellarg($pathdvisvgm);
  235. $cmd = "$pathdvisvgm -E $ps -o $img";
  236. } else {
  237. $pathconvert = escapeshellarg($pathconvert);
  238. $cmd = "$pathconvert -density 240 -trim $ps $img ";
  239. }
  240. $output .= execute($cmd);
  241. if (!$graphic) {
  242. echo $output;
  243. } else if (file_exists($img)) {
  244. send_file($img, "$md5.{$convertformat}");
  245. } else {
  246. echo "Error creating image, see command execution output for more details.";
  247. }
  248. }
  249. function execute($cmd) {
  250. exec($cmd, $result, $code);
  251. $output = "<pre>$ $cmd\n";
  252. $lines = implode("\n", $result);
  253. $output .= "OUTPUT: $lines\n";
  254. $output .= "RETURN CODE: $code\n</pre>\n";
  255. return $output;
  256. }
  257. function slasharguments($texexp) {
  258. global $CFG;
  259. $admin = $CFG->wwwroot.'/'.$CFG->admin.'/settings.php?section=http';
  260. $image = tex2image($texexp,true);
  261. echo "<p>If the following image displays correctly, set your ";
  262. echo "<a href=\"$admin\" target=\"_blank\">Administration->Server->HTTP</a> ";
  263. echo "setting for slasharguments to file.php/1/pic.jpg: ";
  264. echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php/$image\" align=\"absmiddle\"></p>\n";
  265. echo "<p>Otherwise set it to file.php?file=/1/pic.jpg ";
  266. echo "It should display correctly as ";
  267. echo "<img src=\"$CFG->wwwroot/filter/tex/pix.php?file=$image\" align=\"absmiddle\"></p>\n";
  268. echo "<p>If neither equation image displays correctly, please seek ";
  269. echo "further help at moodle.org at the ";
  270. echo "<a href=\"http://moodle.org/mod/forum/view.php?id=752&loginguest=true\" target=\"_blank\">";
  271. echo "Mathematics Tools Forum</a></p>";
  272. }
  273. ?>
  274. <html>
  275. <head><title>TeX Filter Debugger</title></head>
  276. <body>
  277. <p>Please enter an algebraic expression <b>without</b> any surrounding $$ into
  278. the text box below. (Click <a href="#help">here for help.</a>)
  279. <form action="texdebug.php" method="get"
  280. target="inlineframe">
  281. <center>
  282. <input type="text" name="tex" size="50"
  283. value="f(x)=\int_{-\infty}^x~e^{-t^2}dt" />
  284. </center>
  285. <p>The following tests are available:</p>
  286. <ol>
  287. <li><input type="radio" name="action" value="ShowDB" id="ShowDB" />
  288. <label for="ShowDB">See the cache_filters database entry for this expression (if any).</label></li>
  289. <li><input type="radio" name="action" value="DeleteDB" id="DeleteDB" />
  290. <label for="DeleteDB">Delete the cache_filters database entry for this expression (if any).</label></li>
  291. <li><input type="radio" name="action" value="ShowImageMimetex" id="ShowImageMimetex" checked="checked" />
  292. <label for="ShowImageMimetex">Show a graphic image of the algebraic expression rendered with mimetex.</label></li>
  293. <li><input type="radio" name="action" value="ShowImageTex" id="ShowImageTex" />
  294. <label for="ShowImageTex">Show a graphic image of the algebraic expression rendered with Tex/Ghostscript.</label></li>
  295. <li><input type="radio" name="action" value="ShowOutputTex" id="ShowOutputTex" />
  296. <label for="ShowOutputTex">Show command execution output from the algebraic expression rendered with Tex/Ghostscript.</label></li>
  297. <li><input type="radio" name="action" value="SlashArguments" id="SlashArguments" />
  298. <label for="SlashArguments">Check slasharguments setting.</label></li>
  299. </ol>
  300. <input type="submit" value="Do it!" />
  301. <input type="hidden" name="sesskey" value="<?php echo sesskey(); ?>" />
  302. </form> <br /> <br />
  303. <center>
  304. <iframe name="inlineframe" align="middle" width="80%" height="200">
  305. &lt;p&gt;Something is wrong...&lt;/p&gt;
  306. </iframe>
  307. </center> <br />
  308. <hr />
  309. <a name="help">
  310. <h2>Debugging Help</h2>
  311. </a>
  312. <p>First a brief overview of how the TeX filter works. The TeX filter first
  313. searches the database cache_filters table to see if this TeX expression had been
  314. processed before. If not, it adds a DB entry for that expression. It then
  315. replaces the TeX expression by an &lt;img src=&quot;.../filter/tex/pix.php...&quot;&gt;
  316. tag. The filter/tex/pix.php script then searches the database to find an
  317. appropriate gif/png/svg image file for that expression and to create one if it doesn't exist.
  318. It will then use either the LaTex/Ghostscript renderer (using external executables
  319. on your system) or the bundled Mimetex executable. The full Latex/Ghostscript
  320. renderer produces better results and is tried first.
  321. Here are a few common things that can go wrong and some suggestions on how
  322. you might try to fix them.</p>
  323. <ol>
  324. <li>Something had gone wrong on a previous occasion when the filter tried to
  325. process this expression. Then the database entry for that expression contains
  326. a bad TeX expression in the rawtext field (usually blank). You can fix this
  327. by clicking on &quot;Delete DB Entry&quot;</li>
  328. <li>The TeX to gif/png/svg image conversion process does not work.
  329. If paths are specified in the filter configuation screen for the three
  330. executables these will be tried first. Note that they still must be correctly
  331. installed and have the correct permissions. In particular make sure that you
  332. have all the packages installed (e.g., on Debian/Ubuntu you need to install
  333. the 'tetex-extra' package). Running the 'show command execution' test should
  334. give a big clue.
  335. If this fails or is not available, the Mimetex executable is tried. If this
  336. fails a likely cause is that the mimetex binary you are using is
  337. incompatible with your operating system. You can try compiling it from the
  338. C sources downloaded from <a href="http://www.forkosh.com/mimetex.zip">
  339. http://www.forkosh.com/mimetex.zip</a>.
  340. Another possible problem which may affect
  341. both Unix and Windows servers is that the web server doesn't have execute permission
  342. on the mimetex binary. In that case change permissions accordingly</li>
  343. </ol>
  344. </body>
  345. </html>