clean up class, fix php7.3+

This commit is contained in:
Jean-Christian Paul Denis 2021-09-08 01:47:45 +02:00
parent 02d56c5c96
commit 0f17585cad
3 changed files with 97 additions and 113 deletions

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 cinecturlink2
$this->core = $core; $this->core = $core;
$this->con = $core->con; $this->con = $core->con;
$this->table = $core->prefix.'cinecturlink2'; $this->table = $core->prefix . 'cinecturlink2';
$this->blog = $core->con->escape($core->blog->id); $this->blog = $core->con->escape($core->blog->id);
} }
@ -54,91 +53,85 @@ class cinecturlink2
* @param boolean $count_only Count only result * @param boolean $count_only Count only result
* @return record record instance * @return record record instance
*/ */
public function getLinks($params=array(), $count_only=false) public function getLinks($params = [], $count_only = false)
{ {
if ($count_only) { if ($count_only) {
$strReq = 'SELECT count(L.link_id) '; $strReq = 'SELECT count(L.link_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 L.link_id, L.blog_id, L.cat_id, L.user_id, L.link_type, '. 'SELECT L.link_id, L.blog_id, L.cat_id, L.user_id, L.link_type, ' .
$content_req. $content_req .
'L.link_creadt, L.link_upddt, L.link_note, L.link_count, '. 'L.link_creadt, L.link_upddt, L.link_note, L.link_count, ' .
'L.link_title, L.link_desc, L.link_author, '. 'L.link_title, L.link_desc, L.link_author, ' .
'L.link_lang, L.link_url, L.link_img, '. 'L.link_lang, L.link_url, L.link_img, ' .
'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '. 'U.user_name, U.user_firstname, U.user_displayname, U.user_email, ' .
'U.user_url, '. 'U.user_url, ' .
'C.cat_title, C.cat_desc '; 'C.cat_title, C.cat_desc ';
} }
$strReq .= $strReq .=
'FROM '.$this->table.' L '. 'FROM '. $this->table . ' L ' .
'INNER JOIN '.$this->core->prefix.'user U ON U.user_id = L.user_id '. 'INNER JOIN ' . $this->core->prefix . 'user U ON U.user_id = L.user_id ' .
'LEFT OUTER JOIN '.$this->table.'_cat C ON L.cat_id = C.cat_id '; 'LEFT OUTER JOIN ' . $this->table . '_cat C ON L.cat_id = C.cat_id ';
if (!empty($params['from'])) { if (!empty($params['from'])) {
$strReq .= $params['from'].' '; $strReq .= $params['from'] . ' ';
} }
$strReq .= "WHERE L.blog_id = '".$this->blog."' "; $strReq .= "WHERE L.blog_id = '" . $this->blog . "' ";
if (isset($params['link_type'])) { if (isset($params['link_type'])) {
if (is_array($params['link_type']) && !empty($params['link_type'])) { if (is_array($params['link_type']) && !empty($params['link_type'])) {
$strReq .= 'AND L.link_type '.$this->con->in($params['link_type']); $strReq .= 'AND L.link_type ' . $this->con->in($params['link_type']);
} elseif ($params['link_type'] != '') {
$strReq .= "AND L.link_type = '" . $this->con->escape($params['link_type']) . "' ";
} }
elseif ($params['link_type'] != '') { } else {
$strReq .= "AND L.link_type = '".$this->con->escape($params['link_type'])."' ";
}
}
else {
$strReq .= "AND L.link_type = 'cinecturlink' "; $strReq .= "AND L.link_type = 'cinecturlink' ";
} }
if (!empty($params['link_id'])) { if (!empty($params['link_id'])) {
if (is_array($params['link_id'])) { if (is_array($params['link_id'])) {
array_walk($params['link_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}')); array_walk($params['link_id'], function(&$v, $k'){ if($v !== null) { $v = (integer) $v;}});
} else {
$params['link_id'] = [(integer) $params['link_id']];
} }
else { $strReq .= 'AND L.link_id ' . $this->con->in($params['link_id']);
$params['link_id'] = array((integer) $params['link_id']);
}
$strReq .= 'AND L.link_id '.$this->con->in($params['link_id']);
} }
if (!empty($params['cat_id'])) { if (!empty($params['cat_id'])) {
if (is_array($params['cat_id'])) { if (is_array($params['cat_id'])) {
array_walk($params['cat_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}')); array_walk($params['cat_id'], function(&$v, $k) { if($v !== null) { $v = (integer) $v;}});
} else {
$params['cat_id'] = [(integer) $params['cat_id']];
} }
else { $strReq .= 'AND L.cat_id ' . $this->con->in($params['cat_id']);
$params['cat_id'] = array((integer) $params['cat_id']);
}
$strReq .= 'AND L.cat_id '.$this->con->in($params['cat_id']);
} }
if (!empty($params['cat_title'])) { if (!empty($params['cat_title'])) {
$strReq .= "AND C.cat_title = '".$this->con->escape($params['cat_title'])."' "; $strReq .= "AND C.cat_title = '" . $this->con->escape($params['cat_title']) . "' ";
} }
if (!empty($params['link_title'])) { if (!empty($params['link_title'])) {
$strReq .= "AND L.link_title = '".$this->con->escape($params['link_title'])."' "; $strReq .= "AND L.link_title = '" . $this->con->escape($params['link_title']) . "' ";
} }
if (!empty($params['link_lang'])) { if (!empty($params['link_lang'])) {
$strReq .= "AND L.link_lang = '".$this->con->escape($params['link_lang'])."' "; $strReq .= "AND L.link_lang = '" . $this->con->escape($params['link_lang']) . "' ";
} }
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 L.link_upddt DESC '; $strReq .= 'ORDER BY L.link_upddt DESC ';
} }
} }
@ -182,8 +175,7 @@ class cinecturlink2
$cur->link_count = 0; $cur->link_count = 0;
$cur->insert(); $cur->insert();
$this->con->unlock(); $this->con->unlock();
} } catch (Exception $e) {
catch (Exception $e) {
$this->con->unlock(); $this->con->unlock();
throw $e; throw $e;
} }
@ -202,7 +194,7 @@ class cinecturlink2
* @param cursor $cur cursor instance * @param cursor $cur cursor instance
* @param boolean $behavior Call related behaviors * @param boolean $behavior Call related behaviors
*/ */
public function updLink($id, cursor $cur, $behavior=true) public function updLink($id, cursor $cur, $behavior = true)
{ {
$id = (integer) $id; $id = (integer) $id;
@ -212,7 +204,7 @@ class cinecturlink2
$cur->link_upddt = date('Y-m-d H:i:s'); $cur->link_upddt = date('Y-m-d H:i:s');
$cur->update("WHERE link_id = ".$id." AND blog_id = '".$this->blog."' "); $cur->update("WHERE link_id = " . $id . " AND blog_id = '" . $this->blog . "' ");
$this->trigger(); $this->trigger();
if ($behavior) { if ($behavior) {
@ -238,9 +230,9 @@ class cinecturlink2
$this->core->callBehavior('cinecturlink2BeforeDelLink', $id); $this->core->callBehavior('cinecturlink2BeforeDelLink', $id);
$this->con->execute( $this->con->execute(
'DELETE FROM '.$this->table.' '. 'DELETE FROM ' . $this->table . ' ' .
'WHERE link_id = '.$id.' '. 'WHERE link_id = ' . $id . ' ' .
"AND blog_id = '".$this->blog."' " "AND blog_id = '" . $this->blog . "' "
); );
$this->trigger(); $this->trigger();
@ -254,7 +246,7 @@ class cinecturlink2
private function getNextLinkId() private function getNextLinkId()
{ {
return $this->con->select( return $this->con->select(
'SELECT MAX(link_id) FROM '.$this->table.' ' 'SELECT MAX(link_id) FROM ' . $this->table . ' '
)->f(0) + 1; )->f(0) + 1;
} }
@ -265,7 +257,7 @@ class cinecturlink2
* @param boolean $count_only Count only result * @param boolean $count_only Count only result
* @return record record instance * @return record record instance
*/ */
public function getCategories($params=array(), $count_only=false) public function getCategories($params = [], $count_only = false)
{ {
if ($count_only) { if ($count_only) {
$strReq = 'SELECT count(C.cat_id) '; $strReq = 'SELECT count(C.cat_id) ';
@ -273,46 +265,53 @@ class cinecturlink2
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 C.cat_id, C.blog_id, C.cat_title, C.cat_desc, '. 'SELECT C.cat_id, C.blog_id, C.cat_title, C.cat_desc, ' .
$content_req. $content_req .
'C.cat_pos, C.cat_creadt, C.cat_upddt '; 'C.cat_pos, C.cat_creadt, C.cat_upddt ';
} }
$strReq .= 'FROM '.$this->table.'_cat C '; $strReq .= 'FROM ' . $this->table . '_cat C ';
if (!empty($params['from'])) { if (!empty($params['from'])) {
$strReq .= $params['from'].' '; $strReq .= $params['from'] . ' ';
} }
$strReq .= "WHERE C.blog_id = '".$this->blog."' "; $strReq .= "WHERE C.blog_id = '" . $this->blog . "' ";
if (!empty($params['cat_id'])) { if (!empty($params['cat_id'])) {
if (is_array($params['cat_id'])) { if (is_array($params['cat_id'])) {
array_walk($params['cat_id'], create_function('&$v,$k', 'if($v!==null){$v=(integer)$v;}')); array_walk($params['cat_id'], function(&$v, $k) { if($v !== null) { $v = (integer) $v; }});
} else {
$params['cat_id'] = [(integer) $params['cat_id']];
} }
else { $strReq .= 'AND C.cat_id ' . $this->con->in($params['cat_id']);
$params['cat_id'] = array((integer) $params['cat_id']); }
if (isset($params['exclude_cat_id']) && $params['exclude_cat_id'] !== '') {
if (is_array($params['exclude_cat_id'])) {
array_walk($params['exclude_cat_id'], function (&$v, $k) { if ($v !== null) {$v = (integer) $v;}});
} else {
$params['exclude_cat_id'] = [(integer) $params['exclude_cat_id']];
} }
$strReq .= 'AND C.cat_id '.$this->con->in($params['cat_id']); $strReq .= 'AND C.cat_id NOT ' . $this->con->in($params['exclude_cat_id']);
} }
if (!empty($params['cat_title'])) { if (!empty($params['cat_title'])) {
$strReq .= "AND C.cat_title = '".$this->con->escape($params['cat_title'])."' "; $strReq .= "AND C.cat_title = '" . $this->con->escape($params['cat_title']) . "' ";
} }
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 cat_pos ASC '; $strReq .= 'ORDER BY cat_pos ASC ';
} }
} }
@ -332,7 +331,7 @@ class cinecturlink2
*/ */
public function addCategory(cursor $cur) public function addCategory(cursor $cur)
{ {
$this->con->writeLock($this->table.'_cat'); $this->con->writeLock($this->table . '_cat');
try { try {
if ($cur->cat_title == '') { if ($cur->cat_title == '') {
@ -349,8 +348,7 @@ class cinecturlink2
$cur->cat_upddt = date('Y-m-d H:i:s'); $cur->cat_upddt = date('Y-m-d H:i:s');
$cur->insert(); $cur->insert();
$this->con->unlock(); $this->con->unlock();
} } catch (Exception $e) {
catch (Exception $e) {
$this->con->unlock(); $this->con->unlock();
throw $e; throw $e;
} }
@ -375,7 +373,7 @@ class cinecturlink2
$cur->cat_upddt = date('Y-m-d H:i:s'); $cur->cat_upddt = date('Y-m-d H:i:s');
$cur->update("WHERE cat_id = ".$id." AND blog_id = '".$this->blog."' "); $cur->update("WHERE cat_id = " . $id . " AND blog_id = '" . $this->blog . "' ");
$this->trigger(); $this->trigger();
} }
@ -393,16 +391,16 @@ class cinecturlink2
} }
$this->con->execute( $this->con->execute(
'DELETE FROM '.$this->table.'_cat '. 'DELETE FROM ' . $this->table . '_cat ' .
'WHERE cat_id = '.$id.' '. 'WHERE cat_id = ' . $id . ' ' .
"AND blog_id = '".$this->blog."' " "AND blog_id = '" . $this->blog . "' "
); );
# Update link cat to NULL # Update link cat to NULL
$cur = $this->con->openCursor($this->table); $cur = $this->con->openCursor($this->table);
$cur->cat_id = NULL; $cur->cat_id = NULL;
$cur->link_upddt = date('Y-m-d H:i:s'); $cur->link_upddt = date('Y-m-d H:i:s');
$cur->update("WHERE cat_id = ".$id." AND blog_id = '".$this->blog."' "); $cur->update("WHERE cat_id = " . $id . " AND blog_id = '" . $this->blog . "' ");
$this->trigger(); $this->trigger();
} }
@ -415,7 +413,7 @@ class cinecturlink2
private function getNextCatId() private function getNextCatId()
{ {
return $this->con->select( return $this->con->select(
'SELECT MAX(cat_id) FROM '.$this->table.'_cat ' 'SELECT MAX(cat_id) FROM ' . $this->table . '_cat '
)->f(0) + 1; )->f(0) + 1;
} }
@ -427,8 +425,8 @@ class cinecturlink2
private function getNextCatPos() private function getNextCatPos()
{ {
return $this->con->select( return $this->con->select(
'SELECT MAX(cat_pos) FROM '.$this->table.'_cat '. 'SELECT MAX(cat_pos) FROM ' . $this->table . '_cat ' .
"WHERE blog_id = '".$this->blog."' " "WHERE blog_id = '" . $this->blog . "' "
)->f(0) + 1; )->f(0) + 1;
} }
@ -448,18 +446,16 @@ class cinecturlink2
* @param boolean $throw Throw exception or not * @param boolean $throw Throw exception or not
* @return boolean True if exists and writable * @return boolean True if exists and writable
*/ */
public static function test_folder($root, $folder, $throw=false) public static function test_folder($root, $folder, $throw = false)
{ {
if (!is_dir($root.'/'.$folder)) { if (!is_dir($root . '/' . $folder)) {
if (!is_dir($root) || !is_writable($root) || !mkdir($root.'/'.$folder)) { if (!is_dir($root) || !is_writable($root) || !mkdir($root . '/' . $folder)) {
if ($throw) { if ($throw) {
throw new Exception(__('Failed to create public folder for images.')); throw new Exception(__('Failed to create public folder for images.'));
} }
return false; return false;
} }
} }
return true; return true;
} }
} }

View File

@ -18,59 +18,47 @@ class cinecturlink2Context
global $_ctx; global $_ctx;
if ($_ctx->c2_pagination === null) { if ($_ctx->c2_pagination === null) {
return false; return false;
} }
$nb_posts = $_ctx->c2_pagination->f(0); $nb_posts = $_ctx->c2_pagination->f(0);
$nb_per_page = $_ctx->c2_params['limit'][1]; $nb_per_page = $_ctx->c2_params['limit'][1];
$nb_pages = ceil($nb_posts/$nb_per_page); $nb_pages = ceil($nb_posts/$nb_per_page);
return $nb_pages; return $nb_pages;
} }
public static function PaginationPosition($offset=0) public static function PaginationPosition($offset = 0)
{ {
if (isset($GLOBALS['c2_page_number'])) { if (isset($GLOBALS['c2_page_number'])) {
$p = $GLOBALS['c2_page_number']; $p = $GLOBALS['c2_page_number'];
} } else {
else {
$p = 1; $p = 1;
} }
$p = $p+$offset; $p = $p+$offset;
$n = self::PaginationNbPages(); $n = self::PaginationNbPages();
if (!$n) { if (!$n) {
return $p; return $p;
} }
return $p > $n || $p <= 0 ? 1 : $p; return $p > $n || $p <= 0 ? 1 : $p;
} }
public static function PaginationStart() public static function PaginationStart()
{ {
if (isset($GLOBALS['c2_page_number'])) { if (isset($GLOBALS['c2_page_number'])) {
return self::PaginationPosition() == 1; return self::PaginationPosition() == 1;
} }
return true; return true;
} }
public static function PaginationEnd() public static function PaginationEnd()
{ {
if (isset($GLOBALS['c2_page_number'])) { if (isset($GLOBALS['c2_page_number'])) {
return self::PaginationPosition() == self::PaginationNbPages(); return self::PaginationPosition() == self::PaginationNbPages();
} }
return false; return false;
} }
public static function PaginationURL($offset=0) public static function PaginationURL($offset = 0)
{ {
$args = $_SERVER['URL_REQUEST_PART']; $args = $_SERVER['URL_REQUEST_PART'];
@ -78,19 +66,17 @@ class cinecturlink2Context
$args = preg_replace('#(^|/)c2page/([0-9]+)$#', '', $args); $args = preg_replace('#(^|/)c2page/([0-9]+)$#', '', $args);
$url = $GLOBALS['core']->blog->url.$args; $url = $GLOBALS['core']->blog->url . $args;
if ($n > 1) { if ($n > 1) {
$url = preg_replace('#/$#','',$url); $url = preg_replace('#/$#', '', $url);
$url .= '/c2page/'.$n; $url .= '/c2page/' . $n;
} }
# If search param # If search param
if (!empty($_GET['q'])) { if (!empty($_GET['q'])) {
$s = strpos($url,'?') !== false ? '&amp;' : '?'; $s = strpos($url,'?') !== false ? '&amp;' : '?';
$url .= $s.'q='.rawurlencode($_GET['q']); $url .= $s . 'q=' . rawurlencode($_GET['q']);
} }
return $url; return $url;
} }
@ -98,19 +84,21 @@ class cinecturlink2Context
{ {
global $_ctx; global $_ctx;
if (!isset($_ctx->c2_page_params['cat_id']) && !isset($_ctx->c2_page_params['cat_title'])) { if (!isset($_ctx->c2_page_params['cat_id'])
&& !isset($_ctx->c2_page_params['cat_title'])
) {
return false; return false;
} }
if (isset($_ctx->c2_page_params['cat_id']) && $_ctx->c2_page_params['cat_id'] == $_ctx->c2_categories->cat_id) { if (isset($_ctx->c2_page_params['cat_id'])
&& $_ctx->c2_page_params['cat_id'] == $_ctx->c2_categories->cat_id
) {
return true; return true;
} }
if (isset($_ctx->c2_page_params['cat_title']) && $_ctx->c2_page_params['cat_title'] == $_ctx->c2_categories->cat_title) { if (isset($_ctx->c2_page_params['cat_title'])
&& $_ctx->c2_page_params['cat_title'] == $_ctx->c2_categories->cat_title
) {
return true; return true;
} }
return false; return false;
} }
} }

View File

@ -24,11 +24,11 @@ class sitemapsCinecturlink2
$core->blog->settings->addNamespace('sitemaps'); $core->blog->settings->addNamespace('sitemaps');
if ($core->plugins->moduleExists('cinecturlink2') if ($core->plugins->moduleExists('cinecturlink2')
&& $core->blog->settings->sitemaps->sitemaps_cinecturlink2_url) { && $core->blog->settings->sitemaps->sitemaps_cinecturlink2_url
) {
$freq = $sitemaps->getFrequency($core->blog->settings->sitemaps->sitemaps_cinecturlink2_fq); $freq = $sitemaps->getFrequency($core->blog->settings->sitemaps->sitemaps_cinecturlink2_fq);
$prio = $sitemaps->getPriority($core->blog->settings->sitemaps->sitemaps_cinecturlink2_pr); $prio = $sitemaps->getPriority($core->blog->settings->sitemaps->sitemaps_cinecturlink2_pr);
$base = $core->blog->url.$core->url->getBase('cinecturlink2'); $base = $core->blog->url . $core->url->getBase('cinecturlink2');
$sitemaps->addEntry($base, $prio, $freq); $sitemaps->addEntry($base, $prio, $freq);
@ -36,7 +36,7 @@ class sitemapsCinecturlink2
$C2 = new cinecturlink2($core); $C2 = new cinecturlink2($core);
$cats = $C2->getCategories(); $cats = $C2->getCategories();
while ($cats->fetch()) { while ($cats->fetch()) {
$sitemaps->addEntry($base."/".$core->blog->settings->cinecturlink2->cinecturlink2_public_caturl.'/'.urlencode($cats->cat_title), $prio, $freq); $sitemaps->addEntry($base . "/" . $core->blog->settings->cinecturlink2->cinecturlink2_public_caturl . '/' . urlencode($cats->cat_title), $prio, $freq);
} }
} }
} }