'Thomas Urban', 'email' => 'soletan@nihilum.de', 'date' => '2009-11-18', 'name' => 'database2', 'desc' => 'Provides console for querying SQL commands ' . 'to local SQLite database file', 'url' => 'http://wiki.nihilum.de/software:database2', ); } /** * handle user request */ public function handle() { } /** * output appropriate html */ public function html() { if ( !$this->getConf( 'console' ) ) { ptln( $this->getLang( 'consoleoff' ) ); return; } if ( $this->getConf( 'consoleforcehistory') ) { @session_start(); $useHistory = true; } else if ( $useHistory = !headers_sent() ) session_start(); else ptln( $this->getLang( 'consolesession' ) ); $db = $this->connect( $_REQUEST['dbfile'] ); if ( $db ) { if ( $_GET['sectok'] != getSecurityToken() ) $query = ''; else $query = trim( $_GET['q'] ); $queryEsc = strtr( $query, array( '<' => '<' ) ); if ( $useHistory && ( $query !== '' ) ) { if ( !is_array( $_SESSION['DATABASE2_CONSOLE_HISTORY'] ) ) $_SESSION['DATABASE2_CONSOLE_HISTORY'] = array(); $HISTORY =& $_SESSION['DATABASE2_CONSOLE_HISTORY']; $index = array_search( $query, $HISTORY ); if ( $index !== false ) unset( $HISTORY[$index] ); array_unshift( $HISTORY, $query ); $HISTORY = array_slice( $HISTORY, 0, 20 ); } $btn = $this->getLang( 'consolebtn' ); $sqliteLabel = $this->getLang( 'consolesqlitedoc' ); $dbSelectorLabel = $this->getLang( 'consoledbselector' ); $helperShortcutsLabel = $this->getLang( 'consolehelpershortcuts' ); $helperKeys = $this->getLang( 'consolehelperkeys' ); $helperLocks = $this->getLang( 'consolehelperlocks' ); $helperLog = $this->getLang( 'consolehelperlog' ); $helperTables = $this->getLang( 'consolehelpertables' ); $helperVac = $this->getLang( 'consolehelpervac' ); $helperTemplatesLabel = $this->getLang( 'consolehelpertemplates' ); $helperRead = $this->getLang( 'consolehelperread' ); $helperReadSQL = $this->getLang( 'consolehelperreadsql' ); $helperEdit = $this->getLang( 'consolehelperedit' ); $helperEditSQL = $this->getLang( 'consolehelpereditsql' ); $helperDelete = $this->getLang( 'consolehelperdelete' ); $helperDeleteSQL = $this->getLang( 'consolehelperdeletesql' ); $helperAdd = $this->getLang( 'consolehelperadd' ); $helperAddSQL = $this->getLang( 'consolehelperaddsql' ); if ( $useHistory ) { $helperHistoryLabel = $this->getLang( 'consolehelperhistory' ); $history = array(); if ( is_array( $HISTORY ) ) foreach ( $HISTORY as $q ) { $qesc = strtr( $q, array( '<' => '<' ) ); $q = strtr( $q, array( '"' => '"' ) ); $history[] = ''; } $history = implode( "\n", $history ); $history = << $helperHistoryLabel EOT; } $sectok = getSecurityToken(); echo <<
$dbSelectorLabel
$helperShortcutsLabel $helperKeys | $helperLocks | $helperLog | $helperTables | $helperVac
$helperTemplatesLabel $helperRead | $helperEdit | $helperDelete | $helperAdd
$history
| $sqliteLabel
EOT; if ( $query !== '' ) { echo << EOT; try { $result = $db->getLink()->query( $query ); if ( $result instanceof PDOStatement ) { $rows = $result->fetchAll( PDO::FETCH_ASSOC ); if ( count( $rows ) ) { $first = array_slice( $rows, 0, 1 ); $cols = empty( $first ) ? array() : array_keys( $first ); echo $db->__renderTable( null, $cols, $rows, count( $rows ), count( $rows ), 0, null, array(), false, true ); } else echo $this->getLang( 'consolegoodresult' ); } else var_dump( $result ); } catch ( PDOException $e ) { echo '
' . $e->getMessage() . '
'; } echo ''; } } } /** * Connects to local database. * * @return Database2_Admin */ public function connect( $explicitDBPathname = null ) { if ( !( $this->db instanceof Database2_Admin ) ) { self::includeLib(); $db = new Database2_Admin( $this ); $dbFile = trim( $explicitDBPathname ); if ( $dbFile === '' ) $dbFile = $_REQUEST['id']; if ( $db->connect( $dbFile ) ) $this->db = $db; } return $this->db; } public static function includeLib() { if ( !class_exists( 'Database2_Admin' ) ) { $libFile = dirname( __FILE__ ) . '/database2.php'; // support working with development version if available and // selected to enable development in a production wiki // (as used on wiki.nihilum.de) if ( is_file( dirname( __FILE__ ) . '/database2.dev.php' ) ) { @session_start(); if ( $_GET['use_dev'] ) $_SESSION['useDevIP'] = $_SERVER['REMOTE_ADDR']; if ( $_GET['use_prod'] ) unset( $_SESSION['useDevIP'] ); if ( $_SESSION['useDevIP'] ) if ( $_SESSION['useDevIP'] == $_SERVER['REMOTE_ADDR'] ) $libFile = dirname( __FILE__ ) . '/database2.dev.php'; } { include_once( $libFile ); } } } }