Disable a file include if it generates error (in php) - php

I would like to implement a simple plugin system for my script. I'll be including the files of the plugins which the user selects as available.
Currently, if any of the files has some parse error, it causes the complete thing to go down, until I hack into the db to remove that plugin entry.
Is there any easy way to do this? (Somehow check the files for parse errors atleast)?

You could make a HTTP request to a PHP file that tries to include the file, and outputs "OK" at the end.
Check_include.php:
$filename = $_GET["plugin"];
$filename = sanitize_filename($filename); // Here you make sure only plugins can be loaded
include ($filename);
echo "OK";
then, in your admin panel or whatever, call
file_get_contents("http://mydomain/check_include.php?plugin=my_new_plugin");
and see whether the result contains the word "OK". If the result is okay, there was no fatal error, and you can activate the plugin; otherwise, you can even output the error message that got output when trying to include it.
WordPress uses a variation of this involving an IFRAME to check new plugins.
Note: Making a HTTP request is expensive. You should under no circumstances do this every time the plugin is included, but only once on installation. Use a flag of some sort (e.g. a underscore _ in front of the plugin name) to enable / disable plugins.

No. A parse error is a fatal error that you cannot recover from.

Related

Server-sent events in PHP (without echo or print)

We have built a prototype application in PHP and JS using Server-Sent Events (single AJAX requests, multiple streamed events sent by event handlers in PHP). Essentially the PHP at some point is using echo to send data back, much like this example: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#sending_events_from_the_server i.e.
echo "event: ping\n";
However the platform we are building for (Magento) has strict coding standards that prohibit echo and print (and print_r and var_dump). Is there any way around this aside from scrapping SSE and setting up AJAX polling?
Well, I think you have 2 ways of "echoing" something in Magento.
1. Adding your PHP file to /pub
Yes, you can run you custom PHP file to "echo" whatever you want.
But you will need to place it under <magento folder>/pub/yourfile.php.
If you're using nginx you will also need to create an exception for your file. Otherwise, Magento's routing will be used.
For doing that, find something like location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check)\.php$ { in your nginx file, and add yourfile.php there.
For example:
location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check|yourfile)\.php$ {.
Once your file is there, it will be served under yourstore.com/yourfile.php or yourstore.com/pub/yourfile.php (in case you are wrongfully exposing the root directory).
2. The magento way - create a controller
You will need to create a module and a controller.
There are plenty of tutorials out there explaining how to create them.
Here you can find how to create the basic module's structure.
And in this other article you can see how to create different controllers with different types of return.
Magento does have strict standards as part of there PHPCS configuration that need to be adhered to before they will allow you publish a module to there market, there is not Solid fix for the issue you have mentioned regarding using echo or print in a magneto file as part of your module. however we have found a work around by leveraging phps Output Buffering.
We have successfully submitted a module using the below mentioned function and example.
You can use ob_start() and ob_end_flush() it behaves similar to how a print or echo would when interacting with the event stream. See link below for example.
Why?
The ob_flush() function outputs the contents of the topmost output buffer and then clears the buffer of the contents. The output may be caught by another output buffer or, if there are no other output buffers, sent directly to the browser ( IE. your currently open stream ).
Example:
/**
* Send Data
*
* #param string $content
*/
function sseEchoAlternative(string $content)
{
ob_start(function () use ($content) { //Expect a warning here
return $content;
});
ob_end_flush();
}
Ref: ServerSentEvents.php

How to wp_dequeue_script or wp_deregister_script jquery (not effected) file from wp theme

NOTE: I already use wp_dequeue_script or wp_deregister_script but not successfull
Here is the scenario, i make a image slider plugin that use jquery-cycle2 and it work successfully.
There is a user who used a wp theme and in theme there is a jquery-cycle1, now when he install my plugin the jquery-cycle1 and jquery-cycle2 conflicts, when user delete the jquery-cycle1 file all things work fine and perfectly but i don't want to delete file by user.
Now i am trying that when user install my plugin the jquery-cycle1 in theme close or deregister or stop its effect.
I get file from theme successfully
if((file_exists($theme_file_path."/jquery.cycle.all.js"))){
echo "yes";
}
but i have no idea to close jquery-cycle1 file or stop its effect.
Last Option: I have last solution that delete the file from theme but its my last option.
Please any suggestions, help me.
You will have to place an incompatibility notice on your theme.
It is not possible to attempt to detect the existence of script from server side. You are able to detect queued scripts via the word press methods, however, this assumes that the user has not simply linked the file with a <script></script> tag. The file_exists method assume the file is stored on the server itself - it could be linked from a CDN or another server.
Also, whatever methods you use to detect and remove jQuery-Cycle; You are going to break any feature on the site that uses the existing plugin.
Thus, any solution you able to devise would either be extremely complicated, or would not be generalised enough to account for all these possibilities.
You may be able to use the following to prevent loading your script
if (jQuery().cycle) {
// Script already loaded
console.log("Error: Another version of jQuery-Cycle is already loaded!");
} else {
// Load your script
}
but this cannot unload what is already loaded.
There is a simple hack you can do on you end. In the Cycle2 source replace all $.fn.cycle with $.fn.cycle2 and ).cycle( to ).cycle2(.
I am using the source from here http://malsup.github.io/jquery.cycle2.js
After that You can access it like
$("#id").cycle2({option})
Here is a demo http://jsfiddle.net/33g6z79h/
Here i assume that you are not using cycle events http://jquery.malsup.com/cycle2/api/#events
and cycle extra transitions.
If you are using it you can make a fiddle for your cycle2 implementation and i would be glad to help :)

How do I use require_once()?

I create a PHP page with the following content:
<?php session_start(); ?>
<?php require_once(./classes/MaterialUtil.class.php);
$mUtil = new MaterialUtil();
?>
I put the MaterialUtil.class.php file in D:\xampp\htdocs\drupal\sites\all\themes\zeropoint\classes, but I get the following error message:
Parse error: syntax error, unexpected '.' in D:\xampp\htdocs\drupal\modules\php\php.module(80) : eval()'d code on line 7
Could you please tell me what I do wrong?
The error is caused from the fact you didn't use a string for the filename, and PHP understood the dot as being the concatenation operator; as such, because there wasn't any value before the operator, PHP gave you an error saying it found the concatenation operator in the wrong place.
As kalabro said, the correct code is the following one:
<?php session_start(); ?>
<?php require_once('./classes/MaterialUtil.class.php');
$mUtil = new MaterialUtil();
?>
This is the part of the answer that is not strictly related to Drupal.
What you are doing is not what I would suggest to do, for two reasons:
You are putting the "classes" directory in the wrong place. Those files are not related to the theme being enabled, but they are related to the page being viewed. Even if you have a single theme, and users are not allowed to select a theme for themselves, it still wrong to put those files in a theme directory.
Putting the files in the directory containing a theme, which will needs to be updated when a new version is available, could cause you to lose the additional files you added, if you are not careful.
Executing PHP through eval() to, e.g., get content to show in a node is not something that you should do. This is because:
As you have used the PHP filter for the node, the node becomes only editable to a restricted group of users. (I would not suggest to allow untrusted users to use PHP as input format)
When you have PHP code that you need to execute, it is always better to create a custom module that is enabled for the site.
If you were trying to include a PHP file from inside a module, then you should use module_load_include(), instead of require_once(), as already suggested by marcvangend.
XAMP server does not run on windows file system format. You must write your file location like localhost/xyz/abc ..

Vtiger Custom Module : "Sorry! Attempt to access restricted file."

I have created a test module name Mytest. While saving values from the module, I am getting a blank page and it saying "Sorry! Attempt to access restricted file. " . Do anyone know, why this happening. Any help on this is really appreciating.
The most likely cause for the vTiger error “Sorry! Attempt to access restricted file.” is the $root_directory value in the ‘config.inc.php’ is incorrect or misspelled.
In order to correct it follow the steps below:
Go to your vTigerCRM directory
Open “config.inc.php” with your favorite text editor
Go to line 86 and adjust $root_directory value to correct vTiger
directory. Note, that the directory must end with /. It should look
something like this – $root_directory = ‘/var/www/vtigercrm/’;
Also there is a problem with cache memory. So do check your cache file for template files. For that go to your vTigerCRM directory.
Then Go to Smarty->templates_c.
Here you will get list of cache files. Delete this file and check weather your problem is solved or not.
Don't worry about deletion of this file.
When trying to include files from your custom module, you will get these messages because Vtiger thinks you are including these files from a location they find rather unsafe.
To avoid this error you could use the standard way a module is used in Vtiger by navigating to it like so: ......./index.php?module=Mytest&action=index. Vtiger will include your module and now there is no need for you to include CRMEntity and other data or utils related files. It should all be available this way but make sure you are using the global statement for $current_user, $current_module etc though.
Another way is to edit the following functions located in utils/CommonUtils.php:
heckFileAccessForInclusion() and checkFileAccess()
Remove or comment out the die() in these functions to fix it.
In Save.php file, just add a line.
$focus->column_fields['assigned_user_id'] = '';
before the
if($_REQUEST['assigntype'] == 'U') {
$focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_user_id'];
} elseif($_REQUEST['assigntype'] == 'T') {
$focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_group_id'];
}
To second what caspersky said:
Go to /include/database/PearDatabase.php and add
$adb->setDebug(true); right after $adb->connect();
I just wrote a module and received this error and it was because the record could not save because I left out:
$moduleInstance->setEntityIdentifier($fieldInstance);
Check out file permissions and file path it's trying to refer.
If you want to debug more set $adb->setDebug(true) in your index file and checkout for the errors.
A couple of things spring to mind:
Have you actually created the modules/CustomeModule directory and populated
it? (Using the template in vtlib/ModuleDir/5.4.0 and then editing the
filenames and class of CustomeModule.php)
Check the case of your module class definition, e.g. class CustomeModule
vs. class Customemodule
If you are using any version control or symlinks in the development
of your modules/Mytest code then this can trigger the "Sorry! Attempt
to access restricted file." messages.
In module setup script make sure you have added this lines.
$module->initTables();
$module->initWebservice();
Check that all language files exist.
The user module allows the admin user to configure a user's language even though the language file is not present on disk.
To quickly verify this is indeed the issue :-
- Edit the include/utils/CommonUtils.php and print the $realfilepath variable ,and comment out the die();
- In the database, "select distinct language from xxx_users";
You can fix this by downloading the required files.
As a quick fix (read:hack):-
- go to the include/language directory
- copy an existing language file as the required one. (may not always work - for example en_us to en_gb is great, but en_us to sp_es is not)
It seems you did not set write permission for Smarty folder
Probably a file is missing in your vtiger install.
To find out which one is mission you would need to edit the include/utils/CommonUtils.php file. Open it with a text editor, go around line 2755 and add the following
echo “REAL: $realfilepath, ROOT: $rootdirpath”;
Before die(Sorry....)
This would print on the screen which one is the missing file.
Sometimes this error is caused by an nonexistent module, what I mean here is that vtiger thinks you have a module but the files are not in there (might be caused by a bad migration to a new server).
Disable some modules and try again until you find which module is broken.
In my case the broken module was VGS.
I solved this on vtiger 7.3.. (maybe it works for other vesion)
I went to users permission on vtiger inside configuration settings and update tham all again with the same settings .. and got them to a more default settings .. them all users appeared back and I was able to create new users ..change password again.
I suggest logging out and maybe forcing refresh and waiting a little to make it work .

How to include GET & POST data in PHP error log

Is there a way to have PHP log errors to a file or email me errors INCLUDING $_POST[] & $_GET[] and $_SERVER[] data?
Right now I get a log of PHP FATAL and WARNING errors and 404 NOT_FOUND errors but it's hard to debug some errors without knowing things like user input and the referrer.
Thanks
error_log(print_r($_POST, true));
error_log(print_r($_GET, true));
Put that into a custom error handler and it'll log both for you (the 'true' parameter makes print_r return text instead of outputting it).
You might need to boost the max line length in the error log with log_errors_max_len, as it defaults to 1024 chars and will truncate everything after that (it won't split >1024 char data across multiple lines).
If you are using PHP 5 create a custom exception class which will extend the native PHP Exception class and add whatever data collecting / logging methods you like. That way you could easily combine try {} catch() {} block with Exceptions and have your own way of controlling how you want to log all data.
I would recommend the error_log() function. I use that a lot to debug stuff in php.
http://php.net/manual/en/function.error-log.php
I worked on a telecom project that used CI to handle a Felx based frontend, we hijacked all possible error reporting and wrote a wrapper on syslog functions and used it in our application, the result was we could log whatever data we wanted and the log was visible through system log viewer :-)

Categories