Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnuboard5-summernote 사용시 첨부된 이미지의 유효성 검증 및 수정/삭제/관리 #6

Open
dewoweb opened this issue Jan 11, 2016 · 3 comments

Comments

@dewoweb
Copy link
Collaborator

dewoweb commented Jan 11, 2016

MySQL 테이블 생성

CREATE TABLE 'g5_board_image' (
  'bo_table' varchar(20) NOT NULL,
  'wr_id' int(11) NOT NULL default '0',
  'bi_no' int(11) NOT NULL auto_increment,
  'bi_filename' varchar(100) NOT NULL default '',
  'bi_filedir' varchar(255) NOT NULL default '',
  'bi_fileurl' varchar(500) NOT NULL default '',
  'bi_filesize' int(11) NOT NULL default '0',
  'bi_datetime' datetime NOT NULL default '0000-00-00 00:00:00',
  'bi_tmpid' varchar(30) default NULL,
  PRIMARY KEY  ('bo_table','wr_id','bi_no')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

plugin/editor/summernote/editor.lib.php

global $g5, $config, $hl, $tmpid, $bo_table;
// 상단 부분 수정 최상단 함수(function)에 라인 추가 (global 변수 추가)

url: "<?php echo $editor_url; ?>/upload.php?bo_table=<?php echo $bo_table; ?>&tmpid=<?php echo $tmpid; ?>",
// ajax 호출부분 수정

plugin/editor/summernote/upload.php

$tmpid = $_POST['tmpid'];
// 상단에 추가

$data_dir = G5_DATA_PATH.'/editor/'.$bo_table;
$data_url = G5_DATA_URL.'/editor/'.$bo_table;
//위의 두 변수를 이렇게 바꾸어 주시면 파일 분류가 더 간단해 집니다.

// g5_board_image 테이블에 파일 정보 INSERT - 시작
$board_image['bo_table'] = $bo_table;
$board_image['bi_filename'] = $file_name;
$board_image['bi_filedir'] = $save_dir;
$board_image['bi_fileurl'] = $save_url;
$board_image['bi_filesize'] = $tmp_filesize;
$board_image['bi_datetime'] = G5_TIME_YMDHIS;
$board_image['bi_tmpid'] = $tmpid;

$board_image_insert = "INSERT INTO g5_board_image
SET
bo_table = '{$board_image['bo_table']}',
bi_filename = '{$board_image['bi_filename']}',
bi_filedir = '{$board_image['bi_filedir']}',
bi_fileurl = '{$board_image['bi_fileurl']}',
bi_filesize = '{$board_image['bi_filesize']}',
bi_datetime = '{$board_image['bi_datetime']}',
bi_tmpid = '{$board_image['bi_tmpid']}'";
sql_fetch($board_image_insert);
unset($board_image);
unset($board_image_insert);
// g5_board_image 테이블에 파일 정보 INSERT - 끝

bbs/write.php

function Generate_Random_String ($length)   // ** 인수로 지정된 길이만큼 무작위 문자열 리턴
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyz';
    $string_generated = '';
    $nmr_loops = $length;
    while ($nmr_loops--) {
        $string_generated .= $characters[mt_rand(0, strlen($characters))];
    }
    unset($length);
    unset($characters);
    unset($nmr_loops);
    unset($length);
    return $string_generated;
}
// summernote 에디터의 upload.php를 통한 이미지 업로드를 위하여 임시 식별 변수 생성
$tmpid = $member['mb_id'].'_'.Generate_Random_String(8);

write.skin.php

\<input type="hidden" name="tmpid" value="<?php echo $tmpid ?>">

bbs/write_update.php

// 업로드한 이미지 정보 갱신
sql_query("UPDATE g5_board_image SET wr_id = '{$wr_id}' WHERE bo_table = '{$bo_table}' AND bi_tmpid = '{$tmpid}'");

// 업로드한 게시물 본문 내용($wr_content) 중에 존재하지 이미지가 업로드되어 있다면 해당 이미지파일 삭제 및 이미지 정보 테이블에서 행 삭제
$wr_content_for_del_img = strtolower($wr_content);
$sql_board_image_list = " SELECT  bi_filename, bi_filedir FROM g5_board_image WHERE bo_table = '{$bo_table}' AND wr_id = '{$wr_id}' ";
$board_image_list = sql_query($sql_board_image_list);
while ($image_list = sql_fetch_array($board_image_list)) {
if (!stripos($wr_content_for_del_img, $image_list['bi_filename'])) {
@unlink($image_list['bi_filedir']);
sql_query("DELETE FROM g5_board_image WHERE bi_filedir = '{$image_list['bi_filedir']}'");
}
}
unset($wr_content_for_del_img);
unset($sql_board_image_list);
unset($board_image_list);
unset($image_list);

bbs/delete.php

// 이미지 파일 삭제 - 시작
$sql_board_image_list = " SELECT  bi_filename, bi_filedir FROM g5_board_image WHERE bo_table = '{$bo_table}' AND wr_id = '{$row['wr_id']}' ";
$board_image_list = sql_query($sql_board_image_list);
        while ($image_list = sql_fetch_array($board_image_list)) {
            @unlink($image_list['bi_filedir']);
            if(preg_match("/\.({$config['cf_image_extension']})$/i", $image_list['bi_filename'])) {
                delete_board_thumbnail($bo_table, $image_list['bi_filename']);
            }
        }
sql_query(" DELETE FROM g5_board_image where bo_table = '$bo_table' AND wr_id = '{$row['wr_id']}' ");
// 이미지 파일 삭제 - 끝

bbs/delete_all.php

// 이미지 파일 삭제 - 시작
$sql_board_image_list = " SELECT  bi_filename, bi_filedir FROM g5_board_image WHERE bo_table = '{$bo_table}' AND wr_id = '{$row['wr_id']}' ";
$board_image_list = sql_query($sql_board_image_list);
while ($image_list = sql_fetch_array($board_image_list)) {
@unlink($image_list['bi_filedir']);
if(preg_match("/\.({$config['cf_image_extension']})$/i", $image_list['bi_filename'])) {
delete_board_thumbnail($bo_table, $image_list['bi_filename']);
}
}
sql_query(" DELETE FROM g5_board_image where bo_table = '$bo_table' AND wr_id = '{$row['wr_id']}' ");
// 이미지 파일 삭제 - 끝

adm/board_delete.inc.php

// summernote 이미지 삭제
sql_query(" delete from g5_board_image where bo_table = '{$tmp_bo_table}' ");

// summernote 이미지 폴더 전체 삭제
rm_rf(G5_DATA_PATH.'/editor/'.$tmp_bo_table);
@dewoweb
Copy link
Collaborator Author

dewoweb commented Jan 11, 2016

easylogic님의 코드에 적절히 추가하시면
summernote 에디터 사용시 첨부한 이미지파일이
더미로 남지않고 잘 관리됩니다.

예를 들어서 A라는 게시물을 작성할때 3개의 이미지를 첨부하였다가
2개를 textarea에서 삭제하고 submit 하였을 경우 기존에는 3개가 모두 서버에 업로드 되었으나
이 코드를 적용하면 하나만 업로드됩니다. 작성된 글을 수정할 때도 마찬가지로 적용됩니다.

글을 삭제할 때
유저 : delete.php
관리자 : delete_all.php
관리자 모드 에서 게시판 자체 삭제시 : board_delete.inc.php
에 해당 코드를 적용하면 게시물의 wr_id혹은 게시판 자체를 추적하여 삭제합니다.

이미지 파일의 상세 정보 이력은 'g5_board_image 테이블에서 관리됩니다.

위의 코드들의 위치는 적절하게 삽입하셔야 합니다.
저는 좀 불친절해서요 hardlogic 입니다.ㅠㅠ...^^; 아마 쉽게 아실수 있을겁니다.

@dewoweb
Copy link
Collaborator Author

dewoweb commented Jan 11, 2016

github 사용하면서 받을줄만 알았지
업로드는 해본적이 없어서 무식하게 올렸군요.
코드 관리하는 기능을 활용을 했어야 하는데. 차차 적응해보도록 하겠습니다.
그리고... easylogic님... 저는 스마트에디터가 너무 잡스러워서 싫은지라
섬머노트를 잘 가꾸어서 그누보드에서 사용해보고 싶습니다.
단순히 easylogic 님의 작업물을 가져가는 것이 아니라 같이 협업하여 완성도를 높여가는데
조금이나마 일조할 수 있었으면 하는 바램입니다.
감사합니다.

@easylogic
Copy link
Owner

@dewoweb님 너무 소중한 코드네요.. 감사합니다.

현재 gnuboard5-summernote 자체가 plugin 기준으로 관리가 되는거라 gnuboard 의 core 소스를 바로 수정하기는 조금 힘들 듯 합니다.

그래도 너무 좋은 코드들이네요.

나름 가이드 라인을 추가 해서 배포하는 것도 괜찮을 것 같아요.

markdown 형태로 활용법을 정리해서 코드에 넣어 둘게요.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants