LDAPAccountManager/lam/lib/profiles.inc

375 lines
11 KiB
PHP
Raw Normal View History

<?php
2017-01-06 12:56:17 +00:00
namespace LAM\PROFILES;
use LAM\TYPES\TypeManager;
2017-01-06 12:56:17 +00:00
use \LAMException;
/*
2009-10-27 18:47:12 +00:00
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
2020-05-02 16:51:33 +00:00
Copyright (C) 2003 - 2020 Roland Gruber
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2004-05-31 17:58:27 +00:00
/**
2004-10-09 11:04:20 +00:00
* This file provides functions to load and save account profiles.
2004-05-31 17:58:27 +00:00
*
2004-10-09 11:04:20 +00:00
* @package profiles
2004-05-31 17:58:27 +00:00
* @author Roland Gruber
*/
2003-04-28 18:06:18 +00:00
2004-05-31 17:58:27 +00:00
/**
2004-10-06 20:00:17 +00:00
* Returns an array of string with all available profiles for the given account type
2004-05-31 17:58:27 +00:00
*
2017-01-03 19:02:29 +00:00
* @param string $typeId account type
2012-10-28 14:37:54 +00:00
* @param string $profile server profile name
2004-05-31 17:58:27 +00:00
* @return array profile names
*/
2017-01-03 19:02:29 +00:00
function getAccountProfiles($typeId, $profile = null) {
2012-10-28 14:37:54 +00:00
if (!isset($profile)) {
$profile = $_SESSION['config']->getName();
}
2017-01-03 19:02:29 +00:00
$dir = @dir(dirname(__FILE__) . "/../config/profiles/" . $profile);
2012-10-28 14:37:54 +00:00
2003-04-28 18:06:18 +00:00
$ret = array();
2003-09-20 17:02:21 +00:00
if ($dir) {
2004-10-06 20:00:17 +00:00
$entry = $dir->read();
while ($entry){
2017-01-03 19:02:29 +00:00
// check if filename ends with .<typeId>
2004-10-06 20:00:17 +00:00
if (strrpos($entry, '.')) {
$pos = strrpos($entry, '.');
2017-01-03 19:02:29 +00:00
if (substr($entry, $pos + 1) == $typeId) {
2004-10-06 20:00:17 +00:00
$name = substr($entry, 0, $pos);
$ret[] = $name;
2004-02-12 15:58:04 +00:00
}
2003-09-20 17:02:21 +00:00
}
2004-10-06 20:00:17 +00:00
$entry = $dir->read();
2003-09-20 17:02:21 +00:00
}
}
2004-10-06 20:00:17 +00:00
return $ret;
}
/**
* Returns if the given profile exists.
*
* @param string name profile name
* @param string $typeId type id
* @return bool exists
*/
function profileExists($name, $typeId) {
if (!isValidProfileName($name) || !preg_match("/^[a-z0-9_]+$/i", $typeId) || ($typeId == null)) {
return false;
}
$file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $_SESSION['config']->getName() . '/' . $name . "." . $typeId;
return is_file($file);
}
2012-10-28 14:37:54 +00:00
2004-05-31 17:58:27 +00:00
/**
2020-05-02 16:51:33 +00:00
* Loads an profile of the given account type
*
* @param string $profile name of the profile (without .<scope> extension)
* @param string $typeId account type
* @param string $serverProfileName server profile name
* @return array hash array (attribute => value)
*/
function loadAccountProfile($profile, $typeId, $serverProfileName) {
2020-05-03 08:32:35 +00:00
if (!isValidProfileName($profile) || !preg_match("/^[a-z0-9_]+$/i", $typeId)) {
logNewMessage(LOG_NOTICE, "Invalid account profile name: $serverProfileName:$profile:$typeId");
return array();
2017-01-03 19:02:29 +00:00
}
2020-05-02 16:51:33 +00:00
$file = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $serverProfileName . '/' . $profile . "." . $typeId;
2020-05-16 09:10:37 +00:00
try {
return readAccountProfileFile($file);
} catch (LAMException $e) {
StatusMessage('ERROR', $e->getTitle(), $e->getMessage());
}
return array();
2020-05-16 09:10:37 +00:00
}
/**
* Reads an account profile from the given file name.
*
* @param string $fileName file name
* @return array hash array (attribute => value)
* @throws LAMException error reading file
*/
function readAccountProfileFile($fileName) {
$settings = array();
if (is_file($fileName)) {
$file = @fopen($fileName, "r");
2003-06-24 15:50:38 +00:00
if ($file) {
while (!feof($file)) {
$line = fgets($file, 1024);
if (($line === false) || ($line == '') || ($line == "\n") || ($line[0] == "#")) {
2018-12-29 14:49:42 +00:00
continue; // ignore comments
}
2003-06-24 15:50:38 +00:00
// search keywords
2009-08-14 20:18:08 +00:00
$parts = explode(": ", $line);
2018-12-29 14:49:42 +00:00
if (sizeof($parts) == 2) {
$option = $parts[0];
$value = $parts[1];
2004-02-12 15:58:04 +00:00
// remove line ends
$value = chop($value);
$settings[$option] = explode("+::+", $value);
2004-02-12 15:58:04 +00:00
}
2003-06-24 15:50:38 +00:00
}
fclose($file);
2020-05-16 09:10:37 +00:00
return $settings;
2003-06-24 15:50:38 +00:00
}
else {
2020-05-16 09:10:37 +00:00
throw new LAMException(_("Unable to load profile!"), $fileName);
2003-04-30 16:50:48 +00:00
}
}
else {
2020-05-16 09:10:37 +00:00
throw new LAMException(_("Unable to load profile!"), $fileName);
2003-04-30 16:50:48 +00:00
}
}
2004-05-31 17:58:27 +00:00
/**
2004-10-06 20:00:17 +00:00
* Saves an hash array (attribute => value) to an account profile
2004-05-31 17:58:27 +00:00
*
* file is created, if needed
*
* @param array $attributes hash array (attribute => value)
2012-07-15 12:05:47 +00:00
* @param string $profile name of the account profile (without .<scope> extension)
2017-01-03 19:02:29 +00:00
* @param string $typeId account type
2020-05-03 08:32:35 +00:00
* @param \LAMConfig $serverProfile server profile
2004-05-31 17:58:27 +00:00
* @return boolean true, if saving succeeded
*/
2020-05-03 08:32:35 +00:00
function saveAccountProfile($attributes, $profile, $typeId, $serverProfile) {
2017-01-03 19:02:29 +00:00
// check profile name and type id
$typeManager = new TypeManager($serverProfile);
2017-01-03 19:02:29 +00:00
$type = $typeManager->getConfiguredType($typeId);
if (!isValidProfileName($profile) || !preg_match("/^[a-z0-9_]+$/i", $typeId) || ($type == null)) {
logNewMessage(LOG_NOTICE, 'Invalid account profile name: ' . $profile . ':' . $typeId);
2017-01-03 19:02:29 +00:00
return false;
}
if (!is_array($attributes)) {
2020-05-03 08:32:35 +00:00
logNewMessage(LOG_NOTICE, 'Invalid account profile data');
return false;
2003-06-24 15:50:38 +00:00
}
2020-05-03 08:32:35 +00:00
$path = substr(__FILE__, 0, strlen(__FILE__) - 17) . "/config/profiles/" . $serverProfile->getName() . '/' . $profile . "." . $typeId;
return writeProfileDataToFile($path, $attributes);
}
/**
* Writes the profile data to the given file.
*
* @param string $fileName file name
* @param array $data profile data
* @return bool writing was ok
*/
function writeProfileDataToFile($fileName, $data) {
$file = @fopen($fileName, "w");
2003-08-03 11:05:40 +00:00
if ($file) {
// write attributes
$keys = array_keys($data);
for ($i = 0; $i < sizeof($keys); $i++) {
if (isset($data[$keys[$i]])) {
$line = $keys[$i] . ": " . implode("+::+", $data[$keys[$i]]) . "\n";
2004-04-17 14:09:05 +00:00
}
else {
2005-05-08 16:37:36 +00:00
$line = $keys[$i] . ": \n";
2004-04-17 14:09:05 +00:00
}
fputs($file, $line);
2003-09-20 08:04:38 +00:00
}
2003-08-03 11:05:40 +00:00
// close file
fclose($file);
}
2003-08-03 11:05:40 +00:00
else {
logNewMessage(LOG_NOTICE, 'Unable to open account profile file: ' . $fileName);
2003-08-03 11:05:40 +00:00
return false;
}
2003-08-03 11:05:40 +00:00
return true;
}
2004-05-31 17:58:27 +00:00
/**
2004-10-06 20:00:17 +00:00
* Deletes an account profile
2004-05-31 17:58:27 +00:00
*
2004-10-06 20:00:17 +00:00
* @param string $file name of profile (Without .<scope> extension)
2017-01-03 19:02:29 +00:00
* @param string $typeId account type
* @return boolean true if profile was deleted
2004-05-31 17:58:27 +00:00
*/
2017-01-03 19:02:29 +00:00
function delAccountProfile($file, $typeId) {
if (!isLoggedIn()) {
return false;
}
$typeManager = new TypeManager();
2017-01-03 19:02:29 +00:00
$type = $typeManager->getConfiguredType($typeId);
if (!isValidProfileName($file) || !preg_match("/^[a-z0-9_]+$/i", $typeId) || ($type == null)) {
return false;
}
$prof = substr(__FILE__, 0, strlen(__FILE__) - 16) . "config/profiles/". $_SESSION['config']->getName() . '/' . $file . "." . $typeId;
2003-05-25 10:51:10 +00:00
if (is_file($prof)) {
return @unlink($prof);
}
2018-12-29 14:49:42 +00:00
return false;
2003-05-25 10:51:10 +00:00
}
2012-10-28 14:37:54 +00:00
/**
2017-01-03 19:02:29 +00:00
* Returns if the given profile name is valid.
2012-10-28 14:37:54 +00:00
*
2017-01-03 19:02:29 +00:00
* @param string $name profile name
*/
function isValidProfileName($name) {
return preg_match("/^[0-9a-z _-]+$/i", $name);
}
/**
* Copies an account profile from the given source to target.
2012-10-28 14:37:54 +00:00
*
2017-01-03 19:02:29 +00:00
* @param \LAM\TYPES\ConfiguredType $sourceType source type
* @param string $sourceProfileName profile name
* @param \LAM\TYPES\ConfiguredType $targetType target type
* @throws LAMException error during copy
2012-10-28 14:37:54 +00:00
*/
2017-01-03 19:02:29 +00:00
function copyAccountProfile($sourceType, $sourceProfileName, $targetType) {
if (!isValidProfileName($sourceProfileName)) {
throw new LAMException(_('Failed to copy'));
}
$sourceConfig = $sourceType->getTypeManager()->getConfig()->getName();
$sourceTypeId = $sourceType->getId();
$targetConfig = $targetType->getTypeManager()->getConfig()->getName();
$targetTypeId = $targetType->getId();
$profilePath = dirname(__FILE__) . '/../config/profiles/';
$src = $profilePath . $sourceConfig . '/' . $sourceProfileName . '.' . $sourceTypeId;
$dst = $profilePath . $targetConfig . '/' . $sourceProfileName . '.' . $targetTypeId;
if (!@copy($src, $dst)) {
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceProfileName);
2012-10-28 14:37:54 +00:00
}
2017-01-03 19:02:29 +00:00
}
2012-10-28 14:37:54 +00:00
2017-01-03 19:02:29 +00:00
/**
* Copies an account profile from the given source to global templates.
*
* @param \LAM\TYPES\ConfiguredType $sourceType source type
* @param string $sourceProfileName profile name
* @throws LAMException error during copy
2017-01-03 19:02:29 +00:00
*/
function copyAccountProfileToTemplates($sourceType, $sourceProfileName) {
if (!isValidProfileName($sourceProfileName)) {
throw new LAMException(_('Failed to copy'));
}
$sourceConfig = $sourceType->getTypeManager()->getConfig()->getName();
$sourceTypeId = $sourceType->getId();
$profilePath = dirname(__FILE__) . '/../config/profiles/';
$templatePath = dirname(__FILE__) . '/../config/templates/profiles/';
$src = $profilePath . $sourceConfig . '/' . $sourceProfileName . '.' . $sourceTypeId;
$dst = $templatePath . $sourceProfileName . '.' . $sourceType->getScope();
if (!@copy($src, $dst)) {
throw new LAMException(_('Failed to copy'), $sourceConfig . ': ' . $sourceProfileName);
}
2012-10-28 14:37:54 +00:00
}
2017-01-06 12:56:17 +00:00
/**
* Installs template profiles to the current server profile.
*/
function installProfileTemplates() {
2020-05-16 09:10:37 +00:00
$allTemplates = getProfileTemplateNames();
2017-01-22 17:00:47 +00:00
$basePath = dirname(__FILE__) . '/../config/profiles/' . $_SESSION['config']->getName();
if (!file_exists($basePath)) {
2017-05-15 18:11:00 +00:00
mkdir($basePath, 0700, true);
2017-01-22 17:00:47 +00:00
}
$typeManager = new TypeManager();
2017-01-06 12:56:17 +00:00
foreach ($typeManager->getConfiguredTypes() as $type) {
if (empty($allTemplates[$type->getScope()])) {
continue;
}
foreach ($allTemplates[$type->getScope()] as $templateName) {
2017-01-22 17:00:47 +00:00
$path = $basePath . '/' . $templateName . '.' . $type->getId();
2017-01-06 12:56:17 +00:00
if (!is_file($path)) {
2020-05-16 09:10:37 +00:00
$template = getProfileTemplateFileName($type->getScope(), $templateName);
2017-01-11 20:27:28 +00:00
logNewMessage(LOG_DEBUG, 'Copy template ' . $template . ' to ' . $path);
2017-01-06 12:56:17 +00:00
@copy($template, $path);
}
}
}
}
2020-05-16 09:10:37 +00:00
/**
* Returns a list of all global profile templates.
*
* @return array names (array('user' => array('default', 'extra')))
*/
function getProfileTemplateNames() {
$templatePath = __DIR__ . '/../config/templates/profiles';
$templateDir = @dir($templatePath);
$allTemplates = array();
if ($templateDir) {
$entry = $templateDir->read();
while ($entry){
$parts = explode('.', $entry);
if ((strlen($entry) > 3) && (sizeof($parts) == 2)) {
$name = $parts[0];
$scope = $parts[1];
$allTemplates[$scope][] = $name;
}
$entry = $templateDir->read();
}
}
return $allTemplates;
}
/**
* Returns the file name of a global template.
*
* @param string $scope e.g. user
* @param string $name profile name
* @return string file name
*/
function getProfileTemplateFileName($scope, $name) {
return __DIR__ . '/../config/templates/profiles' . '/' . $name . '.' . $scope;
}
/**
* Loads a template profile of the given account scope.
*
* @param string $profile name of the profile (without .<scope> extension)
* @param string $scope account type
* @return array hash array (attribute => value)
* @throws LAMException error reading profile template
*/
function loadTemplateAccountProfile($profile, $scope) {
if (!isValidProfileName($profile) || !preg_match("/^[a-z0-9_]+$/i", $scope)) {
logNewMessage(LOG_NOTICE, "Invalid account profile name: $profile:$scope");
return array();
}
$fileName = getProfileTemplateFileName($scope, $profile);
return readAccountProfileFile($fileName);
}
/**
* Installs a single template from the given data.
*
* @param string $scope account type (e.g. user)
* @param string $name template name
* @param array $data profile data
* @throws LAMException error saving file
*/
function installTemplateAccountProfile($scope, $name, $data) {
if (!isValidProfileName($name) || !preg_match("/^[a-z0-9_]+$/i", $scope)) {
logNewMessage(LOG_NOTICE, "Invalid account profile name: $name:$scope");
return;
}
$fileName = getProfileTemplateFileName($scope, $name);
$success = writeProfileDataToFile($fileName, $data);
if (!$success) {
throw new LAMException('Unable to write account profile template: ' . $fileName);
}
}