| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: |
|
| 11: |
|
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: |
|
| 26: | function xoops_setcookie()
|
| 27: | {
|
| 28: | if (headers_sent()) {
|
| 29: | return false;
|
| 30: | }
|
| 31: | $argNames = array('name', 'value', 'expires', 'path', 'domain', 'secure', 'httponly');
|
| 32: |
|
| 33: |
|
| 34: | $rawArgs = func_get_args();
|
| 35: | $args = array();
|
| 36: | foreach ($rawArgs as $key => $value) {
|
| 37: | if (2 === $key && \is_array($value)) {
|
| 38: |
|
| 39: | $args['options'] = array();
|
| 40: | foreach ($value as $optionKey => $optionValue) {
|
| 41: | $args['options'][strtolower($optionKey)] = $optionValue;
|
| 42: | }
|
| 43: | break;
|
| 44: | }
|
| 45: | if ($key>1) {
|
| 46: | if (null !== $value) {
|
| 47: | $args['options'][$argNames[$key]] = $value;
|
| 48: | }
|
| 49: | } else {
|
| 50: | $args[$argNames[$key]] = $value;
|
| 51: | }
|
| 52: | }
|
| 53: |
|
| 54: |
|
| 55: | $args['options']['samesite'] = isset($args['options']['samesite']) ? $args['options']['samesite'] : 'strict';
|
| 56: | if (!isset($args['value'])){
|
| 57: | $args['value'] = '';
|
| 58: | }
|
| 59: |
|
| 60: | if (PHP_VERSION_ID >= 70300) {
|
| 61: | return setcookie($args['name'], (string)$args['value'], $args['options']);
|
| 62: | }
|
| 63: |
|
| 64: | header(xoops_buildCookieHeader($args), false);
|
| 65: | return true;
|
| 66: | }
|
| 67: |
|
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: |
|
| 73: | function xoops_buildCookieHeader($args)
|
| 74: | {
|
| 75: |
|
| 76: | $options = $args['options'];
|
| 77: |
|
| 78: | $header = 'Set-Cookie: ' . $args['name'] . '=' . rawurlencode($args['value']) . ' ';
|
| 79: |
|
| 80: | if (isset($options['expires']) && 0 !== $options['expires']) {
|
| 81: | $dateTime = new DateTime();
|
| 82: | if (time() >= $options['expires']) {
|
| 83: | $dateTime->setTimestamp(0);
|
| 84: | $header = 'Set-Cookie: ' . $args['name'] . '=deleted ; expires=' . $dateTime->format(DateTime::COOKIE) . ' ; Max-Age=0 ';
|
| 85: | } else {
|
| 86: | $dateTime->setTimestamp($options['expires']);
|
| 87: | $header .= '; expires=' . $dateTime->format(DateTime::COOKIE) . ' ';
|
| 88: | }
|
| 89: | }
|
| 90: |
|
| 91: | if (isset($options['path']) && '' !== $options['path']) {
|
| 92: | $header .= '; path=' . $options['path'] . ' ';
|
| 93: | }
|
| 94: |
|
| 95: | if (isset($options['domain']) && '' !== $options['domain']) {
|
| 96: | $header .= '; domain=' . $options['domain'] . ' ';
|
| 97: | }
|
| 98: |
|
| 99: | if (isset($options['secure']) && true === (bool) $options['secure']) {
|
| 100: | $header .= '; Secure ';
|
| 101: | }
|
| 102: |
|
| 103: | if (isset($options['httponly']) && true === (bool) $options['httponly']) {
|
| 104: | $header .= '; HttpOnly ';
|
| 105: | }
|
| 106: |
|
| 107: | if (isset($options['samesite']) && '' !== $options['samesite']) {
|
| 108: | $header .= '; samesite=' . $options['samesite'] . ' ';
|
| 109: | }
|
| 110: |
|
| 111: | return $header;
|
| 112: | }
|
| 113: | |