diff --git a/lib/uploadlib.php b/lib/uploadlib.php
index 0c773a89775..b9deffb1506 100644
--- a/lib/uploadlib.php
+++ b/lib/uploadlib.php
@@ -1,37 +1,78 @@
maxbytes are used to get the maxbytes from get_max_upload_file_size().
- * @param $silent - whether to notify errors or not.
- * @param $allownull - whether we care if there's no file when we've set the input name.
+ *
+ * @uses $CFG
+ * @param string $inputname If this is given the upload manager will only process the file in $_FILES with this name.
+ * @param boolean $deleteothers Whether to delete other files in the destination directory (optional, defaults to false)
+ * @param boolean $handlecollisions Whether to use {@link handle_filename_collision()} or not. (optional, defaults to false)
+ * @param course $course The course the files are being uploaded for (for logging and virus notifications) {@link $COURSE}
+ * @param boolean $recoverifmultiple If we come across a virus, or if a file doesn't validate or whatever, do we continue? optional, defaults to true.
+ * @param int $modbytes Max bytes for this module - this and $course->maxbytes are used to get the maxbytes from {@link get_max_upload_file_size()}.
+ * @param boolean $silent Whether to notify errors or not.
+ * @param boolean $allownull Whether we care if there's no file when we've set the input name.
*/
- function upload_manager($inputname='',$deleteothers=false,$handlecollisions=false,$course=null,$recoverifmultiple=false,$modbytes=0,$silent=false,$allownull=false) {
+ function upload_manager($inputname='', $deleteothers=false, $handlecollisions=false, $course=null, $recoverifmultiple=false, $modbytes=0, $silent=false, $allownull=false) {
global $CFG;
$this->config->deleteothers = $deleteothers;
$this->config->handlecollisions = $handlecollisions;
$this->config->recoverifmultiple = $recoverifmultiple;
- $this->config->maxbytes = get_max_upload_file_size($CFG->maxbytes,$course->maxbytes,$modbytes);
+ $this->config->maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $modbytes);
$this->config->silent = $silent;
$this->config->allownull = $allownull;
$this->files = array();
@@ -44,8 +85,12 @@ class upload_manager {
}
/**
- * Gets all entries out of $_FILES and stores them locally in $files
- * Checks each one against get_max_upload_file_size and calls cleanfilename and scans them for viruses etc.
+ * Gets all entries out of $_FILES and stores them locally in $files and then
+ * checks each one against {@link get_max_upload_file_size()} and calls {@link cleanfilename()}
+ * and scans them for viruses etc.
+ * @uses $CFG
+ * @uses $_FILES
+ * @return boolean
*/
function preprocess_files() {
global $CFG;
@@ -70,7 +115,7 @@ class upload_manager {
notify(get_string('uploadfailednotrecovering','moodle',$a));
}
else {
- $this->notify .= "
".get_string('uploadfailednotrecovering','moodle',$a);
+ $this->notify .= '
'. get_string('uploadfailednotrecovering','moodle',$a);
}
$this->status = false;
return false;
@@ -80,7 +125,7 @@ class upload_manager {
notify($this->files[$name]['uploadlog']);
}
else {
- $this->notify .= "
".$this->files[$name]['uploadlog'];
+ $this->notify .= '
'. $this->files[$name]['uploadlog'];
}
$this->status = false;
return false;
@@ -91,7 +136,7 @@ class upload_manager {
if ($newname != $this->files[$name]['name']) {
$a->oldname = $this->files[$name]['name'];
$a->newname = $newname;
- $this->files[$name]['uploadlog'] .= get_string('uploadrenamedchars','moodle',$a);
+ $this->files[$name]['uploadlog'] .= get_string('uploadrenamedchars','moodle', $a);
}
$this->files[$name]['name'] = $newname;
$this->files[$name]['clear'] = true; // ok to save.
@@ -107,9 +152,9 @@ class upload_manager {
/**
* Validates a single file entry from _FILES
- * @param $file - the entry from _FILES to validate
- * @param $allowempty - this is to allow module owners to control which files are compulsory if this function is being called straight from the module.
- * @return true if ok.
+ *
+ * @param object $file The entry from _FILES to validate
+ * @return boolean True if ok.
*/
function validate_file(&$file) {
if (empty($file)) {
@@ -120,7 +165,7 @@ class upload_manager {
return false;
}
if ($file['size'] > $this->config->maxbytes) {
- $file['uploadlog'] .= "\n".get_string("uploadedfiletoobig", "moodle", $this->config->maxbytes);
+ $file['uploadlog'] .= "\n". get_string('uploadedfiletoobig', 'moodle', $this->config->maxbytes);
return false;
}
return true;
@@ -128,31 +173,34 @@ class upload_manager {
/**
* Moves all the files to the destination directory.
- * @param $destination - the destination directory.
- * @return status;
+ *
+ * @uses $CFG
+ * @uses $USER
+ * @param string $destination The destination directory.
+ * @return boolean status;
*/
function save_files($destination) {
- global $CFG,$USER;
+ global $CFG, $USER;
if (!$this->status) { // preprocess_files hasn't been run
$this->preprocess_files();
}
if ($this->status) {
- if (!(strpos($destination,$CFG->dataroot) === false)) {
+ if (!(strpos($destination, $CFG->dataroot) === false)) {
// take it out for giving to make_upload_directory
- $destination = substr($destination,strlen($CFG->dataroot)+1);
+ $destination = substr($destination, strlen($CFG->dataroot)+1);
}
- if ($destination{strlen($destination)-1} == "/") { // strip off a trailing / if we have one
- $destination = substr($destination,0,-1);
+ if ($destination{strlen($destination)-1} == '/') { // strip off a trailing / if we have one
+ $destination = substr($destination, 0, -1);
}
- if (!make_upload_directory($destination,true)) { //TODO maybe put this function here instead of moodlelib.php now.
+ if (!make_upload_directory($destination, true)) { //TODO maybe put this function here instead of moodlelib.php now.
$this->status = false;
return false;
}
- $destination = $CFG->dataroot.'/'.$destination; // now add it back in so we have a full path
+ $destination = $CFG->dataroot .'/'. $destination; // now add it back in so we have a full path
$exceptions = array(); //need this later if we're deleting other files.
@@ -164,21 +212,21 @@ class upload_manager {
}
if ($this->config->handlecollisions) {
- $this->handle_filename_collision($destination,$this->files[$i]);
+ $this->handle_filename_collision($destination, $this->files[$i]);
}
if (move_uploaded_file($this->files[$i]['tmp_name'], $destination.'/'.$this->files[$i]['name'])) {
- chmod($destination.'/'.$this->files[$i]['name'], $CFG->directorypermissions);
+ chmod($destination .'/'. $this->files[$i]['name'], $CFG->directorypermissions);
$this->files[$i]['fullpath'] = $destination.'/'.$this->files[$i]['name'];
$this->files[$i]['uploadlog'] .= "\n".get_string('uploadedfile');
$this->files[$i]['saved'] = true;
$exceptions[] = $this->files[$i]['name'];
// now add it to the log (this is important so we know who to notify if a virus is found later on)
- clam_log_upload($this->files[$i]['fullpath'],$this->course);
+ clam_log_upload($this->files[$i]['fullpath'], $this->course);
$savedsomething=true;
}
}
if ($savedsomething && $this->config->deleteothers) {
- $this->delete_other_files($destination,$exceptions);
+ $this->delete_other_files($destination, $exceptions);
}
}
if (!$savedsomething) {
@@ -189,9 +237,10 @@ class upload_manager {
}
/**
- * Wrapper function that calls preprocess_files and viruscheck_files and then save_files
+ * Wrapper function that calls {@link preprocess_files()} and {@link viruscheck_files()} and then {@link save_files()}
* Modules that require the insert id in the filepath should not use this and call these functions seperately in the required order.
- * @parameter $destination - where to save the uploaded files to.
+ * @parameter string $destination Where to save the uploaded files to.
+ * @return boolean
*/
function process_file_uploads($destination) {
if ($this->preprocess_files()) {
@@ -202,14 +251,15 @@ class upload_manager {
/**
* Deletes all the files in a given directory except for the files in $exceptions (full paths)
- * @param $destination - the directory to clean up.
- * @param $exceptions - array of full paths of files to KEEP.
+ *
+ * @param string $destination The directory to clean up.
+ * @param array $exceptions Full paths of files to KEEP.
*/
- function delete_other_files($destination,$exceptions=null) {
+ function delete_other_files($destination, $exceptions=null) {
if ($filestodel = get_directory_list($destination)) {
foreach ($filestodel as $file) {
- if (!is_array($exceptions) || !in_array($file,$exceptions)) {
- unlink("$destination/$file");
+ if (!is_array($exceptions) || !in_array($file, $exceptions)) {
+ unlink($destination .'/'. $file);
$deletedsomething = true;
}
}
@@ -219,40 +269,45 @@ class upload_manager {
notify(get_string('uploadoldfilesdeleted'));
}
else {
- $this->notify .= "
".get_string('uploadoldfilesdeleted');
+ $this->notify .= '
'. get_string('uploadoldfilesdeleted');
}
}
}
/**
* Handles filename collisions - if the desired filename exists it will rename it according to the pattern in $format
- * @param $destination - destination directory (to check existing files against)
- * @param $file - the current file from $files we're processing.
- * @param $format - the printf style format to rename the file to (defaults to filename_number.extn)
- * @return new filename.
+ * @param string $destination Destination directory (to check existing files against)
+ * @param object $file Passed in by reference. The current file from $files we're processing.
+ * @param string $format The printf style format to rename the file to (defaults to filename_number.extn)
+ * @return string The new filename.
+ * @todo verify return type - this function does not appear to return anything since $file is passed in by reference
*/
- function handle_filename_collision($destination,&$file,$format='%s_%d.%s') {
- $bits = explode('.',$file['name']);
+ function handle_filename_collision($destination, &$file, $format='%s_%d.%s') {
+ $bits = explode('.', $file['name']);
// check for collisions and append a nice numberydoo.
- if (file_exists($destination.'/'.$file['name'])) {
+ if (file_exists($destination .'/'. $file['name'])) {
$a->oldname = $file['name'];
for ($i = 1; true; $i++) {
- $try = sprintf($format,$bits[0],$i,$bits[1]);
- if ($this->check_before_renaming($destination,$try,$file)) {
+ $try = sprintf($format, $bits[0], $i, $bits[1]);
+ if ($this->check_before_renaming($destination, $try, $file)) {
$file['name'] = $try;
break;
}
}
$a->newname = $file['name'];
- $file['uploadlog'] .= "\n".get_string('uploadrenamedcollision','moodle',$a);
+ $file['uploadlog'] .= "\n". get_string('uploadrenamedcollision','moodle', $a);
}
}
/**
* This function checks a potential filename against what's on the filesystem already and what's been saved already.
+ * @param string $destination Destination directory (to check existing files against)
+ * @param string $nametocheck The filename to be compared.
+ * @param object $file The current file from $files we're processing.
+ * return boolean
*/
- function check_before_renaming($destination,$nametocheck,$file) {
- if (!file_exists($destination.'/'.$nametocheck)) {
+ function check_before_renaming($destination, $nametocheck, $file) {
+ if (!file_exists($destination .'/'. $nametocheck)) {
return true;
}
if ($this->config->deleteothers) {
@@ -269,7 +324,13 @@ class upload_manager {
return false;
}
-
+ /**
+ * ?
+ *
+ * @param object $file Passed in by reference. The current file from $files we're processing.
+ * @return string
+ * @todo Finish documenting this function
+ */
function get_file_upload_error(&$file) {
switch ($file['error']) {
@@ -309,9 +370,9 @@ class upload_manager {
*/
function print_upload_log($return=false) {
foreach (array_keys($this->files) as $i => $key) {
- $str .= ''.get_string('uploadfilelog','moodle',$i+1).' '
+ $str .= ''. get_string('uploadfilelog', 'moodle', $i+1) .' '
.((!empty($this->files[$key]['originalname'])) ? '('.$this->files[$key]['originalname'].')' : '')
- .' :'.nl2br($this->files[$key]['uploadlog']).'
';
+ .' :'. nl2br($this->files[$key]['uploadlog']) .'
';
}
if ($return) {
return $str;
@@ -321,6 +382,7 @@ class upload_manager {
/**
* If we're only handling one file (if inputname was given in the constructor) this will return the (possibly changed) filename of the file.
+ @return boolean
*/
function get_new_filename() {
if (!empty($this->inputname) && count($this->files) == 1) {
@@ -331,6 +393,7 @@ class upload_manager {
/**
* If we're only handling one file (if input name was given in the constructor) this will return the full path to the saved file.
+ * @return boolean
*/
function get_new_filepath() {
if (!empty($this->inputname) && count($this->files) == 1) {
@@ -341,6 +404,7 @@ class upload_manager {
/**
* If we're only handling one file (if inputname was given in the constructor) this will return the ORIGINAL filename of the file.
+ * @return boolean
*/
function get_original_filename() {
if (!empty($this->inputname) && count($this->files) == 1) {
@@ -350,10 +414,11 @@ class upload_manager {
}
/**
- * This function returns any errors wrapped up in red
+ * This function returns any errors wrapped up in red.
+ * @return string
*/
function get_errors() {
- return '
'.$this->notify.'
'; + return ''. $this->notify .'
'; } } @@ -365,29 +430,31 @@ UPLOAD_PRINT_FORM_FRAGMENT DOESN'T REALLY BELONG IN THE CLASS BUT CERTAINLY IN T /** - * This function prints out a number of upload form elements - * @param $numfiles - the number of elements required (optional, defaults to 1) - * @param $names - array of element names to use (optional, defaults to FILE_n) - * @param $descriptions - array of strings to be printed out before each file bit. - * @param $uselabels - whether to output text fields for file descriptions or not (optional, defaults to false) - * @param $labelnames - array of element names to use for labels (optional, defaults to LABEL_n) - * @param $coursebytes - * @param $modbytes - these last two are used to calculate upload max size ( using get_max_upload_file_size) - * @param $return - whether to return the string (defaults to false - string is echoed) + * This function prints out a number of upload form elements. + * + * @param int $numfiles The number of elements required (optional, defaults to 1) + * @param array $names Array of element names to use (optional, defaults to FILE_n) + * @param array $descriptions Array of strings to be printed out before each file bit. + * @param boolean $uselabels -Whether to output text fields for file descriptions or not (optional, defaults to false) + * @param array $labelnames Array of element names to use for labels (optional, defaults to LABEL_n) + * @param int $coursebytes $coursebytes and $maxbytes are used to calculate upload max size ( using {@link get_max_upload_file_size}) + * @param int $modbytes $coursebytes and $maxbytes are used to calculate upload max size ( using {@link get_max_upload_file_size}) + * @param boolean $return -Whether to return the string (defaults to false - string is echoed) + * @return string Form returned as string if $return is true */ -function upload_print_form_fragment($numfiles=1,$names=null,$descriptions=null,$uselabels=false,$labelnames=null,$coursebytes=0,$modbytes=0,$return=false) { +function upload_print_form_fragment($numfiles=1, $names=null, $descriptions=null, $uselabels=false, $labelnames=null, $coursebytes=0, $modbytes=0, $return=false) { global $CFG; - $maxbytes = get_max_upload_file_size($CFG->maxbytes,$coursebytes,$modbytes); - $str = ''."\n"; + $maxbytes = get_max_upload_file_size($CFG->maxbytes, $coursebytes, $modbytes); + $str = ''."\n"; for ($i = 0; $i < $numfiles; $i++) { if (is_array($descriptions) && !empty($descriptions[$i])) { - $str .= ''.$descriptions[$i].'