cinecturlink2/inc/class.cinecturlink2.php

461 lines
13 KiB
PHP
Raw Normal View History

2021-09-07 12:33:18 +00:00
<?php
2021-09-07 13:21:38 +00:00
/**
* @brief cinecturlink2, a plugin for Dotclear 2
*
* @package Dotclear
* @subpackage Plugin
*
* @author Jean-Christian Denis and Contributors
*
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2021-09-07 12:33:18 +00:00
if (!defined('DC_RC_PATH')) {
2021-09-07 13:21:38 +00:00
return null;
2021-09-07 12:33:18 +00:00
}
/**
* @ingroup DC_PLUGIN_CINECTURLINK2
* @brief Share media you like - main methods.
* @since 2.6
*/
class cinecturlink2
{
2021-09-07 13:21:38 +00:00
/** @var dcCore dcCore instance */
public $core;
/** @var dbLayer dbLayer instance */
public $con;
/** @var string Cinecturlink table name */
public $table;
/** @var string Blog ID */
public $blog;
/**
* Contructor
*
* @param dcCore $core dcCore instance
*/
public function __construct(dcCore $core)
{
$core->blog->settings->addNamespace('cinecturlink2');
$this->core = $core;
$this->con = $core->con;
2021-09-07 23:47:45 +00:00
$this->table = $core->prefix . 'cinecturlink2';
2021-09-07 13:21:38 +00:00
$this->blog = $core->con->escape($core->blog->id);
}
/**
* Get links
*
* @param array $params Query params
* @param boolean $count_only Count only result
* @return record record instance
*/
2021-09-07 23:47:45 +00:00
public function getLinks($params = [], $count_only = false)
2021-09-07 13:21:38 +00:00
{
if ($count_only) {
$strReq = 'SELECT count(L.link_id) ';
2021-09-07 23:47:45 +00:00
} else {
2021-09-07 13:21:38 +00:00
$content_req = '';
if (!empty($params['columns']) && is_array($params['columns'])) {
2021-09-07 23:47:45 +00:00
$content_req .= implode(', ', $params['columns']) . ', ';
2021-09-07 13:21:38 +00:00
}
$strReq =
2021-09-07 23:47:45 +00:00
'SELECT L.link_id, L.blog_id, L.cat_id, L.user_id, L.link_type, ' .
$content_req .
'L.link_creadt, L.link_upddt, L.link_note, L.link_count, ' .
'L.link_title, L.link_desc, L.link_author, ' .
'L.link_lang, L.link_url, L.link_img, ' .
'U.user_name, U.user_firstname, U.user_displayname, U.user_email, ' .
'U.user_url, ' .
2021-09-07 13:21:38 +00:00
'C.cat_title, C.cat_desc ';
}
$strReq .=
2021-09-07 23:47:45 +00:00
'FROM '. $this->table . ' L ' .
'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 ';
2021-09-07 13:21:38 +00:00
if (!empty($params['from'])) {
2021-09-07 23:47:45 +00:00
$strReq .= $params['from'] . ' ';
2021-09-07 13:21:38 +00:00
}
2021-09-07 23:47:45 +00:00
$strReq .= "WHERE L.blog_id = '" . $this->blog . "' ";
2021-09-07 13:21:38 +00:00
if (isset($params['link_type'])) {
if (is_array($params['link_type']) && !empty($params['link_type'])) {
2021-09-07 23:47:45 +00:00
$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']) . "' ";
2021-09-07 13:21:38 +00:00
}
2021-09-07 23:47:45 +00:00
} else {
2021-09-07 13:21:38 +00:00
$strReq .= "AND L.link_type = 'cinecturlink' ";
}
if (!empty($params['link_id'])) {
if (is_array($params['link_id'])) {
2021-09-07 23:50:59 +00:00
array_walk($params['link_id'], function(&$v, $k){ if($v !== null) { $v = (integer) $v;}});
2021-09-07 23:47:45 +00:00
} else {
$params['link_id'] = [(integer) $params['link_id']];
2021-09-07 13:21:38 +00:00
}
2021-09-07 23:47:45 +00:00
$strReq .= 'AND L.link_id ' . $this->con->in($params['link_id']);
2021-09-07 13:21:38 +00:00
}
if (!empty($params['cat_id'])) {
if (is_array($params['cat_id'])) {
2021-09-07 23:47:45 +00:00
array_walk($params['cat_id'], function(&$v, $k) { if($v !== null) { $v = (integer) $v;}});
} else {
$params['cat_id'] = [(integer) $params['cat_id']];
2021-09-07 13:21:38 +00:00
}
2021-09-07 23:47:45 +00:00
$strReq .= 'AND L.cat_id ' . $this->con->in($params['cat_id']);
2021-09-07 13:21:38 +00:00
}
if (!empty($params['cat_title'])) {
2021-09-07 23:47:45 +00:00
$strReq .= "AND C.cat_title = '" . $this->con->escape($params['cat_title']) . "' ";
2021-09-07 13:21:38 +00:00
}
if (!empty($params['link_title'])) {
2021-09-07 23:47:45 +00:00
$strReq .= "AND L.link_title = '" . $this->con->escape($params['link_title']) . "' ";
2021-09-07 13:21:38 +00:00
}
if (!empty($params['link_lang'])) {
2021-09-07 23:47:45 +00:00
$strReq .= "AND L.link_lang = '" . $this->con->escape($params['link_lang']) . "' ";
2021-09-07 13:21:38 +00:00
}
if (!empty($params['sql'])) {
2021-09-07 23:47:45 +00:00
$strReq .= $params['sql'] . ' ';
2021-09-07 13:21:38 +00:00
}
if (!$count_only) {
if (!empty($params['order'])) {
2021-09-07 23:47:45 +00:00
$strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
} else {
2021-09-07 13:21:38 +00:00
$strReq .= 'ORDER BY L.link_upddt DESC ';
}
}
if (!$count_only && !empty($params['limit'])) {
$strReq .= $this->con->limit($params['limit']);
}
return $this->con->select($strReq);
}
/**
* Add link
*
* @param cursor $cur cursor instance
*/
public function addLink(cursor $cur)
{
$this->con->writeLock($this->table);
try {
if ($cur->link_title == '') {
throw new Exception(__('No link title'));
}
if ($cur->link_desc == '') {
throw new Exception(__('No link description'));
}
if ('' == $cur->link_note) {
$cur->link_note = 10;
}
if (0 > $cur->link_note || $cur->link_note > 20) {
$cur->link_note = 10;
}
$cur->link_id = $this->getNextLinkId();
$cur->blog_id = $this->blog;
$cur->user_id = $this->core->auth->userID();
$cur->link_creadt = date('Y-m-d H:i:s');
$cur->link_upddt = date('Y-m-d H:i:s');
$cur->link_pos = 0;
$cur->link_count = 0;
$cur->insert();
$this->con->unlock();
2021-09-07 23:47:45 +00:00
} catch (Exception $e) {
2021-09-07 13:21:38 +00:00
$this->con->unlock();
throw $e;
}
$this->trigger();
# --BEHAVIOR-- cinecturlink2AfterAddLink
$this->core->callBehavior('cinecturlink2AfterAddLink', $cur);
return $cur->link_id;
}
/**
* Update link
*
* @param integer $id Link ID
* @param cursor $cur cursor instance
* @param boolean $behavior Call related behaviors
*/
2021-09-07 23:47:45 +00:00
public function updLink($id, cursor $cur, $behavior = true)
2021-09-07 13:21:38 +00:00
{
$id = (integer) $id;
if (empty($id)) {
throw new Exception(__('No such link ID'));
}
$cur->link_upddt = date('Y-m-d H:i:s');
2021-09-07 23:47:45 +00:00
$cur->update("WHERE link_id = " . $id . " AND blog_id = '" . $this->blog . "' ");
2021-09-07 13:21:38 +00:00
$this->trigger();
if ($behavior) {
# --BEHAVIOR-- cinecturlink2AfterUpdLink
$this->core->callBehavior('cinecturlink2AfterUpdLink', $cur, $id);
}
}
/**
* Delete link
*
* @param integer $id Link ID
*/
public function delLink($id)
{
$id = (integer) $id;
if (empty($id)) {
throw new Exception(__('No such link ID'));
}
# --BEHAVIOR-- cinecturlink2BeforeDelLink
$this->core->callBehavior('cinecturlink2BeforeDelLink', $id);
$this->con->execute(
2021-09-07 23:47:45 +00:00
'DELETE FROM ' . $this->table . ' ' .
'WHERE link_id = ' . $id . ' ' .
"AND blog_id = '" . $this->blog . "' "
2021-09-07 13:21:38 +00:00
);
$this->trigger();
}
/**
* Get next link ID
*
* @return integer Next link ID
*/
private function getNextLinkId()
{
return $this->con->select(
2021-09-07 23:47:45 +00:00
'SELECT MAX(link_id) FROM ' . $this->table . ' '
2021-09-07 13:21:38 +00:00
)->f(0) + 1;
}
/**
* Get categories
*
* @param array $params Query params
* @param boolean $count_only Count only result
* @return record record instance
*/
2021-09-07 23:47:45 +00:00
public function getCategories($params = [], $count_only = false)
2021-09-07 13:21:38 +00:00
{
if ($count_only) {
$strReq = 'SELECT count(C.cat_id) ';
}
else {
$content_req = '';
if (!empty($params['columns']) && is_array($params['columns'])) {
2021-09-07 23:47:45 +00:00
$content_req .= implode(', ', $params['columns']) . ', ';
2021-09-07 13:21:38 +00:00
}
$strReq =
2021-09-07 23:47:45 +00:00
'SELECT C.cat_id, C.blog_id, C.cat_title, C.cat_desc, ' .
$content_req .
2021-09-07 13:21:38 +00:00
'C.cat_pos, C.cat_creadt, C.cat_upddt ';
}
2021-09-07 23:47:45 +00:00
$strReq .= 'FROM ' . $this->table . '_cat C ';
2021-09-07 13:21:38 +00:00
if (!empty($params['from'])) {
2021-09-07 23:47:45 +00:00
$strReq .= $params['from'] . ' ';
2021-09-07 13:21:38 +00:00
}
2021-09-07 23:47:45 +00:00
$strReq .= "WHERE C.blog_id = '" . $this->blog . "' ";
2021-09-07 13:21:38 +00:00
if (!empty($params['cat_id'])) {
if (is_array($params['cat_id'])) {
2021-09-07 23:47:45 +00:00
array_walk($params['cat_id'], function(&$v, $k) { if($v !== null) { $v = (integer) $v; }});
} else {
$params['cat_id'] = [(integer) $params['cat_id']];
2021-09-07 13:21:38 +00:00
}
2021-09-07 23:47:45 +00:00
$strReq .= 'AND C.cat_id ' . $this->con->in($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']];
2021-09-07 13:21:38 +00:00
}
2021-09-07 23:47:45 +00:00
$strReq .= 'AND C.cat_id NOT ' . $this->con->in($params['exclude_cat_id']);
2021-09-07 13:21:38 +00:00
}
if (!empty($params['cat_title'])) {
2021-09-07 23:47:45 +00:00
$strReq .= "AND C.cat_title = '" . $this->con->escape($params['cat_title']) . "' ";
2021-09-07 13:21:38 +00:00
}
if (!empty($params['sql'])) {
2021-09-07 23:47:45 +00:00
$strReq .= $params['sql'] . ' ';
2021-09-07 13:21:38 +00:00
}
if (!$count_only) {
if (!empty($params['order'])) {
2021-09-07 23:47:45 +00:00
$strReq .= 'ORDER BY ' . $this->con->escape($params['order']) . ' ';
} else {
2021-09-07 13:21:38 +00:00
$strReq .= 'ORDER BY cat_pos ASC ';
}
}
if (!$count_only && !empty($params['limit'])) {
$strReq .= $this->con->limit($params['limit']);
}
return $this->con->select($strReq);
}
/**
* Add category
*
* @param cursor $cur cursor instance
* @return integer New category ID
*/
public function addCategory(cursor $cur)
{
2021-09-07 23:47:45 +00:00
$this->con->writeLock($this->table . '_cat');
2021-09-07 13:21:38 +00:00
try {
if ($cur->cat_title == '') {
throw new Exception(__('No category title'));
}
if ($cur->cat_desc == '') {
throw new Exception(__('No category description'));
}
$cur->cat_id = $this->getNextCatId();
$cur->cat_pos = $this->getNextCatPos();
$cur->blog_id = $this->blog;
$cur->cat_creadt = date('Y-m-d H:i:s');
$cur->cat_upddt = date('Y-m-d H:i:s');
$cur->insert();
$this->con->unlock();
2021-09-07 23:47:45 +00:00
} catch (Exception $e) {
2021-09-07 13:21:38 +00:00
$this->con->unlock();
throw $e;
}
$this->trigger();
return $cur->cat_id;
}
/**
* Update category
*
* @param integer $id Category ID
* @param cursor $cur cursor instance
*/
public function updCategory($id, cursor $cur)
{
$id = (integer) $id;
if (empty($id)) {
throw new Exception(__('No such category ID'));
}
$cur->cat_upddt = date('Y-m-d H:i:s');
2021-09-07 23:47:45 +00:00
$cur->update("WHERE cat_id = " . $id . " AND blog_id = '" . $this->blog . "' ");
2021-09-07 13:21:38 +00:00
$this->trigger();
}
/**
* Delete category
*
* @param integer $id Category ID
*/
public function delCategory($id)
{
$id = (integer) $id;
if (empty($id)) {
throw new Exception(__('No such category ID'));
}
$this->con->execute(
2021-09-07 23:47:45 +00:00
'DELETE FROM ' . $this->table . '_cat ' .
'WHERE cat_id = ' . $id . ' ' .
"AND blog_id = '" . $this->blog . "' "
2021-09-07 13:21:38 +00:00
);
# Update link cat to NULL
$cur = $this->con->openCursor($this->table);
$cur->cat_id = NULL;
$cur->link_upddt = date('Y-m-d H:i:s');
2021-09-07 23:47:45 +00:00
$cur->update("WHERE cat_id = " . $id . " AND blog_id = '" . $this->blog . "' ");
2021-09-07 13:21:38 +00:00
$this->trigger();
}
/**
* Get next category ID
*
* @return integer Next category ID
*/
private function getNextCatId()
{
return $this->con->select(
2021-09-07 23:47:45 +00:00
'SELECT MAX(cat_id) FROM ' . $this->table . '_cat '
2021-09-07 13:21:38 +00:00
)->f(0) + 1;
}
/**
* Get next category position
*
* @return integer Next category position
*/
private function getNextCatPos()
{
return $this->con->select(
2021-09-07 23:47:45 +00:00
'SELECT MAX(cat_pos) FROM ' . $this->table . '_cat ' .
"WHERE blog_id = '" . $this->blog . "' "
2021-09-07 13:21:38 +00:00
)->f(0) + 1;
}
/**
* Trigger event
*/
private function trigger()
{
$this->core->blog->triggerBlog();
}
/**
* Check if a directory exists and is writable
*
* @param string $root Root
* @param string $folder Folder to create into root folder
* @param boolean $throw Throw exception or not
* @return boolean True if exists and writable
*/
2021-09-07 23:47:45 +00:00
public static function test_folder($root, $folder, $throw = false)
2021-09-07 13:21:38 +00:00
{
2021-09-07 23:47:45 +00:00
if (!is_dir($root . '/' . $folder)) {
if (!is_dir($root) || !is_writable($root) || !mkdir($root . '/' . $folder)) {
2021-09-07 13:21:38 +00:00
if ($throw) {
throw new Exception(__('Failed to create public folder for images.'));
}
return false;
}
}
return true;
}
}