示例#1
0
 } else {
     // this is for single uploaded files
     if (!($userfilename = $_FILES['query']['name']['sourcedirectory'])) {
         $errorstring = "Error uploading file! \n";
         $errorstring .= "File: " . $userfilename . " \n";
         echo $errorstring;
         exit;
     }
     $filename = $_FILES['query']['tmp_name']['sourcedirectory'];
     $mimetype = '';
     $userfileext = '';
     $dotpos = strrpos($userfilename, '.');
     if (strlen($userfilename) - $dotpos > 3 && strlen($userfilename) - $dotpos < 6) {
         $userfileext = strtolower(substr($userfilename, $dotpos + 1));
     }
     $ret = read_mime($filename, $mimetype, $userfileext);
     if (!$ret) {
         $errorstring = "Error reading image mimetype! \n<br>\n";
         $errorstring .= "File:" . $userfilename . "\n<br>\n";
         echo $errorstring;
     } else {
         echo "Mimetype: " . $mimetype . "\n<br>\n";
         if (in_array($mimetype, $formats_available)) {
             // extract image information
             // we already have the mimetype
             $img_data = array();
             $img_data['mime'] = $mimetype;
             // try to read the rest
             $ret = read_image($filename, $img_data);
             if (!$ret) {
                 $errorstring = "Error reading image information! \n<br>\n";
示例#2
0
 }
 if (!is_uploaded_file($file['tmp_name'])) {
     $upload_error['invalid_upload'] = true;
 }
 if ($file['size'] <= 0) {
     $upload_error['invalid_upload'] = true;
 }
 if ($file['size'] > unformat_size($config['uploader']['max_file_size'])) {
     $upload_error['file_too_big'] = true;
 }
 if (count($upload_error) == 0) {
     if (empty($file['type']) && read_mime($file['tmp_name']) !== false) {
         $mime = read_mime($file['tmp_name']);
     } elseif ($file['type'] != read_mime($file['tmp_name']) && read_mime($file['tmp_name']) !== false) {
         unlink($file['tmp_name']);
         die("Type mismatch! PHP reports: <b>" . $file['type'] . "</b> real type is: <b>" . read_mime($file['tmp_name']) . "</b> <br /> Deleting file... (to protect Jetbird)");
     } else {
         $mime = $file['type'];
     }
     $filename = md5(uniqid(rand(), true));
     $target = $config['uploader']['upload_dir'] . $filename;
     if (move_uploaded_file($file['tmp_name'], $target)) {
         $query = "INSERT INTO attachment_list(\tattachment_file,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tattachment_owner,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tattachment_original_name,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tattachment_type,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tattachment_size,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tattachment_date)\n\t\t\t\t\t\t\t\t\tVALUES ('" . $filename . "',\n\t\t\t\t\t\t\t\t\t\t\t" . $_SESSION['user_id'] . ",\n\t\t\t\t\t\t\t\t\t\t\t'" . $file['name'] . "',\n\t\t\t\t\t\t\t\t\t\t\t'" . $mime . "',\n\t\t\t\t\t\t\t\t\t\t\t" . $file['size'] . ",\n\t\t\t\t\t\t\t\t\t\t\t" . time() . ")";
         if (!$db->query($query)) {
             // Destroy file if query doesn't succeed
             unlink($target);
         } else {
             $download_link = jetbird_root_url() . "attachment/?download&amp;id=" . $db->last_insert_id;
             $smarty->assign("download_link", $download_link);
             $smarty->assign("success", true);
         }
示例#3
0
function process_image_directory($dir, $query, $debug = false)
{
    global $config;
    global $db, $db_prefix, $user;
    global $resolutions_available;
    global $formats_available;
    global $formats_suffix;
    global $imagemagick_identify;
    global $imagemagick_convert;
    global $file_binary, $zip_binary, $unzip_binary;
    $files = array();
    $remove = array();
    if (!read_recursive($dir, $files, $remove)) {
        echo "Error reading directory structure\n<br>\n";
    } else {
        if (!empty($dir)) {
            $offset = strlen($dir) + 1;
        } else {
            $offset = 0;
        }
        foreach ($files as $num => $val) {
            $files[$num] = substr($val, $offset);
        }
        foreach ($remove as $num => $val) {
            $remove[$num] = substr($val, $offset);
        }
        usort($remove, "cmp");
        if ($debug) {
            echo "Files: \n<br>\n<br>\n";
            foreach ($files as $num => $val) {
                echo "[" . $num . "]: " . $val . "\n<br>\n";
            }
            echo "Remove: \n<br>\n<br>\n";
            foreach ($remove as $num => $val) {
                echo "[" . $num . "]: " . $val . "\n<br>\n";
            }
        }
        echo "Reading directory structure done.\n<br>\n<br>\n";
        foreach ($files as $file) {
            echo "<em>Reading file: " . $file . "</em>\n<br>\n";
            if (!empty($dir)) {
                $filename = $dir . addslashes(DIRECTORY_SEPARATOR . $file);
            } else {
                $filename = $file;
            }
            if (is_file($filename)) {
                $mimetype = '';
                $fileext = '';
                $dotpos = strrpos($filename, '.');
                if (strlen($filename) - $dotpos > 3 && strlen($filename) - $dotpos < 6) {
                    $fileext = strtolower(substr($filename, $dotpos + 1));
                }
                $ret = read_mime($filename, $mimetype, $fileext);
                if (!$ret) {
                    $errorstring = "Error reading image mimetype! \n<br>\n";
                    $errorstring .= "File:" . $filename . "\n<br>\n";
                    echo $errorstring;
                } else {
                    echo "Mimetype: " . $mimetype . "\n<br>\n";
                    if (in_array($mimetype, $formats_available)) {
                        // extract image information
                        // we already have the mimetype
                        $img_data = array();
                        $img_data['mime'] = $mimetype;
                        // try to read the rest
                        $ret = read_image($filename, $img_data);
                        if (!$ret) {
                            $errorstring = "Error reading image information! \n<br>\n";
                            $errorstring .= "File:" . $filename . "\n<br>\n";
                            echo $errorstring;
                        } else {
                            echo "Type:\t\t" . $img_data["type"] . "\n<br>\n";
                            echo "Width:\t\t" . $img_data["width"] . "\n<br>\n";
                            echo "Height:\t\t" . $img_data["height"] . "\n<br>\n";
                            // get output directory
                            $baseid = $query['baseid'];
                            $sql = "SELECT base FROM " . $db_prefix . "img_base WHERE img_baseid=" . $db->qstr($baseid);
                            $output_dir = $db->GetOne($sql);
                            if (empty($output_dir)) {
                                $errorstring = "Error reading output directory! \n<br>\n";
                                $errorstring .= "BaseID: " . $baseid . "\n<br>\n";
                                $errorstring .= "Directory: " . $output_dir . "\n<br>\n";
                                die($errorstring);
                            }
                            // use a temporary directory, where we can store the converted files until we have
                            // completed the task for all resolutions.
                            // we will move files from there into the correct directory later
                            $tmpdir = $config['imageTmp'];
                            if (!is_dir($tmpdir)) {
                                $errorstring = "Could not find temporary directory! \n<br>\n";
                                $errorstring .= "Path: " . $tmpdir . "\n<br>\n";
                                die($errorstring);
                            }
                            // check if we already have a cache directory structure, otherwise create
                            $ret = check_dir($tmpdir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR, true, true, 0755);
                            if (!$ret) {
                                $errorstring = "Temporary directory cache does not exist or is not writable! \n<br>\n";
                                die($errorstring);
                            }
                            // generate image in all necessary resolutions
                            // you can skip resolutions by changing the array at this point
                            $resolutions = $resolutions_available;
                            foreach ($resolutions as $res) {
                                $ret = check_dir($tmpdir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . $res, true, true, 0755);
                                if (!$ret) {
                                    $errorstring = "Error creating temporary directory cache for resolution: {$res}! \n<br>\n";
                                    die($errorstring);
                                }
                            }
                            // generate temporary id string
                            $tmpid = generate_random_string();
                            // carry out conversion steps
                            echo "\n<br>\n<em>Generating image versions for all resolutions:</em>\n<br><br>\n";
                            if ($config['imagick_mode'] == 'fast') {
                                $ret = convert_image_batch($filename, $tmpdir . 'cache', $tmpid, $resolutions);
                                if ($ret) {
                                    echo "Generating images (batch mode)\t\t\t: success\n<br>\n";
                                    $convert_success = true;
                                } else {
                                    echo "Generating images (batch mode)\t\t\t: failed\n<br>\n";
                                    echo "Aborting...\n<br>\n";
                                    $convert_success = false;
                                }
                            } else {
                                // convert image to JPG at 1600x1200 as a base for the following conversion
                                $baseimage_filename = $tmpdir . 'cache' . DIRECTORY_SEPARATOR . $tmpid . '.jpg';
                                $res = '1600x1200';
                                $ret = convert_image($filename, $baseimage_filename, $res, false);
                                if ($ret) {
                                    echo "Generating base image\t\t\t: success\n<br>\n";
                                    $convert_success = true;
                                } else {
                                    echo "Generating base image\t\t\t: failed\n<br>\n";
                                    echo "Aborting...\n<br>\n";
                                    $convert_success = false;
                                }
                                // the following lines will generate a JPG-version of your original image
                                // uncomment them, if you need it (requires additional computation time)
                                /*
                                if ($convert_success)
                                {
                                	$output_filename = $tmpdir.'cache'.DIRECTORY_SEPARATOR.$tmpid.'.jpg';
                                	
                                	$res = $img_data['width'].'x'.$img_data['height'];
                                	
                                	$ret = convert_image($filename,$output_filename,$res, false);
                                	
                                	if ($ret)
                                	{
                                		echo ("Writing (".$res.")\t\t\t: success\n<br>\n");
                                		$convert_success = true;
                                	}
                                	else
                                	{
                                		echo ("Writing (".$res.")\t\t\t: failed\n<br>\n");
                                		echo ("Aborting...\n<br>\n");
                                		$convert_success = false;
                                	}
                                }
                                */
                                if ($convert_success) {
                                    foreach ($resolutions as $res) {
                                        $ret = check_dir($tmpdir . 'cache' . DIRECTORY_SEPARATOR . $res, true, true, 0755);
                                        // check if cache subdirectories exist
                                        if (!$ret) {
                                            $errorstring = "Error creating directory for resolution " . $res . "\n<br>\n";
                                            $errorstring .= "or directory exists and is not writable\n<br>\n";
                                            die($errorstring);
                                        }
                                        if ($res == '120x90') {
                                            $is_thumbnail = true;
                                        } else {
                                            $is_thumbnail = false;
                                        }
                                        $output_filename = $tmpdir . 'cache' . DIRECTORY_SEPARATOR . $res . DIRECTORY_SEPARATOR . $tmpid . '.jpg';
                                        $ret = convert_image($baseimage_filename, $output_filename, $res, $is_thumbnail);
                                        if ($ret) {
                                            echo "Writing (" . $res . ")\t\t\t: success\n<br>\n";
                                            $convert_success = true;
                                        } else {
                                            echo "Writing (" . $res . ")\t\t\t: failed\n<br>\n";
                                            echo "Aborting...\n<br>\n";
                                            $convert_success = false;
                                            break;
                                        }
                                    }
                                }
                            }
                            if ($convert_success) {
                                // we have all necessary files now, so we get a correct id and move the files
                                //
                                // in unlikely cases this section will cause trouble, i.e. when someone gets
                                // a new id while we are copying and before we insert into the database
                                echo "\n<br>\n<em>Copying generated image versions to their correct locations:</em> \n<br><br>\n";
                                // check if we already have a cache directory structure, otherwise create
                                $ret = check_dir($output_dir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR, true, true, 0755);
                                if (!$ret) {
                                    $errorstring = "Output directory cache does not exist or is not writable! \n<br>\n";
                                    die($errorstring);
                                }
                                // get new id for filename
                                $sql = "SELECT max(imageid)+1 FROM " . $db_prefix . "img";
                                $newid = $db->GetOne($sql);
                                if (!$newid) {
                                    $newid = 1;
                                }
                                // iterate through all available resolutions and check whether the target directory exists
                                // if yes, move the corresponding file, otherwise abort.
                                foreach ($resolutions as $res) {
                                    $ret = check_dir($output_dir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . $res, true, true, 0755);
                                    // check if cache subdirectories exist
                                    if (!$ret) {
                                        $errorstring = "Error creating directory for resolution " . $res . "\n<br>\n";
                                        $errorstring .= "or directory exists and is not writable\n<br>\n";
                                        die($errorstring);
                                    }
                                    $tmp_filename = $tmpdir . 'cache' . DIRECTORY_SEPARATOR . $res . DIRECTORY_SEPARATOR . $tmpid . '.jpg';
                                    $output_filename = $output_dir . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . $res . DIRECTORY_SEPARATOR . $query['collectionid'] . '-' . $newid . '.jpg';
                                    $ret = @copy($tmp_filename, $output_filename);
                                    if ($ret) {
                                        echo "Writing (" . $res . ")\t\t\t: success\n<br>\n";
                                        $convert_success = true;
                                    } else {
                                        echo "Writing (" . $res . ")\t\t\t: failed\n<br>\n";
                                        echo "Aborting...\n<br>\n";
                                        $convert_success = false;
                                        break;
                                    }
                                    // if unlink fails, we just loose some disk space
                                    @unlink($tmp_filename);
                                }
                                // if you decided to enable JPG-Versions in original resolution,
                                // uncomment the following lines to copy them to the target
                                // directory
                                /*
                                // copy the JPEG version with original resolution
                                if ($convert_success)
                                {
                                	$tmp_filename = $tmpdir.'cache'.DIRECTORY_SEPARATOR.$tmpid.'.jpg';
                                											
                                	$output_filename = $output_dir.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.$query['collectionid'].'-'.$newid.'.jpg';
                                		
                                	$ret = @copy($tmp_filename,$output_filename);
                                	
                                	$res = $img_data['width'].'x'.$img_data['height'];
                                	
                                	if ($ret)
                                	{
                                		echo ("Writing (".$res.")\t\t\t: success\n<br>\n");
                                		$convert_success = true;
                                	}
                                	else
                                	{
                                		echo ("Writing (".$res.")\t\t\t: failed\n<br>\n");
                                		echo ("Aborting...\n<br>\n");
                                		$convert_success = false;
                                	}
                                	
                                	// if unlink fails, we just loose some disk space
                                	@unlink($tmp_filename);	
                                }			
                                */
                                if ($config['imagick_mode'] != 'fast') {
                                    // try to delete temprorary base image
                                    // if unlink fails, we just loose some disk space
                                    @unlink($baseimage_filename);
                                }
                                if ($convert_success) {
                                    // all generated versions copied, so we finally try the original
                                    $newfilename = $query['collectionid'] . '-' . $newid . '.' . $formats_suffix[$img_data["mime"]];
                                    $source = $filename;
                                    $dest = $output_dir . DIRECTORY_SEPARATOR . $newfilename;
                                    $ret = @copy($filename, $output_dir . DIRECTORY_SEPARATOR . $newfilename);
                                    if (!$ret) {
                                        echo "Copying original image\t: failed\n<br>\n";
                                        echo "Aborting...\n<br>\n";
                                    } else {
                                        echo "Copying original image\t: success\n<br>\n";
                                        // set permissions
                                        $ret = @chmod($output_dir . DIRECTORY_SEPARATOR . $newfilename, 0755);
                                        if (!$ret) {
                                            echo "Modyfing permissions\t: failed\n<br>\n";
                                        } else {
                                            echo "Modyfing permissions\t: success\n<br>\n";
                                        }
                                        $ret = @unlink($filename);
                                        if (!$ret) {
                                            echo "Removing uploaded file\t: failed\n<br>\n";
                                        } else {
                                            echo "Removing uploaded file\t: success\n<br>\n";
                                        }
                                        echo "\n<br>\n";
                                        // here our critical section ends, after the next command, the image counter
                                        // is increased by 1
                                        // get current time
                                        $sql = "SELECT NOW()";
                                        $time = $db->GetOne($sql);
                                        $ret = insert_img($query['collectionid'], $newid, $baseid, $file, $img_data, $time);
                                        if (!$ret) {
                                            echo "Inserting into database (image)\t\t\t: failed\n<br>\n";
                                            echo "Aborting...\n<br>\n";
                                        } else {
                                            echo "Inserting into database (image)\t\t\t: success\n<br>\n";
                                            $ret = insert_meta($query['collectionid'], $newid, $time, $user['login'], $query['type']);
                                            if (!$ret) {
                                                echo "Inserting into database (meta)\t\t\t: failed\n<br>\n";
                                                echo "Aborting...\n<br>\n";
                                            } else {
                                                echo "Inserting into database (meta)\t\t\t: success\n<br>\n";
                                                if (!empty($query['group1id'])) {
                                                    $ret = insert_img_group($query['group1id'], $query['collectionid'], $newid);
                                                    if (!$ret) {
                                                        // a failed group insertion is non-critical, so we continue
                                                        echo "Inserting into database (group1)\t\t\t: failed\n<br>\n";
                                                    } else {
                                                        echo "Inserting into database (group1)\t\t\t: success\n<br>\n";
                                                    }
                                                }
                                                if (!empty($query['group2id'])) {
                                                    $ret = insert_img_group($query['group2id'], $query['collectionid'], $newid);
                                                    if (!$ret) {
                                                        // a failed group insertion is non-critical, so we continue
                                                        echo "Inserting into database (group2)\t\t\t: failed\n<br>\n";
                                                    } else {
                                                        echo "Inserting into database (group2)\t\t\t: success\n<br>\n";
                                                    }
                                                }
                                                if (!empty($query['group3id'])) {
                                                    $ret = insert_img_group($query['group3id'], $query['collectionid'], $newid);
                                                    if (!$ret) {
                                                        // a failed group insertion is non-critical, so we continue
                                                        echo "Inserting into database (group3)\t\t\t: failed\n<br>\n";
                                                    } else {
                                                        echo "Inserting into database (group3)\t\t\t: success\n<br>\n";
                                                    }
                                                }
                                            }
                                            // end of actions - output a newline
                                            echo "\n<br>\n";
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // Image is processed completely
            echo "<em>" . $file . " done</em>\n<br><br>\n";
            flush();
        }
    }
    // after all images have been processed, we try to clean up the upload directory, so things do not get inserted twice
    foreach ($remove as $removethis) {
        if (!empty($dir)) {
            $removedir = $dir . DIRECTORY_SEPARATOR . $removethis;
        } else {
            $removedir = $removethis;
        }
        if ($debug) {
            echo "Purging possible thumbs.db file from: " . $removedir . "\n<br>\n";
        }
        $ret = @unlink($removedir . 'Thumbs.db');
        if ($debug) {
            echo "Now removing: " . $removedir . "\n<br>\n";
        }
        $ret = @rmdir($removedir);
        if (!$ret) {
            $errorstring = "Notice: " . $removedir . " could not be removed\n<br>\n";
            $errorstring .= "Insufficient permission or directory not empty\n<br>\n";
            echo $errorstring;
        }
    }
    if ($debug) {
        echo "Purging possible thumbs.db file from: " . $dir . "\n<br>\n";
    }
    $ret = @unlink($dir . 'Thumbs.db');
    if ($debug) {
        echo "Now removing: " . $dir . "\n<br>\n";
    }
    $ret = @rmdir($dir);
    if (!$ret) {
        $errorstring = "Notice: " . $dir . " could not be removed\n<br>\n";
        $errorstring .= "Insufficient permission or directory not empty\n<br>\n";
        echo $errorstring;
    }
    return;
}