Component-Builder/admin/compiler/joomla_3/Helper.php

1244 lines
34 KiB
PHP
Raw Normal View History

2016-01-30 20:28:43 +00:00
<?php
/**
* @package Joomla.Component.Builder
*
* @created 30th April, 2015
* @author Llewellyn van der Merwe <http://www.joomlacomponentbuilder.com>
* @github Joomla Component Builder <https://github.com/vdm-io/Joomla-Component-Builder>
* @copyright Copyright (C) 2015 - 2018 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
2016-01-30 20:28:43 +00:00
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
###BOM###
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* ###Component### component helper.
*/
abstract class ###Component###Helper
{###ADMIN_GLOBAL_EVENT_HELPER### ###CUSTOM_HELPER_SCRIPT### ###BOTH_CUSTOM_HELPER_SCRIPT###
2016-01-30 20:28:43 +00:00
/**
* Load the Component xml manifest.
**/
2017-12-15 01:10:10 +00:00
public static function manifest()
2016-01-30 20:28:43 +00:00
{
2017-12-15 01:10:10 +00:00
$manifestUrl = JPATH_ADMINISTRATOR."/components/com_###component###/###component###.xml";
return simplexml_load_file($manifestUrl);
2016-01-30 20:28:43 +00:00
}
/**
* 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;
}
2016-01-30 20:28:43 +00:00
/**
* Load the Contributors details.
**/
public static function getContributors()
{
// get params
$params = JComponentHelper::getParams('com_###component###');
// start contributors array
$contributors = array();
// get all Contributors (max 20)
$searchArray = range('0','20');
foreach($searchArray as $nr)
2017-12-15 01:10:10 +00:00
{
2016-01-30 20:28:43 +00:00
if ((NULL !== $params->get("showContributor".$nr)) && ($params->get("showContributor".$nr) == 1 || $params->get("showContributor".$nr) == 3))
2017-12-15 01:10:10 +00:00
{
2016-01-30 20:28:43 +00:00
// set link based of selected option
if($params->get("useContributor".$nr) == 1)
2017-12-15 01:10:10 +00:00
{
2016-01-30 20:28:43 +00:00
$link_front = '<a href="mailto:'.$params->get("emailContributor".$nr).'" target="_blank">';
$link_back = '</a>';
}
2017-12-15 01:10:10 +00:00
elseif($params->get("useContributor".$nr) == 2)
{
2016-01-30 20:28:43 +00:00
$link_front = '<a href="'.$params->get("linkContributor".$nr).'" target="_blank">';
$link_back = '</a>';
}
2017-12-15 01:10:10 +00:00
else
{
2016-01-30 20:28:43 +00:00
$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;
}###HELP###
/**
* Configure the Linkbar.
**/
public static function addSubmenu($submenu)
{
2017-12-15 01:10:10 +00:00
// load user for access menus
$user = JFactory::getUser();
// load the submenus to sidebar
###SUBMENU###
2016-01-30 20:28:43 +00:00
}###HELPER_CREATEUSER### ###HELPER_UIKIT### ###HELPER_EXEL###
/**
* Get a Variable
*
* @param string $table The table from which to get the variable
* @param string $where The value where
* @param string $whereString The target/field string where/name
* @param string $what The return field
* @param string $operator The operator between $whereString/field and $where/value
* @param string $main The component in which the table is found
*
* @return mix string/int/float
*
*/
2016-01-30 20:28:43 +00:00
public static function getVar($table, $where = null, $whereString = 'user', $what = 'id', $operator = '=', $main = '###component###')
{
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));
}
2016-01-30 20:28:43 +00:00
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;
}
/**
* Get array of variables
*
* @param string $table The table from which to get the variables
* @param string $where The value where
* @param string $whereString The target/field string where/name
* @param string $what The return field
* @param string $operator The operator between $whereString/field and $where/value
* @param string $main The component in which the table is found
* @param bool $unique The switch to return a unique array
*
* @return array
*
*/
2016-01-30 20:28:43 +00:00
public static function getVars($table, $where = null, $whereString = 'user', $what = 'id', $operator = 'IN', $main = '###component###', $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, '_');
}
2016-01-30 20:28:43 +00:00
// 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));
}
2016-01-30 20:28:43 +00:00
$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 jsonToString($value, $sperator = ", ", $table = null, $id = 'id', $name = 'name')
2016-01-30 20:28:43 +00:00
{
// do some table foot work
$external = false;
if (strpos($table, '#__') !== false)
{
$external = true;
$table = str_replace('#__', '', $table);
}
2017-12-15 01:10:10 +00:00
// check if string is JSON
$result = json_decode($value, true);
if (json_last_error() === JSON_ERROR_NONE)
2016-05-10 05:47:47 +00:00
{
// is JSON
2016-01-30 20:28:43 +00:00
if (self::checkArray($result))
{
2016-05-10 05:47:47 +00:00
if (self::checkString($table))
2016-01-30 20:28:43 +00:00
{
2016-05-10 05:47:47 +00:00
$names = array();
foreach ($result as $val)
2016-01-30 20:28:43 +00:00
{
if ($external)
{
if ($_name = self::getVar(null, $val, $id, $name, '=', $table))
{
$names[] = $_name;
}
}
else
2016-05-10 05:47:47 +00:00
{
if ($_name = self::getVar($table, $val, $id, $name))
{
$names[] = $_name;
}
2016-05-10 05:47:47 +00:00
}
2016-01-30 20:28:43 +00:00
}
2016-05-10 05:47:47 +00:00
if (self::checkArray($names))
2016-01-30 20:28:43 +00:00
{
2016-05-10 05:47:47 +00:00
return (string) implode($sperator,$names);
}
2016-01-30 20:28:43 +00:00
}
2016-05-10 05:47:47 +00:00
return (string) implode($sperator,$result);
2016-01-30 20:28:43 +00:00
}
2017-12-15 01:10:10 +00:00
return (string) json_decode($value);
}
return $value;
}
2016-01-30 20:28:43 +00:00
public static function isPublished($id,$type)
{
if ($type == 'raw')
2017-12-15 01:10:10 +00:00
{
2016-01-30 20:28:43 +00:00
$type = 'item';
}
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('a.published'));
$query->from('#__###component###_'.$type.' AS a');
$query->where('a.id = '. (int) $id);
2016-01-30 20:28:43 +00:00
$query->where('a.published = 1');
$db->setQuery($query);
$db->execute();
$found = $db->getNumRows();
if($found)
2017-12-15 01:10:10 +00:00
{
2016-01-30 20:28:43 +00:00
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);
2016-01-30 20:28:43 +00:00
$db->setQuery($query);
$db->execute();
$found = $db->getNumRows();
if($found)
2017-12-15 01:10:10 +00:00
{
2016-01-30 20:28:43 +00:00
return $db->loadResult();
}
return $id;
}
2017-12-15 01:10:10 +00:00
/**
2016-01-30 20:28:43 +00:00
* Get the actions permissions
**/
2017-12-15 01:10:10 +00:00
public static function getActions($view,&$record = null,$views = null)
2016-01-30 20:28:43 +00:00
{
jimport('joomla.access.access');
$user = JFactory::getUser();
$result = new JObject;
$view = self::safeString($view);
2017-12-15 01:10:10 +00:00
if (self::checkString($views))
{
2016-01-30 20:28:43 +00:00
$views = self::safeString($views);
2017-12-15 01:10:10 +00:00
}
2016-01-30 20:28:43 +00:00
// get all actions from component
$actions = JAccess::getActions('com_###component###', 'component');
2017-12-15 01:10:10 +00:00
// set acctions only set in component settiongs
$componentActions = array('core.admin','core.manage','core.options','core.export');
2016-01-30 20:28:43 +00:00
// loop the actions and set the permissions
foreach ($actions as $action)
2017-12-15 01:10:10 +00:00
{
2016-01-30 20:28:43 +00:00
// set to use component default
$fallback= true;
2016-01-30 20:28:43 +00:00
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_###component###.'.$view.'.' . (int) $record->id);
if (!$permission) // TODO removed && !is_null($permission)
2016-01-30 20:28:43 +00:00
{
if ($action->name == 'core.edit' || $action->name == $view.'.edit')
{
if ($user->authorise('core.edit.own', 'com_###component###.'.$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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
elseif ($user->authorise($view.'edit.own', 'com_###component###.'.$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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
elseif ($user->authorise('core.edit.own', 'com_###component###'))
{
// 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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
elseif ($user->authorise($view.'edit.own', 'com_###component###'))
{
// 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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
}
}
elseif (self::checkString($views) && isset($record->catid) && $record->catid > 0)
{
2017-12-15 01:10:10 +00:00
// 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.
2016-01-30 20:28:43 +00:00
$catpermission = $user->authorise($categoryCheck, 'com_###component###.'.$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_###component###.'.$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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
elseif ($user->authorise($view.'edit.own', 'com_###component###.'.$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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
elseif ($user->authorise('core.edit.own', 'com_###component###'))
{
// 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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
elseif ($user->authorise($view.'edit.own', 'com_###component###'))
{
// 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;
2016-01-30 20:28:43 +00:00
}
else
{
$result->set($action->name, false);
// set not to use component default
$fallback= false;
2016-01-30 20:28:43 +00:00
}
}
}
}
}
}
// if allowed then fallback on component global settings
if ($fallback)
2016-01-30 20:28:43 +00:00
{
$result->set($action->name, $user->authorise($action->name, 'com_###component###'));
}
}
return $result;
}
/**
* Get any component's model
**/
public static function getModel($name, $path = JPATH_COMPONENT_ADMINISTRATOR, $component = '###Component###', $config = array())
2016-01-30 20:28:43 +00:00
{
// fix the name
$name = self::safeString($name);
// full path
$fullPath = $path . '/models';
// set prefix
$prefix = $component.'Model';
2016-01-30 20:28:43 +00:00
// 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;
2016-01-30 20:28:43 +00:00
}
2016-01-30 20:28:43 +00:00
/**
* Add to asset Table
*/
public static function setAsset($id,$table)
{
$parent = JTable::getInstance('Asset');
$parent->loadByName('com_###component###');
$parentId = $parent->id;
$name = 'com_###component###.'.$table.'.'.$id;
$title = '';
$asset = JTable::getInstance('Asset');
$asset->loadByName($name);
// Check for an error.
$error = $asset->getError();
if ($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_###component###',$table);
if ($rules instanceof JAccessRules)
{
$asset->rules = (string) $rules;
}
if (!$asset->check() || !$asset->store())
{
JFactory::getApplication()->enqueueMessage($asset->getError(), 'warning');
2016-01-30 20:28:43 +00:00
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('#__###component###_'.$table, $object, 'id');
}
}
return false;
}
2016-01-30 20:28:43 +00:00
/**
* 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((array)$_result))
2016-01-30 20:28:43 +00:00
{
$_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);
}
/**
* xmlAppend
*
* @param SimpleXMLElement $xml The XML element reference in which to inject a comment
* @param mixed $node A SimpleXMLElement node to append to the XML element reference, or a stdClass object containing a comment attribute to be injected before the XML node and a fieldXML attribute containing a SimpleXMLElement
*
* @return null
*
*/
public static function xmlAppend(&$xml, $node)
{
if (!$node)
{
// element was not returned
return;
}
switch (get_class($node))
{
case 'stdClass':
if (property_exists($node, 'comment'))
{
self::xmlComment($xml, $node->comment);
}
if (property_exists($node, 'fieldXML'))
{
self::xmlAppend($xml, $node->fieldXML);
}
break;
case 'SimpleXMLElement':
$domXML = dom_import_simplexml($xml);
$domNode = dom_import_simplexml($node);
$domXML->appendChild($domXML->ownerDocument->importNode($domNode, true));
$xml = simplexml_import_dom($domXML);
break;
}
}
/**
* xmlComment
*
* @param SimpleXMLElement $xml The XML element reference in which to inject a comment
* @param string $comment The comment to inject
*
* @return null
*
*/
public static function xmlComment(&$xml, $comment)
{
$domXML = dom_import_simplexml($xml);
$domComment = new DOMComment($comment);
$nodeTarget = $domXML->ownerDocument->importNode($domComment, true);
$domXML->appendChild($nodeTarget);
$xml = simplexml_import_dom($domXML);
}
/**
* xmlAddAttributes
*
* @param SimpleXMLElement $xml The XML element reference in which to inject a comment
* @param array $attributes The attributes to apply to the XML element
*
* @return null
*
*/
public static function xmlAddAttributes(&$xml, $attributes = array())
{
foreach ($attributes as $key => $value)
{
$xml->addAttribute($key, $value);
}
}
/**
* xmlAddOptions
*
* @param SimpleXMLElement $xml The XML element reference in which to inject a comment
* @param array $options The options to apply to the XML element
*
* @return void
*
*/
public static function xmlAddOptions(&$xml, $options = array())
{
foreach ($options as $key => $value)
{
$addOption = $xml->addChild('option');
$addOption->addAttribute('value', $key);
$addOption[] = $value;
}
}
/**
* get the field object
*
* @param array $attributes The array of attributes
* @param string $default The default of the field
* @param array $options The options to apply to the XML element
*
* @return object
*
*/
public static function getFieldObject(&$attributes, $default = '', $options = null)
{
// make sure we have attributes and a type value
if (self::checkArray($attributes) && isset($attributes['type']))
{
// make sure the form helper class is loaded
if (!method_exists('JFormHelper', 'loadFieldType'))
{
jimport('joomla.form.form');
}
// get field type
$field = JFormHelper::loadFieldType($attributes['type'],true);
// start field xml
$XML = new SimpleXMLElement('<field/>');
// load the attributes
self::xmlAddAttributes($XML, $attributes);
// check if we have options
if (self::checkArray($options))
{
// load the options
self::xmlAddOptions($XML, $options);
}
// setup the field
$field->setup($XML, $default);
// return the field object
return $field;
}
return false;
}
/**
* Render Bool Button
*
* @param array $args All the args for the button
* 0) name
* 1) additional (options class) // not used at this time
* 2) default
* 3) yes (name)
* 4) no (name)
*
* @return string The input html of the button
*
*/
2016-01-30 20:28:43 +00:00
public static function renderBoolButton()
{
$args = func_get_args();
// check if there is additional button class
$additional = isset($args[1]) ? (string) $args[1] : ''; // not used at this time
// button attributes
$buttonAttributes = array(
'type' => 'radio',
'name' => isset($args[0]) ? self::htmlEscape($args[0]) : 'bool_button',
'label' => isset($args[0]) ? self::safeString(self::htmlEscape($args[0]), 'Ww') : 'Bool Button', // not seen anyway
'class' => 'btn-group',
'filter' => 'INT',
'default' => isset($args[2]) ? (int) $args[2] : 0);
// set the button options
$buttonOptions = array(
'1' => isset($args[3]) ? self::htmlEscape($args[3]) : 'JYES',
'0' => isset($args[4]) ? self::htmlEscape($args[4]) : 'JNO');
// return the input
return self::getFieldObject($buttonAttributes, $buttonAttributes['default'], $buttonOptions)->input;
2016-01-30 20:28:43 +00:00
}
/**
* 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;
}
2016-01-30 20:28:43 +00:00
/**
* Check if have an object with a length
*
* @input object The object to check
*
* @returns bool true on success
**/
2016-01-30 20:28:43 +00:00
public static function checkObject($object)
{
if (isset($object) && is_object($object))
2016-01-30 20:28:43 +00:00
{
return count((array)$object) > 0;
2016-01-30 20:28:43 +00:00
}
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)
2016-01-30 20:28:43 +00:00
{
if (isset($array) && is_array($array) && count((array)$array) > 0)
2016-01-30 20:28:43 +00:00
{
// 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);
}
2016-01-30 20:28:43 +00:00
return true;
}
return false;
}
/**
* Check if have a string with a length
*
* @input string The string to check
*
* @returns bool true on success
**/
2016-01-30 20:28:43 +00:00
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;
}
2016-01-30 20:28:43 +00:00
/**
* Merge an array of array's
*
* @input array The arrays you would like to merge
*
* @returns array on success
**/
2016-01-30 20:28:43 +00:00
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!
2016-01-30 20:28:43 +00:00
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)
2016-01-30 20:28:43 +00:00
{
if (self::checkString($string))
{
2016-01-30 20:28:43 +00:00
$initial = strlen($string);
$words = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
$words_count = count((array)$words);
2016-01-30 20:28:43 +00:00
$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);
2016-01-30 20:28:43 +00:00
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, $keepOnlyCharacters = true)
2016-01-30 20:28:43 +00:00
{
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
2017-12-15 01:10:10 +00:00
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);
// remove all and keep only characters
if ($keepOnlyCharacters)
{
$string = preg_replace("/[^A-Za-z ]/", '', $string);
}
// keep both numbers and characters
else
{
$string = preg_replace("/[^A-Za-z0-9 ]/", '', $string);
}
// select final adaptations
if ($type === 'L' || $type === 'strtolower')
2017-12-15 01:10:10 +00:00
{
// 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);
}
2017-12-15 01:10:10 +00:00
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);
}
2017-12-15 01:10:10 +00:00
// return string
return $string;
}
// not a string
return '';
2016-01-30 20:28:43 +00:00
}
2017-12-15 01:10:10 +00:00
public static function htmlEscape($var, $charset = 'UTF-8', $shorten = false, $length = 40)
2016-01-30 20:28:43 +00:00
{
if (self::checkString($var))
{
$filter = new JFilterInput();
$string = $filter->clean(html_entity_decode(htmlentities($var, ENT_COMPAT, $charset)), 'HTML');
if ($shorten)
2016-01-30 20:28:43 +00:00
{
return self::shorten($string,$length);
2016-01-30 20:28:43 +00:00
}
return $string;
2017-12-15 01:10:10 +00:00
}
2016-01-30 20:28:43 +00:00
else
{
return '';
2017-12-15 01:10:10 +00:00
}
2016-01-30 20:28:43 +00:00
}###HELPER_LICENSE_LOCK###
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;
}
2016-01-30 20:28:43 +00:00
/**
* 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 ';
2016-01-30 20:28:43 +00:00
}
$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_CRYPT_KEY###
}