Jump to content

Requiring a file at a certain point


Overbleed

Recommended Posts


Hello, I have a problem with a script I'm making and I'm pretty stumped.

 

I have coded a template parser that parses tags in {} and I am allowing users to integrate a part of my script with the {integrate} tag.

 

This is my script tags.

 

  // Template Tags
$data = array();
$data[title]  = $config[title];
    $data

 = $mcore->currentPage();
    $data[integrate] = $mcore->pageSwitch();
    

 

page switch function:

    
        function pageSwitch() {
            if(!$_GET['act']) {
                $use = "news.php";
                } else {
                    $use = $_GET['act'];
                }
                
                $this->file = $use;
                
                return $this->file;
            }

 

I want it to include the file where the person puts the {integrate} tag in their template.

 

But when I call it, it includes the file at the top of the template, rather than where the put it.

 

How would I get it to include the template only where they put it?

 

I can post my template class if needed.

 

 

 

Link to comment
Share on other sites

Alright here it is. Thanks.

class TemplateParser {

 var $data;

 /**
 * @initData - 1.0
     * Initializes "macro=>value" array
 */
     
   function initData($data)
   {
      $this->data = array();   	
      $this->data = $data;   	
   }
   
   /**
 * @parseTemplateFile - 1.0
 * parses the template file
 */

   function parseTemplateFile($templateFile)
   {
      $searchPattern          = "/\{([a-zA-Z0-9_]+)\}/i"; // macro delimiter "{" and "}"
      $replacementFunction    = array(&$this, 'parseMatchedText');  //Method callbacks are performed this way 
      $fileData               = file_get_contents($templateFile);                        
      $parsedTemplate         = preg_replace_callback($searchPattern, $replacementFunction, $fileData);
      
      return $parsedTemplate;
   }

   /**
 * @parseTemplateData - 1.0
 * parses the template data
 */
     
   function parseTemplateData($templateData)
   {
      $searchPattern          = "/\{([a-zA-Z0-9_]+)\}/i"; //macro delimiter "{" and "}"
      $replacementFunction    = array(&$this, 'parseMatchedText');  //Method callbacks are performed this way       
      $parsedTemplate         = preg_replace_callback($searchPattern, $replacementFunction, $templateData);
      
      return $parsedTemplate;
   }
   
   /**
     * @parseMatchedText - 1.0
     * Callback function that returns value of a matching macro
     */
   
   function parseMatchedText($matches)
   {
      return $this->data[$matches[1]];	
   }

} /** CLASS END - LAST EDITED IN VERSION 1.0 **/

 

Link to comment
Share on other sites

I put the {integrate} tag in the html file when I call that class.

 

I call it with:

$template = $tpl->parseTemplateFile("templates/".$useTemplate."/template.php");
echo $template;

 

the html file looks like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{title} - {page}</title>
</head>

<body>
{integrate}
</body>
</html>

 

and I want integrate to include my news file, and then switch files when I use the ?act= in my url.

 

 

 

 

Link to comment
Share on other sites

So, at the moment then, this method simply returns the name of the file to be included? And you want it to actually return the included file?

 

function pageSwitch() {
            if(!$_GET['act']) {
                $use = "news.php";
                } else {
                    $use = $_GET['act'];
                }
                
                $this->file = $use;
                
                return $this->file;
            }

 

Should be....

 

function pageSwitch() {
  if (!$_GET['act']) {
    $use = "news.php";
  } else {
    $use = $_GET['act'];
  }
  $this->file = $use;
  ob_start();
  include $this->file;
  return ob_get_clean();
}

 

I would be very careful giving users the ability to execute php scripts passed in via the url like that, this poses quite a few security issues.

Link to comment
Share on other sites

I would (at least) make sure to put all files that can possibly be included within a certain directory, then check for your files existence within that directory before including it. eg;

 

function pageSwitch() {
  if (!$_GET['act']) {
    $use = "news.php";
  } else {
    $use = $_GET['act'];
  }
  $this->file = $use;
  if (file_exists('includes/' . $this->file)) {
    ob_start();
    include 'includes/' . $this->file;
    return ob_get_clean();
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.