[TASK] Re-work/simplify copyright header in PHP files - Part 1
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / FilePermissionUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Upgrade wizard which goes through all users and groups file permissions and stores them as list in a new field.
19 */
20 class FilePermissionUpdate extends AbstractUpdate {
21
22 /**
23 * @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService
24 */
25 protected $installToolSqlParser;
26
27 /**
28 * @var string
29 */
30 protected $title = 'Rewrite binary file permissions into detailed list';
31
32 /**
33 * Constructor function.
34 */
35 public function __construct() {
36 $this->installToolSqlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
37 }
38
39 /**
40 * Checks if an update is needed
41 *
42 * @param string &$description The description for the update
43 * @return boolean TRUE if an update is needed, FALSE otherwise
44 */
45 public function checkForUpdate(&$description) {
46 $description = 'There are backend users and backend groups with specified file permissions.' .
47 ' This update migrates old combined (binary) file permissions to new separate ones.';
48 $updateNeeded = FALSE;
49 $updateStatements = $this->getUpdateStatements();
50 if (!empty($updateStatements['add'])) {
51 // Field might not be there, so we need an update run to add the field
52 return TRUE;
53 }
54 $beUsersFieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields('be_users');
55 if (isset($beUsersFieldInformation['fileoper_perms'])) {
56 // Fetch user records where the old permission field is not empty but the new one is
57 $notMigratedRowsCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
58 'uid',
59 'be_users',
60 $this->getWhereClause()
61 );
62 if ($notMigratedRowsCount > 0) {
63 $updateNeeded = TRUE;
64 }
65 } else {
66 $beGroupsFieldInformation = $GLOBALS['TYPO3_DB']->admin_get_fields('be_groups');
67 if (isset($beGroupsFieldInformation['fileoper_perms'])) {
68 // Fetch group records where the old permission field is not empty but the new one is
69 $notMigratedRowsCount = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
70 'uid',
71 'be_groups',
72 $this->getWhereClause()
73 );
74 if ($notMigratedRowsCount > 0) {
75 $updateNeeded = TRUE;
76 }
77 }
78 }
79 return $updateNeeded;
80 }
81
82 /**
83 * Performs the database update.
84 *
85 * @param array &$dbQueries Queries done in this update
86 * @param mixed &$customMessages Custom messages
87 * @return boolean TRUE on success, FALSE on error
88 */
89 public function performUpdate(array &$dbQueries, &$customMessages) {
90 // First perform all add update statements to database
91 $updateStatements = $this->getUpdateStatements();
92 foreach ((array) $updateStatements['add'] as $query) {
93 $GLOBALS['TYPO3_DB']->admin_query($query);
94 $dbQueries[] = $query;
95 if ($GLOBALS['TYPO3_DB']->sql_error()) {
96 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
97 return FALSE;
98 }
99 }
100
101 // Iterate over users and groups table to perform permission updates
102 $tablesToProcess = array('be_groups', 'be_users');
103 foreach ($tablesToProcess as $table) {
104 $records = $this->getRecordsFromTable($table);
105 foreach ($records as $singleRecord) {
106 $filePermission = $this->getFilePermissions($singleRecord['fileoper_perms']);
107 $updateArray = array(
108 'file_permissions' => $filePermission
109 );
110 $GLOBALS['TYPO3_DB']->exec_UPDATEquery($table, 'uid=' . (int)$singleRecord['uid'], $updateArray);
111 // Get last executed query
112 $dbQueries[] = str_replace(chr(10), ' ', $GLOBALS['TYPO3_DB']->debug_lastBuiltQuery);
113 // Check for errors
114 if ($GLOBALS['TYPO3_DB']->sql_error()) {
115 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($GLOBALS['TYPO3_DB']->sql_error());
116 return FALSE;
117 }
118 }
119 }
120
121 return TRUE;
122 }
123
124 /**
125 * Gets all create, add and change queries from core/ext_tables.sql
126 *
127 * @return array
128 */
129 protected function getUpdateStatements() {
130 $updateStatements = array();
131
132 // Get all necessary statements for ext_tables.sql file
133 $rawDefinitions = \TYPO3\CMS\Core\Utility\GeneralUtility::getUrl(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('core') . '/ext_tables.sql');
134 $fieldDefinitionsFromFile = $this->installToolSqlParser->getFieldDefinitions_fileContent($rawDefinitions);
135 if (count($fieldDefinitionsFromFile)) {
136 $fieldDefinitionsFromCurrentDatabase = $this->installToolSqlParser->getFieldDefinitions_database();
137 $diff = $this->installToolSqlParser->getDatabaseExtra($fieldDefinitionsFromFile, $fieldDefinitionsFromCurrentDatabase);
138 $updateStatements = $this->installToolSqlParser->getUpdateSuggestions($diff);
139 }
140
141 return $updateStatements;
142 }
143
144 /**
145 * Processes the actual transformation from old binary file permissions to new separate list
146 *
147 * @param integer $oldFileOperationPermissions
148 * @return string
149 */
150 protected function getFilePermissions($oldFileOperationPermissions) {
151 if ($oldFileOperationPermissions == 0) {
152 return '';
153 }
154 $defaultOptions = array(
155 // File permissions
156 'addFile' => TRUE,
157 'readFile' => TRUE,
158 'writeFile' => TRUE,
159 'copyFile' => TRUE,
160 'moveFile' => TRUE,
161 'renameFile' => TRUE,
162 'unzipFile' => TRUE,
163 'deleteFile' => TRUE,
164 // Folder permissions
165 'addFolder' => TRUE,
166 'readFolder' => TRUE,
167 'writeFolder' => TRUE,
168 'copyFolder' => TRUE,
169 'moveFolder' => TRUE,
170 'renameFolder' => TRUE,
171 'deleteFolder' => TRUE,
172 'recursivedeleteFolder' => TRUE
173 );
174 if (!($oldFileOperationPermissions & 1)) {
175 unset($defaultOptions['addFile']);
176 unset($defaultOptions['readFile']);
177 unset($defaultOptions['writeFile']);
178 unset($defaultOptions['copyFile']);
179 unset($defaultOptions['moveFile']);
180 unset($defaultOptions['renameFile']);
181 unset($defaultOptions['deleteFile']);
182 }
183 if (!($oldFileOperationPermissions & 2)) {
184 unset($defaultOptions['unzipFile']);
185 }
186 if (!($oldFileOperationPermissions & 4)) {
187 unset($defaultOptions['addFolder']);
188 unset($defaultOptions['writeFolder']);
189 unset($defaultOptions['moveFolder']);
190 unset($defaultOptions['renameFolder']);
191 unset($defaultOptions['deleteFolder']);
192 }
193 if (!($oldFileOperationPermissions & 8)) {
194 unset($defaultOptions['copyFolder']);
195 }
196 if (!($oldFileOperationPermissions & 16)) {
197 unset($defaultOptions['recursivedeleteFolder']);
198 }
199
200 return implode(',', array_keys($defaultOptions));
201 }
202
203 /**
204 * Retrieve every record which needs to be processed
205 *
206 * @param string $table
207 * @return array
208 */
209 protected function getRecordsFromTable($table) {
210 $fields = implode(',', array('uid', 'fileoper_perms'));
211 $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows($fields, $table, $this->getWhereClause());
212 return $records;
213 }
214
215 /**
216 * Returns the where clause for database requests
217 *
218 * @return string
219 */
220 protected function getWhereClause() {
221 return 'fileoper_perms>0 AND ISNULL(file_permissions)';
222 }
223
224 }