Location: PHPKode > projects > SQLite PHP Admin > spa/include.php
<?php
/***********************************************************************

		Copyright (c) 2011, Andrew V. Pleshakov	(hide@address.com)
		All rights reserved.

		This file is part of SPA (SQLite PHP Admin)
		See file LICENSE.txt for details

************************************************************************/

@set_time_limit(0);
session_start();
include('conf.php');
define('DB_SYS_FILE', 'dbaliases.inf');// alias system file
define('LIC_FILE', 'licence.inf');// licence information file
define('SPA_VERSION', '2.01');	// version number

require_once('SQLite23.class.php');

$tar = array();	// arguments for templates
$tar['version_'] = SPA_VERSION;

if(isset($_POST['newenc'])) {
	setcookie('spa_chr',$_POST['newenc'],time()+7776000);
	$_COOKIE['spa_chr'] = $_POST['newenc'];
}

if(isset($_COOKIE['spa_chr']))
		$encoding = $_COOKIE['spa_chr'];
else	$encoding = DEF_CHARSET;
$tar['encoding'] = $encoding;

if(isset($_COOKIE['spa_loc']))
		$LOC_ = $_COOKIE['spa_loc'];
else	$LOC_ = 'en';

if(HTTP_AUTH) {
	if(!($_SERVER['PHP_AUTH_USER'] == HTTP_USER && $_SERVER['PHP_AUTH_PW'] == HTTP_PASS)) {
		header('WWW-Authenticate: Basic realm="Please authenticate to access SQLite PHP Admin"');
		header('HTTP/1.0 401 Unauthorized');
		die(design_render('misc/unauth'));
	}
}

$current_db = '';
if($_SESSION['currentdb'])
	$current_db = $_SESSION['currentdb'];
$tar['curdb'] = $current_db;

unset($pla_db);
if (file_exists($current_db))
	$pla_db =& new SQLite23($current_db);
	
////////////////////////
////  Functions ////////
////////////////////////
function top_links($current_table) {
	$csql = $s = '';
	if($_SESSION['lquery']) {
		$rr['table'] = $current_table;
		$rr['lquery'] = htmlentities($_SESSION['lquery'],ENT_QUOTES,DEF_CHARSET);
		$csql = design_render('extra/lquery',$rr);
		$_SESSION['lquery'] = '';
	}
	$s .= table_links($current_table,true);
	$s .= '<br /><br />'.$csql;
	return $s;
}

function table_links($curtbl, $ifdb = false) {
	$rr['table'] = urlencode($curtbl);
	$rr['ifdb'] = $ifdb;
	return design_render('extra/tblinks',$rr);
}

function print_dbinfo() {
	global $pla_db;
	$ar['ver'] = $pla_db->libVersion();
	$ar['currentdb'] = $_SESSION['currentdb'];
	return design_render('misc/dbinfo',$ar);
}

function putEncodings() {
	global $encoding;
	$list = array('BIG5','CP866','EUC-JP','GB2312','ISO-8859-1','ISO-8859-15',
					'KOI8-R','Shift_JIS','UTF-8','Windows-1251','Windows-1252');
	$s = '';
	foreach($list as $val) {
		$s .= "\t\t\t\t<option value=\"{$val}\"";
		if($encoding == $val) $s .= ' selected="selected"';
		$s .= ">{$val}</option>\n";
	}
	$rr['opts'] = $s;
	return design_render('extra/enc',$rr);
}
function select_numbers($name = 'columns',$from = 1, $to = MAX_NUMBER_TO_SELECT) {
	$s = "<select name=\"$name\">\n";
	for($i = $from; $i <= $to; $i++)
		$s .= "\t<option value=\"$i\">$i</option>\n";
	$s .= "</select>\n";
	return $s;
}
///////////////////////////////////////////////////
function raiseError($message, $info = 'SQLite error') {
	$tar['info'] = $info;
	$tar['message'] = $message;
	die(design_render('misc/sqlerr',$tar));
}

////////////////////////////////////////////////////////////////////////////////////////////
function print_column_types($type = 'INTEGER') {
	$types = array('INTEGER','FLOAT','VARCHAR','TEXT',
				'BLOB','TIMESTAMP','DATETIME','NUMERIC');
	$s = '';
	foreach($types as $val) {
		$s .= "\t\t\t\t<option value=\"$val\"";
		if($val == $type) $s .= ' selected="selected"';
		$s .= ">$val</option>\n";
	}
	return $s;
}
//////////////////////////////////////////////////////////////////////////////
//// Reading and writing aliases file [[ format: alias|path|description  ]] //
//////////////////////////////////////////////////////////////////////////////
function ReadAliasCFG(/*$conv = false*/) {	// return an array
	global $encoding;
	$myarr = @file(DB_SYS_FILE,FILE_IGNORE_NEW_LINES);
	if(!is_array($myarr)) return null;
	// if($conv && ($encoding != DEF_CHARSET))
		// foreach($myarr as $k => $v)
			// $myarr[$k] = iconv(DEF_CHARSET,"$encoding//IGNORE" ,$v);
	return $myarr;
}
function SaveAliasCFG($arr) {
	global $encoding;
	if(!is_array($arr)) return null;
	// if($encoding != DEF_CHARSET)
		// foreach($arr as $k => $v)
			// $arr[$k] = iconv($encoding,DEF_CHARSET,$v);
	$ff = implode("\r\n",$arr)."\r\n";
	file_put_contents(DB_SYS_FILE,$ff);
}
////////////////////////////////////////////////////////////////////////////////////////////
function design_render($template, $data = array()) {
		$content = file_get_contents('tpl/'.$GLOBALS['LOC_']."/{$template}.html");
		$content = design_render_text($content, $data);
		return $content;
}
function design_render_text($content, $data = array()) {
		$content = design_parse_function($content, $data);
		$content = design_parse($content, $data);
		return $content;
}
function design_parse_function($content, $data = array()) {
		preg_match_all('/\<\<(.*?)\>\>/is', $content, $res);
		if (@$res[1])
			foreach ($res[1] as $el) {
				$middle = design_parse($el, $data);
				$middle = '$result = '.$middle.';';
				eval($middle);
				$content = str_ireplace('<<'.$el.'>>', $result, $content);
			}
		return $content;
}
function design_parse($content, $data) {
		preg_match_all('/\%\%(.*?)\%\%/si', $content, $res);
		if (@$res[1])
			foreach ($res[1] as $el) $content = str_ireplace('%%'.$el.'%%', $data[$el], $content);
		return $content;
}

function FindLicence() {
	global $LIC;
	$LIC = array();
	if(file_exists(LIC_FILE)) {
		$far = gzuncompress(file_get_contents(LIC_FILE));
		$licar = unserialize($far);
		$fstr = $licar['data'].$licar['lic'].$licar['name'].$licar['loc'];
		if(crc32($fstr) == $licar['crc32']) $LIC = $licar;
	}
}
Return current item: SQLite PHP Admin