37

PHP content

Create site22 by copying site21.

  1. /cms
    1. ...
    2. site21
    3. 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:

  1. /cms/site22
    1. library
      1. seval.php
  1. function seval($s) {
  2.     ob_start();
  3.     echo eval('?>'. $s);
  4.     return ob_get_clean();
  5. }

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:

ALTER TABLE content_text ADD seval BOOLEAN NOT NULL DEFAULT '0';

Modify content_text 2 in English by replacing the date by some code in PHP:

UPDATE content_text
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><i><?php setlocale(LC_TIME, 'en_US.UTF-8'); echo strftime('%B %e, %Y'); ?></i></p>
<p><b>frasq.org</b></p>

Modify the version in French:

UPDATE content_text
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><i><?php setlocale(LC_TIME, 'fr_FR.UTF-8'); echo strftime('%e %B %Y'); ?></i></p>
<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:

UPDATE content_text SET eval = '1' WHERE content_id = 2;

Modify the file models/node.inc to add reading the field eval in the function node_get_contents:

  1. function node_get_contents($lang, $node_id) {
  2.     $sqllang=db_sql_arg($lang, false);
  3.  
  4.     $tabnodecontent=db_prefix_table('node_content');
  5.     $tabcontenttext=db_prefix_table('content_text');
  6.  
  7.     $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";
  8.  
  9.     $r = db_query($sql);
  10.  
  11.     return $r;
  12. }

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:

  1.                 case 'text':
  2.                     $s = $c['content_text'];
  3.                     if (!empty($s)) {
  4.                         $eval = $c['content_eval'] == 1 ? true : false;
  5.                         if ($eval) {
  6.                             require_once 'seval.php';
  7.                             $s = seval($s);
  8.                         }
  9.                         $text = $s;
  10.                         $contents[] = compact('type', 'text');
  11.                     }
  12.                     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

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].