[Openupload-svn-update] SF.net SVN: openupload:[45] trunk/lib/modules/db/txt.inc.php
Status: Beta
Brought to you by:
tsdogs
|
From: <ts...@us...> - 2008-10-21 18:11:56
|
Revision: 45
http://openupload.svn.sourceforge.net/openupload/?rev=45&view=rev
Author: tsdogs
Date: 2008-10-21 18:11:46 +0000 (Tue, 21 Oct 2008)
Log Message:
-----------
Implement basic txtdb database api. It works but misses features.
Modified Paths:
--------------
trunk/lib/modules/db/txt.inc.php
Modified: trunk/lib/modules/db/txt.inc.php
===================================================================
--- trunk/lib/modules/db/txt.inc.php 2008-10-21 18:11:02 UTC (rev 44)
+++ trunk/lib/modules/db/txt.inc.php 2008-10-21 18:11:46 UTC (rev 45)
@@ -8,27 +8,101 @@
var $prefix;
function txtDB() {
+ $this->tables = array (
+ "files" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "name", "mime", "description", "size", "remove", "user_id", "ip", "upload_date",
+ )
+
+ ),
+ "users" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "login", "password", "name", "group_id", "email", "lang", "active",
+ ),
+ "auto_increment" => "id",
+ ),
+ "groups" => array (
+ "type" => "file",
+ "fields" => array (
+ "name", "description",
+ ),
+ ),
+ "file_options" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "file_id", "module", "name", "value",
+ ),
+ "auto_increment" => "id",
+ ),
+ "acl" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "module", "action", "group_id", "access",
+ ),
+ "auto_increment" => "id",
+ ),
+ "langs" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "name", "locale", "browser", "charset", "active",
+ ),
+ ),
+ "plugin_acl" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "group_id", "plugin", "access",
+ ),
+ "auto_increment" => "id",
+ ),
+ "plugin_options" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "plugin", "group_id", "option", "value",
+ ),
+ "auto_increment" => "id",
+ ),
+ "banned" => array (
+ "type" => "file",
+ "fields" => array (
+ "id", "ip", "access", "priority",
+ ),
+ "auto_increment" => "id",
+ ),
+ );
}
function readTxt ($file) {
- $x = file_get_contents($file);
- /* split the lines */
+ $x = file($file);
+ /* first line is the structure */
+ $fields = explode('|',chop($x[0]));
$result = array();
- $lines = split("\n",$x);
- foreach ($lines as $l) {
- $r = split("\|",$l,2);
- if (count($r)==2) {
- $result[$r[0]]=$r[1];
+ for ($i=1; $i<count($x); $i++) {
+ $r = explode('|',chop($x[$i]));
+ foreach ($fields as $k => $f) {
+ $row[$f] = $r[$k];
}
+ $result[]=$row;
}
return $result;
}
- function writeTxt($file,$lines) {
+ function writeTxt($file,$rows,$fields) {
$x = '';
- foreach ($lines as $k => $l) {
- $x .= $k.'|'.$l."\n";
+ foreach ($fields as $f) {
+ if ($x!='') $x .= '|';
+ $x .= $f;
}
+ $x .= "\n";
+ foreach ($rows as $row) {
+ $r = '';
+ foreach ($fields as $f) {
+ if ($r!='') $r .= '|';
+ $r .= $row[$f];
+ }
+ $x .= $r."\n";
+ }
file_put_contents($file,$x);
}
@@ -39,79 +113,184 @@
if (!is_dir($this->baseDir)) {
die(tr('ERROR: database folder not found!'));
}
- $dirs = array('users','files','groups');
- foreach ($dirs as $d) {
- if (!is_dir($this->baseDir.'/'.$this->prefix.$d)) {
- if (!mkdir($this->baseDir.'/'.$this->prefix.$d)) {
- die(tr('ERROR: Could not create folder for %1 table!',$d));
+ foreach ($this->tables as $n => $d) {
+ if ($d['type'] == "dir") {
+ if (!is_dir($this->baseDir.'/'.$this->prefix.$n)) {
+ if (!mkdir($this->baseDir.'/'.$this->prefix.$n)) {
+ die(tr('ERROR: Could not create folder for %1 table!',$n));
+ }
}
+ } else if ($d['type'] == "file") {
+ if (!file_exists($this->baseDir.'/'.$this->prefix.$n.'.txt')) {
+ $str = '';
+ foreach ($d['fields'] as $f) {
+ if ($str != '') $str .= '|';
+ $str .= $f;
+ }
+ if (!file_put_contents($this->baseDir.'/'.$this->prefix.$n.'.txt',$str."\n")) {
+ die(tr('ERROR: Could not create file for %1 table!',$n));
+ }
+ }
}
}
}
- function queryUser($login) {
- $file = $this->baseDir.'/'.$this->prefix.'users/'.$login.'.txt';
- if (!file_exists($file)) {
- return array();
- } else {
- $result = $this->readTxt($file);
- if ($result['active']==1)
- return $result;
- else
- return array();
- }
+ function newId($tbl,$field = 'id',$keys = array ()) {
+ return 1;
}
- function addUser($user) {
- /* should compute a real new id */
- $id = randomName(1,20);
- $user['id'] = $id;
- $file = $this->baseDir.'/'.$this->prefix.'users/'.$user['login'].'.txt';
- $this->writeTxt($file,$user);
+ function newRandomId($tbl,$field = 'id') {
+ $found = true;
+ $rows = $this->readTxt($this->baseDir.'/'.$tbl.'.txt');
+ while ($found) {
+ $id = randomName(30,30);
+ $found = false;
+ foreach ($rows as $row) {
+ /* should check for the exsistence */
+ if ($row[$field]==$id) {
+ $found = true;
+ }
+ }
+ }
+ return $id;
}
- function updateUser($user) {
- $file = $this->baseDir.'/'.$this->prefix.'users/'.$user['login'].'.txt';
- $this->writeTxt($file,$user);
+ function count($tbl,$keys = array()) {
+// TODO: check for keys
+ if ($this->tables[$tbl]['type'] == "dir") {
+ $a = scandir($this->baseDir.'/'.$this->prefix.$tbl);
+ return count($a)-2; /* remove . and .. */
+ } else {
+ $a = file($this->baseDir.'/'.$this->prefix.$tbl.'.txt');
+ return count($a)-1; /* remove the first line */
+ }
}
- function deleteUser($user) {
- $file = $this->baseDir.'/'.$this->prefix.'users/'.$user['login'].'.txt';
- unlink($file);
- /* probably should remove all the files uploaded too */
+ function read($tbl,$keys = array(), $sort = array(), $limit = '', $assoc = array()) {
+ $file = $this->baseDir.'/'.$this->prefix.$tbl;
+ if ($this->tables[$tbl]['type']=='dir') {
+
+ } else {
+ $result = array();
+ $rows = $this->readTxt($file.'.txt');
+ foreach ($rows as $row) {
+ $add = true;
+ if (count($keys)>0) {
+ foreach ($keys as $n => $k) {
+ if ($row[$n]!=$k) {
+ $add = false;
+ break;
+ }
+ }
+ }
+ if ($add) {
+ if (count($assoc)) { /* maybe there is a better way to do this? */
+ $str = '$result';
+ foreach ($assoc as $k) {
+ $str .= '[\''.$row[$k].'\']';
+ }
+ $str .= '=$row;';
+ eval($str);
+ } else {
+ $result[] = $row;
+ }
+ }
+ }
+/* need to add the limit and the sorting */
+ }
+ return $result;
}
- function getFileInfo($id) {
- $file = $this->baseDir.'/'.$this->prefix.'files/'.$id.'.txt';
- $finfo = $this->readTxt($file);
- return $finfo;
+ function insert($tbl,$values,$fields = array()) {
+ $file = $this->baseDir.'/'.$tbl;
+ if ($this->tables[$tbl]['type']=='dir') {
+ } else {
+ $rows = $this->readTxt($file.'.txt');
+ $row = array();
+ if ($this->tables[$tbl]['auto_increment']!='') {
+ if (count($rows)>0)
+ $row[$this->tables[$tbl]['auto_increment']]=$rows[count($rows)-1][$this->tables[$tbl]['auto_increment']]+1;
+ else
+ $row[$this->tables[$tbl]['auto_increment']]=1;
+ }
+ if (count($fields)>0) {
+ /* init the values */
+ foreach ($this->tables[$tbl]['fields'] as $f) {
+ if ($this->tables[$tbl]['auto_increment']!=$f)
+ $row[$f]='';
+ }
+ foreach ($fields as $f) {
+ if ($this->tables[$tbl]['auto_increment']!=$f)
+ $row[$f]=$values[$f];
+ }
+ $rows[]=$row;
+ } else {
+ foreach ($this->tables[$tbl]['fields'] as $f) {
+ if ($this->tables[$tbl]['auto_increment']!=$f)
+ $row[$f]=$values[$f];
+ }
+ $rows[]=$row;
+ }
+ $this->writeTxt($file.'.txt',$rows,$this->tables[$tbl]['fields']);
+ }
}
- function newFileId($field = 'id') {
- $found = true;
- /* let's hope we never get the duplicate on 2 or more users uploading at the same time */
- while ($found) {
- $id = randomName(30,30);
- $found = file_exists($this->baseDir.'/'.$this->prefix.'files/'.$id.'.txt');
- }
- return $id;
+ function update($tbl,$values,$keys = array(),$fields = array()) {
+ $file = $this->baseDir.'/'.$tbl;
+ if ($this->tables[$tbl]['type']=='dir') {
+ } else {
+ $rows = $this->readTxt($file.'.txt');
+ foreach ($rows as $i => $r) {
+ $update = true;
+ if (count($keys)>0) {
+ foreach ($keys as $k => $v) {
+ if ($r[$k]!=$v) {
+ $update = false;
+ break;
+ }
+ }
+ }
+ if ($update) {
+ $row = $r;
+ if (count($fields)>0) {
+ foreach ($fields as $f) {
+ $row[$f]=$values[$f];
+ }
+ } else {
+ foreach ($this->tables[$tbl]['fields'] as $f) {
+ $row[$f]=$values[$f];
+ }
+ }
+ $rows[$i]=$row;
+ }
+ }
+ $this->writeTxt($file.'.txt',$rows,$this->tables[$tbl]['fields']);
+ }
}
- function addFile(&$finfo) {
- $finfo['user_id']=$_SESSION['user']['login'];
- $finfo['upload_date']=date('Y-m-d h:i:s');
- $file = $this->baseDir.'/'.$this->prefix.'files/'.$finfo['id'].'.txt';
- $this->writeTxt($file,$finfo);
- return $finfo['id'];
+ function delete($tbl,$keys = array()) {
+ $file = $this->baseDir.'/'.$tbl;
+ if ($this->tables[$tbl]['type']=='dir') {
+ } else {
+ $rows = $this->readTxt($file.'.txt');
+ foreach ($rows as $i => $r) {
+ $delete = true;
+ if (count($keys)>0) {
+ foreach ($keys as $k => $v) {
+ if ($r[$k]!=$v) {
+ $delete = false;
+ break;
+ }
+ }
+ }
+ if ($delete) {
+ unset($rows[$i]);
+ }
+ }
+ $this->writeTxt($file.'.txt',$rows,$this->tables[$tbl]['fields']);
+ }
}
- function addFileOption($finfo,$module,$field) {
- /* this has been saved into the file for now, if needed will split it */
- }
- function removeFile($id) {
- unlink($this->baseDir.'/'.$this->prefix.'files/'.$id.'.txt');
- }
-
}
?>
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|