Einzelnen Beitrag anzeigen

Benutzerbild von Valle
Valle

Registriert seit: 26. Dez 2005
Ort: Karlsruhe
1.223 Beiträge
 
#11

Re: [Homepage] Erweiterungen für jedermann?

  Alt 19. Dez 2008, 19:24
Darfst du gerne verwenden, auch ohne irgendwelchen Lizenzangaben etc.

Code:
<?php

  class PageStorage {
 
    private $pages;
    private $filename;
 
    private function assert_index($index) {
      if (intval($index) >= count($this->pages))
        die("Fatal: Invalid index ".htmlspecialchars($index));
    }
   
    private function implode($pages) {
      $buffer = array();
      foreach ($pages as $page)
        if (trim($page) != "")
          $buffer[] = $page;
      return implode("\n", $buffer);
    }
 
    public function __construct($filename) {
      $this->pages = @file_get_contents($filename);
      if ($this->pages === false)
        die("Fatal: Couldn't open file ".htmlspecialchars($filename));
      $this->pages = explode("\n", $this->pages);
      $this->filename = $filename;
    }
   
    public function get_pages() {
      return $this->pages;
    }
   
    public function get_page($index) {
      $this->assert_index($index);
      return $this->pages[$index];
    }
   
    public function delete_page($index) {
      $this->assert_index($index);
      unset($this->pages[$index]);
    }
   
    public function set_page($index, $page) {
      $this->assert_index($index);
      $this->pages[$index] = $page;
    }
   
    public function add_page($page) {
      $this->pages[] = $page;
    }
   
    public function save($filename=false) {
      if ($filename !== false)
        $this->filename = $filename;
      if (!@file_put_contents($this->filename, $this->implode($this->pages)))
        die("Fatal: Couldn't save file to ".htmlspecialchars($this->filename));
    }
 
  }

  // Test:

  $pages = new PageStorage("seiten.txt");
  $pages->delete_page(1);
  $pages->set_page(0, "Test#0");
  $pages->add_page("Test#".microtime());
  var_dump($pages->get_pages());
  $pages->save();

?>
Mit freundlichen Grüßen,

Valle
Valentin Voigt
BOFH excuse #423: „It's not RFC-822 compliant.“
Mein total langweiliger Blog
  Mit Zitat antworten Zitat