PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jetpack/vendor/automattic/jetpack-waf/src/class-waf-transforms.php

https://gitlab.com/chernushov881/charity-fund
PHP | 342 lines | 157 code | 26 blank | 159 comment | 5 complexity | 8262010b42bf51d3e1d736a098d9817b MD5 | raw file
  1. <?php
  2. /**
  3. * Transforms for Jetpack Waf
  4. *
  5. * @package automattic/jetpack-waf
  6. */
  7. namespace Automattic\Jetpack\Waf;
  8. /**
  9. * WafTransforms class
  10. */
  11. class WafTransforms {
  12. /**
  13. * Decode a Base64-encoded string.
  14. *
  15. * @param string $value value to be decoded.
  16. * @return string
  17. */
  18. public function base64_decode( $value ) {
  19. return base64_decode( $value );
  20. }
  21. /**
  22. * Remove all characters that might escape a command line command
  23. *
  24. * @see https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual-%28v2.x%29#cmdLine
  25. * @param string $value value to be escaped.
  26. * @return string
  27. */
  28. public function cmd_line( $value ) {
  29. return strtolower(
  30. preg_replace(
  31. '/\s+/',
  32. ' ',
  33. str_replace(
  34. array( ',', ';' ),
  35. ' ',
  36. preg_replace(
  37. '/\s+(?=[\/\(])/',
  38. '',
  39. str_replace(
  40. array( '^', "'", '"', '\\' ),
  41. '',
  42. $value
  43. )
  44. )
  45. )
  46. )
  47. );
  48. }
  49. /**
  50. * Decode a SQL hex string.
  51. *
  52. * @example 414243 decodes to "ABC"
  53. * @param string $value value to be decoded.
  54. * @return string
  55. */
  56. public function sql_hex_decode( $value ) {
  57. return preg_replace_callback(
  58. '/0x[a-f0-9]+/i',
  59. function ( $matches ) {
  60. $str = substr( $matches[0], 2 );
  61. if ( 0 !== strlen( $str ) % 2 ) {
  62. $str = '0' . $str;
  63. }
  64. return hex2bin( $str );
  65. },
  66. $value
  67. );
  68. }
  69. /**
  70. * Encode a string using Base64 encoding.
  71. *
  72. * @param string $value value to be decoded.
  73. * @return string
  74. */
  75. public function base64_encode( $value ) {
  76. return base64_encode( $value );
  77. }
  78. /**
  79. * Convert all whitespace characters to a space and remove any repeated spaces.
  80. *
  81. * @param string $value value to be converted.
  82. * @return string
  83. */
  84. public function compress_whitespace( $value ) {
  85. return preg_replace( '/\s+/', ' ', $value );
  86. }
  87. /**
  88. * Encode string (possibly containing binary characters) by replacing each input byte with two hexadecimal characters.
  89. *
  90. * @param string $value value to be encoded.
  91. * @return string
  92. */
  93. public function hex_encode( $value ) {
  94. return bin2hex( $value );
  95. }
  96. /**
  97. * Decode string that was previously encoded by hexEncode()
  98. *
  99. * @param string $value value to be decoded.
  100. * @return string
  101. */
  102. public function hex_decode( $value ) {
  103. return pack( 'H*', $value );
  104. }
  105. /**
  106. * Decode the characters encoded as HTML entities.
  107. *
  108. * @param mixed $value value do be decoded.
  109. * @return string
  110. */
  111. public function html_entity_decode( $value ) {
  112. return html_entity_decode( $value );
  113. }
  114. /**
  115. * Return the length of the input string.
  116. *
  117. * @param string $value input string.
  118. * @return int
  119. */
  120. public function length( $value ) {
  121. return strlen( $value );
  122. }
  123. /**
  124. * Convert all characters to lowercase.
  125. *
  126. * @param string $value string to be converted.
  127. * @return string
  128. */
  129. public function lowercase( $value ) {
  130. return strtolower( $value );
  131. }
  132. /**
  133. * Calculate an md5 hash for the given data
  134. *
  135. * @param mixed $value value to be hashed.
  136. * @return string
  137. */
  138. public function md5( $value ) {
  139. return md5( $value, true );
  140. }
  141. /**
  142. * Removes multiple slashes, directory self-references, and directory back-references (except when at the beginning of the input) from input string.
  143. *
  144. * @param string $value value to be normalized.
  145. * @return string
  146. */
  147. public function normalize_path( $value ) {
  148. $parts = explode(
  149. '/',
  150. // replace any duplicate slashes with a single one.
  151. preg_replace( '~/{2,}~', '/', $value )
  152. );
  153. $i = 0;
  154. while ( isset( $parts[ $i ] ) ) {
  155. switch ( $parts[ $i ] ) {
  156. // If this folder is a self-reference, remove it.
  157. case '..':
  158. // If this folder is a backreference, remove it unless we're already at the root.
  159. if ( isset( $parts[ $i - 1 ] ) && ! in_array( $parts[ $i - 1 ], array( '', '..' ), true ) ) {
  160. array_splice( $parts, $i - 1, 2 );
  161. $i--;
  162. continue 2;
  163. }
  164. break;
  165. case '.':
  166. array_splice( $parts, $i, 1 );
  167. continue 2;
  168. }
  169. $i++;
  170. }
  171. return implode( '/', $parts );
  172. }
  173. /**
  174. * Convert backslash characters to forward slashes, and then normalize using `normalizePath`
  175. *
  176. * @param string $value to be normalized.
  177. * @return string
  178. */
  179. public function normalize_path_win( $value ) {
  180. return $this->normalize_path( str_replace( '\\', '/', $value ) );
  181. }
  182. /**
  183. * Removes all NUL bytes from input.
  184. *
  185. * @param string $value value to be filtered.
  186. * @return string
  187. */
  188. public function remove_nulls( $value ) {
  189. return str_replace( "\x0", '', $value );
  190. }
  191. /**
  192. * Remove all whitespace characters from input.
  193. *
  194. * @param string $value value to be filtered.
  195. * @return string
  196. */
  197. public function remove_whitespace( $value ) {
  198. return preg_replace( '/\s/', '', $value );
  199. }
  200. /**
  201. * Replaces each occurrence of a C-style comment (/ * ... * /) with a single space.
  202. * Unterminated comments will also be replaced with a space. However, a standalone termination of a comment (* /) will not be acted upon.
  203. *
  204. * @param string $value value to be filtered.
  205. * @return string
  206. */
  207. public function replace_comments( $value ) {
  208. $value = preg_replace( '~/\*.*?\*/|/\*.*?$~Ds', ' ', $value );
  209. return explode( '/*', $value, 2 )[0];
  210. }
  211. /**
  212. * Removes common comments chars (/ *, * /, --, #).
  213. *
  214. * @param string $value value to be filtered.
  215. * @return string
  216. */
  217. public function remove_comments_char( $value ) {
  218. return preg_replace( '~/*|*/|--|#|//~', '', $value );
  219. }
  220. /**
  221. * Replaces each NUL byte in input with a space.
  222. *
  223. * @param string $value value to be filtered.
  224. * @return string
  225. */
  226. public function replace_nulls( $value ) {
  227. return str_replace( "\x0", ' ', $value );
  228. }
  229. /**
  230. * Decode a URL-encoded input string.
  231. *
  232. * @param string $value value to be decoded.
  233. * @return string
  234. */
  235. public function url_decode( $value ) {
  236. return urldecode( $value );
  237. }
  238. /**
  239. * Decode a URL-encoded input string.
  240. *
  241. * @param string $value value to be decoded.
  242. * @return string
  243. */
  244. public function url_decode_uni( $value ) {
  245. error_log( 'JETPACKWAF TRANSFORM NOT IMPLEMENTED: urlDecodeUni' );
  246. return $value;
  247. }
  248. /**
  249. * Decode a json encoded input string.
  250. *
  251. * @param string $value value to be decoded.
  252. * @return string
  253. */
  254. public function js_decode( $value ) {
  255. error_log( 'JETPACKWAF TRANSFORM NOT IMPLEMENTED: jsDecode' );
  256. return $value;
  257. }
  258. /**
  259. * Convert all characters to uppercase.
  260. *
  261. * @param string $value value to be encoded.
  262. * @return string
  263. */
  264. public function uppercase( $value ) {
  265. return strtoupper( $value );
  266. }
  267. /**
  268. * Calculate a SHA1 hash from the input string.
  269. *
  270. * @param mixed $value value to be hashed.
  271. * @return string
  272. */
  273. public function sha1( $value ) {
  274. return sha1( $value, true );
  275. }
  276. /**
  277. * Remove whitespace from the left side of the input string.
  278. *
  279. * @param string $value value to be trimmed.
  280. * @return string
  281. */
  282. public function trim_left( $value ) {
  283. return ltrim( $value );
  284. }
  285. /**
  286. * Remove whitespace from the right side of the input string.
  287. *
  288. * @param string $value value to be trimmed.
  289. * @return string
  290. */
  291. public function trim_right( $value ) {
  292. return rtrim( $value );
  293. }
  294. /**
  295. * Remove whitespace from both sides of the input string.
  296. *
  297. * @param string $value value to be trimmed.
  298. * @return string
  299. */
  300. public function trim( $value ) {
  301. return trim( $value );
  302. }
  303. /**
  304. * Convert utf-8 characters to unicode characters
  305. *
  306. * @param string $value value to be encoded.
  307. * @return string
  308. */
  309. public function utf8_to_unicode( $value ) {
  310. return preg_replace( '/\\\u(?=[a-f0-9]{4})/', '%u', substr( json_encode( $value ), 1, -1 ) );
  311. }
  312. }