/* Enjoy!!! Dino Ciuffetti - dam2k@users.sourceforge.net
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program (COPYING); if not, go to http://www.fsf.org/ or write
* to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/**
* This is the cmsdAm db containers handling class.
* You can use this class to save files, contents, text, or everything you want on the database.
* Your (binary safe) file content will be associated to a name.
* Class instance: $cms->dbcontainer
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
class cmsdam_dbcontainer
{
/**
* You should not use this variable from your section.
*/
var $dbms;
/**
* You should not use this variable from your section
* @access private
*/
var $conf;
/**
* You should not use this variable from your section
* @access private
*/
var $logs;
/**
* You should not use this variable from your section
* @access private
*/
var $i18n;
/**
* You should not use this variable from your section
* @access private
*/
var $users;
/**
* Class constructor
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function cmsdam_dbcontainer () { // Class constructor
return true;
}
/**
* Return an array (ordered) with all the directories
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function list_directories($like="") {
// Get the ordered directory list
$query = "SELECT file_name FROM " . $this->dbms->DB_tbl_prefix . "dbcontainer where type = 1 ";
if ($like != "") { // User want to refine the search
$query .= " and file_name like \"" . $like . "%\" ";
}
$query .= " order by file_name";
//echo "DEBUG: " . $query;
if ($this->dbms->dbquery($query) == true) { // Query OK
$r = $this->dbms->dbnum_rows();
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer list_directories(): Directories listed: $r");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer list_directories(): Directories listed: $r");
$rs = "";
for ($i=0; $i<$r; $i++) { // Get each directory name
$row = $this->dbms->dbfetch_row();
$rs[] = $row[0];
}
return $rs;
} else {
return false;
}
}
/**
* Create a new directory object (for cmsdAm hackers it is an insert into the database with field type = 1)
* Return false on errors or if the directory already exist.
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function make_directory($dir_name) {
if ($dir_name == "") {
return false;
}
// Verify if the directory already exist
$query = "SELECT file_name from " . $this->dbms->DB_tbl_prefix . "dbcontainer where file_name = \"" . $dir_name . "\" limit 1";
if ($this->dbms->dbquery($query) == true) { // Query OK
if ($this->dbms->dbnum_rows() > 0) { // Directory already exist
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer make_directory(): Directory \"$dir_name\" already exist");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer make_directory(): Directory \"$dir_name\" already exist");
return false;
}
} else {
return false;
}
$query = "INSERT INTO " . $this->dbms->DB_tbl_prefix . "dbcontainer (id, file_name, key_name, key_value, mod_date, owner, type) VALUES (NULL, \"" . $dir_name . "\", \"DIRECTORY\", \"DIRECTORY\", NOW(), \"" . $this->users->logged_user . "\", 1);";
if ($this->dbms->dbquery($query) == true) { // Query OK
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer make_directory(): Directory \"$dir_name\" created");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer make_directory(): Directory \"$dir_name\" created");
return true;
} else {
return false;
}
}
/**
* Remove a directory and all of its contents (any files attached)
* Return false on errors or true on success
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function remove_directory($dir_name) {
if ($dir_name == "") {
return false;
}
$query = "DELETE FROM " . $this->dbms->DB_tbl_prefix . "dbcontainer WHERE file_name = \"" . $dir_name . "\"";
if ($this->dbms->dbquery($query) == true) { // Query OK
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer remove_directory(): Directory \"$dir_name\" removed");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer remove_directory(): Directory \"$dir_name\" removed");
return true;
} else {
return false;
}
}
/**
* Return an array (ordered) with all the file names in the given directory
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function list_files($dir_name) {
if ($dir_name == "") {
return false;
}
// Get the ordered file list for the given directory
// select distinct key_name from cmsdam_dbcontainer where file_name = "testdir2" and type = 0;
$query = "SELECT DISTINCT key_name from " . $this->dbms->DB_tbl_prefix . "dbcontainer where file_name = \"" . $dir_name . "\" and type = 0 order by key_name";
if ($this->dbms->dbquery($query) == true) { // Query OK
$r = $this->dbms->dbnum_rows();
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer list_files(): Files listed for the dir \"$dir_name\": $r");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer list_files(): Files listed for the dir \"$dir_name\": $r");
$rs = "";
for ($i=0; $i<$r; $i++) { // Get each file name
$row = $this->dbms->dbfetch_row();
$rs[] = $row[0];
}
return $rs;
} else {
return false;
}
}
/**
* Count the number of files in the given directory
*/
/*
function count_files() { //TODO: Scrivi questa
}*/
/**
* Create a content (file) object and insert it into a folder (that must exist).
* (for cmsdAm hackers it is an insert into the database with field type = 0).
* It is not possible to have two content (files) with the same name into the same folder.
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function add_file($dir_name, $file_name, $file_data, $content_type = 0) {
// content_type field is reserved for the future.
if (($dir_name == "") || ($file_name == "")) {
return false;
}
// Verify if the directory exist
$query = "SELECT file_name from " . $this->dbms->DB_tbl_prefix . "dbcontainer where file_name = \"" . $dir_name . "\" limit 1";
if ($this->dbms->dbquery($query) == true) { // Query OK
if ($this->dbms->dbnum_rows() == 0) { // Directory does not exist
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer add_file(): Directory \"$dir_name\" does not exist");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer add_file(): Directory \"$dir_name\" does not exist");
return false;
}
} else {
return false;
} // Directory exist
// Verify if the filename exist
// select key_name from cmsdam_dbcontainer where file_name = "testdir" and key_name = "testfile" limit 1;
$query = "SELECT key_name from " . $this->dbms->DB_tbl_prefix . "dbcontainer where file_name = \"" . $dir_name . "\" and key_name = \"" . $file_name . "\" limit 1";
if ($this->dbms->dbquery($query) == true) { // Query OK
if ($this->dbms->dbnum_rows() > 0) { // Filename does exist
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer add_file(): File \"$file_name\" in dir \"$dir_name\" alreadyexist");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer add_file(): File \"$file_name\" in dir \"$dir_name\" alreadyexist");
return false;
}
} else {
return false;
} // Filename not exist
// Create the file $file_name into the directory $dir_name
$query = "INSERT INTO " . $this->dbms->DB_tbl_prefix . "dbcontainer (id, file_name, key_name, key_value, mod_date, owner, type) VALUES (NULL, \"" . $dir_name ."\", \"" . $file_name . "\", \"" . $file_data ."\", NOW(), \"" . $this->users->logged_user . "\", 0)";
if ($this->dbms->dbquery($query) == true) { // Query OK
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer add_file(): Dir: \"$dir_name\", File: \"$file_name\" created");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer add_file(): Dir: \"$dir_name\", File: \"$file_name\" created");
return true;
} else {
return false;
}
}
/**
* Remove a file from the given directory. If the filename is empty, all files in the given directory will be deleted
* and the directory will be let empty.
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function remove_file($dir_name, $file_name = "") {
if ($dir_name == "") {
return false;
}
// Delete the given file from the given directory
// delete from cmsdam_dbcontainer where file_name = "testdir3" and type = 0 and key_name = "testfile";
$query = "DELETE FROM " . $this->dbms->DB_tbl_prefix . "dbcontainer where file_name = \"" . $dir_name . "\" and type = 0";
if ($file_name == "") { // Delete all files into the given dir
$info = "All files into the directory $dir_name removed";
} else { // Delete the given file into the given dir
$query .= " and key_name = \"" . $file_name . "\"";
$info = "File \"$file_name\" into the directory $dir_name removed";
}
if ($this->dbms->dbquery($query) == true) { // Query OK
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer remove_file(): $info");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer remove_file(): $info");
return true;
} else {
return false;
}
}
/**
* Return an array (creation time ordered) with the last $last dir matching the "like" in $match.
* If $last == 0 and $match == "", an array with the last created directory will be returned
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function last_directories($last=0, $match="") {
// Get the ordered directory list
// select file_name from pdata_dbcontainer where type = 1 and file_name like "mini_sites_personalfrimm/%" order by mod_date desc limit 5;
$query = "SELECT file_name FROM " . $this->dbms->DB_tbl_prefix . "dbcontainer where type = 1 and file_name like \"" . $match . "%\" order by mod_date desc ";
if ($last > 0) {
$query .= " limit " . $last;
}
if ($this->dbms->dbquery($query) == true) { // Query OK
$r = $this->dbms->dbnum_rows();
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer list_directories(): Directories listed: $r");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer list_directories(): Directories listed: $r");
$rs = "";
for ($i=0; $i<$r; $i++) { // Get each directory name
$row = $this->dbms->dbfetch_row();
$rs[] = $row[0];
}
return $rs;
} else {
return false;
}
}
/**
* Get the value of the given filename for the given directory
* @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net>
*/
function get_file_value($dir_name, $file_name) {
if (($dir_name == "") || (file_name == "")) {
return false;
}
// Get the value of the file
// select key_value from pdata_dbcontainer where type=0 and file_name = "mini_sites_personalfrimm/04100.personalfrimm.com" and key_name = "fqdn";
$query = "SELECT distinct key_value from " . $this->dbms->DB_tbl_prefix . "dbcontainer where type = 0 and file_name = \"" . $dir_name . "\" and key_name = \"" . $file_name . "\"";
if ($this->dbms->dbquery($query) == true) { // Query OK
$r = $this->dbms->dbnum_rows();
if ($r == 0) { // File not found
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer get_file_value(): File \"$dir_name/$file_name\" was not found");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer get_file_value(): File \"$dir_name/$file_name\" was not found");
return false;
}
$this->logs->log_note(7, "NOTE: cmsdam_dbcontainer get_file_value(): Get value for \"$dir_name/$file_name\"");
$this->debug->debug_write(7, "NOTE: cmsdam_dbcontainer get_file_value(): Get value for \"$dir_name/$file_name\"");
$row = $this->dbms->dbfetch_row();
return $row[0];
} else {
return false;
}
}
}
?>