1
0

fuckhtml.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. class fuckhtml{
  3. public function __construct($html = null, $isfile = false){
  4. if($html !== null){
  5. $this->load($html, $isfile);
  6. }
  7. }
  8. public function load($html, $isfile = false){
  9. if(is_array($html)){
  10. if(!isset($html["innerHTML"])){
  11. throw new Exception("(load) Supplied array doesn't contain an innerHTML index");
  12. }
  13. $html = $html["innerHTML"];
  14. }
  15. if($isfile){
  16. $handle = fopen($html, "r");
  17. $fetch = fread($handle, filesize($html));
  18. fclose($handle);
  19. $this->html = $fetch;
  20. }else{
  21. $this->html = $html;
  22. }
  23. $this->strlen = strlen($this->html);
  24. }
  25. public function getloadedhtml(){
  26. return $this->html;
  27. }
  28. public function getElementsByTagName(string $tagname){
  29. $out = [];
  30. /*
  31. Scrape start of the tag. Example
  32. <div class="mydiv"> ...
  33. */
  34. if($tagname == "*"){
  35. $tagname = '[A-Za-z0-9._-]+';
  36. }else{
  37. $tagname = preg_quote(strtolower($tagname));
  38. }
  39. preg_match_all(
  40. '/<\s*(' . $tagname . ')(\s(?:[^>\'"]*|"[^"]*"|\'[^\']*\')+)?\s*>/i',
  41. /* '/<\s*(' . $tagname . ')(\s[\S\s]*?)?>/i', */
  42. $this->html,
  43. $starting_tags,
  44. PREG_OFFSET_CAPTURE
  45. );
  46. for($i=0; $i<count($starting_tags[0]); $i++){
  47. /*
  48. Parse attributes
  49. */
  50. $attributes = [];
  51. preg_match_all(
  52. '/([^\/\s\\=]+)(?:\s*=\s*("[^"]*"|\'[^\']*\'|[^\s]*))?/i',
  53. $starting_tags[2][$i][0],
  54. $regex_attributes
  55. );
  56. for($k=0; $k<count($regex_attributes[0]); $k++){
  57. if(trim($regex_attributes[2][$k]) == ""){
  58. $attributes[$regex_attributes[1][$k]] =
  59. "true";
  60. continue;
  61. }
  62. $attributes[strtolower($regex_attributes[1][$k])] =
  63. trim($regex_attributes[2][$k], "'\" \n\r\t\v\x00");
  64. }
  65. $out[] = [
  66. "tagName" => strtolower($starting_tags[1][$i][0]),
  67. "startPos" => $starting_tags[0][$i][1],
  68. "endPos" => 0,
  69. "startTag" => $starting_tags[0][$i][0],
  70. "attributes" => $attributes,
  71. "innerHTML" => null
  72. ];
  73. }
  74. /*
  75. Get innerHTML
  76. */
  77. // get closing tag positions
  78. preg_match_all(
  79. '/<\s*\/\s*(' . $tagname . ')\s*>/i',
  80. $this->html,
  81. $regex_closing_tags,
  82. PREG_OFFSET_CAPTURE
  83. );
  84. // merge opening and closing tags together
  85. for($i=0; $i<count($regex_closing_tags[1]); $i++){
  86. $out[] = [
  87. "tagName" => strtolower($regex_closing_tags[1][$i][0]),
  88. "endTag" => $regex_closing_tags[0][$i][0],
  89. "startPos" => $regex_closing_tags[0][$i][1]
  90. ];
  91. }
  92. usort(
  93. $out,
  94. function($a, $b){
  95. return $a["startPos"] > $b["startPos"];
  96. }
  97. );
  98. // compute the indent level for each element
  99. $level = [];
  100. $count = count($out);
  101. for($i=0; $i<$count; $i++){
  102. if(!isset($level[$out[$i]["tagName"]])){
  103. $level[$out[$i]["tagName"]] = 0;
  104. }
  105. if(isset($out[$i]["startTag"])){
  106. // encountered starting tag
  107. $level[$out[$i]["tagName"]]++;
  108. $out[$i]["level"] = $level[$out[$i]["tagName"]];
  109. }else{
  110. // encountered closing tag
  111. $out[$i]["level"] = $level[$out[$i]["tagName"]];
  112. $level[$out[$i]["tagName"]]--;
  113. }
  114. }
  115. // if the indent level is the same for a div,
  116. // we encountered _THE_ closing tag
  117. for($i=0; $i<$count; $i++){
  118. if(!isset($out[$i]["startTag"])){
  119. continue;
  120. }
  121. for($k=$i; $k<$count; $k++){
  122. if(
  123. isset($out[$k]["endTag"]) &&
  124. $out[$i]["tagName"] == $out[$k]["tagName"] &&
  125. $out[$i]["level"]
  126. === $out[$k]["level"]
  127. ){
  128. $startlen = strlen($out[$i]["startTag"]);
  129. $endlen = strlen($out[$k]["endTag"]);
  130. $out[$i]["endPos"] = $out[$k]["startPos"] + $endlen;
  131. $out[$i]["innerHTML"] =
  132. substr(
  133. $this->html,
  134. $out[$i]["startPos"] + $startlen,
  135. $out[$k]["startPos"] - ($out[$i]["startPos"] + $startlen)
  136. );
  137. $out[$i]["outerHTML"] =
  138. substr(
  139. $this->html,
  140. $out[$i]["startPos"],
  141. $out[$k]["startPos"] - $out[$i]["startPos"] + $endlen
  142. );
  143. break;
  144. }
  145. }
  146. }
  147. // filter out ending divs
  148. for($i=0; $i<$count; $i++){
  149. if(isset($out[$i]["endTag"])){
  150. unset($out[$i]);
  151. }
  152. unset($out[$i]["startTag"]);
  153. }
  154. return array_values($out);
  155. }
  156. public function getElementsByAttributeName(string $name, $collection = null){
  157. if($collection === null){
  158. $collection = $this->getElementsByTagName("*");
  159. }elseif(is_string($collection)){
  160. $collection = $this->getElementsByTagName($collection);
  161. }
  162. $return = [];
  163. foreach($collection as $elem){
  164. foreach($elem["attributes"] as $attrib_name => $attrib_value){
  165. if($attrib_name == $name){
  166. $return[] = $elem;
  167. continue 2;
  168. }
  169. }
  170. }
  171. return $return;
  172. }
  173. public function getElementsByFuzzyAttributeValue(string $name, string $value, $collection = null){
  174. $elems = $this->getElementsByAttributeName($name, $collection);
  175. $value =
  176. explode(
  177. " ",
  178. trim(
  179. preg_replace(
  180. '/\s+/',
  181. " ",
  182. $value
  183. )
  184. )
  185. );
  186. $return = [];
  187. foreach($elems as $elem){
  188. foreach($elem["attributes"] as $attrib_name => $attrib_value){
  189. $attrib_value =
  190. explode(
  191. " ",
  192. trim(
  193. preg_replace(
  194. '/\s+/',
  195. " ",
  196. $attrib_value
  197. )
  198. )
  199. );
  200. $ac = count($attrib_value);
  201. $nc = count($value);
  202. $cr = 0;
  203. for($i=0; $i<$nc; $i++){
  204. for($k=0; $k<$ac; $k++){
  205. if($value[$i] == $attrib_value[$k]){
  206. $cr++;
  207. }
  208. }
  209. }
  210. if($cr === $nc){
  211. $return[] = $elem;
  212. continue 2;
  213. }
  214. }
  215. }
  216. return $return;
  217. }
  218. public function getElementsByAttributeValue(string $name, string $value, $collection = null){
  219. $elems = $this->getElementsByAttributeName($name, $collection);
  220. $return = [];
  221. foreach($elems as $elem){
  222. foreach($elem["attributes"] as $attrib_name => $attrib_value){
  223. if($attrib_value == $value){
  224. $return[] = $elem;
  225. continue 2;
  226. }
  227. }
  228. }
  229. return $return;
  230. }
  231. public function getElementById(string $idname, $collection = null){
  232. $id = $this->getElementsByAttributeValue("id", $idname, $collection);
  233. if(count($id) !== 0){
  234. return $id[0];
  235. }
  236. return false;
  237. }
  238. public function getElementsByClassName(string $classname, $collection = null){
  239. return $this->getElementsByFuzzyAttributeValue("class", $classname, $collection);
  240. }
  241. public function getTextContent($html, $whitespace = false, $trim = true){
  242. if(is_array($html)){
  243. if(!isset($html["innerHTML"])){
  244. throw new Exception("(getTextContent) Supplied array doesn't contain an innerHTML index");
  245. }
  246. $html = $html["innerHTML"];
  247. }
  248. $html = preg_split('/\n|<\/?br>/i', $html);
  249. $out = "";
  250. for($i=0; $i<count($html); $i++){
  251. $tmp =
  252. html_entity_decode(
  253. strip_tags(
  254. $html[$i]
  255. ),
  256. ENT_QUOTES | ENT_XML1, "UTF-8"
  257. );
  258. if($trim){
  259. $tmp = trim($tmp);
  260. }
  261. $out .= $tmp;
  262. if($whitespace === true){
  263. $out .= "\n";
  264. }else{
  265. $out .= " ";
  266. }
  267. }
  268. if($trim){
  269. return trim($out);
  270. }
  271. return $out;
  272. }
  273. public function parseJsObject(string $json){
  274. $bracket = false;
  275. $is_close_bracket = false;
  276. $escape = false;
  277. $lastchar = false;
  278. $json_out = null;
  279. $last_char = null;
  280. $keyword_check = null;
  281. for($i=0; $i<strlen($json); $i++){
  282. switch($json[$i]){
  283. case "\"":
  284. case "'":
  285. if($escape === true){
  286. break;
  287. }
  288. if($json[$i] == $bracket){
  289. $bracket = false;
  290. $is_close_bracket = true;
  291. }else{
  292. if($bracket === false){
  293. $bracket = $json[$i];
  294. }
  295. }
  296. break;
  297. default:
  298. $is_close_bracket = false;
  299. break;
  300. }
  301. if(
  302. $json[$i] == "\\" &&
  303. !(
  304. $lastchar !== false &&
  305. $lastchar . $json[$i] == "\\\\"
  306. )
  307. ){
  308. $escape = true;
  309. }else{
  310. $escape = false;
  311. }
  312. if(
  313. $bracket === false &&
  314. $is_close_bracket === false
  315. ){
  316. // do keyword check
  317. $keyword_check .= $json[$i];
  318. if(in_array($json[$i], [":", "{"])){
  319. $keyword_check = substr($keyword_check, 0, -1);
  320. if(
  321. preg_match(
  322. '/function|array|return/i',
  323. $keyword_check
  324. )
  325. ){
  326. $json_out =
  327. preg_replace(
  328. '/[{"]*' . preg_quote($keyword_check, "/") . '$/',
  329. "",
  330. $json_out
  331. );
  332. }
  333. $keyword_check = null;
  334. }
  335. // here we know we're not iterating over a quoted string
  336. switch($json[$i]){
  337. case "[":
  338. case "{":
  339. $json_out .= $json[$i];
  340. break;
  341. case "]":
  342. case "}":
  343. case ",":
  344. case ":":
  345. if(!in_array($last_char, ["[", "{", "}", "]", "\""])){
  346. $json_out .= "\"";
  347. }
  348. $json_out .= $json[$i];
  349. break;
  350. default:
  351. if(in_array($last_char, ["{", "[", ",", ":"])){
  352. $json_out .= "\"";
  353. }
  354. $json_out .= $json[$i];
  355. break;
  356. }
  357. }else{
  358. $json_out .= $json[$i];
  359. }
  360. $last_char = $json[$i];
  361. }
  362. return json_decode($json_out, true);
  363. }
  364. public function parseJsString($string){
  365. return
  366. preg_replace_callback(
  367. '/\\\u[A-Fa-f0-9]{4}|\\\x[A-Fa-f0-9]{2}|\\\n|\\\r/',
  368. function($match){
  369. switch($match[0][1]){
  370. case "u":
  371. return json_decode('"' . $match[0] . '"');
  372. break;
  373. case "x":
  374. return mb_convert_encoding(
  375. stripcslashes($match[0]),
  376. "utf-8",
  377. "windows-1252"
  378. );
  379. break;
  380. default:
  381. return " ";
  382. break;
  383. }
  384. },
  385. $string
  386. );
  387. }
  388. public function extract_json($json){
  389. $len = strlen($json);
  390. $array_level = 0;
  391. $object_level = 0;
  392. $in_quote = null;
  393. $start = null;
  394. for($i=0; $i<$len; $i++){
  395. switch($json[$i]){
  396. case "\"":
  397. case "'":
  398. if(
  399. $i !== 0 && // only check if a quote could be there
  400. (
  401. (
  402. $json[$i - 1] === "\\" &&
  403. (
  404. $i === 2 ||
  405. $json[$i - 2] === "\\"
  406. )
  407. ) ||
  408. $json[$i - 1] !== "\\"
  409. )
  410. ){
  411. // found a non-escaped quote
  412. if($in_quote === null){
  413. // open quote
  414. $in_quote = $json[$i];
  415. }elseif($in_quote === $json[$i]){
  416. // close quote
  417. $in_quote = null;
  418. }
  419. }
  420. break;
  421. case "[":
  422. if($in_quote === null){
  423. $array_level++;
  424. if($start === null){
  425. $start = $i;
  426. }
  427. }
  428. break;
  429. case "]":
  430. if($in_quote === null){
  431. $array_level--;
  432. }
  433. break;
  434. case "{":
  435. if($in_quote === null){
  436. $object_level++;
  437. if($start === null){
  438. $start = $i;
  439. }
  440. }
  441. break;
  442. case "}":
  443. if($in_quote === null){
  444. $object_level--;
  445. }
  446. break;
  447. }
  448. if(
  449. $array_level === 0 &&
  450. $object_level === 0 &&
  451. $start !== null
  452. ){
  453. return substr($json, $start, $i - $start + 1);
  454. break;
  455. }
  456. }
  457. // fallback
  458. return "[]";
  459. }
  460. }