Index: trunk/globals.php
===================================================================
diff -u -r25 -r392
--- trunk/globals.php	(.../globals.php)	(revision 25)
+++ trunk/globals.php	(.../globals.php)	(revision 392)
@@ -1,6 +1,6 @@
 <?php
 
-$vars = parse_ini_file($pathtoroot."config.php");
+$vars = parse_portal_ini($pathtoroot."config.php");
 
 while($key = key($vars))
 {
@@ -16,8 +16,8 @@
 $KeywordIgnore = array();
 global $debuglevel;
 
-$debuglevel=0;
-$_GLOBALS["debuglevel"]=0;
+$debuglevel = 0;
+//$GLOBALS['debuglevel'] = 0;
 
 /*New, Hot, Pop field values */
 define('NEVER', 0);
@@ -32,38 +32,94 @@
 $LogLevel=0;
 $LogFile = NULL;
 
+
+function parse_portal_ini($file, $parse_section = false) { 
+    if(!file_exists($file) && !is_readable($file))  
+        die('Could Not Open Ini File'); 
+     
+    $contents = file($file); 
+     
+    $retval = array(); 
+     
+    $section = ''; 
+    $ln = 1;
+    $resave = false;
+    foreach($contents as $line) { 
+    		if ($ln == 1 && $line != '<'.'?'.'php die() ?'.">\n") {
+    			$resave = true;
+    		}
+    		$ln++;
+        $line = trim($line); 
+        $line = eregi_replace(';[.]*','',$line); 
+        if(strlen($line) > 0) { 
+            //echo $line . " - "; 
+            if(eregi('^[[a-z]+]$',str_replace(' ', '', $line))) { 
+                //echo 'section'; 
+                $section = substr($line,1,(strlen($line)-2)); 
+                if ($parse_section) {
+                	$retval[$section] = array(); 
+                }
+                continue;
+            } elseif(eregi('=',$line)) { 
+                //echo 'main element'; 
+                list($key,$val) = explode(' = ',$line); 
+                if (!$parse_section) {
+                	$retval[trim($key)] = str_replace('"', '', $val); 
+                }
+                else {
+					$retval[$section][trim($key)] = str_replace('"', '', $val);                 	
+                }
+            } //end if 
+            //echo '<br />'; 
+        } //end if 
+    } //end foreach 
+    if ($resave) {
+    	$fp = fopen($file, "w");
+    	reset($contents);
+			fwrite($fp,'<'.'?'.'php die() ?'.">\n\n");
+			foreach($contents as $line) fwrite($fp,"$line");
+			fclose($fp);
+		}   
+    
+    return $retval; 
+}
+
+/**
+ * @return object
+ * @desc Returns reference to database connection
+*/
 function &GetADODBConnection()
 {
-	global $g_adodbConnection, $g_DBType, $g_DBHost,$g_DBUser,$g_DBUserPassword,$g_DBName,$g_DebugMode;
-    global $ADODB_FETCH_MODE,$ADODB_COUNTRECS,$ADODB_CACHE_DIR,$pathtoroot; 
-
-    if(!isset($g_adodbConnection) && strlen($g_DBType)>0)
-    {
-		$g_adodbConnection = ADONewConnection($g_DBType);
-		$connected = $g_adodbConnection->Connect($g_DBHost,$g_DBUser,$g_DBUserPassword,$g_DBName);
-        if(!$connected)
-        {
-            echo "Error connecting to database $g_DBHost <br>\n";
-            die();
-        }
-        $ADODB_CACHE_DIR = $pathtoroot."cache"; 
-        $ADODB_FETCH_MODE = 2;    
-        $ADODB_COUNTRECS = FALSE; 
-        $g_adodbConnection->debug = false;
-        $g_adodbConnection->cacheSecs = 3600;
-    }
-    elseif (!strlen($g_DBType)) {
-    	global $rootURL;
-    	echo "In-Portal is probably not installed, or configuration file is missing.<br>";
-			echo "Please use the installation script to fix the problem.<br><br>";
-			if (!preg_match('/admin/', $_SERVER['SCRIPT_FILENAME'])) {
-				$ins = 'admin/'; 
-			}
-			echo "<a href='$rootURL".$ins."install.php'>Go to installation script</a><br><br>";
-			flush();
-			die();
-    }
-	return $g_adodbConnection;
+	static $DB = null;
+	
+	global $g_DBType, $g_DBHost, $g_DBUser, $g_DBUserPassword, $g_DBName, $g_DebugMode;
+	global $ADODB_FETCH_MODE, $ADODB_COUNTRECS, $ADODB_CACHE_DIR, $pathtoroot;
+	
+	if( !isset($DB) && strlen($g_DBType) > 0 )
+	{
+		$DB = ADONewConnection($g_DBType);
+		$connected = $DB->Connect($g_DBHost, $g_DBUser, $g_DBUserPassword, $g_DBName);
+		if(!$connected) die("Error connecting to database $g_DBHost <br>\n");
+		
+		$ADODB_CACHE_DIR = $pathtoroot."cache";
+		$ADODB_FETCH_MODE = 2;
+		$ADODB_COUNTRECS = false;
+		$DB->debug = defined('ADODB_OUTP') ? 1 : 0;
+		$DB->cacheSecs = 3600;
+		$DB->Execute('SET SQL_BIG_SELECTS = 1');
+	}
+	elseif( !strlen($g_DBType) )
+	{
+		global $rootURL;
+		echo 'In-Portal is probably not installed, or configuration file is missing.<br>';
+		echo 'Please use the installation script to fix the problem.<br><br>';
+		if ( !preg_match('/admin/', $_SERVER['SCRIPT_FILENAME']) ) $ins = 'admin/';
+		
+		echo '<a href="'.$rootURL.$ins.'install.php">Go to installation script</a><br><br>';
+		flush();
+		exit;
+	}
+	return $DB;
 }
 
 function GetNextResourceId($Increment=1)
@@ -134,6 +190,7 @@
         $sql = "SELECT count(*) as TableCount FROM $TableName INNER JOIN ".GetTablePrefix()."CategoryItems ON ".GetTablePrefix()."CategoryItems.ItemResourceId=$TableName.ResourceId";
     if(strlen($where)>0)
         $sql .= " WHERE ".$where;
+   
     $rs = $db->Execute($sql);
         
 //    echo "SQL TABLE COUNT: ".$sql."<br>\n";
@@ -168,6 +225,10 @@
 }
 
 
+/**
+ * @return string
+ * @desc Returns database table prefix entered while installation
+*/
 function GetTablePrefix()
 {
     global $g_TablePrefix;
@@ -590,8 +651,7 @@
           $found = FALSE;
           if(is_array($_FILES))
           {
-            if($_FILES[$r]["size"]>0)
-                $found=TRUE;
+            if( isset($_FILES[$r]) && $_FILES[$r]['size'] > 0 ) $found = TRUE;
           }
         
           if(!strlen(trim($_POST[$r])) && !$found)
@@ -601,7 +661,7 @@
             if (($r == "dob_day") || ($r == "dob_month") || ($r == "dob_year"))
             	$r = "dob";            	
             	
-            $tag = $_POST["errors"][$r];
+            $tag = isset($_POST["errors"]) ? $_POST["errors"][$r] : '';
             if(!strlen($tag))
               $tag = "lu_ferror_".$f."_".$r; 
             $FormError[$f][$r] = language($tag);
@@ -830,23 +890,19 @@
 {
 	global $objCountCache, $objItemTypes;
 
-  if(is_numeric($ItemName))
-  {
-  	$item = $objItemTypes->GetItem($ItemName);
-  }
-  else
-    $item = $objItemTypes->GetTypeByName($ItemName);
-  if(is_object($item))
-  {	
-  	$ItemType  = $item->Get("ItemType");
+  	if(is_numeric($ItemTypeName))
+  		$item = $objItemTypes->GetItem($ItemTypeName);
+  	else
+    	$item = $objItemTypes->GetTypeByName($ItemTypeName);
+  
+  	if(is_object($item))
+  	{	
+  		$ItemType  = $item->Get("ItemType");
   	
-	$sql = "DELETE FROM ".$objCountCache->SourceTable." WHERE ItemType=$ItemType";
-	if(is_numeric($ListType))
-	{
-		$sql .= " AND ListType=$ListType";
-	}
-	$objCountCache->adodbConnection->Execute($sql);
-  }  
+		$sql = "DELETE FROM ".$objCountCache->SourceTable." WHERE ItemType=$ItemType";
+		if( is_numeric($ListType) ) $sql .= " AND ListType=$ListType";
+		$objCountCache->adodbConnection->Execute($sql);
+  	}  
 }
 
 function UpdateModifiedCategoryCount($ItemTypeName,$CatId=NULL,$Modifier=0,$ExtraId=NULL)
@@ -883,7 +939,7 @@
     global $objSystemCache, $objSession, $objConfig;
 
     if($objConfig->Get("SystemTagCache"))
-    {    
+    {
         $name = $tag;
         if(is_array($attribs))
         {    
@@ -1103,7 +1159,8 @@
 function ResetPage($module_prefix, $page_variable = 'p')
 {
 	// resets page in specific module when category is changed	
-	if(!is_object($objSession)) // when changing pages session doesn't exist -> InPortal BUG
+	global $objSession;
+	if( !is_object($objSession) ) // when changing pages session doesn't exist -> InPortal BUG
 	{
 		global $var_list, $SessionQueryString, $FrontEnd;
 		//if(!$var_list["sid"]) $var_list["sid"] = $_COOKIE["sid"];
@@ -1117,9 +1174,15 @@
 	if($prev_cat != $last_cat) $GLOBALS[$module_prefix.'_var_list'][$page_variable] = 1;
 }
 
-function GetVar($name)
+if( !function_exists('GetVar') )
 {
-	return isset($_REQUEST[$name]) ? $_REQUEST[$name] : false;
+	function GetVar($name, $post_priority = false)
+	{
+		if(!$post_priority) // follow gpc_order in php.ini
+			return isset($_REQUEST[$name]) ? $_REQUEST[$name] : false;
+		else	// get variable from post 1stly if not found then from get
+			return isset($_POST[$name]) && $_POST[$name] ? $_POST[$name] : ( isset($_GET[$name]) && $_GET[$name] ? $_GET[$name] : false );	
+	}
 }
 
 function PassVar(&$source)
@@ -1156,9 +1219,14 @@
 function GetModuleByAction()
 {
 	$prefix2module = Array('m' => 'In-Portal', 'l' => 'In-Link', 'n' => 'In-News', 'bb' => 'In-Bulletin');
-	$action = $_REQUEST['Action'];
-	$module_prefix = explode('_', $action);
-	return $prefix2module[ $module_prefix[0] ];
+	$action = GetVar('Action');
+	if($action)
+	{
+		$module_prefix = explode('_', $action);
+		return $prefix2module[ $module_prefix[0] ];
+	}
+	else
+		return false;
 }
 
 function dir_size($dir) {
@@ -1268,4 +1336,82 @@
 	return count($result) ? implode(' ', $result) : false;
 }
 
+function GetImportScripts()
+{
+	// return currently installed import scripts	
+	static $import_scripts = Array();
+	if( count($import_scripts) == 0 )
+	{
+		$sql = 'SELECT * FROM '.GetTablePrefix().'ImportScripts ORDER BY is_id';
+		$db =& GetADODBConnection();
+		$rs = $db->Execute($sql);
+		while(!$rs->EOF)
+		{
+			$rec =& $rs->fields;
+			$import_scripts[] = Array(	'label' => $rec['is_label'], 'url' => $rec['is_script'], 
+										'enabled' => $rec['is_enabled'], 'field_prefix' => $rec['is_field_prefix'],
+										'id' => $rec['is_string_id'], 'required_fields' => $rec['is_requred_fields'],
+										'module' => strtolower($rec['is_Module']) );
+			$rs->MoveNext();	
+		}
+	}
+	return $import_scripts;
+}
+
+function GetImportScript($id)
+{
+	$scripts = GetImportScripts();
+	return isset($scripts[$id]) ? $scripts[$id] : false;
+}
+function GetNextTemplate($current_template) 
+{ 
+	// used on front, returns next template to make 
+	// redirect to 
+	$dest = GetVar('dest', true); 
+	if(!$dest) $dest = GetVar('DestTemplate', true); 
+	return $dest ? $dest : $current_template; 
+}
+
+
+// functions for dealign with enviroment variable construction
+function GenerateModuleEnv($prefix, $var_list)
+{
+	// globalize module varible arrays
+	$main =& $GLOBALS[$prefix.'_var_list'];
+	$update =& $GLOBALS[$prefix.'_var_list_update'];
+	
+	//echo "VAR: [$main]; VAR_UPDATE: [$update]<br>";
+	
+	// enshure that we have no empty values in enviroment variable
+	foreach($update as $vl_key => $vl_value)
+    	if(!$vl_value) $update[$vl_key] = '0'; // unset($update[$vl_key]);
+   
+    // if update var count is zero, then do nothing
+    if(count($update) == 0) return '';
+
+    foreach($main as $vl_key => $vl_value)
+    	if(!$vl_value) $main[$vl_key] = '0'; // unset($main[$vl_key]);
+    
+	$ret = Array();
+	foreach($var_list as $var_name)
+		$ret[] = GetEnvVar($prefix, $var_name);
+	
+	return ':'.$prefix.implode('-',$ret);
+}
+
+function GetEnvVar($prefix, $name)
+{
+	// get variable from template variable's list
+	// (used in module parsers to build env string)
+	$main =& $GLOBALS[$prefix.'_var_list'];
+	$update =& $GLOBALS[$prefix.'_var_list_update'];
+	return isset($update[$name]) ? $update[$name] : ( isset($main[$name]) ? $main[$name] : '');
+}
+
+function IsDebugMode()
+{
+	return defined('DEBUG_MODE') && constant('DEBUG_MODE') == 1 ? 1 : 0;	
+}
+
+
 ?>