라이믹스
라이믹스 입력폼. 비밀번호 암호화
xemaker
2026. 3. 10. 13:15
이전글과 비슷하지만 패스워드를 암호화함. 그래야 나중에 조회시 사용할 수 있음.
<?php
// 1. 에러가 화면에 텍스트로 찍히면 JSON이 깨지므로 일단 끕니다.
error_reporting(0);
ini_set('display_errors', 0);
define('__XE__', true);
// 2. 경로 자동 탐색 (현재 파일 위치 기준으로 config 파일을 찾습니다)
$config_path = $_SERVER['DOCUMENT_ROOT'] . '/config/config.inc.php';
if (!file_exists($config_path)) {
// 만약 루트가 아니라면 상대 경로로 다시 시도
$config_path = dirname(__FILE__) . '/../config/config.inc.php';
}
if (!file_exists($config_path)) {
header('Content-Type: application/json');
die(json_encode(array('error' => -1, 'message' => 'Config file not found.')));
}
require_once($config_path);
$oContext = &Context::getInstance();
$oContext->init();
// 3. 입력값 받기
$title = $_POST['title'];
$content = $_POST['content'];
$nick_name = $_POST['nick_name'] ?: '비회원';
$password = $_POST['password'] ?: '1111';
$module_srl = 51;
header('Content-Type: application/json');
try {
if(!$title || !$content) throw new Exception('제목 또는 내용이 비어있습니다.');
// 4. 라이믹스 비밀번호 해싱
$memberModel = getModel('member');
if(!$memberModel) throw new Exception('Member 모델 로드 실패');
$hash_password = $memberModel->hashPassword($password);
// 5. DB 입력
$oDB = DB::getInstance();
$document_srl = getNextSequence();
$regdate = date('YmdHis');
$query = "INSERT INTO documents
(document_srl, module_srl, title, content, user_id, nick_name, password, regdate, last_update, list_order, update_order, comment_status, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$args = array(
$document_srl, $module_srl, $title, $content,
0, $nick_name, $hash_password,
$regdate, $regdate,
$document_srl * -1, $document_srl * -1,
'ALLOW', 'SECRET'
);
$stmt = $oDB->prepare($query);
$result = $stmt->execute($args);
if($result) {
echo json_encode(array('error' => 0, 'message' => 'success'));
} else {
throw new Exception('DB Insert 실패');
}
} catch (Exception $e) {
// 에러 발생 시에도 JSON 규격에 맞춰 응답
echo json_encode(array('error' => -1, 'message' => $e->getMessage()));
}