I have a project in cakephp, when I call a vendor for decrypting a string using AES, I get the error:
Fatal error: Class declarations may not be nested in /var/www/html/myproject/cake/libs/log/file_log.php on line 30
This is the code in my controller:
App::import('vendor', 'aes', array('file' => 'AES/AES.php')); $aes = new AesCtr(); $decrypted = $aes->decrypt($encrypted, "mykey", 128);
And this is part of the vendor (one single file called AES.php):
class Aes { //....Methods } class AesCtr extends Aes { public static function decrypt($ciphertext, $password, $nBits) { //....Method Logic } //....Other methods }
I've read the explanation given here: PHP Nested classes work... sort of? but I have no much experience in PHP and wasn't able to solve this issue by the "hacky way" that they showed.
Any help is appreciated. If more info is needed, please tell me.
UPDATE Classes Aes and AesCtr (both are in file AES.php).
Sorry I should have mentioned this, this code works well on my computer, I get that error when I upload the project to a server, and the complete message is:
Notice (8): Hex number is too big: 0x100000000 [CORE/vendors/AES/AES.php, line 230]
Fatal error: Class declarations may not be nested in /var/www/html/myproject/cake/libs/log/file_log.php on line 30
So I don't know if the error is in the code above or not.
UPDATE 2: file_log.php, where the fatal error occurs (here is a description of the file)
<?php /** * File Storage stream for Logging * * PHP versions 4 and 5 * * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project * @package cake * @subpackage cake.cake.libs.log * @since CakePHP(tm) v 1.3 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ if (!class_exists('File')) { require LIBS . 'file.php'; } /** * File Storage stream for Logging. Writes logs to different files * based on the type of log it is. * * @package cake * @subpackage cake.cake.libs.log */ class FileLog { /** * Path to save log files on. * * @var string */ var $_path = null; /** * Constructs a new File Logger. * * Options * * - `path` the path to save logs on. * * @param array $options Options for the FileLog, see above. * @return void */ function FileLog($options = array()) { $options += array('path' => LOGS); $this->_path = $options['path']; } /** * Implements writing to log files. * * @param string $type The type of log you are making. * @param string $message The message you want to log. * @return boolean success of write. */ function write($type, $message) { $debugTypes = array('notice', 'info', 'debug'); if ($type == 'error' || $type == 'warning') { $filename = $this->_path . 'error.log'; } elseif (in_array($type, $debugTypes)) { $filename = $this->_path . 'debug.log'; } else { $filename = $this->_path . $type . '.log'; } $output = date('Y-m-d H:i:s') . ' ' . ucfirst($type) . ': ' . $message . "\n"; $log = new File($filename, true); if ($log->writable()) { return $log->append($output); } } }
Ok, this time I focused better and here's one possible problem/solution:
Can you post this file too: /var/www/html/myproject/cake/libs/log/file_log.php
as it seems that error happens here and not in AES.php:
Fatal error: Class declarations may not be nested in /var/www/html/myproject/cake/libs/log/file_log.php on line 30
I think that Notice and Fatal Error are two different things that should not be mixed together. There is one notice about too big integer value (which is converted to float) and other fatal error about nesting classes inside each other.
How Aes and AesCtr should used:
Both Aes and AesCtr are fully static classes so ther's no point to instantiate them:
Change:
$aes = new AesCtr(); $decrypted = $aes->decrypt($encrypted, "mykey", 128);
To:
$decrypted = AesCtr::decrypt($encrypted, "mykey", 128);
Yes you should drop $aes = new AesCtr();
as it is not needed when calling only static functions.
Here's about notice, AES.php line 230:
In AES.php at line 230 there is hex number 0x100000000
which is not valid signed 32-bit integer (in fact, it requires at least 33 bits even if unsigned), some PHP versions will throw an error or notice about it (older mostly). I am using PHP 5.3 and it will not notice anything even with error_reporting(E_ALL);
Signed integer maximum value is 2147483647
or in hex 0x7FFFFFFF
and AES.php tries to use value of 4294967296
which is converted to floating point.
And, as language.types.integer.php says: There is no integer division operator in PHP.
so that value should be converted to float anyway.
I think that problem is, as stated in error message Hex number is too big: 0x100000000
, that php tries to use that hex value as integer but it is too big so it is converted to float first.
Some tests:
var_dump(0x100000000); var_dump((int)0x100000000);
Output in 32bit system:
float(4294967296) int(0)
And same with 64bit system:
int(4294967296) int(4294967296)
How to fix it then?
Here's solution which may or may not work:
Simply replace 0x100000000
with 4294967296.0
so that line 230 will be like this:
for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/4294967296.0, $c*8);
Maybe you should also check what urs()
tries to do and how it try to do that. However it should already expecting floating point values as arguments, see integer division.
Update:
Checked that urs() seems to expect int as first arg, however it gets that from (x/y)
operation which in PHP returns float if there is fractions and int otherwise.
To make it int again is simple type conversion using (int)(x/y)
, there is no drawbacks (or very little) compared to old way, this is only to convert it back.
Originally, before modifications, there was same possibility and propability of conversion to float as there is now (urs()
doc says that first arg should be int).
for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs((int)($b/4294967296.0), $c*8);
Update: file_log.php
I think that there is nothing wrong with file_log.php
, it declares class FileLog
which is properly implemented and closed as far as i can see.
Other thing about file_log.php
is that it seems that it is cakephp's internal library and loaded by cakephp framework. And I don't know anything about cakephp...
However, you really should test your code without AES.php
or maybe try including AES.php
some other way like this way to test if App::import()
does not work correctly:
/* Remember to take into account that filenames are propably * case sensitive at server, but maybe case insensitive at * your workstation (if developing under windows). */ // If needed, change vendors/AES/AES.php to correct path: require_once 'vendors/AES/AES.php'; $decrypted = AesCtr::decrypt($encrypted, "mykey", 128);
or maybe (from cakephp manual):
// Vendor not vendor and AES/AES not aes/AES or aes/aes just to be sure... App::import('Vendor', 'AES/AES'); $decrypted = AesCtr::decrypt($encrypted, "mykey", 128);
or maybe drop it completely out:
$decrypted = 'dummytest';
or maybe include without using it at all:
App::import('vendor', 'aes', array('file' => 'AES/AES.php')); $decrypted = 'dummytest';
instead of:
App::import('vendor', 'aes', array('file' => 'AES/AES.php')); $decrypted = AesCtr::decrypt($encrypted, "mykey", 128);
This way problem is narrowed down, if same error still happens to pop up then problem is somewhere else but if error disappears after this test, then we know that error is either in App::import() or propably somewhere in cakephp framework.
I had once this kind of error because I added twice the name of a Model, e.g.
class MyModel extends AppModel { var $name ='MyModel'; .... var $name = 'Another Name'; }
In general, it is difficult to find the root cause of the problem: Sometimes the cause can be in places other than those you could suspect. For example, a mistake in Model can fire such kind of error.
I started having this error after a php update. Whenever i created a new php.ini
, the error started to pop out. I tried to change error_reporting
, display_errors
and other settings, and the key to the error was the setting short_open_tag.
(I still use old libraries without strict open tag, just "
So, the answer: set short_open_tag
to "On" in php.ini, restarted apache and the error went away.
FYI: it happened to me after upgrading to OSX Maverick