if (!is_readable($file)) { throw new Exception("Unable to load library file: {$file} - File is not readable."); } require $file; } /** * Route is based upon current request uri * Routing is executed by UserRoute::$route() * Uri is stripped of all characters except for letters and numbers to perform php function call * example: /some-page would execute the php function UserRoute::somepage() */ $routing = $template->findRoute($uri); if ($routing !== false && is_callable($routing)) { $routing($template, $mysql, $user, $cookie, $form); } else { Lhp::Debug("NO ROUTING FUNCTION FOUND"); } /** * Redirect to URL if applicable */ if ($template->getRedirect() !== null) { $mysql->disconnect(); LhpBrowser::redirectToUrl($template->getRedirect(), 302); } /** * Load template file, parse template placeholders and print to screen * Template file is based upon current request uri (route) (/somepage.html or /somepage = somepage.tpl.htm) */ if (!$template->getAjax()) { $template->loadTemplateFile($user); $template->parseBlocks();
/** * parseBlocks - parse template blocks <lhp>...</lhp> with object / associative array data * We must parse blocks first, as parsing the whole template first will overwrite the data within the blocks * * @return array * * @throws exception */ public function parseBlocks() { global $mysql, $form; $pagelink_info = array(); /** go through each <lhp>...</lhp> block and parse placeholders */ preg_match_all('/<lhp((?:\\s+(?:\\w+)="(?:.*?)")+?)\\s*>(.*?)<\\/lhp>/si', $this->html, $matches); for ($a = 0, $b = count($matches[1]); $a < $b; $a++) { /** Set block paramaters */ $params_str = $matches[1][$a]; $params = array('type' => null, 'query' => null, 'cols' => 0, 'pagelinks' => null, 'pages' => 10, 'name' => null, 'class_on' => null, 'class_off' => null, 'class_next' => null, 'class_prev' => null, 'func' => null, 'functype' => null); preg_match_all('/\\s+(\\w+)="(.*?)"/', $params_str, $pmatches); for ($c = 0, $d = count($pmatches[1]); $c < $d; $c++) { if (isset($pmatches[1][$c])) { $key = strtolower($pmatches[1][$c]); $params[$key] = $this->parse($pmatches[2][$c]); //print "$key = " . $params[$key] . "<BR>"; if ($params[$key] === 'true') { $params[$key] = true; } else { if ($params[$key] === 'false') { $params[$key] = false; } } } } unset($pmatches); /** Set active and else blocks */ $block = $matches[2][$a]; $block_else = null; if (preg_match('/(<lhp:else>)/i', $block, $imatches)) { list($block, $block_else) = explode($imatches[1], $block); } //print "<BR><BR>outer = " . htmlentities($matches[0][$a]) . "<BR>\n"; //print "params = <BR>\n"; //foreach($params as $key=>$val) { // print " $key = $val<BR>\n"; //} //print "inner = " . htmlentities($block) . "<BR>\n"; //print "else = " . htmlentities($block_else) . "<BR>\n"; /** build html block by specified type */ $complete_html = ''; $counter = 1; if ($params['type'] === 'mysql') { //print "query = " . $params['query'] . "<BR>"; $offset = $form->get('offset'); $start = 0; $increment = 0; $rows_found = 0; if (preg_match('/(?:(\\d+|%d),)?\\s*(\\d+)$/', $params['query'], $smatches)) { $start = isset($smatches[1]) ? $smatches[1] : $start; $increment = isset($smatches[2]) ? $smatches[2] : $increment; } else { if (preg_match('/%d,\\s*%d$/', $params['query'], $smatches)) { $start = $form->get('start_limit'); $params['query'] = sprintf($params['query'], $form->get('start_limit'), $form->get('end_limit')); } } if ($start === '%d') { $start = $offset * $increment; $params['query'] = sprintf($params['query'], $start); } $counter = $counter + $start; while ($row = $mysql->fetch($params['query'])) { /** execute optional function parameter to add more template variables */ if ($params['func'] !== null && $params['functype'] !== null && is_callable($params['func'])) { $params['func']($row, $params['functype']); } if ($params['cols'] > 0 && ($counter - 1) % $params['cols'] === 0) { $complete_html .= "\n<tr>\n"; } $row['counter'] = $counter; $complete_html .= $this->parse($block, $row); if ($params['cols'] > 0 && $counter % $params['cols'] === 0) { $complete_html .= "\n</tr>\n"; } $counter++; } /** get total number of rows if SQL_CALC_FOUND_ROWS found in query */ /** mainly used for generating pagelinks on content listing / search listing pages */ if (preg_match('/SQL_CALC_FOUND_ROWS/', $params['query'])) { $rows_found = $mysql->getFoundRows(); $total_pages = floor($rows_found / $increment) + ($rows_found % $increment > 0 ? 1 : 0); $this->data['pages_found'] = $total_pages; $this->data['rows_found'] = $rows_found; if ($params['pagelinks'] !== null) { $pagelink_info[$params['pagelinks']] = $total_pages; } Lhp::Debug("rows_found = {$rows_found}"); Lhp::Debug("pages_found = {$total_pages}"); } } else { if ($params['type'] === 'pagelinks' && isset($pagelink_info[$params['name']])) { $total_pages = $pagelink_info[$params['name']]; // print "total_pages = $total_pages<BR>"; $pagelinks = ""; $start_page = 0; $end_page = $total_pages; if ($total_pages > $params['pages']) { if ($offset > floor($params['pages'] / 2)) { $start_page = $offset - floor($params['pages'] / 2); } $end_page = $start_page + $params['pages']; if ($end_page > $total_pages) { $end_page = $total_pages; $start_page -= $params['pages'] - ($end_page - $start_page); } } if ($total_pages > 1) { $pagelinks .= '<a href="?offset=' . ($offset === 0 ? $total_pages - 1 : $offset - 1) . '" class="' . $params['class_prev'] . '">« PREV</a>'; } for ($c = $start_page, $d = $end_page; $c < $d; $c++) { $page_number = $c + 1; if ($c == $offset) { $pagelinks .= '<a href="?offset=' . $c . '" class="' . $params['class_on'] . '">' . $page_number . '</a>'; } else { $pagelinks .= '<a href="?offset=' . $c . '" class="' . $params['class_off'] . '">' . $page_number . '</a>'; } } if ($total_pages > 1) { $pagelinks .= '<a href="?offset=' . ($offset === $total_pages - 1 ? 0 : $offset + 1) . '" class="' . $params['class_next'] . '">NEXT »</a>'; } $complete_html = $this->parse($block, array('pagelinks' => $pagelinks)); } } /** for empty results for lists that have an <lhp:else> block */ if (empty($complete_html) && $block_else !== null) { $complete_html = $block_else; } /** update block placeholder with parsed html */ $this->html = preg_replace('/' . preg_quote($matches[0][$a], '/') . '/', $complete_html, $this->html); } }