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(); }
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(); }
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')); }
/** * @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]; }
public function init() { parent::init(); D3Files::registerTranslations(); $this->fileName = D3filesModel::findOne($this->fileModelId)->getD3files()->one()->file_name; }