$debug = 0 ; $ignoreList = array() ; // Read file contents $fileContents = file(basename($_SERVER['PHP_SELF'])) ; // Strip off first line unset($fileContents[0]) ; // Render parseIncludes( implode($fileContents), $depth ) ; function parseIncludes($text, $localDepth) { global $ignoreList, $depth, $debug ; $tryAgain = False ; $end = strpos($text, '>>') ; if ($end !== False) { $start = strrpos(substr($text, 0, $end), '<<') ; if ($start !== False && $end > $start) { // Start and End found, so we'll be replacing something! if ($debug) echo 'Start = ' . $start . ' & End = ' . $end . '
' ; $inc = trim(substr($text, $start+2, $end-$start-2)) ; // Start looking in this directory. $pathPrefix = './' ; $incFound = False ; while ($incFound === False && $localDepth >= 0) { $patt = ($pathPrefix . $inc . '.*') ; $files = glob($patt) ; if (count($files) == 0) { // No exact match of include name. Try looking for an addon instead. $patt2 = $pathPrefix . $inc . '-DCMSADD.php' ; // Is there an addon that matches the include call? $files2 = glob($patt2) ; if (count($files2) == 1 && array_search($files2[0], $ignoreList) === False) { array_push($ignoreList, $files2[0]) ; // Include the addon $GLOBALS['includePath'] = $pathPrefix . $inc . '-DCMSADD.data' ; include ($patt2) ; // Run the addon's own rendering function. $incText = show() ; $incFound = True ; // Stop looking for the include file if ($debug) { echo '
Show() function returned:' ; echo $incText ; } } } else if (array_search($files[0], $ignoreList) === False) { // Include exists. Read contents. array_push($ignoreList, $files[0]) ; $incText = implode(file($files[0])) ; $incFound = True ; // Stop looking for the include file } if ($incFound == True) { // Replace if ($debug) { echo '
  • First bit: ' . substr($text, 0, $start) . '
  • ' ; echo '
  • Inctext: ' . $incText . '
  • ' ; echo '
  • Last bit: ' . substr($text, $end + 2) . '
  • ' ; } $text = substr($text, 0, $start) . $incText . substr($text, $end + 2) ; $tryAgain = True ; } if ($pathPrefix == './') { $pathPrefix = '../' ; } else { $pathPrefix .= '../' ; } // Reduce countdown to root. $localDepth-- ; // Loop round until we either get to root or find a matching include... } if ($incFound === False) { // Strip out the include call $text = substr($text, 0, $start) . '' . substr($text, $end + 2) ; $tryAgain = True ; } } } if ($tryAgain === True) { $start = False ; $end = False ; parseIncludes($text, $depth) ; } else echo $text ; } ?>