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: Wez Furlong <wez@php.net> |
14 +----------------------------------------------------------------------+
15 */
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "pdo/php_pdo.h"
25 #include "pdo/php_pdo_driver.h"
26 #include "php_pdo_oci.h"
27 #include "php_pdo_oci_int.h"
28 #include "Zend/zend_exceptions.h"
29
30 static inline ub4 pdo_oci_sanitize_prefetch(long prefetch);
31
pdo_oci_fetch_error_func(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)32 static void pdo_oci_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
33 {
34 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
35 pdo_oci_error_info *einfo;
36
37 einfo = &H->einfo;
38
39 if (stmt) {
40 pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
41
42 if (S->einfo.errmsg) {
43 einfo = &S->einfo;
44 }
45 }
46
47 if (einfo->errcode) {
48 add_next_index_long(info, einfo->errcode);
49 add_next_index_string(info, einfo->errmsg);
50 }
51 }
52 /* }}} */
53
_oci_error(OCIError * err,pdo_dbh_t * dbh,pdo_stmt_t * stmt,char * what,sword status,int isinit,const char * file,int line)54 ub4 _oci_error(OCIError *err, pdo_dbh_t *dbh, pdo_stmt_t *stmt, char *what, sword status, int isinit, const char *file, int line) /* {{{ */
55 {
56 text errbuf[1024] = "<<Unknown>>";
57 char tmp_buf[2048];
58 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
59 pdo_oci_error_info *einfo;
60 pdo_oci_stmt *S = NULL;
61 pdo_error_type *pdo_err = &dbh->error_code;
62
63 if (stmt) {
64 S = (pdo_oci_stmt*)stmt->driver_data;
65 einfo = &S->einfo;
66 pdo_err = &stmt->error_code;
67 }
68 else {
69 einfo = &H->einfo;
70 }
71
72 if (einfo->errmsg) {
73 pefree(einfo->errmsg, dbh->is_persistent);
74 }
75
76 einfo->errmsg = NULL;
77 einfo->errcode = 0;
78 einfo->file = file;
79 einfo->line = line;
80
81 if (isinit) { /* Initialization error */
82 strcpy(*pdo_err, "HY000");
83 slprintf(tmp_buf, sizeof(tmp_buf), "%s (%s:%d)", what, file, line);
84 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
85 }
86 else {
87 switch (status) {
88 case OCI_SUCCESS:
89 strcpy(*pdo_err, "00000");
90 break;
91 case OCI_ERROR:
92 OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
93 slprintf(tmp_buf, sizeof(tmp_buf), "%s: %s (%s:%d)", what, errbuf, file, line);
94 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
95 break;
96 case OCI_SUCCESS_WITH_INFO:
97 OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
98 slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_SUCCESS_WITH_INFO: %s (%s:%d)", what, errbuf, file, line);
99 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
100 break;
101 case OCI_NEED_DATA:
102 slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NEED_DATA (%s:%d)", what, file, line);
103 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
104 break;
105 case OCI_NO_DATA:
106 slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NO_DATA (%s:%d)", what, file, line);
107 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
108 break;
109 case OCI_INVALID_HANDLE:
110 slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_INVALID_HANDLE (%s:%d)", what, file, line);
111 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
112 break;
113 case OCI_STILL_EXECUTING:
114 slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_STILL_EXECUTING (%s:%d)", what, file, line);
115 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
116 break;
117 case OCI_CONTINUE:
118 slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_CONTINUE (%s:%d)", what, file, line);
119 einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
120 break;
121 }
122
123 if (einfo->errcode) {
124 switch (einfo->errcode) {
125 case 1013: /* user requested cancel of current operation */
126 zend_bailout();
127 break;
128
129 case 12154: /* ORA-12154: TNS:could not resolve service name */
130 strcpy(*pdo_err, "42S02");
131 break;
132
133 case 22: /* ORA-00022: invalid session id */
134 case 378:
135 case 602:
136 case 603:
137 case 604:
138 case 609:
139 case 1012: /* ORA-01012: */
140 case 1033:
141 case 1041:
142 case 1043:
143 case 1089:
144 case 1090:
145 case 1092:
146 case 3113: /* ORA-03133: end of file on communication channel */
147 case 3114:
148 case 3122:
149 case 3135:
150 case 12153:
151 case 27146:
152 case 28511:
153 /* consider the connection closed */
154 dbh->is_closed = 1;
155 H->attached = 0;
156 strcpy(*pdo_err, "01002"); /* FIXME */
157 break;
158
159 default:
160 strcpy(*pdo_err, "HY000");
161 }
162 }
163
164 if (stmt) {
165 /* always propagate the error code back up to the dbh,
166 * so that we can catch the error information when execute
167 * is called via query. See Bug #33707 */
168 if (H->einfo.errmsg) {
169 pefree(H->einfo.errmsg, dbh->is_persistent);
170 }
171 H->einfo = *einfo;
172 H->einfo.errmsg = einfo->errmsg ? pestrdup(einfo->errmsg, dbh->is_persistent) : NULL;
173 strcpy(dbh->error_code, stmt->error_code);
174 }
175 }
176
177 /* little mini hack so that we can use this code from the dbh ctor */
178 if (!dbh->methods && status != OCI_SUCCESS_WITH_INFO) {
179 zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
180 }
181
182 return einfo->errcode;
183 }
184 /* }}} */
185
oci_handle_closer(pdo_dbh_t * dbh)186 static void oci_handle_closer(pdo_dbh_t *dbh) /* {{{ */
187 {
188 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
189
190 if (H->svc) {
191 /* rollback any outstanding work */
192 OCITransRollback(H->svc, H->err, 0);
193 }
194
195 if (H->session) {
196 OCIHandleFree(H->session, OCI_HTYPE_SESSION);
197 H->session = NULL;
198 }
199
200 if (H->svc) {
201 OCIHandleFree(H->svc, OCI_HTYPE_SVCCTX);
202 H->svc = NULL;
203 }
204
205 if (H->server && H->attached) {
206 H->last_err = OCIServerDetach(H->server, H->err, OCI_DEFAULT);
207 if (H->last_err) {
208 oci_drv_error("OCIServerDetach");
209 }
210 H->attached = 0;
211 }
212
213 if (H->server) {
214 OCIHandleFree(H->server, OCI_HTYPE_SERVER);
215 H->server = NULL;
216 }
217
218 if (H->err) {
219 OCIHandleFree(H->err, OCI_HTYPE_ERROR);
220 H->err = NULL;
221 }
222
223 if (H->charset && H->env) {
224 OCIHandleFree(H->env, OCI_HTYPE_ENV);
225 H->env = NULL;
226 }
227
228 if (H->einfo.errmsg) {
229 pefree(H->einfo.errmsg, dbh->is_persistent);
230 H->einfo.errmsg = NULL;
231 }
232
233 pefree(H, dbh->is_persistent);
234 }
235 /* }}} */
236
oci_handle_preparer(pdo_dbh_t * dbh,zend_string * sql,pdo_stmt_t * stmt,zval * driver_options)237 static bool oci_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options) /* {{{ */
238 {
239 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
240 pdo_oci_stmt *S = ecalloc(1, sizeof(*S));
241 ub4 prefetch;
242 zend_string *nsql = NULL;
243 int ret;
244
245 #ifdef HAVE_OCISTMTFETCH2
246 S->exec_type = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
247 PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL ?
248 OCI_STMT_SCROLLABLE_READONLY : OCI_DEFAULT;
249 #else
250 S->exec_type = OCI_DEFAULT;
251 #endif
252
253 S->H = H;
254 stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
255 ret = pdo_parse_params(stmt, sql, &nsql);
256
257 if (ret == 1) {
258 /* query was re-written */
259 sql = nsql;
260 } else if (ret == -1) {
261 /* couldn't grok it */
262 strcpy(dbh->error_code, stmt->error_code);
263 efree(S);
264 return false;
265 }
266
267 /* create an OCI statement handle */
268 OCIHandleAlloc(H->env, (dvoid*)&S->stmt, OCI_HTYPE_STMT, 0, NULL);
269
270 /* and our own private error handle */
271 OCIHandleAlloc(H->env, (dvoid*)&S->err, OCI_HTYPE_ERROR, 0, NULL);
272
273 if (ZSTR_LEN(sql) != 0) {
274 H->last_err = OCIStmtPrepare(S->stmt, H->err, (text*) ZSTR_VAL(sql), (ub4) ZSTR_LEN(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
275 if (nsql) {
276 zend_string_release(nsql);
277 nsql = NULL;
278 }
279 if (H->last_err) {
280 H->last_err = oci_drv_error("OCIStmtPrepare");
281 OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
282 OCIHandleFree(S->err, OCI_HTYPE_ERROR);
283 efree(S);
284 return false;
285 }
286
287 }
288
289 prefetch = H->prefetch; /* Note 0 is allowed so in future REF CURSORs can be used & then passed with no row loss*/
290 H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
291 OCI_ATTR_PREFETCH_ROWS, H->err);
292 if (!H->last_err) {
293 prefetch *= PDO_OCI_PREFETCH_ROWSIZE;
294 H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
295 OCI_ATTR_PREFETCH_MEMORY, H->err);
296 }
297
298 stmt->driver_data = S;
299 stmt->methods = &oci_stmt_methods;
300 if (nsql) {
301 zend_string_release(nsql);
302 nsql = NULL;
303 }
304
305 return true;
306 }
307 /* }}} */
308
oci_handle_doer(pdo_dbh_t * dbh,const zend_string * sql)309 static zend_long oci_handle_doer(pdo_dbh_t *dbh, const zend_string *sql) /* {{{ */
310 {
311 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
312 OCIStmt *stmt;
313 ub2 stmt_type;
314 ub4 rowcount;
315 int ret = -1;
316
317 OCIHandleAlloc(H->env, (dvoid*)&stmt, OCI_HTYPE_STMT, 0, NULL);
318
319 H->last_err = OCIStmtPrepare(stmt, H->err, (text*)ZSTR_VAL(sql), (ub4) ZSTR_LEN(sql), OCI_NTV_SYNTAX, OCI_DEFAULT);
320 if (H->last_err) {
321 H->last_err = oci_drv_error("OCIStmtPrepare");
322 OCIHandleFree(stmt, OCI_HTYPE_STMT);
323 return -1;
324 }
325
326 H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &stmt_type, 0, OCI_ATTR_STMT_TYPE, H->err);
327
328 if (stmt_type == OCI_STMT_SELECT) {
329 /* invalid usage; cancel it */
330 OCIHandleFree(stmt, OCI_HTYPE_STMT);
331 php_error_docref(NULL, E_WARNING, "issuing a SELECT query here is invalid");
332 return -1;
333 }
334
335 /* now we are good to go */
336 H->last_err = OCIStmtExecute(H->svc, stmt, H->err, 1, 0, NULL, NULL,
337 (dbh->auto_commit && !dbh->in_txn) ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
338
339 sword last_err = H->last_err;
340
341 if (last_err) {
342 H->last_err = oci_drv_error("OCIStmtExecute");
343 }
344
345 if (!last_err || last_err == OCI_SUCCESS_WITH_INFO) {
346 /* return the number of affected rows */
347 H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &rowcount, 0, OCI_ATTR_ROW_COUNT, H->err);
348 ret = rowcount;
349 }
350
351 OCIHandleFree(stmt, OCI_HTYPE_STMT);
352
353 return ret;
354 }
355 /* }}} */
356
oci_handle_quoter(pdo_dbh_t * dbh,const zend_string * unquoted,enum pdo_param_type paramtype)357 static zend_string* oci_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype ) /* {{{ */
358 {
359 int qcount = 0;
360 char const *cu, *l, *r;
361 char *c, *quoted;
362 size_t quotedlen;
363 zend_string *quoted_str;
364
365 if (ZSTR_LEN(unquoted) == 0) {
366 return zend_string_init("''", 2, 0);
367 }
368
369 /* count single quotes */
370 for (cu = ZSTR_VAL(unquoted); (cu = strchr(cu,'\'')); qcount++, cu++)
371 ; /* empty loop */
372
373 quotedlen = ZSTR_LEN(unquoted) + qcount + 2;
374 quoted = c = emalloc(quotedlen+1);
375 *c++ = '\'';
376
377 /* foreach (chunk that ends in a quote) */
378 for (l = ZSTR_VAL(unquoted); (r = strchr(l,'\'')); l = r+1) {
379 strncpy(c, l, r-l+1);
380 c += (r-l+1);
381 *c++ = '\''; /* add second quote */
382 }
383
384 /* Copy remainder and add enclosing quote */
385 strncpy(c, l, quotedlen-(c-quoted)-1);
386 quoted[quotedlen-1] = '\'';
387 quoted[quotedlen] = '\0';
388
389 quoted_str = zend_string_init(quoted, quotedlen, 0);
390 efree(quoted);
391 return quoted_str;
392 }
393 /* }}} */
394
oci_handle_begin(pdo_dbh_t * dbh)395 static bool oci_handle_begin(pdo_dbh_t *dbh) /* {{{ */
396 {
397 /* with Oracle, there is nothing special to be done */
398 return true;
399 }
400 /* }}} */
401
oci_handle_commit(pdo_dbh_t * dbh)402 static bool oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */
403 {
404 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
405
406 H->last_err = OCITransCommit(H->svc, H->err, 0);
407
408 if (H->last_err) {
409 H->last_err = oci_drv_error("OCITransCommit");
410 return false;
411 }
412 return true;
413 }
414 /* }}} */
415
oci_handle_rollback(pdo_dbh_t * dbh)416 static bool oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
417 {
418 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
419
420 H->last_err = OCITransRollback(H->svc, H->err, 0);
421
422 if (H->last_err) {
423 H->last_err = oci_drv_error("OCITransRollback");
424 return false;
425 }
426 return true;
427 }
428 /* }}} */
429
oci_handle_set_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)430 static bool oci_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
431 {
432 zend_long lval;
433 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
434
435 switch (attr) {
436 case PDO_ATTR_AUTOCOMMIT:
437 {
438 bool bval;
439 if (!pdo_get_bool_param(&bval, val)) {
440 return false;
441 }
442
443 if (dbh->in_txn) {
444 /* Assume they want to commit whatever is outstanding */
445 H->last_err = OCITransCommit(H->svc, H->err, 0);
446
447 if (H->last_err) {
448 H->last_err = oci_drv_error("OCITransCommit");
449 return false;
450 }
451 dbh->in_txn = false;
452 }
453
454 dbh->auto_commit = (unsigned int) bval;
455 return true;
456 }
457 case PDO_ATTR_PREFETCH:
458 {
459 if (!pdo_get_long_param(&lval, val)) {
460 return false;
461 }
462
463 H->prefetch = pdo_oci_sanitize_prefetch(lval);
464 return true;
465 }
466 case PDO_OCI_ATTR_ACTION:
467 {
468 #if (OCI_MAJOR_VERSION >= 10)
469 zend_string *action = zval_try_get_string(val);
470 if (UNEXPECTED(!action)) {
471 return false;
472 }
473
474 H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
475 (dvoid *) ZSTR_VAL(action), (ub4) ZSTR_LEN(action),
476 OCI_ATTR_ACTION, H->err);
477 if (H->last_err) {
478 oci_drv_error("OCIAttrSet: OCI_ATTR_ACTION");
479 return false;
480 }
481 return true;
482 #else
483 oci_drv_error("Unsupported attribute type");
484 return false;
485 #endif
486 }
487 case PDO_OCI_ATTR_CLIENT_INFO:
488 {
489 #if (OCI_MAJOR_VERSION >= 10)
490 zend_string *client_info = zval_try_get_string(val);
491 if (UNEXPECTED(!client_info)) {
492 return false;
493 }
494
495 H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
496 (dvoid *) ZSTR_VAL(client_info), (ub4) ZSTR_LEN(client_info),
497 OCI_ATTR_CLIENT_INFO, H->err);
498 if (H->last_err) {
499 oci_drv_error("OCIAttrSet: OCI_ATTR_CLIENT_INFO");
500 return false;
501 }
502 return true;
503 #else
504 oci_drv_error("Unsupported attribute type");
505 return false;
506 #endif
507 }
508 case PDO_OCI_ATTR_CLIENT_IDENTIFIER:
509 {
510 #if (OCI_MAJOR_VERSION >= 10)
511 zend_string *identifier = zval_try_get_string(val);
512 if (UNEXPECTED(!identifier)) {
513 return false;
514 }
515
516 H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
517 (dvoid *) ZSTR_VAL(identifier), (ub4) ZSTR_LEN(identifier),
518 OCI_ATTR_CLIENT_IDENTIFIER, H->err);
519 if (H->last_err) {
520 oci_drv_error("OCIAttrSet: OCI_ATTR_CLIENT_IDENTIFIER");
521 return false;
522 }
523 return true;
524 #else
525 oci_drv_error("Unsupported attribute type");
526 return false;
527 #endif
528 }
529 case PDO_OCI_ATTR_MODULE:
530 {
531 #if (OCI_MAJOR_VERSION >= 10)
532 zend_string *module = zval_try_get_string(val);
533 if (UNEXPECTED(!module)) {
534 return false;
535 }
536
537 H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
538 (dvoid *) ZSTR_VAL(module), (ub4) ZSTR_LEN(module),
539 OCI_ATTR_MODULE, H->err);
540 if (H->last_err) {
541 oci_drv_error("OCIAttrSet: OCI_ATTR_MODULE");
542 return false;
543 }
544 return true;
545 #else
546 oci_drv_error("Unsupported attribute type");
547 return false;
548 #endif
549 }
550 case PDO_OCI_ATTR_CALL_TIMEOUT:
551 {
552 #if (OCI_MAJOR_VERSION >= 18)
553 if (!pdo_get_long_param(&lval, val)) {
554 return false;
555 }
556 ub4 timeout = (ub4) lval;
557
558 H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX,
559 (dvoid *) &timeout, (ub4) 0,
560 OCI_ATTR_CALL_TIMEOUT, H->err);
561 if (H->last_err) {
562 oci_drv_error("OCIAttrSet: OCI_ATTR_CALL_TIMEOUT");
563 return false;
564 }
565 return true;
566 #else
567 oci_drv_error("Unsupported attribute type");
568 return false;
569 #endif
570 }
571 default:
572 return false;
573 }
574
575 }
576 /* }}} */
577
oci_handle_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)578 static int oci_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value) /* {{{ */
579 {
580 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
581
582 switch (attr) {
583 case PDO_ATTR_SERVER_VERSION:
584 case PDO_ATTR_SERVER_INFO:
585 {
586 text infostr[512];
587 char verstr[15];
588 ub4 vernum;
589
590 if (OCIServerRelease(H->svc, H->err, infostr, (ub4)sizeof(infostr), (ub1)OCI_HTYPE_SVCCTX, &vernum))
591 {
592 ZVAL_STRING(return_value, "<<Unknown>>");
593 } else {
594 if (attr == PDO_ATTR_SERVER_INFO) {
595 ZVAL_STRING(return_value, (char *)infostr);
596 } else {
597 slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d",
598 (int)((vernum>>24) & 0xFF), /* version number */
599 (int)((vernum>>20) & 0x0F), /* release number*/
600 (int)((vernum>>12) & 0xFF), /* update number */
601 (int)((vernum>>8) & 0x0F), /* port release number */
602 (int)((vernum>>0) & 0xFF)); /* port update number */
603
604 ZVAL_STRING(return_value, verstr);
605 }
606 }
607 return TRUE;
608 }
609
610 case PDO_ATTR_CLIENT_VERSION:
611 {
612 #if OCI_MAJOR_VERSION > 10 || (OCI_MAJOR_VERSION == 10 && OCI_MINOR_VERSION >= 2)
613 /* Run time client version */
614 sword major, minor, update, patch, port_update;
615 char verstr[15];
616
617 OCIClientVersion(&major, &minor, &update, &patch, &port_update);
618 slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d", major, minor, update, patch, port_update);
619 ZVAL_STRING(return_value, verstr);
620 #elif defined(PHP_PDO_OCI_CLIENT_VERSION)
621 /* Compile time client version */
622 ZVAL_STRING(return_value, PHP_PDO_OCI_CLIENT_VERSION);
623 #else
624 return FALSE;
625
626 #endif /* Check for OCIClientVersion() support */
627
628 return TRUE;
629 }
630
631 case PDO_ATTR_AUTOCOMMIT:
632 ZVAL_BOOL(return_value, dbh->auto_commit);
633 return TRUE;
634
635 case PDO_ATTR_PREFETCH:
636 ZVAL_LONG(return_value, H->prefetch);
637 return TRUE;
638 case PDO_OCI_ATTR_CALL_TIMEOUT:
639 {
640 #if (OCI_MAJOR_VERSION >= 18)
641 ub4 timeout;
642
643 H->last_err = OCIAttrGet(H->svc, OCI_HTYPE_SVCCTX,
644 (dvoid *) &timeout, NULL,
645 OCI_ATTR_CALL_TIMEOUT, H->err);
646 if (H->last_err) {
647 oci_drv_error("OCIAttrGet: OCI_ATTR_CALL_TIMEOUT");
648 return FALSE;
649 }
650
651 ZVAL_LONG(return_value, (zend_long) timeout);
652 return TRUE;
653 #else
654 oci_drv_error("Unsupported attribute type");
655 return FALSE;
656 #endif
657 }
658 default:
659 return FALSE;
660
661 }
662 return FALSE;
663
664 }
665 /* }}} */
666
pdo_oci_check_liveness(pdo_dbh_t * dbh)667 static zend_result pdo_oci_check_liveness(pdo_dbh_t *dbh) /* {{{ */
668 {
669 pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
670 sb4 error_code = 0;
671 #if (!((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2))))
672 char version[256];
673 #endif
674
675 /* TODO move attached check to PDO level */
676 if (H->attached == 0) {
677 return FAILURE;
678 }
679 /* TODO add persistent_timeout check at PDO level */
680
681
682 /* Use OCIPing instead of OCIServerVersion. If OCIPing returns ORA-1010 (invalid OCI operation)
683 * such as from Pre-10.1 servers, the error is still from the server and we would have
684 * successfully performed a roundtrip and validated the connection. Use OCIServerVersion for
685 * Pre-10.2 clients
686 */
687 #if ((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2))) /* OCIPing available 10.2 onwards */
688 H->last_err = OCIPing (H->svc, H->err, OCI_DEFAULT);
689 #else
690 /* use good old OCIServerVersion() */
691 H->last_err = OCIServerVersion (H->svc, H->err, (text *)version, sizeof(version), OCI_HTYPE_SVCCTX);
692 #endif
693 if (H->last_err == OCI_SUCCESS) {
694 return SUCCESS;
695 }
696
697 OCIErrorGet (H->err, (ub4)1, NULL, &error_code, NULL, 0, OCI_HTYPE_ERROR);
698
699 if (error_code == 1010) {
700 return SUCCESS;
701 }
702 return FAILURE;
703 }
704 /* }}} */
705
706 static const struct pdo_dbh_methods oci_methods = {
707 oci_handle_closer,
708 oci_handle_preparer,
709 oci_handle_doer,
710 oci_handle_quoter,
711 oci_handle_begin,
712 oci_handle_commit,
713 oci_handle_rollback,
714 oci_handle_set_attribute,
715 NULL, /* last_id not supported */
716 pdo_oci_fetch_error_func,
717 oci_handle_get_attribute,
718 pdo_oci_check_liveness, /* check_liveness */
719 NULL, /* get_driver_methods */
720 NULL, /* request_shutdown */
721 NULL, /* in transaction, use PDO's internal tracking mechanism */
722 NULL /* get_gc */
723 };
724
pdo_oci_handle_factory(pdo_dbh_t * dbh,zval * driver_options)725 static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
726 {
727 pdo_oci_db_handle *H;
728 int i, ret = 0;
729 struct pdo_data_src_parser vars[] = {
730 { "charset", NULL, 0 },
731 { "dbname", "", 0 },
732 { "user", NULL, 0 },
733 { "password", NULL, 0 }
734 };
735
736 php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 4);
737
738 H = pecalloc(1, sizeof(*H), dbh->is_persistent);
739 dbh->driver_data = H;
740
741 dbh->skip_param_evt =
742 1 << PDO_PARAM_EVT_FETCH_PRE |
743 1 << PDO_PARAM_EVT_FETCH_POST |
744 1 << PDO_PARAM_EVT_NORMALIZE;
745
746 H->prefetch = PDO_OCI_PREFETCH_DEFAULT;
747
748 /* allocate an environment */
749 #ifdef HAVE_OCIENVNLSCREATE
750 if (vars[0].optval) {
751 H->charset = OCINlsCharSetNameToId(pdo_oci_Env, (const oratext *)vars[0].optval);
752 if (!H->charset) {
753 oci_init_error("OCINlsCharSetNameToId: unknown character set name");
754 goto cleanup;
755 } else {
756 if (OCIEnvNlsCreate(&H->env, PDO_OCI_INIT_MODE, 0, NULL, NULL, NULL, 0, NULL, H->charset, H->charset) != OCI_SUCCESS) {
757 oci_init_error("OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data");
758 goto cleanup;
759 }
760 }
761 }
762 #endif
763 if (H->env == NULL) {
764 /* use the global environment */
765 H->env = pdo_oci_Env;
766 }
767
768 /* something to hold errors */
769 OCIHandleAlloc(H->env, (dvoid **)&H->err, OCI_HTYPE_ERROR, 0, NULL);
770
771 /* handle for the server */
772 OCIHandleAlloc(H->env, (dvoid **)&H->server, OCI_HTYPE_SERVER, 0, NULL);
773
774 H->last_err = OCIServerAttach(H->server, H->err, (text*)vars[1].optval,
775 (sb4) strlen(vars[1].optval), OCI_DEFAULT);
776
777 if (H->last_err) {
778 oci_drv_error("pdo_oci_handle_factory");
779 goto cleanup;
780 }
781
782 H->attached = 1;
783
784 /* create a service context */
785 H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->svc, OCI_HTYPE_SVCCTX, 0, NULL);
786 if (H->last_err) {
787 oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SVCCTX");
788 goto cleanup;
789 }
790
791 H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->session, OCI_HTYPE_SESSION, 0, NULL);
792 if (H->last_err) {
793 oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SESSION");
794 goto cleanup;
795 }
796
797 /* set server handle into service handle */
798 H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->server, 0, OCI_ATTR_SERVER, H->err);
799 if (H->last_err) {
800 oci_drv_error("OCIAttrSet: OCI_ATTR_SERVER");
801 goto cleanup;
802 }
803
804 /* username */
805 if (!dbh->username && vars[2].optval) {
806 dbh->username = pestrdup(vars[2].optval, dbh->is_persistent);
807 }
808
809 if (dbh->username) {
810 H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
811 dbh->username, (ub4) strlen(dbh->username),
812 OCI_ATTR_USERNAME, H->err);
813 if (H->last_err) {
814 oci_drv_error("OCIAttrSet: OCI_ATTR_USERNAME");
815 goto cleanup;
816 }
817 }
818
819 /* password */
820 if (!dbh->password && vars[3].optval) {
821 dbh->password = pestrdup(vars[3].optval, dbh->is_persistent);
822 }
823
824 if (dbh->password) {
825 H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
826 dbh->password, (ub4) strlen(dbh->password),
827 OCI_ATTR_PASSWORD, H->err);
828 if (H->last_err) {
829 oci_drv_error("OCIAttrSet: OCI_ATTR_PASSWORD");
830 goto cleanup;
831 }
832 }
833
834 /* Now fire up the session */
835 H->last_err = OCISessionBegin(H->svc, H->err, H->session, OCI_CRED_RDBMS, OCI_DEFAULT);
836 if (H->last_err) {
837 oci_drv_error("OCISessionBegin");
838 /* OCISessionBegin returns OCI_SUCCESS_WITH_INFO when
839 * user's password has expired, but is still usable.
840 */
841 if (H->last_err != OCI_SUCCESS_WITH_INFO) {
842 goto cleanup;
843 }
844 }
845
846 /* set the server handle into service handle */
847 H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->session, 0, OCI_ATTR_SESSION, H->err);
848 if (H->last_err) {
849 oci_drv_error("OCIAttrSet: OCI_ATTR_SESSION");
850 goto cleanup;
851 }
852
853 /* Get max character width */
854 H->last_err = OCINlsNumericInfoGet(H->env, H->err, &H->max_char_width, OCI_NLS_CHARSET_MAXBYTESZ);
855 if (H->last_err) {
856 oci_drv_error("OCINlsNumericInfoGet: OCI_NLS_CHARSET_MAXBYTESZ");
857 goto cleanup;
858 }
859
860 dbh->methods = &oci_methods;
861 dbh->alloc_own_columns = 1;
862 dbh->native_case = PDO_CASE_UPPER;
863
864 ret = 1;
865
866 cleanup:
867 for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
868 if (vars[i].freeme) {
869 efree(vars[i].optval);
870 }
871 }
872
873 if (!ret) {
874 oci_handle_closer(dbh);
875 }
876
877 return ret;
878 }
879 /* }}} */
880
881 const pdo_driver_t pdo_oci_driver = {
882 PDO_DRIVER_HEADER(oci),
883 pdo_oci_handle_factory
884 };
885
pdo_oci_sanitize_prefetch(long prefetch)886 static inline ub4 pdo_oci_sanitize_prefetch(long prefetch) /* {{{ */
887 {
888 if (prefetch < 0) {
889 prefetch = 0;
890 } else if (prefetch > UB4MAXVAL / PDO_OCI_PREFETCH_ROWSIZE) {
891 prefetch = PDO_OCI_PREFETCH_DEFAULT;
892 }
893 return ((ub4)prefetch);
894 }
895 /* }}} */
896