Component-Builder/site/helpers/componentbuilder.php

2907 lines
85 KiB
PHP

<?php
/*--------------------------------------------------------------------------------------------------------| www.vdm.io |------/
__ __ _ _____ _ _ __ __ _ _ _
\ \ / / | | | __ \ | | | | | \/ | | | | | | |
\ \ / /_ _ ___| |_ | | | | _____ _____| | ___ _ __ _ __ ___ ___ _ __ | |_ | \ / | ___| |_| |__ ___ __| |
\ \/ / _` / __| __| | | | |/ _ \ \ / / _ \ |/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __| | |\/| |/ _ \ __| '_ \ / _ \ / _` |
\ / (_| \__ \ |_ | |__| | __/\ V / __/ | (_) | |_) | | | | | | __/ | | | |_ | | | | __/ |_| | | | (_) | (_| |
\/ \__,_|___/\__| |_____/ \___| \_/ \___|_|\___/| .__/|_| |_| |_|\___|_| |_|\__| |_| |_|\___|\__|_| |_|\___/ \__,_|
| |
|_|
/-------------------------------------------------------------------------------------------------------------------------------/
@version 2.7.x
@created 30th April, 2015
@package Component Builder
@subpackage componentbuilder.php
@author Llewellyn van der Merwe <http://joomlacomponentbuilder.com>
@github Joomla Component Builder <https://github.com/vdm-io/Joomla-Component-Builder>
@copyright Copyright (C) 2015. All Rights Reserved
@license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
Builds Complex Joomla Components
/-----------------------------------------------------------------------------------------------------------------------------*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Componentbuilder component helper
*/
abstract class ComponentbuilderHelper
{
/**
* Locked Libraries (we can not have these change)
**/
public static $libraryNames = array(1 => 'No Library', 2 => 'Bootstrap v4', 3 => 'Uikit v3', 4 => 'Uikit v2', 5 => 'FooTable v2', 6 => 'FooTable v3');
/**
* The global params
**/
protected static $params = false;
/**
* The local company details
**/
protected static $localCompany = array();
/**
* The snippet paths
**/
public static $snippetPath = 'https://raw.githubusercontent.com/vdm-io/Joomla-Component-Builder-Snippets/master/';
public static $snippetsPath = 'https://api.github.com/repos/vdm-io/Joomla-Component-Builder-Snippets/git/trees/master';
/**
* The packages paths
**/
public static $jcbGithubPackagesUrl = "https://api.github.com/repos/vdm-io/JCB-Packages/git/trees/master";
public static $jcbGithubPackageUrl = "https://github.com/vdm-io/JCB-Packages/raw/master/";
// not needed at this time (maybe latter)
public static $accessToken = "";
/**
* get the github repo file list
*
* @return array on success
*
*/
public static function getGithubRepoFileList($type, $target)
{
// get the current Packages (public)
if (!$repoData = self::get($type))
{
if (self::urlExists($target))
{
$repoData = self::getFileContents($target);
if (self::checkJson($repoData))
{
$test = json_decode($repoData);
if (self::checkObject($test) && isset($test->tree) && self::checkArray($test->tree) )
{
// remember to set it
self::set($type, $repoData);
}
// check if we have error message from github
elseif ($errorMessage = self::githubErrorHandeler(array('error' => null), $test))
{
if (self::checkString($errorMessage['error']))
{
JFactory::getApplication()->enqueueMessage($errorMessage['error'], 'Error');
}
$repoData = false;
}
}
else
{
$repoData = false;
}
}
else
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_URL_S_SET_TO_RETRIEVE_THE_PACKAGES_DOES_NOT_EXIST', $target), 'Error');
}
}
// check if we could find packages
if (isset($repoData) && self::checkJson($repoData))
{
$repoData = json_decode($repoData);
if (self::checkObject($repoData) && isset($repoData->tree) && self::checkArray($repoData->tree) )
{
return $repoData->tree;
}
}
return false;
}
/**
* get the github error messages
*
* @return array of errors on success
*
*/
protected static function githubErrorHandeler($message, &$github)
{
if (self::checkObject($github) && isset($github->message) && self::checkString($github->message))
{
// set the message
$errorMessage = $github->message;
// add the documentation URL
if (isset($github->documentation_url) && self::checkString($github->documentation_url))
{
$errorMessage = $errorMessage.'<br />'.$github->documentation_url;
}
// check the message
if (strpos($errorMessage, 'Authenticated') !== false)
{
// add little more help if it is an access token issue
$errorMessage = JText::sprintf('COM_COMPONENTBUILDER_SBR_YOU_CAN_ADD_AN_BACCESS_TOKENB_TO_GETBIBLE_GLOBAL_OPTIONS_TO_MAKE_AUTHENTICATED_REQUESTS_AN_ACCESS_TOKEN_WITH_ONLY_PUBLIC_ACCESS_WILL_DO', $errorMessage);
}
// set error notice
$message['error'] = $errorMessage;
// we have error message
return $message;
}
return false;
}
/**
* The array of constant paths
*
* JPATH_SITE is meant to represent the root path of the JSite application,
* just as JPATH_ADMINISTRATOR is mean to represent the root path of the JAdministrator application.
*
* JPATH_BASE is the root path for the current requested application.... so if you are in the administrator application:
*
* JPATH_BASE == JPATH_ADMINISTRATOR
*
* If you are in the site application:
*
* JPATH_BASE == JPATH_SITE
*
* If you are in the installation application:
*
* JPATH_BASE == JPATH_INSTALLATION.
*
* JPATH_ROOT is the root path for the Joomla install and does not depend upon any application.
*
* @var array
*/
public static $constantPaths = array(
// The path to the administrator folder.
'JPATH_ADMINISTRATOR' => JPATH_ADMINISTRATOR,
// The path to the installed Joomla! site, or JPATH_ROOT/administrator if executed from the backend.
'JPATH_BASE' => JPATH_BASE,
// The path to the cache folder.
'JPATH_CACHE' => JPATH_CACHE,
// The path to the administration folder of the current component being executed.
'JPATH_COMPONENT_ADMINISTRATOR' => JPATH_COMPONENT_ADMINISTRATOR,
// The path to the site folder of the current component being executed.
'JPATH_COMPONENT_SITE' => JPATH_COMPONENT_SITE,
// The path to the current component being executed.
'JPATH_COMPONENT' => JPATH_COMPONENT,
// The path to folder containing the configuration.php file.
'JPATH_CONFIGURATION' => JPATH_CONFIGURATION,
// The path to the installation folder.
'JPATH_INSTALLATION' => JPATH_INSTALLATION,
// The path to the libraries folder.
'JPATH_LIBRARIES' => JPATH_LIBRARIES,
// The path to the plugins folder.
'JPATH_PLUGINS' => JPATH_PLUGINS,
// The path to the installed Joomla! site.
'JPATH_ROOT' => JPATH_ROOT,
// The path to the installed Joomla! site.
'JPATH_SITE' => JPATH_SITE,
// The path to the templates folder.
'JPATH_THEMES' => JPATH_THEMES
);
/**
* Get the snippet contributor details
*
* @param string $filename The file name
* @param string $type The type of file
*
* @return array On success the contributor details
*
*/
public static function getContributorDetails($filename, $type = 'snippet')
{
// start loading he contributor details
$contributor = array();
// get the path & content
switch ($type)
{
case 'snippet':
$path = $snippetPath.$filename;
// get the file if available
$content = self::getFileContents($path);
if (self::checkJson($content))
{
$content = json_decode($content, true);
}
break;
default:
// only allow types that are being targeted
return false;
break;
}
// see if we have content and all needed details
if (isset($content) && self::checkArray($content)
&& isset($content['contributor_company'])
&& isset($content['contributor_name'])
&& isset($content['contributor_email'])
&& isset($content['contributor_website']))
{
// got the details from file
return array('contributor_company' => $content['contributor_company'] ,'contributor_name' => $content['contributor_name'], 'contributor_email' => $content['contributor_email'], 'contributor_website' => $content['contributor_website'], 'origin' => 'file');
}
// get the global settings
if (!self::checkObject(self::$params))
{
self::$params = JComponentHelper::getParams('com_componentbuilder');
}
// get the global company details
if (!self::checkArray(self::$localCompany))
{
// Set the person sharing information (default VDM ;)
self::$localCompany['company'] = self::$params->get('export_company', 'Vast Development Method');
self::$localCompany['owner'] = self::$params->get('export_owner', 'Llewellyn van der Merwe');
self::$localCompany['email'] = self::$params->get('export_email', 'joomla@vdm.io');
self::$localCompany['website'] = self::$params->get('export_website', 'https://www.vdm.io/');
}
// default global
return array('contributor_company' => self::$localCompany['company'] ,'contributor_name' => self::$localCompany['owner'], 'contributor_email' => self::$localCompany['email'], 'contributor_website' => self::$localCompany['website'], 'origin' => 'global');
}
/**
* Get the library files
*
* @param int $id The library id to target
*
* @return array On success the array of files that belong to this library
*
*/
public static function getLibraryFiles($id)
{
// get the library files, folders, and urls
$files = array();
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array('b.name','a.addurls','a.addfolders','a.addfiles')));
$query->from($db->quoteName('#__componentbuilder_library_files_folders_urls','a'));
$query->join('LEFT', $db->quoteName('#__componentbuilder_library', 'b') . ' ON (' . $db->quoteName('a.library') . ' = ' . $db->quoteName('b.id') . ')');
$query->where($db->quoteName('a.library') . ' = ' . (int) $id);
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
// prepare the files
$result = $db->loadObject();
// first we load the URLs
if (self::checkJson($result->addurls))
{
// convert to array
$result->addurls = json_decode($result->addurls, true);
// set urls
if (self::checkArray($result->addurls))
{
// build media folder path
$mediaPath = '/media/' . strtolower( preg_replace('/\s+/', '-', self::safeString($result->name, 'filename', ' ', false)));
// load the urls
foreach($result->addurls as $url)
{
if (isset($url['url']) && self::checkString($url['url']))
{
// set the path if needed
if (isset($url['type']) && $url['type'] > 1)
{
$fileName = basename($url['url']);
// build sub path
if (strpos($fileName, '.js') !== false)
{
$path = '/js';
}
elseif (strpos($fileName, '.css') !== false)
{
$path = '/css';
}
else
{
$path = '';
}
// set the path to library file
$url['path'] = $mediaPath . $path . '/' . $fileName; // we need this for later
}
// if local path is set, then use it first
if (isset($url['path']))
{
// load document script
$files[md5($url['path'])] = '(' . JText::_('URL') . ') ' . basename($url['url']) . ' - ' . JText::_('COM_COMPONENTBUILDER_LOCAL');
}
// check if link must be added
if (isset($url['url']) && ((isset($url['type']) && $url['type'] == 1) || (isset($url['type']) && $url['type'] == 3) || !isset($url['type'])))
{
// load url also if not building document
$files[md5($url['url'])] = '(' . JText::_('URL') . ') ' . basename($url['url']) . ' - ' . JText::_('COM_COMPONENTBUILDER_LINK');
}
}
}
}
}
// load the local files
if (self::checkJson($result->addfiles))
{
// convert to array
$result->addfiles = json_decode($result->addfiles, true);
// set files
if (self::checkArray($result->addfiles))
{
foreach($result->addfiles as $file)
{
if (isset($file['file']) && isset($file['path']))
{
$path = '/'.trim($file['path'], '/');
// check if path has new file name (has extetion)
$pathInfo = pathinfo($path);
if (isset($pathInfo['extension']) && $pathInfo['extension'])
{
// load document script
$files[md5($path)] = '(' . JText::_('COM_COMPONENTBUILDER_FILE') . ') ' . $file['file'];
}
else
{
// load document script
$files[md5($path.'/'.trim($file['file'],'/'))] = '(' . JText::_('COM_COMPONENTBUILDER_FILE') . ') ' . $file['file'];
}
}
}
}
}
// load the files in the folder
if (self::checkJson($result->addfolders))
{
// convert to array
$result->addfolders = json_decode($result->addfolders, true);
// set folder
if (self::checkArray($result->addfolders))
{
// get the global settings
if (!self::checkObject(self::$params))
{
self::$params = JComponentHelper::getParams('com_componentbuilder');
}
// reset bucket
$bucket = array();
// get custom folder path
$customPath = '/'.trim(self::$params->get('custom_folder_path', JPATH_COMPONENT_ADMINISTRATOR.'/custom'), '/');
// get all the file paths
foreach ($result->addfolders as $folder)
{
if (isset($folder['path']) && isset($folder['folder']))
{
$_path = '/'.trim($folder['path'], '/');
$customFolder = '/'.trim($folder['folder'], '/');
if (isset($folder['rename']) && 1 == $folder['rename'])
{
if ($_paths = self::getAllFilePaths($customPath.$customFolder))
{
$bucket[$_path] = $_paths;
}
}
else
{
$path = $_path.$customFolder;
if ($_paths = self::getAllFilePaths($customPath.$customFolder))
{
$bucket[$path] = $_paths;
}
}
}
}
// now load the script
if (self::checkArray($bucket))
{
foreach ($bucket as $root => $paths)
{
// load per path
foreach($paths as $path)
{
$files[md5($root.'/'.trim($path, '/'))] = '(' . JText::_('COM_COMPONENTBUILDER_FOLDER') . ') ' . basename($path) . ' - ' . basename($root);
}
}
}
}
}
// return files if found
if (self::checkArray($files))
{
return $files;
}
}
return false;
}
/**
* get all the file paths in folder and sub folders
*
* @param string $folder The local path to parse
* @param array $fileTypes The type of files to get
*
* @return void
*
*/
public static function getAllFilePaths($folder, $fileTypes = array('\.php', '\.js', '\.css', '\.less'))
{
if (JFolder::exists($folder))
{
// we must first store the current woking directory
$joomla = getcwd();
// we are changing the working directory to the componet path
chdir($folder);
// get the files
foreach ($fileTypes as $type)
{
// get a list of files in the current directory tree
$files[] = JFolder::files('.', $type, true, true);
}
// change back to Joomla working directory
chdir($joomla);
// return array of files
return array_map( function($file) { return str_replace('./', '/', $file); }, (array) self::mergeArrays($files));
}
return false;
}
/**
* get all component IDs
*/
public static function getComponentIDs()
{
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array('id')));
$query->from($db->quoteName('#__componentbuilder_joomla_component'));
$query->where($db->quoteName('published') . ' >= 1'); // do not backup trash
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
return $db->loadColumn();
}
return false;
}
/**
* Autoloader
*/
public static function autoLoader($type = 'compiler')
{
// load the type classes
if ('smart' !== $type)
{
foreach (glob(JPATH_ADMINISTRATOR."/components/com_componentbuilder/helpers/".$type."/*.php") as $autoFile)
{
require_once $autoFile;
}
}
// load only if compiler
if ('compiler' === $type)
{
// import the Joomla librarys
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');
jimport('joomla.application.component.modellist');
// include class to minify js
require_once JPATH_ADMINISTRATOR.'/components/com_componentbuilder/helpers/js.php';
}
// load only if smart
if ('smart' === $type)
{
// import the Joomla libraries
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');
jimport('joomla.application.component.modellist');
}
// load this for all
jimport('joomla.application');
}
/**
* Remove folders with files
*
* @param string $dir The path to folder to remove
* @param boolean $ignore The folders and files to ignore and not remove
*
* @return boolean True in all is removed
*
*/
public static function removeFolder($dir, $ignore = false)
{
if (JFolder::exists($dir))
{
$it = new RecursiveDirectoryIterator($dir);
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($it as $file)
{
if ('.' === $file->getBasename() || '..' === $file->getBasename()) continue;
if ($file->isDir())
{
$keeper = false;
if (self::checkArray($ignore))
{
foreach ($ignore as $keep)
{
if (strpos($file->getPathname(), $dir.'/'.$keep) !== false)
{
$keeper = true;
}
}
}
if ($keeper)
{
continue;
}
JFolder::delete($file->getPathname());
}
else
{
$keeper = false;
if (self::checkArray($ignore))
{
foreach ($ignore as $keep)
{
if (strpos($file->getPathname(), $dir.'/'.$keep) !== false)
{
$keeper = true;
}
}
}
if ($keeper)
{
continue;
}
JFile::delete($file->getPathname());
}
}
if (!self::checkArray($ignore))
{
return JFolder::delete($dir);
}
return true;
}
return false;
}
/**
* The dynamic builder of views, tables and fields
**/
public static function dynamicBuilder(&$data, $type)
{
self::autoLoader('extrusion');
$extruder = new Extrusion($data);
}
/**
* 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 = JPath::clean($filepath);
// delete an existing zip file (or use an exclusion parameter in JFolder::files()
JFile::delete($filepath);
// get a list of files in the current directory tree
$files = JFolder::files('.', '', true, true);
$zipArray = array();
// setup the zip array
foreach ($files as $file)
{
$tmp = array();
$tmp['name'] = str_replace('./', '', $file);
$tmp['data'] = JFile::read($file);
$tmp['time'] = filemtime($file);
$zipArray[] = $tmp;
}
// change back to joomla working directory
chdir($joomla);
// get the zip adapter
$zip = JArchive::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;
}
public static function getFieldOptions($value, $type, $settings = array())
{
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array('properties', 'short_description', 'description')));
$query->from($db->quoteName('#__componentbuilder_fieldtype'));
$query->where($db->quoteName('published') . ' = 1');
$query->where($db->quoteName($type) . ' = '. $value);
// Reset the query using our newly populated query object.
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
$result = $db->loadObject();
$properties = json_decode($result->properties,true);
$field = array('values' => "<field ", 'values_description' => '<table class="uk-table uk-table-hover uk-table-striped uk-table-condensed">', 'short_description' => $result->short_description, 'description' => $result->description);
// set the headers
$field['values_description'] .= '<thead><tr><th class="uk-text-right">'.JText::_('COM_COMPONENTBUILDER_PROPERTY').'</th><th>'.JText::_('COM_COMPONENTBUILDER_EXAMPLE').'</th><th>'.JText::_('COM_COMPONENTBUILDER_DESCRIPTION').'</th></thead><tbody>';
foreach ($properties as $property)
{
$example = (isset($property['example']) && self::checkString($property['example'])) ? self::shorten($property['example'], 30) : '';
$field['values_description'] .= '<tr><td class="uk-text-right"><code>'.$property['name'].'</code></td><td>'.$example.'</td><td>'.$property['description'].'</td></tr>';
if(isset($settings[$property['name']]))
{
$field['values'] .= "\n\t".$property['name'].'="'.$settings[$property['name']].'" ';
}
else
{
$field['values'] .= "\n\t".$property['name'].'="'.$property['example'].'" ';
}
}
$field['values'] .= "\n/>";
$field['values_description'] .= '</tbody></table>';
// return found field options
return $field;
}
return false;
}
/**
* the basic localkey
**/
protected static $localkey = false;
/**
* get the localkey
**/
public static function getLocalKey()
{
if (!self::$localkey)
{
// get the basic key
self::$localkey = md5(self::getCryptKey('basic', 'localKey34fdWEkl'));
}
return self::$localkey;
}
/**
* indent HTML
*/
public static function indent($html)
{
// load the class
require_once JPATH_ADMINISTRATOR.'/components/com_componentbuilder/helpers/indenter.php';
// set new indenter
$indenter = new Indenter();
// return indented html
return $indenter->indent($html);
}
public static function checkFileType($file, $sufix)
{
// now check if the file ends with the sufix
return $sufix === "" || ($sufix == substr(strrchr($file, "."), -strlen($sufix)));
}
public static function imageInfo($path,$request = 'type')
{
// set image
$image = JPATH_SITE.'/'.$path;
// check if exists
if (file_exists($image) && $result = @getimagesize($image))
{
// return type request
switch ($request)
{
case 'width':
return $result[0];
break;
case 'height':
return $result[1];
break;
case 'type':
$extensions = array(
IMAGETYPE_GIF => "gif",
IMAGETYPE_JPEG => "jpg",
IMAGETYPE_PNG => "png",
IMAGETYPE_SWF => "swf",
IMAGETYPE_PSD => "psd",
IMAGETYPE_BMP => "bmp",
IMAGETYPE_TIFF_II => "tiff",
IMAGETYPE_TIFF_MM => "tiff",
IMAGETYPE_JPC => "jpc",
IMAGETYPE_JP2 => "jp2",
IMAGETYPE_JPX => "jpx",
IMAGETYPE_JB2 => "jb2",
IMAGETYPE_SWC => "swc",
IMAGETYPE_IFF => "iff",
IMAGETYPE_WBMP => "wbmp",
IMAGETYPE_XBM => "xbm",
IMAGETYPE_ICO => "ico"
);
return $extensions[$result[2]];
break;
case 'attr':
return $result[3];
break;
case 'all':
default:
return $result;
break;
}
}
return false;
}
/**
* get between
*
* @param string $content The content to search
* @param string $start The starting value
* @param string $end The ending value
*
* @return string On success / empty string on failure
*
*/
public static function getBetween($content, $start, $end)
{
$r = explode($start, $content);
if (isset($r[1]))
{
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
/**
* 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);
}
public static function typeField($type,$option = 'default')
{
// list of default fields
// https://docs.joomla.org/Form_field
$fields = array(
'default' => array(
'accesslevel','cachehandler','calendar','captcha','category','checkbox',
'checkboxes','color','combo','componentlayout','contentlanguage','editor',
'chromestyle','contenttype','databaseconnection','editors','email','file',
'filelist','folderlist','groupedlist','hidden','file','headertag','helpsite',
'imagelist','integer','language','list','media','menu','note','password',
'plugins','range','radio','repeatable','rules','subform','sessionhandler','spacer','sql','tag',
'tel','menuitem','modulelayout','meter','moduleorder','moduleposition','moduletag',
'templatestyle','text','textarea','timezone','url','user','usergroup'
),
'text' => array(
'calendar','color','editor','email','password','tel','text','textarea','url','number','range'
),
'list' => array(
'checkboxes','checkbox','list','radio'
),
'dynamic' => array(
'category','headertag','tag','rules','user','file','filelist','folderlist','imagelist','integer','timezone','media','meter'
)
);
if (in_array($type,$fields[$option]))
{
return true;
}
return false;
}
/**
* set the session defaults if not set
**/
protected static function setSessionDefaults()
{
// noting for now
return true;
}
/**
* the Butler
**/
public static $session = array();
/**
* the Butler Assistant
**/
protected static $localSession = array();
/**
* start a session if not already set, and load with data
**/
public static function loadSession()
{
if (!isset(self::$session) || !self::checkObject(self::$session))
{
self::$session = JFactory::getSession();
}
// set the defaults
self::setSessionDefaults();
}
/**
* give Session more to keep
**/
public static function set($key, $value)
{
// set to local memory to speed up program
self::$localSession[$key] = $value;
// load to session for later use
return self::$session->set($key, self::$localSession[$key]);
}
/**
* get info from Session
**/
public static function get($key, $default = null)
{
// check if in local memory
if (!isset(self::$localSession[$key]))
{
// set to local memory to speed up program
self::$localSession[$key] = self::$session->get($key, $default);
}
return self::$localSession[$key];
}
/**
* check if it is a new hash
**/
public static function newHash($hash, $name = 'backup', $type = 'hash', $key = '', $fileType = 'txt')
{
// make sure we have a hash
if (self::checkString($hash))
{
// first get the file path
$path_filename = self::getFilePath('path', $name.$type, $fileType, $key, JPATH_COMPONENT_ADMINISTRATOR);
// set as read if not already set
if ($content = self::getFileContents($path_filename, false))
{
if ($hash == $content)
{
return false;
}
}
// set the hash
return self::writeFile($path_filename, $hash);
}
return false;
}
/**
* prepare base64 string for url
**/
public static function base64_urlencode($string, $encode = false)
{
if ($encode)
{
$string = base64_encode($string);
}
return str_replace(array('+', '/'), array('-', '_'), $string);
}
/**
* prepare base64 string form url
**/
public static function base64_urldecode($string, $decode = false)
{
$string = str_replace(array('-', '_'), array('+', '/'), $string);
if ($decode)
{
$string = base64_decode($string);
}
return $string;
}
/**
* Check if the url exist
*
* @param string $url The url to check
*
* @return bool If exist true
*
*/
public static function urlExists($url)
{
$exists = false;
// check if we can use curl
if (function_exists('curl_version'))
{
// initiate curl
$ch = curl_init($url);
// CURLOPT_NOBODY (do not return body)
curl_setopt($ch, CURLOPT_NOBODY, true);
// make call
$result = curl_exec($ch);
// check return value
if ($result !== false)
{
// get the http CODE
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($statusCode !== 404)
{
$exists = true;
}
}
// close the connection
curl_close($ch);
}
elseif ($headers = @get_headers($url))
{
if(isset($headers[0]) && is_string($headers[0]) && strpos($headers[0],'404') === false)
{
$exists = true;
}
}
return $exists;
}
/**
* Get the file path or url
*
* @param string $type The (url/path) type to return
* @param string $target The Params Target name (if set)
* @param string $fileType The kind of filename to generate (if not set no file name is generated)
* @param string $key The key to adjust the filename (if not set ignored)
* @param string $default The default path if not set in Params (fallback path)
* @param bool $createIfNotSet The switch to create the folder if not found
*
* @return string On success the path or url is returned based on the type requested
*
*/
public static function getFilePath($type = 'path', $target = 'filepath', $fileType = null, $key = '', $default = JPATH_SITE . '/images/', $createIfNotSet = true)
{
// get the global settings
if (!self::checkObject(self::$params))
{
self::$params = JComponentHelper::getParams('com_componentbuilder');
}
$filePath = self::$params->get($target, $default);
// check the file path (revert to default only of not a hidden file path)
if ('hiddenfilepath' !== $target && strpos($filePath, JPATH_SITE) === false)
{
$filePath = $default;
}
jimport('joomla.filesystem.folder');
// create the folder if it does not exist
if ($createIfNotSet && !JFolder::exists($filePath))
{
JFolder::create($filePath);
}
// setup the file name
$fileName = '';
// Get basic key
$basickey = 'Th!s_iS_n0t_sAfe_buT_b3tter_then_n0thiug';
if (method_exists(get_called_class(), "getCryptKey"))
{
$basickey = self::getCryptKey('basic', $basickey);
}
// check the key
if (!self::checkString($key))
{
$key = 'vDm';
}
// set the file name
if (self::checkString($fileType))
{
// set the name
$fileName = trim(md5($type.$target.$basickey.$key) . '.' . trim($fileType, '.'));
}
else
{
$fileName = trim(md5($type.$target.$basickey.$key)) . '.txt';
}
// return the url
if ('url' === $type)
{
if (strpos($filePath, JPATH_SITE) !== false)
{
$filePath = trim( str_replace( JPATH_SITE, '', $filePath), '/');
return JURI::root() . $filePath . '/' . $fileName;
}
// since the path is behind the root folder of the site, return only the root url (may be used to build the link)
return JURI::root();
}
// sanitize the path
return '/' . trim( $filePath, '/' ) . '/' . $fileName;
}
/**
* Get the file path or url
*
* @param string $type The (url/path) type to return
* @param string $target The Params Target name (if set)
* @param string $default The default path if not set in Params (fallback path)
* @param bool $createIfNotSet The switch to create the folder if not found
*
* @return string On success the path or url is returned based on the type requested
*
*/
public static function getFolderPath($type = 'path', $target = 'folderpath', $default = JPATH_SITE . '/images/', $createIfNotSet = true)
{
// get the global settings
if (!self::checkObject(self::$params))
{
self::$params = JComponentHelper::getParams('com_componentbuilder');
}
$folderPath = self::$params->get($target, $default);
jimport('joomla.filesystem.folder');
// create the folder if it does not exist
if ($createIfNotSet && !JFolder::exists($folderPath))
{
JFolder::create($folderPath);
}
// return the url
if ('url' === $type)
{
if (strpos($folderPath, JPATH_SITE) !== false)
{
$folderPath = trim( str_replace( JPATH_SITE, '', $folderPath), '/');
return JURI::root() . $folderPath . '/';
}
// since the path is behind the root folder of the site, return only the root url (may be used to build the link)
return JURI::root();
}
// sanitize the path
return '/' . trim( $folderPath, '/' ) . '/';
}
/**
* 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;
}
/**
* Composer Switch
**/
protected static $composer = false;
/**
* Load the Composer Vendors
**/
public static function composerAutoload()
{
// insure we load the composer vendors only once
if (!self::$composer)
{
// load the autoloader
require_once JPATH_SITE.'/libraries/vdm_io/vendor/autoload.php';
// do not load again
self::$composer = true;
}
}
/**
* Move File to Server
*
* @param string $localPath The local path to the file
* @param string $fileName The the actual file name
* @param int $serverID The server local id to use
* @param int $protocol The server protocol to use
* @param string $permission The permission validation area
*
* @return bool true on success
**/
public static function moveToServer($localPath, $fileName, $serverID, $protocol = null, $permission = 'core.export')
{
// get the server
if ($server = self::getServer( (int) $serverID, $protocol, $permission))
{
// use the FTP protocol
if (1 == $server->jcb_protocol)
{
// now move the file
if (!$server->store($localPath, $fileName))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_BSB_FILE_COULD_NOT_BE_MOVED_TO_BSB_SERVER', $fileName, $server->jcb_remote_server_name[(int) $serverID]), 'Error');
return false;
}
// close the connection
$server->quit();
}
// use the SFTP protocol
elseif (2 == $server->jcb_protocol)
{
// now move the file
if (!$server->put($server->jcb_remote_server_path[(int) $serverID] . $fileName, self::getFileContents($localPath, null)))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_BSB_FILE_COULD_NOT_BE_MOVED_TO_BSB_PATH_ON_BSB_SERVER', $fileName, $server->jcb_remote_server_path[(int) $serverID], $server->jcb_remote_server_name[(int) $serverID]), 'Error');
return false;
}
}
return true;
}
return false;
}
/**
* the SFTP objects
**/
protected static $sftp = array();
/**
* the FTP objects
**/
protected static $ftp = array();
/**
* get the server object
*
* @param int $serverID The server local id to use
* @param int $protocol The server protocol to use
* @param string $permission The permission validation area
*
* @return object on success server object
**/
public static function getServer($serverID, $protocol = null, $permission = 'core.export')
{
// if not protocol is given get it (sad I know)
if (!$protocol)
{
$protocol = self::getVar('server', (int) $serverID, 'id', 'protocol');
}
// return the server object
switch ($protocol)
{
case 1: // FTP
return self::getFtp($serverID, $permission);
break;
case 2: // SFTP
return self::getSftp($serverID, $permission);
break;
}
return false;
}
/**
* get the sftp object
*
* @param int $serverID The server local id to use
* @param string $permission The permission validation area
*
* @return object on success with sftp power
**/
public static function getSftp($serverID, $permission = 'core.export')
{
// check if we have a server with that id
if ($server = self::getServerDetails($serverID, 2, $permission))
{
// check if it was already set
if (!isset(self::$sftp[$server->cache]) || !self::checkObject(self::$sftp[$server->cache]))
{
// make sure we have the composer classes loaded
self::composerAutoload();
// make sure we have the phpseclib classes
if (!class_exists('\phpseclib\Net\SFTP'))
{
// class not in place so send out error
JFactory::getApplication()->enqueueMessage(JText::_('COM_COMPONENTBUILDER_THE_BPHPSECLIBNETSFTPB_LIBRARYCLASS_IS_NOT_AVAILABLE_THIS_LIBRARYCLASS_SHOULD_HAVE_BEEN_ADDED_TO_YOUR_BLIBRARIESVDM_IOVENDORB_FOLDER_PLEASE_CONTACT_YOUR_SYSTEM_ADMINISTRATOR_FOR_MORE_INFO'), 'Error');
return false;
}
// insure the port is set
$server->port = (isset($server->port) && is_int($server->port) && $server->port > 0) ? $server->port : 22;
// open the connection
self::$sftp[$server->cache] = new phpseclib\Net\SFTP($server->host, $server->port);
// heads-up on protocol
self::$sftp[$server->cache]->jcb_protocol = 2; // SFTP <-- if called not knowing what type of protocol is being used
// now login based on authentication type
switch($server->authentication)
{
case 1: // password
if (!self::$sftp[$server->cache]->login($server->username, $server->password))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_LOGIN_TO_BSB_HAS_FAILED_PLEASE_CHECK_THAT_YOUR_DETAILS_ARE_CORRECT', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
break;
case 2: // private key file
if (self::checkObject(self::crypt('RSA')))
{
// check if we have a passprase
if (self::checkString($server->secret))
{
self::crypt('RSA')->setPassword($server->secret);
}
// now load the key file
if (!self::crypt('RSA')->loadKey(self::getFileContents($server->private, null)))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_PRIVATE_KEY_FILE_COULD_NOT_BE_LOADEDFOUND_FOR_BSB_SERVER', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
// now login
if (!self::$sftp[$server->cache]->login($server->username, self::crypt('RSA')))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_LOGIN_TO_BSB_HAS_FAILED_PLEASE_CHECK_THAT_YOUR_DETAILS_ARE_CORRECT', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
}
break;
case 3: // both password and private key file
if (self::checkObject(self::crypt('RSA')))
{
// check if we have a passphrase
if (self::checkString($server->secret))
{
self::crypt('RSA')->setPassword($server->secret);
}
// now load the key file
if (!self::crypt('RSA')->loadKey(self::getFileContents($server->private, null)))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_PRIVATE_KEY_FILE_COULD_NOT_BE_LOADEDFOUND_FOR_BSB_SERVER', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
// now login
if (!self::$sftp[$server->cache]->login($server->username, $server->password, self::crypt('RSA')))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_LOGIN_TO_BSB_HAS_FAILED_PLEASE_CHECK_THAT_YOUR_DETAILS_ARE_CORRECT', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
}
break;
case 4: // private key field
if (self::checkObject(self::crypt('RSA')))
{
// check if we have a passprase
if (self::checkString($server->secret))
{
self::crypt('RSA')->setPassword($server->secret);
}
// now load the key field
if (!self::crypt('RSA')->loadKey($server->private_key))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_PRIVATE_KEY_FIELD_COULD_NOT_BE_LOADED_FOR_BSB_SERVER', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
// now login
if (!self::$sftp[$server->cache]->login($server->username, self::crypt('RSA')))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_LOGIN_TO_BSB_HAS_FAILED_PLEASE_CHECK_THAT_YOUR_DETAILS_ARE_CORRECT', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
}
break;
case 5: // both password and private key field
if (self::checkObject(self::crypt('RSA')))
{
// check if we have a passphrase
if (self::checkString($server->secret))
{
self::crypt('RSA')->setPassword($server->secret);
}
// now load the key file
if (!self::crypt('RSA')->loadKey($server->private_key))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_PRIVATE_KEY_FIELD_COULD_NOT_BE_LOADED_FOR_BSB_SERVER', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
// now login
if (!self::$sftp[$server->cache]->login($server->username, $server->password, self::crypt('RSA')))
{
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_LOGIN_TO_BSB_HAS_FAILED_PLEASE_CHECK_THAT_YOUR_DETAILS_ARE_CORRECT', $server->name), 'Error');
unset(self::$sftp[$server->cache]);
return false;
}
}
break;
}
}
// only continue if object is set
if (isset(self::$sftp[$server->cache]) && self::checkObject(self::$sftp[$server->cache]))
{
// set the unique buckets
if (!isset(self::$sftp[$server->cache]->jcb_remote_server_name))
{
self::$sftp[$server->cache]->jcb_remote_server_name = array();
self::$sftp[$server->cache]->jcb_remote_server_path = array();
}
// always set the name and remote server path
self::$sftp[$server->cache]->jcb_remote_server_name[$serverID] = $server->name;
self::$sftp[$server->cache]->jcb_remote_server_path[$serverID] = (self::checkString($server->path) && $server->path !== '/') ? $server->path : '';
// return the sftp object
return self::$sftp[$server->cache];
}
}
return false;
}
/**
* get the JClientFtp object
*
* @param int $serverID The server local id to use
* @param string $permission The permission validation area
*
* @return object on success with ftp power
**/
public static function getFtp($serverID, $permission)
{
// check if we have a server with that id
if ($server = self::getServerDetails($serverID, 1, $permission))
{
// check if we already have the server instance
if (isset(self::$ftp[$server->cache]) && self::$ftp[$server->cache] instanceof JClientFtp)
{
// always set the name and remote server path
self::$ftp[$server->cache]->jcb_remote_server_name[$serverID] = $server->name;
// if still connected we are ready to go
if (self::$ftp[$server->cache]->isConnected())
{
// return the FTP instance
return self::$ftp[$server->cache];
}
// check if we can reinitialise the server
if (self::$ftp[$server->cache]->reinit())
{
// return the FTP instance
return self::$ftp[$server->cache];
}
}
// make sure we have a string and it is not default or empty
if (self::checkString($server->signature))
{
// turn into variables
parse_str($server->signature); // because of this I am using strange variable naming to avoid any collisions.
// set options
if (isset($options) && self::checkArray($options))
{
foreach ($options as $o__p0t1on => $vAln3)
{
if ('timeout' === $o__p0t1on)
{
$options[$o__p0t1on] = (int) $vAln3;
}
if ('type' === $o__p0t1on)
{
$options[$o__p0t1on] = (string) $vAln3;
}
}
}
else
{
$options = array();
}
// get ftp object
if (isset($host) && $host != 'HOSTNAME' && isset($port) && $port != 'PORT_INT' && isset($username) && $username != 'user@name.com' && isset($password) && $password != 'password')
{
// load for reuse
self::$ftp[$server->cache] = JClientFtp::getInstance($host, $port, $options, $username, $password);
}
else
{
// load error to indicate signature was in error
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_FTP_SIGNATURE_FOR_BSB_WAS_NOT_WELL_FORMED_PLEASE_CHECK_YOUR_SIGNATURE_DETAILS', $server->name), 'Error');
return false;
}
// check if we are connected
if (self::$ftp[$server->cache] instanceof JClientFtp && self::$ftp[$server->cache]->isConnected())
{
// heads-up on protocol
self::$ftp[$server->cache]->jcb_protocol = 1; // FTP <-- if called not knowing what type of protocol is being used
// set the unique buckets
if (!isset(self::$ftp[$server->cache]->jcb_remote_server_name))
{
self::$ftp[$server->cache]->jcb_remote_server_name = array();
}
// always set the name and remote server path
self::$ftp[$server->cache]->jcb_remote_server_name[$serverID] = $server->name;
// return the FTP instance
return self::$ftp[$server->cache];
}
// reset since we have no connection
unset(self::$ftp[$server->cache]);
}
// load error to indicate signature was in error
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_FTP_CONNECTION_FOR_BSB_COULD_NOT_BE_MADE_PLEASE_CHECK_YOUR_SIGNATURE_DETAILS', $server->name), 'Error');
}
return false;
}
/**
* get the server details
*
* @param int $serverID The server local id to use
* @param int $protocol The server protocol to use
* @param string $permission The permission validation area
*
* @return object on success with server details
**/
public static function getServerDetails($serverID, $protocol = 2, $permission = 'core.export')
{
// check if this user has permission to access items
if (!JFactory::getUser()->authorise($permission, 'com_componentbuilder'))
{
// set message to inform the user that permission was denied
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_ACCESS_THE_SERVER_DETAILS_BS_DENIEDB_PLEASE_CONTACT_YOUR_SYSTEM_ADMINISTRATOR_FOR_MORE_INFO', self::safeString($permission, 'w')), 'Error');
return false;
}
// now insure we have correct values
if (is_int($serverID) && is_int($protocol))
{
// Get a db connection
$db = JFactory::getDbo();
// start the query
$query = $db->getQuery(true);
// select based to protocol
if (2 == $protocol)
{
// SFTP
$query->select($db->quoteName(array('name','authentication','username','host','password','path','port','private','private_key','secret')));
// cache builder
$cache = array('authentication','username','host','password','port','private','private_key','secret');
}
else
{
// FTP
$query->select($db->quoteName(array('name','signature')));
// cache builder
$cache = array('signature');
}
$query->from($db->quoteName('#__componentbuilder_server'));
$query->where($db->quoteName('id') . ' = ' . (int) $serverID);
$query->where($db->quoteName('protocol') . ' = ' . (int) $protocol);
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
$server = $db->loadObject();
// Get the basic encryption.
$basickey = self::getCryptKey('basic', 'Th1sMnsTbL0ck@d');
// Get the encryption object.
$basic = new FOFEncryptAes($basickey, 128);
// start cache keys
$keys = array();
// unlock the needed fields
foreach($server as $name => &$value)
{
// unlock the needed fields
if ($name !== 'name' && !empty($value) && $basickey && !is_numeric($value) && $value === base64_encode(base64_decode($value, true)))
{
// basic decrypt of data
$value = rtrim($basic->decryptString($value), "\0");
}
// build cache (keys) for lower connection latency
if (in_array($name, $cache))
{
$keys[] = $value;
}
}
// check if cache keys were found
if (self::checkArray($keys))
{
// now set cache
$server->cache = md5(implode('', $keys));
}
else
{
// default is ID
$server->cache = $serverID;
}
// return the server details
return $server;
}
}
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_SERVER_DETAILS_FOR_BID_SB_COULD_NOT_BE_RETRIEVED', $serverID), 'Error');
return false;
}
/**
* the Crypt objects
**/
protected static $CRYPT = array();
/**
* get the Crypt object
*
* @return object on success with Crypt power
**/
public static function crypt($TYPE)
{
// check if it was already set
if (isset(self::$CRYPT[$TYPE]) && self::checkObject(self::$CRYPT[$TYPE]))
{
return self::$CRYPT[$TYPE];
}
// make sure we have the composer classes loaded
self::composerAutoload();
// build class name
$CLASS = '\phpseclib\Crypt\\'.$TYPE;
// make sure we have the phpseclib classes
if (!class_exists($CLASS))
{
// class not in place so send out error
JFactory::getApplication()->enqueueMessage(JText::sprintf('COM_COMPONENTBUILDER_THE_BSB_LIBRARYCLASS_IS_NOT_AVAILABLE_THIS_LIBRARYCLASS_SHOULD_HAVE_BEEN_ADDED_TO_YOUR_BLIBRARIESVDM_IOVENDORB_FOLDER_PLEASE_CONTACT_YOUR_SYSTEM_ADMINISTRATOR_FOR_MORE_INFO', $CLASS), 'Error');
return false;
}
// set the
self::$CRYPT[$TYPE] = new $CLASS();
// return the object
return self::$CRYPT[$TYPE];
}
public static function jsonToString($value, $sperator = ", ", $table = null)
{
// check if string is JSON
$result = json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE)
{
// is JSON
if (self::checkArray($result))
{
if (self::checkString($table))
{
$names = array();
foreach ($result as $val)
{
if ($name = self::getVar($table, $val, 'id', 'name'))
{
$names[] = $name;
}
}
if (self::checkArray($names))
{
return (string) implode($sperator,$names);
}
}
return (string) implode($sperator,$result);
}
return (string) json_decode($value);
}
return $value;
}
/**
* Load the Component xml manifest.
**/
public static function manifest()
{
$manifestUrl = JPATH_ADMINISTRATOR."/components/com_componentbuilder/componentbuilder.xml";
return simplexml_load_file($manifestUrl);
}
/**
* Joomla version object
**/
protected static $JVersion;
/**
* set/get Joomla version
**/
public static function jVersion()
{
// check if set
if (!self::checkObject(self::$JVersion))
{
self::$JVersion = new JVersion();
}
return self::$JVersion;
}
/**
* Load the Contributors details.
**/
public static function getContributors()
{
// get params
$params = JComponentHelper::getParams('com_componentbuilder');
// start contributors array
$contributors = array();
// get all Contributors (max 20)
$searchArray = range('0','20');
foreach($searchArray as $nr)
{
if ((NULL !== $params->get("showContributor".$nr)) && ($params->get("showContributor".$nr) == 2 || $params->get("showContributor".$nr) == 3))
{
// set link based of selected option
if($params->get("useContributor".$nr) == 1)
{
$link_front = '<a href="mailto:'.$params->get("emailContributor".$nr).'" target="_blank">';
$link_back = '</a>';
}
elseif($params->get("useContributor".$nr) == 2)
{
$link_front = '<a href="'.$params->get("linkContributor".$nr).'" target="_blank">';
$link_back = '</a>';
}
else
{
$link_front = '';
$link_back = '';
}
$contributors[$nr]['title'] = self::htmlEscape($params->get("titleContributor".$nr));
$contributors[$nr]['name'] = $link_front.self::htmlEscape($params->get("nameContributor".$nr)).$link_back;
}
}
return $contributors;
}
/**
* Load the Component Help URLs.
**/
public static function getHelpUrl($view)
{
$user = JFactory::getUser();
$groups = $user->get('groups');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('a.id','a.groups','a.target','a.type','a.article','a.url'));
$query->from('#__componentbuilder_help_document AS a');
$query->where('a.site_view = '.$db->quote($view));
$query->where('a.location = 2');
$query->where('a.published = 1');
$db->setQuery($query);
$db->execute();
if($db->getNumRows())
{
$helps = $db->loadObjectList();
if (self::checkArray($helps))
{
foreach ($helps as $nr => $help)
{
if ($help->target == 1)
{
$targetgroups = json_decode($help->groups, true);
if (!array_intersect($targetgroups, $groups))
{
// if user not in those target groups then remove the item
unset($helps[$nr]);
continue;
}
}
// set the return type
switch ($help->type)
{
// set joomla article
case 1:
return self::loadArticleLink($help->article);
break;
// set help text
case 2:
return self::loadHelpTextLink($help->id);
break;
// set Link
case 3:
return $help->url;
break;
}
}
}
}
return false;
}
/**
* Get the Article Link.
**/
protected static function loadArticleLink($id)
{
return JURI::root().'index.php?option=com_content&view=article&id='.$id.'&tmpl=component&layout=modal';
}
/**
* Get the Help Text Link.
**/
protected static function loadHelpTextLink($id)
{
$token = JSession::getFormToken();
return 'index.php?option=com_componentbuilder&task=help.getText&id=' . (int) $id . '&token=' . $token;
}
/**
* Get any component's model
**/
public static function getModel($name, $path = JPATH_COMPONENT_SITE, $component = 'Componentbuilder', $config = array())
{
// fix the name
$name = self::safeString($name);
// full path
$fullPath = $path . '/models';
// set prefix
$prefix = $component.'Model';
// load the model file
JModelLegacy::addIncludePath($fullPath, $prefix);
// get instance
$model = JModelLegacy::getInstance($name, $prefix, $config);
// if model not found (strange)
if ($model == false)
{
jimport('joomla.filesystem.file');
// get file path
$filePath = $path.'/'.$name.'.php';
$fullPath = $fullPath.'/'.$name.'.php';
// check if it exists
if (JFile::exists($filePath))
{
// get the file
require_once $filePath;
}
elseif (JFile::exists($fullPath))
{
// get the file
require_once $fullPath;
}
// build class names
$modelClass = $prefix.$name;
if (class_exists($modelClass))
{
// initialize the model
return new $modelClass($config);
}
}
return $model;
}
/**
* Add to asset Table
*/
public static function setAsset($id,$table)
{
$parent = JTable::getInstance('Asset');
$parent->loadByName('com_componentbuilder');
$parentId = $parent->id;
$name = 'com_componentbuilder.'.$table.'.'.$id;
$title = '';
$asset = JTable::getInstance('Asset');
$asset->loadByName($name);
// Check for an error.
$error = $asset->getError();
if ($error)
{
$this->setError($error);
return false;
}
else
{
// Specify how a new or moved node asset is inserted into the tree.
if ($asset->parent_id != $parentId)
{
$asset->setLocation($parentId, 'last-child');
}
// Prepare the asset to be stored.
$asset->parent_id = $parentId;
$asset->name = $name;
$asset->title = $title;
// get the default asset rules
$rules = self::getDefaultAssetRules('com_componentbuilder',$table);
if ($rules instanceof JAccessRules)
{
$asset->rules = (string) $rules;
}
if (!$asset->check() || !$asset->store())
{
JFactory::getApplication()->enqueueMessage($asset->getError(), 'warning');
return false;
}
else
{
// Create an asset_id or heal one that is corrupted.
$object = new stdClass();
// Must be a valid primary key value.
$object->id = $id;
$object->asset_id = (int) $asset->id;
// Update their asset_id to link to the asset table.
return JFactory::getDbo()->updateObject('#__componentbuilder_'.$table, $object, 'id');
}
}
return false;
}
/**
* Gets the default asset Rules for a component/view.
*/
protected static function getDefaultAssetRules($component,$view)
{
// Need to find the asset id by the name of the component.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('id'))
->from($db->quoteName('#__assets'))
->where($db->quoteName('name') . ' = ' . $db->quote($component));
$db->setQuery($query);
$db->execute();
if ($db->loadRowList())
{
// asset alread set so use saved rules
$assetId = (int) $db->loadResult();
$result = JAccess::getAssetRules($assetId);
if ($result instanceof JAccessRules)
{
$_result = (string) $result;
$_result = json_decode($_result);
foreach ($_result as $name => &$rule)
{
$v = explode('.', $name);
if ($view !== $v[0])
{
// remove since it is not part of this view
unset($_result->$name);
}
else
{
// clear the value since we inherit
$rule = array();
}
}
// check if there are any view values remaining
if (count($_result))
{
$_result = json_encode($_result);
$_result = array($_result);
// Instantiate and return the JAccessRules object for the asset rules.
$rules = new JAccessRules($_result);
return $rules;
}
return $result;
}
}
return JAccess::getAssetRules(0);
}
public static function renderBoolButton()
{
$args = func_get_args();
// get the radio element
$button = JFormHelper::loadFieldType('radio');
// setup the properties
$name = self::htmlEscape($args[0]);
$additional = isset($args[1]) ? (string) $args[1] : '';
$value = $args[2];
$yes = isset($args[3]) ? self::htmlEscape($args[3]) : 'JYES';
$no = isset($args[4]) ? self::htmlEscape($args[4]) : 'JNO';
// prepare the xml
$element = new SimpleXMLElement('<field name="'.$name.'" type="radio" class="btn-group"><option '.$additional.' value="0">'.$no.'</option><option '.$additional.' value="1">'.$yes.'</option></field>');
// run
$button->setup($element, $value);
return $button->input;
}
/**
* UIKIT Component Classes
**/
public static $uk_components = array(
'data-uk-grid' => array(
'grid' ),
'uk-accordion' => array(
'accordion' ),
'uk-autocomplete' => array(
'autocomplete' ),
'data-uk-datepicker' => array(
'datepicker' ),
'uk-form-password' => array(
'form-password' ),
'uk-form-select' => array(
'form-select' ),
'data-uk-htmleditor' => array(
'htmleditor' ),
'data-uk-lightbox' => array(
'lightbox' ),
'uk-nestable' => array(
'nestable' ),
'UIkit.notify' => array(
'notify' ),
'data-uk-parallax' => array(
'parallax' ),
'uk-search' => array(
'search' ),
'uk-slider' => array(
'slider' ),
'uk-slideset' => array(
'slideset' ),
'uk-slideshow' => array(
'slideshow',
'slideshow-fx' ),
'uk-sortable' => array(
'sortable' ),
'data-uk-sticky' => array(
'sticky' ),
'data-uk-timepicker' => array(
'timepicker' ),
'data-uk-tooltip' => array(
'tooltip' ),
'uk-placeholder' => array(
'placeholder' ),
'uk-dotnav' => array(
'dotnav' ),
'uk-slidenav' => array(
'slidenav' ),
'uk-form' => array(
'form-advanced' ),
'uk-progress' => array(
'progress' ),
'upload-drop' => array(
'upload', 'form-file' )
);
/**
* Add UIKIT Components
**/
public static $uikit = false;
/**
* Get UIKIT Components
**/
public static function getUikitComp($content,$classes = array())
{
if (strpos($content,'class="uk-') !== false)
{
// reset
$temp = array();
foreach (self::$uk_components as $looking => $add)
{
if (strpos($content,$looking) !== false)
{
$temp[] = $looking;
}
}
// make sure uikit is loaded to config
if (strpos($content,'class="uk-') !== false)
{
self::$uikit = true;
}
// sorter
if (self::checkArray($temp))
{
// merger
if (self::checkArray($classes))
{
$newTemp = array_merge($temp,$classes);
$temp = array_unique($newTemp);
}
return $temp;
}
}
if (self::checkArray($classes))
{
return $classes;
}
return false;
}
public static function getVar($table, $where = null, $whereString = 'user', $what = 'id', $operator = '=', $main = 'componentbuilder')
{
if(!$where)
{
$where = JFactory::getUser()->id;
}
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array($what)));
if (empty($table))
{
$query->from($db->quoteName('#__'.$main));
}
else
{
$query->from($db->quoteName('#__'.$main.'_'.$table));
}
if (is_numeric($where))
{
$query->where($db->quoteName($whereString) . ' '.$operator.' '.(int) $where);
}
elseif (is_string($where))
{
$query->where($db->quoteName($whereString) . ' '.$operator.' '. $db->quote((string)$where));
}
else
{
return false;
}
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
return $db->loadResult();
}
return false;
}
public static function getVars($table, $where = null, $whereString = 'user', $what = 'id', $operator = 'IN', $main = 'componentbuilder', $unique = true)
{
if(!$where)
{
$where = JFactory::getUser()->id;
}
if (!self::checkArray($where) && $where > 0)
{
$where = array($where);
}
if (self::checkArray($where))
{
// prep main <-- why? well if $main='' is empty then $table can be categories or users
if (self::checkString($main))
{
$main = '_'.ltrim($main, '_');
}
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array($what)));
if (empty($table))
{
$query->from($db->quoteName('#__'.$main));
}
else
{
$query->from($db->quoteName('#_'.$main.'_'.$table));
}
$query->where($db->quoteName($whereString) . ' '.$operator.' (' . implode(',',$where) . ')');
$db->setQuery($query);
$db->execute();
if ($db->getNumRows())
{
if ($unique)
{
return array_unique($db->loadColumn());
}
return $db->loadColumn();
}
}
return false;
}
public static function isPublished($id,$type)
{
if ($type == 'raw')
{
$type = 'item';
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('a.published'));
$query->from('#__componentbuilder_'.$type.' AS a');
$query->where('a.id = '. (int) $id);
$query->where('a.published = 1');
$db->setQuery($query);
$db->execute();
$found = $db->getNumRows();
if($found)
{
return true;
}
return false;
}
public static function getGroupName($id)
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select(array('a.title'));
$query->from('#__usergroups AS a');
$query->where('a.id = '. (int) $id);
$db->setQuery($query);
$db->execute();
$found = $db->getNumRows();
if($found)
{
return $db->loadResult();
}
return $id;
}
/**
* Get the actions permissions
**/
public static function getActions($view,&$record = null,$views = null)
{
jimport('joomla.access.access');
$user = JFactory::getUser();
$result = new JObject;
$view = self::safeString($view);
if (self::checkString($views))
{
$views = self::safeString($views);
}
// get all actions from component
$actions = JAccess::getActions('com_componentbuilder', 'component');
// set acctions only set in component settiongs
$componentActions = array('core.admin','core.manage','core.options','core.export');
// loop the actions and set the permissions
foreach ($actions as $action)
{
// set to use component default
$fallback = true;
if (self::checkObject($record) && isset($record->id) && $record->id > 0 && !in_array($action->name,$componentActions))
{
// The record has been set. Check the record permissions.
$permission = $user->authorise($action->name, 'com_componentbuilder.'.$view.'.' . (int) $record->id);
if (!$permission) // TODO removed && !is_null($permission)
{
if ($action->name == 'core.edit' || $action->name == $view.'.edit')
{
if ($user->authorise('core.edit.own', 'com_componentbuilder.'.$view.'.' . (int) $record->id))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
elseif ($user->authorise($view.'edit.own', 'com_componentbuilder.'.$view.'.' . (int) $record->id))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
elseif ($user->authorise('core.edit.own', 'com_componentbuilder'))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
elseif ($user->authorise($view.'edit.own', 'com_componentbuilder'))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
}
}
elseif (self::checkString($views) && isset($record->catid) && $record->catid > 0)
{
// make sure we use the core. action check for the categories
if (strpos($action->name,$view) !== false && strpos($action->name,'core.') === false ) {
$coreCheck = explode('.',$action->name);
$coreCheck[0] = 'core';
$categoryCheck = implode('.',$coreCheck);
}
else
{
$categoryCheck = $action->name;
}
// The record has a category. Check the category permissions.
$catpermission = $user->authorise($categoryCheck, 'com_componentbuilder.'.$views.'.category.' . (int) $record->catid);
if (!$catpermission && !is_null($catpermission))
{
if ($action->name == 'core.edit' || $action->name == $view.'.edit')
{
if ($user->authorise('core.edit.own', 'com_componentbuilder.'.$views.'.category.' . (int) $record->catid))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
elseif ($user->authorise($view.'edit.own', 'com_componentbuilder.'.$views.'.category.' . (int) $record->catid))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
elseif ($user->authorise('core.edit.own', 'com_componentbuilder'))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
elseif ($user->authorise($view.'edit.own', 'com_componentbuilder'))
{
// If the owner matches 'me' then allow.
if (isset($record->created_by) && $record->created_by > 0 && ($record->created_by == $user->id))
{
$result->set($action->name, true);
// set not to use component default
$fallback = false;
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback = false;
}
}
}
}
}
}
// if allowed then fallback on component global settings
if ($fallback)
{
$result->set($action->name, $user->authorise($action->name, 'com_componentbuilder'));
}
}
return $result;
}
/**
* Check if have an json string
*
* @input string The json string to check
*
* @returns bool true on success
**/
public static function checkJson($string)
{
if (self::checkString($string))
{
json_decode($string);
return (json_last_error() === JSON_ERROR_NONE);
}
return false;
}
/**
* Check if have an object with a length
*
* @input object The object to check
*
* @returns bool true on success
**/
public static function checkObject($object)
{
if (isset($object) && is_object($object))
{
return count((array)$object) > 0;
}
return false;
}
/**
* Check if have an array with a length
*
* @input array The array to check
*
* @returns bool true on success
**/
public static function checkArray($array, $removeEmptyString = false)
{
if (isset($array) && is_array($array) && count($array) > 0)
{
// also make sure the empty strings are removed
if ($removeEmptyString)
{
foreach ($array as $key => $string)
{
if (empty($string))
{
unset($array[$key]);
}
}
return self::checkArray($array, false);
}
return true;
}
return false;
}
/**
* Check if have a string with a length
*
* @input string The string to check
*
* @returns bool true on success
**/
public static function checkString($string)
{
if (isset($string) && is_string($string) && strlen($string) > 0)
{
return true;
}
return false;
}
/**
* Check if we are connected
* Thanks https://stackoverflow.com/a/4860432/1429677
*
* @returns bool true on success
**/
public static function isConnected()
{
// If example.com is down, then probably the whole internet is down, since IANA maintains the domain. Right?
$connected = @fsockopen("www.example.com", 80);
// website, port (try 80 or 443)
if ($connected)
{
//action when connected
$is_conn = true;
fclose($connected);
}
else
{
//action in connection failure
$is_conn = false;
}
return $is_conn;
}
/**
* Merge an array of array's
*
* @input array The arrays you would like to merge
*
* @returns array on success
**/
public static function mergeArrays($arrays)
{
if(self::checkArray($arrays))
{
$arrayBuket = array();
foreach ($arrays as $array)
{
if (self::checkArray($array))
{
$arrayBuket = array_merge($arrayBuket, $array);
}
}
return $arrayBuket;
}
return false;
}
// typo sorry!
public static function sorten($string, $length = 40, $addTip = true)
{
return self::shorten($string, $length, $addTip);
}
/**
* Shorten a string
*
* @input string The you would like to shorten
*
* @returns string on success
**/
public static function shorten($string, $length = 40, $addTip = true)
{
if (self::checkString($string))
{
$initial = strlen($string);
$words = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
$words_count = count($words);
$word_length = 0;
$last_word = 0;
for (; $last_word < $words_count; ++$last_word)
{
$word_length += strlen($words[$last_word]);
if ($word_length > $length)
{
break;
}
}
$newString = implode(array_slice($words, 0, $last_word));
$final = strlen($newString);
if ($initial != $final && $addTip)
{
$title = self::shorten($string, 400 , false);
return '<span class="hasTip" title="'.$title.'" style="cursor:help">'.trim($newString).'...</span>';
}
elseif ($initial != $final && !$addTip)
{
return trim($newString).'...';
}
}
return $string;
}
/**
* Making strings safe (various ways)
*
* @input string The you would like to make safe
*
* @returns string on success
**/
public static function safeString($string, $type = 'L', $spacer = '_', $replaceNumbers = true)
{
if ($replaceNumbers === true)
{
// remove all numbers and replace with english text version (works well only up to millions)
$string = self::replaceNumbers($string);
}
// 0nly continue if we have a string
if (self::checkString($string))
{
// create file name without the extention that is safe
if ($type === 'filename')
{
// make sure VDM is not in the string
$string = str_replace('VDM', 'vDm', $string);
// Remove anything which isn't a word, whitespace, number
// or any of the following caracters -_()
// If you don't need to handle multi-byte characters
// you can use preg_replace rather than mb_ereg_replace
// Thanks @Łukasz Rysiak!
// $string = mb_ereg_replace("([^\w\s\d\-_\(\)])", '', $string);
$string = preg_replace("([^\w\s\d\-_\(\)])", '', $string);
// http://stackoverflow.com/a/2021729/1429677
return preg_replace('/\s+/', ' ', $string);
}
// remove all other characters
$string = trim($string);
$string = preg_replace('/'.$spacer.'+/', ' ', $string);
$string = preg_replace('/\s+/', ' ', $string);
$string = preg_replace("/[^A-Za-z ]/", '', $string);
// select final adaptations
if ($type === 'L' || $type === 'strtolower')
{
// replace white space with underscore
$string = preg_replace('/\s+/', $spacer, $string);
// default is to return lower
return strtolower($string);
}
elseif ($type === 'W')
{
// return a string with all first letter of each word uppercase(no undersocre)
return ucwords(strtolower($string));
}
elseif ($type === 'w' || $type === 'word')
{
// return a string with all lowercase(no undersocre)
return strtolower($string);
}
elseif ($type === 'Ww' || $type === 'Word')
{
// return a string with first letter of the first word uppercase and all the rest lowercase(no undersocre)
return ucfirst(strtolower($string));
}
elseif ($type === 'WW' || $type === 'WORD')
{
// return a string with all the uppercase(no undersocre)
return strtoupper($string);
}
elseif ($type === 'U' || $type === 'strtoupper')
{
// replace white space with underscore
$string = preg_replace('/\s+/', $spacer, $string);
// return all upper
return strtoupper($string);
}
elseif ($type === 'F' || $type === 'ucfirst')
{
// replace white space with underscore
$string = preg_replace('/\s+/', $spacer, $string);
// return with first caracter to upper
return ucfirst(strtolower($string));
}
elseif ($type === 'cA' || $type === 'cAmel' || $type === 'camelcase')
{
// convert all words to first letter uppercase
$string = ucwords(strtolower($string));
// remove white space
$string = preg_replace('/\s+/', '', $string);
// now return first letter lowercase
return lcfirst($string);
}
// return string
return $string;
}
// not a string
return '';
}
public static function htmlEscape($var, $charset = 'UTF-8', $shorten = false, $length = 40)
{
if (self::checkString($var))
{
$filter = new JFilterInput();
$string = $filter->clean(html_entity_decode(htmlentities($var, ENT_COMPAT, $charset)), 'HTML');
if ($shorten)
{
return self::shorten($string,$length);
}
return $string;
}
else
{
return '';
}
}
public static function replaceNumbers($string)
{
// set numbers array
$numbers = array();
// first get all numbers
preg_match_all('!\d+!', $string, $numbers);
// check if we have any numbers
if (isset($numbers[0]) && self::checkArray($numbers[0]))
{
foreach ($numbers[0] as $number)
{
$searchReplace[$number] = self::numberToString((int)$number);
}
// now replace numbers in string
$string = str_replace(array_keys($searchReplace), array_values($searchReplace),$string);
// check if we missed any, strange if we did.
return self::replaceNumbers($string);
}
// return the string with no numbers remaining.
return $string;
}
/**
* Convert an integer into an English word string
* Thanks to Tom Nicholson <http://php.net/manual/en/function.strval.php#41988>
*
* @input an int
* @returns a string
**/
public static function numberToString($x)
{
$nwords = array( "zero", "one", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
"nineteen", "twenty", 30 => "thirty", 40 => "forty",
50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
90 => "ninety" );
if(!is_numeric($x))
{
$w = $x;
}
elseif(fmod($x, 1) != 0)
{
$w = $x;
}
else
{
if($x < 0)
{
$w = 'minus ';
$x = -$x;
}
else
{
$w = '';
// ... now $x is a non-negative integer.
}
if($x < 21) // 0 to 20
{
$w .= $nwords[$x];
}
elseif($x < 100) // 21 to 99
{
$w .= $nwords[10 * floor($x/10)];
$r = fmod($x, 10);
if($r > 0)
{
$w .= ' '. $nwords[$r];
}
}
elseif($x < 1000) // 100 to 999
{
$w .= $nwords[floor($x/100)] .' hundred';
$r = fmod($x, 100);
if($r > 0)
{
$w .= ' and '. self::numberToString($r);
}
}
elseif($x < 1000000) // 1000 to 999999
{
$w .= self::numberToString(floor($x/1000)) .' thousand';
$r = fmod($x, 1000);
if($r > 0)
{
$w .= ' ';
if($r < 100)
{
$w .= 'and ';
}
$w .= self::numberToString($r);
}
}
else // millions
{
$w .= self::numberToString(floor($x/1000000)) .' million';
$r = fmod($x, 1000000);
if($r > 0)
{
$w .= ' ';
if($r < 100)
{
$w .= 'and ';
}
$w .= self::numberToString($r);
}
}
}
return $w;
}
/**
* Random Key
*
* @returns a string
**/
public static function randomkey($size)
{
$bag = "abcefghijknopqrstuwxyzABCDDEFGHIJKLLMMNOPQRSTUVVWXYZabcddefghijkllmmnopqrstuvvwxyzABCEFGHIJKNOPQRSTUWXYZ";
$key = array();
$bagsize = strlen($bag) - 1;
for ($i = 0; $i < $size; $i++)
{
$get = rand(0, $bagsize);
$key[] = $bag[$get];
}
return implode($key);
}
/**
* Get The Encryption Keys
*
* @param string $type The type of key
* @param string/bool $default The return value if no key was found
*
* @return string On success
*
**/
public static function getCryptKey($type, $default = false)
{
// Get the global params
$params = JComponentHelper::getParams('com_componentbuilder', true);
// Basic Encryption Type
if ('basic' === $type)
{
$basic_key = $params->get('basic_key', $default);
if (self::checkString($basic_key))
{
return $basic_key;
}
}
return $default;
}
}