public static function download() { $requestFile = ipFile('') . ipRequest()->getRelativePath(); $fileDir = ipFile('file/'); if (mb_strpos($requestFile, $fileDir) !== 0) { return null; } $file = mb_substr($requestFile, mb_strlen($fileDir)); $file = urldecode($file); if (empty($file)) { throw new \Ip\Exception('Required parameter is missing'); } $absoluteSource = realpath(ipFile('file/' . $file)); if (!$absoluteSource || !is_file($absoluteSource)) { throw new \Ip\Exception\Repository\Transform("File doesn't exist", array('filename' => $absoluteSource)); } if (strpos($absoluteSource, realpath(ipFile('file/'))) !== 0 || strpos($absoluteSource, realpath(ipFile('file/secure'))) === 0) { throw new \Exception("Requested file (" . $file . ") is outside of public dir"); } $mime = \Ip\Internal\File\Functions::getMimeType($absoluteSource); $fsize = filesize($absoluteSource); // set headers header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header('Content-type: ' . $mime); header("Content-Transfer-Encoding: binary"); header("Content-Length: " . $fsize); // download // @readfile($file_path); $file = @fopen($absoluteSource, "rb"); if ($file) { while (!feof($file)) { print fread($file, 1024 * 8); flush(); if (connection_status() != 0) { @fclose($file); die; } } @fclose($file); } //TODO provide method to stop any output by ImpressPages ipDb()->disconnect(); exit; }
/** * Adds email to the queue * * Even if there is a big amount of emails, there is always reserved 20% of traffic for immediate emails. * Such emails are: registration cofirmation, contact form data and other. * Newsletters, greetings always can wait a litle. So they are not immediate and will not be send if is less than 20% of traffic left. * * @param string $from email address from whish an email should be send * @param $fromName * @param string $to email address where an email should be send * @param $toName * @param $subject * @param string $email email html text * @param bool $immediate indicate hurry of an email. * @param bool $html true if email message should be send as html * @param array $files files that should be attached to the email. Files should be accessible for php at this moment. They will be cached until send time. * @internal param $string @fromName * @internal param $string @toName */ function addEmail($from, $fromName, $to, $toName, $subject, $email, $immediate, $html, $files = null) { $cached_files = array(); $cached_fileNames = array(); $cached_fileMimeTypes = array(); if ($files) { if (is_string($files)) { $files = array($files); } foreach ($files as $fileSetting) { $file = array(); if (is_array($fileSetting)) { $file['real_name'] = $fileSetting[0]; $file['required_name'] = basename($fileSetting[1]); } else { $file['real_name'] = $fileSetting; $file['required_name'] = $fileSetting; } $new_name = 'contact_form_' . rand(); $new_name = \Ip\Internal\File\Functions::genUnoccupiedName($new_name, ipFile('file/tmp/')); if (copy($file['real_name'], ipFile('file/tmp/' . $new_name))) { $cached_files[] = ipFile('file/tmp/' . $new_name); $cached_fileNames[] = $file['required_name']; $tmpMimeType = \Ip\Internal\File\Functions::getMimeType($file['real_name']); if ($tmpMimeType == null) { $tmpMimeType = 'Application/octet-stream'; } $cached_fileMimeTypes[] = $tmpMimeType; } else { trigger_error('File caching failed'); } } } $cachedFilesStr = implode("\n", $cached_files); $cachedFileNamesStr = implode("\n", $cached_fileNames); $cachedFileMimeTypesStr = implode("\n", $cached_fileMimeTypes); $email = str_replace('src="' . ipConfig()->baseUrl(), 'src="', $email); Db::addEmail($from, $fromName, $to, $toName, $subject, $email, $immediate, $html, $cachedFilesStr, $cachedFileNamesStr, $cachedFileMimeTypesStr); }
/** * @param string $url * @return string */ protected function downloadFile($url, $title) { //download image to TMP dir and get $resultFilename $net = new \Ip\Internal\NetHelper(); $tmpFilename = $net->downloadFile($url, ipFile('file/tmp/'), 'bigstock_' . time()); if (!$tmpFilename) { return null; } //find out file mime type to know required extension try { $mime = \Ip\Internal\File\Functions::getMimeType(ipFile('file/tmp/' . $tmpFilename)); switch ($mime) { case 'image/png': $ext = '.jpg'; break; case 'image/gif': $ext = '.gif'; break; case 'image/bmp': $ext = '.bmp'; break; case 'image/pjpeg': case 'image/jpeg': default: $ext = '.jpg'; break; } } catch (\Ip\PhpException $e) { $ext = '.jpg'; } //get real nice new file name $title = \Ip\Internal\File\Functions::cleanupFileName($title); $words = explode(' ', $title); $cleanTitle = ''; foreach ($words as $word) { //limit file name to 30 symbols if (strlen($cleanTitle . '_' . $word) > 30) { break; } if ($cleanTitle != '') { $cleanTitle .= '_'; } $cleanTitle .= $word; } if ($cleanTitle == '') { $cleanTitle = 'file'; } $niceFileName = $cleanTitle . $ext; $destinationDir = ipFile('file/repository/'); $destinationFileName = \Ip\Internal\File\Functions::genUnoccupiedName($niceFileName, $destinationDir); copy(ipFile('file/tmp/' . $tmpFilename), $destinationDir . $destinationFileName); unlink(ipFile('file/tmp/' . $tmpFilename)); $browserModel = \Ip\Internal\Repository\BrowserModel::instance(); $file = $browserModel->getFile($destinationFileName); return $file; }