| 1 |
<?php |
| 2 |
|
| 3 |
namespace LAM\PROFILES; |
| 4 |
|
| 5 |
use LAM\PERSISTENCE\ConfigurationDatabase; |
| 6 |
use LAM\TYPES\TypeManager; |
| 7 |
use LAMCfgMain; |
| 8 |
use LAMConfig; |
| 9 |
use \LAMException; |
| 10 |
use PDO; |
| 11 |
use ServerProfilePersistenceManager; |
| 12 |
use function LAM\PERSISTENCE\dbTableExists; |
| 13 |
|
| 14 |
/* |
| 15 |
|
| 16 |
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) |
| 17 |
Copyright (C) 2003 - 2026 Roland Gruber |
| 18 |
|
| 19 |
This program is free software; you can redistribute it and/or modify |
| 20 |
it under the terms of the GNU General Public License as published by |
| 21 |
the Free Software Foundation; either version 2 of the License, or |
| 22 |
(at your option) any later version. |
| 23 |
|
| 24 |
This program is distributed in the hope that it will be useful, |
| 25 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 |
GNU General Public License for more details. |
| 28 |
|
| 29 |
You should have received a copy of the GNU General Public License |
| 30 |
along with this program; if not, write to the Free Software |
| 31 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 32 |
|
| 33 |
*/ |
| 34 |
|
| 35 |
/** |
| 36 |
* This file provides functions to load and save account profiles. |
| 37 |
* |
| 38 |
* @package profiles |
| 39 |
* @author Roland Gruber |
| 40 |
*/ |
| 41 |
|
| 42 |
/** |
| 43 |
* Manages the persistence of account profiles. |
| 44 |
* |
| 45 |
* @package LAM\PROFILES |
| 46 |
*/ |
| 47 |
class AccountProfilePersistenceManager { |
| 48 |
|
| 49 |
/** |
| 50 |
* @var AccountProfilePersistenceStrategy |
| 51 |
*/ |
| 52 |
private $strategy; |
| 53 |
|
| 54 |
/** |
| 55 |
* Constructor |
| 56 |
*/ |
| 57 |
public function __construct() { |
| 58 |
$configDb = new ConfigurationDatabase(new LAMCfgMain()); |
| 59 |
if ($configDb->useRemoteDb()) { |
| 60 |
$this->strategy = new AccountProfilePersistenceStrategyPdo($configDb->getPdo()); |
| 61 |
} |
| 62 |
else { |
| 63 |
$this->strategy = new AccountProfilePersistenceStrategyFiles(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Returns the names of existing account profile templates. |
| 69 |
* |
| 70 |
* @return array<string, string[]> scope => names (e.g. array('user' => array('default'))) |
| 71 |
* @throws LAMException error reading templates |
| 72 |
*/ |
| 73 |
public function getAccountProfileTemplateNames(): array { |
| 74 |
return $this->strategy->getAccountProfileTemplateNames(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Deletes an account profile template. |
| 79 |
* |
| 80 |
* @param string $scope user/group/host |
| 81 |
* @param string $name account profile name |
| 82 |
* @throws LAMException error deleting template |
| 83 |
*/ |
| 84 |
public function deleteAccountProfileTemplate(string $scope, string $name): void { |
| 85 |
if (!$this->isValidAccountProfileName($name) || !TypeManager::isValidTypeId($scope) || ($name === 'default')) { |
| 86 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$scope"); |
| 87 |
throw new LAMException(_("Unable to delete profile!")); |
| 88 |
} |
| 89 |
$this->strategy->deleteAccountProfileTemplate($scope, $name); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Reads an account profile template. |
| 94 |
* |
| 95 |
* @param string $scope account type |
| 96 |
* @param string $name template name |
| 97 |
* @return array<string, string[]> template data |
| 98 |
* @throws LAMException error reading template |
| 99 |
*/ |
| 100 |
public function loadAccountProfileTemplate(string $scope, string $name): array { |
| 101 |
if (!$this->isValidAccountProfileName($name) || !TypeManager::isValidTypeId($scope)) { |
| 102 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$scope"); |
| 103 |
throw new LAMException(_("Unable to load profile!")); |
| 104 |
} |
| 105 |
return $this->strategy->loadAccountProfileTemplate($scope, $name); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Writes an account profile template. |
| 110 |
* |
| 111 |
* @param string $scope account type |
| 112 |
* @param string $name template name |
| 113 |
* @param array<string, string[]> $data profile data |
| 114 |
* @throws LAMException error writing template |
| 115 |
*/ |
| 116 |
public function writeAccountProfileTemplate(string $scope, string $name, array $data): void { |
| 117 |
if (!$this->isValidAccountProfileName($name) || !TypeManager::isValidTypeId($scope)) { |
| 118 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$scope"); |
| 119 |
throw new LAMException(_('Unable to write account profile template.')); |
| 120 |
} |
| 121 |
$this->strategy->writeAccountProfileTemplate($scope, $name, $data); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Installs templates to the given server profile. |
| 126 |
* |
| 127 |
* @param string $confName server profile name |
| 128 |
* @throws LAMException |
| 129 |
*/ |
| 130 |
public function installAccountProfileTemplates(string $confName): void { |
| 131 |
if (!LAMConfig::isValidName($confName)) { |
| 132 |
logNewMessage(LOG_NOTICE, "Invalid server profile name: $confName"); |
| 133 |
throw new LAMException(_("Unable to write account profile template.")); |
| 134 |
} |
| 135 |
$allTemplates = $this->getAccountProfileTemplateNames(); |
| 136 |
$serverProfilesPersistenceManager = new ServerProfilePersistenceManager(); |
| 137 |
$config = $serverProfilesPersistenceManager->loadProfile($confName); |
| 138 |
$typeManager = new TypeManager($config); |
| 139 |
foreach ($typeManager->getConfiguredTypes() as $type) { |
| 140 |
if (empty($allTemplates[$type->getScope()])) { |
| 141 |
continue; |
| 142 |
} |
| 143 |
foreach ($allTemplates[$type->getScope()] as $templateName) { |
| 144 |
if ($this->isAccountProfileExisting($type->getId(), $templateName, $confName)) { |
| 145 |
continue; |
| 146 |
} |
| 147 |
$data = $this->loadAccountProfileTemplate($type->getScope(), $templateName); |
| 148 |
$this->writeAccountProfile($type->getId(), $templateName, $confName, $data); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns all account profile names for the given type id. |
| 155 |
* |
| 156 |
* @param string $typeId type ID |
| 157 |
* @param string $confName server profile name |
| 158 |
* @return string[] names |
| 159 |
* @throws LAMException error reading profiles |
| 160 |
*/ |
| 161 |
public function getAccountProfileNames(string $typeId, string $confName): array { |
| 162 |
if (!TypeManager::isValidTypeId($typeId) || !LAMConfig::isValidName($confName)) { |
| 163 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $confName:$typeId"); |
| 164 |
throw new LAMException(_("Unable to read profiles.")); |
| 165 |
} |
| 166 |
return $this->strategy->getAccountProfileNames($typeId, $confName); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Writes an account profile. |
| 171 |
* |
| 172 |
* @param $typeId $scope |
| 173 |
* @param string $name account profile name |
| 174 |
* @param string $confName server profile name |
| 175 |
* @param array<string, string[]> $data profile data |
| 176 |
* @throws LAMException error writing template |
| 177 |
*/ |
| 178 |
public function writeAccountProfile(string $typeId, string $name, string $confName, array $data): void { |
| 179 |
if (!$this->isValidAccountProfileName($name) |
| 180 |
|| !TypeManager::isValidTypeId($typeId) |
| 181 |
|| !LAMConfig::isValidName($confName)) { |
| 182 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$typeId"); |
| 183 |
throw new LAMException(_("Unable to save profile!")); |
| 184 |
} |
| 185 |
$this->strategy->writeAccountProfile($typeId, $name, $confName, $data); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns if the given profile name is valid. |
| 190 |
* |
| 191 |
* @param string $name profile name |
| 192 |
* @return bool is valid profile |
| 193 |
*/ |
| 194 |
private function isValidAccountProfileName(string $name): bool { |
| 195 |
return preg_match("/^[0-9a-z _-]+$/i", $name) === 1; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Checks if the given profile is already existing. |
| 200 |
* |
| 201 |
* @param string $typeId user/group/host |
| 202 |
* @param string $name profile name |
| 203 |
* @param string $confName server profile name |
| 204 |
* @return bool profile exists |
| 205 |
* @throws LAMException error while checking |
| 206 |
*/ |
| 207 |
public function isAccountProfileExisting(string $typeId, string $name, string $confName): bool { |
| 208 |
if (!$this->isValidAccountProfileName($name) |
| 209 |
|| !TypeManager::isValidTypeId($typeId) |
| 210 |
|| !LAMConfig::isValidName($confName)) { |
| 211 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$typeId"); |
| 212 |
return false; |
| 213 |
} |
| 214 |
return $this->strategy->isAccountProfileExisting($typeId, $name, $confName); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Loads an account profile. |
| 219 |
* |
| 220 |
* @param string $typeId user/group/host |
| 221 |
* @param string $name account profile name |
| 222 |
* @param string $confName server profile name |
| 223 |
* @return array<string, string[]> profile data |
| 224 |
* @throws LAMException error writing template |
| 225 |
*/ |
| 226 |
public function loadAccountProfile(string $typeId, string $name, string $confName): array { |
| 227 |
if (!$this->isValidAccountProfileName($name) |
| 228 |
|| !TypeManager::isValidTypeId($typeId) |
| 229 |
|| !LAMConfig::isValidName($confName)) { |
| 230 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$typeId"); |
| 231 |
throw new LAMException(_("Unable to load profile!")); |
| 232 |
} |
| 233 |
return $this->strategy->loadAccountProfile($typeId, $name, $confName); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Deletes an account profile. |
| 238 |
* |
| 239 |
* @param string $typeId user/group/host |
| 240 |
* @param string $name account profile name |
| 241 |
* @param string $confName server profile name |
| 242 |
* @throws LAMException error deleting profile |
| 243 |
*/ |
| 244 |
public function deleteAccountProfile(string $typeId, string $name, string $confName): void { |
| 245 |
if (!isLoggedIn() || !$this->isValidAccountProfileName($name) |
| 246 |
|| !TypeManager::isValidTypeId($typeId) |
| 247 |
|| !LAMConfig::isValidName($confName)) { |
| 248 |
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$typeId"); |
| 249 |
throw new LAMException(_('Unable to delete profile!')); |
| 250 |
} |
| 251 |
$this->strategy->deleteAccountProfile($typeId, $name, $confName); |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Reads and writes account profiles. |
| 258 |
* |
| 259 |
* @package LAM\PROFILES |
| 260 |
*/ |
| 261 |
interface AccountProfilePersistenceStrategy { |
| 262 |
|
| 263 |
/** |
| 264 |
* Returns the names of existing account profile templates. |
| 265 |
* |
| 266 |
* @return array<string, string[]> scope => names (e.g. array('user' => array('default'))) |
| 267 |
* @throws LAMException error reading templates |
| 268 |
*/ |
| 269 |
public function getAccountProfileTemplateNames(): array; |
| 270 |
|
| 271 |
/** |
| 272 |
* Deletes an account profile template. |
| 273 |
* |
| 274 |
* @param string $scope user/group/host |
| 275 |
* @param string $name account profile name |
| 276 |
* @throws LAMException error deleting template |
| 277 |
*/ |
| 278 |
public function deleteAccountProfileTemplate(string $scope, string $name): void; |
| 279 |
|
| 280 |
/** |
| 281 |
* Reads an account profile template. |
| 282 |
* |
| 283 |
* @param string $scope account type |
| 284 |
* @param string $name template name |
| 285 |
* @return array<string, string[]> template data |
| 286 |
* @throws LAMException error reading template |
| 287 |
*/ |
| 288 |
public function loadAccountProfileTemplate(string $scope, string $name): array; |
| 289 |
|
| 290 |
/** |
| 291 |
* Writes an account profile template. |
| 292 |
* |
| 293 |
* @param string $scope account type |
| 294 |
* @param string $name template name |
| 295 |
* @param array<string, string[]> $data profile data |
| 296 |
* @throws LAMException error writing template |
| 297 |
*/ |
| 298 |
public function writeAccountProfileTemplate(string $scope, string $name, array $data): void; |
| 299 |
|
| 300 |
/** |
| 301 |
* Checks if the given profile is already existing. |
| 302 |
* |
| 303 |
* @param string $typeId user/group/host |
| 304 |
* @param string $name profile name |
| 305 |
* @param string $confName server profile name |
| 306 |
* @return bool profile exists |
| 307 |
* @throws LAMException error while checking |
| 308 |
* @throws LAMException error writing template |
| 309 |
*/ |
| 310 |
public function isAccountProfileExisting(string $typeId, string $name, string $confName): bool; |
| 311 |
|
| 312 |
/** |
| 313 |
* Returns all account profile names for the given type id. |
| 314 |
* |
| 315 |
* @param string $typeId type ID |
| 316 |
* @param string $confName server profile name |
| 317 |
* @return string[] names |
| 318 |
* @throws LAMException error reading profiles |
| 319 |
*/ |
| 320 |
public function getAccountProfileNames(string $typeId, string $confName): array; |
| 321 |
|
| 322 |
/** |
| 323 |
* Writes an account profile. |
| 324 |
* |
| 325 |
* @param $typeId account type ID |
| 326 |
* @param string $name profile name |
| 327 |
* @param string $confName server profile name |
| 328 |
* @param array<string, string[]> $data profile data |
| 329 |
* @throws LAMException error writing template |
| 330 |
*/ |
| 331 |
public function writeAccountProfile(string $typeId, string $name, string $confName, array $data): void; |
| 332 |
|
| 333 |
/** |
| 334 |
* Loads an account profile. |
| 335 |
* |
| 336 |
* @param string $typeId user/group/host |
| 337 |
* @param string $name account profile name |
| 338 |
* @param string $confName server profile name |
| 339 |
* @return array<string, string[]> profile data |
| 340 |
* @throws LAMException error writing template |
| 341 |
*/ |
| 342 |
public function loadAccountProfile(string $typeId, string $name, string $confName): array; |
| 343 |
|
| 344 |
/** |
| 345 |
* Deletes an account profile. |
| 346 |
* |
| 347 |
* @param string $typeId user/group/host |
| 348 |
* @param string $name account profile name |
| 349 |
* @param string $confName server profile name |
| 350 |
* @throws LAMException error deleting profile |
| 351 |
*/ |
| 352 |
public function deleteAccountProfile(string $typeId, string $name, string $confName): void; |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Manages account profiles on file system. |
| 358 |
* |
| 359 |
* @package LAM\PROFILES |
| 360 |
*/ |
| 361 |
class AccountProfilePersistenceStrategyFiles implements AccountProfilePersistenceStrategy { |
| 362 |
|
| 363 |
/** |
| 364 |
* @inheritDoc |
| 365 |
*/ |
| 366 |
public function getAccountProfileTemplateNames(): array { |
| 367 |
$templatePath = __DIR__ . '/../config/templates/profiles'; |
| 368 |
if (!is_dir($templatePath) || !is_readable($templatePath)) { |
| 369 |
throw new LAMException(_('Unable to read templates.')); |
| 370 |
} |
| 371 |
$templateDir = @dir($templatePath); |
| 372 |
$templates = []; |
| 373 |
if ($templateDir) { |
| 374 |
$entry = $templateDir->read(); |
| 375 |
while ($entry) { |
| 376 |
$parts = explode('.', $entry); |
| 377 |
if ((strlen($entry) > 3) && (count($parts) === 2)) { |
| 378 |
$name = $parts[0]; |
| 379 |
$scope = $parts[1]; |
| 380 |
$templates[$scope][] = $name; |
| 381 |
} |
| 382 |
$entry = $templateDir->read(); |
| 383 |
} |
| 384 |
} |
| 385 |
return $templates; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* @inheritDoc |
| 390 |
*/ |
| 391 |
public function deleteAccountProfileTemplate(string $scope, string $name): void { |
| 392 |
$fileName = $this->getAccountProfileTemplateFileName($scope, $name); |
| 393 |
$deleted = @unlink($fileName); |
| 394 |
if (!$deleted) { |
| 395 |
throw new LAMException(_("Unable to delete profile!")); |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Returns the file name of a global template. |
| 401 |
* |
| 402 |
* @param string $scope e.g. user |
| 403 |
* @param string $name profile name |
| 404 |
* @return string file name |
| 405 |
*/ |
| 406 |
private function getAccountProfileTemplateFileName(string $scope, string $name): string { |
| 407 |
return __DIR__ . '/../config/templates/profiles' . '/' . $name . '.' . $scope; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* @inheritDoc |
| 412 |
*/ |
| 413 |
public function loadAccountProfileTemplate(string $scope, string $name): array { |
| 414 |
$fileName = $this->getAccountProfileTemplateFileName($scope, $name); |
| 415 |
return $this->readAccountProfileFile($fileName); |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* @inheritDoc |
| 420 |
*/ |
| 421 |
public function writeAccountProfileTemplate(string $scope, string $name, array $data): void { |
| 422 |
$fileName = $this->getAccountProfileTemplateFileName($scope, $name); |
| 423 |
$this->writeAccountProfileDataToFile($fileName, $data); |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Reads an account profile from the given file name. |
| 428 |
* |
| 429 |
* @param string $fileName file name |
| 430 |
* @return array<string, string[]> hash array (attribute => value) |
| 431 |
* @throws LAMException error reading file |
| 432 |
*/ |
| 433 |
private function readAccountProfileFile(string $fileName): array { |
| 434 |
$settings = []; |
| 435 |
if (is_file($fileName)) { |
| 436 |
$file = @fopen($fileName, "r"); |
| 437 |
if ($file) { |
| 438 |
while (!feof($file)) { |
| 439 |
$line = fgets($file, 1024); |
| 440 |
if (in_array($line, [false, '', "\n"], true) || ($line[0] === "#")) { |
| 441 |
continue; // ignore comments |
| 442 |
} |
| 443 |
// search keywords |
| 444 |
$parts = explode(": ", $line); |
| 445 |
if (count($parts) === 2) { |
| 446 |
$option = $parts[0]; |
| 447 |
if ($option === 'profname') { |
| 448 |
continue; |
| 449 |
} |
| 450 |
$value = $parts[1]; |
| 451 |
// remove line ends |
| 452 |
$value = rtrim($value); |
| 453 |
$settings[$option] = explode("+::+", $value); |
| 454 |
} |
| 455 |
} |
| 456 |
fclose($file); |
| 457 |
return $settings; |
| 458 |
} |
| 459 |
else { |
| 460 |
throw new LAMException(_("Unable to load profile!"), $fileName); |
| 461 |
} |
| 462 |
} |
| 463 |
else { |
| 464 |
throw new LAMException(_("Unable to load profile!"), $fileName); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
/** |
| 469 |
* Writes the profile data to the given file. |
| 470 |
* |
| 471 |
* @param string $fileName file name |
| 472 |
* @param array<string, string[]> $data profile data |
| 473 |
* @throws LAMException error writing file |
| 474 |
*/ |
| 475 |
private function writeAccountProfileDataToFile(string $fileName, array $data): void { |
| 476 |
$basePath = dirname($fileName); |
| 477 |
if (!file_exists($basePath)) { |
| 478 |
mkdir($basePath, 0700, true); |
| 479 |
} |
| 480 |
$file = @fopen($fileName, "w"); |
| 481 |
if (!$file) { |
| 482 |
logNewMessage(LOG_ERR, 'Unable to write ' . $fileName); |
| 483 |
throw new LAMException(_('Unable to save profile!')); |
| 484 |
} |
| 485 |
// write attributes |
| 486 |
$keys = array_keys($data); |
| 487 |
for ($i = 0; $i < count($keys); $i++) { |
| 488 |
$line = isset($data[$keys[$i]]) ? $keys[$i] . ": " . implode("+::+", $data[$keys[$i]]) . "\n" : $keys[$i] . ": \n"; |
| 489 |
fwrite($file, $line); |
| 490 |
} |
| 491 |
// close file |
| 492 |
fclose($file); |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Returns the file name of a global template. |
| 497 |
* |
| 498 |
* @param string $typeId e.g. user |
| 499 |
* @param string $name profile name |
| 500 |
* @param string $confName server profile name |
| 501 |
* @return string file name |
| 502 |
*/ |
| 503 |
private function getAccountProfileFileName(string $typeId, string $name, string $confName): string { |
| 504 |
return __DIR__ . "/../config/profiles/" . $confName . '/' . $name . "." . $typeId; |
| 505 |
} |
| 506 |
|
| 507 |
/** |
| 508 |
* @inheritDoc |
| 509 |
*/ |
| 510 |
public function isAccountProfileExisting(string $typeId, string $name, string $confName): bool { |
| 511 |
$fileName = $this->getAccountProfileFileName($typeId, $name, $confName); |
| 512 |
return file_exists($fileName) && is_readable($fileName); |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* @inheritDoc |
| 517 |
*/ |
| 518 |
public function writeAccountProfile(string $typeId, string $name, string $confName, array $data): void { |
| 519 |
$fileName = $this->getAccountProfileFileName($typeId, $name, $confName); |
| 520 |
$this->writeAccountProfileDataToFile($fileName, $data); |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* @inheritDoc |
| 525 |
*/ |
| 526 |
public function loadAccountProfile(string $typeId, string $name, string $confName): array { |
| 527 |
$fileName = $this->getAccountProfileFileName($typeId, $name, $confName); |
| 528 |
return $this->readAccountProfileFile($fileName); |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* @inheritDoc |
| 533 |
*/ |
| 534 |
public function deleteAccountProfile(string $typeId, string $name, string $confName): void { |
| 535 |
$fileName = $this->getAccountProfileFileName($typeId, $name, $confName); |
| 536 |
$deleted = @unlink($fileName); |
| 537 |
if (!$deleted) { |
| 538 |
throw new LAMException(_("Unable to delete profile!")); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* @inheritDoc |
| 544 |
*/ |
| 545 |
public function getAccountProfileNames(string $typeId, string $confName): array { |
| 546 |
$dir = @dir(__DIR__ . "/../config/profiles/" . $confName); |
| 547 |
$ret = []; |
| 548 |
if ($dir) { |
| 549 |
$entry = $dir->read(); |
| 550 |
while ($entry) { |
| 551 |
// check if filename ends with .<typeId> |
| 552 |
if (strrpos($entry, '.')) { |
| 553 |
$pos = strrpos($entry, '.'); |
| 554 |
if (substr($entry, $pos + 1) === $typeId) { |
| 555 |
$name = substr($entry, 0, $pos); |
| 556 |
$ret[] = $name; |
| 557 |
} |
| 558 |
} |
| 559 |
$entry = $dir->read(); |
| 560 |
} |
| 561 |
} |
| 562 |
return $ret; |
| 563 |
} |
| 564 |
|
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Manages account profiles via PDO. |
| 569 |
* |
| 570 |
* @package LAM\PROFILES |
| 571 |
*/ |
| 572 |
class AccountProfilePersistenceStrategyPdo implements AccountProfilePersistenceStrategy { |
| 573 |
|
| 574 |
const TABLE_NAME = 'account_profiles'; |
| 575 |
const TABLE_NAME_TEMPLATES = 'account_profiles_templates'; |
| 576 |
|
| 577 |
/** |
| 578 |
* @var PDO |
| 579 |
*/ |
| 580 |
private $pdo; |
| 581 |
|
| 582 |
/** |
| 583 |
* Constructor |
| 584 |
* |
| 585 |
* @param PDO $pdo PDO |
| 586 |
*/ |
| 587 |
public function __construct(PDO $pdo) { |
| 588 |
$this->pdo = $pdo; |
| 589 |
$this->checkSchema(); |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Checks if the schema has latest version. |
| 594 |
*/ |
| 595 |
private function checkSchema(): void { |
| 596 |
if (!dbTableExists($this->pdo, self::TABLE_NAME)) { |
| 597 |
$this->createInitialSchema(); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
/** |
| 602 |
* Creates the initial schema. |
| 603 |
*/ |
| 604 |
public function createInitialSchema(): void { |
| 605 |
logNewMessage(LOG_DEBUG, 'Creating database table ' . self::TABLE_NAME); |
| 606 |
$sql = 'create table ' . self::TABLE_NAME . '(' |
| 607 |
. 'position int NOT NULL,' |
| 608 |
. 'confname VARCHAR(300) NOT NULL,' |
| 609 |
. 'typeid VARCHAR(300) NOT NULL,' |
| 610 |
. 'name VARCHAR(300) NOT NULL,' |
| 611 |
. 'data TEXT NOT NULL,' |
| 612 |
. 'PRIMARY KEY(position)' |
| 613 |
. ');'; |
| 614 |
$this->pdo->exec($sql); |
| 615 |
logNewMessage(LOG_DEBUG, 'Creating database table ' . self::TABLE_NAME_TEMPLATES); |
| 616 |
$sql = 'create table ' . self::TABLE_NAME_TEMPLATES . '(' |
| 617 |
. 'scope VARCHAR(100) NOT NULL,' |
| 618 |
. 'name VARCHAR(300) NOT NULL,' |
| 619 |
. 'data TEXT NOT NULL,' |
| 620 |
. 'PRIMARY KEY(scope,name)' |
| 621 |
. ');'; |
| 622 |
$this->pdo->exec($sql); |
| 623 |
$sql = 'insert into ' . ConfigurationDatabase::TABLE_SCHEMA_VERSIONS . ' (name, version) VALUES ("account_profiles", 1);'; |
| 624 |
$this->pdo->exec($sql); |
| 625 |
} |
| 626 |
|
| 627 |
/** |
| 628 |
* @inheritDoc |
| 629 |
*/ |
| 630 |
public function getAccountProfileTemplateNames(): array { |
| 631 |
$statement = $this->pdo->prepare("SELECT scope, name FROM " . self::TABLE_NAME_TEMPLATES); |
| 632 |
$statement->execute(); |
| 633 |
$results = $statement->fetchAll(); |
| 634 |
$profiles = []; |
| 635 |
foreach ($results as $result) { |
| 636 |
$profiles[$result['scope']][] = $result['name']; |
| 637 |
} |
| 638 |
return $profiles; |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* @inheritDoc |
| 643 |
*/ |
| 644 |
public function deleteAccountProfileTemplate(string $scope, string $name): void { |
| 645 |
$statement = $this->pdo->prepare("DELETE FROM " . self::TABLE_NAME_TEMPLATES . " WHERE scope = ? AND name = ?"); |
| 646 |
$statement->execute([$scope, $name]); |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* @inheritDoc |
| 651 |
*/ |
| 652 |
public function loadAccountProfileTemplate(string $scope, string $name): array { |
| 653 |
$statement = $this->pdo->prepare("SELECT data FROM " . self::TABLE_NAME_TEMPLATES . " WHERE scope = ? AND name = ?"); |
| 654 |
$statement->execute([$scope, $name]); |
| 655 |
$results = $statement->fetchAll(); |
| 656 |
if (empty($results)) { |
| 657 |
logNewMessage(LOG_ERR, "Account profile template not found: $scope-$name"); |
| 658 |
throw new LAMException(_("Unable to load profile!"), $name); |
| 659 |
} |
| 660 |
return json_decode($results[0]['data'], true); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* @inheritDoc |
| 665 |
*/ |
| 666 |
public function writeAccountProfileTemplate(string $scope, string $name, array $data): void { |
| 667 |
$json = json_encode($data); |
| 668 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME_TEMPLATES . " WHERE scope = ? AND name = ?"); |
| 669 |
$statement->execute([$scope, $name]); |
| 670 |
$results = $statement->fetchAll(); |
| 671 |
if (empty($results)) { |
| 672 |
$statement = $this->pdo->prepare("INSERT INTO " . self::TABLE_NAME_TEMPLATES . " (scope, name, data) VALUES (?, ?, ?)"); |
| 673 |
$statement->execute([$scope, $name, $json]); |
| 674 |
} |
| 675 |
else { |
| 676 |
$statement = $this->pdo->prepare("UPDATE " . self::TABLE_NAME_TEMPLATES . " SET data = ? WHERE scope = ? AND name = ?"); |
| 677 |
$statement->execute([$json, $scope, $name]); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
/** |
| 682 |
* @inheritDoc |
| 683 |
*/ |
| 684 |
public function isAccountProfileExisting(string $typeId, string $name, string $confName): bool { |
| 685 |
$statement = $this->pdo->prepare("SELECT data FROM " . self::TABLE_NAME . " WHERE confname = ? AND typeid = ? AND name = ?"); |
| 686 |
$statement->execute([$confName, $typeId, $name]); |
| 687 |
$results = $statement->fetchAll(); |
| 688 |
return !empty($results); |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* @inheritDoc |
| 693 |
*/ |
| 694 |
public function getAccountProfileNames(string $typeId, string $confName): array { |
| 695 |
$statement = $this->pdo->prepare("SELECT name FROM " . self::TABLE_NAME . " WHERE confname = ? AND typeid = ?"); |
| 696 |
$statement->execute([$confName, $typeId]); |
| 697 |
$results = $statement->fetchAll(); |
| 698 |
$profiles = []; |
| 699 |
foreach ($results as $result) { |
| 700 |
$profiles[] = $result['name']; |
| 701 |
} |
| 702 |
return $profiles; |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* @inheritDoc |
| 707 |
*/ |
| 708 |
public function writeAccountProfile(string $typeId, string $name, string $confName, array $data): void { |
| 709 |
$json = json_encode($data); |
| 710 |
$isExisting = $this->isAccountProfileExisting($typeId, $name, $confName); |
| 711 |
if ($isExisting) { |
| 712 |
$statement = $this->pdo->prepare("UPDATE " . self::TABLE_NAME . " SET data = ? WHERE confname = ? AND typeid = ? AND name = ?"); |
| 713 |
$statement->execute([$json, $confName, $typeId, $name]); |
| 714 |
} |
| 715 |
else { |
| 716 |
$positionStatement = $this->pdo->prepare("SELECT MAX(position) AS position FROM " . self::TABLE_NAME); |
| 717 |
$positionStatement->execute(); |
| 718 |
$positionResult = $positionStatement->fetchAll(); |
| 719 |
$position = $positionResult[0]['position'] + 1; |
| 720 |
$statement = $this->pdo->prepare("INSERT INTO " . self::TABLE_NAME . " (position, confname, typeid, name, data) VALUES (?, ?, ?, ?, ?)"); |
| 721 |
$statement->execute([$position, $confName, $typeId, $name, $json]); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* @inheritDoc |
| 727 |
*/ |
| 728 |
public function loadAccountProfile(string $typeId, string $name, string $confName): array { |
| 729 |
$statement = $this->pdo->prepare("SELECT data FROM " . self::TABLE_NAME . " WHERE confname = ? AND typeid = ? AND name = ?"); |
| 730 |
$statement->execute([$confName, $typeId, $name]); |
| 731 |
$results = $statement->fetchAll(); |
| 732 |
if (empty($results)) { |
| 733 |
logNewMessage(LOG_ERR, "Account profile not found: $typeId-$name"); |
| 734 |
throw new LAMException(_("Unable to load profile!"), $name); |
| 735 |
} |
| 736 |
return json_decode($results[0]['data'], true); |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* @inheritDoc |
| 741 |
*/ |
| 742 |
public function deleteAccountProfile(string $typeId, string $name, string $confName): void { |
| 743 |
$statement = $this->pdo->prepare("DELETE FROM " . self::TABLE_NAME . " WHERE confname = ? AND typeid = ? AND name = ?"); |
| 744 |
$statement->execute([$confName, $typeId, $name]); |
| 745 |
} |
| 746 |
|
| 747 |
} |