/**
* Utility classes to manage uploaded files
- *
+ *
* Copyright (c) 2011 Olivier Berger & Institut Telecom
*
* This program was developped in the frame of the COCLICO project
* (http://www.coclico-project.org/) with financial support of the Paris
* Region council.
*
- * This file is part of FusionForge.
- *
- * FusionForge 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; either version 2 of the License, or
- * (at your option) any later version.
+ * This file is part of FusionForge. FusionForge 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; either version 2 of the Licence, or (at your option)
+ * any later version.
*
* FusionForge 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 GForge; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * You should have received a copy of the GNU General Public License along
+ * with FusionForge; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// TODO : add cleanup mechanism for storage
/**
* Generic file storage management class
* The files are stored in an arbitrary dir on the server side
- *
+ *
* @author Olivier Berger
*
*/
class AbstractFilesDirectory extends Error {
-
+
protected $dir_path;
-
+
protected $html_generator;
-
+
protected static $finfo;
-
+
/**
* Initializes the directory permissions
*/
}
}
}
-
+
protected static function finfo() {
if (!isset(self::$finfo)) {
- self::$finfo = new finfo(FILEINFO_MIME, forge_get_config('libmagic_db', 'projectimport-plugin'));
+ self::$finfo = new finfo(FILEINFO_MIME, forge_get_config('libmagic_db', 'projectimport'));
}
return self::$finfo;
}
* @param string $storage_base path to the storage directory (if omitted, uses a temprary dir in /tmp)
*/
public function AbstractFilesDirectory($HTML, $storage_base=False) {
-
+
$this->html_generator = $HTML;
-
+
if (!isset(self::$finfo)) {
- self::$finfo = new finfo(FILEINFO_MIME, forge_get_config('libmagic_db', 'projectimport-plugin'));
+ self::$finfo = new finfo(FILEINFO_MIME, forge_get_config('libmagic_db', 'projectimport'));
}
-
+
if(! $storage_base) {
$storage_base = tempnam("/tmp", "ff-projectimport");
}
$this->dir_path = $storage_base;
$this->createStorage();
}
-
+
/**
* Move a file on the server (typically an uploaded one) into the storage dir
* @param string $filename
}
return $newpath;
}
-
+
/**
* Displays an HTML list of the directory contents
* @return string HTML
// maybe use scandir instead ?
if ($dh = opendir($this->dir_path)) {
$html.='<ul>';
-
+
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
$html.='<li>'.'filename: '. $file .': filetype: ' . filetype($this->dir_path . $file) . '</li>';
}
return $html;
}
-
+
public function getMimeType($filepath) {
$finfo = self::finfo();
if (!$finfo) {
}
$mimetype = $finfo->file($filepath);
$mimetype = strstr($mimetype, ';', true);
-
+
// try to identify OpenDocument Package Zip container
if ($mimetype == 'application/octet-stream') {
$zip = new ZipArchive;
$res = $zip->open($filepath);
if ($res === TRUE) {
- // if it has a mimetype file in the zip, then read its contents
+ // if it has a mimetype file in the zip, then read its contents
$contents = $zip->getFromName('mimetype');
if($contents) {
$mimetype = $contents;
}
- }
+ }
}
return $mimetype;
}
-
+
/**
* Returns an HTML box/table containing (single) file selection radio buttons
* @param string $preselected filename
* @return boolean|string
*/
public function displayFileSelectionForm($preselected = False) {
- $html = '';
+ $html = '';
+
+
-
-
if (is_dir($this->dir_path)) {
$contents = scandir($this->dir_path);
if(count($contents) > 2) {
$html .= '<th>'._('type').'</th>';
$html .= '<th>'._('selected ?').'</th>';
$html .= '</tr></thead><tbody>';
-
+
foreach($contents as $file) {
if ($file != '.' && $file != '..') {
$filepath = $this->dir_path . $file;
$html .= $this->html_generator->boxBottom();
}
}
-
+
return $html;
}
-
+
/**
* Returns the path given a SHA1 hash for a filename
* @param unknown_type $filesha1
public function getFilePath($filesha1) {
$filepath = False;
if (is_dir($this->dir_path)) {
- $contents = scandir($this->dir_path);
+ $contents = scandir($this->dir_path);
foreach($contents as $file) {
if ($filesha1 == sha1($file)) {
$filepath = $this->dir_path . $file;
/**
* Specialized file storage management class for site-level files
- *
- * Files are stored inside $storage_base/projectimport-plugin (for instance '$core/data_path/plugins/projectimport/)
- *
+ *
+ * Files are stored inside $projectimport/storage_base (for instance '$core/data_path/plugins/projectimport/)
+ *
* @author Olivier Berger
*
*/
class SiteAdminFilesDirectory extends AbstractFilesDirectory {
public function SiteAdminFilesDirectory($HTML) {
- $storage_base = forge_get_config('storage_base', 'projectimport-plugin');
+ $storage_base = forge_get_config('storage_base', 'projectimport');
parent::AbstractFilesDirectory($HTML, $storage_base);
-
+
}
}
/**
* Specialized file storage management class for project-level files
- *
- * Files are stored inside subdirs of $storage_base/projectimport-plugin (for instance '$core/data_path/plugins/projectimport/_projname_/)
- *
+ *
+ * Files are stored inside subdirs of $projectimport/storage_base (for instance '$core/data_path/plugins/projectimport/_projname_/)
+ *
* @author Olivier Berger
*
*/
class ProjectFilesDirectory extends AbstractFilesDirectory {
-
+
/**
* Constructor
* @param HTML generator $HTML
// store the project files inside a group unix name's subdir
$group = group_get_object($group_id);
- $storage_base = forge_get_config('storage_base', 'projectimport-plugin');
+ $storage_base = forge_get_config('storage_base', 'projectimport');
$storage_base .= '/'. $group->getUnixName().'/';
parent::AbstractFilesDirectory($HTML, $storage_base);
-
+
}
}
/**
* Utility HTML display class for pages containing a file upload and selection form
- *
+ *
* @author Olivier Berger
*
*/
* @var string
*/
protected $posted_uploadedfile;
-
+
protected $html_generator;
-
+
protected $message;
-
+
/**
* File storage
* @var AbstractFilesDirectory
*/
protected $storage;
-
+
/**
* Constructor
* @param HTML generator $HTML
$this->storage = $storage;
} else {
// otherwise create one with temporary directory
- $this->storage = new AbstractFilesDirectory($this->html_generator);
+ $this->storage = new AbstractFilesDirectory($this->html_generator);
}
$this->posted_selecteddumpfile = False;
$this->posted_uploadedfile = False;
if ($feedback) $feedback .= '<br />';
$feedback .= $message;
}
-
+
/**
* Parses the POSTed data to initialize the $posted_selecteddumpfile and $posted_uploadedfile and returns selected file name (if any)
* @return Ambigous <boolean, Ambigous, string>
*/
function initialize_chosenfile_from_submitted() {
-
+
$filechosen = FALSE;
-
+
$uploaded_file = getUploadedFile('uploaded_file');
//print_r($uploaded_file);
-
+
// process chosen file -> $filechosen set after this (or not)
if (getStringFromPost('submit_file')) {
$filesha1s = array();
}
}
if (count($filesha1s) > 1) {
-
+
$this->feedback(_('Please select only one file'));
} else {
if (count($filesha1s) == 1) {
}
}
}
-
+
// Process uploaded file : $this->posted_selecteddumpfile set afterwards (or not)
if($uploaded_file) {
// May use codendi's rules to check results of upload ?
- //$rule_file = new Rule_File();
+ //$rule_file = new Rule_File();
//if ($rule_file->isValid($uploaded_file)) {
if($uploaded_file['error'] == UPLOAD_ERR_OK ) {
if ($filechosen) {
$this->feedback(_('File upload stopped by extension'));
default:
$this->feedback(_('Unknown upload error %d', $error_code));
- }
+ }
}
}
}
if($filechosen) {
$this->posted_selecteddumpfile = $filechosen;
}
-
+
return $filechosen;
}
}