Exemplo n.º 1
0
 /**
  * This function will make directory writable.
  *
  * @param   string  path
  * @return  void
  * @throw   Kohana_Exception
  */
 public static function make_writable($path, $chmod = NULL)
 {
     try {
         $dir = new SplFileInfo($path);
         if ($dir->isFile()) {
             throw new Kohana_Exception('Could not make :path writable directory because it is regular file', array(':path' => Debug::path($path)));
         } elseif ($dir->isLink()) {
             throw new Kohana_Exception('Could not make :path writable directory because it is link', array(':path' => Debug::path($path)));
         } elseif (!$dir->isDir()) {
             // Try create directory
             Ku_Dir::make($path, $chmod);
             clearstatcache(TRUE, $path);
         }
         if (!$dir->isWritable()) {
             // Try make directory writable
             chmod($dir->getRealPath(), $chmod === NULL ? Ku_Dir::$default_dir_chmod : $chmod);
             clearstatcache(TRUE, $path);
             // Check result
             if (!$dir->isWritable()) {
                 throw new Exception('Make dir writable failed', 0);
             }
         }
     } catch (Kohana_Exception $e) {
         // Rethrow exception
         throw $e;
     } catch (Exception $e) {
         throw new Kohana_Exception('Could not make :path directory writable', array(':path' => Debug::path($path)));
     }
 }
Exemplo n.º 2
0
 private function write_to_file($file_name, $str)
 {
     $file_name = str_replace('/', DIRECTORY_SEPARATOR, $file_name);
     if (strpos($file_name, DOCROOT) !== 0) {
         $file_name = DOCROOT . $file_name;
     }
     $dirname = dirname($file_name);
     if (!file_exists($dirname)) {
         Ku_Dir::make($dirname);
     }
     Ku_Dir::make_writable($dirname);
     $handle = fopen($file_name, 'w');
     fwrite($handle, $str);
     fclose($handle);
 }
Exemplo n.º 3
0
 public function action_upload()
 {
     $this->auto_render = FALSE;
     $request = $this->request->current();
     $post = $request->post();
     $album_id = (int) Arr::get($post, 'album');
     $to_head = Arr::get($post, 'to_head') === 'true';
     $album_orm = ORM::factory('photo_Album')->where('id', '=', $album_id)->find();
     if (!$album_orm->loaded() or !$this->acl->is_allowed($this->user, $album_orm, 'edit')) {
         throw new HTTP_Exception_404();
     }
     $response = array('jsonrpc' => '2.0', 'id' => 'id');
     /* $target_dir */
     $target_dir = str_replace('/', DIRECTORY_SEPARATOR, DOCROOT . Kohana::$config->load('_photo.multiupload_dir'));
     if (!is_dir($target_dir)) {
         Ku_Dir::make($target_dir, 0755);
     }
     if (is_dir($target_dir) && ($dir = opendir($target_dir))) {
         while (($file = readdir($dir)) !== false) {
             $tmp_file_path = $target_dir . DIRECTORY_SEPARATOR . $file;
             /* Remove temp file if it is older than the max age and is not the current file */
             if (preg_match('/\\.part$/', $file) and filemtime($tmp_file_path) < time() - $this->max_file_age and $tmp_file_path != "{$file_path}.part") {
                 @unlink($tmp_file_path);
             }
         }
         closedir($dir);
     } else {
         $response['error'] = array('code' => 100, 'message' => 'Failed to open temp directory.');
         $this->json_send($response);
         return;
     }
     /* $chunk, $chunks */
     $chunk = Arr::get($post, 'chunk', 0);
     $chunks = Arr::get($post, 'chunks', 0);
     /* $file_name */
     $file_name = Arr::get($post, 'name', '');
     $file_name = preg_replace('/[^\\w\\._]+/', '_', $file_name);
     $ext = UTF8::strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
     if (!preg_match('/^jpe?g$/s', $ext)) {
         $response['error'] = array('code' => 105, 'message' => 'Invalid file type.');
         $this->json_send($response);
         return;
     }
     if ($chunks < 2 and file_exists($target_dir . DIRECTORY_SEPARATOR . $file_name)) {
         $ext = strrpos($file_name, '.');
         $file_name_a = substr($file_name, 0, $ext);
         $file_name_b = substr($file_name, $ext);
         $count = 1;
         while (file_exists($target_dir . DIRECTORY_SEPARATOR . $file_name_a . '_' . $count . $file_name_b)) {
             $count++;
         }
         $file_name = $file_name_a . '_' . $count . $file_name_b;
     }
     /* $file_path */
     $file_path = $target_dir . DIRECTORY_SEPARATOR . $file_name;
     $_h = $request->headers('http-content-type');
     $content_type = empty($_h) ? '' : $_h;
     $_h = $request->headers('content-type');
     $content_type = empty($_h) ? $content_type : $_h;
     /* Handle non multipart uploads older WebKit versions didn't support multipart in HTML5 */
     if (strpos($content_type, "multipart") !== false) {
         if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
             if ($out = fopen("{$file_path}.part", $chunk == 0 ? "wb" : "ab")) {
                 if ($in = fopen($_FILES['file']['tmp_name'], "rb")) {
                     while ($buff = fread($in, 4096)) {
                         fwrite($out, $buff);
                     }
                 } else {
                     $response['error'] = array('code' => 101, 'message' => 'Failed to open input stream.');
                     $this->json_send($response);
                     return;
                 }
                 fclose($in);
                 fclose($out);
                 @unlink($_FILES['file']['tmp_name']);
             } else {
                 $response['error'] = array('code' => 102, 'message' => 'Failed to open output stream.');
                 $this->json_send($response);
                 return;
             }
         } else {
             $response['error'] = array('code' => 103, 'message' => 'Failed to move uploaded file.');
             $this->json_send($response);
             return;
         }
     } else {
         if ($out = fopen("{$file_path}.part", $chunk == 0 ? "wb" : "ab")) {
             if ($in = fopen("php://input", "rb")) {
                 while ($buff = fread($in, 4096)) {
                     fwrite($out, $buff);
                 }
             } else {
                 $response['error'] = array('code' => 101, 'message' => 'Failed to open input stream.');
                 $this->json_send($response);
                 return;
             }
             fclose($in);
             fclose($out);
         } else {
             $response['error'] = array('code' => 102, 'message' => 'Failed to open output stream.');
             $this->json_send($response);
             return;
         }
     }
     /* Check if file has been uploaded */
     if (!$chunks or $chunk == $chunks - 1) {
         /* Strip the temp .part suffix off */
         rename("{$file_path}.part", $file_path);
         $save_result = $this->save_file($file_path, $album_orm, $to_head);
         if ($save_result !== TRUE) {
             $response['error'] = array('code' => 104, 'message' => $save_result);
             $this->json_send($response);
             return;
         }
     }
     /* Return JSON-RPC response */
     $response['result'] = NULL;
     $this->json_send($response);
 }