mirror of
https://github.com/ZeJMaN/LBCAlerte_ynh.git
synced 2025-07-07 04:10:49 +02:00
Initial commit
Functional, without SSO
This commit is contained in:
167
sources/app/models/Storage/File/Alert.php
Normal file
167
sources/app/models/Storage/File/Alert.php
Normal file
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
|
||||
namespace App\Storage\File;
|
||||
|
||||
class Alert implements \App\Storage\Alert
|
||||
{
|
||||
protected $_filename;
|
||||
|
||||
protected $_header = array(
|
||||
"email",
|
||||
"id",
|
||||
"title",
|
||||
"url",
|
||||
"interval",
|
||||
"time_last_ad",
|
||||
"time_updated",
|
||||
"price_min",
|
||||
"price_max",
|
||||
"price_strict",
|
||||
"cities",
|
||||
"suspend",
|
||||
"group",
|
||||
"group_ads",
|
||||
"categories",
|
||||
"send_mail",
|
||||
"send_sms_free_mobile",
|
||||
"last_id",
|
||||
"max_id",
|
||||
"send_sms_ovh",
|
||||
"send_pushbullet",
|
||||
"send_notifymyandroid",
|
||||
"send_pushover"
|
||||
);
|
||||
|
||||
public function __construct($filename)
|
||||
{
|
||||
$this->_filename = $filename;
|
||||
$this->_checkFile();
|
||||
}
|
||||
|
||||
public function fetchAll()
|
||||
{
|
||||
$alerts = array();
|
||||
if (is_file($this->_filename)) {
|
||||
$fopen = fopen($this->_filename, "r");
|
||||
if ($header = fgetcsv($fopen, 0, ",", '"')) {
|
||||
$nb_columns = count($header);
|
||||
while (false !== $values = fgetcsv($fopen, 0, ",", '"')) {
|
||||
$alert = new \App\Mail\Alert();
|
||||
$options = array_combine(
|
||||
$header,
|
||||
array_slice($values, 0, count($header))
|
||||
);
|
||||
if (isset($options["last_id"]) && !is_numeric($options["last_id"])) {
|
||||
$options["last_id"] = json_decode($options["last_id"], true);
|
||||
if (!is_array($options["last_id"])) {
|
||||
$options["last_id"] = array();
|
||||
}
|
||||
}
|
||||
$alert->fromArray($options);
|
||||
$alerts[$alert->id] = $alert;
|
||||
}
|
||||
}
|
||||
fclose($fopen);
|
||||
}
|
||||
return $alerts;
|
||||
}
|
||||
|
||||
public function fetchById($id)
|
||||
{
|
||||
$alert = null;
|
||||
if (is_file($this->_filename)) {
|
||||
$fopen = fopen($this->_filename, "r");
|
||||
if ($header = fgetcsv($fopen, 0, ",", '"')) {
|
||||
while (false !== $values = fgetcsv($fopen, 0, ",", '"')) {
|
||||
$options = array_combine(
|
||||
$header,
|
||||
array_slice($values, 0, count($header))
|
||||
);
|
||||
if ($options["id"] == $id) {
|
||||
if (isset($options["last_id"]) && !is_numeric($options["last_id"])) {
|
||||
$options["last_id"] = json_decode($options["last_id"], true);
|
||||
if (!is_array($options["last_id"])) {
|
||||
$options["last_id"] = array();
|
||||
}
|
||||
}
|
||||
$alert = new \App\Mail\Alert();
|
||||
$alert->fromArray($options);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose($fopen);
|
||||
}
|
||||
return $alert;
|
||||
}
|
||||
|
||||
public function save(\App\Mail\Alert $alert)
|
||||
{
|
||||
$alerts = $this->fetchAll();
|
||||
$fopen = fopen($this->_filename, "a");
|
||||
flock($fopen, LOCK_EX);
|
||||
$fpNewFile = fopen($this->_filename.".new", "w");
|
||||
flock($fpNewFile, LOCK_EX);
|
||||
|
||||
fputcsv($fpNewFile, $this->_header, ",", '"');
|
||||
$updated = false;
|
||||
foreach ($alerts AS $a) {
|
||||
if ($a->id == $alert->id) {
|
||||
$a = $alert;
|
||||
$updated = true;
|
||||
}
|
||||
$data = $a->toArray();
|
||||
if (is_array($data["last_id"])) {
|
||||
$data["last_id"] = json_encode($data["last_id"]);
|
||||
}
|
||||
fputcsv($fpNewFile, $data, ",", '"');
|
||||
}
|
||||
if (!$updated && !$alert->id) {
|
||||
$alert->id = sha1(uniqid());
|
||||
fputcsv($fpNewFile, $alert->toArray(), ",", '"');
|
||||
}
|
||||
|
||||
fclose($fpNewFile);
|
||||
fclose($fopen);
|
||||
file_put_contents($this->_filename, file_get_contents($this->_filename.".new"));
|
||||
unlink($this->_filename.".new");
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(\App\Mail\Alert $alert)
|
||||
{
|
||||
$alerts = $this->fetchAll();
|
||||
$fopen = fopen($this->_filename, "a");
|
||||
flock($fopen, LOCK_EX);
|
||||
$fpNewFile = fopen($this->_filename.".new", "w");
|
||||
flock($fpNewFile, LOCK_EX);
|
||||
|
||||
fputcsv($fpNewFile, $this->_header, ",", '"');
|
||||
|
||||
unset($alerts[$alert->id]);
|
||||
foreach ($alerts AS $a) {
|
||||
fputcsv($fpNewFile, $a->toArray(), ",", '"');
|
||||
}
|
||||
|
||||
fclose($fpNewFile);
|
||||
fclose($fopen);
|
||||
file_put_contents($this->_filename, file_get_contents($this->_filename.".new"));
|
||||
unlink($this->_filename.".new");
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _checkFile()
|
||||
{
|
||||
if (empty($this->_filename)) {
|
||||
throw new \Exception("Un fichier doit être spécifié.");
|
||||
}
|
||||
$dir = dirname($this->_filename);
|
||||
if (!is_file($this->_filename)) {
|
||||
if (!is_writable($dir)) {
|
||||
throw new \Exception("Pas d'accès en écriture sur le répertoire '".$dir."'.");
|
||||
}
|
||||
} elseif (!is_writable($this->_filename)) {
|
||||
throw new \Exception("Pas d'accès en écriture sur le fichier '".$this->_filename."'.");
|
||||
}
|
||||
}
|
||||
}
|
145
sources/app/models/Storage/File/User.php
Normal file
145
sources/app/models/Storage/File/User.php
Normal file
@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace App\Storage\File;
|
||||
|
||||
class User implements \App\Storage\User
|
||||
{
|
||||
public function __construct($filename)
|
||||
{
|
||||
$this->_filename = $filename;
|
||||
$this->_checkFile();
|
||||
}
|
||||
|
||||
public function fetchAll()
|
||||
{
|
||||
$users = array();
|
||||
if (is_file($this->_filename)) {
|
||||
$fopen = fopen($this->_filename, "r");
|
||||
while (false !== $value = fgets($fopen)) {
|
||||
$value = trim($value);
|
||||
$user = new \App\User\User();
|
||||
$user->setPassword(substr($value, 0, 40))
|
||||
->setUsername(substr($value, 40));
|
||||
$this->_loadUserOptions($user);
|
||||
$users[] = $user;
|
||||
}
|
||||
fclose($fopen);
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
|
||||
public function fetchByUsername($username)
|
||||
{
|
||||
$user = null;
|
||||
if (is_file($this->_filename)) {
|
||||
$fopen = fopen($this->_filename, "r");
|
||||
while (false !== $value = fgets($fopen)) {
|
||||
$value = trim($value);
|
||||
if (substr($value, 40) == $username) {
|
||||
$user = new \App\User\User();
|
||||
$user->setPassword(substr($value, 0, 40))
|
||||
->setUsername($username);
|
||||
$this->_loadUserOptions($user);
|
||||
break;
|
||||
}
|
||||
}
|
||||
fclose($fopen);
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
public function save(\App\User\User $user)
|
||||
{
|
||||
if (!$this->fetchByUsername($user->getUsername()) || !is_file($this->_filename)) {
|
||||
$fopen = fopen($this->_filename, "a");
|
||||
fputs($fopen, $user->getPassword().$user->getUsername()."\n");
|
||||
fclose($fopen);
|
||||
} else {
|
||||
$fopen = fopen($this->_filename, "r");
|
||||
$fpNewFile = fopen($this->_filename.".new", "w");
|
||||
flock($fopen, LOCK_EX);
|
||||
while (false !== $value = fgets($fopen)) {
|
||||
$value = trim($value);
|
||||
if (substr($value, 40) == $user->getUsername()) {
|
||||
$value = $user->getPassword().$user->getUsername();
|
||||
}
|
||||
fputs($fpNewFile, $value."\n");
|
||||
}
|
||||
fclose($fopen);
|
||||
fclose($fpNewFile);
|
||||
file_put_contents($this->_filename, file_get_contents($this->_filename.".new"));
|
||||
unlink($this->_filename.".new");
|
||||
$this->_saveUserOptions($user);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(\App\User\User $user)
|
||||
{
|
||||
if (is_file($this->_filename)) {
|
||||
$fopen = fopen($this->_filename, "r");
|
||||
$fpNewFile = fopen($this->_filename.".new", "w");
|
||||
while (false !== $value = fgets($fopen)) {
|
||||
$value = trim($value);
|
||||
if (substr($value, 40) != $user->getUsername()) {
|
||||
fputs($fpNewFile, $value."\n");
|
||||
}
|
||||
}
|
||||
fclose($fopen);
|
||||
fclose($fpNewFile);
|
||||
file_put_contents($this->_filename, file_get_contents($this->_filename.".new"));
|
||||
unlink($this->_filename.".new");
|
||||
$this->_deleteUserOptions($user);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _checkFile()
|
||||
{
|
||||
if (empty($this->_filename)) {
|
||||
throw new \Exception("Un fichier doit être spécifié.");
|
||||
}
|
||||
$dir = dirname($this->_filename);
|
||||
if (!is_file($this->_filename)) {
|
||||
if (!is_writable($dir)) {
|
||||
throw new \Exception("Pas d'accès en écriture sur le répertoire '".$dir."'.");
|
||||
}
|
||||
} elseif (!is_writable($this->_filename)) {
|
||||
throw new \Exception("Pas d'accès en écriture sur le fichier '".$this->_filename."'.");
|
||||
}
|
||||
}
|
||||
|
||||
protected function _loadUserOptions(\App\User\User $user)
|
||||
{
|
||||
$dir = DOCUMENT_ROOT.DS."var".DS."configs";
|
||||
$filename = $dir.DS."user_".$user->getUsername().".json";
|
||||
if (is_file($filename)) {
|
||||
$data = json_decode(trim(file_get_contents($filename)), true);
|
||||
if ($data && is_array($data)) {
|
||||
$user->setOptions($data);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _saveUserOptions(\App\User\User $user)
|
||||
{
|
||||
$dir = DOCUMENT_ROOT.DS."var".DS."configs";
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir);
|
||||
}
|
||||
$filename = $dir.DS."user_".$user->getUsername().".json";
|
||||
file_put_contents($filename, json_encode($user->getOptions()));
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function _deleteUserOptions(\App\User\User $user)
|
||||
{
|
||||
$dir = DOCUMENT_ROOT.DS."var".DS."configs";
|
||||
$filename = $dir.DS."user_".$user->getUsername().".json";
|
||||
if (is_file($filename)) {
|
||||
unlink($filename);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user