2namespace Bitrix\Main\UI\Uploader;
4use Bitrix\Main\Context;
6use Bitrix\Main\ErrorCollection;
8use Bitrix\Main\UI\FileInputUtility;
10use Bitrix\Main\Web\HttpClient;
11use Bitrix\Main\Web\Uri;
12use Bitrix\Main\Localization\Loc;
13use Bitrix\Main\Application;
33 $hash = self::initHash(
array(
"id" => $file[
"id"],
"name" => $file[
"name"]));
37 "uploadStatus" =>
null,
38 "executeStatus" =>
null,
39 "name" => $file[
"name"],
40 "type" => $file[
"type"],
41 "size" => $file[
"size"],
47 $this->
package = $package;
52 $eventName =
"onFileIsContinued";
56 $eventName =
"onFileIsStarted";
77 if (empty($file[
"id"]))
78 return md5($file[
"name"]);
79 if (preg_match(
"/^file([0-9]+)$/", $file[
"id"]))
81 return md5($file[
"id"]);
89 return $this->data[
"id"];
97 return $this->data[
"hash"];
104 return $this->data[
"name"];
111 return $this->data[
"size"];
118 return $this->data[
"type"];
127 return $this->data[
"files"][
$code];
151 $code = $file[
"code"];
153 if (
$code !==
"default" && !array_key_exists(
$code, $copies))
155 $result->addError(
new Error(
"The copy name is not in the list."));
161 else if (isset($file[
"chunkId"]))
168 "type" => $file[
"type"],
169 "uploadStatus" =>
"inprogress",
170 "count" => $file[
"count"],
171 "chunks" =>
array());
172 $file[
"chunks"] =
$info[
"chunks"];
173 $r =
$storage->copy($this->package->getPath().$this->getHash(), $file);
174 if (!$r->isSuccess())
176 $result->addError($r->getErrorCollection()->current());
181 "size" => $file[
"size"],
182 "number" => $file[
"number"],
183 "start" => $file[
"start"],
184 "error" => $file[
"error"]
186 $file[
"uploadStatus"] =
"uploaded";
187 $data = $r->getData();
192 $data[
"uploadStatus"] =
"uploaded";
193 $info =
$data + array_intersect_key(
$info, [
"width" =>
"",
"height" =>
""]);
197 $info += array_intersect_key(
$data, [
"width" =>
"",
"height" =>
""]);
205 $r =
$storage->copy($this->package->getPath().$this->getHash(), $file);
208 $data = $r->getData();
211 $data[
"uploadStatus"] =
"uploaded";
216 $result->addError($r->getErrorCollection()->current());
222 if (
$info[
"uploadStatus"] ==
"uploaded")
225 $info[
"~url"] = $this->getUrl(
"view",
$code, \COption::GetOptionString(
"main.fileinput",
"entryPointUrl",
"/bitrix/tools/upload.php"));
226 $info[
"sizeFormatted"] = \CFile::FormatSize(
$info[
"size"]);
227 foreach ($this->data[
"files"] as
$k =>
$f)
229 if (
$f[
"uploadStatus"] ==
"uploaded")
246 static $lastSaved =
null;
247 if ($lastSaved != $this->data)
249 $lastSaved = self::arrayWalkRecursive($this->data);
250 self::setIntoCache($this->data[
"hash"], $this->package->getPath(), $lastSaved);
261 foreach ($array as
$k => $v)
265 $array[
$k] = self::arrayWalkRecursive($v);
267 else if (is_object($v))
291 return !$this->errorCollection->isEmpty();
300 return $this->errorCollection;
307 public function getErrorMessage()
310 for ($this->errorCollection->rewind(); $this->errorCollection->valid(); $this->errorCollection->next())
313 $error = $this->errorCollection->current();
316 return implode(
"", $m);
336 $hash = self::initHash($file);
340 if ($file->IsExists())
356 $directory =
$io->GetDirectory(
$path.$hash);
357 if ($directory->Create())
358 $io->GetFile(
$path.$hash.
"/.log")->PutContents(serialize(
$data));
371 $res2 = is_array($res2) ? $res2 :
array();
389 $this->data[
"uploadStatus"] =
$status;
399 return ($this->data[
"uploadStatus"] ===
"uploaded");
409 $this->data[
"executeStatus"] =
$status;
419 return ($this->data[
"executeStatus"] ===
"executed");
438 $data[
"id"] = $this->data[
"id"];
439 $data[
"hash"] = $this->data[
"hash"];
455 $directory =
$io->GetDirectory(
$path.$hash);
456 $res = $directory->GetChildren();
457 foreach(
$res as $file)
475 if (mb_strpos(
$hash,
"_") > 0)
477 $copy = explode(
"_",
$hash);
478 $hash = $copy[0]; $copy = $copy[1];
480 $copy = ($copy ?:
"default");
484 $file = $file[
"files"][$copy];
490 if (mb_strpos(\CTempFile::GetAbsoluteRoot(),
$docRoot) === 0)
491 \CFile::ViewByUser($file,
array(
"content_type" => $file[
"type"]));
493 self::view($file,
array(
"content_type" => $file[
"type"]));
501 private function getUrl($act =
"view", $copy =
"default",
$url =
null)
503 $url = is_null(
$url) ? Context::getCurrent()->getRequest()->getRequestUri() :
$url;
508 "CID" => $this->package->getCid(),
510 "hash" => $this->getHash(),
517 return $uri->getUri();
523 if (($tempRoot = \CTempFile::GetAbsoluteRoot()) && ($filePath = $tempRoot.$tmpName) &&
$io->FileExists($filePath))
525 $f =
$io->GetFile($filePath);
526 $directory =
$io->GetDirectory(
$f->GetPath());
527 $hash = $directory->GetName();
528 if (($cache = self::getFromCache(
$hash, $directory->GetPath().
"/")) && is_array($cache) &&
529 array_key_exists(
"files", $cache) && array_key_exists(
$f->getName(), $cache[
"files"]))
531 return $cache[
"files"][
$f->getName()][
"~url"];
545 case UPLOAD_ERR_INI_SIZE:
546 $message = Loc::getMessage(
"BXU_UPLOAD_ERR_INI_SIZE");
548 case UPLOAD_ERR_FORM_SIZE:
549 $message = Loc::getMessage(
"BXU_UPLOAD_ERR_FORM_SIZE");
551 case UPLOAD_ERR_PARTIAL:
552 $message = Loc::getMessage(
"BXU_UPLOAD_ERR_PARTIAL");
554 case UPLOAD_ERR_NO_FILE:
555 $message = Loc::getMessage(
"BXU_UPLOAD_ERR_NO_FILE");
557 case UPLOAD_ERR_NO_TMP_DIR:
558 $message = Loc::getMessage(
"BXU_UPLOAD_ERR_NO_TMP_DIR");
560 case UPLOAD_ERR_CANT_WRITE:
561 $message = Loc::getMessage(
"BXU_UPLOAD_ERR_CANT_WRITE");
563 case UPLOAD_ERR_EXTENSION:
564 $message = Loc::getMessage(
"BXU_UPLOAD_ERR_EXTENSION");
567 $message =
'Unknown uploading error ['.$error.
']';
578 if (is_null(static::$http))
583 return static::$http;
595 if ($file[
"error"] > 0)
597 else if (array_key_exists(
"tmp_url", $file))
599 $url =
new Uri($file[
"tmp_url"]);
600 if (
$url->getHost() ==
'' && (
$tmp = \CFile::MakeFileArray(
$url->getPath())) && is_array(
$tmp))
602 $file = array_merge(
$tmp, $file);
604 else if (
$url->getHost() <>
'' &&
605 self::http()->query(
"HEAD", $file[
"tmp_url"]) &&
606 self::http()->getStatus() ==
"200")
608 $file = array_merge($file,
array(
609 "size" => self::http()->getHeaders()->
get(
"content-length"),
610 "type" => self::http()->getHeaders()->
get(
"content-type")
615 $result->addError(
new Error(Loc::getMessage(
"BXU_FileIsNotUploaded"),
"BXU347.2"));
620 $result->addError(
new Error(Loc::getMessage(
"BXU_FileIsNotUploaded"),
"BXU347.2.8"));
622 else if (!isset($file[
'bucketId']) && (!file_exists($file[
'tmp_name']) || (
623 (mb_substr($file[
"tmp_name"], 0, mb_strlen(
$params[
"path"])) !==
$params[
"path"]) &&
624 !is_uploaded_file($file[
'tmp_name'])
628 $result->addError(
new Error(Loc::getMessage(
"BXU_FileIsNotUploaded"),
"BXU347.2.7"));
637 if (
$params[
"uploadMaxFilesize"] > 0 &&
$f->getSize() >
$params[
"uploadMaxFilesize"])
644 $ff = array_merge($file,
array(
"name" =>
$name));
645 if (
$params[
"allowUpload"] ===
"I")
647 $error = \CFile::CheckFile($ff,
$params[
"uploadMaxFilesize"],
"image/", \CFile::GetImageExtensions());
651 $error = \CFile::CheckFile($ff,
$params[
"uploadMaxFilesize"],
false,
$params[
"allowUploadExt"]);
655 $error = \CFile::CheckFile($ff,
$params[
"uploadMaxFilesize"]);
662 if (preg_match(
"/^(.+?)\\.ch(\\d+)\\.(\\d+)\\.chs(\\d+)$/", $file[
"code"],
$matches))
668 $file[
"chunkId"] = self::getChunkKey($file[
"count"], $file[
"number"]);
670 $file[
"~size"] =
$f->getSize();
671 $file[
"~name"] =
$f->getName();
672 $file[
"~type"] =
$f->getType();
684 $chunksCount = max(ceil(log10($chunksCount)), 4);
685 return "p".str_pad($chunkNumber, $chunksCount,
"0", STR_PAD_LEFT);
697 $watermark = (array_key_exists(
"watermark", $source) ?
array() : $watermarkParams);
698 if (\CFile::ResizeImageFile(
704 $canvasParams[
"quality"],
709 if (array_key_exists(
"watermark", $source) || !empty($watermarkParams))
710 $dest[
"watermark"] =
true;
713 $dest[
"error"] = 348;
715 $dest[
"type"] =
$dest[
"type"] ?: \CFile::GetContentType(
$dest[
"tmp_name"]);
716 $dest[
"sizeFormatted"] = \CFile::FormatSize(
$dest[
"size"]);
728 if (!array_key_exists(
"tmp_name", $fileData) || empty($fileData[
"tmp_name"]))
734 $fastDownload = (\COption::GetOptionString(
'main',
'bx_fast_download',
'N') ==
'Y');
736 $attachment_name =
"";
737 $content_type = (array_key_exists(
"type", $fileData) && !empty($fileData[
"type"]) ? $fileData[
"type"] :
"");
745 $content_type =
$options[
"content_type"];
747 $specialchars =
$options[
"specialchars"];
748 if(isset(
$options[
"force_download"]))
749 $force_download =
$options[
"force_download"];
751 $cache_time = intval(
$options[
"cache_time"]);
752 if(isset(
$options[
"attachment_name"]))
753 $attachment_name =
$options[
"attachment_name"];
759 $name = str_replace(
array(
"\n",
"\r"),
'', $fileData[
"name"]);
761 if ($attachment_name)
762 $attachment_name = str_replace(
array(
"\n",
"\r"),
'', $attachment_name);
764 $attachment_name =
$name;
770 if (mb_strpos($fileData[
"tmp_name"], \CTempFile::GetAbsoluteRoot()) === 0)
772 $file = new \Bitrix\Main\IO\File($fileData[
"tmp_name"]);
781 $filetime = $file->getModificationTime();
791 $filesize = $fileData[
"size"];
794 $p = $server->get(
"HTTP_RANGE") && mb_strpos($server->get(
"HTTP_RANGE"),
"=");
797 $bytes = mb_substr($server->get(
"HTTP_RANGE"),
$p + 1);
801 $cur_pos = floatval(mb_substr(
$bytes, 0,
$p));
802 $size = floatval(mb_substr(
$bytes,
$p + 1));
805 $size = $filesize - 1;
807 if ($cur_pos > $size)
810 $size = $filesize - 1;
815 if ($server->getRequestMethod() ==
"HEAD")
818 header(
"Accept-Ranges: bytes");
819 header(
"Content-Type: ".$content_type);
820 header(
"Content-Length: ".($size-$cur_pos+1));
823 header(
"Last-Modified: ".date(
"r", $filetime));
831 $ETag = md5($fileData[
"tmp_name"].$filesize.$filetime);
832 if ($server->get(
"HTTP_IF_NONE_MATCH") === $ETag)
835 header(
"Cache-Control: private, max-age=".$cache_time.
", pre-check=".$cache_time);
838 header(
"ETag: ".$ETag);
843 $lastModified = gmdate(
'D, d M Y H:i:s', $filetime).
' GMT';
844 if ($server->get(
"HTTP_IF_NONE_MATCH") === $lastModified)
847 header(
"Cache-Control: private, max-age=".$cache_time.
", pre-check=".$cache_time);
854 $translitName = \CUtil::translit($attachment_name, LANGUAGE_ID,
array(
857 "replace_space" =>
'-',
858 "change_case" =>
false,
863 if(ini_get(
'zlib.output_compression'))
864 ini_set(
'zlib.output_compression',
'Off');
869 \CHTTP::SetStatus(
"200 OK");
871 header(
"Content-Type: ".$content_type);
872 header(
"Content-Disposition: attachment; filename=\"".$translitName.
"\"; filename*=utf-8''".$utfName);
873 header(
"Content-Transfer-Encoding: binary");
874 header(
"Content-Length: ".($size-$cur_pos+1));
875 if(is_resource($src))
877 header(
"Accept-Ranges: bytes");
878 header(
"Content-Range: bytes ".$cur_pos.
"-".$size.
"/".$filesize);
883 header(
"Cache-Control: private, max-age=".$cache_time.
", pre-check=".$cache_time);
885 header(
'Last-Modified: '.$lastModified);
889 header(
"Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0");
892 header(
"Expires: 0");
893 header(
"Pragma: public");
896 if($fastDownload && ($fromClouds || mb_strpos($fileData[
"tmp_name"],
Application::getInstance()->getContext()->getServer()->getDocumentRoot()) === 0))
900 $filename = preg_replace(
'~^(http[s]?)(\://)~i',
'\\1.' , $fileData[
"tmp_name"]);
901 $cloudUploadPath = \COption::GetOptionString(
'main',
'bx_cloud_upload',
'/upload/bx_cloud_upload/');
902 header(
'X-Accel-Redirect: '.$cloudUploadPath.$filename);
906 header(
'X-Accel-Redirect: '.$fileData[
"tmp_name"]);
911 session_write_close();
912 $file->seek($cur_pos);
913 while(!feof($src) && ($cur_pos <= $size))
916 if($cur_pos + $bufsize > $size)
917 $bufsize = $size - $cur_pos + 1;
918 $cur_pos += $bufsize;
919 echo fread($src, $bufsize);
925 $src = new \Bitrix\Main\Web\HttpClient();
926 $fp = fopen(
"php://output",
"wb");
927 $src->setOutputStream($fp);
928 $src->get($fileData[
"tmp_name"]);
931 \CMain::FinalActions();
__construct($package, array $file)
static viewFile($cid, $hash, $path)
static initHash($file=array())
static getUploadErrorMessage($error)
static createCanvas($source, $dest, $canvasParams=array(), $watermarkParams=array())
saveFile(&$file, Storable $storage, array $copies)
static deleteCache(Package $package, array $file)
static checkFile(&$file, File $f, $params)
static arrayWalkRecursive(array $array)
static merge($res, $res2)
static getChunkKey($chunksCount, $chunkNumber)
static view(array $fileData, $options=array())
static setIntoCache($hash, $path, $data)
static getUrlFromRelativePath($tmpName)
static getFromCache($hash, $path)
static deleteFile($cid, $hash, $path)
setExecuteStatus($status)
static normalize($contentType)
static urnEncode($str, $charset='UTF-8')
static SetStatus($status)
if(!is_array($prop["VALUES"])) $tmp
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
const BX_RESIZE_IMAGE_PROPORTIONAL
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/urlrewrite.php")) $uri
if($NS['step']==6) if( $NS[ 'step']==7) if(COption::GetOptionInt('main', 'disk_space', 0) > 0) $info
ExecuteModuleEventEx($arEvent, $arParams=[])
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
GetMessage($name, $aReplace=null)
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
if(empty($signedUserToken)) $key
if(empty($decryptedData)) $storage
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
if(!empty($sellerData)) $dest
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']