Merge branch 'MDL-28562_wip' of https://github.com/jamiepratt/moodle
authorAparup Banerjee <aparup@moodle.com>
Wed, 23 Nov 2011 04:36:35 +0000 (12:36 +0800)
committerAparup Banerjee <aparup@moodle.com>
Wed, 23 Nov 2011 04:36:35 +0000 (12:36 +0800)
1  2 
lib/filestorage/file_storage.php

@@@ -519,13 -519,11 +519,13 @@@ class file_storage 
              throw new file_exception('storedfileproblem', 'Invalid contextid');
          }
  
 -        if ($component === '' or $component !== clean_param($component, PARAM_ALPHAEXT)) {
 +        $component = clean_param($component, PARAM_COMPONENT);
 +        if (empty($component)) {
              throw new file_exception('storedfileproblem', 'Invalid component');
          }
  
 -        if ($filearea === '' or $filearea !== clean_param($filearea, PARAM_ALPHAEXT)) {
 +        $filearea = clean_param($filearea, PARAM_AREA);
 +        if (empty($filearea)) {
              throw new file_exception('storedfileproblem', 'Invalid filearea');
          }
  
              }
  
              if ($key == 'component') {
 -                if ($value === '' or $value !== clean_param($value, PARAM_ALPHAEXT)) {
 +                $value = clean_param($value, PARAM_COMPONENT);
 +                if (empty($value)) {
                      throw new file_exception('storedfileproblem', 'Invalid component');
                  }
              }
  
              if ($key == 'filearea') {
 -                if ($value === '' or $value !== clean_param($value, PARAM_ALPHAEXT)) {
 +                $value = clean_param($value, PARAM_AREA);
 +                if (empty($value)) {
                      throw new file_exception('storedfileproblem', 'Invalid filearea');
                  }
              }
                  }
              }
  
 +            if ($key === 'timecreated' or $key === 'timemodified') {
 +                if (!is_number($value)) {
 +                    throw new file_exception('storedfileproblem', 'Invalid file '.$key);
 +                }
 +                if ($value < 0) {
 +                    //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak)
 +                    $value = 0;
 +                }
 +            }
 +
              $newrecord->$key = $value;
          }
  
          try {
              $newrecord->id = $DB->insert_record('files', $newrecord);
          } catch (dml_exception $e) {
 -            $newrecord->id = false;
 -        }
 -
 -        if (!$newrecord->id) {
              throw new stored_file_creation_exception($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid,
 -                                                     $newrecord->filepath, $newrecord->filename);
 +                                                     $newrecord->filepath, $newrecord->filename, $e->debuginfo);
          }
  
          $this->create_directory($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
              throw new file_exception('storedfileproblem', 'Invalid contextid');
          }
  
 -        if ($file_record->component === '' or $file_record->component !== clean_param($file_record->component, PARAM_ALPHAEXT)) {
 +        $file_record->component = clean_param($file_record->component, PARAM_COMPONENT);
 +        if (empty($file_record->component)) {
              throw new file_exception('storedfileproblem', 'Invalid component');
          }
  
 -        if ($file_record->filearea === '' or $file_record->filearea !== clean_param($file_record->filearea, PARAM_ALPHAEXT)) {
 +        $file_record->filearea = clean_param($file_record->filearea, PARAM_AREA);
 +        if (empty($file_record->filearea)) {
              throw new file_exception('storedfileproblem', 'Invalid filearea');
          }
  
          }
  
          $now = time();
 +        if (isset($file_record->timecreated)) {
 +            if (!is_number($file_record->timecreated)) {
 +                throw new file_exception('storedfileproblem', 'Invalid file timecreated');
 +            }
 +            if ($file_record->timecreated < 0) {
 +                //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak)
 +                $file_record->timecreated = 0;
 +            }
 +        } else {
 +            $file_record->timecreated = $now;
 +        }
 +
 +        if (isset($file_record->timemodified)) {
 +            if (!is_number($file_record->timemodified)) {
 +                throw new file_exception('storedfileproblem', 'Invalid file timemodified');
 +            }
 +            if ($file_record->timemodified < 0) {
 +                //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak)
 +                $file_record->timemodified = 0;
 +            }
 +        } else {
 +            $file_record->timemodified = $now;
 +        }
  
          $newrecord = new stdClass();
  
          $newrecord->filepath  = $file_record->filepath;
          $newrecord->filename  = $file_record->filename;
  
 -        $newrecord->timecreated  = empty($file_record->timecreated) ? $now : $file_record->timecreated;
 -        $newrecord->timemodified = empty($file_record->timemodified) ? $now : $file_record->timemodified;
 +        $newrecord->timecreated  = $file_record->timecreated;
 +        $newrecord->timemodified = $file_record->timemodified;
          $newrecord->mimetype     = empty($file_record->mimetype) ? mimeinfo('type', $file_record->filename) : $file_record->mimetype;
          $newrecord->userid       = empty($file_record->userid) ? null : $file_record->userid;
          $newrecord->source       = empty($file_record->source) ? null : $file_record->source;
          try {
              $newrecord->id = $DB->insert_record('files', $newrecord);
          } catch (dml_exception $e) {
 -            $newrecord->id = false;
 -        }
 -
 -        if (!$newrecord->id) {
              if ($newfile) {
                  $this->deleted_file_cleanup($newrecord->contenthash);
              }
              throw new stored_file_creation_exception($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid,
 -                                                    $newrecord->filepath, $newrecord->filename);
 +                                                    $newrecord->filepath, $newrecord->filename, $e->debuginfo);
          }
  
          $this->create_directory($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
              throw new file_exception('storedfileproblem', 'Invalid contextid');
          }
  
 -        if ($file_record->component === '' or $file_record->component !== clean_param($file_record->component, PARAM_ALPHAEXT)) {
 +        $file_record->component = clean_param($file_record->component, PARAM_COMPONENT);
 +        if (empty($file_record->component)) {
              throw new file_exception('storedfileproblem', 'Invalid component');
          }
  
 -        if ($file_record->filearea === '' or $file_record->filearea !== clean_param($file_record->filearea, PARAM_ALPHAEXT)) {
 +        $file_record->filearea = clean_param($file_record->filearea, PARAM_AREA);
 +        if (empty($file_record->filearea)) {
              throw new file_exception('storedfileproblem', 'Invalid filearea');
          }
  
          }
  
          $now = time();
 +        if (isset($file_record->timecreated)) {
 +            if (!is_number($file_record->timecreated)) {
 +                throw new file_exception('storedfileproblem', 'Invalid file timecreated');
 +            }
 +            if ($file_record->timecreated < 0) {
 +                //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak)
 +                $file_record->timecreated = 0;
 +            }
 +        } else {
 +            $file_record->timecreated = $now;
 +        }
 +
 +        if (isset($file_record->timemodified)) {
 +            if (!is_number($file_record->timemodified)) {
 +                throw new file_exception('storedfileproblem', 'Invalid file timemodified');
 +            }
 +            if ($file_record->timemodified < 0) {
 +                //NOTE: unfortunately I make a mistake when creating the "files" table, we can not have negative numbers there, on the other hand no file should be older than 1970, right? (skodak)
 +                $file_record->timemodified = 0;
 +            }
 +        } else {
 +            $file_record->timemodified = $now;
 +        }
  
          $newrecord = new stdClass();
  
          $newrecord->filepath  = $file_record->filepath;
          $newrecord->filename  = $file_record->filename;
  
 -        $newrecord->timecreated  = empty($file_record->timecreated) ? $now : $file_record->timecreated;
 -        $newrecord->timemodified = empty($file_record->timemodified) ? $now : $file_record->timemodified;
 +        $newrecord->timecreated  = $file_record->timecreated;
 +        $newrecord->timemodified = $file_record->timemodified;
          $newrecord->mimetype     = empty($file_record->mimetype) ? mimeinfo('type', $file_record->filename) : $file_record->mimetype;
          $newrecord->userid       = empty($file_record->userid) ? null : $file_record->userid;
          $newrecord->source       = empty($file_record->source) ? null : $file_record->source;
          try {
              $newrecord->id = $DB->insert_record('files', $newrecord);
          } catch (dml_exception $e) {
 -            $newrecord->id = false;
 -        }
 -
 -        if (!$newrecord->id) {
              if ($newfile) {
                  $this->deleted_file_cleanup($newrecord->contenthash);
              }
              throw new stored_file_creation_exception($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid,
 -                                                    $newrecord->filepath, $newrecord->filename);
 +                                                    $newrecord->filepath, $newrecord->filename, $e->debuginfo);
          }
  
          $this->create_directory($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->userid);
          }
  
          if (!isset($file_record['filename'])) {
-             $file_record['filename'] == $file->get_filename();
+             $file_record['filename'] = $file->get_filename();
          }
  
          if (!isset($file_record['mimetype'])) {