Преглед изворни кода

lib_phpQuery support PHP 8 (#3186)

* Update to support PHP 8

Replaced create_function calls with anonymous functions in lib/lib_phpQuery.php

Ref Issue: #3082

* Remove commented code blocks

* White space

* More PHP8 fixes

Fix: Fatal error: Array and string offset access syntax with curly
braces is no longer supported in /FreshRSS/lib/lib_phpQuery.php on line
2174, etc.

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Prashant Tholia пре 5 година
родитељ
комит
83166a9f5d
1 измењених фајлова са 55 додато и 53 уклоњено
  1. 55 53
      lib/lib_phpQuery.php

+ 55 - 53
lib/lib_phpQuery.php

@@ -1033,7 +1033,22 @@ class CallbackBody extends Callback {
 			$param3 = null) {
 		$params = func_get_args();
 		$params = array_slice($params, 2);
-		$this->callback = create_function($paramList, $code);
+
+		$this->callback = function (&...$args) use ($paramList, $code) {
+			$paramListArray = explode(',', $paramList);
+			for ($i = 0; $i < count($paramListArray); $i++) {
+				$param = trim($paramListArray[$i]);
+				if ($param[0] === '&') {
+					$paramName = ltrim($param, '&$');
+					${$paramName} = &$args[$i];
+				} else {
+					$paramName = ltrim($param, '$');
+					${$paramName} = $args[$i];
+				}
+			}
+			return eval($code);
+		};
+
 		$this->params = $params;
 	}
 }
@@ -2093,16 +2108,18 @@ class phpQueryObject
 			break;
 			case 'parent':
 				$this->elements = $this->map(
-					create_function('$node', '
+					function ($node) {
 						return $node instanceof DOMELEMENT && $node->childNodes->length
-							? $node : null;')
+							? $node : null;
+					}
 				)->elements;
 			break;
 			case 'empty':
 				$this->elements = $this->map(
-					create_function('$node', '
+					function ($node) {
 						return $node instanceof DOMELEMENT && $node->childNodes->length
-							? null : $node;')
+							? null : $node;
+					}
 				)->elements;
 			break;
 			case 'disabled':
@@ -2115,47 +2132,38 @@ class phpQueryObject
 			break;
 			case 'enabled':
 				$this->elements = $this->map(
-					create_function('$node', '
-						return pq($node)->not(":disabled") ? $node : null;')
+					function ($node) {
+						return pq($node)->not(":disabled") ? $node : null;
+					}
 				)->elements;
 			break;
 			case 'header':
 				$this->elements = $this->map(
-					create_function('$node',
-						'$isHeader = isset($node->tagName) && in_array($node->tagName, array(
+					function ($node) {
+						$isHeader = isset($node->tagName) && in_array($node->tagName, array(
 							"h1", "h2", "h3", "h4", "h5", "h6", "h7"
 						));
 						return $isHeader
 							? $node
-							: null;')
+							: null;
+					}
 				)->elements;
-//				$this->elements = $this->map(
-//					create_function('$node', '$node = pq($node);
-//						return $node->is("h1")
-//							|| $node->is("h2")
-//							|| $node->is("h3")
-//							|| $node->is("h4")
-//							|| $node->is("h5")
-//							|| $node->is("h6")
-//							|| $node->is("h7")
-//							? $node
-//							: null;')
-//				)->elements;
 			break;
 			case 'only-child':
 				$this->elements = $this->map(
-					create_function('$node',
-						'return pq($node)->siblings()->size() == 0 ? $node : null;')
+					function ($node) {
+						return pq($node)->siblings()->size() == 0 ? $node : null;
+					}
 				)->elements;
 			break;
 			case 'first-child':
 				$this->elements = $this->map(
-					create_function('$node', 'return pq($node)->prevAll()->size() == 0 ? $node : null;')
+					function ($node) { return pq($node)->prevAll()->size() == 0 ? $node : null; }
 				)->elements;
 			break;
 			case 'last-child':
 				$this->elements = $this->map(
-					create_function('$node', 'return pq($node)->nextAll()->size() == 0 ? $node : null;')
+					function ($node) { return pq($node)->nextAll()->size() == 0 ? $node : null; }
 				)->elements;
 			break;
 			case 'nth-child':
@@ -2163,32 +2171,33 @@ class phpQueryObject
 				if (! $param)
 					break;
 					// nth-child(n+b) to nth-child(1n+b)
-				if ($param{0} == 'n')
+				if ($param[0] == 'n')
 					$param = '1'.$param;
 				// :nth-child(index/even/odd/equation)
 				if ($param == 'even' || $param == 'odd')
 					$mapped = $this->map(
-						create_function('$node, $param',
-							'$index = pq($node)->prevAll()->size()+1;
+						function ($node, $param) {
+							$index = pq($node)->prevAll()->size()+1;
 							if ($param == "even" && ($index%2) == 0)
 								return $node;
 							else if ($param == "odd" && $index%2 == 1)
 								return $node;
 							else
-								return null;'),
+								return null;
+						},
 						new CallbackParam(), $param
 					);
-				else if (mb_strlen($param) > 1 && $param{1} == 'n')
+				else if (mb_strlen($param) > 1 && $param[1] == 'n')
 					// an+b
 					$mapped = $this->map(
-						create_function('$node, $param',
-							'$prevs = pq($node)->prevAll()->size();
+						function ($node, $param) {
+							$prevs = pq($node)->prevAll()->size();
 							$index = 1+$prevs;
 							$b = mb_strlen($param) > 3
-								? $param{3}
+								? $param[3]
 								: 0;
-							$a = $param{0};
-							if ($b && $param{2} == "-")
+							$a = $param[0];
+							if ($b && $param[2] == "-")
 								$b = -$b;
 							if ($a > 0) {
 								return ($index-$b)%$a == 0
@@ -2215,20 +2224,21 @@ class phpQueryObject
 //								return ($index-$b)%$a == 0
 //									? $node
 //									: null;
-							'),
+						},
 						new CallbackParam(), $param
 					);
 				else
 					// index
 					$mapped = $this->map(
-						create_function('$node, $index',
-							'$prevs = pq($node)->prevAll()->size();
+						function ($node, $index) {
+							$prevs = pq($node)->prevAll()->size();
 							if ($prevs && $prevs == $index-1)
 								return $node;
 							else if (! $prevs && $index == 1)
 								return $node;
 							else
-								return null;'),
+								return null;
+						},
 						new CallbackParam(), $param
 					);
 				$this->elements = $mapped->elements;
@@ -4701,11 +4711,6 @@ abstract class phpQuery {
 			while (preg_match($regex, $php, $matches)) {
 				$php = preg_replace_callback(
 					$regex,
-//					create_function('$m, $charset = "'.$charset.'"',
-//						'return $m[1].$m[2]
-//							.htmlspecialchars("<"."?php".$m[4]."?".">", ENT_QUOTES|ENT_NOQUOTES, $charset)
-//							.$m[5].$m[2];'
-//					),
 					array('phpQuery', '_phpToMarkupCallback'),
 					$php
 				);
@@ -4737,9 +4742,6 @@ abstract class phpQuery {
 		/* <php>...</php> to <?php...? > */
 		$content = preg_replace_callback(
 			'@<php>\s*<!--(.*?)-->\s*</php>@s',
-//			create_function('$m',
-//				'return "<'.'?php ".htmlspecialchars_decode($m[1])." ?'.'>";'
-//			),
 			array('phpQuery', '_markupToPHPCallback'),
 			$content
 		);
@@ -4752,15 +4754,15 @@ abstract class phpQuery {
 			while (preg_match($regex, $content))
 				$content = preg_replace_callback(
 					$regex,
-					create_function('$m',
-						'return $m[1].$m[2].$m[3]."<?php "
+					function ($m) {
+						return $m[1].$m[2].$m[3]."<?php "
 							.str_replace(
 								array("%20", "%3E", "%09", "&#10;", "&#9;", "%7B", "%24", "%7D", "%22", "%5B", "%5D"),
-								array(" ", ">", "	", "\n", "	", "{", "$", "}", \'"\', "[", "]"),
+								array(" ", ">", "	", "\n", "	", "{", "$", "}", '"', "[", "]"),
 								htmlspecialchars_decode($m[4])
 							)
-							." ?>".$m[5].$m[2];'
-					),
+							." ?>".$m[5].$m[2];
+					},
 					$content
 				);
 		return $content;