Example #1
0
    /**
     * This method processes the results of the UploadForm.
     * It will save the uploaded files to /assets/ and create new File objects as required.
     */
    function doUpload($data, $form)
    {
        foreach ($data['Files'] as $param => $files) {
            if (!is_array($files)) {
                $files = array($files);
            }
            foreach ($files as $key => $value) {
                $processedFiles[$key][$param] = $value;
            }
        }
        if ($data['ID'] && $data['ID'] != 'root') {
            $folder = DataObject::get_by_id("Folder", $data['ID']);
        } else {
            $folder = singleton('Folder');
        }
        $newFiles = array();
        $fileSizeWarnings = '';
        $uploadErrors = '';
        foreach ($processedFiles as $file) {
            if ($file['error'] == UPLOAD_ERR_NO_TMP_DIR) {
                $status = 'bad';
                $statusMessage = _t('AssetAdmin.NOTEMP', 'There is no temporary folder for uploads. Please set upload_tmp_dir in php.ini.');
                break;
            }
            if ($file['tmp_name']) {
                // Workaround open_basedir problems
                if (ini_get("open_basedir")) {
                    $newtmp = TEMP_FOLDER . '/' . $file['name'];
                    move_uploaded_file($file['tmp_name'], $newtmp);
                    $file['tmp_name'] = $newtmp;
                }
                // check that the file can be uploaded and isn't too large
                $extensionIndex = strripos($file['name'], '.');
                $extension = strtolower(substr($file['name'], $extensionIndex + 1));
                if ($extensionIndex !== FALSE) {
                    list($maxSize, $warnSize) = File::getMaxFileSize($extension);
                } else {
                    list($maxSize, $warnSize) = File::getMaxFileSize();
                }
                // check that the file is not too large or that the current user is an administrator
                if ($this->can('AdminCMS') || File::allowedFileType($extension) && (!isset($maxsize) || $file['size'] < $maxSize)) {
                    $newFiles[] = $folder->addUploadToFolder($file);
                } elseif (!File::allowedFileType($extension)) {
                    $fileSizeWarnings .= "alert( '" . sprintf(_t('AssetAdmin.ONLYADMINS', 'Only administrators can upload %s files.'), $extension) . "' );";
                } else {
                    if ($file['size'] > 1048576) {
                        $fileSize = "" . ceil($file['size'] / 1048576) . "MB";
                    } elseif ($file['size'] > 1024) {
                        $fileSize = "" . ceil($file['size'] / 1024) . "KB";
                    } else {
                        $fileSize = "" . ceil($file['size']) . "B";
                    }
                    $fileSizeWarnings .= "alert( '" . sprintf(_t('AssetAdmin.TOOLARGE', "%s is too large (%s). Files of this type cannot be larger than %s"), "\\'" . $file['name'] . "\\'", $fileSize, $warnSize) . "' );";
                }
            }
        }
        if ($newFiles) {
            $numFiles = sizeof($newFiles);
            $statusMessage = sprintf(_t('AssetAdmin.UPLOADEDX', "Uploaded %s files"), $numFiles);
            $status = "good";
        } else {
            if ($status != 'bad') {
                $statusMessage = _t('AssetAdmin.NOTHINGTOUPLOAD', 'There was nothing to upload');
                $status = "";
            }
        }
        $fileIDs = array();
        $fileNames = array();
        foreach ($newFiles as $newFile) {
            $fileIDs[] = $newFile;
            $fileObj = DataObject::get_one('File', "`File`.ID={$newFile}");
            $fileNames[] = $fileObj->Name;
        }
        $sFileIDs = implode(',', $fileIDs);
        $sFileNames = implode(',', $fileNames);
        echo <<<HTML
\t\t\t<script type="text/javascript">
\t\t\t/* IDs: {$sFileIDs} */
\t\t\t/* Names: {$sFileNames} */
\t\t\t
\t\t\tvar form = parent.document.getElementById('Form_EditForm');
\t\t\tform.getPageFromServer(form.elements.ID.value);
\t\t\tparent.statusMessage("{$statusMessage}","{$status}");
\t\t\t{$fileSizeWarnings}
\t\t\tparent.document.getElementById('sitetree').getTreeNodeByIdx( "{$folder->ID}" ).getElementsByTagName('a')[0].className += ' contents';
\t\t\t</script>
HTML;
    }