1 /*
2   +----------------------------------------------------------------------+
3   | Copyright (c) 2009 The PHP Group                                     |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 3.0 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   | http://www.php.net/license/3_0.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   | Authors: Andrei Zmievski <andrei@php.net>                            |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifndef PHP_MEMCACHED_PRIVATE_H
18 #define PHP_MEMCACHED_PRIVATE_H
19 
20 #ifdef PHP_WIN32
21 #include "main/config.w32.h"
22 #else
23 #include "main/php_config.h"
24 #endif
25 
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29 
30 #include "php_libmemcached_compat.h"
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include <php.h>
35 #include <php_main.h>
36 
37 #ifdef ZTS
38 #  include "TSRM.h"
39 #endif
40 
41 #include <php_ini.h>
42 #include <SAPI.h>
43 #include <ext/standard/info.h>
44 #include <zend_extensions.h>
45 #include <zend_exceptions.h>
46 #include <ext/standard/php_smart_string.h>
47 #include <ext/standard/php_var.h>
48 #include <ext/standard/basic_functions.h>
49 
50 #ifdef PHP_WIN32
51 # include "win32/php_stdint.h"
52 #else
53 /* Used to store the size of the block */
54 #  if defined(HAVE_INTTYPES_H)
55 #    include <inttypes.h>
56 #  elif defined(HAVE_STDINT_H)
57 #    include <stdint.h>
58 #  endif
59 #endif
60 
61 #ifndef HAVE_INT32_T
62 #  if SIZEOF_INT == 4
63 typedef int int32_t;
64 #  elif SIZEOF_LONG == 4
65 typedef long int int32_t;
66 #  endif
67 #endif
68 
69 #ifndef HAVE_UINT32_T
70 #  if SIZEOF_INT == 4
71 typedef unsigned int uint32_t;
72 #  elif SIZEOF_LONG == 4
73 typedef unsigned long int uint32_t;
74 #  endif
75 #endif
76 
77 /* Backwards compatibility for GC API change in PHP 7.3 */
78 #if PHP_VERSION_ID < 70300
79 #  define GC_ADDREF(p)            ++GC_REFCOUNT(p)
80 #  define GC_DELREF(p)            --GC_REFCOUNT(p)
81 #  define GC_SET_REFCOUNT(p, rc)  GC_REFCOUNT(p) = rc
82 #endif
83 
84 /****************************************
85   Structures and definitions
86 ****************************************/
87 typedef enum {
88 	SERIALIZER_PHP        = 1,
89 	SERIALIZER_IGBINARY   = 2,
90 	SERIALIZER_JSON       = 3,
91 	SERIALIZER_JSON_ARRAY = 4,
92 	SERIALIZER_MSGPACK    = 5
93 } php_memc_serializer_type;
94 
95 typedef enum {
96 	COMPRESSION_TYPE_ZLIB   = 1,
97 	COMPRESSION_TYPE_FASTLZ = 2
98 } php_memc_compression_type;
99 
100 typedef struct {
101 	const char *name;
102 	php_memc_serializer_type type;
103 } php_memc_serializer;
104 
105 #if defined(HAVE_MEMCACHED_IGBINARY)
106 # define SERIALIZER_DEFAULT SERIALIZER_IGBINARY
107 # define SERIALIZER_DEFAULT_NAME "igbinary"
108 #elif defined(HAVE_MEMCACHED_MSGPACK)
109 # define SERIALIZER_DEFAULT SERIALIZER_MSGPACK
110 # define SERIALIZER_DEFAULT_NAME "msgpack"
111 #else
112 # define SERIALIZER_DEFAULT SERIALIZER_PHP
113 # define SERIALIZER_DEFAULT_NAME "php"
114 #endif /* HAVE_MEMCACHED_IGBINARY / HAVE_MEMCACHED_MSGPACK */
115 
116 #ifdef HAVE_MEMCACHED_SASL
117 # include <sasl/sasl.h>
118 #endif
119 
120 #ifdef HAVE_MEMCACHED_PROTOCOL
121 typedef enum {
122 	MEMC_SERVER_ON_MIN       = -1,
123 	MEMC_SERVER_ON_CONNECT   = 0,
124 	MEMC_SERVER_ON_ADD       = 1,
125 	MEMC_SERVER_ON_APPEND    = 2,
126 	MEMC_SERVER_ON_DECREMENT = 3,
127 	MEMC_SERVER_ON_DELETE    = 4,
128 	MEMC_SERVER_ON_FLUSH     = 5,
129 	MEMC_SERVER_ON_GET       = 6,
130 	MEMC_SERVER_ON_INCREMENT = 7,
131 	MEMC_SERVER_ON_NOOP      = 8,
132 	MEMC_SERVER_ON_PREPEND   = 9,
133 	MEMC_SERVER_ON_QUIT      = 10,
134 	MEMC_SERVER_ON_REPLACE   = 11,
135 	MEMC_SERVER_ON_SET       = 12,
136 	MEMC_SERVER_ON_STAT      = 13,
137 	MEMC_SERVER_ON_VERSION   = 14,
138 	MEMC_SERVER_ON_MAX
139 } php_memc_event_t;
140 
141 
142 typedef struct {
143 	zend_fcall_info fci;
144 	zend_fcall_info_cache fci_cache;
145 } php_memc_server_cb_t;
146 #endif
147 
148 ZEND_BEGIN_MODULE_GLOBALS(php_memcached)
149 
150 #ifdef HAVE_MEMCACHED_SESSION
151 	/* Session related variables */
152 	struct {
153 		zend_bool lock_enabled;
154 		zend_long lock_wait_max;
155 		zend_long lock_wait_min;
156 		zend_long lock_retries;
157 		zend_long lock_expiration;
158 
159 		zend_bool binary_protocol_enabled;
160 		zend_bool consistent_hash_enabled;
161 		char     *consistent_hash_name;
162 		int       consistent_hash_type;
163 
164 		zend_long server_failure_limit;
165 		zend_long number_of_replicas;
166 		zend_bool randomize_replica_read_enabled;
167 		zend_bool remove_failed_servers_enabled;
168 
169 		zend_long connect_timeout;
170 
171 		char *prefix;
172 		zend_bool persistent_enabled;
173 
174 		char *sasl_username;
175 		char *sasl_password;
176 	} session;
177 #endif
178 
179 	struct {
180 		char     *serializer_name;
181 		char     *compression_name;
182 		zend_long compression_threshold;
183 		double    compression_factor;
184 		zend_long store_retry_count;
185 
186 		/* Converted values*/
187 		php_memc_serializer_type  serializer_type;
188 		php_memc_compression_type compression_type;
189 
190 		/* Whether we have initialised sasl for this process */
191 		zend_bool sasl_initialised;
192 
193 		struct {
194 
195 			zend_bool consistent_hash_enabled;
196 			zend_bool binary_protocol_enabled;
197 			zend_long connect_timeout;
198 
199 		} default_behavior;
200 
201 	} memc;
202 
203 	/* For deprecated values */
204 	zend_long no_effect;
205 
206 #ifdef HAVE_MEMCACHED_PROTOCOL
207 	struct {
208 		php_memc_server_cb_t callbacks [MEMC_SERVER_ON_MAX];
209 	} server;
210 #endif
211 
212 ZEND_END_MODULE_GLOBALS(php_memcached)
213 
214 /* Globals accessor macros */
215 #ifdef ZTS
216 #  define MEMC_G(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, memc.v)
217 #  define MEMC_SERVER_G(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, server.v)
218 #  define MEMC_SESS_INI(v) TSRMG(php_memcached_globals_id, zend_php_memcached_globals *, session.v)
219 #else
220 #  define MEMC_G(v) (php_memcached_globals.memc.v)
221 #  define MEMC_SERVER_G(v) (php_memcached_globals.server.v)
222 #  define MEMC_SESS_INI(v) (php_memcached_globals.session.v)
223 #endif
224 
225 #define MEMC_SESS_STR_INI(vv) ((MEMC_SESS_INI(vv) && *MEMC_SESS_INI(vv)) ? MEMC_SESS_INI(vv) : NULL)
226 
227 PHP_RINIT_FUNCTION(memcached);
228 PHP_RSHUTDOWN_FUNCTION(memcached);
229 PHP_MINIT_FUNCTION(memcached);
230 PHP_MSHUTDOWN_FUNCTION(memcached);
231 PHP_MINFO_FUNCTION(memcached);
232 
233 char *php_memc_printable_func (zend_fcall_info *fci, zend_fcall_info_cache *fci_cache);
234 
235 memcached_return php_memcached_exist (memcached_st *memc, zend_string *key);
236 
237 zend_bool php_memc_init_sasl_if_needed();
238 
239 #endif /* PHP_MEMCACHED_PRIVATE_H */
240 
241 /*
242  * Local variables:
243  * tab-width: 4
244  * c-basic-offset: 4
245  * End:
246  * vim600: noet sw=4 ts=4 fdm=marker
247  * vim<600: noet sw=4 ts=4
248  */
249