Няма описание

functions.cookies.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * This file is meant to be the home for any function handling cookies that can
  4. * be accessed anywhere within Jetpack.
  5. *
  6. * This file is loaded whether or not Jetpack is connected to WP.com.
  7. *
  8. * @package automattic/jetpack
  9. */
  10. /**
  11. * A PHP 5.X compatible version of the array argument version of PHP 7.3's setcookie().
  12. *
  13. * Useful for setting SameSite cookies in PHP 7.2 or earlier.
  14. *
  15. * @param string $name Name of the cookie.
  16. * @param string $value Value of the cookie.
  17. * @param array $options Options to include with the cookie.
  18. * @return bool False when error happens, other wise true.
  19. */
  20. function jetpack_shim_setcookie( $name, $value, $options ) {
  21. $not_allowed_chars = ",; \t\r\n\013\014";
  22. if ( false !== strpbrk( $name, $not_allowed_chars ) ) {
  23. return false;
  24. }
  25. if ( headers_sent() ) {
  26. return false;
  27. }
  28. $cookie = 'Set-Cookie: ' . $name . '=' . rawurlencode( $value ) . '; ';
  29. if ( ! empty( $options['expires'] ) ) {
  30. $cookie_date = gmdate( 'D, d M Y H:i:s \G\M\T', $options['expires'] );
  31. $cookie .= sprintf( 'expires=%s', $cookie_date ) . ';';
  32. }
  33. if ( ! empty( $options['secure'] ) && true === $options['secure'] ) {
  34. $cookie .= 'secure; ';
  35. }
  36. if ( ! empty( $options['httponly'] ) && true === $options['httponly'] ) {
  37. $cookie .= 'HttpOnly; ';
  38. }
  39. if ( ! empty( $options['domain'] ) && is_string( $options['domain'] ) ) {
  40. if ( false !== strpbrk( $options['domain'], $not_allowed_chars ) ) {
  41. return false;
  42. }
  43. $cookie .= sprintf( 'domain=%s', $options['domain'] . '; ' );
  44. }
  45. if ( ! empty( $options['path'] ) && is_string( $options['path'] ) ) {
  46. if ( false !== strpbrk( $options['path'], $not_allowed_chars ) ) {
  47. return false;
  48. }
  49. $cookie .= sprintf( 'path=%s', $options['path'] . '; ' );
  50. }
  51. if ( ! empty( $options['samesite'] ) && is_string( $options['samesite'] ) ) {
  52. $cookie .= sprintf( 'SameSite=%s', $options['samesite'] . '; ' );
  53. }
  54. $cookie = trim( $cookie );
  55. $cookie = trim( $cookie, ';' );
  56. header( $cookie, false );
  57. return true;
  58. }