Example #1
0
/**
 * Delete an image
 *
 * @global array $UNC_GALLERY
 */
function unc_tools_image_delete()
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    if (!is_admin() === true) {
        ob_clean();
        echo "You are not admin!";
        wp_die();
    }
    $file_name_raw = filter_input(INPUT_GET, 'file_name', FILTER_SANITIZE_STRING);
    if (!($file_name = unc_tools_filename_validate($file_name_raw))) {
        ob_clean();
        echo "File name {$file_name_raw} is not allowed!";
        wp_die();
    }
    $date_wrong = filter_input(INPUT_GET, 'date', FILTER_SANITIZE_STRING);
    $date_str = str_replace("-", "/", $date_wrong);
    $paths = array($UNC_GALLERY['photos'] => $file_name, $UNC_GALLERY['thumbnails'] => $file_name);
    foreach ($paths as $path => $del_file_name) {
        $full_path = $UNC_GALLERY['upload_path'] . "/" . $path . "/" . $date_str . "/" . $del_file_name;
        unc_image_info_exiftool($full_path);
        wp_die();
        if (file_exists($full_path)) {
            $check = unlink($full_path);
            if ($check) {
                ob_clean();
                echo "File Deleted!";
            } else {
                ob_clean();
                echo "File delete failed!";
                wp_die();
            }
        } else {
            // we cannot stop at an error so there are no leftover files
            echo "File name {$full_path} could not be found!";
        }
    }
    // delete file data
    $check = unc_image_info_delete($file_name, $date_str);
    if (!$check) {
        ob_clean();
        echo "File data could not be deleted: {$file_name} {$date_str}";
        wp_die();
    }
    unc_tools_folder_delete_empty($UNC_GALLERY['upload_path']);
    unc_display_ajax_folder();
}
Example #2
0
/**
 * On upload, get all information from a file and write it to a PHP file
 * TODO: Move this to MySQL / SQLite
 *
 * @global type $UNC_GALLERY
 * @param type $file_path
 * @return boolean
 */
function unc_image_info_write($file_path)
{
    global $UNC_GALLERY, $UNC_FILE_DATA, $wpdb;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    if (!file_exists($file_path)) {
        if ($UNC_GALLERY['debug']) {
            XMPP_ERROR_trigger("tried to write info for non-existing file!", $file_path);
        }
        return false;
    }
    $exif = unc_exif_get($file_path);
    $xmp = unc_xmp_get($file_path);
    $ipct = unc_ipct_get($file_path);
    if (!isset($exif['created'])) {
        $file_date = unc_ipct_convert_date($ipct['created_date'], $ipct['created_time']);
    } else {
        $file_date = $exif['created'];
    }
    $dtime = DateTime::createFromFormat("Y-m-d G:i:s", $file_date);
    $time_stamp = $dtime->getTimestamp();
    // time stamp is easier to compare
    $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'];
    $orientation = 'landscape';
    if ($exif['file_width'] < $exif['file_height']) {
        $orientation = 'portrait';
    }
    // remove existing file info
    $check = unc_image_info_delete($file_name, $file_date);
    if (!$check) {
        if ($UNC_GALLERY['debug']) {
            XMPP_ERROR_trace("Data did not exist in DB, nothing to delete", $file_path);
        }
    }
    $photo_url = content_url($UNC_GALLERY['upload_folder'] . "/" . $UNC_GALLERY['photos'] . "/{$date_path}/{$file_name}");
    $thumb_url = content_url($UNC_GALLERY['upload_folder'] . "/" . $UNC_GALLERY['thumbnails'] . "/{$date_path}/{$file_name}");
    $file_code = md5($date_path . "/" . $file_name . ".php");
    // insert file into DB
    $wpdb->insert($wpdb->prefix . "unc_gallery_img", array('file_time' => $file_date, 'file_name' => $file_name, 'file_path' => $file_path));
    $insert_id = $wpdb->insert_id;
    if ($insert_id == 0) {
        if ($UNC_GALLERY['debug']) {
            XMPP_ERROR_trace("tried to write info for file, already exists in database", $file_path);
        }
        return false;
    }
    $default = array('data_version' => $UNC_GALLERY['data_version'], 'file_name' => $file_name, 'file_path' => $file_path, 'thumb_url' => $thumb_url, 'file_url' => $photo_url, 'time_stamp' => $time_stamp, 'file_date' => $file_date, 'date_str' => substr($file_date, 0, 10), 'orientation' => $orientation);
    $data_sets = array('default' => $default, 'exif' => $exif, 'xmp' => $xmp, 'ipct' => $ipct);
    foreach ($data_sets as $set_name => $set) {
        foreach ($set as $name => $value) {
            // insert into DB
            if (is_array($value)) {
                // TODO: Create a single SQL line instead of entering every line by itself
                // should make the process much faster
                foreach ($value as $arr_value) {
                    $wpdb->insert($wpdb->prefix . "unc_gallery_att", array('file_id' => $insert_id, 'att_group' => $set_name, 'att_name' => $name, 'att_value' => $arr_value));
                    $insert_id2 = $wpdb->insert_id;
                    if ($insert_id2 == 0) {
                        if ($UNC_GALLERY['debug']) {
                            XMPP_ERROR_trace("tried to write array info for attributes, already exists in database", $file_path);
                        }
                        return false;
                    }
                }
            } else {
                $wpdb->insert($wpdb->prefix . "unc_gallery_att", array('file_id' => $insert_id, 'att_group' => $set_name, 'att_name' => $name, 'att_value' => $value));
                $insert_id2 = $wpdb->insert_id;
                if ($insert_id2 == 0) {
                    if ($UNC_GALLERY['debug']) {
                        XMPP_ERROR_trace("tried to write string info for attributes, already exists in database", $file_path);
                    }
                    return false;
                }
            }
            // insert into global var
            // we split it like this since that was the legacy format.
            if ($set_name == 'default') {
                $UNC_FILE_DATA[$file_code][$name] = $value;
            } else {
                $UNC_FILE_DATA[$file_code][$set_name][$name] = $value;
            }
        }
    }
    return true;
}