Example #1
0
 private function make_directory_writable($path)
 {
     // provides fileop class.
     require_once AWPCP_DIR . '/fileop.class.php';
     $previous_umask = umask(0);
     $fileop = new fileop();
     if (!$fileop->set_permission($path, $this->get_default_directory_mode())) {
         $message = __('There was a problem trying to make directory <directory-name> writable.', 'AWPCP');
         $message = str_replace('<directory-name>', awpcp_utf8_basename($path), $message);
         throw new AWPCP_Exception($message);
     }
     umask($previous_umask);
     return $path;
 }
Example #2
0
/**
 * Check that the given file meets the file size, dimensions and file type
 * constraints and moves the file to the AWPCP Uploads directory.
 *
 * @param $error    if an error occurs the error message will be returned by reference
 *                  using this variable.
 * @param $action   'upload' if the file was uplaoded using an HTML File field.
 *                  'copy' if the file was uplaoded using a different method. Images
 *                  extracted from a ZIP file during Ad import.
 * @return          false if an error occurs or an array with the upload file information
 *                  on success.
 * @since 3.0.2
 * @deprecated  3.4
 */
function awpcp_upload_file($file, $constraints, &$error = false, $action = 'upload')
{
    $filename = sanitize_file_name(strtolower($file['name']));
    $tmpname = $file['tmp_name'];
    $mime_type = $file['type'];
    if (!in_array($mime_type, $constraints['mime_types'])) {
        $error = _x('The type of the uplaoded file %s is not allowed.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    $paths = awpcp_get_uploads_directories();
    if (!file_exists($tmpname)) {
        $error = _x('The specified file does not exists: %s.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    if ($action == 'upload' && !is_uploaded_file($tmpname)) {
        $error = _x('Unknown error encountered while uploading the image.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    $file_size = filesize($tmpname);
    if (empty($file_size) || $file_size <= 0) {
        $error = _x('There was an error trying to find out the file size of the image %s.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    }
    if (in_array($mime_type, awpcp_get_image_mime_types())) {
        if ($file_size > $constraints['max_image_size']) {
            $error = _x('The file %s was larger than the maximum allowed file size of %s bytes. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['max_image_size']);
            return false;
        }
        if ($file_size < $constraints['min_image_size']) {
            $error = _x('The size of %1$s was too small. The file was not uploaded. File size must be greater than %2$d bytes.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['min_image_size']);
            return false;
        }
        $img_info = getimagesize($tmpname);
        if (!isset($img_info[0]) && !isset($img_info[1])) {
            $error = _x('The file %s does not appear to be a valid image file.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>');
            return false;
        }
        if ($img_info[0] < $constraints['min_image_width']) {
            $error = _x('The image %s did not meet the minimum width of %s pixels. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['min_image_width']);
            return false;
        }
        if ($img_info[1] < $constraints['min_image_height']) {
            $error = _x('The image %s did not meet the minimum height of %s pixels. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['min_image_height']);
            return false;
        }
    } else {
        if ($file_size > $constraints['max_attachment_size']) {
            $error = _x('The file %s was larger than the maximum allowed file size of %s bytes. The file was not uploaded.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>', $constraints['max_attachment_size']);
            return false;
        }
    }
    $newname = awpcp_unique_filename($tmpname, $filename, array($paths['files_dir'], $paths['thumbnails_dir']));
    $newpath = trailingslashit($paths['files_dir']) . $newname;
    if ($action == 'upload' && !@move_uploaded_file($tmpname, $newpath)) {
        $error = _x('The file %s could not be moved to the destination directory.', 'upload files', 'AWPCP');
        $error = sprintf($error, '<strong>' . $filename . '</strong>');
        return false;
    } else {
        if ($action == 'copy' && !@copy($tmpname, $newpath)) {
            $error = _x('The file %s could not be copied to the destination directory.', 'upload files', 'AWPCP');
            $error = sprintf($message, '<strong>' . $filename . '</strong>');
            return false;
        }
    }
    if (in_array($mime_type, awpcp_get_image_mime_types())) {
        if (!awpcp_create_image_versions($newname, $paths['files_dir'])) {
            $error = _x('Could not create resized versions of image %s.', 'upload files', 'AWPCP');
            $error = sprintf($error, '<strong>' . $filename . '</strong>');
            # TODO: unlink resized version, thumbnail and primary image
            @unlink($newpath);
            return false;
        }
    }
    @chmod($newpath, 0644);
    return array('original' => $filename, 'filename' => awpcp_utf8_basename($newpath), 'path' => str_replace($paths['files_dir'], '', $newpath), 'mime_type' => $mime_type);
}