1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
<?php
/**
* 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 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 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 function createStorage() {
// initialize the group storage dir
if (!is_dir($this->dir_path)) {
mkdir($this->dir_path,0755);
} else {
if ( fileperms($this->dir_path) != 0x4755 ) {
chmod($this->dir_path,0755);
}
}
}
protected static function finfo() {
if (!isset(self::$finfo)) {
self::$finfo = new finfo(FILEINFO_MIME, forge_get_config('libmagic_db', 'projectimport'));
}
return self::$finfo;
}
/**
* Constructor
* @param HTML generator $HTML
* @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'));
}
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
* @param string $newfilename (optional) rename file on the fly
* @return boolean|string
*/
public function addFileMovingIt($filename, $newfilename=FALSE) {
if($newfilename) {
$newpath = $this->dir_path . $newfilename;
} else {
$newpath = $this->dir_path . basename($filename);
}
// do the move of the file
$ret = rename($filename, $newpath);
if (!$ret) {
$this->setError(sprintf(_('File %s cannot be moved to the storage location %s'), $filename, $newpath));
return false;
}
return $newpath;
}
/**
* Displays an HTML list of the directory contents
* @return string HTML
*/
public function displayContents() {
$html = '';
if (is_dir($this->dir_path)) {
// 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>';
}
}
closedir($dh);
$html.='</ul>';
}
}
return $html;
}
public function getMimeType($filepath) {
$finfo = self::finfo();
if (!$finfo) {
$this->setError(_('Opening fileinfo database failed'));
return false;
}
$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
$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 = '';
if (is_dir($this->dir_path)) {
$contents = scandir($this->dir_path);
if(count($contents) > 2) {
$html .= $this->html_generator->boxTop(_("Uploaded files available"));
$html .= '<table width="100%"><thead><tr>';
$html .= '<th>'._('name').'</th>';
$html .= '<th>'._('type').'</th>';
$html .= '<th>'._('selected ?').'</th>';
$html .= '</tr></thead><tbody>';
foreach($contents as $file) {
if ($file != '.' && $file != '..') {
$filepath = $this->dir_path . $file;
$filetype = filetype($filepath);
$mimetype = False;
if ($filetype == 'file') {
$mimetype = $this->getMimeType($filepath);
}
$html .= '<tr>';
if ($mimetype == 'application/x-planetforge-forge-export') {
$html .= '<td style="white-space: nowrap;"><tt><b>'. $file .'</b></tt></td>';
$mimetype = '<b>PlanetForge forge export container ('.$mimetype.')</b>';
}
else {
$html .= '<td style="white-space: nowrap;"><tt>'. $file .'</tt></td>';
}
if ($filetype == 'file') {
$html .= '<td style="white-space: nowrap;">' . $mimetype . '</td>';
$sha_filename = sha1($file);
if ($file == $preselected) {
$html .= '<td><input type="radio" name="file_'.$sha_filename.'" value="'.$sha_filename.'" checked="checked" /></td>';
} else {
$html .= '<td><input type="radio" $group = group_get_object($group_id);
name="file_'.$sha_filename.'" value="'.$sha_filename.'" /></td>';
}
}
else {
$html .= '<td style="white-space: nowrap;">' . $filetype . '</td>';
$html .= '<td style="white-space: nowrap;" />';
}
}
}
$html .= '<input type="hidden" name="submit_file" value="y" />';
$html .= '</tbody></table>';
$html .= $this->html_generator->boxBottom();
}
}
return $html;
}
/**
* Returns the path given a SHA1 hash for a filename
* @param unknown_type $filesha1
* @return Ambigous <boolean, string>
*/
public function getFilePath($filesha1) {
$filepath = False;
if (is_dir($this->dir_path)) {
$contents = scandir($this->dir_path);
foreach($contents as $file) {
if ($filesha1 == sha1($file)) {
$filepath = $this->dir_path . $file;
break;
}
}
}
return $filepath;
}
}
/**
* Specialized file storage management class for site-level files
*
* 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');
parent::AbstractFilesDirectory($HTML, $storage_base);
}
}
/**
* Specialized file storage management class for project-level files
*
* 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
* @param integer $group_id
*/
public function ProjectFilesDirectory($HTML, $group_id) {
// 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');
$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
*
*/
class FileManagerPage {
/**
* filename of the selected file POSTed
* @var string
*/
protected $posted_selecteddumpfile;
/**
* filename of the uploaded file POSTed
* @var string
*/
protected $posted_uploadedfile;
protected $html_generator;
protected $message;
/**
* File storage
* @var AbstractFilesDirectory
*/
protected $storage;
/**
* Constructor
* @param HTML generator $HTML
* @param AbstractFilesDirectory $storage (optional)
*/
function FileManagerPage($HTML, $storage=False) {
$this->html_generator = $HTML;
$this->message = '';
// If specialized storage provided, then use it
if($storage) {
$this->storage = $storage;
} else {
// otherwise create one with temporary directory
$this->storage = new AbstractFilesDirectory($this->html_generator);
}
$this->posted_selecteddumpfile = False;
$this->posted_uploadedfile = False;
}
/**
* Adds a $feedback message
* @param string $message
*/
function feedback($message) {
global $feedback;
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();
foreach (array_keys($_POST) as $key) {
if(!strncmp($key, 'file_', 5)) {
$filesha1 = substr($key, 5);
$filesha1s[] = $filesha1;
}
}
if (count($filesha1s) > 1) {
$this->feedback(_('Please select only one file'));
} else {
if (count($filesha1s) == 1) {
$filechosen = $this->storage->getFilePath($filesha1s[0]);
if(!$filechosen) {
$this->feedback(_('File not found on server'));
}
}
}
}
// 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();
//if ($rule_file->isValid($uploaded_file)) {
if($uploaded_file['error'] == UPLOAD_ERR_OK ) {
if ($filechosen) {
$this->feedback(_('Please either select existing file OR upload new file'));
$filechosen = False;
}
else {
$imported_file = $uploaded_file['tmp_name'];
$imported_file = $this->storage->addFileMovingIt($imported_file, $uploaded_file['name']);
if(! $imported_file) {
$this->feedback($this->storage->getErrorMessage());
}
else {
$this->posted_uploadedfile = $uploaded_file['name'];
$this->message .= sprintf(_('File ā%sā uploaded and pre-selected'),$this->posted_uploadedfile);
}
}
}
else {
$error_code = $uploaded_file['error'];
if ($error_code != UPLOAD_ERR_NO_FILE ) {
switch ($error_code) {
case UPLOAD_ERR_INI_SIZE:
$this->feedback(_('The uploaded file exceeds the upload_max_filesize directive in php.ini'));
case UPLOAD_ERR_FORM_SIZE:
$this->feedback(_('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'));
case UPLOAD_ERR_PARTIAL:
$this->feedback(_('The uploaded file was only partially uploaded.'));
/* case UPLOAD_ERR_NO_FILE:
return 'No file was uploaded';*/
case UPLOAD_ERR_NO_TMP_DIR:
$this->feedback(_('Missing a temporary folder.'));
case UPLOAD_ERR_CANT_WRITE:
$this->feedback(_('Failed to write file to disk.'));
case UPLOAD_ERR_EXTENSION:
$this->feedback(_('File upload stopped by extension.'));
default:
$this->feedback(_('Unknown upload error %d', $error_code));
}
}
}
}
if($filechosen) {
$this->posted_selecteddumpfile = $filechosen;
}
return $filechosen;
}
}
|