Example #1
0
 /**
  * ให้ค่า url สำหรับเรียกใช้งาน  document
  * @return string
  */
 public function getPublishUri($options = array())
 {
     $encodeId = $this->refId;
     if (is_numeric($this->refId)) {
         $encodeId = strpos($this->refId, '.') ? strval($this->refId) : intval($this->refId);
     }
     $params = array(self::ENCODE_ENTITY => (int) $this->type, self::ENCODE_ID => $encodeId, self::ENCODE_ITEMNO => (int) $this->itemNo) + $options;
     $enc = new TrEnc(\Yii::$app->params['crypto'][0][0], \Yii::$app->params['crypto'][0][1]);
     $arr = explode(".", $this->fileName);
     $extension = $arr[count($arr) - 1];
     $retVal = $enc->encode($params) . '.' . $extension;
     return $retVal;
 }
 public function actionView()
 {
     $request = \Yii::$app->request;
     $enc = new TrEnc(\Yii::$app->params['crypto'][0][0], \Yii::$app->params['crypto'][0][1]);
     $encrypted = substr($request->get('key'), 0, strrpos($request->get('key'), '.'));
     $params = $enc->decode($encrypted);
     $flag = $request->get('falg', '');
     if ($flag) {
         switch ($flag) {
             case '@':
                 $this->displayParams($params);
                 break;
         }
         return;
     }
     $model = new Document();
     $document = $model->findByParams($params);
     // check for view permission
     if ($document == null) {
         throw new HttpException(404);
     }
     $entity = Entity::getInstance($document->type, $document->refId);
     if ($entity == null) {
         throw new HttpException(404);
     }
     $filePath = \Yii::$app->params['document.basePath'] . Entity::$arrType[$document->type] . '/' . $document->srcPath;
     if (!is_readable($filePath)) {
         $filePath = \Yii::$app->params['document.basePath2'] . Entity::$arrType[$document->type] . '/' . $document->srcPath;
     }
     if (!file_exists($filePath)) {
         throw new HttpException(404);
     }
     $response = \Yii::$app->getResponse();
     $response->headers->set('Content-Type', $document->mime);
     $response->format = \yii\web\Response::FORMAT_RAW;
     echo file_get_contents($filePath);
 }