소스 파일 : /navigation/naviUpdate.php (2020-12-28)     소스 설명 : navigation 업데이트
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php // (2022.9.13, 차재복, Cha Jae Bok, http://www.ktword.co.kr)

# 세션 설정
	// 세션 스타트 (매 웹페이지 마다 필요)
	session_start();

	// 세션 변수에 따른 사용자 타입 설정 ('일반사용자' or '테스트편집자' or '정식편집자' or '종합관리자')
	$_SESSION['user_type'] = ( empty($_SESSION['user_name']) ? '일반사용자' : $_SESSION['user_name'] );


# 편집 권한 확인
	if ($_SESSION['user_type']!='종합관리자') {
		echo json_encode(array('err_msg'=>'편집권한없음!'), JSON_UNESCAPED_UNICODE);
		exit;
	}
	// ('일반사용자' or '테스트편집자' or '정식편집자' or '종합관리자')

# db 접속 (dbi)
	include_once "../base_utils/db_conn.php";

# 전송 입력 매개변수
	$type = $_POST['type'];
		$type = trim($type);

	$choice = $_REQUEST['choice']; //$_POST['choice'];
		$choice = trim($choice);

	$subtype = $_POST['subtype'];
		$subtype = trim($subtype);

	$no = $_POST['no'];
		if ( isset($_REQUEST['no']) and !empty($no) and !is_numeric($no) or $no<0 ) exit('잘못된 update no'); // 해킹방지 (no>0)

	$id = $_REQUEST['id'];
		if ( isset($_REQUEST['id']) and !empty($id) and !is_numeric($id) or $id<0 ) exit('잘못된 update id'); // 해킹방지 (id>0)

	$msg = $_POST['msg'];	// name(title), parent
		$msg = trim($msg);

	$text = $_POST['text'];	// yoyak(desc), content, current
		$text = trim($text);

# 전송 매개변수에 따라, 해당 update 함수 호출
	if($choice == 'word_gubun_name_yoyak_update' 
			or $choice == 'source_name_expr_update' 
			or $choice == 'manual_name_yoyak_update'
            or $choice == 'word_titlename_yoyak_update')  {
		// update 함수 호출
		title_yoyak_update($choice, $id, $no, $msg, $text, $dbi);
	} else if($choice == 'db_tbl_fld_comment_update') {
		db_tbl_fld_comment_update($id, $no, $text, $dbi);
	} else if($choice == 'treeNodeCheck' and $type == 'word') {
        include_once "naviUpdate_utils.php";
        treeNodeCheck($id, $dbi);
            // 체크 대상 : path2node, depth, child, linkedNum
	} else if($choice == 'wordEditUpdate') {
        wordEditUpdate ($id, $no, $text, $dbi);
	} else if($choice == 'deleteWordUpdate') {
        deleteWordUpdate ($id, $no, $dbi);
	} else if($choice == 'sourceFileUpdate') {
        include_once "naviUpdate_source.php";
        sourceFileUpdate ($id, $dbi);
	} else if($choice == 'addSrcFile') {
        include_once "naviUpdate_source.php";
        $parent = $msg;
        $current = $text;
        addSrcFile($parent, $current, $dbi);
	} else if($choice == 'deleteSrcFile') {
        include_once "naviUpdate_source.php";
        deleteSrcFile($id, $no, $dbi);
	} else if($choice == 'editSrcFileFetch') {
        include_once "naviUpdate_source.php";
        editSrcFileFetch($id, $dbi);
	} else if($choice == 'editSrcFileUpdate') {
        include_once "naviUpdate_source.php";
        editSrcFileUpdate($id, $msg, $dbi);
    } else {
		echo json_encode( array('err_msg'=>'해당되는 choice 없음'), JSON_UNESCAPED_UNICODE);			
	} 

// (디버깅용)
//echo json_encode(array('err_msg'=>"type=$type, id=$id, msg=$msg, text=$text"), JSON_UNESCAPED_UNICODE);

# 해당 id의 name 및 yoyak 업데이트
function title_yoyak_update($choice, $id, $no, $msg, $text, $dbi) {

    // $choice에 따라, 다른 테이블, 필드명, where절 적용
    if($choice == 'word_gubun_name_yoyak_update') { 
        $table_name = 'gubun_tree_v2';
        $name_fld = 'name';
        $where = "id={$id}";
    } else if($choice == 'source_name_expr_update') {
        $table_name = 'src_files_v3';
        $name_fld = 'name';
        $where = "id={$id}";
    } else if($choice == 'manual_name_yoyak_update') {
        $table_name = 'reform';
        $name_fld = 'name';
        $where = "id={$id}";
    } else if($choice == 'word_titlename_yoyak_update') {
    	$table_name = 'book_idx';
        $name_fld = 'titlename';
        $where = "tree_id={$id} and no={$no}";
    } else return;

	// 해킹 방어
	$name = mysqli_real_escape_string($dbi,$msg);
	$yoyak = mysqli_real_escape_string($dbi,$text);

    // name,yoyak 입력 문자열의 비정상 처리
    if($name==='undefined' and $yoyak==='undefined') exit('입력 문자열 오류');
    if($name==='undefined' and $yoyak!=='undefined') $set = "yoyak='{$yoyak}'";
    if($name!=='undefined' and $yoyak==='undefined') $set = "{$name_fld}='{$name}'";
    if($name!=='undefined' and $yoyak!=='undefined') $set = "{$name_fld}='{$name}',yoyak='{$yoyak}'";

	// name, yoyak 교체 쿼리
//	$query = "update {$table_name} set name='{$name}',yoyak='{$yoyak}' where id={$id} limit 1";
	$query = "update {$table_name} set {$set} where {$where} limit 1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// db 처리 결과의 에러 유무에 따라 판단 수행
	if (!empty($err_msg)){
		$return = array('err_msg'=>$err_msg .' : 업데이트 실패');
	} else {
		if (mysqli_affected_rows($dbi) > 0){
			$return = array('notice'=>'업데이트 성공 !!!','title'=>$msg,'desc'=>$text);
		} else {
			$return = array('notice'=>'업데이트 안함');
		}
	}

	// 송출
	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}

/*
# 해당 no의 title_name 및 yoyak 업데이트
function word_titlename_yoyak_update($id, $no, $msg, $text, $dbi) {

	$table_name = 'book_idx';

	// 해킹 방어
	$name = mysqli_real_escape_string($dbi,$msg);
	$yoyak = mysqli_real_escape_string($dbi,$text);

    // name,yoyak 입력 문자열의 비정상 처리
    if($name==='undefined' and $yoyak==='undefined') exit('입력 문자열 오류');
    if($name==='undefined' and $yoyak!=='undefined') $set = "yoyak='{$yoyak}'";
    if($name!=='undefined' and $yoyak==='undefined') $set = "titlename='{$name}'";
    if($name!=='undefined' and $yoyak!=='undefined') $set = "titlename='{$name}',yoyak='{$yoyak}'";

	// titlename,yoyak 교체 쿼리
	$query = "update {$table_name} set titlename='{$name}',yoyak='{$yoyak}' where no={$no} and tree_id={$id} limit 1";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// db 처리 결과의 에러 유무에 따라 판단 수행
	if (!empty($err_msg)){
		$return = array('err_msg'=>$err_msg .' : 업데이트 실패');
	} else {
		if (mysqli_affected_rows($dbi) > 0){
			$return = array('notice'=>'업데이트 성공 !!!','title'=>$msg,'desc'=>$text);
		} else {
			$return = array('notice'=>'업데이트 안함');
		}
	}

	// 송출
	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}
*/

#
function db_tbl_fld_comment_update($id, $no, $text, $dbi) {
	// id: db table 번호, no : field 번호, text : comment
//	$return = array('err_msg'=>'id='.$id.', no='.$no.', text='.$text);

	if(!empty($_SESSION['db_name'])) $db_name = $_SESSION['db_name'];
	else $db_name = 'test';
	$query = "select (@row_number:=@row_number + 1) as id, 
						table_name as name, 
						table_comment as yoyak, 
						'1' as child,
						'table' as item_type
				from (select @row_number:=0) as t, information_schema.tables
				where table_schema='{$db_name}'";
	$result = mysqli_query($dbi,$query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
	while (	$matched = mysqli_fetch_assoc($result) ) {
		if($matched['id']==$id) {
			$yoyak = mysqli_real_escape_string($dbi,$text);
			// table
			if(empty($no)) {
				$query = "alter table {$matched['name']} comment='{$yoyak}'";
				$update = mysqli_query($dbi,$query);
					if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
				$return = array('desc'=>$text);
			// field
			} else {
				$query = "select (@row_number:=@row_number + 1) as id,
								column_name, 
								column_type 
							from (select @row_number:=0) as t, information_schema.columns 
							where table_name = '{$matched['name']}' 
									and table_schema = '{$db_name}'
							order by column_name";
				$sub_result = mysqli_query($dbi,$query);
					if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
				while (	$column = mysqli_fetch_assoc($sub_result) ) {
					if($no == $column[id]) {
						$query = "alter table {$matched['name']} modify {$column['column_name']} {$column['column_type']} comment '{$text}'";  
						$column_result = mysqli_query($dbi,$query);
							if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
						$return = array('desc'=>$text);			
					}
				}
			}
		}
	}
	
	// 송출
	echo json_encode($return, JSON_UNESCAPED_UNICODE);

}


function wordEditUpdate($id, $no, $text, $dbi) {

	$pure_abbr = mysqli_real_escape_string($dbi, $text);

	// abbr 변환
	include_once '../navigation/naviStrTransform.php';
	$trans_abbr = string_transform($text, $dbi, $no);

    // 링크 대체되지 않은 (변환되지 않은) 원래 abbr 업데이트
	$query = "update cjb_dict set abbr='{$pure_abbr}' where no={$no} limit 1";
	$result = mysqli_query($dbi, $query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

    // 요약(synopsis) 업데이트
	$synopsis = synopsis_extract($pure_abbr);
	$query = "update cjb_dict set synopsis='{$synopsis}' where no=$no limit 1";
	$result = mysqli_query($dbi, $query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

	// abbr 변환 후 dict_anchor 업데이트
	$escaped_abbr = mysqli_real_escape_string($dbi,$trans_abbr);
	$query = "update dict_anchor set abbr='{$escaped_abbr}' where no={$no} limit 1";
	$result = mysqli_query($dbi, $query);
		if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }

    // 정상 업데이트 여부 확인
	if (mysqli_affected_rows($dbi) <= 0)
        $err_msg .= "\n dict_anchor abbr 텍스트가 업데이트 되지는 않았음 (기존과 동일)";
    
    // 결과 송출
	include_once '../navigation/naviFetch_utils.php';
    ajaxFetchDetailSeparate($no, $dbi, $err_msg);

}

function deleteWordUpdate ($parent_id, $child_no, $dbi) {
    // prepare_sql 함수
    include_once "naviUpdate_utils.php";

    // book_idx에서 해당 no 삭제
    $query = "delete from book_idx where tree_id=? and no=? limit 1";
    include_once "naviUpdate_utils.php";
    $resultArr = prepare_sql($dbi, $query, 'ii', array($parent_id,$child_no));
    if(mysqli_stmt_affected_rows($resultArr['stmt'])<=0)
        $err_msg .= 'book_idx에서 해당 no 삭제 실패';

    // 부모 node의 child,linked_num 계산 해주기 
    if(empty($err_msg)) {
        $query = "update gubun_tree_v2 
                    set child = (@linked_num:=(select count(*) from book_idx where tree_id=?)) 
                                + (select count(*) from gubun_tree_v2 where parent=?),
                        linked_num = @linked_num
                    where id=? limit 1";
        $resultArr = prepare_sql($dbi, $query, 'iii', array($parent_id,$parent_id,$parent_id));
        if(mysqli_stmt_affected_rows($resultArr['stmt'])<=0)
            $err_msg .= '부모 node의 child,linked_num 계산 실패';
    }

    // book_idx에서 seq 다시 계산
    if(empty($err_msg)) {
        mysqli_query($dbi,"select 0 into @num");
        $query = "update book_idx set list_ord=@num:=(@num+1) where tree_id=? order by list_ord";
        $resultArr = prepare_sql($dbi, $query, 'i', array($parent_id));
	    if (mysqli_errno($dbi)) { $err_msg .= mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n"; }
    }

	// 송출
    $return = array('err_msg'=>$err_msg);
	echo json_encode($return, JSON_UNESCAPED_UNICODE);
}


function moveAfterNode($dbi){

}

?>


Copyrightⓒ written by 차재복 (Cha Jae Bok)
"본 웹사이트 내 모든 저작물은 원출처를 밝히는 한 자유롭게 사용(상업화포함) 가능합니다"