Location: PHPKode > projects > Modularized Information Environment > mie/includes/start.inc
<?php
/**
 * @file includes/start.inc
 * @brief Main Library
 * @author Kenneth Smith <hide@address.com>
 *
 * This file contains common structures and methods that are accessible to every
 * page or script. It also includes the configuration file, making this the only
 * mandatory include file for a properly initialized script.
 *
 * Modularized Information Environment (MIE)
 * Copyright (C) 2005-2006 by Kenneth Smith. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */

defined('VALIE_MIE') || die(_('Direct access not allowed'));
defined('START_INC') && exit;
define('START_INC', true);

$conf['base'] = dirname(@$_SERVER['SCRIPT_NAME']);

require_once mie_configuration();
require_once 'includes/session.inc';
require_once 'includes/user.inc';
require_once 'includes/log.inc';

mie_load_settings();
mie_locale(mie_conf('locale', 'en_US'));
mie_timezone(mie_conf('timezone', 'US/Central'));

$session = new SESSION();
$user = new USER();
$log = new LOG();

/**
 * @brief Load Settings
 *
 * Loads the $conf variable with settings contained in the database.
 */
function mie_load_settings() {
	global $db, $conf;
	$sql = $db->rewrite('SELECT * FROM t{settings}');
	$conf = array_merge($conf, mie_extract_array($db->table($sql), 'key', 'value'));
}

/**
 * @brief Return Conf Value
 * @param $_id
 *   Key to return
 * @param $_default
 *   Default return value
 * @return
 *   Conf key value
 * Returns the value of a specified $conf key, or $_default if that key has not
 * been set yet.
 */
function mie_conf($_id, $_default = null) {
	global $conf;
	return isset($conf[$_id]) ? $conf[$_id] : $_default;
}

function mie_set_conf($_id, $_value) {
	global $conf;
	$conf[$_id] = $_value;
}

/**
 * @brief Prepend Root
 * @param $_file
 *   Path to appended file
 * @return
 *   root path + / + file path
 * @note
 * The root is the directory where MIE is stored.
 * Example: /srv/mie
 */
function mie_root($_file = '') {
	$path = mie_conf('root');
	if($_file) {
		$path .= "/$_file";
	}
	return $path;
}

/**
 * @brief Prepend Base
 * @param $_file
 *   Path to appended file
 * @return
 *   base url + / + file path
 * @note
 * The base is the url where MIE is visible.
 * Example: http://localhost/mie
 */
function mie_base($_file = '') {
	$path = mie_conf('base');
	if($_file) {
		$path .= "/$_file";
	}
	return $path;
}

/**
 * @brief Prepend Theme
 * @param $_file
 *   Path to appended file
 * @return
 *   theme path + / + file path
 * @note
 * The theme path does not have root or base on it. To get the full path to the
 * theme for includes use: mie_root(mie_theme( [file] )); To get the full url
 * use: mie_base(mie_theme( [file] ));
 */
function mie_theme($_file = '') {
	global $conf;
	if(isset($conf['theme']) && is_file(mie_root("themes/{$conf['theme']}/$_file"))) {
		return "themes/{$conf['theme']}/$_file";
	}
	else {
		return "themes/default/$_file";
	}
}

function mie_configuration() {
	$conf_file = 'sites/' . str_replace('/', '.', @$_SERVER['HTTP_HOST'] . mie_base('php'));
	if(is_file($conf_file)) {
		return $conf_file;
	}
	else {
		return 'sites/default.php';
	}
}

function mie_stamp($_format, $_stamp) {
	switch($_format) {
		case 'time_normal': case 'time';
			return date(mie_conf('time_normal'), $_stamp);
		break; case 'time_short':
			return date(mie_conf('time_short'), $_stamp);
		break; case 'time_long':
			return date(mie_conf('time_long'), $_stamp);
		break; case 'date_normal': case 'date':
			return date(mie_conf('date_normal'), $_stamp);
		break; case 'date_short':
			return date(mie_conf('date_short'), $_stamp);
		break; case 'date_long':
			return date(mie_conf('date_long'), $_stamp);
		break; default: case 'stamp_normal': case 'stamp': case 'normal':
			return date(mie_conf('stamp_normal'), $_stamp);
		break; case 'stamp_short':
			return date(mie_conf('stamp_short'), $_stamp);
		break; case 'stamp_long':
			return date(mie_conf('stamp_long'), $_stamp);
	}
}

function mie_mix_stamp($_date, $_time) {
	$_time = strtotime($_time);
	$_date = strtotime($_date);
	return mktime(
		date('G',$_time), date('i',$_time), date('s',$_time),
		date('n',$_date), date('j',$_date), date('Y',$_date)
	);
}

function mie_unslash(&$_array) {
	foreach($_array as $k => $v) {
		if(is_array($v)) {
			mie_unslash($_array[$k]);
		}
		else {
			$_array[$k] = stripslashes($v);
		}
	}
}

function mie_enum($_value, $_set, $_default = null) {
	return in_array($_value, $_set) ? $_value : $_default;
}

function mie_random($_length, $_use = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') {
	$r = '';
	$l = strlen($_use) - 1;
	for($i = 0; $i < $_length; $i++) {
		$r .= $_use[mt_rand(0, $l)];
	}
	return $r;
}

/**
 * @brief Extract Key
 *
 * Extract the values of a particular key from an array of arrays (table).
 */
function mie_extract_key($_arrays, $_key) {
	$r = array();
	foreach($_arrays as $a) {
		$r[] = $a[$_key];
	}
	return $r;
}

/**
 * @brief Extract Array
 *
 * Extract an array from an array of arrays (table).
 */
function mie_extract_array($_arrays, $_key = 'id', $_value = 'value') {
	$r = array();
	foreach($_arrays as $a) {
		$r[$a[$_key]] = $a[$_value];
	}
	return $r;
}

/**
 * @brief Keep Keys
 * @return
 *   Array of specified keys.
 * Only keep these specified keys.
 */
function mie_keep_keys($_array, $_keys) {
	$r = array();
	$_keys = array_intersect(array_keys($_value), $_keys);
	foreach($_keys as $k) {
		$r[$k] = $_array[$k];
	}
	return $r;
}

/**
 * @brief Change Location
 *
 * Sends the header to jump to a new location.
 */
function mie_jump($_path) {
	header("Location: $_path");
}

function mie_path() {
	global $conf;
	// Already set?
	if(!isset($conf['path'])) {
		if(isset($_GET['path'])) {
			$conf['path'] = $_GET['path'];
		}
		elseif(isset($_POST['path'])) {
			$conf['path'] = $_POST['path'];
		}
		elseif(isset($_SERVER['PATH_INFO'])) {
			$conf['path'] = $_SERVER['PATH_INFO'];
		}
		else {
			$conf['path'] = '/';
		}
		// Should not end with /
		$conf['path'] = ereg_replace('/+$', '', strip_tags($conf['path']));
		// Should begin with /
		if(substr($conf['path'], 0, 1) != '/') {
			$conf['path'] = '/' . $conf['path'];
		}
	}
	return $conf['path'];
}

function mie_mkquery($_query) {
	$ret = array();
	foreach($_query as $k => $v) {
		if(is_numeric($v) || is_string($v)) {
			$ret[] = urlencode($k) . '=' . implode('/', array_map('urlencode', explode('/', $v)));
		}
	}
	return implode('&', $ret);
}

function mie_page($_path = '', $_query = null) {
	$path = mie_base();
	$query = array();
	switch(mie_conf('pathing')) {
		default: case 'query':
			if($_path) {
				$path .= '/index.php';
				$query += array('path'=>$_path);
			}
		break; case 'rewrite':
			if($_path) {
				$path .= $_path;
			}
		break; case 'path_info':
			if($_path) {
				$path .= '/index.php' . $_path;
			}
	}
	if($_query && is_array($_query)) {
		$query += $_query;
	}
	if($query) {
		$path .= '?' . mie_mkquery($query);
	}
	return $path;
}

function mie_script($_path = '', $_query = null) {
	$path = mie_base();
	$query = array();
	switch(mie_conf('pathing')) {
		default: case 'query': case 'rewrite':
			if($_path) {
				$path .= '/script.php';
				$query += array('path'=>$_path);
			}
		break; case 'path_info':
			if($_path) {
				$path .= '/script.php' . $_path;
			}
	}
	if($_query && is_array($_query)) {
		$query += $_query;
	}
	if($query) {
		$path .= '?' . mie_mkquery($query);
	}
	return $path;
}

/**
 * @brief Set Locale
 * @param $_locale
 *   The locale to use
 * @return
 *   The value returned by setlocale.
 *
 * This method sets the locale for gettext.
 */
function mie_locale($_locale) {
	putenv("LANG=$_locale");
	putenv("LANGUAGE=$_locale");
	putenv("LC_MESSAGES=$_locale");
	return setlocale(LC_MESSAGES, $_locale);
}

function mie_timezone($_timezone) {
	ini_set('date.timezone', $_timezone);
	putenv("TZ=$_timezone");
}

function mie_message($_title, $_content, $_count = null, $_stamp = null){
	if(!isset($_SESSION['messages'])) {
		$_SESSION['messages'] = array();
	}
	if(!is_array($_SESSION['messages'])) {
		return false;
	}
	$message['title'] = $_title;
	$message['content'] = $_content;
	if($_count) {
		$message['count'] = $_count;
	}
	if($_stamp) {
		$message['stamp'] = $_stamp;
	}
	array_unshift($_SESSION['messages'], $message);
	return true;
}

function mie_warning($_title, $_content, $_count = null, $_stamp = null){
	if(!isset($_SESSION['warnings'])) {
		$_SESSION['warnings'] = array();
	}
	if(!is_array($_SESSION['warnings'])) {
		return false;
	}
	$warning['title'] = $_title;
	$warning['content'] = $_content;
	if($_count) {
		$warning['count'] = $_count;
	}
	if($_stamp) {
		$warning['stamp'] = $_stamp;
	}
	array_unshift($_SESSION['warnings'], $warning);
	return true;
}

/// Time or now
function mie_torn($_str) {
	$stamp = strtotime($_str);
	return ($stamp == false || $stamp == -1) ? strtotime('now') : $stamp;
}

?>
Return current item: Modularized Information Environment