Passed
Push — master ( 37782b...410bd9 )
by Morris
15:37
created
lib/private/Encryption/DecryptAll.php 2 patches
Indentation   +256 added lines, -256 removed lines patch added patch discarded remove patch
@@ -37,261 +37,261 @@
 block discarded – undo
37 37
 
38 38
 class DecryptAll {
39 39
 
40
-	/** @var  OutputInterface */
41
-	protected $output;
42
-
43
-	/** @var  InputInterface */
44
-	protected $input;
45
-
46
-	/** @var  Manager */
47
-	protected $encryptionManager;
48
-
49
-	/** @var IUserManager */
50
-	protected $userManager;
51
-
52
-	/** @var View */
53
-	protected $rootView;
54
-
55
-	/** @var  array files which couldn't be decrypted */
56
-	protected $failed;
57
-
58
-	/**
59
-	 * @param Manager $encryptionManager
60
-	 * @param IUserManager $userManager
61
-	 * @param View $rootView
62
-	 */
63
-	public function __construct(
64
-		Manager $encryptionManager,
65
-		IUserManager $userManager,
66
-		View $rootView
67
-	) {
68
-		$this->encryptionManager = $encryptionManager;
69
-		$this->userManager = $userManager;
70
-		$this->rootView = $rootView;
71
-		$this->failed = [];
72
-	}
73
-
74
-	/**
75
-	 * start to decrypt all files
76
-	 *
77
-	 * @param InputInterface $input
78
-	 * @param OutputInterface $output
79
-	 * @param string $user which users data folder should be decrypted, default = all users
80
-	 * @return bool
81
-	 * @throws \Exception
82
-	 */
83
-	public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') {
84
-
85
-		$this->input = $input;
86
-		$this->output = $output;
87
-
88
-		if ($user !== '' && $this->userManager->userExists($user) === false) {
89
-			$this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again');
90
-			return false;
91
-		}
92
-
93
-		$this->output->writeln('prepare encryption modules...');
94
-		if ($this->prepareEncryptionModules($user) === false) {
95
-			return false;
96
-		}
97
-		$this->output->writeln(' done.');
98
-
99
-		$this->decryptAllUsersFiles($user);
100
-
101
-		if (empty($this->failed)) {
102
-			$this->output->writeln('all files could be decrypted successfully!');
103
-		} else {
104
-			$this->output->writeln('Files for following users couldn\'t be decrypted, ');
105
-			$this->output->writeln('maybe the user is not set up in a way that supports this operation: ');
106
-			foreach ($this->failed as $uid => $paths) {
107
-				$this->output->writeln('    ' . $uid);
108
-			}
109
-			$this->output->writeln('');
110
-		}
111
-
112
-		return true;
113
-	}
114
-
115
-	/**
116
-	 * prepare encryption modules to perform the decrypt all function
117
-	 *
118
-	 * @param $user
119
-	 * @return bool
120
-	 */
121
-	protected function prepareEncryptionModules($user) {
122
-		// prepare all encryption modules for decrypt all
123
-		$encryptionModules = $this->encryptionManager->getEncryptionModules();
124
-		foreach ($encryptionModules as $moduleDesc) {
125
-			/** @var IEncryptionModule $module */
126
-			$module = call_user_func($moduleDesc['callback']);
127
-			$this->output->writeln('');
128
-			$this->output->writeln('Prepare "' . $module->getDisplayName() . '"');
129
-			$this->output->writeln('');
130
-			if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) {
131
-				$this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!');
132
-				return false;
133
-			}
134
-		}
135
-
136
-		return true;
137
-	}
138
-
139
-	/**
140
-	 * iterate over all user and encrypt their files
141
-	 *
142
-	 * @param string $user which users files should be decrypted, default = all users
143
-	 */
144
-	protected function decryptAllUsersFiles($user = '') {
145
-
146
-		$this->output->writeln("\n");
147
-
148
-		$userList = [];
149
-		if ($user === '') {
150
-
151
-			$fetchUsersProgress = new ProgressBar($this->output);
152
-			$fetchUsersProgress->setFormat(" %message% \n [%bar%]");
153
-			$fetchUsersProgress->start();
154
-			$fetchUsersProgress->setMessage("Fetch list of users...");
155
-			$fetchUsersProgress->advance();
156
-
157
-			foreach ($this->userManager->getBackends() as $backend) {
158
-				$limit = 500;
159
-				$offset = 0;
160
-				do {
161
-					$users = $backend->getUsers('', $limit, $offset);
162
-					foreach ($users as $user) {
163
-						$userList[] = $user;
164
-					}
165
-					$offset += $limit;
166
-					$fetchUsersProgress->advance();
167
-				} while (count($users) >= $limit);
168
-				$fetchUsersProgress->setMessage("Fetch list of users... finished");
169
-				$fetchUsersProgress->finish();
170
-			}
171
-		} else {
172
-			$userList[] = $user;
173
-		}
174
-
175
-		$this->output->writeln("\n\n");
176
-
177
-		$progress = new ProgressBar($this->output);
178
-		$progress->setFormat(" %message% \n [%bar%]");
179
-		$progress->start();
180
-		$progress->setMessage("starting to decrypt files...");
181
-		$progress->advance();
182
-
183
-		$numberOfUsers = count($userList);
184
-		$userNo = 1;
185
-		foreach ($userList as $uid) {
186
-			$userCount = "$uid ($userNo of $numberOfUsers)";
187
-			$this->decryptUsersFiles($uid, $progress, $userCount);
188
-			$userNo++;
189
-		}
190
-
191
-		$progress->setMessage("starting to decrypt files... finished");
192
-		$progress->finish();
193
-
194
-		$this->output->writeln("\n\n");
195
-
196
-	}
197
-
198
-	/**
199
-	 * encrypt files from the given user
200
-	 *
201
-	 * @param string $uid
202
-	 * @param ProgressBar $progress
203
-	 * @param string $userCount
204
-	 */
205
-	protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
206
-
207
-		$this->setupUserFS($uid);
208
-		$directories = array();
209
-		$directories[] = '/' . $uid . '/files';
210
-
211
-		while ($root = array_pop($directories)) {
212
-			$content = $this->rootView->getDirectoryContent($root);
213
-			foreach ($content as $file) {
214
-				// only decrypt files owned by the user
215
-				if($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
216
-					continue;
217
-				}
218
-				$path = $root . '/' . $file['name'];
219
-				if ($this->rootView->is_dir($path)) {
220
-					$directories[] = $path;
221
-					continue;
222
-				} else {
223
-					try {
224
-						$progress->setMessage("decrypt files for user $userCount: $path");
225
-						$progress->advance();
226
-						if ($file->isEncrypted() === false) {
227
-							$progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
228
-							$progress->advance();
229
-						} else {
230
-							if ($this->decryptFile($path) === false) {
231
-								$progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
232
-								$progress->advance();
233
-							}
234
-						}
235
-					} catch (\Exception $e) {
236
-						if (isset($this->failed[$uid])) {
237
-							$this->failed[$uid][] = $path;
238
-						} else {
239
-							$this->failed[$uid] = [$path];
240
-						}
241
-					}
242
-				}
243
-			}
244
-		}
245
-	}
246
-
247
-	/**
248
-	 * encrypt file
249
-	 *
250
-	 * @param string $path
251
-	 * @return bool
252
-	 */
253
-	protected function decryptFile($path) {
254
-
255
-		// skip already decrypted files
256
-		$fileInfo = $this->rootView->getFileInfo($path);
257
-		if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
258
-			return true;
259
-		}
260
-
261
-		$source = $path;
262
-		$target = $path . '.decrypted.' . $this->getTimestamp();
263
-
264
-		try {
265
-			$this->rootView->copy($source, $target);
266
-			$this->rootView->rename($target, $source);
267
-		} catch (DecryptionFailedException $e) {
268
-			if ($this->rootView->file_exists($target)) {
269
-				$this->rootView->unlink($target);
270
-			}
271
-			return false;
272
-		}
273
-
274
-		return true;
275
-	}
276
-
277
-	/**
278
-	 * get current timestamp
279
-	 *
280
-	 * @return int
281
-	 */
282
-	protected function getTimestamp() {
283
-		return time();
284
-	}
285
-
286
-
287
-	/**
288
-	 * setup user file system
289
-	 *
290
-	 * @param string $uid
291
-	 */
292
-	protected function setupUserFS($uid) {
293
-		\OC_Util::tearDownFS();
294
-		\OC_Util::setupFS($uid);
295
-	}
40
+    /** @var  OutputInterface */
41
+    protected $output;
42
+
43
+    /** @var  InputInterface */
44
+    protected $input;
45
+
46
+    /** @var  Manager */
47
+    protected $encryptionManager;
48
+
49
+    /** @var IUserManager */
50
+    protected $userManager;
51
+
52
+    /** @var View */
53
+    protected $rootView;
54
+
55
+    /** @var  array files which couldn't be decrypted */
56
+    protected $failed;
57
+
58
+    /**
59
+     * @param Manager $encryptionManager
60
+     * @param IUserManager $userManager
61
+     * @param View $rootView
62
+     */
63
+    public function __construct(
64
+        Manager $encryptionManager,
65
+        IUserManager $userManager,
66
+        View $rootView
67
+    ) {
68
+        $this->encryptionManager = $encryptionManager;
69
+        $this->userManager = $userManager;
70
+        $this->rootView = $rootView;
71
+        $this->failed = [];
72
+    }
73
+
74
+    /**
75
+     * start to decrypt all files
76
+     *
77
+     * @param InputInterface $input
78
+     * @param OutputInterface $output
79
+     * @param string $user which users data folder should be decrypted, default = all users
80
+     * @return bool
81
+     * @throws \Exception
82
+     */
83
+    public function decryptAll(InputInterface $input, OutputInterface $output, $user = '') {
84
+
85
+        $this->input = $input;
86
+        $this->output = $output;
87
+
88
+        if ($user !== '' && $this->userManager->userExists($user) === false) {
89
+            $this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again');
90
+            return false;
91
+        }
92
+
93
+        $this->output->writeln('prepare encryption modules...');
94
+        if ($this->prepareEncryptionModules($user) === false) {
95
+            return false;
96
+        }
97
+        $this->output->writeln(' done.');
98
+
99
+        $this->decryptAllUsersFiles($user);
100
+
101
+        if (empty($this->failed)) {
102
+            $this->output->writeln('all files could be decrypted successfully!');
103
+        } else {
104
+            $this->output->writeln('Files for following users couldn\'t be decrypted, ');
105
+            $this->output->writeln('maybe the user is not set up in a way that supports this operation: ');
106
+            foreach ($this->failed as $uid => $paths) {
107
+                $this->output->writeln('    ' . $uid);
108
+            }
109
+            $this->output->writeln('');
110
+        }
111
+
112
+        return true;
113
+    }
114
+
115
+    /**
116
+     * prepare encryption modules to perform the decrypt all function
117
+     *
118
+     * @param $user
119
+     * @return bool
120
+     */
121
+    protected function prepareEncryptionModules($user) {
122
+        // prepare all encryption modules for decrypt all
123
+        $encryptionModules = $this->encryptionManager->getEncryptionModules();
124
+        foreach ($encryptionModules as $moduleDesc) {
125
+            /** @var IEncryptionModule $module */
126
+            $module = call_user_func($moduleDesc['callback']);
127
+            $this->output->writeln('');
128
+            $this->output->writeln('Prepare "' . $module->getDisplayName() . '"');
129
+            $this->output->writeln('');
130
+            if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) {
131
+                $this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!');
132
+                return false;
133
+            }
134
+        }
135
+
136
+        return true;
137
+    }
138
+
139
+    /**
140
+     * iterate over all user and encrypt their files
141
+     *
142
+     * @param string $user which users files should be decrypted, default = all users
143
+     */
144
+    protected function decryptAllUsersFiles($user = '') {
145
+
146
+        $this->output->writeln("\n");
147
+
148
+        $userList = [];
149
+        if ($user === '') {
150
+
151
+            $fetchUsersProgress = new ProgressBar($this->output);
152
+            $fetchUsersProgress->setFormat(" %message% \n [%bar%]");
153
+            $fetchUsersProgress->start();
154
+            $fetchUsersProgress->setMessage("Fetch list of users...");
155
+            $fetchUsersProgress->advance();
156
+
157
+            foreach ($this->userManager->getBackends() as $backend) {
158
+                $limit = 500;
159
+                $offset = 0;
160
+                do {
161
+                    $users = $backend->getUsers('', $limit, $offset);
162
+                    foreach ($users as $user) {
163
+                        $userList[] = $user;
164
+                    }
165
+                    $offset += $limit;
166
+                    $fetchUsersProgress->advance();
167
+                } while (count($users) >= $limit);
168
+                $fetchUsersProgress->setMessage("Fetch list of users... finished");
169
+                $fetchUsersProgress->finish();
170
+            }
171
+        } else {
172
+            $userList[] = $user;
173
+        }
174
+
175
+        $this->output->writeln("\n\n");
176
+
177
+        $progress = new ProgressBar($this->output);
178
+        $progress->setFormat(" %message% \n [%bar%]");
179
+        $progress->start();
180
+        $progress->setMessage("starting to decrypt files...");
181
+        $progress->advance();
182
+
183
+        $numberOfUsers = count($userList);
184
+        $userNo = 1;
185
+        foreach ($userList as $uid) {
186
+            $userCount = "$uid ($userNo of $numberOfUsers)";
187
+            $this->decryptUsersFiles($uid, $progress, $userCount);
188
+            $userNo++;
189
+        }
190
+
191
+        $progress->setMessage("starting to decrypt files... finished");
192
+        $progress->finish();
193
+
194
+        $this->output->writeln("\n\n");
195
+
196
+    }
197
+
198
+    /**
199
+     * encrypt files from the given user
200
+     *
201
+     * @param string $uid
202
+     * @param ProgressBar $progress
203
+     * @param string $userCount
204
+     */
205
+    protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
206
+
207
+        $this->setupUserFS($uid);
208
+        $directories = array();
209
+        $directories[] = '/' . $uid . '/files';
210
+
211
+        while ($root = array_pop($directories)) {
212
+            $content = $this->rootView->getDirectoryContent($root);
213
+            foreach ($content as $file) {
214
+                // only decrypt files owned by the user
215
+                if($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
216
+                    continue;
217
+                }
218
+                $path = $root . '/' . $file['name'];
219
+                if ($this->rootView->is_dir($path)) {
220
+                    $directories[] = $path;
221
+                    continue;
222
+                } else {
223
+                    try {
224
+                        $progress->setMessage("decrypt files for user $userCount: $path");
225
+                        $progress->advance();
226
+                        if ($file->isEncrypted() === false) {
227
+                            $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
228
+                            $progress->advance();
229
+                        } else {
230
+                            if ($this->decryptFile($path) === false) {
231
+                                $progress->setMessage("decrypt files for user $userCount: $path (already decrypted)");
232
+                                $progress->advance();
233
+                            }
234
+                        }
235
+                    } catch (\Exception $e) {
236
+                        if (isset($this->failed[$uid])) {
237
+                            $this->failed[$uid][] = $path;
238
+                        } else {
239
+                            $this->failed[$uid] = [$path];
240
+                        }
241
+                    }
242
+                }
243
+            }
244
+        }
245
+    }
246
+
247
+    /**
248
+     * encrypt file
249
+     *
250
+     * @param string $path
251
+     * @return bool
252
+     */
253
+    protected function decryptFile($path) {
254
+
255
+        // skip already decrypted files
256
+        $fileInfo = $this->rootView->getFileInfo($path);
257
+        if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
258
+            return true;
259
+        }
260
+
261
+        $source = $path;
262
+        $target = $path . '.decrypted.' . $this->getTimestamp();
263
+
264
+        try {
265
+            $this->rootView->copy($source, $target);
266
+            $this->rootView->rename($target, $source);
267
+        } catch (DecryptionFailedException $e) {
268
+            if ($this->rootView->file_exists($target)) {
269
+                $this->rootView->unlink($target);
270
+            }
271
+            return false;
272
+        }
273
+
274
+        return true;
275
+    }
276
+
277
+    /**
278
+     * get current timestamp
279
+     *
280
+     * @return int
281
+     */
282
+    protected function getTimestamp() {
283
+        return time();
284
+    }
285
+
286
+
287
+    /**
288
+     * setup user file system
289
+     *
290
+     * @param string $uid
291
+     */
292
+    protected function setupUserFS($uid) {
293
+        \OC_Util::tearDownFS();
294
+        \OC_Util::setupFS($uid);
295
+    }
296 296
 
297 297
 }
Please login to merge, or discard this patch.
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
 		$this->output = $output;
87 87
 
88 88
 		if ($user !== '' && $this->userManager->userExists($user) === false) {
89
-			$this->output->writeln('User "' . $user . '" does not exist. Please check the username and try again');
89
+			$this->output->writeln('User "'.$user.'" does not exist. Please check the username and try again');
90 90
 			return false;
91 91
 		}
92 92
 
@@ -104,7 +104,7 @@  discard block
 block discarded – undo
104 104
 			$this->output->writeln('Files for following users couldn\'t be decrypted, ');
105 105
 			$this->output->writeln('maybe the user is not set up in a way that supports this operation: ');
106 106
 			foreach ($this->failed as $uid => $paths) {
107
-				$this->output->writeln('    ' . $uid);
107
+				$this->output->writeln('    '.$uid);
108 108
 			}
109 109
 			$this->output->writeln('');
110 110
 		}
@@ -125,10 +125,10 @@  discard block
 block discarded – undo
125 125
 			/** @var IEncryptionModule $module */
126 126
 			$module = call_user_func($moduleDesc['callback']);
127 127
 			$this->output->writeln('');
128
-			$this->output->writeln('Prepare "' . $module->getDisplayName() . '"');
128
+			$this->output->writeln('Prepare "'.$module->getDisplayName().'"');
129 129
 			$this->output->writeln('');
130 130
 			if ($module->prepareDecryptAll($this->input, $this->output, $user) === false) {
131
-				$this->output->writeln('Module "' . $moduleDesc['displayName'] . '" does not support the functionality to decrypt all files again or the initialization of the module failed!');
131
+				$this->output->writeln('Module "'.$moduleDesc['displayName'].'" does not support the functionality to decrypt all files again or the initialization of the module failed!');
132 132
 				return false;
133 133
 			}
134 134
 		}
@@ -206,16 +206,16 @@  discard block
 block discarded – undo
206 206
 
207 207
 		$this->setupUserFS($uid);
208 208
 		$directories = array();
209
-		$directories[] = '/' . $uid . '/files';
209
+		$directories[] = '/'.$uid.'/files';
210 210
 
211 211
 		while ($root = array_pop($directories)) {
212 212
 			$content = $this->rootView->getDirectoryContent($root);
213 213
 			foreach ($content as $file) {
214 214
 				// only decrypt files owned by the user
215
-				if($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
215
+				if ($file->getStorage()->instanceOfStorage('OCA\Files_Sharing\SharedStorage')) {
216 216
 					continue;
217 217
 				}
218
-				$path = $root . '/' . $file['name'];
218
+				$path = $root.'/'.$file['name'];
219 219
 				if ($this->rootView->is_dir($path)) {
220 220
 					$directories[] = $path;
221 221
 					continue;
@@ -259,7 +259,7 @@  discard block
 block discarded – undo
259 259
 		}
260 260
 
261 261
 		$source = $path;
262
-		$target = $path . '.decrypted.' . $this->getTimestamp();
262
+		$target = $path.'.decrypted.'.$this->getTimestamp();
263 263
 
264 264
 		try {
265 265
 			$this->rootView->copy($source, $target);
Please login to merge, or discard this patch.
apps/encryption/lib/Crypto/EncryptAll.php 2 patches
Indentation   +435 added lines, -435 removed lines patch added patch discarded remove patch
@@ -46,440 +46,440 @@
 block discarded – undo
46 46
 
47 47
 class EncryptAll {
48 48
 
49
-	/** @var Setup */
50
-	protected $userSetup;
51
-
52
-	/** @var IUserManager */
53
-	protected $userManager;
54
-
55
-	/** @var View */
56
-	protected $rootView;
57
-
58
-	/** @var KeyManager */
59
-	protected $keyManager;
60
-
61
-	/** @var Util */
62
-	protected $util;
63
-
64
-	/** @var array  */
65
-	protected $userPasswords;
66
-
67
-	/** @var  IConfig */
68
-	protected $config;
69
-
70
-	/** @var IMailer */
71
-	protected $mailer;
72
-
73
-	/** @var  IL10N */
74
-	protected $l;
75
-
76
-	/** @var  QuestionHelper */
77
-	protected $questionHelper;
78
-
79
-	/** @var  OutputInterface */
80
-	protected $output;
81
-
82
-	/** @var  InputInterface */
83
-	protected $input;
84
-
85
-	/** @var ISecureRandom */
86
-	protected $secureRandom;
87
-
88
-	/**
89
-	 * @param Setup $userSetup
90
-	 * @param IUserManager $userManager
91
-	 * @param View $rootView
92
-	 * @param KeyManager $keyManager
93
-	 * @param Util $util
94
-	 * @param IConfig $config
95
-	 * @param IMailer $mailer
96
-	 * @param IL10N $l
97
-	 * @param QuestionHelper $questionHelper
98
-	 * @param ISecureRandom $secureRandom
99
-	 */
100
-	public function __construct(
101
-		Setup $userSetup,
102
-		IUserManager $userManager,
103
-		View $rootView,
104
-		KeyManager $keyManager,
105
-		Util $util,
106
-		IConfig $config,
107
-		IMailer $mailer,
108
-		IL10N $l,
109
-		QuestionHelper $questionHelper,
110
-		ISecureRandom $secureRandom
111
-	) {
112
-		$this->userSetup = $userSetup;
113
-		$this->userManager = $userManager;
114
-		$this->rootView = $rootView;
115
-		$this->keyManager = $keyManager;
116
-		$this->util = $util;
117
-		$this->config = $config;
118
-		$this->mailer = $mailer;
119
-		$this->l = $l;
120
-		$this->questionHelper = $questionHelper;
121
-		$this->secureRandom = $secureRandom;
122
-		// store one time passwords for the users
123
-		$this->userPasswords = array();
124
-	}
125
-
126
-	/**
127
-	 * start to encrypt all files
128
-	 *
129
-	 * @param InputInterface $input
130
-	 * @param OutputInterface $output
131
-	 */
132
-	public function encryptAll(InputInterface $input, OutputInterface $output) {
133
-
134
-		$this->input = $input;
135
-		$this->output = $output;
136
-
137
-		$headline = 'Encrypt all files with the ' . Encryption::DISPLAY_NAME;
138
-		$this->output->writeln("\n");
139
-		$this->output->writeln($headline);
140
-		$this->output->writeln(str_pad('', strlen($headline), '='));
141
-		$this->output->writeln("\n");
142
-
143
-		if ($this->util->isMasterKeyEnabled()) {
144
-			$this->output->writeln('Use master key to encrypt all files.');
145
-			$this->keyManager->validateMasterKey();
146
-		} else {
147
-			//create private/public keys for each user and store the private key password
148
-			$this->output->writeln('Create key-pair for every user');
149
-			$this->output->writeln('------------------------------');
150
-			$this->output->writeln('');
151
-			$this->output->writeln('This module will encrypt all files in the users files folder initially.');
152
-			$this->output->writeln('Already existing versions and files in the trash bin will not be encrypted.');
153
-			$this->output->writeln('');
154
-			$this->createKeyPairs();
155
-		}
156
-
157
-
158
-		// output generated encryption key passwords
159
-		if ($this->util->isMasterKeyEnabled() === false) {
160
-			//send-out or display password list and write it to a file
161
-			$this->output->writeln("\n");
162
-			$this->output->writeln('Generated encryption key passwords');
163
-			$this->output->writeln('----------------------------------');
164
-			$this->output->writeln('');
165
-			$this->outputPasswords();
166
-		}
167
-
168
-		//setup users file system and encrypt all files one by one (take should encrypt setting of storage into account)
169
-		$this->output->writeln("\n");
170
-		$this->output->writeln('Start to encrypt users files');
171
-		$this->output->writeln('----------------------------');
172
-		$this->output->writeln('');
173
-		$this->encryptAllUsersFiles();
174
-		$this->output->writeln("\n");
175
-	}
176
-
177
-	/**
178
-	 * create key-pair for every user
179
-	 */
180
-	protected function createKeyPairs() {
181
-		$this->output->writeln("\n");
182
-		$progress = new ProgressBar($this->output);
183
-		$progress->setFormat(" %message% \n [%bar%]");
184
-		$progress->start();
185
-
186
-		foreach($this->userManager->getBackends() as $backend) {
187
-			$limit = 500;
188
-			$offset = 0;
189
-			do {
190
-				$users = $backend->getUsers('', $limit, $offset);
191
-				foreach ($users as $user) {
192
-					if ($this->keyManager->userHasKeys($user) === false) {
193
-						$progress->setMessage('Create key-pair for ' . $user);
194
-						$progress->advance();
195
-						$this->setupUserFS($user);
196
-						$password = $this->generateOneTimePassword($user);
197
-						$this->userSetup->setupUser($user, $password);
198
-					} else {
199
-						// users which already have a key-pair will be stored with a
200
-						// empty password and filtered out later
201
-						$this->userPasswords[$user] = '';
202
-					}
203
-				}
204
-				$offset += $limit;
205
-			} while(count($users) >= $limit);
206
-		}
207
-
208
-		$progress->setMessage('Key-pair created for all users');
209
-		$progress->finish();
210
-	}
211
-
212
-	/**
213
-	 * iterate over all user and encrypt their files
214
-	 */
215
-	protected function encryptAllUsersFiles() {
216
-		$this->output->writeln("\n");
217
-		$progress = new ProgressBar($this->output);
218
-		$progress->setFormat(" %message% \n [%bar%]");
219
-		$progress->start();
220
-		$numberOfUsers = count($this->userPasswords);
221
-		$userNo = 1;
222
-		if ($this->util->isMasterKeyEnabled()) {
223
-			$this->encryptAllUserFilesWithMasterKey($progress);
224
-		} else {
225
-			foreach ($this->userPasswords as $uid => $password) {
226
-				$userCount = "$uid ($userNo of $numberOfUsers)";
227
-				$this->encryptUsersFiles($uid, $progress, $userCount);
228
-				$userNo++;
229
-			}
230
-		}
231
-		$progress->setMessage("all files encrypted");
232
-		$progress->finish();
233
-
234
-	}
235
-
236
-	/**
237
-	 * encrypt all user files with the master key
238
-	 *
239
-	 * @param ProgressBar $progress
240
-	 */
241
-	protected function encryptAllUserFilesWithMasterKey(ProgressBar $progress) {
242
-		$userNo = 1;
243
-		foreach($this->userManager->getBackends() as $backend) {
244
-			$limit = 500;
245
-			$offset = 0;
246
-			do {
247
-				$users = $backend->getUsers('', $limit, $offset);
248
-				foreach ($users as $user) {
249
-					$userCount = "$user ($userNo)";
250
-					$this->encryptUsersFiles($user, $progress, $userCount);
251
-					$userNo++;
252
-				}
253
-				$offset += $limit;
254
-			} while(count($users) >= $limit);
255
-		}
256
-	}
257
-
258
-	/**
259
-	 * encrypt files from the given user
260
-	 *
261
-	 * @param string $uid
262
-	 * @param ProgressBar $progress
263
-	 * @param string $userCount
264
-	 */
265
-	protected function encryptUsersFiles($uid, ProgressBar $progress, $userCount) {
266
-
267
-		$this->setupUserFS($uid);
268
-		$directories = array();
269
-		$directories[] =  '/' . $uid . '/files';
270
-
271
-		while($root = array_pop($directories)) {
272
-			$content = $this->rootView->getDirectoryContent($root);
273
-			foreach ($content as $file) {
274
-				$path = $root . '/' . $file['name'];
275
-				if ($this->rootView->is_dir($path)) {
276
-					$directories[] = $path;
277
-					continue;
278
-				} else {
279
-					$progress->setMessage("encrypt files for user $userCount: $path");
280
-					$progress->advance();
281
-					if($this->encryptFile($path) === false) {
282
-						$progress->setMessage("encrypt files for user $userCount: $path (already encrypted)");
283
-						$progress->advance();
284
-					}
285
-				}
286
-			}
287
-		}
288
-	}
289
-
290
-	/**
291
-	 * encrypt file
292
-	 *
293
-	 * @param string $path
294
-	 * @return bool
295
-	 */
296
-	protected function encryptFile($path) {
297
-
298
-		// skip already encrypted files
299
-		$fileInfo = $this->rootView->getFileInfo($path);
300
-		if ($fileInfo !== false && $fileInfo->isEncrypted()) {
301
-			return true;
302
-		}
303
-
304
-		$source = $path;
305
-		$target = $path . '.encrypted.' . time();
306
-
307
-		try {
308
-			$this->rootView->copy($source, $target);
309
-			$this->rootView->rename($target, $source);
310
-		} catch (DecryptionFailedException $e) {
311
-			if ($this->rootView->file_exists($target)) {
312
-				$this->rootView->unlink($target);
313
-			}
314
-			return false;
315
-		}
316
-
317
-		return true;
318
-	}
319
-
320
-	/**
321
-	 * output one-time encryption passwords
322
-	 */
323
-	protected function outputPasswords() {
324
-		$table = new Table($this->output);
325
-		$table->setHeaders(array('Username', 'Private key password'));
326
-
327
-		//create rows
328
-		$newPasswords = array();
329
-		$unchangedPasswords = array();
330
-		foreach ($this->userPasswords as $uid => $password) {
331
-			if (empty($password)) {
332
-				$unchangedPasswords[] = $uid;
333
-			} else {
334
-				$newPasswords[] = [$uid, $password];
335
-			}
336
-		}
337
-
338
-		if (empty($newPasswords)) {
339
-			$this->output->writeln("\nAll users already had a key-pair, no further action needed.\n");
340
-			return;
341
-		}
342
-
343
-		$table->setRows($newPasswords);
344
-		$table->render();
345
-
346
-		if (!empty($unchangedPasswords)) {
347
-			$this->output->writeln("\nThe following users already had a key-pair which was reused without setting a new password:\n");
348
-			foreach ($unchangedPasswords as $uid) {
349
-				$this->output->writeln("    $uid");
350
-			}
351
-		}
352
-
353
-		$this->writePasswordsToFile($newPasswords);
354
-
355
-		$this->output->writeln('');
356
-		$question = new ConfirmationQuestion('Do you want to send the passwords directly to the users by mail? (y/n) ', false);
357
-		if ($this->questionHelper->ask($this->input, $this->output, $question)) {
358
-			$this->sendPasswordsByMail();
359
-		}
360
-	}
361
-
362
-	/**
363
-	 * write one-time encryption passwords to a csv file
364
-	 *
365
-	 * @param array $passwords
366
-	 */
367
-	protected function writePasswordsToFile(array $passwords) {
368
-		$fp = $this->rootView->fopen('oneTimeEncryptionPasswords.csv', 'w');
369
-		foreach ($passwords as $pwd) {
370
-			fputcsv($fp, $pwd);
371
-		}
372
-		fclose($fp);
373
-		$this->output->writeln("\n");
374
-		$this->output->writeln('A list of all newly created passwords was written to data/oneTimeEncryptionPasswords.csv');
375
-		$this->output->writeln('');
376
-		$this->output->writeln('Each of these users need to login to the web interface, go to the');
377
-		$this->output->writeln('personal settings section "basic encryption module" and');
378
-		$this->output->writeln('update the private key password to match the login password again by');
379
-		$this->output->writeln('entering the one-time password into the "old log-in password" field');
380
-		$this->output->writeln('and their current login password');
381
-	}
382
-
383
-	/**
384
-	 * setup user file system
385
-	 *
386
-	 * @param string $uid
387
-	 */
388
-	protected function setupUserFS($uid) {
389
-		\OC_Util::tearDownFS();
390
-		\OC_Util::setupFS($uid);
391
-	}
392
-
393
-	/**
394
-	 * generate one time password for the user and store it in a array
395
-	 *
396
-	 * @param string $uid
397
-	 * @return string password
398
-	 */
399
-	protected function generateOneTimePassword($uid) {
400
-		$password = $this->secureRandom->generate(8);
401
-		$this->userPasswords[$uid] = $password;
402
-		return $password;
403
-	}
404
-
405
-	/**
406
-	 * send encryption key passwords to the users by mail
407
-	 */
408
-	protected function sendPasswordsByMail() {
409
-		$noMail = [];
410
-
411
-		$this->output->writeln('');
412
-		$progress = new ProgressBar($this->output, count($this->userPasswords));
413
-		$progress->start();
414
-
415
-		foreach ($this->userPasswords as $uid => $password) {
416
-			$progress->advance();
417
-			if (!empty($password)) {
418
-				$recipient = $this->userManager->get($uid);
419
-				$recipientDisplayName = $recipient->getDisplayName();
420
-				$to = $recipient->getEMailAddress();
421
-
422
-				if ($to === '') {
423
-					$noMail[] = $uid;
424
-					continue;
425
-				}
426
-
427
-				$subject = (string)$this->l->t('one-time password for server-side-encryption');
428
-				list($htmlBody, $textBody) = $this->createMailBody($password);
429
-
430
-				// send it out now
431
-				try {
432
-					$message = $this->mailer->createMessage();
433
-					$message->setSubject($subject);
434
-					$message->setTo([$to => $recipientDisplayName]);
435
-					$message->setHtmlBody($htmlBody);
436
-					$message->setPlainBody($textBody);
437
-					$message->setFrom([
438
-						\OCP\Util::getDefaultEmailAddress('admin-noreply')
439
-					]);
440
-
441
-					$this->mailer->send($message);
442
-				} catch (\Exception $e) {
443
-					$noMail[] = $uid;
444
-				}
445
-			}
446
-		}
447
-
448
-		$progress->finish();
449
-
450
-		if (empty($noMail)) {
451
-			$this->output->writeln("\n\nPassword successfully send to all users");
452
-		} else {
453
-			$table = new Table($this->output);
454
-			$table->setHeaders(array('Username', 'Private key password'));
455
-			$this->output->writeln("\n\nCould not send password to following users:\n");
456
-			$rows = [];
457
-			foreach ($noMail as $uid) {
458
-				$rows[] = [$uid, $this->userPasswords[$uid]];
459
-			}
460
-			$table->setRows($rows);
461
-			$table->render();
462
-		}
463
-
464
-	}
465
-
466
-	/**
467
-	 * create mail body for plain text and html mail
468
-	 *
469
-	 * @param string $password one-time encryption password
470
-	 * @return array an array of the html mail body and the plain text mail body
471
-	 */
472
-	protected function createMailBody($password) {
473
-
474
-		$html = new \OC_Template("encryption", "mail", "");
475
-		$html->assign ('password', $password);
476
-		$htmlMail = $html->fetchPage();
477
-
478
-		$plainText = new \OC_Template("encryption", "altmail", "");
479
-		$plainText->assign ('password', $password);
480
-		$plainTextMail = $plainText->fetchPage();
481
-
482
-		return [$htmlMail, $plainTextMail];
483
-	}
49
+    /** @var Setup */
50
+    protected $userSetup;
51
+
52
+    /** @var IUserManager */
53
+    protected $userManager;
54
+
55
+    /** @var View */
56
+    protected $rootView;
57
+
58
+    /** @var KeyManager */
59
+    protected $keyManager;
60
+
61
+    /** @var Util */
62
+    protected $util;
63
+
64
+    /** @var array  */
65
+    protected $userPasswords;
66
+
67
+    /** @var  IConfig */
68
+    protected $config;
69
+
70
+    /** @var IMailer */
71
+    protected $mailer;
72
+
73
+    /** @var  IL10N */
74
+    protected $l;
75
+
76
+    /** @var  QuestionHelper */
77
+    protected $questionHelper;
78
+
79
+    /** @var  OutputInterface */
80
+    protected $output;
81
+
82
+    /** @var  InputInterface */
83
+    protected $input;
84
+
85
+    /** @var ISecureRandom */
86
+    protected $secureRandom;
87
+
88
+    /**
89
+     * @param Setup $userSetup
90
+     * @param IUserManager $userManager
91
+     * @param View $rootView
92
+     * @param KeyManager $keyManager
93
+     * @param Util $util
94
+     * @param IConfig $config
95
+     * @param IMailer $mailer
96
+     * @param IL10N $l
97
+     * @param QuestionHelper $questionHelper
98
+     * @param ISecureRandom $secureRandom
99
+     */
100
+    public function __construct(
101
+        Setup $userSetup,
102
+        IUserManager $userManager,
103
+        View $rootView,
104
+        KeyManager $keyManager,
105
+        Util $util,
106
+        IConfig $config,
107
+        IMailer $mailer,
108
+        IL10N $l,
109
+        QuestionHelper $questionHelper,
110
+        ISecureRandom $secureRandom
111
+    ) {
112
+        $this->userSetup = $userSetup;
113
+        $this->userManager = $userManager;
114
+        $this->rootView = $rootView;
115
+        $this->keyManager = $keyManager;
116
+        $this->util = $util;
117
+        $this->config = $config;
118
+        $this->mailer = $mailer;
119
+        $this->l = $l;
120
+        $this->questionHelper = $questionHelper;
121
+        $this->secureRandom = $secureRandom;
122
+        // store one time passwords for the users
123
+        $this->userPasswords = array();
124
+    }
125
+
126
+    /**
127
+     * start to encrypt all files
128
+     *
129
+     * @param InputInterface $input
130
+     * @param OutputInterface $output
131
+     */
132
+    public function encryptAll(InputInterface $input, OutputInterface $output) {
133
+
134
+        $this->input = $input;
135
+        $this->output = $output;
136
+
137
+        $headline = 'Encrypt all files with the ' . Encryption::DISPLAY_NAME;
138
+        $this->output->writeln("\n");
139
+        $this->output->writeln($headline);
140
+        $this->output->writeln(str_pad('', strlen($headline), '='));
141
+        $this->output->writeln("\n");
142
+
143
+        if ($this->util->isMasterKeyEnabled()) {
144
+            $this->output->writeln('Use master key to encrypt all files.');
145
+            $this->keyManager->validateMasterKey();
146
+        } else {
147
+            //create private/public keys for each user and store the private key password
148
+            $this->output->writeln('Create key-pair for every user');
149
+            $this->output->writeln('------------------------------');
150
+            $this->output->writeln('');
151
+            $this->output->writeln('This module will encrypt all files in the users files folder initially.');
152
+            $this->output->writeln('Already existing versions and files in the trash bin will not be encrypted.');
153
+            $this->output->writeln('');
154
+            $this->createKeyPairs();
155
+        }
156
+
157
+
158
+        // output generated encryption key passwords
159
+        if ($this->util->isMasterKeyEnabled() === false) {
160
+            //send-out or display password list and write it to a file
161
+            $this->output->writeln("\n");
162
+            $this->output->writeln('Generated encryption key passwords');
163
+            $this->output->writeln('----------------------------------');
164
+            $this->output->writeln('');
165
+            $this->outputPasswords();
166
+        }
167
+
168
+        //setup users file system and encrypt all files one by one (take should encrypt setting of storage into account)
169
+        $this->output->writeln("\n");
170
+        $this->output->writeln('Start to encrypt users files');
171
+        $this->output->writeln('----------------------------');
172
+        $this->output->writeln('');
173
+        $this->encryptAllUsersFiles();
174
+        $this->output->writeln("\n");
175
+    }
176
+
177
+    /**
178
+     * create key-pair for every user
179
+     */
180
+    protected function createKeyPairs() {
181
+        $this->output->writeln("\n");
182
+        $progress = new ProgressBar($this->output);
183
+        $progress->setFormat(" %message% \n [%bar%]");
184
+        $progress->start();
185
+
186
+        foreach($this->userManager->getBackends() as $backend) {
187
+            $limit = 500;
188
+            $offset = 0;
189
+            do {
190
+                $users = $backend->getUsers('', $limit, $offset);
191
+                foreach ($users as $user) {
192
+                    if ($this->keyManager->userHasKeys($user) === false) {
193
+                        $progress->setMessage('Create key-pair for ' . $user);
194
+                        $progress->advance();
195
+                        $this->setupUserFS($user);
196
+                        $password = $this->generateOneTimePassword($user);
197
+                        $this->userSetup->setupUser($user, $password);
198
+                    } else {
199
+                        // users which already have a key-pair will be stored with a
200
+                        // empty password and filtered out later
201
+                        $this->userPasswords[$user] = '';
202
+                    }
203
+                }
204
+                $offset += $limit;
205
+            } while(count($users) >= $limit);
206
+        }
207
+
208
+        $progress->setMessage('Key-pair created for all users');
209
+        $progress->finish();
210
+    }
211
+
212
+    /**
213
+     * iterate over all user and encrypt their files
214
+     */
215
+    protected function encryptAllUsersFiles() {
216
+        $this->output->writeln("\n");
217
+        $progress = new ProgressBar($this->output);
218
+        $progress->setFormat(" %message% \n [%bar%]");
219
+        $progress->start();
220
+        $numberOfUsers = count($this->userPasswords);
221
+        $userNo = 1;
222
+        if ($this->util->isMasterKeyEnabled()) {
223
+            $this->encryptAllUserFilesWithMasterKey($progress);
224
+        } else {
225
+            foreach ($this->userPasswords as $uid => $password) {
226
+                $userCount = "$uid ($userNo of $numberOfUsers)";
227
+                $this->encryptUsersFiles($uid, $progress, $userCount);
228
+                $userNo++;
229
+            }
230
+        }
231
+        $progress->setMessage("all files encrypted");
232
+        $progress->finish();
233
+
234
+    }
235
+
236
+    /**
237
+     * encrypt all user files with the master key
238
+     *
239
+     * @param ProgressBar $progress
240
+     */
241
+    protected function encryptAllUserFilesWithMasterKey(ProgressBar $progress) {
242
+        $userNo = 1;
243
+        foreach($this->userManager->getBackends() as $backend) {
244
+            $limit = 500;
245
+            $offset = 0;
246
+            do {
247
+                $users = $backend->getUsers('', $limit, $offset);
248
+                foreach ($users as $user) {
249
+                    $userCount = "$user ($userNo)";
250
+                    $this->encryptUsersFiles($user, $progress, $userCount);
251
+                    $userNo++;
252
+                }
253
+                $offset += $limit;
254
+            } while(count($users) >= $limit);
255
+        }
256
+    }
257
+
258
+    /**
259
+     * encrypt files from the given user
260
+     *
261
+     * @param string $uid
262
+     * @param ProgressBar $progress
263
+     * @param string $userCount
264
+     */
265
+    protected function encryptUsersFiles($uid, ProgressBar $progress, $userCount) {
266
+
267
+        $this->setupUserFS($uid);
268
+        $directories = array();
269
+        $directories[] =  '/' . $uid . '/files';
270
+
271
+        while($root = array_pop($directories)) {
272
+            $content = $this->rootView->getDirectoryContent($root);
273
+            foreach ($content as $file) {
274
+                $path = $root . '/' . $file['name'];
275
+                if ($this->rootView->is_dir($path)) {
276
+                    $directories[] = $path;
277
+                    continue;
278
+                } else {
279
+                    $progress->setMessage("encrypt files for user $userCount: $path");
280
+                    $progress->advance();
281
+                    if($this->encryptFile($path) === false) {
282
+                        $progress->setMessage("encrypt files for user $userCount: $path (already encrypted)");
283
+                        $progress->advance();
284
+                    }
285
+                }
286
+            }
287
+        }
288
+    }
289
+
290
+    /**
291
+     * encrypt file
292
+     *
293
+     * @param string $path
294
+     * @return bool
295
+     */
296
+    protected function encryptFile($path) {
297
+
298
+        // skip already encrypted files
299
+        $fileInfo = $this->rootView->getFileInfo($path);
300
+        if ($fileInfo !== false && $fileInfo->isEncrypted()) {
301
+            return true;
302
+        }
303
+
304
+        $source = $path;
305
+        $target = $path . '.encrypted.' . time();
306
+
307
+        try {
308
+            $this->rootView->copy($source, $target);
309
+            $this->rootView->rename($target, $source);
310
+        } catch (DecryptionFailedException $e) {
311
+            if ($this->rootView->file_exists($target)) {
312
+                $this->rootView->unlink($target);
313
+            }
314
+            return false;
315
+        }
316
+
317
+        return true;
318
+    }
319
+
320
+    /**
321
+     * output one-time encryption passwords
322
+     */
323
+    protected function outputPasswords() {
324
+        $table = new Table($this->output);
325
+        $table->setHeaders(array('Username', 'Private key password'));
326
+
327
+        //create rows
328
+        $newPasswords = array();
329
+        $unchangedPasswords = array();
330
+        foreach ($this->userPasswords as $uid => $password) {
331
+            if (empty($password)) {
332
+                $unchangedPasswords[] = $uid;
333
+            } else {
334
+                $newPasswords[] = [$uid, $password];
335
+            }
336
+        }
337
+
338
+        if (empty($newPasswords)) {
339
+            $this->output->writeln("\nAll users already had a key-pair, no further action needed.\n");
340
+            return;
341
+        }
342
+
343
+        $table->setRows($newPasswords);
344
+        $table->render();
345
+
346
+        if (!empty($unchangedPasswords)) {
347
+            $this->output->writeln("\nThe following users already had a key-pair which was reused without setting a new password:\n");
348
+            foreach ($unchangedPasswords as $uid) {
349
+                $this->output->writeln("    $uid");
350
+            }
351
+        }
352
+
353
+        $this->writePasswordsToFile($newPasswords);
354
+
355
+        $this->output->writeln('');
356
+        $question = new ConfirmationQuestion('Do you want to send the passwords directly to the users by mail? (y/n) ', false);
357
+        if ($this->questionHelper->ask($this->input, $this->output, $question)) {
358
+            $this->sendPasswordsByMail();
359
+        }
360
+    }
361
+
362
+    /**
363
+     * write one-time encryption passwords to a csv file
364
+     *
365
+     * @param array $passwords
366
+     */
367
+    protected function writePasswordsToFile(array $passwords) {
368
+        $fp = $this->rootView->fopen('oneTimeEncryptionPasswords.csv', 'w');
369
+        foreach ($passwords as $pwd) {
370
+            fputcsv($fp, $pwd);
371
+        }
372
+        fclose($fp);
373
+        $this->output->writeln("\n");
374
+        $this->output->writeln('A list of all newly created passwords was written to data/oneTimeEncryptionPasswords.csv');
375
+        $this->output->writeln('');
376
+        $this->output->writeln('Each of these users need to login to the web interface, go to the');
377
+        $this->output->writeln('personal settings section "basic encryption module" and');
378
+        $this->output->writeln('update the private key password to match the login password again by');
379
+        $this->output->writeln('entering the one-time password into the "old log-in password" field');
380
+        $this->output->writeln('and their current login password');
381
+    }
382
+
383
+    /**
384
+     * setup user file system
385
+     *
386
+     * @param string $uid
387
+     */
388
+    protected function setupUserFS($uid) {
389
+        \OC_Util::tearDownFS();
390
+        \OC_Util::setupFS($uid);
391
+    }
392
+
393
+    /**
394
+     * generate one time password for the user and store it in a array
395
+     *
396
+     * @param string $uid
397
+     * @return string password
398
+     */
399
+    protected function generateOneTimePassword($uid) {
400
+        $password = $this->secureRandom->generate(8);
401
+        $this->userPasswords[$uid] = $password;
402
+        return $password;
403
+    }
404
+
405
+    /**
406
+     * send encryption key passwords to the users by mail
407
+     */
408
+    protected function sendPasswordsByMail() {
409
+        $noMail = [];
410
+
411
+        $this->output->writeln('');
412
+        $progress = new ProgressBar($this->output, count($this->userPasswords));
413
+        $progress->start();
414
+
415
+        foreach ($this->userPasswords as $uid => $password) {
416
+            $progress->advance();
417
+            if (!empty($password)) {
418
+                $recipient = $this->userManager->get($uid);
419
+                $recipientDisplayName = $recipient->getDisplayName();
420
+                $to = $recipient->getEMailAddress();
421
+
422
+                if ($to === '') {
423
+                    $noMail[] = $uid;
424
+                    continue;
425
+                }
426
+
427
+                $subject = (string)$this->l->t('one-time password for server-side-encryption');
428
+                list($htmlBody, $textBody) = $this->createMailBody($password);
429
+
430
+                // send it out now
431
+                try {
432
+                    $message = $this->mailer->createMessage();
433
+                    $message->setSubject($subject);
434
+                    $message->setTo([$to => $recipientDisplayName]);
435
+                    $message->setHtmlBody($htmlBody);
436
+                    $message->setPlainBody($textBody);
437
+                    $message->setFrom([
438
+                        \OCP\Util::getDefaultEmailAddress('admin-noreply')
439
+                    ]);
440
+
441
+                    $this->mailer->send($message);
442
+                } catch (\Exception $e) {
443
+                    $noMail[] = $uid;
444
+                }
445
+            }
446
+        }
447
+
448
+        $progress->finish();
449
+
450
+        if (empty($noMail)) {
451
+            $this->output->writeln("\n\nPassword successfully send to all users");
452
+        } else {
453
+            $table = new Table($this->output);
454
+            $table->setHeaders(array('Username', 'Private key password'));
455
+            $this->output->writeln("\n\nCould not send password to following users:\n");
456
+            $rows = [];
457
+            foreach ($noMail as $uid) {
458
+                $rows[] = [$uid, $this->userPasswords[$uid]];
459
+            }
460
+            $table->setRows($rows);
461
+            $table->render();
462
+        }
463
+
464
+    }
465
+
466
+    /**
467
+     * create mail body for plain text and html mail
468
+     *
469
+     * @param string $password one-time encryption password
470
+     * @return array an array of the html mail body and the plain text mail body
471
+     */
472
+    protected function createMailBody($password) {
473
+
474
+        $html = new \OC_Template("encryption", "mail", "");
475
+        $html->assign ('password', $password);
476
+        $htmlMail = $html->fetchPage();
477
+
478
+        $plainText = new \OC_Template("encryption", "altmail", "");
479
+        $plainText->assign ('password', $password);
480
+        $plainTextMail = $plainText->fetchPage();
481
+
482
+        return [$htmlMail, $plainTextMail];
483
+    }
484 484
 
485 485
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -134,7 +134,7 @@  discard block
 block discarded – undo
134 134
 		$this->input = $input;
135 135
 		$this->output = $output;
136 136
 
137
-		$headline = 'Encrypt all files with the ' . Encryption::DISPLAY_NAME;
137
+		$headline = 'Encrypt all files with the '.Encryption::DISPLAY_NAME;
138 138
 		$this->output->writeln("\n");
139 139
 		$this->output->writeln($headline);
140 140
 		$this->output->writeln(str_pad('', strlen($headline), '='));
@@ -183,14 +183,14 @@  discard block
 block discarded – undo
183 183
 		$progress->setFormat(" %message% \n [%bar%]");
184 184
 		$progress->start();
185 185
 
186
-		foreach($this->userManager->getBackends() as $backend) {
186
+		foreach ($this->userManager->getBackends() as $backend) {
187 187
 			$limit = 500;
188 188
 			$offset = 0;
189 189
 			do {
190 190
 				$users = $backend->getUsers('', $limit, $offset);
191 191
 				foreach ($users as $user) {
192 192
 					if ($this->keyManager->userHasKeys($user) === false) {
193
-						$progress->setMessage('Create key-pair for ' . $user);
193
+						$progress->setMessage('Create key-pair for '.$user);
194 194
 						$progress->advance();
195 195
 						$this->setupUserFS($user);
196 196
 						$password = $this->generateOneTimePassword($user);
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
 					}
203 203
 				}
204 204
 				$offset += $limit;
205
-			} while(count($users) >= $limit);
205
+			} while (count($users) >= $limit);
206 206
 		}
207 207
 
208 208
 		$progress->setMessage('Key-pair created for all users');
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
 	 */
241 241
 	protected function encryptAllUserFilesWithMasterKey(ProgressBar $progress) {
242 242
 		$userNo = 1;
243
-		foreach($this->userManager->getBackends() as $backend) {
243
+		foreach ($this->userManager->getBackends() as $backend) {
244 244
 			$limit = 500;
245 245
 			$offset = 0;
246 246
 			do {
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
 					$userNo++;
252 252
 				}
253 253
 				$offset += $limit;
254
-			} while(count($users) >= $limit);
254
+			} while (count($users) >= $limit);
255 255
 		}
256 256
 	}
257 257
 
@@ -266,19 +266,19 @@  discard block
 block discarded – undo
266 266
 
267 267
 		$this->setupUserFS($uid);
268 268
 		$directories = array();
269
-		$directories[] =  '/' . $uid . '/files';
269
+		$directories[] = '/'.$uid.'/files';
270 270
 
271
-		while($root = array_pop($directories)) {
271
+		while ($root = array_pop($directories)) {
272 272
 			$content = $this->rootView->getDirectoryContent($root);
273 273
 			foreach ($content as $file) {
274
-				$path = $root . '/' . $file['name'];
274
+				$path = $root.'/'.$file['name'];
275 275
 				if ($this->rootView->is_dir($path)) {
276 276
 					$directories[] = $path;
277 277
 					continue;
278 278
 				} else {
279 279
 					$progress->setMessage("encrypt files for user $userCount: $path");
280 280
 					$progress->advance();
281
-					if($this->encryptFile($path) === false) {
281
+					if ($this->encryptFile($path) === false) {
282 282
 						$progress->setMessage("encrypt files for user $userCount: $path (already encrypted)");
283 283
 						$progress->advance();
284 284
 					}
@@ -302,7 +302,7 @@  discard block
 block discarded – undo
302 302
 		}
303 303
 
304 304
 		$source = $path;
305
-		$target = $path . '.encrypted.' . time();
305
+		$target = $path.'.encrypted.'.time();
306 306
 
307 307
 		try {
308 308
 			$this->rootView->copy($source, $target);
@@ -424,7 +424,7 @@  discard block
 block discarded – undo
424 424
 					continue;
425 425
 				}
426 426
 
427
-				$subject = (string)$this->l->t('one-time password for server-side-encryption');
427
+				$subject = (string) $this->l->t('one-time password for server-side-encryption');
428 428
 				list($htmlBody, $textBody) = $this->createMailBody($password);
429 429
 
430 430
 				// send it out now
@@ -472,11 +472,11 @@  discard block
 block discarded – undo
472 472
 	protected function createMailBody($password) {
473 473
 
474 474
 		$html = new \OC_Template("encryption", "mail", "");
475
-		$html->assign ('password', $password);
475
+		$html->assign('password', $password);
476 476
 		$htmlMail = $html->fetchPage();
477 477
 
478 478
 		$plainText = new \OC_Template("encryption", "altmail", "");
479
-		$plainText->assign ('password', $password);
479
+		$plainText->assign('password', $password);
480 480
 		$plainTextMail = $plainText->fetchPage();
481 481
 
482 482
 		return [$htmlMail, $plainTextMail];
Please login to merge, or discard this patch.