Back to index

php5  5.3.10
tidy.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author: John Coggeshall <john@php.net>                               |
00016   +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: tidy.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 #include "php_tidy.h"
00027 
00028 #if HAVE_TIDY
00029 
00030 #include "php_ini.h"
00031 #include "ext/standard/info.h"
00032 #include "safe_mode.h"
00033 
00034 #include "tidy.h"
00035 #include "buffio.h"
00036 
00037 /* compatibility with older versions of libtidy */
00038 #ifndef TIDY_CALL
00039 #define TIDY_CALL
00040 #endif
00041 
00042 #define PHP_TIDY_MODULE_VERSION    "2.0"
00043 
00044 /* {{{ ext/tidy macros
00045 */
00046 #define TIDY_SET_CONTEXT \
00047     zval *object = getThis();
00048 
00049 #define TIDY_FETCH_OBJECT   \
00050        PHPTidyObj *obj;     \
00051        TIDY_SET_CONTEXT; \
00052        if (object) { \
00053               if (zend_parse_parameters_none() == FAILURE) {   \
00054                      return;       \
00055               }      \
00056        } else {      \
00057               if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, NULL, "O", &object, tidy_ce_doc) == FAILURE) {      \
00058                      RETURN_FALSE; \
00059               }      \
00060        }      \
00061        obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC);  \
00062 
00063 #define TIDY_FETCH_ONLY_OBJECT     \
00064        PHPTidyObj *obj;     \
00065        TIDY_SET_CONTEXT; \
00066        if (zend_parse_parameters_none() == FAILURE) {   \
00067               return;       \
00068        }      \
00069        obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC);  \
00070 
00071 #define TIDY_APPLY_CONFIG_ZVAL(_doc, _val) \
00072     if(_val) { \
00073         if(Z_TYPE_PP(_val) == IS_ARRAY) { \
00074             _php_tidy_apply_config_array(_doc, HASH_OF(*_val) TSRMLS_CC); \
00075         } else { \
00076             convert_to_string_ex(_val); \
00077             TIDY_SAFE_MODE_CHECK(Z_STRVAL_PP(_val)); \
00078             switch (tidyLoadConfig(_doc, Z_STRVAL_PP(_val))) { \
00079               case -1: \
00080                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not load configuration file '%s'", Z_STRVAL_PP(_val)); \
00081                 break; \
00082               case 1: \
00083                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There were errors while parsing the configuration file '%s'", Z_STRVAL_PP(_val)); \
00084                 break; \
00085             } \
00086         } \
00087     }
00088 
00089 #define REGISTER_TIDY_CLASS(classname, name, parent, __flags) \
00090        { \
00091               zend_class_entry ce; \
00092               INIT_CLASS_ENTRY(ce, # classname, tidy_funcs_ ## name); \
00093               ce.create_object = tidy_object_new_ ## name; \
00094               tidy_ce_ ## name = zend_register_internal_class_ex(&ce, parent, NULL TSRMLS_CC); \
00095               tidy_ce_ ## name->ce_flags |= __flags;  \
00096               memcpy(&tidy_object_handlers_ ## name, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
00097               tidy_object_handlers_ ## name.clone_obj = NULL; \
00098        }
00099 
00100 #define TIDY_TAG_CONST(tag) REGISTER_LONG_CONSTANT("TIDY_TAG_" #tag, TidyTag_##tag, CONST_CS | CONST_PERSISTENT)
00101 #define TIDY_NODE_CONST(name, type) REGISTER_LONG_CONSTANT("TIDY_NODETYPE_" #name, TidyNode_##type, CONST_CS | CONST_PERSISTENT)
00102 
00103 #ifndef TRUE
00104 #define TRUE 1
00105 #endif
00106 
00107 #ifndef FALSE
00108 #define FALSE 0
00109 #endif
00110 
00111 #define ADD_PROPERTY_STRING(_table, _key, _string) \
00112        { \
00113               zval *tmp; \
00114               MAKE_STD_ZVAL(tmp); \
00115               if (_string) { \
00116                      ZVAL_STRING(tmp, (char *)_string, 1); \
00117               } else { \
00118                      ZVAL_EMPTY_STRING(tmp); \
00119               } \
00120               zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
00121        }
00122 
00123 #define ADD_PROPERTY_STRINGL(_table, _key, _string, _len) \
00124    { \
00125        zval *tmp; \
00126        MAKE_STD_ZVAL(tmp); \
00127        if (_string) { \
00128            ZVAL_STRINGL(tmp, (char *)_string, _len, 1); \
00129        } else { \
00130            ZVAL_EMPTY_STRING(tmp); \
00131        } \
00132        zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
00133    }
00134 
00135 #define ADD_PROPERTY_LONG(_table, _key, _long) \
00136        { \
00137               zval *tmp; \
00138               MAKE_STD_ZVAL(tmp); \
00139               ZVAL_LONG(tmp, _long); \
00140               zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
00141        }
00142 
00143 #define ADD_PROPERTY_NULL(_table, _key) \
00144        { \
00145               zval *tmp; \
00146               MAKE_STD_ZVAL(tmp); \
00147               ZVAL_NULL(tmp); \
00148               zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
00149        }
00150 
00151 #define ADD_PROPERTY_BOOL(_table, _key, _bool) \
00152     { \
00153        zval *tmp; \
00154        MAKE_STD_ZVAL(tmp); \
00155        ZVAL_BOOL(tmp, _bool); \
00156        zend_hash_update(_table, #_key, sizeof(#_key), (void *)&tmp, sizeof(zval *), NULL); \
00157    }
00158 
00159 #define TIDY_SAFE_MODE_CHECK(filename) \
00160 if ((PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) || php_check_open_basedir(filename TSRMLS_CC)) { \
00161        RETURN_FALSE; \
00162 } \
00163 
00164 #define TIDY_SET_DEFAULT_CONFIG(_doc) \
00165        if (TG(default_config) && TG(default_config)[0]) { \
00166               if (tidyLoadConfig(_doc, TG(default_config)) < 0) { \
00167                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to load Tidy configuration file at '%s'.", TG(default_config)); \
00168               } \
00169        }
00170 /* }}} */
00171 
00172 /* {{{ ext/tidy structs 
00173 */
00174 typedef struct _PHPTidyDoc PHPTidyDoc;
00175 typedef struct _PHPTidyObj PHPTidyObj;
00176 
00177 typedef enum {
00178        is_node,
00179        is_doc
00180 } tidy_obj_type;
00181 
00182 typedef enum {
00183        is_root_node,
00184        is_html_node,
00185        is_head_node,
00186        is_body_node
00187 } tidy_base_nodetypes;
00188 
00189 struct _PHPTidyDoc {
00190        TidyDoc     doc;
00191        TidyBuffer  *errbuf;
00192        unsigned int ref_count;
00193 };
00194 
00195 struct _PHPTidyObj {
00196        zend_object         std;
00197        TidyNode            node;
00198        tidy_obj_type       type;
00199        PHPTidyDoc          *ptdoc;
00200 };
00201 /* }}} */
00202 
00203 /* {{{ ext/tidy prototypes
00204 */
00205 static char *php_tidy_file_to_mem(char *, zend_bool, int * TSRMLS_DC);
00206 static void tidy_object_free_storage(void * TSRMLS_DC);
00207 static zend_object_value tidy_object_new_node(zend_class_entry * TSRMLS_DC);
00208 static zend_object_value tidy_object_new_doc(zend_class_entry * TSRMLS_DC);
00209 static zval * tidy_instanciate(zend_class_entry *, zval * TSRMLS_DC);
00210 static int tidy_doc_cast_handler(zval *, zval *, int TSRMLS_DC);
00211 static int tidy_node_cast_handler(zval *, zval *, int TSRMLS_DC);
00212 static void tidy_doc_update_properties(PHPTidyObj * TSRMLS_DC);
00213 static void tidy_add_default_properties(PHPTidyObj *, tidy_obj_type TSRMLS_DC);
00214 static void *php_tidy_get_opt_val(PHPTidyDoc *, TidyOption, TidyOptionType * TSRMLS_DC);
00215 static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes);
00216 static int _php_tidy_set_tidy_opt(TidyDoc, char *, zval * TSRMLS_DC);
00217 static int _php_tidy_apply_config_array(TidyDoc doc, HashTable *ht_options TSRMLS_DC);
00218 static void _php_tidy_register_nodetypes(INIT_FUNC_ARGS);
00219 static void _php_tidy_register_tags(INIT_FUNC_ARGS);
00220 
00221 static PHP_MINIT_FUNCTION(tidy);
00222 static PHP_MSHUTDOWN_FUNCTION(tidy);
00223 static PHP_RINIT_FUNCTION(tidy);
00224 static PHP_MINFO_FUNCTION(tidy);
00225 
00226 static PHP_FUNCTION(tidy_getopt);
00227 static PHP_FUNCTION(tidy_parse_string);
00228 static PHP_FUNCTION(tidy_parse_file);
00229 static PHP_FUNCTION(tidy_clean_repair);
00230 static PHP_FUNCTION(tidy_repair_string);
00231 static PHP_FUNCTION(tidy_repair_file);
00232 static PHP_FUNCTION(tidy_diagnose);
00233 static PHP_FUNCTION(tidy_get_output);
00234 static PHP_FUNCTION(tidy_get_error_buffer);
00235 static PHP_FUNCTION(tidy_get_release);
00236 static PHP_FUNCTION(tidy_get_config);
00237 static PHP_FUNCTION(tidy_get_status);
00238 static PHP_FUNCTION(tidy_get_html_ver);
00239 #if HAVE_TIDYOPTGETDOC
00240 static PHP_FUNCTION(tidy_get_opt_doc);
00241 #endif
00242 static PHP_FUNCTION(tidy_is_xhtml);
00243 static PHP_FUNCTION(tidy_is_xml);
00244 static PHP_FUNCTION(tidy_error_count);
00245 static PHP_FUNCTION(tidy_warning_count);
00246 static PHP_FUNCTION(tidy_access_count);
00247 static PHP_FUNCTION(tidy_config_count);
00248 
00249 static PHP_FUNCTION(ob_tidyhandler);
00250 
00251 static PHP_FUNCTION(tidy_get_root);
00252 static PHP_FUNCTION(tidy_get_html);
00253 static PHP_FUNCTION(tidy_get_head);
00254 static PHP_FUNCTION(tidy_get_body);
00255 
00256 static TIDY_DOC_METHOD(__construct);
00257 static TIDY_DOC_METHOD(parseFile);
00258 static TIDY_DOC_METHOD(parseString);
00259 
00260 static TIDY_NODE_METHOD(hasChildren);
00261 static TIDY_NODE_METHOD(hasSiblings);
00262 static TIDY_NODE_METHOD(isComment);
00263 static TIDY_NODE_METHOD(isHtml);
00264 static TIDY_NODE_METHOD(isText);
00265 static TIDY_NODE_METHOD(isJste);
00266 static TIDY_NODE_METHOD(isAsp);
00267 static TIDY_NODE_METHOD(isPhp);
00268 static TIDY_NODE_METHOD(getParent);
00269 static TIDY_NODE_METHOD(__construct);
00270 /* }}} */
00271 
00272 ZEND_DECLARE_MODULE_GLOBALS(tidy)
00273 
00274 PHP_INI_BEGIN()
00275 STD_PHP_INI_ENTRY("tidy.default_config",  "",    PHP_INI_SYSTEM,             OnUpdateString,             default_config,             zend_tidy_globals,   tidy_globals)
00276 PHP_INI_ENTRY("tidy.clean_output",     "0",    PHP_INI_PERDIR,         NULL)
00277 PHP_INI_END()
00278 
00279 /* {{{ arginfo */
00280 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_parse_string, 0, 0, 1)
00281        ZEND_ARG_INFO(0, input)
00282        ZEND_ARG_INFO(0, config_options)
00283        ZEND_ARG_INFO(0, encoding)
00284 ZEND_END_ARG_INFO()
00285 
00286 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_get_error_buffer, 0, 0, 0)
00287        ZEND_ARG_INFO(0, detailed)
00288 ZEND_END_ARG_INFO()
00289 
00290 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_output, 0)
00291 ZEND_END_ARG_INFO()
00292 
00293 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_parse_file, 0, 0, 1)
00294        ZEND_ARG_INFO(0, file)
00295        ZEND_ARG_INFO(0, config_options)
00296        ZEND_ARG_INFO(0, encoding)
00297        ZEND_ARG_INFO(0, use_include_path)
00298 ZEND_END_ARG_INFO()
00299 
00300 ZEND_BEGIN_ARG_INFO(arginfo_tidy_clean_repair, 0)
00301 ZEND_END_ARG_INFO()
00302 
00303 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_repair_string, 0, 0, 1)
00304        ZEND_ARG_INFO(0, data)
00305        ZEND_ARG_INFO(0, config_file)
00306        ZEND_ARG_INFO(0, encoding)
00307 ZEND_END_ARG_INFO()
00308 
00309 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_repair_file, 0, 0, 1)
00310        ZEND_ARG_INFO(0, filename)
00311        ZEND_ARG_INFO(0, config_file)
00312        ZEND_ARG_INFO(0, encoding)
00313        ZEND_ARG_INFO(0, use_include_path)
00314 ZEND_END_ARG_INFO()
00315 
00316 ZEND_BEGIN_ARG_INFO(arginfo_tidy_diagnose, 0)
00317 ZEND_END_ARG_INFO()
00318 
00319 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_release, 0)
00320 ZEND_END_ARG_INFO()
00321 
00322 #if HAVE_TIDYOPTGETDOC
00323 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_get_opt_doc, 0, 0, 2)
00324        ZEND_ARG_INFO(0, resource)
00325        ZEND_ARG_INFO(0, optname)
00326 ZEND_END_ARG_INFO()
00327 #endif
00328 
00329 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_config, 0)
00330 ZEND_END_ARG_INFO()
00331 
00332 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_status, 0)
00333 ZEND_END_ARG_INFO()
00334 
00335 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_html_ver, 0)
00336 ZEND_END_ARG_INFO()
00337 
00338 ZEND_BEGIN_ARG_INFO(arginfo_tidy_is_xhtml, 0)
00339 ZEND_END_ARG_INFO()
00340 
00341 ZEND_BEGIN_ARG_INFO(arginfo_tidy_is_xml, 0)
00342 ZEND_END_ARG_INFO()
00343 
00344 ZEND_BEGIN_ARG_INFO(arginfo_tidy_error_count, 0)
00345 ZEND_END_ARG_INFO()
00346 
00347 ZEND_BEGIN_ARG_INFO(arginfo_tidy_warning_count, 0)
00348 ZEND_END_ARG_INFO()
00349 
00350 ZEND_BEGIN_ARG_INFO(arginfo_tidy_access_count, 0)
00351 ZEND_END_ARG_INFO()
00352 
00353 ZEND_BEGIN_ARG_INFO(arginfo_tidy_config_count, 0)
00354 ZEND_END_ARG_INFO()
00355 
00356 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_getopt, 0, 0, 1)
00357        ZEND_ARG_INFO(0, option)
00358 ZEND_END_ARG_INFO()
00359 
00360 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_root, 0)
00361 ZEND_END_ARG_INFO()
00362 
00363 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_html, 0)
00364 ZEND_END_ARG_INFO()
00365 
00366 ZEND_BEGIN_ARG_INFO(arginfo_tidy_get_head, 0)
00367 ZEND_END_ARG_INFO()
00368 
00369 ZEND_BEGIN_ARG_INFO_EX(arginfo_tidy_get_body, 0, 0, 1)
00370        ZEND_ARG_INFO(0, tidy)
00371 ZEND_END_ARG_INFO()
00372 
00373 ZEND_BEGIN_ARG_INFO_EX(arginfo_ob_tidyhandler, 0, 0, 1)
00374        ZEND_ARG_INFO(0, input)
00375        ZEND_ARG_INFO(0, mode)
00376 ZEND_END_ARG_INFO()
00377 /* }}} */
00378 
00379 static const zend_function_entry tidy_functions[] = {
00380        PHP_FE(tidy_getopt,             arginfo_tidy_getopt)
00381        PHP_FE(tidy_parse_string,       arginfo_tidy_parse_string)
00382        PHP_FE(tidy_parse_file,         arginfo_tidy_parse_file)
00383        PHP_FE(tidy_get_output,         arginfo_tidy_get_output)
00384        PHP_FE(tidy_get_error_buffer,   arginfo_tidy_get_error_buffer) 
00385        PHP_FE(tidy_clean_repair,       arginfo_tidy_clean_repair)
00386        PHP_FE(tidy_repair_string,  arginfo_tidy_repair_string)
00387        PHP_FE(tidy_repair_file,    arginfo_tidy_repair_file)
00388        PHP_FE(tidy_diagnose,           arginfo_tidy_diagnose)
00389        PHP_FE(tidy_get_release,    arginfo_tidy_get_release)
00390        PHP_FE(tidy_get_config,            arginfo_tidy_get_config)
00391        PHP_FE(tidy_get_status,            arginfo_tidy_get_status)
00392        PHP_FE(tidy_get_html_ver,   arginfo_tidy_get_html_ver)
00393        PHP_FE(tidy_is_xhtml,              arginfo_tidy_is_xhtml)
00394        PHP_FE(tidy_is_xml,         arginfo_tidy_is_xml)
00395        PHP_FE(tidy_error_count,    arginfo_tidy_error_count)
00396        PHP_FE(tidy_warning_count,  arginfo_tidy_warning_count)
00397        PHP_FE(tidy_access_count,   arginfo_tidy_access_count)
00398        PHP_FE(tidy_config_count,   arginfo_tidy_config_count) 
00399 #if HAVE_TIDYOPTGETDOC
00400        PHP_FE(tidy_get_opt_doc,    arginfo_tidy_get_opt_doc)
00401 #endif
00402        PHP_FE(tidy_get_root,              arginfo_tidy_get_root)
00403        PHP_FE(tidy_get_head,              arginfo_tidy_get_head)
00404        PHP_FE(tidy_get_html,              arginfo_tidy_get_html)
00405        PHP_FE(tidy_get_body,              arginfo_tidy_get_body)
00406        PHP_FE(ob_tidyhandler,             arginfo_ob_tidyhandler)
00407        PHP_FE_END
00408 };
00409 
00410 static const zend_function_entry tidy_funcs_doc[] = {
00411        TIDY_METHOD_MAP(getOpt, tidy_getopt, NULL)
00412        TIDY_METHOD_MAP(cleanRepair, tidy_clean_repair, NULL)
00413        TIDY_DOC_ME(parseFile, NULL)
00414        TIDY_DOC_ME(parseString, NULL)
00415        TIDY_METHOD_MAP(repairString, tidy_repair_string, NULL)
00416        TIDY_METHOD_MAP(repairFile, tidy_repair_file, NULL)
00417        TIDY_METHOD_MAP(diagnose, tidy_diagnose, NULL)
00418        TIDY_METHOD_MAP(getRelease, tidy_get_release, NULL)
00419        TIDY_METHOD_MAP(getConfig, tidy_get_config, NULL)
00420        TIDY_METHOD_MAP(getStatus, tidy_get_status, NULL)
00421        TIDY_METHOD_MAP(getHtmlVer, tidy_get_html_ver, NULL)
00422 #if HAVE_TIDYOPTGETDOC
00423        TIDY_METHOD_MAP(getOptDoc, tidy_get_opt_doc, NULL)
00424 #endif
00425        TIDY_METHOD_MAP(isXhtml, tidy_is_xhtml, NULL)
00426        TIDY_METHOD_MAP(isXml, tidy_is_xml, NULL)
00427        TIDY_METHOD_MAP(root, tidy_get_root, NULL)
00428        TIDY_METHOD_MAP(head, tidy_get_head, NULL)
00429        TIDY_METHOD_MAP(html, tidy_get_html, NULL)
00430        TIDY_METHOD_MAP(body, tidy_get_body, NULL)
00431        TIDY_DOC_ME(__construct, NULL)
00432        PHP_FE_END
00433 };
00434 
00435 static const zend_function_entry tidy_funcs_node[] = {
00436        TIDY_NODE_ME(hasChildren, NULL)
00437        TIDY_NODE_ME(hasSiblings, NULL)
00438        TIDY_NODE_ME(isComment, NULL)
00439        TIDY_NODE_ME(isHtml, NULL)
00440        TIDY_NODE_ME(isText, NULL)
00441        TIDY_NODE_ME(isJste, NULL)
00442        TIDY_NODE_ME(isAsp, NULL)
00443        TIDY_NODE_ME(isPhp, NULL)
00444        TIDY_NODE_ME(getParent, NULL)
00445        TIDY_NODE_PRIVATE_ME(__construct, NULL)
00446        PHP_FE_END
00447 };
00448 
00449 static zend_class_entry *tidy_ce_doc, *tidy_ce_node;
00450 
00451 static zend_object_handlers tidy_object_handlers_doc;
00452 static zend_object_handlers tidy_object_handlers_node;
00453 
00454 zend_module_entry tidy_module_entry = {
00455        STANDARD_MODULE_HEADER,
00456        "tidy",
00457        tidy_functions,
00458        PHP_MINIT(tidy),
00459        PHP_MSHUTDOWN(tidy),
00460        PHP_RINIT(tidy),
00461        NULL,
00462        PHP_MINFO(tidy),
00463        PHP_TIDY_MODULE_VERSION,
00464        PHP_MODULE_GLOBALS(tidy),
00465        NULL,
00466        NULL,
00467        NULL,
00468        STANDARD_MODULE_PROPERTIES_EX
00469 };
00470 
00471 #ifdef COMPILE_DL_TIDY
00472 ZEND_GET_MODULE(tidy)
00473 #endif
00474 
00475 static void* TIDY_CALL php_tidy_malloc(size_t len)
00476 {
00477        return emalloc(len);
00478 }
00479 
00480 static void* TIDY_CALL php_tidy_realloc(void *buf, size_t len)
00481 {
00482        return erealloc(buf, len);
00483 }
00484 
00485 static void TIDY_CALL php_tidy_free(void *buf)
00486 {
00487        efree(buf);
00488 }
00489 
00490 static void TIDY_CALL php_tidy_panic(ctmbstr msg)
00491 {
00492        TSRMLS_FETCH();
00493        php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not allocate memory for tidy! (Reason: %s)", (char *)msg);
00494 }
00495 
00496 static int _php_tidy_set_tidy_opt(TidyDoc doc, char *optname, zval *value TSRMLS_DC)
00497 {
00498        TidyOption opt = tidyGetOptionByName(doc, optname);
00499        zval conv = *value;
00500 
00501        if (!opt) {
00502               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown Tidy Configuration Option '%s'", optname);
00503               return FAILURE;
00504        }
00505        
00506        if (tidyOptIsReadOnly(opt)) {
00507               php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Attempting to set read-only option '%s'", optname);
00508               return FAILURE;
00509        }
00510 
00511        switch(tidyOptGetType(opt)) {
00512               case TidyString:
00513                      if (Z_TYPE(conv) != IS_STRING) {
00514                             zval_copy_ctor(&conv);
00515                             convert_to_string(&conv);
00516                      }
00517                      if (tidyOptSetValue(doc, tidyOptGetId(opt), Z_STRVAL(conv))) {
00518                             if (Z_TYPE(conv) != Z_TYPE_P(value)) {
00519                                    zval_dtor(&conv);
00520                             }
00521                             return SUCCESS;
00522                      }
00523                      if (Z_TYPE(conv) != Z_TYPE_P(value)) {
00524                             zval_dtor(&conv);
00525                      }
00526                      break;
00527 
00528               case TidyInteger:
00529                      if (Z_TYPE(conv) != IS_LONG) {
00530                             zval_copy_ctor(&conv);
00531                             convert_to_long(&conv);
00532                      }
00533                      if (tidyOptSetInt(doc, tidyOptGetId(opt), Z_LVAL(conv))) {
00534                             return SUCCESS;
00535                      }
00536                      break;
00537 
00538               case TidyBoolean:
00539                      if (Z_TYPE(conv) != IS_LONG) {
00540                             zval_copy_ctor(&conv);
00541                             convert_to_long(&conv);
00542                      }
00543                      if (tidyOptSetBool(doc, tidyOptGetId(opt), Z_LVAL(conv))) {
00544                             return SUCCESS;
00545                      }
00546                      break;
00547 
00548               default:
00549                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to determine type of configuration option");
00550                      break;
00551        }      
00552 
00553        return FAILURE;
00554 }
00555 
00556 static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_file)
00557 {
00558        char *data=NULL, *arg1, *enc = NULL;
00559        int arg1_len, enc_len = 0, data_len = 0;
00560        zend_bool use_include_path = 0;
00561        TidyDoc doc;
00562        TidyBuffer *errbuf;
00563        zval **config = NULL;
00564 
00565        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &arg1, &arg1_len, &config, &enc, &enc_len, &use_include_path) == FAILURE) {
00566               RETURN_FALSE;
00567        }
00568        
00569        if (is_file) {
00570               if (strlen(arg1) != arg1_len) {
00571                      RETURN_FALSE;
00572               }
00573               if (!(data = php_tidy_file_to_mem(arg1, use_include_path, &data_len TSRMLS_CC))) {
00574                      RETURN_FALSE;
00575               }
00576        } else {
00577               data = arg1;
00578               data_len = arg1_len;
00579        }
00580 
00581        doc = tidyCreate();
00582        errbuf = emalloc(sizeof(TidyBuffer));
00583        tidyBufInit(errbuf);
00584        
00585        if (tidySetErrorBuffer(doc, errbuf) != 0) {
00586               tidyBufFree(errbuf);
00587               efree(errbuf);
00588               tidyRelease(doc);
00589               php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not set Tidy error buffer");
00590        }
00591        
00592        tidyOptSetBool(doc, TidyForceOutput, yes);
00593        tidyOptSetBool(doc, TidyMark, no);
00594        
00595        TIDY_SET_DEFAULT_CONFIG(doc);
00596        
00597        if (config) {
00598               TIDY_APPLY_CONFIG_ZVAL(doc, config);
00599        }
00600 
00601        if(enc_len) {
00602               if (tidySetCharEncoding(doc, enc) < 0) {
00603                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not set encoding '%s'", enc);
00604                      RETVAL_FALSE;
00605               }
00606        }
00607        
00608        if (data) {
00609               TidyBuffer buf;
00610 
00611               tidyBufInit(&buf);
00612               tidyBufAppend(&buf, data, data_len);
00613 
00614               if (tidyParseBuffer(doc, &buf) < 0) {
00615                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errbuf->bp);
00616                      RETVAL_FALSE;
00617               } else {
00618                      if (tidyCleanAndRepair(doc) >= 0) {
00619                             TidyBuffer output;
00620                             tidyBufInit(&output);
00621 
00622                             tidySaveBuffer (doc, &output);
00623                             RETVAL_STRINGL((char*)output.bp, output.size ? output.size-1 : 0, 1);
00624                             tidyBufFree(&output);
00625                      } else {
00626                             RETVAL_FALSE;
00627                      }
00628               }
00629               
00630               tidyBufFree(&buf);
00631        }
00632 
00633        if (is_file) {
00634               efree(data);
00635        }
00636        
00637        tidyBufFree(errbuf);
00638        efree(errbuf);
00639        tidyRelease(doc);
00640 }
00641 
00642 static char *php_tidy_file_to_mem(char *filename, zend_bool use_include_path, int *len TSRMLS_DC)
00643 {
00644        php_stream *stream;
00645        char *data = NULL;
00646 
00647        if (!(stream = php_stream_open_wrapper(filename, "rb", (use_include_path ? USE_PATH : 0) | ENFORCE_SAFE_MODE, NULL))) {
00648               return NULL;
00649        }
00650        if ((*len = (int) php_stream_copy_to_mem(stream, &data, PHP_STREAM_COPY_ALL, 0)) == 0) {
00651               data = estrdup("");
00652               *len = 0;
00653        }
00654        php_stream_close(stream);
00655 
00656        return data;
00657 }
00658 
00659 static void tidy_object_free_storage(void *object TSRMLS_DC)
00660 {
00661        PHPTidyObj *intern = (PHPTidyObj *)object;
00662 
00663        zend_object_std_dtor(&intern->std TSRMLS_CC);
00664 
00665        if (intern->ptdoc) {
00666               intern->ptdoc->ref_count--;
00667 
00668               if (intern->ptdoc->ref_count <= 0) {
00669                      tidyBufFree(intern->ptdoc->errbuf);
00670                      efree(intern->ptdoc->errbuf);
00671                      tidyRelease(intern->ptdoc->doc);
00672                      efree(intern->ptdoc);
00673               }
00674        }
00675 
00676        efree(object);
00677 }
00678 
00679 static void tidy_object_new(zend_class_entry *class_type, zend_object_handlers *handlers,
00680                                                  zend_object_value *retval, tidy_obj_type objtype TSRMLS_DC)
00681 {
00682        PHPTidyObj *intern;
00683        zval *tmp;
00684 
00685        intern = emalloc(sizeof(PHPTidyObj));
00686        memset(intern, 0, sizeof(PHPTidyObj));
00687        zend_object_std_init(&intern->std, class_type TSRMLS_CC);
00688        
00689        zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
00690 
00691        switch(objtype) {
00692               case is_node:
00693                      break;
00694 
00695               case is_doc:
00696                      tidySetMallocCall(php_tidy_malloc);
00697                      tidySetReallocCall(php_tidy_realloc);
00698                      tidySetFreeCall(php_tidy_free);
00699                      tidySetPanicCall(php_tidy_panic);
00700 
00701                      intern->ptdoc = emalloc(sizeof(PHPTidyDoc));
00702                      intern->ptdoc->doc = tidyCreate();
00703                      intern->ptdoc->ref_count = 1;
00704                      intern->ptdoc->errbuf = emalloc(sizeof(TidyBuffer));
00705                      tidyBufInit(intern->ptdoc->errbuf);
00706 
00707                      if (tidySetErrorBuffer(intern->ptdoc->doc, intern->ptdoc->errbuf) != 0) {
00708                             tidyBufFree(intern->ptdoc->errbuf);
00709                             efree(intern->ptdoc->errbuf);
00710                             tidyRelease(intern->ptdoc->doc);
00711                             efree(intern->ptdoc);
00712                             efree(intern);
00713                             php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not set Tidy error buffer");
00714                      }
00715 
00716                      tidyOptSetBool(intern->ptdoc->doc, TidyForceOutput, yes);
00717                      tidyOptSetBool(intern->ptdoc->doc, TidyMark, no);
00718 
00719                      TIDY_SET_DEFAULT_CONFIG(intern->ptdoc->doc);
00720 
00721                      tidy_add_default_properties(intern, is_doc TSRMLS_CC);
00722                      break;
00723 
00724               default:
00725                      break;
00726        }
00727 
00728        retval->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) tidy_object_free_storage, NULL TSRMLS_CC);
00729        retval->handlers = handlers;
00730 }
00731 
00732 static zend_object_value tidy_object_new_node(zend_class_entry *class_type TSRMLS_DC)
00733 {
00734        zend_object_value retval;
00735        tidy_object_new(class_type, &tidy_object_handlers_node, &retval, is_node TSRMLS_CC);
00736        return retval;
00737 }
00738 
00739 static zend_object_value tidy_object_new_doc(zend_class_entry *class_type TSRMLS_DC)
00740 {
00741        zend_object_value retval;
00742        tidy_object_new(class_type, &tidy_object_handlers_doc, &retval, is_doc TSRMLS_CC);
00743        return retval;
00744 }
00745 
00746 static zval * tidy_instanciate(zend_class_entry *pce, zval *object TSRMLS_DC)
00747 {
00748        if (!object) {
00749               ALLOC_ZVAL(object);
00750        }
00751 
00752        Z_TYPE_P(object) = IS_OBJECT;
00753        object_init_ex(object, pce);
00754        Z_SET_REFCOUNT_P(object, 1);
00755        Z_SET_ISREF_P(object);
00756        return object;
00757 }
00758 
00759 static int tidy_doc_cast_handler(zval *in, zval *out, int type TSRMLS_DC)
00760 {
00761        TidyBuffer output;
00762        PHPTidyObj *obj;
00763 
00764        switch(type) {
00765               case IS_LONG:
00766                      ZVAL_LONG(out, 0);
00767                      break;
00768 
00769               case IS_DOUBLE:
00770                      ZVAL_DOUBLE(out, 0);
00771                      break;
00772 
00773               case IS_BOOL:
00774                      ZVAL_BOOL(out, TRUE);
00775                      break;
00776 
00777               case IS_STRING:
00778                      obj = (PHPTidyObj *)zend_object_store_get_object(in TSRMLS_CC);
00779                      tidyBufInit(&output);
00780                      tidySaveBuffer (obj->ptdoc->doc, &output);
00781                      ZVAL_STRINGL(out, (char*)output.bp, output.size ? output.size-1 : 0, TRUE);
00782                      tidyBufFree(&output);
00783                      break;
00784 
00785               default:
00786                      return FAILURE;
00787        }
00788 
00789        return SUCCESS;
00790 }
00791 
00792 static int tidy_node_cast_handler(zval *in, zval *out, int type TSRMLS_DC)
00793 {
00794        TidyBuffer buf;
00795        PHPTidyObj *obj;
00796 
00797        switch(type) {
00798               case IS_LONG:
00799                      ZVAL_LONG(out, 0);
00800                      break;
00801 
00802               case IS_DOUBLE:
00803                      ZVAL_DOUBLE(out, 0);
00804                      break;
00805 
00806               case IS_BOOL:
00807                      ZVAL_BOOL(out, TRUE);
00808                      break;
00809 
00810               case IS_STRING:
00811                      obj = (PHPTidyObj *)zend_object_store_get_object(in TSRMLS_CC);
00812                      tidyBufInit(&buf);
00813                      if (obj->ptdoc) {
00814                             tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf);
00815                      }
00816                      ZVAL_STRINGL(out, (char*)buf.bp, buf.size ? buf.size-1 : 0, TRUE);
00817                      tidyBufFree(&buf);
00818                      break;
00819 
00820               default:
00821                      return FAILURE;
00822        }
00823 
00824        return SUCCESS;
00825 }
00826 
00827 static void tidy_doc_update_properties(PHPTidyObj *obj TSRMLS_DC)
00828 {
00829 
00830        TidyBuffer output;
00831        zval *temp;
00832 
00833        tidyBufInit(&output);
00834        tidySaveBuffer (obj->ptdoc->doc, &output);
00835        
00836        if (output.size) {
00837               MAKE_STD_ZVAL(temp);
00838               ZVAL_STRINGL(temp, (char*)output.bp, output.size-1, TRUE);
00839               zend_hash_update(obj->std.properties, "value", sizeof("value"), (void *)&temp, sizeof(zval *), NULL);
00840        }
00841        
00842        tidyBufFree(&output);
00843 
00844        if (obj->ptdoc->errbuf->size) {
00845               MAKE_STD_ZVAL(temp);
00846               ZVAL_STRINGL(temp, (char*)obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1, TRUE);
00847               zend_hash_update(obj->std.properties, "errorBuffer", sizeof("errorBuffer"), (void *)&temp, sizeof(zval *), NULL);
00848        }
00849 }
00850 
00851 static void tidy_add_default_properties(PHPTidyObj *obj, tidy_obj_type type TSRMLS_DC)
00852 {
00853 
00854        TidyBuffer buf;
00855        TidyAttr      tempattr;
00856        TidyNode      tempnode;
00857        zval *attribute, *children, *temp;
00858        PHPTidyObj *newobj;
00859 
00860        switch(type) {
00861 
00862               case is_node:
00863                      tidyBufInit(&buf);
00864                      tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf);
00865                      ADD_PROPERTY_STRINGL(obj->std.properties, value, buf.bp, buf.size-1);
00866                      tidyBufFree(&buf);
00867 
00868                      ADD_PROPERTY_STRING(obj->std.properties, name, tidyNodeGetName(obj->node));
00869                      ADD_PROPERTY_LONG(obj->std.properties, type, tidyNodeGetType(obj->node));
00870                      ADD_PROPERTY_LONG(obj->std.properties, line, tidyNodeLine(obj->node));
00871                      ADD_PROPERTY_LONG(obj->std.properties, column, tidyNodeColumn(obj->node));
00872                      ADD_PROPERTY_BOOL(obj->std.properties, proprietary, tidyNodeIsProp(obj->ptdoc->doc, obj->node));
00873 
00874                      switch(tidyNodeGetType(obj->node)) {
00875                             case TidyNode_Root:
00876                             case TidyNode_DocType:
00877                             case TidyNode_Text:
00878                             case TidyNode_Comment:
00879                                    break;
00880        
00881                             default:
00882                                    ADD_PROPERTY_LONG(obj->std.properties, id, tidyNodeGetId(obj->node));
00883                      }
00884 
00885                      tempattr = tidyAttrFirst(obj->node);
00886                      MAKE_STD_ZVAL(attribute);
00887 
00888                      if (tempattr) {
00889                             char *name, *val;
00890                             array_init(attribute);
00891 
00892                             do {
00893                                    name = (char *)tidyAttrName(tempattr);
00894                                    val = (char *)tidyAttrValue(tempattr);
00895                                    if (name && val) {
00896                                           add_assoc_string(attribute, name, val, TRUE);
00897                                    }
00898                             } while((tempattr = tidyAttrNext(tempattr)));
00899                      } else {
00900                             ZVAL_NULL(attribute);
00901                      }
00902                      zend_hash_update(obj->std.properties, "attribute", sizeof("attribute"), (void *)&attribute, sizeof(zval *), NULL);
00903 
00904                      tempnode = tidyGetChild(obj->node);
00905 
00906                      MAKE_STD_ZVAL(children);
00907                      if (tempnode) {
00908                             array_init(children);
00909                             do {
00910                                    MAKE_STD_ZVAL(temp);
00911                                    tidy_instanciate(tidy_ce_node, temp TSRMLS_CC);
00912                                    newobj = (PHPTidyObj *) zend_object_store_get_object(temp TSRMLS_CC);
00913                                    newobj->node = tempnode;
00914                                    newobj->type = is_node;
00915                                    newobj->ptdoc = obj->ptdoc;
00916                                    newobj->ptdoc->ref_count++;
00917 
00918                                    tidy_add_default_properties(newobj, is_node TSRMLS_CC);
00919                                    add_next_index_zval(children, temp);
00920 
00921                             } while((tempnode = tidyGetNext(tempnode)));
00922 
00923                      } else {
00924                             ZVAL_NULL(children);
00925                      }
00926 
00927                      zend_hash_update(obj->std.properties, "child", sizeof("child"), (void *)&children, sizeof(zval *), NULL);
00928 
00929                      break;
00930 
00931               case is_doc:
00932                      ADD_PROPERTY_NULL(obj->std.properties, errorBuffer);
00933                      ADD_PROPERTY_NULL(obj->std.properties, value);
00934                      break;
00935        }
00936 }
00937 
00938 static void *php_tidy_get_opt_val(PHPTidyDoc *ptdoc, TidyOption opt, TidyOptionType *type TSRMLS_DC)
00939 {
00940        *type = tidyOptGetType(opt);
00941 
00942        switch (*type) {
00943               case TidyString: {
00944                      char *val = (char *) tidyOptGetValue(ptdoc->doc, tidyOptGetId(opt));
00945                      if (val) {
00946                             return (void *) estrdup(val);
00947                      } else {
00948                             return (void *) estrdup("");
00949                      }
00950               }
00951                      break;
00952 
00953               case TidyInteger:
00954                      return (void *) tidyOptGetInt(ptdoc->doc, tidyOptGetId(opt));
00955                      break;
00956 
00957               case TidyBoolean:
00958                      return (void *) tidyOptGetBool(ptdoc->doc, tidyOptGetId(opt));
00959                      break;
00960        }
00961 
00962        /* should not happen */
00963        return NULL;
00964 }
00965 
00966 static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes node_type)
00967 {
00968        PHPTidyObj *newobj;
00969        TidyNode node;
00970        TIDY_FETCH_OBJECT;
00971 
00972        switch (node_type) {
00973               case is_root_node:
00974                      node = tidyGetRoot(obj->ptdoc->doc);
00975                      break;
00976 
00977               case is_html_node:
00978                      node = tidyGetHtml(obj->ptdoc->doc);
00979                      break;
00980 
00981               case is_head_node:
00982                      node = tidyGetHead(obj->ptdoc->doc);
00983                      break;
00984 
00985               case is_body_node:
00986                      node = tidyGetBody(obj->ptdoc->doc);
00987                      break;
00988 
00989               default:
00990                      RETURN_NULL();
00991                      break;
00992        }
00993 
00994        if (!node) {
00995               RETURN_NULL();
00996        }
00997 
00998        tidy_instanciate(tidy_ce_node, return_value TSRMLS_CC);
00999        newobj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
01000        newobj->type  = is_node;
01001        newobj->ptdoc = obj->ptdoc;
01002        newobj->node  = node;
01003        newobj->ptdoc->ref_count++;
01004 
01005        tidy_add_default_properties(newobj, is_node TSRMLS_CC);
01006 }
01007 
01008 static int _php_tidy_apply_config_array(TidyDoc doc, HashTable *ht_options TSRMLS_DC)
01009 {
01010        char *opt_name = NULL;
01011        zval **opt_val;
01012        ulong opt_indx;
01013        
01014        for (zend_hash_internal_pointer_reset(ht_options);
01015                zend_hash_get_current_data(ht_options, (void **)&opt_val) == SUCCESS;
01016                zend_hash_move_forward(ht_options)) {
01017               
01018               if(zend_hash_get_current_key(ht_options, &opt_name, &opt_indx, FALSE) == FAILURE) {
01019                      php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not retrieve key from option array");
01020                      return FAILURE;
01021               }
01022 
01023               if(opt_name) {
01024                      _php_tidy_set_tidy_opt(doc, opt_name, *opt_val TSRMLS_CC);
01025                      opt_name = NULL;
01026               }
01027                                    
01028        }
01029        
01030        return SUCCESS;
01031 }
01032 
01033 static int php_tidy_parse_string(PHPTidyObj *obj, char *string, int len, char *enc TSRMLS_DC)
01034 {
01035        TidyBuffer buf;
01036        
01037        if(enc) {
01038               if (tidySetCharEncoding(obj->ptdoc->doc, enc) < 0) {
01039                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not set encoding '%s'", enc);
01040                      return FAILURE;
01041               }
01042        }
01043        
01044        tidyBufInit(&buf);
01045        tidyBufAppend(&buf, string, len);
01046        if (tidyParseBuffer(obj->ptdoc->doc, &buf) < 0) {
01047               tidyBufFree(&buf);
01048               php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", obj->ptdoc->errbuf->bp);
01049               return FAILURE;
01050        
01051        }
01052        tidyBufFree(&buf);
01053        tidy_doc_update_properties(obj TSRMLS_CC);
01054 
01055        return SUCCESS;
01056 }
01057 
01058 static PHP_MINIT_FUNCTION(tidy)
01059 {
01060        REGISTER_INI_ENTRIES();
01061        REGISTER_TIDY_CLASS(tidy, doc,     NULL, 0);
01062        REGISTER_TIDY_CLASS(tidyNode, node,       NULL, ZEND_ACC_FINAL_CLASS);
01063 
01064        tidy_object_handlers_doc.cast_object = tidy_doc_cast_handler;
01065        tidy_object_handlers_node.cast_object = tidy_node_cast_handler;
01066 
01067        _php_tidy_register_tags(INIT_FUNC_ARGS_PASSTHRU);
01068        _php_tidy_register_nodetypes(INIT_FUNC_ARGS_PASSTHRU);
01069 
01070        return SUCCESS;
01071 }
01072 
01073 static PHP_RINIT_FUNCTION(tidy)
01074 {
01075        if (INI_BOOL("tidy.clean_output") == TRUE) {
01076               if (php_start_ob_buffer_named("ob_tidyhandler", 0, 1 TSRMLS_CC) == FAILURE) {
01077                      zend_error(E_NOTICE, "Failure installing Tidy output buffering.");
01078               }
01079        }
01080 
01081        return SUCCESS;
01082 }
01083 
01084 static PHP_MSHUTDOWN_FUNCTION(tidy)
01085 {
01086        UNREGISTER_INI_ENTRIES();
01087        return SUCCESS;
01088 }
01089 
01090 static PHP_MINFO_FUNCTION(tidy)
01091 {
01092        php_info_print_table_start();
01093        php_info_print_table_header(2, "Tidy support", "enabled");
01094        php_info_print_table_row(2, "libTidy Release", (char *)tidyReleaseDate());
01095        php_info_print_table_row(2, "Extension Version", PHP_TIDY_MODULE_VERSION " ($Id: tidy.c 321634 2012-01-01 13:15:04Z felipe $)");
01096        php_info_print_table_end();
01097 
01098        DISPLAY_INI_ENTRIES();
01099 }
01100 
01101 static PHP_FUNCTION(ob_tidyhandler)
01102 {
01103        char *input;
01104        int input_len;
01105        long mode;
01106        TidyBuffer errbuf;
01107        TidyDoc doc;
01108 
01109        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &input, &input_len, &mode) == FAILURE) {
01110               return;
01111        }
01112 
01113        doc = tidyCreate();
01114        tidyBufInit(&errbuf);
01115 
01116        tidyOptSetBool(doc, TidyForceOutput, yes);
01117        tidyOptSetBool(doc, TidyMark, no);
01118 
01119        if (tidySetErrorBuffer(doc, &errbuf) != 0) {
01120               tidyRelease(doc);
01121               tidyBufFree(&errbuf);
01122 
01123               php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not set Tidy error buffer");
01124        }
01125 
01126        TIDY_SET_DEFAULT_CONFIG(doc);
01127 
01128        if (input_len > 1) {
01129               TidyBuffer buf;
01130               
01131               tidyBufInit(&buf);
01132               tidyBufAppend(&buf, input, input_len);
01133               
01134               if (tidyParseBuffer(doc, &buf) < 0 || tidyCleanAndRepair(doc) < 0) {
01135                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", errbuf.bp);
01136                      RETVAL_NULL();
01137               } else {
01138                      TidyBuffer output;
01139                      tidyBufInit(&output);
01140 
01141                      tidySaveBuffer(doc, &output);
01142                      RETVAL_STRINGL((char*)output.bp, output.size ? output.size-1 : 0, 1);
01143 
01144                      tidyBufFree(&output);
01145               }
01146               
01147               tidyBufFree(&buf);
01148        } else {
01149               RETVAL_NULL();
01150        }
01151 
01152        tidyRelease(doc);
01153        tidyBufFree(&errbuf);
01154 }
01155 
01156 /* {{{ proto bool tidy_parse_string(string input [, mixed config_options [, string encoding]])
01157    Parse a document stored in a string */
01158 static PHP_FUNCTION(tidy_parse_string)
01159 {
01160        char *input, *enc = NULL;
01161        int input_len, enc_len = 0;
01162        zval **options = NULL;
01163        
01164        PHPTidyObj *obj;
01165 
01166        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zs", &input, &input_len, &options, &enc, &enc_len) == FAILURE) {
01167               RETURN_FALSE;
01168        }
01169 
01170        tidy_instanciate(tidy_ce_doc, return_value TSRMLS_CC);
01171        obj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
01172               
01173        TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
01174        
01175        if(php_tidy_parse_string(obj, input, input_len, enc TSRMLS_CC) == FAILURE) {
01176               zval_dtor(return_value);
01177               INIT_ZVAL(*return_value);
01178               RETURN_FALSE;
01179        }
01180        
01181 }
01182 /* }}} */
01183 
01184 /* {{{ proto string tidy_get_error_buffer([boolean detailed])
01185    Return warnings and errors which occured parsing the specified document*/
01186 static PHP_FUNCTION(tidy_get_error_buffer)
01187 {
01188        TIDY_FETCH_OBJECT;
01189 
01190        if (obj->ptdoc->errbuf && obj->ptdoc->errbuf->bp) {
01191               RETURN_STRINGL((char*)obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1, 1);
01192        } else {
01193               RETURN_FALSE;
01194        }
01195 }
01196 /* }}} */
01197 
01198 /* {{{ proto string tidy_get_output()
01199    Return a string representing the parsed tidy markup */
01200 static PHP_FUNCTION(tidy_get_output)
01201 {
01202        TidyBuffer output;
01203        TIDY_FETCH_OBJECT;
01204 
01205        tidyBufInit(&output);
01206        tidySaveBuffer(obj->ptdoc->doc, &output);
01207 
01208        RETVAL_STRINGL((char*)output.bp, output.size ? output.size-1 : 0, 1);
01209 
01210        tidyBufFree(&output);
01211 }
01212 /* }}} */
01213 
01214 /* {{{ proto boolean tidy_parse_file(string file [, mixed config_options [, string encoding [, bool use_include_path]]])
01215    Parse markup in file or URI */
01216 static PHP_FUNCTION(tidy_parse_file)
01217 {
01218        char *inputfile, *enc = NULL;
01219        int input_len, contents_len, enc_len = 0;
01220        zend_bool use_include_path = 0;
01221        char *contents;
01222        zval **options = NULL;
01223        
01224        PHPTidyObj *obj;
01225 
01226        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &inputfile, &input_len,
01227                                                    &options, &enc, &enc_len, &use_include_path) == FAILURE) {
01228               RETURN_FALSE;
01229        }
01230 
01231        if (strlen(inputfile) != input_len) {
01232               RETURN_FALSE;
01233        }
01234        tidy_instanciate(tidy_ce_doc, return_value TSRMLS_CC);
01235        obj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
01236 
01237        if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path, &contents_len TSRMLS_CC))) {
01238               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory %s", inputfile, (use_include_path) ? "(Using include path)" : "");
01239               RETURN_FALSE;
01240        }
01241 
01242        TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
01243 
01244        if(php_tidy_parse_string(obj, contents, contents_len, enc TSRMLS_CC) == FAILURE) {
01245               zval_dtor(return_value);
01246               INIT_ZVAL(*return_value);
01247               RETVAL_FALSE;
01248        }
01249 
01250        efree(contents);
01251 }
01252 /* }}} */
01253 
01254 /* {{{ proto boolean tidy_clean_repair()
01255    Execute configured cleanup and repair operations on parsed markup */
01256 static PHP_FUNCTION(tidy_clean_repair)
01257 {
01258        TIDY_FETCH_OBJECT;
01259 
01260        if (tidyCleanAndRepair(obj->ptdoc->doc) >= 0) {
01261               tidy_doc_update_properties(obj TSRMLS_CC);
01262               RETURN_TRUE;
01263        }
01264 
01265        RETURN_FALSE;
01266 }
01267 /* }}} */
01268 
01269 /* {{{ proto boolean tidy_repair_string(string data [, mixed config_file [, string encoding]])
01270    Repair a string using an optionally provided configuration file */
01271 static PHP_FUNCTION(tidy_repair_string)
01272 {
01273        php_tidy_quick_repair(INTERNAL_FUNCTION_PARAM_PASSTHRU, FALSE);
01274 }
01275 /* }}} */
01276 
01277 /* {{{ proto boolean tidy_repair_file(string filename [, mixed config_file [, string encoding [, bool use_include_path]]])
01278    Repair a file using an optionally provided configuration file */
01279 static PHP_FUNCTION(tidy_repair_file)
01280 {
01281        php_tidy_quick_repair(INTERNAL_FUNCTION_PARAM_PASSTHRU, TRUE);
01282 }
01283 /* }}} */
01284 
01285 /* {{{ proto boolean tidy_diagnose()
01286    Run configured diagnostics on parsed and repaired markup. */
01287 static PHP_FUNCTION(tidy_diagnose)
01288 {
01289        TIDY_FETCH_OBJECT;
01290 
01291        if (tidyStatus(obj->ptdoc->doc) != 0 && tidyRunDiagnostics(obj->ptdoc->doc) >= 0) {
01292               tidy_doc_update_properties(obj TSRMLS_CC);
01293               RETURN_TRUE;
01294        }
01295 
01296        RETURN_FALSE;
01297 }
01298 /* }}} */
01299 
01300 /* {{{ proto string tidy_get_release()
01301    Get release date (version) for Tidy library */
01302 static PHP_FUNCTION(tidy_get_release)
01303 {
01304        if (zend_parse_parameters_none() == FAILURE) {
01305               return;
01306        }
01307 
01308        RETURN_STRING((char *)tidyReleaseDate(), 1);
01309 }
01310 /* }}} */
01311 
01312 
01313 #if HAVE_TIDYOPTGETDOC
01314 /* {{{ proto string tidy_get_opt_doc(tidy resource, string optname)
01315    Returns the documentation for the given option name */
01316 static PHP_FUNCTION(tidy_get_opt_doc)
01317 {
01318        PHPTidyObj *obj;
01319        char *optname, *optval;
01320        int optname_len;
01321        TidyOption opt;
01322 
01323        TIDY_SET_CONTEXT;
01324 
01325        if (object) {
01326               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &optname, &optname_len) == FAILURE) {
01327                      RETURN_FALSE;
01328               }
01329        } else {
01330               if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, NULL, "Os", &object, tidy_ce_doc, &optname, &optname_len) == FAILURE) {
01331                      RETURN_FALSE;
01332               }
01333        }
01334 
01335        obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC);
01336 
01337        opt = tidyGetOptionByName(obj->ptdoc->doc, optname);
01338 
01339        if (!opt) {
01340               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown Tidy Configuration Option '%s'", optname);
01341               RETURN_FALSE;
01342        }
01343 
01344        if ( (optval = (char *) tidyOptGetDoc(obj->ptdoc->doc, opt)) ) {
01345               RETURN_STRING(optval, 1);
01346        }
01347 
01348        RETURN_FALSE;
01349 }
01350 /* }}} */
01351 #endif
01352 
01353 
01354 /* {{{ proto array tidy_get_config()
01355    Get current Tidy configuarion */
01356 static PHP_FUNCTION(tidy_get_config)
01357 {
01358        TidyIterator itOpt;
01359        char *opt_name;
01360        void *opt_value;
01361        TidyOptionType optt;
01362 
01363        TIDY_FETCH_OBJECT;
01364 
01365        itOpt = tidyGetOptionList(obj->ptdoc->doc);
01366 
01367        array_init(return_value);
01368 
01369        while (itOpt) {
01370               TidyOption opt = tidyGetNextOption(obj->ptdoc->doc, &itOpt);
01371 
01372               opt_name = (char *)tidyOptGetName(opt);
01373               opt_value = php_tidy_get_opt_val(obj->ptdoc, opt, &optt TSRMLS_CC);
01374               switch (optt) {
01375                      case TidyString:
01376                             add_assoc_string(return_value, opt_name, (char*)opt_value, 0);
01377                             break;
01378 
01379                      case TidyInteger:
01380                             add_assoc_long(return_value, opt_name, (long)opt_value);
01381                             break;
01382 
01383                      case TidyBoolean:
01384                             add_assoc_bool(return_value, opt_name, (long)opt_value);
01385                             break;
01386               }
01387        }
01388 
01389        return;
01390 }
01391 /* }}} */
01392 
01393 /* {{{ proto int tidy_get_status()
01394    Get status of specfied document. */
01395 static PHP_FUNCTION(tidy_get_status)
01396 {
01397        TIDY_FETCH_OBJECT;
01398 
01399        RETURN_LONG(tidyStatus(obj->ptdoc->doc));
01400 }
01401 /* }}} */
01402 
01403 /* {{{ proto int tidy_get_html_ver()
01404    Get the Detected HTML version for the specified document. */
01405 static PHP_FUNCTION(tidy_get_html_ver)
01406 {
01407        TIDY_FETCH_OBJECT;
01408 
01409        RETURN_LONG(tidyDetectedHtmlVersion(obj->ptdoc->doc));
01410 }
01411 /* }}} */
01412 
01413 /* {{{ proto boolean tidy_is_xhtml()
01414    Indicates if the document is a XHTML document. */
01415 static PHP_FUNCTION(tidy_is_xhtml)
01416 {
01417        TIDY_FETCH_OBJECT;
01418 
01419        RETURN_BOOL(tidyDetectedXhtml(obj->ptdoc->doc));
01420 }
01421 /* }}} */
01422 
01423 /* {{{ proto boolean tidy_is_xml()
01424    Indicates if the document is a generic (non HTML/XHTML) XML document. */
01425 static PHP_FUNCTION(tidy_is_xml)
01426 {
01427        TIDY_FETCH_OBJECT;
01428 
01429        RETURN_BOOL(tidyDetectedGenericXml(obj->ptdoc->doc));
01430 }
01431 /* }}} */
01432 
01433 /* {{{ proto int tidy_error_count()
01434    Returns the Number of Tidy errors encountered for specified document. */
01435 static PHP_FUNCTION(tidy_error_count)
01436 {
01437        TIDY_FETCH_OBJECT;
01438 
01439        RETURN_LONG(tidyErrorCount(obj->ptdoc->doc));
01440 }
01441 /* }}} */
01442 
01443 /* {{{ proto int tidy_warning_count()
01444    Returns the Number of Tidy warnings encountered for specified document. */
01445 static PHP_FUNCTION(tidy_warning_count)
01446 {
01447        TIDY_FETCH_OBJECT;
01448 
01449        RETURN_LONG(tidyWarningCount(obj->ptdoc->doc));
01450 }
01451 /* }}} */
01452 
01453 /* {{{ proto int tidy_access_count()
01454    Returns the Number of Tidy accessibility warnings encountered for specified document. */
01455 static PHP_FUNCTION(tidy_access_count)
01456 {
01457        TIDY_FETCH_OBJECT;
01458 
01459        RETURN_LONG(tidyAccessWarningCount(obj->ptdoc->doc));
01460 }
01461 /* }}} */
01462 
01463 /* {{{ proto int tidy_config_count()
01464    Returns the Number of Tidy configuration errors encountered for specified document. */
01465 static PHP_FUNCTION(tidy_config_count)
01466 {
01467        TIDY_FETCH_OBJECT;
01468 
01469        RETURN_LONG(tidyConfigErrorCount(obj->ptdoc->doc));
01470 }
01471 /* }}} */
01472 
01473 /* {{{ proto mixed tidy_getopt(string option)
01474    Returns the value of the specified configuration option for the tidy document. */
01475 static PHP_FUNCTION(tidy_getopt)
01476 {  
01477        PHPTidyObj *obj;
01478        char *optname;
01479        void *optval;
01480        int optname_len;
01481        TidyOption opt;
01482        TidyOptionType optt;
01483        
01484        TIDY_SET_CONTEXT;
01485 
01486        if (object) {
01487               if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &optname, &optname_len) == FAILURE) {
01488                      RETURN_FALSE;
01489               }
01490        } else {
01491               if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, NULL, "Os", &object, tidy_ce_doc, &optname, &optname_len) == FAILURE) {
01492                      RETURN_FALSE;
01493               }
01494        }
01495 
01496        obj = (PHPTidyObj *) zend_object_store_get_object(object TSRMLS_CC);
01497 
01498        opt = tidyGetOptionByName(obj->ptdoc->doc, optname);
01499 
01500        if (!opt) {
01501               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown Tidy Configuration Option '%s'", optname);
01502               RETURN_FALSE;
01503        }
01504 
01505        optval = php_tidy_get_opt_val(obj->ptdoc, opt, &optt TSRMLS_CC);
01506        switch (optt) {
01507               case TidyString:
01508                      RETURN_STRING((char *)optval, 0);
01509                      break;
01510 
01511               case TidyInteger:
01512                      RETURN_LONG((long)optval);
01513                      break;
01514 
01515               case TidyBoolean:
01516                      if (optval) {
01517                             RETURN_TRUE;
01518                      } else {
01519                             RETURN_FALSE;
01520                      }
01521                      break;
01522 
01523               default:
01524                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to determine type of configuration option");
01525                      break;
01526        }
01527 
01528        RETURN_FALSE;
01529 }
01530 /* }}} */
01531 
01532 static TIDY_DOC_METHOD(__construct)
01533 {
01534        char *inputfile = NULL, *enc = NULL;
01535        int input_len = 0, enc_len = 0, contents_len = 0;
01536        zend_bool use_include_path = 0;
01537        char *contents;
01538        zval **options = NULL;
01539        
01540        PHPTidyObj *obj;
01541        TIDY_SET_CONTEXT;    
01542        
01543        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sZsb", &inputfile, &input_len,
01544                                                    &options, &enc, &enc_len, &use_include_path) == FAILURE) {
01545               RETURN_FALSE;
01546        }
01547 
01548        obj = (PHPTidyObj *)zend_object_store_get_object(object TSRMLS_CC);
01549        
01550        if (inputfile) {
01551               if (strlen(inputfile) != input_len) {
01552                      RETURN_FALSE;
01553               }
01554               if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path, &contents_len TSRMLS_CC))) {
01555                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory %s", inputfile, (use_include_path) ? "(Using include path)" : "");
01556                      return;
01557               }
01558 
01559               TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
01560 
01561               php_tidy_parse_string(obj, contents, contents_len, enc TSRMLS_CC);
01562 
01563               efree(contents);
01564        }
01565 }
01566 
01567 static TIDY_DOC_METHOD(parseFile)
01568 {
01569        char *inputfile, *enc = NULL;
01570        int input_len, enc_len = 0, contents_len = 0;
01571        zend_bool use_include_path = 0;
01572        char *contents;
01573        zval **options = NULL;
01574        PHPTidyObj *obj;
01575 
01576        TIDY_SET_CONTEXT;
01577 
01578        obj = (PHPTidyObj *)zend_object_store_get_object(object TSRMLS_CC);
01579 
01580        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zsb", &inputfile, &input_len,
01581                                                    &options, &enc, &enc_len, &use_include_path) == FAILURE) {
01582               RETURN_FALSE;
01583        }
01584 
01585        if (strlen(inputfile) != input_len) {
01586               RETURN_FALSE;
01587        }
01588        if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path, &contents_len TSRMLS_CC))) {
01589               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot Load '%s' into memory %s", inputfile, (use_include_path) ? "(Using include path)" : "");
01590               RETURN_FALSE;
01591        }
01592 
01593        TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
01594 
01595        if(php_tidy_parse_string(obj, contents, contents_len, enc TSRMLS_CC) == FAILURE) {
01596               RETVAL_FALSE;
01597        } else {
01598               RETVAL_TRUE;
01599        }
01600 
01601        efree(contents);
01602 }
01603 
01604 static TIDY_DOC_METHOD(parseString)
01605 {
01606        char *input, *enc = NULL;
01607        int input_len, enc_len = 0;
01608        zval **options = NULL;
01609        PHPTidyObj *obj;
01610 
01611        TIDY_SET_CONTEXT;
01612 
01613        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|Zs", &input, &input_len, &options, &enc, &enc_len) == FAILURE) {
01614               RETURN_FALSE;
01615        }
01616 
01617        obj = (PHPTidyObj *)zend_object_store_get_object(object TSRMLS_CC);
01618 
01619        TIDY_APPLY_CONFIG_ZVAL(obj->ptdoc->doc, options);
01620 
01621        if(php_tidy_parse_string(obj, input, input_len, enc TSRMLS_CC) == SUCCESS) {
01622               RETURN_TRUE;
01623        }
01624        
01625        RETURN_FALSE;
01626 }
01627 
01628 
01629 /* {{{ proto TidyNode tidy_get_root()
01630    Returns a TidyNode Object representing the root of the tidy parse tree */
01631 static PHP_FUNCTION(tidy_get_root)
01632 {
01633        php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_root_node);
01634 }
01635 /* }}} */
01636 
01637 /* {{{ proto TidyNode tidy_get_html()
01638    Returns a TidyNode Object starting from the <HTML> tag of the tidy parse tree */
01639 static PHP_FUNCTION(tidy_get_html)
01640 {
01641        php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_html_node);
01642 }
01643 /* }}} */
01644 
01645 /* {{{ proto TidyNode tidy_get_head()
01646    Returns a TidyNode Object starting from the <HEAD> tag of the tidy parse tree */
01647 static PHP_FUNCTION(tidy_get_head)
01648 {
01649        php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_head_node);
01650 }
01651 /* }}} */
01652 
01653 /* {{{ proto TidyNode tidy_get_body(resource tidy)
01654    Returns a TidyNode Object starting from the <BODY> tag of the tidy parse tree */
01655 static PHP_FUNCTION(tidy_get_body)
01656 {
01657        php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_body_node);
01658 }
01659 /* }}} */
01660 
01661 /* {{{ proto boolean tidyNode::hasChildren()
01662    Returns true if this node has children */
01663 static TIDY_NODE_METHOD(hasChildren)
01664 {
01665        TIDY_FETCH_ONLY_OBJECT;
01666 
01667        if (tidyGetChild(obj->node)) {
01668               RETURN_TRUE;
01669        } else {
01670               RETURN_FALSE;
01671        }
01672 }
01673 /* }}} */
01674 
01675 /* {{{ proto boolean tidyNode::hasSiblings()
01676    Returns true if this node has siblings */
01677 static TIDY_NODE_METHOD(hasSiblings)
01678 {
01679        TIDY_FETCH_ONLY_OBJECT;
01680 
01681        if (obj->node && tidyGetNext(obj->node)) {
01682               RETURN_TRUE;
01683        } else {
01684               RETURN_FALSE;
01685        }
01686 }
01687 /* }}} */
01688 
01689 /* {{{ proto boolean tidyNode::isComment()
01690    Returns true if this node represents a comment */
01691 static TIDY_NODE_METHOD(isComment)
01692 {
01693        TIDY_FETCH_ONLY_OBJECT;
01694 
01695        if (tidyNodeGetType(obj->node) == TidyNode_Comment) {
01696               RETURN_TRUE;
01697        } else {
01698               RETURN_FALSE;
01699        }
01700 }
01701 /* }}} */
01702 
01703 /* {{{ proto boolean tidyNode::isHtml()
01704    Returns true if this node is part of a HTML document */
01705 static TIDY_NODE_METHOD(isHtml)
01706 {
01707        TIDY_FETCH_ONLY_OBJECT;
01708 
01709        if (tidyNodeGetType(obj->node) & (TidyNode_Start | TidyNode_End | TidyNode_StartEnd)) {
01710               RETURN_TRUE;
01711        }
01712 
01713        RETURN_FALSE;
01714 }
01715 /* }}} */
01716 
01717 /* {{{ proto boolean tidyNode::isText()
01718    Returns true if this node represents text (no markup) */
01719 static TIDY_NODE_METHOD(isText)
01720 {
01721        TIDY_FETCH_ONLY_OBJECT;
01722 
01723        if (tidyNodeGetType(obj->node) == TidyNode_Text) {
01724               RETURN_TRUE;
01725        } else {
01726               RETURN_FALSE;
01727        }
01728 }
01729 /* }}} */
01730 
01731 /* {{{ proto boolean tidyNode::isJste()
01732    Returns true if this node is JSTE */
01733 static TIDY_NODE_METHOD(isJste)
01734 {
01735        TIDY_FETCH_ONLY_OBJECT;
01736 
01737        if (tidyNodeGetType(obj->node) == TidyNode_Jste) {
01738               RETURN_TRUE;
01739        } else {
01740               RETURN_FALSE;
01741        }
01742 }
01743 /* }}} */
01744 
01745 /* {{{ proto boolean tidyNode::isAsp()
01746    Returns true if this node is ASP */
01747 static TIDY_NODE_METHOD(isAsp)
01748 {
01749        TIDY_FETCH_ONLY_OBJECT;
01750 
01751        if (tidyNodeGetType(obj->node) == TidyNode_Asp) {
01752               RETURN_TRUE;
01753        } else {
01754               RETURN_FALSE;
01755        }
01756 }
01757 /* }}} */
01758 
01759 /* {{{ proto boolean tidyNode::isPhp()
01760    Returns true if this node is PHP */
01761 static TIDY_NODE_METHOD(isPhp)
01762 {
01763        TIDY_FETCH_ONLY_OBJECT;
01764 
01765        if (tidyNodeGetType(obj->node) == TidyNode_Php) {
01766               RETURN_TRUE;
01767        } else {
01768               RETURN_FALSE;
01769        }
01770 }
01771 /* }}} */
01772 
01773 /* {{{ proto tidyNode tidyNode::getParent()
01774    Returns the parent node if available or NULL */
01775 static TIDY_NODE_METHOD(getParent)
01776 {
01777        TidyNode      parent_node;
01778        PHPTidyObj *newobj;
01779        TIDY_FETCH_ONLY_OBJECT;
01780 
01781        parent_node = tidyGetParent(obj->node);
01782        if(parent_node) {
01783               tidy_instanciate(tidy_ce_node, return_value TSRMLS_CC);
01784               newobj = (PHPTidyObj *) zend_object_store_get_object(return_value TSRMLS_CC);
01785               newobj->node = parent_node;
01786               newobj->type = is_node;
01787               newobj->ptdoc = obj->ptdoc;
01788               newobj->ptdoc->ref_count++;
01789               tidy_add_default_properties(newobj, is_node TSRMLS_CC);
01790        } else {
01791               ZVAL_NULL(return_value);
01792        }
01793 }
01794 /* }}} */
01795 
01796 /* {{{ proto void tidyNode::__construct()
01797          __constructor for tidyNode. */
01798 static TIDY_NODE_METHOD(__construct)
01799 {
01800        php_error_docref(NULL TSRMLS_CC, E_ERROR, "You should not create a tidyNode manually");
01801 }   
01802 /* }}} */
01803 
01804 static void _php_tidy_register_nodetypes(INIT_FUNC_ARGS)
01805 {
01806        TIDY_NODE_CONST(ROOT, Root);
01807        TIDY_NODE_CONST(DOCTYPE, DocType);
01808        TIDY_NODE_CONST(COMMENT, Comment);
01809        TIDY_NODE_CONST(PROCINS, ProcIns);
01810        TIDY_NODE_CONST(TEXT, Text);
01811        TIDY_NODE_CONST(START, Start);
01812        TIDY_NODE_CONST(END, End);
01813        TIDY_NODE_CONST(STARTEND, StartEnd);
01814        TIDY_NODE_CONST(CDATA, CDATA);
01815        TIDY_NODE_CONST(SECTION, Section);
01816        TIDY_NODE_CONST(ASP, Asp);
01817        TIDY_NODE_CONST(JSTE, Jste);
01818        TIDY_NODE_CONST(PHP, Php);
01819        TIDY_NODE_CONST(XMLDECL, XmlDecl);
01820 }
01821 
01822 static void _php_tidy_register_tags(INIT_FUNC_ARGS)
01823 {
01824        TIDY_TAG_CONST(UNKNOWN);
01825        TIDY_TAG_CONST(A);
01826        TIDY_TAG_CONST(ABBR);
01827        TIDY_TAG_CONST(ACRONYM);
01828        TIDY_TAG_CONST(ADDRESS);
01829        TIDY_TAG_CONST(ALIGN);
01830        TIDY_TAG_CONST(APPLET);
01831        TIDY_TAG_CONST(AREA);
01832        TIDY_TAG_CONST(B);
01833        TIDY_TAG_CONST(BASE);
01834        TIDY_TAG_CONST(BASEFONT);
01835        TIDY_TAG_CONST(BDO);
01836        TIDY_TAG_CONST(BGSOUND);
01837        TIDY_TAG_CONST(BIG);
01838        TIDY_TAG_CONST(BLINK);
01839        TIDY_TAG_CONST(BLOCKQUOTE);
01840        TIDY_TAG_CONST(BODY);
01841        TIDY_TAG_CONST(BR);
01842        TIDY_TAG_CONST(BUTTON);
01843        TIDY_TAG_CONST(CAPTION);
01844        TIDY_TAG_CONST(CENTER);
01845        TIDY_TAG_CONST(CITE);
01846        TIDY_TAG_CONST(CODE);
01847        TIDY_TAG_CONST(COL);
01848        TIDY_TAG_CONST(COLGROUP);
01849        TIDY_TAG_CONST(COMMENT);
01850        TIDY_TAG_CONST(DD);
01851        TIDY_TAG_CONST(DEL);
01852        TIDY_TAG_CONST(DFN);
01853        TIDY_TAG_CONST(DIR);
01854        TIDY_TAG_CONST(DIV);
01855        TIDY_TAG_CONST(DL);
01856        TIDY_TAG_CONST(DT);
01857        TIDY_TAG_CONST(EM);
01858        TIDY_TAG_CONST(EMBED);
01859        TIDY_TAG_CONST(FIELDSET);
01860        TIDY_TAG_CONST(FONT);
01861        TIDY_TAG_CONST(FORM);
01862        TIDY_TAG_CONST(FRAME);
01863        TIDY_TAG_CONST(FRAMESET);
01864        TIDY_TAG_CONST(H1);
01865        TIDY_TAG_CONST(H2);
01866        TIDY_TAG_CONST(H3);
01867        TIDY_TAG_CONST(H4);
01868        TIDY_TAG_CONST(H5);
01869        TIDY_TAG_CONST(H6);
01870        TIDY_TAG_CONST(HEAD);
01871        TIDY_TAG_CONST(HR);
01872        TIDY_TAG_CONST(HTML);
01873        TIDY_TAG_CONST(I);
01874        TIDY_TAG_CONST(IFRAME);
01875        TIDY_TAG_CONST(ILAYER);
01876        TIDY_TAG_CONST(IMG);
01877        TIDY_TAG_CONST(INPUT);
01878        TIDY_TAG_CONST(INS);
01879        TIDY_TAG_CONST(ISINDEX);
01880        TIDY_TAG_CONST(KBD);
01881        TIDY_TAG_CONST(KEYGEN);
01882        TIDY_TAG_CONST(LABEL);
01883        TIDY_TAG_CONST(LAYER);
01884        TIDY_TAG_CONST(LEGEND);
01885        TIDY_TAG_CONST(LI);
01886        TIDY_TAG_CONST(LINK);
01887        TIDY_TAG_CONST(LISTING);
01888        TIDY_TAG_CONST(MAP);
01889        TIDY_TAG_CONST(MARQUEE);
01890        TIDY_TAG_CONST(MENU);
01891        TIDY_TAG_CONST(META);
01892        TIDY_TAG_CONST(MULTICOL);
01893        TIDY_TAG_CONST(NOBR);
01894        TIDY_TAG_CONST(NOEMBED);
01895        TIDY_TAG_CONST(NOFRAMES);
01896        TIDY_TAG_CONST(NOLAYER);
01897        TIDY_TAG_CONST(NOSAVE);
01898        TIDY_TAG_CONST(NOSCRIPT);
01899        TIDY_TAG_CONST(OBJECT);
01900        TIDY_TAG_CONST(OL);
01901        TIDY_TAG_CONST(OPTGROUP);
01902        TIDY_TAG_CONST(OPTION);
01903        TIDY_TAG_CONST(P);
01904        TIDY_TAG_CONST(PARAM);
01905        TIDY_TAG_CONST(PLAINTEXT);
01906        TIDY_TAG_CONST(PRE);
01907        TIDY_TAG_CONST(Q);
01908        TIDY_TAG_CONST(RB);
01909        TIDY_TAG_CONST(RBC);
01910        TIDY_TAG_CONST(RP);
01911        TIDY_TAG_CONST(RT);
01912        TIDY_TAG_CONST(RTC);
01913        TIDY_TAG_CONST(RUBY);
01914        TIDY_TAG_CONST(S);
01915        TIDY_TAG_CONST(SAMP);
01916        TIDY_TAG_CONST(SCRIPT);
01917        TIDY_TAG_CONST(SELECT);
01918        TIDY_TAG_CONST(SERVER);
01919        TIDY_TAG_CONST(SERVLET);
01920        TIDY_TAG_CONST(SMALL);
01921        TIDY_TAG_CONST(SPACER);
01922        TIDY_TAG_CONST(SPAN);
01923        TIDY_TAG_CONST(STRIKE);
01924        TIDY_TAG_CONST(STRONG);
01925        TIDY_TAG_CONST(STYLE);
01926        TIDY_TAG_CONST(SUB);
01927        TIDY_TAG_CONST(SUP);
01928        TIDY_TAG_CONST(TABLE);
01929        TIDY_TAG_CONST(TBODY);
01930        TIDY_TAG_CONST(TD);
01931        TIDY_TAG_CONST(TEXTAREA);
01932        TIDY_TAG_CONST(TFOOT);
01933        TIDY_TAG_CONST(TH);
01934        TIDY_TAG_CONST(THEAD);
01935        TIDY_TAG_CONST(TITLE);
01936        TIDY_TAG_CONST(TR);
01937        TIDY_TAG_CONST(TT);
01938        TIDY_TAG_CONST(U);
01939        TIDY_TAG_CONST(UL);
01940        TIDY_TAG_CONST(VAR);
01941        TIDY_TAG_CONST(WBR);
01942        TIDY_TAG_CONST(XMP);
01943 }
01944 
01945 #endif
01946 
01947 /*
01948  * Local variables:
01949  * tab-width: 4
01950  * c-basic-offset: 4
01951  * End:
01952  * vim600: noet sw=4 ts=4 fdm=marker
01953  * vim<600: noet sw=4 ts=4
01954  */