Example #1
0
 protected function DoUpload()
 {
     $file = JRequest::getVar('b2jstdupload', NULL, 'files', 'array');
     if (!$this->Submitted || !$file || $file['error'] == UPLOAD_ERR_NO_FILE) {
         return true;
     }
     $upload_directory = JPATH_SITE . "/components/" . $GLOBALS["com_name"] . "/uploads/";
     if (!is_writable($upload_directory)) {
         $this->MessageBoard->Add(JText::_($GLOBALS["COM_NAME"] . '_ERR_DIR_NOT_WRITABLE'), B2JMessageBoard::error);
         return false;
     }
     if ($file['error']) {
         $this->MessageBoard->Add(JText::sprintf($GLOBALS["COM_NAME"] . '_ERR_UPLOAD', $file['error']), B2JMessageBoard::error);
         return false;
     }
     $size = $file['size'];
     if ($size == 0) {
         $this->MessageBoard->Add(JText::_($GLOBALS["COM_NAME"] . '_ERR_FILE_EMPTY'), B2JMessageBoard::error);
         return false;
     }
     $max_filesize = intval($this->Params->get("uploadmax_file_size", "0")) * KB;
     if ($size > $max_filesize) {
         $this->MessageBoard->Add(JText::_($GLOBALS["COM_NAME"] . '_ERR_FILE_TOO_LARGE'), B2JMessageBoard::error);
         return false;
     }
     $mimetype = new B2JMimeType();
     if (!$mimetype->Check($file['tmp_name'], $this->Params)) {
         $this->MessageBoard->Add(JText::_($GLOBALS["COM_NAME"] . '_ERR_MIME') . " [" . $mimetype->Mimetype . "]", B2JMessageBoard::error);
         return false;
     }
     jimport('joomla.filesystem.file');
     $filename = JFile::makeSafe($file['name']);
     $filename = uniqid() . "-" . $filename;
     $dest = $upload_directory . $filename;
     if (!JFile::upload($file['tmp_name'], $dest)) {
         return false;
     }
     $jsession =& JFactory::getSession();
     $b2jsession = new B2JSession($jsession->getId(), $this->Application->b2jcomid, $this->Application->b2jmoduleid, $this->Application->bid);
     // session_id, cid, mid
     $data = $b2jsession->Load('filelist');
     if ($data) {
         $filelist = explode("|", $data);
     } else {
         $filelist = array();
     }
     $filelist[] = $filename;
     $data = implode("|", $filelist);
     $b2jsession->Save($data, "filelist");
     return true;
 }
Example #2
0
 public function HandleUpload($uploadDirectory)
 {
     $this->DebugLog->Write("HandleUpload() started");
     if (!is_writable($uploadDirectory)) {
         $this->DebugLog->Write("Directory " . $uploadDirectory . " is not writable");
         return array('error' => JFactory::getLanguage()->_($GLOBALS["COM_NAME"] . '_ERR_DIR_NOT_WRITABLE'));
     }
     $this->DebugLog->Write("Directory " . $uploadDirectory . " is ok");
     $size = $this->get_file_size();
     if ($size == 0) {
         $this->DebugLog->Write("File size is 0");
         return array('error' => JFactory::getLanguage()->_($GLOBALS["COM_NAME"] . '_ERR_FILE_EMPTY'));
     }
     $this->DebugLog->Write("File size is > 0");
     $max = $this->Params->get("uploadmax_file_size", 0) * KB;
     if ($size > $max) {
         $this->DebugLog->Write("File size too large ({$size} > {$max})");
         return array('error' => JFactory::getLanguage()->_($GLOBALS["COM_NAME"] . '_ERR_FILE_TOO_LARGE'));
     }
     $this->DebugLog->Write("File size ({$size} / {$max}) is ok");
     $filename = preg_replace("/[^\\w\\.-_]/", "_", $this->get_file_name());
     $filename = uniqid() . "-" . $filename;
     $full_filename = $uploadDirectory . $filename;
     if (!$this->save_file($full_filename)) {
         $this->DebugLog->Write("Error saving file");
         return array('error' => JFactory::getLanguage()->_($GLOBALS["COM_NAME"] . '_ERR_SAVE_FILE'));
     }
     $this->DebugLog->Write("File saved");
     $mimetype = new B2JMimeType();
     if (!$mimetype->Check($full_filename, $this->Params)) {
         unlink($full_filename);
         $this->DebugLog->Write("File type [" . $mimetype->Mimetype . "] is not allowed. Allowed types are:" . PHP_EOL . print_r($mimetype->Allowed, true));
         return array('error' => JFactory::getLanguage()->_($GLOBALS["COM_NAME"] . '_ERR_MIME') . " [" . $mimetype->Mimetype . "]");
     }
     $this->DebugLog->Write("File type [" . $mimetype->Mimetype . "] is allowed");
     $b2jcomid = JFactory::getApplication()->input->get("b2jcomid", NULL);
     $b2jmoduleid = JFactory::getApplication()->input->get("b2jmoduleid", NULL);
     $owner = JFactory::getApplication()->input->get("owner", NULL);
     $id = JFactory::getApplication()->input->get("id", NULL);
     $bid = JFactory::getApplication()->input->get("bid", NULL);
     $jsession = JFactory::getSession();
     $b2jsession = new B2JSession($jsession->getId(), $b2jcomid, $b2jmoduleid, $bid);
     $data = $b2jsession->Load('filelist');
     if ($data) {
         $filelist = explode("|", $data);
     } else {
         $filelist = array();
     }
     $filelist[] = $filename;
     $data = implode("|", $filelist);
     $b2jsession->Save($data, "filelist");
     $this->Log->Write("File " . $filename . " uploaded succesful.");
     $this->DebugLog->Write("File uploaded succesful.");
     return array("success" => true);
 }