Adding PHP code to master pages

This forum is for all Flare issues related to the HTML5, WebHelp, WebHelp Plus, and Adobe Air Targets
Post Reply
epenet
Jr. Propeller Head
Posts: 4
Joined: Tue Nov 17, 2015 2:37 am

Adding PHP code to master pages

Post by epenet »

Hi,
In order to add authentication to our madcap output, I need to add some PHP code both in the middle and at the top of the master page.

However, when I add the PHP code to the top of the master page, I get an error (in French) which roughly translates as "root element must be html" and I cannot switch back to XML editor.

Code: Select all

<?php include_once '/local_path/session_config.php'; ?>
<?xml version="1.0" encoding="utf-8"?>
<html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="7" MadCap:lastHeight="205" MadCap:lastWidth="1156">
Is there another way to add this PHP line at the top of every page?
Or if it is possible to tell the XML editor to ignore PHP code placed at the top of the screen?

Cheers,
Erwann

Note: I am able to embed PHP code just fine in the middle of the HTML. The issue appears only when I need to add the PHP code before the HTML output.
ChoccieMuffin
Senior Propellus Maximus
Posts: 2632
Joined: Wed Apr 14, 2010 8:01 am
Location: Surrey, UK

Re: Adding PHP code to master pages

Post by ChoccieMuffin »

Sounds like it works in the middle of the file because you are including it in the topic. Could you add it immediately after the <body> tag?
Started as a newbie with Flare 6.1, now using Flare 2023.
Report bugs at http://www.madcapsoftware.com/bugs/submit.aspx.
Request features at https://www.madcapsoftware.com/feedback ... quest.aspx
epenet
Jr. Propeller Head
Posts: 4
Joined: Tue Nov 17, 2015 2:37 am

Re: Adding PHP code to master pages

Post by epenet »

Hi,

The trouble is that I am trying to work with the "session_start" command, so the PHP code needs to be set right at the top of the page, before the <?xml declaration.

For now I've worked around it by having a separate PHP script on the server to automatically loop through all the pages and add the line at the top of the file if it's not there yet.
The issue is that I need to remember to run the script after every publish.

Is it possible to run a command automatically at the end of a publish?
epenet
Jr. Propeller Head
Posts: 4
Joined: Tue Nov 17, 2015 2:37 am

Re: Adding PHP code to master pages

Post by epenet »

I've almost finished sorting out my authentication/session, and I'll be happy to publish it here once it's finished.

Now the issue that I have is that flare doesn't seem to recognise links with just question marks, and throws up lots of warnings during compilation.

Code: Select all

<a href="?action=login">Login</a>
Is there a way to reference the current file in madcap?
epenet
Jr. Propeller Head
Posts: 4
Joined: Tue Nov 17, 2015 2:37 am

Re: Adding PHP code to master pages

Post by epenet »

For those interested, this is how I have implemented it using two files:

/root/includes/session.php: this is a where the authentication logic resides.
There is an array $attributes which is ALWAYS set. If the user is authenticated, then it contains the user data. If the user isn't authenticated, then it is an empty array.

Code: Select all

$attributes = array();
if (...) {
  $attributes = array('id' => $userid);
}
/root/includes/update.php: this script it used to prepend the madcap output with require_once, if it's not yet there.

Code: Select all

function prepend($filename) {
  $filecontent = file_get_contents($filename);
  if(strpos($filecontent, "<?php require_once '/root/includes/session.php'; ?>") !== 0) {
    file_put_contents($filename, "<?php require_once '/root/includes/session.php'; ?>\r\n" . $filecontent);
    return true;
  }
  return false;
}

function processdirectory($dir) {
  if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle)))
    {
      if ($file == ".") {
      }
      elseif ($file == "..") {
      }
      elseif (is_dir($dir . '/' . $file)) {
        $list .= processdirectory($dir . '/' . $file, $list);
      }
      elseif (strtolower(substr($file, strrpos($file, '.') + 1)) == 'php') {
        if (prepend($dir . '/' . $file)) {
          $list .= '<li>updated ' . $dir . '/' . $file . '</li>';
        }
      }
    }
  }
  closedir($handle);
  return $list;
}
processdirectory('/root/content/madcap');
Finally, in the master page, I use the following logic:
  1. If the $attributes array doesn't exists, run the update script and show "maintenance in progress"
  2. If the $attributes array exists, but not the id, then the user isn't logged in, and show "login"
  3. If the $attributes array exists, and also the id, then show the content

Code: Select all

  <?php if (!isset($attributes)) : require_once '/root/includes/update.php'; echo('<div class="MCBreadcrumbsBox_0">Maintenance in progress... Please refresh the page.</div>'); ?>
  <?php elseif (!isset($attributes['id'])) : ?>
    <div class="MCBreadcrumbsBox_0">
      Hello guest. You need to <a href="?action=login">login</a> or <a href="?action=register">register</a> to view the content of the page.
    </div>
  <?php else : ?>
    <div class="row collapse">
      <div class="sideContent">
        <div class="clearfix">
          <MadCap:topicToolbarProxy />
        </div>
      </div>
      <MadCap:breadcrumbsProxy />
      <MadCap:bodyProxy />
    </div>
  <?php endif; ?>
fvila
Propeller Head
Posts: 46
Joined: Fri Feb 27, 2015 3:44 am

Re: Adding PHP code to master pages

Post by fvila »

Thanks for sharing this, epenet!
arshabhirai
Propeller Head
Posts: 44
Joined: Tue Feb 28, 2017 12:15 pm

Re: Adding PHP code to master pages

Post by arshabhirai »

epenet wrote:For those interested, this is how I have implemented it using two files:

/root/includes/session.php: this is a where the authentication logic resides.
There is an array $attributes which is ALWAYS set. If the user is authenticated, then it contains the user data. If the user isn't authenticated, then it is an empty array.

Code: Select all

$attributes = array();
if (...) {
  $attributes = array('id' => $userid);
}
/root/includes/update.php: this script it used to prepend the madcap output with require_once, if it's not yet there.

Code: Select all

function prepend($filename) {
  $filecontent = file_get_contents($filename);
  if(strpos($filecontent, "<?php require_once '/root/includes/session.php'; ?>") !== 0) {
    file_put_contents($filename, "<?php require_once '/root/includes/session.php'; ?>\r\n" . $filecontent);
    return true;
  }
  return false;
}

function processdirectory($dir) {
  if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle)))
    {
      if ($file == ".") {
      }
      elseif ($file == "..") {
      }
      elseif (is_dir($dir . '/' . $file)) {
        $list .= processdirectory($dir . '/' . $file, $list);
      }
      elseif (strtolower(substr($file, strrpos($file, '.') + 1)) == 'php') {
        if (prepend($dir . '/' . $file)) {
          $list .= '<li>updated ' . $dir . '/' . $file . '</li>';
        }
      }
    }
  }
  closedir($handle);
  return $list;
}
processdirectory('/root/content/madcap');
Finally, in the master page, I use the following logic:
  1. If the $attributes array doesn't exists, run the update script and show "maintenance in progress"
  2. If the $attributes array exists, but not the id, then the user isn't logged in, and show "login"
  3. If the $attributes array exists, and also the id, then show the content

Code: Select all

  <?php if (!isset($attributes)) : require_once '/root/includes/update.php'; echo('<div class="MCBreadcrumbsBox_0">Maintenance in progress... Please refresh the page.</div>'); ?>
  <?php elseif (!isset($attributes['id'])) : ?>
    <div class="MCBreadcrumbsBox_0">
      Hello guest. You need to <a href="?action=login">login</a> or <a href="?action=register">register</a> to view the content of the page.
    </div>
  <?php else : ?>
    <div class="row collapse">
      <div class="sideContent">
        <div class="clearfix">
          <MadCap:topicToolbarProxy />
        </div>
      </div>
      <MadCap:breadcrumbsProxy />
      <MadCap:bodyProxy />
    </div>
  <?php endif; ?>
Hi epenet,
Thanks for sharing!
I am in the process of creating a web-based feedback form and your implementation may work for me. I have zero experience/knowledge using php, though.

This directory: /root/includes/session.php, did you create it Flare "Content Explorer"? And where in the Masterpage did you embed the php code?

Thanks,
A
Post Reply