private function correctProtectedFiles() { $protectedConfigs = $this->protectedSettingsToArray(); if (count($protectedConfigs) > 0) { $ftpObect = new EasyWiFTP($this->appMasterServerDetails['ssh2IP'], $this->appMasterServerDetails['ftpPort'], $this->appServerDetails['userNameExecute'], $this->appServerDetails['ftpPasswordExecute']); if ($ftpObect->loggedIn === true) { foreach ($protectedConfigs as $config => $values) { $cvarsNotFound = $values['cvars']; $fileWithPath = $this->appServerDetails['absoluteFTPPath'] . '/' . $config; $fileAndPath = $this->getFileAndPathName($fileWithPath); $path = $fileAndPath['path']; $fileName = $fileAndPath['file']; if (!$ftpObect->downloadToTemp($fileWithPath)) { $fileWithPath = $this->appServerDetails['absoluteFTPPathNoChroot'] . '/' . $config; $fileAndPath = $this->getFileAndPathName($fileWithPath); $path = $fileAndPath['path']; $fileName = $fileAndPath['file']; } $configFileContent = $ftpObect->getTempFileContent(); // We have one temp handle for all files to reduce the amount of needed ram $ftpObect->tempHandle = null; // Depending how the file was uploaded and written, there might be lots of not needed characters in the file // A clean up will make the file handling lot easier $configFileContent = str_replace(array("", "\\b", "\r", "\\Z"), "", $configFileContent); $lines = explode("\n", $configFileContent); $lineCount = count($lines) - 1; $i = 0; // iterate over all lines foreach ($lines as $singeLine) { // Set to false on each iteration to be able to detect config overwrites $edited = false; // For easier comparison make a string to lower $loweredSingleLine = strtolower($singeLine); foreach ($values['cvars'] as $cvar => $value) { if ($values['type'] == 'cfg' and preg_match('/^[\\s\\/]{0,}' . strtolower($cvar) . '\\s+(.*)$/', $loweredSingleLine)) { $edited = true; unset($cvarsNotFound[$cvar]); $splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY); $ftpObect->writeContentToTemp(isset($splitLine[1]) ? $splitLine[0] . $cvar . ' ' . $value : $cvar . ' ' . $value); } else { if ($values['type'] == 'yml' and preg_match('/^[\\s\\/]{0,}' . strtolower($cvar) . '\\s+(.*)$/', $loweredSingleLine)) { $edited = true; unset($cvarsNotFound[$cvar]); $splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY); $ftpObect->writeContentToTemp(isset($splitLine[1]) ? $splitLine[0] . $cvar . ' ' . $value : $cvar . ' ' . $value); } else { if ($values['type'] == 'ini' and preg_match('/^[\\s\\/]{0,}' . strtolower($cvar) . '[\\s+]{0,}\\=[\\s+]{0,}(.*)$/', $loweredSingleLine)) { $edited = true; unset($cvarsNotFound[$cvar]); $ftpObect->writeContentToTemp($cvar . '=' . $value); } else { if ($values['type'] == 'ddot' and preg_match('/^[\\s\\/]{0,}' . strtolower($cvar) . '[\\s+]{0,}\\:[\\s+]{0,}(.*)$/', $loweredSingleLine)) { $edited = true; unset($cvarsNotFound[$cvar]); $ftpObect->writeContentToTemp($cvar . ':' . $value); } else { if ($values['type'] == 'lua' and preg_match("/^(.*)" . strtolower($cvar) . "[\\s+]{0,}\\=[\\s+]{0,}(.*)[\\,]\$/", $loweredSingleLine)) { $edited = true; unset($cvarsNotFound[$cvar]); $splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY); $ftpObect->writeContentToTemp(isset($splitLine[1]) ? $splitLine[0] . $cvar . ' = ' . $value : $cvar . '=' . $value); } else { if ($values['type'] == 'json' and preg_match("/^(.*)[\"]" . strtolower($cvar) . "[\\s+]{0,}:[\\s+]{0,}(.*)[\\,]{0,1}\$/", $loweredSingleLine)) { $edited = true; unset($cvarsNotFound[$cvar]); $splitLine = preg_split('/' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY); $ftpObect->writeContentToTemp(isset($splitLine[1]) ? $splitLine[0] . $cvar . ' : ' . $value : $cvar . ':' . $value); } else { if ($values['type'] == 'xml' and preg_match("/^(.*)<" . strtolower($cvar) . ">(.*)<\\/" . strtolower($cvar) . ">(.*)\$/", $loweredSingleLine)) { $edited = true; unset($cvarsNotFound[$cvar]); $splitLine = preg_split('/\\<' . $cvar . '/', $singeLine, -1, PREG_SPLIT_NO_EMPTY); $ftpObect->writeContentToTemp(isset($splitLine[1]) ? $splitLine[0] . '<' . $cvar . '>' . $value . '</' . $cvar . '>' : '<' . $cvar . '> ' . $value . '</' . $cvar . '>'); } } } } } } } } // Write untouched content if ($edited == false) { $ftpObect->writeContentToTemp($singeLine); } // If we do not count, we would add a newline at the end every time, a file is edited if ($i < $lineCount) { $ftpObect->writeContentToTemp("\r\n"); } $i++; } $debug = array(); // In case of ini or CFG files we can add entries, which are missing from the file and should be protected foreach ($cvarsNotFound as $cvar => $value) { if ($values['type'] == 'cfg') { $ftpObect->writeContentToTemp($cvar . ' ' . $value . "\r\n"); } else { if ($values['type'] == 'ini') { $ftpObect->writeContentToTemp($cvar . '=' . $value . "\r\n"); } else { if ($values['type'] == 'ddot') { $ftpObect->writeContentToTemp($cvar . ':' . $value . "\r\n"); } else { $debug[] = 'Type is: ' . $values['type'] . ' and key value: ' . $cvar . '=>' . $value; } } } } $ftpObect->uploadFileFromTemp($path, $fileName, false); } } $ftpObect->logOut(); } }