| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - e4j - Extensionsforjoomla.com |
| 6 |
* @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
/** |
| 14 |
* This class is used to handle the operators. |
| 15 |
* |
| 16 |
* @since 1.11 |
| 17 |
*/ |
| 18 |
class VikBookingOperator |
| 19 |
{ |
| 20 |
/** |
| 21 |
* The singleton instance of the class. |
| 22 |
* |
| 23 |
* @var VikBookingOperator |
| 24 |
*/ |
| 25 |
protected static $instance = null; |
| 26 |
|
| 27 |
/** |
| 28 |
* The database handler instance. |
| 29 |
* |
| 30 |
* @var object |
| 31 |
*/ |
| 32 |
protected $dbo; |
| 33 |
|
| 34 |
/** |
| 35 |
* Class constructor is protected. |
| 36 |
* |
| 37 |
* @see getInstance() |
| 38 |
*/ |
| 39 |
protected function __construct() |
| 40 |
{ |
| 41 |
$this->dbo = JFactory::getDbo(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the global Tracker object, either |
| 46 |
* a new instance or the existing instance |
| 47 |
* if the class was already instantiated. |
| 48 |
* |
| 49 |
* @return self A new instance of the class. |
| 50 |
*/ |
| 51 |
public static function getInstance() |
| 52 |
{ |
| 53 |
if (is_null(static::$instance)) { |
| 54 |
static::$instance = new static(); |
| 55 |
} |
| 56 |
|
| 57 |
return static::$instance; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Returns the list of all the operators. |
| 62 |
* |
| 63 |
* @return array The list of operators |
| 64 |
*/ |
| 65 |
public function getAll() |
| 66 |
{ |
| 67 |
$operators = array(); |
| 68 |
|
| 69 |
$q = "SELECT * FROM `#__vikbooking_operators` ORDER BY `first_name` ASC, `last_name` ASC;"; |
| 70 |
$this->dbo->setQuery($q); |
| 71 |
$this->dbo->execute(); |
| 72 |
if ($this->dbo->getNumRows()) { |
| 73 |
$all = $this->dbo->loadAssocList(); |
| 74 |
foreach ($all as $o) { |
| 75 |
$operators[$o['id']] = $o; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $operators; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns the list of all the operators filtered by a |
| 84 |
* specific permission type and with an array of its permissions. |
| 85 |
* |
| 86 |
* @param string $permtype the type of permission to look for |
| 87 |
* @param array [$list] the list of operators |
| 88 |
* |
| 89 |
* @return array The list of operators |
| 90 |
*/ |
| 91 |
public function getOperatorsFromPermissions($permtype, $list = array()) |
| 92 |
{ |
| 93 |
if (!count($list)) { |
| 94 |
$list = $this->getAll(); |
| 95 |
} |
| 96 |
|
| 97 |
$operators = $list; |
| 98 |
|
| 99 |
foreach ($operators as $k => $v) { |
| 100 |
$perms = !empty($v['perms']) ? json_decode($v['perms'], true) : array(); |
| 101 |
$perms = !is_array($perms) ? array() : $perms; |
| 102 |
$enabled = false; |
| 103 |
foreach ($perms as $perm) { |
| 104 |
if (isset($perm['type']) && $perm['type'] == $permtype) { |
| 105 |
$operators[$k]['perms'] = $perm['perms']; |
| 106 |
$enabled = true; |
| 107 |
break; |
| 108 |
} |
| 109 |
} |
| 110 |
if (!$enabled) { |
| 111 |
unset($operators[$k]); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $operators; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Tells whether the operator is logged in by renewing |
| 120 |
* the session cookie if the auth method is through code. |
| 121 |
* Returns the information of the logged in operator or false. |
| 122 |
* |
| 123 |
* @return mixed Array if the operator is logged in, false otherwise. |
| 124 |
*/ |
| 125 |
public function getOperatorAccount() |
| 126 |
{ |
| 127 |
$user = JFactory::getUser(); |
| 128 |
|
| 129 |
/** |
| 130 |
* @wponly because of the magic method __get() in JUser, we should not |
| 131 |
* let empty evaluate an internal property of the $user object, or this |
| 132 |
* will return false, even if the property is actually not empty. |
| 133 |
* Do not use !empty($user->id) or this will be false even if the user |
| 134 |
* is actually logged in. Assign $user->id to a variable instead. |
| 135 |
*/ |
| 136 |
$ujid = $user->id; |
| 137 |
|
| 138 |
if (!$user->guest && !empty($ujid)) { |
| 139 |
$q = "SELECT * FROM `#__vikbooking_operators` WHERE `ujid`=".(int)$user->id." LIMIT 1;"; |
| 140 |
$this->dbo->setQuery($q); |
| 141 |
$this->dbo->execute(); |
| 142 |
if ($this->dbo->getNumRows()) { |
| 143 |
// user is logged in to the site account |
| 144 |
return $this->dbo->loadAssoc(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$session = JFactory::getSession(); |
| 149 |
$sessval = $session->get('vboOpFngpt', ''); |
| 150 |
$cookie = JFactory::getApplication()->input->cookie; |
| 151 |
$cksess = $cookie->get('vboOpFngpt', '', 'string'); |
| 152 |
|
| 153 |
if (!empty($sessval)) { |
| 154 |
// look up for this fingerprint in the session |
| 155 |
$q = "SELECT * FROM `#__vikbooking_operators` WHERE `fingpt`=".$this->dbo->quote($sessval)." LIMIT 1;"; |
| 156 |
$this->dbo->setQuery($q); |
| 157 |
$this->dbo->execute(); |
| 158 |
if ($this->dbo->getNumRows()) { |
| 159 |
// user is logged in through the session |
| 160 |
return $this->dbo->loadAssoc(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if (!empty($cksess)) { |
| 165 |
// look up for this fingerprint in the cookie |
| 166 |
$q = "SELECT * FROM `#__vikbooking_operators` WHERE `fingpt`=".$this->dbo->quote($cksess)." LIMIT 1;"; |
| 167 |
$this->dbo->setQuery($q); |
| 168 |
$this->dbo->execute(); |
| 169 |
if ($this->dbo->getNumRows()) { |
| 170 |
// user is logged in through the cookie, renew it for 2 weeks before returning the array |
| 171 |
VikRequest::setCookie('vboOpFngpt', $cksess, (time() + (86400 * 14)), '/', '', false, true); |
| 172 |
return $this->dbo->loadAssoc(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// user is not logged in |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Login request through the authentication code. |
| 182 |
* If code is valid, the session and cookie are set. |
| 183 |
* |
| 184 |
* @param string $code the authentication code to check |
| 185 |
* |
| 186 |
* @return boolean true if the code exists, false otherwise |
| 187 |
*/ |
| 188 |
public function authOperator($code) |
| 189 |
{ |
| 190 |
if (empty($code)) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
$session = JFactory::getSession(); |
| 195 |
$cookie = JFactory::getApplication()->input->cookie; |
| 196 |
|
| 197 |
$q = "SELECT * FROM `#__vikbooking_operators` WHERE `code`=".$this->dbo->quote($code)." LIMIT 1;"; |
| 198 |
$this->dbo->setQuery($q); |
| 199 |
$this->dbo->execute(); |
| 200 |
if ($this->dbo->getNumRows()) { |
| 201 |
// valid code provided |
| 202 |
$operator = $this->dbo->loadAssoc(); |
| 203 |
|
| 204 |
// send cookie |
| 205 |
VikRequest::setCookie('vboOpFngpt', $operator['fingpt'], (time() + (86400 * 14)), '/', '', false, true); |
| 206 |
// update session value |
| 207 |
$session->set('vboOpFngpt', $operator['fingpt']); |
| 208 |
|
| 209 |
return true; |
| 210 |
} |
| 211 |
|
| 212 |
// invalid code provided |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Unsets the session value and sends an expired cookie. |
| 218 |
* This action completely logs out an operator that should log back in. |
| 219 |
* |
| 220 |
* @return void |
| 221 |
*/ |
| 222 |
public function logoutOperator() |
| 223 |
{ |
| 224 |
$app = JFactory::getApplication(); |
| 225 |
$session = JFactory::getSession(); |
| 226 |
$cookie = $app->input->cookie; |
| 227 |
|
| 228 |
$session->set('vboOpFngpt', ''); |
| 229 |
VikRequest::setCookie('vboOpFngpt', $operator['fingpt'], (time() - (86400 * 14)), '/', '', false, true); |
| 230 |
|
| 231 |
// logout from the site |
| 232 |
$app->logout(JFactory::getUser()->id); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Checks whether the given operator has the permissions to access |
| 237 |
* the given view type (permission type). Permissions are converted |
| 238 |
* into an array if still JSON encoded, and updated via reference. |
| 239 |
* |
| 240 |
* @param array $operator the operator record |
| 241 |
* @param string $permtype the name of the view to access |
| 242 |
* |
| 243 |
* @return boolean |
| 244 |
*/ |
| 245 |
public function checkPermissions(&$operator, $permtype) |
| 246 |
{ |
| 247 |
if (!is_array($operator) || !count($operator) || empty($operator['perms'])) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
if (is_scalar($operator['perms'])) { |
| 252 |
$operator['perms'] = json_decode($operator['perms'], true); |
| 253 |
$operator['perms'] = is_array($operator['perms']) ? $operator['perms'] : array(); |
| 254 |
} |
| 255 |
|
| 256 |
foreach ($operator['perms'] as $k => $perm) { |
| 257 |
if (isset($perm['type']) && $perm['type'] == $permtype) { |
| 258 |
// has permission for this View, take only these perms |
| 259 |
$operator['perms'] = $operator['perms'][$k]['perms']; |
| 260 |
return true; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
// permission not found |
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
|