function wppa_import_dir_to_album($file, $parent)
{
    global $photocount;
    global $wpdb;
    global $wppa_session;
    // Session should survive the default hour
    wppa_extend_session();
    // see if album exists
    if (is_dir($file)) {
        // Check parent
        if (wppa_switch('import_parent_check')) {
            $alb = wppa_get_album_id(basename($file), $parent);
            // If parent = 0 ( top-level album ) and album not found,
            // try a 'separate' album ( i.e. parent = -1 ) with this name
            if (!$alb && $parent == '0') {
                $alb = wppa_get_album_id(basename($file), '-1');
            }
        } else {
            $alb = wppa_get_album_id(basename($file), false);
        }
        if (!$alb) {
            // Album must be created
            $name = basename($file);
            $uplim = wppa_opt('upload_limit_count') . '/' . wppa_opt('upload_limit_time');
            $alb = wppa_create_album_entry(array('name' => $name, 'a_parent' => $parent));
            if ($alb === false) {
                wppa_error_message(__('Could not create album.', 'wp-photo-album-plus') . '<br/>Query = ' . $query);
                wp_die('Sorry, cannot continue');
            } else {
                wppa_set_last_album($alb);
                wppa_flush_treecounts($alb);
                wppa_index_add('album', $alb);
                wppa_create_pl_htaccess();
                wppa_ok_message(__('Album #', 'wp-photo-album-plus') . ' ' . $alb . ' ( ' . $name . ' ) ' . __('Added.', 'wp-photo-album-plus'));
                if (wppa_switch('newpag_create') && $parent <= '0') {
                    // Create post object
                    $my_post = array('post_title' => $name, 'post_content' => str_replace('w#album', $alb, wppa_opt('newpag_content')), 'post_status' => wppa_opt('newpag_status'), 'post_type' => wppa_opt('newpag_type'));
                    // Insert the post into the database
                    $pagid = wp_insert_post($my_post);
                    if ($pagid) {
                        wppa_ok_message(sprintf(__('Page <a href="%s" target="_blank" >%s</a> created.', 'wp-photo-album-plus'), home_url() . '?page_id=' . $pagid, $name));
                        $wpdb->query($wpdb->prepare("UPDATE `" . WPPA_ALBUMS . "` SET `cover_linkpage` = %s WHERE `id` = %s", $pagid, $alb));
                    } else {
                        wppa_error_message(__('Could not create page.', 'wp-photo-album-plus'));
                    }
                }
            }
        }
        // Now import the files
        $photofiles = glob($file . '/*');
        if ($photofiles) {
            foreach ($photofiles as $photofile) {
                if (!is_dir($photofile)) {
                    if (!isset($wppa_session[$photofile]) || !wppa_switch('keep_import_files')) {
                        if (wppa_albumphoto_exists($alb, basename($photofile))) {
                            if (!wppa_switch('keep_import_files')) {
                                wppa_warning_message('Photo ' . basename($photofile) . ' already exists in album ' . $alb . '. Removed. (2)');
                            }
                        } else {
                            $bret = wppa_insert_photo($photofile, $alb, basename($photofile));
                            $photocount++;
                        }
                        if (!wppa_switch('keep_import_files')) {
                            @unlink($photofile);
                        }
                        $wppa_session[$photofile] = true;
                    }
                    if (wppa_is_time_up($photocount)) {
                        return false;
                    }
                }
            }
        }
        // Now go deeper, process the subdirs
        $subdirs = glob($file . '/*');
        if ($subdirs) {
            foreach ($subdirs as $subdir) {
                if (is_dir($subdir)) {
                    if (basename($subdir) != '.' && basename($subdir) != '..') {
                        $bret = wppa_import_dir_to_album($subdir, $alb);
                        if (!$bret) {
                            return false;
                        }
                        // Time out
                    }
                }
            }
        }
        @rmdir($file);
        // Try to remove dir, ignore error
    } else {
        wppa_dbg_msg('Invalid file in wppa_import_dir_to_album(): ' . $file);
        return false;
    }
    return true;
}
function wppa_remake_files($alb = '', $pid = '')
{
    global $wpdb;
    // Init
    $count = '0';
    // Find the album( s ) if any
    if (!$alb && !$pid) {
        $start_time = get_option('wppa_remake_start', '0');
        $albums = $wpdb->get_results('SELECT `id` FROM `' . WPPA_ALBUMS . '`', ARRAY_A);
    } elseif ($alb) {
        $start_time = get_option('wppa_remake_start_album_' . $alb, '0');
        $albums = array(array('id' => $alb));
    } else {
        $albums = false;
    }
    // Do it with albums
    if ($albums) {
        foreach ($albums as $album) {
            $source_dir = wppa_get_source_album_dir($album['id']);
            if (is_dir($source_dir)) {
                $files = glob($source_dir . '/*');
                if ($files) {
                    foreach ($files as $file) {
                        if (!is_dir($file)) {
                            $filename = basename($file);
                            $photos = $wpdb->get_results($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `filename` = %s OR ( `filename` = '' AND `name` = %s )", $filename, $filename), ARRAY_A);
                            if ($photos) {
                                foreach ($photos as $photo) {
                                    // Photo exists
                                    $modified_time = $photo['modified'];
                                    if ($modified_time < $start_time) {
                                        wppa_update_single_photo($file, $photo['id'], $filename);
                                        //							$wpdb->query( $wpdb->prepare( 'UPDATE `'.WPPA_PHOTOS.'` SET `modified` = %s WHERE `id` = %s', time(), $photo['id'] ) );
                                        $count++;
                                    }
                                    if (wppa_is_time_up($count)) {
                                        return false;
                                    }
                                }
                            } else {
                                // No photo yet
                                if (wppa_switch('remake_add')) {
                                    wppa_insert_photo($file, $album['id'], $filename);
                                    //	$wpdb->query( $wpdb->prepare( 'UPDATE `'.WPPA_PHOTOS.'` SET `modified` = %s WHERE `id` = %s', time(), $photo['id'] ) );
                                    $count++;
                                }
                            }
                            if (wppa_is_time_up($count)) {
                                return false;
                            }
                        }
                    }
                }
            }
        }
    } elseif ($pid) {
        $photo = $wpdb->get_row($wpdb->prepare("SELECT * FROM `" . WPPA_PHOTOS . "` WHERE `id` = %s", $pid), ARRAY_A);
        if ($photo) {
            $file = wppa_get_source_path($photo['id']);
            if (is_file($file)) {
                $name = $photo['filename'];
                wppa_update_single_photo($file, $pid, $name);
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
}
function wppa_upload_photos()
{
    global $wpdb;
    global $warning_given;
    global $upload_album;
    $warning_given = false;
    $uploaded_a_file = false;
    $count = '0';
    foreach ($_FILES as $file) {
        if ($file['tmp_name'] != '') {
            $id = wppa_insert_photo($file['tmp_name'], $upload_album, $file['name']);
            if ($id) {
                $uploaded_a_file = true;
                $count++;
                wppa_backend_upload_mail($id, $upload_album, $file['name']);
            } else {
                wppa_error_message(__('Error inserting photo', 'wp-photo-album-plus') . ' ' . wppa_sanitize_file_name(basename($file['name'])) . '.');
                return;
            }
        }
    }
    if ($uploaded_a_file) {
        wppa_update_message($count . ' ' . __('Photos Uploaded in album nr', 'wp-photo-album-plus') . ' ' . $upload_album);
        wppa_set_last_album($upload_album);
    }
}