5.0 from trustcommerce
[tclink.git] / php_tclink.c
CommitLineData
d667da5b
IK
1/*
2 * TCLink PHP Module
3 *
c235c6ac 4 * TCLink Copyright (c) 2010-2020 TrustCommerce.
d667da5b
IK
5 * http://www.trustcommerce.com
6 * techsupport@trustcommerce.com
7 * (949) 387-3747
8 *
9 * PHP Port Copyright (c) 2000
10 * Andrew Barnett <andrew@dataclarity.com>
11 * 2000-11-21
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include "php.h"
29#include "php_config.h"
30
31#if HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include "php_tclink.h"
36
37#if PHP_API_VERSION >= 20151012
38#include "ext/standard/info.h"
39#endif
40
41#if HAVE_TCLINK
42
43zend_function_entry php_tclink_functions[] = {
44 PHP_FE(tclink_send, NULL)
45 PHP_FE(tclink_getversion, NULL)
46 {NULL, NULL, NULL}
47};
48
49zend_module_entry php_tclink_module_entry = {
50#ifdef STANDARD_MODULE_HEADER
51 STANDARD_MODULE_HEADER,
52#endif
53 "tclink",
54 php_tclink_functions,
55 NULL,
56 NULL,
57 NULL,
58 NULL,
59 PHP_MINFO(tclink),
60 NO_VERSION_YET,
61#if PHP_API_VERSION < 20090626
62 STANDARD_MODULE_PROPERTIES
63#else
64 0,
65 NULL,
66 NULL,
67 NULL,
68 NULL,
69 STANDARD_MODULE_PROPERTIES_EX
70#endif
71};
72
73#define PHP_TCLINK_DEFAULT_BUFFER_SIZE 8196
74
75#ifdef COMPILE_DL_TCLINK
76ZEND_GET_MODULE(php_tclink)
77#endif
78
79PHP_MINFO_FUNCTION(tclink)
80{
81 char *tmp = (char *)malloc(1024);
82 php_info_print_table_start();
83 if(tmp == NULL) {
84 php_info_print_table_row(2, "TCLink PHP Module", "enabled");
85 } else {
86 php_info_print_table_row(2, "TCLink PHP Module", TCLinkGetVersion(tmp));
87 free(tmp);
88 }
89 php_info_print_table_end();
90}
91
92/* {{{ proto void tclink_send(array params)
93 Send the transaction in for processing. */
94PHP_FUNCTION(tclink_send)
95{
96/* start with a clean break using PHP7 APIs */
97#if PHP_API_VERSION >= 20151012
98 zval *arr;
99 TCLinkHandle tclink;
100 HashTable *hash;
101 zval *zvalue;
102 zend_string * zkey;
103 char buf[4096];
104 char *key, *value, *next_key;
105
106 if (ZEND_NUM_ARGS() != 1)
107 {
108 WRONG_PARAM_COUNT;
109 }
110
111 /* initialize it here so that it always has a return value is always something */
112 array_init(return_value);
113 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &arr) == FAILURE) {
114 return;
115 }
116
117 tclink = TCLinkCreate();
118 hash = HASH_OF(arr);
119
120 ZEND_HASH_FOREACH_STR_KEY_VAL(hash, zkey, zvalue)
121 /* there aren't any numeric keys in tclink parameters */
122 if (ZSTR_VAL(zkey))
123 {
124 /* for converting the numeric values in case they are provided */
125 if (Z_TYPE_P(zvalue) != IS_STRING)
126 {
127 convert_to_string(zvalue);
128 }
129 TCLinkPushParam(tclink, ZSTR_VAL(zkey), Z_STRVAL_P(zvalue));
130 }
131 ZEND_HASH_FOREACH_END();
132
133 /* send the transaction */
134 TCLinkSend(tclink);
135
136 /* pull out the parameters and put them in a hash */
137 TCLinkGetEntireResponse(tclink, buf, sizeof(buf));
138
139 key = value = buf;
140 while (key && (value = strchr(key, '=')))
141 {
142 *value++ = 0;
143 next_key = strchr(value, '\n');
144 if (next_key) *next_key++ = 0;
145
146 add_assoc_string(return_value, key, value);
147
148 key = next_key;
149 }
150
151 TCLinkDestroy(tclink);
152/* for older PHP 4 / PHP 5 clients */
153#else
154 zval **params, **zvalue;
155 HashTable *hash;
156 char *key, *value, *next_key;
157
158 TCLinkHandle h;
159 char buf[4096];
160
161 /* check parameters */
162 if((ZEND_NUM_ARGS() != 1) ||
163 (zend_get_parameters_ex(1, &params) == FAILURE)) {
164 WRONG_PARAM_COUNT;
165 }
166
167 convert_to_array_ex(params);
168
169 h = TCLinkCreate();
170
171 /* grab the hash and stuff each parameter set into TCLink */
172 hash = HASH_OF(*params);
173 zend_hash_internal_pointer_reset(hash);
174 while (zend_hash_get_current_data(hash, (void **)&zvalue) == SUCCESS)
175 {
176 /* The Zend API added an extra parameter between 4.04 (sometime in
177 * 1999) and 4.06 (in early 2001). Assume that anything prior to
178 * 1/1/2001 is the older version. */
179#if PHP_API_VERSION < 20000101
180 zend_hash_get_current_key(hash, &key, NULL);
181#else
182 zend_hash_get_current_key(hash, &key, NULL, 0);
183#endif
184 convert_to_string_ex(zvalue);
185 value = Z_STRVAL_PP(zvalue);
186 TCLinkPushParam(h, key, value);
187 zend_hash_move_forward(hash);
188 }
189
190 /* send the transaction */
191 TCLinkSend(h);
192
193 /* pull out the parameters and put them in a hash */
194 TCLinkGetEntireResponse(h, buf, sizeof(buf));
195
196 array_init(return_value);
197
198 key = value = buf;
199 while (key && (value = strchr(key, '=')))
200 {
201 *value++ = 0;
202 next_key = strchr(value, '\n');
203 if (next_key) *next_key++ = 0;
204
205 add_assoc_string(return_value, key, value, 1);
206
207 key = next_key;
208 }
209
210 TCLinkDestroy(h);
211
212 /* return_value is returned automatically, we need not explictly call a
213 return macro */
214#endif
215}
216/* }}} */
217
218/* {{{ proto string tclink_getversion()
219 returns the API version information */
220PHP_FUNCTION(tclink_getversion)
221{
222 char version[1024];
223
224 TCLinkGetVersion(version);
225
226#if PHP_API_VERSION >= 20151012
227 char *str = estrdup(version);
228 RETURN_STRING(str);
229#else
230 RETURN_STRING(version, 1);
231#endif
232}
233/* }}} */
234
235#endif