Forum Moderators: coopster
<?php
if(@include("config.php"))
{
$sqlconnect = mysql_connect($mysqlhost, $mysqluser, $mysqlpass);
mysql_select_db($mysqldb, $sqlconnect);
$grabinfo = mysql_query("SELECT * FROM xaler_info");
$info = mysql_fetch_array($grabinfo);
extract($info);
if($_COOKIE[layout] == "")
{
$code = mysql_query("SELECT * FROM xaler_layouts WHERE name='$defaultloc'");
while($source = mysql_fetch_array($code))
{
$final_layout = preg_replace("/#layout/", "includes", $source[code]);
echo $final_layout;
}
}
else
{
if(!eregi('^[a-z]', $_COOKIE[layout]))
{
echo "We could not locate the layout";
}
else
{
$getlayouts = mysql_query("SELECT * FROM xaler_layouts");
while($all_lays = mysql_fetch_array($getlayouts))
{
if($_COOKIE[layout] == $all_lays[name])
{
$foundlayout = "yes";
$laycode = $all_lays[code];
}
}
if($foundlayout!= "yes")
{
"This layout no longer exists";
}
else
{
$final_layout = preg_replace("/#layout/", "includes", $laycode);
echo $final_layout;
}
}
}
}
else
{
echo "The Configuration File Is Missing";
}
?>
However, be forewarned that many a seasoned php coder will advise against the use of eval(). A lot of really freaky things can happen, some of them really vile. You'd want to be sure, sure, and utterly sure of the code that goes into eval() before it gets executed, to make sure these freaky things don't happen. Also, in most cases, instead of using eval(), you can use a library of functions which are called up using variables. E.g., populate an array $dostuff = array('function_1' => 'content_', 'function_2' => 'content_2'); etc - name your functions: myscript_function(), and loop through your array:
foreach($dostuff as $v){
if(function_exists('myscript_'.$v[0])) call_user_function('myscript_'.$v, $v[1]);
}
This way, the only functions which are permitted are those which begin with the myscript_ preface, so, for example, someone who got access to this part of your script could do 'print_r(file(passwords.php))';
In the days of php 4.0x, it wasn't uncommon for content management scripts to have 'php blocks', sidebars which were produced by a php snippet which the webmaster added to the database, which was then 'eval()'d. With the greater security-consciousness on the net these days, I don't think you'll find this functionality any more, most will have removed it.
<?php
if(@include("config.php"))
{
$sqlconnect = mysql_connect($mysqlhost, $mysqluser, $mysqlpass);
mysql_select_db($mysqldb, $sqlconnect);
$grabinfo = mysql_query("SELECT * FROM xaler_info");
$info = mysql_fetch_array($grabinfo);
extract($info);
if($_COOKIE[layout] == "")
{
$code = mysql_query("SELECT * FROM xaler_layouts WHERE name='$defaultloc'");
while($source = mysql_fetch_array($code))
{
function includethis()
{
$error_file = "error.htm";
$default = "news/news.php";
if($id == "")
{
echo "includes";
}
if(isset($id))
{
if(file_exists("$id.php"))
{
include("$id.php");
}
elseif(file_exists("$id.htm"))
{
include("$id.htm");
}
else
{
include($error_file);
}
}
}
$final_layout = preg_replace("/#layout/", call_user_func('includethis'), $laycode);
echo $final_layout;
}
}
else
{
if(!eregi('^[a-z]', $_COOKIE[layout]))
{
echo "We could not locate the layout";
}
else
{
$getlayouts = mysql_query("SELECT * FROM xaler_layouts");
while($all_lays = mysql_fetch_array($getlayouts))
{
if($_COOKIE[layout] == $all_lays[name])
{
$foundlayout = "yes";
$laycode = $all_lays[code];
}
}
if($foundlayout!= "yes")
{
"This layout no longer exists";
}
else
{
function includethis()
{
$error_file = "error.htm";
$default = "news/news.php";
if($id == "")
{
include($default);
}
if(isset($id))
{
if(file_exists("$id.php"))
{
include("$id.php");
}
elseif(file_exists("$id.htm"))
{
include("$id.htm");
}
else
{
include($error_file);
}
}
}
$final_layout = preg_replace("/#layout/", includethis(), $laycode);
}
}
}
}
else
{
echo "The Configuration File Is Missing";
}
?>
but that just outputs "includes" and doesnt show the layout code from mysql. I also messed around with eval, but take for instance this code:
<?php
$this = "I am going to replace this #word";
$this = preg_replace("/#word/", eval("echo \"food\";"), $this);
echo $this;
?>
it outputs "foodI am going to replace this".
Other than this, thanks for the help, I've only been messing around with functions for an hour now, if no one makes a post I'll hopefully come up with a solution by tommorow :)
Anyways, with an array like I posted, the keys are the functions, and the values are the content (what gets passed as an arg)
$array = array(
'make_header' => '',
'output_stuff' => 'Welcome to my groovy site',
'show_rss_feed' => 'http://example.com',
'make_menu_from_directory' => '/articlecontent/',
'grab_and_display_article' => 48);
You use this array because you have your little function library with functions, each beginning with a unique preface, that take the 'content' part (the value) of the array as an argument - these functions are the key names - so you have functions like
function mysite_make_header($arg){
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>my groovy site</title>
</head>
<body>';
} // notice - just ignores $arg
function mysite_output_stuff($arg){
echo htmlspecialchars($arg);
}
function mysite_grab_and_display_article($arg){
$content = file_get_contents('articles/'.$arg.'.php');
echo htmlspecialchars($content);
}
functions like that, with 'mysite_' or your unique preface to keep this from accessing normal php functions. Then the loop that actually executes this all looks like this:
foreach($array as $k => $v){
if(function_exists('mysite_'.$k)) call_user_func('mysite_'.$k, $v);
else die('function mysite_'.htmlspecialchars($k).' does not exist');
}
In general, if you can just avoid eval(), and avoid systems like this for giving you extra flexible code (which usually also brings its extra dose of bugs to deal with), and just use 'normal' control structures like dynamic includes (include 'dir/'.$var.'.php'; - after you've checked $var to make sure it's 'ok' to include), switch structures, or the ole classic if / then, you save yourself a whole lot of grief. However, if you really feel like you need eval(), chances are you're working with something that doesn't fit nicely into the classical control structures.
Good luck however you manage it.