valid_node.module 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright (C) 2005-2006 Frederic G. MARAND
  4. * Licensed under the CeCILL, version 2
  5. * $Id: $
  6. */
  7. define('VALIDNODEVERSION', '$Id: $');
  8. /**
  9. * Persistent variables
  10. */
  11. define ('VALIDNODEVARTIDY', 'valid_node_tidy');
  12. define ('VALIDNODEVARHEAD', 'valid_node_head');
  13. define ('VALIDNODEVARTAIL', 'valid_node_tail');
  14. /**
  15. * Default values for persistent variables
  16. */
  17. define ('VALIDNODEDEFTIDY', 'tidy -asxml');
  18. define ('VALIDNODEDEFHEAD', 9);
  19. define ('VALIDNODEDEFTAIL', 4);
  20. function valid_node_help($section)
  21. {
  22. $ret = '';
  23. switch ($section) {
  24. case 'admin/modules#name':
  25. $ret = 'valid_node';
  26. break;
  27. case 'admin/help#valid_node':
  28. $ret = t('');
  29. break;
  30. case 'admin/modules#description':
  31. $ret = t('valid_node automatically converts node content to XHTML fragments. This helps to maintain valid content on all pages, in spite of the contributors\' HTML quality or lack thereof.');
  32. break;
  33. default: // ignore, this hook is called all over the place in 4.7b1
  34. //$ret = "g2_help($section)";
  35. }
  36. return $ret;
  37. }
  38. /**
  39. * Act on nodes defined by other modules.
  40. */
  41. function valid_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
  42. {
  43. // dprint_r("vnna op = $op");
  44. switch ($op)
  45. {
  46. case 'validate':
  47. case 'view':
  48. case 'update':
  49. case 'insert':
  50. case 'load':
  51. case 'submit':
  52. valid_node_validate($node);
  53. break;
  54. break;
  55. }
  56. }
  57. function valid_node_validate(&$node)
  58. {
  59. $prolog = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  60. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">' . "\n";
  61. $epilog = ' </html>';
  62. $body = $prolog . $node->body . $epilog ;
  63. $descriptorspec = array
  64. (
  65. 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
  66. 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
  67. 2 => array("file", "e:/tmp/error-output.txt", "a"), // stderr is a file to write to
  68. );
  69. $process = proc_open("e:\\images\\w3c\\tidy.exe -asxml", $descriptorspec, $pipes);
  70. if (is_resource($process))
  71. {
  72. // $pipes now looks like this:
  73. // 0 => writeable handle connected to child stdin
  74. // 1 => readable handle connected to child stdout
  75. // Any error output will be appended to /tmp/error-output.txt
  76. fwrite($pipes[0], $body);
  77. fclose($pipes[0]);
  78. $s = '';
  79. while(!feof($pipes[1]))
  80. {
  81. $s .= fgets($pipes[1], 1024);
  82. }
  83. fclose($pipes[1]);
  84. $ar = explode(chr(10), $s);
  85. $head = variable_get(VALIDNODEVARHEAD, VALIDNODEDEFHEAD);
  86. for ($i = 0 ; $i < $head ; $i++)
  87. {
  88. array_shift($ar);
  89. }
  90. $tail = variable_get(VALIDNODEVARTAIL, VALIDNODEDEFTAIL);
  91. for ($i = 0 ; $i < $tail ; $i++)
  92. {
  93. array_pop($ar);
  94. }
  95. $s = implode($ar);
  96. // It is important that you close any pipes before calling
  97. // proc_close in order to avoid a deadlock
  98. $return_value = proc_close($process);
  99. $node->body = $s ;
  100. }
  101. }
  102. function valid_node_settings()
  103. {
  104. $form [VALIDNODEVARTIDY] = array
  105. (
  106. '#type' => 'textfield',
  107. '#title' => t('"%tidy" command, including path if necessary', array('%tidy' => 'tidy')),
  108. '#default_value' => variable_get(VALIDNODEVARTIDY, VALIDNODEDEFTIDY),
  109. '#description' => t('This command takes as input the unformatted content wrapped within a XHTML prolog and epilog, and returns them as a tidied XHTML document, which is then stripped from both prolog and epilog to return the body as an XHTML fragment.')
  110. );
  111. $form [VALIDNODEVARHEAD] = array
  112. (
  113. '#type' => 'textfield',
  114. '#title' => t('Number of lines to strip from top of tidied content to remove the prolog'),
  115. '#size' => 3,
  116. '#maxlength' => 3,
  117. '#default_value' => variable_get(VALIDNODEVARHEAD, VALIDNODEDEFHEAD),
  118. );
  119. $form [VALIDNODEVARTAIL] = array
  120. (
  121. '#type' => 'textfield',
  122. '#title' => t('Number of lines to strip from bottom of tidied content to remove the epilog'),
  123. '#size' => 3,
  124. '#maxlength' => 3,
  125. '#default_value' => variable_get(VALIDNODEVARTAIL, VALIDNODEDEFTAIL),
  126. );
  127. $form[VALIDNODEVERSION] = array
  128. (
  129. '#value' => '<p>'
  130. . t('This site is running valid_node version %version.',
  131. array('%version' => VALIDNODEVERSION)
  132. )
  133. . "</p>\n<p>"
  134. . t('For information about tidy, see %tidy',
  135. array('%tidy' => '<a href="http://www.w3.org/Markup" title="Markup activity at W3C">http://www.w3.org/Markup</a>')
  136. )
  137. . "</p>\n"
  138. );
  139. return $form;
  140. }