TYPO3 CMS  TYPO3_6-2
FilePermissionUpdate.php
Go to the documentation of this file.
1 <?php
3 
21 
26 
30  protected $title = 'Rewrite binary file permissions into detailed list';
31 
35  public function __construct() {
36  $this->installToolSqlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
37  }
38 
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 
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 
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 
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 
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 
220  protected function getWhereClause() {
221  return 'fileoper_perms>0 AND file_permissions IS NULL';
222  }
223 
224 }
static getUrl($url, $includeHeader=0, $requestHeaders=FALSE, &$report=NULL)
performUpdate(array &$dbQueries, &$customMessages)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]