Files
grocy/services/SessionService.php

51 lines
982 B
PHP
Raw Normal View History

2018-04-10 20:30:11 +02:00
<?php
2018-04-11 19:49:35 +02:00
namespace Grocy\Services;
class SessionService extends BaseService
2018-04-10 20:30:11 +02:00
{
/**
* @return boolean
*/
2018-04-11 19:49:35 +02:00
public function IsValidSession($sessionKey)
2018-04-10 20:30:11 +02:00
{
if ($sessionKey === null || empty($sessionKey))
{
return false;
}
else
{
return file_exists(__DIR__ . "/../data/sessions/$sessionKey.txt");
}
}
/**
* @return string
*/
2018-04-11 19:49:35 +02:00
public function CreateSession()
2018-04-10 20:30:11 +02:00
{
if (!file_exists(__DIR__ . '/../data/sessions'))
{
mkdir(__DIR__ . '/../data/sessions');
}
$now = time();
2018-04-11 19:49:35 +02:00
foreach (new \FilesystemIterator(__DIR__ . '/../data/sessions') as $file)
2018-04-10 20:30:11 +02:00
{
if ($now - $file->getCTime() >= 2678400) //31 days
{
unlink(__DIR__ . '/../data/sessions/' . $file->getFilename());
}
}
$newSessionKey = uniqid() . uniqid() . uniqid();
file_put_contents(__DIR__ . "/../data/sessions/$newSessionKey.txt", '');
return $newSessionKey;
}
2018-04-11 19:49:35 +02:00
public function RemoveSession($sessionKey)
2018-04-10 20:30:11 +02:00
{
unlink(__DIR__ . "/../data/sessions/$sessionKey.txt");
}
}