connect_error) { $detail = 'connect error (' . $db->connect_errno . ') ' . $db->connect_error; $db = NULL; db_fail($detail); } return TRUE; } else return FALSE; } else if ($state == 'close') { if ($db) { $db->close(); $db = NULL; return TRUE; } else return FALSE; } } /* * TRUE if an editor account named $username exists. A session or a * persistent login of an editor whose account was removed ends with it. */ function editor_exists($username) { global $db; if (!is_string($username) || $username === '') return FALSE; $opened = open_db('open'); $result = $db->execute_query("SELECT username FROM editors WHERE username = ?", array($username)) or db_fail(); $row = $result->fetch_assoc(); if ($opened) open_db('close'); return (bool)$row; } /* * Stop the page after a database error. The details, with the file and * line of the failed call, go to the PHP error log; the visitor only * sees a short apology. */ function db_fail($detail = NULL) { global $db; if ($detail === NULL) $detail = $db ? $db->error : 'no database connection'; $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]; error_log('xxx: database error at ' . basename($caller['file']) . ':' . $caller['line'] . ': ' . $detail); if (!headers_sent()) header('HTTP/1.1 500 Internal Server Error'); die('

Sorry, something went wrong. Please try again later.

'); } ?>