forked from peter279k/nttunews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.php
213 lines (189 loc) · 5.22 KB
/
route.php
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
<?php
require 'Slim/Slim.php';
require 'lib/db.connection.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
//http get all categories of news
$app -> get('/news/:table_name', 'get_news_content'); //取得其中一個種類所有公告
$app -> get('/news/:table_name/:publish_date', 'get_assign_news'); //取得其中一個種類的指定日期所有公告
$app -> get('/rss/:table_name', function ($table_name) use ($app) {
set_headers($app);
rss_service($table_name);
}); //取得其中一個種類RSS
$app -> run();
/*
create procedure name get_news and input the table name string to query
the right tables.
create procedure name search_record and input the table name,publish_date string to query
the right tables.
create procedure name rss_service and input the table name string to query
the right table that has records within a week.
*/
function get_news_content($table_name)
{
$response = array();
$response["data"] = "";
$link_db = db_connection();
if($link_db==null)
$response['data'] = 'cannot link db.';
else
{
$stmt = $link_db -> prepare('CALL get_news(:table_name)'); //CALL procedure_name
$stmt -> execute(array(":table_name"=>$table_name));
$res_count = 0;
$result = array();
while($res=$stmt->fetch())
{
$result[$res_count]["date"] = $res["date"];
$result[$res_count]["title"] = $res["title"];
if(stristr($res["link"], 'lic.nttu.edu.tw') || stristr($res["link"], 'lib.nttu.edu.tw'))
{
$result[$res_count]["link"] = $res["link"];
}
else if(stristr($res["link"],'?'))
{
$temp = explode('?',$res["link"]);
$result[$res_count]["link"] = $temp[0];
}
else
$result[$res_count]["link"] = $res["link"];
$res_count++;
}
$response["data"] = $result;
$response_count = count($result);
if($response_count==0)
$response["data"] = '';
}
echo json_encode($response);
}
function get_assign_news($table_name,$publish_date)
{
$response = array();
$response["data"] = "";
if(check_date($publish_date))
{
$link_db = db_connection();
if($link_db==null)
$response["data"] = "cannot link db.";
else
{
$stmt = $link_db -> prepare("CALL search_record(:table_name,:publish_date)");
$stmt -> execute(array(":table_name"=>$table_name,":publish_date"=>$publish_date));
$res_count = 0;
$result = array();
while($res=$stmt->fetch())
{
$result[$res_count]["date"] = $res["date"];
$result[$res_count]["title"] = $res["title"];
if(stristr($res["link"], 'lic.nttu.edu.tw') || stristr($res["link"], 'lib.nttu.edu.tw'))
{
$result[$res_count]["link"] = $res["link"];
}
else if(stristr($res["link"],'?'))
{
$temp = explode('?',$res["link"]);
$result[$res_count]["link"] = $temp[0];
}
else
$result[$res_count]["link"] = $res["link"];
$res_count++;
}
}
}
else
$response["data"] = "invalid_date";
return json_encode($response);
}
function rss_service($table_name)
{
$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>';
$rssfeed .= '<rss version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<description>'.$table_name.'feed</description>';
$rssfeed .= '<copyright>Copyright (C) 2015 peter279k.com</copyright>';
$link_db = db_connection();
if($link_db==null)
exit();
else
{
$stmt = $link_db -> prepare("CALL rss_service(:table_name)");
$stmt -> execute(array(":table_name"=>$table_name));
$check = true;
while($res=$stmt->fetch())
{
if(stristr($res["link"], 'lic.nttu.edu.tw') || stristr($res["link"], 'lib.nttu.edu.tw'))
$check = false;
if(stristr($res["link"],'?') && $check)
{
$temp = explode('?',$res["link"]);
$res["link"] = $temp[0];
}
$rssfeed .= '<item>';
$rssfeed .= '<date>'.$res['date'].'</date>';
$rssfeed .= '<title><![CDATA['.stripInvalidXml($res['title']).']]></title>';
$rssfeed .= '<link><![CDATA['.$res['link'].']]></link>';
$rssfeed .= '</item>';
$check = true;
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
$xml = new SimpleXMLElement($rssfeed);
echo $xml->asXML();
}
}
function set_headers($app)
{
$app->response->headers->set("Content-Type",'application/xml');
}
function check_date($chk_date)
{
$result = null;
$arr = explode("-",$chk_date);
if(count($arr)!=3)
$result = false;
if(!stristr($chk_date,'-'))
$result = false;
else if(strlen($arr[0])!=4)
$result = false;
else if(strlen($arr[1])!=2 || strlen($arr[2])!=2)
$result = false;
else
$result = true;
return $result;
}
/**
* Removes invalid XML
*
* @access public
* @param string $value
* @return string
*/
function stripInvalidXml($value)
{
$ret = "";
$current;
if (empty($value))
{
return $ret;
}
$length = strlen($value);
for ($i=0; $i < $length; $i++)
{
$current = ord($value{$i});
if (($current == 0x9) ||
($current == 0xA) ||
($current == 0xD) ||
(($current >= 0x20) && ($current <= 0xD7FF)) ||
(($current >= 0xE000) && ($current <= 0xFFFD)) ||
(($current >= 0x10000) && ($current <= 0x10FFFF)))
{
$ret .= chr($current);
}
else
{
$ret .= " ";
}
}
return $ret;
}
?>