How to Parse Methods and Functions to Log the Code - php

Is there an easy way to parse the Functions and their code from a PHP file so you can log the contents of each function separately in a database?
I would like to log each separate function and the comments for the function as an individual database entry.
I started writing my own script from scratch to parse the PHP file, but it seemed like this really ought to be something someone else has already done before. So before reinventing the wheel I thought I'd ask if any of you know of another way to do it?
I found get_defined_functions() which provided the function names currently loaded. But I'm looking for the function's arguments, contents and its comments as well.

You're looking for token_get_all(), which takes a PHP source file and parses it into its components (keywords, comments, whitespace, strings, etc.).
If what you trying to do is analyze performance, such as logging slow calls, you will need a debugger like xDebug.

Related

How to get the final php code to be executed replacing all the require and include with their code

First let me apologize if this question was answered before, I just don't seem to find the proper terms to search for it.
The context
I'm viewing some complex PHP code with a lot of require in it, it is kind of ineffective and time consuming to search for a function's or variable's definition through all the multiple level tree of files included in the current file.
The desired result :
so I'm wondering if a function exists that does the following:
Remove all those requires
Replace them with their code recursively
Output it on the browser or return it to a variable
In a nutshell, a function that prints the final PHP code to be executed.
The question :
Is there such a function? if yes, what is its name?
Thank you for the help.
You cannot get the code which is going to be executed because inclusion of code can be done at run-time, based on variables whose values you don't know before running the code.
What you can do is archive all the possible code to be executed, and pack it up as a phar archive.
Using modern tools, that could mean using composer archive.
More information available here https://getcomposer.org/doc/03-cli.md#archive
Besides, it would go against the ecosystem: in modern PHP applications, you use autoloading. Which means that even in projects with millions of lines of code and hundred of thousands of files, there is only one include/require statement.

Process content from database as php

I'm not exactly sure what I need to search for in Google and have been struggling with this for a while.
I wrote my own CMS for a project and am stuck with processing content stored in the database, for example.
This is a link to a related page.
In the above example I'm getting a page url by its ID, this way it doesn't matter if the url changes, it will always be up to date.
The issue is PHP sees it as a string and will not process it.
Im working around the issue by writing the contents to a file, using PHP include on the file, and then deleting the file. I don't see this as an efficient and would like a better solution.
PHP reads that content as a string because it is a string.
To make your string function as PHP, you'll need to use PHP's eval() function.
// The string that is loaded from the DB, or wherever
$string = 'This is a link to a related page.'
// Run the string as PHP code (notice the "echo" command)
eval("echo {$string}");
This can be very dangerous, however! If you're going to do this, be very certain you know what string is being executed! Because the eval() function will run any PHP code that is placed in it! Even site-destroying-dog-kicking PHP code!
More about the eval() function can be found in the PHP Docs for eval()
--
I don't know your exact scenario, but I would generally advise against using eval() wherever possible. There is normally a safer way to doing something than using the eval() function.

can i merged 2 (or maybe more) php functions in on php script?

im a begginer.
is it possible to put two functions in one php script?, per example:
having a php script loading a xml file (simplexml_load_file)
and then after I've got the data i want to from that xml also put the DomDocument functions(and variables and all that) to create another xml and write the data parsed(to just the data i want,or maybe convert the data to json format.
Question1:is it possible?
Question2: or do i need to run those functions in separate scripts with exec();function ?
any explicit explanation is appreciated or link to learn about it.
-Thanks.
You can call any number of functions in your one script file.
A php file holds php code. Your php code can contain classes, functions, function calls and so on. You have to focus on the following though:
Typing: have correct syntax
Correctness: avoid bugs in your code, test everything
Separation: a php file should hold closely related code. This is not mandated by the language, but it is common-sense. You will not want to have a file containing conversion functions and date functions, because it is frustrating to read it, so it is beneficial to separate conversion functions from date functions
There is no limitation for function numbers or function call numbers per file for php.
I would suggest that you should watch some nice tutorial movies about php. A good and comfortable way to learn the language. Example.

php codeigniter controllers with scrambled/uglified code

I just inherited a codeigniter application and looking trough the source code all the controllers have scrambled code. Example:
<?php $_X='-8588104972344438462';vMASFkAm('MTExMTAxMDAwMTAxMDEwMDAwMDAwEMMTA=');*
Is there any way to get back the original code?
Is this something codeigniter specific?
I don't have much experience with codeigniter. Please help.
If you have time and will, you can debug all that. See what each function outputs while making sure you replace any evals with echos, just to be on the safe side. The string parameter for that function looks like base64 encoded string, so you might want to decode that as well (or track where it's decoded in the code, and see the output). If it's not base64, it uses some more sophisticated algorithm where x and w seem like separators...
Unfortunately, unless you do it manually, variable by variable, function by function, etc... there's no way to get it back to the original state.
And, like the other answer said, this is in no way CodeIgniter-specific.
This is not CodeIgniter related. Looks like obfuscated code -- likely due to an exploit of some kind. I'd be concerned this server has been compromised.
https://www.google.com/search?q=is+my+server+compromised
To search a little deeper, I'd start by grepping through the codebase looking for
function vMASFkAm(
to see what's there.

can I log which PHP files and/or functions are accessed while browsing an application on my server?

So I have a php application with lots of different files, the code is a bit of a mess.
What I want to do is install the application on my server, use it briefly, try to cover all functionality clicking here and there, and then get a log somehow that says 'these php files were used' or 'these functions were accessed'.
I don't care about function arguments, time needed, how many times each thing was called etc etc.
I just want to see if there are functions or at least whole files that are never called - so they are dead.
I know that
1) it could be possibly done by editing each file and writing something out when it is called or included
2) there may be static tools that try to follow all code paths etc.
I'm not interested in the above - i need just something, probably a php module/debugger I'm not sure, that logs which files are requested/included/run by php.
If specific functions can be traced too, even better. But even just files would be awesome.
Sound like you are looking for debugger like xdebug, specially this feature and this function trace
This is just to get a list of files that are in use. A debugger would be required to do a profiling of your project. Also reference this answer to a similar question.
The list of included files will be saved to a text file.
In your php.ini, set up auto_prepend_file to point to a file that contains the following.
<?php
function shutdown_file_output()
{
file_put_contents( 'included_files.txt', implode( PHP_EOL, get_included_files() );
}
register_shutdown_function('shutdown_file_output');
PHP's debug_backtrace() function can be used to get a full trace of a script's execution, including called functions and arguments. If you place a call to this function at the end of your script and log the output to a file or files then that should accomplish what you're looking for.

Categories