Page 1 of 1

using <?php include ?> in descriptions fields

Posted: Sat Sep 14, 2019 7:01 am
by Fabienlolo
OC 3.0.2
Journal 2 theme
Hi everyone,
i have my OC in a subfolder of my domain. And then i got my domain with static pages, among them a "Privacy Policy" page. It is made out of different php files, one of them being the privacy policy text itself.
Now to make it one source only through out my web site, static pages and OC, i tried to use <? php include ("mypoliciyprivacytextfile.php"); ? > in the description box using code view, but opencart comments out the php code, my <? php include ("mypoliciyprivacytextfile.php"); ? > code becoming <!--? php include ("mypoliciyprivacytextfile.php"); ?-- >.
I found this topic viewtopic.php?f=202&t=202308&p=714911&h ... de#p714733 and this topic viewtopic.php?f=202&t=201321#p711081 but if they don't match my issue.
Is is possible to use <? php include ("file.php"); ?> in opencart description fields? If yes, then how?
Thank you all for your help

Re: using <?php include ?> in descriptions fields

Posted: Sat Sep 14, 2019 9:35 am
by letxobnav
you can include static html.

Re: using <?php include ?> in descriptions fields

Posted: Sat Sep 14, 2019 11:27 am
by Fabienlolo
Great news. thanks. any clue how?

Re: using <?php include ?> in descriptions fields

Posted: Sat Sep 14, 2019 12:46 pm
by letxobnav
you can create a directory includes under catalog/view/theme/default/template
put your privacy-policy.twig twig file in there and use:

Code: Select all

{% include directory ~ '/default/template/includes/privacy-policy.twig' %}
to include a twig file into a twig file.
of course this assumes you are using twig.
problem there is if you have multi-lingual privacy-policy files, twig wouldn't know what language we are in.

or as a general principle:

you can add to the controller:

Code: Select all

ob_start();
include('YOUR_INCLUDE_FILENAME');
$data['my_included_stuff'] = ob_get_clean();
and add to your twig:

Code: Select all

{{ my_included_stuff }}
or your tpl:

Code: Select all

<?php echo $data['my_include_stuff']; ?> 

Re: using <?php include ?> in descriptions fields

Posted: Sat Sep 14, 2019 1:35 pm
by Fabienlolo
Hi letxobnav,
thanks for your complete answer with 2 solutions. The first one is closer to what i am intending to do and I can see the second one is more a work around using a key that i can add in the templates if i mention the function in the corresponding controller, and i can make this through out my OC using only one source .
But actually i am trying to 'echo' in OC description fields some file from my own domain but from outside OC files. So my question should be more like, is there anyway to avoid description fields, for instance the one from privacy policy of my store (admin > catalog > information > Privacy Policy), to turn my <?php include ?> code into a <!--?php include ?--> comment ?
I guess it might be for some safety reason, but is there anyway to clear security? And if yes, what might be the issues doing so?

Re: using <?php include ?> in descriptions fields

Posted: Sat Sep 14, 2019 6:33 pm
by letxobnav
well, no, you cannot add php to a description field unless you use the very unsafe "eval()" function on it.
I do not know which part is making it comments, perhaps the editor or the save function.

I use includes in the html module by adding {{ filename }} to the description in admin.
I have the include files in an include directory.

I then use:

Code: Select all

// get an include file instead of the entered html if indicated by {{ filename }} pulled from the includes directory
$html = $data['description'];
$btoken = '{{';
$etoken = '}}';
$final_html = '';
$begin = '';
$end = '';
if (stristr($html,$btoken) and stristr($html,$etoken)) {
while (stristr($html,$btoken) and stristr($html,$etoken)) {
	$bpos = strpos($html,$btoken);
	$epos = strpos($html,$etoken);
	$begin = mb_substr($html,0,$bpos);
	$end = mb_substr($html,$epos+strlen($btoken));
	$include_file = mb_substr($html,$bpos+strlen($btoken),$epos-$bpos-strlen($etoken));
	ob_start();
	include(DIR_INCLUDES . $include_file);
	$include_html = ob_get_clean();
	$final_html .= $begin.$include_html;
	$html = $end;
}
$data['description'] = $final_html . $end;
}
in the description field I simply have {{my_include_html_filename}}
The code basically replaces {{my_include_html_filename}} with the content of the file "my_include_html_filename" in the DIR_INCLUDES directory if such a reference exists.

you could do the same with description in information.php after:

Code: Select all

$data['description'] = html_entity_decode($information_info['description'], ENT_QUOTES, 'UTF-8');
just make sure you define the DIR_INCLUDES constant in config.php or use the full path.

Re: using <?php include ?> in descriptions fields

Posted: Sun Sep 15, 2019 1:19 am
by Fabienlolo
Hi letxobnav,
thank you to clear that up, and i understand then i cannot make it per language as you mentioned in a previous post.
One last thing now, i guess i can have the file source out of my OC files right? as i can setup any directory if i follow you.

Re: using <?php include ?> in descriptions fields

Posted: Sun Sep 15, 2019 8:45 am
by letxobnav
You can make it per language if you keep the control inside the controller.
With only the twig include twig you cannot (unless you use separate twigs per language):

Code: Select all

{% include directory ~ '/default/template/includes/privacy-policy.twig' %}
Twig include takes it from the theme directory on down, above that it cannot find it.

For the other solutions you can define any directory in your filesystem as DIR_INCLUDES.

Re: using <?php include ?> in descriptions fields

Posted: Fri Dec 22, 2023 5:06 am
by BPPhotog
I know this is an old thread but I think this is exactly what I am trying to do; trying to get html from a file to show in a description.
Could someone tell me where the "get an include file instead of the entered html.." code goes?
Thanks!
letxobnav wrote:
Sat Sep 14, 2019 6:33 pm
well, no, you cannot add php to a description field unless you use the very unsafe "eval()" function on it.
I do not know which part is making it comments, perhaps the editor or the save function.

I use includes in the html module by adding {{ filename }} to the description in admin.
I have the include files in an include directory.

I then use:

Code: Select all

// get an include file instead of the entered html if indicated by {{ filename }} pulled from the includes directory
$html = $data['description'];
$btoken = '{{';
$etoken = '}}';
$final_html = '';
$begin = '';
$end = '';
if (stristr($html,$btoken) and stristr($html,$etoken)) {
while (stristr($html,$btoken) and stristr($html,$etoken)) {
	$bpos = strpos($html,$btoken);
	$epos = strpos($html,$etoken);
	$begin = mb_substr($html,0,$bpos);
	$end = mb_substr($html,$epos+strlen($btoken));
	$include_file = mb_substr($html,$bpos+strlen($btoken),$epos-$bpos-strlen($etoken));
	ob_start();
	include(DIR_INCLUDES . $include_file);
	$include_html = ob_get_clean();
	$final_html .= $begin.$include_html;
	$html = $end;
}
$data['description'] = $final_html . $end;
}
in the description field I simply have {{my_include_html_filename}}
The code basically replaces {{my_include_html_filename}} with the content of the file "my_include_html_filename" in the DIR_INCLUDES directory if such a reference exists.

you could do the same with description in information.php after:

Code: Select all

$data['description'] = html_entity_decode($information_info['description'], ENT_QUOTES, 'UTF-8');
just make sure you define the DIR_INCLUDES constant in config.php or use the full path.

Re: using <?php include ?> in descriptions fields

Posted: Fri Dec 22, 2023 6:20 pm
by ADD Creative
BPPhotog wrote:
Fri Dec 22, 2023 5:06 am
I know this is an old thread but I think this is exactly what I am trying to do; trying to get html from a file to show in a description.
Could someone tell me where the "get an include file instead of the entered html.." code goes?
Thanks!
In one of the controllers. What pages are you trying to change?