5
This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
6
Copyright (C) 2012 Christian Kropp
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
14
This program is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
* This file includes functions to upgrade the pdf/profiles configuration files.
30
* @author Christian Kropp
31
* @author Roland Gruber
36
* check the write/read permission for the upgrade
38
* @return array - permission messages
40
function testPermissions() {
42
if (!is_writable('../config')) {
43
$result[] = htmlspecialchars(realpath('../config'));
45
$result = array_merge($result, testPermissionRecursive('../config/profiles/'));
46
$result = array_merge($result, testPermissionRecursive('../config/pdf/'));
51
* Recursively checks the permissions in a directory.
53
* @param String $dir directory
54
* @return array list of files/directories with wrong permission
56
function testPermissionRecursive($dir) {
58
if (!is_writable($dir)) {
59
$result[] = htmlspecialchars(realpath($dir));
61
$dirHandle = @opendir($dir);
62
if ($dirHandle != null) {
63
$file = @readdir($dirHandle);
64
while ($file !== false) {
65
if (($file != '.') && ($file != '..')) {
66
if (is_dir($dir . '/' . $file)) {
67
$result = array_merge($result, testPermissionRecursive($dir . '/' . $file));
69
elseif (!is_writable($dir . '/' . $file)) {
70
$result[] = htmlspecialchars(realpath($dir . '/' . $file));
73
$file = @readdir($dirHandle);
76
@closedir($dirHandle);
81
* Checks if the given directory contains files.
82
* This is used to check if config files need to be migrated.
84
* @param String $dir directory path
86
function containsFiles($dir) {
88
$dirHandle = @opendir($dir);
89
if ($dirHandle != null) {
90
$file = @readdir($dirHandle);
91
while ($file !== false) {
92
if (is_file($dir . '/' . $file)) {
96
$file = @readdir($dirHandle);
99
@closedir($dirHandle);
105
* Saves an hash array (attribute => value) to an account profile
107
* @param array $profiles server profiles
109
function upgradeConfigToServerProfileFolders($profiles) {
110
if (!is_writable('../config')) {
111
StatusMessage('ERROR', 'Upgrade failed.', 'The directory \'/config\' has missing write permissions.');
115
// copy default configs
116
if (!file_exists('../config/templates')) {
117
@mkdir('../config/templates');
118
recursiveCopy('../config/pdf/', '../config/templates/pdf/', $profiles, 'default.');
119
recursiveCopy('../config/profiles/', '../config/templates/profiles/', $profiles, 'default.');
122
foreach ($profiles as $profile) {
123
// upgrade PDF configs
124
$dir = '../config/pdf/' . $profile;
125
if (!file_exists($dir)) {
126
recursiveCopy('../config/pdf/', $dir, $profiles);
129
// upgrade profiles configs
130
$dir = '../config/profiles/' . $profile;
131
if (!file_exists($dir)) {
132
recursiveCopy('../config/profiles/', $dir, $profiles);
137
recursiveDelete('../config/pdf', $profiles);
138
recursiveDelete('../config/profiles', $profiles);
142
* Saves an hash array (attribute => value) to an account profile
144
* @param array $profiles server profiles
146
function copyConfigTemplates($profiles) {
147
foreach ($profiles as $profile) {
148
// copy templates but do not override existing files
149
recursiveCopy('../config/templates/pdf/', '../config/pdf/' . $profile, $profiles, null, false);
150
recursiveCopy('../config/templates/profiles/', '../config/profiles/' . $profile, $profiles, null, false);
155
* Copy a file or recursively copy a directory
157
* @param string $src - source path to file or directory
158
* @param string $dst - destination path to file or directory
159
* @param array $profiles - server profiles (used to avoid copying of newly created folders)
160
* @param string $fileFilter copy only files that start with the given filter
161
* @param boolean $overwrite overwrite existing files
163
function recursiveCopy($src, $dst, $profiles, $fileFilter = null, $overwrite = true) {
164
$dir = @opendir($src);
165
if (!file_exists($dst)) {
166
$tmpState = @mkdir($dst);
167
if ($tmpState === false) {
168
StatusMessage('ERROR', 'Upgrade failed.', 'The directory \'' . $dst . '\' could not be created.');
171
while (false !== ($file = readdir($dir))) {
172
if ($file != '.' && $file != '..' && !in_array($file, $profiles)) {
173
if (is_dir($src . '/' . $file) && ($file == 'logos')) {
174
recursiveCopy($src . '/' . $file, $dst . '/' . $file, $profiles, $fileFilter, $overwrite);
176
elseif ((isset($fileFilter) && (strpos($file, $fileFilter) === 0 || $file == '.htaccess'))
177
|| (!isset($fileFilter))) {
178
if (!is_file($src . '/' . $file)) {
181
if ($overwrite || !file_exists($dst . '/' . $file)) {
182
$tmpState = @copy($src . '/' . $file, $dst . '/' . $file);
183
if ($tmpState === false) {
184
StatusMessage('ERROR', 'Upgrade failed.', 'The file ' . $file . ' could not be copied.');
195
* Delete a file or recursively delete a directory
197
* @param string $src - path to file or directory
198
* @param array $profiles - server profiles (used to avoid copying of newly created folders)
200
function recursiveDelete($src, $profiles) {
202
$tmpState = @unlink($src);
203
if ($tmpState === false) {
204
StatusMessage('ERROR', 'Upgrade failed.', 'The file ' . $src . ' could not be deleted.');
207
} else if (is_dir($src) && is_writable($src)) {
208
$dir = @opendir($src);
209
while (false !== ($path = readdir($dir))) {
210
if ($path != '.' && $path != '..' && !in_array($path, $profiles)) {
211
recursiveDelete($src . '/' . $path, $profiles);
216
if ($src != '../config/pdf' && $src != '../config/profiles') {
217
$tmpState = @rmdir($src);
218
if ($tmpState === false) {
219
StatusMessage('ERROR', 'Upgrade failed.', 'The directory ' . $src . ' could not be deleted.');
224
StatusMessage('ERROR', 'Upgrade failed.', 'The directory ' . $src . ' has missing write permissions.');