Moved more helper methods out to the utility classes.

This commit is contained in:
2022-03-12 06:00:16 +02:00
parent 3331a4c28d
commit ec827b7369
20 changed files with 824 additions and 793 deletions

View File

@ -2167,172 +2167,6 @@ abstract class ComponentbuilderHelper
return $default;
}
/**
* Returns a GUIDv4 string
*
* Thanks to Dave Pearson (and other)
* https://www.php.net/manual/en/function.com-create-guid.php#119168
*
* Uses the best cryptographically secure method
* for all supported platforms with fallback to an older,
* less secure version.
*
* @param bool $trim
* @return string
*/
public static function GUID ($trim = true)
{
// Windows
if (function_exists('com_create_guid') === true)
{
if ($trim === true)
{
return trim(com_create_guid(), '{}');
}
return com_create_guid();
}
// set the braces if needed
$lbrace = $trim ? "" : chr(123); // "{"
$rbrace = $trim ? "" : chr(125); // "}"
// OSX/Linux
if (function_exists('openssl_random_pseudo_bytes') === true)
{
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return $lbrace . vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)) . $lbrace;
}
// Fallback (PHP 4.2+)
mt_srand((double)microtime() * 10000);
$charid = strtolower(md5(uniqid(rand(), true)));
$hyphen = chr(45); // "-"
$guidv4 = $lbrace.
substr($charid, 0, 8).$hyphen.
substr($charid, 8, 4).$hyphen.
substr($charid, 12, 4).$hyphen.
substr($charid, 16, 4).$hyphen.
substr($charid, 20, 12).
$rbrace;
return $guidv4;
}
/**
* Validate the Globally Unique Identifier ( and check if table already has this identifier)
*
* @param string $guid
* @param string $table
* @param int $id
* @return bool
*/
public static function validGUID ($guid, $table = null, $id = 0)
{
// check if we have a string
if (self::validateGUID($guid))
{
// check if table already has this identifier
if (self::checkString($table))
{
// Get the database object and a new query object.
$db = \JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('COUNT(*)')
->from('#__componentbuilder_' . (string) $table)
->where($db->quoteName('guid') . ' = ' . $db->quote($guid));
// remove this item from the list
if ($id > 0)
{
$query->where($db->quoteName('id') . ' <> ' . (int) $id);
}
// Set and query the database.
$db->setQuery($query);
$duplicate = (bool) $db->loadResult();
if ($duplicate)
{
return false;
}
}
return true;
}
return false;
}
/**
* get the ITEM of a GUID by table
*
* @param string $guid
* @param string $table
* @param string/array $what
*
* @return mix
*/
public static function getGUID ($guid, $table, $what = 'a.id')
{
// check if we have a string
if (self::validateGUID($guid))
{
// check if table already has this identifier
if (self::checkString($table))
{
// Get the database object and a new query object.
$db = \JFactory::getDbo();
$query = $db->getQuery(true);
if (self::checkArray($what))
{
$query->select($db->quoteName($what));
}
else
{
$query->select($what);
}
$query->from($db->quoteName('#__componentbuilder_' . (string) $table, 'a'))
->where($db->quoteName('a.guid') . ' = ' . $db->quote($guid));
// Set and query the database.
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
if (self::checkArray($what) || $what === 'a.*')
{
return $db->loadObject();
}
else
{
return $db->loadResult();
}
}
}
}
return false;
}
/**
* Validate the Globally Unique Identifier
*
* Thanks to Lewie
* https://stackoverflow.com/a/1515456/1429677
*
* @param string $guid
* @return bool
*/
protected static function validateGUID ($guid)
{
// check if we have a string
if (self::checkString($guid))
{
return preg_match("/^(\{)?[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}(?(1)\})$/i", $guid);
}
return false;
}
/**
* Tab/spacer bucket (to speed-up the build)
*
@ -2691,91 +2525,6 @@ abstract class ComponentbuilderHelper
}
/**
* The zipper method
*
* @param string $workingDIR The directory where the items must be zipped
* @param string $filepath The path to where the zip file must be placed
*
* @return bool true On success
*
*/
public static function zip($workingDIR, &$filepath)
{
// store the current joomla working directory
$joomla = getcwd();
// we are changing the working directory to the component temp folder
chdir($workingDIR);
// the full file path of the zip file
$filepath = Path::clean($filepath);
// delete an existing zip file (or use an exclusion parameter in Folder::files()
File::delete($filepath);
// get a list of files in the current directory tree (also the hidden files)
$files = Folder::files('.', '', true, true, array('.svn', 'CVS', '.DS_Store', '__MACOSX'), array('.*~'));
$zipArray = array();
// setup the zip array
foreach ($files as $file)
{
$tmp = array();
$tmp['name'] = str_replace('./', '', $file);
$tmp['data'] = self::getFileContents($file);
$tmp['time'] = filemtime($file);
$zipArray[] = $tmp;
}
// change back to joomla working directory
chdir($joomla);
// get the zip adapter
$adapter = new Archive();
$zip = $adapter->getAdapter('zip');
//create the zip file
if ($zip->create($filepath, $zipArray))
{
return true;
}
return false;
}
/**
* Write a file to the server
*
* @param string $path The path and file name where to safe the data
* @param string $data The data to safe
*
* @return bool true On success
*
*/
public static function writeFile($path, $data)
{
$klaar = false;
if (self::checkString($data))
{
// open the file
$fh = fopen($path, "w");
if (!is_resource($fh))
{
return $klaar;
}
// write to the file
if (fwrite($fh, $data))
{
// has been done
$klaar = true;
}
// close file.
fclose($fh);
}
return $klaar;
}
/**
* Remove folders with files
*
@ -3804,70 +3553,6 @@ abstract class ComponentbuilderHelper
return false;
}
/**
* get between
*
* @param string $content The content to search
* @param string $start The starting value
* @param string $end The ending value
* @param string $default The default value if none found
*
* @return string On success / empty string on failure
*
*/
public static function getBetween($content, $start, $end, $default = '')
{
$r = explode($start, $content);
if (isset($r[1]))
{
$r = explode($end, $r[1]);
return $r[0];
}
return $default;
}
/**
* get all between
*
* @param string $content The content to search
* @param string $start The starting value
* @param string $end The ending value
*
* @return array On success
*
*/
public static function getAllBetween($content, $start, $end)
{
// reset bucket
$bucket = array();
for ($i = 0; ; $i++)
{
// search for string
$found = self::getBetween($content,$start,$end);
if (self::checkString($found))
{
// add to bucket
$bucket[] = $found;
// build removal string
$remove = $start.$found.$end;
// remove from content
$content = str_replace($remove,'',$content);
}
else
{
break;
}
// safety catch
if ($i == 500)
{
break;
}
}
// only return unique array of values
return array_unique($bucket);
}
/**
* Field Grouping https://docs.joomla.org/Form_field
**/
@ -4220,59 +3905,6 @@ abstract class ComponentbuilderHelper
}
/**
* get the content of a file
*
* @param string $path The path to the file
* @param string/bool $none The return value if no content was found
*
* @return string On success
*
*/
public static function getFileContents($path, $none = '')
{
if (self::checkString($path))
{
// use basic file get content for now
if (($content = @file_get_contents($path)) !== FALSE)
{
return $content;
}
// use curl if available
elseif (function_exists('curl_version'))
{
// start curl
$ch = curl_init();
// set the options
$options = array();
$options[CURLOPT_URL] = $path;
$options[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
$options[CURLOPT_RETURNTRANSFER] = TRUE;
$options[CURLOPT_SSL_VERIFYPEER] = FALSE;
// load the options
curl_setopt_array($ch, $options);
// get the content
$content = curl_exec($ch);
// close the connection
curl_close($ch);
// return if found
if (self::checkString($content))
{
return $content;
}
}
elseif (property_exists('ComponentbuilderHelper', 'curlErrorLoaded') && !self::$curlErrorLoaded)
{
// set the notice
JFactory::getApplication()->enqueueMessage(JText::_('COM_COMPONENTBUILDER_HTWOCURL_NOT_FOUNDHTWOPPLEASE_SETUP_CURL_ON_YOUR_SYSTEM_OR_BCOMPONENTBUILDERB_WILL_NOT_FUNCTION_CORRECTLYP'), 'Error');
// load this notice only once
self::$curlErrorLoaded = true;
}
}
return $none;
}
/**
* the Crypt objects
**/