175 lines
5.1 KiB
PHP
175 lines
5.1 KiB
PHP
|
|
<?php
|
||
|
|
class HashtagAddon extends Addon
|
||
|
|
{
|
||
|
|
public $name = "게시글 해시태그 추적 애드온";
|
||
|
|
public $description = "해시태그 추적 애드온";
|
||
|
|
public $author = "Amber";
|
||
|
|
public $link = "https://info.drk.st/about";
|
||
|
|
public $version = "1.0.0";
|
||
|
|
|
||
|
|
public $className;
|
||
|
|
public $addonPath;
|
||
|
|
public $addonFile;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 애드온 생성자입니다.
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
public function init($data = [])
|
||
|
|
{
|
||
|
|
$that = $this;
|
||
|
|
|
||
|
|
EventHAndler::addEventHandler(
|
||
|
|
"amber.load_config_after",
|
||
|
|
function ($args = null) {
|
||
|
|
global $g5;
|
||
|
|
|
||
|
|
$g5['hashtags_table'] = G5_TABLE_PREFIX . 'hashtags';
|
||
|
|
$g5['hashtag_posts_table'] = G5_TABLE_PREFIX . 'hashtag_posts';
|
||
|
|
|
||
|
|
if (!sql_fetch_array(sql_query("DESC {$g5['hashtags_table']}"))) {
|
||
|
|
sql_query("CREATE TABLE {$g5['hashtags_table']} (
|
||
|
|
`srl` INT(11) NOT NULL AUTO_INCREMENT,
|
||
|
|
`h_name` VARCHAR(255) NULL DEFAULT '' COLLATE 'utf8mb3_general_ci',
|
||
|
|
`h_hidden` INT(11) NOT NULL DEFAULT '0',
|
||
|
|
`h_created_at` DATETIME NULL DEFAULT current_timestamp(),
|
||
|
|
`h_updated_at` DATETIME NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
|
||
|
|
PRIMARY KEY (`srl`) USING BTREE
|
||
|
|
)
|
||
|
|
COLLATE='utf8mb3_general_ci'
|
||
|
|
ENGINE=MyISAM");
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!sql_fetch_array(sql_query("DESC {$g5['hashtag_posts_table']}"))) {
|
||
|
|
sql_query("CREATE TABLE {$g5['hashtag_posts_table']} (
|
||
|
|
`post_id` INT(11) NOT NULL,
|
||
|
|
`board_id` VARCHAR(255) NOT NULL,
|
||
|
|
`srl` INT NOT NULL,
|
||
|
|
`created_at` DATETIME NULL DEFAULT current_timestamp(),
|
||
|
|
PRIMARY KEY (`post_id`, `board_id`, `srl`),
|
||
|
|
FOREIGN KEY (`srl`) REFERENCES {$g5['hashtags_table']}(`srl`) ON DELETE CASCADE
|
||
|
|
)
|
||
|
|
COLLATE='utf8mb3_general_ci'
|
||
|
|
ENGINE=MyISAM");
|
||
|
|
}
|
||
|
|
},
|
||
|
|
10
|
||
|
|
);
|
||
|
|
|
||
|
|
EventHandler::addEventHandler(
|
||
|
|
"gnuboard.bbs.write_update_document_create",
|
||
|
|
function ($args = null) use ($that) {
|
||
|
|
$that->writeUpdateDocumentUpdate($args);
|
||
|
|
}
|
||
|
|
,
|
||
|
|
10
|
||
|
|
);
|
||
|
|
|
||
|
|
EventHandler::addEventHandler(
|
||
|
|
"gnuboard.bbs.write_update_document_update",
|
||
|
|
function ($args = null) use ($that) {
|
||
|
|
$that->writeUpdateDocumentUpdate($args);
|
||
|
|
}
|
||
|
|
,
|
||
|
|
10
|
||
|
|
);
|
||
|
|
|
||
|
|
EventHandler::addEventHandler(
|
||
|
|
"gnuboard.bbs.delete",
|
||
|
|
function ($args = null) use ($that) {
|
||
|
|
$bo_table = $_POST["bo_table"];
|
||
|
|
$write = $args[0];
|
||
|
|
|
||
|
|
$that->delete_hashtags($bo_table, $write['wr_id']);
|
||
|
|
}
|
||
|
|
,
|
||
|
|
10
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function extract_hashtags($content)
|
||
|
|
{
|
||
|
|
preg_match_all('/#([A-Za-z0-9가-힣ㄱ-ㅎㅏ-ㅣ_]+)/', $content, $matches);
|
||
|
|
return $matches[1];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function delete_hashtags($bo_table, $wr_id)
|
||
|
|
{
|
||
|
|
global $g5;
|
||
|
|
sql_query("DELETE FROM {$g5['hashtag_posts_table']} WHERE `board_id` = '{$bo_table}' AND `post_id` = '{$wr_id}'");
|
||
|
|
$sql = "SELECT h.srl, COUNT(hp.srl) AS count
|
||
|
|
FROM {$g5['hashtags_table']} h
|
||
|
|
LEFT JOIN {$g5['hashtag_posts_table']} hp
|
||
|
|
ON h.srl = hp.srl
|
||
|
|
GROUP BY h.srl
|
||
|
|
HAVING count = 0";
|
||
|
|
|
||
|
|
$result = sql_query($sql);
|
||
|
|
|
||
|
|
while ($row = sql_fetch_array($result)) {
|
||
|
|
$delete_srl = $row['srl'];
|
||
|
|
$delete_sql = "DELETE FROM {$g5['hashtags_table']} WHERE srl = '$delete_srl'";
|
||
|
|
sql_query($delete_sql);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function proc_hashtags($bo_table, $wr_id, $content)
|
||
|
|
{
|
||
|
|
global $g5;
|
||
|
|
// if post update, reset hashtag data - arcturus
|
||
|
|
$this->delete_hashtags($bo_table, $wr_id);
|
||
|
|
|
||
|
|
$hashtags = $this->extract_hashtags($content);
|
||
|
|
foreach ($hashtags as $hashtag) {
|
||
|
|
$q = sql_fetch("SELECT `srl` FROM `{$g5['hashtags_table']}` WHERE `h_name` = '{$hashtag}'");
|
||
|
|
if (!$q) {
|
||
|
|
sql_query("INSERT INTO {$g5['hashtags_table']} SET `h_name` = '{$hashtag}', `h_hidden` = 0");
|
||
|
|
$q = sql_fetch("SELECT `srl` FROM `{$g5['hashtags_table']}` WHERE `h_name` = '{$hashtag}'");
|
||
|
|
}
|
||
|
|
sql_query("INSERT INTO {$g5['hashtag_posts_table']} SET `board_id` = '{$bo_table}', `post_id` = '{$wr_id}', `srl` = '{$q['srl']}'");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function writeUpdateDocumentUpdate($data)
|
||
|
|
{
|
||
|
|
global $is_admin;
|
||
|
|
|
||
|
|
if ($is_admin) {
|
||
|
|
$wr_content = '';
|
||
|
|
if (isset($_POST['wr_content'])) {
|
||
|
|
$wr_content = substr(trim($_POST['wr_content']), 0, 65536);
|
||
|
|
$wr_content = preg_replace("#[\\\]+$#", "", $wr_content);
|
||
|
|
}
|
||
|
|
|
||
|
|
$bo_table = isset($data[0]) ? $data[0] : '';
|
||
|
|
$wr_id = isset($data[2]) ? $data[2] : '';
|
||
|
|
|
||
|
|
$this->proc_hashtags($bo_table, $wr_id, $wr_content);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function printConfigForm()
|
||
|
|
{
|
||
|
|
// $config = $this->getConfig();
|
||
|
|
echo '현재 애드온에 별도의 설정이 없습니다.';
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getConfig()
|
||
|
|
{
|
||
|
|
global $g5;
|
||
|
|
|
||
|
|
$sql = "SELECT addon_config FROM {$g5['addons_config_table']} WHERE addon_name = '{$this->className}'";
|
||
|
|
$result = sql_fetch($sql);
|
||
|
|
|
||
|
|
if ($result && isset($result['addon_config'])) {
|
||
|
|
return json_decode($result['addon_config'], true) ?: [];
|
||
|
|
}
|
||
|
|
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
}
|