Are you a regular stikked user? Signup so you can keep track of your pastes!
  1. <?php
  2.  
  3. require(APPPATH.'/libraries/REST_Controller.php');
  4.  
  5. class File extends REST_Controller
  6. {
  7.         function get()
  8.         {
  9.     $this->load->model('file_model','file');
  10.  
  11.     if(isset($_REQUEST["stream"])){
  12.       $this->file->stream();
  13.       exit;
  14.     }
  15.     else if(isset($_REQUEST["download"])){
  16.       $this->file->download();
  17.       exit;
  18.     }
  19.     else if(isset($_REQUEST["image_gallery_pdf"])){
  20.       return readfile($this->file->image_gallery_pdf());
  21.       exit;
  22.     }
  23.     else if(isset($_REQUEST["detail_page_pdf"])){
  24.       return readfile($this->file->detail_page_pdf());
  25.       exit;
  26.     }
  27.     else if(isset($_REQUEST["all_files_as_zip"])){
  28.       return readfile($this->file->files_as_zip($_REQUEST['adjustment_id']));
  29.       exit;
  30.     }
  31.  
  32.     $file = $this->file->run_api();
  33.     $this->total_count =  $this->file->total_count;
  34.     $this->success = TRUE;    
  35.     $this->message = 'Successfully Grabbed file Info.';
  36.  
  37.     $this->response($file, 200); // 200 being the HTTP response code
  38.         }
  39.        
  40.         function post()
  41.         {
  42.         // Settings
  43.         $target_dir = UPLOAD_PATH . DIRECTORY_SEPARATOR . "plupload";
  44.         $cleanuptarget_dir = false; // Remove old files
  45.         $maxFileAge = 60 * 60; // Temp file age in seconds
  46.  
  47.         // 5 minutes execution time
  48.         @set_time_limit(5 * 60);
  49.         // usleep(5000);
  50.  
  51.         // Get parameters
  52.         $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
  53.         $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
  54.         $file_name = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
  55.  
  56.         // Clean the file_name for security reasons
  57.         $file_name = preg_replace('/[^\w\._]+/', '', $file_name);
  58.  
  59.         // Create target dir
  60.         if (!file_exists($target_dir))
  61.                 @mkdir($target_dir);
  62.  
  63.         // Remove old temp files
  64.         if (is_dir($target_dir) && ($dir = opendir($target_dir))) {
  65.                 while (($file = readdir($dir)) !== false) {
  66.                         $filePath = $target_dir . DIRECTORY_SEPARATOR . $file;
  67.  
  68.                         // Remove temp files if they are older than the max age
  69.                         if (preg_match('/\\.tmp$/', $file) && (filemtime($filePath) < time() - $maxFileAge))
  70.                                 @unlink($filePath);
  71.                 }
  72.  
  73.                 closedir($dir);
  74.         } else
  75.                 die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  76.        
  77.         if(file_exists($target_dir . DIRECTORY_SEPARATOR . $file_name))
  78.       die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "File already Uploaded."}, "id" : "id"}');
  79.                
  80.         $this->load->helper('file');
  81.     $this->load->model('file_model','file');
  82.     $this->load->library('cj');
  83.     $adjustment_id = $_POST['adjustment_id'];
  84.    
  85.     $linked_type = $_REQUEST['linked_type'];
  86.     $linked_id = $_REQUEST['linked_id'] != 'undefined'?$_REQUEST['linked_id']:$adjustment_id;
  87.    
  88.     $this->load->model('adjustment_model','adj');
  89.     $this->adj->load_data($adjustment_id);
  90.    
  91.     $company_id = $this->adj->get('company_id');
  92.     $adj_number = $this->adj->get('adj_number');
  93.     $date_time_created = $this->adj->get('date_time_created');
  94.    
  95.     $file_name_details = pathinfo($file_name);
  96.    
  97.     $file_extension = $file_name_details['extension'];
  98.    
  99.     if($this->cj->contains('png,gif,jpg,jpeg',$file_extension)){
  100.       $thumbnail_source = $_REQUEST['file']['tmp_name'];
  101.      
  102.       $file_type_folder = 'images';
  103.      
  104.     } else {
  105.       switch($file_extension){
  106.         case 'dds':
  107.         case 'doc':
  108.         case "pdf":
  109.           $file_type_folder = 'documents';
  110.           break;
  111.         case "mp3":
  112.           $file_type_folder = 'audio';
  113.           break;
  114.         default:
  115.           $file_type_folder = 'documents';
  116.           break;
  117.       }
  118.     }
  119.  
  120.         if (!isset($_SERVER['PHP_AUTH_DIGEST']) OR isset($_REQUEST['multipart']) AND $_REQUEST['multipart'] == TRUE) {
  121.                 if (isset($_REQUEST['file']['tmp_name'])) {
  122.                   var $temp_name = $_REQUEST['file']['tmp_name'];
  123.                         // Open temp file
  124.                         $out = fopen($target_dir . DIRECTORY_SEPARATOR . $file_name, $chunk == 0 ? "wb" : "ab");
  125.                         if ($out) {
  126.                                 // Read binary input stream and append it to temp file
  127.                                 $in = fopen($_REQUEST['file']['tmp_name'], "rb");
  128.                                
  129.                                 $file_information = get_file_info($_REQUEST['file']['tmp_name']);
  130.           $file_length = $file_information['size'];
  131.  
  132.           if($file_length == 0) die("Error:The file is corrupt.  Please fix and re-upload.:".$file_name);
  133.  
  134.                                 if ($in) {
  135.                                         while ($buff = fread($in, 4096))
  136.                                                 fwrite($out, $buff);
  137.                                 } else
  138.                                         die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  139.  
  140.                                 fclose($out);
  141.                                 unlink($_REQUEST['file']['tmp_name']);
  142.                         } else
  143.                                 die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  144.                 } else
  145.                         die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  146.         } else {
  147.                 // Open temp file
  148.                 $out = fopen($target_dir . DIRECTORY_SEPARATOR . $file_name, $chunk == 0 ? "wb" : "ab");
  149.                 if ($out) {
  150.                         // Read binary input stream and append it to temp file
  151.                        
  152.                         fwrite($out, $_REQUEST['file_contents']);
  153.         fclose($out);
  154.  
  155.                 } else
  156.                         die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  157.         }
  158.  
  159.         // Return JSON-RPC response
  160.         die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
  161.         }
  162.        
  163.         function put()
  164.         {
  165.         $this->load->library('cj');
  166.     $this->load->model('file_model','file');
  167.     $this->file->load_data($_POST['id']);
  168.    
  169.     $is_valid = $this->file->get_form();
  170.        
  171.     if($is_valid){
  172.      
  173.       $this->file->save();
  174.      
  175.       $this->total_count = 1;
  176.       $this->success = TRUE;    
  177.       $this->message = 'Updated File.';
  178.       $this->response($this->file->data, 200);
  179.     } else {
  180.       $this->total_count = 0;
  181.       $this->success = FALSE;    
  182.       $this->message = 'There were errors in the form.';
  183.       $this->response($this->file->errors, 200);
  184.     }
  185.   }
  186.        
  187.         function delete()
  188.         {
  189.           $db_common = DB_COMMON;
  190.     $db_adjustlink = DB_ADJUST;
  191.    
  192.           $this->load->model('file_model','file');
  193.  
  194.  
  195.           if(isset($_REQUEST['id'])){
  196.             $arFiles = explode("|",$_REQUEST['id']);
  197.             foreach($arFiles as $i => $file_id){
  198.               $file = $this->file->get_file_details($file_id);
  199.               $path = UPLOAD_PATH.$file['company_id'].'/'.date('Y/m',strtotime($file['date_time_created'])).'/'.$file['adj_number'].'/'.$file['type'].'/'.$file['name'];
  200.               if(file_exists($path)){
  201.                 unlink($path);
  202.             }
  203.     @unlink(UPLOAD_PATH.$file['company_id'].'/'.date('Y/m',strtotime($file['date_time_created'])).'/'.$file['adj_number'].'/'.$file['type'].'/'.(str_replace(".".$file['extension'],"_thumb.".$file['extension'],$file['name'])));
  204.     @unlink(UPLOAD_PATH.$file['company_id'].'/'.date('Y/m',strtotime($file['date_time_created'])).'/'.$file['adj_number'].'/'.$file['type'].'/'.(str_replace(".".$file['extension'],"_sm.".$file['extension'],$file['name'])));
  205.               $this->db->delete("$db_adjustlink.file", array('id' => $file_id));
  206.             }
  207.           }
  208.         print_r(json_encode(array('success'=>isset($_REQUEST['id'])?TRUE:FALSE)));
  209.         }
  210.        
  211.         private function _image_resize($src_path, $dest_path, $width = 150, $quality = 80){
  212.  
  213.          // image src size
  214.     list($width_orig, $height_orig, $image_type)  = getImageSize($src_path);
  215.  
  216.     if($width_orig<$width) return; //if width is smaller than current width don't resize
  217.  
  218.     $height = (int) (($width / $width_orig) * $height_orig);
  219.  
  220.   //  print IMAGEMAGICK_CONVERT_PATH." ".$src_path." -quality ".$quality." -resize ".$width."x".$height." ".fixPath($dest_path); exit;
  221.     exec(IMAGEMAGICK_CONVERT_PATH." ".$src_path." -quality ".$quality." -resize ".$width."x".$height." ".$dest_path);
  222.  
  223.   }
  224.        
  225. }
  226. ?>

Reply to "file.php"

Here you can reply to the paste above

Create a snipurl

Make Private

Feeling clever? Set some advanced options.