Page 1 of 1

index.php causing Server Error

Posted: Sat Mar 05, 2011 7:32 am
by mgessner
Hi,

I have a site that has been running fine for 2 years and suddenly in the last few days, it has started going up and down. After investigation with hostgator, it was determined the max number of processes on the server is reached which causes a server error. There are processes that don't finish and hang around and have to be killed. These are all related to the index.php script. My understanding is that this script should run very quickly and exit and they shouldn't be hanging around.

The traffic to the site is not higher than normal and I haven't ever touched the index.php file or made any other changes to the site for a couple of months.

Hostgator have now taken down the site until I 'fix' the index.php script.

Hostgator recommend that the homepage should be cached so that this script doesn't have to run all the time.

I can't give version numbers as I can't login to the admin panel anymore.

Would really appreciate any help or suggestions what to do.

Thanks

Martin

Re: index.php causing Server Error

Posted: Sat Mar 05, 2011 3:06 pm
by jmcgin51
have you looked at the index.php file to see if it contains any odd code. You may have been hacked. If it only started in the last few days, either something on your site changed (i.e. a hack) or something in their server configuration changed. Hostgator should be able to tell you if something on their side changed.

Re: index.php causing Server Error

Posted: Sat Mar 05, 2011 11:12 pm
by Dr.CSS
If you can FTP to the site in the root is a filer called version.php that will tell you what version of CMSMS you have...

Re: index.php causing Server Error

Posted: Sun Mar 06, 2011 5:24 am
by mgessner
I have ftped to the site and found the version is :

$CMS_VERSION = "1.5.4";
$CMS_VERSION_NAME = "Carolina";
$CMS_SCHEMA_VERSION = "31";

I see that is an old version and I probably should upgrade, but as the site has been running fine for a couple of years, I haven't touched it. Before thinking about upgrading, I would like to get the site back up and running if possible.

I have had a look at the index.php and don't see anything obvious, but I don't know php well. I have pasted below in case anyone is able to assist?

<?php
#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#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
#
#$Id: index.php 5465 2009-03-02 18:44:01Z calguy1000 $

$orig_memory = (function_exists('memory_get_usage')?memory_get_usage():0);
$dirname = dirname(__FILE__);
require_once($dirname.'/fileloc.php');

/**
* Entry point for all non-admin pages
*
* @package CMS
*/
#echo '<code style="align: left;">';
#var_dump($_SERVER);
#echo '</code>';

$starttime = microtime();

clearstatcache();

if (!isset($_SERVER['REQUEST_URI']) && isset($_SERVER['QUERY_STRING']))
{
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
}

if (!file_exists(CONFIG_FILE_LOCATION) || filesize(CONFIG_FILE_LOCATION) < 800)
{
require_once($dirname.'/lib/misc.functions.php');
if (FALSE == is_file($dirname.'/install/index.php')) {
die ('There is no config.php file or install/index.php please correct one these errors!');
} else {
redirect('install/');
}
}
else if (file_exists(TMP_CACHE_LOCATION.'/SITEDOWN'))
{
echo "<__html><head><title>Maintenance</title></head></__body><p>Site down for maintenance.</p><__body></__html>";
exit;
}

if (!is_writable(TMP_TEMPLATES_C_LOCATION) || !is_writable(TMP_CACHE_LOCATION))
{
echo '<__html><title>Error</title></head></__body>';
echo '<p>The following directories must be writable by the web server:<br />';
echo 'tmp/cache<br />';
echo 'tmp/templates_c<br /></p>';
echo '<p>Please correct by executing:<br /><em>chmod 777 tmp/cache<br />chmod 777 tmp/templates_c</em><br />or the equivilent for your platform before continuing.</p>';
echo '<__body></__html>';
exit;
}

require_once($dirname.'/include.php'); #Makes gCms object


// optionally enable output compression (as long as debug mode isn't on)
if( isset($config['output_compression']) && ($config['output_compression']) && $config['debug'] != true )
{
@ob_start('ob_gzhandler');
}
else
{
@ob_start();
}


$params = array_merge($_GET, $_POST);

$smarty = &$gCms->smarty;
$smarty->params = $params;

$page = '';

if (isset($params['mact']))
{
$ary = explode(',', cms_htmlentities($params['mact']), 4);
$smarty->id = (isset($ary[1])?$ary[1]:'');
}
else
{
$smarty->id = (isset($params['id'])?intval($params['id']):'');
}

if (isset($smarty->id) && isset($params[$smarty->id . 'returnid']))
{
$page = $params[$smarty->id . 'returnid'];
}
else if (isset($config["query_var"]) && $config["query_var"] != '' && isset($_GET[$config["query_var"]]))
{
$page = $_GET[$config["query_var"]];

//trim off the extension, if there is one set
if ($config['page_extension'] != '' && endswith($page, $config['page_extension']))
{
$page = substr($page, 0, strlen($page) - strlen($config['page_extension']));
}
}
else
{
$calced = cms_calculate_url();
if ($calced != '')
$page = $calced;
}

//See if our page matches any predefined routes
$page = rtrim($page, '/');
if (strpos($page, '/') !== FALSE)
{

$routes =& $gCms->variables['routes'];

$matched = false;
foreach ($routes as $route)
{
$matches = array();
if (preg_match($route->regex, $page, $matches))
{
//Now setup some assumptions
if (!isset($matches['id']))
$matches['id'] = 'cntnt01';
if (!isset($matches['action']))
$matches['action'] = 'defaulturl';
if (!isset($matches['inline']))
$matches['inline'] = 0;
if (!isset($matches['returnid']))
$matches['returnid'] = ''; #Look for default page
if (!isset($matches['module']))
$matches['module'] = $route->module;

//Get rid of numeric matches
foreach ($matches as $key=>$val)
{
if (is_int($key))
{
unset($matches[$key]);
}
else
{
if ($key != 'id')
$_REQUEST[$matches['id'] . $key] = $val;
}
}

//Now set any defaults that might not have been in the url
if (isset($route->defaults) && count($route->defaults) > 0)
{
foreach ($route->defaults as $key=>$val)
{
$_REQUEST[$matches['id'] . $key] = $val;
if (array_key_exists($key, $matches))
{
$matches[$key] = $val;
}
}
}

//Get a decent returnid
if ($matches['returnid'] == '') {
global $gCms;
$contentops =& $gCms->GetContentOperations();
$matches['returnid'] = $contentops->GetDefaultPageID();
}

$_REQUEST['mact'] = $matches['module'] . ',' . $matches['id'] . ',' . $matches['action'] . ',' . $matches['inline'];

$page = $matches['returnid'];
$smarty->id = $matches['id'];

$matched = true;
}
}

if (!$matched)
{
$page = substr($page, strrpos($page, '/') + 1);
}
}
if ($page == '')
{
global $gCms;
$contentops =& $gCms->GetContentOperations();
$page =& $contentops->GetDefaultContent();
}
else
{
$page = preg_replace('/\</','',$page);
}

$pageinfo = PageInfoOperations::LoadPageInfoByContentAlias($page);

if (isset($pageinfo) && $pageinfo !== FALSE)
{
$gCms->variables['pageinfo'] =& $pageinfo;

if( isset($pageinfo->template_encoding) &&
$pageinfo->template_encoding != '' )
{
set_encoding($pageinfo->template_encoding);
}

if($pageinfo->content_id > 0)
{
$manager =& $gCms->GetHierarchyManager();
$node =& $manager->sureGetNodeById($pageinfo->content_id);
if(is_object($node))
{
$contentobj =& $node->GetContent();
$smarty->assign('content_obj',$contentobj);
}
}

$gCms->variables['content_id'] = $pageinfo->content_id;
$gCms->variables['page'] = $page;
$gCms->variables['page_id'] = $page;

$gCms->variables['page_name'] = $pageinfo->content_alias;
$gCms->variables['position'] = $pageinfo->content_hierarchy;
global $gCms;
$contentops =& $gCms->GetContentOperations();
$gCms->variables['friendly_position'] = $contentops->CreateFriendlyHierarchyPosition($pageinfo->content_hierarchy);

$smarty->assign('content_id', $pageinfo->content_id);
$smarty->assign('page', $page);
$smarty->assign('page_id', $page);
$smarty->assign('page_name', $pageinfo->content_alias);
$smarty->assign('page_alias', $pageinfo->content_alias);
$smarty->assign('position', $pageinfo->content_hierarchy);
$smarty->assign('friendly_position', $gCms->variables['friendly_position']);
}
else if (get_site_preference('enablecustom404') == '' || get_site_preference('enablecustom404') == "0")
{
ErrorHandler404();
exit;
}

$html = '';
$cached = '';

if (isset($_GET["print"]))
{
($smarty->is_cached('print:'.$page, '', $pageinfo->template_id)?$cached="":$cached="not ");
$html = $smarty->fetch('print:'.$page, '', $pageinfo->template_id) . "\n";
}
else
{
#If this is a case where a module doesn't want a template to be shown, just disable caching
if ((isset($_REQUEST['showtemplate']) && $_REQUEST['showtemplate'] == 'false') ||
(isset($smarty->id) && $smarty->id != '' && isset($_REQUEST[$smarty->id.'showtemplate']) && $_REQUEST[$smarty->id.'showtemplate'] == 'false'))
{
$html = $smarty->fetch('template:notemplate') . "\n";
}
else
{
$smarty->caching = false;
$smarty->compile_check = true;
($smarty->is_cached('template:'.$pageinfo->template_id)?$cached="":$cached="not ");

// we allow backward compatibility (for a while)
// for people that have hacks for setting page title
// or header variables by capturing a modules output
// to a smarty variable, and then displaying it later.

if( isset($config['process_whole_template']) && $config['process_whole_template'] === false )
{
$top = $smarty->fetch('tpl_top:'.$pageinfo->template_id);
$body = $smarty->fetch('tpl_body:'.$pageinfo->template_id);
$head = $smarty->fetch('tpl_head:'.$pageinfo->template_id);
$html = $top.$head.$body;
}
else
{
$html = $smarty->fetch('template:'.$pageinfo->template_id);
}
}
}

#if ((get_site_preference('enablecustom404') == '' || get_site_preference('enablecustom404') == "0") && (!$config['debug']))
#{
# set_error_handler($old_error_handler);
#}

if (!$cached)
{
#Perform the content postrendernoncached callback
reset($gCms->modules);
while (list($key) = each($gCms->modules))
{
$value =& $gCms->modules[$key];
if ($gCms->modules[$key]['installed'] == true &&
$gCms->modules[$key]['active'] == true)
{
$gCms->modules[$key]['object']->ContentPostRenderNonCached($html);
}
}
//Events::SendEvent('Core', 'ContentPostRenderNonCached', array(&$html));
}

#Perform the content postrender callback
reset($gCms->modules);
while (list($key) = each($gCms->modules))
{
$value =& $gCms->modules[$key];
if ( isset($gCms->modules[$key]['installed']) &&
$gCms->modules[$key]['installed'] == true &&
$gCms->modules[$key]['active'] == true)
{
$gCms->modules[$key]['object']->ContentPostRender($html);
}
}

Events::SendEvent('Core', 'ContentPostRender', array('content' => &$html));

header("Content-Type: " . $gCms->variables['content-type'] . "; charset=" . (isset($pageinfo->template_encoding) && $pageinfo->template_encoding != ''?$pageinfo->template_encoding:get_encoding()));

echo $html;

@ob_flush();

$endtime = microtime();

$db =& $gCms->GetDb();

$memory = (function_exists('memory_get_usage')?memory_get_usage():0);
$memory = $memory - $orig_memory;
$memory_peak = (function_exists('memory_get_peak_usage')?memory_get_peak_usage():0);
if ($config["debug"] == true)
{
echo "<p>Generated in ".microtime_diff($starttime,$endtime)." seconds by CMS Made Simple using ".(isset($db->query_count)?$db->query_count:'')." SQL queries and {$memory} bytes of memory (peak memory usage was {$memory_peak}</p>";
}
else if( !isset($config['hide_performance_info']) )
{
echo "<!-- ".microtime_diff($starttime,$endtime)." / ".(isset($db->query_count)?$db->query_count:'')." / {$memory} / {$memory_peak} -->\n";

}

if (get_site_preference('enablesitedownmessage') == "1" || $config['debug'] == true)
{
$smarty->clear_compiled_tpl();
#$smarty->clear_all_cache();
}

if ($config["debug"] == true)
{
#$db->LogSQL(false); // turn off logging

# output summary of SQL logging results
#$perf = NewPerfMonitor($db);
#echo $perf->SuspiciousSQL();
#echo $perf->ExpensiveSQL();

#echo $sql_queries;
foreach ($gCms->errors as $error)
{
echo $error;
}
}

# vim:ts=4 sw=4 noet
?>

Re: index.php causing Server Error

Posted: Mon Mar 07, 2011 6:30 pm
by Dr.CSS
I would check to make sure they haven't upgraded the PHP as that old of a CMSMS doesn't work with newer PHP...

Go to cPanel and look at left side near bottom, should be PHP 4.*...

Best way to insure index.php is good is to upload fresh one from same version of CMSMS...

Re: index.php causing Server Error

Posted: Mon Mar 07, 2011 9:28 pm
by mgessner
Thanks for your help - I have checked the version, it is 5.2.4.

I think I have found the problem though, as index.php
is as far as I understand, just building the (home) page, I
changed the template to something very basic and the problem
didn't appear. I gradually added more into the template and found that the problem appeared when I added in the comments module. I had a look at the comments, but didn't see any recent comments, but now that I have removed the comments module from all pages, the site seems to be stable and is not leaving index.php processes hanging around anymore.

Re: index.php causing Server Error

Posted: Tue Mar 08, 2011 11:33 am
by mgessner
I thought the problem was fixed, but after 24 hours
the processes had built up again. If I add the comments
module back, then the problem appears quickly. If it is not there,
there is doesn't come back quickly but still seems to be there.

Are there any logs I could look at to find out what might be the problem? Maybe a database query is not returning data?

Re: index.php causing Server Error

Posted: Tue Mar 08, 2011 1:56 pm
by jmcgin51
now that you have a rough idea of what is causing the problem, and the site is working at least at a basic level, please upgrade to 1.9.4. If the issue still is present after the update, let us know. Many changes to both the core and other modules since your 1.5.4 version, and a good chance that whatever you're seeing has been fixed by now, so no use trying to troubleshoot such an old version.

Re: index.php causing Server Error

Posted: Tue Mar 08, 2011 4:49 pm
by Dr.CSS
Version 1.5.4 is not made to work with PHP 5.* since it is what you have you need to upgrade...

Re: index.php causing Server Error

Posted: Tue Mar 08, 2011 8:41 pm
by mgessner
Thanks for your help.

I will look into how to upgrade and hopefully find some information on the forum here about whats involved, how difficult it is and how long it would take.

Martin

Re: index.php causing Server Error

Posted: Tue Mar 08, 2011 8:45 pm
by Dr.CSS
Not hard...

Backup folders/files and DataBase...

Unpack newest version over older one overwriting all, run mysite.com/install/upgrade.php...

After that you will most like need to go to Module manager to see what other extra modules need to be upgraded such as CGExtensions, etc...