public function run() { // system name must be defined and valid if (!isset($this->application->parameters['file'])) { throw new ApplicationException('System name of the file is missing.', 400); } if (!ApplicationModel_File::validateSystemName($this->application->parameters['file'])) { throw new ApplicationException('System name of the file is invalid.', 400); } $systemName = $this->application->parameters['file']; // load file's information $file = new ApplicationModel_File($this->application); $file->setType(ApplicationModel_File::TYPE_SOURCE); $file->setSystemName($systemName); try { $file->load(); } catch (ApplicationModelException_File $e) { // file does not exist in the database, but if the file exists in the filesystem, then fill the model with default data $file->setTime(time()); $file->setDescription(''); try { $file->setExtension(pathinfo($file->getSystemName(), PATHINFO_EXTENSION)); $file->setName(basename($file->getSystemName(), '.' . $file->getExtension())); } catch (ApplicationModelException_File $e) { // we have got problems with file's name/extension $file->setExtension('txt'); $file->setName('untitled'); } // if the file does not exist both in db and in fs, this is a problem if (!is_file($file->getPath())) { throw new ApplicationException('File is not found.', 404); } } // load file's owner try { $fileUploader = $file->getUploader(); $owner = new ApplicationModel_User($this->application); $owner->setId(ApplicationModel_User::getIdForUuid($this->application, $fileUploader)); $owner->load(); } catch (ApplicationModelException_File $e) { // file has no defined owner $owner = null; } catch (ApplicationModelException_User $e) { // file has the owner, but it cannot be loaded throw new ApplicationException('Cannot load the owner of the file.', 500); } // render html $view = new ApplicationView($this->application, $this->application->path . '/views/file_source_view.php'); $view->url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $view->owner = $owner; $view->file = $file; $view->fileData = file_get_contents($file->getPath()); // choose the right color scheme $view->isDarkColorScheme = true; if (isset($this->application->parameters['light'])) { $view->isDarkColorScheme = false; } // display html $view->render(); }
public function run() { if (!isset($this->application->parameters['file'])) { throw new ApplicationException('File identifier is missing.', 400); } if (!ApplicationModel_File::validateSystemName($this->application->parameters['file'])) { throw new ApplicationException('System name of the file is invalid.', 400); } $systemName = $this->application->parameters['file']; $file = new ApplicationModel_File($this->application); $file->setType(ApplicationModel_File::TYPE_SOURCE); $file->setSystemName($systemName); try { $file->load(); } catch (ApplicationModelException_File $e) { $file->setType(ApplicationModel_File::TYPE_SOURCE); $file->setExtension(pathinfo($systemName, PATHINFO_EXTENSION)); try { $file->setName(basename($file->getSystemName(), '.' . pathinfo($file->getSystemName(), PATHINFO_EXTENSION))); } catch (ApplicationModelException_File $e) { // we have got problems with file's name $file->setName('untitled'); } if (!is_file($file->getPath())) { throw new ApplicationException('File is not found.', 404); } } $this->application->outputHeaders = array('Content-type: application/force-download', 'Content-Disposition: attachment; filename="' . $file->getName() . '.' . $file->getProgrammingLanguage() . '"', 'Content-Length: ' . filesize($file->getPath())); $this->application->outputContent = file_get_contents($file->getPath()); }
public function run() { // file id must be defined and valid if (!isset($this->application->parameters['file'])) { throw new ApplicationException('File identifier is missing.', 400); } $fileId = (int) $this->application->parameters['file']; if (!ApplicationModel_File::validateId($fileId)) { throw new ApplicationException('Id of the file is invalid.', 400); } // user must be authorized if (!isset($_SESSION['authorized_user_id'])) { $this->application->outputHeaders[] = 'HTTP/1.1 302 Found'; $this->application->outputHeaders[] = 'Location: /login.php'; $this->application->outputContent = ''; return; } // filename must be present and valid $name = ''; $nameBad = false; if (!isset($this->application->parameters['name'])) { $nameBad = true; } else { $name = $this->application->parameters['name']; if (!ApplicationModel_File::validateName($name)) { $nameBad = true; } } // description must be valid $description = ''; $descriptionBad = false; if (isset($this->application->parameters['description'])) { $description = $this->application->parameters['description']; if (!ApplicationModel_File::validateDescription($description)) { $descriptionBad = true; } } // load user's information $user = new ApplicationModel_User($this->application); try { $user->setId($_SESSION['authorized_user_id']); $user->load(); } catch (ApplicationModelException_User $e) { throw new ApplicationException('Cannot load user.', 500); } // load file's information $file = new ApplicationModel_File($this->application); try { $file->setId($fileId); $file->load(); } catch (ApplicationModelException_File $e) { throw new ApplicationException('File is not found.', 404); } // ignore extension for images if ($file->getType() == ApplicationModel_File::TYPE_IMAGE) { $extension = $file->getExtension(); $extensionBad = false; } else { $extension = ''; $extensionBad = false; if (!isset($this->application->parameters['extension'])) { $extensionBad = true; } else { $extension = $this->application->parameters['extension']; if (!$file->validateExtension($extension)) { $extensionBad = true; } } } // load file owner's information try { $owner = new ApplicationModel_User($this->application); $owner->setId(ApplicationModel_User::getIdForUuid($this->application, $file->getUploader())); $owner->load(); } catch (ApplicationModelException_User $e) { throw new ApplicationException('Cannot load file\'s owner.', 500); } // authorized user must be the owner of the file if ($user->getId() != $owner->getId()) { throw new ApplicationException('Cannot edit file which belongs to a different user.', 403); } // check if everything is fine $success = !$nameBad && !$extensionBad && !$descriptionBad; // edit the file if ($success) { try { $file->setName($name); $file->setExtension($extension); $file->setDescription($description); $file->save(); } catch (ApplicationModelException_File $e) { throw new ApplicationException('Cannot save file\'s information.', 500); } // redirect user back to his account $this->application->outputHeaders[] = 'HTTP/1.1 302 Found'; $this->application->outputHeaders[] = 'Location: /account.php'; $this->application->outputContent = ''; return; } // render the html with errors $view = new ApplicationView($this->application, $this->application->path . '/views/file_edit_handler.php'); $view->success = $success; $view->filenameBad = $nameBad; $view->extensionBad = $extensionBad; $view->descriptionBad = $descriptionBad; $view->user = $user; $view->file = $file; $view->name = $name; $view->extension = $extension; $view->description = $description; $view->render(); }