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

/filter/tex/texdebug.php

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