registry.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Zeev Suraski <zeev@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include "php.h"
  17. #include "php_ini.h"
  18. #include "php_win32_globals.h"
  19. #define PHP_REGISTRY_KEY "SOFTWARE\\PHP"
  20. #define PHP_VER1(V1) #V1
  21. #define PHP_VER2(V1,V2) #V1"."#V2
  22. #define PHP_VER3(V1,V2,V3) #V1"."#V2"."#V3
  23. #define PHP_REGISTRY_KEYV(VER) PHP_REGISTRY_KEY"\\"VER
  24. #define PHP_REGISTRY_KEY1(V1) PHP_REGISTRY_KEY"\\"PHP_VER1(V1)
  25. #define PHP_REGISTRY_KEY2(V1,V2) PHP_REGISTRY_KEY"\\"PHP_VER2(V1,V2)
  26. #define PHP_REGISTRY_KEY3(V1,V2,V3) PHP_REGISTRY_KEY"\\"PHP_VER3(V1,V2,V3)
  27. static const char* registry_keys[] = {
  28. PHP_REGISTRY_KEYV(PHP_VERSION),
  29. PHP_REGISTRY_KEY3(PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION),
  30. PHP_REGISTRY_KEY2(PHP_MAJOR_VERSION, PHP_MINOR_VERSION),
  31. PHP_REGISTRY_KEY1(PHP_MAJOR_VERSION),
  32. PHP_REGISTRY_KEY,
  33. NULL
  34. };
  35. static int OpenPhpRegistryKey(char* sub_key, HKEY *hKey)
  36. {/*{{{*/
  37. const char **key_name = registry_keys;
  38. if (sub_key) {
  39. size_t main_key_len;
  40. size_t sub_key_len = strlen(sub_key);
  41. char *reg_key;
  42. while (*key_name) {
  43. LONG ret;
  44. main_key_len = strlen(*key_name);
  45. reg_key = emalloc(main_key_len + sub_key_len + 1);
  46. memcpy(reg_key, *key_name, main_key_len);
  47. memcpy(reg_key + main_key_len, sub_key, sub_key_len + 1);
  48. ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, reg_key, 0, KEY_READ, hKey);
  49. efree(reg_key);
  50. if (ret == ERROR_SUCCESS) {
  51. return 1;
  52. }
  53. ++key_name;
  54. }
  55. } else {
  56. while (*key_name) {
  57. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, *key_name, 0, KEY_READ, hKey) == ERROR_SUCCESS) {
  58. return 1;
  59. }
  60. ++key_name;
  61. }
  62. }
  63. return 0;
  64. }/*}}}*/
  65. static int LoadDirectory(HashTable *directories, HKEY key, char *path, int path_len, HashTable *parent_ht)
  66. {/*{{{*/
  67. DWORD keys, values, max_key, max_name, max_value;
  68. int ret = 0;
  69. HashTable *ht = NULL;
  70. if (RegQueryInfoKey(key, NULL, NULL, NULL, &keys, &max_key, NULL, &values, &max_name, &max_value, NULL, NULL) == ERROR_SUCCESS) {
  71. if (values) {
  72. DWORD i;
  73. char *name = (char*)emalloc(max_name+1);
  74. char *value = (char*)emalloc(max_value+1);
  75. DWORD name_len, type, value_len;
  76. for (i = 0; i < values; i++) {
  77. name_len = max_name+1;
  78. value_len = max_value+1;
  79. memset(name, '\0', max_name+1);
  80. memset(value, '\0', max_value+1);
  81. if (RegEnumValue(key, i, name, &name_len, NULL, &type, value, &value_len) == ERROR_SUCCESS) {
  82. if ((type == REG_SZ) || (type == REG_EXPAND_SZ)) {
  83. zval data;
  84. if (!ht) {
  85. ht = (HashTable*)malloc(sizeof(HashTable));
  86. if (!ht) {
  87. return ret;
  88. }
  89. zend_hash_init(ht, 0, NULL, ZVAL_INTERNAL_PTR_DTOR, 1);
  90. }
  91. ZVAL_PSTRINGL(&data, value, value_len-1);
  92. zend_hash_str_update(ht, name, name_len, &data);
  93. }
  94. }
  95. }
  96. if (ht) {
  97. if (parent_ht) {
  98. zend_string *index;
  99. zend_ulong num;
  100. zval *tmpdata;
  101. ZEND_HASH_FOREACH_KEY_VAL(parent_ht, num, index, tmpdata) {
  102. zend_hash_add(ht, index, tmpdata);
  103. } ZEND_HASH_FOREACH_END();
  104. }
  105. zend_hash_str_update_mem(directories, path, path_len, ht, sizeof(HashTable));
  106. ret = 1;
  107. }
  108. efree(name);
  109. efree(value);
  110. }
  111. if (ht == NULL) {
  112. ht = parent_ht;
  113. }
  114. if (keys) {
  115. DWORD i;
  116. char *name = (char*)emalloc(max_key+1);
  117. char *new_path = (char*)emalloc(path_len+max_key+2);
  118. DWORD name_len;
  119. FILETIME t;
  120. HKEY subkey;
  121. for (i = 0; i < keys; i++) {
  122. name_len = max_key+1;
  123. if (RegEnumKeyEx(key, i, name, &name_len, NULL, NULL, NULL, &t) == ERROR_SUCCESS) {
  124. if (RegOpenKeyEx(key, name, 0, KEY_READ, &subkey) == ERROR_SUCCESS) {
  125. if (path_len) {
  126. memcpy(new_path, path, path_len);
  127. new_path[path_len] = '/';
  128. memcpy(new_path+path_len+1, name, name_len+1);
  129. zend_str_tolower(new_path, path_len+name_len+1);
  130. name_len += path_len+1;
  131. } else {
  132. memcpy(new_path, name, name_len+1);
  133. zend_str_tolower(new_path, name_len);
  134. }
  135. if (LoadDirectory(directories, subkey, new_path, name_len, ht)) {
  136. ret = 1;
  137. }
  138. RegCloseKey(subkey);
  139. }
  140. }
  141. }
  142. efree(new_path);
  143. efree(name);
  144. }
  145. }
  146. return ret;
  147. }/*}}}*/
  148. static void delete_internal_hashtable(zval *zv)
  149. {/*{{{*/
  150. HashTable *ht = (HashTable *)Z_PTR_P(zv);
  151. zend_hash_destroy(ht);
  152. free(ht);
  153. }/*}}}*/
  154. #define RegNotifyFlags (REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_ATTRIBUTES | REG_NOTIFY_CHANGE_LAST_SET)
  155. void UpdateIniFromRegistry(char *path)
  156. {/*{{{*/
  157. char *p, *orig_path;
  158. int path_len;
  159. if(!path) {
  160. return;
  161. }
  162. if (!PW32G(registry_directories)) {
  163. PW32G(registry_directories) = (HashTable*)malloc(sizeof(HashTable));
  164. if (!PW32G(registry_directories)) {
  165. return;
  166. }
  167. zend_hash_init(PW32G(registry_directories), 0, NULL, delete_internal_hashtable, 1);
  168. if (!OpenPhpRegistryKey("\\Per Directory Values", &PW32G(registry_key))) {
  169. PW32G(registry_key) = NULL;
  170. return;
  171. }
  172. PW32G(registry_event) = CreateEvent(NULL, TRUE, FALSE, NULL);
  173. if (PW32G(registry_event)) {
  174. RegNotifyChangeKeyValue(PW32G(registry_key), TRUE, RegNotifyFlags, PW32G(registry_event), TRUE);
  175. }
  176. if (!LoadDirectory(PW32G(registry_directories), PW32G(registry_key), "", 0, NULL)) {
  177. return;
  178. }
  179. } else if (PW32G(registry_event) && WaitForSingleObject(PW32G(registry_event), 0) == WAIT_OBJECT_0) {
  180. RegNotifyChangeKeyValue(PW32G(registry_key), TRUE, RegNotifyFlags, PW32G(registry_event), TRUE);
  181. zend_hash_clean(PW32G(registry_directories));
  182. if (!LoadDirectory(PW32G(registry_directories), PW32G(registry_key), "", 0, NULL)) {
  183. return;
  184. }
  185. } else if (zend_hash_num_elements(PW32G(registry_directories)) == 0) {
  186. return;
  187. }
  188. orig_path = path = estrdup(path);
  189. /* Get rid of C:, if exists */
  190. p = strchr(path, ':');
  191. if (p) {
  192. *p = path[0]; /* replace the colon with the drive letter */
  193. path = p; /* make path point to the drive letter */
  194. } else {
  195. if (path[0] != '\\' && path[0] != '/') {
  196. char tmp_buf[MAXPATHLEN], *cwd;
  197. char drive_letter;
  198. /* get current working directory and prepend it to the path */
  199. if (!VCWD_GETCWD(tmp_buf, MAXPATHLEN)) {
  200. efree(orig_path);
  201. return;
  202. }
  203. cwd = strchr(tmp_buf, ':');
  204. if (!cwd) {
  205. drive_letter = 'C';
  206. cwd = tmp_buf;
  207. } else {
  208. drive_letter = tmp_buf[0];
  209. cwd++;
  210. }
  211. while (*cwd == '\\' || *cwd == '/') {
  212. cwd++;
  213. }
  214. spprintf(&path, 0, "%c\\%s\\%s", drive_letter, cwd, orig_path);
  215. efree(orig_path);
  216. orig_path = path;
  217. }
  218. }
  219. path_len = 0;
  220. while (path[path_len] != 0) {
  221. if (path[path_len] == '\\') {
  222. path[path_len] = '/';
  223. }
  224. path_len++;
  225. }
  226. zend_str_tolower(path, path_len);
  227. while (path_len > 0) {
  228. HashTable *ht = (HashTable *)zend_hash_str_find_ptr(PW32G(registry_directories), path, path_len);
  229. if (ht != NULL) {
  230. zend_string *index;
  231. zval *data;
  232. ZEND_HASH_FOREACH_STR_KEY_VAL(ht, index, data) {
  233. zend_alter_ini_entry(index, Z_STR_P(data), PHP_INI_USER, PHP_INI_STAGE_ACTIVATE);
  234. } ZEND_HASH_FOREACH_END();
  235. }
  236. do {
  237. path_len--;
  238. } while (path_len > 0 && path[path_len] != '/');
  239. path[path_len] = 0;
  240. }
  241. efree(orig_path);
  242. }/*}}}*/
  243. #define PHPRC_REGISTRY_NAME "IniFilePath"
  244. char *GetIniPathFromRegistry()
  245. {/*{{{*/
  246. char *reg_location = NULL;
  247. HKEY hKey;
  248. if (OpenPhpRegistryKey(NULL, &hKey)) {
  249. DWORD buflen = MAXPATHLEN;
  250. reg_location = emalloc(MAXPATHLEN+1);
  251. if(RegQueryValueEx(hKey, PHPRC_REGISTRY_NAME, 0, NULL, reg_location, &buflen) != ERROR_SUCCESS) {
  252. RegCloseKey(hKey);
  253. efree(reg_location);
  254. reg_location = NULL;
  255. return reg_location;
  256. }
  257. RegCloseKey(hKey);
  258. }
  259. return reg_location;
  260. }/*}}}*/