sqlite3.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480
  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. | Authors: Scott MacVicar <scottmac@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "php_ini.h"
  21. #include "ext/standard/info.h"
  22. #include "php_sqlite3.h"
  23. #include "php_sqlite3_structs.h"
  24. #include "sqlite3_arginfo.h"
  25. #include "main/SAPI.h"
  26. #include <sqlite3.h>
  27. #include "zend_exceptions.h"
  28. #include "SAPI.h"
  29. ZEND_DECLARE_MODULE_GLOBALS(sqlite3)
  30. static PHP_GINIT_FUNCTION(sqlite3);
  31. static int php_sqlite3_authorizer(void *autharg, int action, const char *arg1, const char *arg2, const char *arg3, const char *arg4);
  32. static void sqlite3_param_dtor(zval *data);
  33. static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement);
  34. /* {{{ Error Handler */
  35. static void php_sqlite3_error(php_sqlite3_db_object *db_obj, char *format, ...)
  36. {
  37. va_list arg;
  38. char *message;
  39. va_start(arg, format);
  40. vspprintf(&message, 0, format, arg);
  41. va_end(arg);
  42. if (db_obj && db_obj->exception) {
  43. zend_throw_exception(zend_ce_exception, message, 0);
  44. } else {
  45. php_error_docref(NULL, E_WARNING, "%s", message);
  46. }
  47. if (message) {
  48. efree(message);
  49. }
  50. }
  51. /* }}} */
  52. #define SQLITE3_CHECK_INITIALIZED(db_obj, member, class_name) \
  53. if (!(db_obj) || !(member)) { \
  54. zend_throw_error(NULL, "The " #class_name " object has not been correctly initialised or is already closed"); \
  55. RETURN_THROWS(); \
  56. }
  57. #define SQLITE3_CHECK_INITIALIZED_STMT(member, class_name) \
  58. if (!(member)) { \
  59. zend_throw_error(NULL, "The " #class_name " object has not been correctly initialised or is already closed"); \
  60. RETURN_THROWS(); \
  61. }
  62. /* {{{ PHP_INI */
  63. PHP_INI_BEGIN()
  64. STD_PHP_INI_ENTRY("sqlite3.extension_dir", NULL, PHP_INI_SYSTEM, OnUpdateString, extension_dir, zend_sqlite3_globals, sqlite3_globals)
  65. #if SQLITE_VERSION_NUMBER >= 3026000
  66. STD_PHP_INI_BOOLEAN("sqlite3.defensive", "1", PHP_INI_SYSTEM, OnUpdateBool, dbconfig_defensive, zend_sqlite3_globals, sqlite3_globals)
  67. #endif
  68. PHP_INI_END()
  69. /* }}} */
  70. /* Handlers */
  71. static zend_object_handlers sqlite3_object_handlers;
  72. static zend_object_handlers sqlite3_stmt_object_handlers;
  73. static zend_object_handlers sqlite3_result_object_handlers;
  74. /* Class entries */
  75. zend_class_entry *php_sqlite3_sc_entry;
  76. zend_class_entry *php_sqlite3_stmt_entry;
  77. zend_class_entry *php_sqlite3_result_entry;
  78. /* {{{ Opens a SQLite 3 Database, if the build includes encryption then it will attempt to use the key. */
  79. PHP_METHOD(SQLite3, open)
  80. {
  81. php_sqlite3_db_object *db_obj;
  82. zval *object = ZEND_THIS;
  83. char *filename, *encryption_key, *fullpath;
  84. size_t filename_len, encryption_key_len = 0;
  85. zend_long flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
  86. int rc;
  87. db_obj = Z_SQLITE3_DB_P(object);
  88. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "p|ls", &filename, &filename_len, &flags, &encryption_key, &encryption_key_len)) {
  89. RETURN_THROWS();
  90. }
  91. if (db_obj->initialised) {
  92. zend_throw_exception(zend_ce_exception, "Already initialised DB Object", 0);
  93. RETURN_THROWS();
  94. }
  95. if (filename_len != 0 && (filename_len != sizeof(":memory:")-1 ||
  96. memcmp(filename, ":memory:", sizeof(":memory:")-1) != 0)) {
  97. if (!(fullpath = expand_filepath(filename, NULL))) {
  98. zend_throw_exception(zend_ce_exception, "Unable to expand filepath", 0);
  99. RETURN_THROWS();
  100. }
  101. if (php_check_open_basedir(fullpath)) {
  102. zend_throw_exception_ex(zend_ce_exception, 0, "open_basedir prohibits opening %s", fullpath);
  103. efree(fullpath);
  104. RETURN_THROWS();
  105. }
  106. } else {
  107. /* filename equals "" or ":memory:" */
  108. fullpath = filename;
  109. }
  110. rc = sqlite3_open_v2(fullpath, &(db_obj->db), flags, NULL);
  111. if (rc != SQLITE_OK) {
  112. zend_throw_exception_ex(zend_ce_exception, 0, "Unable to open database: %s",
  113. #ifdef HAVE_SQLITE3_ERRSTR
  114. db_obj->db ? sqlite3_errmsg(db_obj->db) : sqlite3_errstr(rc));
  115. #else
  116. db_obj->db ? sqlite3_errmsg(db_obj->db) : "");
  117. #endif
  118. sqlite3_close(db_obj->db);
  119. if (fullpath != filename) {
  120. efree(fullpath);
  121. }
  122. return;
  123. }
  124. #ifdef SQLITE_HAS_CODEC
  125. if (encryption_key_len > 0) {
  126. if (sqlite3_key(db_obj->db, encryption_key, encryption_key_len) != SQLITE_OK) {
  127. zend_throw_exception_ex(zend_ce_exception, 0, "Unable to open database: %s", sqlite3_errmsg(db_obj->db));
  128. sqlite3_close(db_obj->db);
  129. RETURN_THROWS();
  130. }
  131. }
  132. #endif
  133. db_obj->initialised = 1;
  134. db_obj->authorizer_fci = empty_fcall_info;
  135. db_obj->authorizer_fcc = empty_fcall_info_cache;
  136. sqlite3_set_authorizer(db_obj->db, php_sqlite3_authorizer, db_obj);
  137. #if SQLITE_VERSION_NUMBER >= 3026000
  138. if (SQLITE3G(dbconfig_defensive)) {
  139. sqlite3_db_config(db_obj->db, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
  140. }
  141. #endif
  142. if (fullpath != filename) {
  143. efree(fullpath);
  144. }
  145. }
  146. /* }}} */
  147. /* {{{ Close a SQLite 3 Database. */
  148. PHP_METHOD(SQLite3, close)
  149. {
  150. php_sqlite3_db_object *db_obj;
  151. zval *object = ZEND_THIS;
  152. int errcode;
  153. db_obj = Z_SQLITE3_DB_P(object);
  154. if (zend_parse_parameters_none() == FAILURE) {
  155. RETURN_THROWS();
  156. }
  157. if (db_obj->initialised) {
  158. zend_llist_clean(&(db_obj->free_list));
  159. if(db_obj->db) {
  160. errcode = sqlite3_close(db_obj->db);
  161. if (errcode != SQLITE_OK) {
  162. php_sqlite3_error(db_obj, "Unable to close database: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  163. RETURN_FALSE;
  164. }
  165. }
  166. db_obj->initialised = 0;
  167. }
  168. RETURN_TRUE;
  169. }
  170. /* }}} */
  171. /* {{{ Executes a result-less query against a given database. */
  172. PHP_METHOD(SQLite3, exec)
  173. {
  174. php_sqlite3_db_object *db_obj;
  175. zval *object = ZEND_THIS;
  176. zend_string *sql;
  177. char *errtext = NULL;
  178. db_obj = Z_SQLITE3_DB_P(object);
  179. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  180. RETURN_THROWS();
  181. }
  182. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  183. if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
  184. php_sqlite3_error(db_obj, "%s", errtext);
  185. sqlite3_free(errtext);
  186. RETURN_FALSE;
  187. }
  188. RETURN_TRUE;
  189. }
  190. /* }}} */
  191. /* {{{ Returns the SQLite3 Library version as a string constant and as a number. */
  192. PHP_METHOD(SQLite3, version)
  193. {
  194. if (zend_parse_parameters_none() == FAILURE) {
  195. RETURN_THROWS();
  196. }
  197. array_init(return_value);
  198. add_assoc_string(return_value, "versionString", (char*)sqlite3_libversion());
  199. add_assoc_long(return_value, "versionNumber", sqlite3_libversion_number());
  200. return;
  201. }
  202. /* }}} */
  203. /* {{{ Returns the rowid of the most recent INSERT into the database from the database connection. */
  204. PHP_METHOD(SQLite3, lastInsertRowID)
  205. {
  206. php_sqlite3_db_object *db_obj;
  207. zval *object = ZEND_THIS;
  208. db_obj = Z_SQLITE3_DB_P(object);
  209. if (zend_parse_parameters_none() == FAILURE) {
  210. RETURN_THROWS();
  211. }
  212. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  213. RETURN_LONG((zend_long) sqlite3_last_insert_rowid(db_obj->db));
  214. }
  215. /* }}} */
  216. /* {{{ Returns the numeric result code of the most recent failed sqlite API call for the database connection. */
  217. PHP_METHOD(SQLite3, lastErrorCode)
  218. {
  219. php_sqlite3_db_object *db_obj;
  220. zval *object = ZEND_THIS;
  221. db_obj = Z_SQLITE3_DB_P(object);
  222. if (zend_parse_parameters_none() == FAILURE) {
  223. RETURN_THROWS();
  224. }
  225. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  226. if (db_obj->initialised) {
  227. RETURN_LONG(sqlite3_errcode(db_obj->db));
  228. } else {
  229. RETURN_LONG(0);
  230. }
  231. }
  232. /* }}} */
  233. /* {{{ Returns the numeric extended result code of the most recent failed sqlite API call for the database connection. */
  234. PHP_METHOD(SQLite3, lastExtendedErrorCode)
  235. {
  236. php_sqlite3_db_object *db_obj;
  237. zval *object = ZEND_THIS;
  238. db_obj = Z_SQLITE3_DB_P(object);
  239. if (zend_parse_parameters_none() == FAILURE) {
  240. RETURN_THROWS();
  241. }
  242. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  243. if (db_obj->initialised) {
  244. RETURN_LONG(sqlite3_extended_errcode(db_obj->db));
  245. } else {
  246. RETURN_LONG(0);
  247. }
  248. }
  249. /* }}} */
  250. /* {{{ Turns on or off the extended result codes feature of SQLite. */
  251. PHP_METHOD(SQLite3, enableExtendedResultCodes)
  252. {
  253. php_sqlite3_db_object *db_obj;
  254. zval *object = ZEND_THIS;
  255. bool enable = 1;
  256. db_obj = Z_SQLITE3_DB_P(object);
  257. int ret;
  258. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable) == FAILURE) {
  259. RETURN_THROWS();
  260. }
  261. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  262. if (db_obj->initialised) {
  263. ret = sqlite3_extended_result_codes(db_obj->db, enable ? 1 : 0);
  264. if (ret == SQLITE_OK)
  265. {
  266. RETURN_TRUE;
  267. }
  268. }
  269. RETURN_FALSE;
  270. }
  271. /* }}} */
  272. /* {{{ Returns english text describing the most recent failed sqlite API call for the database connection. */
  273. PHP_METHOD(SQLite3, lastErrorMsg)
  274. {
  275. php_sqlite3_db_object *db_obj;
  276. zval *object = ZEND_THIS;
  277. db_obj = Z_SQLITE3_DB_P(object);
  278. if (zend_parse_parameters_none() == FAILURE) {
  279. RETURN_THROWS();
  280. }
  281. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->db, SQLite3)
  282. if (db_obj->initialised) {
  283. RETURN_STRING((char *)sqlite3_errmsg(db_obj->db));
  284. } else {
  285. RETURN_EMPTY_STRING();
  286. }
  287. }
  288. /* }}} */
  289. /* {{{ Sets a busy handler that will sleep until database is not locked or timeout is reached. Passing a value less than or equal to zero turns off all busy handlers. */
  290. PHP_METHOD(SQLite3, busyTimeout)
  291. {
  292. php_sqlite3_db_object *db_obj;
  293. zval *object = ZEND_THIS;
  294. zend_long ms;
  295. #ifdef SQLITE_ENABLE_API_ARMOR
  296. int return_code;
  297. #endif
  298. db_obj = Z_SQLITE3_DB_P(object);
  299. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ms)) {
  300. RETURN_THROWS();
  301. }
  302. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  303. #ifdef SQLITE_ENABLE_API_ARMOR
  304. return_code = sqlite3_busy_timeout(db_obj->db, ms);
  305. if (return_code != SQLITE_OK) {
  306. php_sqlite3_error(db_obj, "Unable to set busy timeout: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  307. RETURN_FALSE;
  308. }
  309. #else
  310. php_ignore_value(sqlite3_busy_timeout(db_obj->db, ms));
  311. #endif
  312. RETURN_TRUE;
  313. }
  314. /* }}} */
  315. #ifndef SQLITE_OMIT_LOAD_EXTENSION
  316. /* {{{ Attempts to load an SQLite extension library. */
  317. PHP_METHOD(SQLite3, loadExtension)
  318. {
  319. php_sqlite3_db_object *db_obj;
  320. zval *object = ZEND_THIS;
  321. char *extension, *lib_path, *extension_dir, *errtext = NULL;
  322. char fullpath[MAXPATHLEN];
  323. size_t extension_len, extension_dir_len;
  324. db_obj = Z_SQLITE3_DB_P(object);
  325. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &extension, &extension_len)) {
  326. RETURN_THROWS();
  327. }
  328. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  329. #ifdef ZTS
  330. if ((strncmp(sapi_module.name, "cgi", 3) != 0) &&
  331. (strcmp(sapi_module.name, "cli") != 0) &&
  332. (strncmp(sapi_module.name, "embed", 5) != 0)
  333. ) { php_sqlite3_error(db_obj, "Not supported in multithreaded Web servers");
  334. RETURN_FALSE;
  335. }
  336. #endif
  337. if (!SQLITE3G(extension_dir)) {
  338. php_sqlite3_error(db_obj, "SQLite Extension are disabled");
  339. RETURN_FALSE;
  340. }
  341. if (extension_len == 0) {
  342. php_sqlite3_error(db_obj, "Empty string as an extension");
  343. RETURN_FALSE;
  344. }
  345. extension_dir = SQLITE3G(extension_dir);
  346. extension_dir_len = strlen(SQLITE3G(extension_dir));
  347. if (IS_SLASH(extension_dir[extension_dir_len-1])) {
  348. spprintf(&lib_path, 0, "%s%s", extension_dir, extension);
  349. } else {
  350. spprintf(&lib_path, 0, "%s%c%s", extension_dir, DEFAULT_SLASH, extension);
  351. }
  352. if (!VCWD_REALPATH(lib_path, fullpath)) {
  353. php_sqlite3_error(db_obj, "Unable to load extension at '%s'", lib_path);
  354. efree(lib_path);
  355. RETURN_FALSE;
  356. }
  357. efree(lib_path);
  358. if (strncmp(fullpath, extension_dir, extension_dir_len) != 0) {
  359. php_sqlite3_error(db_obj, "Unable to open extensions outside the defined directory");
  360. RETURN_FALSE;
  361. }
  362. /* Extension loading should only be enabled for when we attempt to load */
  363. sqlite3_enable_load_extension(db_obj->db, 1);
  364. if (sqlite3_load_extension(db_obj->db, fullpath, 0, &errtext) != SQLITE_OK) {
  365. php_sqlite3_error(db_obj, "%s", errtext);
  366. sqlite3_free(errtext);
  367. sqlite3_enable_load_extension(db_obj->db, 0);
  368. RETURN_FALSE;
  369. }
  370. sqlite3_enable_load_extension(db_obj->db, 0);
  371. RETURN_TRUE;
  372. }
  373. /* }}} */
  374. #endif
  375. /* {{{ Returns the number of database rows that were changed (or inserted or deleted) by the most recent SQL statement. */
  376. PHP_METHOD(SQLite3, changes)
  377. {
  378. php_sqlite3_db_object *db_obj;
  379. zval *object = ZEND_THIS;
  380. db_obj = Z_SQLITE3_DB_P(object);
  381. if (zend_parse_parameters_none() == FAILURE) {
  382. RETURN_THROWS();
  383. }
  384. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  385. RETURN_LONG(sqlite3_changes(db_obj->db));
  386. }
  387. /* }}} */
  388. /* {{{ Returns a string that has been properly escaped. */
  389. PHP_METHOD(SQLite3, escapeString)
  390. {
  391. zend_string *sql;
  392. char *ret;
  393. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  394. RETURN_THROWS();
  395. }
  396. if (ZSTR_LEN(sql)) {
  397. ret = sqlite3_mprintf("%q", ZSTR_VAL(sql));
  398. if (ret) {
  399. RETVAL_STRING(ret);
  400. sqlite3_free(ret);
  401. }
  402. } else {
  403. RETURN_EMPTY_STRING();
  404. }
  405. }
  406. /* }}} */
  407. /* {{{ Returns a prepared SQL statement for execution. */
  408. PHP_METHOD(SQLite3, prepare)
  409. {
  410. php_sqlite3_db_object *db_obj;
  411. php_sqlite3_stmt *stmt_obj;
  412. zval *object = ZEND_THIS;
  413. zend_string *sql;
  414. int errcode;
  415. php_sqlite3_free_list *free_item;
  416. db_obj = Z_SQLITE3_DB_P(object);
  417. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  418. RETURN_THROWS();
  419. }
  420. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  421. if (!ZSTR_LEN(sql)) {
  422. RETURN_FALSE;
  423. }
  424. object_init_ex(return_value, php_sqlite3_stmt_entry);
  425. stmt_obj = Z_SQLITE3_STMT_P(return_value);
  426. stmt_obj->db_obj = db_obj;
  427. ZVAL_OBJ_COPY(&stmt_obj->db_obj_zval, Z_OBJ_P(object));
  428. errcode = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
  429. if (errcode != SQLITE_OK) {
  430. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  431. zval_ptr_dtor(return_value);
  432. RETURN_FALSE;
  433. }
  434. stmt_obj->initialised = 1;
  435. free_item = emalloc(sizeof(php_sqlite3_free_list));
  436. free_item->stmt_obj = stmt_obj;
  437. ZVAL_OBJ(&free_item->stmt_obj_zval, Z_OBJ_P(return_value));
  438. zend_llist_add_element(&(db_obj->free_list), &free_item);
  439. }
  440. /* }}} */
  441. /* {{{ Returns true or false, for queries that return data it will return a SQLite3Result object. */
  442. PHP_METHOD(SQLite3, query)
  443. {
  444. php_sqlite3_db_object *db_obj;
  445. php_sqlite3_result *result;
  446. php_sqlite3_stmt *stmt_obj;
  447. zval *object = ZEND_THIS;
  448. zval stmt;
  449. zend_string *sql;
  450. char *errtext = NULL;
  451. int return_code;
  452. db_obj = Z_SQLITE3_DB_P(object);
  453. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &sql)) {
  454. RETURN_THROWS();
  455. }
  456. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  457. if (!ZSTR_LEN(sql)) {
  458. RETURN_FALSE;
  459. }
  460. /* If there was no return value then just execute the query */
  461. if (!USED_RET()) {
  462. if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
  463. php_sqlite3_error(db_obj, "%s", errtext);
  464. sqlite3_free(errtext);
  465. }
  466. RETURN_FALSE;
  467. }
  468. object_init_ex(&stmt, php_sqlite3_stmt_entry);
  469. stmt_obj = Z_SQLITE3_STMT_P(&stmt);
  470. stmt_obj->db_obj = db_obj;
  471. ZVAL_OBJ_COPY(&stmt_obj->db_obj_zval, Z_OBJ_P(object));
  472. return_code = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
  473. if (return_code != SQLITE_OK) {
  474. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  475. zval_ptr_dtor(&stmt);
  476. RETURN_FALSE;
  477. }
  478. stmt_obj->initialised = 1;
  479. object_init_ex(return_value, php_sqlite3_result_entry);
  480. result = Z_SQLITE3_RESULT_P(return_value);
  481. result->db_obj = db_obj;
  482. result->stmt_obj = stmt_obj;
  483. ZVAL_OBJ(&result->stmt_obj_zval, Z_OBJ(stmt));
  484. return_code = sqlite3_step(result->stmt_obj->stmt);
  485. switch (return_code) {
  486. case SQLITE_ROW: /* Valid Row */
  487. case SQLITE_DONE: /* Valid but no results */
  488. {
  489. php_sqlite3_free_list *free_item;
  490. free_item = emalloc(sizeof(php_sqlite3_free_list));
  491. free_item->stmt_obj = stmt_obj;
  492. free_item->stmt_obj_zval = stmt;
  493. zend_llist_add_element(&(db_obj->free_list), &free_item);
  494. sqlite3_reset(result->stmt_obj->stmt);
  495. break;
  496. }
  497. default:
  498. if (!EG(exception)) {
  499. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  500. }
  501. sqlite3_finalize(stmt_obj->stmt);
  502. stmt_obj->initialised = 0;
  503. zval_ptr_dtor(return_value);
  504. RETURN_FALSE;
  505. }
  506. }
  507. /* }}} */
  508. static void sqlite_value_to_zval(sqlite3_stmt *stmt, int column, zval *data) /* {{{ */
  509. {
  510. sqlite3_int64 val;
  511. switch (sqlite3_column_type(stmt, column)) {
  512. case SQLITE_INTEGER:
  513. val = sqlite3_column_int64(stmt, column);
  514. #if LONG_MAX <= 2147483647
  515. if (val > ZEND_LONG_MAX || val < ZEND_LONG_MIN) {
  516. ZVAL_STRINGL(data, (char *)sqlite3_column_text(stmt, column), sqlite3_column_bytes(stmt, column));
  517. } else {
  518. #endif
  519. ZVAL_LONG(data, (zend_long) val);
  520. #if LONG_MAX <= 2147483647
  521. }
  522. #endif
  523. break;
  524. case SQLITE_FLOAT:
  525. ZVAL_DOUBLE(data, sqlite3_column_double(stmt, column));
  526. break;
  527. case SQLITE_NULL:
  528. ZVAL_NULL(data);
  529. break;
  530. case SQLITE3_TEXT:
  531. ZVAL_STRING(data, (char*)sqlite3_column_text(stmt, column));
  532. break;
  533. case SQLITE_BLOB:
  534. default:
  535. ZVAL_STRINGL(data, (char*)sqlite3_column_blob(stmt, column), sqlite3_column_bytes(stmt, column));
  536. }
  537. }
  538. /* }}} */
  539. /* {{{ Returns a string of the first column, or an array of the entire row. */
  540. PHP_METHOD(SQLite3, querySingle)
  541. {
  542. php_sqlite3_db_object *db_obj;
  543. zval *object = ZEND_THIS;
  544. zend_string *sql;
  545. char *errtext = NULL;
  546. int return_code;
  547. bool entire_row = 0;
  548. sqlite3_stmt *stmt;
  549. db_obj = Z_SQLITE3_DB_P(object);
  550. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "S|b", &sql, &entire_row)) {
  551. RETURN_THROWS();
  552. }
  553. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  554. if (!ZSTR_LEN(sql)) {
  555. RETURN_FALSE;
  556. }
  557. /* If there was no return value then just execute the query */
  558. if (!USED_RET()) {
  559. if (sqlite3_exec(db_obj->db, ZSTR_VAL(sql), NULL, NULL, &errtext) != SQLITE_OK) {
  560. php_sqlite3_error(db_obj, "%s", errtext);
  561. sqlite3_free(errtext);
  562. }
  563. RETURN_FALSE;
  564. }
  565. return_code = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &stmt, NULL);
  566. if (return_code != SQLITE_OK) {
  567. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", return_code, sqlite3_errmsg(db_obj->db));
  568. RETURN_FALSE;
  569. }
  570. return_code = sqlite3_step(stmt);
  571. switch (return_code) {
  572. case SQLITE_ROW: /* Valid Row */
  573. {
  574. if (!entire_row) {
  575. sqlite_value_to_zval(stmt, 0, return_value);
  576. } else {
  577. int i = 0;
  578. array_init(return_value);
  579. for (i = 0; i < sqlite3_data_count(stmt); i++) {
  580. zval data;
  581. sqlite_value_to_zval(stmt, i, &data);
  582. add_assoc_zval(return_value, (char*)sqlite3_column_name(stmt, i), &data);
  583. }
  584. }
  585. break;
  586. }
  587. case SQLITE_DONE: /* Valid but no results */
  588. {
  589. if (!entire_row) {
  590. RETVAL_NULL();
  591. } else {
  592. RETVAL_EMPTY_ARRAY();
  593. }
  594. break;
  595. }
  596. default:
  597. if (!EG(exception)) {
  598. php_sqlite3_error(db_obj, "Unable to execute statement: %s", sqlite3_errmsg(db_obj->db));
  599. }
  600. RETVAL_FALSE;
  601. }
  602. sqlite3_finalize(stmt);
  603. }
  604. /* }}} */
  605. static int sqlite3_do_callback(struct php_sqlite3_fci *fc, zval *cb, int argc, sqlite3_value **argv, sqlite3_context *context, int is_agg) /* {{{ */
  606. {
  607. zval *zargs = NULL;
  608. zval retval;
  609. int i;
  610. int ret;
  611. int fake_argc;
  612. php_sqlite3_agg_context *agg_context = NULL;
  613. if (is_agg) {
  614. is_agg = 2;
  615. }
  616. fake_argc = argc + is_agg;
  617. fc->fci.size = sizeof(fc->fci);
  618. ZVAL_COPY_VALUE(&fc->fci.function_name, cb);
  619. fc->fci.object = NULL;
  620. fc->fci.retval = &retval;
  621. fc->fci.param_count = fake_argc;
  622. /* build up the params */
  623. if (fake_argc) {
  624. zargs = (zval *)safe_emalloc(fake_argc, sizeof(zval), 0);
  625. }
  626. if (is_agg) {
  627. /* summon the aggregation context */
  628. agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  629. if (Z_ISUNDEF(agg_context->zval_context)) {
  630. ZVAL_NULL(&agg_context->zval_context);
  631. }
  632. ZVAL_COPY(&zargs[0], &agg_context->zval_context);
  633. ZVAL_LONG(&zargs[1], agg_context->row_count);
  634. }
  635. for (i = 0; i < argc; i++) {
  636. switch (sqlite3_value_type(argv[i])) {
  637. case SQLITE_INTEGER:
  638. #if ZEND_LONG_MAX > 2147483647
  639. ZVAL_LONG(&zargs[i + is_agg], sqlite3_value_int64(argv[i]));
  640. #else
  641. ZVAL_LONG(&zargs[i + is_agg], sqlite3_value_int(argv[i]));
  642. #endif
  643. break;
  644. case SQLITE_FLOAT:
  645. ZVAL_DOUBLE(&zargs[i + is_agg], sqlite3_value_double(argv[i]));
  646. break;
  647. case SQLITE_NULL:
  648. ZVAL_NULL(&zargs[i + is_agg]);
  649. break;
  650. case SQLITE_BLOB:
  651. case SQLITE3_TEXT:
  652. default:
  653. ZVAL_STRINGL(&zargs[i + is_agg], (char*)sqlite3_value_text(argv[i]), sqlite3_value_bytes(argv[i]));
  654. break;
  655. }
  656. }
  657. fc->fci.params = zargs;
  658. if ((ret = zend_call_function(&fc->fci, &fc->fcc)) == FAILURE) {
  659. php_error_docref(NULL, E_WARNING, "An error occurred while invoking the callback");
  660. }
  661. if (is_agg) {
  662. zval_ptr_dtor(&zargs[0]);
  663. }
  664. /* clean up the params */
  665. if (fake_argc) {
  666. for (i = is_agg; i < argc + is_agg; i++) {
  667. zval_ptr_dtor(&zargs[i]);
  668. }
  669. if (is_agg) {
  670. zval_ptr_dtor(&zargs[1]);
  671. }
  672. efree(zargs);
  673. }
  674. if (!is_agg || !argv) {
  675. /* only set the sqlite return value if we are a scalar function,
  676. * or if we are finalizing an aggregate */
  677. if (!Z_ISUNDEF(retval)) {
  678. switch (Z_TYPE(retval)) {
  679. case IS_LONG:
  680. #if ZEND_LONG_MAX > 2147483647
  681. sqlite3_result_int64(context, Z_LVAL(retval));
  682. #else
  683. sqlite3_result_int(context, Z_LVAL(retval));
  684. #endif
  685. break;
  686. case IS_NULL:
  687. sqlite3_result_null(context);
  688. break;
  689. case IS_DOUBLE:
  690. sqlite3_result_double(context, Z_DVAL(retval));
  691. break;
  692. default: {
  693. zend_string *str = zval_try_get_string(&retval);
  694. if (UNEXPECTED(!str)) {
  695. ret = FAILURE;
  696. break;
  697. }
  698. sqlite3_result_text(context, ZSTR_VAL(str), ZSTR_LEN(str), SQLITE_TRANSIENT);
  699. zend_string_release(str);
  700. break;
  701. }
  702. }
  703. } else {
  704. sqlite3_result_error(context, "failed to invoke callback", 0);
  705. }
  706. if (agg_context && !Z_ISUNDEF(agg_context->zval_context)) {
  707. zval_ptr_dtor(&agg_context->zval_context);
  708. }
  709. } else {
  710. /* we're stepping in an aggregate; the return value goes into
  711. * the context */
  712. if (agg_context && !Z_ISUNDEF(agg_context->zval_context)) {
  713. zval_ptr_dtor(&agg_context->zval_context);
  714. }
  715. ZVAL_COPY_VALUE(&agg_context->zval_context, &retval);
  716. ZVAL_UNDEF(&retval);
  717. }
  718. if (!Z_ISUNDEF(retval)) {
  719. zval_ptr_dtor(&retval);
  720. }
  721. return ret;
  722. }
  723. /* }}}*/
  724. static void php_sqlite3_callback_func(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  725. {
  726. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  727. sqlite3_do_callback(&func->afunc, &func->func, argc, argv, context, 0);
  728. }
  729. /* }}}*/
  730. static void php_sqlite3_callback_step(sqlite3_context *context, int argc, sqlite3_value **argv) /* {{{ */
  731. {
  732. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  733. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  734. agg_context->row_count++;
  735. sqlite3_do_callback(&func->astep, &func->step, argc, argv, context, 1);
  736. }
  737. /* }}} */
  738. static void php_sqlite3_callback_final(sqlite3_context *context) /* {{{ */
  739. {
  740. php_sqlite3_func *func = (php_sqlite3_func *)sqlite3_user_data(context);
  741. php_sqlite3_agg_context *agg_context = (php_sqlite3_agg_context *)sqlite3_aggregate_context(context, sizeof(php_sqlite3_agg_context));
  742. agg_context->row_count = 0;
  743. sqlite3_do_callback(&func->afini, &func->fini, 0, NULL, context, 1);
  744. }
  745. /* }}} */
  746. static int php_sqlite3_callback_compare(void *coll, int a_len, const void *a, int b_len, const void* b) /* {{{ */
  747. {
  748. php_sqlite3_collation *collation = (php_sqlite3_collation*)coll;
  749. zval zargs[2];
  750. zval retval;
  751. int ret;
  752. // Exception occurred on previous callback. Don't attempt to call function.
  753. if (EG(exception)) {
  754. return 0;
  755. }
  756. collation->fci.fci.size = (sizeof(collation->fci.fci));
  757. ZVAL_COPY_VALUE(&collation->fci.fci.function_name, &collation->cmp_func);
  758. collation->fci.fci.object = NULL;
  759. collation->fci.fci.retval = &retval;
  760. collation->fci.fci.param_count = 2;
  761. ZVAL_STRINGL(&zargs[0], a, a_len);
  762. ZVAL_STRINGL(&zargs[1], b, b_len);
  763. collation->fci.fci.params = zargs;
  764. if ((ret = zend_call_function(&collation->fci.fci, &collation->fci.fcc)) == FAILURE) {
  765. php_error_docref(NULL, E_WARNING, "An error occurred while invoking the compare callback");
  766. }
  767. zval_ptr_dtor(&zargs[0]);
  768. zval_ptr_dtor(&zargs[1]);
  769. if (EG(exception)) {
  770. ret = 0;
  771. } else if (Z_TYPE(retval) != IS_LONG){
  772. //retval ought to contain a ZVAL_LONG by now
  773. // (the result of a comparison, i.e. most likely -1, 0, or 1)
  774. //I suppose we could accept any scalar return type, though.
  775. php_error_docref(NULL, E_WARNING, "An error occurred while invoking the compare callback (invalid return type). Collation behaviour is undefined.");
  776. } else {
  777. ret = Z_LVAL(retval);
  778. }
  779. zval_ptr_dtor(&retval);
  780. return ret;
  781. }
  782. /* }}} */
  783. /* {{{ Allows registration of a PHP function as a SQLite UDF that can be called within SQL statements. */
  784. PHP_METHOD(SQLite3, createFunction)
  785. {
  786. php_sqlite3_db_object *db_obj;
  787. zval *object = ZEND_THIS;
  788. php_sqlite3_func *func;
  789. char *sql_func;
  790. size_t sql_func_len;
  791. zend_fcall_info fci;
  792. zend_fcall_info_cache fcc;
  793. zend_long sql_func_num_args = -1;
  794. zend_long flags = 0;
  795. db_obj = Z_SQLITE3_DB_P(object);
  796. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sf|ll", &sql_func, &sql_func_len, &fci, &fcc, &sql_func_num_args, &flags) == FAILURE) {
  797. RETURN_THROWS();
  798. }
  799. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  800. if (!sql_func_len) {
  801. RETURN_FALSE;
  802. }
  803. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  804. if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, flags | SQLITE_UTF8, func, php_sqlite3_callback_func, NULL, NULL) == SQLITE_OK) {
  805. func->func_name = estrdup(sql_func);
  806. ZVAL_COPY(&func->func, &fci.function_name);
  807. func->argc = sql_func_num_args;
  808. func->next = db_obj->funcs;
  809. db_obj->funcs = func;
  810. RETURN_TRUE;
  811. }
  812. efree(func);
  813. RETURN_FALSE;
  814. }
  815. /* }}} */
  816. /* {{{ Allows registration of a PHP function for use as an aggregate. */
  817. PHP_METHOD(SQLite3, createAggregate)
  818. {
  819. php_sqlite3_db_object *db_obj;
  820. zval *object = ZEND_THIS;
  821. php_sqlite3_func *func;
  822. char *sql_func;
  823. size_t sql_func_len;
  824. zend_fcall_info step_fci, fini_fci;
  825. zend_fcall_info_cache step_fcc, fini_fcc;
  826. zend_long sql_func_num_args = -1;
  827. db_obj = Z_SQLITE3_DB_P(object);
  828. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sff|l", &sql_func, &sql_func_len, &step_fci, &step_fcc, &fini_fci, &fini_fcc, &sql_func_num_args) == FAILURE) {
  829. RETURN_THROWS();
  830. }
  831. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  832. if (!sql_func_len) {
  833. RETURN_FALSE;
  834. }
  835. func = (php_sqlite3_func *)ecalloc(1, sizeof(*func));
  836. if (sqlite3_create_function(db_obj->db, sql_func, sql_func_num_args, SQLITE_UTF8, func, NULL, php_sqlite3_callback_step, php_sqlite3_callback_final) == SQLITE_OK) {
  837. func->func_name = estrdup(sql_func);
  838. ZVAL_COPY(&func->step, &step_fci.function_name);
  839. ZVAL_COPY(&func->fini, &fini_fci.function_name);
  840. func->argc = sql_func_num_args;
  841. func->next = db_obj->funcs;
  842. db_obj->funcs = func;
  843. RETURN_TRUE;
  844. }
  845. efree(func);
  846. RETURN_FALSE;
  847. }
  848. /* }}} */
  849. /* {{{ Registers a PHP function as a comparator that can be used with the SQL COLLATE operator. Callback must accept two strings and return an integer (as strcmp()). */
  850. PHP_METHOD(SQLite3, createCollation)
  851. {
  852. php_sqlite3_db_object *db_obj;
  853. zval *object = ZEND_THIS;
  854. php_sqlite3_collation *collation;
  855. char *collation_name;
  856. size_t collation_name_len;
  857. zend_fcall_info fci;
  858. zend_fcall_info_cache fcc;
  859. db_obj = Z_SQLITE3_DB_P(object);
  860. if (zend_parse_parameters(ZEND_NUM_ARGS(), "sf", &collation_name, &collation_name_len, &fci, &fcc) == FAILURE) {
  861. RETURN_THROWS();
  862. }
  863. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  864. if (!collation_name_len) {
  865. RETURN_FALSE;
  866. }
  867. collation = (php_sqlite3_collation *)ecalloc(1, sizeof(*collation));
  868. if (sqlite3_create_collation(db_obj->db, collation_name, SQLITE_UTF8, collation, php_sqlite3_callback_compare) == SQLITE_OK) {
  869. collation->collation_name = estrdup(collation_name);
  870. ZVAL_COPY(&collation->cmp_func, &fci.function_name);
  871. collation->next = db_obj->collations;
  872. db_obj->collations = collation;
  873. RETURN_TRUE;
  874. }
  875. efree(collation);
  876. RETURN_FALSE;
  877. }
  878. /* }}} */
  879. typedef struct {
  880. sqlite3_blob *blob;
  881. size_t position;
  882. size_t size;
  883. int flags;
  884. } php_stream_sqlite3_data;
  885. static ssize_t php_sqlite3_stream_write(php_stream *stream, const char *buf, size_t count)
  886. {
  887. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  888. if (sqlite3_stream->flags & SQLITE_OPEN_READONLY) {
  889. php_error_docref(NULL, E_WARNING, "Can't write to blob stream: is open as read only");
  890. return -1;
  891. }
  892. if (sqlite3_stream->position + count > sqlite3_stream->size) {
  893. php_error_docref(NULL, E_WARNING, "It is not possible to increase the size of a BLOB");
  894. return -1;
  895. }
  896. if (sqlite3_blob_write(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
  897. return -1;
  898. }
  899. if (sqlite3_stream->position + count >= sqlite3_stream->size) {
  900. stream->eof = 1;
  901. sqlite3_stream->position = sqlite3_stream->size;
  902. }
  903. else {
  904. sqlite3_stream->position += count;
  905. }
  906. return count;
  907. }
  908. static ssize_t php_sqlite3_stream_read(php_stream *stream, char *buf, size_t count)
  909. {
  910. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  911. if (sqlite3_stream->position + count >= sqlite3_stream->size) {
  912. count = sqlite3_stream->size - sqlite3_stream->position;
  913. stream->eof = 1;
  914. }
  915. if (count) {
  916. if (sqlite3_blob_read(sqlite3_stream->blob, buf, count, sqlite3_stream->position) != SQLITE_OK) {
  917. return -1;
  918. }
  919. sqlite3_stream->position += count;
  920. }
  921. return count;
  922. }
  923. static int php_sqlite3_stream_close(php_stream *stream, int close_handle)
  924. {
  925. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  926. if (sqlite3_blob_close(sqlite3_stream->blob) != SQLITE_OK) {
  927. /* Error occurred, but it still closed */
  928. }
  929. efree(sqlite3_stream);
  930. return 0;
  931. }
  932. static int php_sqlite3_stream_flush(php_stream *stream)
  933. {
  934. /* do nothing */
  935. return 0;
  936. }
  937. /* {{{ */
  938. static int php_sqlite3_stream_seek(php_stream *stream, zend_off_t offset, int whence, zend_off_t *newoffs)
  939. {
  940. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  941. switch(whence) {
  942. case SEEK_CUR:
  943. if (offset < 0) {
  944. if (sqlite3_stream->position < (size_t)(-offset)) {
  945. sqlite3_stream->position = 0;
  946. *newoffs = -1;
  947. return -1;
  948. } else {
  949. sqlite3_stream->position = sqlite3_stream->position + offset;
  950. *newoffs = sqlite3_stream->position;
  951. stream->eof = 0;
  952. return 0;
  953. }
  954. } else {
  955. if (sqlite3_stream->position + (size_t)(offset) > sqlite3_stream->size) {
  956. sqlite3_stream->position = sqlite3_stream->size;
  957. *newoffs = -1;
  958. return -1;
  959. } else {
  960. sqlite3_stream->position = sqlite3_stream->position + offset;
  961. *newoffs = sqlite3_stream->position;
  962. stream->eof = 0;
  963. return 0;
  964. }
  965. }
  966. case SEEK_SET:
  967. if (sqlite3_stream->size < (size_t)(offset)) {
  968. sqlite3_stream->position = sqlite3_stream->size;
  969. *newoffs = -1;
  970. return -1;
  971. } else {
  972. sqlite3_stream->position = offset;
  973. *newoffs = sqlite3_stream->position;
  974. stream->eof = 0;
  975. return 0;
  976. }
  977. case SEEK_END:
  978. if (offset > 0) {
  979. sqlite3_stream->position = sqlite3_stream->size;
  980. *newoffs = -1;
  981. return -1;
  982. } else if (sqlite3_stream->size < (size_t)(-offset)) {
  983. sqlite3_stream->position = 0;
  984. *newoffs = -1;
  985. return -1;
  986. } else {
  987. sqlite3_stream->position = sqlite3_stream->size + offset;
  988. *newoffs = sqlite3_stream->position;
  989. stream->eof = 0;
  990. return 0;
  991. }
  992. default:
  993. *newoffs = sqlite3_stream->position;
  994. return -1;
  995. }
  996. }
  997. /* }}} */
  998. static int php_sqlite3_stream_cast(php_stream *stream, int castas, void **ret)
  999. {
  1000. return FAILURE;
  1001. }
  1002. static int php_sqlite3_stream_stat(php_stream *stream, php_stream_statbuf *ssb)
  1003. {
  1004. php_stream_sqlite3_data *sqlite3_stream = (php_stream_sqlite3_data *) stream->abstract;
  1005. ssb->sb.st_size = sqlite3_stream->size;
  1006. return 0;
  1007. }
  1008. static const php_stream_ops php_stream_sqlite3_ops = {
  1009. php_sqlite3_stream_write,
  1010. php_sqlite3_stream_read,
  1011. php_sqlite3_stream_close,
  1012. php_sqlite3_stream_flush,
  1013. "SQLite3",
  1014. php_sqlite3_stream_seek,
  1015. php_sqlite3_stream_cast,
  1016. php_sqlite3_stream_stat,
  1017. NULL
  1018. };
  1019. /* {{{ Open a blob as a stream which we can read / write to. */
  1020. PHP_METHOD(SQLite3, openBlob)
  1021. {
  1022. php_sqlite3_db_object *db_obj;
  1023. zval *object = ZEND_THIS;
  1024. char *table, *column, *dbname = "main", *mode = "rb";
  1025. size_t table_len, column_len, dbname_len;
  1026. zend_long rowid, flags = SQLITE_OPEN_READONLY, sqlite_flags = 0;
  1027. sqlite3_blob *blob = NULL;
  1028. php_stream_sqlite3_data *sqlite3_stream;
  1029. php_stream *stream;
  1030. db_obj = Z_SQLITE3_DB_P(object);
  1031. if (zend_parse_parameters(ZEND_NUM_ARGS(), "ssl|pl", &table, &table_len, &column, &column_len, &rowid, &dbname, &dbname_len, &flags) == FAILURE) {
  1032. RETURN_THROWS();
  1033. }
  1034. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1035. sqlite_flags = (flags & SQLITE_OPEN_READWRITE) ? 1 : 0;
  1036. if (sqlite3_blob_open(db_obj->db, dbname, table, column, rowid, sqlite_flags, &blob) != SQLITE_OK) {
  1037. php_sqlite3_error(db_obj, "Unable to open blob: %s", sqlite3_errmsg(db_obj->db));
  1038. RETURN_FALSE;
  1039. }
  1040. sqlite3_stream = emalloc(sizeof(php_stream_sqlite3_data));
  1041. sqlite3_stream->blob = blob;
  1042. sqlite3_stream->flags = flags;
  1043. sqlite3_stream->position = 0;
  1044. sqlite3_stream->size = sqlite3_blob_bytes(blob);
  1045. if (sqlite_flags != 0) {
  1046. mode = "r+b";
  1047. }
  1048. stream = php_stream_alloc(&php_stream_sqlite3_ops, sqlite3_stream, 0, mode);
  1049. if (stream) {
  1050. php_stream_to_zval(stream, return_value);
  1051. } else {
  1052. RETURN_FALSE;
  1053. }
  1054. }
  1055. /* }}} */
  1056. /* {{{ Enables an exception error mode. */
  1057. PHP_METHOD(SQLite3, enableExceptions)
  1058. {
  1059. php_sqlite3_db_object *db_obj;
  1060. zval *object = ZEND_THIS;
  1061. bool enableExceptions = 0;
  1062. db_obj = Z_SQLITE3_DB_P(object);
  1063. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enableExceptions) == FAILURE) {
  1064. RETURN_THROWS();
  1065. }
  1066. RETVAL_BOOL(db_obj->exception);
  1067. db_obj->exception = enableExceptions;
  1068. }
  1069. /* }}} */
  1070. /* {{{ Register a callback function to be used as an authorizer by SQLite. The callback should return SQLite3::OK, SQLite3::IGNORE or SQLite3::DENY. */
  1071. PHP_METHOD(SQLite3, setAuthorizer)
  1072. {
  1073. php_sqlite3_db_object *db_obj;
  1074. zval *object = ZEND_THIS;
  1075. db_obj = Z_SQLITE3_DB_P(object);
  1076. zend_fcall_info fci;
  1077. zend_fcall_info_cache fcc;
  1078. ZEND_PARSE_PARAMETERS_START(1, 1)
  1079. Z_PARAM_FUNC_OR_NULL(fci, fcc)
  1080. ZEND_PARSE_PARAMETERS_END();
  1081. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1082. /* Clear previously set callback */
  1083. if (ZEND_FCI_INITIALIZED(db_obj->authorizer_fci)) {
  1084. zval_ptr_dtor(&db_obj->authorizer_fci.function_name);
  1085. db_obj->authorizer_fci.size = 0;
  1086. }
  1087. /* Only enable userland authorizer if argument is not NULL */
  1088. if (ZEND_FCI_INITIALIZED(fci)) {
  1089. db_obj->authorizer_fci = fci;
  1090. Z_ADDREF(db_obj->authorizer_fci.function_name);
  1091. db_obj->authorizer_fcc = fcc;
  1092. }
  1093. RETURN_TRUE;
  1094. }
  1095. /* }}} */
  1096. #if SQLITE_VERSION_NUMBER >= 3006011
  1097. /* {{{ Backups the current database to another one. */
  1098. PHP_METHOD(SQLite3, backup)
  1099. {
  1100. php_sqlite3_db_object *source_obj;
  1101. php_sqlite3_db_object *destination_obj;
  1102. char *source_dbname = "main", *destination_dbname = "main";
  1103. size_t source_dbname_length, destination_dbname_length;
  1104. zval *source_zval = ZEND_THIS;
  1105. zval *destination_zval;
  1106. sqlite3_backup *dbBackup;
  1107. int rc; // Return code
  1108. if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|pp", &destination_zval, php_sqlite3_sc_entry, &source_dbname, &source_dbname_length, &destination_dbname, &destination_dbname_length) == FAILURE) {
  1109. RETURN_THROWS();
  1110. }
  1111. source_obj = Z_SQLITE3_DB_P(source_zval);
  1112. SQLITE3_CHECK_INITIALIZED(source_obj, source_obj->initialised, SQLite3)
  1113. destination_obj = Z_SQLITE3_DB_P(destination_zval);
  1114. SQLITE3_CHECK_INITIALIZED(destination_obj, destination_obj->initialised, SQLite3)
  1115. dbBackup = sqlite3_backup_init(destination_obj->db, destination_dbname, source_obj->db, source_dbname);
  1116. if (dbBackup) {
  1117. do {
  1118. rc = sqlite3_backup_step(dbBackup, -1);
  1119. } while (rc == SQLITE_OK);
  1120. /* Release resources allocated by backup_init(). */
  1121. rc = sqlite3_backup_finish(dbBackup);
  1122. }
  1123. else {
  1124. rc = sqlite3_errcode(source_obj->db);
  1125. }
  1126. if (rc != SQLITE_OK) {
  1127. if (rc == SQLITE_BUSY) {
  1128. php_sqlite3_error(source_obj, "Backup failed: source database is busy");
  1129. }
  1130. else if (rc == SQLITE_LOCKED) {
  1131. php_sqlite3_error(source_obj, "Backup failed: source database is locked");
  1132. }
  1133. else {
  1134. php_sqlite3_error(source_obj, "Backup failed: %d, %s", rc, sqlite3_errmsg(source_obj->db));
  1135. }
  1136. RETURN_FALSE;
  1137. }
  1138. RETURN_TRUE;
  1139. }
  1140. /* }}} */
  1141. #endif
  1142. /* {{{ Returns the number of parameters within the prepared statement. */
  1143. PHP_METHOD(SQLite3Stmt, paramCount)
  1144. {
  1145. php_sqlite3_stmt *stmt_obj;
  1146. zval *object = ZEND_THIS;
  1147. stmt_obj = Z_SQLITE3_STMT_P(object);
  1148. ZEND_PARSE_PARAMETERS_NONE();
  1149. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1150. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1151. RETURN_LONG(sqlite3_bind_parameter_count(stmt_obj->stmt));
  1152. }
  1153. /* }}} */
  1154. /* {{{ Closes the prepared statement. */
  1155. PHP_METHOD(SQLite3Stmt, close)
  1156. {
  1157. php_sqlite3_stmt *stmt_obj;
  1158. zval *object = ZEND_THIS;
  1159. stmt_obj = Z_SQLITE3_STMT_P(object);
  1160. ZEND_PARSE_PARAMETERS_NONE();
  1161. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1162. if(stmt_obj->db_obj) {
  1163. zend_llist_del_element(&(stmt_obj->db_obj->free_list), object, (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  1164. }
  1165. RETURN_TRUE;
  1166. }
  1167. /* }}} */
  1168. /* {{{ Reset the prepared statement to the state before it was executed, bindings still remain. */
  1169. PHP_METHOD(SQLite3Stmt, reset)
  1170. {
  1171. php_sqlite3_stmt *stmt_obj;
  1172. zval *object = ZEND_THIS;
  1173. stmt_obj = Z_SQLITE3_STMT_P(object);
  1174. ZEND_PARSE_PARAMETERS_NONE();
  1175. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1176. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1177. if (sqlite3_reset(stmt_obj->stmt) != SQLITE_OK) {
  1178. php_sqlite3_error(stmt_obj->db_obj, "Unable to reset statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1179. RETURN_FALSE;
  1180. }
  1181. RETURN_TRUE;
  1182. }
  1183. /* }}} */
  1184. /* {{{ Clear all current bound parameters. */
  1185. PHP_METHOD(SQLite3Stmt, clear)
  1186. {
  1187. php_sqlite3_stmt *stmt_obj;
  1188. zval *object = ZEND_THIS;
  1189. stmt_obj = Z_SQLITE3_STMT_P(object);
  1190. ZEND_PARSE_PARAMETERS_NONE();
  1191. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1192. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1193. if (sqlite3_clear_bindings(stmt_obj->stmt) != SQLITE_OK) {
  1194. php_sqlite3_error(stmt_obj->db_obj, "Unable to clear statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1195. RETURN_FALSE;
  1196. }
  1197. if (stmt_obj->bound_params) {
  1198. zend_hash_destroy(stmt_obj->bound_params);
  1199. FREE_HASHTABLE(stmt_obj->bound_params);
  1200. stmt_obj->bound_params = NULL;
  1201. }
  1202. RETURN_TRUE;
  1203. }
  1204. /* }}} */
  1205. /* {{{ Returns true if a statement is definitely read only */
  1206. PHP_METHOD(SQLite3Stmt, readOnly)
  1207. {
  1208. php_sqlite3_stmt *stmt_obj;
  1209. zval *object = ZEND_THIS;
  1210. stmt_obj = Z_SQLITE3_STMT_P(object);
  1211. ZEND_PARSE_PARAMETERS_NONE();
  1212. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1213. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1214. if (sqlite3_stmt_readonly(stmt_obj->stmt)) {
  1215. RETURN_TRUE;
  1216. }
  1217. RETURN_FALSE;
  1218. }
  1219. /* }}} */
  1220. /* bind parameters to a statement before execution */
  1221. static int php_sqlite3_bind_params(php_sqlite3_stmt *stmt_obj) /* {{{ */
  1222. {
  1223. struct php_sqlite3_bound_param *param;
  1224. int return_code;
  1225. if (stmt_obj->bound_params) {
  1226. ZEND_HASH_FOREACH_PTR(stmt_obj->bound_params, param) {
  1227. zval *parameter;
  1228. /* parameter must be a reference? */
  1229. if (Z_ISREF(param->parameter)) {
  1230. parameter = Z_REFVAL(param->parameter);
  1231. } else {
  1232. parameter = &param->parameter;
  1233. }
  1234. /* If the ZVAL is null then it should be bound as that */
  1235. if (Z_TYPE_P(parameter) == IS_NULL) {
  1236. return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1237. if (return_code != SQLITE_OK) {
  1238. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1239. }
  1240. continue;
  1241. }
  1242. switch (param->type) {
  1243. case SQLITE_INTEGER:
  1244. convert_to_long(parameter);
  1245. #if ZEND_LONG_MAX > 2147483647
  1246. return_code = sqlite3_bind_int64(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
  1247. #else
  1248. return_code = sqlite3_bind_int(stmt_obj->stmt, param->param_number, Z_LVAL_P(parameter));
  1249. #endif
  1250. if (return_code != SQLITE_OK) {
  1251. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1252. }
  1253. break;
  1254. case SQLITE_FLOAT:
  1255. convert_to_double(parameter);
  1256. return_code = sqlite3_bind_double(stmt_obj->stmt, param->param_number, Z_DVAL_P(parameter));
  1257. if (return_code != SQLITE_OK) {
  1258. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1259. }
  1260. break;
  1261. case SQLITE_BLOB:
  1262. {
  1263. php_stream *stream = NULL;
  1264. zend_string *buffer = NULL;
  1265. if (Z_TYPE_P(parameter) == IS_RESOURCE) {
  1266. php_stream_from_zval_no_verify(stream, parameter);
  1267. if (stream == NULL) {
  1268. php_sqlite3_error(stmt_obj->db_obj, "Unable to read stream for parameter %ld", param->param_number);
  1269. return FAILURE;
  1270. }
  1271. buffer = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  1272. } else {
  1273. buffer = zval_get_string(parameter);
  1274. }
  1275. if (buffer) {
  1276. return_code = sqlite3_bind_blob(stmt_obj->stmt, param->param_number, ZSTR_VAL(buffer), ZSTR_LEN(buffer), SQLITE_TRANSIENT);
  1277. zend_string_release_ex(buffer, 0);
  1278. if (return_code != SQLITE_OK) {
  1279. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1280. }
  1281. } else {
  1282. return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1283. if (return_code != SQLITE_OK) {
  1284. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1285. }
  1286. }
  1287. break;
  1288. }
  1289. case SQLITE3_TEXT: {
  1290. zend_string *str = zval_try_get_string(parameter);
  1291. if (UNEXPECTED(!str)) {
  1292. return FAILURE;
  1293. }
  1294. return_code = sqlite3_bind_text(stmt_obj->stmt, param->param_number, ZSTR_VAL(str), ZSTR_LEN(str), SQLITE_TRANSIENT);
  1295. if (return_code != SQLITE_OK) {
  1296. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1297. }
  1298. zend_string_release(str);
  1299. break;
  1300. }
  1301. case SQLITE_NULL:
  1302. return_code = sqlite3_bind_null(stmt_obj->stmt, param->param_number);
  1303. if (return_code != SQLITE_OK) {
  1304. php_sqlite3_error(stmt_obj->db_obj, "Unable to bind parameter number " ZEND_LONG_FMT " (%d)", param->param_number, return_code);
  1305. }
  1306. break;
  1307. default:
  1308. php_sqlite3_error(stmt_obj->db_obj, "Unknown parameter type: %pd for parameter %pd", param->type, param->param_number);
  1309. return FAILURE;
  1310. }
  1311. } ZEND_HASH_FOREACH_END();
  1312. }
  1313. return SUCCESS;
  1314. }
  1315. /* }}} */
  1316. /* {{{ Returns the SQL statement used to prepare the query. If expanded is true, binded parameters and values will be expanded. */
  1317. PHP_METHOD(SQLite3Stmt, getSQL)
  1318. {
  1319. php_sqlite3_stmt *stmt_obj;
  1320. bool expanded = 0;
  1321. zval *object = getThis();
  1322. stmt_obj = Z_SQLITE3_STMT_P(object);
  1323. int bind_rc;
  1324. ZEND_PARSE_PARAMETERS_START(0, 1)
  1325. Z_PARAM_OPTIONAL
  1326. Z_PARAM_BOOL(expanded)
  1327. ZEND_PARSE_PARAMETERS_END();
  1328. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1329. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1330. bind_rc = php_sqlite3_bind_params(stmt_obj);
  1331. if (bind_rc == FAILURE || EG(exception)) {
  1332. RETURN_FALSE;
  1333. }
  1334. if (expanded) {
  1335. #ifdef HAVE_SQLITE3_EXPANDED_SQL
  1336. char *sql = sqlite3_expanded_sql(stmt_obj->stmt);
  1337. RETVAL_STRING(sql);
  1338. sqlite3_free(sql);
  1339. #else
  1340. php_sqlite3_error(stmt_obj->db_obj, "The expanded parameter requires SQLite3 >= 3.14 and %s is installed", sqlite3_libversion());
  1341. RETURN_FALSE;
  1342. #endif
  1343. } else {
  1344. const char *sql = sqlite3_sql(stmt_obj->stmt);
  1345. RETVAL_STRING(sql);
  1346. }
  1347. }
  1348. /* }}} */
  1349. static int register_bound_parameter_to_sqlite(struct php_sqlite3_bound_param *param, php_sqlite3_stmt *stmt) /* {{{ */
  1350. {
  1351. HashTable *hash;
  1352. hash = stmt->bound_params;
  1353. if (!hash) {
  1354. ALLOC_HASHTABLE(hash);
  1355. zend_hash_init(hash, 13, NULL, sqlite3_param_dtor, 0);
  1356. stmt->bound_params = hash;
  1357. }
  1358. /* We need a : prefix to resolve a name to a parameter number */
  1359. if (param->name) {
  1360. if (ZSTR_VAL(param->name)[0] != ':' && ZSTR_VAL(param->name)[0] != '@') {
  1361. /* pre-increment for character + 1 for null */
  1362. zend_string *temp = zend_string_alloc(ZSTR_LEN(param->name) + 1, 0);
  1363. ZSTR_VAL(temp)[0] = ':';
  1364. memmove(ZSTR_VAL(temp) + 1, ZSTR_VAL(param->name), ZSTR_LEN(param->name) + 1);
  1365. param->name = temp;
  1366. } else {
  1367. param->name = zend_string_copy(param->name);
  1368. }
  1369. /* do lookup*/
  1370. param->param_number = sqlite3_bind_parameter_index(stmt->stmt, ZSTR_VAL(param->name));
  1371. }
  1372. if (param->param_number < 1) {
  1373. if (param->name) {
  1374. zend_string_release_ex(param->name, 0);
  1375. }
  1376. return 0;
  1377. }
  1378. if (param->param_number >= 1) {
  1379. zend_hash_index_del(hash, param->param_number);
  1380. }
  1381. if (param->name) {
  1382. zend_hash_update_mem(hash, param->name, param, sizeof(struct php_sqlite3_bound_param));
  1383. } else {
  1384. zend_hash_index_update_mem(hash, param->param_number, param, sizeof(struct php_sqlite3_bound_param));
  1385. }
  1386. return 1;
  1387. }
  1388. /* }}} */
  1389. /* {{{ Best try to map between PHP and SQLite. Default is still text. */
  1390. #define PHP_SQLITE3_SET_TYPE(z, p) \
  1391. switch (Z_TYPE_P(z)) { \
  1392. default: \
  1393. (p).type = SQLITE_TEXT; \
  1394. break; \
  1395. case IS_LONG: \
  1396. case IS_TRUE: \
  1397. case IS_FALSE: \
  1398. (p).type = SQLITE_INTEGER; \
  1399. break; \
  1400. case IS_DOUBLE: \
  1401. (p).type = SQLITE_FLOAT; \
  1402. break; \
  1403. case IS_NULL: \
  1404. (p).type = SQLITE_NULL; \
  1405. break; \
  1406. }
  1407. /* }}} */
  1408. /* {{{ Common implementation of ::bindParam() and ::bindValue */
  1409. static void sqlite3stmt_bind(INTERNAL_FUNCTION_PARAMETERS)
  1410. {
  1411. php_sqlite3_stmt *stmt_obj;
  1412. zval *object = ZEND_THIS;
  1413. struct php_sqlite3_bound_param param = {0};
  1414. zval *parameter;
  1415. stmt_obj = Z_SQLITE3_STMT_P(object);
  1416. param.param_number = -1;
  1417. param.type = SQLITE3_TEXT;
  1418. ZEND_PARSE_PARAMETERS_START(2, 3)
  1419. Z_PARAM_STR_OR_LONG(param.name, param.param_number)
  1420. Z_PARAM_ZVAL(parameter)
  1421. Z_PARAM_OPTIONAL
  1422. Z_PARAM_LONG(param.type)
  1423. ZEND_PARSE_PARAMETERS_END();
  1424. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1425. SQLITE3_CHECK_INITIALIZED_STMT(stmt_obj->stmt, SQLite3Stmt);
  1426. ZVAL_COPY(&param.parameter, parameter);
  1427. if (ZEND_NUM_ARGS() < 3) {
  1428. PHP_SQLITE3_SET_TYPE(parameter, param);
  1429. }
  1430. if (!register_bound_parameter_to_sqlite(&param, stmt_obj)) {
  1431. if (!Z_ISUNDEF(param.parameter)) {
  1432. zval_ptr_dtor(&(param.parameter));
  1433. ZVAL_UNDEF(&param.parameter);
  1434. }
  1435. RETURN_FALSE;
  1436. }
  1437. RETURN_TRUE;
  1438. }
  1439. /* }}} */
  1440. /* {{{ Bind Parameter to a stmt variable. */
  1441. PHP_METHOD(SQLite3Stmt, bindParam)
  1442. {
  1443. sqlite3stmt_bind(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1444. }
  1445. /* }}} */
  1446. /* {{{ Bind Value of a parameter to a stmt variable. */
  1447. PHP_METHOD(SQLite3Stmt, bindValue)
  1448. {
  1449. sqlite3stmt_bind(INTERNAL_FUNCTION_PARAM_PASSTHRU);
  1450. }
  1451. /* }}} */
  1452. #undef PHP_SQLITE3_SET_TYPE
  1453. /* {{{ Executes a prepared statement and returns a result set object. */
  1454. PHP_METHOD(SQLite3Stmt, execute)
  1455. {
  1456. php_sqlite3_stmt *stmt_obj;
  1457. php_sqlite3_result *result;
  1458. zval *object = ZEND_THIS;
  1459. int return_code = 0;
  1460. int bind_rc = 0;
  1461. stmt_obj = Z_SQLITE3_STMT_P(object);
  1462. ZEND_PARSE_PARAMETERS_NONE();
  1463. SQLITE3_CHECK_INITIALIZED(stmt_obj->db_obj, stmt_obj->initialised, SQLite3);
  1464. /* Always reset statement before execution, see bug #77051 */
  1465. sqlite3_reset(stmt_obj->stmt);
  1466. /* Bind parameters to the statement */
  1467. bind_rc = php_sqlite3_bind_params(stmt_obj);
  1468. if (bind_rc == FAILURE || EG(exception)) {
  1469. RETURN_FALSE;
  1470. }
  1471. return_code = sqlite3_step(stmt_obj->stmt);
  1472. switch (return_code) {
  1473. case SQLITE_ROW: /* Valid Row */
  1474. case SQLITE_DONE: /* Valid but no results */
  1475. {
  1476. sqlite3_reset(stmt_obj->stmt);
  1477. object_init_ex(return_value, php_sqlite3_result_entry);
  1478. result = Z_SQLITE3_RESULT_P(return_value);
  1479. result->is_prepared_statement = 1;
  1480. result->db_obj = stmt_obj->db_obj;
  1481. result->stmt_obj = stmt_obj;
  1482. ZVAL_OBJ_COPY(&result->stmt_obj_zval, Z_OBJ_P(object));
  1483. break;
  1484. }
  1485. case SQLITE_ERROR:
  1486. sqlite3_reset(stmt_obj->stmt);
  1487. ZEND_FALLTHROUGH;
  1488. default:
  1489. if (!EG(exception)) {
  1490. php_sqlite3_error(stmt_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(stmt_obj->stmt)));
  1491. }
  1492. zval_ptr_dtor(return_value);
  1493. RETURN_FALSE;
  1494. }
  1495. return;
  1496. }
  1497. /* }}} */
  1498. /* {{{ __constructor for SQLite3Stmt. */
  1499. PHP_METHOD(SQLite3Stmt, __construct)
  1500. {
  1501. php_sqlite3_stmt *stmt_obj;
  1502. php_sqlite3_db_object *db_obj;
  1503. zval *object = ZEND_THIS;
  1504. zval *db_zval;
  1505. zend_string *sql;
  1506. int errcode;
  1507. zend_error_handling error_handling;
  1508. php_sqlite3_free_list *free_item;
  1509. stmt_obj = Z_SQLITE3_STMT_P(object);
  1510. if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &db_zval, php_sqlite3_sc_entry, &sql) == FAILURE) {
  1511. RETURN_THROWS();
  1512. }
  1513. db_obj = Z_SQLITE3_DB_P(db_zval);
  1514. zend_replace_error_handling(EH_THROW, NULL, &error_handling);
  1515. SQLITE3_CHECK_INITIALIZED(db_obj, db_obj->initialised, SQLite3)
  1516. zend_restore_error_handling(&error_handling);
  1517. if (!ZSTR_LEN(sql)) {
  1518. RETURN_FALSE;
  1519. }
  1520. stmt_obj->db_obj = db_obj;
  1521. ZVAL_OBJ_COPY(&stmt_obj->db_obj_zval, Z_OBJ_P(db_zval));
  1522. errcode = sqlite3_prepare_v2(db_obj->db, ZSTR_VAL(sql), ZSTR_LEN(sql), &(stmt_obj->stmt), NULL);
  1523. if (errcode != SQLITE_OK) {
  1524. php_sqlite3_error(db_obj, "Unable to prepare statement: %d, %s", errcode, sqlite3_errmsg(db_obj->db));
  1525. zval_ptr_dtor(return_value);
  1526. RETURN_FALSE;
  1527. }
  1528. stmt_obj->initialised = 1;
  1529. free_item = emalloc(sizeof(php_sqlite3_free_list));
  1530. free_item->stmt_obj = stmt_obj;
  1531. //?? free_item->stmt_obj_zval = ZEND_THIS;
  1532. ZVAL_OBJ(&free_item->stmt_obj_zval, Z_OBJ_P(object));
  1533. zend_llist_add_element(&(db_obj->free_list), &free_item);
  1534. }
  1535. /* }}} */
  1536. /* {{{ Number of columns in the result set. */
  1537. PHP_METHOD(SQLite3Result, numColumns)
  1538. {
  1539. php_sqlite3_result *result_obj;
  1540. zval *object = ZEND_THIS;
  1541. result_obj = Z_SQLITE3_RESULT_P(object);
  1542. ZEND_PARSE_PARAMETERS_NONE();
  1543. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1544. RETURN_LONG(sqlite3_column_count(result_obj->stmt_obj->stmt));
  1545. }
  1546. /* }}} */
  1547. /* {{{ Returns the name of the nth column. */
  1548. PHP_METHOD(SQLite3Result, columnName)
  1549. {
  1550. php_sqlite3_result *result_obj;
  1551. zval *object = ZEND_THIS;
  1552. zend_long column = 0;
  1553. char *column_name;
  1554. result_obj = Z_SQLITE3_RESULT_P(object);
  1555. ZEND_PARSE_PARAMETERS_START(1, 1)
  1556. Z_PARAM_LONG(column)
  1557. ZEND_PARSE_PARAMETERS_END();
  1558. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1559. column_name = (char*) sqlite3_column_name(result_obj->stmt_obj->stmt, column);
  1560. if (column_name == NULL) {
  1561. RETURN_FALSE;
  1562. }
  1563. RETVAL_STRING(column_name);
  1564. }
  1565. /* }}} */
  1566. /* {{{ Returns the type of the nth column. */
  1567. PHP_METHOD(SQLite3Result, columnType)
  1568. {
  1569. php_sqlite3_result *result_obj;
  1570. zval *object = ZEND_THIS;
  1571. zend_long column = 0;
  1572. result_obj = Z_SQLITE3_RESULT_P(object);
  1573. ZEND_PARSE_PARAMETERS_START(1, 1)
  1574. Z_PARAM_LONG(column)
  1575. ZEND_PARSE_PARAMETERS_END();
  1576. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1577. if (!sqlite3_data_count(result_obj->stmt_obj->stmt)) {
  1578. RETURN_FALSE;
  1579. }
  1580. RETURN_LONG(sqlite3_column_type(result_obj->stmt_obj->stmt, column));
  1581. }
  1582. /* }}} */
  1583. /* {{{ Fetch a result row as both an associative or numerically indexed array or both. */
  1584. PHP_METHOD(SQLite3Result, fetchArray)
  1585. {
  1586. php_sqlite3_result *result_obj;
  1587. zval *object = ZEND_THIS;
  1588. int i, ret;
  1589. zend_long mode = PHP_SQLITE3_BOTH;
  1590. result_obj = Z_SQLITE3_RESULT_P(object);
  1591. ZEND_PARSE_PARAMETERS_START(0, 1)
  1592. Z_PARAM_OPTIONAL
  1593. Z_PARAM_LONG(mode)
  1594. ZEND_PARSE_PARAMETERS_END();
  1595. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1596. ret = sqlite3_step(result_obj->stmt_obj->stmt);
  1597. switch (ret) {
  1598. case SQLITE_ROW:
  1599. /* If there was no return value then just skip fetching */
  1600. if (!USED_RET()) {
  1601. RETURN_FALSE;
  1602. }
  1603. array_init(return_value);
  1604. int column_count = sqlite3_data_count(result_obj->stmt_obj->stmt);
  1605. for (i = 0; i < column_count; i++) {
  1606. zval data;
  1607. sqlite_value_to_zval(result_obj->stmt_obj->stmt, i, &data);
  1608. if (mode & PHP_SQLITE3_NUM) {
  1609. add_index_zval(return_value, i, &data);
  1610. }
  1611. if (mode & PHP_SQLITE3_ASSOC) {
  1612. if (mode & PHP_SQLITE3_NUM) {
  1613. if (Z_REFCOUNTED(data)) {
  1614. Z_ADDREF(data);
  1615. }
  1616. }
  1617. add_assoc_zval(return_value, (char*)sqlite3_column_name(result_obj->stmt_obj->stmt, i), &data);
  1618. }
  1619. }
  1620. break;
  1621. case SQLITE_DONE:
  1622. RETURN_FALSE;
  1623. break;
  1624. default:
  1625. php_sqlite3_error(result_obj->db_obj, "Unable to execute statement: %s", sqlite3_errmsg(sqlite3_db_handle(result_obj->stmt_obj->stmt)));
  1626. }
  1627. }
  1628. /* }}} */
  1629. /* {{{ Resets the result set back to the first row. */
  1630. PHP_METHOD(SQLite3Result, reset)
  1631. {
  1632. php_sqlite3_result *result_obj;
  1633. zval *object = ZEND_THIS;
  1634. result_obj = Z_SQLITE3_RESULT_P(object);
  1635. ZEND_PARSE_PARAMETERS_NONE();
  1636. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1637. if (sqlite3_reset(result_obj->stmt_obj->stmt) != SQLITE_OK) {
  1638. RETURN_FALSE;
  1639. }
  1640. RETURN_TRUE;
  1641. }
  1642. /* }}} */
  1643. /* {{{ Closes the result set. */
  1644. PHP_METHOD(SQLite3Result, finalize)
  1645. {
  1646. php_sqlite3_result *result_obj;
  1647. zval *object = ZEND_THIS;
  1648. result_obj = Z_SQLITE3_RESULT_P(object);
  1649. ZEND_PARSE_PARAMETERS_NONE();
  1650. SQLITE3_CHECK_INITIALIZED(result_obj->db_obj, result_obj->stmt_obj->initialised, SQLite3Result)
  1651. /* We need to finalize an internal statement */
  1652. if (result_obj->is_prepared_statement == 0) {
  1653. zend_llist_del_element(&(result_obj->db_obj->free_list), &result_obj->stmt_obj_zval,
  1654. (int (*)(void *, void *)) php_sqlite3_compare_stmt_zval_free);
  1655. } else {
  1656. sqlite3_reset(result_obj->stmt_obj->stmt);
  1657. }
  1658. RETURN_TRUE;
  1659. }
  1660. /* }}} */
  1661. /* {{{ __constructor for SQLite3Result. */
  1662. PHP_METHOD(SQLite3Result, __construct)
  1663. {
  1664. zend_throw_exception(zend_ce_exception, "SQLite3Result cannot be directly instantiated", 0);
  1665. }
  1666. /* }}} */
  1667. /* {{{ Authorization Callback */
  1668. static int php_sqlite3_authorizer(void *autharg, int action, const char *arg1, const char *arg2, const char *arg3, const char *arg4)
  1669. {
  1670. /* Check open_basedir restrictions first */
  1671. if (PG(open_basedir) && *PG(open_basedir)) {
  1672. if (action == SQLITE_ATTACH) {
  1673. if (!arg1) {
  1674. return SQLITE_DENY;
  1675. }
  1676. if (memcmp(arg1, ":memory:", sizeof(":memory:")) && *arg1) {
  1677. if (strncmp(arg1, "file:", 5) == 0) {
  1678. /* starts with "file:" */
  1679. if (!arg1[5]) {
  1680. return SQLITE_DENY;
  1681. }
  1682. if (php_check_open_basedir(arg1 + 5)) {
  1683. return SQLITE_DENY;
  1684. }
  1685. }
  1686. if (php_check_open_basedir(arg1)) {
  1687. return SQLITE_DENY;
  1688. }
  1689. }
  1690. }
  1691. }
  1692. php_sqlite3_db_object *db_obj = (php_sqlite3_db_object *)autharg;
  1693. zend_fcall_info *fci = &db_obj->authorizer_fci;
  1694. /* fallback to access allowed if authorizer callback is not defined */
  1695. if (fci->size == 0) {
  1696. return SQLITE_OK;
  1697. }
  1698. /* call userland authorizer callback, if set */
  1699. zval retval;
  1700. zval argv[5];
  1701. ZVAL_LONG(&argv[0], action);
  1702. if (NULL == arg1) {
  1703. ZVAL_NULL(&argv[1]);
  1704. } else {
  1705. ZVAL_STRING(&argv[1], arg1);
  1706. }
  1707. if (NULL == arg2) {
  1708. ZVAL_NULL(&argv[2]);
  1709. } else {
  1710. ZVAL_STRING(&argv[2], arg2);
  1711. }
  1712. if (NULL == arg3) {
  1713. ZVAL_NULL(&argv[3]);
  1714. } else {
  1715. ZVAL_STRING(&argv[3], arg3);
  1716. }
  1717. if (NULL == arg4) {
  1718. ZVAL_NULL(&argv[4]);
  1719. } else {
  1720. ZVAL_STRING(&argv[4], arg4);
  1721. }
  1722. fci->retval = &retval;
  1723. fci->param_count = 5;
  1724. fci->params = argv;
  1725. int authreturn = SQLITE_DENY;
  1726. if (zend_call_function(fci, &db_obj->authorizer_fcc) != SUCCESS || Z_ISUNDEF(retval)) {
  1727. php_sqlite3_error(db_obj, "An error occurred while invoking the authorizer callback");
  1728. } else {
  1729. if (Z_TYPE(retval) != IS_LONG) {
  1730. php_sqlite3_error(db_obj, "The authorizer callback returned an invalid type: expected int");
  1731. } else {
  1732. authreturn = Z_LVAL(retval);
  1733. if (authreturn != SQLITE_OK && authreturn != SQLITE_IGNORE && authreturn != SQLITE_DENY) {
  1734. php_sqlite3_error(db_obj, "The authorizer callback returned an invalid value");
  1735. authreturn = SQLITE_DENY;
  1736. }
  1737. }
  1738. }
  1739. zend_fcall_info_args_clear(fci, 0);
  1740. zval_ptr_dtor(&retval);
  1741. return authreturn;
  1742. }
  1743. /* }}} */
  1744. /* {{{ php_sqlite3_free_list_dtor */
  1745. static void php_sqlite3_free_list_dtor(void **item)
  1746. {
  1747. php_sqlite3_free_list *free_item = (php_sqlite3_free_list *)*item;
  1748. if (free_item->stmt_obj && free_item->stmt_obj->initialised) {
  1749. sqlite3_finalize(free_item->stmt_obj->stmt);
  1750. free_item->stmt_obj->initialised = 0;
  1751. }
  1752. efree(*item);
  1753. }
  1754. /* }}} */
  1755. static int php_sqlite3_compare_stmt_zval_free(php_sqlite3_free_list **free_list, zval *statement ) /* {{{ */
  1756. {
  1757. return ((*free_list)->stmt_obj->initialised && Z_PTR_P(statement) == Z_PTR((*free_list)->stmt_obj_zval));
  1758. }
  1759. /* }}} */
  1760. static int php_sqlite3_compare_stmt_free( php_sqlite3_free_list **free_list, sqlite3_stmt *statement ) /* {{{ */
  1761. {
  1762. return ((*free_list)->stmt_obj->initialised && statement == (*free_list)->stmt_obj->stmt);
  1763. }
  1764. /* }}} */
  1765. static void php_sqlite3_object_free_storage(zend_object *object) /* {{{ */
  1766. {
  1767. php_sqlite3_db_object *intern = php_sqlite3_db_from_obj(object);
  1768. php_sqlite3_func *func;
  1769. php_sqlite3_collation *collation;
  1770. if (!intern) {
  1771. return;
  1772. }
  1773. /* Release function_name from authorizer */
  1774. if (intern->authorizer_fci.size > 0) {
  1775. zval_ptr_dtor(&intern->authorizer_fci.function_name);
  1776. }
  1777. while (intern->funcs) {
  1778. func = intern->funcs;
  1779. intern->funcs = func->next;
  1780. if (intern->initialised && intern->db) {
  1781. sqlite3_create_function(intern->db, func->func_name, func->argc, SQLITE_UTF8, func, NULL, NULL, NULL);
  1782. }
  1783. efree((char*)func->func_name);
  1784. if (!Z_ISUNDEF(func->func)) {
  1785. zval_ptr_dtor(&func->func);
  1786. }
  1787. if (!Z_ISUNDEF(func->step)) {
  1788. zval_ptr_dtor(&func->step);
  1789. }
  1790. if (!Z_ISUNDEF(func->fini)) {
  1791. zval_ptr_dtor(&func->fini);
  1792. }
  1793. efree(func);
  1794. }
  1795. while (intern->collations){
  1796. collation = intern->collations;
  1797. intern->collations = collation->next;
  1798. if (intern->initialised && intern->db){
  1799. sqlite3_create_collation(intern->db, collation->collation_name, SQLITE_UTF8, NULL, NULL);
  1800. }
  1801. efree((char*)collation->collation_name);
  1802. if (!Z_ISUNDEF(collation->cmp_func)) {
  1803. zval_ptr_dtor(&collation->cmp_func);
  1804. }
  1805. efree(collation);
  1806. }
  1807. if (intern->initialised && intern->db) {
  1808. sqlite3_close(intern->db);
  1809. intern->initialised = 0;
  1810. }
  1811. zend_object_std_dtor(&intern->zo);
  1812. }
  1813. /* }}} */
  1814. static void php_sqlite3_stmt_object_free_storage(zend_object *object) /* {{{ */
  1815. {
  1816. php_sqlite3_stmt *intern = php_sqlite3_stmt_from_obj(object);
  1817. if (!intern) {
  1818. return;
  1819. }
  1820. if (intern->bound_params) {
  1821. zend_hash_destroy(intern->bound_params);
  1822. FREE_HASHTABLE(intern->bound_params);
  1823. intern->bound_params = NULL;
  1824. }
  1825. if (intern->initialised) {
  1826. zend_llist_del_element(&(intern->db_obj->free_list), intern->stmt,
  1827. (int (*)(void *, void *)) php_sqlite3_compare_stmt_free);
  1828. }
  1829. if (!Z_ISUNDEF(intern->db_obj_zval)) {
  1830. zval_ptr_dtor(&intern->db_obj_zval);
  1831. }
  1832. zend_object_std_dtor(&intern->zo);
  1833. }
  1834. /* }}} */
  1835. static void php_sqlite3_result_object_free_storage(zend_object *object) /* {{{ */
  1836. {
  1837. php_sqlite3_result *intern = php_sqlite3_result_from_obj(object);
  1838. if (!intern) {
  1839. return;
  1840. }
  1841. if (!Z_ISNULL(intern->stmt_obj_zval)) {
  1842. if (intern->stmt_obj && intern->stmt_obj->initialised) {
  1843. sqlite3_reset(intern->stmt_obj->stmt);
  1844. }
  1845. zval_ptr_dtor(&intern->stmt_obj_zval);
  1846. }
  1847. zend_object_std_dtor(&intern->zo);
  1848. }
  1849. /* }}} */
  1850. static zend_object *php_sqlite3_object_new(zend_class_entry *class_type) /* {{{ */
  1851. {
  1852. php_sqlite3_db_object *intern;
  1853. /* Allocate memory for it */
  1854. intern = zend_object_alloc(sizeof(php_sqlite3_db_object), class_type);
  1855. /* Need to keep track of things to free */
  1856. zend_llist_init(&(intern->free_list), sizeof(php_sqlite3_free_list *), (llist_dtor_func_t)php_sqlite3_free_list_dtor, 0);
  1857. zend_object_std_init(&intern->zo, class_type);
  1858. object_properties_init(&intern->zo, class_type);
  1859. intern->zo.handlers = &sqlite3_object_handlers;
  1860. return &intern->zo;
  1861. }
  1862. /* }}} */
  1863. static zend_object *php_sqlite3_stmt_object_new(zend_class_entry *class_type) /* {{{ */
  1864. {
  1865. php_sqlite3_stmt *intern;
  1866. /* Allocate memory for it */
  1867. intern = zend_object_alloc(sizeof(php_sqlite3_stmt), class_type);
  1868. zend_object_std_init(&intern->zo, class_type);
  1869. object_properties_init(&intern->zo, class_type);
  1870. intern->zo.handlers = &sqlite3_stmt_object_handlers;
  1871. return &intern->zo;
  1872. }
  1873. /* }}} */
  1874. static zend_object *php_sqlite3_result_object_new(zend_class_entry *class_type) /* {{{ */
  1875. {
  1876. php_sqlite3_result *intern;
  1877. /* Allocate memory for it */
  1878. intern = zend_object_alloc(sizeof(php_sqlite3_result), class_type);
  1879. zend_object_std_init(&intern->zo, class_type);
  1880. object_properties_init(&intern->zo, class_type);
  1881. intern->zo.handlers = &sqlite3_result_object_handlers;
  1882. return &intern->zo;
  1883. }
  1884. /* }}} */
  1885. static void sqlite3_param_dtor(zval *data) /* {{{ */
  1886. {
  1887. struct php_sqlite3_bound_param *param = (struct php_sqlite3_bound_param*)Z_PTR_P(data);
  1888. if (param->name) {
  1889. zend_string_release_ex(param->name, 0);
  1890. }
  1891. if (!Z_ISNULL(param->parameter)) {
  1892. zval_ptr_dtor(&(param->parameter));
  1893. ZVAL_UNDEF(&param->parameter);
  1894. }
  1895. efree(param);
  1896. }
  1897. /* }}} */
  1898. /* {{{ PHP_MINIT_FUNCTION */
  1899. PHP_MINIT_FUNCTION(sqlite3)
  1900. {
  1901. #ifdef ZTS
  1902. /* Refuse to load if this wasn't a threasafe library loaded */
  1903. if (!sqlite3_threadsafe()) {
  1904. php_error_docref(NULL, E_WARNING, "A thread safe version of SQLite is required when using a thread safe version of PHP.");
  1905. return FAILURE;
  1906. }
  1907. #endif
  1908. memcpy(&sqlite3_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  1909. memcpy(&sqlite3_stmt_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  1910. memcpy(&sqlite3_result_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  1911. /* Register SQLite 3 Class */
  1912. sqlite3_object_handlers.offset = XtOffsetOf(php_sqlite3_db_object, zo);
  1913. sqlite3_object_handlers.clone_obj = NULL;
  1914. sqlite3_object_handlers.free_obj = php_sqlite3_object_free_storage;
  1915. php_sqlite3_sc_entry = register_class_SQLite3();
  1916. php_sqlite3_sc_entry->create_object = php_sqlite3_object_new;
  1917. /* Register SQLite 3 Prepared Statement Class */
  1918. sqlite3_stmt_object_handlers.offset = XtOffsetOf(php_sqlite3_stmt, zo);
  1919. sqlite3_stmt_object_handlers.clone_obj = NULL;
  1920. sqlite3_stmt_object_handlers.free_obj = php_sqlite3_stmt_object_free_storage;
  1921. php_sqlite3_stmt_entry = register_class_SQLite3Stmt();
  1922. php_sqlite3_stmt_entry->create_object = php_sqlite3_stmt_object_new;
  1923. /* Register SQLite 3 Result Class */
  1924. sqlite3_result_object_handlers.offset = XtOffsetOf(php_sqlite3_result, zo);
  1925. sqlite3_result_object_handlers.clone_obj = NULL;
  1926. sqlite3_result_object_handlers.free_obj = php_sqlite3_result_object_free_storage;
  1927. php_sqlite3_result_entry = register_class_SQLite3Result();
  1928. php_sqlite3_result_entry->create_object = php_sqlite3_result_object_new;
  1929. REGISTER_INI_ENTRIES();
  1930. REGISTER_LONG_CONSTANT("SQLITE3_ASSOC", PHP_SQLITE3_ASSOC, CONST_CS | CONST_PERSISTENT);
  1931. REGISTER_LONG_CONSTANT("SQLITE3_NUM", PHP_SQLITE3_NUM, CONST_CS | CONST_PERSISTENT);
  1932. REGISTER_LONG_CONSTANT("SQLITE3_BOTH", PHP_SQLITE3_BOTH, CONST_CS | CONST_PERSISTENT);
  1933. REGISTER_LONG_CONSTANT("SQLITE3_INTEGER", SQLITE_INTEGER, CONST_CS | CONST_PERSISTENT);
  1934. REGISTER_LONG_CONSTANT("SQLITE3_FLOAT", SQLITE_FLOAT, CONST_CS | CONST_PERSISTENT);
  1935. REGISTER_LONG_CONSTANT("SQLITE3_TEXT", SQLITE3_TEXT, CONST_CS | CONST_PERSISTENT);
  1936. REGISTER_LONG_CONSTANT("SQLITE3_BLOB", SQLITE_BLOB, CONST_CS | CONST_PERSISTENT);
  1937. REGISTER_LONG_CONSTANT("SQLITE3_NULL", SQLITE_NULL, CONST_CS | CONST_PERSISTENT);
  1938. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READONLY", SQLITE_OPEN_READONLY, CONST_CS | CONST_PERSISTENT);
  1939. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_READWRITE", SQLITE_OPEN_READWRITE, CONST_CS | CONST_PERSISTENT);
  1940. REGISTER_LONG_CONSTANT("SQLITE3_OPEN_CREATE", SQLITE_OPEN_CREATE, CONST_CS | CONST_PERSISTENT);
  1941. /* Class constants */
  1942. zend_declare_class_constant_long(php_sqlite3_sc_entry, "OK", sizeof("OK") - 1, SQLITE_OK);
  1943. /* Constants for authorizer return */
  1944. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DENY", sizeof("DENY") - 1, SQLITE_DENY);
  1945. zend_declare_class_constant_long(php_sqlite3_sc_entry, "IGNORE", sizeof("IGNORE") - 1, SQLITE_IGNORE);
  1946. /* Constants for authorizer actions */
  1947. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_INDEX", sizeof("CREATE_INDEX") - 1, SQLITE_CREATE_INDEX);
  1948. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TABLE", sizeof("CREATE_TABLE") - 1, SQLITE_CREATE_TABLE);
  1949. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_INDEX", sizeof("CREATE_TEMP_INDEX") - 1, SQLITE_CREATE_TEMP_INDEX);
  1950. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_TABLE", sizeof("CREATE_TEMP_TABLE") - 1, SQLITE_CREATE_TEMP_TABLE);
  1951. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_TRIGGER", sizeof("CREATE_TEMP_TRIGGER") - 1, SQLITE_CREATE_TEMP_TRIGGER);
  1952. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TEMP_VIEW", sizeof("CREATE_TEMP_VIEW") - 1, SQLITE_CREATE_TEMP_VIEW);
  1953. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_TRIGGER", sizeof("CREATE_TRIGGER") - 1, SQLITE_CREATE_TRIGGER);
  1954. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_VIEW", sizeof("CREATE_VIEW") - 1, SQLITE_CREATE_VIEW);
  1955. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DELETE", sizeof("DELETE") - 1, SQLITE_DELETE);
  1956. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_INDEX", sizeof("DROP_INDEX") - 1, SQLITE_DROP_INDEX);
  1957. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TABLE", sizeof("DROP_TABLE") - 1, SQLITE_DROP_TABLE);
  1958. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_INDEX", sizeof("DROP_TEMP_INDEX") - 1, SQLITE_DROP_TEMP_INDEX);
  1959. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_TABLE", sizeof("DROP_TEMP_TABLE") - 1, SQLITE_DROP_TEMP_TABLE);
  1960. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_TRIGGER", sizeof("DROP_TEMP_TRIGGER") - 1, SQLITE_DROP_TEMP_TRIGGER);
  1961. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TEMP_VIEW", sizeof("DROP_TEMP_VIEW") - 1, SQLITE_DROP_TEMP_VIEW);
  1962. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_TRIGGER", sizeof("DROP_TRIGGER") - 1, SQLITE_DROP_TRIGGER);
  1963. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_VIEW", sizeof("DROP_VIEW") - 1, SQLITE_DROP_VIEW);
  1964. zend_declare_class_constant_long(php_sqlite3_sc_entry, "INSERT", sizeof("INSERT") - 1, SQLITE_INSERT);
  1965. zend_declare_class_constant_long(php_sqlite3_sc_entry, "PRAGMA", sizeof("PRAGMA") - 1, SQLITE_PRAGMA);
  1966. zend_declare_class_constant_long(php_sqlite3_sc_entry, "READ", sizeof("READ") - 1, SQLITE_READ);
  1967. zend_declare_class_constant_long(php_sqlite3_sc_entry, "SELECT", sizeof("SELECT") - 1, SQLITE_SELECT);
  1968. zend_declare_class_constant_long(php_sqlite3_sc_entry, "TRANSACTION", sizeof("TRANSACTION") - 1, SQLITE_TRANSACTION);
  1969. zend_declare_class_constant_long(php_sqlite3_sc_entry, "UPDATE", sizeof("UPDATE") - 1, SQLITE_UPDATE);
  1970. zend_declare_class_constant_long(php_sqlite3_sc_entry, "ATTACH", sizeof("ATTACH") - 1, SQLITE_ATTACH);
  1971. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DETACH", sizeof("DETACH") - 1, SQLITE_DETACH);
  1972. zend_declare_class_constant_long(php_sqlite3_sc_entry, "ALTER_TABLE", sizeof("ALTER_TABLE") - 1, SQLITE_ALTER_TABLE);
  1973. zend_declare_class_constant_long(php_sqlite3_sc_entry, "REINDEX", sizeof("REINDEX") - 1, SQLITE_REINDEX);
  1974. zend_declare_class_constant_long(php_sqlite3_sc_entry, "ANALYZE", sizeof("ANALYZE") - 1, SQLITE_ANALYZE);
  1975. zend_declare_class_constant_long(php_sqlite3_sc_entry, "CREATE_VTABLE", sizeof("CREATE_VTABLE") - 1, SQLITE_CREATE_VTABLE);
  1976. zend_declare_class_constant_long(php_sqlite3_sc_entry, "DROP_VTABLE", sizeof("DROP_VTABLE") - 1, SQLITE_DROP_VTABLE);
  1977. zend_declare_class_constant_long(php_sqlite3_sc_entry, "FUNCTION", sizeof("FUNCTION") - 1, SQLITE_FUNCTION);
  1978. zend_declare_class_constant_long(php_sqlite3_sc_entry, "SAVEPOINT", sizeof("SAVEPOINT") - 1, SQLITE_SAVEPOINT);
  1979. zend_declare_class_constant_long(php_sqlite3_sc_entry, "COPY", sizeof("COPY") - 1, SQLITE_COPY);
  1980. #ifdef SQLITE_RECURSIVE
  1981. zend_declare_class_constant_long(php_sqlite3_sc_entry, "RECURSIVE", sizeof("RECURSIVE") - 1, SQLITE_RECURSIVE);
  1982. #endif
  1983. #ifdef SQLITE_DETERMINISTIC
  1984. REGISTER_LONG_CONSTANT("SQLITE3_DETERMINISTIC", SQLITE_DETERMINISTIC, CONST_CS | CONST_PERSISTENT);
  1985. #endif
  1986. return SUCCESS;
  1987. }
  1988. /* }}} */
  1989. /* {{{ PHP_MSHUTDOWN_FUNCTION */
  1990. PHP_MSHUTDOWN_FUNCTION(sqlite3)
  1991. {
  1992. UNREGISTER_INI_ENTRIES();
  1993. return SUCCESS;
  1994. }
  1995. /* }}} */
  1996. /* {{{ PHP_MINFO_FUNCTION */
  1997. PHP_MINFO_FUNCTION(sqlite3)
  1998. {
  1999. php_info_print_table_start();
  2000. php_info_print_table_header(2, "SQLite3 support", "enabled");
  2001. php_info_print_table_row(2, "SQLite Library", sqlite3_libversion());
  2002. php_info_print_table_end();
  2003. DISPLAY_INI_ENTRIES();
  2004. }
  2005. /* }}} */
  2006. /* {{{ PHP_GINIT_FUNCTION */
  2007. static PHP_GINIT_FUNCTION(sqlite3)
  2008. {
  2009. #if defined(COMPILE_DL_SQLITE3) && defined(ZTS)
  2010. ZEND_TSRMLS_CACHE_UPDATE();
  2011. #endif
  2012. memset(sqlite3_globals, 0, sizeof(*sqlite3_globals));
  2013. }
  2014. /* }}} */
  2015. /* {{{ sqlite3_module_entry */
  2016. zend_module_entry sqlite3_module_entry = {
  2017. STANDARD_MODULE_HEADER,
  2018. "sqlite3",
  2019. NULL,
  2020. PHP_MINIT(sqlite3),
  2021. PHP_MSHUTDOWN(sqlite3),
  2022. NULL,
  2023. NULL,
  2024. PHP_MINFO(sqlite3),
  2025. PHP_SQLITE3_VERSION,
  2026. PHP_MODULE_GLOBALS(sqlite3),
  2027. PHP_GINIT(sqlite3),
  2028. NULL,
  2029. NULL,
  2030. STANDARD_MODULE_PROPERTIES_EX
  2031. };
  2032. /* }}} */
  2033. #ifdef COMPILE_DL_SQLITE3
  2034. #ifdef ZTS
  2035. ZEND_TSRMLS_CACHE_DEFINE()
  2036. #endif
  2037. ZEND_GET_MODULE(sqlite3)
  2038. #endif