5.4 HOOK을 이용한 스마트에디터 첨부파일관리 정보
5.4 HOOK을 이용한 스마트에디터 첨부파일관리첨부파일
본문
안녕하세요. 스마트에디터에서 첨부한 파일의 경우 서버에 그대로 남아있어서 관리할 수 있는걸 만들어 봤습니다.
1. 스마트에디터의 수정이 필요합니다.
/plugin/editor/smarteditor2/photo_uploader/popup/php/index.php
if( $is_editor_upload ) {
require('UploadHandler.php');
$options = array(
'upload_dir' => $data_dir,
'upload_url' => $data_url,
// This option will disable creating thumbnail images and will not create that extra folder.
// However, due to this, the images preview will not be displayed after upload
'image_versions' => array()
);
$upload_handler = new UploadHandler($options);
//파일 업로드후 등록을 위해 추가
$files = $upload_handler->files;
if( $files[0] ) {
$sql = "insert into g5_editor_file set ef_file='{$files[0]}', upload_dir='{$data_dir}', ef_datetime='". G5_TIME_YMDHIS ."'";
sql_query($sql);
}
} else {
echo json_encode(array('files'=>array('0'=>array('error'=>'정상적인 업로드가 아닙니다.'))));
exit;
}
위 코드를 참고하셔서 대략 53줄 이후로 추가해주시면 됩니다.
2. extend 폴더에 아래 소스를 추가해줍니다.
새로 파일을 만드셔도 되고 기존파일에 추가해주셔도 됩니다.
<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가
$editor_table = sql_fetch("select count(*) as cnt from INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'g5_editor_file' and table_schema = '". G5_MYSQL_DB ."'");
if( !$editor_table['cnt'] ) {
$sql = "
CREATE TABLE `g5_editor_file` (
`ef_no` int(11) NOT NULL AUTO_INCREMENT,
`bo_table` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`wr_id` int(11) NOT NULL,
`ef_file` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`upload_dir` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`ef_datetime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`ef_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
sql_query($sql, true);
}
add_event('common_header', 'fn_common_header_editor',0,0);
function fn_common_header_editor() {
$date = date("Y-m-d 23:59:59", strtotime("-2 days", G5_SERVER_TIME));
$sql = "select * from g5_editor_file where bo_table='' and ef_datetime <= '{$date}'";
$result = sql_query($sql);
while($row = sql_fetch_array($result)) {
delete_editor_file($row['upload_dir'], $row['ef_file']);
$sql = "delete from g5_editor_file where ef_file='{$row['ef_file']}'";
sql_query($sql);
}
}
add_event('write_update_after','fn_write_update_after_editor', 0, 5);
function fn_write_update_after_editor($board, $wr_id, $w, $qstr, $redirect_url) {
preg_match_all("/<img[^>]*src=[\"']?([^>\"']+)[\"']?[^>]*>/i", stripslashes($_POST['wr_content']), $matches);
$match = $matches[1];
$filelist = [];
foreach($match as $val) {
$filename = basename($val);
$filelist[] = $filename;
$sql = "select count(*) as cnt from g5_editor_file where ef_file='{$filename}'";
$row = sql_fetch($sql);
if( $row['cnt'] > 0 ) { // 파일이 있으면 게시판에 연결시키고 없으면 업로드된 파일이 있는지 확인하여 테이블에 입력시켜준다.
$sql = "update g5_editor_file set bo_table='{$board['bo_table']}', wr_id='{$wr_id}' where ef_file='{$filename}'";
sql_query($sql);
} else {
$parse_url = parse_url($val);
$path = $parse_url['path'];
$upload_dir = G5_PATH.str_replace($filename,"", $path);
if( file_exists($upload_dir.$filename) ) {
$sql = "
insert into g5_editor_file
set bo_table='{$board['bo_table']}',
wr_id='{$wr_id}',
ef_file='{$filename}',
upload_dir='{$upload_dir}',
ef_datetime='". G5_TIME_YMDHIS ."'";
sql_query($sql);
}
}
}
if( $w == 'u' ) {
$sql = "
select * from g5_editor_file
where bo_table='{$board['bo_table']}' and wr_id='{$wr_id}' and
ef_file not in ('". implode("','", $filelist) ."') ";
$result = sql_query($sql);
while($row = sql_fetch_array($result)) {
delete_editor_file($row['upload_dir'], $row['ef_file']);
$sql = "delete from g5_editor_file where ef_file='{$row['ef_file']}'";
sql_query($sql);
}
}
}
add_event('bbs_delete', 'fn_bbs_delete_editor', 0, 2);
function fn_bbs_delete_editor($write, $board) {
$sql = "
select * from g5_editor_file
where bo_table='{$board['bo_table']}' and wr_id='{$write['wr_id']}'";
$result = sql_query($sql);
while($row = sql_fetch_array($result)) {
delete_editor_file($row['upload_dir'], $row['ef_file']);
}
$sql = "delete from g5_editor_file where bo_table='{$board['bo_table']}' and wr_id='{$write['wr_id']}'";
sql_query($sql);
}
add_event('bbs_delete_all', 'fn_bbs_delete_all_editor', 0, 2);
function fn_bbs_delete_all_editor($tmp_array, $board) {
for ($i=0;$i<count($tmp_array);$i++)
{
$write = sql_fetch(" select * from $write_table where wr_id = '$tmp_array[$i]' ");
if( !$write['wr_id'] ) {
$sql = "
select * from g5_editor_file
where bo_table='{$board['bo_table']}' and wr_id='{$tmp_array[$i]}'";
$result = sql_query($sql);
while($row = sql_fetch_array($result)) {
delete_editor_file($row['upload_dir'], $row['ef_file']);
}
$sql = "delete from g5_editor_file where bo_table='{$board['bo_table']}' and wr_id='{$tmp_array[$i]}'";
sql_query($sql);
}
}
}
function delete_editor_file($upload_dir, $filename) {
$file_path = $upload_dir.$filename;
$delete_file = run_replace('delete_file_path', $file_path, $row);
if( file_exists($delete_file) ){
@unlink($delete_file);
}
// 썸네일삭제
$fn = preg_replace("/\.[^\.]+$/i", "", $filename);
$files = glob($upload_dir.'thumb-'.$fn.'*');
if (is_array($files)) {
foreach ($files as $filename)
unlink($filename);
}
}
도움이 되었으면 하는 바람에서 공유해봅니다^^~
12