123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- /**
- * Copyright (C) 2005-2006 Frederic G. MARAND
- * Licensed under the CeCILL, version 2
- * $Id: $
- */
- define('VALIDNODEVERSION', '$Id: $');
- /**
- * Persistent variables
- */
- define ('VALIDNODEVARTIDY', 'valid_node_tidy');
- define ('VALIDNODEVARHEAD', 'valid_node_head');
- define ('VALIDNODEVARTAIL', 'valid_node_tail');
- /**
- * Default values for persistent variables
- */
- define ('VALIDNODEDEFTIDY', 'tidy -asxml');
- define ('VALIDNODEDEFHEAD', 9);
- define ('VALIDNODEDEFTAIL', 4);
- function valid_node_help($section)
- {
- $ret = '';
- switch ($section) {
- case 'admin/modules#name':
- $ret = 'valid_node';
- break;
- case 'admin/help#valid_node':
- $ret = t('');
- break;
- case 'admin/modules#description':
- $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.');
- break;
- default: // ignore, this hook is called all over the place in 4.7b1
- //$ret = "g2_help($section)";
- }
- return $ret;
- }
- /**
- * Act on nodes defined by other modules.
- */
- function valid_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
- {
- // dprint_r("vnna op = $op");
- switch ($op)
- {
- case 'validate':
- case 'view':
- case 'update':
- case 'insert':
- case 'load':
- case 'submit':
- valid_node_validate($node);
- break;
- break;
- }
- }
- function valid_node_validate(&$node)
- {
- $prolog = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">' . "\n";
- $epilog = ' </html>';
- $body = $prolog . $node->body . $epilog ;
- $descriptorspec = array
- (
- 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
- 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
- 2 => array("file", "e:/tmp/error-output.txt", "a"), // stderr is a file to write to
- );
- $process = proc_open("e:\\images\\w3c\\tidy.exe -asxml", $descriptorspec, $pipes);
- if (is_resource($process))
- {
- // $pipes now looks like this:
- // 0 => writeable handle connected to child stdin
- // 1 => readable handle connected to child stdout
- // Any error output will be appended to /tmp/error-output.txt
- fwrite($pipes[0], $body);
- fclose($pipes[0]);
- $s = '';
- while(!feof($pipes[1]))
- {
- $s .= fgets($pipes[1], 1024);
- }
- fclose($pipes[1]);
- $ar = explode(chr(10), $s);
- $head = variable_get(VALIDNODEVARHEAD, VALIDNODEDEFHEAD);
- for ($i = 0 ; $i < $head ; $i++)
- {
- array_shift($ar);
- }
- $tail = variable_get(VALIDNODEVARTAIL, VALIDNODEDEFTAIL);
- for ($i = 0 ; $i < $tail ; $i++)
- {
- array_pop($ar);
- }
- $s = implode($ar);
- // It is important that you close any pipes before calling
- // proc_close in order to avoid a deadlock
- $return_value = proc_close($process);
- $node->body = $s ;
- }
- }
- function valid_node_settings()
- {
- $form [VALIDNODEVARTIDY] = array
- (
- '#type' => 'textfield',
- '#title' => t('"%tidy" command, including path if necessary', array('%tidy' => 'tidy')),
- '#default_value' => variable_get(VALIDNODEVARTIDY, VALIDNODEDEFTIDY),
- '#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.')
- );
- $form [VALIDNODEVARHEAD] = array
- (
- '#type' => 'textfield',
- '#title' => t('Number of lines to strip from top of tidied content to remove the prolog'),
- '#size' => 3,
- '#maxlength' => 3,
- '#default_value' => variable_get(VALIDNODEVARHEAD, VALIDNODEDEFHEAD),
- );
- $form [VALIDNODEVARTAIL] = array
- (
- '#type' => 'textfield',
- '#title' => t('Number of lines to strip from bottom of tidied content to remove the epilog'),
- '#size' => 3,
- '#maxlength' => 3,
- '#default_value' => variable_get(VALIDNODEVARTAIL, VALIDNODEDEFTAIL),
- );
- $form[VALIDNODEVERSION] = array
- (
- '#value' => '<p>'
- . t('This site is running valid_node version %version.',
- array('%version' => VALIDNODEVERSION)
- )
- . "</p>\n<p>"
- . t('For information about tidy, see %tidy',
- array('%tidy' => '<a href="http://www.w3.org/Markup" title="Markup activity at W3C">http://www.w3.org/Markup</a>')
- )
- . "</p>\n"
- );
- return $form;
- }
|