Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob
  • Smarty
    • Cacher
    • Compiler
    • Config
    • Debug
    • PluginsBlock
    • PluginsFilter
    • PluginsFunction
    • PluginsInternal
    • PluginsModifier
    • PluginsModifierCompiler
    • PluginsShared
    • Security
    • Template
    • TemplateResources
  • Swift
    • ByteStream
    • CharacterStream
    • Encoder
    • Events
    • KeyCache
    • Mailer
    • Mime
    • Plugins
    • Transport

Classes

  • Swift_FailoverTransport
  • Swift_LoadBalancedTransport
  • Swift_MailTransport
  • Swift_Plugins_Loggers_ArrayLogger
  • Swift_Plugins_Loggers_EchoLogger
  • Swift_SendmailTransport
  • Swift_SmtpTransport
  • Swift_Transport_AbstractSmtpTransport
  • Swift_Transport_Esmtp_Auth_CramMd5Authenticator
  • Swift_Transport_Esmtp_Auth_LoginAuthenticator
  • Swift_Transport_Esmtp_Auth_PlainAuthenticator
  • Swift_Transport_Esmtp_AuthHandler
  • Swift_Transport_EsmtpTransport
  • Swift_Transport_FailoverTransport
  • Swift_Transport_LoadBalancedTransport
  • Swift_Transport_MailTransport
  • Swift_Transport_SendmailTransport
  • Swift_Transport_SimpleMailInvoker
  • Swift_Transport_StreamBuffer

Interfaces

  • Swift_Plugins_Logger
  • Swift_Plugins_Pop_Pop3Exception
  • Swift_Transport
  • Swift_Transport_Esmtp_Authenticator
  • Swift_Transport_EsmtpHandler
  • Swift_Transport_IoBuffer
  • Swift_Transport_MailInvoker
  • Swift_Transport_SmtpAgent
  • Swift_TransportException
  • Overview
  • Package
  • Function
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * This file contains the html validator class.
  4:  *
  5:  * @package Core
  6:  * @subpackage Backend
  7:  * @version SVN Revision $Rev:$
  8:  *
  9:  * @author timo.hummel
 10:  * @copyright four for business AG <www.4fb.de>
 11:  * @license http://www.contenido.org/license/LIZENZ.txt
 12:  * @link http://www.4fb.de
 13:  * @link http://www.contenido.org
 14:  */
 15: 
 16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 17: 
 18: /**
 19:  * This class validates HTML.
 20:  *
 21:  * @package Core
 22:  * @subpackage Backend
 23:  */
 24: class cHTMLValidator {
 25: 
 26:     /**
 27:      *
 28:      * @var array
 29:      */
 30:     protected $_doubleTags = array(
 31:         "form",
 32:         "head",
 33:         "body",
 34:         "html",
 35:         "td",
 36:         "tr",
 37:         "table",
 38:         "a",
 39:         "tbody",
 40:         "title",
 41:         "container",
 42:         "span",
 43:         "div"
 44:     );
 45: 
 46:     /**
 47:      *
 48:      * @var array
 49:      */
 50:     public $missingNodes = array();
 51: 
 52:     /**
 53:      *
 54:      * @var string
 55:      * @deprecated not used anymore
 56:      */
 57:     public $iNodeName;
 58: 
 59:     /**
 60:      *
 61:      * @var string
 62:      */
 63:     protected $_html;
 64: 
 65:     /**
 66:      *
 67:      * @var array
 68:      */
 69:     protected $_nestingLevel = array();
 70: 
 71:     /**
 72:      *
 73:      * @var array
 74:      */
 75:     protected $_nestingNodes = array();
 76: 
 77:     /**
 78:      *
 79:      * @var array
 80:      */
 81:     protected $_existingTags = array();
 82: 
 83:     /**
 84:      *
 85:      * @param string $html
 86:      */
 87:     public function validate($html) {
 88:         $nestingLevel = 0;
 89: 
 90:         // Clean up HTML first from any PHP scripts, and clean up line breaks
 91:         $this->_html = $this->_cleanHTML($html);
 92: 
 93:         $htmlParser = new HtmlParser($this->_html);
 94: 
 95:         while ($htmlParser->parse()) {
 96:             $this->_existingTags[] = $htmlParser->iNodeName;
 97:             // Check if we found a double tag
 98:             if (in_array($htmlParser->iNodeName, $this->_doubleTags)) {
 99:                 if (!array_key_exists($htmlParser->iNodeName, $this->_nestingLevel)) {
100:                     $this->_nestingLevel[$htmlParser->iNodeName] = 0;
101:                 }
102: 
103:                 if (!array_key_exists($htmlParser->iNodeName, $this->_nestingNodes)) {
104:                     $this->_nestingNodes[$htmlParser->iNodeName][intval($this->_nestingLevel[$htmlParser->iNodeName])] = array();
105:                 }
106: 
107:                 // Check if it's a start tag
108:                 if ($htmlParser->iNodeType == HtmlParser::NODE_TYPE_ELEMENT) {
109:                     // Push the current element to the stack, remember ID and
110:                     // Name, if possible
111:                     $nestingLevel++;
112: 
113:                     $this->_nestingNodes[$htmlParser->iNodeName][intval($this->_nestingLevel[$htmlParser->iNodeName])]["name"] = $htmlParser->iNodeAttributes["name"];
114:                     $this->_nestingNodes[$htmlParser->iNodeName][intval($this->_nestingLevel[$htmlParser->iNodeName])]["id"] = $htmlParser->iNodeAttributes["id"];
115:                     $this->_nestingNodes[$htmlParser->iNodeName][intval($this->_nestingLevel[$htmlParser->iNodeName])]["level"] = $nestingLevel;
116:                     $this->_nestingNodes[$htmlParser->iNodeName][intval($this->_nestingLevel[$htmlParser->iNodeName])]["char"] = $htmlParser->iHtmlTextIndex;
117:                     $this->_nestingLevel[$htmlParser->iNodeName]++;
118:                 }
119: 
120:                 if ($htmlParser->iNodeType == HtmlParser::NODE_TYPE_ENDELEMENT) {
121:                     // Check if we've an element of this type on the stack
122:                     if ($this->_nestingLevel[$htmlParser->iNodeName] > 0) {
123:                         unset($this->_nestingNodes[$htmlParser->iNodeName][$this->_nestingLevel[$htmlParser->iNodeName]]);
124:                         $this->_nestingLevel[$htmlParser->iNodeName]--;
125: 
126:                         if ($this->_nestingNodes[$htmlParser->iNodeName][intval($this->_nestingLevel[$htmlParser->iNodeName])]["level"] != $nestingLevel) {
127:                             // Todo: Check for the wrong nesting level
128:                         }
129: 
130:                         $nestingLevel--;
131:                     }
132:                 }
133:             }
134:         }
135: 
136:         // missingNodes should be an empty array by default
137:         $this->missingNodes = array();
138: 
139:         // Collect all missing nodes
140:         foreach ($this->_nestingLevel as $key => $value) {
141:             // One or more missing tags found
142:             if ($value > 0) {
143:                 // Step trough all missing tags
144:                 for ($i = 0; $i < $value; $i++) {
145:                     $node = $this->_nestingNodes[$key][$i];
146: 
147:                     list($line, $char) = $this->_getLineAndCharPos($node["char"]);
148:                     $this->missingNodes[] = array(
149:                         "tag" => $key,
150:                         "id" => $node["id"],
151:                         "name" => $node["name"],
152:                         "line" => $line,
153:                         "char" => $char
154:                     );
155: 
156:                     $this->missingTags[$line][$char] = true;
157:                 }
158:             }
159:         }
160:     }
161: 
162:     /**
163:      *
164:      * @param string $tag
165:      * @return boolean
166:      */
167:     public function tagExists($tag) {
168:         if (in_array($tag, $this->_existingTags)) {
169:             return true;
170:         } else {
171:             return false;
172:         }
173:     }
174: 
175:     /**
176:      *
177:      * @param string $html
178:      * @return mixed
179:      */
180:     protected function _cleanHTML($html) {
181:         // remove all php code from layout
182:         $resultingHTML = preg_replace('/<\?(php)?((.)|(\s))*?\?>/i', '', $html);
183: 
184:         // We respect only \n, but we need to take care of windows (\n\r) and
185:         // other systems (\r)
186:         $resultingHTML = str_replace("\r\n", "\n", $resultingHTML);
187:         $resultingHTML = str_replace("\r", "\n", $resultingHTML);
188: 
189:         return $resultingHTML;
190:     }
191: 
192:     /**
193:      *
194:      * @return string
195:      * @deprecated not used anymore
196:      */
197:     protected function _returnErrorMap() {
198:         $html = "<pre>";
199: 
200:         $chunks = explode("\n", $this->_html);
201: 
202:         foreach ($chunks as $key => $value) {
203:             $html .= ($key + 1) . " ";
204: 
205:             for ($i = 0; $i < strlen($value); $i++) {
206:                 $char = substr($value, $i, 1);
207: 
208:                 if (is_array($this->missingTags[$key + 1])) {
209:                     // echo ($key+1) . " ". $i."<br>";
210:                     if (array_key_exists($i + 2, $this->missingTags[$key + 1])) {
211:                         $html .= "<u><b>" . conHtmlSpecialChars($char) . "</b></u>";
212:                     } else {
213:                         $html .= conHtmlSpecialChars($char);
214:                     }
215:                 } else {
216:                     $html .= conHtmlSpecialChars($char);
217:                 }
218:             }
219: 
220:             $html .= "<br>";
221:         }
222: 
223:         return $html;
224:     }
225: 
226:     /**
227:      *
228:      * @param int $charpos
229:      * @return array
230:      */
231:     protected function _getLineAndCharPos($charpos) {
232:         $mangled = substr($this->_html, 0, $charpos);
233: 
234:         $line = substr_count($mangled, "\n") + 1;
235:         $char = $charpos - strrpos($mangled, "\n");
236: 
237:         return array(
238:             $line,
239:             $char
240:         );
241:     }
242: }
243: 
CMS CONTENIDO 4.9.7 API documentation generated by ApiGen