Exemplo n.º 1
0
 public function run($id)
 {
     if (!($fileModel = D3filesModel::findOne(['id' => $id, 'deleted' => 0]))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     if (!($file = D3files::findOne($fileModel->d3files_id))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     if (!($fileModelName = D3filesModelName::findOne($fileModel->model_name_id))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     /**
      * validate modelname
      */
     if (Yii::$app->getModule('d3files')->disableController) {
         if ($fileModelName->name != $this->modelName) {
             throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
         }
     }
     // Check access rights to the record the file is attached to
     D3files::performReadValidation($fileModelName->name, $fileModel->model_id);
     $modelName = $fileModelName->name;
     if (!$fileModel->is_file) {
         if (!($realFileModel = D3filesModel::findOne(['d3files_id' => $fileModel->d3files_id, 'is_file' => 1]))) {
             throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
         }
         if (!($realfileModelName = D3filesModelName::findOne($realFileModel->model_name_id))) {
             throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
         }
         $modelName = $realfileModelName->name;
         //$modelName
     }
     $fileHandler = new FileHandler(['model_name' => $modelName, 'model_id' => $file->id, 'file_name' => $file->file_name]);
     $fileHandler->download();
 }
Exemplo n.º 2
0
 public function init()
 {
     parent::init();
     D3Files::registerTranslations();
     $this->model_name = $this->model->className();
     $this->fileList = ModelD3Files::fileListForWidget($this->model_name, $this->model_id);
 }
Exemplo n.º 3
0
 public function run($id, $hash)
 {
     // Pause every request
     sleep(1);
     /**
      * Validate both parameters:
      * id - only digits > 0
      * hash - only hex, exactly 32 chars long
      */
     if (!preg_match('#^[1-9][0-9]*$#', $id)) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     $hash = strtoupper($hash);
     if (!preg_match('#^[0-9A-F]{32}$#', $hash)) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     if (!($fileModelShared = D3filesModelShared::find()->where(['and', "id={$id}", "hash='{$hash}'", "left_loadings>0", "expire_date>=CURDATE()"])->one())) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     if (!($fileModel = D3filesModel::findOne(['id' => $fileModelShared->d3files_model_id, 'deleted' => 0, 'is_file' => 1]))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     if (!($file = D3files::findOne($fileModel->d3files_id))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     if (!($fileModelName = D3filesModelName::findOne($fileModel->model_name_id))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     $fileModelShared->left_loadings--;
     $fileModelShared->save();
     $fileHandler = new FileHandler(['model_name' => $fileModelName->name, 'model_id' => $file->id, 'file_name' => $file->file_name]);
     $fileHandler->download();
 }
Exemplo n.º 4
0
 public function run($id)
 {
     // $id here is id for model to which will be attached attachments
     Yii::$app->response->format = Response::FORMAT_JSON;
     if (!isset($_FILES['upload_file'])) {
         throw new NotFoundHttpException(Yii::t('d3files', 'File not uploaded.'));
     }
     // If controller actions are not disabled, use $_POST['model_name']
     if (!Yii::$app->getModule('d3files')->disableController) {
         $this->modelName = Yii::$app->request->post('model_name');
     }
     if (empty($this->modelName)) {
         throw new HttpException(422, Yii::t('d3files', 'mandatory POST parameter modelName is not set'));
     }
     // Check access rights to the record the file is attached to
     D3files::performReadValidation($this->modelName, $id);
     $tmp_id = uniqid();
     $fileHandler = new FileHandler(['model_name' => $this->modelName, 'model_id' => $tmp_id, 'file_name' => $_FILES['upload_file']['name']]);
     $fileHandler->upload();
     $model = new D3files();
     $model->file_name = $_FILES['upload_file']['name'];
     $model->add_datetime = new \yii\db\Expression('NOW()');
     $model->user_id = Yii::$app->user->getId();
     if ($model->save()) {
         // Get or create model name id
         $modelMN = new D3filesModelName();
         $model_name_id = $modelMN->getByName($this->modelName, true);
         $modelM = new D3filesModel();
         $modelM->d3files_id = $model->id;
         $modelM->is_file = 1;
         $modelM->model_name_id = $model_name_id;
         $modelM->model_id = $id;
         $modelM->save();
         $fileHandler->rename($model->id);
     } else {
         $fileHandler->remove();
         throw new HttpException(500, Yii::t('d3files', 'Insert DB record failed'));
     }
     $renderParam = ['id' => $model->id, 'file_name' => $model->file_name, 'file_model_id' => $modelM->id];
     return $this->controller->renderFile(Yii::$app->getModule('d3files')->getView('d3files/upload'), $renderParam);
 }
Exemplo n.º 5
0
 public function run($id)
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     if (!($fileModel = D3filesModel::findOne(['id' => $id, 'deleted' => 0]))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     if (!($fileModelName = D3filesModelName::findOne($fileModel->model_name_id))) {
         throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
     }
     /**
      * validate modelname
      */
     if (Yii::$app->getModule('d3files')->disableController) {
         if ($fileModelName->name != $this->modelName) {
             throw new NotFoundHttpException(Yii::t('d3files', 'The requested file does not exist.'));
         }
     }
     // Check access rights to the record the file is attached to
     D3files::performReadValidation($fileModelName->name, $fileModel->model_id);
     $fileModel->deleted = 1;
     $fileModel->save();
     return $this->controller->renderFile(Yii::$app->getModule('d3files')->getView('d3files/delete'));
 }
Exemplo n.º 6
0
 /**
  * @param integer $id D3filesModel ID
  * @param integer $expireDays the period of validity days
  * @param integer $leftLoadings allowed download count
  *
  * @return array [integer D3filesModelShared ID, string hex hash]
  */
 public function createSharedModel($id, $expireDays = null, $leftLoadings = null)
 {
     if (!($hashSalt = Yii::$app->getModule('d3files')->hashSalt)) {
         return false;
     }
     if (!$expireDays && !($expireDays = Yii::$app->getModule('d3files')->sharedExpireDays)) {
         $expireDays = self::SHARED_EXPIRE_DAYS;
     }
     if (!$leftLoadings && !($leftLoadings = Yii::$app->getModule('d3files')->sharedLeftLoadings)) {
         $leftLoadings = self::SHARED_LEFT_LOADINGS;
     }
     if (!($fileModel = D3filesModel::findOne(['id' => $id, 'deleted' => 0, 'is_file' => 1]))) {
         return false;
     }
     if (!($file = D3files::findOne($fileModel->d3files_id))) {
         return false;
     }
     $fileModelShared = new D3filesModelShared();
     $fileModelShared->d3files_model_id = $id;
     $fileModelShared->expire_date = new \yii\db\Expression('DATE_ADD(CURDATE(), INTERVAL ' . $expireDays . ' DAY)');
     $fileModelShared->left_loadings = $leftLoadings;
     $fileModelShared->save();
     $hashText = sprintf('%s:%s:%s', $fileModelShared->id, $file->file_name, $hashSalt);
     $fileModelShared->hash = strtoupper(md5($hashText));
     $fileModelShared->save();
     return ['id' => $fileModelShared->id, 'hash' => $fileModelShared->hash];
 }
Exemplo n.º 7
0
 /**
  * Upload yii\web\UploadedFile
  * @param UploadedFile $uploadFile
  * @param string $modelName model name with name space
  * @param int $modelId
  * @throws Exception
  */
 public static function saveYii2UploadFile(UploadedFile $uploadFile, $modelName, $modelId)
 {
     $fileHandler = new FileHandler(['model_name' => $modelName, 'model_id' => uniqid(), 'file_name' => $uploadFile->name, 'file_types' => '*']);
     $fileHandler->uploadYii2UloadFile($uploadFile);
     $model = new D3files();
     $model->file_name = $uploadFile->name;
     $model->add_datetime = new \yii\db\Expression('NOW()');
     $model->user_id = \Yii::$app->person->user_id;
     if ($model->save()) {
         // Get or create model name id
         $modelMN = new D3filesModelName();
         $model_name_id = $modelMN->getByName($modelName, true);
         $modelM = new D3filesModel();
         $modelM->d3files_id = $model->id;
         $modelM->is_file = 1;
         $modelM->model_name_id = $model_name_id;
         $modelM->model_id = $modelId;
         $modelM->save();
         $fileHandler->rename($model->id);
     } else {
         $fileHandler->remove();
         throw new Exception(500, Yii::t('d3files', 'Insert DB record failed'));
     }
 }
Exemplo n.º 8
0
 public static function readImap(EmailContainerInerface $cc, $containerClass)
 {
     $error = false;
     $tempDirectory = Yii::getAlias('@runtime/temp');
     while ($cc->featchData()) {
         $imapConnection = new ImapConnection();
         $imapConnection->imapPath = $cc->getImapPath();
         $imapConnection->imapLogin = $cc->getUserName();
         $imapConnection->imapPassword = $cc->getPassword();
         $imapConnection->serverEncoding = 'utf-8';
         // utf-8 default.
         $imapConnection->attachmentsDir = $tempDirectory;
         /**
          * connect to IMAP
          */
         try {
             $mailbox = new Mailbox($imapConnection);
         } catch (\Exception $e) {
             \Yii::error('Container class: ' . $containerClass . '; Can not connect to: ' . $imapPath . '; Error: ' . $e->getMessage());
             return false;
         }
         $mailbox->readMailParts = false;
         $mailsIds = $mailbox->searchMailbox('ALL');
         if (!$mailsIds) {
             echo 'Mailbox is empty' . PHP_EOL;
             continue;
         }
         echo 'Messages count:' . count($mailsIds) . PHP_EOL;
         foreach ($mailsIds as $i => $mailId) {
             $msg = $mailbox->getMail($mailId);
             echo $i . ' Subject:' . $msg->subject . PHP_EOL;
             echo $i . ' Date:' . $msg->date . PHP_EOL;
             echo $i . ' MessageId:' . $msg->messageId . PHP_EOL;
             if (D3pop3Email::findOne(['email_id' => $msg->messageId])) {
                 echo $i . ' Message already loaded' . PHP_EOL;
                 continue;
             }
             /**
              * load attachments and bodies
              */
             $msg = $mailbox->getMailParts($msg);
             $email = new D3pop3Email();
             //$msg->date
             $email->email_id = $msg->messageId;
             $email->email_datetime = $msg->date;
             $email->receive_datetime = new \yii\db\Expression('NOW()');
             $email->subject = $msg->subject;
             $email->body = $msg->textHtml;
             $email->body_plain = $msg->textPlain;
             $email->from = $msg->fromAddress;
             $email->from_name = $msg->fromName;
             $email->email_container_class = $containerClass;
             if (!$email->save()) {
                 $errorList = \yii\helpers\Json::encode($email->getErrors());
                 echo $errorList . PHP_EOL;
                 \Yii::error('Container class: ' . $containerClass . '; Can not save email. Error: ' . $errorList);
                 $error = true;
                 continue;
             }
             foreach ($msg->to as $toEmail => $toName) {
                 $ea = new D3pop3EmailAddress();
                 $ea->email_id = $email->id;
                 $ea->address_type = D3pop3EmailAddress::ADDRESS_TYPE_TO;
                 $ea->email_address = $toEmail;
                 $ea->name = $toName;
                 $ea->save();
             }
             foreach ($msg->cc as $ccEmail => $ccName) {
                 $ea = new D3pop3EmailAddress();
                 $ea->email_id = $email->id;
                 $ea->address_type = D3pop3EmailAddress::ADDRESS_TYPE_CC;
                 $ea->email_address = $ccEmail;
                 $ea->name = $ccName;
                 $ea->save();
             }
             foreach ($msg->replyTo as $rtEmail => $rtName) {
                 $ea = new D3pop3EmailAddress();
                 $ea->email_id = $email->id;
                 $ea->address_type = D3pop3EmailAddress::ADDRESS_TYPE_REPLAY;
                 $ea->email_address = $rtEmail;
                 $ea->name = $rtName;
                 $ea->save();
             }
             $attachModelList = $cc->getModelForattach($msg);
             foreach ($attachModelList as $attachModel) {
                 $emailModel = new D3pop3EmailModel();
                 $emailModel->email_id = $email->id;
                 $emailModel->model_name = $attachModel['model_name'];
                 $emailModel->model_id = $attachModel['id'];
                 $emailModel->save();
             }
             $fileTypes = '/(gif|pdf|dat|jpe?g|png|doc|docx|xls|xlsx|htm|txt|log|mxl|xml|zip)$/i';
             /** @var Attachment $t */
             foreach ($msg->getAttachments() as $t) {
                 echo $i . ' A:' . $t->name . PHP_EOL;
                 /**
                  *  save to d3file
                  */
                 try {
                     D3files::saveFile($t->name, D3pop3Email::className(), $email->id, $t->filePath, $fileTypes);
                 } catch (\Exception $e) {
                     $errorMessage = Yii::t('d3pop3', 'Can not save attachment.') . Yii::t('d3pop3', 'Error: ') . $e->getMessage();
                     echo $errorMessage . PHP_EOL;
                     $error = new D3pop3EmailError();
                     $error->email_id = $email->id;
                     $error->message = $errorMessage;
                     $error->save();
                 }
                 //unlink($t->filePath);
             }
             echo PHP_EOL;
         }
     }
     /**
      * remove all attachment files
      */
     $files = FileHelper::findFiles($tempDirectory);
     foreach ($files as $f) {
         unlink($f);
     }
     return !$error;
 }