Skip to content

Commit

Permalink
Merge pull request #4372 from corentin-soriano/api
Browse files Browse the repository at this point in the history
Replace tp.config.php by ConfigManager and remove obsoletes functions.
nilsteampassnet authored Sep 27, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 662ef86 + 82a8aa9 commit 67da6b2
Showing 24 changed files with 41 additions and 9,889 deletions.
15 changes: 12 additions & 3 deletions api/Model/AuthModel.php
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@

use TeampassClasses\PasswordManager\PasswordManager;
use TeampassClasses\NestedTree\NestedTree;
use TeampassClasses\ConfigManager\ConfigManager;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

@@ -83,8 +84,11 @@ public function getUserAuth(string $login, string $password, string $apikey): ar
return ["error" => "Login failed.", "info" => "User not allowed to use API"];
}

// Load config
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();

// Log user
include API_ROOT_PATH . '/../includes/config/tp.config.php';
logEvents($SETTINGS, 'api', 'user_connection', (string) $apiInfo['increment_id'], stripslashes($inputData['login']));

// create JWT
@@ -153,8 +157,11 @@ public function getUserAuth(string $login, string $password, string $apikey): ar
// get user folders list
$ret = $this->buildUserFoldersList($userInfo);

// Load config
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();

// Log user
include API_ROOT_PATH . '/../includes/config/tp.config.php';
logEvents($SETTINGS, 'api', 'user_connection', (string) $userInfo['id'], stripslashes($userInfo['login']));

// create JWT
@@ -228,7 +235,9 @@ private function createUserJWT(
int $allowed_to_delete,
): array
{
include API_ROOT_PATH . '/../includes/config/tp.config.php';
// Load config
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();

$payload = [
'username' => $login,
6 changes: 5 additions & 1 deletion api/Model/ItemModel.php
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
*/

use TeampassClasses\NestedTree\NestedTree;
use TeampassClasses\ConfigManager\ConfigManager;
use ZxcvbnPhp\Zxcvbn;

require_once API_ROOT_PATH . "/Model/Database.php";
@@ -148,7 +149,10 @@ public function addItem(
{
try {
include_once API_ROOT_PATH . '/../sources/main.functions.php';
include API_ROOT_PATH . '/../includes/config/tp.config.php';

// Load config
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();

// Step 1: Prepare data and sanitize inputs
$data = $this->prepareData($folderId, $label, $password, $description, $login, $email, $url, $tags, $anyone_can_modify, $icon);
8 changes: 4 additions & 4 deletions api/inc/bootstrap.php
Original file line number Diff line number Diff line change
@@ -23,15 +23,13 @@
* @see https://www.teampass.net
*/

use TeampassClasses\ConfigManager\ConfigManager;

define("API_ROOT_PATH", __DIR__ . "/..");

// include main configuration file
require API_ROOT_PATH . '/../sources/main.functions.php';

// init
loadClasses('DB');

// include the base controller file
require API_ROOT_PATH . "/Controller/Api/BaseController.php";

@@ -108,7 +106,9 @@ function checkUSerCRUDRights($userData, $actionToPerform): bool
*/
function apiIsEnabled(): string
{
include API_ROOT_PATH . '/../includes/config/tp.config.php';
// Load config
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();

if (isset($SETTINGS) === true && isset($SETTINGS['api']) === true && (int) $SETTINGS['api'] === 1) {
return json_encode(
1 change: 0 additions & 1 deletion docs/install/upgrade.md
Original file line number Diff line number Diff line change
@@ -49,7 +49,6 @@ git pull
* Copy next files from `folderv2` to `folderv3`
```
./includes/config/settings.php
./includes/config/tp.config.php
./includes/libraries/csrfp/libs/csrfp.config.php
./includes/teampass-seckey.txt
./includes/avatars/*
2 changes: 1 addition & 1 deletion includes/js/functions.js
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ function jsonErrorHdl(message)
function prepareExchangedData(data, type, key, fileName = '', functionName = '')
{
if (type === 'decode') {
if (parseInt($('#encryptClientServer').val()) === 0) {
if (parseInt($('input#encryptClientServer').val()) === 0) {
try {
return $.parseJSON(data);
}
1,594 changes: 0 additions & 1,594 deletions install/install/install.queries.php

This file was deleted.

1,263 changes: 0 additions & 1,263 deletions install/install/upgrade.php

This file was deleted.

1,239 changes: 0 additions & 1,239 deletions install/install/upgrade_ajax.php

This file was deleted.

463 changes: 0 additions & 463 deletions install/install/upgrade_operations.php

This file was deleted.

2,050 changes: 0 additions & 2,050 deletions install/install/upgrade_run_3.0.0.php

This file was deleted.

182 changes: 0 additions & 182 deletions install/install/upgrade_run_3.0.0_fields.php

This file was deleted.

224 changes: 0 additions & 224 deletions install/install/upgrade_run_3.0.0_files.php

This file was deleted.

184 changes: 0 additions & 184 deletions install/install/upgrade_run_3.0.0_logs.php

This file was deleted.

198 changes: 0 additions & 198 deletions install/install/upgrade_run_3.0.0_passwords.php

This file was deleted.

191 changes: 0 additions & 191 deletions install/install/upgrade_run_3.0.0_suggestions.php

This file was deleted.

808 changes: 0 additions & 808 deletions install/install/upgrade_run_3.0.0_users.php

This file was deleted.

716 changes: 0 additions & 716 deletions install/install/upgrade_run_3.0.php

This file was deleted.

598 changes: 0 additions & 598 deletions install/install/upgrade_run_3.1.php

This file was deleted.

8 changes: 4 additions & 4 deletions pages/uploads.js.php
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@

use TeampassClasses\PerformChecks\PerformChecks;
use TeampassClasses\SessionManager\SessionManager;
use TeampassClasses\ConfigManager\ConfigManager;
use Symfony\Component\HttpFoundation\Request;
use TeampassClasses\Language\Language;
// Load functions
@@ -47,10 +48,9 @@
}

// Load config if $SETTINGS not defined
try {
include_once __DIR__.'/../includes/config/tp.config.php';
} catch (Exception $e) {
throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
if (empty($SETTINGS)) {
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();
}

// Do checks
8 changes: 4 additions & 4 deletions pages/uploads.php
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@
use TeampassClasses\Language\Language;
use TeampassClasses\NestedTree\NestedTree;
use TeampassClasses\PerformChecks\PerformChecks;
use TeampassClasses\ConfigManager\ConfigManager;

// Load functions
require_once __DIR__.'/../sources/main.functions.php';
@@ -43,10 +44,9 @@
$lang = new Language();

// Load config if $SETTINGS not defined
try {
include_once __DIR__.'/../includes/config/tp.config.php';
} catch (Exception $e) {
throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
if (empty($SETTINGS)) {
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();
}

// Do checks
82 changes: 0 additions & 82 deletions scripts/task_maintenance_rebuild_config_file.php

This file was deleted.

70 changes: 0 additions & 70 deletions sources/main.functions.php
Original file line number Diff line number Diff line change
@@ -1738,36 +1738,6 @@ function noHTML(string $input, string $encoding = 'UTF-8'): string
return htmlspecialchars($input, ENT_QUOTES | ENT_XHTML, $encoding, false);
}


/**
* Handles the Teampass config file.
*
* @param string $action Action to perform. Accepts "rebuild" and "update".
* @param array $settings Teampass settings.
* @param string $field Field to refresh. Optional.
* @param string $value Value to set. Optional.
* @param int $isEncrypted Is the value encrypted? Optional.
*
* @return string|bool
*/
function handleConfigFile(string $action, array $settings, string $field = null, string $value = null, int $isEncrypted = 0)
{
$configFilePath = $settings['cpassman_dir'] . '/includes/config/tp.config.php';

// Load class DB
loadClasses('DB');

if (!file_exists($configFilePath) || $action === 'rebuild') {
return rebuildConfigFile($configFilePath, $settings);
}

if ($action === 'update' && !empty($field)) {
return updateConfigFile($configFilePath, (string) $field, (string) $value, (int) $isEncrypted);
}

return true;
}

/**
* Rebuilds the Teampass config file.
*
@@ -1802,44 +1772,6 @@ function rebuildConfigFile(string $configFilePath, array $settings)
return true;
}

/**
* Updates the Teampass config file.
*
* @param string $configFilePath Path to the config file.
* @param string $field Field to refresh.
* @param string $value Value to set.
* @param int $isEncrypted Is the value encrypted?
*
* @return string|bool
*/
function updateConfigFile(string $configFilePath, string $field, string $value, int $isEncrypted)
{
$data = file($configFilePath);
$lineIndex = null;
foreach ($data as $index => $line) {
if (stristr($line, ');')) {
break;
}

if (stristr($line, "'$field' => '")) {
$value = getEncryptedValue($value, $isEncrypted);
$data[$index] = " '$field' => '" . htmlspecialchars_decode($value, ENT_COMPAT) . "',\n";
$lineIndex = $index;
break;
}
}

if ($lineIndex === null) {
$value = getEncryptedValue($value, $isEncrypted);
$data[] = " '$field' => '" . htmlspecialchars_decode($value, ENT_COMPAT) . "',\n);\n";
}

// Update the file
file_put_contents($configFilePath, implode('', $data));

return true;
}

/**
* Returns the encrypted value if needed.
*
@@ -1853,8 +1785,6 @@ function getEncryptedValue(string $value, int $isEncrypted): string
return $isEncrypted ? cryption($value, '', 'encrypt')['string'] : $value;
}



/**
* Permits to replace \ to permit correct display
*
12 changes: 7 additions & 5 deletions sources/upload.attachments.php
Original file line number Diff line number Diff line change
@@ -44,10 +44,9 @@
$lang = new Language();

// Load config if $SETTINGS not defined
try {
include_once __DIR__.'/../includes/config/tp.config.php';
} catch (Exception $e) {
throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
if (empty($SETTINGS)) {
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();
}

// Do checks
@@ -191,7 +190,10 @@
}

// Load Settings
include_once $SETTINGS['cpassman_dir'] . '/includes/config/tp.config.php';
if (empty($SETTINGS)) {
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();
}
}

// HTTP headers for no cache etc
8 changes: 4 additions & 4 deletions sources/upload.files.php
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@
use TeampassClasses\Language\Language;
use EZimuel\PHPSecureSession;
use TeampassClasses\PerformChecks\PerformChecks;
use TeampassClasses\ConfigManager\ConfigManager;

// Load functions
require_once 'main.functions.php';
@@ -49,10 +50,9 @@


// Load config if $SETTINGS not defined
try {
include_once __DIR__.'/../includes/config/tp.config.php';
} catch (Exception $e) {
throw new Exception("Error file '/includes/config/tp.config.php' not exists", 1);
if (empty($SETTINGS)) {
$configManager = new ConfigManager();
$SETTINGS = $configManager->getAllSettings();
}

// Do checks

0 comments on commit 67da6b2

Please sign in to comment.