/** * @name tplrefresh * @author SkyWorld * @date 2011-1-16 * @description This funtion used to parse templates * If teplates has been complied return the complied filename * Otherwise complie the templates, and then return the complied filename * @parameters void * @return bool **/ function template($fileName) { $fileName = trim($fileName); $tplFile = NCG_TEMPLATES . $fileName; $comFileName = NCG_TEMPLATES_C . dirname($fileName) . '/com_' . basename($fileName) . '.php'; $templates_c_dir = dirname($comFileName); if (is_readable($comFileName) && !tplrefresh($comFileName)) { return $comFileName; } if (file_exists($tplFile)) { $temp = "<?php if (!defined('IN_NCG')) exit ('Access Denied.'); ?>\n"; $temp .= file_get_contents($tplFile); /*patterns array*/ $pattern = array('/\\{\\s*template\\s*(.+?)\\s*\\}/', '/\\s*\\$([\\w_]+)\\.([\\w_]+)\\.([\\w_]+).([\\w_]+)\\s*/', '/\\s*\\$([\\w_]+)\\.([\\w_]+)\\.([\\w_]+)\\s*/', '/\\s*\\$([\\w_]+)\\.([\\w_]+)\\s*/', '/\\{\\s*\\$([\\w\\x7f-\\xff][\\w\\x7f-\\xff\\[\\]\\"\']*)\\s*\\}/i', '/<!--\\s*if\\s*(.+?)\\s*-->/', '/<!--\\s*else\\s*-->/', '/<!--\\s*else\\s*if\\s*(.+?)\\s*-->/', '/<!--\\s*\\/if\\s*-->/', '/<!--\\s*loop\\s+\\$(.+?)\\s+\\$(.+?)\\s+\\$(.+?)\\s*-->/', '/<!--\\s*loop\\s+\\$(.+?)\\s+\\$(.+?)\\s*-->/', '/<!--\\s*\\/loop\\s*-->/', '/\\{([A-Z].+?)\\}\\}/'); /*replacements array*/ $replacement = array('<?php include template ${1} ?>', '\\$${1}[\'${2}\'][\'${3}\'][\'${4}\']', '\\$${1}[\'${2}\'][\'${3}\']', '\\$${1}[\'${2}\']', '<?php echo $${1}; ?>', '<?php if ${1} { ?>', '<?php } else { ?>', '<?php } elseif ${1} { ?>', '<?php } ?>', '<?php foreach ($${1} as $${2} => $${3}) { ?>', '<?php foreach ($${1} as $${2}) { ?>', '<?php } ?>', '<?php echo ${1}; ?>'); $temp = preg_replace($pattern, $replacement, $temp); $temp = trim($temp); if (!file_exists($templates_c_dir)) { @mkdir($templates_c_dir, 0777, true) or die("File dir can not be created!"); } file_put_contents($comFileName, $temp); return $comFileName; } else { die("Template file cannot be found."); } }
/** * @name tplrefresh * @author SkyWorld * @date 2011-9-17 * @description This funtion used to parse templates * If teplates has been complied return the complied filename * Otherwise complie the templates, and then return the complied filename * @parameters void * @return bool **/ function template($fileName) { $fileName = trim($fileName); $fileDir = dirname($fileName) == '.' || dirname($fileName) == '/' ? '' : dirname($fileName) . '/'; $tplFile = SKY_TEMPLATES . $fileName; $comFile = SKY_TEMPLATES_C . $fileDir . 'com_' . basename($fileName) . '.php'; $templates_c_dir = dirname($comFile); if (is_readable($comFile) && !tplrefresh($comFile, $tplFile)) { return $comFile; } $temp = "<?php if (!defined('IN_SKY')) exit ('Access Denied.'); ?>\n"; $temp .= @file_get_contents($tplFile); /*patterns array*/ if (file_exists($tplFile)) { $pattern = array('/\\{\\s*template\\s*(.+?)\\s*\\}/', '/\\{\\s*\\$([\\w\\x7f-\\xff][\\w\\x7f-\\xff\\[\\]\\"\']*)\\s*\\}/i', '/<!--\\s*if\\s*(.+?)\\s*-->/', '/<!--\\s*else\\s*-->/', '/<!--\\s*else\\s*if\\s*(.+?)\\s*-->/', '/<!--\\s*\\/if\\s*-->/', '/<!--\\s*loop\\s+\\$(.+?)\\s+\\$(.+?)\\s+\\$(.+?)\\s*-->/', '/<!--\\s*loop\\s+\\$(.+?)\\s+\\$(.+?)\\s*-->/', '/<!--\\s*\\/loop\\s*-->/', '/\\{\\s*([A-Z_]+?)\\s*\\}/', '/\\{\\s*lang\\s+(\\w+?)\\s*\\}/', '/\\{\\s*lang\\s+(\\w+?)\\s+(\\w+?)\\s*\\}/', '/\\{\\s*php\\s+(.+?)\\s*\\}/'); /*replacements array*/ $replacement = array('<?php include template ${1} ?>', '<?php echo $${1}; ?>', '<?php if ${1} { ?>', '<?php } else { ?>', '<?php } elseif ${1} { ?>', '<?php } ?>', '<?php foreach ($${1} as $${2} => $${3}) { ?>', '<?php foreach ($${1} as $${2}) { ?>', '<?php } ?>', '<?php echo ${1}; ?>', '<?php echo \\$lang[\'${1}\']; ?>', '<?php echo \\$lang[\'${1}\'][\'${2}\']; ?>', '<?php ${1}?>'); $temp = preg_replace($pattern, $replacement, $temp); $temp = trim($temp); if (!file_exists($templates_c_dir)) { @mkdir($templates_c_dir, 0777, true) or die("File dir can not be created!"); } file_put_contents($comFile, $temp); } else { die('Template file cannot be found.'); } return $comFile; }