/
lideapi.php
404 lines (399 loc) · 13.5 KB
/
lideapi.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/**
* JavaScipt API
* =============
* li-de AJAX request server
* this file must in li-de joomla site root path.
*
* call this servis in client side:
*
* in Joomla: "JHtml::_('jquery.framework', false);"
* jQuery.support.cors = true;
* $.ajax({
* url: "http://..../lideapi.php",
* data: "{ ....... }",
* timeout: 30000,
* dataType: "txt",
* success: function(data) {
* process data
* },
* error: function(jqXHR, textStatus, ex) {
* process error
* }
* });
* input and output data structure:
* {"task":"login", "usr":"....", "psw":"..."} --> }"api_status":"OK"}
* {"api_staus":"error acces voiolation"}
* {"task":"logout"} --> {"status":"OK"}
* {"task":"getPoll","poll_id":"..."} --> {"api_status":"OK", szavazas'fields...,"alternatives":[{alternative' fields},....]}
* {"api_staus":"error poll not found"}
* ("task":"getSimpleResult","poll_id":"...") --> {"api_status":"OK", "voters":"...",
* "result:[{"alernativa_id":"...","megnevezes"."firstPosCoont":"..."},...]}
* {"api_staus":"error poll not found"}
* {"task":"setSimpleVoks","poll_id","alt_id","voter_id":"..."} --> {"api_status":"OK"}
* {"api_staus":"error poll not found"}
* {"api_staus":"error poll status is wrong"}
* {"api_staus":"error this voter alredy voted"}
* set selected alertative is 1. position, another alernatives in 99. position
* ("task":"getResult","poll_id":"...") --> {"api_status":"OK", "result":[{....},{...}...]}
* {"api_staus":"error poll not found"}
* {"task":"setVoks","poll_id":szám,"voter_id":szám,
* "voks":[{"alt_id":szám,"position":szám},...]} --> {"api_status":"OK"}
* {"api_staus":"error poll not found"}
* {"api_staus":"error poll status is wrong"}
* {"api_staus":"error this voter alredy voted"}
*
* REST stilusú hivás esetén: REQUESZ_URI: domain/api/poll/szám
*
*/
header("Access-Control-Allow-Origin: *");
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'components'.DS.'com_szavazasok'.DS.'condorcet.php');
JDEBUG ? $_PROFILER->mark( 'afterLoad' ) : null;
$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
jimport('joomla.plugin.helper');
$db = JFactory::getDBO();
$session = JFactory::getSession();
$querystr = $_SERVER['QUERY_STRING'];
if ($querystr != '') {
$data = JSON_decode(rawurldecode($querystr));
} else {
$data = new stdClass();
$data->task = 'getPoll';
$uriStr = $_SERVER['REQUEST_URI'];
$w = explode('/',$uriStr);
for ($i = 0; $i < count($w); $i++) {
if ($w[$i] == 'poll') {
$data->poll_id = $w[$i+1];
}
if ($w[$i] == 'result') {
$data->task = 'getSimpleResult';
}
}
}
if (isset($data->task))
$task = $data->task;
else
$task = '';
$result = new stdClass();
$result->api_status = "error invalid task (".$task.")";
if ($task == 'login') {
if (!isset($data->usr)) $data->usr='';
if (!isset($data->psw)) $data->psw='';
$credentials = array();
$credentials['username'] = $data->usr;
$credentials['password'] = $data->psw;
$mainframe->login($credentials);
$logged_user = JFactory::getUser();
if ($logged_user->id > 0)
$result->api_status = "OK";
else
$result->api_status = "error";
} else if ($task == 'logout') {
$logged_user = JFactory::getUser();
if ($logged_user->id > 0)
$mainframe->logout();
$result = new stdClass();
$result->api_status= "OK";
} else if ($task == 'opensql') {
$logged_user = JFactory::getUser();
// $logged_user->getAuthorisedGroups() tömb ellenörzése is szükséges!
// illetve itt kellene egy komoly hozzáférés ellenörzés
if ($logged_user->id > 0) {
$db->setQuery($data->sql);
try {
$res = $db->loadObjectList();
if ($db->getErrorNum() == 0) {
$result->records = $res;
$result->api_status= "OK";
} else {
$result->api_status= "error ".$db->getErrorNum().' '.$db->getErrorMsg();
}
} catch (Exception $e) {
$result->api_status="error ".$db->getErrorNum().' '.$db->getErrorMsg();
}
}
} else if ($task == 'execsql') {
$logged_user = JFactory::getUser();
// $logged_user->getAuthorisedGroups() tömb ellenörzése is szükséges!
// illetve itt kellene egy komoly hozzáférés ellenörzés
if ($logged_user->id > 0) {
$db = JFactory::getDBO();
$db->setQuery($data->sql);
try {
$res = $db->query();
if ($res) {
$result->api_status= "OK";
} else {
$result->api_status= "error ".$db->getErrorNum().' '.$db->getErrorMsg();
}
} catch (Exception $e) {
$result->api_status="error ".$db->getErrorNum().' '.$db->getErrorMsg();
}
}
} else if ($task == 'getPoll') {
$user = JFactory::getUser();
$result = new stdClass();
if (!isset($data->poll_id)) $data->poll_id=0;
$db->setQuery('select sz.*
from #__szavazasok sz
left outer join #__temakorok t on t.id = sz.temakor_id
left outer join #__tagok ta on ta.temakor_id = sz.temakor_id
where sz.id='.$db->quote($data->poll_id).' and
((ta.user_id = '.$db->quote($user->id).') or (t.lathatosag = 0))
limit 1');
$result = $db->loadObject();
if ($result) {
$db->setQuery('select * from #__alternativak where szavazas_id='.$db->quote($data->poll_id).' order by id' );
$result->alternatives = $db->loadObjectList();
$result->api_status = 'OK';
} else {
$result = new stdClass();
$result->api_status = "error poll not found";
}
} else if ($task == 'getSimpleResult') {
// láthatja ezt a szavazást?
$user = JFactory::getUser();
$db->setQuery('select sz.*
from #__szavazasok sz
left outer join #__temakorok t on t.id = sz.temakor_id
left outer join #__tagok ta on ta.temakor_id = sz.temakor_id
where sz.id='.$db->quote($data->poll_id).' and
((ta.user_id = '.$db->quote($user->id).') or (t.lathatosag = 0))
limit 1');
$res = $db->loadObject();
if ($res) {
$result = new stdClass();
if (!isset($data->poll_id)) $data->poll_id=0;
$db->setQuery('select sz.alternativa_id, a.megnevezes, sum(if(sz.pozicio=1,1,0)) darab
from #__szavazatok sz, #__alternativak a
where sz.szavazas_id='.$db->quote($data->poll_id).' and a.id = sz.alternativa_id
group by sz.alternativa_id, a.megnevezes
order by 3 desc');
$res = $db->loadObjectList();
for ($i=0; $i<count($res); $i++) {
unset($res[$i]->user_id);
}
if ($res == false) {
$result = new stdClass();
$result->api_status = "error voks not found (getSimpleResult )";
} else {
$result->api_status = 'OK';
$result->result = $res;
$db->setQuery('select count(distinct szavazo_id) darab
from #__szavazatok
where szavazas_id='.$db->quote($data->poll_id)
);
$res = $db->loadObject();
$result->voters = $res->darab;
}
} else {
$result = new stdClass();
$result->api_status = "error poll not found (getSimpleResult )";
}
} else if ($task == 'setSimpleVoks') {
$user = JFactory::getUser();
$db = JFactory::getDBO();
$result = new stdClass();
if (!isset($data->poll_id)) $data->poll_id=0;
if (!isset($data->alt_id)) $data->alt_id=0;
if (!isset($data->voter_id)) $data->voter_id=time();
$db->setQuery('select sz.*
from #__szavazasok sz
left outer join #__temakorok t on t.id = sz.temakor_id
left outer join #__tagok ta on ta.temakor_id = sz.temakor_id
where sz.id='.$db->quote($data->poll_id).' and
((ta.user_id = '.$db->quote($user->id).') or (t.lathatosag = 0)) and
((sz.szavazok = 1) or (ta.user_id = '.$db->quote($user->id).'))
limit 1');
$szavazas = $db->loadObject();
if ($szavazas == false) {
$result->api_status = "error poll not found (setSimpleVoks) ";
} else if ($szavazas->szavazas != 1) {
$result->api_status = "error poll status is wrong";
} else {
$szavazo_id = $data->voter_id;
// ellenörzés; szavazott már?
$db->setQuery('select * from #__szavazok where user_id='.$db->quote($data->voter_id).' and szavazas_id='.$db->quote($data->poll_id));
$res = $db->loadObject();
if ($res == false) {
$db->setQuery('select * from #__alternativak where szavazas_id='.$db->quote($data->poll_id));
$res = $db->loadObjectList();
foreach ($res as $res1) {
if ($res1->id == $data->alt_id)
$pozicio = 1;
else
$pozicio= 99;
$db->setQuery(
'INSERT INTO #__szavazatok
(`id`,
`temakor_id`,
`szavazas_id`,
`szavazo_id`,
`user_id`,
`alternativa_id`,
`pozicio`
)
VALUES
(0,
"'.$szavazas->temakor_id.'",
"'.$data->poll_id.'",
"'.$data->voter_id.'",
0,
"'.$res1->id.'",
"'.$pozicio.'"
);
');
$db->query();
} // for
// irás a szavazok tálába
$db->setQuery('INSERT INTO #__szavazok
(`id`,
`temakor_id`,
`szavazas_id`,
`user_id`,
`idopont`,
`kepviselo_id`
)
VALUES
(0,
"'.$szavazas->temakor_id.'",
"'.$szavazas->id.'",
'.$db->quote($data->voter_id).',
"'.date('Y-m-d H:i:s').'",
0
);
');
$db->query();
$result->api_status = "OK";
} else {
$result->api_status = "error this voter alredy vote";
}
} // szavazas record found
} else if ($task == 'setVoks') {
$user = JFactory::getUser();
$db = JFactory::getDBO();
$result = new stdClass();
if (!isset($data->poll_id)) $data->poll_id=0;
if (!isset($data->alt_id)) $data->alt_id=0;
if (!isset($data->voter_id)) $data->voter_id=time();
$db->setQuery('select sz.*
from #__szavazasok sz
left outer join #__temakorok t on t.id = sz.temakor_id
left outer join #__tagok ta on ta.temakor_id = sz.temakor_id
where sz.id='.$db->quote($data->poll_id).' and
((ta.user_id = '.$db->quote($user->id).') or (t.lathatosag = 0)) and
((sz.szavazok = 1) or (ta.user_id = '.$db->quote($user->id).'))
limit 1');
$szavazas = $db->loadObject();
if ($szavazas == false) {
$result->api_status = "error poll not found (setVoks) ";
} else if ($szavazas->szavazas != 1) {
$result->api_status = "error poll status is wrong";
} else {
foreach ($data->voks as $voks) {
$db->setQuery(
'INSERT INTO #__szavazatok
(`id`,
`temakor_id`,
`szavazas_id`,
`szavazo_id`,
`user_id`,
`alternativa_id`,
`pozicio`
)
VALUES
(0,
"'.$szavazas->temakor_id.'",
"'.$data->poll_id.'",
"'.$data->voter_id.'",
0,
"'.$voks->alt_id.'",
"'.$voks->position.'"
);
');
$db->query();
}
$db->setQuery('INSERT INTO #__szavazok
(`id`,
`temakor_id`,
`szavazas_id`,
`user_id`,
`idopont`,
`kepviselo_id`
)
VALUES
(0,
"'.$szavazas->temakor_id.'",
"'.$szavazas->id.'",
'.$db->quote($data->voter_id).',
"'.date('Y-m-d H:i:s').'",
0
);
');
$db->query();
$result = new stdClass();
$result->api_status = 'OK';
}
} else if ($task == 'getResult') {
// likvid + condorcet feldolgozás
$result = new stdClass();
if (!isset($data->poll_id)) $data->poll_id=0;
$db = JFactory::getDBO();
$user = JFactory::getUser();
$db->setQuery('select sz.*
from #__szavazasok sz
left outer join #__temakorok t on t.id = sz.temakor_id
left outer join #__tagok ta on ta.temakor_id = sz.temakor_id
where sz.id='.$db->quote($data->poll_id).' and
((ta.user_id = '.$db->quote($user->id).') or (t.lathatosag = 0))
limit 1');
$szavazas = $db->loadObject();
JRequest::setVar('szavazas',$szavazas->id);
JRequest::setVar('temakor',$szavazas->temakor_id);
if ($szavazas) {
// ha van cachelt eredmény beolvasom azt és azt használom
$db->setQuery('select * from #__poll_value_cache where pollid="'.$szavazas->id.'" and vote_count = -1');
$res = $db->loadObject();
if ($res) {
$result1 = JSON_decode($res->report);
$result = $result->result;
$result->voters = $result1->voters;
$result->api_status = $result1->api_status;
} else {
// ha nincs most képzem az eredményt
$c = new Condorcet($db, $szavazas->temmakor_id, $szavazas->id);
$result = $c->report('json');
$db->setQuery('select count(*) cc from #__szavazok where szavazas_id = '.$db->quote($szavazas->id));
$res = $db->loadObject();
$result->voters = $res->cc;
$result->api_status = "OK";
// ha lezárt szavazás akkor tárolom a cache -be
if ($szavazas->lezart == 1) {
$db->setQuery('INSERT INTO #__poll_value_cache
VALUES ('.$szavazas->temakor_id.',
'.$szavazas->id.',
-1,
'.$db->quote(JSON_encode($result)).'
)'
);
/*
$fp = fopen('debug.txt','w+');
fwrite($fp,$db->getQuery().'<br />'.$db->getErrorMsg());
fclose($fp);
$db->query();
*/
}
}
} else {
$result = new stdClass();
$result->api_status = "error poll not found ".$data->poll_id;
}
}
echo JSON_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
?>