IT

그누보드_에디터로 올린 이미지 삭제하는 코드 (썸네일도 삭제)

elisha2672 2019. 11. 15. 23:43

현재 그누보드는 에디터로 첨부한 이미지는 글 삭제시에도 함께 삭제되지 않고 서버에 남아있습니다.

아래 코드를 적용하시면 에디터로 첨부한 이미지가 있는 글을 삭제할 경우 해당 이미지도 삭제하게 됩니다.

 

/lib/common.lib.php 에서 기존 

 

// 에디터 썸네일 삭제
function delete_editor_thumbnail($contents)
 

함수 내용을 아래와 같이 변경한다.

 

// 에디터 썸네일 삭제
function delete_editor_thumbnail($contents)
{
    if(!$contents)
        return;

    // $contents 중 img 태그 추출
    $matchs = get_editor_image($contents, false);

    if(!$matchs)
        return;

    for($i=0; $i<count($matchs[1]); $i++) {
        // 이미지 path 구함
        $imgurl = @parse_url($matchs[1][$i]);


        if(strpos($imgurl['path'], "/data/") != 0) {
            $data_path = preg_replace("/^\/.*\/data/", "/data", $imgurl['path']);
        } else {
            $data_path = $imgurl['path'];
        }

        $is_destfile = false;
        if(preg_match('/(gif|jpe?g|bmp|png)$/i', strtolower(end(explode('.', $data_path))))){

            $destfile = ( ! preg_match('/\w+\/\.\.\//', $data_path) ) ? G5_PATH.$data_path : '';

            if($destfile && preg_match('/\/data\/editor\/[A-Za-z0-9_]{1,20}\//', $destfile) && is_file($destfile)) {
                $is_destfile = true;
            }
        }
        
        if($is_destfile) {
            //원본파일 삭제
            @chmod($destfile, G5_FILE_PERMISSION);
            @unlink($destfile);
            
            //썸네일파일 삭제
            $files = glob(dirname($destfile).'/thumb-'.preg_replace("/\.[^\.]+$/i", "", basename($imgurl['path'])).'*');
            //return $files;
            if (is_array($files)) {
                foreach($files as $filename)
                    unlink($filename);
            }
            
        }


    }
}