/web/htdocs/www.euroroma.net/home/zenphoto/zp-core/functions-db-MySQL.php


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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php

/**
 * Database core functions forthe  MySQL legacy library
 *
 * Note: PHP version 5 states that the MySQL library is "Maintenance only, Long term deprecation announced."
 * It recommends using the PDO::MySQL or the MySQLi library instead.
 *
 * @package core
 */
// force UTF-8 Ø

define('DATABASE_SOFTWARE''MySQL');
Define('DATABASE_MIN_VERSION''5.0.7');
Define('DATABASE_DESIRED_VERSION''5.5.0');

/**
 * Connect to the database server and select the database.
 * @param array $config the db configuration parameters
 * @param bool $errorstop set to false to omit error messages
 * @return true if successful connection
 */
function db_connect($config$errorstop true) {
    global 
$_zp_DB_connection$_zp_DB_details;
    
$_zp_DB_details unserialize(DB_NOT_CONNECTED);
    if (
function_exists('mysql_connect')) {
        
$_zp_DB_connection = @mysql_connect($config['mysql_host'], $config['mysql_user'], $config['mysql_pass']);
    } else {
        
$_zp_DB_connection NULL;
    }
    if (!
$_zp_DB_connection) {
        if (
$errorstop) {
            
zp_error(sprintf(gettext('MySQL Error: Zenphoto received the error %s when connecting to the database server.'), mysql_error()));
        }
        return 
false;
    }
    
$_zp_DB_details['mysql_host'] = $config['mysql_host'];
    if (!@
mysql_select_db($config['mysql_database'])) {
        if (
$errorstop) {
            
zp_error(sprintf(gettext('MySQL Error: MySQL returned the error %1$s when Zenphoto tried to select the database %2$s.'), mysql_error(), $config['mysql_database']));
        }
        return 
false;
    }
    
$_zp_DB_details $config;
    if (
array_key_exists('UTF-8'$config) && $config['UTF-8']) {
        
mysql_set_charset('utf8'$_zp_DB_connection);
    }
    
// set the sql_mode to relaxed (if possible)
    
@mysql_query('SET SESSION sql_mode="";');
    return 
$_zp_DB_connection;
}

/**
 * The main query function. Runs the SQL on the connection and handles errors.
 * @param string $sql sql code
 * @param bool $errorstop set to false to supress the error message
 * @return results of the sql statements
 * @since 0.6
 */
function query($sql$errorstop true) {
    global 
$_zp_DB_connection$_zp_DB_details;
    if (
$result = @mysql_query($sql$_zp_DB_connection)) {
        return 
$result;
    }
    if (
$errorstop) {
        
$sql str_replace('`' $_zp_DB_details['mysql_prefix'], '`[' gettext('prefix') . ']'$sql);
        
$sql str_replace($_zp_DB_details['mysql_database'], '[' gettext('DB') . ']'$sql);
        
trigger_error(sprintf(gettext('%1$s Error: ( %2$s ) failed. %1$s returned the error %3$s'), DATABASE_SOFTWARE$sqldb_error()), E_USER_ERROR);
    }
    return 
false;
}

/**
 * Runs a SQL query and returns an associative array of the first row.
 * Doesn't handle multiple rows, so this should only be used for unique entries.
 * @param string $sql sql code
 * @param bool $errorstop set to false to supress the error message
 * @return results of the sql statements
 * @since 0.6
 */
function query_single_row($sql$errorstop true) {
    
$result query($sql$errorstop);
    if (
is_resource($result)) {
        
$row mysql_fetch_assoc($result);
        
mysql_free_result($result);
        return 
$row;
    } else {
        return 
false;
    }
}

/**
 * Runs a SQL query and returns an array of associative arrays of every row returned.
 * @param string $sql sql code
 * @param bool $errorstop set to false to supress the error message
 * @param string $key optional array index key
 * @return results of the sql statements
 * @since 0.6
 */
function query_full_array($sql$errorstop true$key NULL) {
    
$result query($sql$errorstop);
    if (
is_resource($result)) {
        
$allrows = array();
        if (
is_null($key)) {
            while (
$row mysql_fetch_assoc($result)) {
                
$allrows[] = $row;
            }
        } else {
            while (
$row mysql_fetch_assoc($result)) {
                
$allrows[$row[$key]] = $row;
            }
        }
        
mysql_free_result($result);
        return 
$allrows;
    } else {
        return 
false;
    }
}

/**
 * mysql_real_escape_string standin that insures the DB connection is passed.
 *
 * @param string $string
 * @return string
 */
function db_quote($string) {
    global 
$_zp_DB_connection;
    return 
"'" mysql_real_escape_string($string$_zp_DB_connection) . "'";
}

/*
 * returns the insert id of the last database insert
 */

function db_insert_id() {
    return 
mysql_insert_id();
}

/*
 * Fetch a result row as an associative array
 */

function db_fetch_assoc($resource) {
    if (
$resource) {
        return 
mysql_fetch_assoc($resource);
    }
    return 
false;
}

/*
 * Returns the text of the error message from previous operation
 */

function db_error() {
    global 
$_zp_DB_connection;
    if (
is_object($_zp_DB_connection)) {
        return 
mysql_error();
    }
    if (!
$msg mysql_error())
        
$msg sprintf(gettext('%s not connected'), DATABASE_SOFTWARE);
    return 
$msg;
}

/*
 * Get number of affected rows in previous operation
 */

function db_affected_rows() {
    return 
mysql_affected_rows();
}

/*
 * Get a result row as an enumerated array
 */

function db_fetch_row($result) {
    if (
is_resource($result)) {
        return 
mysql_fetch_row($result);
    }
    return 
false;
}

/*
 * Get number of rows in result
 */

function db_num_rows($result) {
    return 
mysql_num_rows($result);
}

/**
 * Closes the database
 */
function db_close() {
    global 
$_zp_DB_connection;
    if (
$_zp_DB_connection) {
        
$rslt mysql_close($_zp_DB_connection);
    } else {
        
$rslt true;
    }
    
$_zp_DB_connection NULL;
    return 
$rslt;
}

/*
 * report the software of the database
 */

function db_software() {
    
$dbversion trim(@mysql_get_server_info());
    
preg_match('/[0-9,\.]*/'$dbversion$matches);
    return array(
'application' => DATABASE_SOFTWARE'required' => DATABASE_MIN_VERSION'desired' => DATABASE_DESIRED_VERSION'version' => $matches[0]);
}

/**
 * create the database
 */
function db_create() {
    global 
$_zp_DB_details;
    
$sql 'CREATE DATABASE IF NOT EXISTS ' '`' $_zp_DB_details['mysql_database'] . '`' db_collation();
    return 
query($sqlfalse);
}

/**
 * Returns user's permissions on the database
 */
function db_permissions() {
    global 
$_zp_DB_details;
    
$sql "SHOW GRANTS FOR " $_zp_DB_details['mysql_user'] . ";";
    
$result query($sqlfalse);
    if (!
$result) {
        
$result query("SHOW GRANTS;"false);
    }
    if (
is_resource($result)) {
        
$db_results = array();
        while (
$onerow db_fetch_row($result)) {
            
$db_results[] = $onerow[0];
        }
        return 
$db_results;
    } else {
        return 
false;
    }
}

/**
 * Sets the SQL session mode to empty
 */
function db_setSQLmode() {
    return 
query('SET SESSION sql_mode=""'false);
}

/**
 * Queries the SQL session mode
 */
function db_getSQLmode() {
    
$result query('SELECT @@SESSION.sql_mode;'false);
    if (
is_resource($result)) {
        
$row db_fetch_row($result);
        return 
$row[0];
    }
    return 
false;
}

function 
db_collation() {
    
$collation ' CHARACTER SET utf8 COLLATE utf8_unicode_ci';
    return 
$collation;
}

function 
db_create_table(&$sql) {
    return 
query($sqlfalse);
}

function 
db_table_update(&$sql) {
    return 
query($sqlfalse);
}

function 
db_show($what$aux '') {
    global 
$_zp_DB_details;
    switch (
$what) {
        case 
'tables':
            
$sql "SHOW TABLES FROM `" $_zp_DB_details['mysql_database'] . "` LIKE '" db_LIKE_escape($_zp_DB_details['mysql_prefix']) . "%'";
            return 
query($sqlfalse);
        case 
'columns':
            
$sql 'SHOW FULL COLUMNS FROM `' $_zp_DB_details['mysql_prefix'] . $aux '`';
            return 
query($sqltrue);
        case 
'variables':
            
$sql "SHOW VARIABLES LIKE '$aux'";
            return 
query_full_array($sql);
        case 
'index':
            
$sql "SHOW INDEX FROM `" $_zp_DB_details['mysql_database'] . '`.' $aux;
            return 
query_full_array($sql);
    }
}

function 
db_list_fields($table) {
    
$result db_show('columns'$table);
    if (
is_resource($result)) {
        
$fields = array();
        while (
$row db_fetch_assoc($result)) {
            
$fields[] = $row;
        }
        return 
$fields;
    } else {
        return 
false;
    }
}

function 
db_truncate_table($table) {
    global 
$_zp_DB_details;
    
$sql 'TRUNCATE ' $_zp_DB_details['mysql_prefix'] . $table;
    return 
query($sqlfalse);
}

function 
db_LIKE_escape($str) {
    return 
strtr($str, array('_' => '\\_''%' => '\\%'));
}

function 
db_free_result($result) {
    return 
mysql_free_result($result);
}

?>