00001 <?php
00002
00003
00014 define('DB_ERROR', 'a515ac9c2796ca0e23adbe92c68fc9fc');
00015
00064 function update_sql($sql) {
00065 $result = db_query($sql, true);
00066 return array('success' => $result !== FALSE, 'query' => check_plain($sql));
00067 }
00068
00082 function db_prefix_tables($sql) {
00083 global $db_prefix;
00084
00085 if (is_array($db_prefix)) {
00086 if (array_key_exists('default', $db_prefix)) {
00087 $tmp = $db_prefix;
00088 unset($tmp['default']);
00089 foreach ($tmp as $key => $val) {
00090 $sql = strtr($sql, array('{' . $key . '}' => $val . $key));
00091 }
00092 return strtr($sql, array('{' => $db_prefix['default'], '}' => ''));
00093 }
00094 else {
00095 foreach ($db_prefix as $key => $val) {
00096 $sql = strtr($sql, array('{' . $key . '}' => $val . $key));
00097 }
00098 return strtr($sql, array('{' => '', '}' => ''));
00099 }
00100 }
00101 else {
00102 return strtr($sql, array('{' => $db_prefix, '}' => ''));
00103 }
00104 }
00105
00124 function db_set_active($name = 'default') {
00125 global $db_url, $db_type, $active_db, $db_prefix;
00126 static $db_conns, $active_name = FALSE;
00127
00128 if (empty($db_url)) {
00129 include_once 'includes/install.inc';
00130 install_goto('install.php');
00131 }
00132
00133 if (!isset($db_conns[$name])) {
00134
00135 if (is_array($db_url)) {
00136 $connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default'];
00137 }
00138 else {
00139 $connect_url = $db_url;
00140 }
00141
00142 $db_type = substr($connect_url, 0, strpos($connect_url, '://'));
00143 $handler = "./includes/database.$db_type.inc";
00144
00145 if (is_file($handler)) {
00146 include_once $handler;
00147 }
00148 else {
00149 _db_error_page("The database type '" . $db_type . "' is unsupported. Please use either 'mysql' or 'mysqli' for MySQL, or 'pgsql' for PostgreSQL databases.");
00150 }
00151
00152 $db_conns[$name] = db_connect($connect_url);
00153
00154
00155 if (preg_match("/^simpletest\d+$/", $_SERVER['HTTP_USER_AGENT'])) {
00156 $db_prefix = $_SERVER['HTTP_USER_AGENT'];
00157 }
00158
00159 }
00160
00161 $previous_name = $active_name;
00162
00163 $active_name = $name;
00164 $active_db = $db_conns[$name];
00165
00166 return $previous_name;
00167 }
00168
00179 function _db_error_page($error = '') {
00180 global $db_type;
00181 drupal_maintenance_theme();
00182 drupal_set_header('HTTP/1.1 503 Service Unavailable');
00183 drupal_set_title('Site off-line');
00184
00185 $message = '<p>The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.</p>';
00186 $message .= '<hr /><p><small>If you are the maintainer of this site, please check your database settings in the <code>settings.php</code> file and ensure that your hosting provider\'s database server is running. For more help, see the <a href="http://drupal.org/node/258">handbook</a>, or contact your hosting provider.</small></p>';
00187
00188 if ($error && ini_get('display_errors')) {
00189 $message .= '<p><small>The ' . theme('placeholder', $db_type) . ' error was: ' . theme('placeholder', $error) . '.</small></p>';
00190 }
00191
00192 print theme('maintenance_page', $message);
00193 exit;
00194 }
00195
00199 function db_is_active() {
00200 global $active_db;
00201 return !empty($active_db);
00202 }
00203
00207 function _db_query_callback($match, $init = FALSE) {
00208 static $args = NULL;
00209 if ($init) {
00210 $args = $match;
00211 return;
00212 }
00213
00214 switch ($match[1]) {
00215 case '%d':
00216 return (int) array_shift($args);
00217 case '%s':
00218 return db_escape_string(array_shift($args));
00219 case '%%':
00220 return '%';
00221 case '%f':
00222 return (float) array_shift($args);
00223 case '%b':
00224 return db_encode_blob(array_shift($args));
00225 }
00226 }
00227
00239 function db_placeholders($arguments, $type = 'int') {
00240 $placeholder = db_type_placeholder($type);
00241 return implode(',', array_fill(0, count($arguments), $placeholder));
00242 }
00243
00247 define('DB_QUERY_REGEXP', '/(%d|%s|%%|%f|%b)/');
00248
00269 function _db_rewrite_sql($query = '', $primary_table = 'n', $primary_field = 'nid', $args = array()) {
00270 $where = array();
00271 $join = array();
00272 $distinct = FALSE;
00273 foreach (module_implements('db_rewrite_sql') as $module) {
00274 $result = module_invoke($module, 'db_rewrite_sql', $query, $primary_table, $primary_field, $args);
00275 if (isset($result) && is_array($result)) {
00276 if (isset($result['where'])) {
00277 $where[] = $result['where'];
00278 }
00279 if (isset($result['join'])) {
00280 $join[] = $result['join'];
00281 }
00282 if (isset($result['distinct']) && $result['distinct']) {
00283 $distinct = TRUE;
00284 }
00285 }
00286 elseif (isset($result)) {
00287 $where[] = $result;
00288 }
00289 }
00290
00291 $where = empty($where) ? '' : '(' . implode(') AND (', $where) . ')';
00292 $join = empty($join) ? '' : implode(' ', $join);
00293
00294 return array($join, $where, $distinct);
00295 }
00296
00316 function db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array()) {
00317 list($join, $where, $distinct) = _db_rewrite_sql($query, $primary_table, $primary_field, $args);
00318
00319 if ($distinct) {
00320 $query = db_distinct_field($primary_table, $primary_field, $query);
00321 }
00322
00323 if (!empty($where) || !empty($join)) {
00324 $pattern = '{
00325 # Beginning of the string
00326 ^
00327 ((?P<anonymous_view>
00328 # Everything within this set of parentheses is named "anonymous view"
00329 (?:
00330 [^()]++ # anything not parentheses
00331 |
00332 \( (?P>anonymous_view) \) # an open parenthesis, more "anonymous view" and finally a close parenthesis.
00333 )*
00334 )[^()]+WHERE)
00335 }x';
00336 preg_match($pattern, $query, $matches);
00337 if ($where) {
00338 $n = strlen($matches[1]);
00339 $second_part = substr($query, $n);
00340 $first_part = substr($matches[1], 0, $n - 5) . " $join WHERE $where AND ( ";
00341
00342 $haystack_reverse = strrev($second_part);
00343
00344
00345 foreach (array('PUORG', 'REDRO', 'TIMIL') as $needle_reverse) {
00346 $pos = strpos($haystack_reverse, $needle_reverse);
00347 if ($pos !== FALSE) {
00348
00349 $pos += 5;
00350 break;
00351 }
00352 }
00353 if ($pos === FALSE) {
00354 $query = $first_part . $second_part . ')';
00355 }
00356 else {
00357 $query = $first_part . substr($second_part, 0, -$pos) . ')' . substr($second_part, -$pos);
00358 }
00359 }
00360 else {
00361 $query = $matches[1] . " $join " . substr($query, strlen($matches[1]));
00362 }
00363 }
00364
00365 return $query;
00366 }
00367
00373 function db_escape_table($string) {
00374 return preg_replace('/[^A-Za-z0-9_]+/', '', $string);
00375 }
00376
00501 function db_create_table(&$ret, $name, $table) {
00502 $statements = db_create_table_sql($name, $table);
00503 foreach ($statements as $statement) {
00504 $ret[] = update_sql($statement);
00505 }
00506 }
00507
00519 function db_field_names($fields) {
00520 $ret = array();
00521 foreach ($fields as $field) {
00522 if (is_array($field)) {
00523 $ret[] = $field[0];
00524 }
00525 else {
00526 $ret[] = $field;
00527 }
00528 }
00529 return $ret;
00530 }
00531
00543 function db_type_placeholder($type) {
00544 switch ($type) {
00545 case 'varchar':
00546 case 'char':
00547 case 'text':
00548 case 'datetime':
00549 return '\'%s\'';
00550
00551 case 'numeric':
00552
00553
00554
00555
00556
00557
00558 return '%s';
00559
00560 case 'serial':
00561 case 'int':
00562 return '%d';
00563
00564 case 'float':
00565 return '%f';
00566
00567 case 'blob':
00568 return '%b';
00569 }
00570
00571
00572
00573 return 'unsupported type ' . $type . 'for db_type_placeholder';
00574 }
00575