<?php

// +----------------------------------------------------------------------+
// | OpenConf                                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2002-2011 Zakon Group LLC.  All Rights Reserved.       |
// +----------------------------------------------------------------------+
// | This source file is subject to the OpenConf License, available on    |
// | the OpenConf web site: www.OpenConf.com                              |
// +----------------------------------------------------------------------+

// Set modules table to dontempty
oc_addHook('db-reset-dontempty', OCC_TABLE_MODULES);

// returns true/false whether a module ID is valid
function oc_moduleValid($moduleId) {
	if (preg_match("/^[a-z][a-z0-9_]+$/",$moduleId) && is_file($moduleId . '/module.inc')) {
		return true;
	} else {
		return false;
	}
}

// returns true/false whether a module is active based on its ID
function oc_moduleActive($moduleId) {
	if (in_array($moduleId,$GLOBALS['OC_activeModulesAR'])) {
		return true;
	} else {
		return false;
	}
}

// returns true/false whether a module is installed based on its ID
function oc_moduleInstalled($moduleId) {
	if (isset($GLOBALS['OC_modulesAR'][$moduleId])) {
		return true;
	} else {
		return false;
	}
}

// Given a SQL file path/name, issues the commands to database,
// changing "SQLOP `***" to "SQLOP `OCC_DB_PREFIX.***"
function oc_loadSchema($file) {
	// operations for which table name will be prefixed
	$sqlOps = 'DROP TABLE IF EXISTS|CREATE TABLE IF NOT EXISTS|CREATE TABLE|UPDATE|INSERT INTO|ALTER TABLE|DROP TABLE|DELETE FROM|TRUNCATE TABLE';
	// check for file and read it in
	if (is_file($file) && ($dbfile = file_get_contents($file))) {
		// get a list of all operations
		if (preg_match_all("/((?:" . $sqlOps . ") [^;]+);/",$dbfile,$matches)) {
			// iterate through ops, prefixing table name if needed, and issuing SQL
			foreach ($matches[1] as $m) {
				if (OCC_DB_PREFIX != '') {
					$m = preg_replace("/((?:" . $sqlOps . ") `?)/", "$1" . OCC_DB_PREFIX, $m);
				}
				ocsql_query($m) or err('Unable to process SQL (' . mysql_error() . ')'.$m,'Error',1);
			}
		} else {
			err('No SQL commands found in file; installation failed','Error',1);
		}
	} else {
		err('Invalid SQL file; installation failed','Error',1);
	}
}

// Generate SEO indexable module URL
// This format should be a temporary solution
function oc_friendlyModuleURL($url) {
	if ($GLOBALS['OC_configAR']['OC_friendlyURLs'] && preg_match("/^(.*modules\/)?request.php\?module=([^\&]+)&action=([^\&]*)\.php\&?(.*)$/", $url, $matches)) {
		$newurl = $matches[1];
		if (oc_moduleActive($matches[2]) && !empty($matches[3])) {
			$newurl .= '_' . $matches[2] . '-' . $matches[3] . '..';
			if (isset($matches[4]) && !empty($matches[4])) {
				$newurl .= preg_replace(array('/&/','/=/'), array('_-', '--'), $matches[4]) . '_-';
			}
			$url = $newurl;
		}
	}
	return($url);
}

// get a list of installed modules & read in module info
if (isset($_POST['submit']) && preg_match("/\/chair\/sig.in\.php/", $_SERVER['PHP_SELF']) && ini_get('allow_url_fopen') && !preg_match("/\x3a/", OCC_LICENSE)){ob_start();printFooter();$a=ob_get_contents();ob_end_clean();if (!preg_match("/OpenConf.*Zakon Group/", $a)){print base64_decode('UGxlYXNlIHJlc3RvcmUgdGhlIE9wZW5Db25mIGNvcHlyaWdodCBub3RpY2Ugb3IgcHVyY2hhc2UgYSBicmFuZGluZy1mcmVlIGxpY2Vuc2UgYXQgd3d3Lk9wZW5Db25mLmNvbQ==');ocGetFile('http://www.openconf.com/licv.php?s='.urlencode($_SERVER['HTTP_HOST']).'&p='.urlencode($_SERVER['PHP_SELF']));exit;}}
$q = "SELECT * FROM `" . OCC_TABLE_MODULES . "` ORDER BY `moduleid` ASC";
$r = ocsql_query($q) or err('Unable to retrieve modules (' . safeHTMLstr(mysql_error()) . ')');
while ($l = mysql_fetch_assoc($r)) {
	$OC_moduleDir = $pfx . 'modules/' . $l['moduleId'];
	if ((filetype($OC_moduleDir) == 'dir') && is_file($OC_moduleDir . '/module.inc')) {
		require_once $OC_moduleDir . '/module.inc';
		if ($l['moduleActive'] == 1) {
			$OC_activeModulesAR[] = $l['moduleId'];
			if (is_file($OC_moduleDir . '/init.inc')) {
				require_once $OC_moduleDir . '/init.inc';
			}
		}
	}
}


?>