PHP content
Create site22 by copying site21.
- /cms
- ...
- site21
- site22
In this chapter, we are going to add code in PHP directly in a text content.
To test the result online, enter http://www.frasq.org/cms/site22/en in the address bar of your navigator. Click on the link Legal information. The signature by frasq.org is preceded by the current date which is computed in PHP when the page is displayed.
Add the file seval.php in the folder library with the following content:
- /cms/site22
- library
- seval.php
- library
- function seval($s) {
- ob_start();
- echo eval('?>'. $s);
- return ob_get_clean();
- }
seval
returns the result of the evaluation by PHP of $s
. Adding the closing tag ?>
at the beginning of the text protects from a tag <?php
left opened in $s
and garantees that all the code between the tags <?php
and ?>
contained in $s
will be evaluated.
Test seval
:
$ php -a
php> require_once 'library/seval.php';
php> $text='<p>1 + 1 = <?php echo 1+1; ?></p>';
php> echo seval($text);
<p>1 + 1 = 2</p>
php> quit
Add the field eval
to the content_text
table:
Modify content_text
2 in English by replacing the date by some code in PHP:
SET text = '<p><i><?php setlocale(LC_TIME, ''en_US.UTF-8''); echo strftime(''%B %e, %Y''); ?></i></p>\r\n<p><b>frasq.org</b></p>'
WHERE content_text.content_id = 2 AND content_text.locale = 'en';
Inserts the following content at the end of the page in English:
<p><b>frasq.org</b></p>
Modify the version in French:
SET text = '<p><i><?php setlocale(LC_TIME, ''fr_FR.UTF-8''); echo strftime(''%e %B %Y''); ?></i></p>\r\n<p><b>frasq.org</b></p>'
WHERE content_text.content_id = 2 AND content_text.locale = 'fr';
Inserts the following content at the end of the page in French:
<p><b>frasq.org</b></p>
IMPORTANT: Depending on the host system of the site, the format of the parameter to the setlocale
function can vary.
Set the field eval
of content_text
2 to 1:
Modify the file models/node.inc to add reading the field eval
in the function node_get_contents
:
- function node_get_contents($lang, $node_id) {
- $sqllang=db_sql_arg($lang, false);
- $tabnodecontent=db_prefix_table('node_content');
- $tabcontenttext=db_prefix_table('content_text');
- $sql="SELECT nc.content_id AS content_id, nc.content_type AS content_type, nc.number AS content_number, ct.text AS content_text, ct.eval AS content_eval FROM $tabnodecontent nc LEFT JOIN $tabcontenttext ct ON nc.content_type='text' AND ct.content_id=nc.content_id AND ct.locale=$sqllang WHERE nc.node_id=$node_id ORDER BY nc.number";
- $r = db_query($sql);
- return $r;
- }
Adds returning the field called content_eval
to a content.
Modify the file blocks/nodecontent.php to take into account the value of the field content_eval
:
- case 'text':
- $s = $c['content_text'];
- if (!empty($s)) {
- $eval = $c['content_eval'] == 1 ? true : false;
- if ($eval) {
- require_once 'seval.php';
- $s = seval($s);
- }
- $text = $s;
- $contents[] = compact('type', 'text');
- }
- break;
Passes the value of content_text
to seval
if content_eval
is 1.
Enter http://localhost/cms/site22/en in the address bar of your navigator. Click on the link Legal information. Check that the signature is preceded by the date of the day.
Comments