Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

mix_text()

Description

Utility function to randomly scramble string

Parameters

ColumnTypeDefaultDescription
$string null|string Text string to scramble
$recurse boolean true: string { global $mixcache Optionally prevent recursion (maybe called by another mix unction)
$mime_types_by_extension;

Return

string

Location

include/migration_functions.php lines 287 to 408

Definition

 
function mix_text(?string $stringbool $recurse=true): string
    
{
    global 
$mixcache$mime_types_by_extension;

    
$string ??= '';
    if (
trim($string) === '')
        {
        return 
'';
        }

    if(isset(
$mixcache[md5($string)]))
        {
        return 
$mixcache[md5($string)];
        }
    
    
debug"Converting string<br/>" $string ", recurse=" . ($recurse "TRUE" "FALSE"));

    
// Check if another function is better
    
if(validateDatetime($string) && $recurse)
        {
        
debug("This is a date - calling mix_date()");
        return 
mix_date($string);
        }
    elseif(
strpos($string,"http") === 0  && $recurse)
        {
        
debug("This is a URL - calling mix_url()");
        return 
mix_url($string);
        }
    elseif(
in_array(mb_substr($string,strrpos($string,".")),$mime_types_by_extension) && $recurse)
        {
        
debug("This is a filename - calling mix_filename()");
        return 
mix_filename($string);
        }
    
    
$numbers '0123456789';
    
$uppercons 'BCDFGHJKLMNPQRSTVWXZ';
    
$uppervowels 'AEIOUY';
    
$lowercons 'bcdfghjklmnpqrstvwxz';
    
$lowervowels 'aeiouy';
    
$noreplace "'\".,<>#-_&\$£:;^?!@+()*% \n";

    
$newstring "";
    
$bytelength strlen($string);
    
$mbytelength mb_strlen($string);

    
// Simple conversion if numbers
    
if($bytelength == $mbytelength && (string)(int)$string == $string)
        {
        
$newstring =  mt_rand(0,(int)$string);
        }
    else
        {
        
// Process each character
        
for($i=0;$i<$mbytelength;$i++)
            {
            
$oldchar mb_substr($string,$i,1);

            if(
$i && strpos($noreplace,$oldchar) === false)
                {
                
// Randomly add or remove character after first
                
$randaction mt_rand(0,10);
                if(
$randaction == 0)
                    {
                    
// Skip a character
                    
$i++;
                    }
                elseif(
$randaction == 1)
                    {
                    
// Add a character
                    
$i--;
                    }
                }

            if(
$i >= $mbytelength || $oldchar == "")
                {
                
$newstring .=  substr(str_shuffle($lowervowels $lowercons), 0,1);   
                }
            elseif(
strpos($noreplace,$oldchar) !== false)
                {
                
$newstring .= $oldchar;
                }
            elseif(
strlen($oldchar)==1)
                {
                
// Non- multibyte
                
if(strpos($lowercons,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($lowercons), 0,1);
                    }
                elseif(
strpos($uppercons,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($uppercons), 0,1);
                    }
                elseif(
strpos($lowervowels,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($lowervowels), 0,1);
                    }
                elseif(
strpos($uppervowels,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($uppervowels), 0,1);
                    }                    
                elseif(
strpos($numbers,$oldchar) !== false)
                    {
                    
$newchar substr(str_shuffle($numbers), 0,1);
                    }
                else
                    {
                    
$newchar substr(str_shuffle($noreplace), 0,1);
                    }
                
$newstring .= $newchar;        
                }                         
            else
                {
                
$newchar random_char();
                
$newstring .= $newchar;   
                } 
// End of multibyte conversion
            
}
        }

    
// Update cache
    
$mixcache[md5($string)] = $newstring;
    return 
$newstring;
    }

This article was last updated 13th December 2024 20:35 Europe/London time based on the source file dated 15th April 2024 16:05 Europe/London time.