Custom PHP Global Functions available across a server - php

I am familiar with built in php functions such as str_rev, str_replace, etc... Is there a way to enable our own custom functions to be automatically to a php document instead of being forced to use include or require?
In other words, I would like to make my own custom built-in like functions that can be called without having to include or require a file prior to function call. Can this be done?

Typically, I don't consider global functions a good idea, however, yes, you can create a file, anywhere on your server and included in the php.ini file like so:
auto_prepend_file = "/path/to/global_functions.php"
Your php.ini will most probably already have the auto_prepend_file property so you may need to locate it and avoid having duplicate properties.

Related

PHP: Return all user-defined functions

I have PHP shell and I want user to be able to call functions directly. I got it done. But then I found out that user can call even pre-defined functions like unlink and more. I think this is security hole so I want to restrict callable functions to only those which I have defined, e.g.:calling unlink wont work.
You can get the list of define functions with
get_defined_functions();
It will return an assoc array with user defined and internal functions. You can use that information to decide whether a functions is user defined or internal. (manual)
You can disable functions using the disable_functions INI setting (in your php.ini file).
For example:
disable_functions=unlink,fopen,file_get_contents [and so on...]

Can I create a function in PHP that works as a system function (as echo,die, etc)

I'm always using a function to write to a log file, but this function is defined in a file among many other things that I don't need to include.
I was wondering, is it possible to define a function somewhere inside php to make it available without the need to include the source file? Sort of like how I can just use echo or die, or isset. Could I create my own function to use it this way?
Thank you.
No. To do that, you'll have to write a PHP extension in C. Any PHP code will always need to be included explicitly one way or another.
PHP has the option to always automatically include a file at the beginning though: http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file
Actually, you need to make a module with your function.
Other ways:
make autoload. http://www.php.net/manual/en/language.oop5.autoload.php
put only this function to other file and include it everytime you need.
you can add you log class in set_include_path path or add this function to pear library class

how to apply a config file site-wide?

I'm modifying a website and I would like to add a config file for it so that when when moving the website to another server, I only need to change environment parameters like database information only once. But I don't know how to apply the config file for the whole site. I tried to use include_once() and make it global using keyword global but it only works in the current file. If I don't use define() to make these parameters as constant, are there any other way to achieve this? Thank you.
You can create a class Config and store your config parameters as Class Constants. Then include this class everywhere you need it, or use Autoload it.
class Config {
const databaseUsername = "user123";
const databasePassword = "pass456";
}
echo Config::databaseUsername;
echo Config::databasePassword;
But as in my experience, define() works perfectly well, unless you need to store more complicated data types (arrays for example).
To avoid include the config file everywhere, you could set auto_prepend_file in your PHP.ini, but I personally find this solution obscure, since it's not obvious for another developer working on your code that a PHP file is auto-included.

Can Boost.PHP to overwrite core PHP functions, such as require_once and include?

I wonder it is possible to use Boost.PHP to replace some PHP core functions logic and/or extend them. So What I want is simple: When a php script calls for require_once(dirname(__FILE__) . "/file.php"); I want to receive dirname(__FILE__) . "/file.php" with my function, create somehow that required file (in my case, go to some distributed storage), and return that file to PHP interpreter as if it was desired file.
Is it possible with Boost.PHP? How could I do such thing?
I don't know boost, but I doubt it, as they are language constructs, not functions.
You can however write your own wrapper for a stream / protocol (either define your own, like 'mywrapper://path/to/something', or you can override the file:// wrapper, but the latter means you'll have to keep flipping between override & normal, otherwise you cannot open files on your filesystem yourself :)
To change the beavior of a core php function you can use the runkit extension.
bool runkit_function_rename ( string $funcname , string $newname )
Note: By default, only userspace functions may be removed, renamed, or modified. In order to override internal functions, you must enable the runkit.internal_override setting in php.ini.
Please not that the extension is mainly used for unittesting and not used in production that often (as far as i know at least).
So you can to
runkit_function_rename("strlen", "org_strlen");
function strlen($string) {
return org_strlen($string + 1);
}
For things like require or isset that are language constructs and not functions see #Wrikken answer (+1)

dynamically load PHP code from external file

Code is in a static class in an external file eg. /home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod
How do I load this into my script on demand, and be able to call its functions?
As long as the included code is wrapped in PHP blocks, you can use include or require for this.
Like so:
include( '/home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod' );
You can then do whatever you wish, call functions, etc.
To use the variables or classes (static or otherwise) they need to be loaded before being used. Typically you would make a call like:
<?php
require('/home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod');
?>
You can also do without the parentheses:
<?php
require '/home/test/public_html/fg2/templatecode/RecordMOD/photoslide.mod';
?>
...somewhere at the top of your code.
It would be good to review include(), require(), include_once(), and require_once()
Might be too advanced right now, but PHP supports an autoloader. You would still use the include/require code that is mentioned above. But instead that code would live inside a special function that will be called anytime you access a class/interface that hasn't already been loaded. This will allow you to see what is being requested and dynamically load files on demand.
Including/requiring a few files is fine. Once you get into a large site with tons of files it will be easier to use the autoloader then explicitly writing an include/require line for each. Plus you will save memory by not loading things you aren't using.
Autoloader Docs, thats the easy version. The better implimentation is with spl_autoload_register.

Categories