diff --git a/f_func/func.php b/f_func/func.php
index 1b6b9c33..4cddb5b9 100644
--- a/f_func/func.php
+++ b/f_func/func.php
@@ -162,15 +162,45 @@ function generateFileUsingSimpleTemplate(string $srcFilePath, string $destFilePa
if(file_exists($destFilePath) && !$canOverwrite){
return 'destFilePath is already exists';
}
- if(!is_writable($destFilePath)){
- return 'destFilePath is not writabble';
+ if(!is_writable(dirname($destFilePath))){
+ return 'destFilePath is not writable';
}
$text = file_get_contents($srcFilePath);
foreach($params as $key => $value){
- $text = str_replace("_tK_{$key}_", $value);
+ $text = str_replace("_tK_{$key}_", $value, $text);
}
file_put_contents($destFilePath, $text);
return true;
+}
+
+/**
+ * '비교적' 안전한 int 변환
+ * null -> null
+ * int -> int
+ * float -> int
+ * numeric(int, float) 포함 -> int
+ * 기타 -> 예외처리
+ *
+ * @return int|null
+ */
+function toInt($val, $force=false){
+ if($val === null){
+ return null;
+ }
+ if(is_int($val)){
+ return $val;
+ }
+ if(is_numeric($val)){
+ return intval($val);//
+ }
+ if($val === 'NULL' || $val === 'null'){
+ return null;
+ }
+
+ if($force){
+ return intval($val);
+ }
+ throw new InvalidArgumentException('올바르지 않은 타입형 :'.$val);
}
\ No newline at end of file
diff --git a/f_install/install.php b/f_install/install.php
index bb531c39..fab1e230 100644
--- a/f_install/install.php
+++ b/f_install/install.php
@@ -36,11 +36,16 @@ session_start();
-
+
diff --git a/f_install/j_setup_db.php b/f_install/j_setup_db.php
new file mode 100644
index 00000000..6f6e554a
--- /dev/null
+++ b/f_install/j_setup_db.php
@@ -0,0 +1,146 @@
+false,
+ 'reason'=>'DB 접속에 실패했습니다.'
+ ]);
+}
+
+function dbSQLFail($params){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'SQL을 제대로 실행하지 못했습니다. DB상태를 확인해 주세요.'
+ ]);
+}
+
+$host = util::array_get($_POST['db_host']);
+$port = util::array_get($_POST['db_port']);
+$username = util::array_get($_POST['db_id']);
+$password = util::array_get($_POST['db_pw']);
+$dbName = util::array_get($_POST['db_name']);
+
+if(!$host || !$port || !$username || !$password || !$dbName){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'입력 값이 올바르지 않습니다'
+ ]);
+}
+
+if(file_exists(ROOT.'/d_setting/conf.php') && is_dir(ROOT.'/d_setting/conf.php')){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'d_setting/conf.php 가 디렉토리입니다'
+ ]);
+}
+
+if($SETTING->isExist()){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'이미 conf.php 파일이 있습니다'
+ ]);
+}
+
+//파일 권한 검사
+if(file_exists(ROOT.'/d_pic') && !is_dir(ROOT.'/d_pic')){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'d_pic 이 디렉토리가 아닙니다'
+ ]);
+}
+
+if(file_exists(ROOT.'/d_log') && !is_dir(ROOT.'/d_log')){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'d_log 가 디렉토리가 아닙니다'
+ ]);
+}
+
+if(!file_exists(ROOT.'/d_setting')){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'d_setting 이 존재하지 않습니다'
+ ]);
+}
+
+if(!is_writable(ROOT.'/d_pic')){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'d_pic 디렉토리의 쓰기 권한이 없습니다'
+ ]);
+}
+
+if(!is_writable(ROOT.'/d_log')){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'d_log 디렉토리의 쓰기 권한이 없습니다'
+ ]);
+}
+
+if(!is_writable(ROOT.'/d_setting')){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>'d_setting 디렉토리의 쓰기 권한이 없습니다.'
+ ]);
+}
+
+//기본 파일 생성
+if(!file_exists(ROOT.'/d_pic')){
+ mkdir(ROOT.'/d_pic');
+}
+
+if(!file_exists(ROOT.'/d_log')){
+ mkdir(ROOT.'/d_log');
+}
+
+if(!file_exists(ROOT.'/d_log/.htaccess')){
+ @file_put_contents(ROOT.'/d_log/.htaccess', 'Deny from all');
+}
+
+if(!file_exists(ROOT.'/d_setting/.htaccess')){
+ @file_put_contents(ROOT.'/d_setting/.htaccess', 'Deny from all');
+}
+
+//DB 접근 권한 검사
+
+$rootDB = new MeekroDB($host,$username,$password,$dbName,$port,'utf8');
+$rootDB->connect_options[MYSQLI_OPT_INT_AND_FLOAT_NATIVE] = true;
+
+$rootDB->throw_exception_on_nonsql_error = false;
+$rootDB->nonsql_error_handler = 'dbConnFail';
+$rootDB->error_handler = 'dbSQLFail';
+
+$mysqli_obj = $rootDB->get(); //로그인에 실패할 경우 자동으로 dbConnFail()이 실행됨.
+
+$mysqli_obj->multi_query(file_get_contents(__dir__.'/sql/common_schema.sql'));
+$globalSalt = bin2hex(random_bytes(16));
+
+$result = generateFileUsingSimpleTemplate(
+ ROOT.'/d_setting/conf.orig.php',
+ ROOT.'/d_setting/conf.php',[
+ 'host'=>$host,
+ 'user'=>$username,
+ 'password'=>$password,
+ 'dbName'=>$dbName,
+ 'port'=>$port,
+ 'globalSalt'=>$globalSalt
+ ]
+);
+
+if($result !== true){
+ returnJson([
+ 'result'=>false,
+ 'reason'=>$result
+ ]);
+}
+
+returnJson([
+ 'result'=>true,
+ 'reason'=>'NYI'
+]);
\ No newline at end of file
diff --git a/js/install.js b/js/install.js
index 5c6d309d..b919b94f 100644
--- a/js/install.js
+++ b/js/install.js
@@ -40,4 +40,66 @@ function changeInstallMode(){
$(document).ready( function () {
changeInstallMode();
+
+ $('#db_form').validate({
+ rules:{
+ db_host:"required",
+ db_port:"required",
+ db_id:"required",
+ db_pw:"required",
+ db_name:"required"
+ },
+ errorElement: "div",
+ errorPlacement: function ( error, element ) {
+ // Add the `help-block` class to the error element
+ error.addClass( "invalid-feedback" );
+
+ if ( element.prop( "type" ) === "checkbox" ) {
+ error.insertAfter( element.parent( "label" ) );
+ } else {
+ error.insertAfter( element );
+ }
+ },
+ highlight: function ( element, errorClass, validClass ) {
+ $( element ).addClass( "is-invalid" ).removeClass( "is-valid" );
+ },
+ unhighlight: function (element, errorClass, validClass) {
+ $( element ).addClass( "is-valid" ).removeClass( "is-invalid" );
+ }
+ })
+ $('#db_form').submit(function(e){
+ e.preventDefault();
+ if(!$("#db_form").valid()){
+ return;
+ }
+ $.ajax({
+ cache:false,
+ type:'post',
+ url:'j_setup_db.php',
+ dataType:'json',
+ data:{
+ db_host:$('#db_host').val(),
+ db_port:$('#db_port').val(),
+ db_id:$('#db_id').val(),
+ db_pw:$('#db_pw').val(),
+ db_name:$('#db_name').val()
+ }
+ }).then(function(result){
+ var deferred = $.Deferred();
+
+ if(!result.result){
+ alert(result.reason);
+ deferred.reject('fail');
+ }
+ else{
+ alert('conf.php가 생성되었습니다. 관리자 계정 생성을 진행합니다.');
+ deferred.resolve();
+ }
+
+ return deferred.promise();
+ }).then(function(){
+ changeInstallMode();
+ });
+
+ });
});
\ No newline at end of file