Ticket #6: cookie_helper.patch

File cookie_helper.patch, 4.2 kB (added by phoop, 20 months ago)

A SVN patch with the package name corrected

  • cookie_helper.php

     
    3636 * @access      public 
    3737 * @param       mixed 
    3838 * @param       string  the value of the cookie 
    39  * @param       string  the number of seconds until expiration 
     39 * @param       int             the number of seconds until expiration 
    4040 * @param       string  the cookie domain.  Usually:  .yourdomain.com 
    4141 * @param       string  the cookie path 
    4242 * @param       string  the cookie prefix 
     43 * @param       bool    whether the cookie should be sent ONLY over SSL 
    4344 * @return      void 
    4445 */ 
    45 function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '') 
     46function set_cookie($name, $value = '', $expire = 0, $domain = null, $path = null, $prefix = null, $secure = false) 
    4647{ 
    4748        if (is_array($name)) 
    48         {                
    49                 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'name') as $item) 
     49        { 
     50                foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item) 
    5051                { 
    5152                        if (isset($name[$item])) 
    5253                        { 
     
    5455                        } 
    5556                } 
    5657        } 
    57          
     58 
    5859        // Set the config file options 
    5960        $CI =& get_instance(); 
    60          
    61         if ($prefix == '' AND $CI->config->item('cookie_prefix') != '') 
     61        if ( ! is_numeric($expire)) 
    6262        { 
    63                 $CI->config->item('cookie_prefix'); 
     63                $default_cookie_lifetime = $CI->config->item('cookie_lifetime'); 
     64                $expire = 
     65                        (! is_numeric($default_cookie_lifetime) || $default_cookie_lifetime<=0) ? 
     66                                0 : 
     67                                (time() + $default_cookie_lifetime); 
    6468        } 
    65         if ($domain == '' AND $CI->config->item('cookie_domain') != '') 
     69        else 
    6670        { 
    67                 $CI->config->item('cookie_domain'); 
     71                $expire = 
     72                        ($expire==0) ? 
     73                                $expire : 
     74                                (time() + $expire); 
    6875        } 
    69         if ($prefix == '/' AND $CI->config->item('cookie_path') != '/') 
     76        $default_domain = $CI->config->item('cookie_domain'); 
     77        if (is_null($domain) AND !empty($default_domain)) 
    7078        { 
    71                 $CI->config->item('cookie_path'); 
     79                $domain = $default_domain; 
    7280        } 
    73                  
    74         if ( ! is_numeric($expire)) 
     81        $default_path = $CI->config->item('cookie_path'); 
     82        if (is_null($path) AND ! empty($default_path)) 
    7583        { 
    76                 $expire = time() - 86500; 
     84                $path = $default_path; 
    7785        } 
    78         else 
     86        $default_prefix = $CI->config->item('cookie_prefix'); 
     87        if (is_null($prefix) AND ! empty($default_prefix)) 
    7988        { 
    80                 if ($expire > 0) 
    81                 { 
    82                         $expire = time() + $expire; 
    83                 } 
    84                 else 
    85                 { 
    86                         $expire = 0; 
    87                 } 
     89                $prefix = $default_prefix; 
    8890        } 
    89          
    90         setcookie($prefix.$name, $value, $expire, $path, $domain, 0); 
     91        $secure = is_bool($secure) ? $secure : false; 
     92 
     93        setcookie($prefix.$name, $value, $expire, $path, $domain, $secure); 
    9194} 
    92          
     95 
    9396// -------------------------------------------------------------------- 
    9497 
    9598/** 
     
    97100 * 
    98101 * @access      public 
    99102 * @param       string 
     103 * @param       string 
    100104 * @param       bool 
    101105 * @return      mixed 
    102106 */ 
    103 function get_cookie($index = '', $xss_clean = FALSE) 
     107function get_cookie($name = '', $prefix=null, $xss_clean = FALSE) 
    104108{ 
    105109        $CI =& get_instance(); 
    106         return $CI->input->cookie($index, $xss_clean); 
     110        $default_prefix = $CI->config->item('cookie_prefix'); 
     111        if (is_null($prefix) AND ! empty($default_prefix)) 
     112        { 
     113                $prefix = $default_prefix; 
     114        } 
     115        return $CI->input->cookie($prefix.$name, $xss_clean); 
    107116} 
    108117 
    109118// -------------------------------------------------------------------- 
     
    111120/** 
    112121 * Delete a COOKIE 
    113122 * 
     123 * @access      public 
    114124 * @param       mixed 
    115125 * @param       string  the cookie domain.  Usually:  .yourdomain.com 
    116126 * @param       string  the cookie path 
    117127 * @param       string  the cookie prefix 
     128 * @param       bool 
    118129 * @return      void 
    119130 */ 
    120 function delete_cookie($name = '', $domain = '', $path = '/', $prefix = '') 
     131function delete_cookie($name = '', $domain = null, $path = null, $prefix = null, $remove_live = TRUE) 
    121132{ 
    122         set_cookie($name, '', '', $domain, $path, $prefix); 
     133        if($remove_live===TRUE) 
     134        { 
     135                $CI =& get_instance(); 
     136                $default_prefix = $CI->config->item('cookie_prefix'); 
     137                if (is_null($prefix) AND ! empty($default_prefix)) 
     138                { 
     139                        $prefix = $default_prefix; 
     140                } 
     141                $cIndex = is_array($name) ? $name['name'] : $name; 
     142                if(isset($_COOKIE[$prefix.$cIndex])) 
     143                { 
     144                        unset($_COOKIE[$prefix.$cIndex]); 
     145                } 
     146        } 
     147        set_cookie($name, '', -86500, $domain, $path, $prefix); 
    123148} 
    124149 
    125150