Ejemplo n.º 1
0
/**
 * Analyse the shortcode vars relevant for the day type.
 * @global type $UNC_GALLERY
 * @param type $a
 * @return boolean
 */
function unc_day_var_init($a)
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    // we convert the start time and end time to unix timestamp for better
    // comparison
    $UNC_GALLERY['display']['range'] = array('start_time' => false, 'end_time' => false);
    foreach ($UNC_GALLERY['display']['range'] as $key => $value) {
        if ($a[$key]) {
            //re-name the variable
            $UNC_GALLERY['display']['date_range'][$key] = $a[$key];
            // convert to UNIX timestamp
            $dtime = DateTime::createFromFormat("Y-m-d G:i:s", $a[$key]);
            // TODO: catch here if the date is invalid
            $UNC_GALLERY['display']['range'][$key] = $dtime->getTimestamp();
            // get the date for the same
            $var_name = 'date_' . $key;
            ${$var_name} = substr($a[$key], 0, 10);
        }
    }
    if ($a['end_time']) {
        $date_end_time = substr(trim($a['end_time']), 0, 10);
    }
    if ($a['start_time']) {
        $date_start_time = substr(trim($a['start_time']), 0, 10);
    }
    $UNC_GALLERY['display']['date_description'] = false;
    // false by default, only true if not set explicitly (latest or random date)
    if ($a['end_time'] && $a['start_time']) {
        $date_arr = unc_day_date_span($date_start_time, $date_end_time);
        $UNC_GALLERY['display']['dates'] = $date_arr;
    } else {
        if ($a['end_time']) {
            $date_str = $date_end_time;
            $UNC_GALLERY['display']['dates'] = array($date_str);
        } else {
            if ($a['start_time']) {
                $date_str = $date_start_time;
                $UNC_GALLERY['display']['dates'] = array($date_str);
            } else {
                if ($a['date'] && in_array($a['date'], $UNC_GALLERY['keywords']['date'])) {
                    // we have a latest or random date
                    // get the latest or a random date if required
                    if ($a['date'] == 'random') {
                        $date_str = unc_day_date_random();
                    } else {
                        if ($a['date'] == 'latest') {
                            $date_str = unc_day_date_latest();
                        }
                    }
                    if (!$date_str) {
                        // we have no images in the database
                        $UNC_GALLERY['display']['files'] = array();
                        $UNC_GALLERY['display']['file'] = false;
                        $UNC_GALLERY['display']['dates'] = array();
                    } else {
                        $UNC_GALLERY['display']['date_description'] = true;
                        $UNC_GALLERY['display']['dates'] = array($date_str);
                    }
                } else {
                    if ($a['date'] && strstr($a['date'], ",")) {
                        // we have several dates in the string
                        $dates = explode(",", $a['date']);
                        if (count($dates) > 2) {
                            echo unc_display_errormsg("You can only enter 2 dates!");
                            return false;
                        }
                        // validate both dates
                        $date_str1 = unc_day_validate_date(trim($dates[0]));
                        $date_str2 = unc_day_validate_date(trim($dates[1]));
                        if (!$date_str1 || !$date_str2) {
                            echo unc_display_errormsg("All dates needs to be in the format '2016-01-31'");
                            return false;
                        }
                        // create a list of dates between the 1st and the 2nd
                        $date_arr = unc_day_date_span($dates[0], $dates[1]);
                        $UNC_GALLERY['display']['dates'] = $date_arr;
                    } else {
                        if ($a['date']) {
                            $date_str = unc_day_validate_date($a['date']);
                            if (!$date_str) {
                                echo unc_display_errormsg("All dates needs to be in the format '2016-01-31'");
                                return false;
                            }
                            $UNC_GALLERY['display']['dates'] = array($date_str);
                        }
                    }
                }
            }
        }
    }
    // get the actual images
    if ($a['file'] && $a['type'] == 'day') {
        $UNC_GALLERY['display']['file'] = unc_tools_filename_validate(trim($a['file']));
        $UNC_GALLERY['display']['files'] = array();
    } else {
        $UNC_GALLERY['display']['file'] = false;
        $UNC_GALLERY['display']['files'] = unc_day_images_list();
    }
    $UNC_GALLERY['display']['date_selector'] = false;
    if (in_array('calendar', $UNC_GALLERY['display']['options'])) {
        $UNC_GALLERY['display']['date_selector'] = 'calendar';
    } else {
        if (in_array('datelist', $UNC_GALLERY['display']['options'])) {
            $UNC_GALLERY['display']['date_selector'] = 'datelist';
        }
    }
    if (count($UNC_GALLERY['display']['files']) == 0 && !$UNC_GALLERY['display']['file']) {
        if ($UNC_GALLERY['debug']) {
            XMPP_ERROR_trace("No files found in date range!");
        }
        if ($UNC_GALLERY['no_image_alert'] == 'error') {
            $UNC_GALLERY['errors'][] = unc_display_errormsg("No images found for this date!");
        } else {
            if ($UNC_GALLERY['no_image_alert'] == 'not_found') {
                $UNC_GALLERY['errors'][] = "No images available.";
            }
        }
        return false;
    }
    return true;
}
Ejemplo n.º 2
0
/**
 * converts a 2013-12-12 to 2013/12/12 and checks if the file exists
 *
 * @global type $UNC_GALLERY
 * @param type $date
 * @return type
 */
function unc_tools_date_path($date)
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    $date_obj = new DateTime($date . " 00:00:00");
    if ($date_obj) {
        $format = implode("/", array('Y', 'm', 'd'));
        $date_str = $date_obj->format($format);
        $photo_folder = $UNC_GALLERY['upload_path'] . "/" . $UNC_GALLERY['photos'];
        if (!file_exists($photo_folder . "/" . $date_str)) {
            if ($UNC_GALLERY['no_image_alert'] == 'error') {
                $UNC_GALLERY['errors'][] = unc_display_errormsg("No images found for this date!");
            } else {
                if ($UNC_GALLERY['no_image_alert'] == 'not_found') {
                    $UNC_GALLERY['errors'][] = "No images available for {$date}";
                }
            }
            return false;
        }
    } else {
        $UNC_GALLERY['errors'][] = unc_display_errormsg("Date not found (invalid date)");
        return false;
    }
    return $date_str;
}
Ejemplo n.º 3
0
/**
 * 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);
}
Ejemplo n.º 4
0
/**
 * Process and validate $UNC_GALLERY['display'] settings
 *
 * @global type $UNC_GALLERY
 * @param type $atts
 * @return type
 */
function unc_gallery_display_var_init($atts = array())
{
    global $UNC_GALLERY, $post;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    $possible_attributes = array('type' => 'day', 'date' => 'latest', 'file' => false, 'featured' => false, 'options' => false, 'start_time' => false, 'end_time' => false, 'description' => false, 'details' => false, 'echo' => false, 'offset' => false, 'limit_rows' => false, 'limit_images' => false, 'filter' => false, 'files' => false);
    // defaults
    $UNC_GALLERY['not_shown'] = false;
    $UNC_GALLERY['errors'] = array();
    // check if all the attributes exist
    foreach ($atts as $key => $value) {
        if (!isset($possible_attributes[$key])) {
            if ($UNC_GALLERY['debug']) {
                XMPP_ERROR_trigger("You have an invalid setting '{$key}' in your gallery shortcode!");
            }
            echo unc_display_errormsg("You have an invalid setting '{$key}' in your gallery shortcode!");
            return false;
        }
    }
    // parse the attributes
    $a = shortcode_atts($possible_attributes, $atts);
    $type = $a['type'];
    // several featured
    // we do not need to validate featured files since we only compare with the list
    // of found files in a folder
    if (strstr($a['featured'], ",")) {
        $UNC_GALLERY['display']['featured_image'] = explode(",", trim($a['featured']));
    } else {
        if ($a['featured']) {
            $UNC_GALLERY['display']['featured_image'] = array(trim($a['featured']));
        } else {
            $UNC_GALLERY['display']['featured_image'] = array();
        }
    }
    // there can be several options, separated by space
    if (!$a['options']) {
        $options = array();
    } else {
        $options = explode(" ", trim($a['options']));
    }
    $UNC_GALLERY['display']['options'] = $options;
    $UNC_GALLERY['display']['echo'] = trim($a['echo']);
    // icon or image?
    $thumb = false;
    if ($type == 'icon') {
        $thumb = true;
    }
    $UNC_GALLERY['display']['filter'] = trim($a['filter']);
    $UNC_GALLERY['display']['description'] = trim($a['description']);
    $UNC_GALLERY['display']['limit_rows'] = trim($a['limit_rows']);
    $UNC_GALLERY['display']['limit_images'] = trim($a['limit_images']);
    // date
    $keywords = $UNC_GALLERY['keywords'];
    if ($type == 'day') {
        $check = unc_day_var_init($a);
    } else {
        if ($type == 'filter') {
            $check = unc_filter_var_init($a);
        }
    }
    if (!$check) {
        // there was some critical error, let's return that
        return false;
    }
    // details
    $details_raw = $a['details'];
    $UNC_GALLERY['display']['details'] = false;
    if ($details_raw) {
        // explode by colon
        $file_details = explode(";", $details_raw);
        if (count($file_details) == 0) {
            echo unc_display_errormsg("File details are invalid!");
            return;
        }
        foreach ($file_details as $file_detail) {
            $tmp_arr = explode(":", $file_detail);
            if (count($tmp_arr) !== 2) {
                echo unc_display_errormsg("File details are invalid!");
                return;
            }
            $details_filename = trim($tmp_arr[0]);
            $details_description = trim($tmp_arr[1]);
            $UNC_GALLERY['display']['details'][$details_filename] = $details_description;
        }
    }
    // options
    if (!isset($keywords['type'][$type])) {
        echo unc_display_errormsg("You have an invalid type value in your tag." . "<br>Valid options are: " . implode(", ", $keywords['type']));
        return false;
    }
    $possible_type_options = $keywords['type'][$type];
    foreach ($UNC_GALLERY['display']['options'] as $option) {
        if (!in_array($option, $possible_type_options)) {
            echo unc_display_errormsg("You have an invalid option for the display type \"option\" in your tag." . "<br>Valid options are: " . implode(", ", $keywords['type'][$type]));
            return false;
        }
    }
    $UNC_GALLERY['display']['type'] = $a['type'];
    // TODO: this is likely redundant
    $UNC_GALLERY['display']['link'] = false;
    if (in_array('link', $UNC_GALLERY['display']['options'])) {
        $UNC_GALLERY['display']['link'] = 'image';
    }
    $UNC_GALLERY['display']['slideshow'] = false;
    if (in_array('slideshow', $UNC_GALLERY['display']['options'])) {
        $UNC_GALLERY['display']['slideshow'] = true;
    }
    // this is needed to the JS function calls used for the displays
    $slug = '';
    if (isset($post->post_name)) {
        $slug = str_replace("-", "_", $post->post_name);
    } else {
        $slug = 'none';
    }
    // we list all the slugs to make sure we do not re-use
    // practically we could just use incremental numbers, but debugging is easier with a name
    if (isset($UNC_GALLERY['slugs']) && in_array($slug, $UNC_GALLERY['slugs'])) {
        $slug = $slug . count($UNC_GALLERY['slugs']);
    }
    $UNC_GALLERY['slugs'][] = $slug;
    $UNC_GALLERY['display']['slug'] = $slug;
    return true;
}
Ejemplo n.º 5
0
/**
 * standard wordpress function to activate the plugin.
 * creates the uploads folder
 *
 * @global type $UNC_GALLERY
 */
function unc_gallery_plugin_activate()
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    if (!file_exists($UNC_GALLERY['upload_path'])) {
        $result = mkdir($UNC_GALLERY['upload_path'], 0755);
        // check success
        if (!$result) {
            echo unc_display_errormsg("There was an error creating the upload folder {$UNC_GALLERY['upload_path']}!");
        }
    }
    // create the DB structure
    unc_mysql_db_create();
}
Ejemplo n.º 6
0
/**
 * this will manage the settings
 */
function unc_gallery_admin_settings()
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    remove_filter('the_content', 'wpautop');
    echo '<div class="wrap">
    <h2>Uncovery Gallery</h2>
    <script type="text/javascript">
        jQuery(document).ready(function() {
        // Initialize jquery-ui tabs
        jQuery(\'.unc_jquery_tabs\').tabs();
        // Fade in sections that we wanted to pre-render
        jQuery(\'.unc_fade_in\').fadeIn(\'fast\');
        });
    </script>';
    if (!function_exists('exif_read_data')) {
        echo unc_display_errormsg("EXIF Library does not exist! This plugin will not work properly! See <a href=\"http://php.net/manual/en/book.exif.php\">http://php.net/manual/en/book.exif.php</a>");
    }
    echo '<div class="unc_jquery_tabs unc_fade_in">
    <ul>' . "\n";
    # Set up tab titles
    echo "<li><a href='#tab1'><span>Settings</span></a></li>\n" . "<li><a href='#tab2'><span>Upload</span></a></li>\n" . "<li><a href='#tab3'><span>Manage Images</span></a></li>\n" . "<li><a href='#tab4'><span>Maintenance</span></a></li>\n" . "<li><a href='#tab5'><span>Documentation</span></a></li>\n" . "</ul>\n";
    echo "<div id='tab1'>\n";
    echo '<form method="post" action="options.php">' . "\n";
    settings_fields('unc_gallery_settings_page');
    do_settings_sections('unc_gallery_settings_page');
    submit_button();
    echo "</form>\n";
    echo "</div>\n";
    echo "<div id='tab2'>\n";
    echo unc_uploads_form();
    echo "</div>\n";
    echo "<div id='tab3'>\n";
    echo unc_gallery_admin_display_images();
    echo "</div>\n";
    echo "<div id='tab4'>\n";
    echo unc_gallery_admin_maintenance();
    echo "</div>\n";
    echo "<div id='tab5'>\n";
    echo unc_gallery_admin_show_documentation();
    echo "</div>\n";
    echo "</div>";
}
Ejemplo n.º 7
0
function unc_filter_check_type($group, $key)
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    // get the possible filter values from the codes
    $codes = $UNC_GALLERY['codes'];
    if (!isset($codes[$group])) {
        $valid_filter_types_arr = array_keys($codes);
        $valid_filter_types = implode(",", $valid_filter_types_arr);
        return unc_display_errormsg("You have an invalid filter type set. Possible values are: {$valid_filter_types}");
    }
    $code_group = $codes[$group];
    if (!isset($code_group[$key])) {
        $valid_filter_keys_arr = array_keys($code_group);
        $valid_filter_keys = implode(",", $valid_filter_keys_arr);
        return unc_display_errormsg("You have an invalid filter key set. Possible values are: {$valid_filter_keys}");
    }
    return false;
}