Advertisement
andrewb

PHPDrive.php

Feb 22nd, 2015
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.55 KB | None | 0 0
  1. <?php
  2.  
  3. class PHPDrive {
  4.     // Class-wide variables
  5.     public $FILE;
  6.     public $CONN;
  7.     public $ERRMSG;
  8.     // The SQL used below
  9.     protected $SELECTALL = "SELECT * FROM fs WHERE FILE = '%s' ORDER BY ROWID DESC";
  10.     protected $SELECTONE = "SELECT * FROM fs WHERE FILE = '%s' ORDER BY ROWID DESC LIMIT 1";
  11.     protected $CREATE = "CREATE TABLE fs (FILE TEXT,DATA TEXT,CREATED DATETIME,UPDATED DATETIME)";
  12.     protected $INSERT = "INSERT INTO fs (FILE,DATA,CREATED,UPDATED) VALUES ('%s','%s',%s,%s)";
  13.     protected $DELETE = "DELETE FROM fs WHERE FILE = '%s'";
  14.    
  15.     function __construct($database=null) {
  16.         // Set the file
  17.         if ($database == null) {
  18.             $this->FILE = sprintf("%s.db", date("YmdHis"));
  19.         } else {
  20.             if (strtolower(substr($database, -3)) != ".db") {
  21.                 $database .= ".db";
  22.             }
  23.             $this->FILE = $database;
  24.         }
  25.         // Check for the file
  26.         if (is_file($this->FILE)) {
  27.             // Connect to the database
  28.             $this->OpenConn($this->FILE);
  29.         } else {
  30.             // Connect to the database
  31.             $this->OpenConn($this->FILE);
  32.             // Create the table
  33.             $this->WriteQuery($this->CREATE);
  34.         }
  35.     }
  36.    
  37.     function __destruct() {
  38.         if (isset($this->CONN)) { $this->CloseConn(); }
  39.     }
  40.    
  41.     protected function WriteQuery($sql=null) {
  42.         if (function_exists("sqlite_query")) {
  43.             $res = sqlite_query($this->CONN, $sql);
  44.             if ($res == false) {
  45.                 $this->ERRMSG = "Sqlite2 could not write data";
  46.                 return false;
  47.             }
  48.         } else {
  49.             $res = $this->CONN->exec($sql);
  50.             if ($res == false) {
  51.                 $this->ERRMSG = "Sqlite3 could not write data";
  52.                 return false;
  53.             }
  54.         }
  55.         return $res;
  56.     }
  57.    
  58.     protected function ReadQuery($sql=null) {
  59.         $rows = array();
  60.         if (function_exists("sqlite_query")) {
  61.             $res = sqlite_query($this->CONN, $sql, SQLITE_ASSOC, $this->ERRMSG);
  62.             if ($this->ERRMSG) { return false; }
  63.             if ($res == false) {
  64.                 $this->ERRMSG = "Sqlite2 query failed";
  65.                 return false;
  66.             }
  67.             if ($res->numRows() == 0) {
  68.                 $this->ERRMSG = "Sqlite2 returned no results";
  69.                 return false;
  70.             }
  71.             while($row = sqlite_fetch_array($res, SQLITE_ASSOC)) {
  72.                 $rows[] = $row;
  73.             }
  74.         } else {
  75.             $res = $this->CONN->query($sql);
  76.             if (is_object($res) == false) {
  77.                 $this->ERRMSG = "Sqlite3 found no results";
  78.                 return false;
  79.             }
  80.             while($row = $res->fetchArray()) {
  81.                 $rows[] = $row;
  82.             }
  83.             if (count($rows) == 0) {
  84.                 $this->ERRMSG = "Sqlite3 looped no results";
  85.                 return false;
  86.             }
  87.         }
  88.         return $rows;
  89.     }
  90.    
  91.     protected function OpenConn() {
  92.         if (function_exists("sqlite_open")) {
  93.             $this->CONN = sqlite_open($this->FILE, 0666);
  94.         } else {
  95.             $this->CONN = new Sqlite3($this->FILE, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
  96.         }
  97.     }
  98.    
  99.     protected function CloseConn() {
  100.         if (function_exists("sqlite_close")) {
  101.             sqlite_close($this->CONN);
  102.         } else {
  103.             $this->CONN->close();
  104.         }
  105.         $this->CONN = null;
  106.     }
  107.    
  108.     public function GetFile($name=null) {
  109.         if ($name == null) {
  110.             $this->ERRMSG = "No file name provided";
  111.             return false;
  112.         }
  113.         $rows = $this->ReadQuery(sprintf($this->SELECTALL, $name));
  114.         return $rows;
  115.     }
  116.    
  117.     public function GetLatestFile($name=null) {
  118.         if ($name == null) {
  119.             $this->ERRMSG = "No file name provided";
  120.             return false;
  121.         }
  122.         $rows = $this->ReadQuery(sprintf($this->SELECTONE, $name));
  123.         return $rows[0];
  124.     }
  125.    
  126.     public function CreateFile($name=null, $data=null) {
  127.         if ($name == null) {
  128.             $this->ERRMSG = "Missing file name to create a file";
  129.             return false;
  130.         }
  131.         if ($data == null) { $data = ""; }
  132.         $res = $this->WriteQuery(sprintf($this->INSERT,$name,$data,time(),time()));
  133.         if ($res == false) {
  134.             $this->ERRMSG = "Could not insert data";
  135.             return false;
  136.         }
  137.         return true;
  138.     }
  139.    
  140.     public function UpdateFile($name=null, $data=null) {
  141.         if ($name == null) {
  142.             $this->ERRMSG = "Missing file name to create a file";
  143.             return false;
  144.         }
  145.         if ($data == null) { $data = ""; }
  146.         $file = $this->GetLatestFile($name);
  147.         if (is_array($file) == false) {
  148.             return $this->CreateFile($name, $data);
  149.         }
  150.         $res = $this->WriteQuery(sprintf($this->INSERT,$name,$data,$file['CREATED'],time()));
  151.         if ($res == false) {
  152.             $this->ERRMSG = "Could not insert data";
  153.             return false;
  154.         }
  155.         return true;
  156.     }
  157.    
  158.     public function DeleteFile($name=null) {
  159.         if ($name == null) {
  160.             $this->ERRMSG = "Missing file name to delete a file";
  161.             return false;
  162.         }
  163.         $res = $this->WriteQuery(sprintf($this->DELETE, $name));
  164.         if ($res == false) {
  165.             $this->ERRMSG = "Could not delete file";
  166.             return false;
  167.         }
  168.         return true;
  169.     }
  170. }
  171.  
  172. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement