Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

generate_collection_access_key()

Description

Generate an external access key to allow external people to view the resources in this collection.

this which is carried forward to email_collection())

Parameters

ColumnTypeDefaultDescription
$collection integer Collection ref -or- collection data structure
$feedback integer 0
$email string ""
$access integer -1
$expires string ""
$group string ""
$sharepwd string ""
$sub_fcs array array List of sub-featured collections IDs (collection_email.php page has logic to determine

Return

string The generated key used for external sharing

Location

include/collections_functions.php lines 1636 to 1717

Definition

 
function generate_collection_access_key($collection,$feedback=0,$email="",$access=-1,$expires="",$group=""$sharepwd="", array $sub_fcs = array())
    {
    global 
$userref$usergroup$scramble_key;

    
// Default to sharing with the permission of the current usergroup if not specified OR no access to alternative group selection.
    
if($group == "" || !checkperm("x"))
        {
        
$group $usergroup;
        }

    if(!
is_array($collection))
        {
        
$collection get_collection($collection);
        }

    if(!empty(
$collection) && $collection["type"] == COLLECTION_TYPE_FEATURED && !isset($collection["has_resources"]))
        {
        
$collection_resources get_collection_resources($collection["ref"]);
        
$collection["has_resources"] = (is_array($collection_resources) && !empty($collection_resources) ? 0);
        }
    
$is_featured_collection_category is_featured_collection_category($collection);

    
// We build a collection list to allow featured collections children that are externally shared as part of a parent,
    // to all be shared with the same parameters (e.g key, access, group). When the collection is not COLLECTION_TYPE_FEATURED
    // this will hold just that collection
    
$collections = array($collection["ref"]);
    if(
$is_featured_collection_category)
        {
        
$collections = (!empty($sub_fcs) ? $sub_fcs get_featured_collection_categ_sub_fcs($collection));
        }

    
// Generate the key based on the original collection. For featured collection category, all sub featured collections
    // will share the same key
    
$k substr(md5($collection["ref"] . "," time()), 010);

    
$main_collection $collection// keep record of this info as we need it at the end to record the successful generation of a key for a featured collection category
    
$created_sub_fc_access_key false;
    foreach(
$collections as $collection)
        {
        
$r get_collection_resources($collection);
        
$shareable_resources array_filter($r, function($resource_ref) { return can_share_resource($resource_ref); });
        foreach(
$shareable_resources as $resource_ref)
            {
            
$sql sprintf("INSERT INTO external_access_keys(resource, access_key, collection, `user`, usergroup, request_feedback, email, `date`, access, expires, password_hash) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', NOW(), '%s', %s, %s)",
                
$resource_ref,
                
$k,
                
escape_check($collection),
                
$userref,
                
escape_check($group),
                
escape_check($feedback),
                
escape_check($email),
                
escape_check($access),
                
sql_null_or_val($expires$expires == ""),
                
sql_null_or_val(hash("sha256"$k $sharepwd $scramble_key), !($sharepwd != "" && $sharepwd != "(unchanged)"))
            );
            
sql_query($sql);
            
$created_sub_fc_access_key true;
            }

        
hook("generate_collection_access_key""", array($collection$k$userref$feedback$email$access$expires$group$sharepwd));
        }

    if(
$is_featured_collection_category && $created_sub_fc_access_key)
        {
        
// add for FC category. No resource. This is a dummy record so we can have a way to edit the external share done 
        // at the featured collection category level
        
$sql sprintf("INSERT INTO external_access_keys(resource, access_key, collection, `user`, usergroup, request_feedback, email, `date`, access, expires, password_hash) VALUES (NULL, '%s', '%s', '%s', '%s', '%s', '%s', NOW(), '%s', %s, %s)",
            
$k,
            
escape_check($main_collection["ref"]),
            
$userref,
            
escape_check($group),
            
escape_check($feedback),
            
escape_check($email),
            
escape_check($access),
            
sql_null_or_val($expires$expires == ""),
            
sql_null_or_val(hash("sha256"$k $sharepwd $scramble_key), !($sharepwd != "" && $sharepwd != "(unchanged)"))
        );
        
sql_query($sql);
        }

    return 
$k;
    }

This article was last updated 30th November 2020 14:35 Europe/London time based on the source file dated 25th November 2020 10:35 Europe/London time.