Skip to content

Commit

Permalink
Move more queries (comments, config) to respective repository. Cope
Browse files Browse the repository at this point in the history
with #20
  • Loading branch information
nikrou committed Oct 2, 2018
1 parent f721b83 commit a2406c3
Show file tree
Hide file tree
Showing 19 changed files with 312 additions and 141 deletions.
3 changes: 2 additions & 1 deletion admin/configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
define('CONFIGURATION_BASE_URL', \Phyxo\Functions\URL::get_root_url() . 'admin/index.php?page=configuration');

use Phyxo\TabSheet\TabSheet;
use App\Repository\ConfigRepository;

// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok |
Expand Down Expand Up @@ -229,7 +230,7 @@

// updating configuration if no error found
if (!in_array($page['section'], ['sizes', 'watermark']) and count($page['errors']) == 0) {
$result = $conn->db_query('SELECT param FROM ' . CONFIG_TABLE);
$result = (new ConfigRepository($conn))->findAll();
while ($row = $conn->db_fetch_assoc($result)) {
if (isset($_POST[$row['param']])) {
$value = $_POST[$row['param']];
Expand Down
20 changes: 10 additions & 10 deletions admin/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}

use Phyxo\Template\FileCombiner;
use App\Repository\UserFeedRepository;

// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok |
Expand Down Expand Up @@ -41,7 +42,7 @@
case 'unlock_gallery':
{
$conf['gallery_locked'] = false;
$_SESSION['page_infos'] = array(\Phyxo\Functions\Language::l10n('Gallery unlocked'));
$_SESSION['page_infos'] = [\Phyxo\Functions\Language::l10n('Gallery unlocked')];
\Phyxo\Functions\Utils::redirect(\Phyxo\Functions\URL::get_root_url() . 'admin/index.php?page=maintenance');
break;
}
Expand Down Expand Up @@ -91,8 +92,7 @@
}
case 'feeds':
{
$query = 'DELETE FROM ' . USER_FEED_TABLE . ' WHERE last_check IS NULL;';
$conn->db_query($query);
(new UserFeedRepository($conn))->deleteUserFeedNotChecked();
break;
}
case 'database':
Expand Down Expand Up @@ -141,7 +141,7 @@
$purge_urls[\Phyxo\Functions\Language::l10n(IMG_CUSTOM)] = sprintf($url_format, 'derivatives') . '&type=' . IMG_CUSTOM;

$template->assign(
array(
[
'U_MAINT_CATEGORIES' => sprintf($url_format, 'categories'),
'U_MAINT_IMAGES' => sprintf($url_format, 'images'),
'U_MAINT_ORPHAN_TAGS' => sprintf($url_format, 'delete_orphan_tags'),
Expand All @@ -156,29 +156,29 @@
'U_MAINT_DERIVATIVES' => sprintf($url_format, 'derivatives'),
'purge_derivatives' => $purge_urls,
//'U_HELP' => \Phyxo\Functions\URL::get_root_url().'admin/popuphelp.php?page=maintenance',
)
]
);


if ($conf['gallery_locked']) {
$template->assign(
array(
[
'U_MAINT_UNLOCK_GALLERY' => sprintf($url_format, 'unlock_gallery'),
)
]
);
} else {
$template->assign(
array(
[
'U_MAINT_LOCK_GALLERY' => sprintf($url_format, 'lock_gallery'),
)
]
);
}

// +-----------------------------------------------------------------------+
// | Define advanced features |
// +-----------------------------------------------------------------------+

$advanced_features = array();
$advanced_features = [];

//$advanced_features is array of array composed of CAPTION & URL
$advanced_features = \Phyxo\Functions\Plugin::trigger_change(
Expand Down
8 changes: 5 additions & 3 deletions admin/notification_by_mail_params.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
die("Hacking attempt!");
}

use App\Repository\ConfigRepository;

if (isset($_POST['param_submit'])) {
$updated_param_count = 0;
// Update param
$result = $conn->db_query('select param, value from ' . CONFIG_TABLE . ' where param like \'nbm\\_%\'');
$result = (new ConfigRepository($conn))->findAll('param like \'nbm\\_%\'');
while ($nbm_user = $conn->db_fetch_assoc($result)) {
if (isset($_POST[$nbm_user['param']])) {
$value = $_POST[$nbm_user['param']];
Expand All @@ -36,11 +38,11 @@
$conf->loadFromDB('param like \'nbm\\_%\'');
}

$template->assign(array(
$template->assign([
'SEND_HTML_MAIL' => $conf['nbm_send_html_mail'],
'SEND_MAIL_AS' => $conf['nbm_send_mail_as'],
'SEND_DETAILED_CONTENT' => $conf['nbm_send_detailed_content'],
'COMPLEMENTARY_MAIL_CONTENT' => $conf['nbm_complementary_mail_content'],
'SEND_RECENT_POST_DATES' => $conf['nbm_send_recent_post_dates'],
'F_ACTION' => NOTIFICATION_BY_MAIL_BASE_URL . '&section=params'
));
]);
9 changes: 3 additions & 6 deletions include/section_init.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Repository\TagRepository;
use App\Repository\CategoryRepository;
use App\Repository\FavoriteRepository;
use App\Repository\ImageRepository;

/**
* This included page checks section related parameter and provides
Expand Down Expand Up @@ -291,12 +292,8 @@
(new FavoriteRepository($conn))->removeAllFavorites($user['id']);
\Phyxo\Functions\Utils::redirect(\Phyxo\Functions\URL::make_index_url(['section' => 'favorites']));
} else {
$query = 'SELECT image_id FROM ' . IMAGES_TABLE;
$query .= ' LEFT JOIN ' . FAVORITES_TABLE . ' ON image_id = id';
$query .= ' WHERE user_id = ' . $user['id'];
$query .= ' ' . \Phyxo\Functions\SQL::get_sql_condition_FandF(['visible_images' => 'id'], 'AND');
$query .= ' ' . $conf['order_by'];
$page = array_merge($page, ['items' => $conn->query2array($query, null, 'image_id')]);
$result = (new ImageRepository($conn))->getFavorites($user['id'], $conf['order_by']);
$page = array_merge($page, ['items' => $conn->result2array($result, null, 'image_id')]);

if (count($page['items']) > 0) {
$template->assign(
Expand Down
21 changes: 9 additions & 12 deletions src/LegacyPages/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
define('PHPWG_ROOT_PATH', '../../');
include_once(PHPWG_ROOT_PATH . 'include/common.inc.php');

use App\Repository\UserFeedRepository;

// +-----------------------------------------------------------------------+
// | functions |
// +-----------------------------------------------------------------------+
Expand Down Expand Up @@ -54,9 +56,8 @@ function ts_to_iso8601($ts)
$image_only = isset($_GET['image_only']);

if (!empty($feed_id)) {
$query = 'SELECT user_id,last_check FROM ' . USER_FEED_TABLE;
$query .= ' WHERE id = \'' . $conn->db_real_escape_string($feed_id) . '\'';
$feed_row = $conn->db_fetch_assoc($conn->db_query($query));
$result = (new UserFeedRepository($conn))->findById($feed_id);
$feed_row = $conn->db_fetch_assoc($result);
if (empty($feed_row)) {
\Phyxo\Functions\HTTP::page_not_found(\Phyxo\Functions\Language::l10n('Unknown feed identifier'));
}
Expand Down Expand Up @@ -87,7 +88,7 @@ function ts_to_iso8601($ts)
// | Feed creation |
// +-----------------------------------------------------------------------+

$news = array();
$news = [];
if (!$image_only) {
$news = \Phyxo\Functions\Notification::news($feed_row['last_check'], $dbnow, true, true);

Expand All @@ -110,17 +111,13 @@ function ts_to_iso8601($ts)

$rss->addItem($item);

$query = 'UPDATE ' . USER_FEED_TABLE . ' SET last_check = \'' . $dbnow;
$query .= '\' where id = \'' . $conn->db_real_escape_string($feed_id) . '\'';
$conn->db_query($query);
(new UserFeedRepository($conn))->updateUserFeed(['last_check' => $dbnow], $feed_id);
}
}

if (!empty($feed_id) and empty($news)) {// update the last check from time to time to avoid deletion by maintenance tasks
if (!isset($feed_row['last_check']) or time() - datetime_to_ts($feed_row['last_check']) > 30 * 24 * 3600) {
$query = 'UPDATE ' . USER_FEED_TABLE . ' SET last_check = ' . $conn->db_get_recent_period_expression(-15, $dbnow);
$query .= ' WHERE id = \'' . $conn->db_real_escape_string($feed_id) . '\'';
$conn->db_query($query);
(new UserFeedRepository($conn))->updateUserFeed(['last_check' => $conn->db_get_recent_period_expression(-15, $dbnow)], $feed_id);
}
}

Expand All @@ -131,12 +128,12 @@ function ts_to_iso8601($ts)
$date = $date_detail['date_available'];
$item->title = \Phyxo\Functions\Notification::get_title_recent_post_date($date_detail);
$item->link = \Phyxo\Functions\URL::make_index_url(
array(
[
'chronology_field' => 'posted',
'chronology_style' => 'monthly',
'chronology_view' => 'calendar',
'chronology_date' => explode('-', substr($date, 0, 10))
)
]
);

$item->description .= '<a href="' . \Phyxo\Functions\URL::make_index_url() . '">' . $conf['gallery_title'] . '</a><br> ';
Expand Down
11 changes: 6 additions & 5 deletions src/LegacyPages/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Phyxo\Template\Template;
use Phyxo\Session\SessionDbHandler;
use App\Repository\SiteRepository;
use App\Repository\ConfigRepository;

// container
if (!empty($GLOBALS['container'])) {
Expand Down Expand Up @@ -211,11 +212,11 @@
$conf->loadFromFile(PHPWG_ROOT_PATH . 'local/config/config.inc.php');
$conf['dblayer'] = $dblayer;

$query = 'INSERT INTO ' . CONFIG_TABLE . ' (param,value,comment)';
$query .= 'VALUES (\'secret_key\',';
$query .= 'md5(' . $conn->db_cast_to_text($conn::RANDOM_FUNCTION . '()') . '),';
$query .= '\'a secret key specific to the gallery for internal use\')';
$conn->db_query($query);
(new ConfigRepository($conn))->addParam(
'secret_key',
md5(openssl_random_pseudo_bytes(15)),
'\'a secret key specific to the gallery for internal use\')'
);

$conf['phyxo_db_version'] = \Phyxo\Functions\Utils::get_branch_from_version(PHPWG_VERSION);
$conf['gallery_title'] = \Phyxo\Functions\Language::l10n('Just another Phyxo gallery');
Expand Down
7 changes: 3 additions & 4 deletions src/LegacyPages/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
define('PHPWG_ROOT_PATH', '../../');
include_once(PHPWG_ROOT_PATH . 'include/common.inc.php');

use App\Repository\UserFeedRepository;

// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok |
// +-----------------------------------------------------------------------+
Expand All @@ -28,10 +30,7 @@
// +-----------------------------------------------------------------------+

$page['feed'] = md5(uniqid(true));

$query = 'INSERT INTO ' . USER_FEED_TABLE . ' (id, user_id, last_check) VALUES (\'' . $page['feed'] . '\', ' . $user['id'] . ', NULL);';
$conn->db_query($query);

(new UserFeedRepository($conn))->addUserFeed(['id' => $page['feed'], 'user_id' => $user['id']]);
$feed_url = \Phyxo\Functions\URL::get_root_url() . 'feed.php';
if ($services['users']->isGuest()) {
$feed_image_only_url = $feed_url;
Expand Down
38 changes: 6 additions & 32 deletions src/Phyxo/Conf.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Phyxo;

use Phyxo\DBLayer\DBLayer;
use App\Repository\ConfigRepository;

/**
* Manage configuration of Phyxo in two ways :
Expand Down Expand Up @@ -56,12 +57,7 @@ public function loadFromFile($conf_file)
*/
public function loadFromDB($condition = '')
{
$query = 'SELECT param, value FROM ' . CONFIG_TABLE;
if (!empty($condition)) {
$query .= ' WHERE ' . $condition;
}

$result = $this->conn->db_query($query);
$result = (new ConfigRepository($this->conn))->findAll($condition);
while ($row = $this->conn->db_fetch_assoc($result)) {
$value = isset($row['value']) ? $row['value'] : '';
if ($this->conn->is_boolean($value)) {
Expand All @@ -79,26 +75,7 @@ public function loadFromDB($condition = '')
*/
protected function addOrUpdateParam($param, $value)
{
if (is_array($value) || is_object($value)) {
$dbValue = json_encode($value);
} else {
$dbValue = $this->conn->boolean_to_string($value);
}

$query = 'SELECT count(1) FROM ' . CONFIG_TABLE;
$query .= ' WHERE param = \'' . $this->conn->db_real_escape_string($param) . '\'';

list($counter) = $this->conn->db_fetch_row($this->conn->db_query($query));
if ($counter == 0) {
$query = 'INSERT INTO ' . CONFIG_TABLE . ' (param, value)';
$query .= ' VALUES(\'' . $this->conn->db_real_escape_string($param) . '\', \'' . $this->conn->db_real_escape_string($dbValue) . '\')';
$this->conn->db_query($query);
} else {
$query = 'UPDATE ' . CONFIG_TABLE;
$query .= ' SET value = \'' . $this->conn->db_real_escape_string($dbValue) . '\'';
$query .= ' WHERE param = \'' . $this->conn->db_real_escape_string($param) . '\'';
$this->conn->db_query($query);
}
(new ConfigRepository($this->conn))->addOrUpdateParam($param, $value);

$this->keys[self::DB_PREFIX . $param] = $value;
}
Expand Down Expand Up @@ -149,20 +126,17 @@ public function offsetUnset($param)
protected function deleteParam($params)
{
if (!is_array($params)) {
$params = array($params);
$params = [$params];
}

if (empty($params)) {
return;
}

$query = 'DELETE FROM ' . CONFIG_TABLE;
$query .= ' WHERE param ' . $this->conn->in($params);
$this->conn->db_query($query);
(new ConfigRepository($this->conn))->delete($params);

foreach ($params as $param) {
unset($this->keys[self::DB_PREFIX . $param]);
}
}

}
}
Loading

0 comments on commit a2406c3

Please sign in to comment.