DeltaDev

WebPage.php (PHP Script)

Aug 17th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.72 KB | None | 0 0
  1. <?PHP
  2.  
  3. /* Created by Sirius / James Walston, All rights are reserved.
  4. Failure to comply to the NOTICE of this and/or claim of own
  5. written code is by of losing all legitimacy of the script itself. */
  6.  
  7. include "errors.php";
  8.  
  9. interface webclass {
  10.   public function __construct($html_file);
  11.   public function append_header($tag, $value);
  12.   public function delete_header($tag);
  13.   public function commit();
  14. }
  15.  
  16. final class WebPage implements webclass {
  17.   public $page;
  18.   public $page_headers;
  19.  
  20.   // Initializes construction of the class
  21.   public function __construct($html_file) {
  22.     if(file_exists($_SERVER["DOCUMENT_ROOT"] . "/templates/{$html_file}")) {
  23.       $this->page = file_get_contents($_SERVER["DOCUMENT_ROOT"] . "/templates/{$html_file}");
  24.       $this->page_headers = [];
  25.     }
  26.  
  27.     else die("The file {$html_file} does not exist.");
  28.   }
  29.  
  30.   // Creates for allowable appending to headers list
  31.   public function append_header($tag, $value) {
  32.     if(!empty($this->page_headers[$tag])) {
  33.       die("This tag already exists.");
  34.     }
  35.  
  36.     else $this->page_headers[$tag] = $value;
  37.   }
  38.  
  39.   // Deletes a tag in the headers list
  40.   public function delete_header($tag) {
  41.     if(!in_array($tag, $this->page_headers)) {
  42.       die("This tag does not exist.");
  43.     }
  44.  
  45.     else unset($this->page_headers[$tag]);
  46.   }
  47.  
  48.   // Begins re-modifying the headers/tags for the page
  49.   public function commit() {
  50.     if(!empty($this->page_headers)) {
  51.       foreach($this->page_headers as $header_tag => $value) {
  52.         # Works as: str_replace(find text, change to this text, inside var);
  53.        $this->page = str_replace(
  54.           $header_tag,
  55.           $value,
  56.           $this->page
  57.         );
  58.       }
  59.     }
  60.   }
  61. }
  62.  
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment