├── errmsg.md
├── README.md
├── LICENSE
├── exercise.md
├── zf.md
├── exercise.php
├── library.md
├── zf.php
├── library.php
└── VC2.php
/errmsg.md:
--------------------------------------------------------------------------------
1 | ### 格式:JSON
2 |
3 | {
4 | "err": 错误码,
5 | }
6 |
7 |
8 | ### 错误返回码
9 |
10 | 0 => 无错误
11 | 100 => 响应超时,请重试
12 | 101 => 南邮服务器超时
13 | 200 => 参数错误
14 | 201 => app_secert 错误
15 | 9000301 => 正方账户密码错误
16 | 9000302 => 正方系统错误
17 | 9000303 => dom解析失败
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # nuptOpenApi
2 | RESTFul API service for NUPT
3 |
4 | I started this project at early 2015. At that point, I must say that I was a very beginner of web programming. This project is written in PHP, it's too slow and too jumbled. To be honest, this code is a piece of s\*\*t. I uploaded the code here since someone think it would be helpful. I suggest you not to read this project cause it maybe confuse you.
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Rijn Bian
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/exercise.md:
--------------------------------------------------------------------------------
1 | ##NUPT Open API Exercise 模块
2 | Exercise模块提供南邮体育部晨跑服务器接口,通过exercise可以访问学生跑操记录。
3 |
4 | ###方法列表
5 |
6 | * Fetch 方法
7 |
8 | ---
9 | ###Fetch方法
10 | 通过Fetch方法,获取体育部服务器数据。
11 |
12 | HTTP请求方式:GET
13 | http://nuptopenapi.pixelnfinite.com/exercise
14 |
15 | ####参数
16 |
17 | {
18 | "method" : "fetch",
19 | "type" : "", /* 数据类型 */
20 | "student_id" : "", /* 用户学号 */
21 | "student_name" : "", /* 用户姓名 */
22 | }
23 |
24 | ####数据类型
25 |
26 | TIMES => 跑操次数
27 | DETAIL => 详细跑操记录
28 |
29 | ####样例
30 |
31 | http://nuptopenapi.pixelnfinite.com/exercise?method=fetch&type=TIMES&student_id=B14010315&student_name=******
32 |
33 | ####返回
34 |
35 | {
36 | "query": "success",
37 | "times": "27",
38 | }
39 |
40 | ####样例
41 |
42 | http://nuptopenapi.pixelnfinite.com/exercise?method=fetch&type=DETAIL&student_id=B14010315&student_name=******
43 |
44 | ####返回
45 |
46 | {
47 | "query": "success",
48 | "times": "27",
49 | "list": [
50 | "2015-04-17 07:06",
51 | "2015-04-16 07:04",
52 | "2015-04-15 06:59",
53 | "2015-04-14 07:04",
54 | ...
55 | /* 返回详细时间 */
56 | ]
57 | }
58 |
59 | ####错误
60 | 错误时,NOA会返回error字段和错误提示。
61 |
62 | [
63 | "error" : "1",
64 | "msg" : "",
65 | ]
66 |
67 | 错误信息
68 |
69 | Wrong parameter => 请求参数出错
70 | Network error => 网络延时或体育部服务器访问出错
71 | Wrong authenticityToken => 页面解析错误
72 |
--------------------------------------------------------------------------------
/zf.md:
--------------------------------------------------------------------------------
1 | ##NUPT Open API ZF 模块
2 | ZF模块提供正方教育系统接口,通过ZF可以访问GPA、课表、空教室及检验身份。
3 |
4 | _由于正方服务器限制,请求速度较慢。_
5 |
6 | ###方法列表
7 |
8 | * Login
9 | * Info
10 | * Score
11 | * EmptyClassroom
12 |
13 | ---
14 | ### Login方法
15 | 通过Login方法,登录正方服务器,验证用户身份。请求正确则返回姓名
16 |
17 | HTTP请求方式:GET
18 | http://nuptopenapi.pixelnfinite.com/zf
19 |
20 | ####参数
21 |
22 | {
23 | "method" : "login",
24 | "student_id" : "", /* 用户学号 */
25 | "student_password" : "", /* 用户姓名 */
26 | }
27 |
28 | ####样例
29 |
30 | http://nuptopenapi.pixelnfinite.com/zf?method=login&student_id=B14010312&student_password=******
31 |
32 | ####返回
33 |
34 | {
35 | "student_name": "XXX",
36 | }
37 |
38 | ---
39 | ### Info方法
40 | 通过Info方法,获取用户信息
41 |
42 | HTTP请求方式:GET
43 | http://nuptopenapi.pixelnfinite.com/zf
44 |
45 | ####参数
46 |
47 | {
48 | "method" : "info",
49 | "student_id" : "", /* 用户学号 */
50 | "student_password" : "", /* 用户姓名 */
51 | }
52 |
53 | ####样例
54 |
55 | http://nuptopenapi.pixelnfinite.com/zf?method=info&student_id=B14010312&student_password=******
56 |
57 | ####返回
58 | {
59 | "student_idcard": "3201021995...", /* 身份证号 */
60 | "student_name": "卞远哲", /* 姓名 */
61 | "student_mobile": "1866120...", /* 手机号码 */
62 | "student_tel": "186612...", /* 联系方式 */
63 | "student_email": "48928791...", /* 邮箱 */
64 | "student_sex": "男", /* 性别 */
65 | "student_depatment": "通信与信息工程学院", /* 学院 */
66 | "student_major": "通信工程" /* 专业 */
67 | }
68 |
69 | ---
70 | ####Score方法
71 | 通过Score方法,获取学生在校成绩
72 |
73 | HTTP请求方式:GET
74 | http://nuptopenapi.pixelnfinite.com/zf
75 |
76 | ####参数
77 |
78 | {
79 | "method" : "score",
80 | "student_id" : "", /* 用户学号 */
81 | "student_password" : "", /* 用户姓名 */
82 | }
83 |
84 | ####样例
85 |
86 | http://nuptopenapi.pixelnfinite.com/zf?method=score&student_id=B14010312&student_password=******
87 |
88 | ####返回
89 |
90 | {
91 | "gpa": "3.59",
92 | "detail": [
93 | {
94 | "学年": "2014-2015",
95 | "学期": "1",
96 | "课程代码": "B3500011S",
97 | "课程名称": "大学生心理健康",
98 | "课程性质": "必修",
99 | "课程归属": " ",
100 | "学分": "0.5",
101 | "绩点": "3.70",
102 | "成绩": "87",
103 | "辅修标记": "0",
104 | "补考成绩": " ",
105 | "重修成绩": " ",
106 | "学院名称": "教育科学与技术学院",
107 | "备注": " ",
108 | "重修标记": "0",
109 | "课程英文名称": ""
110 | },
111 | ...
112 | ]
113 | }
114 |
115 | ---
116 | ### 错误
117 | 请见[NUPTOpenAPI 全局错误返回码](http://document.pixelnfinite.com/page!13)
--------------------------------------------------------------------------------
/exercise.php:
--------------------------------------------------------------------------------
1 | /is';
72 | preg_match_all($pattern, $result, $matches);
73 | }
74 |
75 | if ( ! isset($matches[1][0]))
76 | {
77 | exit('{"error":"1","msg":"Wrong authenticityToken"}');
78 | }
79 |
80 | $authenticityToken = $matches[1][0];
81 |
82 | $post['authenticityToken'] = $authenticityToken;
83 | $post['number'] = $id;
84 | $post['name'] = $name;
85 |
86 | $url = "http://zccx.tyb.njupt.edu.cn/student";
87 |
88 | for ($i = 0; $i < 5 && ! strstr($result, "刷卡计数"); $i++)
89 | {
90 | $result = curl_request($url, $post, $cookie_file, TRUE, $url);
91 | }
92 |
93 | if ( ! strstr($result, "刷卡计数"))
94 | {
95 | exit("error");
96 | }
97 |
98 | $pattern = '/(.*?)<\/span>/is';
99 | preg_match_all($pattern, $result, $matches);
100 |
101 | if ( ! isset($matches[1][0]))
102 | {
103 | exit('{"error":"1","msg":"Network error"}');
104 | }
105 |
106 | echo ('{"query":"success","times":"'.$matches[1][0].'"}');
107 |
108 | break;
109 |
110 | case "DETAIL":
111 |
112 | $url = 'http://zccx.tyb.njupt.edu.cn/';
113 |
114 | for ($i = 0; $i < 5 && ! isset($matches[1][0]); $i++)
115 | {
116 | $result = curl_request($url, '', '', FALSE, $url);
117 |
118 | $pattern = '//is';
119 | preg_match_all($pattern, $result, $matches);
120 | }
121 |
122 | if ( ! isset($matches[1][0]))
123 | {
124 | exit('{"error":"1","msg":"Wrong authenticityToken"}');
125 | }
126 |
127 | $authenticityToken = $matches[1][0];
128 |
129 | $post['authenticityToken'] = $authenticityToken;
130 | $post['number'] = $id;
131 | $post['name'] = $name;
132 |
133 | $url = "http://zccx.tyb.njupt.edu.cn/student";
134 |
135 | for ($i = 0; $i < 5 && ! strstr($result, "刷卡计数"); $i++)
136 | {
137 | $result = curl_request($url, $post, $cookie_file, TRUE, $url);
138 | }
139 |
140 | if ( ! strstr($result, "刷卡计数"))
141 | {
142 | exit("error");
143 | }
144 |
145 | $pattern = '/(.*?)<\/span>/is';
146 | preg_match_all($pattern, $result, $matches);
147 |
148 | if ( ! isset($matches[1][0]))
149 | {
150 | exit('{"error":"1","msg":"Network error"}');
151 | }
152 |
153 | $doc = new DomDocument;
154 | $doc->validateOnParse = true;
155 | $doc->LoadHTML($result);
156 |
157 | $result = array();
158 |
159 | $node = $doc->getElementsByTagName('tbody')->item(0)->getElementsByTagName('tr');
160 | for ($c = 0; $c < $node->length; $c++)
161 | {
162 | $child = $node->item($c)->getElementsByTagName('td');
163 | $str = "";
164 | for ($d = 0; $d < $child->length; $d++)
165 | {
166 | $temp = $child->item($d);
167 | while ($temp->hasChildNodes())
168 | {
169 | $temp = $temp->childNodes->item(0);
170 | }
171 | $str .= $doc->saveXML($temp);
172 | }
173 |
174 | $str = str_replace(array("\n", "\r", "\r\n", " ", "\t", "
"), "", $str);
175 | $str = urlencode($str);
176 | $str = str_replace(array("%C3%A5%C2%B9%C2%B4", "%C3%A6%C2%9C%C2%88", "%C3%A6%C2%97%C2%A5", "%C3%A6%C2%97%C2%B6", "%C3%A5%C2%88%C2%86"), "|", $str);
177 |
178 | $pieces = explode("|", $str);
179 | array_push($result, $pieces[0]."-".$pieces[1]."-".$pieces[2]." ".$pieces[3].":".$pieces[4]);
180 | }
181 |
182 | $output = array(
183 | "query" => "success",
184 | "times" => $matches[1][0],
185 | "list" => $result,
186 | );
187 |
188 | echo (str_replace("
", "", json_encode($output)));
189 |
190 | break;
191 |
192 | break;
193 |
194 | default:
195 | exit('{"error":"1","msg":"Wrong parameter"}');
196 | break;
197 | }
198 |
199 | break;
200 | default:
201 | exit('{"error":"1","msg":"Wrong parameter"}');
202 | break;
203 | }
204 |
--------------------------------------------------------------------------------
/library.md:
--------------------------------------------------------------------------------
1 | ##NUPT Open API Library 模块
2 | Library模块提供南京邮电大学图书馆检索接口,通过此接口,NOA会把请求转接给OPAC,并将OPAC的查询结果打包成JSON返回给开发者,用以实现图书检索,借阅查询,续借等功能。
3 |
4 | ###方法列表
5 |
6 | * Search
7 | * Detail
8 | * Checkout
9 | * Renew
10 | * Hisroty
11 | * Appointment
12 |
13 | ---
14 |
15 | ###Search 方法
16 | 开发者通过Search方法,检索南京邮电大学图书馆数据库。
17 |
18 | HTTP请求方式:GET
19 | http://nuptopenapi.pixelnfinite.com/library
20 |
21 | ####参数
22 |
23 | {
24 | "method" : "search", /* 固定为Search */
25 | "keyword" : "", /* 书名或ISBN */
26 | "start" : "1", /* 起始位,格式为5i+1,从1开始,如果不填即为1
27 | * 非5i+1可能会抛出未知错误
28 | */
29 | }
30 |
31 | ####样例
32 |
33 | http://nuptopenapi.pixelnfinite.com/library?method=search&keyword=stm32&start=6
34 |
35 | ####返回
36 |
37 | [
38 | {
39 | "id": "6", /* 该keyword条目索引 */
40 | "name": "基于ARM的单片机应用及实践:STM32案例式教学", /* 书名 */
41 | "author": "武奇生 ... [等] 编著", /* 作者 */
42 | "press": "机械工业出版社 2014", /* 出版社 */
43 | "category": "TP368.1/3-1066", /* 标准分类号 */
44 | "type": "中文图书",
45 | "marc_no": "0000604875", /* 数据库书目唯一编号 */
46 | "available": 2, /* 仙林图书馆可借数量 */
47 | "location": "XLYB样本图书阅览室", /* 仙林图书馆馆藏位置 */
48 | },
49 | ...
50 | /* 默认返回start后五项 */
51 | ]
52 |
53 | ####错误
54 | 错误时,NOA会返回error字段和错误提示。
55 |
56 | [
57 | "error" : "1",
58 | "msg" : "",
59 | ]
60 |
61 | 错误信息
62 |
63 | Wrong parameter => 请求参数出错
64 | Network error => 网络延时或OPAC访问出错
65 | System error => OPAC参数出错
66 |
67 | ---
68 |
69 | ###Detail 方法
70 | Detail方法用以返回书目详细信息。
71 |
72 | HTTP请求方式:GET
73 | http://nuptopenapi.pixelnfinite.com/library
74 |
75 | ####参数
76 |
77 | {
78 | "method" : "detail",
79 | "marc_no" : "", /* Search时获得的MARC号码 */
80 | }
81 |
82 | ####样例
83 |
84 | http://nuptopenapi.pixelnfinite.com/library?method=detail&marc_no=0000601381
85 |
86 | ####返回
87 |
88 | {
89 | "name": "梦游书",
90 | "author": "简媜著",
91 | "press": "北京:九州出版社,2014",
92 | "isbn": "978-7-5108-2889-8",
93 | "price": "CNY23.00",
94 | "carrier": "187页, [3] 页图版:图;21cm",
95 | "subject": "散文集-中国-当代",
96 | "category": "I267",
97 | "summary": "本书收录了《台北小脸盆》、《榕树的早晨》、《水昆兄》、《魔女的厨房》、《女作家的爱情观》、《一只等人的猴子》、《粉圆女人》、《上殿》、《白雪茶树》、《寂寞的冰箱》等作品。",
98 | "type": "中文图书",
99 | "view": "9",
100 | "list": [
101 | {
102 | "demanding": "I267/3-4387 ",
103 | "bar": "1809010",
104 | "location": "XLWY人文艺术图书阅览室(仙林)",
105 | "state": "可借"
106 | },
107 | {
108 | "demanding": "I267/3-4387 ",
109 | "bar": "1809011",
110 | "location": "XLWY人文艺术图书阅览室(仙林)",
111 | "state": "可借"
112 | }
113 | ]
114 | }
115 |
116 | ####错误
117 | 错误时,NOA会返回error字段和错误提示。
118 |
119 | [
120 | "error" : "1",
121 | "msg" : "",
122 | ]
123 |
124 | 错误信息
125 |
126 | Wrong parameter => 请求参数出错
127 | Network error => 网络延时或OPAC访问出错
128 | Wrong marc_no => 错误的MARC码
129 |
130 | ---
131 |
132 | ###Checkout 方法
133 | 通过Checkout方法,检索用户当前借阅情况。
134 |
135 | HTTP请求方式:POST
136 | http://nuptopenapi.pixelnfinite.com/library
137 |
138 | ####参数
139 |
140 | {
141 | "method" : "checkout",
142 | "student_id" : "", /* 用户学号 */
143 | "password" : "", /* OPAC密码 */
144 | "student_name" : "", /* 若用户已认证,则此项可跳过,否则系统会自动认证 */
145 | }
146 |
147 | ####样例
148 |
149 | http://nuptopenapi.pixelnfinite.com/library?method=checkout&student_id=B14010312&password=********&student_name=%E5%8D%9E%E8%BF%9C%E5%93%B2
150 |
151 | ####返回
152 |
153 | {
154 | "card": "110201400809900", /* 用户图书卡号 */
155 | "times": "20", /* 累计借阅量 */
156 | "state": ".2", /* 欠款状态 */
157 | "list": [
158 | {
159 | "bar": "1547970", /* 图书条码 */
160 | "marc_no": "0000455211", /* MARC码,用以获取图书信息 */
161 | "name": "EAGLE电路原理图与PCB设计方法及应用", /* 书名 */
162 | "deadline": "2015-05-07" /* 应还日期 */
163 | }
164 | ]
165 | }
166 |
167 | ####错误
168 | 错误时,NOA会返回error字段和错误提示。
169 |
170 | [
171 | "error" : "1",
172 | "msg" : "",
173 | ]
174 |
175 | 错误信息
176 |
177 | Wrong parameter => 请求参数出错
178 | Wrong data => 用户信息错误
179 | System error => 网络延时或OPAC访问出错
180 |
181 | ---
182 |
183 | ###Renew 方法
184 | 通过Renew方法,进行续借操作。
185 |
186 | HTTP请求方式:POST
187 | http://nuptopenapi.pixelnfinite.com/library
188 |
189 | ####参数
190 |
191 | {
192 | "method" : "renew",
193 | "student_id" : "", /* 用户学号 */
194 | "password" : "", /* OPAC密码 */
195 | "student_name" : "", /* 若用户已认证,则此项可跳过,否则系统会自动认证 */
196 | "bar" : "", /* 书籍条码 */
197 | }
198 |
199 | ####样例
200 |
201 | http://nuptopenapi.pixelnfinite.com/library?method=renew&student_id=B14010315&password=******&student_name=******&bar=1162846
202 |
203 | ####返回
204 |
205 | {
206 | "query": "success",
207 | "result": "", /* failed 或 succeed */
208 | }
209 |
210 | ####错误
211 | 错误时,NOA会返回error字段和错误提示。
212 |
213 | [
214 | "error" : "1",
215 | "msg" : "",
216 | ]
217 |
218 | 错误信息
219 |
220 | Wrong parameter => 请求参数出错
221 | Network error => 网络延时或OPAC访问出错
222 | Wrong data => 错误的用户账号
223 | Bar_code error => 错误的条码
224 |
225 | ---
226 |
227 | ### History 方法
228 | 通过History方法,检索用户借阅历史。
229 |
230 | HTTP请求方式:POST
231 | http://nuptopenapi.pixelnfinite.com/library
232 |
233 | ####参数
234 |
235 | {
236 | "method" : "history",
237 | "student_id" : "", /* 用户学号 */
238 | "password" : "", /* OPAC密码 */
239 | "student_name" : "", /* 若用户已认证,则此项可跳过,否则系统会自动认证 */
240 | }
241 |
242 | ####样例
243 |
244 | http://nuptopenapi.pixelnfinite.com/library?method=history&student_id=B14010312&password=********&student_name=%E5%8D%9E%E8%BF%9C%E5%93%B2
245 |
246 | ####返回
247 |
248 | {
249 | "list": [
250 | {
251 | "bar": "1547970", /* 图书条码 */
252 | "marc_no": "0000455211", /* MARC码,用以获取图书信息 */
253 | "name": "EAGLE电路原理图与PCB设计方法及应用", /* 书名 */
254 | "borrowdate": "2015-05-07" /* 借阅日期 */
255 | "returndate": "2015-05-07" /* 归还日期 */
256 | }
257 | ]
258 | }
259 |
260 | ####错误
261 | 错误时,NOA会返回error字段和错误提示。
262 |
263 | [
264 | "error" : "1",
265 | "msg" : "",
266 | ]
267 |
268 | 错误信息
269 |
270 | Wrong parameter => 请求参数出错
271 | Wrong data => 用户信息错误
272 | System error => 网络延时或OPAC访问出错
273 |
274 | ---
275 |
276 | ###Appointment 方法
277 | 通过Appointment方法进行图书预约。
--------------------------------------------------------------------------------
/zf.php:
--------------------------------------------------------------------------------
1 | Import_Database($data);
12 |
13 | /* request resource */
14 | function curl_request($url, $post = '', $cookie_file = '', $fetch_cookie = 0, $referer = '', $timeout = 10)
15 | {
16 |
17 | $curl = curl_init();
18 | curl_setopt($curl, CURLOPT_URL, $url);
19 | curl_setopt($curl, CURLOPT_HTTPHEADER, array("Expect:"));
20 | curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
21 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
22 | curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
23 | curl_setopt($curl, CURLOPT_REFERER, $referer);
24 | if ($post)
25 | {
26 | curl_setopt($curl, CURLOPT_POST, 1);
27 | curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
28 | }
29 | if ($fetch_cookie)
30 | {
31 | curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file);
32 | }
33 | else
34 | {
35 | curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);
36 | }
37 | curl_setopt($curl, CURLOPT_HEADER, 0);
38 | curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
39 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
40 | $data = curl_exec($curl);
41 | if (curl_errno($curl))
42 | {
43 | return false;
44 | }
45 |
46 | return $data;
47 | }
48 |
49 | function login($id, $password, $cookie_file)
50 | {
51 | global $object;
52 | for ($k = 1; $k <= 100000; $k++)
53 | {
54 | $name = time().uniqid();
55 |
56 | $url = "http://202.119.225.34/default2.aspx";
57 | $code_url = "http://202.119.225.34/CheckCode.aspx";
58 | $refreer = "http://202.119.225.34/default2.aspx";
59 |
60 | $viewresult = curl_request($url, '', $cookie_file, true, $referer, 10);
61 | $image = curl_request($code_url, '', $cookie_file, false, $referer, 10);
62 |
63 | $fp = fopen("./temp/$name.gif", "w");
64 | fwrite($fp, $image);
65 | fclose($fp);
66 |
67 | $result = $object->Recognize_Image("./temp/$name.gif");
68 |
69 | $viewresult = iconv('gb2312', 'utf-8//IGNORE', $viewresult);
70 | $pattern = '//is';
71 | preg_match_all($pattern, $viewresult, $matches);
72 |
73 | $post = array(
74 | __VIEWSTATE => @$matches[1][0],
75 | txtUserName => $id,
76 | TextBox2 => $password,
77 | txtSecretCode => $result,
78 | RadioButtonList1 => "",
79 | Button1 => "",
80 | lbLanguage => "",
81 | hidPdrs => "",
82 | hidsc => "",
83 | );
84 |
85 | $checkresult = curl_request($url, $post, $cookie_file, false, $referer, 10);
86 | $checkresult = iconv('gb2312', 'utf-8//IGNORE', $checkresult);
87 |
88 | if (strstr($checkresult, "验证码不正确") || false == $result || 'iiii' == $result)
89 | {
90 |
91 | }
92 | else
93 | {
94 | if (strstr($checkresult, "密码错误"))
95 | {
96 | exit("{\"err\":9000301}");
97 | }
98 | else
99 | {
100 | $pattern = '/xm=(.*?)&gnmkdm=N/is';
101 | preg_match_all($pattern, $checkresult, $matches);
102 |
103 | return @$matches[1][0];
104 | }
105 |
106 | }
107 |
108 | unlink("./temp/$name.gif");
109 |
110 | }
111 | }
112 |
113 | function getView($result)
114 | {
115 | if ($result)
116 | {
117 | $result = iconv('gb2312', 'utf-8//IGNORE', $result);
118 | $pattern = '//is';
119 | preg_match_all($pattern, $result, $matches);
120 |
121 | if (isset($matches[1][0]))
122 | {
123 | return $matches[1][0];
124 | }
125 | else
126 | {
127 | exit("{\"err\":9000302}");
128 | }
129 | }
130 | else
131 | {
132 | exit("{\"err\":9000302}");
133 | }
134 | }
135 |
136 | $method = strtolower(@$_REQUEST['method']);
137 |
138 | switch ($method)
139 | {
140 | case "login":
141 | $id = @$_REQUEST['student_id'];
142 | $password = @$_REQUEST['student_password'];
143 |
144 | if (null == $id || null == $password)
145 | {
146 | exit("{\"err\":200}");
147 | }
148 |
149 | $cookie_file = tempnam('./temp', 'cookie');
150 |
151 | $name = login($id, $password, $cookie_file);
152 |
153 | unlink($cookie_file);
154 |
155 | if (null == $name)
156 | {
157 | exit("{\"err\":9000302}");
158 | }
159 | else
160 | {
161 | exit("{\"student_name\":\"$name\"}");
162 | }
163 |
164 | break;
165 |
166 | case "info":
167 |
168 | $id = @$_REQUEST['student_id'];
169 | $password = @$_REQUEST['student_password'];
170 |
171 | if (null == $id || null == $password)
172 | {
173 | exit("{\"err\":200}");
174 | }
175 |
176 | $cookie_file = tempnam('./temp', 'cookie');
177 |
178 | $name = login($id, $password, $cookie_file);
179 |
180 | if (null == $name)
181 | {
182 | echo ("{\"err\":9000302}");
183 | }
184 | else
185 | {
186 | $output = array();
187 | $matches = NULL;
188 | $url = 'http://202.119.225.34/xsgrxx.aspx?xh='.$id.'&xm='.iconv('utf-8', 'gb2312//IGNORE', $name).'&gnmkdm=N121501';
189 | for ($i = 0; $i < 5 && ! isset($matches[1][0]); $i++)
190 | {
191 | $result = curl_request($url, '', $cookie_file, FALSE, 'http://202.119.225.34/xs_main.aspx?xh=B14010312');
192 | $result = iconv('gb2312', 'utf-8//IGNORE', $result);
193 | $pattern = '/(.*?)<\/span>/is';
194 | preg_match_all($pattern, $result, $matches);
195 | }
196 | $output['student_idcard'] = $matches[1][0];
197 |
198 | $output['student_name'] = $name;
199 |
200 | $pattern = '//is';
201 | preg_match_all($pattern, $result, $matches);
202 | $output['student_mobile'] = $matches[1][0];
203 |
204 | $pattern = '//is';
205 | preg_match_all($pattern, $result, $matches);
206 | $output['student_tel'] = $matches[1][0];
207 |
208 | $pattern = '//is';
209 | preg_match_all($pattern, $result, $matches);
210 | $output['student_email'] = $matches[1][0];
211 |
212 | $pattern = '/(.*?)<\/span>/is';
213 | preg_match_all($pattern, $result, $matches);
214 | $output['student_sex'] = $matches[1][0];
215 |
216 | $pattern = '/(.*?)<\/span>/is';
217 | preg_match_all($pattern, $result, $matches);
218 | $output['student_depatment'] = $matches[1][0];
219 |
220 | $pattern = '/(.*?)<\/span>/is';
221 | preg_match_all($pattern, $result, $matches);
222 | $output['student_major'] = $matches[1][0];
223 |
224 | echo (json_encode($output));
225 | }
226 |
227 | unlink($cookie_file);
228 |
229 | exit();
230 |
231 | break;
232 |
233 | case "score":
234 |
235 | $id = @$_REQUEST['student_id'];
236 | $password = @$_REQUEST['student_password'];
237 |
238 | if (null == $id || null == $password)
239 | {
240 | exit("{\"err\":200}");
241 | }
242 |
243 | $cookie_file = tempnam('./temp', 'cookie');
244 |
245 | $name = login($id, $password, $cookie_file);
246 |
247 | if (null == $name)
248 | {
249 | echo ("{\"err\":9000302}");
250 | }
251 | else
252 | {
253 | //get new VIEWSTATE
254 | $url = 'http://202.119.225.34/xscj_gc.aspx?xh='.$id.'&xm='.iconv('utf-8', 'gb2312//IGNORE', $name).'&gnmkdm=N121605';
255 |
256 | $result = curl_request($url, '', $cookie_file, FALSE, 'http://202.119.225.34/');
257 | $VIEWSTATE = getView($result);
258 |
259 | //request score
260 | $post_2['__VIEWSTATE'] = $VIEWSTATE;
261 | $post_2['ddlXN'] = iconv('utf-8', 'gb2312//IGNORE', '');
262 | $post_2['ddlXQ'] = iconv('utf-8', 'gb2312//IGNORE', '');
263 | $post_2['Button2'] = iconv('utf-8', 'gb2312//IGNORE', '在校学习成绩查询');
264 |
265 | $result = curl_request($url, $post_2, $cookie_file, FALSE, $url);
266 |
267 | //match result
268 | //$result = iconv('gb2312', 'utf-8//IGNORE', $result);
269 |
270 | $doc = new DomDocument;
271 | $doc->validateOnParse = true;
272 | $doc->LoadHTML($result);
273 | $node = $doc->getElementById('Datagrid1')->getElementsByTagName('tr');
274 |
275 | $key = array();
276 | $value = array();
277 | $detail = array();
278 |
279 | for ($a = 0; $a < $node->length; $a++)
280 | {
281 | $ta = $node->item($a);
282 | $list = $ta->getElementsByTagName('td');
283 | $value = array();
284 | if ($ta->getAttribute('class') == 'datelisthead')
285 | {
286 | for ($b = 0; $b < $list->length; $b++)
287 | {
288 | array_push($key, str_replace("
", "", str_replace(" | ", "", $doc->saveXML($list->item($b)))));
289 | }
290 | }
291 | else
292 | {
293 | for ($b = 0; $b < $list->length; $b++)
294 | {
295 | $value[$key[$b]] = str_replace("", "", str_replace(" | ", "", $doc->saveXML($list->item($b))));
296 | }
297 | array_push($detail, $value);
298 | }
299 | }
300 |
301 | /*
302 | $pattern = '//is';
303 | preg_match_all($pattern, $result, $matches_0);
304 | $result_0 = $matches_0[0][0];
305 | print_r($result_0);
306 | */
307 | $result = iconv('gb2312', 'utf-8//IGNORE', $result);
308 | $pattern = '/平均学分绩点:(.*?)<\/b><\/span>/is';
309 | preg_match_all($pattern, $result, $matches_1);
310 | $result_1 = $matches_1[1][0];
311 |
312 | echo (
313 | json_encode(
314 | array(
315 | "gpa" => $result_1,
316 | "detail" => $detail,
317 | )
318 | )
319 | );
320 | }
321 |
322 | unlink($cookie_file);
323 |
324 | exit();
325 |
326 | break;
327 |
328 | default:
329 | exit("{\"err\":200}");
330 | }
331 |
--------------------------------------------------------------------------------
/library.php:
--------------------------------------------------------------------------------
1 | validateOnParse = true;
62 | $doc->LoadHTML($result);
63 |
64 | $node = $doc->getElementsByTagName('table')->item(0)->getElementsByTagName('tr');
65 |
66 | $result = array();
67 |
68 | $result["available"] = 0;
69 |
70 | for ($c = 1; $c < $node->length; $c++)
71 | {
72 | $data = $node->item($c)->getElementsByTagName('td');
73 | $t_a = array();
74 | for ($d = 0; $d < $data->length; $d++)
75 | {
76 | $temp = $data->item($d);
77 |
78 | while ($temp->hasChildNodes())
79 | {
80 | $temp = $temp->childNodes->item(0);
81 | }
82 |
83 | array_push($t_a, $doc->saveXML($temp));
84 |
85 | //echo ($doc->saveXML($temp));
86 | }
87 | if (strpos($t_a[4], "仙林"))
88 | {
89 | $result["location"] = strstr($t_a[4], "(", true);
90 | if ("可借" == $t_a[5])
91 | {
92 | $result["available"]++;
93 | }
94 |
95 | }
96 |
97 | }
98 |
99 | return $result;
100 | }
101 |
102 | $method = @$_REQUEST['method'];
103 |
104 | switch ($method)
105 | {
106 | case 'search':
107 |
108 | $keyword = @$_REQUEST['keyword'];
109 |
110 | $start = 1;
111 |
112 | if (isset($_REQUEST['start']))
113 | {
114 | $start = $_REQUEST['start'];
115 | }
116 |
117 | if ($start < 1)
118 | {
119 | $start = 1;
120 | }
121 |
122 | $url = 'http://202.119.228.6:8080/opac/openlink.php?match_flag=forward&displaypg=5&showmode=table&orderby=DESC&sort=CATA_DATE&onlylendable=no';
123 |
124 | $value = str_replace('-', '', $keyword);
125 | $pattern = '/[0-9]{12}[0-9xX]/is';
126 | preg_match_all($pattern, $value, $matches);
127 | if (isset($matches[0][0]))
128 | {
129 | $url .= '&isbn='.$value;
130 | }
131 | else
132 | {
133 | $url .= '&title='.$value;
134 | }
135 |
136 | $result = "";
137 |
138 | for ($i = 0; $i < 5 && strpos($result, "版权") === false; $i++)
139 | {
140 | $result = curl_request($url."&page=1", '', '', FALSE, '');
141 | }
142 |
143 | if (strpos($result, "版权") === false)
144 | {
145 | exit('{"error":"1","msg":"Network error"}');
146 | }
147 |
148 | $pattern = '/(.*?)<\/strong>/is';
149 | preg_match_all($pattern, $result, $matches);
150 |
151 | if (isset($matches[1][0]))
152 | {
153 | $scan_count = $matches[1][0];
154 | }
155 | else
156 | {
157 | exit('{"error":"1","msg":"System error"}');
158 | }
159 |
160 | if ($start > $scan_count)
161 | {
162 | exit("{}");
163 | }
164 |
165 | $url = $url."&page=".((int) ($start / 20) + 1);
166 |
167 | $doc = new DomDocument;
168 | $doc->validateOnParse = true;
169 | $doc->LoadHTML($result);
170 |
171 | $node = $doc->getElementById('result_content')->getElementsByTagName('tr');
172 |
173 | $result = array();
174 |
175 | for ($c = ((int) ($start % 20)); $c < $node->length && $c < ($start % 20) + 5 && $c <= $scan_count - ((int) ($start / 20)) * 20; $c++)
176 | {
177 | $data = $node->item($c)->getElementsByTagName('td');
178 | $t_a = array();
179 | for ($d = 0; $d < $data->length; $d++)
180 | {
181 | $temp = $data->item($d);
182 |
183 | $a = $temp->getElementsByTagName('a');
184 |
185 | for ($i = 0; $i < $a->length; $i++)
186 | {
187 | $href = $a->item($i)->getAttribute('href');
188 | }
189 |
190 | while ($temp->hasChildNodes())
191 | {
192 | $temp = $temp->childNodes->item(0);
193 | }
194 |
195 | array_push($t_a, $doc->saveXML($temp));
196 |
197 | //array_push($t_a, $href);
198 | //echo ($doc->saveXML($temp));
199 | }
200 | array_push($t_a, substr($href, strpos($href, "marc_no=") + 8));
201 | $sub_url = "http://202.119.228.6:8080/opac/$href";
202 | array_push(
203 | $result,
204 | array_merge(
205 | array(
206 | "id" => $t_a[0],
207 | "name" => $t_a[1],
208 | "author" => $t_a[2],
209 | "press" => $t_a[3],
210 | "category" => $t_a[4],
211 | "type" => $t_a[5],
212 | "marc_no" => $t_a[6],
213 | ),
214 | getRemain($sub_url)
215 | )
216 | );
217 | }
218 |
219 | exit(json_encode($result));
220 |
221 | break;
222 |
223 | case 'detail':
224 |
225 | $marc_no = @$_REQUEST['marc_no'];
226 |
227 | $url = 'http://202.119.228.6:8080/opac/item.php?marc_no='.$marc_no;
228 |
229 | $request = "";
230 |
231 | for ($i = 0; $i < 5 && strpos($request, "版权") === false; $i++)
232 | {
233 | $request = curl_request($url."&page=1", '', '', FALSE, '');
234 | }
235 |
236 | if (strpos($request, "版权") === false)
237 | {
238 | exit('{"error":"1","msg":"Network error"}');
239 | }
240 |
241 | if (strpos($request, "题名") === false)
242 | {
243 | exit('{"error":"1","msg":"Wrong marc_no"}');
244 | }
245 |
246 | $doc = new DomDocument;
247 | $doc->validateOnParse = true;
248 | $doc->LoadHTML($request);
249 |
250 | $output = array();
251 |
252 | $node = $doc->getElementsByTagName('dl');
253 | $result = array();
254 |
255 | for ($c = 0; $c < $node->length; $c++)
256 | {
257 | $dt = $node->item($c)->childNodes->item(0);
258 | $dd = $node->item($c)->getElementsByTagName('dd');
259 | if ($dd->length > 0 && strpos($doc->saveXML($dt), '相关电子资源') === false)
260 | {
261 | $dd = $dd->item(0)->childNodes;
262 |
263 | $str = "";
264 | for ($d = 0; $d < $dd->length; $d++)
265 | {
266 | $temp = $dd->item($d);
267 | while ($temp->hasChildNodes())
268 | {
269 | $temp = $temp->childNodes->item(0);
270 | }
271 | $str .= $doc->saveXML($temp);
272 | }
273 | $key = str_replace(array(":", ":", "", ""), "", $doc->saveXML($dt));
274 | if ( ! isset($result[$key]))
275 | {
276 | $result[$key] = $str;
277 | }
278 |
279 | }
280 | }
281 |
282 | $output = array(
283 | "name" => strstr($result["题名/责任者"], '/', true),
284 | "author" => substr(strstr($result["题名/责任者"], '/'), 1),
285 | "press" => $result["出版发行项"],
286 | "isbn" => strstr($result["ISBN及定价"], '/', true),
287 | "price" => substr(strstr($result["ISBN及定价"], '/'), 1),
288 | "carrier" => $result["载体形态项"],
289 | "subject" => $result["学科主题"],
290 | "category" => $result["中图法分类号"],
291 | "summary" => $result["提要文摘附注"],
292 | );
293 |
294 | $pattern = '/文献类型:(.*?)<\/dt>/is';
295 | preg_match_all($pattern, $request, $matches);
296 |
297 | if (isset($matches[1][0]))
298 | {
299 | $output["type"] = $matches[1][0];
300 | }
301 |
302 | $pattern = '/浏览次数:(.*?)<\/dt>/is';
303 | preg_match_all($pattern, $request, $matches);
304 |
305 | if (isset($matches[1][0]))
306 | {
307 | $output["view"] = $matches[1][0];
308 | }
309 |
310 | $node = $doc->getElementsByTagName('table')->item(0)->getElementsByTagName('tr');
311 | $result = array();
312 |
313 | for ($c = 1; $c < $node->length; $c++)
314 | {
315 | $data = $node->item($c)->getElementsByTagName('td');
316 | $t_a = array();
317 | for ($d = 0; $d < $data->length; $d++)
318 | {
319 | $temp = $data->item($d);
320 |
321 | while ($temp->hasChildNodes())
322 | {
323 | $temp = $temp->childNodes->item(0);
324 | }
325 | array_push($t_a, $doc->saveXML($temp));
326 | }
327 | array_push($result, array(
328 | "demanding" => $t_a[0],
329 | "bar" => $t_a[1],
330 | "location" => str_replace(array("\r\n", "\r", "\n", " ", "\t", "\0"), "", $t_a[4]) || "",
331 | "state" => $t_a[5],
332 | ));
333 | }
334 |
335 | $output["list"] = $result;
336 |
337 | exit(str_replace(array("
"), "", json_encode($output)));
338 |
339 | break;
340 |
341 | case ("checkout"):
342 |
343 | $cookie_file = tempnam('./temp', 'cookie');
344 | $id = @$_REQUEST['student_id'];
345 | $password = @$_REQUEST['password'];
346 | $name = @$_REQUEST['student_name'];
347 |
348 | $url = 'http://202.119.228.6:8080/reader/redr_verify.php';
349 | $post['number'] = $id;
350 | $post['passwd'] = $password;
351 | $post['select'] = 'cert_no';
352 | $post['returnUrl'] = '';
353 |
354 | $result = 0;
355 |
356 | for ($i = 0; $i < 5 && ! strstr($result, "注销"); $i++)
357 | {
358 | $result = curl_request($url, $post, $cookie_file, TRUE, 'http://202.119.228.6:8080/reader/login.php');
359 |
360 | if (strstr($result, "您尚未完成身份认证"))
361 | {
362 |
363 | $post_con['name'] = $name;
364 | for ($j = 0; $j < 5 && ! strstr($result, "修改密码"); $j++)
365 | {
366 | $result = curl_request('http://202.119.228.6:8080/reader/redr_con_result.php', $post_con, $cookie_file, FALSE, 'http://202.119.228.6:8080/reader/redr_con.php');
367 | }
368 | if ( ! strstr($result, "修改密码"))
369 | {
370 | unlink($cookie_file);
371 | exit(0);
372 | }
373 |
374 | }
375 | if (strstr($result, "密码错误"))
376 | {
377 | exit('{"error":"1","msg":"Wrong data"}');
378 | unlink($cookie_file);
379 | exit(0);
380 | }
381 | }
382 |
383 | if ( ! strstr($result, "注销"))
384 | {
385 | exit('{"error":"1","msg":"Wrong data"}');
386 | exit(0);
387 | }
388 |
389 | $output = array();
390 |
391 | $url = 'http://202.119.228.6:8080/reader/redr_info.php';
392 |
393 | for ($i = 0; $i < 5 && ! strstr($result, "欠款状态"); $i++)
394 | {
395 | $result = curl_request($url, $post, $cookie_file, FALSE, 'http://202.119.228.6:8080/');
396 | }
397 |
398 | if ( ! strstr($result, "欠款状态"))
399 | {
400 | exit('{"error":"1","msg":"System error"}');
401 | exit(0);
402 | }
403 |
404 | $doc = new DomDocument;
405 | $doc->validateOnParse = true;
406 | $doc->LoadHTML($result);
407 |
408 | $key = array("读者条码号:", "累计借书:", "欠款状态:");
409 |
410 | $temp = array();
411 |
412 | $node = $doc->getElementsByTagName('td');
413 | for ($c = 0; $c < $node->length; $c++)
414 | {
415 | $child = $node->item($c)->getElementsByTagName('span');
416 | if ($child->length)
417 | {
418 | $item = urldecode($doc->saveXML($child->item(0)->childNodes->item(0)));
419 | if (in_array($item, $key))
420 | {
421 | array_push($temp, $doc->saveXML($node->item($c)->childNodes->item(1)));
422 | }
423 | }
424 | }
425 |
426 | $output["card"] = $temp[0];
427 | $output['times'] = str_replace("册次", "", $temp[1]);
428 | $output['state'] = $temp[2];
429 |
430 | $url = 'http://202.119.228.6:8080/reader/book_lst.php';
431 |
432 | for ($i = 0; $i < 5 && ! strstr($result, "当前借阅"); $i++)
433 | {
434 | $result = curl_request($url, $post, $cookie_file, FALSE, 'http://202.119.228.6:8080/');
435 | }
436 |
437 | if ( ! strstr($result, "当前借阅"))
438 | {
439 | unlink($cookie_file);
440 | exit(0);
441 | }
442 |
443 | $doc->LoadHTML($result);
444 |
445 | $key = array(0, 1, 4);
446 |
447 | $result = array();
448 |
449 | $node = $doc->getElementsByTagName('tr');
450 | for ($c = 1; $c < $node->length; $c++)
451 | {
452 | $text = array();
453 | $child = $node->item($c)->getElementsByTagName('td');
454 | foreach ($key as $k)
455 | {
456 |
457 | $pattern = '/marc_no=(.*?)">/is';
458 | preg_match_all($pattern, $doc->saveXML($child->item($k)), $matches);
459 |
460 | if (isset($matches[1][0]))
461 | {
462 | $text["marc_no"] = $matches[1][0];
463 | }
464 |
465 | if ($child->item($k)->hasChildNodes())
466 | {
467 | if ($child->item($k)->childNodes->item(0)->hasChildNodes())
468 | {
469 | $text[$k] = $doc->saveXML($child->item($k)->childNodes->item(0)->childNodes->item(0));
470 | }
471 | else
472 | {
473 | $text[$k] = $doc->saveXML($child->item($k)->childNodes->item(0));
474 | }
475 | }
476 | else
477 | {
478 | $text[$k] = $doc->saveXML($child->item($k));
479 | }
480 | }
481 | array_push(
482 | $result,
483 | array(
484 | "bar" => $text[0],
485 | "marc_no" => $text[marc_no],
486 | "name" => $text[1],
487 | "deadline" => str_replace(array(" ", "\n", "\r", "\r\n", "\t", " "), "", $text[4]),
488 | )
489 | );
490 | }
491 |
492 | $output['list'] = $result;
493 |
494 | unlink($cookie_file);
495 |
496 | exit(json_encode($output));
497 |
498 | break;
499 |
500 | case ("history"):
501 |
502 | $cookie_file = tempnam('./temp', 'cookie');
503 | $id = @$_REQUEST['student_id'];
504 | $password = @$_REQUEST['password'];
505 | $name = @$_REQUEST['student_name'];
506 |
507 | $url = 'http://202.119.228.6:8080/reader/redr_verify.php';
508 | $post['number'] = $id;
509 | $post['passwd'] = $password;
510 | $post['select'] = 'cert_no';
511 | $post['returnUrl'] = '';
512 |
513 | $result = 0;
514 |
515 | for ($i = 0; $i < 5 && ! strstr($result, "注销"); $i++)
516 | {
517 | $result = curl_request($url, $post, $cookie_file, TRUE, 'http://202.119.228.6:8080/reader/login.php');
518 |
519 | if (strstr($result, "您尚未完成身份认证"))
520 | {
521 |
522 | $post_con['name'] = $name;
523 | for ($j = 0; $j < 5 && ! strstr($result, "修改密码"); $j++)
524 | {
525 | $result = curl_request('http://202.119.228.6:8080/reader/redr_con_result.php', $post_con, $cookie_file, FALSE, 'http://202.119.228.6:8080/reader/redr_con.php');
526 | }
527 | if ( ! strstr($result, "修改密码"))
528 | {
529 | unlink($cookie_file);
530 | exit(0);
531 | }
532 |
533 | }
534 | if (strstr($result, "密码错误"))
535 | {
536 | exit('{"error":"1","msg":"Wrong data"}');
537 | unlink($cookie_file);
538 | exit(0);
539 | }
540 | }
541 |
542 | if ( ! strstr($result, "注销"))
543 | {
544 | exit('{"error":"1","msg":"Wrong data"}');
545 | exit(0);
546 | }
547 |
548 | $output = array();
549 |
550 | $url = 'http://202.119.228.6:8080/reader/book_hist.php';
551 |
552 | $post = array(
553 | "para_string" => "all",
554 | "topage" => "1",
555 | );
556 |
557 | for ($i = 0; $i < 5 && ! strstr($result, "显示全部"); $i++)
558 | {
559 | $result = curl_request($url, $post, $cookie_file, FALSE, 'http://202.119.228.6:8080/');
560 | }
561 |
562 | if ( ! strstr($result, "显示全部"))
563 | {
564 | exit('{"error":"1","msg":"System error"}');
565 | exit(0);
566 | }
567 |
568 | $doc = new DomDocument;
569 | $doc->validateOnParse = true;
570 |
571 | $doc->LoadHTML($result);
572 |
573 | $key = array(1, 2, 4, 5);
574 |
575 | $result = array();
576 |
577 | $node = $doc->getElementsByTagName('tr');
578 | for ($c = 1; $c < $node->length; $c++)
579 | {
580 | $text = array();
581 | $child = $node->item($c)->getElementsByTagName('td');
582 | foreach ($key as $k)
583 | {
584 |
585 | $pattern = '/marc_no=(.*?)">/is';
586 | preg_match_all($pattern, $doc->saveXML($child->item($k)), $matches);
587 |
588 | if (isset($matches[1][0]))
589 | {
590 | $text["marc_no"] = $matches[1][0];
591 | }
592 |
593 | if ($child->item($k)->hasChildNodes())
594 | {
595 | if ($child->item($k)->childNodes->item(0)->hasChildNodes())
596 | {
597 | $text[$k] = $doc->saveXML($child->item($k)->childNodes->item(0)->childNodes->item(0));
598 | }
599 | else
600 | {
601 | $text[$k] = $doc->saveXML($child->item($k)->childNodes->item(0));
602 | }
603 | }
604 | else
605 | {
606 | $text[$k] = $doc->saveXML($child->item($k));
607 | }
608 | }
609 | array_push(
610 | $result,
611 | array(
612 | "bar" => $text[1],
613 | "marc_no" => $text[marc_no],
614 | "name" => $text[2],
615 | "borrowdate" => $text[4],
616 | "returndate" => $text[5],
617 | )
618 | );
619 | }
620 |
621 | $output['list'] = $result;
622 |
623 | unlink($cookie_file);
624 |
625 | exit(json_encode($output));
626 |
627 | break;
628 |
629 | case "renew":
630 |
631 | $cookie_file = tempnam('./temp', 'cookie');
632 | $id = @$_REQUEST['student_id'];
633 | $password = @$_REQUEST['password'];
634 | $name = @$_REQUEST['student_name'];
635 | $bar = @$_REQUEST['bar'];
636 |
637 | $url = 'http://202.119.228.6:8080/reader/redr_verify.php';
638 | $post['number'] = $id;
639 | $post['passwd'] = $password;
640 | $post['select'] = 'cert_no';
641 | $post['returnUrl'] = '';
642 |
643 | $result = 0;
644 |
645 | for ($i = 0; $i < 5 && ! strstr($result, "注销"); $i++)
646 | {
647 | $result = curl_request($url, $post, $cookie_file, TRUE, 'http://202.119.228.6:8080/reader/login.php');
648 | if (strstr($result, "您尚未完成身份认证"))
649 | {
650 |
651 | $post_con['name'] = $name;
652 | for ($j = 0; $j < 5 && ! strstr($result, "修改密码"); $j++)
653 | {
654 | $result = curl_request('http://202.119.228.6:8080/reader/redr_con_result.php', $post_con, $cookie_file, FALSE, 'http://202.119.228.6:8080/reader/redr_con.php');
655 | }
656 | if ( ! strstr($result, "修改密码"))
657 | {
658 | unlink($cookie_file);
659 | exit(0);
660 | }
661 |
662 | }
663 | if (strstr($result, "密码错误"))
664 | {
665 | exit('{"error":"1","msg":"Wrong data"}');
666 | unlink($cookie_file);
667 | exit(0);
668 | }
669 | }
670 |
671 | if ( ! strstr($result, "注销"))
672 | {
673 | exit('{"error":"1","msg":"Wrong data"}');
674 | exit(0);
675 | }
676 |
677 | $result = "";
678 |
679 | $post = array();
680 |
681 | $post['bar_code'] = $bar;
682 | $post['time'] = microtime_int();
683 |
684 | $url = "http://202.119.228.6:8080/reader/ajax_renew.php?bar_code=$bar&time=".$post['time'];
685 |
686 | for ($i = 0; $i < 5 && ! strstr($result, "mylib"); $i++)
687 | {
688 | $result = curl_request($url, $post, $cookie_file, FALSE, 'http://202.119.228.6:8080/reader/book_lst.php');
689 | }
690 |
691 | unlink($cookie_file);
692 |
693 | if ( ! strstr($result, "mylib"))
694 | {
695 | exit('{"error":"1","msg":"Network error"}');
696 | }
697 |
698 | if ( ! strstr($result, "red"))
699 | {
700 | exit('{"error":"1","msg":"Bar_code error"}');
701 | }
702 |
703 | if ( ! strstr($result, "成功"))
704 | {
705 | exit('{"query":"success","result":"failed"}');
706 | }
707 | else
708 | {
709 | exit('{"query":"success","result":"succeed"}');
710 | }
711 |
712 | break;
713 | default:
714 | exit('{"error":"1","msg":"Wrong parameter"}');
715 | break;
716 | }
717 |
--------------------------------------------------------------------------------
/VC2.php:
--------------------------------------------------------------------------------
1 | p == $b->p)
16 | {
17 | return 0;
18 | }
19 | return ($a->p > $b->p) ? -1 : 1;
20 | }
21 |
22 | class VerifyCode {
23 |
24 | public $Resource = array();
25 | public $k = 0;
26 |
27 | public function Import_Database($object)
28 | {
29 | for ($k = 0; $k < count($object); $k++)
30 | {
31 | $temp = $object[$k]['image'];
32 | $object[$k]['array'] = $temp;
33 | $im = imagecreate(strlen($temp[0]), count($temp));
34 | $black = imagecolorallocate($im, 0, 0, 0);
35 | $white = imagecolorallocate($im, 255, 255, 255);
36 | for ($i = 0; $i < count($temp); $i++)
37 | {
38 | for ($j = 0; $j < strlen($temp[$i]); $j++)
39 | {
40 | if (1 == $temp[$i][$j])
41 | {
42 | imagesetpixel($im, $j, $i, $black);
43 | }
44 | else
45 | {
46 | imagesetpixel($im, $j, $i, $white);
47 | }
48 | }
49 | }
50 |
51 | $object[$k]['image'] = $im;
52 | };
53 |
54 | $this->Resource = $object;
55 |
56 | return true;
57 | }
58 |
59 | public function Output_Database()
60 | {
61 |
62 | $output = $this->Resource;
63 |
64 | for ($k = 0; $k < count($output); $k++)
65 | {
66 | $temp = $output[$k]['image'];
67 | $image = array();
68 |
69 | for ($i = 0; $i < imagesy($temp); $i++)
70 | {
71 | $image[$i] = "";
72 | for ($j = 0; $j < imagesx($temp); $j++)
73 | {
74 | $pixelrgb = imagecolorat($temp, $j, $i);
75 | $cols = imagecolorsforindex($temp, $pixelrgb);
76 | $r = $cols['red'];
77 |
78 | if ($r < 127)
79 | {
80 | $image[$i] .= "1";
81 | }
82 | else
83 | {
84 | $image[$i] .= "0";
85 | }
86 | }
87 | }
88 |
89 | $output[$k]['image'] = $image;
90 | };
91 | return $output;
92 | }
93 |
94 | /**
95 | * This was image binaryzation function
96 | *
97 | * @author Rijn
98 | * @param image verify code
99 | * @param width height
100 | * @return image
101 | */
102 | public function Binaryzation($image, $width, $height)
103 | {
104 | $im = imagecreate($width, $height);
105 | $black = imagecolorallocate($im, 0, 0, 0);
106 | $white = imagecolorallocate($im, 255, 255, 255);
107 | for ($i = 0; $i < $width; $i++)
108 | {
109 | for ($j = 0; $j < $height; $j++)
110 | {
111 | $pixelrgb = imagecolorat($image, $i, $j);
112 | $cols = imagecolorsforindex($image, $pixelrgb);
113 | $r = $cols['red'];
114 | $g = $cols['green'];
115 | $b = $cols['blue'];
116 | //echo ("$i,$j=>$r,$g,$b
");
117 | if ($b - $r > 70 && $b - $g > 70)
118 | {
119 | imagesetpixel($im, $i, $j, $black);
120 | }
121 | else
122 | {
123 | imagesetpixel($im, $i, $j, $white);
124 | }
125 | }
126 | }
127 |
128 | return $im;
129 | }
130 |
131 | /**
132 | * This was image binaryzation function
133 | *
134 | * @author Rijn
135 | * @param image verify code
136 | * @param width height
137 | * @return image
138 | */
139 | public function Binaryzation2($image, $width, $height)
140 | {
141 | $im = imagecreate($width, $height);
142 | $black = imagecolorallocate($im, 0, 0, 0);
143 | $white = imagecolorallocate($im, 255, 255, 255);
144 | for ($i = 0; $i < $width; $i++)
145 | {
146 | for ($j = 0; $j < $height; $j++)
147 | {
148 | $pixelrgb = imagecolorat($image, $i, $j);
149 | $cols = imagecolorsforindex($image, $pixelrgb);
150 | $r = $cols['red'];
151 | $g = $cols['green'];
152 | $b = $cols['blue'];
153 | //echo ("$i,$j=>$r,$g,$b
");
154 | if ($r < 127)
155 | {
156 | imagesetpixel($im, $i, $j, $black);
157 | }
158 | else
159 | {
160 | imagesetpixel($im, $i, $j, $white);
161 | }
162 | }
163 | }
164 |
165 | return $im;
166 | }
167 |
168 | /**
169 | * Erosion
170 | *
171 | * @author Rijn
172 | * @param image
173 | * @return image
174 | */
175 | public function Erosion($image, $width, $height)
176 | {
177 | $im = imagecreate($width, $height);
178 | $black = imagecolorallocate($im, 0, 0, 0);
179 | $white = imagecolorallocate($im, 255, 255, 255);
180 | for ($i = 0; $i < $width; $i++)
181 | {
182 | for ($j = 0; $j < $height; $j++)
183 | {
184 | $count = 0;
185 | for ($di = -1; $di <= 1; $di++)
186 | {
187 | for ($dj = -1; $dj <= 1; $dj++)
188 | {
189 | $pixelrgb = imagecolorat($image, $i + $di, $j + $dj);
190 | $cols = imagecolorsforindex($image, $pixelrgb);
191 | $r = $cols['red'];
192 | if ($r < 127)
193 | {
194 | $count++;
195 | }
196 | }
197 | }
198 | $pixelrgb = imagecolorat($image, $i, $j);
199 | $cols = imagecolorsforindex($image, $pixelrgb);
200 | $r = $cols['red'];
201 | if ($count > 1 && $r < 127 && $i > 0 && $i < $width && $j > 0 && $j < $height)
202 | {
203 | imagesetpixel($im, $i, $j, $black);
204 | }
205 | else
206 | {
207 | imagesetpixel($im, $i, $j, $white);
208 | }
209 | }
210 | }
211 |
212 | return $im;
213 | }
214 |
215 | /**
216 | * Integrate image through x
217 | *
218 | * @author Rijn
219 | * @param image
220 | * @return array
221 | */
222 | public function x_cWave($image, $width, $height)
223 | {
224 | $wave = array();
225 | $peak = array();
226 | for ($i = 0; $i < $width; $i++)
227 | {
228 | $count = 0;
229 | for ($j = 0; $j < $height; $j++)
230 | {
231 | $pixelrgb = imagecolorat($image, $i, $j);
232 | $cols = imagecolorsforindex($image, $pixelrgb);
233 | $r = $cols['red'];
234 | if ($r < 127)
235 | {
236 | $count++;
237 | }
238 | }
239 | array_push($wave, $count);
240 | }
241 |
242 | for ($i = 0; $i < $width + 1; $i++)
243 | {
244 | if (($wave[$i - 1] > $wave[$i] && $wave[$i] < $wave[$i + 1]) || ($wave[$i - 1] >= $wave[$i] && $wave[$i] < $wave[$i + 1]) || ($wave[$i - 1] > $wave[$i] && $wave[$i] <= $wave[$i + 1]))
245 | {
246 | array_push($peak, $wave[$i]);
247 | }
248 | else
249 | {
250 | array_push($peak, $height - 1);
251 | }
252 | }
253 |
254 | $split = array();
255 | for ($i = 0, $count = 0; $i < $height && $count < 5; $i++)
256 | {
257 | for ($j = 0; $j < count($peak) && $count < 5; $j++)
258 | {
259 | if ($peak[$j] == $i)
260 | {
261 | $flag = true;
262 | for ($k = 0; $k < count($split); $k++)
263 | {
264 | if (abs($split[$k] - $j) < 7)
265 | {
266 | $flag = false;
267 | }
268 | }
269 | if ($flag)
270 | {
271 | array_push($split, $j);
272 | $count++;
273 | }
274 | }
275 | }
276 | }
277 |
278 | //print_r($split);
279 |
280 | return $split;
281 | }
282 |
283 | public function x_aWave($image)
284 | {
285 | $th = 127;
286 |
287 | $flag = false;
288 | $x0 = 0;
289 | while ( ! $flag && $x0 < imagesx($image))
290 | {
291 | $flag = false;
292 | for ($j = 0; $j < imagesy($image); $j++)
293 | {
294 | $pixelrgb = imagecolorat($image, $x0, $j);
295 | $cols = imagecolorsforindex($image, $pixelrgb);
296 | $r = $cols['red'];
297 | if ($r < $th)
298 | {
299 | $flag = true;
300 | }
301 | }
302 | $x0++;
303 | }
304 | $x0--;
305 |
306 | $flag = false;
307 | $x1 = imagesx($image) - 1;
308 | while ( ! $flag && $x1 > 0)
309 | {
310 | $flag = false;
311 | for ($j = 0; $j < imagesy($image); $j++)
312 | {
313 | $pixelrgb = imagecolorat($image, $x1, $j);
314 | $cols = imagecolorsforindex($image, $pixelrgb);
315 | $r = $cols['red'];
316 | if ($r < $th)
317 | {
318 | $flag = true;
319 | }
320 | }
321 | $x1--;
322 | }
323 | $x1++;
324 |
325 | $k = array();
326 | for ($p = 1; $p <= 3; $p++)
327 | {
328 | $t = round(($x1 - $x0) / 4 * $p) + $x0;
329 | $wave = array();
330 | for ($i = $t - 4; $i <= $t + 4; $i++)
331 | {
332 | $count = 0;
333 | for ($j = 0; $j < imagesy($image); $j++)
334 | {
335 | $pixelrgb = imagecolorat($image, $i, $j);
336 | $cols = imagecolorsforindex($image, $pixelrgb);
337 | $r = $cols['red'];
338 | if ($r < 127)
339 | {
340 | $count++;
341 | }
342 | }
343 | $wave[$i] = $count;
344 | }
345 |
346 | asort($wave);
347 |
348 | foreach ($wave as $key => $val)
349 | {
350 | $id = $key;
351 | break;
352 | }
353 |
354 | array_push($k, $id);
355 | }
356 |
357 | return array(
358 | $x0,
359 | $k[0],
360 | $k[1],
361 | $k[2],
362 | $x1,
363 | );
364 | }
365 |
366 | /**
367 | * Rotate image to find thinest position
368 | *
369 | * @author Rijn
370 | * @param image
371 | * @return image
372 | */
373 | public function Thinest($image, $x0, $x1, $height)
374 | {
375 | $im = imagecreate($x1 - $x0 + 1, $height);
376 | $black = imagecolorallocate($im, 0, 0, 0);
377 | $white = imagecolorallocate($im, 255, 255, 255);
378 | for ($i = $x0; $i <= $x1; $i++)
379 | {
380 | for ($j = 0; $j < $height; $j++)
381 | {
382 | $pixelrgb = imagecolorat($image, $i, $j);
383 | $cols = imagecolorsforindex($image, $pixelrgb);
384 | $r = $cols['red'];
385 | if ($r < 127)
386 | {
387 | imagesetpixel($im, $i - $x0, $j, $black);
388 | }
389 | else
390 | {
391 | imagesetpixel($im, $i - $x0, $j, $white);
392 | }
393 | }
394 | }
395 |
396 | $rem = null;
397 | $d = $x1 - $x0 + 100;
398 | for ($angle = -30; $angle <= 30; $angle++)
399 | {
400 | $newwidth = ($x1 - $x0 + 1); // / cos(deg2rad($angle));
401 | $newheight = $height; /// cos(deg2rad($angle));
402 | $resize = imagecreatetruecolor($newwidth, $newheight);
403 | $white = imagecolorallocate($resize, 255, 255, 255);
404 | imagefill($resize, 0, 0, $white);
405 |
406 | imagecopyresampled($resize, $im, 0, 0, 0, 0, $newwidth, $newheight, $x1 - $x0 + 1, $height);
407 |
408 | $temp = imagerotate($resize, $angle, $white);
409 |
410 | $flag = false;
411 | $min = 0;
412 | while ( ! $flag && $min < imagesx($temp))
413 | {
414 | $flag = false;
415 | for ($j = 0; $j < imagesy($temp); $j++)
416 | {
417 | $pixelrgb = imagecolorat($temp, $min, $j);
418 | $cols = imagecolorsforindex($temp, $pixelrgb);
419 | $r = $cols['red'];
420 | if ($r < 127)
421 | {
422 | $flag = true;
423 | }
424 | }
425 | $min++;
426 | }
427 | $min--;
428 | $flag = false;
429 | $max = imagesx($temp) - 1;
430 | while ( ! $flag && $max >= 0)
431 | {
432 | $flag = false;
433 | for ($j = 0; $j < imagesy($temp); $j++)
434 | {
435 | $pixelrgb = imagecolorat($temp, $min, $j);
436 | $cols = imagecolorsforindex($temp, $pixelrgb);
437 | $r = $cols['red'];
438 | if ($r < 127)
439 | {
440 | $flag = true;
441 | }
442 | }
443 | $max--;
444 | }
445 | $max++;
446 | if ($max - $min + 1 < $d)
447 | {
448 | $d = $max - $min + 1;
449 | //echo ($d);
450 | $rem = imagecreatetruecolor($max - $min + 1, imagesy($temp));
451 | $white = imagecolorallocate($rem, 255, 255, 255);
452 | imagefill($rem, 0, 0, $white);
453 | imagecopyresized($rem, $temp, 0, 0, $min - 1, 0, $max - $min + 1, imagesy($temp), $max - $min + 1, imagesy($temp));
454 |
455 | }
456 | //echo ($min.",$max");
457 | //return $temp;
458 | }
459 | //echo ($rem);
460 | return $rem;
461 | }
462 |
463 | /**
464 | * Trim image
465 | *
466 | * @author Rijn
467 | * @param image
468 | * @return image
469 | */
470 | public function Trim($image)
471 | {
472 | $th = 127;
473 |
474 | $flag = false;
475 | $x0 = 0;
476 | while ( ! $flag && $x0 < imagesx($image))
477 | {
478 | $flag = false;
479 | for ($j = 0; $j < imagesy($image); $j++)
480 | {
481 | $pixelrgb = imagecolorat($image, $x0, $j);
482 | $cols = imagecolorsforindex($image, $pixelrgb);
483 | $r = $cols['red'];
484 | if ($r < $th)
485 | {
486 | $flag = true;
487 | }
488 | }
489 | $x0++;
490 | }
491 | $x0--;
492 |
493 | $flag = false;
494 | $x1 = imagesx($image) - 1;
495 | while ( ! $flag && $x1 >= 0)
496 | {
497 | $flag = false;
498 | for ($j = 0; $j < imagesy($image); $j++)
499 | {
500 | $pixelrgb = imagecolorat($image, $x1, $j);
501 | $cols = imagecolorsforindex($image, $pixelrgb);
502 | $r = $cols['red'];
503 | if ($r < $th)
504 | {
505 | $flag = true;
506 | }
507 | }
508 | $x1--;
509 | }
510 | $x1++;
511 |
512 | $flag = false;
513 | $y0 = 0;
514 | while ( ! $flag && $y0 < imagesy($image))
515 | {
516 | $flag = false;
517 | for ($j = 0; $j < imagesx($image); $j++)
518 | {
519 | $pixelrgb = imagecolorat($image, $j, $y0);
520 | $cols = imagecolorsforindex($image, $pixelrgb);
521 | $r = $cols['red'];
522 | if ($r < $th)
523 | {
524 | $flag = true;
525 | }
526 | }
527 | $y0++;
528 | }
529 | $y0--;
530 |
531 | $flag = false;
532 | $y1 = imagesy($image) - 1;
533 | while ( ! $flag && $y1 >= 0)
534 | {
535 | $flag = false;
536 | for ($j = 0; $j < imagesx($image); $j++)
537 | {
538 | $pixelrgb = imagecolorat($image, $j, $y1);
539 | $cols = imagecolorsforindex($image, $pixelrgb);
540 | $r = $cols['red'];
541 | if ($r < $th)
542 | {
543 | $flag = true;
544 | }
545 | }
546 | $y1--;
547 | }
548 | $y1 += 2;
549 |
550 | $result = imagecreatetruecolor($x1 - $x0 + 1, $y1 - $y0 + 1);
551 | $white = imagecolorallocate($result, 255, 255, 255);
552 | imagefill($result, 0, 0, $white);
553 | imagecopyresized($result, $image, 0, 0, $x0 - 1, $y0 - 1, $x1 - $x0 + 1, $y1 - $y0 + 1, $x1 - $x0 + 1, $y1 - $y0 + 1);
554 |
555 | return $result;
556 | }
557 |
558 | /**
559 | * This was image learning function
560 | *
561 | * @author Rijn
562 | * @param image verify code
563 | * @param string code
564 | * @return true
565 | */
566 | public function Init_Image($image, $string)
567 | {
568 | list($width, $height, $type, $attr) = getimagesize($image);
569 | $img = imagecreatefromgif($image);
570 |
571 | if (false)
572 | {
573 | $result = imagecreatetruecolor($width, $height * 4);
574 | $white = imagecolorallocate($result, 255, 255, 255);
575 | imagefill($result, 0, 0, $white);
576 |
577 | imagecopy($result, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
578 | }
579 |
580 | $img = $this->Binaryzation($img, $width, $height);
581 |
582 | if (false)
583 | {
584 | imagecopy($result, $img, 0, $height, 0, 0, imagesx($img), imagesy($img));
585 | }
586 |
587 | $img = $this->Erosion($img, $width, $height);
588 |
589 | if (false)
590 | {
591 | imagecopy($result, $img, 0, $height * 2, 0, 0, imagesx($img), imagesy($img));
592 | }
593 |
594 | $split = $this->x_aWave($img, $width, $height);
595 | sort($split);
596 | $chars = array();
597 | for ($i = 0; $i < 4; $i++)
598 | {
599 | //echo ($split[$i]."-".$split[$i + 1]."
");
600 | $chars[$i] = $this->Thinest($img, $split[$i] - 1, $split[$i + 1] + 1, $height);
601 | $chars[$i] = $this->Binaryzation2($chars[$i], imagesx($chars[$i]), imagesy($chars[$i]));
602 | $chars[$i] = $this->Trim($chars[$i]);
603 |
604 | if (false)
605 | {
606 | imagecopy($result, $chars[$i], $split[$i], $height * 3, 0, 0, imagesx($chars[$i]), imagesy($chars[$i]));
607 | }
608 |
609 | if ('*' != $string[$i] && imagesx($chars[$i]) > 3 && imagesy($chars[$i]) > 4)
610 | {
611 | $this->Resource[count($this->Resource)] = array(
612 | 'image' => $chars[$i],
613 | 'key' => $string[$i],
614 | );
615 | }
616 | }
617 |
618 | //print_r($this->Resource);
619 |
620 | if (false)
621 | {
622 | $this->view($result);
623 | }
624 |
625 | return true;
626 | }
627 |
628 | public function Image_2_String($image)
629 | {
630 | $output = array();
631 | for ($y = 0; $y < imagesy($image); $y++)
632 | {
633 | $output[$y] = "";
634 | for ($x = 0; $x < imagesx($image); $x++)
635 | {
636 | $pixelrgb = imagecolorat($image, $x, $y);
637 | $cols = imagecolorsforindex($image, $pixelrgb);
638 | $a = $cols['red'];
639 | if ($a < 127)
640 | {
641 | $output[$y] .= "1";
642 | }
643 | else
644 | {
645 | $output[$y] .= "0";
646 | }
647 | }
648 | }
649 | return $output;
650 | }
651 |
652 | /**
653 | * This was image compare function
654 | *
655 | * @author Rijn
656 | * @param image a
657 | * @param image b
658 | * @return float
659 | */
660 | public function Calc_Percentage($imageA, $imageB)
661 | {
662 | $widthA = imagesx($imageA);
663 | $widthB = imagesx($imageB);
664 | $heightA = imagesy($imageA);
665 | $heightB = imagesy($imageB);
666 | if (abs($widthA - $widthB) / ($widthB + $widthA) + abs($heightA - $heightB) / ($widthB + $widthA) > 0.6)
667 | {
668 | return -2;
669 | }
670 |
671 | $countA = 0;
672 | $countB = 0;
673 |
674 | for ($x = 0; $x < $widthB; $x++)
675 | {
676 | for ($y = 0; $y < $heightB; $y++)
677 | {
678 | $pixelrgb = imagecolorat($imageA, $x, $y);
679 | $cols = imagecolorsforindex($imageA, $pixelrgb);
680 | $a = $cols['red'];
681 | $pixelrgb = imagecolorat($imageB, $x, $y);
682 | $cols = imagecolorsforindex($imageB, $pixelrgb);
683 | $b = $cols['red'];
684 | if ($a < 127 && $b < 127)
685 | {
686 | $countA++;
687 | }
688 | if ($b < 127)
689 | {
690 | $countB++;
691 | }
692 |
693 | }
694 | }
695 |
696 | return $countA / $countB - abs($widthA - $widthB) / ($widthB + $widthA) - abs($heightA - $heightB) / ($widthB + $widthA);
697 | }
698 |
699 | public function Calc_Array_Percentage($arrayA, $arrayB)
700 | {
701 | $widthA = strlen($arrayA[0]);
702 | $widthB = strlen($arrayB[0]);
703 | $heightA = count($arrayA);
704 | $heightB = count($arrayA);
705 | if (abs($widthA - $widthB) / ($widthB + $widthA) + 2 * abs($heightA - $heightB) / ($widthB + $widthA) > 0.5)
706 | {
707 | return -2;
708 | }
709 |
710 | $countA = 0;
711 | $countB = 0;
712 |
713 | for ($x = 0; $x < $widthB/*|| $x < $widthA*/; $x++)
714 | {
715 | for ($y = 0; $y < $heightB/*|| $y < $heightA*/; $y++)
716 | {
717 | $a = @$arrayA[$y][$x];
718 | $b = @$arrayB[$y][$x];
719 | if (1 == $a && 1 == $b)
720 | {
721 | $countA++;
722 | }
723 | if (1 == $b || 1 == $a)
724 | {
725 | $countB++;
726 | }
727 |
728 | }
729 | }
730 |
731 | return $countA / $countB - abs($widthA - $widthB) / ($widthB + $widthA) - 2 * abs($heightA - $heightB) / ($widthB + $widthA);
732 | }
733 |
734 | /**
735 | * This was identify verify code function.
736 | *
737 | * @author Rijn
738 | * @param image verify code
739 | * @return string
740 | */
741 | public function Recognize_Image($image)
742 | {
743 | $result = array();
744 | $text = "";
745 |
746 | list($width, $height, $type, $attr) = getimagesize($image);
747 | $img = imagecreatefromgif($image);
748 |
749 | $img = $this->Binaryzation($img, $width, $height);
750 | $img = $this->Erosion($img, $width, $height);
751 | $split = $this->x_aWave($img, $width, $height);
752 | sort($split);
753 |
754 | $chars = array();
755 | for ($i = 0; $i < 4; $i++)
756 | {
757 | $chars[$i] = $this->Thinest($img, $split[$i] - 1, $split[$i + 1] + 1, $height);
758 | $chars[$i] = $this->Binaryzation2($chars[$i], imagesx($chars[$i]), imagesy($chars[$i]));
759 | $chars[$i] = $this->Erosion($chars[$i], imagesx($chars[$i]), imagesy($chars[$i]));
760 | $chars[$i] = $this->Trim($chars[$i]);
761 | $chars[$i] = $this->Image_2_String($chars[$i]);
762 | }
763 |
764 | for ($k = 0; $k < 4; $k++)
765 | {
766 | $result = array();
767 | $sort = array();
768 |
769 | for ($i = 0; $i < count($this->Resource); $i++)
770 | {
771 | $key = $this->Resource[$i]['key'];
772 | $p = $this->Calc_Array_Percentage($chars[$k], $this->Resource[$i]['array']);
773 | array_push($result,
774 | (object) array(
775 | 'key' => $key,
776 | 'p' => $p,
777 | 'id' => $i,
778 | )
779 | );
780 | array_push($sort, $result[$i]->p);
781 | }
782 |
783 | usort($result, "cmp");
784 |
785 | //var_dump($result[0]);
786 |
787 | /*arsort($sort);
788 |
789 | foreach ($sort as $key => $val)
790 | {
791 | $id = $key;
792 | break;
793 | }
794 |
795 | echo ("[$id] ".$result[$id]->key." => ".$result[$id]->p."
");
796 | */
797 | //echo ("[".$result[0]->id."] ".$result[0]->key." => ".$result[0]->p."
");
798 | $text .= $result[0]->key;
799 |
800 | }
801 | return $text;
802 | }
803 |
804 | public function Optimize_Database($image, $result)
805 | {
806 | list($width, $height, $type, $attr) = getimagesize($image);
807 | $img = imagecreatefromgif($image);
808 |
809 | $img = $this->Binaryzation($img, $width, $height);
810 | $img = $this->Erosion($img, $width, $height);
811 | $split = $this->x_aWave($img, $width, $height);
812 | sort($split);
813 |
814 | $chars = array();
815 | for ($k = 0; $k < 4; $k++)
816 | {
817 | $chars[$k] = $this->Thinest($img, $split[$k] - 1, $split[$k + 1] + 1, $height);
818 | $chars[$k] = $this->Binaryzation2($chars[$k], imagesx($chars[$k]), imagesy($chars[$k]));
819 | $chars[$k] = $this->Trim($chars[$k]);
820 |
821 | $a = $result[$k];
822 |
823 | for ($i = 0; $i < count($this->Resource); $i++)
824 | {
825 | $key = $this->Resource[$i]['key'];
826 |
827 | if ($key == $a)
828 | {
829 | $p = $this->Calc_Percentage($chars[$k], $this->Resource[$i]['image']);
830 |
831 | if ($p < 1)
832 | {
833 | //echo ("[$i] => ($key) => $p
");
834 | $this->Resource[$i]['d'] = (@$this->Resource[$i]['d'] + $p);
835 | }
836 |
837 | }
838 |
839 | }
840 | }
841 | }
842 |
843 | public function Trim_Database()
844 | {
845 | $output = array();
846 | $list = array();
847 |
848 | $sort = array();
849 |
850 | for ($i = 0; $i < count($this->Resource); $i++)
851 | {
852 | array_push($sort, $this->Resource[$i]['d']);
853 | }
854 |
855 | arsort($sort);
856 |
857 | //var_dump($sort);
858 |
859 | foreach ($sort as $id => $val)
860 | {
861 | $key = $this->Resource[$id]['key'];
862 |
863 | if ($list[$key] < 30)
864 | {
865 | $list[$key] = @$list[$key] + 1;
866 | array_push($output, $this->Resource[$id]);
867 | }
868 | }
869 |
870 | $this->Resource = $output;
871 |
872 | for ($i = 0; $i < count($this->Resource) - 1; $i++)
873 | {
874 | for ($j = $i + 1; $j < count($this->Resource); $j++)
875 | {
876 | if ($this->Resource[$i]['key'] > $this->Resource[$j]['key'])
877 | {
878 | $temp = $this->Resource[$i];
879 | $this->Resource[$i] = $this->Resource[$j];
880 | $this->Resource[$j] = $temp;
881 |
882 | }
883 | }
884 | }
885 |
886 | for ($i = 0; $i < count($this->Resource) - 1; $i++)
887 | {
888 | for ($j = $i + 1; $j < count($this->Resource); $j++)
889 | {
890 | if ($this->Resource[$i]['d'] < $this->Resource[$j]['d'] && $this->Resource[$i]['key'] == $this->Resource[$j]['key'])
891 | {
892 | $temp = $this->Resource[$i];
893 | $this->Resource[$i] = $this->Resource[$j];
894 | $this->Resource[$j] = $temp;
895 | }
896 | }
897 | }
898 |
899 | ksort($list);
900 |
901 | foreach ($list as $id => $val)
902 | {
903 | echo ("[$id]=>$val
");
904 | }
905 |
906 | echo ("
");
907 |
908 | for ($i = 0; $i < count($this->Resource); $i++)
909 | {
910 | $key = $this->Resource[$i]['key'];
911 | echo ("[$key]=>".$this->Resource[$i]['d']."
");
912 | }
913 |
914 | return true;
915 | }
916 |
917 | public function View_Database($sort = true)
918 | {
919 | $result = imagecreatetruecolor(600, (int) (count($this->Resource) / 10 * 35 + 100));
920 | $white = imagecolorallocate($result, 255, 255, 255);
921 | $black = imagecolorallocate($result, 0, 0, 0);
922 | imagefill($result, 0, 0, $white);
923 | imagestring($result, 2, 5, 0, "Database count = ".count($this->Resource), $black);
924 |
925 | for ($i = 0; $i < count($this->Resource); $i++)
926 | {
927 | $this->Resource[$i]['id'] = $i;
928 | }
929 |
930 | if ($sort)
931 | {
932 | for ($i = 0; $i < count($this->Resource) - 1; $i++)
933 | {
934 | for ($j = $i + 1; $j < count($this->Resource); $j++)
935 | {
936 | if ($this->Resource[$i]['key'] > $this->Resource[$j]['key'])
937 | {
938 | $temp = $this->Resource[$i];
939 | $this->Resource[$i] = $this->Resource[$j];
940 | $this->Resource[$j] = $temp;
941 |
942 | }
943 | }
944 | }
945 |
946 | for ($i = 0; $i < count($this->Resource) - 1; $i++)
947 | {
948 | for ($j = $i + 1; $j < count($this->Resource); $j++)
949 | {
950 | if ($this->Resource[$i]['d'] < $this->Resource[$j]['d'] && $this->Resource[$i]['key'] == $this->Resource[$j]['key'])
951 | {
952 | $temp = $this->Resource[$i];
953 | $this->Resource[$i] = $this->Resource[$j];
954 | $this->Resource[$j] = $temp;
955 | }
956 | }
957 | }
958 | }
959 |
960 | for ($i = 0; $i < count($this->Resource); $i++)
961 | {
962 | $image = $this->Resource[$i]['image'];
963 | $key = $this->Resource[$i]['key'];
964 |
965 | imagecopyresized($result, $image, (int) ($i % 10) * 60 + 5, (int) ($i / 10) * 35 + 25, 0, 0, imagesx($image), imagesy($image), imagesx($image), imagesy($image));
966 |
967 | imagechar($result, 2, (int) ($i % 10) * 60 + 20, (int) ($i / 10) * 35 + 25, $key, $black);
968 | imagestring($result, 2, (int) ($i % 10) * 60 + 25, (int) ($i / 10) * 35 + 25, "[".$this->Resource[$i]['id']."]", $black);
969 | imagestring($result, 2, (int) ($i % 10) * 60, (int) ($i / 10) * 35 + 40, "d=".(int) ($this->Resource[$i]['d'] * 1000), $black);
970 | }
971 | $this->view($result);
972 | }
973 |
974 | public function view($image)
975 | {
976 | header('Content-Type: image/jpeg');
977 | imagejpeg($image, NULL, 100);
978 | }
979 |
980 | }
981 |
--------------------------------------------------------------------------------