function wppa_get_import_files()
{
    // Init
    $user = wppa_get_user();
    $source_type = get_option('wppa_import_source_type_' . $user, 'local');
    $files = array();
    // Ajax? one file
    if (isset($_POST['import-ajax-file'])) {
        $files = array($_POST['import-ajax-file']);
    } elseif ($source_type == 'local') {
        $source = get_option('wppa_import_source_' . $user, WPPA_DEPOT_PATH);
        $source_path = $source;
        // Filesystem
        $files = glob($source_path . '/*');
    } else {
        // remote
        $max_tries = get_option('wppa_import_remote_max_' . $user, '10');
        $setting = get_option('wppa_import_source_url_' . $user, 'http://');
        $pattern = '/src=".*?"/';
        // Is it a photofile in a wppa tree filestructure?
        $old_setting = $setting;
        // assume not
        if (wppa_is_url_a_photo($setting)) {
            wppa('is_wppa_tree', false);
            $is_image = true;
        } else {
            $setting = wppa_expand_tree_path($old_setting);
            // though?
            if (wppa_is_url_a_photo($setting)) {
                wppa('is_wppa_tree', true);
                $is_image = true;
            } else {
                $is_image = false;
            }
        }
        // Is it a photofile?
        if ($is_image) {
            $files = array($setting);
            $pid = wppa_strip_ext(basename($old_setting));
            if (is_numeric($pid)) {
                $tries = 1;
                $before = substr($old_setting, 0, strpos($old_setting, $pid));
                while ($tries < $max_tries) {
                    $tries++;
                    $pid++;
                    if (wppa('is_wppa_tree')) {
                        $files[] = $before . wppa_expand_id($pid) . '.jpg';
                    } else {
                        $files[] = $before . $pid . '.jpg';
                    }
                }
            }
        } else {
            $files = get_option('wppa_import_source_url_found_' . $user, false);
            if (!$files) {
                // Init
                $files = array();
                // Get page content
                $curl = curl_init();
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($curl, CURLOPT_URL, $setting);
                $contents = curl_exec($curl);
                curl_close($curl);
                // Process result
                if ($contents) {
                    // Preprocess
                    $contents = str_replace('\'', '"', $contents);
                    // Find matches
                    preg_match_all($pattern, $contents, $matches, PREG_PATTERN_ORDER);
                    if (is_array($matches[0])) {
                        // Sort
                        sort($matches[0]);
                        // Copy to $files, skipping dups
                        $val = '';
                        $count = 0;
                        $sfxs = array('jpg', 'jpeg', 'gif', 'png', 'JPG', 'JPEG', 'GIF', 'PNG');
                        foreach (array_keys($matches[0]) as $idx) {
                            if ($matches[0][$idx] != $val) {
                                $val = $matches[0][$idx];
                                // Post process found item
                                $match = substr($matches[0][$idx], 5);
                                $matchpos = strpos($contents, $match);
                                $match = trim($match, '"');
                                if (strpos($match, '?')) {
                                    $match = substr($match, 0, strpos($match, '?'));
                                }
                                $match = str_replace('/uploads/wppa/thumbs/', '/uploads/wppa/', $match);
                                $sfx = wppa_get_ext($match);
                                if (in_array($sfx, $sfxs)) {
                                    // Save it
                                    $count++;
                                    if ($count <= $max_tries) {
                                        $files[] = $match;
                                    }
                                }
                            }
                        }
                    }
                }
                update_option('wppa_import_source_url_found_' . $user, $files);
            }
        }
    }
    // Remove non originals
    if (is_array($files)) {
        foreach (array_keys($files) as $key) {
            if (!wppa_is_orig($files[$key])) {
                unset($files[$key]);
            }
        }
    }
    // Sort to keep synchronicity when doing ajax import
    if (is_array($files)) {
        sort($files);
    }
    // Done, return result
    return $files;
}
function wppa_insert_photo($file = '', $alb = '', $name = '', $desc = '', $porder = '0', $id = '0', $linkurl = '', $linktitle = '')
{
    global $wpdb;
    global $warning_given_small;
    $album = wppa_cache_album($alb);
    if (!wppa_allow_uploads($alb)) {
        if (is_admin() && !wppa('ajax')) {
            wppa_error_message(sprintf(__('Album %s is full', 'wp-photo-album-plus'), wppa_get_album_name($alb)));
        } else {
            wppa_alert(sprintf(__('Album %s is full', 'wp-photo-album-plus'), wppa_get_album_name($alb)));
        }
        return false;
    }
    if ($file != '' && $alb != '') {
        // Get the name if not given
        if ($name == '') {
            $name = basename($file);
        }
        // Sanitize name
        $filename = wppa_sanitize_file_name($name);
        $name = wppa_sanitize_photo_name($name);
        // If not dups allowed and its already here, quit
        if (isset($_POST['wppa-nodups']) || wppa_switch('void_dups')) {
            $exists = wppa_file_is_in_album($filename, $alb);
            if ($exists) {
                if (isset($_POST['del-after-p'])) {
                    unlink($file);
                    $msg = __('Photo %s already exists in album number %s. Removed from depot.', 'wp-photo-album-plus');
                } else {
                    $msg = __('Photo %s already exists in album number %s.', 'wp-photo-album-plus');
                }
                wppa_warning_message(sprintf($msg, $name, $alb));
                return false;
            }
        }
        // Verify file exists
        if (!wppa('is_remote') && !file_exists($file)) {
            if (!is_dir(dirname($file))) {
                wppa_error_message('Error: Directory ' . dirname($file) . ' does not exist.');
                return false;
            }
            if (!is_writable(dirname($file))) {
                wppa_error_message('Error: Directory ' . dirname($file) . ' is not writable.');
                return false;
            }
            wppa_error_message('Error: File ' . $file . ' does not exist.');
            return false;
        } elseif (wppa('is_remote')) {
            if (!wppa_is_url_a_photo($file)) {
                if (wppa('ajax')) {
                    wppa('ajax_import_files_error', __('Not found', 'wp-photo-album-plus'));
                }
                return false;
            }
        }
        // Get and verify the size
        $img_size = getimagesize($file);
        if ($img_size) {
            if (wppa_check_memory_limit('', $img_size['0'], $img_size['1']) === false) {
                wppa_error_message(sprintf(__('ERROR: Attempt to upload a photo that is too large to process (%s).', 'wp-photo-album-plus'), $name) . wppa_check_memory_limit());
                wppa('ajax_import_files_error', __('Too big', 'wp-photo-album-plus'));
                return false;
            }
            if (!$warning_given_small && ($img_size['0'] < wppa_get_minisize() && $img_size['1'] < wppa_get_minisize())) {
                wppa_warning_message(__('WARNING: You are uploading photos that are too small. Photos must be larger than the thumbnail size and larger than the coverphotosize.', 'wp-photo-album-plus'));
                wppa('ajax_import_files_error', __('Too small', 'wp-photo-album-plus'));
                $warning_given_small = true;
            }
        } else {
            wppa_error_message(__('ERROR: Unable to retrieve image size of', 'wp-photo-album-plus') . ' ' . $name . ' ' . __('Are you sure it is a photo?', 'wp-photo-album-plus'));
            wppa('ajax_import_files_error', __('No photo found', 'wp-photo-album-plus'));
            return false;
        }
        // Get ext based on mimetype, regardless of ext
        switch ($img_size[2]) {
            // mime type
            case 1:
                $ext = 'gif';
                break;
            case 2:
                $ext = 'jpg';
                break;
            case 3:
                $ext = 'png';
                break;
            default:
                wppa_error_message(__('Unsupported mime type encountered:', 'wp-photo-album-plus') . ' ' . $img_size[2] . '.');
                return false;
        }
        // Get an id if not yet there
        if ($id == '0') {
            $id = wppa_nextkey(WPPA_PHOTOS);
        }
        // Get opt deflt desc if empty
        if ($desc == '' && wppa_switch('apply_newphoto_desc')) {
            $desc = stripslashes(wppa_opt('newphoto_description'));
        }
        // Reset rating
        $mrat = '0';
        // Find ( new ) owner
        $owner = wppa_get_user();
        // Validate album
        if (!is_numeric($alb) || $alb < '1') {
            wppa_error_message(__('Album not known while trying to add a photo', 'wp-photo-album-plus'));
            return false;
        }
        if (!wppa_have_access($alb)) {
            wppa_error_message(sprintf(__('Album %s does not exist or is not accessable while trying to add a photo', 'wp-photo-album-plus'), $alb));
            return false;
        }
        $status = wppa_switch('upload_moderate') && !current_user_can('wppa_admin') ? 'pending' : 'publish';
        // Add photo to db
        $id = wppa_create_photo_entry(array('id' => $id, 'album' => $alb, 'ext' => $ext, 'name' => $name, 'p_order' => $porder, 'description' => $desc, 'linkurl' => $linkurl, 'linktitle' => $linktitle, 'status' => $status, 'filename' => $filename));
        if (!$id) {
            wppa_error_message(__('Could not insert photo.', 'wp-photo-album-plus'));
        } else {
            // Save the source
            wppa_save_source($file, $filename, $alb);
            wppa_make_o1_source($id);
            wppa_flush_treecounts($alb);
            wppa_update_album(array('id' => $alb, 'modified' => time()));
            wppa_flush_upldr_cache('photoid', $id);
        }
        // Make the photo files
        if (wppa_make_the_photo_files($file, $id, $ext)) {
            // Repair photoname if not supplied and not standard
            wppa_set_default_name($id, $name);
            // Tags
            wppa_set_default_tags($id);
            // Index
            wppa_index_add('photo', $id);
            // and add watermark ( optionally ) to fullsize image only
            wppa_add_watermark($id);
            // also to thumbnail?
            if (wppa_switch('watermark_thumbs')) {
                wppa_create_thumbnail($id);
            }
            // Is it a default coverimage?
            wppa_check_coverimage($id);
            return $id;
        }
    } else {
        wppa_error_message(__('ERROR: Unknown file or album.', 'wp-photo-album-plus'));
        return false;
    }
}