Tag Archives: date.timezone

SonataAdminBundle: Fix “The “ batch action is not defined” at app/vendor/sonata-project/admin-bundle/Controller/CRUDController.php in a shared hosting: magic_quotes_gpc

I was getting http 500 errors while deleting entities in SonataAdminBundle using the batch mode (selecting some of them with the check box and then applying the delete action):

batch-delete

[2014-03-06 13:05:37] request.CRITICAL: Uncaught PHP Exception RuntimeException:
"The `` batch action is not defined" at /xxx/vendor/sonata-project/admin-bundle/Controller/CRUDController.php line 424
{"exception":
"[object] (RuntimeException: The `` batch action is not defined at /xxx/vendor/sonata-project/admin-bundle/Controller/CRUDController.php:424)"
} []

The problem was that my shared hosting has the PHP magic_quotes_gpc option set to 1 (or true). This is deprecated for PHP 5.3 and it’s even deleted from 5.4 and up. This option is a PHP_INI_PERDIR option so it can’t be changed in runtime, thus ini_set('magic_quotes_gpc', '0') in my PHP code won’t work. Only PHP_INI_ALL settings can be changed in runtime with ini_set('...', '...').

I also tried using .htaccess to set this value to off by adding this line:

<IfModule mod_php5.c>
  php_flag magic_quotes_gpc off
</IfModule>

But this caused an http 500 error and all the application failed. No luck there either.

My next approach was to use an adapted php.ini file for my app. I added a new php.ini in my Symfony2 applications web folder with this:

magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off

But this made the app complain that it didn’t had the timezone configured as this is mandatory for Symfony2:

Warning: date_default_timezone_get() [function.date-default-timezone-get]:
It is not safe to rely on the system's timezone settings.
You are *required* to use the date.timezone setting or the date_default_timezone_set() function.
In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
We selected 'Europe/Berlin' for 'CET/1.0/no DST' instead in /xxx/app/cache/prod/classes.php on line 5148

I added it but then the app said that it couldn’t find the PDO:

Fatal error: Class 'PDO' not found in /xxx/vendor/doctrine/dbal/lib/Doctrine/DBAL/DriverManager.php on line 148

Finally this was the php.ini that worked for me:

date.timezone = "Europe/Berlin"
magic_quotes_gpc = Off
magic_quotes_runtime = Off
magic_quotes_sybase = Off
extension=pdo.so
extension=pdo_mysql.so

¡CAUTION! As this php.ini is placed inside the web folder and is accesible from the internet you have to add this to the .htaccess file in the same folder so no one can download it:

<files php.ini>
  order deny,allow
  deny from all
</files>

Adding a php.ini file to your app will override/add the options defined in it but if you want to only use your php.ini and override completely the servers one you can add the following to the .htaccess and the php.ini from your shared hosting will be ignored:

SetEnv PHPRC /path/to/your/php.ini

Remember that in this case you’ll have to configure it with all the options needed to make PHP work correctly.

Ref: http://stackoverflow.com/questions/517008/how-to-turn-off-magic-quotes-on-shared-hosting
http://jesin.tk/using-custom-php-ini-files-on-shared-hosting/
http://www.php.net/manual/en/security.magicquotes.disabling.php
http://www.php.net/manual/en/configuration.changes.php