Skip to content

Commit fe5c8f2

Browse files
flaupretresgolemon
authored andcommitted
Allow loading PHP and Zend extensions by name
Allow extension name as INI 'extension=' and dl() argument No BC break, as file name is still accepted. When using the '-z' command line option (CLI/CGI), an absolute file name must still be provided (nothing changed here) Change comments in example INI files
1 parent 0f15a03 commit fe5c8f2

File tree

5 files changed

+162
-105
lines changed

5 files changed

+162
-105
lines changed

NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ PHP NEWS
66
. Fixed bug #74780 (parse_url() borken when query string contains colon).
77
(jhdxr)
88
. Fixed bug #74761 (Unary operator expected error on some systems). (petk)
9+
. Allow loading PHP/Zend extensions by name in ini files (extension=<name>).
10+
(francois at tekwire dot net)
911

1012
- SPL:
1113
. Fixed bug #73471 (PHP freezes with AppendIterator). (jhdxr)
1214

15+
- Standard:
16+
. Add support for extension name as argument to dl().
17+
(francois at tekwire dot net)
18+
1319
22 Jun 2017, PHP 7.2.0alpha2
1420

1521
- Core:

ext/standard/dl.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ PHPAPI PHP_FUNCTION(dl)
8181
PHPAPI int php_load_extension(char *filename, int type, int start_now)
8282
{
8383
void *handle;
84-
char *libpath;
84+
char *libpath, *orig_libpath;
8585
zend_module_entry *module_entry;
8686
zend_module_entry *(*get_module)(void);
87-
int error_type;
87+
int error_type, slash_suffix;
8888
char *extension_dir;
8989

9090
if (type == MODULE_PERSISTENT) {
@@ -109,12 +109,37 @@ PHPAPI int php_load_extension(char *filename, int type, int start_now)
109109
libpath = estrdup(filename);
110110
} else if (extension_dir && extension_dir[0]) {
111111
int extension_dir_len = (int)strlen(extension_dir);
112-
113-
if (IS_SLASH(extension_dir[extension_dir_len-1])) {
112+
slash_suffix = IS_SLASH(extension_dir[extension_dir_len-1]);
113+
/* Try as filename first */
114+
if (slash_suffix) {
114115
spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
115116
} else {
116117
spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
117118
}
119+
if (VCWD_ACCESS(libpath, F_OK)) {
120+
/* If file does not exist, consider as extension name and build file name */
121+
orig_libpath = libpath;
122+
#if PHP_WIN32
123+
if (slash_suffix) {
124+
spprintf(&libpath, 0, "%sphp_%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
125+
} else {
126+
spprintf(&libpath, 0, "%s%cphp_%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
127+
}
128+
#else
129+
if (slash_suffix) {
130+
spprintf(&libpath, 0, "%s%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
131+
} else {
132+
spprintf(&libpath, 0, "%s%c%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
133+
}
134+
#endif
135+
if (VCWD_ACCESS(libpath, F_OK)) {
136+
php_error_docref(NULL TSRMLS_CC, error_type, "Cannot access dynamic library '%s' (tried : %s, %s)", filename, orig_libpath, libpath);
137+
efree(orig_libpath);
138+
efree(libpath);
139+
return FAILURE;
140+
}
141+
efree(orig_libpath);
142+
}
118143
} else {
119144
return FAILURE; /* Not full path given or extension_dir is not set */
120145
}

main/php_ini.c

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,43 @@ static void php_load_zend_extension_cb(void *arg)
362362
if (IS_ABSOLUTE_PATH(filename, length)) {
363363
zend_load_extension(filename);
364364
} else {
365-
char *libpath;
365+
char *libpath, *orig_libpath;
366366
char *extension_dir = INI_STR("extension_dir");
367367
int extension_dir_len = (int)strlen(extension_dir);
368-
369-
if (IS_SLASH(extension_dir[extension_dir_len-1])) {
370-
spprintf(&libpath, 0, "%s%s", extension_dir, filename);
368+
int slash_suffix = IS_SLASH(extension_dir[extension_dir_len-1]);
369+
/* Try as filename first */
370+
if (slash_suffix) {
371+
spprintf(&libpath, 0, "%s%s", extension_dir, filename); /* SAFE */
371372
} else {
372-
spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename);
373+
spprintf(&libpath, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, filename); /* SAFE */
374+
}
375+
if (VCWD_ACCESS(libpath, F_OK)) {
376+
/* If file does not exist, consider as extension name and build file name */
377+
orig_libpath = libpath;
378+
#if PHP_WIN32
379+
if (slash_suffix) {
380+
spprintf(&libpath, 0, "%sphp_%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
381+
} else {
382+
spprintf(&libpath, 0, "%s%cphp_%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
383+
}
384+
#else
385+
if (slash_suffix) {
386+
spprintf(&libpath, 0, "%s%s." PHP_SHLIB_SUFFIX, extension_dir, filename); /* SAFE */
387+
} else {
388+
spprintf(&libpath, 0, "%s%c%s." PHP_SHLIB_SUFFIX, extension_dir, DEFAULT_SLASH, filename); /* SAFE */
389+
}
390+
#endif
391+
if (VCWD_ACCESS(libpath, F_OK)) {
392+
fprintf(stderr, "Cannot access Zend extension %s (Tried: %s, %s)\n", filename, orig_libpath, libpath);
393+
/* See http://support.microsoft.com/kb/190351 */
394+
fflush(stderr);
395+
efree(orig_libpath);
396+
efree(libpath);
397+
return;
398+
}
399+
efree(orig_libpath);
373400
}
401+
374402
zend_load_extension(libpath);
375403
efree(libpath);
376404
}

php.ini-development

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -860,64 +860,63 @@ default_socket_timeout = 60
860860
; If you wish to have an extension loaded automatically, use the following
861861
; syntax:
862862
;
863-
; extension=modulename.extension
863+
; extension=modulename
864864
;
865-
; For example, on Windows:
865+
; For example:
866866
;
867-
; extension=mysqli.dll
868-
;
869-
; ... or under UNIX:
870-
;
871-
; extension=mysqli.so
872-
;
873-
; ... or with a path:
867+
; extension=mysqli
868+
;
869+
; When the extension library to load is not located in the default extension
870+
; directory, You may specify an absolute path to the library file:
874871
;
875872
; extension=/path/to/extension/mysqli.so
876873
;
877-
; If you only provide the name of the extension, PHP will look for it in its
878-
; default extension directory.
874+
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
875+
; 'extension='php_<ext>.dll') is supported for legacy reasons and may be
876+
; deprecated in a future PHP major version. So, when it is possible, please
877+
; move to the new ('extension=<ext>) syntax.
878+
;
879+
; Notes for Windows environments :
879880
;
880-
; Windows Extensions
881-
; Note that ODBC support is built in, so no dll is needed for it.
882-
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5+)
883-
; extension folders as well as the separate PECL DLL download (PHP 5+).
884-
; Be sure to appropriately set the extension_dir directive.
881+
; - ODBC support is built in, so no dll is needed for it.
882+
; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)
883+
; extension folders as well as the separate PECL DLL download (PHP 5+).
884+
; Be sure to appropriately set the extension_dir directive.
885885
;
886-
;extension=php_bz2.dll
887-
;extension=php_curl.dll
888-
;extension=php_fileinfo.dll
889-
;extension=php_ftp.dll
890-
;extension=php_gd2.dll
891-
;extension=php_gettext.dll
892-
;extension=php_gmp.dll
893-
;extension=php_intl.dll
894-
;extension=php_imap.dll
895-
;extension=php_interbase.dll
896-
;extension=php_ldap.dll
897-
;extension=php_mbstring.dll
898-
;extension=php_exif.dll ; Must be after mbstring as it depends on it
899-
;extension=php_mysqli.dll
900-
;extension=php_oci8_12c.dll ; Use with Oracle Database 12c Instant Client
901-
;extension=php_openssl.dll
902-
;extension=php_pdo_firebird.dll
903-
;extension=php_pdo_mysql.dll
904-
;extension=php_pdo_oci.dll
905-
;extension=php_pdo_odbc.dll
906-
;extension=php_pdo_pgsql.dll
907-
;extension=php_pdo_sqlite.dll
908-
;extension=php_pgsql.dll
909-
;extension=php_shmop.dll
886+
;extension=bz2
887+
;extension=curl
888+
;extension=fileinfo
889+
;extension=gd2
890+
;extension=gettext
891+
;extension=gmp
892+
;extension=intl
893+
;extension=imap
894+
;extension=interbase
895+
;extension=ldap
896+
;extension=mbstring
897+
;extension=exif ; Must be after mbstring as it depends on it
898+
;extension=mysqli
899+
;extension=oci8_12c ; Use with Oracle Database 12c Instant Client
900+
;extension=openssl
901+
;extension=pdo_firebird
902+
;extension=pdo_mysql
903+
;extension=pdo_oci
904+
;extension=pdo_odbc
905+
;extension=pdo_pgsql
906+
;extension=pdo_sqlite
907+
;extension=pgsql
908+
;extension=shmop
910909

911910
; The MIBS data available in the PHP distribution must be installed.
912911
; See http://www.php.net/manual/en/snmp.installation.php
913-
;extension=php_snmp.dll
914-
915-
;extension=php_soap.dll
916-
;extension=php_sockets.dll
917-
;extension=php_sqlite3.dll
918-
;extension=php_tidy.dll
919-
;extension=php_xmlrpc.dll
920-
;extension=php_xsl.dll
912+
;extension=snmp
913+
914+
;extension=soap
915+
;extension=sockets
916+
;extension=sqlite3
917+
;extension=tidy
918+
;extension=xmlrpc
919+
;extension=xsl
921920

922921
;;;;;;;;;;;;;;;;;;;
923922
; Module Settings ;

php.ini-production

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -867,64 +867,63 @@ default_socket_timeout = 60
867867
; If you wish to have an extension loaded automatically, use the following
868868
; syntax:
869869
;
870-
; extension=modulename.extension
870+
; extension=modulename
871871
;
872-
; For example, on Windows:
872+
; For example:
873873
;
874-
; extension=mysqli.dll
875-
;
876-
; ... or under UNIX:
877-
;
878-
; extension=mysqli.so
879-
;
880-
; ... or with a path:
874+
; extension=mysqli
875+
;
876+
; When the extension library to load is not located in the default extension
877+
; directory, You may specify an absolute path to the library file:
881878
;
882879
; extension=/path/to/extension/mysqli.so
883880
;
884-
; If you only provide the name of the extension, PHP will look for it in its
885-
; default extension directory.
881+
; Note : The syntax used in previous PHP versions ('extension=<ext>.so' and
882+
; 'extension='php_<ext>.dll') is supported for legacy reasons and may be
883+
; deprecated in a future PHP major version. So, when it is possible, please
884+
; move to the new ('extension=<ext>) syntax.
885+
;
886+
; Notes for Windows environments :
886887
;
887-
; Windows Extensions
888-
; Note that ODBC support is built in, so no dll is needed for it.
889-
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5+)
890-
; extension folders as well as the separate PECL DLL download (PHP 5+).
891-
; Be sure to appropriately set the extension_dir directive.
888+
; - ODBC support is built in, so no dll is needed for it.
889+
; - Many DLL files are located in the extensions/ (PHP 4) or ext/ (PHP 5+)
890+
; extension folders as well as the separate PECL DLL download (PHP 5+).
891+
; Be sure to appropriately set the extension_dir directive.
892892
;
893-
;extension=php_bz2.dll
894-
;extension=php_curl.dll
895-
;extension=php_fileinfo.dll
896-
;extension=php_ftp.dll
897-
;extension=php_gd2.dll
898-
;extension=php_gettext.dll
899-
;extension=php_gmp.dll
900-
;extension=php_intl.dll
901-
;extension=php_imap.dll
902-
;extension=php_interbase.dll
903-
;extension=php_ldap.dll
904-
;extension=php_mbstring.dll
905-
;extension=php_exif.dll ; Must be after mbstring as it depends on it
906-
;extension=php_mysqli.dll
907-
;extension=php_oci8_12c.dll ; Use with Oracle Database 12c Instant Client
908-
;extension=php_openssl.dll
909-
;extension=php_pdo_firebird.dll
910-
;extension=php_pdo_mysql.dll
911-
;extension=php_pdo_oci.dll
912-
;extension=php_pdo_odbc.dll
913-
;extension=php_pdo_pgsql.dll
914-
;extension=php_pdo_sqlite.dll
915-
;extension=php_pgsql.dll
916-
;extension=php_shmop.dll
893+
;extension=bz2
894+
;extension=curl
895+
;extension=fileinfo
896+
;extension=gd2
897+
;extension=gettext
898+
;extension=gmp
899+
;extension=intl
900+
;extension=imap
901+
;extension=interbase
902+
;extension=ldap
903+
;extension=mbstring
904+
;extension=exif ; Must be after mbstring as it depends on it
905+
;extension=mysqli
906+
;extension=oci8_12c ; Use with Oracle Database 12c Instant Client
907+
;extension=openssl
908+
;extension=pdo_firebird
909+
;extension=pdo_mysql
910+
;extension=pdo_oci
911+
;extension=pdo_odbc
912+
;extension=pdo_pgsql
913+
;extension=pdo_sqlite
914+
;extension=pgsql
915+
;extension=shmop
917916

918917
; The MIBS data available in the PHP distribution must be installed.
919918
; See http://www.php.net/manual/en/snmp.installation.php
920-
;extension=php_snmp.dll
921-
922-
;extension=php_soap.dll
923-
;extension=php_sockets.dll
924-
;extension=php_sqlite3.dll
925-
;extension=php_tidy.dll
926-
;extension=php_xmlrpc.dll
927-
;extension=php_xsl.dll
919+
;extension=snmp
920+
921+
;extension=soap
922+
;extension=sockets
923+
;extension=sqlite3
924+
;extension=tidy
925+
;extension=xmlrpc
926+
;extension=xsl
928927

929928
;;;;;;;;;;;;;;;;;;;
930929
; Module Settings ;

0 commit comments

Comments
 (0)