PHP eval() function - php

The PHP function eval() appears to be a rather interesting function. Can someone explain why it works in this given situaton, on line: 14
function Parse($inFrontEnd)
{
// Now create an array holding translation tokens with some from above
// Load translation table into buffer
$tableLines = file(Utilities::GetRelativePath(TTABLE_DIR).TTABLE); // Array of lines from TTable.cfg
// Explode by whitespace
foreach($tableLines as $aLine)
{
$lineParts = EXPLODE(' ', $aLine);
$word = "/".$lineParts[0]."/";
$definition = $lineParts[1];
// Add key (word) => value (definition) to array
// Eval() to return value of the const
Main::$translateChars[$word] = eval("return $definition;");
}
// Read data from template file
$parseArray = file($inFrontEnd); // Load FrontEnd source code into array ready for parse
/* Perform the translation of template by the translation table defined data */
$parseArray = preg_replace(array_keys(Main::$translateChars), array_values(Main::$translateChars), $parseArray);
return $parseArray;
}
So what I'm doing here is reading in a template from a template directory. The templatename.php file comprises of text tokens written constant-like, which are then translated by regular expressions replacing the tokens with the data the constants with their names hold, thus returning a fully validated page of a webpage, which is printed for the user to view. This allows pages to be very dynamic by allowing the reuse of these tokens over many webpages (templates).
My question is: I had trouble for a while with the line that uses eval(). What I'm trying to do there is fill an array with each key being the name of the constant read in from, what I've named, the translation table (TTable.cfg), which holds the name of each token and the constant associated with it:
TITLE TITLE
CSS_INCLUDE CSS_INCLUDE
SHOW_ALL_POSTS SHOW_ALL_POSTS
...
So with the protocol [TOKEN] [CONSTANT][CR][LF]
The keys within the array would be created fine, but the values would return null or break my code when I had the key be associated with: constant($definition);
It complained it couldn't find the constants being declared. However, when I use eval as is on this line, each key associated with: eval("return $definition;");
it works as I want it - the values as their corresponding constant's data.
I do apologise for the length of this post. I couldn't find any other example for my question other than the case I found it in.

The value of the variable $definition is replaced into the string definition "return $definition;", as you're using double quotes. Hence, what is passed to eval is essential something like this: "return FOO;", where FOO is the value of the variable $definition.
Now, that code is evaluated using eval(), and the result is returned as the result of the evaluation, which is the value of the constant FOO (different in each iteration).
Using the constant in this case makes more sense: It is faster, potentially securer, and more readable.
if ( defined( $definition ) ) {
$constval = constant( $definition );
}
else {
$constval = $definition;
}
This will also give you some insight of why it works when using eval() and not just constant(); PHP replaces unknown constants with their respective names, thus eval works in your case. However, any other way would raise warnings and be a bad practice, as it doesn't make clear what's going on to the reader.

Remember, eval is evil, so don't use it, when you can avoid it. Here you could just use constant instead.

The tiniest of oversights. I forgot to clean $definition after I exploded it from each line of the translation table. So a simple trim() has solved the problem.
constant($definition);
Now works.
Crazy :D

Related

Checking for empty attributes while parsing an XML file

A critical function in a PHP script I am debugging get's two attributes from an XML file on an external site. The attributes are labeled 'code' and 'locationCode' within a tag called Channel. The issue is that sometimes the locationCode is posted as an empty string ('') or not defined at all by the site for channels I cannot use, so I need to loop through the channels until I find a non-empty locationCode string. To do this, I created a while loop, but my current implementation does not successfully loop through the location codes. Is there a better way to implement this?
Current code:
public function setChannelAndLocation(){
$channelUrl="http://service.iris.edu/fdsnws/station/1/query?net=".$this->nearestNetworkCode.
"&sta=".$this->nearestStationCode."&starttime=2013-06-07T01:00:00&endtime=".$this->impulseDate.
"&level=channel&format=xml&nodata=404";
$channelXml= file_get_contents($channelUrl);
$channel_table = new SimpleXMLElement($channelXml);
$this->channelUrlTest=$channelUrl;
//FIXME: Check for empty locationCode string
$this->channelCode = $channel_table->Network->Station->Channel[0]['code'];
$this->locationCode = $channel_table->Network->Station->Channel[0]['locationCode'];
$i = 1;
while($this->locationCode=''){
$this->channelCode = $channel_table->Network->Station->Channel[$i]['code'];
$this->locationCode = $channel_table->Network->Station->Channel[$i]['locationCode'];
$i++;
}
}
sample XML file for code: http://service.iris.edu/fdsnws/station/1/query?net=PS&sta=BAG&starttime=2013-06-07T01:00:00&endtime=2013-10-12T18:47:09.5000&level=channel&format=xml&nodata=404
There are two problems I can see with this line:
while($this->locationCode=''){
Firstly, you have typed an assignment (=) when what you wanted was a comparison (==). So instead of testing the condition, this line is over-writing the current value of $this->locationCode and then testing the "truthiness" of '', which evaluates to false, so the while loop never runs.
Secondly, the sample XML file shows that the attribute is not in fact empty, but contains some whitespace. Assuming these are the values you want to ignore (there are none in the sample right now which have any other value), you can use trim() to eliminate the whitespace from the comparison, giving you this:
while( trim($this->locationCode) == '' ) {

drupal---theme code

$i = 0;
$suggestion = 'page';
$suggestions = array();
while ($arg = arg($i++)) {
$arg = str_replace(array("/", "\\", "\0"), '', $arg);
$suggestions[] = $suggestion . '-' . $arg;
if (!is_numeric($arg)) {
$suggestion .= '-' . $arg;
}
}
i am a newbie of drupal,i can't follow the above code well, hope someone can explain it to me.i know the first line is assign the 0 to $i,then assign 'page' to an array. and i know arg is an array in drupal.
for example, now the url is example.com/node/1. how to use this url to use the above code.
It looks like its purpose is to generate ID strings (probably for a CSS class) depending on paths and excludes numeric components of the path out of the generated ID.
For example, 'my/123/article' produces the ID "page-my-article".
It seems this comes from a function (because the loop reads parameters using arg()) and that it expects Drupal paths such as "node/123/edit".
So the function would be called something like that:
mystery_function("my/123/article", "even/better/article");
Variables:
$i is the variable that stores the loop index
$suggestion is a String that store the generated ID. It is initialized to "page" because the ID is meant to have the syntax "page-SOMETHING".
$arg comes from the while loop: it reads the parameters passed to the mystery function one by one
$suggestions is an array that contains the generated IDs, one per argument passed to the mystery function.
In the loop:
The "$arg = str_replace..." line removes unwanted characters like "\" (however that line could definitely be improved).
The "$suggestions[] = ..." line adds the ID to the array of results.
The "if (!is_numeric($arg)..." line excludes numbers from the generated ID (e.g. "my/123/article" is probably supposed to produce "my-article")
The "$suggestion .= ..." line appends the value of "$arg" to the value of "$suggestion" and stores it in "$suggestion"
But honestly, I wouldn't advise to use that code: I doubt it works as intended given $suggestion isn't reinitialized at each loop so the value of the first path will get attached to the second, and to the third, and so on, and I doubt that is intentional.
Most likely this code is located in a theme's template.php in the preprocess_page hook. If that is the case, it's used to create template suggestions based on an argument supplied like the node id, to make it possible to create a page template per node.
What this code does, is that it loops though all the arguments in the drupal url, This could be user/3, node/3, taxonomy/term/3 or any custom url.
It first does some cleanup in the argument, to make sure that no weird symbols is added. This is not needed for most urls, but is probably there as a safety to avoid needing to have to create weird template names in some cases. This is done with str_replace
Next it adds the a suggestion to the list, based on the arg.
If the arg isn't numeric it adds that to the suggestion so it will be used in the next loop.
The idea is that you with the above urls will get added some template suggestions that look like this:
page
page-user
page-user-3
And
page
page-taxonomy
page-taxonomy-term
page-taxonomy-term-3
In this list drupal will use the last one possible, so if page-user-3.tpl.php exists, that will be used as page template for user/3, if not page-user.tpl.php will be used and so on.
This can be desired if you want to create customize page templates for the users page, or the nodes page while being able to create customized page templates for specific users.
This is not, however, a strategy I would want to employ. If you do this, you will end of with lots of different versions of a page template, and it will end up creating the maintenance nightmare that CMS systems was supposed to eliminate. If you truly need this many different page templates, you should instead look at context or panels, and put some logic into this instead.

Unify variable types of array elements

After hours of debugging, I found an error in one of my scripts. For saving different event types in a database, I have an array of unique data for each event that can be used to identify the event.
So I basically have some code like
$key = md5(json_encode($data));
to generate a unique key for each event.
Now, in some cases, a value in the $data array is an integer, sometimes a string (depending on where it comes from - database or URL). That causes the outputs of json_encode() to be different from each other, though - once including quotes, once not.
Does anybody know a way to "unify" the variable types in the $data array? That would probably mean converting all strings that only contain an integer value to integer. Anything else I have to take care of when using json_encode()?
array_walk_recursive combined with a function you have written to the effect of maybe_intval which performs the conversion you talk about on a single element.
EDIT: having read the documentation for array_walk_recursive more closely you'll actually want to write your own recursive function
function to_json($obj){
if(is_object($obj))
$obj=(array)$obj;
if(is_array($obj))
return array_map('to_json',$obj);
return "$obj"; // or return is_int($obj)?intval($obj):$obj;
}

how do i use php to read an external file and put insides into variables?

I had no idea to correctly form the title of this question, because I don't even know if what I'm trying to do has a name.
Let's say I've got an external file (called, for instance, settings.txt) with the following in it:
template:'xaddict';
editor:'true';
wysiwyg:'false';
These are simple name:value pairs.
I would like to have php take care of this file in such a way that I end up with php variables with the following values:
$template = 'xaddict';
$editor = 'true';
$wysiwyg = 'false';
I don't know how many of these variables I'll have.
How should i go and do this?
The data inside the file is in simple name:value pairs. No nesting, no complex data. All the names need to be converted to $name and all the values need to be converted to 'value', disregarding whether it is truly a string or not.
$settings = json_decode(file_get_contents($filename));
assuming your file is in valid JSON format. If not, you can either massage it so it is or you'll have to use a different approach.
Do you want 'true' in "editor:'true'" to be interpreted as a string or as a boolean? If sometimes string, sometimes boolean, how do you know which?
If you have "number='9'" do you want '9' interpreted as a string or an as an integer? Would '09' be a decimal number or octal? How about "number='3.14'": string or float? If sometimes one, sometimes the other, how do you know which?
If you have "'" (single quote) inside a value is it escaped? If so, how?
Is there any expectation of array data?
Etc.
Edit: This would be the simplest way, imo, to use your example input to retrieve your example data:
$file = 'test.csv';
$fp = fopen($file, 'r');
while ($line = fgetcsv($fp, 1024, ':')) {
$$line[0] = $line[1];
}
If you use JSON, you can use something like:
extract(json_decode(file_get_contents('settings.json')));
Using extract may be dangerous, so I suggest to store these settings in an array:
$settings = json_decode(file_get_contents('settings.json'));
You should read your file to an array, with the file() function, then you should cycle on it: for each line (the file() function will return an array, one line per item), check if the line is not blank, then explode() on the ":" character, trim the pieces, and put them into an array.
You will end up win an array like this:
[template] = xaddict
[editor] = true
then you can use this information.
Do not automatically convert this into local variables: it's a great way to introduce security risks, and potentially very obscure bugs (local variables obscured by those introduced by this parsing).

Help loading contstants stored in serialized array using eval() and constant()

DISCLAIMER:
Please read carefully as this is NOT a question about storing arrays in constants or simple eval() or serialize() techniques. This IS a question primarily about how constants work in PHP and why the constant() function is not working to convert a constant name into a constant value. Thanks.
BACKGROUND:
For various reasons, I started out with a flat config file for a homebrewed LAMP(PHP) CMS (in private development). Looking back this may have been misguided, and I have transitioned my variable storage into a DB table. However, the bulk of the code still depends on the CONSTs, so I use eval("define(A, B...);") to load the DB values A and B into constants. This part works fine. But it gets a bit more complicated.
PROBLEM:
The problem I'm having now is with constants in arrays (NB. NOT arrays in constants). I have a big, GLOBAL array called defaults that contains config settings in the format shown below.
Initially, I declare: <?php define('THIS_IS_A_CONSTANT', 'abcdefg'); ?> (And THIS WORKS...)
Next, I define $GLOBALS['defaults'] as the following nested array:
Array
(
'var_name' => Array
(
'display' => THIS_IS_A_CONSTANT,
'value' => 12,
'type' => 'int',
'params' => Array ( ... )
),
...
Lots more variables...
...
)
To prevent the client (who has file system access but no direct DB access but can change certain values, including most constants, via the CMS's administrative backend) from mucking up this array structure, I serialize the array structure and store that string in the DB. On each page request, I first define all the constants (stored in the DB) using the eval(define(A,B...)), then I unserialize the array above (which was serialized and stored in the DB). However, no matter what I try I cannot get the values at $GLOBALS['defaults']['var_name']['display'] to be recognized as the values that the constants contain. Instead, the constant name shows up and NOT the constant value (in other words, my output contains THIS_IS_A_CONSTANT instead of 'abcdefg'). Very frustrating, right?
I've tried something like the following (where $arr contains the unserialized array that I fetch from the DB):
foreach ($arr as $item => $contents) {
$display = isset($contents['display']) ? $contents['display'] : 1;
$value = constant("$display");
// This doesn't work, though it seems like it should
$contents['display'] = $value;
// Neither does this, no matter how much I juggle the quotation marks and backslashes
eval("\$contents['display'] = constant(\"$value\");");
// or this ...
eval("\$contents['display'] = $value;");
// or this ...
eval("\$contents['display'] = \$value;");
// or a number of other things...
}
$GLOBALS['defaults'] = $arr;
QUESTIONS:
Has anyone dealt with this kind of situation before? Can anyone advise me how to force my constants to be recognized as CONSTANTS and not strings. Do I need to serialize my array differently? Or maybe process the unserialized array differently (after retrieving it from the DB)? Is these some combination of eval() and constant() that will allow me to do this? Why are the constants within my array behaving badly while the constants I define normally are working without problem? Any help at all would be greatly appreciated, as I've been puzzling over this for a few days now and haven't come to any solutions.
All the best, Dakota.
Although eval does have its uses, this is not one of those cases. You have no idea what is being submitted to the eval at run time - and by the sounds of things you are storing something which you are then treating as code in a user-data storage location.
If the constant was defined before the array was declared then the serialized version would contain the value instead of the label. I can only assume that the value may differ depending on the context at run-time.
I would suggest that a better solution would be to roll your own macro language in PHP, e.g. something like:
<?php
global $defs;
$defs=array(
'THIS_IS_A_CONSTANT' => 'abcdefg',
'SO_IS_THIS' => 23
);
// for inline constants
foreach ($defs as $label =>$val) {
define($label, $key);
}
replacer($defs,true);
function replacer($in, $init=false;)
{
static $defs;
static $vals;
if ($init) {
$defs=array_keys($in);
$vals=array_values($in);
return count($in);
}
return str_replace($defs, $vals, $in);
// you might want to use preg_replace() with a pattern based on $defs for a neater solution
}
function fix_var(&$in)
{
if (is_array($in)) {
foreach ($in as $key=>$dummy) {
fix_var($in[$key]);
}
} else {
$in=replacer($in);
}
}
?>
C.
First, why are you evaling? From what you are saying you want the value of $GLOBALS['defaults']['var_name']['display'] to be the value of the constant THIS_IS_A_CONSTANT. You have de-serialized the string from your database and shoved in in $GLOBALS['defaults'] and the string stored the value as the constant name, not the value of the constant.
Have you tried:
<?php
define('THIS_IS_A_CONSTANT', 'abcdefg');
$value = constant($GLOBALS['defaults']['var_name']['display']);
//$value should now be abcdefg not THIS_IS_A_CONSTANT
$GLOBALS['defaults']['var_name']['display'] = $value;
?>
If "$GLOBALS['defaults']['var_name']['display']" does contain the string THIS_IS_A_CONSTANT then all you should need to do is pass that string to the constant funct.
Am I missing something?

Categories