~ubuntu-branches/ubuntu/wily/ldap-account-manager/wily

« back to all changes in this revision

Viewing changes to lib/upgrade.inc

  • Committer: Package Import Robot
  • Author(s): Roland Gruber
  • Date: 2012-12-17 19:41:29 UTC
  • mfrom: (1.2.18)
  • Revision ID: package-import@ubuntu.com-20121217194129-x6g7jgqxelr9o0mv
Tags: 4.0-1
new upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
$Id:
 
4
 
 
5
  This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/)
 
6
  Copyright (C) 2012  Christian Kropp
 
7
                2012  Roland Gruber
 
8
 
 
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.
 
13
 
 
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.
 
18
 
 
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
 
22
 
 
23
*/
 
24
 
 
25
 
 
26
/**
 
27
* This file includes functions to upgrade the pdf/profiles configuration files.
 
28
*
 
29
* @package main
 
30
* @author Christian Kropp
 
31
* @author Roland Gruber
 
32
*/
 
33
 
 
34
 
 
35
/**
 
36
 * check the write/read permission for the upgrade
 
37
 *
 
38
 * @return array - permission messages
 
39
 */
 
40
function testPermissions() {
 
41
        $result = array();
 
42
        if (!is_writable('../config')) {
 
43
                $result[] = htmlspecialchars(realpath('../config'));
 
44
        }
 
45
        $result = array_merge($result, testPermissionRecursive('../config/profiles/'));
 
46
    $result = array_merge($result, testPermissionRecursive('../config/pdf/'));
 
47
    return $result;
 
48
}
 
49
 
 
50
/**
 
51
 * Recursively checks the permissions in a directory.
 
52
 * 
 
53
 * @param String $dir directory
 
54
 * @return array list of files/directories with wrong permission
 
55
 */
 
56
function testPermissionRecursive($dir) {
 
57
        $result = array();
 
58
        if (!is_writable($dir)) {
 
59
                $result[] = htmlspecialchars(realpath($dir));
 
60
        }
 
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));
 
68
                                }
 
69
                                elseif (!is_writable($dir . '/' . $file)) {
 
70
                                        $result[] = htmlspecialchars(realpath($dir . '/' . $file));
 
71
                                }
 
72
                        }
 
73
                        $file = @readdir($dirHandle);
 
74
                }
 
75
        }
 
76
        @closedir($dirHandle);
 
77
        return $result; 
 
78
}
 
79
 
 
80
/**
 
81
 * Checks if the given directory contains files.
 
82
 * This is used to check if config files need to be migrated.
 
83
 * 
 
84
 * @param String $dir directory path
 
85
 */
 
86
function containsFiles($dir) {
 
87
        $return = false;
 
88
        $dirHandle = @opendir($dir);
 
89
        if ($dirHandle != null) {
 
90
                $file = @readdir($dirHandle);
 
91
                while ($file !== false) {
 
92
                        if (is_file($dir . '/' . $file)) {
 
93
                                $return = true;
 
94
                                break;
 
95
                        }
 
96
                        $file = @readdir($dirHandle);
 
97
                }
 
98
        }
 
99
        @closedir($dirHandle);
 
100
        return $return; 
 
101
}
 
102
 
 
103
 
 
104
/**
 
105
 * Saves an hash array (attribute => value) to an account profile
 
106
 *
 
107
 * @param array $profiles server profiles
 
108
 */
 
109
function upgradeConfigToServerProfileFolders($profiles) {
 
110
        if (!is_writable('../config')) {
 
111
                StatusMessage('ERROR', 'Upgrade failed.', 'The directory \'/config\' has missing write permissions.');
 
112
                return;
 
113
        }
 
114
 
 
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.');
 
120
        }
 
121
 
 
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);
 
127
                }
 
128
 
 
129
                // upgrade profiles configs
 
130
                $dir = '../config/profiles/' . $profile;
 
131
                if (!file_exists($dir)) {
 
132
                        recursiveCopy('../config/profiles/', $dir, $profiles);
 
133
                }
 
134
        }
 
135
 
 
136
        // delete old files
 
137
        recursiveDelete('../config/pdf', $profiles);
 
138
        recursiveDelete('../config/profiles', $profiles);
 
139
}
 
140
 
 
141
/**
 
142
 * Saves an hash array (attribute => value) to an account profile
 
143
 *
 
144
 * @param array $profiles server profiles
 
145
 */
 
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);
 
151
        }
 
152
}
 
153
 
 
154
/**
 
155
 * Copy a file or recursively copy a directory
 
156
 *
 
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
 
162
 */
 
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.');
 
169
                }
 
170
        }
 
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);
 
175
                        }
 
176
                        elseif ((isset($fileFilter) && (strpos($file, $fileFilter) === 0 || $file == '.htaccess'))
 
177
                                                || (!isset($fileFilter))) {
 
178
                                if (!is_file($src . '/' . $file)) {
 
179
                                        continue;
 
180
                                }
 
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.');
 
185
                                        }
 
186
                                }
 
187
                        }
 
188
                }
 
189
        }
 
190
        closedir($dir);
 
191
}
 
192
 
 
193
 
 
194
/**
 
195
 * Delete a file or recursively delete a directory
 
196
 *
 
197
 * @param string $src - path to file or directory
 
198
 * @param array $profiles - server profiles (used to avoid copying of newly created folders)
 
199
 */
 
200
function recursiveDelete($src, $profiles) {
 
201
        if (is_file($src)) {
 
202
                $tmpState = @unlink($src);
 
203
                if ($tmpState === false) {
 
204
                        StatusMessage('ERROR', 'Upgrade failed.', 'The file ' . $src . ' could not be deleted.');
 
205
                }
 
206
                return;
 
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);
 
212
                        }
 
213
                }
 
214
                @closedir($dir);
 
215
 
 
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.');
 
220
                        }
 
221
                }
 
222
                return;
 
223
        } else {
 
224
                StatusMessage('ERROR', 'Upgrade failed.', 'The directory ' . $src . ' has missing write permissions.');
 
225
                return;
 
226
        }
 
227
}
 
228
 
 
229
?>