Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/vendors/Textpattern/Server/Files.php - 132 lines - 3542 bytes - Summary - Text - Print

Description: Treat file uploads.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Treat file uploads.
  26   *
  27   * <code>
  28   * Txp::get('\Textpattern\Server\Files')->refactor($_FILES['thefile']);
  29   * </code>
  30   *
  31   * @since   4.7.0
  32   * @package Server
  33   */
  34  
  35  namespace Textpattern\Server;
  36  
  37  class Files
  38  {
  39      /**
  40       * Temporary chunked upload directory.
  41       *
  42       * @var string
  43       */
  44  
  45      private $tempdir = false;
  46  
  47      /**
  48       * Constructor.
  49       */
  50  
  51      public function __construct()
  52      {
  53          global $tempdir;
  54  
  55          $this->tempdir = (!empty($tempdir) ? $tempdir : ini_get('upload_tmp_dir')) or $this->tempdir = sys_get_temp_dir();
  56      }
  57  
  58      /**
  59       * Transforms a multiple $_FILES entry into int-indexed array.
  60       *
  61       * @param  array $file The file
  62       * @return array of files
  63       */
  64  
  65      public function refactor(&$file)
  66      {
  67          $is_array = is_array($file['name']);
  68  
  69          if (empty($file['name']) || $is_array && empty($file['name'][0])) {
  70              return array();
  71          }
  72  
  73          $file_array = array();
  74          $file_count = $is_array ? count($file['name']) : 1;
  75          $file_keys = array_keys($file);
  76  
  77          for ($i = 0; $i < $file_count; $i++) {
  78              $file_array[$i] = array();
  79  
  80              foreach ($file_keys as $key) {
  81                  $file_array[$i][$key] = $is_array ? $file[$key][$i] : $file[$key];
  82              }
  83          }
  84  
  85          return $file_array;
  86      }
  87  
  88      /**
  89       * Treats chunked file uploads.
  90       *
  91       * @param  array $file The file
  92       * @return bool
  93       */
  94  
  95      public function dechunk(&$file)
  96      {
  97          global $txp_user;
  98          // Chuncked upload, anyone?
  99          if (!empty($_SERVER['HTTP_CONTENT_RANGE'])
 100              && isset($_SERVER['CONTENT_LENGTH'])
 101              && preg_match('/\b(\d+)\-(\d+)\/(\d+)\b/', $_SERVER['HTTP_CONTENT_RANGE'], $match)) {
 102              extract($file);
 103              $tmpfile = build_file_path($this->tempdir, md5($txp_user.':'.$name).'.part');
 104  
 105              // Get the range of the file uploaded from the client
 106              list($range, $begin, $end, $filesize) = $match;
 107  
 108              if (is_file($tmpfile) && filesize($tmpfile) == $begin) {
 109                  file_put_contents($tmpfile, fopen($tmp_name, 'r'), FILE_APPEND);
 110                  @unlink($tmp_name);
 111  
 112                  // Stop here if the file is not completely loaded
 113                  if ($end + 1 < $filesize) {
 114                      exit;
 115                  } else {
 116                      $file['tmp_name'] = $tmpfile;
 117                      $file['size'] = filesize($tmpfile);
 118                  }
 119              } elseif ($begin == 0) {
 120                  shift_uploaded_file($tmp_name, $tmpfile);
 121                  exit;
 122              } else { // Chunk error, clean up
 123                  @unlink($tmpfile);
 124                  $file['size'] = 0;
 125              }
 126  
 127              return true;
 128          }
 129  
 130          return false;
 131      }
 132  }

title

Description

title

Description

title

Description

title

title

Body