PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/fusion-core/libs/class-avada-sanitize.php

https://gitlab.com/webkod3r/tripolis
PHP | 419 lines | 288 code | 35 blank | 96 comment | 35 complexity | 86e7492ce21ea28d46c28872f6e4b888 MD5 | raw file
  1. <?php
  2. //if ( ! class_exists( 'Avada_Sanitize' ) ) {
  3. class Avada_Sanitize {
  4. /**
  5. * Sanitize values like for example 10px, 30% etc.
  6. */
  7. public static function size( $value ) {
  8. if ( $value == 'auto' ) {
  9. return $value;
  10. }
  11. // Return empty if there are no numbers in the value.
  12. // Prevents some CSS errors.
  13. if ( ! preg_match( '#[0-9]#' , $value ) ) {
  14. return;
  15. }
  16. // Trim the value
  17. $value = trim( $value );
  18. // The array of valid units
  19. $units = array( 'rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax' );
  20. // The raw value without the units
  21. $raw_value = filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
  22. foreach ( $units as $unit ) {
  23. // Find what unit we're using
  24. if ( false !== strpos( $value, $unit ) ) {
  25. $unit_used = $unit;
  26. }
  27. }
  28. $unit_used = ( isset( $unit_used ) ) ? $unit_used : '';
  29. return $raw_value . $unit_used;
  30. }
  31. /**
  32. * Adds a specified unit to a unitless value and keeps the value unchanged if a unit is present.
  33. * A forced unit replace can also be done.
  34. *
  35. * @param string $value A value like a margin setting etc., with or without unit
  36. * @param string $unit A unit that should be appended to unitless values
  37. * @param string $unit_handling 'add': only add $unit if $value is unitless.
  38. * 'force_replace': replace the unit of $value with $unit
  39. */
  40. public static function get_value_with_unit( $value, $unit = 'px', $unit_handling = 'add' ) {
  41. if ( $value == 'auto' ) {
  42. return $value;
  43. }
  44. // Return empty if there are no numbers in the value.
  45. // Prevents some CSS errors.
  46. if ( ! preg_match( '#[0-9]#' , $value ) ) {
  47. return;
  48. }
  49. // Trim the value
  50. $value = trim( $value );
  51. $raw_value = filter_var( $value, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION );
  52. // If the value already has an unit, return the original string
  53. if ( $value == $raw_value ) {
  54. return $raw_value . $unit;
  55. } elseif( $unit_handling == 'force_replace' ) {
  56. return $raw_value . $unit;
  57. }
  58. return $value;
  59. }
  60. /**
  61. * Sanitises a HEX value.
  62. * (part of the Kirki Toolkit)
  63. * The way this works is by splitting the string in 6 substrings.
  64. * Each sub-string is individually sanitized, and the result is then returned.
  65. *
  66. * @var string The hex value of a color
  67. * @param boolean Whether we want to include a hash (#) at the beginning or not
  68. * @return string The sanitized hex color.
  69. */
  70. public static function hex( $color, $default = false ) {
  71. if ( ! $color ) {
  72. $color = $default;
  73. }
  74. $word_colors = array(
  75. 'aliceblue'=>'F0F8FF',
  76. 'antiquewhite'=>'FAEBD7',
  77. 'aqua'=>'00FFFF',
  78. 'aquamarine'=>'7FFFD4',
  79. 'azure'=>'F0FFFF',
  80. 'beige'=>'F5F5DC',
  81. 'bisque'=>'FFE4C4',
  82. 'black'=>'000000',
  83. 'blanchedalmond '=>'FFEBCD',
  84. 'blue'=>'0000FF',
  85. 'blueviolet'=>'8A2BE2',
  86. 'brown'=>'A52A2A',
  87. 'burlywood'=>'DEB887',
  88. 'cadetblue'=>'5F9EA0',
  89. 'chartreuse'=>'7FFF00',
  90. 'chocolate'=>'D2691E',
  91. 'coral'=>'FF7F50',
  92. 'cornflowerblue'=>'6495ED',
  93. 'cornsilk'=>'FFF8DC',
  94. 'crimson'=>'DC143C',
  95. 'cyan'=>'00FFFF',
  96. 'darkblue'=>'00008B',
  97. 'darkcyan'=>'008B8B',
  98. 'darkgoldenrod'=>'B8860B',
  99. 'darkgray'=>'A9A9A9',
  100. 'darkgreen'=>'006400',
  101. 'darkgrey'=>'A9A9A9',
  102. 'darkkhaki'=>'BDB76B',
  103. 'darkmagenta'=>'8B008B',
  104. 'darkolivegreen'=>'556B2F',
  105. 'darkorange'=>'FF8C00',
  106. 'darkorchid'=>'9932CC',
  107. 'darkred'=>'8B0000',
  108. 'darksalmon'=>'E9967A',
  109. 'darkseagreen'=>'8FBC8F',
  110. 'darkslateblue'=>'483D8B',
  111. 'darkslategray'=>'2F4F4F',
  112. 'darkslategrey'=>'2F4F4F',
  113. 'darkturquoise'=>'00CED1',
  114. 'darkviolet'=>'9400D3',
  115. 'deeppink'=>'FF1493',
  116. 'deepskyblue'=>'00BFFF',
  117. 'dimgray'=>'696969',
  118. 'dimgrey'=>'696969',
  119. 'dodgerblue'=>'1E90FF',
  120. 'firebrick'=>'B22222',
  121. 'floralwhite'=>'FFFAF0',
  122. 'forestgreen'=>'228B22',
  123. 'fuchsia'=>'FF00FF',
  124. 'gainsboro'=>'DCDCDC',
  125. 'ghostwhite'=>'F8F8FF',
  126. 'gold'=>'FFD700',
  127. 'goldenrod'=>'DAA520',
  128. 'gray'=>'808080',
  129. 'green'=>'008000',
  130. 'greenyellow'=>'ADFF2F',
  131. 'grey'=>'808080',
  132. 'honeydew'=>'F0FFF0',
  133. 'hotpink'=>'FF69B4',
  134. 'indianred'=>'CD5C5C',
  135. 'indigo'=>'4B0082',
  136. 'ivory'=>'FFFFF0',
  137. 'khaki'=>'F0E68C',
  138. 'lavender'=>'E6E6FA',
  139. 'lavenderblush'=>'FFF0F5',
  140. 'lawngreen'=>'7CFC00',
  141. 'lemonchiffon'=>'FFFACD',
  142. 'lightblue'=>'ADD8E6',
  143. 'lightcoral'=>'F08080',
  144. 'lightcyan'=>'E0FFFF',
  145. 'lightgoldenrodyellow'=>'FAFAD2',
  146. 'lightgray'=>'D3D3D3',
  147. 'lightgreen'=>'90EE90',
  148. 'lightgrey'=>'D3D3D3',
  149. 'lightpink'=>'FFB6C1',
  150. 'lightsalmon'=>'FFA07A',
  151. 'lightseagreen'=>'20B2AA',
  152. 'lightskyblue'=>'87CEFA',
  153. 'lightslategray'=>'778899',
  154. 'lightslategrey'=>'778899',
  155. 'lightsteelblue'=>'B0C4DE',
  156. 'lightyellow'=>'FFFFE0',
  157. 'lime'=>'00FF00',
  158. 'limegreen'=>'32CD32',
  159. 'linen'=>'FAF0E6',
  160. 'magenta'=>'FF00FF',
  161. 'maroon'=>'800000',
  162. 'mediumaquamarine'=>'66CDAA',
  163. 'mediumblue'=>'0000CD',
  164. 'mediumorchid'=>'BA55D3',
  165. 'mediumpurple'=>'9370D0',
  166. 'mediumseagreen'=>'3CB371',
  167. 'mediumslateblue'=>'7B68EE',
  168. 'mediumspringgreen'=>'00FA9A',
  169. 'mediumturquoise'=>'48D1CC',
  170. 'mediumvioletred'=>'C71585',
  171. 'midnightblue'=>'191970',
  172. 'mintcream'=>'F5FFFA',
  173. 'mistyrose'=>'FFE4E1',
  174. 'moccasin'=>'FFE4B5',
  175. 'navajowhite'=>'FFDEAD',
  176. 'navy'=>'000080',
  177. 'oldlace'=>'FDF5E6',
  178. 'olive'=>'808000',
  179. 'olivedrab'=>'6B8E23',
  180. 'orange'=>'FFA500',
  181. 'orangered'=>'FF4500',
  182. 'orchid'=>'DA70D6',
  183. 'palegoldenrod'=>'EEE8AA',
  184. 'palegreen'=>'98FB98',
  185. 'paleturquoise'=>'AFEEEE',
  186. 'palevioletred'=>'DB7093',
  187. 'papayawhip'=>'FFEFD5',
  188. 'peachpuff'=>'FFDAB9',
  189. 'peru'=>'CD853F',
  190. 'pink'=>'FFC0CB',
  191. 'plum'=>'DDA0DD',
  192. 'powderblue'=>'B0E0E6',
  193. 'purple'=>'800080',
  194. 'red'=>'FF0000',
  195. 'rosybrown'=>'BC8F8F',
  196. 'royalblue'=>'4169E1',
  197. 'saddlebrown'=>'8B4513',
  198. 'salmon'=>'FA8072',
  199. 'sandybrown'=>'F4A460',
  200. 'seagreen'=>'2E8B57',
  201. 'seashell'=>'FFF5EE',
  202. 'sienna'=>'A0522D',
  203. 'silver'=>'C0C0C0',
  204. 'skyblue'=>'87CEEB',
  205. 'slateblue'=>'6A5ACD',
  206. 'slategray'=>'708090',
  207. 'slategrey'=>'708090',
  208. 'snow'=>'FFFAFA',
  209. 'springgreen'=>'00FF7F',
  210. 'steelblue'=>'4682B4',
  211. 'tan'=>'D2B48C',
  212. 'teal'=>'008080',
  213. 'thistle'=>'D8BFD8',
  214. 'tomato'=>'FF6347',
  215. 'turquoise'=>'40E0D0',
  216. 'violet'=>'EE82EE',
  217. 'wheat'=>'F5DEB3',
  218. 'white'=>'FFFFFF',
  219. 'whitesmoke'=>'F5F5F5',
  220. 'yellow'=>'FFFF00',
  221. 'yellowgreen'=>'9ACD32'
  222. );
  223. // Remove any spaces and special characters before and after the string
  224. $color = trim( $color );
  225. // Check if the color is a standard word-color.
  226. // If it is, then convert to hex.
  227. if ( array_key_exists( $color, $word_colors ) ) {
  228. $color = $word_colors[ $color ];
  229. }
  230. // Remove any trailing '#' symbols from the color value
  231. $color = str_replace( '#', '', $color );
  232. // If the string is 6 characters long then use it in pairs.
  233. if ( 3 == strlen( $color ) ) {
  234. $color = substr( $color, 0, 1 ) . substr( $color, 0, 1 ) . substr( $color, 1, 1 ) . substr( $color, 1, 1 ) . substr( $color, 2, 1 ) . substr( $color, 2, 1 );
  235. }
  236. $substr = array();
  237. for ( $i = 0; $i <= 5; $i++ ) {
  238. $default = ( 0 == $i ) ? 'F' : ( $substr[$i-1] );
  239. $substr[$i] = substr( $color, $i, 1 );
  240. $substr[$i] = ( false === $substr[$i] || ! ctype_xdigit( $substr[$i] ) ) ? $default : $substr[$i];
  241. }
  242. $hex = implode( '', $substr );
  243. return '#' . $hex;
  244. }
  245. /**
  246. * Sanitizes an rgba color value
  247. * (part of the Kirki Toolkit)
  248. */
  249. public static function rgba( $value ) {
  250. // If empty or an array return transparent
  251. if ( empty( $value ) || is_array( $value ) ) {
  252. return 'rgba(0,0,0,0)';
  253. }
  254. // If string does not start with 'rgba', then treat as hex
  255. // sanitize the hex color and finally convert hex to rgba
  256. if ( false === strpos( $value, 'rgba' ) ) {
  257. return self::get_rgba( self::hex( $value ) );
  258. }
  259. // By now we know the string is formatted as an rgba color so we need to further sanitize it.
  260. $value = str_replace( ' ', '', $value );
  261. sscanf( $value, 'rgba(%d,%d,%d,%f)', $red, $green, $blue, $alpha );
  262. return 'rgba(' . $red . ',' . $green . ',' . $blue . ',' . $alpha . ')';
  263. }
  264. /**
  265. * Sanitize colors.
  266. * (part of the Kirki Toolkit)
  267. * Determine if the current value is a hex or an rgba color and call the appropriate method.
  268. *
  269. * @since 0.8.5
  270. *
  271. * @param $value string hex or rgba color
  272. * @param $default string hex or rgba color
  273. * @return string
  274. */
  275. public static function color( $value, $default = null ) {
  276. if ( 'transparent' == $value ) {
  277. return 'transparent';
  278. }
  279. // If no value exists and we've set a default, use the default value instead.
  280. if ( null !== $default && ( ! $value || '' == $value || null === $value ) ) {
  281. return $default;
  282. }
  283. // Is this an rgba color or a hex?
  284. $mode = ( false === strpos( $value, 'rgba' ) ) ? 'rgba' : 'hex';
  285. if ( 'rgba' == $mode ) {
  286. return self::hex( $value );
  287. } else {
  288. return self::rgba( $value );
  289. }
  290. }
  291. /**
  292. * Gets the rgba value of the $hex color.
  293. * (part of the Kirki Toolkit)
  294. *
  295. * @var string The hex value of a color
  296. * @param int Opacity level (1-100)
  297. * @return string
  298. */
  299. public static function get_rgba( $hex = '#fff', $opacity = 100 ) {
  300. $hex = self::hex( $hex, false );
  301. // Make sure that opacity is properly formatted :
  302. // Set the opacity to 100 if a larger value has been entered by mistake.
  303. // If a negative value is used, then set to 0.
  304. // If an opacity value is entered in a decimal form (for example 0.25), then multiply by 100.
  305. if ( $opacity >= 100 ) {
  306. $opacity = 100;
  307. } elseif ( $opacity < 0 ) {
  308. $opacity = 0;
  309. } elseif ( $opacity < 1 && $opacity != 0 ) {
  310. $opacity = ( $opacity * 100 );
  311. } else {
  312. $opacity = $opacity;
  313. }
  314. // Divide the opacity by 100 to end-up with a CSS value for the opacity
  315. $opacity = ( $opacity / 100 );
  316. $color = 'rgba(' . self::get_rgb( $hex, true ) . ', ' . $opacity . ')';
  317. return $color;
  318. }
  319. /**
  320. * Gets the rgb value of the $hex color.
  321. * (part of the Kirki Toolkit)
  322. *
  323. * @var string The hex value of a color
  324. * @param boolean Whether we want to implode the values or not
  325. * @return mixed array|string
  326. */
  327. public static function get_rgb( $hex, $implode = false ) {
  328. // Remove any trailing '#' symbols from the color value
  329. $hex = self::hex( $hex, false );
  330. $red = hexdec( substr( $hex, 0, 2 ) );
  331. $green = hexdec( substr( $hex, 2, 2 ) );
  332. $blue = hexdec( substr( $hex, 4, 2 ) );
  333. // rgb is an array
  334. $rgb = array( $red, $green, $blue );
  335. return ( $implode ) ? implode( ',', $rgb ) : $rgb;
  336. }
  337. /**
  338. * Strips the alpha value from an RGBA color string.
  339. *
  340. * @param string $rgba The RGBA color string.
  341. * @return string The corresponding RGB string.
  342. */
  343. public static function rgba_to_rgb( $rgba ) {
  344. $rgba = str_replace( ' ', '', $rgba );
  345. $rgba_array = explode( ',', $rgba );
  346. $rgba_array[0] = str_replace( 'rgba(', '', $rgba_array[0] );
  347. if ( isset( $rgba_array[3] ) ) {
  348. unset( $rgba_array[3] );
  349. }
  350. $rgb = sprintf( 'rgb(%s)', implode( ',', $rgba_array ) );
  351. return $rgb;
  352. }
  353. /**
  354. * Properly escape some characters in image URLs so that they may be properly used in CSS.
  355. * From W3C:
  356. * > Some characters appearing in an unquoted URI,
  357. * > such as parentheses, white space characters, single quotes (') and double quotes ("),
  358. * > must be escaped with a backslash so that the resulting URI value is a URI token: '\(', '\)'.
  359. */
  360. public static function css_asset_url( $url ) {
  361. $url = esc_url_raw( $url );
  362. $url = str_replace( '(', '\(', $url );
  363. $url = str_replace( ')', '\)', $url );
  364. $url = str_replace( '"', '\"', $url );
  365. $url = str_replace( ' ', '\ ', $url );
  366. $url = str_replace( "'", "\'", $url );
  367. return $url;
  368. }
  369. /**
  370. * Removes the scheme of the passed URL to fit the current page
  371. *
  372. * @var string The URL that needs sanitation
  373. * @return string Full URL without scheme
  374. */
  375. public static function get_url_with_correct_scheme( $url ) {
  376. $url = str_replace( 'http://', '//', str_replace( 'https://', '//', $url ) );
  377. return $url;
  378. }
  379. }
  380. //}