331 lines
7.0 KiB
Plaintext
331 lines
7.0 KiB
Plaintext
/**
|
|
* The Load class
|
|
*
|
|
* @var Load
|
|
* @since 2.0.1
|
|
*/
|
|
protected Load $load;
|
|
|
|
/**
|
|
* The Insert class
|
|
*
|
|
* @var Insert
|
|
* @since 2.0.1
|
|
*/
|
|
protected Insert $insert;
|
|
|
|
/**
|
|
* The Update class
|
|
*
|
|
* @var Update
|
|
* @since 2.0.1
|
|
*/
|
|
protected Update $update;
|
|
|
|
/**
|
|
* The Session class
|
|
*
|
|
* @var Session
|
|
* @since 2.0.1
|
|
*/
|
|
protected Session $session;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param Load $load The load object.
|
|
* @param Insert $insert The insert object.
|
|
* @param Update $update The update object.
|
|
* @param Session $session The session object.
|
|
*
|
|
* @since 2.0.1
|
|
*/
|
|
public function __construct(
|
|
Load $load,
|
|
Insert $insert,
|
|
Update $update,
|
|
Session $session)
|
|
{
|
|
$this->load = $load;
|
|
$this->insert = $insert;
|
|
$this->update = $update;
|
|
$this->session = $session;
|
|
}
|
|
|
|
/**
|
|
* Get Linker that has Access
|
|
*
|
|
* @return string|null Linker GUID that has access
|
|
* @since 2.0.1
|
|
**/
|
|
public function get(): ?string
|
|
{
|
|
if (($linker = $this->session->get('getbible_active_linker_guid', null)) === null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (($access = $this->session->get("getbible_active_{$linker}", null)) === null
|
|
|| $access !== 'valid_access')
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return $linker;
|
|
}
|
|
|
|
/**
|
|
* Get active Linker
|
|
*
|
|
* @param bool $setup The setup switch
|
|
*
|
|
* @return string|null Linker GUID that has access
|
|
* @since 2.0.1
|
|
**/
|
|
public function active(bool $setup = true): ?string
|
|
{
|
|
if (($linker = $this->session->get('getbible_active_linker_guid', null)) !== null)
|
|
{
|
|
return $linker;
|
|
}
|
|
|
|
if ($setup)
|
|
{
|
|
$guid = (string) GuidHelper::get();
|
|
$this->session->set('getbible_active_linker_guid', $guid);
|
|
|
|
return $guid;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Set Active Linker
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
*
|
|
* @return bool True on success
|
|
* @since 2.0.1
|
|
**/
|
|
public function set(string $linker): bool
|
|
{
|
|
if (!GuidHelper::valid($linker))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
$this->session->set('getbible_active_linker_guid', $linker);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Set Access
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
* @param string $pass The linker pass value
|
|
* @param string|null $oldPass The linker old pass value
|
|
*
|
|
* @return array|null The success or error details
|
|
* @since 2.0.1
|
|
**/
|
|
public function access(string $linker, string $pass, ?string $oldPass): ?array
|
|
{
|
|
// trim all empty space
|
|
$pass = trim($pass);
|
|
|
|
// password to short
|
|
if (strlen($pass) <= 3)
|
|
{
|
|
return [
|
|
'error' => Text::_('Password to short, use a longer password.')
|
|
];
|
|
}
|
|
// linker not valid GUID
|
|
elseif (!GuidHelper::valid($linker))
|
|
{
|
|
return [
|
|
'error' => Text::_('Invalid session key value.')
|
|
];
|
|
}
|
|
|
|
// get linker
|
|
if (($_linker = $this->load->item(['guid' => $linker],'linker')) !== null)
|
|
{
|
|
// publish the linker if needed
|
|
if ($_linker->published != 1 && $this->update->value(1, 'published', $_linker->id, 'id', 'linker'))
|
|
{
|
|
return [
|
|
'error' => Text::_('Access already exist, but could not be reactivated.')
|
|
];
|
|
}
|
|
|
|
if (!empty($oldPass))
|
|
{
|
|
|
|
$oldPass = trim($oldPass);
|
|
|
|
if (($guid = $this->getPassGuid($linker, $oldPass)) === null)
|
|
{
|
|
return [
|
|
'error' => Text::_('Incorrect favourite verse selected.')
|
|
];
|
|
}
|
|
|
|
if (!$this->setPassword($linker, $pass))
|
|
{
|
|
return [
|
|
'error' => Text::_('Favourite verse could not be changed.')
|
|
];
|
|
}
|
|
|
|
// unpublished the old pass word only if new password was set
|
|
$this->update->value(-2, 'published', $guid, 'guid', 'password');
|
|
}
|
|
elseif (!$this->hasAccess($linker, $pass))
|
|
{
|
|
return [
|
|
'error' => Text::_('Incorrect favourite verse selected.')
|
|
];
|
|
}
|
|
}
|
|
elseif (!$this->setLinker($linker))
|
|
{
|
|
return [
|
|
'error' => Text::_('Session key could not be stored.')
|
|
];
|
|
}
|
|
elseif (!$this->setPassword($linker, $pass))
|
|
{
|
|
return [
|
|
'error' => Text::_('Favourite verse could not be stored.')
|
|
];
|
|
}
|
|
elseif (($_linker = $this->load->item(['guid' => $linker],'linker')) === null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
$_linker->published = 1;
|
|
$_linker->success = Text::_('Favourite verse successfully set.');
|
|
|
|
// add to session
|
|
$this->session->set('getbible_active_linker_guid', $linker);
|
|
$this->session->set("getbible_active_{$linker}", 'valid_access');
|
|
|
|
return (array) $_linker;
|
|
}
|
|
|
|
/**
|
|
* Has Access
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
* @param string $pass The linker pass value
|
|
*
|
|
* @return bool True on success
|
|
* @since 2.0.1
|
|
**/
|
|
private function hasAccess(string $linker, string $pass): bool
|
|
{
|
|
if (($password = $this->getPassword($linker, $pass)) !== null)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Get Password GUID
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
* @param string $pass The linker pass value
|
|
*
|
|
* @return string|null The GUID on success
|
|
* @since 2.0.1
|
|
**/
|
|
private function getPassGuid(string $linker, string $pass): ?string
|
|
{
|
|
if (($password = $this->getPassword($linker, $pass)) !== null)
|
|
{
|
|
return $password->guid;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get Password
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
* @param string $pass The linker pass value
|
|
*
|
|
* @return object|null The GUID on success
|
|
* @since 2.0.1
|
|
**/
|
|
private function getPassword(string $linker, string $pass): ?object
|
|
{
|
|
if (strlen($pass) > 3 && ($passwords = $this->load->items(
|
|
['linker' => $linker, 'published' => 1],
|
|
'password'
|
|
)) !== null)
|
|
{
|
|
foreach ($passwords as $password)
|
|
{
|
|
if (UserHelper::verifyPassword($pass, $password->password))
|
|
{
|
|
return $password;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Set Linker
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
*
|
|
* @return bool True on success
|
|
* @since 2.0.1
|
|
**/
|
|
private function setLinker(string $linker): bool
|
|
{
|
|
return $this->insert->row([
|
|
'name' => 'Default-Name',
|
|
'guid' => $linker
|
|
], 'linker');
|
|
}
|
|
|
|
/**
|
|
* Set Password
|
|
*
|
|
* @param string $linker The linker GUID value
|
|
* @param string $pass The linker pass value
|
|
*
|
|
* @return bool True on success
|
|
* @since 2.0.1
|
|
**/
|
|
private function setPassword(string $linker, string $pass): bool
|
|
{
|
|
if (strlen($pass) <= 3)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
$guid = (string) GuidHelper::get();
|
|
while (!GuidHelper::valid($guid, 'password', 0, '[[[component]]]'))
|
|
{
|
|
// must always be set
|
|
$guid = (string) GuidHelper::get();
|
|
}
|
|
|
|
return $this->insert->row([
|
|
'name' => 'Favourite-Verse',
|
|
'linker' => $linker,
|
|
'password' => UserHelper::hashPassword($pass),
|
|
'guid' => $guid
|
|
], 'password');
|
|
} |