cinecturlink2/inc/class.cinecturlink2.php

497 lines
14 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
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @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 dbLayer dbLayer instance */
public $con;
/** @var string Cinecturlink table name */
public $table;
2022-11-30 22:43:36 +00:00
/** @var string Cinecturlink category table name */
public $cat_table;
2021-09-07 13:21:38 +00:00
/** @var string Blog ID */
public $blog;
/**
* Contructor
*/
2022-11-17 21:01:00 +00:00
public function __construct()
2021-09-07 13:21:38 +00:00
{
2022-11-17 21:01:00 +00:00
dcCore::app()->blog->settings->addNamespace('cinecturlink2');
2021-09-07 13:21:38 +00:00
2022-11-17 21:01:00 +00:00
$this->con = dcCore::app()->con;
2022-11-30 22:43:36 +00:00
$this->table = dcCore::app()->prefix . initCinecturlink2::CINECTURLINK_TABLE_NAME;
$this->cat_table = dcCore::app()->prefix . initCinecturlink2::CATEGORY_TABLE_NAME;
2022-11-17 21:01:00 +00:00
$this->blog = dcCore::app()->con->escape(dcCore::app()->blog->id);
2021-09-07 13:21:38 +00:00
}
/**
* Get links
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @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
}
2021-11-02 22:55:41 +00:00
$strReq = 'SELECT L.link_id, L.blog_id, L.cat_id, L.user_id, L.link_type, ' .
2021-09-07 23:47:45 +00:00
$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 ';
}
2021-11-02 22:55:41 +00:00
$strReq .= 'FROM ' . $this->table . ' L ' .
2022-11-30 22:43:36 +00:00
'INNER JOIN ' . dcCore::app()->prefix . dcAuth::USER_TABLE_NAME . ' U ON U.user_id = L.user_id ' .
'LEFT OUTER JOIN ' . $this->cat_table . ' 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-11-02 22:55:41 +00:00
array_walk($params['link_id'], function (&$v, $k) { if ($v !== null) { $v = (int) $v;}});
2021-09-07 23:47:45 +00:00
} else {
2021-11-02 22:55:41 +00:00
$params['link_id'] = [(int) $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'])) {
2022-11-20 17:01:26 +00:00
array_walk($params['cat_id'], function (&$v, $k) {
if ($v !== null) {
$v = (int) $v;
}
});
2021-09-07 23:47:45 +00:00
} else {
2021-11-02 22:55:41 +00:00
$params['cat_id'] = [(int) $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
}
2021-11-04 01:03:49 +00:00
if (!empty($params['q'])) {
$q = $this->con->escape(str_replace('*', '%', strtolower($params['q'])));
$strReq .= "AND LOWER(L.link_title) LIKE '%" . $q . "%' ";
}
if (!empty($params['where'])) {
$strReq .= $params['where'] . ' ';
}
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
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @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;
}
2021-11-02 22:55:41 +00:00
$cur->link_id = $this->getNextLinkId();
$cur->blog_id = $this->blog;
2022-11-17 21:01:00 +00:00
$cur->user_id = dcCore::app()->auth->userID();
2021-09-07 13:21:38 +00:00
$cur->link_creadt = date('Y-m-d H:i:s');
2021-11-02 22:55:41 +00:00
$cur->link_upddt = date('Y-m-d H:i:s');
$cur->link_pos = 0;
$cur->link_count = 0;
2021-09-07 13:21:38 +00:00
$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();
2021-11-02 22:55:41 +00:00
2021-09-07 13:21:38 +00:00
throw $e;
}
$this->trigger();
# --BEHAVIOR-- cinecturlink2AfterAddLink
2022-11-17 21:01:00 +00:00
dcCore::app()->callBehavior('cinecturlink2AfterAddLink', $cur);
2021-09-07 13:21:38 +00:00
return $cur->link_id;
}
/**
* Update link
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @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
{
2021-11-02 22:55:41 +00:00
$id = (int) $id;
2021-09-07 13:21:38 +00:00
if (empty($id)) {
throw new Exception(__('No such link ID'));
}
$cur->link_upddt = date('Y-m-d H:i:s');
2021-11-02 22:55:41 +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
2022-11-17 21:01:00 +00:00
dcCore::app()->callBehavior('cinecturlink2AfterUpdLink', $cur, $id);
2021-09-07 13:21:38 +00:00
}
}
/**
* Delete link
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @param integer $id Link ID
*/
public function delLink($id)
{
2021-11-02 22:55:41 +00:00
$id = (int) $id;
2021-09-07 13:21:38 +00:00
if (empty($id)) {
throw new Exception(__('No such link ID'));
}
# --BEHAVIOR-- cinecturlink2BeforeDelLink
2022-11-17 21:01:00 +00:00
dcCore::app()->callBehavior('cinecturlink2BeforeDelLink', $id);
2021-09-07 13:21:38 +00:00
$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
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @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
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @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) ';
2021-11-02 22:55:41 +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
}
2021-11-02 22:55:41 +00:00
$strReq = 'SELECT C.cat_id, C.blog_id, C.cat_title, C.cat_desc, ' .
2021-09-07 23:47:45 +00:00
$content_req .
2021-09-07 13:21:38 +00:00
'C.cat_pos, C.cat_creadt, C.cat_upddt ';
}
2022-11-30 22:43:36 +00:00
$strReq .= 'FROM ' . $this->cat_table . ' 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'])) {
2022-11-20 17:01:26 +00:00
array_walk($params['cat_id'], function (&$v, $k) {
if ($v !== null) {
$v = (int) $v;
}
});
2021-09-07 23:47:45 +00:00
} else {
2021-11-02 22:55:41 +00:00
$params['cat_id'] = [(int) $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'])) {
2022-11-20 17:01:26 +00:00
array_walk($params['exclude_cat_id'], function (&$v, $k) {
if ($v !== null) {
$v = (int) $v;
}
});
2021-09-07 23:47:45 +00:00
} else {
2021-11-02 22:55:41 +00:00
$params['exclude_cat_id'] = [(int) $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
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @param cursor $cur cursor instance
* @return integer New category ID
*/
public function addCategory(cursor $cur)
{
2022-11-30 22:43:36 +00:00
$this->con->writeLock($this->cat_table);
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'));
}
2021-11-02 22:55:41 +00:00
$cur->cat_id = $this->getNextCatId();
$cur->cat_pos = $this->getNextCatPos();
$cur->blog_id = $this->blog;
2021-09-07 13:21:38 +00:00
$cur->cat_creadt = date('Y-m-d H:i:s');
2021-11-02 22:55:41 +00:00
$cur->cat_upddt = date('Y-m-d H:i:s');
2021-09-07 13:21:38 +00:00
$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();
2021-11-02 22:55:41 +00:00
2021-09-07 13:21:38 +00:00
throw $e;
}
$this->trigger();
return $cur->cat_id;
}
/**
* Update category
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @param integer $id Category ID
* @param cursor $cur cursor instance
*/
public function updCategory($id, cursor $cur)
{
2021-11-02 22:55:41 +00:00
$id = (int) $id;
2021-09-07 13:21:38 +00:00
if (empty($id)) {
throw new Exception(__('No such category ID'));
}
$cur->cat_upddt = date('Y-m-d H:i:s');
2021-11-02 22:55:41 +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
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @param integer $id Category ID
*/
public function delCategory($id)
{
2021-11-02 22:55:41 +00:00
$id = (int) $id;
2021-09-07 13:21:38 +00:00
if (empty($id)) {
throw new Exception(__('No such category ID'));
}
$this->con->execute(
2022-11-30 22:43:36 +00:00
'DELETE FROM ' . $this->cat_table . ' ' .
2021-09-07 23:47:45 +00:00
'WHERE cat_id = ' . $id . ' ' .
"AND blog_id = '" . $this->blog . "' "
2021-09-07 13:21:38 +00:00
);
# Update link cat to NULL
2021-11-02 22:55:41 +00:00
$cur = $this->con->openCursor($this->table);
$cur->cat_id = null;
2021-09-07 13:21:38 +00:00
$cur->link_upddt = date('Y-m-d H:i:s');
2021-11-02 22:55:41 +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
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @return integer Next category ID
*/
private function getNextCatId()
{
return $this->con->select(
2022-11-30 22:43:36 +00:00
'SELECT MAX(cat_id) FROM ' . $this->cat_table . ' '
2021-09-07 13:21:38 +00:00
)->f(0) + 1;
}
/**
* Get next category position
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @return integer Next category position
*/
private function getNextCatPos()
{
return $this->con->select(
2022-11-30 22:43:36 +00:00
'SELECT MAX(cat_pos) FROM ' . $this->cat_table . ' ' .
2021-09-07 23:47:45 +00:00
"WHERE blog_id = '" . $this->blog . "' "
2021-09-07 13:21:38 +00:00
)->f(0) + 1;
}
/**
* Trigger event
*/
private function trigger()
{
2022-11-17 21:01:00 +00:00
dcCore::app()->blog->triggerBlog();
2021-09-07 13:21:38 +00:00
}
/**
* Check if a directory exists and is writable
2021-11-02 22:55:41 +00:00
*
2021-09-07 13:21:38 +00:00
* @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-11-02 22:53:06 +00:00
public static function makePublicDir($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.'));
}
2021-11-02 22:55:41 +00:00
2021-09-07 13:21:38 +00:00
return false;
}
}
2021-11-02 22:55:41 +00:00
2021-09-07 13:21:38 +00:00
return true;
}
2021-11-02 22:53:06 +00:00
/**
* Get list of public directories
2021-11-02 22:55:41 +00:00
*
2021-11-02 22:53:06 +00:00
* @return array Directories
*/
2022-11-17 21:01:00 +00:00
public static function getPublicDirs()
2021-11-02 22:53:06 +00:00
{
$dirs = [];
2022-11-17 21:01:00 +00:00
$all = files::getDirList(dcCore::app()->blog->public_path);
2021-11-02 22:55:41 +00:00
foreach ($all['dirs'] as $dir) {
2022-11-17 21:01:00 +00:00
$dir = substr($dir, strlen(dcCore::app()->blog->public_path) + 1);
2021-11-02 22:53:06 +00:00
$dirs[$dir] = $dir;
}
return $dirs;
}
2021-11-02 22:55:41 +00:00
}