home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / admin / data_manager.FS.inc < prev    next >
Encoding:
Text File  |  2003-03-16  |  4.4 KB  |  199 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    include/data_manager.FS.inc
  5. //
  6. //    (C)Copyright 2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
  7. //
  8. //    This file is part of IlohaMail. IlohaMail is free software released 
  9. //    under the GPL license.  See enclosed file COPYING for details, or 
  10. //    see http://www.fsf.org/copyleft/gpl.html
  11. //
  12. /////////////////////////////////////////////////////////
  13.  
  14. /********************************************************
  15.  
  16.     PURPOSE:
  17.         Data handling abstraction class.
  18.     PRE-CONDITIONS:
  19.         $USER_DIR - Where user directories reside (e.g. "../users/")
  20.     POST-CONDITIONS:
  21.     COMMENTS:
  22.         For FS based backend, $data will contain all data.
  23.  
  24. ********************************************************/
  25.  
  26. include_once("../include/sort2darray.inc");
  27.  
  28. class DataManager_obj{
  29.     var $user;
  30.     var $host;
  31.     var $storage;        //directory for FS backend, table nmae for DB
  32.     var $location;        //file name for FS backend, user's ID for DB backend
  33.     var $path;
  34.     var $data;            //actually contains
  35.     var $error;
  36.     
  37.     function initialize($user, $host, $location, $backend){
  38.         global $USER_DIR;
  39.  
  40.         $this->error = "";        
  41.         
  42.         $storage = $USER_DIR;
  43.         $location = $location.".dat";
  44.         
  45.         if (!file_exists(realpath($storage))){
  46.             $this->error = "Folder: $storage does not exist\n";
  47.             return false;
  48.         }
  49.         
  50.         $storage = $storage.ereg_replace("[\\/]", "", $user.".".$host);
  51.         if (!file_exists(realpath($storage))){
  52.             $this->error = "Folder: $storage does not exist\n";
  53.             return false;
  54.         }
  55.         
  56.         $this->storage = $storage;
  57.         $this->location = $location;
  58.         $this->path = $storage."/".$location;
  59.         $this->user = $user;
  60.         $this->host = $host;
  61.         $this->data = false;
  62.         
  63.         return true;
  64.     }
  65.     
  66.     function read(){
  67.         $filePath = $this->path;
  68.         $this->data = false;
  69.         
  70.         $fp = fopen($filePath, "a");        //force create file
  71.         if ($fp) fclose($fp);
  72.  
  73.         $lines = file($filePath);
  74.  
  75.         if (is_array($lines)){
  76.             $i=1;
  77.             while ( list($key, $line) = each ($lines) ){
  78.                 $a = explode(",", chop($line));
  79.                 while ( list($k2, $data) = each($a) ){
  80.                     list($type, $string) = explode(":", $data);
  81.                     if ($type!="id") $string = base64_decode($string);
  82.                     //$string = base64_decode($string);
  83.                     $this->data[$i][$type] = $string;
  84.                 }
  85.                 $this->data[$i]["id"] = $i;
  86.                 $i++;
  87.             }
  88.         }else{
  89.             $this->error.= "Failed to read from: $filePath.\n";
  90.             $this->data = array();
  91.         }
  92.         
  93.         return $this->data;
  94.     }
  95.     
  96.     function save(){
  97.         $filePath = $this->path;
  98.  
  99.         $fp = fopen($filePath, "w+");
  100.         if ($fp){
  101.             reset($this->data);
  102.             $i=1;
  103.             while ( list($key, $foo) = each($this->data)){
  104.                 $line="id:".$i;
  105.                 $this->data[$key]["id"] = $i;
  106.                 while ( list($k2, $val) = each($this->data[$key])) 
  107.                     if ($k2!="id") $line .= ",".$k2.":".base64_encode($val);
  108.                 fputs($fp, $line."\n");
  109.                 $i++;
  110.             }
  111.             fclose($fp);
  112.             
  113.             return true;
  114.         }else{
  115.             $this->error = "Couldn't open file \"$filePath\" for writing\n";
  116.             return false;
  117.         }
  118.     }
  119.     
  120.     
  121.     function delete($id){
  122.         if (!$this->data) $this->read();
  123.         $result = array();
  124.         $deleted = false;
  125.         
  126.         reset($this->data);
  127.         while ( list($k,$v) = each($this->data) ){
  128.             if ($this->data[$k]["id"] != $id) $result[$k] = $this->data[$k];
  129.             else $deleted = true;
  130.         }
  131.         if ($deleted){
  132.             $this->data = $result;
  133.             $this->save();
  134.         }else{
  135.             $this->error = "Delete failed: $id not found\n";
  136.         }
  137.         
  138.         return $deleted;
  139.     }
  140.     
  141.     function update($id, $array){
  142.         if (!$this->data) $this->read();
  143.         $updated = false;
  144.         reset($this->data);
  145.         while ( list($k,$v) = each($this->data) ){
  146.             if ($this->data[$k]["id"]==$id){
  147.                 $this->data[$k] = $array;
  148.                 $updated = true;
  149.             }
  150.         }
  151.         if ($updated) $this->save();
  152.         else $this->error = "Update failed:  $id not found\n";
  153.         
  154.         return $updated;
  155.     }
  156.     
  157.     function insert($array){
  158.         if (!$this->data) $this->read();
  159.         $this->data[] = $array;
  160.         return $this->save();
  161.     }
  162.     
  163.     
  164.     function sort($field, $order){
  165.         if (!$this->data) $this->read();
  166.         $sort_a = explode(",", $field);
  167.         $num_fields = count($sort_a);
  168.         for ($i=$num_fields;$i>0;$i--){
  169.             $this->data = sort2darray($this->data, $sort_a[$i-1], $order);
  170.         }
  171.         return $this->data;
  172.     }
  173.     
  174.     
  175.     function getDistinct($field, $order){
  176.         $this->sort($field, $order);
  177.         
  178.         $index = array();
  179.         $result = array();
  180.         
  181.         while ( list($k,$v) = each($this->data) ){
  182.             $value = $this->data[$k][$field];
  183.             if (empty($index[$value])){
  184.                 $index[$value] = 1;
  185.                 $result[] = $value;
  186.             }
  187.         }
  188.         
  189.         return $result;
  190.     }
  191.     
  192.  
  193.     function search($array){
  194.     }
  195.     
  196.     function showError(){
  197.         echo nl2br($this->error);
  198.     }
  199. }