-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.php
More file actions
198 lines (178 loc) · 5.23 KB
/
store.php
File metadata and controls
198 lines (178 loc) · 5.23 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
<?php
include_once "db.php";
include_once "utils.php";
$html_response="<html><body>
<script type='text/javascript'>
var result='%s';
window.opener.postMessage(result,window.location.origin);
document.write(result);
window.close();
</script>
</body></html>";
session_start();
$action='';
$bookid='';
$store='';
$html='';
$tag='';
if(isset($_GET['action'])) $action=$_GET['action'];
else if(isset($_POST['action'])) $action=$_POST['action'];
if(isset($_GET['bookid'])) $bookid=$_GET['bookid'];
if(isset($_GET['store_code'])) $store=$_GET['store_code'];
if(isset($_GET['html'])) $html=$_GET['html'];
if(isset($_GET['tag'])) $tag=$_GET['tag'];
if($action==='login')
{
if(strlen($store)==0)
{
http_response_code(400);
header("Content-Type: application/json");
echo '{"error":"no vendor store code"}';
exit;
}
$client=\utils\checkLogin($store);
if($client)
{
$info=json_encode($client->store_info(),JSON_UNESCAPED_SLASHES );
if($html==='true') //browser is wating html code to process login info
{
$info=sprintf($html_response,$info);
\Logs\logDebug($info);
header("Content-Type: text/html");
echo $info;
}
else
{
\Logs\logDebug('raw resp: ' . $info);
header("Content-Type: application/json");
echo $info;
}
}
}
if($action==='downloadLink')
{
if(strlen($bookid)==0)
{
http_response_code(400);
\Logs\logError("book id null");
header("Content-Type: application/json");
echo '{"error" : "book id null" }';
exit;
}
$dbase=\Database\odbc()->connect();
if($dbase)
{
$sql="SELECT FILE_ID,FILE_NAME,FILE_SIZE,VENDOR_CODE FROM BOOKS_LINKS,FILE_STORE AS FS WHERE BOOK_ID=$bookid AND STORE_ID=FS.ID";
$rec=$dbase->query($sql);
if($rec->next())
{
$fileid=$rec->field_value('FILE_ID');
$filename=$rec->field_value('FILE_NAME');
$file_size=$rec->field_value('FILE_SIZE');
$vendor=$rec->field_value('VENDOR_CODE');
$client=\utils\checkLogin($vendor);
if($client)
{
$access_token=$client->getAccessToken();
$link=(object)$client->downloadLink($fileid);
if($client->useDownloadProxy())
{
$link->url='proxy.php?action=download&url=' . base64_encode($link->url) . '&filesize=' . $file_size;
array_push($link->headers,"Authorization: Basic " . base64_encode('b13_17778490:tecste1'));
array_push($link->headers,"Store-Token: $access_token");
}
$link=json_encode($link);
$result = "{\"access_token\": \"$access_token\", \"fileid\": \"$fileid\", \"filename\":\"$filename\", \"filesize\": $file_size, \"vendor_code\": \"$vendor\", \"downloadLink\": $link }";
\Logs\logInfo(var_export($result,true));
if($html==='true')
{
header("Content-Type: text/html");
echo sprintf($html_response,$result);
}
else
{
header("Content-Type: application/json");
echo $result;
}
}
}
else {
\Logs\logError("book not found in database");
http_response_code(404);
header("Content-Type: application/json");
echo '{ "error": "book not found in database" }';
}
$dbase->close();
}
else
{
http_response_code(400);
\Logs\logError("cannot open database");
header("Content-Type: application/json");
echo '{"error":"cannot open database"}';
}
}
if($action==='taglist' && strlen($tag)>0)
{
\Logs\logDebug('tag=' . $tag);
$dbase=\Database\odbc()->connect();
$sql="SELECT ID,NAME FROM IT_SUBJECT WHERE NAME REGEXP '$tag' ORDER BY NAME";
$rec=$dbase->query($sql);
$res=array();
while($rec->next())
{
//\Logs\logDebug('tag ' . $rec->field_value('NAME'));
array_push($res, (object)array('id'=>$rec->field_value('ID'),'name'=>$rec->field_value('NAME')));
}
$dbase->close();
\Logs\logDebug(json_encode($res));
header("Content-Type: application/json");
echo json_encode($res);
}
if($action==='newtag' && strlen($tag)>0)
{
$dbase=\Database\odbc()->connect();
$res='';
$tag=strtoupper($tag);
$sql='SELECT ID FROM IT_SUBJECT WHERE NAME=\'' . $tag . '\'';
$rec=$dbase->query($sql);
if($rec->next())
{
$res = json_encode((object)array('id' => $rec->field_value('ID'),'name' => $rec->field_value('NAME')));
\Logs\logWarning('Tag \'' . $tag . '\' already created');
}
else
{
$dbase->query('INSERT INTO IT_SUBJECT (NAME) VALUES(\''. $tag .'\')');
$rec=$dbase->query($sql);
\Logs\logInfo('Create tag \'' . $tag . '\'');
$res = json_encode((object)array('id' => $rec->field_value('ID'),'name' => $rec->field_value('NAME')));
}
$dbase->close();
header("Content-Type: application/json");
echo $res;
}
if($action=='userlogin')
{
$user='';
$pass='';
if(isset($_POST['user']))
{
$user=$_POST['user'];
}
if(isset($_POST['password']))
{
$pass=$_POST['password'];
}
\Logs\logInfo('log request: user="' . $user . '" - password="' . $pass . '"' );
if($user=='aldu' && $pass=='tecste1')
{
$_SESSION['user'] = $user;
}
else
{
$_SESSION['error'] = '<h4>Invalid user credentials</h4>';
}
header('Location: home.php');
}
?>