@@ -1664,21 +1664,31 @@ PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t
1664
1664
* Userspace exported functions *
1665
1665
******************************** */
1666
1666
1667
- /* {{{ proto void session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly[, string samesite]]]]])
1667
+ /* {{{ proto bool session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly]]]])
1668
+ session_set_cookie_params(array options)
1668
1669
Set session cookie parameters */
1669
1670
static PHP_FUNCTION (session_set_cookie_params )
1670
1671
{
1671
- zval * lifetime ;
1672
- zend_string * path = NULL , * domain = NULL , * samesite = NULL ;
1673
- int argc = ZEND_NUM_ARGS () ;
1674
- zend_bool secure = 0 , httponly = 0 ;
1672
+ zval * lifetime_or_options = NULL ;
1673
+ zend_string * lifetime = NULL , * path = NULL , * domain = NULL , * samesite = NULL ;
1674
+ zend_bool secure = 0 , secure_null = 1 ;
1675
+ zend_bool httponly = 0 , httponly_null = 1 ;
1675
1676
zend_string * ini_name ;
1677
+ int result ;
1678
+ int found = 0 ;
1676
1679
1677
- if (!PS (use_cookies ) ||
1678
- zend_parse_parameters (argc , "z|SSbbS" , & lifetime , & path , & domain , & secure , & httponly , & samesite ) == FAILURE ) {
1680
+ if (!PS (use_cookies )) {
1679
1681
return ;
1680
1682
}
1681
1683
1684
+ ZEND_PARSE_PARAMETERS_START (1 , 5 )
1685
+ Z_PARAM_ZVAL (lifetime_or_options )
1686
+ Z_PARAM_OPTIONAL
1687
+ Z_PARAM_STR (path )
1688
+ Z_PARAM_STR (domain )
1689
+ Z_PARAM_BOOL_EX (secure , secure_null , 1 , 0 )
1690
+ Z_PARAM_BOOL_EX (httponly , httponly_null , 1 , 0 )
1691
+ ZEND_PARSE_PARAMETERS_END ();
1682
1692
1683
1693
if (PS (session_status ) == php_session_active ) {
1684
1694
php_error_docref (NULL , E_WARNING , "Cannot change session cookie parameters when session is active" );
@@ -1690,55 +1700,108 @@ static PHP_FUNCTION(session_set_cookie_params)
1690
1700
RETURN_FALSE ;
1691
1701
}
1692
1702
1693
- convert_to_string_ex (lifetime );
1703
+ if (Z_TYPE_P (lifetime_or_options ) == IS_ARRAY ) {
1704
+ zend_string * key ;
1705
+ zval * value ;
1706
+
1707
+ ZEND_HASH_FOREACH_STR_KEY_VAL (Z_ARRVAL_P (lifetime_or_options ), key , value ) {
1708
+ if (key ) {
1709
+ ZVAL_DEREF (value );
1710
+ if (!strcasecmp ("lifetime" , ZSTR_VAL (key ))) {
1711
+ lifetime = zval_get_string (value );
1712
+ found ++ ;
1713
+ } else if (!strcasecmp ("path" , ZSTR_VAL (key ))) {
1714
+ path = zval_get_string (value );
1715
+ found ++ ;
1716
+ } else if (!strcasecmp ("domain" , ZSTR_VAL (key ))) {
1717
+ domain = zval_get_string (value );
1718
+ found ++ ;
1719
+ } else if (!strcasecmp ("secure" , ZSTR_VAL (key ))) {
1720
+ secure = zval_is_true (value );
1721
+ secure_null = 0 ;
1722
+ found ++ ;
1723
+ } else if (!strcasecmp ("httponly" , ZSTR_VAL (key ))) {
1724
+ httponly = zval_is_true (value );
1725
+ httponly_null = 0 ;
1726
+ found ++ ;
1727
+ } else if (!strcasecmp ("samesite" , ZSTR_VAL (key ))) {
1728
+ samesite = zval_get_string (value );
1729
+ found ++ ;
1730
+ } else {
1731
+ php_error_docref (NULL , E_WARNING , "Unrecognized key '%s' found in the options array" , ZSTR_VAL (key ));
1732
+ }
1733
+ } else {
1734
+ php_error_docref (NULL , E_WARNING , "Numeric key found in the options array" );
1735
+ }
1736
+ } ZEND_HASH_FOREACH_END ();
1694
1737
1695
- ini_name = zend_string_init ("session.cookie_lifetime" , sizeof ("session.cookie_lifetime" ) - 1 , 0 );
1696
- if (zend_alter_ini_entry (ini_name , Z_STR_P (lifetime ), PHP_INI_USER , PHP_INI_STAGE_RUNTIME ) == FAILURE ) {
1697
- zend_string_release_ex (ini_name , 0 );
1698
- RETURN_FALSE ;
1738
+ if (found == 0 ) {
1739
+ php_error_docref (NULL , E_WARNING , "No valid keys were found in the options array" );
1740
+ RETURN_FALSE ;
1741
+ }
1742
+ } else {
1743
+ lifetime = zval_get_string (lifetime_or_options );
1699
1744
}
1700
- zend_string_release_ex (ini_name , 0 );
1701
1745
1746
+ if (lifetime ) {
1747
+ ini_name = zend_string_init ("session.cookie_lifetime" , sizeof ("session.cookie_lifetime" ) - 1 , 0 );
1748
+ result = zend_alter_ini_entry (ini_name , lifetime , PHP_INI_USER , PHP_INI_STAGE_RUNTIME );
1749
+ zend_string_release (lifetime );
1750
+ zend_string_release_ex (ini_name , 0 );
1751
+ if (result == FAILURE ) {
1752
+ RETURN_FALSE ;
1753
+ }
1754
+ }
1702
1755
if (path ) {
1703
1756
ini_name = zend_string_init ("session.cookie_path" , sizeof ("session.cookie_path" ) - 1 , 0 );
1704
- if ( zend_alter_ini_entry (ini_name , path , PHP_INI_USER , PHP_INI_STAGE_RUNTIME ) == FAILURE ) {
1705
- zend_string_release_ex ( ini_name , 0 );
1706
- RETURN_FALSE ;
1757
+ result = zend_alter_ini_entry (ini_name , path , PHP_INI_USER , PHP_INI_STAGE_RUNTIME );
1758
+ if ( found > 0 ) {
1759
+ zend_string_release ( path ) ;
1707
1760
}
1708
1761
zend_string_release_ex (ini_name , 0 );
1762
+ if (result == FAILURE ) {
1763
+ RETURN_FALSE ;
1764
+ }
1709
1765
}
1710
1766
if (domain ) {
1711
1767
ini_name = zend_string_init ("session.cookie_domain" , sizeof ("session.cookie_domain" ) - 1 , 0 );
1712
- if ( zend_alter_ini_entry (ini_name , domain , PHP_INI_USER , PHP_INI_STAGE_RUNTIME ) == FAILURE ) {
1713
- zend_string_release_ex ( ini_name , 0 );
1714
- RETURN_FALSE ;
1768
+ result = zend_alter_ini_entry (ini_name , domain , PHP_INI_USER , PHP_INI_STAGE_RUNTIME );
1769
+ if ( found > 0 ) {
1770
+ zend_string_release ( domain ) ;
1715
1771
}
1716
1772
zend_string_release_ex (ini_name , 0 );
1773
+ if (result == FAILURE ) {
1774
+ RETURN_FALSE ;
1775
+ }
1717
1776
}
1718
-
1719
- if (argc > 3 ) {
1777
+ if (!secure_null ) {
1720
1778
ini_name = zend_string_init ("session.cookie_secure" , sizeof ("session.cookie_secure" ) - 1 , 0 );
1721
- if (zend_alter_ini_entry_chars (ini_name , secure ? "1" : "0" , 1 , PHP_INI_USER , PHP_INI_STAGE_RUNTIME ) == FAILURE ) {
1722
- zend_string_release_ex (ini_name , 0 );
1779
+ result = zend_alter_ini_entry_chars (ini_name , secure ? "1" : "0" , 1 , PHP_INI_USER , PHP_INI_STAGE_RUNTIME );
1780
+ zend_string_release_ex (ini_name , 0 );
1781
+ if (result == FAILURE ) {
1723
1782
RETURN_FALSE ;
1724
1783
}
1725
- zend_string_release_ex (ini_name , 0 );
1726
1784
}
1727
- if (argc > 4 ) {
1785
+ if (! httponly_null ) {
1728
1786
ini_name = zend_string_init ("session.cookie_httponly" , sizeof ("session.cookie_httponly" ) - 1 , 0 );
1729
- if (zend_alter_ini_entry_chars (ini_name , httponly ? "1" : "0" , 1 , PHP_INI_USER , PHP_INI_STAGE_RUNTIME ) == FAILURE ) {
1730
- zend_string_release_ex (ini_name , 0 );
1787
+ result = zend_alter_ini_entry_chars (ini_name , httponly ? "1" : "0" , 1 , PHP_INI_USER , PHP_INI_STAGE_RUNTIME );
1788
+ zend_string_release_ex (ini_name , 0 );
1789
+ if (result == FAILURE ) {
1731
1790
RETURN_FALSE ;
1732
1791
}
1792
+ }
1793
+ if (samesite ) {
1794
+ ini_name = zend_string_init ("session.cookie_samesite" , sizeof ("session.cookie_samesite" ) - 1 , 0 );
1795
+ result = zend_alter_ini_entry (ini_name , samesite , PHP_INI_USER , PHP_INI_STAGE_RUNTIME );
1796
+ if (found > 0 ) {
1797
+ zend_string_release (samesite );
1798
+ }
1733
1799
zend_string_release_ex (ini_name , 0 );
1800
+ if (result == FAILURE ) {
1801
+ RETURN_FALSE ;
1802
+ }
1734
1803
}
1735
1804
1736
- if (argc > 5 ) {
1737
- ini_name = zend_string_init ("session.cookie_samesite" , sizeof ("session.cookie_samesite" ) - 1 , 0 );
1738
- zend_alter_ini_entry (ini_name , samesite , PHP_INI_USER , PHP_INI_STAGE_RUNTIME );
1739
- zend_string_release (ini_name );
1740
- }
1741
-
1742
1805
RETURN_TRUE ;
1743
1806
}
1744
1807
/* }}} */
@@ -2638,12 +2701,11 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_expire, 0, 0, 0)
2638
2701
ZEND_END_ARG_INFO ()
2639
2702
2640
2703
ZEND_BEGIN_ARG_INFO_EX (arginfo_session_set_cookie_params , 0 , 0 , 1 )
2641
- ZEND_ARG_INFO (0 , lifetime )
2704
+ ZEND_ARG_INFO (0 , lifetime_or_options )
2642
2705
ZEND_ARG_INFO (0 , path )
2643
2706
ZEND_ARG_INFO (0 , domain )
2644
2707
ZEND_ARG_INFO (0 , secure )
2645
2708
ZEND_ARG_INFO (0 , httponly )
2646
- ZEND_ARG_INFO (0 , samesite )
2647
2709
ZEND_END_ARG_INFO ()
2648
2710
2649
2711
ZEND_BEGIN_ARG_INFO (arginfo_session_class_open , 0 )
0 commit comments