function __construct($interface, $files_directory)
 {
     $field = filter_input(INPUT_POST, 'field', FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
     // Check for file upload.
     if ($field === NULL || empty($_FILES) || !isset($_FILES['file'])) {
         return;
     }
     $this->interface = $interface;
     // Create a new result object.
     $this->result = new stdClass();
     // Set directory.
     $this->files_directory = $files_directory;
     // Create the temporary directory if it doesn't exist.
     $dirs = array('', '/files', '/images', '/videos', '/audios');
     foreach ($dirs as $dir) {
         if (!H5PCore::dirReady($this->files_directory . $dir)) {
             $this->result->error = $this->interface->t('Unable to create directory.');
             return;
         }
     }
     // Get the field.
     $this->field = json_decode($field);
     if (function_exists('finfo_file')) {
         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         $this->type = finfo_file($finfo, $_FILES['file']['tmp_name']);
         finfo_close($finfo);
     } elseif (function_exists('mime_content_type')) {
         // Deprecated, only when finfo isn't available.
         $this->type = mime_content_type($_FILES['file']['tmp_name']);
     } else {
         $this->type = $_FILES['file']['type'];
     }
     $this->extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
     $this->size = $_FILES['file']['size'];
 }
Esempio n. 2
0
 /**
  * Recursive function that makes sure the specified directory exists and
  * is writable.
  *
  * @param string $path
  * @return bool
  */
 public static function dirReady($path)
 {
     if (!file_exists($path)) {
         $parent = preg_replace("/\\/[^\\/]+\\/?\$/", '', $path);
         if (!H5PCore::dirReady($parent)) {
             return FALSE;
         }
         mkdir($path, 0777, true);
     }
     if (!is_dir($path)) {
         trigger_error('Path is not a directory ' . $path, E_USER_WARNING);
         return FALSE;
     }
     if (!is_writable($path)) {
         trigger_error('Unable to write to ' . $path . ' – check directory permissions –', E_USER_WARNING);
         return FALSE;
     }
     return TRUE;
 }
 /**
  * Implements getUploadedH5PPath
  */
 public function getUploadedH5pPath()
 {
     static $path;
     if (is_null($path)) {
         $path = $this->getH5pPath() . '/temp';
         H5PCore::dirReady($path);
         // Make sure dir exists!
         $path .= '/' . $_FILES['h5p_file']['name'];
     }
     return $path;
 }