MythTV  master
internetContent.cpp
Go to the documentation of this file.
1 // Program Name: internetContent.cpp
2 //
3 // Purpose - Html & XML status HttpServerExtension
4 //
5 // Created By : David Blain Created On : Oct. 24, 2005
6 // Modified By : Daniel Kristjansson Modified On: Oct. 31, 2007
7 //
9 
10 #include <QTextStream>
11 #include <QDir>
12 #include <QFile>
13 #include <QRegExp>
14 #include <QBuffer>
15 #include <QEventLoop>
16 #include <QImage>
17 
18 #include "internetContent.h"
19 
20 #include "mythcorecontext.h"
21 #include "mythdate.h"
22 #include "mythsystemlegacy.h"
23 #include "mythdirs.h"
24 
25 #include "rssparse.h"
26 #include "netgrabbermanager.h"
27 
29 //
31 
32 InternetContent::InternetContent( const QString &sSharePath)
33  : HttpServerExtension( "InternetContent", sSharePath)
34 {
35 }
36 
38 //
40 
42 {
43  return QStringList( "/InternetContent" );
44 }
45 
47 //
49 
51 {
52  try
53  {
54  if (pRequest)
55  {
56  if (pRequest->m_sBaseUrl != "/InternetContent")
57  return false;
58 
59  LOG(VB_UPNP, LOG_INFO,
60  QString("InternetContent::ProcessRequest: %1 : %2")
61  .arg(pRequest->m_sMethod)
62  .arg(pRequest->m_sRawRequest));
63 
64  // --------------------------------------------------------------
65 
66  if (pRequest->m_sMethod == "GetInternetSearch")
67  {
68  GetInternetSearch( pRequest );
69  return true;
70  }
71 
72  // --------------------------------------------------------------
73 
74  if (pRequest->m_sMethod == "GetInternetSources")
75  {
76  GetInternetSources( pRequest );
77  return true;
78  }
79 
80  // --------------------------------------------------------------
81 
82  if (pRequest->m_sMethod == "GetInternetContent")
83  {
84  GetInternetContent( pRequest );
85  return true;
86  }
87  }
88  }
89  catch( ... )
90  {
91  LOG(VB_GENERAL, LOG_ERR,
92  "InternetContent::ProcessRequest() - Unexpected Exception" );
93  }
94 
95  return false;
96 }
97 
98 // ==========================================================================
99 // Request handler Methods
100 // ==========================================================================
101 
103 //
105 
107 {
108  pRequest->m_eResponseType = ResponseTypeHTML;
109 
110  QString grabber = pRequest->m_mapParams[ "Grabber" ];
111  QString query = pRequest->m_mapParams[ "Query" ];
112  QString page = pRequest->m_mapParams[ "Page" ];
113 
114  if (grabber.isEmpty() || query.isEmpty())
115  return;
116 
117  QString command = QString("%1internetcontent/%2").arg(GetShareDir())
118  .arg(grabber);
119 
120  if (!QFile::exists(command))
121  {
122  pRequest->FormatRawResponse( QString("<HTML>Grabber %1 does "
123  "not exist!</HTML>").arg(command) );
124  return;
125  }
126 
127  auto *search = new Search();
128  QEventLoop loop;
129 
130  QObject::connect(search, &Search::finishedSearch,
131  &loop, &QEventLoop::quit);
132  QObject::connect(search, &Search::searchTimedOut,
133  &loop, &QEventLoop::quit);
134 
135  search->executeSearch(command, query, page);
136  loop.exec();
137 
138  search->process();
139 
140  QDomDocument ret;
141  ret.setContent(search->GetData());
142 
143  delete search;
144 
145  if (ret.isNull())
146  return;
147 
148  pRequest->FormatRawResponse( ret.toString() );
149 }
150 
152 //
154 
156 {
157  pRequest->m_eResponseType = ResponseTypeHTML;
158 
159  QString ret;
160  QString GrabberDir = QString("%1/internetcontent/").arg(GetShareDir());
161  QDir GrabberPath(GrabberDir);
162  QStringList Grabbers = GrabberPath.entryList(QDir::Files | QDir::Executable);
163 
164  for (const auto & name : qAsConst(Grabbers))
165  {
166  QString commandline = GrabberDir + name;
167  MythSystemLegacy scriptcheck(commandline, QStringList("-v"),
169  scriptcheck.Run();
170  scriptcheck.Wait();
171  QByteArray result = scriptcheck.ReadAll();
172 
173  if (!result.isEmpty() && result.toLower().startsWith("<grabber>"))
174  ret += result;
175  }
176 
177  NameValues list;
178 
179  list.push_back( NameValue( "InternetContent", ret ));
180 
181  pRequest->FormatActionResponse( list );
182 }
183 
185 //
187 
189 {
190  pRequest->m_eResponseType = ResponseTypeHTML;
191 
192  QString grabber = pRequest->m_mapParams[ "Grabber" ];
193 
194  if (grabber.isEmpty())
195  return;
196 
197  QString contentDir = QString("%1internetcontent/").arg(GetShareDir());
198  QString htmlFile(contentDir + grabber);
199 
200  // Try to prevent directory traversal
201  QFileInfo fileInfo(htmlFile);
202  if (fileInfo.canonicalFilePath().startsWith(contentDir) &&
203  QFile::exists( htmlFile ))
204  {
205  pRequest->m_eResponseType = ResponseTypeFile;
206  pRequest->m_nResponseStatus = 200;
207  pRequest->m_sFileName = htmlFile;
208  }
209  else
210  {
211  pRequest->FormatRawResponse( QString("<HTML>File %1 does "
212  "not exist!</HTML>").arg(htmlFile) );
213  }
214 }
215 
216 // vim:set shiftwidth=4 tabstop=4 expandtab:
NameValue
Definition: upnputil.h:53
HTTPRequest::m_sBaseUrl
QString m_sBaseUrl
Definition: httprequest.h:125
Search::finishedSearch
void finishedSearch(Search *item)
fileInfo
QFileInfo fileInfo(filename)
HTTPRequest::FormatRawResponse
void FormatRawResponse(const QString &sXML)
Definition: httprequest.cpp:844
HTTPRequest
Definition: httprequest.h:108
InternetContent::GetInternetSearch
static void GetInternetSearch(HTTPRequest *pRequest)
Definition: internetContent.cpp:106
InternetContent::GetInternetSources
static void GetInternetSources(HTTPRequest *pRequest)
Definition: internetContent.cpp:155
MythSystemLegacy
Definition: mythsystemlegacy.h:68
InternetContent::InternetContent
InternetContent(const QString &sSharePath)
Definition: internetContent.cpp:32
HTTPRequest::m_sMethod
QString m_sMethod
Definition: httprequest.h:127
rssparse.h
arg
arg(title).arg(filename).arg(doDelete))
InternetContent::GetBasePaths
QStringList GetBasePaths() override
Definition: internetContent.cpp:41
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythSystemLegacy::ReadAll
QByteArray & ReadAll()
Definition: mythsystemlegacy.cpp:397
MythSystemLegacy::Run
void Run(time_t timeout=0)
Runs a command inside the /bin/sh shell. Returns immediately.
Definition: mythsystemlegacy.cpp:212
mythdirs.h
mythsystemlegacy.h
ResponseTypeFile
@ ResponseTypeFile
Definition: httprequest.h:82
quit
@ quit
Definition: lirc_client.h:30
NameValues
Definition: upnputil.h:89
HTTPRequest::FormatActionResponse
void FormatActionResponse(Serializer *ser)
Definition: httprequest.cpp:771
mythdate.h
HTTPRequest::m_nResponseStatus
long m_nResponseStatus
Definition: httprequest.h:150
InternetContent::ProcessRequest
bool ProcessRequest(HTTPRequest *pRequest) override
Definition: internetContent.cpp:50
netgrabbermanager.h
HTTPRequest::m_mapParams
QStringMap m_mapParams
Definition: httprequest.h:129
GetShareDir
QString GetShareDir(void)
Definition: mythdirs.cpp:222
HTTPRequest::m_sRawRequest
QString m_sRawRequest
Definition: httprequest.h:121
Search
Definition: netgrabbermanager.h:138
HTTPRequest::m_sFileName
QString m_sFileName
Definition: httprequest.h:153
kMSRunShell
@ kMSRunShell
run process through shell
Definition: mythsystem.h:41
ResponseTypeHTML
@ ResponseTypeHTML
Definition: httprequest.h:77
mythcorecontext.h
Search::searchTimedOut
void searchTimedOut(Search *item)
HTTPRequest::m_eResponseType
HttpResponseType m_eResponseType
Definition: httprequest.h:147
internetContent.h
InternetContent::GetInternetContent
static void GetInternetContent(HTTPRequest *pRequest)
Definition: internetContent.cpp:188
HttpServerExtension
Definition: httpserver.h:72
query
MSqlQuery query(MSqlQuery::InitCon())
kMSStdOut
@ kMSStdOut
allow access to stdout
Definition: mythsystem.h:39
MythSystemLegacy::Wait
uint Wait(time_t timeout=0)
Definition: mythsystemlegacy.cpp:242