As adviced in Smarty's instruction, in order to use deprecated functions like include_php in newest PrestaShop, I had to switch from Smarty.class.php to SmartyBC.class.php. I did it by modyfing in smarty.config.inc.php following lines:
require_once(_PS_SMARTY_DIR_.'SmartyBC.class.php');
// require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
global $smarty;
// $smarty = new Smarty();
$smarty = new SmartyBC();
However, using {include_php file='./custom_php/manufacturers.php'} in theme's header.tpl still results in blank screen. Errors from php_error_log :
[22-Jul-2014 15:05:55 Europe/Warsaw] PHP Notice: Undefined property: SmartyBC::$trusted_dir in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\Smarty.class.php on line 676
[22-Jul-2014 15:05:55 Europe/Warsaw] PHP Fatal error: Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template "C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\themes\trzmiel\header.tpl" on line 101 "{include_php file='./custom_php/manufacturers.php'}" {include_php} file './custom_php/manufacturers.php' is not readable' in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php:667
Stack trace:
#0 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_compile_include_php.php(81): Smarty_Internal_TemplateCompilerBase->trigger_template_error('{include_php} f...', 101)
#1 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php(485): Smarty_Internal_Compile_Include_Php->compile(Array, Object(Smarty_Internal_SmartyTemplateCompiler), Array, NULL, NULL)
#2 C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templ in C:\BACKUP\Dropbox\!_PC\LOCALHOST\_INTERCLICK\trzmiel5\tools\smarty\sysplugins\smarty_internal_templatecompilerbase.php on line 667
It seems that trusted_dir property is available only when using Security Policy:
class My_Security_Policy extends Smarty_Security {
public $trusted_dir = './custom_php/';
}
$smarty = new SmartyBC();
// enable security
$smartyBC->enableSecurity('My_Security_Policy');
so you should provide here correct path (I don't know is it relative or not - you should give a try) and then you should be able to use manufacturers.php file in your template file but you should also here look at correct file path.
Related
I'm trying to use MONGODB in my project and I just started using it when it has this problem. I created another directory and ran the files with no problems whatsoever. What is wrong with this? I keep getting Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() error message.
This is my code to the class called Database which is producing the error.
<?php
namespace auth;
include_once dirname(__DIR__) . "/config.php";
class Database
{
public function __construct(
private readonly string $dbname,
){
$this->run();
}
private function run(): void
{
$collection = (new \MongoDB\Client)->{$this->dbname};
$insertOneResult = $collection->insertOne([
'username' => 'admin',
'email' => 'admin#example.com',
'name' => 'Admin User',
]);
printf("Inserted %d document(s)\n", $insertOneResult->getInsertedCount());
var_dump($insertOneResult->getInsertedId());
}
}
and my test.php file just contained some lines of code
include_once "./assets/php/config.php";
$database = new \auth\Database("test->users");
I definitely loaded the class MongoDB and my config.php includes the vendor file of composer's.
This is the error in full.
Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PP\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
PHP Fatal error: Uncaught Error: Call to undefined method MongoDB\Database::insertOne() in C:\xampp\htdocs\PPa\assets\php\Classes\Database.php:18
Stack trace:
#0 C:\xampp\htdocs\PP\assets\php\Classes\Database.php(11): auth\Database->run()
#1 C:\xampp\htdocs\PP\test.php(15): auth\Database->__construct('test->users')
#2 {main}
thrown in C:\xampp\htdocs\PP\assets\php\Classes\Database.php on line 18
Process finished with exit code 255
The config.phpfile that I have included
include_once dirname(__DIR__, 2) . "/vendor/autoload.php";
include_once "autoload.php";
Thanks for any help.
Ah yes answer to my question, I'm just well not thinking properly at the time of writing the code. The `MongoDB\Collection" class accepts two arguments from the user and one internally set. The user provided argument is the database name and collection name. Doing it with (new \MongoDB\Client)->{$this->dbname} is wrong because it provides just the database string and not the collection name.
I resolved this issue by changing how the structure looks of the collection. md is just the alias for MongoDB class as I do not want to type such a long string everytime.
$collection = (new md\Client)->$db->$collection_name;
However, I have no idea how mongoDB uses the db and collection_name as magic constants as I have looked through the entire MongoDB\Client file and found there is no code stating those "magic constants"? I would appreciate if someone has an answer to this.
So i am working on some code and have come across this error and its starting to bug me a little as i cant find out what is wrong
i have 2 files main.php and that includes functions.php now in the main file i have the following code
<?php
include "functions.php";
$NAME = GET_USER_NAME();
?>
That is all thats in the main file now in the functions file i have the following code
<?php
function GET_USER_NAME() {
return 'bob';
}
function GET_USER_AGE() {
return '5';
}
?>
now when i try and open main in my browser i get the error
Fatal error: Uncaught Error: Call to undefined function GET_USER_NAME() in /var/www/html/main.php:3 Stack trace: #0 {main} thrown in /var/www/html/main.php on line 3
i know the file is being included properly as i can run the function
GET_USER_AGE();
from the main file and it returns 5
The only possible solutions to your problem are :
A typo in the function called in the main, which is not the exact same as in your included file (case sensitivity ?)
The include references a file which is not the one you want (but has one of the 2 desired functions)
And also yes, as #Markus-Zeller says, don't use method names like that, as stated here : https://www.php-fig.org/psr/psr-1/#1-overview
I have a smarty project, in a php file, I set a abc variable:
$smarty->assign('abc', '123');
now I can use the {$abc} in its corresponding .tpl file.
but now can I use the abc in php file?
I tried $smarty->abc and $smarty->$abc all can not access.
EDIT-01
If I use
$smarty->assign('abc', '1234');
$abc2 = $smarty->get_template_vars('abc');
I will get bellow error:
Fatal error: Uncaught Error: Using $this when not in object context in /Users/sof3/Desktop/smarty-test02/php/test.php:36 Stack trace: #0 {main} thrown in /Users/sof3/Desktop/smarty-test02/php/test.php on line 36
If your Smarty version is version 2. you can use:
$smarty->get_template_vars('abc')
but if your Smarty version is 3, you should use:
$smarty->getTemplateVars('abc')
more detail refer to: https://www.smarty.net/docs/zh_CN/language.syntax.functions.tpl
in api.php i use severall classes, with autoloaders. in include them like this
use \protoware\cms\Account as Account;
use \protoware\cms\Content as Content;
use protoware\cms\Count as Count;
include __DIR__ . '/vendor/autoload.php';
Local this works fine, when i upload it on a server i get this
Europe/Brussels] PHP Fatal error: Uncaught Error: Class 'protoware\cms\Content' not found in /data/vhosts/achielvolckaert.be/wwwroot/api.php:97
Stack trace:
#0 {main}
thrown in /data/vhosts/achielvolckaert.be/wwwroot/api.php on line 97
line 97 is the first line where i open a connection:
$content = new protoware\cms\Content();
$data = $content->get_content('medisch');
I have 0 clue how to fix this, i updated autloader, checked if all files were uploaded...
Try add a \ before class name, like $content = new \protoware\cms\Content(); or just $content = new Content() since you have a use statement.
If this class is a dependency, check if its correctly installed (inside your vendor folder, i.e.). Maybe run composer install?
I was reading Beginning PHP and MySQL ecommerce. The author asks to download latest smarty version.When book was published it was smarty 2.x now its 3.x.
I copied all the files from code download to my folder. The error i got was
Fatal error: Uncaught exception 'SmartyException' with message 'PHP5 requires you to call __construct() instead of Smarty()'
To correct it i changed my code to
<?php
// Reference Smarty library
require_once SMARTY_DIR . 'Smarty.class.php';
/* Class that extends Smarty, used to process and display Smarty files */
class Application extends Smarty
{
// Class constructor
public function __construct()
{
// Call Smarty's constructor
parent::__construct();
// Change the default template directories
$this->template_dir = TEMPLATE_DIR;
$this->compile_dir = COMPILE_DIR;
$this->config_dir = CONFIG_DIR;
}
}
?>
I've follow the following links
Fatal error: Uncaught exception 'SmartyException'
Then I got this error:
ERRNO: 2 TEXT: filemtime(): stat failed for
C:\xampp\htdocs\tshirtshop/presentation/templates_c\b34e4db7de306d57170626b504196f0c5fe34fa4.file.store_front.tpl.php
LOCATION:
C:\xampp\htdocs\tshirtshop\libs\smarty\sysplugins\smarty_resource.php,
line 772, at August 22, 2014, 9:59 am Showing backtrace:
filemtime("C:\xampp\htdocs\tshirtshop/presentation/templates_c\b34e4db7de30...")
line 772, file: C:\xampp\htdocs\tshirtshop\libs\smarty\sysplugins\smarty_resource.php
Smarty_Template_Source.getCompiled(Object: Smarty_Internal_Template) #
line 699, file:
C:\xampp\htdocs\tshirtshop\libs\smarty\sysplugins\smarty_internal_template.php
Smarty_Internal_Template.__get("compiled") # line 154, file:
C:\xampp\htdocs\tshirtshop\libs\smarty\sysplugins\smarty_internal_templatebase.php
Smarty_Internal_TemplateBase.fetch("store_front.tpl", null, null,
null, true) # line 394, file:
C:\xampp\htdocs\tshirtshop\libs\smarty\sysplugins\smarty_internal_templatebase.php
Smarty_Internal_TemplateBase.display("store_front.tpl") # line 16,
file: C:\xampp\htdocs\tshirtshop\index.php
Your PHP code looks fine.
You should clear your compiled templates directory, check your directory/files permissions and if it doesn't help, in fact you shouldn't care. Those warnings are because of some file system operations optimization.
If you want to turn them off you can use:
$this->muteExpectedErrors();
inside your constructor (probably after calling parent constructor). Documentation on muteExpectedErrors