/** * processes one uploaded file. Creates folders, moves files * * @global type $UNC_GALLERY * @param type $i * @param type $overwrite * @return boolean */ function unc_uploads_process_file($i, $overwrite) { global $UNC_GALLERY; if ($UNC_GALLERY['debug']) { XMPP_ERROR_trace(__FUNCTION__, func_get_args()); } $action = false; //$_FILES(1) { // ["userImage"]=> array(5) { // ["name"]=> array(1) { [0]=> string(23) "2013-11-02 21.00.38.jpg" } // ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } // ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phptgNK2k" } // ["error"]=> array(1) { [0]=> int(0) } // ["size"]=> array(1) { [0]=> int(213485) } // } //} // the FILE array from the server if (isset($UNC_GALLERY['import'])) { $type = 'import'; $F = $UNC_GALLERY['import']; } else { $type = 'upload'; $F = $UNC_GALLERY['upload_files']; } // get the current path of the temp name if ($type == 'upload' && is_uploaded_file($F['tmp_name'][$i])) { $sourcePath = $F['tmp_name'][$i]; } else { if ($type == 'import' && is_file($F['tmp_name'][$i])) { $sourcePath = $F['tmp_name'][$i]; } else { return array('date' => false, 'action' => "Cannot find uploaded file {$F['tmp_name'][$i]}!"); } } // is there an error with the file? if ($F["error"][$i] > 0) { return array('date' => false, 'action' => "Unable to read the file, upload cancelled of file " . $F['name'][$i]); } // if there is an imagesize, we have a valid image $image_check = getimagesize($F['tmp_name'][$i]); if (!$image_check) { return array('date' => false, 'action' => "Not image file, upload cancelled of file " . $F['name'][$i]); } // let's set variables for the currently uploaded file so we do not have to get the same data twice. $UNC_GALLERY['upload_file_info'] = array('image_size' => $image_check, 'temp_name' => $F['tmp_name'][$i], 'type' => $type); $original_width = $image_check[0]; $original_height = $image_check[1]; // let's make sure the image is not 0-size if ($original_width == 0 || $original_height == 0) { echo unc_display_errormsg("Image size {$F['name'][$i]} = 0"); return false; } // let's shrink only if we need to if ($original_width == $UNC_GALLERY['thumbnail_height'] && $original_height == $UNC_GALLERY['thumbnail_height']) { return array('date' => false, 'action' => "Image size {$F['name'][$i]} is smaller than thumbnail!"); } // get imagetype $exif_imagetype = $image_check[2]; if (!$exif_imagetype) { return array('date' => false, 'action' => "Could not determine image type of file " . $F['name'][$i] . ", upload cancelled!"); } $UNC_GALLERY['upload_file_info']['exif_imagetype'] = $exif_imagetype; // get mime-type and check if it's in the list of valid ones $mime_type = image_type_to_mime_type($exif_imagetype); if (!isset($mime_type, $UNC_GALLERY['valid_filetypes'])) { return array('date' => false, 'action' => "Invalid file type :" . $F["type"][$i]); } else { // get extension for optional resize $extension = $UNC_GALLERY['valid_filetypes'][$mime_type]; } $UNC_GALLERY['upload_file_info']['extension'] = $extension; // we set the new filename of the image including extension so there is no guessing $file_no_ext = pathinfo($F['name'][$i], PATHINFO_FILENAME); $target_filename = $file_no_ext . "." . $extension; $UNC_GALLERY['upload_file_info']['target_filename'] = $target_filename; // we need the exif date to know when the image was taken $date_str = unc_image_date($sourcePath); if (!$date_str) { return array('date' => false, 'action' => "Cannot read EXIF or IPCT of file {$sourcePath}"); } $UNC_GALLERY['upload_file_info']['date_str'] = $date_str; $date_check = date_create($date_str); if (!$date_check) { return array('date' => false, 'action' => "'{$date_str}' is invalid date in EXIF or IPCT"); } // echo "File date is $date_str"; // create all the by-day folders $date_obj = unc_date_folder_create($date_str); // if it failed return back if (!$date_obj) { return array('date' => false, 'action' => "Could not create date folders!"); } // get the upload directory $dirPath = $UNC_GALLERY['upload_path']; // let's make the path with system-specific dir. separators $format = implode("/", array('Y', 'm', 'd')); $date_str_folder = $date_obj->format($format); // echo "Folder date is $date_str_folder<br>"; $target_subfolder = $dirPath . "/" . $UNC_GALLERY['photos'] . "/" . $date_str_folder; $thumb_subfolder = $dirPath . "/" . $UNC_GALLERY['thumbnails'] . "/" . $date_str_folder; $new_path = $target_subfolder . "/" . $target_filename; $new_thumb_path = $thumb_subfolder . "/" . $target_filename; // act on overwrite options if ($overwrite == 'new' && file_exists($new_path)) { return array('date' => false, 'action' => "skipped file {$target_filename}, already exists<br>"); } else { if ($overwrite == 'existing' && !file_exists($new_path)) { return array('date' => false, 'action' => "skipped file {$target_filename}, is new<br>"); } else { if ($overwrite == 'existing' && file_exists($new_path)) { unlink($new_path); $action = 'overwritten'; } else { if ($overwrite == 'all' && file_exists($new_path)) { unlink($new_path); $action = 'overwritten'; } } } } // finally, move the file if ($UNC_GALLERY['picture_long_edge'] > 0) { $resize_check = unc_import_image_resize($F['tmp_name'][$i], $new_path, $UNC_GALLERY['picture_long_edge'], $extension, $UNC_GALLERY['image_quality'], 'max_height'); if (!$resize_check) { return array('date' => false, 'action' => "Could not resize {$F['name'][$i]} from {$F['tmp_name'][$i]} to {$new_path}"); } } else { if ($type == 'upload') { $rename_chk = move_uploaded_file($F['tmp_name'][$i], $new_path); } else { // import $rename_chk = copy($F['tmp_name'][$i], $new_path); } if (!$rename_chk) { return array('date' => false, 'action' => "Could not move {$F['name'][$i]} from {$F['tmp_name'][$i]} to {$new_path}"); } } // chmod file to make sure it cannot be executed $check_chmod = chmod($new_path, 0644); if (!$check_chmod) { return array('date' => false, 'action' => "Could not chmod 644 file {$new_path}"); } // now make the thumbnail $thumb_format = $UNC_GALLERY['thumbnail_format']; $check = unc_import_image_resize($new_path, $new_thumb_path, $UNC_GALLERY['thumbnail_height'], $UNC_GALLERY['thumbnail_ext'], $UNC_GALLERY['thumbnail_quality'], $thumb_format); if (!$check) { return array('date' => false, 'action' => "Could not create the thumbnail for {$F['tmp_name'][$i]} / {$new_thumb_path}!"); } else { if (!$action) { $action = 'written'; } } $check_xmp = unc_image_info_write($new_path); if (!$check_xmp) { return array('date' => false, 'action' => "Could not write XMP/IPCT/EXIF data to file"); } return array('date' => $date_str, 'action' => $target_filename . ": " . $action); }
function unc_image_info_read($file_path) { global $UNC_GALLERY, $UNC_FILE_DATA, $wpdb; if ($UNC_GALLERY['debug']) { XMPP_ERROR_trace(__FUNCTION__, $file_path); } if (!file_exists($file_path)) { if ($UNC_GALLERY['debug']) { XMPP_ERROR_trigger("tried to read info for non-existing file!"); } return false; } $folder_info = pathinfo($file_path); $date_str = unc_tools_folder_date($folder_info['dirname']); $date_path = str_replace("-", "/", $date_str); $file_name = $folder_info['basename']; $img_table_name = $wpdb->prefix . "unc_gallery_img"; $att_table_name = $wpdb->prefix . "unc_gallery_att"; $sql = "SELECT `att_group`, `att_name`, `att_value` FROM {$img_table_name}\r\n LEFT JOIN {$att_table_name} ON id=file_id\r\n WHERE file_name = '{$file_name}' AND file_time LIKE '{$date_str}%';"; $file_data = $wpdb->get_results($sql); // TODO: check if the file exists 2x for sanity check, here or somewhere else if (count($file_data) == 0) { if ($UNC_GALLERY['debug']) { XMPP_ERROR_trace("File not found in DB, reading from file"); } $check = unc_image_info_write($file_path); if (!$check) { if ($UNC_GALLERY['debug']) { XMPP_ERROR_trigger("could not write file data to database!"); } } $file_code = md5($date_path . "/" . $file_name . ".php"); return $UNC_FILE_DATA[$file_code]; } $F = array(); foreach ($file_data as $D) { $field = $D->att_name; $group = $D->att_group; $value = $D->att_value; if ($group == 'default') { if (isset($F[$field])) { if (is_array($F[$field])) { $F[$field][] = $value; } else { $F[$field] = array($F[$field], $value); } } else { $F[$field] = $value; } } else { if (isset($F[$group][$field])) { if (is_array($F[$group][$field])) { $F[$group][$field][] = $value; } else { $F[$group][$field] = array($F[$group][$field], $value); } } else { $F[$group][$field] = $value; } } } if (count($F) == 0) { if ($UNC_GALLERY['debug']) { XMPP_ERROR_trigger("did not read any information from file!"); } } $file_code = md5($date_path . "/" . $file_name . ".php"); $UNC_FILE_DATA[$file_code] = $F; return $F; }
/** * build the missing data * * @global type $UNC_GALLERY */ function unc_gallery_admin_rebuild_data() { global $UNC_GALLERY, $wpdb; if ($UNC_GALLERY['debug']) { XMPP_ERROR_trace(__FUNCTION__, func_get_args()); } ob_clean(); $max_time = ini_get('max_execution_time'); if (!current_user_can('manage_options')) { echo "Cannot rebuild data, you are not admin!"; wp_die(); } $dirPath = $UNC_GALLERY['upload_path']; // let's count the number of files in the database so // we get an image how much work is to do $count_files_sql = "SELECT count(id) AS counter FROM " . $wpdb->prefix . "unc_gallery_img;"; $file_counter = $wpdb->get_results($count_files_sql, 'ARRAY_A'); $count = $file_counter[0]['counter']; // get the Process ID for the Ajax live update $process_id = filter_input(INPUT_POST, 'process_id'); // send the first update $process_step_id = unc_tools_progress_update($process_id, "Cleared existing data"); // TODO Check where do we delete empty folders? // unc_tools_folder_delete_empty($data_folder); // iterate all image folders $photo_folder = $dirPath . "/" . $UNC_GALLERY['photos']; $target_folders = unc_tools_recurse_folders($photo_folder); // calculate progress update percentages $overall_one_percent = 100 / $count; $overall_percentage = 0; $text = ''; foreach ($target_folders as $date => $folder) { $process_step_id++; $text = "Processing {$date}: <span class=\"file_progress\" style=\"width:0%\">0 %</span>"; $process_step_id = unc_tools_progress_update($process_id, $text, $overall_percentage); // iterate all files in a folder, write file info to DB $folder_files = glob($folder . "/*"); $folder_file_count = count($folder_files); $file_one_percent = 100 / $folder_file_count; $folder_percentage = 0; foreach ($folder_files as $image_file) { if (!is_dir($image_file)) { // TODO: ERror in case the info cannot be written unc_image_info_write($image_file); $folder_percentage += $file_one_percent; $overall_percentage += $overall_one_percent; } $folder_percentage_text = intval($folder_percentage); $text = "Processing {$date}: <span class=\"file_progress\" style=\"width:{$folder_percentage_text}%\">{$folder_percentage_text} %</span>"; unc_tools_progress_update($process_id, $text, $overall_percentage, $process_step_id); } $text = "Processing {$date}: <span class=\"file_progress\" style=\"width:100%\">100 %</span>"; unc_tools_progress_update($process_id, $text, $overall_percentage, $process_step_id); } unc_tools_progress_update($process_id, "Done!", 100); // this signals to the JS function that we can terminate the process_get loop unc_tools_progress_update($process_id, false); wp_die(); }