info343/lib/writeups/php/solutions.php

<?php
   function load($filename, $from = 1, $to = 0, $context = 0) {
      $file = file($filename, FILE_IGNORE_NEW_LINES);
      $from = max(1, $from - $context);
      $to = ($to ? min($to + $context, count($file)) : count($file));
      return implode("\n", array_slice($file, $from - 1, $to - $from + 1));
   }
   
   function escape($file, $tab = '   ') {
      global $metahtml_replacements;
      $metahtml_replacements = array();
      $tabconverted = str_replace("\t", $tab, $file);
      $metahtml_removed = preg_replace_callback('~<!--\\[(.+?)\\]-->|/\*\\[(.+?)\\]\*/~', 'metahtml_remove', $tabconverted);
      $escaped = htmlspecialchars($metahtml_removed);
      $metahtml_replaced = preg_replace_callback('/%%%(\d+)%%%/', 'metahtml_replace', $escaped);
      return $metahtml_replaced;
   }
   
   $metahtml_replacements = array();
   
   function metahtml_remove($matches) {
      global $metahtml_replacements;
      $i = count($metahtml_replacements);
      $metahtml = $matches[1] ? $matches[1] : $matches[2];
      array_push($metahtml_replacements, $metahtml);
      return "%%%$i%%%";
   }
   
   function metahtml_replace($matches) {
      global $metahtml_replacements;
      $replacement = $metahtml_replacements[$matches[1]];
      return $replacement;
   }
?>