Members-Manager/admin/helpers/membersmanager.php

2171 lines
59 KiB
PHP

<?php
/**
* @package Joomla.Members.Manager
*
* @created 6th September, 2015
* @author Llewellyn van der Merwe <https://www.joomlacomponentbuilder.com/>
* @github Joomla Members Manager <https://github.com/vdm-io/Joomla-Members-Manager>
* @copyright Copyright (C) 2015. All Rights Reserved
* @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
/**
* Membersmanager component helper.
*/
abstract class MembersmanagerHelper
{
/**
* The Global Admin Event Method.
**/
public static function globalEvent($document)
{
// the Session keeps track of all data related to the current session of this user
self::loadSession();
}
/**
* the params
**/
protected static $params;
/**
* 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];
}
/**
* 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_membersmanager');
}
$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, '/' ) . '/';
}
/**
* @param $fileName
* @param $fileFormat
* @param $target
* @param $path
* @param $fullPath
* @return bool
*/
public static function resizeImage($fileName, $fileFormat, $target, $path, $fullPath)
{
// get the global settings
if (!self::checkObject(self::$params))
{
self::$params = JComponentHelper::getParams('com_membersmanager');
}
// first check if we should resize this target
if (1 == self::$params->get('crop_'.$target, 0))
{
// load the size to be set
$height = self::$params->get($target.'_height', 'not_set');
$width = self::$params->get($target.'_width', 'not_set');
// get image properties
$image = self::getImageFileProperties($fileName.'.'.$fileFormat, $path);
// make sure we have an object
if (self::checkObject($image))
{
if ($width !== 'not_set' && $height !== 'not_set' && ($image->width != $width || $image->height != $height))
{
// if image is huge and should only be scaled, resize it on the fly
if(($image->width > 900 || $image->height > 700) && ($height == 0 || $width == 0))
{
if($fileFormat == "jpg" || $fileFormat == "jpeg" )
{
$src = imagecreatefromjpeg($fullPath);
}
elseif($fileFormat == "png")
{
$src = imagecreatefrompng($fullPath);
}
elseif($fileFormat == "gif")
{
$src = imagecreatefromgif($fullPath);
}
else
{
return false;
}
if ($height != 0)
{
$hRatio = $image->height / $height;
}
if ($width != 0)
{
$wRatio = $image->width / $width;
}
if (isset($hRatio) && isset($wRatio))
{
$maxRatio = max($wRatio, $hRatio);
}
elseif (isset($wRatio))
{
$maxRatio = $wRatio;
}
elseif (isset($hRatio))
{
$maxRatio = $hRatio;
}
if ($maxRatio > 1)
{
$newwidth = $image->width / $maxRatio;
$newheight = $image->height / $maxRatio;
}
else
{
$newwidth = $image->width;
$newheight = $image->height;
}
$tmp = imagecreatetruecolor($newwidth, $newheight);
$backgroundColor = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $backgroundColor);
imagecopyresampled($tmp, $src, 0, 0, 0, 0,$newwidth, $newheight, $image->width, $image->height);
imagejpeg($tmp, $fullPath, 100);
imagedestroy($src);
imagedestroy($tmp);
}
// only continue if image should be cropped
if ($height != 0 && $width != 0)
{
// Include wideimage - http://wideimage.sourceforge.net
require_once(JPATH_ADMINISTRATOR . '/components/com_membersmanager/helpers/wideimage/WideImage.php');
$builder = WideImage::load($fullPath);
$resized = $builder->resize($width, $height, 'outside')->crop('center', 'middle', $width, $height);
$resized->saveToFile($fullPath);
}
}
return true;
}
}
return false;
}
/**
* @param $image
* @return bool|stdClass
*/
public static function getImageFileProperties($image, $folder = false)
{
if ($folder)
{
$localfolder = $folder;
}
else
{
$setimagesfolder = JComponentHelper::getParams('com_membersmanager')->get('setimagesfolder', 1);
if (2 == $setimagesfolder)
{
$localfolder = JComponentHelper::getParams('com_membersmanager')->get('imagesfolder', JPATH_SITE.'/images/membersmanager');
}
elseif (1 == $setimagesfolder)
{
$localfolder = JPATH_SITE.'/images';
}
else // just in-case :)
{
$localfolder = JPATH_SITE.'/images/membersmanager';
}
}
// import all needed classes
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
jimport('joomla.image.image');
// setup the folder if it does not exist
if (JFolder::exists($localfolder) && JFile::exists($localfolder.'/'.$image))
{
$properties = JImage::getImageFileProperties($localfolder.'/'.$image);
// check if we have properties
if (self::checkObject($properties))
{
// remove the server path
$imagePath = trim(str_replace(JPATH_SITE,'',$localfolder),'/').'/'.$image;
// now add the src path to show the image
$properties->src = JURI::root().$imagePath;
// return the image properties
return $properties;
}
}
return false;
}
/**
* @return array of link options
*/
public static function getLinkOptions($lock = 0, $session = 0)
{
// get the global settings
if (!self::checkObject(self::$params))
{
self::$params = JComponentHelper::getParams('com_membersmanager');
}
$linkoptions = self::$params->get('link_option', null);
// set the options to array
$options = array('lock' => $lock, 'session' => $session);
if (MembersmanagerHelper::checkArray($linkoptions))
{
if (in_array(1, $linkoptions))
{
// lock the filename
$options['lock'] = 1;
}
if (in_array(2, $linkoptions))
{
// add session to the links
$options['session'] = 1;
}
}
return $options;
}
/**
* Get the html/link of the image
*
* @param object $item The item to get image for
* @param string $target The target in the item to use
* @param string $name The name target in item to use
* @param string $filelink The file link
*
* @return string image html/link
*
*/
public static function getImageLink(&$item, $target, $name = 'name', $filelink = null, $html = true)
{
// check that we have a value
if (isset($item->{$target}) && MembersmanagerHelper::checkString($item->{$target}))
{
// load the file link path if not set
if (!$filelink)
{
$filelink = self::getFolderPath('url');
}
// set image link
if (strpos($item->{$target}, '_') !== false)
{
$extention = explode('_', $item->{$target});
$actualName = self::safeString($target, 'w');
if (strpos($item->{$target}, 'VDM') !== false)
{
$fileNameArray = explode('VDM', $item->{$target});
if (isset($fileNameArray[1]) && MembersmanagerHelper::checkString($fileNameArray[1]))
{
$actualName = $fileNameArray[1];
}
}
// check if we have the extention
if (isset($extention[2]))
{
// set the link
$link = $filelink . $item->{$target} . '.' . $extention[2];
// return ready html
if ($html)
{
return '<img src="' . $link . '" alt="' . $actualName . ' ' . $item->{$name} . '" data-uk-tooltip title="' . $item->{$name} . '"/>';
}
// return just the link
else
{
return $link;
}
}
}
}
return false;
}
/**
* Get the edit button
*
* @param int $item The item to edit
* @param string $view The type of item to edit
* @param string $views The list view controller name
* @param string $ref The return path
* @param string $headsup The message to show on click of button
*
* @return string On success the full html edit button
*
*/
public static function getEditButton(&$item, $view, $views, $ref = '', $headsup = 'COM_MEMBERSMANAGER_ALL_UNSAVED_WORK_ON_THIS_PAGE_WILL_BE_LOST_ARE_YOU_SURE_YOU_WANT_TO_CONTINUE')
{
// check that we have the ID
if (self::checkObject($item) && isset($item->id))
{
$id = (int) $item->id;
// check if the checked_out is available
if (isset($item->checked_out))
{
$checked_out = (int) $item->checked_out;
}
}
elseif (self::checkArray($item) && isset($item['id']))
{
$id = (int) $item['id'];
// check if the checked_out is available
if (isset($item['checked_out']))
{
$checked_out = (int) $item['checked_out'];
}
}
elseif (is_numeric($item))
{
$id = (int) $item;
}
// check ID
if (isset($id) && $id > 0)
{
// can edit
if (JFactory::getUser()->authorise($view.'.edit', 'com_membersmanager.'.$view.'.' . (int) $id))
{
// set the edit link
$edit = "index.php?option=com_membersmanager&view=".$views."&task=".$view.".edit&id=".$id.$ref;
// set the link title
$title = self::safeString(JText::_('COM_MEMBERSMANAGER_EDIT').' '.$view, 'W');
// check that there is a check message
if (self::checkString($headsup))
{
$href = 'onclick="UIkit.modal.confirm(\''.JText::_($headsup).'\', function(){ window.location.href = \'' . $edit . '\' })" href="javascript:void(0)"';
}
else
{
$href = 'href="' . $edit . '"';
}
// check if it is checked out
if (isset($checked_out) && $checked_out > 0)
{
// is this user the one who checked it out
if ($checked_out == JFactory::getUser()->id)
{
return ' <a ' . $href . ' class="uk-icon-lock" title="' . $title . '"></a>';
}
return ' <a href="#" disabled class="uk-icon-lock" title="' . JText::sprintf('COM_MEMBERSMANAGER__HAS_BEEN_CHECKED_OUT_BY_S', self::safeString($view, 'W'), JFactory::getUser($checked_out)->name) . '"></a>';
}
// return normal edit link
return ' <a ' . $href . ' class="uk-icon-pencil" title="' . $title . '"></a>';
}
}
return '';
}
/**
* Load the Component xml manifest.
**/
public static function manifest()
{
$manifestUrl = JPATH_ADMINISTRATOR."/components/com_membersmanager/membersmanager.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_membersmanager');
// 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) == 1 || $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;
}
/**
* Can be used to build help urls.
**/
public static function getHelpUrl($view)
{
return false;
}
/**
* Configure the Linkbar.
**/
public static function addSubmenu($submenu)
{
// load user for access menus
$user = JFactory::getUser();
// load the submenus to sidebar
if ($user->authorise('member.access', 'com_membersmanager') && $user->authorise('member.submenu', 'com_membersmanager'))
{
JHtmlSidebar::addEntry(JText::_('COM_MEMBERSMANAGER_SUBMENU_MEMBERS'), 'index.php?option=com_membersmanager&view=members', $submenu === 'members');
}
if ($user->authorise('country.access', 'com_membersmanager') && $user->authorise('country.submenu', 'com_membersmanager'))
{
JHtmlSidebar::addEntry(JText::_('COM_MEMBERSMANAGER_SUBMENU_COUNTRIES'), 'index.php?option=com_membersmanager&view=countries', $submenu === 'countries');
}
if ($user->authorise('currency.access', 'com_membersmanager') && $user->authorise('currency.submenu', 'com_membersmanager'))
{
JHtmlSidebar::addEntry(JText::_('COM_MEMBERSMANAGER_SUBMENU_CURRENCIES'), 'index.php?option=com_membersmanager&view=currencies', $submenu === 'currencies');
}
}
/**
* Greate user and update given table
*/
public static function createUser($new)
{
// load the user component language files if there is an error.
$lang = JFactory::getLanguage();
$extension = 'com_users';
$base_dir = JPATH_SITE;
$language_tag = 'en-GB';
$reload = true;
$lang->load($extension, $base_dir, $language_tag, $reload);
// load the user regestration model
$model = self::getModel('registration', JPATH_ROOT. '/components/com_users', 'Users');
// make sure no activation is needed
$useractivation = self::setParams('com_users','useractivation',0);
// make sure password is send
$sendpassword = self::setParams('com_users','sendpassword',1);
// Check if password was set
if (isset($new['password']) && isset($new['password2']) && self::checkString($new['password']) && self::checkString($new['password2']))
{
// Use the users passwords
$password = $new['password'];
$password2 = $new['password2'];
}
else
{
// Set random password
$password = self::randomkey(8);
$password2 = $password;
}
// set username if not set
if (!isset($new['username']) || !self::checkString($new['username']))
{
$new['username'] = self::safeString($new['name']);
}
// linup new user data
$data = array(
'username' => $new['username'],
'name' => $new['name'],
'email1' => $new['email'],
'password1' => $password, // First password field
'password2' => $password2, // Confirm password field
'block' => 0 );
// register the new user
$userId = $model->register($data);
// set activation back to default
self::setParams('com_users','useractivation',$useractivation);
// set send password back to default
self::setParams('com_users','sendpassword',$sendpassword);
// if user is created
if ($userId > 0)
{
return $userId;
}
return $model->getError();
}
protected static function setParams($component,$target,$value)
{
// Get the params and set the new values
$params = JComponentHelper::getParams($component);
$was = $params->get($target, null);
if ($was != $value)
{
$params->set($target, $value);
// Get a new database query instance
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query
$query->update('#__extensions AS a');
$query->set('a.params = ' . $db->quote((string)$params));
$query->where('a.element = ' . $db->quote((string)$component));
// Execute the query
$db->setQuery($query);
$db->query();
}
return $was;
}
/**
* Update user values
*/
public static function updateUser($new)
{
// load the user component language files if there is an error.
$lang = JFactory::getLanguage();
$extension = 'com_users';
$base_dir = JPATH_ADMINISTRATOR;
$language_tag = 'en-GB';
$reload = true;
$lang->load($extension, $base_dir, $language_tag, $reload);
// load the user model
$model = self::getModel('user', JPATH_ADMINISTRATOR . '/components/com_users', 'Users');
// Check if password was set
if (isset($new['password']) && isset($new['password2']) && self::checkString($new['password']) && self::checkString($new['password2']))
{
// Use the users passwords
$password = $new['password'];
$password2 = $new['password2'];
}
// set username
if (isset($new['username']) && self::checkString($new['username']))
{
$new['username'] = self::safeString($new['username']);
}
else
{
$new['username'] = self::safeString($new['name']);
}
// linup update user data
$data = array(
'id' => $new['id'],
'username' => $new['username'],
'name' => $new['name'],
'email' => $new['email'],
'password1' => $password, // First password field
'password2' => $password2, // Confirm password field
'block' => 0 );
// set groups if found
if (isset($new['groups']) && self::checkArray($new['groups']))
{
$data['groups'] = $new['groups'];
}
// register the new user
$done = $model->save($data);
// if user is updated
if ($done)
{
return $new['id'];
}
return $model->getError();
}
/**
* 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;
}
/**
* Prepares the xml document
*/
public static function xls($rows,$fileName = null,$title = null,$subjectTab = null,$creator = 'Joomla Component Builder',$description = null,$category = null,$keywords = null,$modified = null)
{
// set the user
$user = JFactory::getUser();
// set fieldname if not set
if (!$fileName)
{
$fileName = 'exported_'.JFactory::getDate()->format('jS_F_Y');
}
// set modiefied if not set
if (!$modified)
{
$modified = $user->name;
}
// set title if not set
if (!$title)
{
$title = 'Book1';
}
// set tab name if not set
if (!$subjectTab)
{
$subjectTab = 'Sheet1';
}
// make sure the file is loaded
JLoader::import('PHPExcel', JPATH_COMPONENT_ADMINISTRATOR . '/helpers');
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator($creator)
->setCompany('Joomla Component Builder')
->setLastModifiedBy($modified)
->setTitle($title)
->setSubject($subjectTab);
if (!$description)
{
$objPHPExcel->getProperties()->setDescription($description);
}
if (!$keywords)
{
$objPHPExcel->getProperties()->setKeywords($keywords);
}
if (!$category)
{
$objPHPExcel->getProperties()->setCategory($category);
}
// Some styles
$headerStyles = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => '1171A3'),
'size' => 12,
'name' => 'Verdana'
));
$sideStyles = array(
'font' => array(
'bold' => true,
'color' => array('rgb' => '444444'),
'size' => 11,
'name' => 'Verdana'
));
$normalStyles = array(
'font' => array(
'color' => array('rgb' => '444444'),
'size' => 11,
'name' => 'Verdana'
));
// Add some data
if (self::checkArray($rows))
{
$i = 1;
foreach ($rows as $array){
$a = 'A';
foreach ($array as $value){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($a.$i, $value);
if ($i == 1){
$objPHPExcel->getActiveSheet()->getColumnDimension($a)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getStyle($a.$i)->applyFromArray($headerStyles);
$objPHPExcel->getActiveSheet()->getStyle($a.$i)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
} elseif ($a === 'A'){
$objPHPExcel->getActiveSheet()->getStyle($a.$i)->applyFromArray($sideStyles);
} else {
$objPHPExcel->getActiveSheet()->getStyle($a.$i)->applyFromArray($normalStyles);
}
$a++;
}
$i++;
}
}
else
{
return false;
}
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($subjectTab);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client's web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
jexit();
}
/**
* Get CSV Headers
*/
public static function getFileHeaders($dataType)
{
// make sure these files are loaded
JLoader::import('PHPExcel', JPATH_COMPONENT_ADMINISTRATOR . '/helpers');
JLoader::import('ChunkReadFilter', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/PHPExcel/Reader');
// get session object
$session = JFactory::getSession();
$package = $session->get('package', null);
$package = json_decode($package, true);
// set the headers
if(isset($package['dir']))
{
$chunkFilter = new PHPExcel_Reader_chunkReadFilter();
// only load first three rows
$chunkFilter->setRows(2,1);
// identify the file type
$inputFileType = PHPExcel_IOFactory::identify($package['dir']);
// create the reader for this file type
$excelReader = PHPExcel_IOFactory::createReader($inputFileType);
// load the limiting filter
$excelReader->setReadFilter($chunkFilter);
$excelReader->setReadDataOnly(true);
// load the rows (only first three)
$excelObj = $excelReader->load($package['dir']);
$headers = array();
foreach ($excelObj->getActiveSheet()->getRowIterator() as $row)
{
if($row->getRowIndex() == 1)
{
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell)
{
if (!is_null($cell))
{
$headers[$cell->getColumn()] = $cell->getValue();
}
}
$excelObj->disconnectWorksheets();
unset($excelObj);
break;
}
}
return $headers;
}
return false;
}
/**
* 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
*
*/
public static function getVar($table, $where = null, $whereString = 'user', $what = 'id', $operator = '=', $main = 'membersmanager')
{
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;
}
/**
* 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
*
*/
public static function getVars($table, $where = null, $whereString = 'user', $what = 'id', $operator = 'IN', $main = 'membersmanager', $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 jsonToString($value, $sperator = ", ", $table = null, $id = 'id', $name = 'name')
{
// do some table foot work
$external = false;
if (strpos($table, '#__') !== false)
{
$external = true;
$table = str_replace('#__', '', $table);
}
// 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 ($external)
{
if ($_name = self::getVar(null, $val, $id, $name, '=', $table))
{
$names[] = $_name;
}
}
else
{
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;
}
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('#__membersmanager_'.$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_membersmanager', '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_membersmanager.'.$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_membersmanager.'.$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_membersmanager.'.$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_membersmanager'))
{
// 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_membersmanager'))
{
// 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_membersmanager.'.$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_membersmanager.'.$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_membersmanager.'.$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_membersmanager'))
{
// 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_membersmanager'))
{
// 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_membersmanager'));
}
}
return $result;
}
/**
* Get any component's model
**/
public static function getModel($name, $path = JPATH_COMPONENT_ADMINISTRATOR, $component = 'Membersmanager', $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_membersmanager');
$parentId = $parent->id;
$name = 'com_membersmanager.'.$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_membersmanager',$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('#__membersmanager_'.$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((array)$_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);
}
/**
* 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;
}
}
/**
* 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
*
*/
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
// start the xml
$buttonXML = new SimpleXMLElement('<field/>');
// 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);
// load the haskey attributes
self::xmlAddAttributes($buttonXML, $buttonAttributes);
// set the button options
$buttonOptions = array(
'1' => isset($args[3]) ? self::htmlEscape($args[3]) : 'JYES',
'0' => isset($args[4]) ? self::htmlEscape($args[4]) : 'JNO');
// load the button options
self::xmlAddOptions($buttonXML, $buttonOptions);
// get the radio element
$button = JFormHelper::loadFieldType('radio');
// run
$button->setup($buttonXML, $buttonAttributes['default']);
return $button->input;
}
/**
* 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)$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((array)$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, $keepOnlyCharacters = 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);
// 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')
{
// 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_membersmanager', true);
// Medium Encryption Type
if ('medium' === $type)
{
// check if medium key is already loaded.
if (self::checkString(self::$mediumCryptKey))
{
return (self::$mediumCryptKey !== 'none') ? trim(self::$mediumCryptKey) : $default;
}
// get the path to the medium encryption key.
$medium_key_path = $params->get('medium_key_path', null);
if (self::checkString($medium_key_path))
{
// load the key from the file.
if (self::getMediumCryptKey($medium_key_path))
{
return trim(self::$mediumCryptKey);
}
}
}
return $default;
}
/**
* The Medium Encryption Key
*
* @var string/bool
**/
protected static $mediumCryptKey = false;
/**
* Get The Medium Encryption Key
*
* @param string $path The path to the medium crypt key folder
*
* @return string On success
*
**/
public static function getMediumCryptKey($path)
{
// Prep the path a little
$path = '/'. trim(str_replace('//', '/', $path), '/');
jimport('joomla.filesystem.folder');
/// Check if folder exist
if (!JFolder::exists($path))
{
// Lock key.
self::$mediumCryptKey = 'none';
// Set the error message.
JFactory::getApplication()->enqueueMessage(JText::_('COM_MEMBERSMANAGER_CONFIG_MEDIUM_KEY_PATH_ERROR'), 'Error');
return false;
}
// Create FileName and set file path
$filePath = $path.'/.'.md5('medium_crypt_key_file');
// Check if we already have the file set
if ((self::$mediumCryptKey = @file_get_contents($filePath)) !== FALSE)
{
return true;
}
// Set the key for the first time
self::$mediumCryptKey = self::randomkey(128);
// Open the key file
$fh = @fopen($filePath, 'w');
if (!is_resource($fh))
{
// Lock key.
self::$mediumCryptKey = 'none';
// Set the error message.
JFactory::getApplication()->enqueueMessage(JText::_('COM_MEMBERSMANAGER_CONFIG_MEDIUM_KEY_PATH_ERROR'), 'Error');
return false;
}
// Write to the key file
if (!fwrite($fh, self::$mediumCryptKey))
{
// Close key file.
fclose($fh);
// Lock key.
self::$mediumCryptKey = 'none';
// Set the error message.
JFactory::getApplication()->enqueueMessage(JText::_('COM_MEMBERSMANAGER_CONFIG_MEDIUM_KEY_PATH_ERROR'), 'Error');
return false;
}
// Close key file.
fclose($fh);
// Key is set.
return true;
}
}