0 && (int)$file['size'] > $maxSize)) { $err = 'COM_VISFORMS_ERROR_WARNFILETOOLARGE'; return false; } $images = explode(',', "bmp,gif,jpg,jpeg,png"); if (in_array($format, $images)) { // if it's an image run it through getimagesize // if tmp_name is empty, then the file was bigger than the PHP limit if (!empty($file['tmp_name'])) { if (($imginfo = getimagesize($file['tmp_name'])) === FALSE) { $err = 'COM_VISFORMS_ERROR_WARNINVALID_IMG'; return false; } } else { $err = 'COM_VISFORMS_ERROR_WARNFILETOOLARGE'; return false; } } $xss_check = file_get_contents($file['tmp_name'], false, null, -1, 256); $html_tags = array('abbr', 'acronym', 'address', 'applet', 'area', 'audioscope', 'base', 'basefont', 'bdo', 'bgsound', 'big', 'blackface', 'blink', 'blockquote', 'body', 'bq', 'br', 'button', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'comment', 'custom', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'fn', 'font', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'hr', 'html', 'iframe', 'ilayer', 'img', 'input', 'ins', 'isindex', 'keygen', 'kbd', 'label', 'layer', 'legend', 'li', 'limittext', 'link', 'listing', 'map', 'marquee', 'menu', 'meta', 'multicol', 'nobr', 'noembed', 'noframes', 'noscript', 'nosmartquotes', 'object', 'ol', 'optgroup', 'option', 'param', 'plaintext', 'pre', 'rt', 'ruby', 's', 'samp', 'script', 'select', 'server', 'shadow', 'sidebar', 'small', 'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'title', 'tr', 'tt', 'ul', 'var', 'wbr', 'xml', 'xmp', '!DOCTYPE', '!--'); foreach ($html_tags as $tag) { // A tag is '' if (stristr($xss_check, '<' . $tag . ' ') || stristr($xss_check, '<' . $tag . '>')) { $err = 'COM_VISFORMS_ERROR_WARNIEXSS'; return false; } } return true; } // throws RuntimeException with error message public static function uploadFiles($visform, $client = 'site') { $input = Factory::getApplication()->getInput(); $files = $input->files->get('jform', array(), 'array'); $savemode = (empty($visform->savemode)) ? 'cmd' : 'raw'; // upload files $n = count($visform->fields); for ($i = 0; $i < $n; $i++) { $field = $visform->fields[$i]; // Request has an fileupload with values if ((($client == 'site') && ($field->typefield == 'file') && (isset($_FILES[$field->name]['name'])) && ($_FILES[$field->name]['name'] != '')) || (($client == 'admin') && ($field->typefield == 'file') && (isset($_FILES['jform']['name'][$field->name])) && ($_FILES['jform']['name'][$field->name] != ''))) { // only upload if field is not disabled if (empty($field->isDisabled)) { $file = ($client == 'site') ? $input->files->get($field->name, '', $savemode) : $files[$field->name]; $allowedextensions = (!empty($field->allowedextensions)) ? $field->allowedextensions : $visform->allowedextensions; // minimum from php ini and Visforms form settings $maxfilesize = self::getMaxFileSizeInByte($field->maxfilesize); if (empty($file)) { // InvalidArgumentException will stop the submit process throw new \InvalidArgumentException(Text::_('COM_VISFORMS_FILE_NOT_SAFE')); } $folder = (!empty($visform->uploadpath)) ? $visform->uploadpath : ''; try { $path = Path::clean(JPATH_ROOT . '/' . $folder); if (!file_exists($path)) { throw new \InvalidArgumentException(Text::_('COM_VISFORMS_UPLOAD_DIRECTORY_DOES_NOT_EXIST')); } } catch (\UnexpectedValueException $e) { throw new \InvalidArgumentException(Text::_('COM_VISFORMS_UPLOAD_DIRECTORY_DOES_NOT_EXIST')); } // Set FTP credentials, if given ClientHelper::setCredentialsFromRequest('ftp'); // Make the filename safe $ext = File::getExt($file['name']); $file['name_org'] = $file['name']; // first remove extension incl. the dot and re-add it after making name safe // makeSafe also removes dots at the end of the string (if there were multiple dots in front of the extension) $file['name'] = File::makeSafe(basename($file['name'], "." . $ext)). "." . $ext; if ($field->keep_original_filename && $file['name_org'] !== $file['name']) { throw new \InvalidArgumentException(Text::sprintf('COM_VISFORMS_ERROR_WARNFILENAME', $file['name_org'], $maxfilesize)); } // Check upload conditions $err = null; if (!self::canUpload($file, $err, $maxfilesize, $allowedextensions)) { // The file can't be uploaded // RuntimeException is caught in the model; submit process is NOT stopped; Error is only logged throw new \InvalidArgumentException(Text::sprintf($err, $file['name_org'], $maxfilesize) . ' (' . $field->label . ')'); } else { // Check file extension is safe $checkoptions = (empty($visform->savemode)) ? array() : array('fobidden_ext_in_content' => false); if (!InputFilter::isSafeFile($file, $checkoptions)) { throw new \InvalidArgumentException(Text::sprintf('COM_VISFORMS_ERROR_UNABLE_TO_UPLOAD_FILE', $file['name_org'])); } if (empty($field->keep_original_filename)) { // get a unique id to rename the uploaded file $fileuid = uniqid(''); // rename file $pathInf = pathinfo($file['name']); $ext = $pathInf['extension']; $file['new_name'] = basename($file['name'], "." . $ext) . "_" . $fileuid . "." . $ext; $file['new_name'] = strtolower($file['new_name']); } else { $file['new_name'] = basename($file['name']); } // get complete upload path with filename of renamed file $filepath = Path::clean(JPATH_SITE . DIRECTORY_SEPARATOR . $folder . DIRECTORY_SEPARATOR . $file['new_name']); $file['filepath'] = $filepath; $file['filelink'] = Uri::base() . $folder . '/' . $file['new_name']; // try to upload file if (file_exists($file['filepath']) && empty($field->keep_original_filename)) { // File exists throw new \InvalidArgumentException(Text::sprintf('COM_VISFORMS_ERROR_FILE_EXISTS', $file['name_org'])); } else { try { File::upload($file['tmp_name'], $file['filepath'], false); } catch (FilesystemException $e) { // Error in upload throw new \InvalidArgumentException(Text::sprintf('COM_VISFORMS_ERROR_UNABLE_TO_UPLOAD_FILE', $file['name_org'])); } } } foreach ($file as $name => $value) { $visform->fields[$i]->file[$name] = $value; } $dbValue = new \stdClass(); $dbValue->folder = $folder; $dbValue->file = $field->file['new_name']; $registry = new Registry($dbValue); $visform->fields[$i]->dbValue = $registry->toString(); } } } } public static function deletefile($path):bool { try { File::delete(JPATH_ROOT . '/' . $path); return true; } catch (FilesystemException $e) { return false; } } public static function copyfile($name, $oldpath, $restore) { $newpath = JPATH_ROOT . '/images/visforms_save'; $orgpath = JPATH_ROOT . '/' . $oldpath; if (empty($restore)) { $fileexists = file_exists($newpath); if (empty($fileexists)) { Folder::create($newpath); } try { File::copy($orgpath, $newpath . '/' . $name); return true; } catch (FilesystemException $e) { return false; } catch (\UnexpectedValueException $e) { return false; } } else { try { File::move($newpath . '/' . $name, $orgpath); return true; } catch (FilesystemException $e) { return false; } catch (\UnexpectedValueException $e) { return false; } } } public static function isImage($file): bool { if (empty($file)) { return false; } $saveFile = File::makeSafe($file); if (empty($saveFile)) { return false; } $ext = strtolower(File::getExt($saveFile)); $imageExts = array('jpg', 'png', 'gif', 'bmp', 'jpeg'); if ((!empty($ext)) && (in_array($ext, $imageExts))) { return true; } return false; } public static function getFileInfo($dbValue) { $file = new \stdClass(); $file->name = HTMLHelper::_('visforms.getUploadFileName', $dbValue); $file->path = HTMLHelper::_('visforms.getUploadFilePath', $dbValue); $file->filepath = (!empty($file->path)) ? Path::clean(JPATH_SITE . DIRECTORY_SEPARATOR . $file->path) : ''; $file->link = HTMLHelper::_('visforms.getUploadFileLink', $dbValue); if (empty($file->name) || empty($file->path)) { return false; } $file->isimage = self::isImage($file->name); if (!empty($file->isimage)) { $info = @getimagesize(JPATH_SITE . '/' . $file->path); if ($info) { $file->width = @$info[0]; $file->height = @$info[1]; if (($info[0] > 60) || ($info[1] > 60)) { $dimensions = JoomlaMediaHelper::imageResize($file->width, $file->height, 60); $file->width = $dimensions[0]; $file->height = $dimensions[1]; } if ((!empty($file->width)) && (!empty($file->height))) { $file->displayDimension = 'width="' . $file->width . '" height="' . $file->height . '" '; } } else { $file->displayDimension = 'width="60" height="60" '; } } return $file; } // Small helper function that properly converts any configuration options to their byte representation. public static function toBytes($val) { switch ($val[strlen($val) - 1]) { case 'M': case 'm': return (int)$val * 1048576; case 'K': case 'k': return (int)$val * 1024; case 'G': case 'g': return (int)$val * 1073741824; default: return $val; } } public static function getMaxFileSizeInByte($value) { $uploadMaxFileSize = self::toBytes(ini_get('upload_max_filesize')); return (((int) $uploadMaxFileSize > 0) && (((int) $value === 0) || ($value * 1024) > $uploadMaxFileSize)) ? $uploadMaxFileSize : $value * 1024; } }