public function post_upload_media()
 {
     $auth = Auth::check();
     if ($auth) {
         $input = Input::all();
         $file_name = strtolower(preg_replace('/[^\\w\\._]+/', '_', $input['file']['name']));
         $file_name = str_replace('.jpeg', '.jpg', $file_name);
         $file_size = $input['file']['size'];
         $file_ext = strtolower(File::extension($file_name));
         $page_id = $input['page_id'];
         $file_w = 0;
         $file_h = 0;
         //CREATE UPLOAD PATH
         $img_mimes = array('jpg', 'jpeg', 'gif', 'png');
         if (in_array($file_ext, $img_mimes)) {
             $up = 'img/';
             $is_image = true;
         } else {
             $up = $file_ext . '/';
             $is_image = false;
         }
         //BUILD UPLOAD PATH
         $upload_path = Config::get('cms::settings.data') . $up;
         //VALIDATION CHECK
         $get_mimes = str_replace(' ', '', Config::get('cms::settings.mimes'));
         $get_max = Config::get('cms::settings.max_size') * 1024000;
         //1Mb
         // CHECK SIZE EXCEEDS
         $exceeds = (!$is_image and $file_size > $get_max) ? true : false;
         // CHECK SIZE IF IMAGE
         if ($is_image) {
             $rules = array('page_id' => 'required', 'file' => 'mimes:' . $get_mimes . '|max:' . $get_max . '|unique_file:' . $file_name . ',' . $upload_path);
         } else {
             $rules = array('page_id' => 'required', 'file' => 'mimes:' . $get_mimes . '|unique_file:' . $file_name . ',' . $upload_path);
         }
         $messages = array('required' => LL('cms::validation.page_not_set', CMSLANG)->get(), 'mimes' => LL('cms::validation.mimes_not_valid', CMSLANG)->get(), 'max' => LL('cms::validation.max_file_size', CMSLANG)->get(), 'unique_file' => LL('cms::validation.unique_file', CMSLANG)->get());
         $validation = Validator::make($input, $rules, $messages);
         if ($validation->fails()) {
             //ERROR RESPONSE
             return json_encode(array('type' => 'label-important', 'message' => $validation->errors->first()));
         } else {
             //CREATE DIR IF NOT EXISTS
             if (!file_exists(path('public') . $upload_path)) {
                 mkdir(path('public') . $upload_path);
             }
             $path = strtolower($upload_path . $file_name);
             // DO UPLOAD ONLY IF IS IMAGE OR LOWER THAN MAX SIZE ALLOWED
             $status_upload = !$exceeds ? move_uploaded_file($input['file']['tmp_name'], path('public') . $path) : true;
             // UPLOAD OK
             if ($status_upload) {
                 //SET ICO PATH
                 $thumb_path = '/bundles/cms/img/' . $file_ext . '_ico.png';
                 //GET IMG DIMENSIONS
                 if ($is_image) {
                     $img = getimagesize(path('public') . $path);
                     $file_w = $img[0];
                     $file_h = $img[1];
                     //GENERATE THUMBS
                     $thumb_path = CmsFile::create_thumb($upload_path, $file_name, $file_ext);
                 }
                 //SAVE TO DB
                 $file_attr = array('name' => $file_name, 'ext' => $file_ext, 'size' => $file_size, 'w' => $file_w, 'h' => $file_h, 'path' => '/' . $path, 'thumb' => $thumb_path, 'is_image' => $is_image ? 1 : 0, 'is_valid' => 1);
                 $file = new CmsFile($file_attr);
                 $page = CmsPage::find($page_id);
                 $page->files()->insert($file);
                 // SUCCESS RESPONSE
                 $resp = array('type' => $exceeds ? 'label-info' : 'label-success', 'message' => $exceeds ? LL('cms::ajax_resp.page_upload_ftp', CMSLANG, array('path' => $upload_path))->get() : LL('cms::ajax_resp.page_upload_success', CMSLANG)->get(), 'name' => $file_name, 'path' => '/' . $path, 'thumb_path' => $thumb_path, 'is_img' => $is_image ? true : false);
                 return json_encode($resp);
             } else {
                 // ERROR RESPONSE
                 $resp = array('type' => 'label-important', 'message' => LL('cms::ajax_resp.page_upload_error', CMSLANG)->get());
                 return json_encode($resp);
             }
         }
     }
 }