if ($cfg['file_hash'] == "md5") {
     header('Content-MD5: ' . hex_to_base64($link['hash']));
 }
-
+if ($cfg['litespeed_workaround']) {
+    // Work around that LiteSpeed truncates large files.
+    // See https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:config:internal-redirect
+    if ($_GET['litespeed_workaround'] == 'phase2') {
+        $file_web_path = preg_replace('#^' . $_SERVER['DOCUMENT_ROOT'] . '#', '', VAR_FILES);
+        header('X-LiteSpeed-Location: ' . $file_web_path . $p . $link['hash']);
+    } else {
+        // Since Content-Type isn't forwarded by LiteSpeed, first
+        // redirect to the same URL but append the file name.
+        header('Location: ' . $_SERVER['PHP_SELF'] . '/' . $link['file_name'] . '?' .
+               $_SERVER['QUERY_STRING'] . '&litespeed_workaround=phase2');
+    }
+}
 /* Read encrypted file. */
-if ($link['crypted']) {
+else if ($link['crypted']) {
     /* Init module */
     $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
     /* Extract key and iv. */
 
  */
 $cfg['file_hash'] = 'md5';
 
+/* Work around that LiteSpeed truncates large files when downloading.
+ * Only for use with the LiteSpeed web server!
+ * An internal redirect is made using X-LiteSpeed-Location instead
+ * of streaming the file from PHP.
+ * Limitations:
+ *  - The Jirafeau files folder has to be placed under the document root and should be
+ *    protected from unauthorized access using rewrite rules.
+ *    See https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:config:internal-redirect#protection_from_direct_access
+ *  - Incompatible with server side encryption.
+ *  - Incompatible with one time download.
+ */
+$cfg['litespeed_workaround'] = false;
+
 /* Required flag to test if the installation is already installed
  * or needs to start the installation script
  */
 
     if (!is_writable(VAR_ASYNC)) {
         add_error(t('ASYNC_DIR_W'), VAR_ASYNC);
     }
+
+    if ($cfg['enable_crypt'] && $cfg['litespeed_workaround']) {
+        add_error(t('INCOMPATIBLE_OPTIONS_W'), 'enable_crypt=true<br>litespeed_workaround=true');
+    }
+
+    if ($cfg['one_time_download'] && $cfg['litespeed_workaround']) {
+        add_error(t('INCOMPATIBLE_OPTIONS_W'), 'one_time_download=true<br>litespeed_workaround=true');
+    }
 }
 
 /**
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "قد لا يدعم متصفحك HTML5، لذا فإن الحد الأقصى لحجم الملف هو ",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "الدخول محظور",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Ваш браўзер не падтрымлівае HTML5, таму максімальны памер файла ",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "Jirafeau — ваша вэб-сховішча файлаў",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "আপনার ব্রাউজার HTML5 সমর্থন নাও করতে পারে, তাই সর্বোচ্চ ফাইলের আকার ",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "El teu navegador no és compatible amb HTML5, per tant, la mida màxima del fitxers és ",
     "PLURAL_ENDING": "s",
     "JI_WEB_RE": "Jirafeau, el teu repositori web de fitxers",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Váš prohlížeč nepodporuje HTML5, proto je maximílní velikost souboru ",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Přístup zamítnut",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Ihr Browser unterstützt möglicherweise kein HTML5, demnach beträgt die maximale Dateigröße ",
     "PLURAL_ENDING": "n",
     "ACCESS_KO": "Zugriff verweigert",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "Χώρος αποθήκευσης των διαδικτυακών σας αρχείων",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "The following configuration options are incompatible:",
     "NO_BROWSER_SUPPORT": "Your browser may not support HTML5, so the maximum file size is ",
     "PLURAL_ENDING": "s",
     "JI_WEB_RE":  "Jirafeau, your web file repository",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Tu navegador puede no soportar HTML5, debido a esto el tamaño máximo de archivo es ",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Acceso denegado",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Pääsy estetty",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Votre navigateur ne supporte apparemment pas HTML5, la taille maximale de fichier téléchargable est de ",
     "PLURAL_ENDING": "s",
     "ACCESS_KO": "Accès interdit",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "Jirafeau , אתר הקבצים שלך",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "", 
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "Jirafeau, Vaše mrežno skladište datoteka",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Hozzáférés megtagadva",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Il tuo browser potrebbe non supportare HTML5, quindi la dimensione massima del file è ",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Accesso negato",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "", 
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "", 
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "Jirafeau, web filibus depositum tuum",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Det kan hende at nettleseren din ikke støtter HTML5, så største tillatte filstørrelse er ",
     "PLURAL_ENDING": "er",
     "JI_WEB_RE": "Jirafeau, din vevfilpakkebrønn",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Toegang geweigerd",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "Jirafeau, twoje internetowe repozytorium plików",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
     "NO_BROWSER_SUPPORT": "O seu navegador pode não suportar HTML5, então o tamanho máximo do ficheiro é ",
+    "INCOMPATIBLE_OPTIONS_W": "",
     "PLURAL_ENDING": "s",
     "ACCESS_KO": "Acesso negado",
     "ACTION": "Acção",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Seu navegador pode não suportar HTML5, portanto, o tamanho máximo do arquivo é ",
     "PLURAL_ENDING": "s",
     "JI_WEB_RE": "Jirafeau, o seu repositório web de arquivos",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Acces refuzat", 
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Доступ запрещён",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Zdá sa, že Váš prehliadač nepodporuje HTML5. Maximálna veľkosť súboru je preto obmedzená na ",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Prístup odoprený",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "Приступ одбијен", 
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "Följande inställningar är inkompatibla:",
     "NO_BROWSER_SUPPORT": "Din webbläsare verkar inte stödja HTML5, så max filstorlek är ",
     "PLURAL_ENDING": "er",
     "JI_WEB_RE": "Jirafeau, ditt filutrymme på nätet",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "JI_WEB_RE": "",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "Tarayıcınız HTML5'i desteklemeyebilir, bu nedenle en fazla dosya boyutu ",
     "PLURAL_ENDING": "sn",
     "JI_WEB_RE": "Jirafeau, web dosya deponuz",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "",
     "PLURAL_ENDING": "",
     "ACCESS_KO": "", 
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "您的浏览器可能不支持 HTML5,因此文件最大为 ",
     "PLURAL_ENDING": "s",
     "ACCESS_KO": "拒绝访问",
 
 {
+    "INCOMPATIBLE_OPTIONS_W": "",
     "NO_BROWSER_SUPPORT": "您的瀏覽器可能不支持 HTML5,因此文件最大為 ",
     "PLURAL_ENDING": "s",
     "ACCESS_KO": "拒絕訪問",