File: funzioni_sqlite.php

package info (click to toggle)
hoteldruid 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,808 kB
  • sloc: php: 170,672; javascript: 2,296; sh: 304; xml: 52; makefile: 17
file content (279 lines) | stat: -rw-r--r-- 7,533 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php

##################################################################################
#    HOTELDRUID
#    Copyright (C) 2001-2023 by Marco Maria Francesco De Santis (marco@digitaldruid.net)
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    any later version accepted by Marco Maria Francesco De Santis, which
#    shall act as a proxy as defined in Section 14 of version 3 of the
#    license.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
##################################################################################

#Funzioni per usare il database SQLITE

ignore_user_abort(1);

# variabili per le differenze nella sintassi delle query
#global $ILIKE,$LIKE;
$ILIKE = "LIKE";
$LIKE = "GLOB";
$DATETIME = "text";
$MEDIUMTEXT = "text";



function connetti_db ($database,$host,$port,$user,$password,$estensione) {

if ($estensione == "SI") dl("sqlite3.so");
if (defined("C_PERCORSO_A_DATI")) $numconnessione = new SQLite3(C_PERCORSO_A_DATI."db_".$database);
else $numconnessione = new SQLite3(C_DATI_PATH."/db_".$database);
$numconnessione->busyTimeout(60000);
return $numconnessione;

} # fine function connetti_db



function disconnetti_db ($numconnessione) {

$risul = $numconnessione->close();
return $risul;

} # fine function disconnetti_db



function prepara_query_sqlite (&$query) {

if (str_replace(" GLOB '","",$query) != $query) {
$query .= " ";
$q_vett = explode(" GLOB '",$query);
for ($n = 1 ; $n < count($q_vett) ; $n++) {
if (substr(str_replace("''","",$q_vett[$n]),0,1) != "'") {
$arg = str_replace("''","^'^",$q_vett[$n]);
$arg = explode("' ",$arg);
$arg = str_replace("^'^","''",$arg[0]);
if (str_replace("''","",$arg) == str_replace("'","",str_replace("''","",$arg))) {
$query = str_replace(" GLOB '$arg' "," GLOB '".str_replace("%","*",str_replace("_","?",$arg))."' ",$query);
} # fine if (str_replace("''","",$arg) == str_replace("'","",str_replace("''","",$arg)))
} # fine if (substr(str_replace("''","",$q_vett[$n]),0,1) != "'")
} # fine for $n
} # fine if (str_replace(" GLOB '","",$query) != $query)

} # fine function prepara_query_sqlite



function esegui_query_reale (&$query,$silenzio = "") {
global $numconnessione;
prepara_query_sqlite($query);

$risul = $numconnessione->query($query);
if ($risul) {
$risultato = array();
$num1 = 0;
if (strtolower(substr(trim($query),0,6)) == "select" and is_object($risul)) {
while ($risultato[$num1] = $risul->fetchArray(SQLITE3_ASSOC)) $num1++;
$risultato['numcol'] = $risul->numColumns();
for ($num2 = 0 ; $num2 < $risultato['numcol'] ; $num2++) $risultato['col'][$num2] = $risul->columnName($num2);
$risul->finalize();
} # fine if (strtolower(substr(trim($query),0,6)) == "select" and is_object($risul))
$risultato['num'] = $num1;
} # fine if ($risul)
else $risultato = $risul;

if (!$risul and $silenzio != "totale") {
global $PHPR_TAB_PRE;
if (!$silenzio) echo "<br>ERROR in: ".htmlspecialchars(str_replace(" ".$PHPR_TAB_PRE," ",$query))."<br>";
error_log("IN ".$_SERVER['PHP_SELF']." SQLITE ERROR: ".substr(str_replace(" ".$PHPR_TAB_PRE," ",$query),0,25)."...");
} # fine (!$risul and $silenzio != "totale")

return $risultato;

} # fine function esegui_query_reale



if (substr($PHPR_LOG,0,2) != "SI") {

function esegui_query ($query,$silenzio = "",$idlog = "") {
$risul = esegui_query_reale($query,$silenzio);
return $risul;
} # fine function esegui_query

} # fine if (substr($PHPR_LOG,0,2) != "SI")


else {
if (!function_exists("inserisci_log")) include("./includes/funzioni_log.php");

function esegui_query ($query,$silenzio = "",$idlog = "") {
$risul = esegui_query_reale($query,$silenzio);

if ($idlog != 1) inserisci_log($query,$idlog);

return $risul;

} # fine function esegui_query

} # fine else if (substr($PHPR_LOG,0,2) != "SI")



function risul_query ($query,$riga,$colonna,$tab="") {

#if ($tab) $colonna = "$tab.$colonna";
if (is_integer($colonna)) $colonna = $query['col'][$colonna];
$risul = $query[$riga][$colonna];

return $risul;

} # fine function risul_query



function numlin_query ($query) {

return $query['num'];

} # fine function numlin_query



function correggi_utf8_reale (&$str) {
# espressione regolare da standard w3: https://www.w3.org/International/questions/qa-forms-utf-8.en
if (!preg_match('%^(?:
   [\x09\x0A\x0D\x20-\x7E]           # ASCII
 | [\xC2-\xDF][\x80-\xBF]            # non-overlong 2-byte
 | \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
 | \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
 | \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
 | [\xF1-\xF3][\x80-\xBF]{3}         # planes 4-15
 | \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
)*$%xs',$str)) {
if (function_exists('mb_convert_encoding')) $str = mb_convert_encoding($str,'UTF-8','UTF-8');
else {
if (function_exists('iconv')) $str = @iconv('CP1252','UTF-8//IGNORE',$str);
else $str = "?";
} # fine else if (function_exists('mb_convert_encoding'))
} # fine if (!preg_match('%^(?:...
} # fine function correggi_utf8_reale

if (function_exists('version_compare') and version_compare(PHP_VERSION,'7.0.0','>=')) {
function correggi_utf8 (&$str) {
if (isset($str)) correggi_utf8_reale($str);
}
} # fine if (function_exists('version_compare') and version_compare(PHP_VERSION,'7.0.0','>='))
else {
function correggi_utf8 (&$str) {
# bug con preg_match troppo lungo di php5
if (isset($str) and strlen($str) < 7000) correggi_utf8_reale($str);
}
} # fine else if (function_exists('version_compare') and version_compare(PHP_VERSION,'7.0.0','>='))



function aggslashdb ($stringa) {
global $numconnessione;

correggi_utf8($stringa);
$risul = $numconnessione->escapeString((string) $stringa);
return $risul;

} # fine function aggslashdb



function arraylin_query ($query,$num) {

for ($num1 = 0 ; $num1 < $query['numcol'] ; $num1++) $risul[$num1] = $query[$num][$query['col'][$num1]];
return $risul;

} # fine function arraylin_query



function numcampi_query ($query) {

return $query['numcol'];

} # fine function numcampi_query



function nomecampo_query ($query,$num) {

return $query['col'][$num];

} # fine function nomecampo_query



function tipocampo_query ($query,$num) {

$risul = "unknown";
return $risul;

} # fine function tipocampo_query



function dimcampo_query ($query,$num) {

$risul = "unknown";
return $risul;

} # fine function dimcampo_query



function chiudi_query (&$query) {

$query = array();

} # fine function chiudi_query



function lock_tabelle ($tabelle,$altre_tab_usate = "") {
global $numconnessione;

$risul = $numconnessione->exec("begin transaction");
return $risul;

} # fine function lock_tabelle



function unlock_tabelle (&$tabelle_lock,$azione = "") {
global $numconnessione;

$numconnessione->exec("commit transaction");
$tabelle_lock = null;

} # fine function unlock_tabelle



function crea_indice ($tabella,$colonne,$nome) {
global $numconnessione;

$numconnessione->exec("create index $nome on $tabella ($colonne)");

} # fine function crea_indice



?>