This repository was archived by the owner on Jul 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkrzip.model.php
More file actions
218 lines (190 loc) · 5.69 KB
/
krzip.model.php
File metadata and controls
218 lines (190 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
<?php
/* Copyright (C) NAVER <http://www.navercorp.com> */
/* Copyright (C) DAOL Project <http://www.daolcms.org> */
/**
* @class krzipModel
* @author NAVER (developers@xpressengine.com)
* @Adaptor DAOL Project (developer@daolcms.org)
* @brief Krzip module model class.
*/
class krzipModel extends krzip {
/**
* @brief 한국 우편번호 모듈 설정 반환
* @return object
*/
function getConfig(){
if(!isset($this->module_config)){
$oModuleModel = getModel('module');
$module_config = $oModuleModel->getModuleConfig('krzip');
if(!is_object($module_config)){
$module_config = new stdClass();
}
/* 기본 설정 추가 */
$default_config = self::$default_config;
foreach($default_config as $key => $val){
if(!isset($module_config->{$key})){
$module_config->{$key} = $val;
}
}
$this->module_config = $module_config;
}
return $this->module_config;
}
/**
* @brief 여러 포맷의 우편번호를 모듈 표준 포맷으로 변환
* @param mixed $values
* @return array
*/
function getMigratedPostcode($values){
if(is_array($values)){
$values = implode(' ', $values);
}
$output = array('', trim(preg_replace('/\s+/', ' ', $values)), '', '', '');
/* 우편번호 */
if(preg_match('/\(?([0-9]{3}-[0-9]{3})\)?/', $output[1], $matches)){
$output[1] = trim(preg_replace('/\s+/', ' ', str_replace($matches[0], '', $output[1])));
$output[0] = $matches[1];
}
/* 지번 주소 */
if(preg_match('/\(.+\s.+[읍면동리(마을)(0-9+가)]\s[0-9-]+\)/', $output[1], $matches)){
$output[1] = trim(str_replace($matches[0], '', $output[1]));
$output[2] = $matches[0];
}
/* 부가 정보 */
if(preg_match('/\(.+[읍면동리(마을)(0-9+가)](?:,.*)?\)/u', $output[1], $matches)){
$output[1] = trim(str_replace($matches[0], '', $output[1]));
$output[4] = $matches[0];
}
/* 상세 주소 */
if(preg_match('/^(.+ [가-힝]+[0-9]*[동리로길]\s*[0-9-]+(?:번지?)?),?\s+(.+)$/u', $output[1], $matches)){
$output[1] = trim($matches[1]);
$output[3] = trim($matches[2]);
}
return $output;
}
/**
* @brief 외부 서버로부터 주소 검색 결과 반환
* @param string $query
* @return mixed
*/
function getKrzipCodeList($query){
$module_config = $this->getConfig();
if($module_config->api_handler != 1){
return new Object(-1, 'msg_invalid_request');
}
if(!isset($query)){
$query = Context::get('query');
}
$query = trim(strval($query));
if($query === ''){
return $this->stop('msg_krzip_no_query');
}
$output = $this->getEpostapiSearch($query);
/* XML Request에서는 Array 치환에 대한 문제로 이 함수를 제대로 사용할 수 없음 */
$this->add('address_list', $output->get('address_list'));
if(!$output->toBool()){
return $output;
}
}
/**
* @brief 우체국 우편번호 API 검색 결과 반환
* @param string $query
* @return object
*/
function getEpostapiSearch($query = ''){
/**
* @brief 문자열 인코딩 변환
* @note 우체국 우편번호 API는 검색어를 EUC-KR로 넘겨주어야 함
*/
$encoding = strtoupper(mb_detect_encoding($query));
if($encoding !== 'EUC-KR'){
$query = iconv($encoding, 'EUC-KR', $query);
}
$module_config = $this->getConfig();
$regkey = $module_config->epostapi_regkey;
$fields = array(
'target' => 'postRoad', /* 도로명 주소 */
'regkey' => $regkey,
'query' => $query
);
$headers = array(
'accept-language' => 'ko'
);
$request_config = array(
'ssl_verify_peer' => FALSE
);
$buff = FileHandler::getRemoteResource(
self::$epostapi_host,
NULL,
30,
'POST',
'application/x-www-form-urlencoded',
$headers,
array(),
$fields,
$request_config
);
$oXmlParser = new XmlParser();
$result = $oXmlParser->parse($buff);
if($result->error){
$err_msg = trim($result->error->message->body);
if(!$err_msg){
$err_code = intval(str_replace('ERR-', '', $result->error->error_code->body));
switch($err_code){
case 1:
$err_msg = 'msg_krzip_is_maintenance';
break;
case 2:
$err_msg = 'msg_krzip_wrong_regkey';
break;
case 3:
$err_msg = 'msg_krzip_no_result';
break;
default:
$err_msg = 'msg_krzip_riddling_wrong';
break;
}
}
return new Object(-1, $err_msg);
}
if(!$result->post){
return new Object(-1, 'msg_krzip_riddling_wrong');
}
$item_list = $result->post->itemlist->item;
if(!is_array($item_list)){
$item_list = array($item_list);
}
if(!$item_list){
return new Object(-1, 'msg_krzip_no_result');
}
$addr_list = array();
foreach($item_list as $key => $val){
$postcode = substr($val->postcd->body, 0, 3) . '-' . substr($val->postcd->body, 3, 3);
$road_addr = $val->lnmaddress->body;
$jibun_addr = $val->rnaddress->body;
$addr_list[] = $this->getMigratedPostcode('(' . $postcode . ') (' . $jibun_addr . ') ' . $road_addr);
}
$output = new Object();
$output->add('address_list', $addr_list);
return $output;
}
/**
* @brief HTML 입력 폼 반환
* @param string $column_name
* @param mixed $values
* @return string
*/
function getKrzipCodeSearchHtml($column_name, $values){
$template_config = $this->getConfig();
$template_config->sequence_id = ++self::$sequence_id;
$template_config->column_name = $column_name;
$template_config->values = $this->getMigratedPostcode($values);
Context::set('template_config', $template_config);
$api_name = strval(self::$api_list[$template_config->api_handler]);
$oTemplate = TemplateHandler::getInstance();
$output = $oTemplate->compile($this->module_path . 'tpl', 'template.' . $api_name);
return $output;
}
}
/* End of file krzip.model.php */
/* Location: ./modules/krzip/krzip.model.php */