Overbleed Posted October 23, 2010 Share Posted October 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/ Share on other sites More sharing options...
trq Posted October 23, 2010 Share Posted October 23, 2010 I can post my template class if needed. Considering that is where all the work is done, it would be most helpful. Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125487 Share on other sites More sharing options...
Overbleed Posted October 23, 2010 Author Share Posted October 23, 2010 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 **/ Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125490 Share on other sites More sharing options...
trq Posted October 23, 2010 Share Posted October 23, 2010 I don't see where you parse these {integrate} tags specifically. Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125492 Share on other sites More sharing options...
Overbleed Posted October 23, 2010 Author Share Posted October 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125495 Share on other sites More sharing options...
trq Posted October 23, 2010 Share Posted October 23, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125497 Share on other sites More sharing options...
Overbleed Posted October 23, 2010 Author Share Posted October 23, 2010 Thanks, I'll give that a try. What would you suggest I do then, so I dont have that security issue. I have a defined variable on my index that only allows files to be called from the index, if that even makes a difference. Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125499 Share on other sites More sharing options...
trq Posted October 23, 2010 Share Posted October 23, 2010 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(); } } Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125501 Share on other sites More sharing options...
Overbleed Posted October 23, 2010 Author Share Posted October 23, 2010 Thank You!! This works great now! Quote Link to comment https://forums.phpfreaks.com/topic/216625-requiring-a-file-at-a-certain-point/#findComment-1125505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.