What is getScenarioPath and getScenarionResourceFolder? - php

I am looking at someones project and keep getting this line:
$resource_folder = getScenarioResourceFolder(getScenarioPath($scenario));
I cannot find any function that he implemented under those two names - getScenarioResourceFolder and getScenarioPath.
I started wandering that maybe the name Scenario has something to do with $scenario variable being in those functions. I know it might sound dumb, but I do not know what else to think.
Does anyone know about these function?

I know these:
You can search the function:
http://id1.php.net/manual-lookup.php?pattern=getScenarioPath if no
result, That is must be user-defined function.
You can check yourself by using
if (function_exists('getScenarioPath')) {
...
} else {
...
}

These are clearly custom functions that have been written in.
The simplest solution would be to GREP your entire system for getScenarioResourceFolder - you are looking for a .php file.
If you can't grep or don't know how, then it's time to go digging. Open any PHP files that are related to that project and look for getScenarioResourceFolder().
If you really don't have it, then you'll have to get in touch with the original architect of the project.

Related

The variable seems to be independent, but the program works fine. why?

I'm using the following library to give a update feature to my WordPress and this works fine with the code of documentation.
https://github.com/YahnisElsts/plugin-update-checker/blob/master/README.md
require 'plugin-update-checker/plugin-update-checker.php';
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://github.com/user-name/repo-name/',
__FILE__,
'unique-plugin-or-theme-slug'
);
But I'm really wondering where this '$myUpdateChecker' variable came from and how this is working, because I can't find any part of the library's files using this variable.
It seems to be totally independent for me.
Thank you in advance.
You're creating the variable right there. You can even name it something else if you want (eg. $update_checker), that shouldn't cause any issues in this particular case as the variable isn't being used anywhere else (according to your own words.)
For more details: PHP variables.

PHP - how to test / check if directory / folder contains old file / file older than?

Strangely when I tried to google this question entering queries similar to this question nothing useful popped out, not even close.
Now I am not looking for a solution where you scan or open directory and read its contents file by file. For those looking for such inreliable loop, checkout these functions: scandir(...) to load all at once and then loop it, and opendir(...), readdir(...), stat(...)['mtime'] to do it file by file and possibly setting some limit on how many is done and make it at least a bit safer...
What I would like to know is whether there is something in PHP that could check/test, in some simple way, whether a folder/directory contains file older than [sometime], or even how many of such files are there without any kind of "manual" go-through?
PS: I've seen some shell_execs but that does not seem quite cross-OS...
Obviously there is no built-in function to do that, as this is something too specific. You will have to do it yourself one way or another, just like those shell functions you like so much are doing internally.
Write a function that does that so you can use it where its needed. And naturally PHP offers a bunch of functions that make the whole thing smaller and potentially faster. You can for instance do this:
function folder_has_older_file($folder, $time)
{
return count(array_filter(array_map('filemtime', glob("$folder/*")), function ($a) use ($time) { return $a < $time; })) > 0;
}
You can use filectime(path) or filemtime(path) function to check creation time from file or folder. It will return unixtime, then you can compare with sometime

Would setting a function name by a variable be valid PHP?

I know this code works but I don't know if it'd be valid to do. The reason I wanted to support this is to allow people to overwrite this function with another file. Sounds slightly strange, but for my purpose it makes sense.
I only ask because I seriously can't find an example of this being done and have been searching for an hour or so.
$jerry = function(){
return 'hello world';
};
echo $jerry();
Yes, it is valid to use anonymous functions assigned to variables.
You should make use of the PHP documentation (under "Language Reference" > "Functions" > "Anonymous functions") when you are not sure about something. The website is actually quite easy to use.
Another way of finding stuff on php.net is to use the URL path. For example, if you are interested in functions then you can just go to php.net/functions, and it will attempt to direct you to the right place. This will work with quite a lot of stuff; php.net/juggling, for instance, will direct you to the "type juggling" page. If it is unable to direct you to something relevant then it will show you search results instead, using the URL path as the search query.
I believe you can do something else... say you have
function1() {
//code for function1 here
}
function2() {
//code for function2 here
}
you can actually do this
$var = 'function1';
$var(); //runs function1()
$var = 'function2';
$var(); //runs function2()
Hope it clears out things
Otherwise all the information you seek is in www.php.net/functions as the fellow user pointed out earlier. Learn to search php.net for any issues, at the bottom of every documentation page there are EXAMPLES that 95% help you for what you're looking to achieve

Any way to subvert class redeclaration issue? No namespaces

Heads up: I dont have the possibility to rename the classes or use name spaces for this.
Im looking for any crazy way to subvert class redeclaration issues in php. I actually only need 3 static variables from a web application, but the only way to get them requires including a file that declares a user class. However I already have a user class, so I get an error.
I tried to no avail to include the file in a class hoping it would isolate the included file - But no.
I tried reading an interface file I created that just echos the 3 values, but that actually just reads the php code and not the rendered values.
Is there anything like an opto-isolation system for code?
The only think I can think of is using ajax to do it, but it seems super sketchy. Is there a plain php version of this?
(Was a comment, but got too long.) Doesn't sound doable with your constraints. (You might need to show some code.) -- But if you are asking for a crazy way, and the option to rename the classes just applies to not editing the php script, then:
Load the include file into a variable, then transform it, and finally eval:
$source = file_get_contents("user.php");
$source = str_replace("class user", "class workaround_123", $source);
eval($source); // will give you a workaround_user instead of class conflict
Someone will probably comment on the advisability of eval... But it foremost depends on your code/situation if that's an applicable wacky workaround.
Alternatively you could invoke the user fetching code with a separate PHP process :
exec("QUERY_STRING=user=123 php-cgi user.php");
You could tokenize the whole file and go through it "by hand" to find the values you need.

PHP: Where should I store the text for info & warning messages

I have a question concerning the design of my project's Code.
In most cases, it is important to separate content from code (HTML mixed with PHP in bigger apps=No good idea, etc.) but how should I handle things like the text of error messages?
Assuming this one message will be only used in one case/PHP file:
MessageBox( 'Something gone wrong, probably your fault' );
or
MessageBox( Lang::error_usersfault );
(Where Lang is a class located in some config file, full of consts)
If you have any experiences (I think every PHP programmer will come across something like this) with things like in the example above - What are they? How do you solve it?
What is the better solution?
May be you'll find php extension gettext useful?
MessageBox(_("Have a nice day"));
PHP Manual of Gettext::gettext

Categories