Ejemplo n.º 1
0
function ProcessFolder($folder, $version_dir, &$resource_array, &$resource_error)
{
    global $lang, $syncdir, $nogo, $staticsync_max_files, $count, $done, $modtimes, $lastsync, $ffmpeg_preview_extension, $staticsync_autotheme, $staticsync_folder_structure, $staticsync_extension_mapping_default, $staticsync_extension_mapping, $staticsync_mapped_category_tree, $staticsync_title_includes_path, $staticsync_ingest, $staticsync_mapfolders, $staticsync_alternatives_suffix, $theme_category_levels, $staticsync_defaultstate, $additional_archive_states, $staticsync_extension_mapping_append_values, $image_alternatives, $exclude_resize, $post_host, $media_endpoint, $image_required_height, $sync_bucket, $aws_key, $aws_secret_key;
    $collection = 0;
    echo "Processing Folder: {$folder}" . PHP_EOL;
    #$alt_path = get_resource_path(59, TRUE, '', FALSE, 'png', -1, 1, FALSE, '', 4);
    # List all files in this folder.
    $dh = opendir($folder);
    while (($file = readdir($dh)) !== false) {
        if ($file == '.' || $file == '..') {
            continue;
        }
        $filetype = filetype($folder . "/" . $file);
        $fullpath = $folder . "/" . $file;
        $shortpath = str_replace($syncdir . "/", '', $fullpath);
        # Work out extension
        $extension = explode(".", $file);
        if (count($extension) > 1) {
            $extension = trim(strtolower($extension[count($extension) - 1]));
        } else {
            //No extension
            $extension = "";
        }
        if (strpos($fullpath, $nogo)) {
            echo "This directory is to be ignored." . PHP_EOL;
            continue;
        }
        if ($staticsync_mapped_category_tree) {
            $path_parts = explode("/", $shortpath);
            array_pop($path_parts);
            touch_category_tree_level($path_parts);
        }
        # -----FOLDERS-------------
        if (($filetype == "dir" || $filetype == "link") && strpos($nogo, "[{$file}]") === false && strpos($file, $staticsync_alternatives_suffix) === false) {
            # Get current version direcotries.
            if (preg_match("/[0-9]{2}-[0-9]{2}-[0-9]{4}\$/", $file)) {
                if (!in_array($file, $version_dir)) {
                    array_push($version_dir, $file);
                }
                if (preg_match('/in_progress*/', $file)) {
                    echo "The Barcode is still being processed." . PHP_EOL;
                    continue;
                }
            }
            # Recurse
            ProcessFolder($folder . "/" . $file, $version_dir, $resource_array, $resource_error);
        }
        $psd_files = array();
        if (preg_match('/images/', $fullpath)) {
            $path_array = explode('/', $fullpath);
            $psd_array = array_splice($path_array, 0, array_search('images', $path_array));
            $psd_path = implode('/', $psd_array) . '/psd/';
            $psd_files = array_diff(scandir($psd_path), array('..', '.'));
            foreach ($psd_files as $index => $psd_file) {
                $psd_files[$index] = pathinfo($psd_file, PATHINFO_FILENAME);
            }
        }
        # -------FILES---------------
        if ($filetype == "file" && substr($file, 0, 1) != "." && strtolower($file) != "thumbs.db") {
            /* Below Code Adapted  from CMay's bug report */
            global $banned_extensions;
            # Check to see if extension is banned, do not add if it is banned
            if (array_search($extension, $banned_extensions)) {
                continue;
            }
            /* Above Code Adapted from CMay's bug report */
            $count++;
            if ($count > $staticsync_max_files) {
                return true;
            }
            $last_sync_date = sql_value("select value from sysvars where name = 'last_sync'", "");
            $file_creation_date = date("Y-m-d H:i:s", filectime($fullpath));
            if (isset($last_sync_date) && $last_sync_date > $file_creation_date) {
                echo "No new file found.." . PHP_EOL;
                continue;
            }
            # Already exists?
            if (!isset($done[$shortpath])) {
                echo "Processing file: {$fullpath}" . PHP_EOL;
                if ($collection == 0 && $staticsync_autotheme) {
                    # Make a new collection for this folder.
                    $e = explode("/", $shortpath);
                    $theme = ucwords($e[0]);
                    $themesql = "theme='" . ucwords(escape_check($e[0])) . "'";
                    $themecolumns = "theme";
                    $themevalues = "'" . ucwords(escape_check($e[0])) . "'";
                    if ($staticsync_folder_structure) {
                        for ($x = 0; $x < count($e) - 1; $x++) {
                            if ($x != 0) {
                                $themeindex = $x + 1;
                                if ($themeindex > $theme_category_levels) {
                                    $theme_category_levels = $themeindex;
                                    if ($x == count($e) - 2) {
                                        echo PHP_EOL . PHP_EOL . "UPDATE THEME_CATEGORY_LEVELS TO {$themeindex} IN CONFIG!!!!" . PHP_EOL . PHP_EOL;
                                    }
                                }
                                $th_name = ucwords(escape_check($e[$x]));
                                $themesql .= " AND theme{$themeindex} = '{$th_name}'";
                                $themevalues .= ",'{$th_name}'";
                                $themecolumns .= ",theme{$themeindex}";
                            }
                        }
                    }
                    $name = count($e) == 1 ? '' : $e[count($e) - 2];
                    echo "Collection {$name}, theme={$theme}" . PHP_EOL;
                    $ul_username = $theme;
                    $escaped_name = escape_check($name);
                    $collection = sql_value("SELECT ref value FROM collection WHERE name='{$escaped_name}' AND {$themesql}", 0);
                    if ($collection == 0) {
                        sql_query("INSERT INTO collection (name,created,public,{$themecolumns},allow_changes)\n                                                   VALUES ('{$escaped_name}', NOW(), 1, {$themevalues}, 0)");
                        $collection = sql_insert_id();
                    }
                }
                # Work out a resource type based on the extension.
                $type = $staticsync_extension_mapping_default;
                reset($staticsync_extension_mapping);
                foreach ($staticsync_extension_mapping as $rt => $extensions) {
                    if (in_array($extension, $extensions)) {
                        $type = $rt;
                    }
                }
                $modified_type = hook('modify_type', 'staticsync', array($type));
                if (is_numeric($modified_type)) {
                    $type = $modified_type;
                }
                # Formulate a title
                if ($staticsync_title_includes_path) {
                    $title_find = array('/', '_', ".{$extension}");
                    $title_repl = array(' - ', ' ', '');
                    $title = ucfirst(str_ireplace($title_find, $title_repl, $shortpath));
                } else {
                    $title = str_ireplace(".{$extension}", '', $file);
                }
                $modified_title = hook('modify_title', 'staticsync', array($title));
                if ($modified_title !== false) {
                    $title = $modified_title;
                }
                # Import this file
                #$r = import_resource($shortpath, $type, $title, $staticsync_ingest);
                #Check for file name containing the psd.
                if (!empty($psd_files)) {
                    $image_file_array = explode('/', $fullpath);
                    $image_file = $image_file_array[count($image_file_array) - 1];
                    $image_psd_name = explode('_', $image_file)[0];
                    if (array_search($image_psd_name, $psd_files)) {
                        #Image name is in right format.
                        if (!validate_image_size($fullpath, $image_required_height)) {
                            $resource_error['size'][$file] = $fullpath;
                        }
                        $r = import_resource($fullpath, $type, $title, $staticsync_ingest);
                        sql_query("INSERT INTO resource_data (resource,resource_type_field,value)\n                               VALUES ('{$r}', (SELECT ref FROM resource_type_field WHERE name = 'logical_id'), '{$image_psd_name}')");
                        $original_filepath = sql_query("SELECT value FROM resource_data WHERE resource = '{$r}' AND\n                                                     resource_type_field = (SELECT ref FROM resource_type_field where name = 'original_filepath')");
                        if (isset($original_filepath)) {
                            sql_query("INSERT INTO resource_data (resource,resource_type_field,value)\n                                 VALUES ('{$r}',(SELECT ref FROM resource_type_field WHERE name = 'original_filepath'), '{$fullpath}')");
                        }
                    } else {
                        echo "Filename '{$fullpath}' is not in right format.." . PHP_EOL;
                        $resource_error['name'][$file] = $fullpath;
                        continue;
                    }
                } elseif (word_in_string($exclude_resize, explode('/', $fullpath))) {
                    $r = import_resource($fullpath, $type, $title, $staticsync_ingest);
                }
                if ($r !== false) {
                    array_push($resource_array, $r);
                    # Create current version for resource.
                    #print_r($version_dir);
                    if (count($version_dir) == 1) {
                        sql_query("INSERT into resource_data (resource,resource_type_field,value)\n                                    VALUES ('{$r}',(SELECT ref FROM resource_type_field WHERE name = 'current'), 'TRUE')");
                    }
                    $sync_status = sync_to_s3($syncdir, $sync_bucket, $aws_key, $aws_secret_key);
                    if (!$sync_status) {
                        echo "Failed to sync";
                    }
                    # Add to mapped category tree (if configured)
                    if (isset($staticsync_mapped_category_tree)) {
                        $basepath = '';
                        # Save tree position to category tree field
                        # For each node level, expand it back to the root so the full path is stored.
                        for ($n = 0; $n < count($path_parts); $n++) {
                            if ($basepath != '') {
                                $basepath .= "~";
                            }
                            $basepath .= $path_parts[$n];
                            $path_parts[$n] = $basepath;
                        }
                        update_field($r, $staticsync_mapped_category_tree, "," . join(",", $path_parts));
                    }
                    #This is an override to add user data to the resouces
                    if (!isset($userref)) {
                        $ul_username = ucfirst(strtolower($ul_username));
                        $current_user_ref = sql_query("Select ref from user where username = '******' ");
                        if (!empty($current_user_ref)) {
                            $current_user_ref = $current_user_ref[0]['ref'];
                            sql_query("UPDATE resource SET created_by='{$current_user_ref}' where ref = {$r}");
                        }
                    }
                    # default access level. This may be overridden by metadata mapping.
                    $accessval = 0;
                    # StaticSync path / metadata mapping
                    # Extract metadata from the file path as per $staticsync_mapfolders in config.php
                    if (isset($staticsync_mapfolders)) {
                        foreach ($staticsync_mapfolders as $mapfolder) {
                            $match = $mapfolder["match"];
                            $field = $mapfolder["field"];
                            $level = $mapfolder["level"];
                            if (strpos("/" . $shortpath, $match) !== false) {
                                # Match. Extract metadata.
                                $path_parts = explode("/", $shortpath);
                                if ($level < count($path_parts)) {
                                    // special cases first.
                                    if ($field == 'access') {
                                        # access level is a special case
                                        # first determine if the value matches a defined access level
                                        $value = $path_parts[$level - 1];
                                        for ($n = 0; $n < 3; $n++) {
                                            # if we get an exact match or a match except for case
                                            if ($value == $lang["access" . $n] || strtoupper($value) == strtoupper($lang['access' . $n])) {
                                                $accessval = $n;
                                                echo "Will set access level to " . $lang['access' . $n] . " ({$n})" . PHP_EOL;
                                            }
                                        }
                                    } else {
                                        if ($field == 'archive') {
                                            # archive level is a special case
                                            # first determin if the value matches a defined archive level
                                            $value = $mapfolder["archive"];
                                            $archive_array = array_merge(array(-2, -1, 0, 1, 2, 3), $additional_archive_states);
                                            if (in_array($value, $archive_array)) {
                                                $archiveval = $value;
                                                echo "Will set archive level to " . $lang['status' . $value] . " ({$archiveval})" . PHP_EOL;
                                            }
                                        } else {
                                            # Save the value
                                            #print_r($path_parts);
                                            $value = $path_parts[$level - 1];
                                            if ($staticsync_extension_mapping_append_values) {
                                                $given_value = $value;
                                                // append the values if possible...not used on dropdown, date, categroy tree, datetime, or radio buttons
                                                $field_info = get_resource_type_field($field);
                                                if (in_array($field['type'], array(0, 1, 2, 4, 5, 6, 7, 8))) {
                                                    $old_value = sql_value("select value value from resource_data where resource={$r} and resource_type_field={$field}", "");
                                                    $value = append_field_value($field_info, $value, $old_value);
                                                }
                                            }
                                            update_field($r, $field, trim($value));
                                            if (strtotime(trim($value))) {
                                                add_keyword_mappings($r, trim($value), $field, false, true);
                                            } else {
                                                add_keyword_mappings($r, trim($value), $field);
                                            }
                                            if ($staticsync_extension_mapping_append_values) {
                                                $value = $given_value;
                                            }
                                            echo " - Extracted metadata from path: {$value}" . PHP_EOL;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #Resize only original images.
                    if (!word_in_string($exclude_resize, explode('/', $fullpath))) {
                        echo "Creating preview..";
                        create_previews($r, false, $extension, false, false, -1, false, $staticsync_ingest);
                    }
                    # update access level
                    sql_query("UPDATE resource SET access = '{$accessval}',archive='{$staticsync_defaultstate}' WHERE ref = '{$r}'");
                    # Add any alternative files
                    $altpath = $fullpath . $staticsync_alternatives_suffix;
                    if ($staticsync_ingest && file_exists($altpath)) {
                        $adh = opendir($altpath);
                        while (($altfile = readdir($adh)) !== false) {
                            $filetype = filetype($altpath . "/" . $altfile);
                            if ($filetype == "file" && substr($file, 0, 1) != "." && strtolower($file) != "thumbs.db") {
                                # Create alternative file
                                # Find extension
                                $ext = explode(".", $altfile);
                                $ext = $ext[count($ext) - 1];
                                $description = str_replace("?", strtoupper($ext), $lang["originalfileoftype"]);
                                $file_size = filesize_unlimited($altpath . "/" . $altfile);
                                $aref = add_alternative_file($r, $altfile, $description, $altfile, $ext, $file_size);
                                $path = get_resource_path($r, true, '', true, $ext, -1, 1, false, '', $aref);
                                rename($altpath . "/" . $altfile, $path);
                                # Move alternative file
                            }
                        }
                    }
                    # Add to collection
                    if ($staticsync_autotheme) {
                        $test = '';
                        $test = sql_query("SELECT * FROM collection_resource WHERE collection='{$collection}' AND resource='{$r}'");
                        if (count($test) == 0) {
                            sql_query("INSERT INTO collection_resource (collection, resource, date_added)\n                                            VALUES ('{$collection}', '{$r}', NOW())");
                        }
                    }
                } else {
                    # Import failed - file still being uploaded?
                    echo " *** Skipping file - it was not possible to move the file (still being imported/uploaded?)" . PHP_EOL;
                }
            } else {
                # check modified date and update previews if necessary
                $filemod = filemtime($fullpath);
                if (array_key_exists($shortpath, $modtimes) && $filemod > strtotime($modtimes[$shortpath])) {
                    # File has been modified since we last created previews. Create again.
                    $rd = sql_query("SELECT ref, has_image, file_modified, file_extension FROM resource\n                                        WHERE file_path='" . escape_check($shortpath) . "'");
                    if (count($rd) > 0) {
                        $rd = $rd[0];
                        $rref = $rd["ref"];
                        echo "Resource {$rref} has changed, regenerating previews: {$fullpath}" . PHP_EOL;
                        extract_exif_comment($rref, $rd["file_extension"]);
                        # extract text from documents (e.g. PDF, DOC).
                        global $extracted_text_field;
                        if (isset($extracted_text_field)) {
                            if (isset($unoconv_path) && in_array($extension, $unoconv_extensions)) {
                                // omit, since the unoconv process will do it during preview creation below
                            } else {
                                extract_text($rref, $extension);
                            }
                        }
                        # Store original filename in field, if set
                        global $filename_field;
                        if (isset($filename_field)) {
                            update_field($rref, $filename_field, $file);
                        }
                        create_previews($rref, false, $rd["file_extension"], false, false, -1, false, $staticsync_ingest);
                        sql_query("UPDATE resource SET file_modified=NOW() WHERE ref='{$rref}'");
                    }
                }
            }
        }
    }
}
Ejemplo n.º 2
0
function ProcessFolder($folder)
    {
    global $lang, $syncdir, $nogo, $staticsync_max_files, $count, $done, $modtimes, $lastsync, $ffmpeg_preview_extension, 
           $staticsync_autotheme, $staticsync_folder_structure, $staticsync_extension_mapping_default, 
           $staticsync_extension_mapping, $staticsync_mapped_category_tree, $staticsync_title_includes_path, 
           $staticsync_ingest, $staticsync_mapfolders, $staticsync_alternatives_suffix, $theme_category_levels, $staticsync_defaultstate;
    
    $collection = 0;
    
    echo "Processing Folder: $folder" . PHP_EOL;
    
    # List all files in this folder.
    $dh = opendir($folder);
    while (($file = readdir($dh)) !== false)
        {
        if ( $file == '.' || $file == '..')
            {
            continue;
            }
        $filetype  = filetype($folder . "/" . $file);
        $fullpath  = $folder . "/" . $file;
        $shortpath = str_replace($syncdir . "/", '', $fullpath);
        # Work out extension
        $extension = explode(".", $file);
        if(count($extension)>1)
            {
            $extension = trim(strtolower($extension[count($extension)-1]));
            }
        else
            {
            //No extension
            $extension="";
            }
       
        
        if ($staticsync_mapped_category_tree)
            {
            $path_parts = explode("/", $shortpath);
            array_pop($path_parts);
            touch_category_tree_level($path_parts);
            }   

        # -----FOLDERS-------------
        if ((($filetype == "dir") || $filetype == "link") && 
            (strpos($nogo, "[$file]") === false) && 
            (strpos($file, $staticsync_alternatives_suffix) === false))
            {
            # Recurse
            ProcessFolder($folder . "/" . $file);
            }

        # -------FILES---------------
        if (($filetype == "file") && (substr($file,0,1) != ".") && (strtolower($file) != "thumbs.db"))
            {

            /* Below Code Adapted  from CMay's bug report */
            global $banned_extensions;
            # Check to see if extension is banned, do not add if it is banned
            if(array_search($extension, $banned_extensions)){continue;}
            /* Above Code Adapted from CMay's bug report */
            
            $count++;
            if ($count > $staticsync_max_files) { return(true); }

            # Already exists?
            if (!isset($done[$shortpath]))
                {
                echo "Processing file: $fullpath" . PHP_EOL;
                
                if ($collection == 0 && $staticsync_autotheme)
                    {
                    # Make a new collection for this folder.
                    $e = explode("/", $shortpath);
                    $theme        = ucwords($e[0]);
                    $themesql     = "theme='" . ucwords(escape_check($e[0])) . "'";
                    $themecolumns = "theme";
                    $themevalues  = "'" . ucwords(escape_check($e[0])) . "'";
                    
                    if ($staticsync_folder_structure)
                        {
                        for ($x=0;$x<count($e)-1;$x++)
                            {
                            if ($x != 0)
                                {
                                $themeindex = $x+1;
                                if ($themeindex >$theme_category_levels)
                                    {
                                    $theme_category_levels = $themeindex;
                                    if ($x == count($e)-2)
                                        {
                                        echo PHP_EOL . PHP_EOL . 
                                             "UPDATE THEME_CATEGORY_LEVELS TO $themeindex IN CONFIG!!!!" . 
                                             PHP_EOL . PHP_EOL;
                                        }
                                    }
                                $th_name       = ucwords(escape_check($e[$x]));
                                $themesql     .= " AND theme{$themeindex} = '$th_name'";
                                $themevalues  .= ",'$th_name'";
                                $themecolumns .= ",theme{$themeindex}";
                                }
                            }
                        }

                    $name = (count($e) == 1) ? '' : $e[count($e)-2];
                    echo "Collection $name, theme=$theme" . PHP_EOL;
                    $escaped_name = escape_check($name);
                    $collection = sql_value("SELECT ref value FROM collection WHERE name='$escaped_name' AND $themesql", 0);
                    if ($collection == 0)
                        {
                        sql_query("INSERT INTO collection (name,created,public,$themecolumns,allow_changes) 
                                                   VALUES ('$escaped_name', NOW(), 1, $themevalues, 0)");
                        $collection = sql_insert_id();
                        }
                    }

                # Work out a resource type based on the extension.
                $type = $staticsync_extension_mapping_default;
                reset($staticsync_extension_mapping);
                foreach ($staticsync_extension_mapping as $rt => $extensions)
                    {
                    if (in_array($extension,$extensions)) { $type = $rt; }
                    }
                $modified_type = hook('modify_type', 'staticsync', array( $type ));
                if (is_numeric($modified_type)) { $type = $modified_type; }

                # Formulate a title
                if ($staticsync_title_includes_path)
                    {
                    $title_find = array('/',   '_', ".$extension" );
                    $title_repl = array(' - ', ' ', '');
                    $title      = ucfirst(str_ireplace($title_find, $title_repl, $shortpath));
                    }
                else
                    {
                    $title = str_ireplace(".$extension", '', $file);
                    }
                $modified_title = hook('modify_title', 'staticsync', array( $title ));
                if ($modified_title !== false) { $title = $modified_title; }

                # Import this file
                $r = import_resource($shortpath, $type, $title, $staticsync_ingest);
                if ($r !== false)
                    {
                    # Add to mapped category tree (if configured)
                    if (isset($staticsync_mapped_category_tree))
                        {
                        $basepath = '';
                        # Save tree position to category tree field

                        # For each node level, expand it back to the root so the full path is stored.
                        for ($n=0;$n<count($path_parts);$n++)
                            {
                            if ($basepath != '') 
                                { 
                                $basepath .= "~";
                                }
                            $basepath .= $path_parts[$n];
                            $path_parts[$n] = $basepath;
                            }
                        
                        update_field($r, $staticsync_mapped_category_tree, "," . join(",", $path_parts));
                        }           

                    # default access level. This may be overridden by metadata mapping.
                    $accessval = 0;

                    # StaticSync path / metadata mapping
                    # Extract metadata from the file path as per $staticsync_mapfolders in config.php
                    if (isset($staticsync_mapfolders))
                        {
                        foreach ($staticsync_mapfolders as $mapfolder)
                            {
                            $match = $mapfolder["match"];
                            $field = $mapfolder["field"];
                            $level = $mapfolder["level"];

                            if (strpos("/" . $shortpath, $match) !== false)
                                {
                                # Match. Extract metadata.
                                $path_parts = explode("/", $shortpath);
                                if ($level < count($path_parts))
                                    {
                                    // special cases first.
                                    if ($field == 'access')
                                        {
                                        # access level is a special case
                                        # first determine if the value matches a defined access level

                                        $value = $path_parts[$level-1];

                                        for ($n=0; $n<3; $n++){
                                            # if we get an exact match or a match except for case
                                            if ($value == $lang["access" . $n] || strtoupper($value) == strtoupper($lang['access' . $n]))
                                                {
                                                $accessval = $n;
                                                echo "Will set access level to " . $lang['access' . $n] . " ($n)" . PHP_EOL;
                                                }
                                            }

                                        }
                                    else 
                                        {
                                        # Save the value
                                        print_r($path_parts);
                                        $value = $path_parts[$level-1];
                                        update_field ($r, $field, $value);
                                        echo " - Extracted metadata from path: $value" . PHP_EOL;
                                        }
                                    }
                                }
                            }
                        }

                    # update access level
                    sql_query("UPDATE resource SET access = '$accessval',archive='$staticsync_defaultstate' WHERE ref = '$r'");

                    # Add any alternative files
                    $altpath = $fullpath . $staticsync_alternatives_suffix;
                    if ($staticsync_ingest && file_exists($altpath))
                        {
                        $adh = opendir($altpath);
                        while (($altfile = readdir($adh)) !== false)
                            {
                            $filetype = filetype($altpath . "/" . $altfile);
                            if (($filetype == "file") && (substr($file,0,1) != ".") && (strtolower($file) != "thumbs.db"))
                                {
                                # Create alternative file                               
                                # Find extension
                                $ext = explode(".", $altfile);
                                $ext = $ext[count($ext)-1];
                                
                                $description = str_replace("?", strtoupper($ext), $lang["originalfileoftype"]);
                                $file_size   = filesize_unlimited($altpath . "/" . $altfile);
                                
                                $aref = add_alternative_file($r, $altfile, $description, $altfile, $ext, $file_size);
                                $path = get_resource_path($r, true, '', true, $ext, -1, 1, false, '', $aref);
                                rename($altpath . "/" . $altfile,$path); # Move alternative file
                                }
                            }   
                        }

                    # Add to collection
                    if ($staticsync_autotheme)
                        {
                        $test = ''; 
                        $test = sql_query("SELECT * FROM collection_resource WHERE collection='$collection' AND resource='$r'");
                        if (count($test) == 0)
                            {
                            sql_query("INSERT INTO collection_resource (collection, resource, date_added) 
                                            VALUES ('$collection', '$r', NOW())");
                            }
                        }
                    }
                else
                    {
                    # Import failed - file still being uploaded?
                    echo " *** Skipping file - it was not possible to move the file (still being imported/uploaded?)" . PHP_EOL;
                    }
                }
            else
                {
                # check modified date and update previews if necessary
                $filemod = filemtime($fullpath);
                if (array_key_exists($shortpath,$modtimes) && ($filemod > strtotime($modtimes[$shortpath])))
                    {
                    # File has been modified since we last created previews. Create again.
                    $rd = sql_query("SELECT ref, has_image, file_modified, file_extension FROM resource 
                                        WHERE file_path='" . escape_check($shortpath) . "'");
                    if (count($rd) > 0)
                        {
                        $rd   = $rd[0];
                        $rref = $rd["ref"];

                        echo "Resource $rref has changed, regenerating previews: $fullpath" . PHP_EOL;
                        extract_exif_comment($rref,$rd["file_extension"]);

                        # extract text from documents (e.g. PDF, DOC).
                        global $extracted_text_field;
                        if (isset($extracted_text_field)) {
                            if (isset($unoconv_path) && in_array($extension,$unoconv_extensions)){
                                // omit, since the unoconv process will do it during preview creation below
                                }
                            else {
                            extract_text($rref,$extension);
                            }
                        }

                        # Store original filename in field, if set
                        global $filename_field;
                        if (isset($filename_field))
                            {
                            update_field($rref,$filename_field,$file);  
                            }

                        create_previews($rref, false, $rd["file_extension"], false, false, -1, false, $staticsync_ingest);
                        sql_query("UPDATE resource SET file_modified=NOW() WHERE ref='$rref'");
                        }
                    }
                }
            }   
        }   
    }
function ProcessFolder($folder)
{
    #echo "<br>processing folder $folder";
    global $syncdir, $nogo, $max, $count, $done, $modtimes, $lastsync, $ffmpeg_preview_extension, $staticsync_autotheme, $staticsync_extension_mapping_default, $staticsync_extension_mapping, $staticsync_mapped_category_tree, $staticsync_title_includes_path, $staticsync_ingest, $staticsync_mapfolders, $staticsync_alternatives_suffix, $staticsync_alt_suffixes, $staticsync_alt_suffix_array, $file_minimum_age, $staticsync_run_timestamp;
    $collection = 0;
    echo "Processing Folder: {$folder}\n";
    # List all files in this folder.
    $dh = opendir($folder);
    echo date('Y-m-d H:i:s    ');
    echo "Reading from {$folder}\n";
    while (($file = readdir($dh)) !== false) {
        // because of alternative processing, some files may disappear during the run
        // that's ok - just ignore it and move on
        if (!file_exists($folder . "/" . $file)) {
            echo date('Y-m-d H:i:s    ');
            echo "File {$file} missing. Moving on.\n";
            continue;
        }
        $filetype = filetype($folder . "/" . $file);
        $fullpath = $folder . "/" . $file;
        $shortpath = str_replace($syncdir . "/", "", $fullpath);
        if ($staticsync_mapped_category_tree) {
            $path_parts = explode("/", $shortpath);
            array_pop($path_parts);
            touch_category_tree_level($path_parts);
        }
        # -----FOLDERS-------------
        if (($filetype == "dir" || $filetype == "link") && $file != "." && $file != ".." && strpos($nogo, "[" . $file . "]") === false && strpos($file, $staticsync_alternatives_suffix) === false) {
            # Recurse
            #echo "\n$file : " . filemtime($folder . "/" . $file) . " > " . $lastsync;
            if (true || strlen($lastsync) == "" || filemtime($folder . "/" . $file) > $lastsync - 26000) {
                ProcessFolder($folder . "/" . $file);
            }
        }
        # -------FILES---------------
        if ($filetype == "file" && substr($file, 0, 1) != "." && strtolower($file) != "thumbs.db" && !ss_is_alt($file)) {
            // we want to make sure we don't touch files that are too new
            // so check this
            if (time() - filectime($folder . "/" . $file) < $file_minimum_age) {
                echo date('Y-m-d H:i:s    ');
                echo "   {$file} too new -- skipping .\n";
                //echo filectime($folder . "/" . $file) . " " . time() . "\n";
                continue;
            }
            # Already exists?
            if (!in_array($shortpath, $done)) {
                $count++;
                if ($count > $max) {
                    return true;
                }
                echo date('Y-m-d H:i:s    ');
                echo "Processing file: {$fullpath}\n";
                if ($collection == 0 && $staticsync_autotheme) {
                    # Make a new collection for this folder.
                    $e = explode("/", $shortpath);
                    $theme = ucwords($e[0]);
                    $name = count($e) == 1 ? "" : $e[count($e) - 2];
                    echo date('Y-m-d H:i:s    ');
                    echo "\nCollection {$name}, theme={$theme}";
                    $collection = sql_value("select ref value from collection where name='" . escape_check($name) . "' and theme='" . escape_check($theme) . "'", 0);
                    if ($collection == 0) {
                        sql_query("insert into collection (name,created,public,theme,allow_changes) values ('" . escape_check($name) . "',now(),1,'" . escape_check($theme) . "',0)");
                        $collection = sql_insert_id();
                    }
                }
                # Work out extension
                $extension = explode(".", $file);
                $extension = trim(strtolower($extension[count($extension) - 1]));
                // if coming from collections or la folders, assume these are the resource types
                if (stristr(strtolower($fullpath), 'collection services/curatorial')) {
                    $type = 5;
                } elseif (stristr(strtolower($fullpath), 'collection services/conservation')) {
                    $type = 5;
                } elseif (stristr(strtolower($fullpath), 'collection services/library_archives')) {
                    $type = 6;
                } else {
                    # Work out a resource type based on the extension.
                    $type = $staticsync_extension_mapping_default;
                    reset($staticsync_extension_mapping);
                    foreach ($staticsync_extension_mapping as $rt => $extensions) {
                        if ($rt == 5 or $rt == 6) {
                            continue;
                        }
                        // we already eliminated those
                        if (in_array($extension, $extensions)) {
                            $type = $rt;
                        }
                    }
                }
                # Formulate a title
                if ($staticsync_title_includes_path) {
                    $title = str_ireplace("." . $extension, "", str_replace("/", " - ", $shortpath));
                    $title = ucfirst(str_replace("_", " ", $title));
                } else {
                    $title = str_ireplace("." . $extension, "", $file);
                }
                # Import this file
                $r = import_resource($shortpath, $type, $title, $staticsync_ingest);
                if ($r !== false) {
                    # Add to mapped category tree (if configured)
                    if (isset($staticsync_mapped_category_tree)) {
                        $basepath = "";
                        # Save tree position to category tree field
                        # For each node level, expand it back to the root so the full path is stored.
                        for ($n = 0; $n < count($path_parts); $n++) {
                            if ($basepath != "") {
                                $basepath .= "~";
                            }
                            $basepath .= $path_parts[$n];
                            $path_parts[$n] = $basepath;
                        }
                        update_field($r, $staticsync_mapped_category_tree, "," . join(",", $path_parts));
                        #echo "update_field($r,$staticsync_mapped_category_tree," . "," . join(",",$path_parts) . ");\n";
                    }
                    # StaticSync path / metadata mapping
                    # Extract metadata from the file path as per $staticsync_mapfolders in config.php
                    if (isset($staticsync_mapfolders)) {
                        foreach ($staticsync_mapfolders as $mapfolder) {
                            $match = $mapfolder["match"];
                            $field = $mapfolder["field"];
                            $level = $mapfolder["level"];
                            if (strpos("/" . $shortpath, $match) !== false) {
                                # Match. Extract metadata.
                                $path_parts = explode("/", $shortpath);
                                if ($level < count($path_parts)) {
                                    # Save the value
                                    print_r($path_parts);
                                    $value = $path_parts[$level - 1];
                                    update_field($r, $field, $value);
                                    echo " - Extracted metadata from path: {$value}\n";
                                }
                            }
                        }
                    }
                    // add the timestamp from this run to the keywords field to help retrieve this batch later
                    $currentkeywords = sql_value("select value from resource_data where resource = '{$r}' and resource_type_field = '1'", "");
                    if (strlen($currentkeywords) > 0) {
                        $currentkeywords .= ',';
                    }
                    update_field($r, 1, $currentkeywords . $staticsync_run_timestamp);
                    if (function_exists('staticsync_local_functions')) {
                        // if local cleanup functions have been defined, run them
                        staticsync_local_functions($r);
                    }
                    # Add any alternative files
                    $altpath = $fullpath . $staticsync_alternatives_suffix;
                    if ($staticsync_ingest && file_exists($altpath)) {
                        $adh = opendir($altpath);
                        while (($altfile = readdir($adh)) !== false) {
                            $filetype = filetype($altpath . "/" . $altfile);
                            if ($filetype == "file" && substr($file, 0, 1) != "." && strtolower($file) != "thumbs.db") {
                                # Create alternative file
                                global $lang;
                                # Find extension
                                $ext = explode(".", $altfile);
                                $ext = $ext[count($ext) - 1];
                                $aref = add_alternative_file($r, $altfile, strtoupper($ext) . " " . $lang["file"], $altfile, $ext, filesize_unlimited($altpath . "/" . $altfile));
                                $path = get_resource_path($r, true, "", true, $ext, -1, 1, false, "", $aref);
                                rename($altpath . "/" . $altfile, $path);
                                # Move alternative file
                            }
                        }
                    }
                    # check for alt files that match suffix list
                    if ($staticsync_alt_suffixes) {
                        $ss_nametocheck = substr($file, 0, strlen($file) - strlen($extension) - 1);
                        //review all files still in directory and see if they are alt files matching this one
                        $althandle = opendir($folder);
                        while (($altcandidate = readdir($althandle)) !== false) {
                            if ($filetype == "file" && substr($file, 0, 1) != "." && strtolower($file) != "thumbs.db") {
                                # Find extension
                                $ext = explode(".", $altcandidate);
                                $ext = $ext[count($ext) - 1];
                                $altcandidate_name = substr($altcandidate, 0, strlen($altcandidate) - strlen($ext) - 1);
                                $altcandidate_validated = false;
                                foreach ($staticsync_alt_suffix_array as $sssuffix) {
                                    if ($altcandidate_name == $ss_nametocheck . $sssuffix) {
                                        $altcandidate_validated = true;
                                        $thisfilesuffix = $sssuffix;
                                        break;
                                    }
                                }
                                if ($altcandidate_validated) {
                                    echo date('Y-m-d H:i:s    ');
                                    echo "    Attaching {$altcandidate} as alternative.\n";
                                    $filetype = filetype($folder . "/" . $altcandidate);
                                    # Create alternative file
                                    global $lang;
                                    if (preg_match("/^_VERSO[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "Verso";
                                    } elseif (preg_match("/^_DNG[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "DNG";
                                    } elseif (preg_match("/^_ORIG[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "Original Scan";
                                    } elseif (preg_match("/^_TPV[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "Title Page Verso";
                                    } elseif (preg_match("/^_TP[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "Title Page";
                                    } elseif (preg_match("/^_COV[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "Cover";
                                    } elseif (preg_match("/^_SCR[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "Inscription";
                                    } elseif (preg_match("/^_EX[0-9]*/i", $thisfilesuffix)) {
                                        $alt_title = "Enclosure";
                                    } else {
                                        $alt_title = $altcandidate;
                                    }
                                    $aref = add_alternative_file($r, $alt_title, strtoupper($ext) . " " . $lang["file"], $altcandidate, $ext, filesize_unlimited($folder . "/" . $altcandidate));
                                    $path = get_resource_path($r, true, "", true, $ext, -1, 1, false, "", $aref);
                                    rename($folder . "/" . $altcandidate, $path);
                                    # Move alternative file
                                    global $alternative_file_previews;
                                    if ($alternative_file_previews) {
                                        create_previews($r, false, $ext, false, false, $aref);
                                    }
                                }
                            }
                        }
                    }
                    # Add to collection
                    if ($staticsync_autotheme) {
                        sql_query("insert into collection_resource(collection,resource,date_added) values ('{$collection}','{$r}',now())");
                    }
                    // fix permissions
                    // get directory to fix
                    global $scramble_key;
                    $permfixfolder = "/hne/rs/filestore/";
                    for ($n = 0; $n < strlen($r); $n++) {
                        $permfixfolder .= substr($r, $n, 1);
                        if ($n == strlen($r) - 1) {
                            $permfixfolder .= "_" . substr(md5($r . "_" . $scramble_key), 0, 15);
                        }
                        $permfixfolder .= "/";
                    }
                    exec("/bin/chown -R wwwrun {$permfixfolder}");
                    exec("/bin/chgrp -R www {$permfixfolder}");
                } else {
                    # Import failed - file still being uploaded?
                    echo date('Y-m-d H:i:s    ');
                    echo " *** Skipping file - it was not possible to move the file (still being imported/uploaded?) \n";
                }
            } else {
                # check modified date and update previews if necessary
                $filemod = filemtime($fullpath);
                if (array_key_exists($shortpath, $modtimes) && $filemod > strtotime($modtimes[$shortpath])) {
                    # File has been modified since we last created previews. Create again.
                    $rd = sql_query("select ref,has_image,file_modified,file_extension from resource where file_path='" . escape_check($shortpath) . "'");
                    if (count($rd) > 0) {
                        $rd = $rd[0];
                        $rref = $rd["ref"];
                        echo date('Y-m-d H:i:s    ');
                        echo "Resource {$rref} has changed, regenerating previews: {$fullpath}\n";
                        create_previews($rref, false, $rd["file_extension"]);
                        sql_query("update resource set file_modified=now() where ref='{$rref}'");
                    }
                }
            }
        }
    }
}
Ejemplo n.º 4
0
function ProcessFolder($folder)
	{
	#echo "<br>processing folder $folder";
	global $syncdir,$nogo,$max,$count,$done,$modtimes,$lastsync, $ffmpeg_preview_extension, $staticsync_autotheme, $staticsync_folder_structure,$staticsync_extension_mapping_default, $staticsync_extension_mapping, $staticsync_mapped_category_tree,$staticsync_title_includes_path, $staticsync_ingest, $staticsync_mapfolders,$staticsync_alternatives_suffix;
	
	$collection=0;
	
	echo "Processing Folder: $folder\n";
	
	# List all files in this folder.
	$dh=opendir($folder);
	while (($file = readdir($dh)) !== false)
		{
		$filetype=filetype($folder . "/" . $file);
		$fullpath=$folder . "/" . $file;
		$shortpath=str_replace($syncdir . "/","",$fullpath);
		# Work out extension
		$extension=explode(".",$file);$extension=trim(strtolower($extension[count($extension)-1]));
		
		if ($staticsync_mapped_category_tree)
			{
			$path_parts=explode("/",$shortpath);
			array_pop($path_parts);
			touch_category_tree_level($path_parts);
			}	
		
		# -----FOLDERS-------------
		if ((($filetype=="dir") || $filetype=="link") && ($file!=".") && ($file!="..") && (strpos($nogo,"[" . $file . "]")===false) && strpos($file,$staticsync_alternatives_suffix)===false)
			{
			# Recurse
			#echo "\n$file : " . filemtime($folder . "/" . $file) . " > " . $lastsync;
			if (true || (strlen($lastsync)=="") || (filemtime($folder . "/" . $file)>($lastsync-26000)))
				{
				ProcessFolder($folder . "/" . $file);
				}
			}
			
		# -------FILES---------------
		if (($filetype=="file") && (substr($file,0,1)!=".") && (strtolower($file)!="thumbs.db"))
			{
			# Already exists?
			if (!in_array($shortpath,$done))
				{
				$count++;if ($count>$max) {return(true);}

				echo "Processing file: $fullpath\n";
				
				if ($collection==0 && $staticsync_autotheme)
					{
					# Make a new collection for this folder.
					$e=explode("/",$shortpath);
					$theme=ucwords($e[0]);
					$themesql="theme='".ucwords(escape_check($e[0]))."'";
					$themecolumns="theme";
					$themevalues="'".ucwords(escape_check($e[0]))."'";
					
					if ($staticsync_folder_structure){
						for ($x=0;$x<count($e)-1;$x++){
							if ($x==0){} else {$themeindex=$x+1;
							global $theme_category_levels;
							if ($themeindex>$theme_category_levels){
								$theme_category_levels=$themeindex;
								if ($x==count($e)-2){echo "\n\nUPDATE THEME_CATEGORY_LEVELS TO $themeindex IN CONFIG!!!!\n\n";}
							}
							$themesql.=" and theme".$themeindex."='".ucwords(escape_check($e[$x]))."'";
							$themevalues.=",'".ucwords(escape_check($e[$x]))."'";
							$themecolumns.=",theme".$themeindex;
							}
						}
					}
					
					$name=(count($e)==1?"":$e[count($e)-2]);
					echo "\nCollection $name, theme=$theme";
					$collection=sql_value("select ref value from collection where name='" . escape_check($name) . "' and " . $themesql ,0);
					if ($collection==0){
						sql_query("insert into collection (name,created,public,$themecolumns,allow_changes) values ('" . escape_check($name) . "',now(),1,".$themevalues.",0)");
						$collection=sql_insert_id();
					}
				}

				# Work out a resource type based on the extension.
				$type=$staticsync_extension_mapping_default;
				reset ($staticsync_extension_mapping);
				foreach ($staticsync_extension_mapping as $rt=>$extensions)
					{
					if (in_array($extension,$extensions)) {$type=$rt;}
					}
				
				# Formulate a title
				if ($staticsync_title_includes_path)
					{
					$title=str_ireplace("." . $extension,"",str_replace("/"," - ",$shortpath));
					$title=ucfirst(str_replace("_"," ",$title));
					}
				else
					{
					$title=str_ireplace("." . $extension,"",$file);
					}
				
				# Import this file
				$r=import_resource($shortpath,$type,$title,$staticsync_ingest);
				if ($r!==false)
					{
					# Add to mapped category tree (if configured)
					if (isset($staticsync_mapped_category_tree))
						{
						$basepath="";
						# Save tree position to category tree field
				
						# For each node level, expand it back to the root so the full path is stored.
						for ($n=0;$n<count($path_parts);$n++)
							{
							if ($basepath!="") {$basepath.="~";}
							$basepath.=$path_parts[$n];
							$path_parts[$n]=$basepath;
							}
						
						update_field ($r,$staticsync_mapped_category_tree,"," . join(",",$path_parts));
						#echo "update_field($r,$staticsync_mapped_category_tree," . "," . join(",",$path_parts) . ");\n";
						}			

					// default access level. This may be overridden by metadata mapping.
					$accessval = 0;

					# StaticSync path / metadata mapping
					# Extract metadata from the file path as per $staticsync_mapfolders in config.php
					if (isset($staticsync_mapfolders))
						{
						foreach ($staticsync_mapfolders as $mapfolder)
							{
							$match=$mapfolder["match"];
							$field=$mapfolder["field"];
							$level=$mapfolder["level"];
														
							global $lang;

							if (strpos("/" . $shortpath,$match)!==false)
								{
								# Match. Extract metadata.
								$path_parts=explode("/",$shortpath);
								if ($level<count($path_parts))
									{
									// special cases first.
									if ($field == 'access')
										{
											// access level is a special case
											// first determine if the value matches a defined access level

											$value = $path_parts[$level-1];

											for ($n=0; $n<3; $n++){
												// if we get an exact match or a match except for case
												if ($value == $lang["access" . $n] || strtoupper($value) == strtoupper($lang['access' . $n])){
													$accessval = $n;
													echo "Will set access level to " . $lang['access' . $n] . " ($n)\n";
												}
											}

										} else {
										# Save the value
										print_r($path_parts);
										$value=$path_parts[$level-1];
										update_field ($r,$field,$value);
										echo " - Extracted metadata from path: $value\n";
										}
									}
								}
							}
						}
					
					// update access level
					sql_query("update resource set access = '$accessval' where ref = '$r'");
					
					# Add any alternative files
					$altpath=$fullpath . $staticsync_alternatives_suffix;
					if ($staticsync_ingest && file_exists($altpath))
						{
						$adh=opendir($altpath);
						while (($altfile = readdir($adh)) !== false)
							{
							$filetype=filetype($altpath . "/" . $altfile);
							if (($filetype=="file") && (substr($file,0,1)!=".") && (strtolower($file)!="thumbs.db"))
								{
								# Create alternative file
								global $lang;
								
								# Find extension
								$ext=explode(".",$altfile);$ext=$ext[count($ext)-1];
								
								$aref = add_alternative_file($r, $altfile, str_replace("?",strtoupper($ext),$lang["originalfileoftype"]), $altfile, $ext, filesize_unlimited($altpath . "/" . $altfile));
								$path=get_resource_path($r, true, "", true, $ext, -1, 1, false, "", $aref);
								rename ($altpath . "/" . $altfile,$path); # Move alternative file
								}
							}	
						}
					
					# Add to collection
					if ($staticsync_autotheme)
						{
						$test="";	
						$test=sql_query("select * from collection_resource where collection='$collection' and resource='$r'");
						if (count($test)==0){
							sql_query("insert into collection_resource(collection,resource,date_added) values ('$collection','$r',now())");
							}
						}
					}
				else
					{
					# Import failed - file still being uploaded?
					echo " *** Skipping file - it was not possible to move the file (still being imported/uploaded?) \n";
					}
				}
			else
				{
				# check modified date and update previews if necessary
				$filemod=filemtime($fullpath);
				if (array_key_exists($shortpath,$modtimes) && ($filemod>strtotime($modtimes[$shortpath])))
					{
					# File has been modified since we last created previews. Create again.
					$rd=sql_query("select ref,has_image,file_modified,file_extension from resource where file_path='" . (escape_check($shortpath)) . "'");
					if (count($rd)>0)
						{
						$rd=$rd[0];
						$rref=$rd["ref"];
						
						echo "Resource $rref has changed, regenerating previews: $fullpath\n";
						extract_exif_comment($rref,$rd["file_extension"]);
						
						# extract text from documents (e.g. PDF, DOC).
						global $extracted_text_field;
						if (isset($extracted_text_field)) {
							if (isset($unoconv_path) && in_array($extension,$unoconv_extensions)){
								// omit, since the unoconv process will do it during preview creation below
								}
							else {
							extract_text($rref,$extension);
							}
						}

						# Store original filename in field, if set
						global $filename_field;
						if (isset($filename_field))
							{
							update_field($rref,$filename_field,$file);	
							}
						
						create_previews($rref,false,$rd["file_extension"]);
						sql_query("update resource set file_modified=now() where ref='$rref'");
						}
					}
				}
			}	
		}	
	}