Members-Manager/site/models/ajax.php

1197 lines
39 KiB
PHP
Raw Normal View History

2018-07-25 03:00:13 +00:00
<?php
/**
* @package Joomla.Members.Manager
*
2018-12-27 10:38:37 +00:00
* @created 6th July, 2018
2018-07-25 03:00:13 +00:00
* @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
*/
2019-08-14 02:14:40 +00:00
2018-07-25 03:00:13 +00:00
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
2021-01-06 14:00:15 +00:00
use Joomla\Utilities\ArrayHelper;
2018-07-25 03:00:13 +00:00
/**
* Membersmanager Ajax Model
*/
class MembersmanagerModelAjax extends JModelList
{
protected $app_params;
public function __construct()
{
parent::__construct();
// get params
$this->app_params = JComponentHelper::getParams('com_membersmanager');
}
// Used in member
// allowed views
protected $allowedViews = array('member');
// allowed targets
protected $targets = array('profile');
// allowed types
protected $types = array('image' => 'image');
public function getUserDetails($user)
{
//first we check if this is an allowed query
$view = $this->getViewID();
if (isset($view['a_view']) && in_array($view['a_view'], $this->allowedViews))
{
// since the connected member user can not be changed, check if this member has a user value set
if (isset($view['a_id']) && $view['a_id'] > 0 && ($alreadyUser = MembersmanagerHelper::getVar($view['a_view'], $view['a_id'], 'id', 'user')) !== false && is_numeric($alreadyUser) && $alreadyUser > 0 && $user != $alreadyUser)
{
return false;
}
// return user details
$user = JFactory::getUser($user);
return array(
'name' => $user->name,
'username' => $user->username,
'useremail' => $user->email
);
}
return false;
}
2018-12-27 10:38:37 +00:00
// get placeholder header if available
public function getPlaceHolderHeaders($component)
{
2019-04-04 11:58:49 +00:00
if ('com_membersmanager' === $component || 'com_corecomponent' === $component)
2018-12-27 10:38:37 +00:00
{
2019-04-04 11:58:49 +00:00
// just return the core placeholders
return JText::_('COM_MEMBERSMANAGER_CORE_PLACEHOLDERS');
2018-12-27 10:38:37 +00:00
}
return MembersmanagerHelper::getComponentName($component);
}
// get chart image link
public function getChartImageLink($image)
{
$view = $this->getViewID();
// make sure we are in the (allowed) view
2019-04-04 11:58:49 +00:00
if (isset($view['a_view']) && ($view['a_view'] === 'compose' || $view['a_view'] === 'profile'))
2018-12-27 10:38:37 +00:00
{
// build image name
$imageName = md5($image . 'jnst_f0r_dumm!es');
// build image data
$image = explode('base64,', $image); unset($image[0]); $image = str_replace(' ', '+', implode('', $image));
// validate Base64
if (($image = MembersmanagerHelper::openValidBase64($image, null, false)) !== false)
{
// validate just png (for now)
$png_binary_check = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a";
if (substr($image, 0, strlen($png_binary_check)) === $png_binary_check)
{
// build image path
$imagepath = MembersmanagerHelper::getFolderPath('path', 'chartpath') . $imageName . '.png';
2021-01-06 14:00:15 +00:00
// check if the file exist
if (file_exists($imagepath))
2018-12-27 10:38:37 +00:00
{
// build and return image link
return array('link' => MembersmanagerHelper::getFolderPath('url', 'chartpath') . $imageName . '.png');
}
2021-01-06 14:00:15 +00:00
// now write the file if not exists
if (MembersmanagerHelper::writeFile($imagepath, $image))
{
// now check if this type of image is allowed to be uploaded
if (JHelperMedia::canUpload($imagepath, 'com_membersmanager'))
{
// build and return image link
return array('link' => MembersmanagerHelper::getFolderPath('url', 'chartpath') . $imageName . '.png');
}
// load the file class
jimport('joomla.filesystem.file');
// remove the image
JFile::delete($imagepath);
}
2018-12-27 10:38:37 +00:00
}
}
}
return false;
}
2018-07-25 03:00:13 +00:00
// set some buckets
protected $target;
protected $targetType;
protected $formatType;
// file details
protected $fileName;
protected $folderPath;
protected $fullPath;
protected $fileFormat;
// return error if upload fails
protected $errorMessage;
// set uploading values
protected $use_streams = false;
protected $allow_unsafe = false;
protected $safeFileOptions = array();
public function uploadfile($target, $type)
{
// get the view values
$view = $this->getViewID();
if (in_array($target, $this->targets) && isset($this->types[$type]) && isset($view['a_view']) && in_array($view['a_view'], $this->allowedViews))
{
$this->target = (string) $target;
$this->targetType = (string) $type;
$this->formatType = (string) $this->types[$type];
2021-01-06 14:00:15 +00:00
if (($package = $this->_getPackageFromUpload()) !== false)
2018-07-25 03:00:13 +00:00
{
// now we move the file into place
return $this->uploadNow($package, $view);
}
return array('error' => $this->errorMessage);
}
return array('error' => JText::_('COM_MEMBERSMANAGER_THERE_HAS_BEEN_AN_ERROR'));
}
protected function uploadNow($package, $view)
{
// set the package name to file name if found
$name = $this->formatType;
if (isset($package['packagename']))
{
$name = MembersmanagerHelper::safeString(str_replace('.'.$this->fileFormat, '', $package['packagename']), 'filename', '_', false);
}
2021-01-06 14:00:15 +00:00
$this->fileName = $this->target . '_' . $this->targetType . '_' . $this->fileFormat . '_' . MembersmanagerHelper::randomkey(20) . 'VDM' . $name;
2018-07-25 03:00:13 +00:00
// set the folder path
2021-01-06 14:00:15 +00:00
if ($this->formatType === 'file' || $this->formatType === 'document' || $this->formatType === 'media')
2018-07-25 03:00:13 +00:00
{
// get the folder path
$this->folderPath = MembersmanagerHelper::getFolderPath('path', 'hiddenfilepath');
}
else
{
// get the file path
$this->folderPath = MembersmanagerHelper::getFolderPath();
}
// set full path to the file
$this->fullPath = $this->folderPath . $this->fileName . '.' . $this->fileFormat;
// move to target folder
if (JFile::move($package['dir'], $this->fullPath))
{
// do crop/resize if it is an image and cropping is set
if ($this->formatType === 'image')
{
MembersmanagerHelper::resizeImage($this->fileName, $this->fileFormat, $this->target, $this->folderPath, $this->fullPath);
}
2021-01-06 14:00:15 +00:00
$encryption = null;
$expertmode = false;
// basic encryption of these format types
if ($this->formatType === 'document' || $this->formatType === 'media')
{
// Get the basic encryption.
$encryptionkey = MembersmanagerHelper::getCryptKey('basic');
}
// medium encryption of these format types
elseif ($this->formatType === 'file')
{
// check if we have expert Mode
if (method_exists('MembersmanagerHelper', 'encrypt'))
{
$expertmode = true;
}
else
{
// Get the medium encryption.
$encryptionkey = MembersmanagerHelper::getCryptKey('medium');
}
}
2018-07-25 03:00:13 +00:00
// set link options
2021-01-06 14:00:15 +00:00
if (isset($encryptionkey) && $encryptionkey)
2018-07-25 03:00:13 +00:00
{
// Get the encryption object.
2021-01-06 14:00:15 +00:00
$encryption = new FOFEncryptAes($encryptionkey, 128);
2018-07-25 03:00:13 +00:00
}
// when it is documents we need to give file name in base64
2021-01-06 14:00:15 +00:00
if ($this->formatType === 'file' || $this->formatType === 'document' || $this->formatType === 'media')
2018-07-25 03:00:13 +00:00
{
// store the name
$keyName = $this->fileName;
2021-01-06 14:00:15 +00:00
if (MembersmanagerHelper::checkObject($encryption) || $expertmode)
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
// also encrypt the actual content of the file
if ($this->formatType === 'file')
{
// add notice to name that file is encrypted
$this->fileName = $keyName = '.' . $this->fileName;
$securefullPath = $this->folderPath . $this->fileName;
// also encrypt the actual content of the file
if ($expertmode)
{
MembersmanagerHelper::writeFile($securefullPath, wordwrap(MembersmanagerHelper::encrypt(file_get_contents($this->fullPath)), 128, "\n", true));
}
else
{
MembersmanagerHelper::writeFile($securefullPath, wordwrap($encryption->encryptString(file_get_contents($this->fullPath)), 128, "\n", true));
}
// remove the original
jimport('joomla.filesystem.file');
JFile::delete($this->fullPath);
}
2018-07-25 03:00:13 +00:00
// Get the encryption object.
2021-01-06 14:00:15 +00:00
if ($expertmode)
{
$localFile = MembersmanagerHelper::base64_urlencode(MembersmanagerHelper::encrypt($keyName, false), true);
}
else
{
$localFile = MembersmanagerHelper::base64_urlencode($encryption->encryptString($keyName));
}
2018-07-25 03:00:13 +00:00
}
else
{
// can not get the encryption object so only base64 encode
$localFile = MembersmanagerHelper::base64_urlencode($keyName, true);
}
}
// check if we must update the current item
2021-01-06 14:00:15 +00:00
if (isset($view['a_id']) && $view['a_id'] > 0)
2018-07-25 03:00:13 +00:00
{
$object = new stdClass();
$object->id = (int) $view['a_id'];
2021-01-06 14:00:15 +00:00
if ($this->formatType === 'file' || $this->targetType === 'image' || $this->targetType === 'document')
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
if (MembersmanagerHelper::checkObject($encryption) || $expertmode)
2018-07-25 03:00:13 +00:00
{
// Get the encryption object.
2021-01-06 14:00:15 +00:00
if ($expertmode)
{
$object->{$this->target . '_' . $this->targetType} = MembersmanagerHelper::encrypt($this->fileName);
}
else
{
$object->{$this->target . '_' . $this->targetType} = $encryption->encryptString($this->fileName);
}
2018-07-25 03:00:13 +00:00
}
else
{
// can not get the encryption object.
2021-01-06 14:00:15 +00:00
$object->{$this->target . '_' . $this->targetType} = $this->fileName;
2018-07-25 03:00:13 +00:00
}
}
elseif ($this->targetType === 'images' || $this->targetType === 'documents' || $this->targetType === 'media')
{
2021-01-06 14:00:15 +00:00
$this->fileName = $this->setFileNameArray('add', $encryption, $view);
if (MembersmanagerHelper::checkObject($encryption))
2018-07-25 03:00:13 +00:00
{
// Get the encryption object.
2021-01-06 14:00:15 +00:00
$object->{$this->target . '_' . $this->targetType} = $encryption->encryptString($this->fileName);
2018-07-25 03:00:13 +00:00
}
else
{
// can not get the encryption object.
2021-01-06 14:00:15 +00:00
$object->{$this->target . '_' . $this->targetType} = $this->fileName;
2018-07-25 03:00:13 +00:00
}
}
2021-01-06 14:00:15 +00:00
JFactory::getDbo()->updateObject('#__membersmanager_' . (string) $view['a_view'], $object, 'id');
2018-07-25 03:00:13 +00:00
}
elseif ($this->targetType === 'images' || $this->targetType === 'documents' || $this->targetType === 'media')
{
$this->fileName = array($this->fileName);
$this->fileName = '["'.implode('", "', $this->fileName).'"]';
}
// set the results
$result = array('success' => $this->fileName, 'fileformat' => $this->fileFormat);
// add some more values if document format type
2021-01-06 14:00:15 +00:00
if ($this->formatType === 'file' || $this->formatType === 'document' || $this->formatType === 'media')
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
// set link options
$linkOptions = MembersmanagerHelper::getLinkOptions();
// do not lock file for link unless lock is set
2018-07-25 03:00:13 +00:00
if ($linkOptions['lock'] == 0)
{
$localFile = MembersmanagerHelper::base64_urlencode($keyName, true);
}
2021-01-06 14:00:15 +00:00
$tokenLink = '';
2018-07-25 03:00:13 +00:00
if ($linkOptions['session'])
{
2021-01-06 14:00:15 +00:00
$tokenLink = '&' . JSession::getFormToken() . '=1';
2018-07-25 03:00:13 +00:00
}
2021-01-06 14:00:15 +00:00
// if document or file
if ($this->formatType === 'file' || $this->formatType === 'document')
2018-07-25 03:00:13 +00:00
{
$result['link'] = 'index.php?option=com_membersmanager&task=download.document&file=' . $localFile . $tokenLink;
}
// if media
elseif ($this->formatType === 'media')
{
$result['link'] = 'index.php?option=com_membersmanager&task=download.media&file=' . $localFile . $tokenLink;
}
$result['key'] = $keyName;
}
return $result;
}
$this->remove($package['packagename']);
return array('error' => JText::_('COM_MEMBERSMANAGER_THERE_HAS_BEEN_AN_ERROR'));
}
public function removeFile($oldFile, $target, $clearDB, $type)
{
// get view values
$view = $this->getViewID();
if (in_array($target, $this->targets) && isset($this->types[$type]) && isset($view['a_view']) && in_array($view['a_view'], $this->allowedViews))
{
$this->target = (string) $target;
$this->targetType = (string) $type;
$this->formatType = (string) $this->types[$type];
$this->fileName = (string) $oldFile;
2021-01-06 14:00:15 +00:00
// check permissions
2018-07-25 03:00:13 +00:00
if (isset($view['a_id']) && $view['a_id'] > 0 && isset($view['a_view']))
{
// get user to see if he has permission to upload
$user = JFactory::getUser();
2021-01-06 14:00:15 +00:00
if (!$user->authorise($view['a_view'] . '.edit.'. $this->target . '_' . $this->targetType, 'com_membersmanager'))
2018-07-25 03:00:13 +00:00
{
return array('error' => JText::_('COM_MEMBERSMANAGER_YOU_DO_NOT_HAVE_PERMISSION_TO_REMOVE_THIS_FILE'));
}
}
2021-01-06 14:00:15 +00:00
if ($this->formatType === 'file' || $this->formatType === 'document' || $this->formatType === 'media')
2018-07-25 03:00:13 +00:00
{
// get the file path
$this->folderPath = MembersmanagerHelper::getFolderPath('path', 'hiddenfilepath');
}
else
{
// get the file path
$this->folderPath = MembersmanagerHelper::getFolderPath();
}
// remove from the db if there is an id
2021-01-06 14:00:15 +00:00
if ($clearDB == 1 && isset($view['a_id']) && $view['a_id'] > 0)
2018-07-25 03:00:13 +00:00
{
$object = new stdClass();
$object->id = (int) $view['a_id'];
2021-01-06 14:00:15 +00:00
if ($this->formatType === 'file' || $this->targetType === 'image' || $this->targetType === 'document')
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
$object->{$this->target . '_' . $this->targetType} = '';
JFactory::getDbo()->updateObject('#__membersmanager_' . $view['a_view'], $object, 'id');
2018-07-25 03:00:13 +00:00
}
elseif ($this->targetType === 'images' || $this->targetType === 'documents' || $this->targetType === 'media')
{
// Get the basic encription.
2021-01-06 14:00:15 +00:00
$encryptionkey = MembersmanagerHelper::getCryptKey('basic');
$encryption = null;
if ($encryptionkey)
2018-07-25 03:00:13 +00:00
{
// Get the encryption object.
2021-01-06 14:00:15 +00:00
$encryption = new FOFEncryptAes($encryptionkey, 128);
2018-07-25 03:00:13 +00:00
}
2021-01-06 14:00:15 +00:00
$fileNameArray = $this->setFileNameArray('remove', $encryption, $view);
if (MembersmanagerHelper::checkObject($encryption))
2018-07-25 03:00:13 +00:00
{
// Get the encryption object.
2021-01-06 14:00:15 +00:00
$object->{$this->target.'_'.$this->targetType} = $encryption->encryptString($fileNameArray);
2018-07-25 03:00:13 +00:00
}
else
{
// can not get the encryption object.
$object->{$this->target.'_'.$this->targetType} = $fileNameArray;
}
JFactory::getDbo()->updateObject('#__membersmanager_'.$view['a_view'], $object, 'id');
}
}
// load the file class
jimport('joomla.filesystem.file');
2021-01-06 14:00:15 +00:00
// check if this is a locked file
if (substr($this->fileName, 0, 1) === '.' && JFile::exists($this->folderPath . $this->fileName))
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
// remove the file
return JFile::delete($this->folderPath . $this->fileName);
}
else
{
// set formats
$this->formats = MembersmanagerHelper::getFileExtensions($this->formatType);
foreach ($this->formats as $fileFormat)
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
if (JFile::exists($this->folderPath . $this->fileName . '.' . $fileFormat))
{
// remove the file
return JFile::delete($this->folderPath . $this->fileName . '.' . $fileFormat);
}
2018-07-25 03:00:13 +00:00
}
}
}
return array('error' => JText::_('COM_MEMBERSMANAGER_THERE_HAS_BEEN_AN_ERROR'));
}
2021-01-06 14:00:15 +00:00
protected function setFileNameArray($action, $encryption, $view)
2018-07-25 03:00:13 +00:00
{
$curentFiles = MembersmanagerHelper::getVar($view['a_view'], $view['a_id'], 'id', $this->target.'_'.$this->targetType);
// unlock if needed
2021-01-06 14:00:15 +00:00
if ($encryption && $curentFiles === base64_encode(base64_decode($curentFiles, true)))
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
// decrypt data image.
$curentFiles = rtrim($encryption->decryptString($curentFiles), "\0");
2018-07-25 03:00:13 +00:00
}
// convert to array if needed
if (MembersmanagerHelper::checkJson($curentFiles))
{
$curentFiles = json_decode($curentFiles, true);
}
// remove or add the file name
if (MembersmanagerHelper::checkArray($curentFiles))
{
if ('add' === $action)
{
$curentFiles[] = $this->fileName;
}
else
{
if(($key = array_search($this->fileName, $curentFiles)) !== false)
{
unset($curentFiles[$key]);
}
}
}
elseif ('add' === $action)
{
$curentFiles = array($this->fileName);
}
else
{
$curentFiles = '';
}
// convert to json
if (MembersmanagerHelper::checkArray($curentFiles))
{
return '["'.implode('", "', $curentFiles).'"]';
}
return '';
}
/**
* Works out an importation file from a HTTP upload
*
* @return file definition or false on failure
*/
protected function _getPackageFromUpload()
{
// Get the uploaded file information
2019-04-04 11:58:49 +00:00
$app = JFactory::getApplication();
$input = $app->input;
2018-07-25 03:00:13 +00:00
// See JInputFiles::get.
$userfiles = $input->files->get('files', null, 'array');
// Make sure that file uploads are enabled in php
if (!(bool) ini_get('file_uploads'))
{
$this->errorMessage = JText::_('COM_MEMBERSMANAGER_WARNING_IMPORT_FILE_ERROR');
return false;
}
// get the files from array
$userfile = null;
if (is_array($userfiles))
{
$userfile = array_values($userfiles)[0];
}
// If there is no uploaded file, we have a problem...
if (!is_array($userfile))
{
$this->errorMessage = JText::_('COM_MEMBERSMANAGER_NO_IMPORT_FILE_SELECTED');
return false;
}
// Check if there was a problem uploading the file.
if ($userfile['error'] || $userfile['size'] < 1)
{
$this->errorMessage = JText::_('COM_MEMBERSMANAGER_WARNING_IMPORT_UPLOAD_ERROR');
return false;
}
// Build the appropriate paths
2019-04-04 11:58:49 +00:00
$config = JFactory::getConfig();
$tmp_dest = $config->get('tmp_path') . '/' . $userfile['name'];
$tmp_src = $userfile['tmp_name'];
2018-07-25 03:00:13 +00:00
// Move uploaded file
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
$p_file = JFile::upload($tmp_src, $tmp_dest, $this->use_streams, $this->allow_unsafe, $this->safeFileOptions);
// Was the package downloaded?
if (!$p_file)
{
$session = JFactory::getSession();
$session->clear('package');
$session->clear('dataType');
$session->clear('hasPackage');
$this->errorMessage = JText::_('COM_MEMBERSMANAGER_COULD_NOT_UPLOAD_THE_FILE');
// was not uploaded
return false;
}
// check that this is a valid file
$package = $this->check($userfile['name']);
return $package;
}
/**
* Check a file and verifies it as a allowed file format file
*
* @param string $archivename The uploaded package filename or import directory
*
* @return array of elements
*
*/
protected function check($archivename)
{
2021-01-06 14:00:15 +00:00
// set formats
$this->formats = MembersmanagerHelper::getFileExtensions($this->formatType);
2018-07-25 03:00:13 +00:00
// Clean the name
$archivename = JPath::clean($archivename);
// get file format
$this->fileFormat = strtolower(pathinfo($archivename, PATHINFO_EXTENSION));
// get fileFormat key
$allowedFormats = array();
2021-01-06 14:00:15 +00:00
if (in_array($this->fileFormat, $this->formats))
2018-07-25 03:00:13 +00:00
{
// get allowed formats
2021-01-06 14:00:15 +00:00
$allowedFormats = (array) $this->app_params->get($this->formatType.'_formats', array());
2018-07-25 03:00:13 +00:00
}
// check the extension
if (!in_array($this->fileFormat, $allowedFormats))
{
// Cleanup the import files
$this->remove($archivename);
$this->errorMessage = JText::_('COM_MEMBERSMANAGER_DOES_NOT_HAVE_A_VALID_FILE_TYPE');
return false;
}
// check permission if user
$view = $this->getViewID();
2021-01-06 14:00:15 +00:00
// get user to see if he has permission to upload
$user = JFactory::getUser();
if (!$user->authorise($view['a_view'] . '.edit.' . $this->target . '_' . $this->targetType, 'com_membersmanager'))
2018-07-25 03:00:13 +00:00
{
2021-01-06 14:00:15 +00:00
// Cleanup the import files
$this->remove($archivename);
$this->errorMessage = JText::_('COM_MEMBERSMANAGER_YOU_DO_NOT_HAVE_PERMISSION_TO_UPLOAD_AN' . $this->targetType);
return false;
2018-07-25 03:00:13 +00:00
}
2021-01-06 14:00:15 +00:00
2019-04-04 11:58:49 +00:00
$config = JFactory::getConfig();
2018-07-25 03:00:13 +00:00
// set Package Name
2019-04-04 11:58:49 +00:00
$check['packagename'] = $archivename;
2021-01-06 14:00:15 +00:00
2018-07-25 03:00:13 +00:00
// set directory
2019-04-04 11:58:49 +00:00
$check['dir'] = $config->get('tmp_path'). '/' .$archivename;
2021-01-06 14:00:15 +00:00
2018-07-25 03:00:13 +00:00
return $check;
}
2021-01-06 14:00:15 +00:00
2018-07-25 03:00:13 +00:00
/**
* Clean up temporary uploaded file
*
* @param string $package Name of the uploaded file
*
* @return boolean True on success
*
*/
protected function remove($package)
{
jimport('joomla.filesystem.file');
2021-01-06 14:00:15 +00:00
2019-04-04 11:58:49 +00:00
$config = JFactory::getConfig();
$package = $config->get('tmp_path'). '/' .$package;
2018-07-25 03:00:13 +00:00
// Is the package file a valid file?
if (is_file($package))
{
JFile::delete($package);
}
elseif (is_file(JPath::clean($package)))
{
// It might also be just a base filename
JFile::delete(JPath::clean($package));
}
}
protected $viewid = array();
protected function getViewID($call = 'table')
{
if (!isset($this->viewid[$call]))
{
// get the vdm key
$jinput = JFactory::getApplication()->input;
$vdm = $jinput->get('vdm', null, 'WORD');
2021-01-06 14:00:15 +00:00
if ($vdm)
2018-07-25 03:00:13 +00:00
{
// set view and id
2018-07-25 03:00:13 +00:00
if ($view = MembersmanagerHelper::get($vdm))
{
$current = (array) explode('__', $view);
if (MembersmanagerHelper::checkString($current[0]) && isset($current[1]) && is_numeric($current[1]))
{
// get the view name & id
$this->viewid[$call] = array(
'a_id' => (int) $current[1],
'a_view' => $current[0]
);
}
}
2021-01-06 14:00:15 +00:00
// set GUID if found
if (($guid = MembersmanagerHelper::get($vdm . '__guid')) !== false && method_exists('MembersmanagerHelper', 'validGUID'))
{
if (MembersmanagerHelper::validGUID($guid))
{
$this->viewid[$call]['a_guid'] = $guid;
}
}
// set return if found
2021-01-06 14:00:15 +00:00
if (($return = MembersmanagerHelper::get($vdm . '__return')) !== false)
{
if (MembersmanagerHelper::checkString($return))
{
$this->viewid[$call]['a_return'] = $return;
}
}
2018-07-25 03:00:13 +00:00
}
}
if (isset($this->viewid[$call]))
{
return $this->viewid[$call];
}
return false;
}
public function checkUnique($field, $value)
{
// split at upper case
$valueArray = (array) preg_split('/(?=[A-Z])/', trim($value), -1, PREG_SPLIT_NO_EMPTY);
// make string safe
2018-07-25 03:00:13 +00:00
$value = MembersmanagerHelper::safeString(trim(implode(' ', $valueArray), '-'), 'L', '-', false, false);
// get table and current ID
2018-07-25 03:00:13 +00:00
$view = $this->getViewID();
// check if it is unique
if (MembersmanagerHelper::checkUnique($view['a_id'], $field, $value, $view['a_view']))
2018-07-25 03:00:13 +00:00
{
return array (
'value' => $value,
'message' => JText::sprintf('COM_MEMBERSMANAGER_GREAT_SS_IS_AVAILABLE', $field, $value),
'status' => 'success');
}
return array (
'message' => JText::sprintf('COM_MEMBERSMANAGER_BSB_IS_ALREADY_IN_USE_PLEASE_TRY_ANOTHER', $value),
'status' => 'danger');
2018-07-25 03:00:13 +00:00
}
2018-12-27 10:38:37 +00:00
/**
* get any placeholder
*
* @param string $getType Name get type
*
* @return string The html string of placeholders
*
*/
public function getAnyPlaceHolders($getType)
{
// check if we should add a header
if (method_exists(__CLASS__, 'getPlaceHolderHeaders') && ($string = $this->getPlaceHolderHeaders($getType)) !== false)
{
$string = JText::_($string) . ' ';
$header = '<h4>' . $string . '</h4>';
}
else
{
$string = '';
$header = '';
}
2019-04-04 11:58:49 +00:00
// get the core component helper class & get placeholders
2019-08-14 02:14:40 +00:00
if (($helperClass = MembersmanagerHelper::getHelperClass('membersmanager')) !== false && ($placeholders = $helperClass::getAnyPlaceHolders($getType)) !== false)
2018-12-27 10:38:37 +00:00
{
return '<div>' . $header . '<code style="display: inline-block; padding: 2px; margin: 3px;">' .
implode('</code> <code style="display: inline-block; padding: 2px; margin: 3px;">', $placeholders) .
'</code></div>';
}
// not found
return '<div class="alert alert-error"><h4 class="alert-heading">' .
$string . JText::_('COM_MEMBERSMANAGER_PLACEHOLDERS_NOT_FOUND') .
'!</h4><div class="alert-message">' .
JText::_('COM_MEMBERSMANAGER_THERE_WAS_AN_ERROR_PLEASE_TRY_AGAIN_LATER_IF_THIS_ERROR_CONTINUES_CONTACT_YOUR_SYSTEM_ADMINISTRATOR') .
'</div></div>';
}
/**
* get the placeholder
*
* @param string $getType Name get type
*
* @return string The html string of placeholders
*
*/
public function getPlaceHolders($getType)
2018-07-25 03:00:13 +00:00
{
2018-12-27 10:38:37 +00:00
// check if we should add a header
if (method_exists(__CLASS__, 'getPlaceHolderHeaders') && ($string = $this->getPlaceHolderHeaders($getType)) !== false)
{
$string = JText::_($string) . ' ';
$header = '<h4>' . $string . '</h4>';
}
else
{
$string = '';
$header = '';
}
// get placeholders
if ($placeholders = MembersmanagerHelper::getPlaceHolders($getType))
2018-07-25 03:00:13 +00:00
{
2018-12-27 10:38:37 +00:00
return '<div>' . $header . '<code style="display: inline-block; padding: 2px; margin: 3px;">' .
implode('</code> <code style="display: inline-block; padding: 2px; margin: 3px;">', $placeholders) .
'</code></div>';
2018-07-25 03:00:13 +00:00
}
2018-12-27 10:38:37 +00:00
// not found
return '<div class="alert alert-error"><h4 class="alert-heading">' .
$string . JText::_('COM_MEMBERSMANAGER_PLACEHOLDERS_NOT_FOUND') .
'!</h4><div class="alert-message">' .
JText::_('COM_MEMBERSMANAGER_THERE_WAS_AN_ERROR_PLEASE_TRY_AGAIN_LATER_IF_THIS_ERROR_CONTINUES_CONTACT_YOUR_SYSTEM_ADMINISTRATOR') .
'</div></div>';
2018-07-25 03:00:13 +00:00
}
// Used in cpanel
public function searchMembers($search)
2018-07-25 03:00:13 +00:00
{
// get user
$user = JFactory::getUser();
// get the session details
$view = $this->getViewID();
if (isset($view['a_id']) && $view['a_id'] > 0 && $user->id == $view['a_id'])
{
// start building the result set
$result = array();
// get the members
$members = $this->getMembers($search, $user);
// make sure we have some member values
if (MembersmanagerHelper::checkArray($members))
2018-07-25 03:00:13 +00:00
{
$result[] = '<ul class="uk-list uk-list-striped">';
$result[] = '<li>' . implode('</li><li>', $members) . '</li>';
$result[] = '</ul>';
}
else
{
$result[] = '<p>' . JText::_('COM_MEMBERSMANAGER_NO_MEMBER_WAS_FOUND') . '...</p>';
2018-07-25 03:00:13 +00:00
}
return implode("\n", $result);
2018-07-25 03:00:13 +00:00
}
return false;
}
protected function getMembers($search, $user)
2018-07-25 03:00:13 +00:00
{
// get members from DB
2019-08-14 02:14:40 +00:00
$this->searching($search, $user, $members, 1);
// make sure we have members
if (MembersmanagerHelper::checkArray($members))
{
2019-08-14 02:14:40 +00:00
// only sort if not email search
if (strpos($search, '@') === false)
{
// little sorter based on name
usort($members, function ($a, $b) use($search) {
// first check if this is a name with space (basically hen name and surname is in one field = name)
$length = strlen($search);
$evalue = substr($a->name, 0, $length) == $search;
$fvalue = substr($b->name, 0, $length) == $search;
// if not found
if (!$evalue && !$fvalue)
{
// best break up the string and search for each word
$searchArray = (array) preg_split('/\s+/', $search);
$name = array_shift($searchArray);
$length = strlen($name);
$avalue = substr($a->name, 0, $length) == $name;
$bvalue = substr($b->name, 0, $length) == $name;
// if the name is the same, use the surname (works only if second string is surname)
if ($avalue == $bvalue)
{
if (MembersmanagerHelper::checkArray($searchArray))
{
// make sure we have surnames
if (MembersmanagerHelper::checkString($a->surname) && MembersmanagerHelper::checkString($b->surname) )
{
$surname = implode(' ', $searchArray);
$length = strlen($surname);
$cvalue = substr($a->surname, 0, $length) == $surname;
$dvalue = substr($b->surname, 0, $length) == $surname;
// if the surname is the same
if ($cvalue == $dvalue) return strcmp($a->surname, $b->surname);
/*else*/
if ($cvalue) return -1;
if ($dvalue) return 1;
}
// try for longer name
else
{
$name = implode(' ', $searchArray);
$length = strlen($name);
$cvalue = substr($a->name, 0, $length) == $name;
$dvalue = substr($b->name, 0, $length) == $name;
// if the surname is the same
if ($cvalue == $dvalue) return strcmp($a->name, $b->name);
/*else*/
if ($cvalue) return -1;
if ($dvalue) return 1;
}
}
return strcmp($a->name, $b->name);
}
/*else*/
if ($avalue) return -1;
if ($bvalue) return 1;
}
else
{
/*else*/
if ($evalue) return -1;
if ($fvalue) return 1;
}
});
}
return (array) array_map( function ($member){
// build the details
$slug = (isset($member->token)) ? $member->id . ':' . $member->token : $member->id;
2019-08-14 02:14:40 +00:00
$profile_uri = MembersmanagerHelperRoute::getProfileRoute($slug);
$profile_link = JRoute::_($profile_uri);
2019-04-04 11:58:49 +00:00
$name = (isset($member->user_name) && MembersmanagerHelper::checkString($member->user_name)) ? $member->user_name : $member->name;
2019-08-14 02:14:40 +00:00
$surname = (isset($member->surname) && MembersmanagerHelper::checkString($member->surname)) ? ' ' . $member->surname : '';
2019-04-04 11:58:49 +00:00
$email = (isset($member->user_email) && MembersmanagerHelper::checkString($member->user_email)) ? $member->user_email : $member->email;
// build the link to the member
2019-08-14 02:14:40 +00:00
return '<a href="' . $profile_link . '" title="' . JText::_('COM_MEMBERSMANAGER_OPEN') . ' ' . $name . $surname . ' ' . JText::_('COM_MEMBERSMANAGER_PROFILE') . '" >' . $name . $surname . ' ' . $email . ' (' . $member->token . ')</a> ' .
MembersmanagerHelper::getEditButton($member, 'member', 'members', '&return=' . urlencode(base64_encode($profile_uri)), 'com_membersmanager', null);
}, $members);
2018-07-25 03:00:13 +00:00
}
return false;
}
2019-08-14 02:14:40 +00:00
protected function searching($search, $user, &$members, $action)
2018-07-25 03:00:13 +00:00
{
// Create a new query object.
$db = JFactory::getDBO();
$query = $db->getQuery(true);
2018-07-25 03:00:13 +00:00
// Select some fields
2019-08-14 02:14:40 +00:00
$query->select($db->quoteName(array('a.id','a.name','a.email','a.token','a.type','a.surname'),array('id','name','email','token','type','surname')));
2018-07-25 03:00:13 +00:00
// From the membersmanager_item table
$query->from($db->quoteName('#__membersmanager_member', 'a'));
2018-07-25 03:00:13 +00:00
// From the users table.
$query->select($db->quoteName(array('g.name','g.email'),array('user_name','user_email')));
$query->join('LEFT', $db->quoteName('#__users', 'g') . ' ON (' . $db->quoteName('a.user') . ' = ' . $db->quoteName('g.id') . ')');
2018-07-25 03:00:13 +00:00
// Filter by published state always (for now)
2018-12-27 10:38:37 +00:00
$query->where('a.published = 1');
2018-07-25 03:00:13 +00:00
// Implement View Level Access
if (!$user->authorise('core.options', 'com_membersmanager'))
2018-07-25 03:00:13 +00:00
{
// Join over the asset groups.
$query->join('LEFT', '#__viewlevels AS ag ON ag.id = a.access');
$groups = implode(',', $user->getAuthorisedViewLevels());
$query->where('a.access IN (' . $groups . ')');
2018-07-25 03:00:13 +00:00
}
2019-08-14 02:14:40 +00:00
// if we aready have members, stop them from being queried again
if (MembersmanagerHelper::checkArray($members))
{
$ids = (array) array_map( function ($member){
// return idies
return $member->id;
}, $members);
// remove the members already loaded
$query->where('a.id NOT IN (' . implode(',', $ids) . ')');
}
// check if they are searching for an id
if (stripos($search, 'id:') === 0)
2018-07-25 03:00:13 +00:00
{
$query->where('a.id = ' . (int) substr($search, 3));
2018-07-25 03:00:13 +00:00
}
else
2018-07-25 03:00:13 +00:00
{
2019-08-14 02:14:40 +00:00
// best break up the string and search for each word
$searchArray = (array) preg_split('/\s+/', $search);
// start search bucket
$searchBucket = array();
// try to get it only
if (1 == $action)
{
if (strpos($search, '@') !== false)
{
foreach ($searchArray as $i => $searchString)
{
$searchReady = $db->quote('%' . $db->escape($searchString) . '%');
// only if string hints to be an email
if (strpos($searchString, '@') !== false)
{
$searchBucket[] = 'g.email LIKE ' . $searchReady;
$searchBucket[] = 'a.email LIKE ' . $searchReady;
// remove from array since it was an email
unset($searchArray[$i]);
break;
}
}
}
// not email with more then one word
if (count($searchArray) >= 2)
{
// get the name
$searchBucket[] = 'a.name LIKE ' . $db->quote('%' . $db->escape(array_shift($searchArray)) . '%');
$searchBucket[] = 'a.surname LIKE ' . $db->quote('%' . $db->escape( implode(' ', $searchArray) ) . '%');
}
// one word, so
else
{
$searchReady = $db->quote('%' . $db->escape($search) . '%');
$searchBucket[] = 'a.name LIKE ' . $searchReady;
$searchBucket[] = 'a.surname LIKE ' . $searchReady;
}
}
elseif (2 == $action)
{
if (strpos($search, '@') !== false)
{
foreach ($searchArray as $i => $searchString)
{
// only if string hints to be an email
if (strpos($searchString, '@') !== false)
{
// remove from array since it was an email
unset($searchArray[$i]);
break;
}
}
}
// not email with more then one word
if (count($searchArray) >= 2)
{
// get the name
$searchBucket[] = 'g.name LIKE ' . $db->quote('%' . $db->escape(array_shift($searchArray)) . '%');
$searchBucket[] = 'a.surname LIKE ' . $db->quote('%' . $db->escape( implode(' ', $searchArray) ) . '%');
}
// one word, so
else
{
$searchReady = $db->quote('%' . $db->escape($search) . '%');
$searchBucket[] = 'g.name LIKE ' . $searchReady;
$searchBucket[] = 'a.surname LIKE ' . $searchReady;
}
}
elseif (3 == $action)
2018-12-27 10:38:37 +00:00
{
2019-08-14 02:14:40 +00:00
if (strpos($search, '@') !== false)
{
foreach ($searchArray as $i => $searchString)
{
// only if string hints to be an email
if (strpos($searchString, '@') !== false)
{
// remove from array since it was an email
unset($searchArray[$i]);
break;
}
}
}
// not email with more then one word
if (count($searchArray) >= 2)
{
// get by token
$searchBucket[] = 'a.token LIKE ' . $db->quote('%' . $db->escape( implode(' ', $searchArray) ) . '%');
}
// one word, so
else
{
$searchReady = $db->quote('%' . $db->escape($search) . '%');
$searchBucket[] = 'a.token LIKE ' . $searchReady;
}
2018-12-27 10:38:37 +00:00
}
2019-08-14 02:14:40 +00:00
else
2018-12-27 10:38:37 +00:00
{
2019-08-14 02:14:40 +00:00
if (count($searchArray) > 1)
2018-12-27 10:38:37 +00:00
{
2019-08-14 02:14:40 +00:00
foreach ($searchArray as $i => $searchString)
2018-12-27 10:38:37 +00:00
{
2019-08-14 02:14:40 +00:00
$searchReady = $db->quote('%' . $db->escape($searchString) . '%');
// only if string hints to be an email
if (strpos($searchString, '@') !== false)
{
$searchBucket[] = 'g.email LIKE ' . $searchReady;
$searchBucket[] = 'a.email LIKE ' . $searchReady;
// remove from array since it was an email
unset($searchArray[$i]);
}
// only if string is longer then 3 characters
elseif (strlen($searchString) > 3)
{
$searchBucket[] = 'a.name LIKE ' . $searchReady;
$searchBucket[] = 'a.token LIKE ' . $searchReady;
$searchBucket[] = 'a.surname LIKE ' . $searchReady;
$searchBucket[] = 'g.name LIKE ' . $searchReady;
// if this is the first string in array then remove since it is probably the name
if ($i == 0)
{
unset($searchArray[$i]);
}
}
2018-12-27 10:38:37 +00:00
}
2019-08-14 02:14:40 +00:00
// load to surname as a whole
if (MembersmanagerHelper::checkArray($searchArray))
2018-12-27 10:38:37 +00:00
{
2019-08-14 02:14:40 +00:00
$searchBucket[] = 'a.surname LIKE ' . $db->quote('%' . $db->escape( implode(' ', $searchArray) ) . '%');
2018-12-27 10:38:37 +00:00
}
2019-08-14 02:14:40 +00:00
}
else
{
// the basic search
$searchString = $db->quote('%' . $db->escape($search) . '%');
$searchBucket[] = 'g.name LIKE ' . $searchString;
$searchBucket[] = 'g.username LIKE ' . $searchString;
// only if string hints to be an email
if (strpos($searchString, '@') !== false)
2018-12-27 10:38:37 +00:00
{
2019-08-14 02:14:40 +00:00
$searchBucket[] = 'g.email LIKE ' . $searchString;
$searchBucket[] = 'a.email LIKE ' . $searchString;
2018-12-27 10:38:37 +00:00
}
2019-08-14 02:14:40 +00:00
$searchBucket[] = 'a.name LIKE ' . $searchString;
$searchBucket[] = 'a.surname LIKE ' . $searchString;
$searchBucket[] = 'a.token LIKE ' . $searchString;
}
}
// load the search bucket
$query->where('('. implode(' OR ', $searchBucket) . ')');
}
// set query (to only get last 10)
$db->setQuery($query, 0, 10);
// get the members
$_members = $db->loadObjectList();
// did we get any
if (MembersmanagerHelper::checkArray($_members) && ($_members = $this->permissionFilter($_members, $user)) !== false)
{
// merger the data
$members = MembersmanagerHelper::mergeArrays(array($_members, $members));
// if we have 8 or more we return
if (count($members) >= 18)
{
return true;
}
}
// increment
$action++;
// try again
if ($action <= 4 )
{
$this->searching($search, $user, $members, $action);
}
return false;
}
protected function permissionFilter($members, &$user)
{
// if system admin, return all found
if ($user->authorise('core.options', 'com_membersmanager'))
{
return $members;
}
// filter by access type
$type_access = MembersmanagerHelper::getAccess($user);
// filter to only these access types
if (MembersmanagerHelper::checkArray($type_access))
{
// our little function to check if two arrays intersect on values
$intersect = function ($a, $b) { $b = array_flip($b); foreach ($a as $v) { if (isset($b[$v])) return true; } return false; };
// the new bucket
$member_bucket = array();
foreach ($members as $member)
{
// convert type json to array
if (MembersmanagerHelper::checkJson($member->type))
{
$member->type = json_decode($member->type, true);
}
// convert type int to array
if (is_numeric($member->type) && $member->type > 0)
{
$member->type = array($member->type);
2018-12-27 10:38:37 +00:00
}
2019-08-14 02:14:40 +00:00
// now check intersection
if (MembersmanagerHelper::checkArray($member->type) && $intersect($member->type, $type_access))
2018-12-27 10:38:37 +00:00
{
2019-08-14 02:14:40 +00:00
$member_bucket[] = $member;
2018-12-27 10:38:37 +00:00
}
}
2019-08-14 02:14:40 +00:00
// did we get any
if (MembersmanagerHelper::checkArray($member_bucket))
{
return $member_bucket;
}
2018-12-27 10:38:37 +00:00
}
return false;
2019-08-14 02:14:40 +00:00
}
// Used in profile
public function getReport($key)
2018-07-25 03:00:13 +00:00
{
2019-04-04 11:58:49 +00:00
// first we check if this is an allowed query
$view = $this->getViewID();
if (isset($view['a_view']) && 'profile' === $view['a_view'])
{
// unlock the request
if (($check = MembersmanagerHelper::unlock($key)) !== false &&
MembersmanagerHelper::checkArray($check) &&
isset($check['id']) && $check['id'] > 0 &&
isset($check['element']) && MembersmanagerHelper::checkString($check['element']) &&
isset($check['type']) && $check['type'] > 0 &&
isset($check['account']) && $check['account'] > 0)
2018-07-25 03:00:13 +00:00
{
2019-04-04 11:58:49 +00:00
// now check if this component is active to this member
if (($html = MembersmanagerHelper::getReport($check['id'], $check['element'])) !== false && MembersmanagerHelper::checkString($html))
{
return array('html' => $html);
}
}
}
return false;
}
public function getListMessages($key)
{
// first we check if this is an allowed query
$view = $this->getViewID();
if (isset($view['a_view']) && 'profile' === $view['a_view'])
{
// unlock the request
if (($check = MembersmanagerHelper::unlock($key)) !== false &&
MembersmanagerHelper::checkArray($check) &&
isset($check['id']) && $check['id'] > 0 &&
isset($check['return']) && MembersmanagerHelper::checkString($check['return']))
{
// now check if this component is active to this member
if (($html = MembersmanagerHelper::communicate('list_messages', $check['id'], $check['return'])) !== false && MembersmanagerHelper::checkString($html))
{
return array('html' => $html);
}
2018-07-25 03:00:13 +00:00
}
}
return false;
2018-07-25 03:00:13 +00:00
}
}