コード例 #1
0
ファイル: Doc.php プロジェクト: huynt57/bluebee-uet.com
 protected function beforeValidate()
 {
     if (!parent::beforeValidate()) {
         return false;
     }
     if ($this->scenario == "fromAdmin") {
         if ($this->doc_path_file == NULL) {
             $this->addError("doc_path", "doc_path is required");
             return false;
         }
         if (!$this->doc_path_file->checkExt(array('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'jpg', 'jpeg', 'png'))) {
             return FALSE;
         }
         $api_key = "24cxjtv3vw69wu5p7pqd9";
         $secret = "sec-b2rlvg8kxwwpkz9fo3i02mo9vo";
         $scribd = new Scribd($api_key, $secret);
         $ext = $this->doc_path_file->getExtension();
         $name = StringHelper::unicode_str_filter($this->doc_path_file->name);
         $storeFolder = Yii::getPathOfAlias('webroot') . '/uploads/document/user_id_' . $this->doc_author;
         //2
         $targetPath = $storeFolder;
         //4
         $doc_path = Yii::app()->createAbsoluteUrl('uploads') . '/document/user_id_' . $this->doc_author . '/' . $name;
         $targetFile = $targetPath . "/" . $name;
         //5
         // move_uploaded_file($tempFile, $targetFile); //6
         if (!file_exists($storeFolder)) {
             mkdir($storeFolder, 0777, true);
         }
         if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "pjepg" || $ext == "png" || $ext == "x-png" || $ext == "GIF" || $ext == "JPG" || $ext == "JPEG" || $ext == "PJEPG" || $ext == "PNG" || $ext == "X_PNG") {
             $this->doc_path_file->save($targetPath, $name);
             $this->doc_url = $targetFile;
             $this->doc_path = $doc_path;
             $this->doc_type = 1;
         } else {
             if ($ext == "doc" || $ext == "docx" || $ext == "ppt" || $ext == "pptx" || $ext == "xls" || $ext == "xlsx" || $ext == 'txt' || $ext == 'pdf') {
                 $this->doc_path_file->save($targetPath, $name);
                 $upload_scribd = @$scribd->upload($targetFile);
                 //                var_dump($targetFile);
                 //                die();
                 $thumbnail_info = array('doc_id' => $upload_scribd["doc_id"], 'method' => NULL, 'session_key' => NULL, 'my_user_id' => NULL, 'width' => '220', 'height' => '250');
                 $get_thumbnail = @$scribd->postRequest('thumbnail.get', $thumbnail_info);
                 $this->doc_scribd_id = @$upload_scribd["doc_id"];
                 $this->doc_url = @$get_thumbnail["thumbnail_url"];
                 $this->doc_path = $doc_path;
                 $this->doc_type = 2;
             } else {
                 $this->doc_path_file->save($targetPath, $name);
                 $url_file_image = Yii::app()->theme->baseUrl . '/assets/img/document.png';
                 $this->doc_url = $url_file_image;
                 $this->doc_path = $doc_path;
                 $this->doc_type = 3;
             }
         }
     }
     return TRUE;
 }
コード例 #2
0
 public function actionUpload()
 {
     //$ds = DIRECTORY_SEPARATOR;  //1
     //    $cnt = DocumentController::$cnt++;
     $subject_id = StringHelper::filterString($_POST['subject_id']);
     $size = 100 * 1024 * 1024;
     $doc_name = StringHelper::filterString($_POST['doc_name']);
     $doc_description = StringHelper::filterString($_POST['doc_description']);
     $doc_author = Yii::app()->session['user_id'];
     $doc_author_name = Yii::app()->session['user_name'];
     $api_key = "24cxjtv3vw69wu5p7pqd9";
     $secret = "sec-b2rlvg8kxwwpkz9fo3i02mo9vo";
     $this->retVal = new stdClass();
     if ($_FILES['file']) {
         if ($doc_name != "") {
             if ($doc_description != "") {
                 if ($subject_id != "") {
                     if ($_FILES['file']['size'] <= $size) {
                         $scribd = new Scribd($api_key, $secret);
                         $name = $this->unicode_str_filter($_FILES['file']['name']);
                         $storeFolder = Yii::getPathOfAlias('webroot') . '/uploads/document/user_id_' . $doc_author . '/';
                         //2
                         if (!file_exists($storeFolder)) {
                             mkdir($storeFolder, 0777, true);
                         }
                         $tempFile = $_FILES['file']['tmp_name'];
                         //3
                         $targetPath = $storeFolder;
                         //4
                         $targetFile = $targetPath . $name;
                         //5
                         $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
                         move_uploaded_file($tempFile, $targetFile);
                         //6
                         $doc_path = Yii::app()->createAbsoluteUrl('uploads') . '/document/user_id_' . $doc_author . '/' . $name;
                         if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "pjepg" || $ext == "png" || $ext == "x-png") {
                             $this->saveDoc($doc_name, $doc_description, $doc_path, $doc_author, $subject_id, NULL, 1, $doc_path, $doc_author_name);
                             $this->retVal->url = $targetFile;
                             $this->retVal->doc_name = $doc_name;
                             $this->retVal->doc_path = $doc_path;
                             $this->retVal->user_name = Yii::app()->session['user_name'];
                             $this->retVal->success = 1;
                         } else {
                             if ($ext == "doc" || $ext == "docx" || $ext == "ppt" || $ext == "pptx" || $ext == "xls" || $ext == "xlsx" || $ext == 'txt' || $ext == 'pdf') {
                                 $upload_scribd = @$scribd->upload($targetFile);
                                 $thumbnail_info = array('doc_id' => $upload_scribd["doc_id"], 'method' => NULL, 'session_key' => NULL, 'my_user_id' => NULL, 'width' => '220', 'height' => '250');
                                 $get_thumbnail = @$scribd->postRequest('thumbnail.get', $thumbnail_info);
                                 // var_dump($get_thumbnail);
                                 $this->saveDoc($doc_name . '.' . $ext, $doc_description, @$get_thumbnail["thumbnail_url"], $doc_author, $subject_id, $upload_scribd["doc_id"], 2, $doc_path, $doc_author_name);
                                 $this->retVal->docid = @$upload_scribd["doc_id"];
                                 $this->retVal->thumbnail = @$get_thumbnail["thumbnail_url"];
                                 $this->retVal->doc_name = $doc_name;
                                 $this->retVal->doc_path = $doc_path;
                                 $this->retVal->user_name = Yii::app()->session['user_name'];
                                 $this->retVal->success = 1;
                             } else {
                                 if ($ext == "html" || $ext == "php" || $ext == "htaccess" || $ext == "js") {
                                     $this->retVal->info = "File không được hỗ trợ";
                                     $this->retVal->success = 0;
                                 } else {
                                     $url_file_image = Yii::app()->theme->baseUrl . '/assets/img/document.png';
                                     $this->saveDoc($doc_name . "." . $ext, $doc_description, $url_file_image, $doc_author, $subject_id, NULL, 3, $doc_path, $doc_author_name);
                                     $this->retVal->doc_url = $url_file_image;
                                     $this->retVal->doc_name = $doc_name;
                                     $this->retVal->doc_path = $doc_path;
                                     $this->retVal->user_name = Yii::app()->session['user_name'];
                                     $this->retVal->success = 1;
                                 }
                             }
                         }
                     } else {
                         $this->retVal->message = "Bạn không thể upload file nặng quá 8MB";
                         $this->retVal->success = 0;
                     }
                 } else {
                     $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                     $this->retVal->success = 0;
                 }
             } else {
                 $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                 $this->retVal->success = 0;
             }
         } else {
             $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
             $this->retVal->success = 0;
         }
     } else {
         $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
         $this->retVal->success = 0;
     }
     echo CJSON::encode($this->retVal);
     Yii::app()->end();
 }
コード例 #3
0
    public function actionUpload()
    {
        //$ds = DIRECTORY_SEPARATOR;  //1
        $subject_id = $_POST['subject_id'];
        $size = 100 * 1024 * 1024;
        $doc_name = StringHelper::filterString($_POST['doc_name']);
        $doc_description = StringHelper::filterString($_POST['doc_description']);
        $doc_author = $_POST['user_id'];
        $doc_author_name = $_POST['username'];
        $api_key = "24cxjtv3vw69wu5p7pqd9";
        $secret = "sec-b2rlvg8kxwwpkz9fo3i02mo9vo";
        $this->retVal = new stdClass();
        if ($_FILES['file']) {
            // print_r ($_FILES['file']);
            if ($doc_name != "") {
                if ($doc_description != "") {
                    if ($subject_id != "") {
                        if ($_FILES['file']['size'] <= $size) {
                            $scribd = new Scribd($api_key, $secret);
                            $name = StringHelper::unicode_str_filter($_FILES['file']['name']);
                            $storeFolder = Yii::getPathOfAlias('webroot') . '/uploads/document/user_id_' . $doc_author . '/';
                            //2
                            if (!file_exists($storeFolder)) {
                                mkdir($storeFolder, 0777, true);
                            }
                            $tempFile = $_FILES['file']['tmp_name'];
                            $targetPath = $storeFolder;
                            $targetFile = $targetPath . $name;
                            $ourFileName = $storeFolder . ".htaccess";
                            $myfile = fopen($ourFileName, "w") or die("Unable to open file!");
                            $txt = 'Options -Indexes
Options -ExecCGI
AddHandler cgi-script .php .php3 .php4 .phtml .pl .py .jsp .asp .htm .shtml .sh .cgi .js .html';
                            fwrite($myfile, $txt);
                            fclose($myfile);
                            $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
                            $ext = strtolower($ext);
                            $doc_path = Yii::app()->createAbsoluteUrl('uploads') . '/document/user_id_' . $doc_author . '/' . $name;
                            if ($ext == "gif" || $ext == "jpg" || $ext == "jpeg" || $ext == "pjepg" || $ext == "png" || $ext == "x-png" || $ext == "GIF" || $ext == "JPG" || $ext == "JPEG" || $ext == "PJEPG" || $ext == "PNG" || $ext == "X_PNG") {
                                move_uploaded_file($tempFile, $targetFile);
                                //6
                                $this->saveDoc($doc_name, $doc_description, $doc_path, $doc_author, $subject_id, NULL, 1, $doc_path, $doc_author_name);
                                $this->retVal->url = $targetFile;
                                $this->retVal->doc_name = $doc_name;
                                $this->retVal->doc_path = $doc_path;
                                $this->retVal->user_name = $doc_author_name;
                                $this->retVal->success = 1;
                            } else {
                                if ($ext == "doc" || $ext == "docx" || $ext == "ppt" || $ext == "pptx" || $ext == "xls" || $ext == "xlsx" || $ext == 'txt' || $ext == 'pdf') {
                                    move_uploaded_file($tempFile, $targetFile);
                                    //6
                                    $upload_scribd = @$scribd->upload($targetFile);
                                    $thumbnail_info = array('doc_id' => $upload_scribd["doc_id"], 'method' => NULL, 'session_key' => NULL, 'my_user_id' => NULL, 'width' => '220', 'height' => '250');
                                    $get_thumbnail = @$scribd->postRequest('thumbnail.get', $thumbnail_info);
                                    // var_dump($get_thumbnail);
                                    $this->saveDoc($doc_name . '.' . $ext, $doc_description, @$get_thumbnail["thumbnail_url"], $doc_author, $subject_id, $upload_scribd["doc_id"], 2, $doc_path, $doc_author_name);
                                    $this->retVal->docid = @$upload_scribd["doc_id"];
                                    $this->retVal->thumbnail = @$get_thumbnail["thumbnail_url"];
                                    $this->retVal->doc_name = $doc_name;
                                    $this->retVal->doc_path = $doc_path;
                                    $this->retVal->user_name = $doc_author_name;
                                    $this->retVal->success = 1;
                                } else {
                                    if ($ext == "rar" || $ext == "zip" || $ext == "iso") {
                                        move_uploaded_file($tempFile, $targetFile);
                                        //6
                                        $url_file_image = Yii::app()->theme->baseUrl . '/assets/img/document.png';
                                        $this->saveDoc($doc_name . "." . $ext, $doc_description, $url_file_image, $doc_author, $subject_id, NULL, 3, $doc_path, $doc_author_name);
                                        $this->retVal->doc_url = $url_file_image;
                                        $this->retVal->doc_name = $doc_name;
                                        $this->retVal->doc_path = $doc_path;
                                        $this->retVal->user_name = $doc_author_name;
                                        $this->retVal->success = 1;
                                    } else {
                                        $this->retVal->message = "File không được hỗ trợ";
                                        $this->retVal->success = 0;
                                    }
                                }
                            }
                        } else {
                            $this->retVal->message = "Bạn không thể upload file nặng quá 8MB";
                            $this->retVal->success = 0;
                        }
                    } else {
                        $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                        $this->retVal->success = 0;
                    }
                } else {
                    $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                    $this->retVal->success = 0;
                }
            } else {
                $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
                $this->retVal->success = 0;
            }
        } else {
            $this->retVal->message = "Bạn phải nhập đầy đủ các thông tin";
            $this->retVal->success = 0;
        }
        echo CJSON::encode($this->retVal);
        Yii::app()->end();
    }