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
|
/*
* httpd.h
*
* See the README file for copyright information and how to reach the author.
*
*/
#ifndef __EPG_HTTPD_H
#define __EPG_HTTPD_H
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdio.h> // before microhttpd.h
#include <stdint.h> // for old microhttpd.h versions
#include <microhttpd.h>
#if MHD_VERSION < 0x00097002
# define MHD_Result int
#endif
#include "lib/epgservice.h"
#include "lib/json.h"
#include "lib/configuration.h"
#include "lib/searchtimer.h"
#include "epgdconfig.h"
#include "HISTORY.h"
#define confDirDefault "/etc/epgd"
#define EPGDNAME "epgd"
extern const char* confDir;
/*
enum mediaType
{
mtBanner1, // 0
mtBanner2, // 1
mtBanner3, // 2
mtPoster1, // 3
mtPoster2, // 4
mtPoster3, // 5
mtSeasonPoster, // 6
mtFanart1, // 7
mtFanart2, // 8
mtFanart3, // 9
mtEpisodePic, // 10
mtActorThumb // 11
};
*/
//***************************************************************************
// Http Tools
//***************************************************************************
class cWebTools
{
public:
static int loadFromFs(MemoryStruct* data, const char* filename, const char* subPath);
static time_t getModTimeOf(const char* filename, const char* subPath);
static int getIntParameter(MHD_Connection* tcp, const char* name, int def = 0);
static const char* getStrParameter(MHD_Connection* tcp, const char* name, const char* def = 0);
static const char* getStrHeader(MHD_Connection* tcp, const char* name, const char* def = 0);
static time_t getTimeHeader(MHD_Connection* tcp, const char* name, const char* def = 0);
static MHD_Response* createHttpResponse(MemoryStruct* data);
static int addHeaderItem(MHD_Response* response, const char* name, const char* value);
};
//***************************************************************************
// EPG Http Daemon
//***************************************************************************
class cEpgHttpd : public cFrame, public cWebTools, public cSystemNotification
{
public:
struct Session
{
std::string id; // session id
std::string user;
int rights;
time_t start;
time_t last; // last activity
};
struct UserRight
{
const char* url;
unsigned int mask;
};
cEpgHttpd();
~cEpgHttpd();
int init();
int exit();
int initDb();
int exitDb();
int checkConnection();
int setSession(const char* sessionId);
int hasRights(const char* url, json_t* response, int& statusCode);
int needLogin();
int loop();
int atConfigItem(const char* Name, const char* Value);
int doShutDown() { return shutdown; }
static void downF(int signal) { tell(0, "Shutdown triggered with signal %d", signal); shutdown = yes; }
protected:
//
int dbConnected(int force = no) { return connection && (!force || connection->check() == success); }
// dispatcher
int performHttpGet(MHD_Connection* connection, const char* inurl, MemoryStruct* data);
int performDataRequest(MHD_Connection* connection, const char* url, MemoryStruct* data);
int performPostData(const char* url, MemoryStruct* data);
//
int collectScraperData(json_t* oScraperData, int movieId, int seriesId, int episodeId);
// action requests
int doChannelSwitch(MHD_Connection* tcp, json_t* obj);
int doUpdateSearchtimer(MHD_Connection* tcp, json_t* obj);
int doUpdateRecordingTable(MHD_Connection* tcp, json_t* obj);
int doWakeupVdr(MHD_Connection* tcp, json_t* obj);
int doRenameRecording(MHD_Connection* tcp, json_t* obj);
int doReplayRecording(MHD_Connection* tcp, json_t* obj);
int doDeleteRecording(MHD_Connection* tcp, json_t* obj);
int doHitKey(MHD_Connection* tcp, json_t* obj);
// post requests
int storeChannels(json_t* jobj, json_t* obj);
int deleteTimerJobs(json_t* jInData, json_t* response);
int deleteDoneTimers(json_t* jInData, json_t* response);
int storeTimerJob(json_t* jobj, json_t* obj);
int storeSearchTimer(json_t* jobj, json_t* obj);
int storeParameters(json_t* jInData, json_t* response);
int doSearch(json_t* jInData, json_t* response);
int doSendMail(json_t* jInData, json_t* response);
int doLogin(json_t* jInData, json_t* response);
int storeUsers(json_t* jInData, json_t* response);
int markMessages(json_t* jInData, json_t* response);
// tools
int modifyCreateTimer(cDbRow* timerRow);
// query requests
int doEvents(MHD_Connection* connection, json_t* obj);
int doEvent(MHD_Connection* connection, json_t* obj, MemoryStruct* data);
int doTimers(MHD_Connection* connection, json_t* obj);
int doRecordings(MHD_Connection* connection, json_t* obj);
int doRecording(MHD_Connection* connection, json_t* obj);
int doParameters(MHD_Connection* tcp, json_t* obj);
int doRecDirs(MHD_Connection* connection, json_t* obj);
int doTimerJobs(MHD_Connection* tcp, json_t* obj);
int doMessages(MHD_Connection* tcp, json_t* obj);
int doDoneTimers(MHD_Connection* tcp, json_t* obj);
int doDoneTimer(MHD_Connection* tcp, json_t* obj);
int doSearchtimers(MHD_Connection* tcp, json_t* obj);
int doEpgImage(MHD_Connection* connection, json_t* obj, MemoryStruct* data);
int doChannelLogo(MHD_Connection* connection, json_t* obj, MemoryStruct* data);
int doMovieMedia(MHD_Connection* connection, json_t* obj, MemoryStruct* data);
int doSeriesMedia(MHD_Connection* connection, json_t* obj, MemoryStruct* data);
int doGenres(MHD_Connection* connection, json_t* obj);
int doCategories(MHD_Connection* connection, json_t* obj);
int doChannels(MHD_Connection* connection, json_t* obj);
int doVdrs(MHD_Connection* connection, json_t* obj);
int doUsers(MHD_Connection* connection, json_t* obj);
int doProxy(MHD_Connection* tcp, json_t* obj, MemoryStruct* data);
int doLog(MHD_Connection* tcp, json_t* obj);
int doDebug(MHD_Connection* tcp, json_t* obj);
// helper ..
int __attribute__ ((format(printf, 4, 5))) buildResponse(json_t* obj, int state, const char* format, ...);
int __attribute__ ((format(printf, 5, 6))) message(int level, char type, const char* title, const char* format, ...);
const char* methodOf(const char* url);
int ipOfVdr(const char* uuid, const char*& ip, int& port);
int triggerVdrs(const char* trg, const char* plug = 0, const char* options = 0);
int triggerVdr(const char* ip, unsigned int port, const char* trg,
const char* plug = 0, const char* options = 0, char* res = 0);
int triggerEpgd();
int wakeupVdr(const char* uuid);
// static
static MHD_Result dispatcher(void* cls, struct MHD_Connection* connection,
const char* url, const char* method,
const char* version, const char* upload_data,
size_t* upload_data_size, void** con_cls);
static MHD_Result askForAuthentication(struct MHD_Connection* connection, const char* realm);
static int isAuthenticated(struct MHD_Connection* connection,
const char* username, const char* password);
// data
MHD_Daemon* daemon;
MemoryStruct keyPem;
MemoryStruct certPem;
std::map<std::string,Session> sessions;
Session* currentSession;
int loginWithSession;
time_t lastSdWatchdogAt;
int withutf8;
cSearchTimer* search;
Python* ptyRecName;
// database stuff
cDbConnection* connection;
cDbTable* useeventsDb;
cDbTable* mapDb;
cDbTable* imageDb;
cDbTable* imageRefDb;
cDbTable* vdrDb;
cDbTable* timerDb;
cDbTable* timersDoneDb;
cDbTable* searchtimerDb;
cDbTable* movieDb;
cDbTable* movieActorsDb;
cDbTable* movieActorDb;
cDbTable* movieMediaDb;
cDbTable* seriesDb;
cDbTable* seriesEpisodeDb;
cDbTable* seriesMediaDb;
cDbTable* seriesActorsDb;
cDbTable* recordingDirDb;
cDbTable* recordingListDb;
cDbTable* userDb;
cDbTable* messageDb;
cDbStatement* updateMap;
cDbStatement* updateTimerAName;
cDbStatement* updateDoneAName;
cDbStatement* selectEventsAt;
cDbStatement* selectEventsNext;
cDbStatement* selectEventsStartInRange;
cDbStatement* selectGenres;
cDbStatement* selectCategories;
cDbStatement* selectEvent;
cDbStatement* selectEventByTime;
cDbStatement* selectAllMap;
cDbStatement* selectMapById;
cDbStatement* selectVdrs;
cDbStatement* selectUsers;
cDbStatement* selectAllTimer;
cDbStatement* selectTimerById;
cDbStatement* selectTimerByEventId;
cDbStatement* selectAllSearchTimer;
cDbStatement* selectPendingTimerActions;
cDbStatement* selectDoneTimers;
cDbStatement* selectMovie;
cDbStatement* selectMovieActors;
cDbStatement* selectMovieMedia;
cDbStatement* selectSerie;
cDbStatement* selectSeriesEpisode;
cDbStatement* selectSeriesMedia;
cDbStatement* selectActiveVdrs;
cDbStatement* selectRecordingDirs;
cDbStatement* selectRecordingByPath;
cDbStatement* selectChannelFromMap;
cDbStatement* selectUserByMd5;
cDbStatement* selectAllRecordings;
cDbStatement* selectRecordingForEventByLv;
cDbStatement* selectPendingMessages;
cDbStatement* selectWebUsers;
// special values for statements
cDbValue endTime;
cDbValue startTime;
cDbValue imageSize;
cDbValue merge;
cDbValue imageid;
cDbValue matchDensityTitle;
cDbValue matchDensityShorttext;
// just pointer to the values of cDbTable instances. Don't free them ;)
cDbValue* vdrState;
cDbValue* vdrIp;
cDbValue* vdrSvdrp;
cDbValue* vdrUuid;
cDbValue* eventsUpdSp;
cDbValue* eventsGenre;
cDbValue* eventsCategory;
cDbValue* mapChannelName;
cDbValue* mapChannelId;
cDbValue* mapSource;
cDbValue* imagerefImgName;
cDbValue* imageUpdSp;
cDbValue* imageImage;
cDbValue* moviemediaMediaContent;
cDbValue* moviemediaMediaType;
cDbValue* movieactorActorId;
cDbValue* seriesepisodeSeasonNumber;
cDbValue* seriesepisodeEpisodeId;
cDbValue* seriesmediaMediaType;
cDbValue* seriesmediaActorId;
cDbValue* seriesmediaMediaContent;
// static
static int shutdown;
static cEpgHttpd* singleton;
static UserRight userrights[];
UserMask toRightMask(const char* url);
};
//***************************************************************************
// cHttpService
//***************************************************************************
/*
class cHttpService
{
public:
cHttpService()
{
daemon = 0;
}
virtual int init()
{
if (!(daemon = MHD_start_daemon(MHD_USE_SELECT_INTERNALLY,
EPG2VDRConfig.httpPort,
0, 0, // accept policy callback
&dispatcher, 0, // access handler callback
MHD_OPTION_END)))
{
tell(0, "Error: Start of http server failed '%s'", strerror(errno));
return fail;
}
return success;
}
virtual int exit()
{
MHD_stop_daemon(daemon);
daemon = 0;
return success;
}
protected:
static int dispatcher(void* cls, MHD_Connection* con,
const char* url, const char* method,
const char* version, const char* upload_data,
size_t* upload_data_size, void** con_cls);
MHD_Daemon* daemon;
};
*/
//***************************************************************************
#endif // __EPG_HTTPD_H
|