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.MySQL.inc < prev    next >
Encoding:
Text File  |  2003-03-22  |  4.9 KB  |  210 lines

  1. <?php
  2. /////////////////////////////////////////////////////////
  3. //    
  4. //    include/data_manager.MySQL.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.         $DB_USERS_TABLE - Name of table cotaining users
  20.     POST-CONDITIONS:
  21.     COMMENTS:
  22.         For DB based backend, none of the data will be stored
  23.         in memory, and will be accessed through DB in real time.
  24.  
  25. ********************************************************/
  26.  
  27. include_once("../include/array2sql.inc");
  28.  
  29. class DataManager_obj{
  30.     var $user;
  31.     var $host;
  32.     var $table;        //directory for FS backend, table nmae for DB
  33.     var $dataID;        //file name for FS backend, user's ID for DB backend
  34.     var $data;            //actually contains
  35.     var $error;
  36.     var $db;
  37.     
  38.     function initialize($user, $host, $table, $backend){
  39.         global $DB_USERS_TABLE;
  40.         
  41.         if (empty($table)){
  42.             $this->error = "DB table name or ID is empty\n";
  43.             return false;
  44.         }
  45.         
  46.         $this->db = new idba_obj;
  47.         if (!$this->db->connect()) return false;
  48.         
  49.         $sql = "SELECT * FROM $DB_USERS_TABLE WHERE login='$user' and host='$host'";
  50.         $result = $this->db->query($sql);
  51.         if (($result) && ($this->db->num_rows($result)>0)){
  52.             $dataID = $this->db->result($result, 0, "id");
  53.         }else{
  54.             $this->error = $error;
  55.         }
  56.         
  57.         if (!$dataID){
  58.             $this->error.="User not found in database\n";
  59.             return false;
  60.         }
  61.         
  62.         $this->backend = $backend;
  63.         $this->table = $table;
  64.         $this->dataID = $dataID;
  65.         $this->data = array();
  66.         
  67.         return true;
  68.     }
  69.     
  70.     
  71.     function read(){
  72.         $data = array();
  73.         $sql = "SELECT * FROM ".$this->table." WHERE owner='".$this->dataID."'";
  74.         $result = $this->db->query($sql);
  75.         if (($result) && ($this->db->num_rows($result)>0)){
  76.             while ($a = $this->db->fetch_row($result)){
  77.                 $id = $a["id"];
  78.                 $data[$id] = $a;
  79.             }
  80.         }else{
  81.             $this->error .= $error;
  82.             return false;
  83.         }
  84.         
  85.         return $data;
  86.     }
  87.     
  88.     
  89.     function save(){
  90.         //everything's done in real time anyway
  91.         return true;
  92.     }
  93.     
  94.     
  95.     function delete($id){
  96.         $sql = "DELETE FROM ".$this->table;
  97.         $sql.= " WHERE id='".$id."' and owner='".$this->dataID."'";
  98.         return $this->db->query($sql);
  99.     }
  100.     
  101.     
  102.     function insert($array){
  103.         //get list of fields in table
  104.         $backend_fields = $this->db->list_fields($this->table);
  105.         if (!is_array($backend_fields)){
  106.             $this->error .= "Failed to fetch fields\n";
  107.             $this->error .= $error;
  108.             return false;
  109.         }
  110.         
  111.         //pick out relevant fields
  112.         $insert_data = array();
  113.         while ( list($k,$field) = each($backend_fields) ){
  114.             if (!empty($array[$field])){
  115.                 $insert_data[$field] = $array[$field];
  116.             }
  117.         }
  118.         if (empty($insert_data["owner"])) $insert_data["owner"] = $this->dataID;
  119.         
  120.         //$this->error .= "Inserting: ".implode("," $insert_data)."\n";
  121.         
  122.         //insert
  123.         $sql = Array2SQL($this->table, $insert_data, "INSERT");
  124.         $backend_result = $this->db->query($sql);
  125.                 
  126.         $this->error = $error;
  127.         
  128.         return $backend_result;
  129.     }
  130.     
  131.     function update($id, $array){
  132.         //get list of fields in table
  133.         $backend_fields = $this->db->list_fields($this->table);
  134.         if (!is_array($backend_fields)){
  135.             $this->error .= "Failed to fetch fields\n";
  136.             $this->error .= $error;
  137.             return false;
  138.         }
  139.         
  140.         //pick out relevant fields
  141.         $insert_data = array();
  142.         while ( list($k,$field) = each($backend_fields) ){
  143.             if (isset($array[$field]))
  144.                 $insert_data[$field] = $array[$field];
  145.         }
  146.         
  147.         //insert
  148.         $sql = Array2SQL($this->table, $insert_data, "UPDATE");
  149.         $sql.= " WHERE id='$id' and owner='".$this->dataID."'";
  150.         $this->db->query($sql);
  151.         
  152.         //echo "updating: $sql<br>\n";
  153.         $backend_result = $this->db->query($sql);
  154.         $this->error .= $this->db->error();
  155.         
  156.         return $backend_result;
  157.     }
  158.  
  159.  
  160.     function sort($field, $order){
  161.         $data = array();
  162.  
  163.         $backend_query = "SELECT * FROM ".$this->table;
  164.         $backend_query.=" WHERE owner='".$this->dataID."'";
  165.         $backend_query.=" ORDER BY $field $order";        
  166.         
  167.         $backend_result = $this->db->query($backend_query);
  168.         
  169.         if (($backend_result) && ($this->db->num_rows($backend_result)>0)){
  170.             while ($a = $this->db->fetch_row($backend_result)){
  171.                 $data[] = $a;
  172.             }
  173.         }else{
  174.             $this->error .= $this->db->error();
  175.             return false;
  176.         }
  177.         
  178.         return $data;
  179.     }
  180.  
  181.  
  182.     function getDistinct($field, $order){
  183.         $data = array();
  184.  
  185.         $backend_query = "SELECT distinct $field FROM ".$this->table;
  186.         $backend_query.=" WHERE owner='".$this->dataID."'";
  187.         $backend_query.=" ORDER BY $field $order";        
  188.         
  189.         $backend_result = $this->db->query($backend_query);
  190.         
  191.         if (($backend_result) && ($this->db->num_rows($backend_result)>0)){
  192.             while ($a = $this->db->fetch_row($backend_result)){
  193.                 $data[] = $a[$field];
  194.             }
  195.         }else{
  196.             $this->error .= $this->db->error();
  197.             return false;
  198.         }
  199.         
  200.         return $data;
  201.     }
  202.  
  203.  
  204.     function search($array){
  205.     }
  206.     
  207.     function showError(){
  208.         echo nl2br($this->error);
  209.     }
  210. }