fix php 8 deprecated function, clean code

master
Jean-Christian Paul Denis 2021-09-12 22:32:21 +02:00
parent 0ae0ca03d1
commit 234a496ac4
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
1 changed files with 113 additions and 138 deletions

View File

@ -12,7 +12,6 @@
*/
if (!defined('DC_RC_PATH')) {
return null;
}
@ -43,7 +42,7 @@ class zoneclearFeedServer
$this->core = $core;
$this->con = $core->con;
$this->blog = $core->con->escape($core->blog->id);
$this->table = $core->prefix.'zc_feed';
$this->table = $core->prefix . 'zc_feed';
}
/**
@ -72,6 +71,7 @@ class zoneclearFeedServer
if ($id < 1) {
throw new Exception(__('No such ID'));
}
$cur->feed_upddt = date('Y-m-d H:i:s');
$cur->update(sprintf(
@ -81,8 +81,7 @@ class zoneclearFeedServer
));
$this->con->unlock();
$this->trigger();
}
catch (Exception $e) {
} catch (Exception $e) {
$this->con->unlock();
throw $e;
}
@ -115,8 +114,7 @@ class zoneclearFeedServer
$cur->insert();
$this->con->unlock();
$this->trigger();
}
catch (Exception $e) {
} catch (Exception $e) {
$this->con->unlock();
throw $e;
}
@ -137,7 +135,7 @@ class zoneclearFeedServer
* @param boolean $enable Enable or disable feed
* @param integer $time Force update time
*/
public function enableFeed($id, $enable=true, $time=null)
public function enableFeed($id, $enable = true, $time = null)
{
try {
$id = (integer) $id;
@ -145,11 +143,11 @@ class zoneclearFeedServer
if ($id < 1) {
throw new Exception(__('No such ID'));
}
$cur = $this->openCursor();
$this->con->writeLock($this->table);
$cur->feed_upddt = date('Y-m-d H:i:s');
$cur->feed_status = (integer) $enable;
if (null !== $time) {
$cur->feed_upd_last = (integer) $time;
@ -162,8 +160,7 @@ class zoneclearFeedServer
));
$this->con->unlock();
$this->trigger();
}
catch (Exception $e) {
} catch (Exception $e) {
$this->con->unlock();
throw $e;
}
@ -213,20 +210,19 @@ class zoneclearFeedServer
* @param boolean $count_only Return only result count
* @return record record instance
*/
public function getPostsByFeed($params=array(), $count_only=false)
public function getPostsByFeed($params = array(), $count_only = false)
{
if (!isset($params['feed_id'])) {
return null;
}
$params['from'] =
'LEFT JOIN '.$this->core->prefix.'meta F '.
'LEFT JOIN ' . $this->core->prefix . 'meta F ' .
'ON P.post_id = F.post_id ';
$params['sql'] =
"AND P.blog_id = '".$this->blog."' ".
"AND F.meta_type = 'zoneclearfeed_id' ".
"AND F.meta_id = '".$this->con->escape($params['feed_id'])."' ";
"AND P.blog_id = '" . $this->blog . "' " .
"AND F.meta_type = 'zoneclearfeed_id' " .
"AND F.meta_id = '" . $this->con->escape($params['feed_id']) . "' ";
unset($params['feed_id']);
@ -240,75 +236,71 @@ class zoneclearFeedServer
* @param boolean $count_only Return only result count
* @return record record instance
*/
public function getFeeds($params=array(), $count_only=false)
public function getFeeds($params = array(), $count_only = false)
{
if ($count_only) {
$strReq = 'SELECT count(Z.feed_id) ';
}
else {
} else {
$content_req = '';
if (!empty($params['columns']) && is_array($params['columns'])) {
$content_req .= implode(', ',$params['columns']).', ';
$content_req .= implode(', ', $params['columns']) . ', ';
}
$strReq =
'SELECT Z.feed_id, Z.feed_creadt, Z.feed_upddt, Z.feed_type, '.
'Z.blog_id, Z.cat_id, '.
'Z.feed_upd_int, Z.feed_upd_last, Z.feed_status, '.
$content_req.
'LOWER(Z.feed_name) as lowername, Z.feed_name, Z.feed_desc, '.
'Z.feed_url, Z.feed_feed, Z.feed_get_tags, '.
'Z.feed_tags, Z.feed_owner, Z.feed_tweeter, Z.feed_lang, '.
'Z.feed_nb_out, Z.feed_nb_in, '.
'SELECT Z.feed_id, Z.feed_creadt, Z.feed_upddt, Z.feed_type, ' .
'Z.blog_id, Z.cat_id, ' .
'Z.feed_upd_int, Z.feed_upd_last, Z.feed_status, ' .
$content_req .
'LOWER(Z.feed_name) as lowername, Z.feed_name, Z.feed_desc, ' .
'Z.feed_url, Z.feed_feed, Z.feed_get_tags, ' .
'Z.feed_tags, Z.feed_owner, Z.feed_tweeter, Z.feed_lang, ' .
'Z.feed_nb_out, Z.feed_nb_in, ' .
'C.cat_title, C.cat_url, C.cat_desc ';
}
$strReq .=
'FROM '.$this->table.' Z '.
'LEFT OUTER JOIN '.$this->core->prefix.'category C ON Z.cat_id = C.cat_id ';
'FROM ' . $this->table . ' Z ' .
'LEFT OUTER JOIN ' . $this->core->prefix . 'category C ON Z.cat_id = C.cat_id ';
if (!empty($params['from'])) {
$strReq .= $params['from'].' ';
$strReq .= $params['from'] . ' ';
}
$strReq .= "WHERE Z.blog_id = '".$this->blog."' ";
$strReq .= "WHERE Z.blog_id = '" . $this->blog . "' ";
if (isset($params['feed_type'])) {
$strReq .= "AND Z.feed_type = '".$this->con->escape($params['type'])."' ";
}
else {
$strReq .= "AND Z.feed_type = '" . $this->con->escape($params['type']) . "' ";
} else {
$strReq .= "AND Z.feed_type = 'feed' ";
}
if (!empty($params['feed_id'])) {
if (is_array($params['feed_id'])) {
array_walk($params['feed_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}'));
array_walk($params['feed_id'], function(&$v, $k) { if($v !== null) { $v = (integer) $v; }});
} else {
$params['feed_id'] = [(integer) $params['feed_id']];
}
else {
$params['feed_id'] = array((integer) $params['feed_id']);
}
$strReq .= 'AND Z.feed_id '.$this->con->in($params['feed_id']);
$strReq .= 'AND Z.feed_id ' . $this->con->in($params['feed_id']);
}
if (isset($params['feed_feed'])) {
$strReq .= "AND Z.feed_feed = '".$this->con->escape($params['feed_feed'])."' ";
$strReq .= "AND Z.feed_feed = '" . $this->con->escape($params['feed_feed']) . "' ";
}
if (isset($params['feed_url'])) {
$strReq .= "AND Z.feed_url = '".$this->con->escape($params['feed_url'])."' ";
$strReq .= "AND Z.feed_url = '" . $this->con->escape($params['feed_url']) . "' ";
}
if (isset($params['feed_status'])) {
$strReq .= "AND Z.feed_status = ".((integer) $params['feed_status'])." ";
$strReq .= "AND Z.feed_status = " . ((integer) $params['feed_status']) . " ";
}
if (!empty($params['sql'])) {
$strReq .= $params['sql'].' ';
$strReq .= $params['sql'] . ' ';
}
if (!$count_only) {
if (!empty($params['order'])) {
$strReq .= 'ORDER BY '.$this->con->escape($params['order']).' ';
}
else {
$strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
} else {
$strReq .= 'ORDER BY Z.feed_upddt DESC ';
}
}
@ -331,7 +323,7 @@ class zoneclearFeedServer
private function getNextId()
{
return $this->con->select(
'SELECT MAX(feed_id) FROM '.$this->table
'SELECT MAX(feed_id) FROM ' . $this->table
)->f(0) + 1;
}
@ -387,8 +379,7 @@ class zoneclearFeedServer
$this->lock = $fp;
return true;
}
catch (Exception $e) {
} catch (Exception $e) {
throw $e;
}
@ -412,13 +403,12 @@ class zoneclearFeedServer
* @param boolean $throw Throw exception or end silently
* @return boolean True if process succeed
*/
public function checkFeedsUpdate($id=null, $throw=false)
public function checkFeedsUpdate($id = null, $throw = false)
{
# Limit to one update at a time
try {
$this->lockUpdate();
}
catch (Exception $e) {
} catch (Exception $e) {
if ($throw) {
throw $e;
}
@ -432,17 +422,11 @@ class zoneclearFeedServer
# All feeds or only one (from admin)
$f = !$id ?
$this->getFeeds(array(
'feed_status'=>1,
'order'=>'feed_upd_last ASC'
)) :
$this->getFeeds(array(
'feed_id'=>$id
));
$this->getFeeds(['feed_status' => 1, 'order' => 'feed_upd_last ASC']) :
$this->getFeeds(['feed_id' => $id]);
# No feed
if ($f->isEmpty()) {
return false;
}
@ -450,7 +434,7 @@ class zoneclearFeedServer
$this->enableUser($s->zoneclearFeedServer_user);
$updates = false;
$loop_mem = array();
$loop_mem = [];
$limit = abs((integer) $s->zoneclearFeedServer_update_limit);
if ($limit < 1) {
@ -458,13 +442,13 @@ class zoneclearFeedServer
}
$i = 0;
$cur_post = $this->con->openCursor($this->core->prefix.'post');
$cur_meta = $this->con->openCursor($this->core->prefix.'meta');
$cur_post = $this->con->openCursor($this->core->prefix . 'post');
$cur_meta = $this->con->openCursor($this->core->prefix . 'meta');
while($f->fetch()) {
# Check if feed need update
if ($id || $i < $limit && $f->feed_status == 1
&& $time > $f->feed_upd_last + $f->feed_upd_int
&& $time > $f->feed_upd_last + $f->feed_upd_int
) {
$i++;
$feed = self::readFeed($f->feed_feed);
@ -474,23 +458,21 @@ class zoneclearFeedServer
# Keep active empty feed or disable it ?
if (!$s->zoneclearFeedServer_keep_empty_feed) {
$this->enableFeed($f->feed_id, false);
}
else {
} else {
# Set update time of this feed
$this->enableFeed($f->feed_id, true, $time);
}
$i++;
}
# Not updated since last visit
elseif (!$id
&& '' != $feed->pubdate
&& strtotime($feed->pubdate) < $f->feed_upd_last
} elseif (!$id
&& '' != $feed->pubdate
&& strtotime($feed->pubdate) < $f->feed_upd_last
) {
# Set update time of this feed
$this->enableFeed($f->feed_id, true, $time);
$i++;
}
else {
} else {
# Set update time of this feed
$this->enableFeed($f->feed_id, $f->feed_status, $time);
@ -525,13 +507,13 @@ class zoneclearFeedServer
# Check if entry exists
$old_post = $this->con->select(
'SELECT P.post_id, P.post_status '.
'FROM '.$this->core->prefix.'post P '.
'INNER JOIN '.$this->core->prefix.'meta M '.
'ON P.post_id = M.post_id '.
"WHERE blog_id='".$this->blog."' ".
"AND meta_type = 'zoneclearfeed_url' ".
"AND meta_id = '".$item_link."' "
'SELECT P.post_id, P.post_status ' .
'FROM ' . $this->core->prefix . 'post P ' .
'INNER JOIN ' . $this->core->prefix . 'meta M '.
'ON P.post_id = M.post_id ' .
"WHERE blog_id='" . $this->blog . "' " .
"AND meta_type = 'zoneclearfeed_url' " .
"AND meta_id = '" . $item_link . "' "
);
# Prepare entry cursor
@ -542,7 +524,7 @@ class zoneclearFeedServer
}
$post_content = $item->content ? $item->content : $item->description;
$cur_post->post_content = html::absoluteURLs($post_content,$feed->link);
$cur_post->post_title = $item->title ? $item->title : text::cutString(html::clean($cur_post->post_content),60);
$cur_post->post_title = $item->title ? $item->title : text::cutString(html::clean($cur_post->post_content), 60);
$creator = $item->creator ? $item->creator : $f->feed_owner;
try {
@ -562,7 +544,7 @@ class zoneclearFeedServer
);
$post_id = $this->core->auth->sudo(
array($this->core->blog, 'addPost'),
[$this->core->blog, 'addPost'],
$cur_post
);
@ -577,9 +559,9 @@ class zoneclearFeedServer
if ($cur_post->post_status == 1) {
$is_new_published_entry = true;
}
}
# Update entry
else {
} else {
$post_id = $old_post->post_id;
# --BEHAVIOR-- zoneclearFeedServerBeforePostUpdate
@ -590,20 +572,20 @@ class zoneclearFeedServer
);
$this->core->auth->sudo(
array($this->core->blog, 'updPost'),
[$this->core->blog, 'updPost'],
$post_id,
$cur_post
);
# Quick delete old meta
$this->con->execute(
'DELETE FROM '.$this->core->prefix.'meta '.
'WHERE post_id = '.$post_id.' '.
'DELETE FROM ' . $this->core->prefix . 'meta ' .
'WHERE post_id = ' . $post_id . ' ' .
"AND meta_type LIKE 'zoneclearfeed_%' "
);
# Delete old tags
$this->core->auth->sudo(
array($this->core->meta, 'delPostMeta'),
[$this->core->meta, 'delPostMeta'],
$post_id,
'tag'
);
@ -661,20 +643,20 @@ class zoneclearFeedServer
}
$tags = array_unique($tags);
}
$formated_tags = array();
$formated_tags = [];
foreach ($tags as $tag) {
# Change tags case
switch((integer) $s->zoneclearFeedServer_tag_case) {
case 3: $tag = strtoupper($tag); break;
case 2: $tag = strtolower($tag); break;
case 3: $tag = strtoupper($tag); break;
case 2: $tag = strtolower($tag); break;
case 1: $tag = ucfirst(strtolower($tag)); break;
default: /* do nothing */ break;
default: /* do nothing */ break;
}
if (!in_array($tag, $formated_tags)) {
$formated_tags[] = $tag;
$this->core->auth->sudo(
array($this->core->meta, 'setPostMeta'),
[$this->core->meta, 'setPostMeta'],
$post_id,
'tag',
dcMeta::sanitizeMetaID($tag)
@ -692,8 +674,7 @@ class zoneclearFeedServer
$meta
);
}
catch (Exception $e) {
} catch (Exception $e) {
$this->con->rollback();
$this->enableUser(false);
$this->unlockUpdate();
@ -716,16 +697,15 @@ class zoneclearFeedServer
*
* @param boolean $enable Enable or disable perm
*/
public function enableUser($enable=false)
public function enableUser($enable = false)
{
# Enable
if ($enable) {
if (!$this->core->auth->checkUser($enable)) {
throw new Exception('Unable to set user');
}
}
# Disable
else {
} else {
$this->core->auth = null;
$this->core->auth = new dcAuth($this->core);
}
@ -747,9 +727,7 @@ class zoneclearFeedServer
$feed_reader->setUserAgent(self::$nethttp_agent);
return $feed_reader->parse($f);
}
catch (Exception $e) {
} catch (Exception $e) {
return null;
}
}
@ -771,7 +749,7 @@ class zoneclearFeedServer
public static function validateURL($url)
{
return false !== strpos($url, 'http://')
|| false !== strpos($url, 'https://');
|| false !== strpos($url, 'https://');
}
/**
@ -794,17 +772,14 @@ class zoneclearFeedServer
$parse = parse_url($url);
if (empty($parse['scheme'])) {
if (strpos($url,'/') === 0) {
$url = $host.$url;
}
elseif (strpos($url, '#') === 0) {
$url = $root.$url;
}
elseif (preg_match('|/$|', $root)) {
$url = $root.$url;
}
else {
$url = dirname($root).'/'.$url;
if (strpos($url, '/') === 0) {
$url = $host . $url;
} elseif (strpos($url, '#') === 0) {
$url = $root . $url;
} elseif (preg_match('|/$|', $root)) {
$url = $root . $url;
} else {
$url = dirname($root) . '/' . $url;
}
}
@ -818,10 +793,10 @@ class zoneclearFeedServer
*/
public static function getAllStatus()
{
return array(
__('Disabled') => '0',
__('Enabled') => '1'
);
return [
__('Disabled') => '0',
__('Enabled') => '1'
];
}
/**
@ -831,14 +806,14 @@ class zoneclearFeedServer
*/
public static function getAllUpdateInterval()
{
return array(
return [
__('Every hour') => 3600,
__('Every two hours') => 7200,
__('Two times per day') => 43200,
__('Every day') => 86400,
__('Every two hours') => 7200,
__('Two times per day') => 43200,
__('Every day') => 86400,
__('Every two days') => 172800,
__('Every week') => 604800
);
];
}
/**
@ -848,12 +823,12 @@ class zoneclearFeedServer
*/
public function getAllBlogAdmins()
{
$admins = array();
$admins = [];
# Get super admins
$rs = $this->con->select(
'SELECT user_id, user_super, user_name, user_firstname, user_displayname '.
'FROM '.$this->con->escapeSystem($this->core->prefix.'user').' '.
'SELECT user_id, user_super, user_name, user_firstname, user_displayname ' .
'FROM ' . $this->con->escapeSystem($this->core->prefix . 'user') . ' ' .
'WHERE user_super = 1 AND user_status = 1 '
);
@ -865,18 +840,18 @@ class zoneclearFeedServer
$rs->user_firstname,
$rs->user_displayname
);
$admins[$user_cn.' (super admin)'] = $rs->user_id;
$admins[$user_cn . ' (super admin)'] = $rs->user_id;
}
}
# Get admins
$rs = $this->con->select(
'SELECT U.user_id, U.user_super, U.user_name, U.user_firstname, U.user_displayname '.
'FROM '.$this->con->escapeSystem($this->core->prefix.'user').' U '.
'LEFT JOIN '.$this->con->escapeSystem($this->core->prefix.'permissions').' P '.
'ON U.user_id=P.user_id '.
'SELECT U.user_id, U.user_super, U.user_name, U.user_firstname, U.user_displayname ' .
'FROM ' . $this->con->escapeSystem($this->core->prefix . 'user') . ' U ' .
'LEFT JOIN ' . $this->con->escapeSystem($this->core->prefix . 'permissions') . ' P ' .
'ON U.user_id=P.user_id ' .
'WHERE U.user_status = 1 '.
"AND P.blog_id = '".$this->blog."' ".
"AND P.blog_id = '" .$this->blog . "' " .
"AND P.permissions LIKE '%|admin|%' "
);
@ -888,7 +863,7 @@ class zoneclearFeedServer
$rs->user_firstname,
$rs->user_displayname
);
$admins[$user_cn.' (admin)'] = $rs->user_id;
$admins[$user_cn . ' (admin)'] = $rs->user_id;
}
}
@ -908,12 +883,12 @@ class zoneclearFeedServer
# --BEHAVIOR-- zoneclearFeedServerPublicUrlTypes
$core->callBehavior('zoneclearFeedServerPublicUrlTypes', $types);
$types[__('Home page')] = 'default';
$types[__('Entries pages')] = 'post';
$types[__('Tags pages')] = 'tag';
$types[__('Archives pages')] = 'archive';
$types[__('Category pages')] = 'category';
$types[__('Entries feed')] = 'feed';
$types[__('Home page')] = 'default';
$types[__('Entries pages')] = 'post';
$types[__('Tags pages')] = 'tag';
$types[__('Archives pages')] = 'archive';
$types[__('Category pages')] = 'category';
$types[__('Entries feed')] = 'feed';
return $types;
}