Initial commit

Functional, without SSO
This commit is contained in:
Jimmy Monin
2016-09-18 11:03:26 +02:00
commit 57708e3169
253 changed files with 30787 additions and 0 deletions

View File

@ -0,0 +1,88 @@
<?php
namespace App\Mail;
class Alert
{
public $email;
public $id;
public $title;
public $url;
public $interval = 30;
public $time_last_ad = 0;
public $time_updated = 0;
public $price_min = -1;
public $price_max = -1;
public $price_strict = false;
public $cities;
public $categories;
public $suspend = 0;
public $group = "";
public $group_ads = 1;
public $send_mail = 1;
public $send_sms_free_mobile = 0;
public $last_id = array();
public $max_id = 0;
public $send_sms_ovh = 0;
public $send_pushbullet = 0;
public $send_notifymyandroid = 0;
public $send_pushover = 0;
public function fromArray(array $values)
{
foreach ($values AS $key => $value) {
$this->$key = $value;
}
if (!is_numeric($this->group_ads)) {
$this->group_ads = 1;
}
/**
* Depuis 3.1, last_id contient les derniers IDs de la liste d'annonce
* et max_id l'ID max trouvé.
*/
if ($this->last_id && is_numeric($this->last_id) && !$this->max_id) {
$this->max_id = $this->last_id;
}
}
public function getCategories()
{
if ($this->categories && is_string($this->categories)) {
return explode(",", $this->categories);
}
if (is_array($this->categories)) {
return $this->categories;
}
return array();
}
public function toArray()
{
return array(
"email" => $this->email,
"id" => $this->id,
"title" => $this->title,
"url" => $this->url,
"interval" => $this->interval,
"time_last_ad" => $this->time_last_ad,
"time_updated" => $this->time_updated,
"price_min" => $this->price_min,
"price_max" => $this->price_max,
"price_strict" => $this->price_strict,
"cities" => $this->cities,
"suspend" => $this->suspend,
"group" => $this->group,
"group_ads" => $this->group_ads,
"categories" => $this->categories,
"send_mail" => $this->send_mail,
"send_sms_free_mobile" => $this->send_sms_free_mobile,
"last_id" => $this->last_id,
"max_id" => (int) $this->max_id,
"send_sms_ovh" => $this->send_sms_ovh,
"send_pushbullet" => $this->send_pushbullet,
"send_notifymyandroid" => $this->send_notifymyandroid,
"send_pushover" => $this->send_pushover
);
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Storage;
interface Alert
{
public function fetchAll();
public function fetchById($id);
public function save(\App\Mail\Alert $alert);
public function delete(\App\Mail\Alert $alert);
}

View File

@ -0,0 +1,141 @@
<?php
namespace App\Storage\Db;
class Alert implements \App\Storage\Alert
{
/**
* @var \mysqli
*/
protected $_connection;
protected $_table = "LBC_Alert";
/**
* @var \App\User\User
*/
protected $_user;
public function __construct(\mysqli $connection, \App\User\User $user)
{
$this->_connection = $connection;
$this->_user = $user;
}
public function fetchAll()
{
$alerts = array();
$alertsDb = $this->_connection->query("SELECT * FROM ".$this->_table
." WHERE user_id = ".$this->_user->getId());
while ($alertDb = $alertsDb->fetch_assoc()) {
$alert = new \App\Mail\Alert();
if (isset($alertDb["last_id"]) && !is_numeric($alertDb["last_id"])) {
$alertDb["last_id"] = json_decode($alertDb["last_id"], true);
if (!is_array($alertDb["last_id"])) {
$alertDb["last_id"] = array();
}
}
$alert->fromArray($alertDb);
$alert->id = $alertDb["idstr"];
$alerts[] = $alert;
}
return $alerts;
}
public function fetchById($id)
{
$alert = null;
$alertDb = $this->_connection->query(
"SELECT * FROM ".$this->_table." WHERE user_id = ".$this->_user->getId()."
AND idstr = '".$this->_connection->real_escape_string($id)."'")
->fetch_assoc();
if ($alertDb) {
$alert = new \App\Mail\Alert();
if (isset($alertDb["last_id"]) && !is_numeric($alertDb["last_id"])) {
$alertDb["last_id"] = json_decode($alertDb["last_id"], true);
if (!is_array($alertDb["last_id"])) {
$alertDb["last_id"] = array();
}
}
$alert->fromArray($alertDb);
$alert->id = $alertDb["idstr"];
}
return $alert;
}
public function save(\App\Mail\Alert $alert, $forceInsert = false)
{
$options = $alert->toArray();
if (is_array($options["last_id"])) {
$options["last_id"] = json_encode($options["last_id"]);
}
if (!$alert->id || $forceInsert) {
$options["user_id"] = $this->_user->getId();
if (!$alert->id) {
$id = sha1(uniqid());
$alert->id = $id;
}
$options["idstr"] = $alert->id;
unset($options["id"]);
$sqlOptions = array();
foreach ($options AS $name => $value) {
if ($value === null) {
$value = "NULL";
} elseif (is_bool($value)) {
$value = (int) $value;
} elseif (!is_numeric($value)) {
$value = "'".$this->_connection->real_escape_string($value)."'";
}
$sqlOptions[$name] = $value;
}
$this->_connection->query("INSERT INTO ".$this->_table.
" (`".implode("`, `", array_keys($options)).
"`, `date_created`) VALUES (".implode(", ", $sqlOptions).", NOW())");
} else {
$idStr = $options["id"];
$sqlOptions = array();
unset($options["id"]);
foreach ($options AS $name => $value) {
if ($value === null) {
$value = "NULL";
} elseif (is_bool($value)) {
$value = (int) $value;
} elseif (!is_numeric($value)) {
$value = "'".$this->_connection->real_escape_string($value)."'";
}
$sqlOptions[] = "`".$name."` = ".$value;
}
$this->_connection->query("UPDATE ".$this->_table." SET
".implode(",", $sqlOptions).
" WHERE idstr = '".$this->_connection->real_escape_string($idStr)."'");
}
return $this;
}
public function delete(\App\Mail\Alert $alert)
{
$this->_connection->query("DELETE FROM ".$this->_table."
WHERE idstr = '".$this->_connection->real_escape_string($alert->id)."'");
return $this;
}
/**
* @param \mysqli $dbConnection
* @return \App\Storage\Db\User
*/
public function setDbConnection($dbConnection)
{
$this->_connection = $dbConnection;
return $this;
}
/**
* @return \mysqli
*/
public function getDbConnection()
{
return $this->_connection;
}
}

View File

@ -0,0 +1,102 @@
<?php
namespace App\Storage\Db;
class User implements \App\Storage\User
{
/**
* @var \mysqli
*/
protected $_connection;
protected $_table = "LBC_User";
public function __construct(\mysqli $connection)
{
$this->_connection = $connection;
}
public function fetchAll()
{
$users = array();
$usersDb = $this->_connection->query("SELECT * FROM ".$this->_table);
while ($userDb = $usersDb->fetch_object()) {
$user = new \App\User\User();
$user->setId($userDb->id)
->setPassword($userDb->password)
->setUsername($userDb->username);
if (!empty($userDb->options)) {
$options = json_decode($userDb->options, true);
if (is_array($options)) {
$user->setOptions($options);
}
}
$users[] = $user;
}
return $users;
}
public function fetchByUsername($username)
{
$user = null;
$userDb = $this->_connection->query(
"SELECT * FROM ".$this->_table." WHERE username = '".
$this->_connection->real_escape_string($username)."'")
->fetch_object();
if ($userDb) {
$user = new \App\User\User();
$user->setId($userDb->id)
->setPassword($userDb->password)
->setUsername($userDb->username);
if (!empty($userDb->options)) {
$options = json_decode($userDb->options, true);
if (is_array($options)) {
$user->setOptions($options);
}
}
}
return $user;
}
public function save(\App\User\User $user)
{
if (!$this->fetchByUsername($user->getUsername())) {
$this->_connection->query("INSERT INTO `".$this->_table.
"` (`username`, `password`, `options`) VALUES (
'".$this->_connection->real_escape_string($user->getUsername())."',
'".$this->_connection->real_escape_string($user->getPassword())."',
'".$this->_connection->real_escape_string(json_encode($user->getOptions()))."'
)");
} else {
$this->_connection->query("UPDATE `".$this->_table."` SET
`password` = '".$this->_connection->real_escape_string($user->getPassword())."',
`options` = '".$this->_connection->real_escape_string(json_encode($user->getOptions()))."'
WHERE id = ".$user->getId());
}
return $this;
}
public function delete(\App\User\User $user)
{
$this->_connection->query("DELETE FROM ".$this->_table." WHERE id = ".$user->getId());
return $this;
}
/**
* @param \mysqli $dbConnection
* @return \App\Storage\Db\User
*/
public function setDbConnection($dbConnection)
{
$this->_connection = $dbConnection;
return $this;
}
/**
* @return \mysqli
*/
public function getDbConnection()
{
return $this->_connection;
}
}

View 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."'.");
}
}
}

View 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;
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Storage;
interface User
{
public function fetchAll();
public function fetchByUsername($username);
public function save(\App\User\User $user);
public function delete(\App\User\User $user);
}

View File

@ -0,0 +1,186 @@
<?php
namespace App;
class Updater
{
protected $_tmp_dir;
protected $_destination;
/**
* Adresse pour la récupération de la dernière version
* @var string
*/
protected $_url_version = "https://raw.githubusercontent.com/Blount/LBCAlerte/master/version.php";
/**
* Adresse pour récupérer l'archive ZIP.
* %VERSION% est remplacer parle numéro de version à télécharger.
* @var string
*/
protected $_url_archive = "https://github.com/Blount/LBCAlerte/archive/%VERSION%.zip";
public function __construct()
{
$this->_tmp_dir = DOCUMENT_ROOT."/var/tmp";
$this->_destination = DOCUMENT_ROOT;
}
/**
* @param string $tmp_dir
* @return Updater
*/
public function setTmpDir($tmp_dir)
{
$this->_tmp_dir = $tmp_dir;
return $this;
}
/**
* @return string
*/
public function getTmpDir()
{
return $this->_tmp_dir;
}
/**
* @param string $destination
* @return Updater
*/
public function setDestination($destination)
{
$this->_destination = $destination;
return $this;
}
/**
* @return string
*/
public function getDestination()
{
return $this->_destination;
}
/**
* @param string $url_version
* @return Updater
*/
public function setUrlVersion($url_version)
{
$this->_url_version = $url_version;
return $this;
}
/**
* @return string
*/
public function getUrlVersion()
{
return $this->_url_version;
}
/**
* @param string $url_archive
* @return Updater
*/
public function setUrlArchive($url_archive)
{
$this->_url_archive = $url_archive;
return $this;
}
/**
* @return string
*/
public function getUrlArchive()
{
return $this->_url_archive;
}
public function getLastVersion()
{
$lastVersion = file_get_contents($this->getUrlVersion());
if (preg_match('#return\s+"(.*)"\s*;#imsU', $lastVersion, $m)) {
return $m[1];
}
throw new \Exception("Impossible de récupérer la dernière version.");
}
public function installFiles($version)
{
$tmpZip = $this->_tmp_dir."/latest.zip";
if (!is_dir($this->_tmp_dir)) {
mkdir($this->_tmp_dir);
}
if (!is_writable($this->_tmp_dir)) {
throw new \Exception("Impossible d'écrire dans '".$this->_tmp_dir."'");
}
$archive = str_replace("%VERSION%", $version, $this->_url_archive);
if (!is_file($tmpZip) && !copy($archive, $tmpZip)) {
throw new \Exception("Impossible de récupérer l'archive.");
}
$zip = new \ZipArchive();
if (!$zip->open($tmpZip)) {
throw new \Exception("L'archive semble erronée.");
}
// extraire l'archive
$zip->extractTo($this->_tmp_dir);
$zip->close();
// mise à jour des fichiers.
$this->_copyFiles($this->_tmp_dir."/LBCAlerte-".$version, $this->_destination);
rmdir($this->_tmp_dir."/LBCAlerte-".$version);
unlink($tmpZip);
}
public function update($fromVersion, $toVersion)
{
// exécute les mises à jour
$directory = $this->_destination."/others/update";
if (is_dir($directory)) {
$filenames = scandir($directory);
$filenames_php = array();
foreach ($filenames AS $filename) {
if ($filename != "update.php" && false !== strpos($filename, ".php")) {
$filenames_php[basename($filename, ".php")] = $filename;
}
}
$versions = array_keys($filenames_php);
usort($versions, function ($a1, $a2) {
return version_compare($a1, $a2, "<") ? -1 : 1;
});
foreach ($versions AS $version) {
if (version_compare($fromVersion, $version, "<")
&& version_compare($toVersion, $version, ">=")) {
require $directory."/".$filenames_php[$version];
$class = "Update_".str_replace(".", "", $version);
if (class_exists($class, false)) {
$class = new $class();
$class->update();
}
}
}
}
}
protected function _copyFiles($dir, $to)
{
foreach (scandir($dir) AS $file) {
if ($file == "." || $file == "..") {
continue;
}
$destFile = $to."/".$file;
if (is_file($dir."/".$file)) {
rename($dir."/".$file, $destFile);
} elseif (is_dir($dir."/".$file)) {
if (!is_dir($destFile)) {
mkdir($destFile);
}
$this->_copyFiles($dir."/".$file, $destFile);
rmdir($dir."/".$file);
}
}
}
}

View File

@ -0,0 +1,190 @@
<?php
namespace App\User;
class User
{
protected $_id;
protected $_username;
protected $_password;
protected $_options = array();
protected $_optionsLoaded = false;
public function __construct(array $options = array())
{
if (isset($options["id"])) {
$this->setId($options["id"]);
}
if (isset($options["username"])) {
$this->setUsername($options["username"]);
}
if (isset($options["password"])) {
$this->setPassword($options["password"]);
}
}
/**
* @param int $id
* @return User
*/
public function setId($id)
{
$this->_id = $id;
return $this;
}
/**
* @return int
*/
public function getId()
{
if (!$this->_id) {
return md5($this->_username);
}
return $this->_id;
}
/**
* @param string $username
* @return User
*/
public function setUsername($username)
{
$this->_username = $username;
return $this;
}
/**
* @return string
*/
public function getUsername()
{
return $this->_username;
}
/**
* @param string $password
* @return User
*/
public function setPassword($password)
{
$this->_password = $password;
return $this;
}
/**
* @return string
*/
public function getPassword()
{
return $this->_password;
}
/**
* Retourne vrai si la notification SMS Free Mobile est activée.
* @return boolean
*/
public function hasSMSFreeMobile()
{
return false != $this->getOption("notification.freeMobile");
}
/**
* Retourne vrai si la notification SMS OVH est activée.
* @return boolean
*/
public function hasSMSOvh()
{
return false != $this->getOption("notification.ovh");
}
/**
* Retourne vrai si la notification Pushbullet est activée.
* @return boolean
*/
public function hasPushbullet()
{
return false != $this->getOption("notification.pushbullet");
}
/**
* Retourne vrai si la notification NotifyMyAndroid est activée.
* @return boolean
*/
public function hasNotifyMyAndroid()
{
return false != $this->getOption("notification.notifymyandroid");
}
/**
* Retourne vrai si la notification Pushover est activée.
* @return boolean
*/
public function hasPushover()
{
return false != $this->getOption("notification.pushover");
}
/**
* Indique si l'utilisateur est administrateur.
* @return boolean
*/
public function isAdmin()
{
return $this->getUsername() == "admin";
}
public function getOption($name, $default = null)
{
if (strpos($name, ".")) {
$options = explode(".", $name);
$nbOptions = count($options);
$current = $this->_options;
for ($i = 0; $i < $nbOptions; $i++) {
if (is_array($current) && isset($current[$options[$i]])) {
$current = $current[$options[$i]];
} else {
break;
}
}
if ($i == $nbOptions) {
return $current;
}
return $default;
}
return isset($this->_options[$name])?$this->_options[$name]:$default;
}
public function setOption($name, $value)
{
if (strpos($name, ".")) {
$options = explode(".", $name);
$nbOptions = count($options);
$current = $value;
for ($i = $nbOptions - 1; $i >= 0; $i--) {
$current = array($options[$i] => $current);
}
$this->_options = array_replace_recursive($this->_options, $current);
} else {
$this->_options[$name] = $value;
}
return $this;
}
public function getOptions()
{
return $this->_options;
}
public function setOptions(array $options)
{
$this->_options = $options;
return $this;
}
public function mergeOptions(array $options)
{
$this->_options = array_replace_recursive($this->_options, $options);
return $this;
}
}