fix php 8 deprecated function, clean code

This commit is contained in:
Jean-Christian Paul Denis 2021-09-12 22:32:21 +02:00
parent 0ae0ca03d1
commit 234a496ac4
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951

View File

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