| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           namespace core\session\utility;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           /**
  | 
        
        
            | 
            | 
           20 | 
            * Helper class providing utils dealing with cookies, particularly 3rd party cookies.
  | 
        
        
            | 
            | 
           21 | 
            *
  | 
        
        
            | 
            | 
           22 | 
            * This class primarily provides a means to augment outbound cookie headers, in order to satisfy browser-specific
  | 
        
        
            | 
            | 
           23 | 
            * requirements for setting 3rd party cookies.
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * @package    core
  | 
        
        
            | 
            | 
           26 | 
            * @copyright  2024 Jake Dallimore <jrhdallimore@gmail.com>
  | 
        
        
            | 
            | 
           27 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           28 | 
            */
  | 
        
        
            | 
            | 
           29 | 
           final class cookie_helper {
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
               /**
  | 
        
        
            | 
            | 
           32 | 
                * Make sure the given attributes are set on the Set-Cookie response header identified by name=$cookiename.
  | 
        
        
            | 
            | 
           33 | 
                *
  | 
        
        
            | 
            | 
           34 | 
                * This function only affects Set-Cookie headers and modifies the headers directly with the required changes, if any.
  | 
        
        
            | 
            | 
           35 | 
                *
  | 
        
        
            | 
            | 
           36 | 
                * @param string $cookiename the cookie name.
  | 
        
        
            | 
            | 
           37 | 
                * @param array $attributes the attributes to set/ensure are set.
  | 
        
        
            | 
            | 
           38 | 
                * @return void
  | 
        
        
            | 
            | 
           39 | 
                */
  | 
        
        
            | 
            | 
           40 | 
               public static function add_attributes_to_cookie_response_header(string $cookiename, array $attributes): void {
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
                   $setcookieheaders = array_filter(headers_list(), function($val) {
  | 
        
        
            | 
            | 
           43 | 
                       return preg_match("/Set-Cookie:/i", $val);
  | 
        
        
            | 
            | 
           44 | 
                   });
  | 
        
        
            | 
            | 
           45 | 
                   if (empty($setcookieheaders)) {
  | 
        
        
            | 
            | 
           46 | 
                       return;
  | 
        
        
            | 
            | 
           47 | 
                   }
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
                   $updatedheaders = self::cookie_response_headers_add_attributes($setcookieheaders, [$cookiename], $attributes);
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
                   // Note: The header_remove() method is quite crude and removes all headers of that header name.
  | 
        
        
            | 
            | 
           52 | 
                   header_remove('Set-Cookie');
  | 
        
        
            | 
            | 
           53 | 
                   foreach ($updatedheaders as $header) {
  | 
        
        
            | 
            | 
           54 | 
                       header($header, false);
  | 
        
        
            | 
            | 
           55 | 
                   }
  | 
        
        
            | 
            | 
           56 | 
               }
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
               /**
  | 
        
        
            | 
            | 
           59 | 
                * Given a list of HTTP header strings, return a list of HTTP header strings where the matched 'Set-Cookie' headers
  | 
        
        
            | 
            | 
           60 | 
                * have been updated with the attributes defined in $attribute - an array of strings.
  | 
        
        
            | 
            | 
           61 | 
                *
  | 
        
        
            | 
            | 
           62 | 
                * This method does not verify whether a given attribute is valid or not. It blindly sets it and returns the header
  | 
        
        
            | 
            | 
           63 | 
                * strings. It's up to calling code to determine whether an attribute makes sense or not.
  | 
        
        
            | 
            | 
           64 | 
                *
  | 
        
        
            | 
            | 
           65 | 
                * @param array $headerstrings the array of header strings.
  | 
        
        
            | 
            | 
           66 | 
                * @param array $cookiestomatch the array of cookie names to match.
  | 
        
        
            | 
            | 
           67 | 
                * @param array $attributes the attributes to set on each matched 'Set-Cookie' header.
  | 
        
        
            | 
            | 
           68 | 
                * @param bool $casesensitive whether to match the attribute in a case-sensitive way.
  | 
        
        
            | 
            | 
           69 | 
                * @return array the updated array of header strings.
  | 
        
        
            | 
            | 
           70 | 
                */
  | 
        
        
            | 
            | 
           71 | 
               public static function cookie_response_headers_add_attributes(array $headerstrings, array $cookiestomatch, array $attributes,
  | 
        
        
            | 
            | 
           72 | 
                       bool $casesensitive = false): array {
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   return array_map(function($headerstring) use ($attributes, $casesensitive, $cookiestomatch) {
  | 
        
        
            | 
            | 
           75 | 
                       if (!self::cookie_response_header_matches_names($headerstring, $cookiestomatch)) {
  | 
        
        
            | 
            | 
           76 | 
                           return $headerstring;
  | 
        
        
            | 
            | 
           77 | 
                       }
  | 
        
        
            | 
            | 
           78 | 
                       foreach ($attributes as $attribute) {
  | 
        
        
            | 
            | 
           79 | 
                           if (!self::cookie_response_header_contains_attribute($headerstring, $attribute, $casesensitive)) {
  | 
        
        
            | 
            | 
           80 | 
                               $headerstring = self::cookie_response_header_append_attribute($headerstring, $attribute);
  | 
        
        
            | 
            | 
           81 | 
                           }
  | 
        
        
            | 
            | 
           82 | 
                       }
  | 
        
        
            | 
            | 
           83 | 
                       return $headerstring;
  | 
        
        
            | 
            | 
           84 | 
                   }, $headerstrings);
  | 
        
        
            | 
            | 
           85 | 
               }
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
               /**
  | 
        
        
            | 
            | 
           88 | 
                * Forces the expiry of the MoodleSession cookie.
  | 
        
        
            | 
            | 
           89 | 
                *
  | 
        
        
            | 
            | 
           90 | 
                * This is useful to force a new Set-Cookie header on the next redirect.
  | 
        
        
            | 
            | 
           91 | 
                *
  | 
        
        
            | 
            | 
           92 | 
                * @return void
  | 
        
        
            | 
            | 
           93 | 
                */
  | 
        
        
            | 
            | 
           94 | 
               public static function expire_moodlesession(): void {
  | 
        
        
            | 
            | 
           95 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                   $setcookieheader = array_filter(headers_list(), function($val) use ($CFG) {
  | 
        
        
            | 
            | 
           98 | 
                       return self::cookie_response_header_matches_name($val, 'MoodleSession'.$CFG->sessioncookie);
  | 
        
        
            | 
            | 
           99 | 
                   });
  | 
        
        
            | 
            | 
           100 | 
                   if (!empty($setcookieheader)) {
  | 
        
        
            | 
            | 
           101 | 
                       $expirestr = 'Expires='.gmdate(DATE_RFC7231, time() - 60);
  | 
        
        
            | 
            | 
           102 | 
                       self::add_attributes_to_cookie_response_header('MoodleSession'.$CFG->sessioncookie, [$expirestr]);
  | 
        
        
            | 
            | 
           103 | 
                   } else {
  | 
        
        
            | 
            | 
           104 | 
                       setcookie('MoodleSession'.$CFG->sessioncookie, '', time() - 60);
  | 
        
        
            | 
            | 
           105 | 
                   }
  | 
        
        
            | 
            | 
           106 | 
               }
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
               /**
  | 
        
        
            | 
            | 
           109 | 
                * Check whether the header string is a 'Set-Cookie' header for the cookie identified by $cookiename.
  | 
        
        
            | 
            | 
           110 | 
                *
  | 
        
        
            | 
            | 
           111 | 
                * @param string $headerstring the header string to check.
  | 
        
        
            | 
            | 
           112 | 
                * @param string $cookiename the name of the cookie to match.
  | 
        
        
            | 
            | 
           113 | 
                * @return bool true if the header string is a Set-Cookie header for the named cookie, false otherwise.
  | 
        
        
            | 
            | 
           114 | 
                */
  | 
        
        
            | 
            | 
           115 | 
               private static function cookie_response_header_matches_name(string $headerstring, string $cookiename): bool {
  | 
        
        
            | 
            | 
           116 | 
                   // Generally match the format, but in a case-insensitive way so that 'set-cookie' and "SET-COOKIE" are both valid.
  | 
        
        
            | 
            | 
           117 | 
                   return preg_match("/Set-Cookie: *$cookiename=/i", $headerstring)
  | 
        
        
            | 
            | 
           118 | 
                       // Case-sensitive match on cookiename, which is case-sensitive.
  | 
        
        
            | 
            | 
           119 | 
                       && preg_match("/: *$cookiename=/", $headerstring);
  | 
        
        
            | 
            | 
           120 | 
               }
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
               /**
  | 
        
        
            | 
            | 
           123 | 
                * Check whether the header string is a 'Set-Cookie' header for the cookies identified in the $cookienames array.
  | 
        
        
            | 
            | 
           124 | 
                *
  | 
        
        
            | 
            | 
           125 | 
                * @param string $headerstring the header string to check.
  | 
        
        
            | 
            | 
           126 | 
                * @param array $cookienames the array of cookie names to match.
  | 
        
        
            | 
            | 
           127 | 
                * @return bool true if the header string is a Set-Cookie header for one of the named cookies, false otherwise.
  | 
        
        
            | 
            | 
           128 | 
                */
  | 
        
        
            | 
            | 
           129 | 
               private static function cookie_response_header_matches_names(string $headerstring, array $cookienames): bool {
  | 
        
        
            | 
            | 
           130 | 
                   foreach ($cookienames as $cookiename) {
  | 
        
        
            | 
            | 
           131 | 
                       if (self::cookie_response_header_matches_name($headerstring, $cookiename)) {
  | 
        
        
            | 
            | 
           132 | 
                           return true;
  | 
        
        
            | 
            | 
           133 | 
                       }
  | 
        
        
            | 
            | 
           134 | 
                   }
  | 
        
        
            | 
            | 
           135 | 
                   return false;
  | 
        
        
            | 
            | 
           136 | 
               }
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
               /**
  | 
        
        
            | 
            | 
           139 | 
                * Check whether the header string contains the given attribute.
  | 
        
        
            | 
            | 
           140 | 
                *
  | 
        
        
            | 
            | 
           141 | 
                * @param string $headerstring the header string to check.
  | 
        
        
            | 
            | 
           142 | 
                * @param string $attribute the attribute to check for.
  | 
        
        
            | 
            | 
           143 | 
                * @param bool $casesensitive whether to perform a case-sensitive check.
  | 
        
        
            | 
            | 
           144 | 
                * @return bool true if the header contains the attribute, false otherwise.
  | 
        
        
            | 
            | 
           145 | 
                */
  | 
        
        
            | 
            | 
           146 | 
               private static function cookie_response_header_contains_attribute(string $headerstring, string $attribute,
  | 
        
        
            | 
            | 
           147 | 
                       bool $casesensitive): bool {
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
                   if ($casesensitive) {
  | 
        
        
            | 
            | 
           150 | 
                       return str_contains($headerstring, $attribute);
  | 
        
        
            | 
            | 
           151 | 
                   }
  | 
        
        
            | 
            | 
           152 | 
                   return str_contains(strtolower($headerstring), strtolower($attribute));
  | 
        
        
            | 
            | 
           153 | 
               }
  | 
        
        
            | 
            | 
           154 | 
              | 
        
        
            | 
            | 
           155 | 
               /**
  | 
        
        
            | 
            | 
           156 | 
                * Append the given attribute to the header string.
  | 
        
        
            | 
            | 
           157 | 
                *
  | 
        
        
            | 
            | 
           158 | 
                * @param string $headerstring the header string to append to.
  | 
        
        
            | 
            | 
           159 | 
                * @param string $attribute the attribute to append.
  | 
        
        
            | 
            | 
           160 | 
                * @return string the updated header string.
  | 
        
        
            | 
            | 
           161 | 
                */
  | 
        
        
            | 
            | 
           162 | 
               private static function cookie_response_header_append_attribute(string $headerstring, string $attribute): string {
  | 
        
        
            | 
            | 
           163 | 
                   $headerstring = rtrim($headerstring, ';'); // Sometimes included.
  | 
        
        
            | 
            | 
           164 | 
                   return "$headerstring; $attribute;";
  | 
        
        
            | 
            | 
           165 | 
               }
  | 
        
        
            | 
            | 
           166 | 
           }
  |