1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
|
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: John Coggeshall <john@php.net> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "php.h"
#include "php_tidy.h"
#ifdef HAVE_TIDY
#include "php_ini.h"
#include "ext/standard/info.h"
#ifdef HAVE_TIDY_H
#include "tidy.h"
#elif defined(HAVE_TIDYP_H)
#include "tidyp.h"
#endif
#ifdef HAVE_TIDYBUFFIO_H
#include "tidybuffio.h"
#else
#include "buffio.h"
#endif
#include "tidy_arginfo.h"
#include "Zend/zend_exceptions.h"
/* compatibility with older versions of libtidy */
#ifndef TIDY_CALL
#define TIDY_CALL
#endif
/* {{{ ext/tidy macros */
#define FIX_BUFFER(bptr) do { if ((bptr)->size) { (bptr)->bp[(bptr)->size-1] = '\0'; } } while(0)
#define TIDY_SET_CONTEXT \
zval *object = getThis();
#define TIDY_FETCH_OBJECT \
PHPTidyObj *obj; \
zval *object; \
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &object, tidy_ce_doc) != SUCCESS) { \
RETURN_THROWS(); \
} \
obj = Z_TIDY_P(object); \
#define TIDY_FETCH_INITIALIZED_OBJECT \
TIDY_FETCH_OBJECT; \
if (!obj->ptdoc->initialized) { \
zend_throw_error(NULL, "tidy object is not initialized"); \
return; \
}
#define TIDY_FETCH_ONLY_OBJECT \
PHPTidyObj *obj; \
TIDY_SET_CONTEXT; \
if (zend_parse_parameters_none() != SUCCESS) { \
RETURN_THROWS(); \
} \
obj = Z_TIDY_P(object); \
#define TIDY_SET_DEFAULT_CONFIG(_doc) \
if (TG(default_config) && TG(default_config)[0]) { \
php_tidy_load_config(_doc, TG(default_config)); \
}
/* }}} */
/* {{{ ext/tidy structs */
typedef struct _PHPTidyDoc PHPTidyDoc;
typedef struct _PHPTidyObj PHPTidyObj;
typedef enum {
is_node,
is_doc
} tidy_obj_type;
typedef enum {
is_root_node,
is_html_node,
is_head_node,
is_body_node
} tidy_base_nodetypes;
struct _PHPTidyDoc {
TidyDoc doc;
TidyBuffer *errbuf;
unsigned int ref_count;
unsigned int initialized:1;
};
struct _PHPTidyObj {
TidyNode node;
tidy_obj_type type;
PHPTidyDoc *ptdoc;
zend_object std;
};
static inline PHPTidyObj *php_tidy_fetch_object(zend_object *obj) {
return (PHPTidyObj *)((char*)(obj) - XtOffsetOf(PHPTidyObj, std));
}
#define Z_TIDY_P(zv) php_tidy_fetch_object(Z_OBJ_P((zv)))
/* }}} */
/* {{{ ext/tidy prototypes */
static zend_string *php_tidy_file_to_mem(const char *, bool);
static void tidy_object_free_storage(zend_object *);
static zend_object *tidy_object_new_node(zend_class_entry *);
static zend_object *tidy_object_new_doc(zend_class_entry *);
static zval *tidy_instantiate(zend_class_entry *, zval *);
static zend_result tidy_doc_cast_handler(zend_object *, zval *, int);
static zend_result tidy_node_cast_handler(zend_object *, zval *, int);
static void tidy_doc_update_properties(PHPTidyObj *);
static void tidy_add_node_default_properties(PHPTidyObj *);
static void *php_tidy_get_opt_val(PHPTidyDoc *, TidyOption, TidyOptionType *);
static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes);
static int _php_tidy_set_tidy_opt(TidyDoc, const char *, zval *);
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options);
static PHP_INI_MH(php_tidy_set_clean_output);
static void php_tidy_clean_output_start(const char *name, size_t name_len);
static php_output_handler *php_tidy_output_handler_init(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags);
static zend_result php_tidy_output_handler(void **nothing, php_output_context *output_context);
static PHP_MINIT_FUNCTION(tidy);
static PHP_MSHUTDOWN_FUNCTION(tidy);
static PHP_RINIT_FUNCTION(tidy);
static PHP_RSHUTDOWN_FUNCTION(tidy);
static PHP_MINFO_FUNCTION(tidy);
ZEND_DECLARE_MODULE_GLOBALS(tidy)
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("tidy.default_config", "", PHP_INI_SYSTEM, OnUpdateString, default_config, zend_tidy_globals, tidy_globals)
STD_PHP_INI_BOOLEAN("tidy.clean_output", "0", PHP_INI_USER, php_tidy_set_clean_output, clean_output, zend_tidy_globals, tidy_globals)
PHP_INI_END()
static zend_class_entry *tidy_ce_doc, *tidy_ce_node;
static zend_object_handlers tidy_object_handlers_doc;
static zend_object_handlers tidy_object_handlers_node;
zend_module_entry tidy_module_entry = {
STANDARD_MODULE_HEADER,
"tidy",
ext_functions,
PHP_MINIT(tidy),
PHP_MSHUTDOWN(tidy),
PHP_RINIT(tidy),
PHP_RSHUTDOWN(tidy),
PHP_MINFO(tidy),
PHP_TIDY_VERSION,
PHP_MODULE_GLOBALS(tidy),
NULL,
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
#ifdef COMPILE_DL_TIDY
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(tidy)
#endif
static void* TIDY_CALL php_tidy_malloc(size_t len)
{
return emalloc(len);
}
static void* TIDY_CALL php_tidy_realloc(void *buf, size_t len)
{
return erealloc(buf, len);
}
static void TIDY_CALL php_tidy_free(void *buf)
{
efree(buf);
}
static void TIDY_CALL php_tidy_panic(ctmbstr msg)
{
php_error_docref(NULL, E_ERROR, "Could not allocate memory for tidy! (Reason: %s)", (const char *)msg);
}
static void php_tidy_load_config(TidyDoc doc, const char *path)
{
int ret = tidyLoadConfig(doc, path);
if (ret < 0) {
php_error_docref(NULL, E_WARNING, "Could not load the Tidy configuration file \"%s\"", path);
} else if (ret > 0) {
php_error_docref(NULL, E_NOTICE, "There were errors while parsing the Tidy configuration file \"%s\"", path);
}
}
static zend_result php_tidy_apply_config(TidyDoc doc, const zend_string *str_string, const HashTable *ht_options)
{
if (ht_options) {
return _php_tidy_apply_config_array(doc, ht_options);
} else if (str_string) {
if (php_check_open_basedir(ZSTR_VAL(str_string))) {
return FAILURE;
}
php_tidy_load_config(doc, ZSTR_VAL(str_string));
}
return SUCCESS;
}
static int _php_tidy_set_tidy_opt(TidyDoc doc, const char *optname, zval *value)
{
TidyOption opt = tidyGetOptionByName(doc, optname);
zend_string *str, *tmp_str;
zend_long lval;
if (!opt) {
php_error_docref(NULL, E_WARNING, "Unknown Tidy configuration option \"%s\"", optname);
return FAILURE;
}
#if defined(HAVE_TIDYOPTGETCATEGORY)
if (tidyOptGetCategory(opt) == TidyInternalCategory) {
#else
if (tidyOptIsReadOnly(opt)) {
#endif
php_error_docref(NULL, E_WARNING, "Attempting to set read-only option \"%s\"", optname);
return FAILURE;
}
switch(tidyOptGetType(opt)) {
case TidyString:
str = zval_get_tmp_string(value, &tmp_str);
if (tidyOptSetValue(doc, tidyOptGetId(opt), ZSTR_VAL(str))) {
zend_tmp_string_release(tmp_str);
return SUCCESS;
}
zend_tmp_string_release(tmp_str);
break;
case TidyInteger:
lval = zval_get_long(value);
if (tidyOptSetInt(doc, tidyOptGetId(opt), lval)) {
return SUCCESS;
}
break;
case TidyBoolean:
lval = zval_get_long(value);
if (tidyOptSetBool(doc, tidyOptGetId(opt), lval)) {
return SUCCESS;
}
break;
default:
php_error_docref(NULL, E_WARNING, "Unable to determine type of configuration option");
break;
}
return FAILURE;
}
static void tidy_create_node_object(zval *zv, PHPTidyDoc *ptdoc, TidyNode node)
{
tidy_instantiate(tidy_ce_node, zv);
PHPTidyObj *newobj = Z_TIDY_P(zv);
newobj->node = node;
newobj->type = is_node;
newobj->ptdoc = ptdoc;
newobj->ptdoc->ref_count++;
tidy_add_node_default_properties(newobj);
}
static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, bool is_file)
{
char *enc = NULL;
size_t enc_len = 0;
TidyDoc doc;
TidyBuffer *errbuf;
zend_string *data, *arg1, *config_str = NULL;
HashTable *config_ht = NULL;
if (is_file) {
bool use_include_path = 0;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_PATH_STR(arg1)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(config_ht, config_str)
Z_PARAM_STRING(enc, enc_len)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();
if (!(data = php_tidy_file_to_mem(ZSTR_VAL(arg1), use_include_path))) {
RETURN_FALSE;
}
} else {
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(arg1)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(config_ht, config_str)
Z_PARAM_STRING(enc, enc_len)
ZEND_PARSE_PARAMETERS_END();
data = arg1;
}
if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(data))) {
if (is_file) {
zend_string_release_ex(data, false);
zend_argument_value_error(1, "File content is too long");
} else {
zend_argument_value_error(1, "is too long");
}
RETURN_THROWS();
}
doc = tidyCreate();
errbuf = emalloc(sizeof(TidyBuffer));
tidyBufInit(errbuf);
if (tidySetErrorBuffer(doc, errbuf) != 0) {
tidyBufFree(errbuf);
efree(errbuf);
tidyRelease(doc);
php_error_docref(NULL, E_ERROR, "Could not set Tidy error buffer");
}
tidyOptSetBool(doc, TidyForceOutput, yes);
tidyOptSetBool(doc, TidyMark, no);
TIDY_SET_DEFAULT_CONFIG(doc);
if (php_tidy_apply_config(doc, config_str, config_ht) != SUCCESS) {
RETVAL_FALSE;
} else if (enc_len) {
if (tidySetCharEncoding(doc, enc) < 0) {
php_error_docref(NULL, E_WARNING, "Could not set encoding \"%s\"", enc);
RETVAL_FALSE;
}
}
if (data) {
TidyBuffer buf;
tidyBufInit(&buf);
tidyBufAttach(&buf, (byte *) ZSTR_VAL(data), (uint32_t)ZSTR_LEN(data));
if (tidyParseBuffer(doc, &buf) < 0) {
php_error_docref(NULL, E_WARNING, "%s", errbuf->bp);
RETVAL_FALSE;
} else {
if (tidyCleanAndRepair(doc) >= 0) {
TidyBuffer output;
tidyBufInit(&output);
tidySaveBuffer (doc, &output);
FIX_BUFFER(&output);
RETVAL_STRINGL((const char *) output.bp, output.size ? output.size-1 : 0);
tidyBufFree(&output);
} else {
RETVAL_FALSE;
}
}
}
if (is_file) {
zend_string_release_ex(data, 0);
}
tidyBufFree(errbuf);
efree(errbuf);
tidyRelease(doc);
}
static zend_string *php_tidy_file_to_mem(const char *filename, bool use_include_path)
{
php_stream *stream;
zend_string *data = NULL;
if (!(stream = php_stream_open_wrapper(filename, "rb", (use_include_path ? USE_PATH : 0), NULL))) {
return NULL;
}
if ((data = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0)) == NULL) {
data = ZSTR_EMPTY_ALLOC();
}
php_stream_close(stream);
return data;
}
static void tidy_object_free_storage(zend_object *object)
{
PHPTidyObj *intern = php_tidy_fetch_object(object);
zend_object_std_dtor(&intern->std);
if (intern->ptdoc) {
intern->ptdoc->ref_count--;
if (intern->ptdoc->ref_count <= 0) {
tidyBufFree(intern->ptdoc->errbuf);
efree(intern->ptdoc->errbuf);
tidyRelease(intern->ptdoc->doc);
efree(intern->ptdoc);
}
}
}
static zend_object *tidy_object_new(zend_class_entry *class_type, zend_object_handlers *handlers, tidy_obj_type objtype)
{
PHPTidyObj *intern;
intern = zend_object_alloc(sizeof(PHPTidyObj), class_type);
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
switch(objtype) {
case is_node:
break;
case is_doc:
intern->ptdoc = emalloc(sizeof(PHPTidyDoc));
intern->ptdoc->doc = tidyCreate();
intern->ptdoc->ref_count = 1;
intern->ptdoc->initialized = 0;
intern->ptdoc->errbuf = emalloc(sizeof(TidyBuffer));
tidyBufInit(intern->ptdoc->errbuf);
if (tidySetErrorBuffer(intern->ptdoc->doc, intern->ptdoc->errbuf) != 0) {
tidyBufFree(intern->ptdoc->errbuf);
efree(intern->ptdoc->errbuf);
tidyRelease(intern->ptdoc->doc);
efree(intern->ptdoc);
efree(intern);
php_error_docref(NULL, E_ERROR, "Could not set Tidy error buffer");
}
tidyOptSetBool(intern->ptdoc->doc, TidyForceOutput, yes);
tidyOptSetBool(intern->ptdoc->doc, TidyMark, no);
TIDY_SET_DEFAULT_CONFIG(intern->ptdoc->doc);
break;
}
intern->std.handlers = handlers;
return &intern->std;
}
static zend_object *tidy_object_new_node(zend_class_entry *class_type)
{
return tidy_object_new(class_type, &tidy_object_handlers_node, is_node);
}
static zend_object *tidy_object_new_doc(zend_class_entry *class_type)
{
return tidy_object_new(class_type, &tidy_object_handlers_doc, is_doc);
}
static zval *tidy_instantiate(zend_class_entry *pce, zval *object)
{
object_init_ex(object, pce);
return object;
}
static zend_result tidy_doc_cast_handler(zend_object *in, zval *out, int type)
{
TidyBuffer output;
PHPTidyObj *obj;
switch (type) {
case IS_LONG:
case _IS_NUMBER:
ZVAL_LONG(out, 0);
break;
case IS_DOUBLE:
ZVAL_DOUBLE(out, 0);
break;
case _IS_BOOL:
ZVAL_TRUE(out);
break;
case IS_STRING:
obj = php_tidy_fetch_object(in);
tidyBufInit(&output);
tidySaveBuffer (obj->ptdoc->doc, &output);
if (output.size) {
ZVAL_STRINGL(out, (const char *) output.bp, output.size-1);
} else {
ZVAL_EMPTY_STRING(out);
}
tidyBufFree(&output);
break;
default:
return FAILURE;
}
return SUCCESS;
}
static zend_result tidy_node_cast_handler(zend_object *in, zval *out, int type)
{
TidyBuffer buf;
PHPTidyObj *obj;
switch(type) {
case IS_LONG:
case _IS_NUMBER:
ZVAL_LONG(out, 0);
break;
case IS_DOUBLE:
ZVAL_DOUBLE(out, 0);
break;
case _IS_BOOL:
ZVAL_TRUE(out);
break;
case IS_STRING:
obj = php_tidy_fetch_object(in);
tidyBufInit(&buf);
if (obj->ptdoc) {
tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf);
ZVAL_STRINGL(out, (const char *) buf.bp, buf.size-1);
} else {
ZVAL_EMPTY_STRING(out);
}
tidyBufFree(&buf);
break;
default:
return FAILURE;
}
return SUCCESS;
}
static void tidy_doc_update_properties(PHPTidyObj *obj)
{
TidyBuffer output;
tidyBufInit(&output);
tidySaveBuffer (obj->ptdoc->doc, &output);
if (output.size) {
zend_update_property_stringl(
tidy_ce_doc,
&obj->std,
"value",
sizeof("value") - 1,
(char*) output.bp,
output.size-1
);
}
tidyBufFree(&output);
if (obj->ptdoc->errbuf->size) {
zend_update_property_stringl(
tidy_ce_doc,
&obj->std,
"errorBuffer",
sizeof("errorBuffer") - 1,
(char*) obj->ptdoc->errbuf->bp,
obj->ptdoc->errbuf->size-1
);
}
}
static void tidy_add_node_default_properties(PHPTidyObj *obj)
{
TidyBuffer buf;
TidyAttr tempattr;
TidyNode tempnode;
zval attribute, children, temp;
const char *name;
tidyBufInit(&buf);
tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf);
zend_update_property_stringl(
tidy_ce_node,
&obj->std,
"value",
sizeof("value") - 1,
buf.size ? (const char *) buf.bp : "",
buf.size ? buf.size - 1 : 0
);
tidyBufFree(&buf);
name = (const char *) tidyNodeGetName(obj->node);
zend_update_property_string(
tidy_ce_node,
&obj->std,
"name",
sizeof("name") - 1,
name ? name : ""
);
zend_update_property_long(
tidy_ce_node,
&obj->std,
"type",
sizeof("type") - 1,
tidyNodeGetType(obj->node)
);
zend_update_property_long(
tidy_ce_node,
&obj->std,
"line",
sizeof("line") - 1,
tidyNodeLine(obj->node)
);
zend_update_property_long(
tidy_ce_node,
&obj->std,
"column",
sizeof("column") - 1,
tidyNodeColumn(obj->node)
);
zend_update_property_bool(
tidy_ce_node,
&obj->std,
"proprietary",
sizeof("proprietary") - 1,
tidyNodeIsProp(obj->ptdoc->doc, obj->node)
);
switch(tidyNodeGetType(obj->node)) {
case TidyNode_Root:
case TidyNode_DocType:
case TidyNode_Text:
case TidyNode_Comment:
zend_update_property_null(
tidy_ce_node,
&obj->std,
"id",
sizeof("id") - 1
);
break;
default:
zend_update_property_long(
tidy_ce_node,
&obj->std,
"id",
sizeof("id") - 1,
tidyNodeGetId(obj->node)
);
}
tempattr = tidyAttrFirst(obj->node);
if (tempattr) {
const char *name, *val;
array_init(&attribute);
do {
name = (const char *)tidyAttrName(tempattr);
val = (const char *)tidyAttrValue(tempattr);
if (name) {
if (val) {
add_assoc_string(&attribute, name, val);
} else {
add_assoc_str(&attribute, name, zend_empty_string);
}
}
} while((tempattr = tidyAttrNext(tempattr)));
} else {
ZVAL_NULL(&attribute);
}
zend_update_property(
tidy_ce_node,
&obj->std,
"attribute",
sizeof("attribute") - 1,
&attribute
);
zval_ptr_dtor(&attribute);
tempnode = tidyGetChild(obj->node);
if (tempnode) {
array_init(&children);
do {
tidy_create_node_object(&temp, obj->ptdoc, tempnode);
add_next_index_zval(&children, &temp);
} while((tempnode = tidyGetNext(tempnode)));
} else {
ZVAL_NULL(&children);
}
zend_update_property(
tidy_ce_node,
&obj->std,
"child",
sizeof("child") - 1,
&children
);
zval_ptr_dtor(&children);
}
static void *php_tidy_get_opt_val(PHPTidyDoc *ptdoc, TidyOption opt, TidyOptionType *type)
{
*type = tidyOptGetType(opt);
switch (*type) {
case TidyString: {
char *val = (char *) tidyOptGetValue(ptdoc->doc, tidyOptGetId(opt));
if (val) {
return (void *) zend_string_init(val, strlen(val), 0);
} else {
return (void *) ZSTR_EMPTY_ALLOC();
}
}
break;
case TidyInteger:
return (void *) (uintptr_t) tidyOptGetInt(ptdoc->doc, tidyOptGetId(opt));
break;
case TidyBoolean:
return (void *) tidyOptGetBool(ptdoc->doc, tidyOptGetId(opt));
break;
}
/* should not happen */
return NULL;
}
static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes node_type)
{
TidyNode node;
TIDY_FETCH_OBJECT;
switch (node_type) {
case is_root_node:
node = tidyGetRoot(obj->ptdoc->doc);
break;
case is_html_node:
node = tidyGetHtml(obj->ptdoc->doc);
break;
case is_head_node:
node = tidyGetHead(obj->ptdoc->doc);
break;
case is_body_node:
node = tidyGetBody(obj->ptdoc->doc);
break;
EMPTY_SWITCH_DEFAULT_CASE()
}
if (!node) {
RETURN_NULL();
}
tidy_create_node_object(return_value, obj->ptdoc, node);
}
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options)
{
zval *opt_val;
zend_string *opt_name;
if (!HT_IS_PACKED(ht_options)) {
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(ht_options, opt_name, opt_val) {
if (opt_name == NULL) {
continue;
}
_php_tidy_set_tidy_opt(doc, ZSTR_VAL(opt_name), opt_val);
} ZEND_HASH_FOREACH_END();
}
return SUCCESS;
}
static int php_tidy_parse_string(PHPTidyObj *obj, const char *string, uint32_t len, const char *enc)
{
TidyBuffer buf;
if(enc) {
if (tidySetCharEncoding(obj->ptdoc->doc, enc) < 0) {
php_error_docref(NULL, E_WARNING, "Could not set encoding \"%s\"", enc);
return FAILURE;
}
}
obj->ptdoc->initialized = 1;
tidyBufInit(&buf);
tidyBufAttach(&buf, (byte *) string, len);
if (tidyParseBuffer(obj->ptdoc->doc, &buf) < 0) {
php_error_docref(NULL, E_WARNING, "%s", obj->ptdoc->errbuf->bp);
return FAILURE;
}
tidy_doc_update_properties(obj);
return SUCCESS;
}
static PHP_MINIT_FUNCTION(tidy)
{
tidySetMallocCall(php_tidy_malloc);
tidySetReallocCall(php_tidy_realloc);
tidySetFreeCall(php_tidy_free);
tidySetPanicCall(php_tidy_panic);
REGISTER_INI_ENTRIES();
tidy_ce_doc = register_class_tidy();
tidy_ce_doc->create_object = tidy_object_new_doc;
memcpy(&tidy_object_handlers_doc, &std_object_handlers, sizeof(zend_object_handlers));
tidy_object_handlers_doc.clone_obj = NULL;
tidy_ce_node = register_class_tidyNode();
tidy_ce_node->create_object = tidy_object_new_node;
memcpy(&tidy_object_handlers_node, &std_object_handlers, sizeof(zend_object_handlers));
tidy_object_handlers_node.clone_obj = NULL;
tidy_object_handlers_doc.cast_object = tidy_doc_cast_handler;
tidy_object_handlers_node.cast_object = tidy_node_cast_handler;
tidy_object_handlers_node.offset = tidy_object_handlers_doc.offset = XtOffsetOf(PHPTidyObj, std);
tidy_object_handlers_node.free_obj = tidy_object_handlers_doc.free_obj = tidy_object_free_storage;
register_tidy_symbols(module_number);
php_output_handler_alias_register(ZEND_STRL("ob_tidyhandler"), php_tidy_output_handler_init);
return SUCCESS;
}
static PHP_RINIT_FUNCTION(tidy)
{
#if defined(COMPILE_DL_TIDY) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
php_tidy_clean_output_start(ZEND_STRL("ob_tidyhandler"));
return SUCCESS;
}
static PHP_RSHUTDOWN_FUNCTION(tidy)
{
TG(clean_output) = INI_ORIG_BOOL("tidy.clean_output");
return SUCCESS;
}
static PHP_MSHUTDOWN_FUNCTION(tidy)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
static PHP_MINFO_FUNCTION(tidy)
{
php_info_print_table_start();
php_info_print_table_row(2, "Tidy support", "enabled");
#ifdef HAVE_TIDYBUFFIO_H
php_info_print_table_row(2, "libTidy Version", (const char *)tidyLibraryVersion());
#elif defined(HAVE_TIDYP_H)
php_info_print_table_row(2, "libtidyp Version", (const char *)tidyVersion());
#endif
#ifdef HAVE_TIDYRELEASEDATE
php_info_print_table_row(2, "libTidy Release", (const char *)tidyReleaseDate());
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
static PHP_INI_MH(php_tidy_set_clean_output)
{
int status;
bool value;
value = zend_ini_parse_bool(new_value);
if (stage == PHP_INI_STAGE_RUNTIME) {
status = php_output_get_status();
if (value && (status & PHP_OUTPUT_WRITTEN)) {
php_error_docref(NULL, E_WARNING, "Cannot enable tidy.clean_output - there has already been output");
return FAILURE;
}
if (status & PHP_OUTPUT_SENT) {
php_error_docref(NULL, E_WARNING, "Cannot change tidy.clean_output - headers already sent");
return FAILURE;
}
}
status = OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
if (stage == PHP_INI_STAGE_RUNTIME && value) {
if (!php_output_handler_started(ZEND_STRL("ob_tidyhandler"))) {
php_tidy_clean_output_start(ZEND_STRL("ob_tidyhandler"));
}
}
return status;
}
/*
* NOTE: tidy does not support iterative/cumulative parsing, so chunk-sized output handler is not possible
*/
static void php_tidy_clean_output_start(const char *name, size_t name_len)
{
php_output_handler *h;
if (TG(clean_output) && (h = php_tidy_output_handler_init(name, name_len, 0, PHP_OUTPUT_HANDLER_STDFLAGS))) {
php_output_handler_start(h);
}
}
static php_output_handler *php_tidy_output_handler_init(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags)
{
if (chunk_size) {
php_error_docref(NULL, E_WARNING, "Cannot use a chunk size for ob_tidyhandler");
return NULL;
}
if (!TG(clean_output)) {
TG(clean_output) = 1;
}
return php_output_handler_create_internal(handler_name, handler_name_len, php_tidy_output_handler, chunk_size, flags);
}
static zend_result php_tidy_output_handler(void **nothing, php_output_context *output_context)
{
zend_result status = FAILURE;
TidyDoc doc;
TidyBuffer inbuf, outbuf, errbuf;
if (TG(clean_output) && (output_context->op & PHP_OUTPUT_HANDLER_START) && (output_context->op & PHP_OUTPUT_HANDLER_FINAL)) {
if (ZEND_SIZE_T_UINT_OVFL(output_context->in.used)) {
php_error_docref(NULL, E_WARNING, "Input string is too long");
return status;
}
doc = tidyCreate();
tidyBufInit(&errbuf);
if (0 == tidySetErrorBuffer(doc, &errbuf)) {
tidyOptSetBool(doc, TidyForceOutput, yes);
tidyOptSetBool(doc, TidyMark, no);
TIDY_SET_DEFAULT_CONFIG(doc);
tidyBufInit(&inbuf);
tidyBufAttach(&inbuf, (byte *) output_context->in.data, (uint32_t)output_context->in.used);
if (0 <= tidyParseBuffer(doc, &inbuf) && 0 <= tidyCleanAndRepair(doc)) {
tidyBufInit(&outbuf);
tidySaveBuffer(doc, &outbuf);
FIX_BUFFER(&outbuf);
output_context->out.data = (char *) outbuf.bp;
output_context->out.used = outbuf.size ? outbuf.size-1 : 0;
output_context->out.free = 1;
status = SUCCESS;
}
}
tidyRelease(doc);
tidyBufFree(&errbuf);
}
return status;
}
/* {{{ Parse a document stored in a string */
PHP_FUNCTION(tidy_parse_string)
{
char *enc = NULL;
size_t enc_len = 0;
zend_string *input, *options_str = NULL;
HashTable *options_ht = NULL;
PHPTidyObj *obj;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(input)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(options_ht, options_str)
Z_PARAM_STRING_OR_NULL(enc, enc_len)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(input))) {
zend_argument_value_error(1, "is too long");
RETURN_THROWS();
}
tidy_instantiate(tidy_ce_doc, return_value);
obj = Z_TIDY_P(return_value);
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS
|| php_tidy_parse_string(obj, ZSTR_VAL(input), (uint32_t)ZSTR_LEN(input), enc) != SUCCESS) {
zval_ptr_dtor(return_value);
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Return warnings and errors which occurred parsing the specified document*/
PHP_FUNCTION(tidy_get_error_buffer)
{
TIDY_FETCH_OBJECT;
if (obj->ptdoc->errbuf && obj->ptdoc->errbuf->bp) {
RETURN_STRINGL((const char*)obj->ptdoc->errbuf->bp, obj->ptdoc->errbuf->size-1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Return a string representing the parsed tidy markup */
PHP_FUNCTION(tidy_get_output)
{
TidyBuffer output;
TIDY_FETCH_OBJECT;
tidyBufInit(&output);
tidySaveBuffer(obj->ptdoc->doc, &output);
FIX_BUFFER(&output);
RETVAL_STRINGL((const char *) output.bp, output.size ? output.size-1 : 0);
tidyBufFree(&output);
}
/* }}} */
/* {{{ Parse markup in file or URI */
PHP_FUNCTION(tidy_parse_file)
{
char *enc = NULL;
size_t enc_len = 0;
bool use_include_path = 0;
zend_string *inputfile, *contents, *options_str = NULL;
HashTable *options_ht = NULL;
PHPTidyObj *obj;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_PATH_STR(inputfile)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(options_ht, options_str)
Z_PARAM_STRING_OR_NULL(enc, enc_len)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();
if (!(contents = php_tidy_file_to_mem(ZSTR_VAL(inputfile), use_include_path))) {
php_error_docref(NULL, E_WARNING, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_FALSE;
}
if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(contents))) {
zend_string_release_ex(contents, 0);
zend_value_error("File content is too long");
RETURN_THROWS();
}
tidy_instantiate(tidy_ce_doc, return_value);
obj = Z_TIDY_P(return_value);
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS
|| php_tidy_parse_string(obj, ZSTR_VAL(contents), (uint32_t)ZSTR_LEN(contents), enc) != SUCCESS) {
zval_ptr_dtor(return_value);
RETVAL_FALSE;
}
zend_string_release_ex(contents, 0);
}
/* }}} */
/* {{{ Execute configured cleanup and repair operations on parsed markup */
PHP_FUNCTION(tidy_clean_repair)
{
TIDY_FETCH_OBJECT;
if (tidyCleanAndRepair(obj->ptdoc->doc) >= 0) {
tidy_doc_update_properties(obj);
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ Repair a string using an optionally provided configuration file */
PHP_FUNCTION(tidy_repair_string)
{
php_tidy_quick_repair(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
}
/* }}} */
/* {{{ Repair a file using an optionally provided configuration file */
PHP_FUNCTION(tidy_repair_file)
{
php_tidy_quick_repair(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
}
/* }}} */
/* {{{ Run configured diagnostics on parsed and repaired markup. */
PHP_FUNCTION(tidy_diagnose)
{
TIDY_FETCH_OBJECT;
if (obj->ptdoc->initialized && tidyRunDiagnostics(obj->ptdoc->doc) >= 0) {
tidy_doc_update_properties(obj);
RETURN_TRUE;
}
RETURN_FALSE;
}
/* }}} */
/* {{{ Get release date (version) for Tidy library */
PHP_FUNCTION(tidy_get_release)
{
if (zend_parse_parameters_none() != SUCCESS) {
RETURN_THROWS();
}
#ifdef HAVE_TIDYRELEASEDATE
RETURN_STRING((const char *)tidyReleaseDate());
#else
RETURN_STRING((const char *)"unknown");
#endif
}
/* }}} */
#ifdef HAVE_TIDYOPTGETDOC
/* {{{ Returns the documentation for the given option name */
PHP_FUNCTION(tidy_get_opt_doc)
{
PHPTidyObj *obj;
const char *optval;
char *optname;
size_t optname_len;
TidyOption opt;
zval *object;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, tidy_ce_doc, &optname, &optname_len) != SUCCESS) {
RETURN_THROWS();
}
obj = Z_TIDY_P(object);
opt = tidyGetOptionByName(obj->ptdoc->doc, optname);
if (!opt) {
zend_argument_value_error(hasThis() ? 1 : 2, "is an invalid configuration option, \"%s\" given", optname);
RETURN_THROWS();
}
if ( (optval = (const char *) tidyOptGetDoc(obj->ptdoc->doc, opt)) ) {
RETURN_STRING(optval);
}
RETURN_FALSE;
}
/* }}} */
#endif
/* {{{ Get current Tidy configuration */
PHP_FUNCTION(tidy_get_config)
{
TidyIterator itOpt;
const char *opt_name;
void *opt_value;
TidyOptionType optt;
TIDY_FETCH_OBJECT;
itOpt = tidyGetOptionList(obj->ptdoc->doc);
array_init(return_value);
while (itOpt) {
TidyOption opt = tidyGetNextOption(obj->ptdoc->doc, &itOpt);
opt_name = (const char *)tidyOptGetName(opt);
opt_value = php_tidy_get_opt_val(obj->ptdoc, opt, &optt);
switch (optt) {
case TidyString:
add_assoc_str(return_value, opt_name, (zend_string*)opt_value);
break;
case TidyInteger:
add_assoc_long(return_value, opt_name, (zend_long)opt_value);
break;
case TidyBoolean:
add_assoc_bool(return_value, opt_name, opt_value ? 1 : 0);
break;
}
}
}
/* }}} */
/* {{{ Get status of specified document. */
PHP_FUNCTION(tidy_get_status)
{
TIDY_FETCH_OBJECT;
RETURN_LONG(tidyStatus(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Get the Detected HTML version for the specified document. */
PHP_FUNCTION(tidy_get_html_ver)
{
TIDY_FETCH_INITIALIZED_OBJECT;
RETURN_LONG(tidyDetectedHtmlVersion(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Indicates if the document is a XHTML document. */
PHP_FUNCTION(tidy_is_xhtml)
{
TIDY_FETCH_INITIALIZED_OBJECT;
RETURN_BOOL(tidyDetectedXhtml(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Indicates if the document is a generic (non HTML/XHTML) XML document. */
PHP_FUNCTION(tidy_is_xml)
{
TIDY_FETCH_INITIALIZED_OBJECT;
RETURN_BOOL(tidyDetectedGenericXml(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Returns the Number of Tidy errors encountered for specified document. */
PHP_FUNCTION(tidy_error_count)
{
TIDY_FETCH_OBJECT;
RETURN_LONG(tidyErrorCount(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Returns the Number of Tidy warnings encountered for specified document. */
PHP_FUNCTION(tidy_warning_count)
{
TIDY_FETCH_OBJECT;
RETURN_LONG(tidyWarningCount(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Returns the Number of Tidy accessibility warnings encountered for specified document. */
PHP_FUNCTION(tidy_access_count)
{
TIDY_FETCH_OBJECT;
RETURN_LONG(tidyAccessWarningCount(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Returns the Number of Tidy configuration errors encountered for specified document. */
PHP_FUNCTION(tidy_config_count)
{
TIDY_FETCH_OBJECT;
RETURN_LONG(tidyConfigErrorCount(obj->ptdoc->doc));
}
/* }}} */
/* {{{ Returns the value of the specified configuration option for the tidy document. */
PHP_FUNCTION(tidy_getopt)
{
PHPTidyObj *obj;
char *optname;
void *optval;
size_t optname_len;
TidyOption opt;
TidyOptionType optt;
zval *object;
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &object, tidy_ce_doc, &optname, &optname_len) != SUCCESS) {
RETURN_THROWS();
}
obj = Z_TIDY_P(object);
opt = tidyGetOptionByName(obj->ptdoc->doc, optname);
if (!opt) {
zend_argument_value_error(hasThis() ? 1 : 2, "is an invalid configuration option, \"%s\" given", optname);
RETURN_THROWS();
}
optval = php_tidy_get_opt_val(obj->ptdoc, opt, &optt);
switch (optt) {
case TidyString:
RETVAL_STR((zend_string*)optval);
return;
case TidyInteger:
RETURN_LONG((zend_long)optval);
break;
case TidyBoolean:
if (optval) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
break;
default:
php_error_docref(NULL, E_WARNING, "Unable to determine type of configuration option");
break;
}
RETURN_FALSE;
}
/* }}} */
PHP_METHOD(tidy, __construct)
{
char *enc = NULL;
size_t enc_len = 0;
bool use_include_path = 0;
HashTable *options_ht = NULL;
zend_string *contents, *inputfile = NULL, *options_str = NULL;
PHPTidyObj *obj;
ZEND_PARSE_PARAMETERS_START(0, 4)
Z_PARAM_OPTIONAL
Z_PARAM_PATH_STR_OR_NULL(inputfile)
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(options_ht, options_str)
Z_PARAM_STRING_OR_NULL(enc, enc_len)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();
TIDY_SET_CONTEXT;
obj = Z_TIDY_P(object);
if (inputfile) {
if (!(contents = php_tidy_file_to_mem(ZSTR_VAL(inputfile), use_include_path))) {
zend_throw_error(zend_ce_exception, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_THROWS();
}
if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(contents))) {
zend_string_release_ex(contents, 0);
zend_value_error("File content is too long");
RETURN_THROWS();
}
zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS) {
zend_restore_error_handling(&error_handling);
zend_string_release_ex(contents, 0);
RETURN_THROWS();
}
zend_restore_error_handling(&error_handling);
php_tidy_parse_string(obj, ZSTR_VAL(contents), (uint32_t)ZSTR_LEN(contents), enc);
zend_string_release_ex(contents, 0);
}
}
PHP_METHOD(tidy, parseFile)
{
char *enc = NULL;
size_t enc_len = 0;
bool use_include_path = 0;
HashTable *options_ht = NULL;
zend_string *inputfile, *contents, *options_str = NULL;
PHPTidyObj *obj;
ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_PATH_STR(inputfile)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(options_ht, options_str)
Z_PARAM_STRING_OR_NULL(enc, enc_len)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();
TIDY_SET_CONTEXT;
obj = Z_TIDY_P(object);
if (!(contents = php_tidy_file_to_mem(ZSTR_VAL(inputfile), use_include_path))) {
php_error_docref(NULL, E_WARNING, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_FALSE;
}
if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(contents))) {
zend_string_release_ex(contents, 0);
zend_value_error("File content is too long");
RETURN_THROWS();
}
RETVAL_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) == SUCCESS
&& php_tidy_parse_string(obj, ZSTR_VAL(contents), (uint32_t)ZSTR_LEN(contents), enc) == SUCCESS);
zend_string_release_ex(contents, 0);
}
PHP_METHOD(tidy, parseString)
{
char *enc = NULL;
size_t enc_len = 0;
HashTable *options_ht = NULL;
PHPTidyObj *obj;
zend_string *input, *options_str = NULL;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(input)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(options_ht, options_str)
Z_PARAM_STRING_OR_NULL(enc, enc_len)
ZEND_PARSE_PARAMETERS_END();
if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(input))) {
zend_argument_value_error(1, "is too long");
RETURN_THROWS();
}
TIDY_SET_CONTEXT;
obj = Z_TIDY_P(object);
RETURN_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) == SUCCESS
&& php_tidy_parse_string(obj, ZSTR_VAL(input), (uint32_t)ZSTR_LEN(input), enc) == SUCCESS);
}
/* {{{ Returns a TidyNode Object representing the root of the tidy parse tree */
PHP_FUNCTION(tidy_get_root)
{
php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_root_node);
}
/* }}} */
/* {{{ Returns a TidyNode Object starting from the <HTML> tag of the tidy parse tree */
PHP_FUNCTION(tidy_get_html)
{
php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_html_node);
}
/* }}} */
/* {{{ Returns a TidyNode Object starting from the <HEAD> tag of the tidy parse tree */
PHP_FUNCTION(tidy_get_head)
{
php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_head_node);
}
/* }}} */
/* {{{ Returns a TidyNode Object starting from the <BODY> tag of the tidy parse tree */
PHP_FUNCTION(tidy_get_body)
{
php_tidy_create_node(INTERNAL_FUNCTION_PARAM_PASSTHRU, is_body_node);
}
/* }}} */
/* {{{ Returns true if this node has children */
PHP_METHOD(tidyNode, hasChildren)
{
TIDY_FETCH_ONLY_OBJECT;
if (tidyGetChild(obj->node)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns true if this node has siblings */
PHP_METHOD(tidyNode, hasSiblings)
{
TIDY_FETCH_ONLY_OBJECT;
if (obj->node && tidyGetNext(obj->node)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns true if this node represents a comment */
PHP_METHOD(tidyNode, isComment)
{
TIDY_FETCH_ONLY_OBJECT;
if (tidyNodeGetType(obj->node) == TidyNode_Comment) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns true if this node is part of a HTML document */
PHP_METHOD(tidyNode, isHtml)
{
TIDY_FETCH_ONLY_OBJECT;
switch (tidyNodeGetType(obj->node)) {
case TidyNode_Start:
case TidyNode_End:
case TidyNode_StartEnd:
RETURN_TRUE;
default:
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns true if this node represents text (no markup) */
PHP_METHOD(tidyNode, isText)
{
TIDY_FETCH_ONLY_OBJECT;
if (tidyNodeGetType(obj->node) == TidyNode_Text) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns true if this node is JSTE */
PHP_METHOD(tidyNode, isJste)
{
TIDY_FETCH_ONLY_OBJECT;
if (tidyNodeGetType(obj->node) == TidyNode_Jste) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns true if this node is ASP */
PHP_METHOD(tidyNode, isAsp)
{
TIDY_FETCH_ONLY_OBJECT;
if (tidyNodeGetType(obj->node) == TidyNode_Asp) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns true if this node is PHP */
PHP_METHOD(tidyNode, isPhp)
{
TIDY_FETCH_ONLY_OBJECT;
if (tidyNodeGetType(obj->node) == TidyNode_Php) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ Returns the parent node if available or NULL */
PHP_METHOD(tidyNode, getParent)
{
TIDY_FETCH_ONLY_OBJECT;
TidyNode parent_node = tidyGetParent(obj->node);
if (parent_node) {
tidy_create_node_object(return_value, obj->ptdoc, parent_node);
}
}
/* }}} */
PHP_METHOD(tidyNode, getPreviousSibling)
{
TIDY_FETCH_ONLY_OBJECT;
TidyNode previous_node = tidyGetPrev(obj->node);
if (previous_node) {
tidy_create_node_object(return_value, obj->ptdoc, previous_node);
}
}
PHP_METHOD(tidyNode, getNextSibling)
{
TIDY_FETCH_ONLY_OBJECT;
TidyNode next_node = tidyGetNext(obj->node);
if (next_node) {
tidy_create_node_object(return_value, obj->ptdoc, next_node);
}
}
/* {{{ __constructor for tidyNode. */
PHP_METHOD(tidyNode, __construct)
{
zend_throw_error(NULL, "You should not create a tidyNode manually");
}
/* }}} */
#endif
|