Exemple #1
0
 /**
  * Check if the image is valid (to be used by form_fileUploaded)
  *
  * @param string $file File pathname
  * @param array $prm
  * @return bool|string True if valid or string representing the error
  */
 public function valid(array $file, array $prm = array())
 {
     if (empty($file) || isset($file['size']) && $file['size'] == 0) {
         return true;
     }
     if ($this->cfg->maxsize && $file['size'] > $this->cfg->maxsize) {
         return sprintf($this->cfg->getInArray('validErrors', 'maxsize'), '%s', file::humanSize($this->cfg->maxsize, true));
     }
     $type = $file['type'] != 'application/octet-stream' ? $file['type'] : file::getType($file['name']);
     if (count($this->cfg->mime) > 0 && !in_array($type, $this->cfg->mime)) {
         return $this->cfg->getInArray('validErrors', 'mime');
     }
     return true;
 }
 protected function afterInit()
 {
     $this->dir = $this->cfg->dir;
     if ($this->cfg->subdir) {
         $this->subdir = $this->cfg->subdir . DS;
         $this->dir .= $this->subdir;
     }
     file::createDir($this->dir);
     $uploaded = false;
     if (strpos($this->cfg->name, '[')) {
         $tmp = explode('[', str_replace(']', '', $this->cfg->name));
         $tmpfile = utils::getValInArray($_FILES, $tmp);
         if (!empty($tmpfile) && is_array($tmpfile)) {
             $this->file = $tmpfile;
             $this->file['saved'] = false;
             $uploaded = $this->file['error'] == UPLOAD_ERR_OK;
         }
     } else {
         if (array_key_exists($this->cfg->name, $_FILES)) {
             $this->file = $_FILES[$this->cfg->name];
             $this->file['saved'] = false;
             $uploaded = $this->file['error'] == UPLOAD_ERR_OK;
         }
     }
     if ($this->cfg->current && !$uploaded) {
         $name = basename($this->cfg->current);
         $savePath = $this->dir . $name;
         $webPath = str_replace(DS, '/', $this->subdir . $name);
         $this->file = array('name' => $name, 'type' => file::getType($savePath), 'tmp_name' => '', 'error' => UPLOAD_ERR_OK, 'size' => file::size($savePath), 'saved' => array('name' => $name, 'savePath' => $savePath, 'webPath' => $webPath), 'savedFromValue' => true);
         $this->saved = $webPath;
     }
     $this->helper = factory::isCreable('helper_' . $this->cfg->helper) ? factory::getHelper($this->cfg->helper, $this->getHelperPrm('factory')) : null;
     if ($this->cfg->autoSave && !empty($this->file) && $this->isValid() === true) {
         $this->save();
     }
 }
Exemple #3
0
 /**
  * Get the body content
  *
  * @param string|null $headers Header to edit or null to add the header in the email
  * @return string
  */
 protected function getBody(&$headers = null)
 {
     $body = null;
     //$text = $this->quotePrintable($this->cfg->text);
     $text = $this->cfg->text;
     if ($this->cfg->html) {
         if (empty($this->cfg->text)) {
             //$text = $this->quotePrintable(utils::html2Text($this->cfg->html));
             $text = utils::html2Text($this->cfg->html);
         }
         $boundary = '------------' . $this->getBoundary();
         if ($headers) {
             $headers .= $this->headerLine('Content-Type', 'multipart/alternative;' . $this->cfg->crlf . ' boundary="' . $boundary . '"');
             //$headers.= $this->textLine(' boundary="'.$boundary.'"');
         } else {
             $body .= $this->headerLine('Content-Type', 'multipart/alternative;' . $this->cfg->crlf . ' boundary="' . $boundary . '"');
             //$body.= $this->textLine(' boundary="'.$boundary.'"');
             $body .= $this->textLine('');
         }
         // Text part
         $body .= $this->textLine('This is a multi-part message in MIME format.');
         $body .= $this->textLine('--' . $boundary);
     }
     $body .= $this->headerLine('Content-Type', 'text/plain; charset=' . $this->cfg->charset);
     //$body.= $this->textLine(' charset="'.$this->cfg->charset.'"');
     $body .= $this->headerLine('Content-Transfer-Encoding', $this->cfg->encoding);
     //$body.= $this->headerLine('Content-Disposition', 'inline');
     $body .= $this->textLine(null);
     $body .= $this->textLine($this->encode($this->wrapText($text)));
     if ($this->cfg->html) {
         // HTML part
         $body .= $this->textLine('--' . $boundary);
         $html = $this->cfg->html;
         $inlineImages = false;
         if ($this->cfg->htmlInlineImage) {
             $rootUri = request::get('rootUri');
             preg_match_all('@src="(' . $rootUri . '|/)(.+)"@siU', $html, $matches);
             if (!empty($matches)) {
                 $images = array_unique($matches[2]);
                 $inlineImages = array();
                 $i = 1;
                 foreach ($images as $img) {
                     if (file::webExists($img)) {
                         $file = WEBROOT . str_replace('/', DS, $img);
                         $cid = 'part' . $i . '.' . $this->getBoundary(16) . '@' . $this->cfg->serverName;
                         $inlineImages[] = array('cid' => $cid, 'file' => $file, 'name' => file::name($file), 'type' => file::getType($file));
                         $i++;
                         $html = preg_replace('@src="(' . $rootUri . '|/)(' . $img . ')"@siU', 'src="cid:' . $cid . '"', $html);
                     }
                 }
             }
         }
         if (!empty($inlineImages)) {
             $boundaryRel = '------------' . $this->getBoundary();
             $body .= $this->headerLine('Content-Type', 'multipart/related;' . $this->cfg->crlf . ' boundary="' . $boundaryRel . '"');
             //$body.= $this->textLine(' boundary="'.$boundaryRel.'"');
             $body .= $this->textLine(null);
             $body .= $this->textLine(null);
             $body .= $this->textLine('--' . $boundaryRel);
         }
         $body .= $this->headerLine('Content-Type', 'text/html; charset=' . $this->cfg->charset . '');
         //$body.= $this->textLine(' charset="'.$this->cfg->charset.'"');
         $body .= $this->headerLine('Content-Transfer-Encoding', $this->cfg->encoding);
         //$body.= $this->headerLine('Content-Disposition', 'inline');
         $body .= $this->textLine(null);
         //$body.= $this->textLine($this->quotePrintable($html));
         $body .= $this->textLine($this->encode($this->wrapText($html)));
         if (!empty($inlineImages)) {
             foreach ($inlineImages as $img) {
                 $body .= $this->textLine('--' . $boundaryRel);
                 $body .= $this->headerLine('Content-Type', $img['type']);
                 //.'; name="'.$img['name'].'"');
                 $body .= $this->headerLine('Content-Transfer-Encoding', $this->cfg->fileEncoding);
                 $body .= $this->headerLine('Content-ID', '<' . $img['cid'] . '>');
                 //$body.= $this->headerLine('Content-Disposition', 'inline; filename="'.$img['name'].'"');
                 $body .= $this->textLine(null);
                 $body .= $this->encode(file::read($img['file']), $this->cfg->fileEncoding);
             }
             $body .= $this->textLine('--' . $boundaryRel . '--');
             $body .= $this->textLine(null);
         }
         $body .= '--' . $boundary . '--';
     }
     return $body;
 }
Exemple #4
0
	/**
	 * Send a media to download, using HTTP range or not, is possible
	 *
	 * @param string $file File Path
	 * @param bool $forceDownload True if the media should be forced to download
	 * @param string $fileName Filename to send to the browser. If null, basename will be used
	 * @param bool $delete Indicate if the file should be deleted after download
	 */
	protected function mediaDownload($file, $forceDownload = false, $fileName = null, $delete = false) {
		$fileName = $fileName ? $fileName : basename($file);
		if(strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE'))
			$fileName = preg_replace('/\./', '%2e', $fileName, substr_count($fileName, '.') - 1);
		$fileModified = filemtime($file);
		$fileSize = filesize($file);
		$fileType = file::getType($file);
		$audio = strpos($fileType, 'audio') === 0;
		$video = strpos($fileType, 'video') === 0;

		$seekStart = 0;
		$seekEnd = -1;
		$bufferSize = 8*1024;
		$partialDownload = false;
		$httpRangeDownload = false;

		if (isset($_SERVER['HTTP_RANGE'])) {
			$range = explode('-', substr($_SERVER['HTTP_RANGE'], strlen('bytes=')));
			if($range[0] > 0)
				$seekStart = intval($range[0]);
			$seekEnd = $range[1] > 0 ? intval($range[1]) : -1;
			$partialDownload = true;
			$httpRangeDownload = true;
		} else if ($audio && request::isMobile()) {
			$partialDownload = true;
			$httpRangeDownload = true;
		}

		if ($seekEnd < $seekStart)
			$seekEnd = $fileSize - 1;

		$contentLength = $seekEnd - $seekStart + 1;

		if(!$fileHandle = fopen($file, 'rb'))
			$this->error();

		// headers
		header('Pragma: public');
		if ($forceDownload) {
			if (ini_get('zlib.output_compression'))
				ini_set('zlib.output_compression', 'Off');

			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Cache-Control: private', false);

			header('Content-Type: application/force-download');
			header('Content-Type: application/octet-stream');
			header('Content-Type: application/download');
			header('Content-type: '.$fileType);
			header('Content-Disposition: attachment; filename='.$fileName.'');
		} else {
			header('Cache-Control: public');
			header('Content-type: '.$fileType);
			header('Content-Disposition: inline; filename='.$fileName.'');
		}
		header('Last-Modified: '.date('D, d M Y H:i:s \G\M\T', $fileModified));
		header("Content-Transfer-Encoding: binary\n");
		if ($httpRangeDownload) {
			header('HTTP/1.0 206 Partial Content');
			header('Status: 206 Partial Content');
			header('Accept-Ranges: bytes');
			header('Content-Range: bytes '.$seekStart.'-'.$seekEnd.'/'.$fileSize);
		}
		header('Content-Length: '.$contentLength);

		if ($seekStart > 0) {
			$partialDownload = true;
			fseek($fileHandle, $seekStart);
			if ($fileType == 'video/x-flv')
				echo 'FLV', pack('C', 1), pack('C', 1), pack('N', 9), pack('N', 9);
		}

		$this->setCompress(false);
		$this->beforeOut();

		$speed = 0;
		$bytesSent = 0;
		$chunk = 1;
		$throttle = $video ? 320 : ($audio ? 84 : 0);
		$burst = 1024 * ($video ? 500 : ($audio ? 120 : 0));
		while (!(connection_aborted() || connection_status() == 1) && $bytesSent < $contentLength) {
			// 1st buffer size after the first burst has been sent
			if ($bytesSent >= $burst)
				$speed = $throttle;

			// make sure we don't read past the total file size
			if ($bytesSent + $bufferSize > $contentLength)
				$bufferSize = $contentLength - $bytesSent;

			// send data
			echo fread($fileHandle, $bufferSize);
			$bytesSent+= $bufferSize;

			// clean up
			flush();

			// throttle
			if($speed && ($bytesSent - $burst > $speed * $chunk*1024)) {
				sleep(1);
				$chunk++;
			}
		}

		fclose($fileHandle);

		if ($delete)
			file::delete($file);
		exit;
	}