Upgrade LBCAlerte to version 3.3

Add upgrade script
This commit is contained in:
Jimmy Monin
2016-11-26 19:19:16 +01:00
parent a7c054b535
commit 58ffd500e6
89 changed files with 6436 additions and 758 deletions

View File

@ -0,0 +1,16 @@
<?php
namespace App\Storage;
use App\Ad\Ad as AdItem;
interface Ad
{
public function fetchAll();
public function fetchById($id);
public function save(AdItem $ad);
public function delete(AdItem $ad);
}

View File

@ -0,0 +1,57 @@
<?php
namespace App\Storage;
use App\Ad\Ad as AdItem;
class AdPhoto
{
protected $_user;
public function __construct(\App\User\User $user)
{
$this->_user = $user;
}
public function getPublicDestination($filename = null)
{
$destination = "static/media/annonce/".$this->_user->getUsername();
if ($filename) {
$destination .= "/".$filename;
}
return $destination;
}
public function getDestination()
{
return DOCUMENT_ROOT."/static/media/annonce/".$this->_user->getUsername();
}
public function import(AdItem $ad, $override = false)
{
$destination = $this->getDestination();
if (!is_dir($destination) && !mkdir($destination)) {
return false;
}
foreach ($ad->getPhotos() AS $photo) {
$filename = $destination."/".$photo["local"];
if (!is_file($filename) || $override) {
copy($photo["remote"], $filename);
}
}
return true;
}
public function delete(AdItem $ad)
{
$destination = $this->getDestination();
foreach ($ad->getPhotos() AS $photo) {
$filename = $destination."/".$photo["local"];
if (is_file($filename)) {
unlink($filename);
}
}
}
}

View File

@ -0,0 +1,154 @@
<?php
namespace App\Storage\Db;
use App\Ad\Ad as AdItem;
class Ad implements \App\Storage\Ad
{
/**
* @var \mysqli
*/
protected $_connection;
protected $_table = "LBC_BackupAd";
/**
* @var \App\User\User
*/
protected $_user;
public function __construct(\mysqli $connection, \App\User\User $user)
{
$this->_connection = $connection;
$this->_user = $user;
}
public function fetchAll($order = null)
{
$ads = array();
$query = "SELECT * FROM ".$this->_table
." WHERE user_id = ".$this->_user->getId();
if ($order) {
if (is_string($order)) {
$query .= " ORDER By ".$order;
} elseif (is_array($order)) {
$query .= implode(", ", $order);
}
}
$adsDb = $this->_connection->query($query);
while ($adDb = $adsDb->fetch_assoc()) {
foreach (array("photos", "properties") AS $key) {
$adDb[$key] = json_decode($adDb[$key], true);
if (!$adDb[$key]) {
$adDb[$key] = array();
}
}
$ad = new AdItem();
$ad->setFromArray($adDb);
$ads[] = $ad;
}
return $ads;
}
public function fetchById($id)
{
$ad = null;
$adDb = $this->_connection->query(
"SELECT * FROM ".$this->_table." WHERE user_id = ".$this->_user->getId()."
AND id = '".$this->_connection->real_escape_string($id)."'")
->fetch_assoc();
if ($adDb) {
foreach (array("photos", "properties") AS $key) {
$adDb[$key] = json_decode($adDb[$key], true);
if (!$adDb[$key]) {
$adDb[$key] = array();
}
}
$ad = new AdItem();
$ad->setFromArray($adDb);
}
return $ad;
}
public function save(AdItem $ad)
{
$options = $ad->toArray();
$options["photos"] = json_encode($options["photos"]);
$options["properties"] = json_encode($options["properties"]);
if (!$ad->getAid()) {
$options["user_id"] = $this->_user->getId();
unset($options["aid"]);
if (empty($options["date_created"])) {
$options["date_created"] = date("Y-m-d H:i:s");
}
$sqlOptions = array();
foreach ($options AS $name => $value) {
if ($value === null) {
$value = "NULL";
} elseif (is_bool($value)) {
$value = (int) $value;
} else {
$value = "'".$this->_connection->real_escape_string($value)."'";
}
$sqlOptions[$name] = $value;
}
$this->_connection->query("INSERT INTO ".$this->_table.
" (`".implode("`, `", array_keys($options)).
"`) VALUES (".implode(", ", $sqlOptions).")");
if ($this->_connection->error) {
var_dump($this->_connection->error);
exit;
}
} else {
$sqlOptions = array();
unset($options["aid"]);
foreach ($options AS $name => $value) {
if ($value === null) {
$value = "NULL";
} elseif (is_bool($value)) {
$value = (int) $value;
} else {
$value = "'".$this->_connection->real_escape_string($value)."'";
}
$sqlOptions[] = "`".$name."` = ".$value;
}
$this->_connection->query("UPDATE ".$this->_table." SET
".implode(",", $sqlOptions).
" WHERE `aid` = ".$ad->getAid());
if ($this->_connection->error) {
var_dump($this->_connection->error);
exit;
}
}
return $this;
}
public function delete(AdItem $ad)
{
$this->_connection->query("DELETE FROM ".$this->_table."
WHERE `aid` = ".$ad->getAid());
return $this;
}
/**
* @param \mysqli $dbConnection
* @return \App\Storage\Db\Ad
*/
public function setDbConnection($dbConnection)
{
$this->_connection = $dbConnection;
return $this;
}
/**
* @return \mysqli
*/
public function getDbConnection()
{
return $this->_connection;
}
}

View File

@ -24,13 +24,9 @@ class User implements \App\Storage\User
$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);
}
}
->setUsername($userDb->username)
->setApiKey($userDb->api_key);
$this->_loadUserOptions($user, $userDb->options);
$users[] = $user;
}
return $users;
@ -47,29 +43,56 @@ class User implements \App\Storage\User
$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);
}
}
->setUsername($userDb->username)
->setApiKey($userDb->api_key);
$this->_loadUserOptions($user, $userDb->options);
}
return $user;
}
protected function _loadUserOptions(\App\User\User $user, $options)
{
if (empty($options)) {
return $this;
}
$options = json_decode($options, true);
if (!is_array($options)) {
return $this;
}
if (!empty($options["notification"]) && is_array($options["notification"])) {
foreach ($options["notification"] AS $key => $params) {
if ($params && !isset($params["active"])) {
$options["notification"][$key]["active"] = true;
}
}
}
$user->setOptions($options);
return $this;
}
public function save(\App\User\User $user)
{
if (!$api_key = $user->getApiKey()) {
$api_key = "NULL";
} else {
$api_key = "'".$this->_connection->real_escape_string($api_key)."'";
}
if (!$this->fetchByUsername($user->getUsername())) {
$this->_connection->query("INSERT INTO `".$this->_table.
"` (`username`, `password`, `options`) VALUES (
"` (`username`, `password`, `api_key`, `options`) VALUES (
'".$this->_connection->real_escape_string($user->getUsername())."',
'".$this->_connection->real_escape_string($user->getPassword())."',
".$api_key.",
'".$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())."',
`api_key` = ".$api_key.",
`options` = '".$this->_connection->real_escape_string(json_encode($user->getOptions()))."'
WHERE id = ".$user->getId());
}

View File

@ -0,0 +1,205 @@
<?php
namespace App\Storage\File;
use App\Ad\Ad as AdItem;
class Ad implements \App\Storage\Ad
{
protected $_filename;
public function __construct($filename)
{
$this->_filename = $filename;
$this->_checkFile();
}
public function fetchAll($order = null)
{
$ads = 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, ",", '"')) {
$ad = new AdItem();
$options = array_combine(
$header,
array_slice($values, 0, count($header))
);
if (!empty($options["photos"])) {
$options["photos"] = json_decode($options["photos"], true);
}
if (!empty($options["properties"])) {
$options["properties"] = json_decode($options["properties"], true);
}
$ad->setFromArray($options);
$ads[$ad->getId()] = $ad;
}
}
fclose($fopen);
}
if (null !== $order) {
if (is_array($order)) {
$order = array_shift($order);
}
if (is_string($order) && preg_match("#(?<sort>.+)\s+(?<order>asc|desc)#i", $order, $m)) {
$sort = $m["sort"];
$order = strtolower($m["order"]);
$method = "get".str_replace(" ", "", ucwords(str_replace("_", " ", $sort)));
if (!method_exists(new \App\Ad\Ad(), $method)) {
unset($sort, $order, $method);
}
}
if (isset($sort)) {
setlocale(LC_CTYPE, "fr_FR.UTF-8");
usort($ads, function ($ad1, $ad2) use ($sort, $method) {
$param1 = mb_strtolower($ad1->$method());
$param2 = mb_strtolower($ad2->$method());
if ($sort == "title" && function_exists("iconv")) {
$param1 = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $param1);
$param2 = iconv("UTF-8", "ASCII//TRANSLIT//IGNORE", $param2);
}
if ($param1 < $param2) {
return -1;
}
if ($param1 > $param2) {
return 1;
}
return 0;
});
if ($order == "desc") {
$ads = array_reverse($ads);
}
}
}
return $ads;
}
public function fetchById($id)
{
$ad = 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) {
$ad = new AdItem();
if (!empty($options["photos"])) {
$options["photos"] = json_decode($options["photos"], true);
}
if (!empty($options["properties"])) {
$options["properties"] = json_decode($options["properties"], true);
}
$ad->setFromArray($options);
break;
}
}
}
fclose($fopen);
}
return $ad;
}
public function save(AdItem $ad)
{
$ads = $this->fetchAll();
$fopen = fopen($this->_filename, "a");
flock($fopen, LOCK_EX);
$fpNewFile = fopen($this->_filename.".new", "w");
flock($fpNewFile, LOCK_EX);
// Entête du fichier CSV
$headers = array_keys($ad->toArray());
fputcsv($fpNewFile, $headers, ",", '"');
$updated = false;
foreach ($ads AS $a) {
if ($a->getId() == $ad->getId()) {
$a = $ad;
$updated = true;
}
$data = $a->toArray();
$data["photos"] = json_encode($data["photos"]);
$data["properties"] = json_encode($data["properties"]);
if (empty($data["date_created"])) {
$data["date_created"] = date("Y-m-d H:i:s");
}
fputcsv($fpNewFile, $data, ",", '"');
}
if (!$updated) {
$data = $ad->toArray();
$data["photos"] = json_encode($data["photos"]);
$data["properties"] = json_encode($data["properties"]);
if (empty($data["date_created"])) {
$data["date_created"] = date("Y-m-d H:i:s");
}
fputcsv($fpNewFile, $data, ",", '"');
}
fclose($fpNewFile);
fclose($fopen);
file_put_contents($this->_filename, file_get_contents($this->_filename.".new"));
unlink($this->_filename.".new");
return $this;
}
public function delete(AdItem $ad)
{
$ads = $this->fetchAll();
$fopen = fopen($this->_filename, "a");
flock($fopen, LOCK_EX);
$fpNewFile = fopen($this->_filename.".new", "w");
flock($fpNewFile, LOCK_EX);
// Entête du fichier CSV
$headers = array_keys($ad->toArray());
fputcsv($fpNewFile, $headers, ",", '"');
unset($ads[$ad->getId()]);
foreach ($ads AS $a) {
$data = $a->toArray();
$data["photos"] = json_encode($data["photos"]);
$data["properties"] = json_encode($data["properties"]);
fputcsv($fpNewFile, $data, ",", '"');
}
fclose($fpNewFile);
fclose($fopen);
file_put_contents($this->_filename, file_get_contents($this->_filename.".new"));
unlink($this->_filename.".new");
// Si aucune annonce trouvée, on supprime le fichier CSV
$ads = $this->fetchAll();
if (0 == count($ads) && is_file($this->_filename)) {
unlink($this->_filename);
}
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

@ -29,7 +29,9 @@ class Alert implements \App\Storage\Alert
"send_sms_ovh",
"send_pushbullet",
"send_notifymyandroid",
"send_pushover"
"send_pushover",
"send_joaoappsjoin",
"send_slack",
);
public function __construct($filename)

View File

@ -115,7 +115,18 @@ class User implements \App\Storage\User
$filename = $dir.DS."user_".$user->getUsername().".json";
if (is_file($filename)) {
$data = json_decode(trim(file_get_contents($filename)), true);
if (isset($data["api_key"])) {
$user->setApiKey($data["api_key"]);
unset($data["api_key"]);
}
if ($data && is_array($data)) {
if (!empty($data["notification"]) && is_array($data["notification"])) {
foreach ($data["notification"] AS $key => $params) {
if ($params && !isset($params["active"])) {
$data["notification"][$key]["active"] = true;
}
}
}
$user->setOptions($data);
}
}
@ -129,7 +140,11 @@ class User implements \App\Storage\User
mkdir($dir);
}
$filename = $dir.DS."user_".$user->getUsername().".json";
file_put_contents($filename, json_encode($user->getOptions()));
$data = $user->getOptions();
if ($api_key = $user->getApiKey()) {
$data["api_key"] = $api_key;
}
file_put_contents($filename, json_encode($data));
return $this;
}