| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "napiprojektdownloadengine.h" |
| 16 | #include "subtitlelanguage.h" |
| 17 | #include "utils/synchttp.h" |
| 18 | |
| 19 | #include <cmath> |
| 20 | |
| 21 | #include <QCryptographicHash> |
| 22 | #include <QNetworkReply> |
| 23 | #include <QNetworkRequest> |
| 24 | #include <QProcess> |
| 25 | #include <QUrl> |
| 26 | |
| 27 | #ifdef Q_OS_WIN // for SetFileAttributes |
| 28 | #include <qt_windows.h> |
| 29 | #endif |
| 30 | |
| 31 | // This sadly has to be added in order to build under VS >= 2005 |
| 32 | #ifdef _MSC_VER |
| 33 | // from Visual Studio 2005 onwards |
| 34 | // https://msdn.microsoft.com/en-us/library/2ts7cx93%28v=vs.80%29.aspx |
| 35 | #include <stdio.h> |
| 36 | #define snprintf _snprintf_s |
| 37 | #endif |
| 38 | |
| 39 | namespace NapiProjektDownloadEngineConsts { |
| 40 | const unsigned long NAPI_10MB = 10485760; |
| 41 | |
| 42 | const QString napiZipPassword = "iBlm8NTigvru0Jr0"; |
| 43 | const QString napiDownloadUrlTpl = |
| 44 | "http://www.napiprojekt.pl/unit_napisy/dl.php" |
| 45 | "?l=%1&f=%2&t=%3&v=other&kolejka=false&nick=%4&pass=%5&napios=%6"; |
| 46 | const QString napiCheckUserUrlTpl = |
| 47 | "http://www.napiprojekt.pl/users_check.php?nick=%1&pswd=%2"; |
| 48 | const QString napiUploadUrlTpl = |
| 49 | "http://www.napiprojekt.pl/unit_napisy/upload.php" |
| 50 | "?m_length=%1&m_resolution=%2x%3&m_fps=%4&m_hash=%5&m_filesize=%6"; |
| 51 | const QString napiUploadUrlSimpleTpl = |
| 52 | "http://www.napiprojekt.pl/unit_napisy/upload.php" |
| 53 | "?m_hash=%5&m_filesize=%6"; |
| 54 | const QString napiReportBadUrlTpl = |
| 55 | "http://www.napiprojekt.pl/unit_napisy/zlenapisyadd.php"; |
| 56 | } // namespace NapiProjektDownloadEngineConsts |
| 57 | |
| 58 | using namespace NapiProjektDownloadEngineConsts; |
| 59 | |
| 60 | SubtitleDownloadEngineMetadata NapiProjektDownloadEngine::metadata = |
| 61 | SubtitleDownloadEngineMetadata( |
| 62 | "NapiProjekt", |
| 63 | QObject::tr("<b>www.napiprojekt.pl</b> subtitles download engine"), |
| 64 | just(QUrl("http://www.napiprojekt.pl/rejestracja")), nothing()); |
| 65 | |
| 66 | NapiProjektDownloadEngine::NapiProjektDownloadEngine( |
| 67 | const QString &tmpPath, const EngineConfig &config, |
| 68 | const QSharedPointer<const P7ZipDecoder> &p7zipDecoder) |
| 69 | : SubtitleDownloadEngine(tmpPath), |
| 70 | engineConfig(config), |
| 71 | p7zipDecoder(p7zipDecoder) {} |
| 72 | |
| 73 | NapiProjektDownloadEngine::~NapiProjektDownloadEngine() { cleanup(); } |
| 74 | |
| 75 | SubtitleDownloadEngineMetadata NapiProjektDownloadEngine::meta() const { |
| 76 | return NapiProjektDownloadEngine::metadata; |
| 77 | } |
| 78 | |
| 79 | const char *const *NapiProjektDownloadEngine::enginePixmapData() const { |
| 80 | return NapiProjektDownloadEngine::pixmapData; |
| 81 | } |
| 82 | |
| 83 | QString NapiProjektDownloadEngine::checksum(QString filename) { |
| 84 | if (filename.isEmpty()) filename = movie; |
| 85 | return (checkSum = checksum(filename, true)); |
| 86 | } |
| 87 | |
| 88 | bool NapiProjektDownloadEngine::lookForSubtitles(QString lang) { |
| 89 | Maybe<QString> tmpPackedFileOpt = downloadByLangAndChecksum(lang, checkSum); |
| 90 | |
| 91 | if (!tmpPackedFileOpt) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | QString tmpPackedFile = tmpPackedFileOpt.value(); |
| 96 | |
| 97 | subtitlesList << SubtitleInfo(lang, meta().name(), tmpPackedFile, |
| 98 | QFileInfo(movie).completeBaseName(), "", "txt", |
| 99 | SUBTITLE_UNKNOWN); |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | QList<SubtitleInfo> NapiProjektDownloadEngine::listSubtitles() { |
| 104 | return subtitlesList; |
| 105 | } |
| 106 | |
| 107 | bool NapiProjektDownloadEngine::download(QUuid id) { |
| 108 | Maybe<SubtitleInfo> ms = resolveById(id); |
| 109 | |
| 110 | return ms && QFile::exists(ms.value().sourceLocation); |
| 111 | } |
| 112 | |
| 113 | Maybe<QString> NapiProjektDownloadEngine::downloadByLangAndChecksum( |
| 114 | QString lang, QString checksum) const { |
| 115 | if (checksum.isEmpty()) return nothing(); |
| 116 | |
| 117 | QString urlTxt = napiDownloadUrlTpl.arg(npLangWrapper(lang)) |
| 118 | .arg(checksum) |
| 119 | .arg(npFDigest(checksum)) |
| 120 | .arg(engineConfig.nick()) |
| 121 | .arg(engineConfig.password()) |
| 122 | .arg(napiOS()); |
| 123 | |
| 124 | QUrl url(urlTxt); |
| 125 | |
| 126 | SyncHTTP http; |
| 127 | QNetworkReply *reply = http.syncGet(QNetworkRequest(url)); |
| 128 | |
| 129 | if (reply->error() != QNetworkReply::NoError) return nothing(); |
| 130 | |
| 131 | QByteArray buffer = reply->readAll(); |
| 132 | |
| 133 | if (buffer.indexOf("NPc") == 0) return nothing(); |
| 134 | |
| 135 | QString tmpPackedFile = generateTmpPath(); |
| 136 | |
| 137 | QFile file(tmpPackedFile); |
| 138 | if (file.exists()) file.remove(); |
| 139 | if (!file.open(QIODevice::WriteOnly)) return nothing(); |
| 140 | |
| 141 | long r = file.write(buffer); |
| 142 | file.close(); |
| 143 | |
| 144 | if (r < 0) return nothing(); |
| 145 | |
| 146 | return just(tmpPackedFile); |
| 147 | } |
| 148 | |
| 149 | bool NapiProjektDownloadEngine::unpack(QUuid id) { |
| 150 | Maybe<SubtitleInfo> ms = resolveById(id); |
| 151 | if (!ms) return false; |
| 152 | |
| 153 | if (!QFile::exists(movie)) return false; |
| 154 | subtitlesTmp = tmpPath + "/" + checkSum + ".txt"; |
| 155 | |
| 156 | if (QFile::exists(subtitlesTmp)) QFile::remove(subtitlesTmp); |
| 157 | |
| 158 | QString archivePath = ms.value().sourceLocation; |
| 159 | |
| 160 | bool unpacked = p7zipDecoder->unpackSecureArchiveFiles( |
| 161 | archivePath, napiZipPassword, tmpPath); |
| 162 | |
| 163 | return unpacked && QFile::exists(subtitlesTmp); |
| 164 | } |
| 165 | |
| 166 | void NapiProjektDownloadEngine::cleanup() { |
| 167 | clearSubtitlesList(); |
| 168 | if (QFile::exists(subtitlesTmp)) QFile::remove(subtitlesTmp); |
| 169 | } |
| 170 | |
| 171 | bool NapiProjektDownloadEngine::checkUser(const QString &nick, |
| 172 | const QString &pass) { |
| 173 | SyncHTTP http; |
| 174 | QString urlTxt = napiCheckUserUrlTpl.arg(nick).arg(pass); |
| 175 | |
| 176 | QUrl url(urlTxt); |
| 177 | QNetworkReply *reply = http.syncGet(QNetworkRequest(url)); |
| 178 | if (reply->error() != QNetworkReply::NoError) return false; |
| 179 | |
| 180 | QString buffer = reply->readAll(); |
| 181 | if (buffer.indexOf("ok") == 0) return true; |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | QString NapiProjektDownloadEngine::checksum(QString filename, bool limit10M) { |
| 186 | QFile file(filename); |
| 187 | QByteArray fileArray; |
| 188 | |
| 189 | if (!file.open(QIODevice::ReadOnly)) return QString(""); |
| 190 | |
| 191 | fileArray = limit10M ? file.read(NAPI_10MB) : file.readAll(); |
| 192 | file.close(); |
| 193 | |
| 194 | QByteArray b = QCryptographicHash::hash(fileArray, QCryptographicHash::Md5); |
| 195 | fileArray.clear(); |
| 196 | |
| 197 | QString out; |
| 198 | char next[3]; |
| 199 | |
| 200 | for (int i = 0; i < 16; i++) { |
| 201 | snprintf(next, 3, "%.2x", static_cast<unsigned char>(b[i])); |
| 202 | out += next; |
| 203 | } |
| 204 | |
| 205 | checkSum = out; |
| 206 | return checkSum; |
| 207 | } |
| 208 | |
| 209 | QString NapiProjektDownloadEngine::npFDigest(const QString &input) const { |
| 210 | if (input.size() != 32) return ""; |
| 211 | |
| 212 | int idx[] = {0xe, 0x3, 0x6, 0x8, 0x2}, mul[] = {2, 2, 5, 4, 3}, |
| 213 | add[] = {0x0, 0xd, 0x10, 0xb, 0x5}, a, m, i, t, v; |
| 214 | |
| 215 | QString b; |
| 216 | char tmp[2] = {0, 0}; |
| 217 | |
| 218 | for (int j = 0; j <= 4; j++) { |
| 219 | a = add[j]; |
| 220 | m = mul[j]; |
| 221 | i = idx[j]; |
| 222 | |
| 223 | tmp[0] = input[i].toLatin1(); |
| 224 | t = a + static_cast<int>(strtol(tmp, NULL, 16)); |
| 225 | v = static_cast<int>(strtol(input.mid(t, 2).toLocal8Bit(), NULL, 16)); |
| 226 | |
| 227 | snprintf(tmp, 2, "%x", (v * m) % 0x10); |
| 228 | b += tmp; |
| 229 | } |
| 230 | |
| 231 | return b; |
| 232 | } |
| 233 | |
| 234 | QString NapiProjektDownloadEngine::npLangWrapper(QString lang) const { |
| 235 | lang = SubtitleLanguage(lang).toTwoLetter().toUpper(); |
| 236 | |
| 237 | if (lang == "EN") lang = "ENG"; |
| 238 | |
| 239 | return lang; |
| 240 | } |
| 241 | |
| 242 | QString NapiProjektDownloadEngine::napiOS() const { |
| 243 | #if defined(Q_OS_WIN) |
| 244 | return "Windows"; |
| 245 | #elif defined(Q_OS_MAC) |
| 246 | return "Mac OS X"; |
| 247 | #else |
| 248 | return "Linux/UNIX"; |
| 249 | #endif |
| 250 | } |
| 251 | |
| 252 | const char *const NapiProjektDownloadEngine::pixmapData[] = { |
| 253 | "16 16 5 1", " c #FFFFFF", ". c #000080", |
| 254 | "+ c #0052CC", "@ c #005CE6", "# c #BFD9FF", |
| 255 | " ... ", " ... . ", " ... ... ", |
| 256 | " ... .... ", " ... .... ", " ... .... ", |
| 257 | ".. .... ", "....+ ", " ...............", |
| 258 | " . . . . . . . .", " ...............", " .@@####@####@@.", |
| 259 | " .@@#@@#@#@@#@@.", " .@@#@@#@####@@.", " .@@#@@#@#@@@@@.", |
| 260 | " ..............."}; |