public function webcamHandler() { if (!OW::getRequest()->isPost()) { throw new Redirect404Exception(); } $service = BOL_AttachmentService::getInstance(); $attachDto = new BOL_Attachment(); $attachDto->setUserId(OW::getUser()->getId()); $attachDto->setAddStamp(time()); $attachDto->setStatus(0); $service->saveAttachment($attachDto); $fileName = 'attach_' . $attachDto->getId() . '.jpg'; $attachDto->setFileName($fileName); $service->saveAttachment($attachDto); $uploadPath = $service->getAttachmentsTempDir() . $fileName; $uploadUrl = $service->getAttachmentsTempUrl() . $fileName; // The JPEG snapshot is sent as raw input: $input = file_get_contents('php://input'); if (md5($input) == '7d4df9cc423720b7f1f3d672b89362be') { // Blank image. We don't need this one. echo json_encode(array('type' => 'takeError', 'error' => 'Empty photo', 'result' => array())); exit; } $result = file_put_contents($uploadPath, $input); if (!$result) { echo json_encode(array('type' => 'takeError', 'error' => 'Failed save the image. Make sure you chmod the uploads folder and its subfolders to 777', 'result' => array())); exit; } @chmod($uploadPath, 0666); $info = getimagesize($uploadPath); if ($info['mime'] != 'image/jpeg') { @unlink($uploadPath); echo json_encode(array('type' => 'takeError', 'error' => 'Wrong file', 'result' => array())); exit; } $content = new EQUESTIONS_CMP_AttPhotoPreview($uploadUrl); $xml = "<content><html><![CDATA[" . $content->render() . "]]></html><js></js></content><filePath>" . $uploadPath . "</filePath><fileId>" . $attachDto->getId() . "</fileId>"; $out = '<root>' . $xml . '</root>'; echo $out; exit; }
public function processPhotoAttachment(array $fileInfo) { $language = OW::getLanguage(); $error = false; if (!OW::getUser()->isAuthenticated() || empty($fileInfo) || !is_uploaded_file($fileInfo['tmp_name'])) { $error = $language->text('base', 'upload_file_fail'); } if ($fileInfo['error'] != UPLOAD_ERR_OK) { switch ($fileInfo['error']) { case UPLOAD_ERR_INI_SIZE: $error = $language->text('base', 'upload_file_max_upload_filesize_error'); break; case UPLOAD_ERR_PARTIAL: $error = $language->text('base', 'upload_file_file_partially_uploaded_error'); break; case UPLOAD_ERR_NO_FILE: $error = $language->text('base', 'upload_file_no_file_error'); break; case UPLOAD_ERR_NO_TMP_DIR: $error = $language->text('base', 'upload_file_no_tmp_dir_error'); break; case UPLOAD_ERR_CANT_WRITE: $error = $language->text('base', 'upload_file_cant_write_file_error'); break; case UPLOAD_ERR_EXTENSION: $error = $language->text('base', 'upload_file_invalid_extention_error'); break; default: $error = $language->text('base', 'upload_file_fail'); } } if (!in_array(UTIL_File::getExtension($_FILES['attachment']['name']), array('jpeg', 'jpg', 'png', 'gif'))) { $error = $language->text('base', 'upload_file_extension_is_not_allowed'); } if ((int) $_FILES['attachment']['size'] > (double) OW::getConfig()->getValue('base', 'tf_max_pic_size') * 1024 * 1024) { $error = $language->text('base', 'upload_file_max_upload_filesize_error'); } if ($error !== false) { throw new InvalidArgumentException($error); } $attachDto = new BOL_Attachment(); $attachDto->setUserId(OW::getUser()->getId()); $attachDto->setAddStamp(time()); $attachDto->setStatus(0); $this->attachmentDao->save($attachDto); $fileName = 'attach_' . $attachDto->getId() . '.' . UTIL_File::getExtension($_FILES['attachment']['name']); $attachDto->setFileName($fileName); $this->attachmentDao->save($attachDto); $uploadPath = $this->getAttachmentsDir() . $fileName; $uploadUrl = $this->getAttachmentsUrl() . $fileName; try { $image = new UTIL_Image($fileInfo['tmp_name']); $image->resizeImage(1000, 1000)->orientateImage()->saveImage($uploadPath); } catch (Exception $e) { throw new InvalidArgumentException($language->text('base', 'upload_file_fail')); } chmod($uploadPath, 0666); return array('genId' => $attachDto->getId(), 'url' => $uploadUrl); }
public function processUploadedFile($pluginKey, array $fileInfo, $bundle = null, $validFileExtensions = array(), $maxUploadSize = null, $dimensions = null) { $language = OW::getLanguage(); $error = false; if (!OW::getUser()->isAuthenticated()) { throw new InvalidArgumentException($language->text('base', 'user_is_not_authenticated')); } if (empty($fileInfo) || !is_uploaded_file($fileInfo['tmp_name'])) { throw new InvalidArgumentException($language->text('base', 'upload_file_fail')); } if ($fileInfo['error'] != UPLOAD_ERR_OK) { switch ($fileInfo['error']) { case UPLOAD_ERR_INI_SIZE: $error = $language->text('base', 'upload_file_max_upload_filesize_error'); break; case UPLOAD_ERR_PARTIAL: $error = $language->text('base', 'upload_file_file_partially_uploaded_error'); break; case UPLOAD_ERR_NO_FILE: $error = $language->text('base', 'upload_file_no_file_error'); break; case UPLOAD_ERR_NO_TMP_DIR: $error = $language->text('base', 'upload_file_no_tmp_dir_error'); break; case UPLOAD_ERR_CANT_WRITE: $error = $language->text('base', 'upload_file_cant_write_file_error'); break; case UPLOAD_ERR_EXTENSION: $error = $language->text('base', 'upload_file_invalid_extention_error'); break; default: $error = $language->text('base', 'upload_file_fail'); } throw new InvalidArgumentException($error); } if (empty($validFileExtensions)) { $validFileExtensions = json_decode(OW::getConfig()->getValue('base', 'attch_ext_list'), true); } if ($maxUploadSize === null) { $maxUploadSize = OW::getConfig()->getValue('base', 'attch_file_max_size_mb'); } if (!empty($validFileExtensions) && !in_array(UTIL_File::getExtension($fileInfo['name']), $validFileExtensions)) { throw new InvalidArgumentException($language->text('base', 'upload_file_extension_is_not_allowed')); } // get all bundle upload size $bundleSize = floor($fileInfo['size'] / 1024); if ($bundle !== null) { $list = $this->attachmentDao->findAttahcmentByBundle($pluginKey, $bundle); /* @var $item BOL_Attachment */ foreach ($list as $item) { $bundleSize += $item->getSize(); } } if ($maxUploadSize > 0 && $bundleSize > $maxUploadSize * 1024) { throw new InvalidArgumentException($language->text('base', 'upload_file_max_upload_filesize_error')); } $attachDto = new BOL_Attachment(); $attachDto->setUserId(OW::getUser()->getId()); $attachDto->setAddStamp(time()); $attachDto->setStatus(0); $attachDto->setSize(floor($fileInfo['size'] / 1024)); $attachDto->setOrigFileName(htmlspecialchars($fileInfo['name'])); $attachDto->setFileName(uniqid() . '_' . UTIL_File::sanitizeName($attachDto->getOrigFileName())); $attachDto->setPluginKey($pluginKey); if ($bundle !== null) { $attachDto->setBundle($bundle); } $this->attachmentDao->save($attachDto); $uploadPath = $this->getAttachmentsDir() . $attachDto->getFileName(); $tempPath = $this->getAttachmentsDir() . 'temp_' . $attachDto->getFileName(); if (in_array(UTIL_File::getExtension($fileInfo['name']), array('jpg', 'jpeg', 'gif', 'png'))) { try { $image = new UTIL_Image($fileInfo['tmp_name']); if (empty($dimensions)) { $dimensions = array('width' => 1000, 'height' => 1000); } $image->resizeImage($dimensions['width'], $dimensions['height'])->orientateImage()->saveImage($tempPath); $image->destroy(); @unlink($fileInfo['tmp_name']); } catch (Exception $e) { throw new InvalidArgumentException($language->text('base', 'upload_file_fail')); } } else { move_uploaded_file($fileInfo['tmp_name'], $tempPath); } OW::getStorage()->copyFile($tempPath, $uploadPath); OW::getStorage()->chmod($uploadPath, 0666); unlink($tempPath); return array('uid' => $attachDto->getBundle(), 'dto' => $attachDto, 'path' => $uploadPath, 'url' => $this->getAttachmentsUrl() . $attachDto->getFileName()); }