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 file containers handling class. * You can use this class to save files, contents, text, or everything you want on the server's * filesystem. * Your (binary safe) file content will be associated to a name and is only visible from your website. * cmsdAm uses hashing mechanisms to store your files, that can be viewed from your sections. * Files can be only modified by the same logged user, from the same server name and with the same lang * that created the file. This is to make sure that anyone modify files used by other and increase security * to your cmsdAm powered website. Statistics scripts (will be written soon) let you know about files not * used for a long period of time.

* Class instance: $cms->filecontainer * @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net> */ class cmsdam_filecontainer { /** * 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_filecontainer () { // Class constructor return true; } /** * Directory is writable... Internal use! * @access private * @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net> */ function _check_writable () { // Internal filesystem permission check if ((is_writable($this->conf->filecontainer_basedir)) && (is_dir($this->conf->filecontainer_basedir))) { return true; } else { return false; } } /** * Delete a file stored on the server filesystem * @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net> */ function delete_file($content_name) { $cmsdam_lang = $this->i18n->cmsdam_get_preferred_lang(); $key_to_save = md5($_SERVER['QUERY_STRING'].$cmsdam_lang.$this->users->logged_user.$content_name); $pattern3 = substr($key_to_save, 0, 3); $pattern2 = substr($key_to_save, 0, 2); $pattern1 = substr($key_to_save, 0, 1); $pattern = $pattern1 . "/" . $pattern2 . "/" . $pattern3; $filename = $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save; $this->logs->log_note(6, "NOTE: filecontainer: " . "Deleting content: \"$content_name\", realname: \"$key_to_save\", unlinking file \"" . $filename . "\""); if (!file_exists($filename)) { $this->logs->log_note(3, "WARNING: filecontainer: File " . $filename . " does not exists"); return false; } $errflag = @unlink($filename); if ($errflag == false) { $this->logs->log_note(3, "WARNING: filecontainer: Error unlinking file \"" . $filename . "\""); return false; } else { $this->logs->log_note(6, "NOTE: filecontainer: File \"" . $filename . "\" successfully deleted"); return true; } } /* * Add a form uploaded file into the server's filesystem. This is binary safe. * This way is faster than with add_file, because a move is faster than a copy. * @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net> */ function add_uploaded_file($content_name, $input_name) { $cmsdam_lang = $this->i18n->cmsdam_get_preferred_lang(); $key_to_save = md5($_SERVER['QUERY_STRING'].$cmsdam_lang.$this->users->logged_user.$content_name); if ($this->_check_writable() == true) { $this->logs->log_note(6, "NOTE: filecontainer: " . "Adding content: \"$content_name\", length: \"$content_length\", realname: \"$key_to_save\" to \"" . $this->conf->filecontainer_basedir . "\""); } else { $this->logs->log_note(6, "NOTE: filecontainer: " . "NOT Adding content: \"$content_name\", length: \"$content_length\", realname: \"$key_to_save\" to \"" . $this->conf->filecontainer_basedir . "\", permission denied or directory does not exist"); return false; } // Make the structured file tree (it's a robust, quick and scalable method to storage files!!) $pattern3 = substr($key_to_save, 0, 3); $pattern2 = substr($key_to_save, 0, 2); $pattern1 = substr($key_to_save, 0, 1); $pattern = $pattern1 . "/" . $pattern2 . "/" . $pattern3; if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1)) { $errflag = @mkdir ($this->conf->filecontainer_basedir . "/" . $pattern1, 0755); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to mkdir " . $this->conf->filecontainer_basedir . "/" . $pattern1); } } if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2)) { $errflag = @mkdir ($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2, 0755); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to mkdir " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2); } } if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3)) { $errflag = @mkdir ($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3, 0755); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to mkdir " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3); } } if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save)) { $errflag = touch ($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to write to file " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save); return false; } } else { $this->logs->log_note(3, "ERROR: filecontainer: File " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save . " already exists!"); return false; } $filename = $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save; // Let's make sure the file exists and is writable first. if (!is_writable($filename)) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to write to file $filename"); return false; } // Move the uploaded file to the right position if (move_uploaded_file($_FILES[$input_name]['tmp_name'], $filename)) { $this->logs->log_note(6, "NOTE: filecontainer: Success, wrote to file $filename"); return $filename; } else { $this->logs->log_note(6, "NOTE: filecontainer: Error moving uploaded file to $filename"); return false; } } /** * Add a file on the server's filesystem. $content_data is binary safe. * @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net> */ function add_file($content_name, $content_data) { // Binary safe string length $content_length = strlen($content_data); $cmsdam_lang = $this->i18n->cmsdam_get_preferred_lang(); $key_to_save = md5($_SERVER['QUERY_STRING'].$cmsdam_lang.$this->users->logged_user.$content_name); if ($this->_check_writable() == true) { $this->logs->log_note(6, "NOTE: filecontainer: " . "Adding content: \"$content_name\", length: \"$content_length\", realname: \"$key_to_save\" to \"" . $this->conf->filecontainer_basedir . "\""); } else { $this->logs->log_note(6, "NOTE: filecontainer: " . "NOT Adding content: \"$content_name\", length: \"$content_length\", realname: \"$key_to_save\" to \"" . $this->conf->filecontainer_basedir . "\", permission denied or directory does not exist"); return false; } // Make the structured file tree (it's a robust, quick and scalable method to storage files!!) $pattern3 = substr($key_to_save, 0, 3); $pattern2 = substr($key_to_save, 0, 2); $pattern1 = substr($key_to_save, 0, 1); $pattern = $pattern1 . "/" . $pattern2 . "/" . $pattern3; if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1)) { $errflag = @mkdir ($this->conf->filecontainer_basedir . "/" . $pattern1, 0755); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to mkdir " . $this->conf->filecontainer_basedir . "/" . $pattern1); } } if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2)) { $errflag = @mkdir ($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2, 0755); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to mkdir " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2); } } if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3)) { $errflag = @mkdir ($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3, 0755); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to mkdir " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3); } } if (!file_exists($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save)) { $errflag = touch ($this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save); if ($errflag == false) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to write to file " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save); return false; } } else { $this->logs->log_note(3, "ERROR: filecontainer: File " . $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save . " already exists!"); return false; } $filename = $this->conf->filecontainer_basedir . "/" . $pattern1 . "/" . $pattern2 . "/" . $pattern3 . "/" . $key_to_save; // Let's make sure the file exists and is writable first. if (!is_writable($filename)) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to write to file $filename"); return false; } if (!$handle = @fopen($filename, 'w')) { $this->logs->log_note(3, "ERROR: filecontainer: Unable to open file $filename"); return false; } if (@fwrite($handle, $content_data, $content_length) === FALSE) { $this->logs->log_note(3, "ERROR: filecontainer: Cannot write to file $filename"); return false; } $this->logs->log_note(6, "NOTE: filecontainer: Success, wrote to file $filename"); @fclose($handle); return $filename; } /** * Get the binary stream to the file stored on the server filesystem * @author dAm2K (Dino Ciuffetti) <dam2k@users.sourceforge.net> */ function get_file($filename) { $this->logs->log_note(6, "NOTE: filecontainer: " . "Getting filecontainer file: \"$filename\""); if (!file_exists($filename)) { $this->logs->log_note(3, "WARNING: filecontainer: File " . $filename . " does not exists"); return false; } if (!is_readable($filename)) { $this->logs->log_note(3, "WARNING: filecontainer: File " . $filename . " is not readable"); return false; } return file_get_contents($filename); } } ?>