Completed
Pull Request — master (#8056)
by Morris
59:31 queued 42:19
created
lib/private/Collaboration/Collaborators/UserPlugin.php 1 patch
Indentation   +122 added lines, -122 removed lines patch added patch discarded remove patch
@@ -35,126 +35,126 @@
 block discarded – undo
35 35
 use OCP\Share;
36 36
 
37 37
 class UserPlugin implements ISearchPlugin {
38
-	/* @var bool */
39
-	protected $shareWithGroupOnly;
40
-	protected $shareeEnumeration;
41
-
42
-	/** @var IConfig */
43
-	private $config;
44
-	/** @var IGroupManager */
45
-	private $groupManager;
46
-	/** @var IUserSession */
47
-	private $userSession;
48
-	/** @var IUserManager */
49
-	private $userManager;
50
-
51
-	public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession) {
52
-		$this->config = $config;
53
-
54
-		$this->groupManager = $groupManager;
55
-		$this->userSession = $userSession;
56
-		$this->userManager = $userManager;
57
-
58
-		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
59
-		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
60
-	}
61
-
62
-	public function search($search, $limit, $offset, ISearchResult $searchResult) {
63
-		$result = ['wide' => [], 'exact' => []];
64
-		$users = [];
65
-		$hasMoreResults = false;
66
-
67
-		$userGroups = [];
68
-		if ($this->shareWithGroupOnly) {
69
-			// Search in all the groups this user is part of
70
-			$userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
71
-			foreach ($userGroups as $userGroup) {
72
-				$usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset);
73
-				foreach ($usersTmp as $uid => $userDisplayName) {
74
-					$users[$uid] = $userDisplayName;
75
-				}
76
-			}
77
-		} else {
78
-			// Search in all users
79
-			$usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
80
-
81
-			foreach ($usersTmp as $user) {
82
-				$users[$user->getUID()] = $user->getDisplayName();
83
-			}
84
-		}
85
-
86
-		$this->takeOutCurrentUser($users);
87
-
88
-		if (!$this->shareeEnumeration || sizeof($users) < $limit) {
89
-			$hasMoreResults = true;
90
-		}
91
-
92
-		$foundUserById = false;
93
-		$lowerSearch = strtolower($search);
94
-		foreach ($users as $uid => $userDisplayName) {
95
-			if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
96
-				if (strtolower($uid) === $lowerSearch) {
97
-					$foundUserById = true;
98
-				}
99
-				$result['exact'][] = [
100
-					'label' => $userDisplayName,
101
-					'value' => [
102
-						'shareType' => Share::SHARE_TYPE_USER,
103
-						'shareWith' => $uid,
104
-					],
105
-				];
106
-			} else {
107
-				$result['wide'][] = [
108
-					'label' => $userDisplayName,
109
-					'value' => [
110
-						'shareType' => Share::SHARE_TYPE_USER,
111
-						'shareWith' => $uid,
112
-					],
113
-				];
114
-			}
115
-		}
116
-
117
-		if ($offset === 0 && !$foundUserById) {
118
-			// On page one we try if the search result has a direct hit on the
119
-			// user id and if so, we add that to the exact match list
120
-			$user = $this->userManager->get($search);
121
-			if ($user instanceof IUser) {
122
-				$addUser = true;
123
-
124
-				if ($this->shareWithGroupOnly) {
125
-					// Only add, if we have a common group
126
-					$commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
127
-					$addUser = !empty($commonGroups);
128
-				}
129
-
130
-				if ($addUser) {
131
-					$result['exact'][] = [
132
-						'label' => $user->getDisplayName(),
133
-						'value' => [
134
-							'shareType' => Share::SHARE_TYPE_USER,
135
-							'shareWith' => $user->getUID(),
136
-						],
137
-					];
138
-				}
139
-			}
140
-		}
141
-
142
-		if (!$this->shareeEnumeration) {
143
-			$result['wide'] = [];
144
-		}
145
-
146
-		$type = new SearchResultType('users');
147
-		$searchResult->addResultSet($type, $result['wide'], $result['exact']);
148
-
149
-		return $hasMoreResults;
150
-	}
151
-
152
-	public function takeOutCurrentUser(array &$users) {
153
-		$currentUser = $this->userSession->getUser();
154
-		if(!is_null($currentUser)) {
155
-			if (isset($users[$currentUser->getUID()])) {
156
-				unset($users[$currentUser->getUID()]);
157
-			}
158
-		}
159
-	}
38
+    /* @var bool */
39
+    protected $shareWithGroupOnly;
40
+    protected $shareeEnumeration;
41
+
42
+    /** @var IConfig */
43
+    private $config;
44
+    /** @var IGroupManager */
45
+    private $groupManager;
46
+    /** @var IUserSession */
47
+    private $userSession;
48
+    /** @var IUserManager */
49
+    private $userManager;
50
+
51
+    public function __construct(IConfig $config, IUserManager $userManager, IGroupManager $groupManager, IUserSession $userSession) {
52
+        $this->config = $config;
53
+
54
+        $this->groupManager = $groupManager;
55
+        $this->userSession = $userSession;
56
+        $this->userManager = $userManager;
57
+
58
+        $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
59
+        $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
60
+    }
61
+
62
+    public function search($search, $limit, $offset, ISearchResult $searchResult) {
63
+        $result = ['wide' => [], 'exact' => []];
64
+        $users = [];
65
+        $hasMoreResults = false;
66
+
67
+        $userGroups = [];
68
+        if ($this->shareWithGroupOnly) {
69
+            // Search in all the groups this user is part of
70
+            $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
71
+            foreach ($userGroups as $userGroup) {
72
+                $usersTmp = $this->groupManager->displayNamesInGroup($userGroup, $search, $limit, $offset);
73
+                foreach ($usersTmp as $uid => $userDisplayName) {
74
+                    $users[$uid] = $userDisplayName;
75
+                }
76
+            }
77
+        } else {
78
+            // Search in all users
79
+            $usersTmp = $this->userManager->searchDisplayName($search, $limit, $offset);
80
+
81
+            foreach ($usersTmp as $user) {
82
+                $users[$user->getUID()] = $user->getDisplayName();
83
+            }
84
+        }
85
+
86
+        $this->takeOutCurrentUser($users);
87
+
88
+        if (!$this->shareeEnumeration || sizeof($users) < $limit) {
89
+            $hasMoreResults = true;
90
+        }
91
+
92
+        $foundUserById = false;
93
+        $lowerSearch = strtolower($search);
94
+        foreach ($users as $uid => $userDisplayName) {
95
+            if (strtolower($uid) === $lowerSearch || strtolower($userDisplayName) === $lowerSearch) {
96
+                if (strtolower($uid) === $lowerSearch) {
97
+                    $foundUserById = true;
98
+                }
99
+                $result['exact'][] = [
100
+                    'label' => $userDisplayName,
101
+                    'value' => [
102
+                        'shareType' => Share::SHARE_TYPE_USER,
103
+                        'shareWith' => $uid,
104
+                    ],
105
+                ];
106
+            } else {
107
+                $result['wide'][] = [
108
+                    'label' => $userDisplayName,
109
+                    'value' => [
110
+                        'shareType' => Share::SHARE_TYPE_USER,
111
+                        'shareWith' => $uid,
112
+                    ],
113
+                ];
114
+            }
115
+        }
116
+
117
+        if ($offset === 0 && !$foundUserById) {
118
+            // On page one we try if the search result has a direct hit on the
119
+            // user id and if so, we add that to the exact match list
120
+            $user = $this->userManager->get($search);
121
+            if ($user instanceof IUser) {
122
+                $addUser = true;
123
+
124
+                if ($this->shareWithGroupOnly) {
125
+                    // Only add, if we have a common group
126
+                    $commonGroups = array_intersect($userGroups, $this->groupManager->getUserGroupIds($user));
127
+                    $addUser = !empty($commonGroups);
128
+                }
129
+
130
+                if ($addUser) {
131
+                    $result['exact'][] = [
132
+                        'label' => $user->getDisplayName(),
133
+                        'value' => [
134
+                            'shareType' => Share::SHARE_TYPE_USER,
135
+                            'shareWith' => $user->getUID(),
136
+                        ],
137
+                    ];
138
+                }
139
+            }
140
+        }
141
+
142
+        if (!$this->shareeEnumeration) {
143
+            $result['wide'] = [];
144
+        }
145
+
146
+        $type = new SearchResultType('users');
147
+        $searchResult->addResultSet($type, $result['wide'], $result['exact']);
148
+
149
+        return $hasMoreResults;
150
+    }
151
+
152
+    public function takeOutCurrentUser(array &$users) {
153
+        $currentUser = $this->userSession->getUser();
154
+        if(!is_null($currentUser)) {
155
+            if (isset($users[$currentUser->getUID()])) {
156
+                unset($users[$currentUser->getUID()]);
157
+            }
158
+        }
159
+    }
160 160
 }
Please login to merge, or discard this patch.
lib/private/Collaboration/Collaborators/GroupPlugin.php 1 patch
Indentation   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -33,92 +33,92 @@
 block discarded – undo
33 33
 use OCP\Share;
34 34
 
35 35
 class GroupPlugin implements ISearchPlugin {
36
-	protected $shareeEnumeration;
37
-	protected $shareWithGroupOnly;
36
+    protected $shareeEnumeration;
37
+    protected $shareWithGroupOnly;
38 38
 
39
-	/** @var IGroupManager */
40
-	private $groupManager;
41
-	/** @var IConfig */
42
-	private $config;
43
-	/** @var IUserSession */
44
-	private $userSession;
39
+    /** @var IGroupManager */
40
+    private $groupManager;
41
+    /** @var IConfig */
42
+    private $config;
43
+    /** @var IUserSession */
44
+    private $userSession;
45 45
 
46
-	public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
47
-		$this->groupManager = $groupManager;
48
-		$this->config = $config;
49
-		$this->userSession = $userSession;
46
+    public function __construct(IConfig $config, IGroupManager $groupManager, IUserSession $userSession) {
47
+        $this->groupManager = $groupManager;
48
+        $this->config = $config;
49
+        $this->userSession = $userSession;
50 50
 
51
-		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
52
-		$this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
53
-	}
51
+        $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
52
+        $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
53
+    }
54 54
 
55
-	public function search($search, $limit, $offset, ISearchResult $searchResult) {
56
-		$hasMoreResults = false;
57
-		$result = ['wide' => [], 'exact' => []];
55
+    public function search($search, $limit, $offset, ISearchResult $searchResult) {
56
+        $hasMoreResults = false;
57
+        $result = ['wide' => [], 'exact' => []];
58 58
 
59
-		$groups = $this->groupManager->search($search, $limit, $offset);
60
-		$groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
59
+        $groups = $this->groupManager->search($search, $limit, $offset);
60
+        $groupIds = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
61 61
 
62
-		if (!$this->shareeEnumeration || sizeof($groups) < $limit) {
63
-			$hasMoreResults = true;
64
-		}
62
+        if (!$this->shareeEnumeration || sizeof($groups) < $limit) {
63
+            $hasMoreResults = true;
64
+        }
65 65
 
66
-		$userGroups =  [];
67
-		if (!empty($groups) && $this->shareWithGroupOnly) {
68
-			// Intersect all the groups that match with the groups this user is a member of
69
-			$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
70
-			$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
71
-			$groupIds = array_intersect($groupIds, $userGroups);
72
-		}
66
+        $userGroups =  [];
67
+        if (!empty($groups) && $this->shareWithGroupOnly) {
68
+            // Intersect all the groups that match with the groups this user is a member of
69
+            $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
70
+            $userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
71
+            $groupIds = array_intersect($groupIds, $userGroups);
72
+        }
73 73
 
74
-		$lowerSearch = strtolower($search);
75
-		foreach ($groups as $group) {
76
-			// FIXME: use a more efficient approach
77
-			$gid = $group->getGID();
78
-			if (!in_array($gid, $groupIds)) {
79
-				continue;
80
-			}
81
-			if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
82
-				$result['exact'][] = [
83
-					'label' => $group->getDisplayName(),
84
-					'value' => [
85
-						'shareType' => Share::SHARE_TYPE_GROUP,
86
-						'shareWith' => $gid,
87
-					],
88
-				];
89
-			} else {
90
-				$result['wide'][] = [
91
-					'label' => $group->getDisplayName(),
92
-					'value' => [
93
-						'shareType' => Share::SHARE_TYPE_GROUP,
94
-						'shareWith' => $gid,
95
-					],
96
-				];
97
-			}
98
-		}
74
+        $lowerSearch = strtolower($search);
75
+        foreach ($groups as $group) {
76
+            // FIXME: use a more efficient approach
77
+            $gid = $group->getGID();
78
+            if (!in_array($gid, $groupIds)) {
79
+                continue;
80
+            }
81
+            if (strtolower($gid) === $lowerSearch || strtolower($group->getDisplayName()) === $lowerSearch) {
82
+                $result['exact'][] = [
83
+                    'label' => $group->getDisplayName(),
84
+                    'value' => [
85
+                        'shareType' => Share::SHARE_TYPE_GROUP,
86
+                        'shareWith' => $gid,
87
+                    ],
88
+                ];
89
+            } else {
90
+                $result['wide'][] = [
91
+                    'label' => $group->getDisplayName(),
92
+                    'value' => [
93
+                        'shareType' => Share::SHARE_TYPE_GROUP,
94
+                        'shareWith' => $gid,
95
+                    ],
96
+                ];
97
+            }
98
+        }
99 99
 
100
-		if ($offset === 0 && empty($result['exact'])) {
101
-			// On page one we try if the search result has a direct hit on the
102
-			// user id and if so, we add that to the exact match list
103
-			$group = $this->groupManager->get($search);
104
-			if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
105
-				$result['exact'][] = [
106
-					'label' => $group->getDisplayName(),
107
-					'value' => [
108
-						'shareType' => Share::SHARE_TYPE_GROUP,
109
-						'shareWith' => $group->getGID(),
110
-					],
111
-				];
112
-			}
113
-		}
100
+        if ($offset === 0 && empty($result['exact'])) {
101
+            // On page one we try if the search result has a direct hit on the
102
+            // user id and if so, we add that to the exact match list
103
+            $group = $this->groupManager->get($search);
104
+            if ($group instanceof IGroup && (!$this->shareWithGroupOnly || in_array($group->getGID(), $userGroups))) {
105
+                $result['exact'][] = [
106
+                    'label' => $group->getDisplayName(),
107
+                    'value' => [
108
+                        'shareType' => Share::SHARE_TYPE_GROUP,
109
+                        'shareWith' => $group->getGID(),
110
+                    ],
111
+                ];
112
+            }
113
+        }
114 114
 
115
-		if (!$this->shareeEnumeration) {
116
-			$result['wide'] = [];
117
-		}
115
+        if (!$this->shareeEnumeration) {
116
+            $result['wide'] = [];
117
+        }
118 118
 
119
-		$type = new SearchResultType('groups');
120
-		$searchResult->addResultSet($type, $result['wide'], $result['exact']);
119
+        $type = new SearchResultType('groups');
120
+        $searchResult->addResultSet($type, $result['wide'], $result['exact']);
121 121
 
122
-		return $hasMoreResults;
123
-	}
122
+        return $hasMoreResults;
123
+    }
124 124
 }
Please login to merge, or discard this patch.
lib/private/AppFramework/Middleware/MiddlewareDispatcher.php 2 patches
Indentation   +123 added lines, -123 removed lines patch added patch discarded remove patch
@@ -37,128 +37,128 @@
 block discarded – undo
37 37
  */
38 38
 class MiddlewareDispatcher {
39 39
 
40
-	/**
41
-	 * @var array array containing all the middlewares
42
-	 */
43
-	private $middlewares;
44
-
45
-	/**
46
-	 * @var int counter which tells us what middlware was executed once an
47
-	 *                  exception occurs
48
-	 */
49
-	private $middlewareCounter;
50
-
51
-
52
-	/**
53
-	 * Constructor
54
-	 */
55
-	public function __construct(){
56
-		$this->middlewares = array();
57
-		$this->middlewareCounter = 0;
58
-	}
59
-
60
-
61
-	/**
62
-	 * Adds a new middleware
63
-	 * @param Middleware $middleWare the middleware which will be added
64
-	 */
65
-	public function registerMiddleware(Middleware $middleWare){
66
-		$this->middlewares[] = $middleWare;
67
-	}
68
-
69
-
70
-	/**
71
-	 * returns an array with all middleware elements
72
-	 * @return array the middlewares
73
-	 */
74
-	public function getMiddlewares(){
75
-		return $this->middlewares;
76
-	}
77
-
78
-
79
-	/**
80
-	 * This is being run in normal order before the controller is being
81
-	 * called which allows several modifications and checks
82
-	 *
83
-	 * @param Controller $controller the controller that is being called
84
-	 * @param string $methodName the name of the method that will be called on
85
-	 *                           the controller
86
-	 */
87
-	public function beforeController(Controller $controller, $methodName){
88
-		// we need to count so that we know which middlewares we have to ask in
89
-		// case there is an exception
90
-		$middlewareCount = count($this->middlewares);
91
-		for($i = 0; $i < $middlewareCount; $i++){
92
-			$this->middlewareCounter++;
93
-			$middleware = $this->middlewares[$i];
94
-			$middleware->beforeController($controller, $methodName);
95
-		}
96
-	}
97
-
98
-
99
-	/**
100
-	 * This is being run when either the beforeController method or the
101
-	 * controller method itself is throwing an exception. The middleware is asked
102
-	 * in reverse order to handle the exception and to return a response.
103
-	 * If the response is null, it is assumed that the exception could not be
104
-	 * handled and the error will be thrown again
105
-	 *
106
-	 * @param Controller $controller the controller that is being called
107
-	 * @param string $methodName the name of the method that will be called on
108
-	 *                            the controller
109
-	 * @param \Exception $exception the thrown exception
110
-	 * @return Response a Response object if the middleware can handle the
111
-	 * exception
112
-	 * @throws \Exception the passed in exception if it can't handle it
113
-	 */
114
-	public function afterException(Controller $controller, $methodName, \Exception $exception){
115
-		for($i=$this->middlewareCounter-1; $i>=0; $i--){
116
-			$middleware = $this->middlewares[$i];
117
-			try {
118
-				return $middleware->afterException($controller, $methodName, $exception);
119
-			} catch(\Exception $exception){
120
-				continue;
121
-			}
122
-		}
123
-		throw $exception;
124
-	}
125
-
126
-
127
-	/**
128
-	 * This is being run after a successful controllermethod call and allows
129
-	 * the manipulation of a Response object. The middleware is run in reverse order
130
-	 *
131
-	 * @param Controller $controller the controller that is being called
132
-	 * @param string $methodName the name of the method that will be called on
133
-	 *                            the controller
134
-	 * @param Response $response the generated response from the controller
135
-	 * @return Response a Response object
136
-	 */
137
-	public function afterController(Controller $controller, $methodName, Response $response){
138
-		for($i=count($this->middlewares)-1; $i>=0; $i--){
139
-			$middleware = $this->middlewares[$i];
140
-			$response = $middleware->afterController($controller, $methodName, $response);
141
-		}
142
-		return $response;
143
-	}
144
-
145
-
146
-	/**
147
-	 * This is being run after the response object has been rendered and
148
-	 * allows the manipulation of the output. The middleware is run in reverse order
149
-	 *
150
-	 * @param Controller $controller the controller that is being called
151
-	 * @param string $methodName the name of the method that will be called on
152
-	 *                           the controller
153
-	 * @param string $output the generated output from a response
154
-	 * @return string the output that should be printed
155
-	 */
156
-	public function beforeOutput(Controller $controller, $methodName, $output){
157
-		for($i=count($this->middlewares)-1; $i>=0; $i--){
158
-			$middleware = $this->middlewares[$i];
159
-			$output = $middleware->beforeOutput($controller, $methodName, $output);
160
-		}
161
-		return $output;
162
-	}
40
+    /**
41
+     * @var array array containing all the middlewares
42
+     */
43
+    private $middlewares;
44
+
45
+    /**
46
+     * @var int counter which tells us what middlware was executed once an
47
+     *                  exception occurs
48
+     */
49
+    private $middlewareCounter;
50
+
51
+
52
+    /**
53
+     * Constructor
54
+     */
55
+    public function __construct(){
56
+        $this->middlewares = array();
57
+        $this->middlewareCounter = 0;
58
+    }
59
+
60
+
61
+    /**
62
+     * Adds a new middleware
63
+     * @param Middleware $middleWare the middleware which will be added
64
+     */
65
+    public function registerMiddleware(Middleware $middleWare){
66
+        $this->middlewares[] = $middleWare;
67
+    }
68
+
69
+
70
+    /**
71
+     * returns an array with all middleware elements
72
+     * @return array the middlewares
73
+     */
74
+    public function getMiddlewares(){
75
+        return $this->middlewares;
76
+    }
77
+
78
+
79
+    /**
80
+     * This is being run in normal order before the controller is being
81
+     * called which allows several modifications and checks
82
+     *
83
+     * @param Controller $controller the controller that is being called
84
+     * @param string $methodName the name of the method that will be called on
85
+     *                           the controller
86
+     */
87
+    public function beforeController(Controller $controller, $methodName){
88
+        // we need to count so that we know which middlewares we have to ask in
89
+        // case there is an exception
90
+        $middlewareCount = count($this->middlewares);
91
+        for($i = 0; $i < $middlewareCount; $i++){
92
+            $this->middlewareCounter++;
93
+            $middleware = $this->middlewares[$i];
94
+            $middleware->beforeController($controller, $methodName);
95
+        }
96
+    }
97
+
98
+
99
+    /**
100
+     * This is being run when either the beforeController method or the
101
+     * controller method itself is throwing an exception. The middleware is asked
102
+     * in reverse order to handle the exception and to return a response.
103
+     * If the response is null, it is assumed that the exception could not be
104
+     * handled and the error will be thrown again
105
+     *
106
+     * @param Controller $controller the controller that is being called
107
+     * @param string $methodName the name of the method that will be called on
108
+     *                            the controller
109
+     * @param \Exception $exception the thrown exception
110
+     * @return Response a Response object if the middleware can handle the
111
+     * exception
112
+     * @throws \Exception the passed in exception if it can't handle it
113
+     */
114
+    public function afterException(Controller $controller, $methodName, \Exception $exception){
115
+        for($i=$this->middlewareCounter-1; $i>=0; $i--){
116
+            $middleware = $this->middlewares[$i];
117
+            try {
118
+                return $middleware->afterException($controller, $methodName, $exception);
119
+            } catch(\Exception $exception){
120
+                continue;
121
+            }
122
+        }
123
+        throw $exception;
124
+    }
125
+
126
+
127
+    /**
128
+     * This is being run after a successful controllermethod call and allows
129
+     * the manipulation of a Response object. The middleware is run in reverse order
130
+     *
131
+     * @param Controller $controller the controller that is being called
132
+     * @param string $methodName the name of the method that will be called on
133
+     *                            the controller
134
+     * @param Response $response the generated response from the controller
135
+     * @return Response a Response object
136
+     */
137
+    public function afterController(Controller $controller, $methodName, Response $response){
138
+        for($i=count($this->middlewares)-1; $i>=0; $i--){
139
+            $middleware = $this->middlewares[$i];
140
+            $response = $middleware->afterController($controller, $methodName, $response);
141
+        }
142
+        return $response;
143
+    }
144
+
145
+
146
+    /**
147
+     * This is being run after the response object has been rendered and
148
+     * allows the manipulation of the output. The middleware is run in reverse order
149
+     *
150
+     * @param Controller $controller the controller that is being called
151
+     * @param string $methodName the name of the method that will be called on
152
+     *                           the controller
153
+     * @param string $output the generated output from a response
154
+     * @return string the output that should be printed
155
+     */
156
+    public function beforeOutput(Controller $controller, $methodName, $output){
157
+        for($i=count($this->middlewares)-1; $i>=0; $i--){
158
+            $middleware = $this->middlewares[$i];
159
+            $output = $middleware->beforeOutput($controller, $methodName, $output);
160
+        }
161
+        return $output;
162
+    }
163 163
 
164 164
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
 	/**
53 53
 	 * Constructor
54 54
 	 */
55
-	public function __construct(){
55
+	public function __construct() {
56 56
 		$this->middlewares = array();
57 57
 		$this->middlewareCounter = 0;
58 58
 	}
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 	 * Adds a new middleware
63 63
 	 * @param Middleware $middleWare the middleware which will be added
64 64
 	 */
65
-	public function registerMiddleware(Middleware $middleWare){
65
+	public function registerMiddleware(Middleware $middleWare) {
66 66
 		$this->middlewares[] = $middleWare;
67 67
 	}
68 68
 
@@ -71,7 +71,7 @@  discard block
 block discarded – undo
71 71
 	 * returns an array with all middleware elements
72 72
 	 * @return array the middlewares
73 73
 	 */
74
-	public function getMiddlewares(){
74
+	public function getMiddlewares() {
75 75
 		return $this->middlewares;
76 76
 	}
77 77
 
@@ -84,11 +84,11 @@  discard block
 block discarded – undo
84 84
 	 * @param string $methodName the name of the method that will be called on
85 85
 	 *                           the controller
86 86
 	 */
87
-	public function beforeController(Controller $controller, $methodName){
87
+	public function beforeController(Controller $controller, $methodName) {
88 88
 		// we need to count so that we know which middlewares we have to ask in
89 89
 		// case there is an exception
90 90
 		$middlewareCount = count($this->middlewares);
91
-		for($i = 0; $i < $middlewareCount; $i++){
91
+		for ($i = 0; $i < $middlewareCount; $i++) {
92 92
 			$this->middlewareCounter++;
93 93
 			$middleware = $this->middlewares[$i];
94 94
 			$middleware->beforeController($controller, $methodName);
@@ -111,12 +111,12 @@  discard block
 block discarded – undo
111 111
 	 * exception
112 112
 	 * @throws \Exception the passed in exception if it can't handle it
113 113
 	 */
114
-	public function afterException(Controller $controller, $methodName, \Exception $exception){
115
-		for($i=$this->middlewareCounter-1; $i>=0; $i--){
114
+	public function afterException(Controller $controller, $methodName, \Exception $exception) {
115
+		for ($i = $this->middlewareCounter - 1; $i >= 0; $i--) {
116 116
 			$middleware = $this->middlewares[$i];
117 117
 			try {
118 118
 				return $middleware->afterException($controller, $methodName, $exception);
119
-			} catch(\Exception $exception){
119
+			} catch (\Exception $exception) {
120 120
 				continue;
121 121
 			}
122 122
 		}
@@ -134,8 +134,8 @@  discard block
 block discarded – undo
134 134
 	 * @param Response $response the generated response from the controller
135 135
 	 * @return Response a Response object
136 136
 	 */
137
-	public function afterController(Controller $controller, $methodName, Response $response){
138
-		for($i=count($this->middlewares)-1; $i>=0; $i--){
137
+	public function afterController(Controller $controller, $methodName, Response $response) {
138
+		for ($i = count($this->middlewares) - 1; $i >= 0; $i--) {
139 139
 			$middleware = $this->middlewares[$i];
140 140
 			$response = $middleware->afterController($controller, $methodName, $response);
141 141
 		}
@@ -153,8 +153,8 @@  discard block
 block discarded – undo
153 153
 	 * @param string $output the generated output from a response
154 154
 	 * @return string the output that should be printed
155 155
 	 */
156
-	public function beforeOutput(Controller $controller, $methodName, $output){
157
-		for($i=count($this->middlewares)-1; $i>=0; $i--){
156
+	public function beforeOutput(Controller $controller, $methodName, $output) {
157
+		for ($i = count($this->middlewares) - 1; $i >= 0; $i--) {
158 158
 			$middleware = $this->middlewares[$i];
159 159
 			$output = $middleware->beforeOutput($controller, $methodName, $output);
160 160
 		}
Please login to merge, or discard this patch.
lib/private/AppFramework/DependencyInjection/DIContainer.php 1 patch
Indentation   +383 added lines, -383 removed lines patch added patch discarded remove patch
@@ -65,387 +65,387 @@
 block discarded – undo
65 65
 
66 66
 class DIContainer extends SimpleContainer implements IAppContainer {
67 67
 
68
-	/**
69
-	 * @var array
70
-	 */
71
-	private $middleWares = array();
72
-
73
-	/** @var ServerContainer */
74
-	private $server;
75
-
76
-	/**
77
-	 * Put your class dependencies in here
78
-	 * @param string $appName the name of the app
79
-	 * @param array $urlParams
80
-	 * @param ServerContainer|null $server
81
-	 */
82
-	public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
83
-		parent::__construct();
84
-		$this['AppName'] = $appName;
85
-		$this['urlParams'] = $urlParams;
86
-
87
-		/** @var \OC\ServerContainer $server */
88
-		if ($server === null) {
89
-			$server = \OC::$server;
90
-		}
91
-		$this->server = $server;
92
-		$this->server->registerAppContainer($appName, $this);
93
-
94
-		// aliases
95
-		$this->registerAlias('appName', 'AppName');
96
-		$this->registerAlias('webRoot', 'WebRoot');
97
-		$this->registerAlias('userId', 'UserId');
98
-
99
-		/**
100
-		 * Core services
101
-		 */
102
-		$this->registerService(IOutput::class, function($c){
103
-			return new Output($this->getServer()->getWebRoot());
104
-		});
105
-
106
-		$this->registerService(Folder::class, function() {
107
-			return $this->getServer()->getUserFolder();
108
-		});
109
-
110
-		$this->registerService(IAppData::class, function (SimpleContainer $c) {
111
-			return $this->getServer()->getAppDataDir($c->query('AppName'));
112
-		});
113
-
114
-		$this->registerService(IL10N::class, function($c) {
115
-			return $this->getServer()->getL10N($c->query('AppName'));
116
-		});
117
-
118
-		$this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
119
-		$this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
120
-
121
-		$this->registerService(IRequest::class, function() {
122
-			return $this->getServer()->query(IRequest::class);
123
-		});
124
-		$this->registerAlias('Request', IRequest::class);
125
-
126
-		$this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
127
-		$this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
128
-
129
-		$this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
130
-
131
-		$this->registerService(IServerContainer::class, function ($c) {
132
-			return $this->getServer();
133
-		});
134
-		$this->registerAlias('ServerContainer', IServerContainer::class);
135
-
136
-		$this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
137
-			return $c->query('OCA\WorkflowEngine\Manager');
138
-		});
139
-
140
-		$this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
141
-			return $c;
142
-		});
143
-
144
-		// commonly used attributes
145
-		$this->registerService('UserId', function ($c) {
146
-			return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
147
-		});
148
-
149
-		$this->registerService('WebRoot', function ($c) {
150
-			return $c->query('ServerContainer')->getWebRoot();
151
-		});
152
-
153
-		$this->registerService('fromMailAddress', function() {
154
-			return Util::getDefaultEmailAddress('no-reply');
155
-		});
156
-
157
-		$this->registerService('OC_Defaults', function ($c) {
158
-			return $c->getServer()->getThemingDefaults();
159
-		});
160
-
161
-		$this->registerService('OCP\Encryption\IManager', function ($c) {
162
-			return $this->getServer()->getEncryptionManager();
163
-		});
164
-
165
-		$this->registerService(IConfig::class, function ($c) {
166
-			return $c->query(OC\GlobalScale\Config::class);
167
-		});
168
-
169
-		$this->registerService(IValidator::class, function($c) {
170
-			return $c->query(Validator::class);
171
-		});
172
-
173
-		$this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
174
-			return new \OC\Security\IdentityProof\Manager(
175
-				$this->getServer()->query(\OC\Files\AppData\Factory::class),
176
-				$this->getServer()->getCrypto(),
177
-				$this->getServer()->getConfig()
178
-			);
179
-		});
180
-
181
-		$this->registerService('Protocol', function($c){
182
-			/** @var \OC\Server $server */
183
-			$server = $c->query('ServerContainer');
184
-			$protocol = $server->getRequest()->getHttpProtocol();
185
-			return new Http($_SERVER, $protocol);
186
-		});
187
-
188
-		$this->registerService('Dispatcher', function($c) {
189
-			return new Dispatcher(
190
-				$c['Protocol'],
191
-				$c['MiddlewareDispatcher'],
192
-				$c['ControllerMethodReflector'],
193
-				$c['Request']
194
-			);
195
-		});
196
-
197
-		/**
198
-		 * App Framework default arguments
199
-		 */
200
-		$this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
201
-		$this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
202
-		$this->registerParameter('corsMaxAge', 1728000);
203
-
204
-		/**
205
-		 * Middleware
206
-		 */
207
-		$app = $this;
208
-		$this->registerService('SecurityMiddleware', function($c) use ($app){
209
-			/** @var \OC\Server $server */
210
-			$server = $app->getServer();
211
-
212
-			return new SecurityMiddleware(
213
-				$c['Request'],
214
-				$c['ControllerMethodReflector'],
215
-				$server->getNavigationManager(),
216
-				$server->getURLGenerator(),
217
-				$server->getLogger(),
218
-				$c['AppName'],
219
-				$server->getUserSession()->isLoggedIn(),
220
-				$server->getGroupManager()->isAdmin($this->getUserId()),
221
-				$server->getContentSecurityPolicyManager(),
222
-				$server->getCsrfTokenManager(),
223
-				$server->getContentSecurityPolicyNonceManager(),
224
-				$server->getAppManager()
225
-			);
226
-		});
227
-
228
-		$this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function ($c) use ($app) {
229
-			/** @var \OC\Server $server */
230
-			$server = $app->getServer();
231
-
232
-			return new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
233
-				$c['ControllerMethodReflector'],
234
-				$server->getSession(),
235
-				$server->getUserSession(),
236
-				$server->query(ITimeFactory::class)
237
-			);
238
-		});
239
-
240
-		$this->registerService('BruteForceMiddleware', function($c) use ($app) {
241
-			/** @var \OC\Server $server */
242
-			$server = $app->getServer();
243
-
244
-			return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
245
-				$c['ControllerMethodReflector'],
246
-				$server->getBruteForceThrottler(),
247
-				$server->getRequest()
248
-			);
249
-		});
250
-
251
-		$this->registerService('RateLimitingMiddleware', function($c) use ($app) {
252
-			/** @var \OC\Server $server */
253
-			$server = $app->getServer();
254
-
255
-			return new RateLimitingMiddleware(
256
-				$server->getRequest(),
257
-				$server->getUserSession(),
258
-				$c['ControllerMethodReflector'],
259
-				$c->query(OC\Security\RateLimiting\Limiter::class)
260
-			);
261
-		});
262
-
263
-		$this->registerService('CORSMiddleware', function($c) {
264
-			return new CORSMiddleware(
265
-				$c['Request'],
266
-				$c['ControllerMethodReflector'],
267
-				$c->query(IUserSession::class),
268
-				$c->getServer()->getBruteForceThrottler()
269
-			);
270
-		});
271
-
272
-		$this->registerService('SessionMiddleware', function($c) use ($app) {
273
-			return new SessionMiddleware(
274
-				$c['Request'],
275
-				$c['ControllerMethodReflector'],
276
-				$app->getServer()->getSession()
277
-			);
278
-		});
279
-
280
-		$this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
281
-			$twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
282
-			$userSession = $app->getServer()->getUserSession();
283
-			$session = $app->getServer()->getSession();
284
-			$urlGenerator = $app->getServer()->getURLGenerator();
285
-			$reflector = $c['ControllerMethodReflector'];
286
-			$request = $app->getServer()->getRequest();
287
-			return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
288
-		});
289
-
290
-		$this->registerService('OCSMiddleware', function (SimpleContainer $c) {
291
-			return new OCSMiddleware(
292
-				$c['Request']
293
-			);
294
-		});
295
-
296
-		$this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function (SimpleContainer $c) {
297
-			return new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
298
-				$c['Request'],
299
-				$c['ControllerMethodReflector']
300
-			);
301
-		});
302
-
303
-		$middleWares = &$this->middleWares;
304
-		$this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
305
-			$dispatcher = new MiddlewareDispatcher();
306
-			$dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class]);
307
-			$dispatcher->registerMiddleware($c['CORSMiddleware']);
308
-			$dispatcher->registerMiddleware($c['OCSMiddleware']);
309
-			$dispatcher->registerMiddleware($c['SecurityMiddleware']);
310
-			$dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class]);
311
-			$dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
312
-			$dispatcher->registerMiddleware($c['BruteForceMiddleware']);
313
-			$dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
314
-
315
-			foreach($middleWares as $middleWare) {
316
-				$dispatcher->registerMiddleware($c[$middleWare]);
317
-			}
318
-
319
-			$dispatcher->registerMiddleware($c['SessionMiddleware']);
320
-			return $dispatcher;
321
-		});
322
-
323
-	}
324
-
325
-	/**
326
-	 * @return \OCP\IServerContainer
327
-	 */
328
-	public function getServer()
329
-	{
330
-		return $this->server;
331
-	}
332
-
333
-	/**
334
-	 * @param string $middleWare
335
-	 * @return boolean|null
336
-	 */
337
-	public function registerMiddleWare($middleWare) {
338
-		$this->middleWares[] = $middleWare;
339
-	}
340
-
341
-	/**
342
-	 * used to return the appname of the set application
343
-	 * @return string the name of your application
344
-	 */
345
-	public function getAppName() {
346
-		return $this->query('AppName');
347
-	}
348
-
349
-	/**
350
-	 * @deprecated use IUserSession->isLoggedIn()
351
-	 * @return boolean
352
-	 */
353
-	public function isLoggedIn() {
354
-		return \OC::$server->getUserSession()->isLoggedIn();
355
-	}
356
-
357
-	/**
358
-	 * @deprecated use IGroupManager->isAdmin($userId)
359
-	 * @return boolean
360
-	 */
361
-	public function isAdminUser() {
362
-		$uid = $this->getUserId();
363
-		return \OC_User::isAdminUser($uid);
364
-	}
365
-
366
-	private function getUserId() {
367
-		return $this->getServer()->getSession()->get('user_id');
368
-	}
369
-
370
-	/**
371
-	 * @deprecated use the ILogger instead
372
-	 * @param string $message
373
-	 * @param string $level
374
-	 * @return mixed
375
-	 */
376
-	public function log($message, $level) {
377
-		switch($level){
378
-			case 'debug':
379
-				$level = \OCP\Util::DEBUG;
380
-				break;
381
-			case 'info':
382
-				$level = \OCP\Util::INFO;
383
-				break;
384
-			case 'warn':
385
-				$level = \OCP\Util::WARN;
386
-				break;
387
-			case 'fatal':
388
-				$level = \OCP\Util::FATAL;
389
-				break;
390
-			default:
391
-				$level = \OCP\Util::ERROR;
392
-				break;
393
-		}
394
-		\OCP\Util::writeLog($this->getAppName(), $message, $level);
395
-	}
396
-
397
-	/**
398
-	 * Register a capability
399
-	 *
400
-	 * @param string $serviceName e.g. 'OCA\Files\Capabilities'
401
-	 */
402
-	public function registerCapability($serviceName) {
403
-		$this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
404
-			return $this->query($serviceName);
405
-		});
406
-	}
407
-
408
-	/**
409
-	 * @param string $name
410
-	 * @return mixed
411
-	 * @throws QueryException if the query could not be resolved
412
-	 */
413
-	public function query($name) {
414
-		try {
415
-			return $this->queryNoFallback($name);
416
-		} catch (QueryException $firstException) {
417
-			try {
418
-				return $this->getServer()->query($name);
419
-			} catch (QueryException $secondException) {
420
-				if ($firstException->getCode() === 1) {
421
-					throw $secondException;
422
-				}
423
-				throw $firstException;
424
-			}
425
-		}
426
-	}
427
-
428
-	/**
429
-	 * @param string $name
430
-	 * @return mixed
431
-	 * @throws QueryException if the query could not be resolved
432
-	 */
433
-	public function queryNoFallback($name) {
434
-		$name = $this->sanitizeName($name);
435
-
436
-		if ($this->offsetExists($name)) {
437
-			return parent::query($name);
438
-		} else {
439
-			if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
440
-				return parent::query($name);
441
-			} else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
442
-				return parent::query($name);
443
-			} else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
444
-				return parent::query($name);
445
-			}
446
-		}
447
-
448
-		throw new QueryException('Could not resolve ' . $name . '!' .
449
-			' Class can not be instantiated', 1);
450
-	}
68
+    /**
69
+     * @var array
70
+     */
71
+    private $middleWares = array();
72
+
73
+    /** @var ServerContainer */
74
+    private $server;
75
+
76
+    /**
77
+     * Put your class dependencies in here
78
+     * @param string $appName the name of the app
79
+     * @param array $urlParams
80
+     * @param ServerContainer|null $server
81
+     */
82
+    public function __construct($appName, $urlParams = array(), ServerContainer $server = null){
83
+        parent::__construct();
84
+        $this['AppName'] = $appName;
85
+        $this['urlParams'] = $urlParams;
86
+
87
+        /** @var \OC\ServerContainer $server */
88
+        if ($server === null) {
89
+            $server = \OC::$server;
90
+        }
91
+        $this->server = $server;
92
+        $this->server->registerAppContainer($appName, $this);
93
+
94
+        // aliases
95
+        $this->registerAlias('appName', 'AppName');
96
+        $this->registerAlias('webRoot', 'WebRoot');
97
+        $this->registerAlias('userId', 'UserId');
98
+
99
+        /**
100
+         * Core services
101
+         */
102
+        $this->registerService(IOutput::class, function($c){
103
+            return new Output($this->getServer()->getWebRoot());
104
+        });
105
+
106
+        $this->registerService(Folder::class, function() {
107
+            return $this->getServer()->getUserFolder();
108
+        });
109
+
110
+        $this->registerService(IAppData::class, function (SimpleContainer $c) {
111
+            return $this->getServer()->getAppDataDir($c->query('AppName'));
112
+        });
113
+
114
+        $this->registerService(IL10N::class, function($c) {
115
+            return $this->getServer()->getL10N($c->query('AppName'));
116
+        });
117
+
118
+        $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
119
+        $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
120
+
121
+        $this->registerService(IRequest::class, function() {
122
+            return $this->getServer()->query(IRequest::class);
123
+        });
124
+        $this->registerAlias('Request', IRequest::class);
125
+
126
+        $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
127
+        $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
128
+
129
+        $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class);
130
+
131
+        $this->registerService(IServerContainer::class, function ($c) {
132
+            return $this->getServer();
133
+        });
134
+        $this->registerAlias('ServerContainer', IServerContainer::class);
135
+
136
+        $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) {
137
+            return $c->query('OCA\WorkflowEngine\Manager');
138
+        });
139
+
140
+        $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) {
141
+            return $c;
142
+        });
143
+
144
+        // commonly used attributes
145
+        $this->registerService('UserId', function ($c) {
146
+            return $c->query('OCP\\IUserSession')->getSession()->get('user_id');
147
+        });
148
+
149
+        $this->registerService('WebRoot', function ($c) {
150
+            return $c->query('ServerContainer')->getWebRoot();
151
+        });
152
+
153
+        $this->registerService('fromMailAddress', function() {
154
+            return Util::getDefaultEmailAddress('no-reply');
155
+        });
156
+
157
+        $this->registerService('OC_Defaults', function ($c) {
158
+            return $c->getServer()->getThemingDefaults();
159
+        });
160
+
161
+        $this->registerService('OCP\Encryption\IManager', function ($c) {
162
+            return $this->getServer()->getEncryptionManager();
163
+        });
164
+
165
+        $this->registerService(IConfig::class, function ($c) {
166
+            return $c->query(OC\GlobalScale\Config::class);
167
+        });
168
+
169
+        $this->registerService(IValidator::class, function($c) {
170
+            return $c->query(Validator::class);
171
+        });
172
+
173
+        $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) {
174
+            return new \OC\Security\IdentityProof\Manager(
175
+                $this->getServer()->query(\OC\Files\AppData\Factory::class),
176
+                $this->getServer()->getCrypto(),
177
+                $this->getServer()->getConfig()
178
+            );
179
+        });
180
+
181
+        $this->registerService('Protocol', function($c){
182
+            /** @var \OC\Server $server */
183
+            $server = $c->query('ServerContainer');
184
+            $protocol = $server->getRequest()->getHttpProtocol();
185
+            return new Http($_SERVER, $protocol);
186
+        });
187
+
188
+        $this->registerService('Dispatcher', function($c) {
189
+            return new Dispatcher(
190
+                $c['Protocol'],
191
+                $c['MiddlewareDispatcher'],
192
+                $c['ControllerMethodReflector'],
193
+                $c['Request']
194
+            );
195
+        });
196
+
197
+        /**
198
+         * App Framework default arguments
199
+         */
200
+        $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH');
201
+        $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept');
202
+        $this->registerParameter('corsMaxAge', 1728000);
203
+
204
+        /**
205
+         * Middleware
206
+         */
207
+        $app = $this;
208
+        $this->registerService('SecurityMiddleware', function($c) use ($app){
209
+            /** @var \OC\Server $server */
210
+            $server = $app->getServer();
211
+
212
+            return new SecurityMiddleware(
213
+                $c['Request'],
214
+                $c['ControllerMethodReflector'],
215
+                $server->getNavigationManager(),
216
+                $server->getURLGenerator(),
217
+                $server->getLogger(),
218
+                $c['AppName'],
219
+                $server->getUserSession()->isLoggedIn(),
220
+                $server->getGroupManager()->isAdmin($this->getUserId()),
221
+                $server->getContentSecurityPolicyManager(),
222
+                $server->getCsrfTokenManager(),
223
+                $server->getContentSecurityPolicyNonceManager(),
224
+                $server->getAppManager()
225
+            );
226
+        });
227
+
228
+        $this->registerService(OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class, function ($c) use ($app) {
229
+            /** @var \OC\Server $server */
230
+            $server = $app->getServer();
231
+
232
+            return new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware(
233
+                $c['ControllerMethodReflector'],
234
+                $server->getSession(),
235
+                $server->getUserSession(),
236
+                $server->query(ITimeFactory::class)
237
+            );
238
+        });
239
+
240
+        $this->registerService('BruteForceMiddleware', function($c) use ($app) {
241
+            /** @var \OC\Server $server */
242
+            $server = $app->getServer();
243
+
244
+            return new OC\AppFramework\Middleware\Security\BruteForceMiddleware(
245
+                $c['ControllerMethodReflector'],
246
+                $server->getBruteForceThrottler(),
247
+                $server->getRequest()
248
+            );
249
+        });
250
+
251
+        $this->registerService('RateLimitingMiddleware', function($c) use ($app) {
252
+            /** @var \OC\Server $server */
253
+            $server = $app->getServer();
254
+
255
+            return new RateLimitingMiddleware(
256
+                $server->getRequest(),
257
+                $server->getUserSession(),
258
+                $c['ControllerMethodReflector'],
259
+                $c->query(OC\Security\RateLimiting\Limiter::class)
260
+            );
261
+        });
262
+
263
+        $this->registerService('CORSMiddleware', function($c) {
264
+            return new CORSMiddleware(
265
+                $c['Request'],
266
+                $c['ControllerMethodReflector'],
267
+                $c->query(IUserSession::class),
268
+                $c->getServer()->getBruteForceThrottler()
269
+            );
270
+        });
271
+
272
+        $this->registerService('SessionMiddleware', function($c) use ($app) {
273
+            return new SessionMiddleware(
274
+                $c['Request'],
275
+                $c['ControllerMethodReflector'],
276
+                $app->getServer()->getSession()
277
+            );
278
+        });
279
+
280
+        $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) {
281
+            $twoFactorManager = $c->getServer()->getTwoFactorAuthManager();
282
+            $userSession = $app->getServer()->getUserSession();
283
+            $session = $app->getServer()->getSession();
284
+            $urlGenerator = $app->getServer()->getURLGenerator();
285
+            $reflector = $c['ControllerMethodReflector'];
286
+            $request = $app->getServer()->getRequest();
287
+            return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request);
288
+        });
289
+
290
+        $this->registerService('OCSMiddleware', function (SimpleContainer $c) {
291
+            return new OCSMiddleware(
292
+                $c['Request']
293
+            );
294
+        });
295
+
296
+        $this->registerService(OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class, function (SimpleContainer $c) {
297
+            return new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware(
298
+                $c['Request'],
299
+                $c['ControllerMethodReflector']
300
+            );
301
+        });
302
+
303
+        $middleWares = &$this->middleWares;
304
+        $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) {
305
+            $dispatcher = new MiddlewareDispatcher();
306
+            $dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware::class]);
307
+            $dispatcher->registerMiddleware($c['CORSMiddleware']);
308
+            $dispatcher->registerMiddleware($c['OCSMiddleware']);
309
+            $dispatcher->registerMiddleware($c['SecurityMiddleware']);
310
+            $dispatcher->registerMiddleware($c[OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware::class]);
311
+            $dispatcher->registerMiddleware($c['TwoFactorMiddleware']);
312
+            $dispatcher->registerMiddleware($c['BruteForceMiddleware']);
313
+            $dispatcher->registerMiddleware($c['RateLimitingMiddleware']);
314
+
315
+            foreach($middleWares as $middleWare) {
316
+                $dispatcher->registerMiddleware($c[$middleWare]);
317
+            }
318
+
319
+            $dispatcher->registerMiddleware($c['SessionMiddleware']);
320
+            return $dispatcher;
321
+        });
322
+
323
+    }
324
+
325
+    /**
326
+     * @return \OCP\IServerContainer
327
+     */
328
+    public function getServer()
329
+    {
330
+        return $this->server;
331
+    }
332
+
333
+    /**
334
+     * @param string $middleWare
335
+     * @return boolean|null
336
+     */
337
+    public function registerMiddleWare($middleWare) {
338
+        $this->middleWares[] = $middleWare;
339
+    }
340
+
341
+    /**
342
+     * used to return the appname of the set application
343
+     * @return string the name of your application
344
+     */
345
+    public function getAppName() {
346
+        return $this->query('AppName');
347
+    }
348
+
349
+    /**
350
+     * @deprecated use IUserSession->isLoggedIn()
351
+     * @return boolean
352
+     */
353
+    public function isLoggedIn() {
354
+        return \OC::$server->getUserSession()->isLoggedIn();
355
+    }
356
+
357
+    /**
358
+     * @deprecated use IGroupManager->isAdmin($userId)
359
+     * @return boolean
360
+     */
361
+    public function isAdminUser() {
362
+        $uid = $this->getUserId();
363
+        return \OC_User::isAdminUser($uid);
364
+    }
365
+
366
+    private function getUserId() {
367
+        return $this->getServer()->getSession()->get('user_id');
368
+    }
369
+
370
+    /**
371
+     * @deprecated use the ILogger instead
372
+     * @param string $message
373
+     * @param string $level
374
+     * @return mixed
375
+     */
376
+    public function log($message, $level) {
377
+        switch($level){
378
+            case 'debug':
379
+                $level = \OCP\Util::DEBUG;
380
+                break;
381
+            case 'info':
382
+                $level = \OCP\Util::INFO;
383
+                break;
384
+            case 'warn':
385
+                $level = \OCP\Util::WARN;
386
+                break;
387
+            case 'fatal':
388
+                $level = \OCP\Util::FATAL;
389
+                break;
390
+            default:
391
+                $level = \OCP\Util::ERROR;
392
+                break;
393
+        }
394
+        \OCP\Util::writeLog($this->getAppName(), $message, $level);
395
+    }
396
+
397
+    /**
398
+     * Register a capability
399
+     *
400
+     * @param string $serviceName e.g. 'OCA\Files\Capabilities'
401
+     */
402
+    public function registerCapability($serviceName) {
403
+        $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) {
404
+            return $this->query($serviceName);
405
+        });
406
+    }
407
+
408
+    /**
409
+     * @param string $name
410
+     * @return mixed
411
+     * @throws QueryException if the query could not be resolved
412
+     */
413
+    public function query($name) {
414
+        try {
415
+            return $this->queryNoFallback($name);
416
+        } catch (QueryException $firstException) {
417
+            try {
418
+                return $this->getServer()->query($name);
419
+            } catch (QueryException $secondException) {
420
+                if ($firstException->getCode() === 1) {
421
+                    throw $secondException;
422
+                }
423
+                throw $firstException;
424
+            }
425
+        }
426
+    }
427
+
428
+    /**
429
+     * @param string $name
430
+     * @return mixed
431
+     * @throws QueryException if the query could not be resolved
432
+     */
433
+    public function queryNoFallback($name) {
434
+        $name = $this->sanitizeName($name);
435
+
436
+        if ($this->offsetExists($name)) {
437
+            return parent::query($name);
438
+        } else {
439
+            if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) {
440
+                return parent::query($name);
441
+            } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) {
442
+                return parent::query($name);
443
+            } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) {
444
+                return parent::query($name);
445
+            }
446
+        }
447
+
448
+        throw new QueryException('Could not resolve ' . $name . '!' .
449
+            ' Class can not be instantiated', 1);
450
+    }
451 451
 }
Please login to merge, or discard this patch.
lib/private/Activity/Manager.php 1 patch
Indentation   +684 added lines, -684 removed lines patch added patch discarded remove patch
@@ -40,688 +40,688 @@
 block discarded – undo
40 40
 use OCP\RichObjectStrings\IValidator;
41 41
 
42 42
 class Manager implements IManager {
43
-	/** @var IRequest */
44
-	protected $request;
45
-
46
-	/** @var IUserSession */
47
-	protected $session;
48
-
49
-	/** @var IConfig */
50
-	protected $config;
51
-
52
-	/** @var IValidator */
53
-	protected $validator;
54
-
55
-	/** @var string */
56
-	protected $formattingObjectType;
57
-
58
-	/** @var int */
59
-	protected $formattingObjectId;
60
-
61
-	/** @var bool */
62
-	protected $requirePNG;
63
-
64
-	/** @var string */
65
-	protected $currentUserId;
66
-
67
-	/**
68
-	 * constructor of the controller
69
-	 *
70
-	 * @param IRequest $request
71
-	 * @param IUserSession $session
72
-	 * @param IConfig $config
73
-	 * @param IValidator $validator
74
-	 */
75
-	public function __construct(IRequest $request,
76
-								IUserSession $session,
77
-								IConfig $config,
78
-								IValidator $validator) {
79
-		$this->request = $request;
80
-		$this->session = $session;
81
-		$this->config = $config;
82
-		$this->validator = $validator;
83
-	}
84
-
85
-	/** @var \Closure[] */
86
-	private $consumersClosures = array();
87
-
88
-	/** @var IConsumer[] */
89
-	private $consumers = array();
90
-
91
-	/** @var \Closure[] */
92
-	private $extensionsClosures = array();
93
-
94
-	/** @var IExtension[] */
95
-	private $extensions = array();
96
-
97
-	/** @var array list of filters "name" => "is valid" */
98
-	protected $validFilters = array(
99
-		'all'	=> true,
100
-		'by'	=> true,
101
-		'self'	=> true,
102
-	);
103
-
104
-	/** @var array list of type icons "type" => "css class" */
105
-	protected $typeIcons = array();
106
-
107
-	/** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */
108
-	protected $specialParameters = array();
109
-
110
-	/**
111
-	 * @return \OCP\Activity\IConsumer[]
112
-	 */
113
-	protected function getConsumers() {
114
-		if (!empty($this->consumers)) {
115
-			return $this->consumers;
116
-		}
117
-
118
-		$this->consumers = [];
119
-		foreach($this->consumersClosures as $consumer) {
120
-			$c = $consumer();
121
-			if ($c instanceof IConsumer) {
122
-				$this->consumers[] = $c;
123
-			} else {
124
-				throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
125
-			}
126
-		}
127
-
128
-		return $this->consumers;
129
-	}
130
-
131
-	/**
132
-	 * @return \OCP\Activity\IExtension[]
133
-	 */
134
-	protected function getExtensions() {
135
-		if (!empty($this->extensions)) {
136
-			return $this->extensions;
137
-		}
138
-
139
-		$this->extensions = [];
140
-		foreach($this->extensionsClosures as $extension) {
141
-			$e = $extension();
142
-			if ($e instanceof IExtension) {
143
-				$this->extensions[] = $e;
144
-			} else {
145
-				throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface');
146
-			}
147
-		}
148
-
149
-		return $this->extensions;
150
-	}
151
-
152
-	/**
153
-	 * Generates a new IEvent object
154
-	 *
155
-	 * Make sure to call at least the following methods before sending it to the
156
-	 * app with via the publish() method:
157
-	 *  - setApp()
158
-	 *  - setType()
159
-	 *  - setAffectedUser()
160
-	 *  - setSubject()
161
-	 *
162
-	 * @return IEvent
163
-	 */
164
-	public function generateEvent() {
165
-		return new Event($this->validator);
166
-	}
167
-
168
-	/**
169
-	 * Publish an event to the activity consumers
170
-	 *
171
-	 * Make sure to call at least the following methods before sending an Event:
172
-	 *  - setApp()
173
-	 *  - setType()
174
-	 *  - setAffectedUser()
175
-	 *  - setSubject()
176
-	 *
177
-	 * @param IEvent $event
178
-	 * @throws \BadMethodCallException if required values have not been set
179
-	 */
180
-	public function publish(IEvent $event) {
181
-		if ($event->getAuthor() === '') {
182
-			if ($this->session->getUser() instanceof IUser) {
183
-				$event->setAuthor($this->session->getUser()->getUID());
184
-			}
185
-		}
186
-
187
-		if (!$event->getTimestamp()) {
188
-			$event->setTimestamp(time());
189
-		}
190
-
191
-		if (!$event->isValid()) {
192
-			throw new \BadMethodCallException('The given event is invalid');
193
-		}
194
-
195
-		foreach ($this->getConsumers() as $c) {
196
-			$c->receive($event);
197
-		}
198
-	}
199
-
200
-	/**
201
-	 * @param string $app           The app where this event is associated with
202
-	 * @param string $subject       A short description of the event
203
-	 * @param array  $subjectParams Array with parameters that are filled in the subject
204
-	 * @param string $message       A longer description of the event
205
-	 * @param array  $messageParams Array with parameters that are filled in the message
206
-	 * @param string $file          The file including path where this event is associated with
207
-	 * @param string $link          A link where this event is associated with
208
-	 * @param string $affectedUser  Recipient of the activity
209
-	 * @param string $type          Type of the notification
210
-	 * @param int    $priority      Priority of the notification
211
-	 */
212
-	public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
213
-		$event = $this->generateEvent();
214
-		$event->setApp($app)
215
-			->setType($type)
216
-			->setAffectedUser($affectedUser)
217
-			->setSubject($subject, $subjectParams)
218
-			->setMessage($message, $messageParams)
219
-			->setObject('', 0, $file)
220
-			->setLink($link);
221
-
222
-		$this->publish($event);
223
-	}
224
-
225
-	/**
226
-	 * In order to improve lazy loading a closure can be registered which will be called in case
227
-	 * activity consumers are actually requested
228
-	 *
229
-	 * $callable has to return an instance of OCA\Activity\IConsumer
230
-	 *
231
-	 * @param \Closure $callable
232
-	 */
233
-	public function registerConsumer(\Closure $callable) {
234
-		$this->consumersClosures[] = $callable;
235
-		$this->consumers = [];
236
-	}
237
-
238
-	/**
239
-	 * In order to improve lazy loading a closure can be registered which will be called in case
240
-	 * activity consumers are actually requested
241
-	 *
242
-	 * $callable has to return an instance of OCA\Activity\IExtension
243
-	 *
244
-	 * @param \Closure $callable
245
-	 */
246
-	public function registerExtension(\Closure $callable) {
247
-		$this->extensionsClosures[] = $callable;
248
-		$this->extensions = [];
249
-	}
250
-
251
-	/** @var string[] */
252
-	protected $filterClasses = [];
253
-
254
-	/** @var IFilter[] */
255
-	protected $filters = [];
256
-
257
-	/** @var bool */
258
-	protected $loadedLegacyFilters = false;
259
-
260
-	/**
261
-	 * @param string $filter Class must implement OCA\Activity\IFilter
262
-	 * @return void
263
-	 */
264
-	public function registerFilter($filter) {
265
-		$this->filterClasses[$filter] = false;
266
-	}
267
-
268
-	/**
269
-	 * @return IFilter[]
270
-	 * @throws \InvalidArgumentException
271
-	 */
272
-	public function getFilters() {
273
-		if (!$this->loadedLegacyFilters) {
274
-			$legacyFilters = $this->getNavigation();
275
-
276
-			foreach ($legacyFilters['top'] as $filter => $data) {
277
-				$this->filters[$filter] = new LegacyFilter(
278
-					$this, $filter, $data['name'], true
279
-				);
280
-			}
281
-
282
-			foreach ($legacyFilters['apps'] as $filter => $data) {
283
-				$this->filters[$filter] = new LegacyFilter(
284
-					$this, $filter, $data['name'], false
285
-				);
286
-			}
287
-			$this->loadedLegacyFilters = true;
288
-		}
289
-
290
-		foreach ($this->filterClasses as $class => $false) {
291
-			/** @var IFilter $filter */
292
-			$filter = \OC::$server->query($class);
293
-
294
-			if (!$filter instanceof IFilter) {
295
-				throw new \InvalidArgumentException('Invalid activity filter registered');
296
-			}
297
-
298
-			$this->filters[$filter->getIdentifier()] = $filter;
299
-
300
-			unset($this->filterClasses[$class]);
301
-		}
302
-		return $this->filters;
303
-	}
304
-
305
-	/**
306
-	 * @param string $id
307
-	 * @return IFilter
308
-	 * @throws \InvalidArgumentException when the filter was not found
309
-	 * @since 11.0.0
310
-	 */
311
-	public function getFilterById($id) {
312
-		$filters = $this->getFilters();
313
-
314
-		if (isset($filters[$id])) {
315
-			return $filters[$id];
316
-		}
317
-
318
-		throw new \InvalidArgumentException('Requested filter does not exist');
319
-	}
320
-
321
-	/** @var string[] */
322
-	protected $providerClasses = [];
323
-
324
-	/** @var IProvider[] */
325
-	protected $providers = [];
326
-
327
-	/**
328
-	 * @param string $provider Class must implement OCA\Activity\IProvider
329
-	 * @return void
330
-	 */
331
-	public function registerProvider($provider) {
332
-		$this->providerClasses[$provider] = false;
333
-	}
334
-
335
-	/**
336
-	 * @return IProvider[]
337
-	 * @throws \InvalidArgumentException
338
-	 */
339
-	public function getProviders() {
340
-		foreach ($this->providerClasses as $class => $false) {
341
-			/** @var IProvider $provider */
342
-			$provider = \OC::$server->query($class);
343
-
344
-			if (!$provider instanceof IProvider) {
345
-				throw new \InvalidArgumentException('Invalid activity provider registered');
346
-			}
347
-
348
-			$this->providers[] = $provider;
349
-
350
-			unset($this->providerClasses[$class]);
351
-		}
352
-		return $this->providers;
353
-	}
354
-
355
-	/** @var string[] */
356
-	protected $settingsClasses = [];
357
-
358
-	/** @var ISetting[] */
359
-	protected $settings = [];
360
-
361
-	/** @var bool */
362
-	protected $loadedLegacyTypes = false;
363
-
364
-	/**
365
-	 * @param string $setting Class must implement OCA\Activity\ISetting
366
-	 * @return void
367
-	 */
368
-	public function registerSetting($setting) {
369
-		$this->settingsClasses[$setting] = false;
370
-	}
371
-
372
-	/**
373
-	 * @return ISetting[]
374
-	 * @throws \InvalidArgumentException
375
-	 */
376
-	public function getSettings() {
377
-		if (!$this->loadedLegacyTypes) {
378
-			$l = \OC::$server->getL10N('core');
379
-			$legacyTypes = $this->getNotificationTypes($l->getLanguageCode());
380
-			$streamTypes = $this->getDefaultTypes(IExtension::METHOD_STREAM);
381
-			$mailTypes = $this->getDefaultTypes(IExtension::METHOD_MAIL);
382
-			foreach ($legacyTypes as $type => $data) {
383
-				if (is_string($data)) {
384
-					$desc = $data;
385
-					$canChangeStream = true;
386
-					$canChangeMail = true;
387
-				} else {
388
-					$desc = $data['desc'];
389
-					$canChangeStream = in_array(IExtension::METHOD_STREAM, $data['methods']);
390
-					$canChangeMail = in_array(IExtension::METHOD_MAIL, $data['methods']);
391
-				}
392
-
393
-				$this->settings[$type] = new LegacySetting(
394
-					$type, $desc,
395
-					$canChangeStream, in_array($type, $streamTypes),
396
-					$canChangeMail, in_array($type, $mailTypes)
397
-				);
398
-			}
399
-			$this->loadedLegacyTypes = true;
400
-		}
401
-
402
-		foreach ($this->settingsClasses as $class => $false) {
403
-			/** @var ISetting $setting */
404
-			$setting = \OC::$server->query($class);
405
-
406
-			if (!$setting instanceof ISetting) {
407
-				throw new \InvalidArgumentException('Invalid activity filter registered');
408
-			}
409
-
410
-			$this->settings[$setting->getIdentifier()] = $setting;
411
-
412
-			unset($this->settingsClasses[$class]);
413
-		}
414
-		return $this->settings;
415
-	}
416
-
417
-	/**
418
-	 * @param string $id
419
-	 * @return ISetting
420
-	 * @throws \InvalidArgumentException when the setting was not found
421
-	 * @since 11.0.0
422
-	 */
423
-	public function getSettingById($id) {
424
-		$settings = $this->getSettings();
425
-
426
-		if (isset($settings[$id])) {
427
-			return $settings[$id];
428
-		}
429
-
430
-		throw new \InvalidArgumentException('Requested setting does not exist');
431
-	}
432
-
433
-	/**
434
-	 * @param string $type
435
-	 * @return string
436
-	 */
437
-	public function getTypeIcon($type) {
438
-		if (isset($this->typeIcons[$type])) {
439
-			return $this->typeIcons[$type];
440
-		}
441
-
442
-		foreach ($this->getExtensions() as $c) {
443
-			$icon = $c->getTypeIcon($type);
444
-			if (is_string($icon)) {
445
-				$this->typeIcons[$type] = $icon;
446
-				return $icon;
447
-			}
448
-		}
449
-
450
-		$this->typeIcons[$type] = '';
451
-		return '';
452
-	}
453
-
454
-	/**
455
-	 * @param string $type
456
-	 * @param string $id
457
-	 */
458
-	public function setFormattingObject($type, $id) {
459
-		$this->formattingObjectType = $type;
460
-		$this->formattingObjectId = (string) $id;
461
-	}
462
-
463
-	/**
464
-	 * @return bool
465
-	 */
466
-	public function isFormattingFilteredObject() {
467
-		return $this->formattingObjectType !== null && $this->formattingObjectId !== null
468
-			&& $this->formattingObjectType === $this->request->getParam('object_type')
469
-			&& $this->formattingObjectId === $this->request->getParam('object_id');
470
-	}
471
-
472
-	/**
473
-	 * @param bool $status Set to true, when parsing events should not use SVG icons
474
-	 */
475
-	public function setRequirePNG($status) {
476
-		$this->requirePNG = $status;
477
-	}
478
-
479
-	/**
480
-	 * @return bool
481
-	 */
482
-	public function getRequirePNG() {
483
-		return $this->requirePNG;
484
-	}
485
-
486
-	/**
487
-	 * @param string $app
488
-	 * @param string $text
489
-	 * @param array $params
490
-	 * @param boolean $stripPath
491
-	 * @param boolean $highlightParams
492
-	 * @param string $languageCode
493
-	 * @return string|false
494
-	 */
495
-	public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
496
-		foreach ($this->getExtensions() as $c) {
497
-			$translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
498
-			if (is_string($translation)) {
499
-				return $translation;
500
-			}
501
-		}
502
-
503
-		return false;
504
-	}
505
-
506
-	/**
507
-	 * @param string $app
508
-	 * @param string $text
509
-	 * @return array|false
510
-	 */
511
-	public function getSpecialParameterList($app, $text) {
512
-		if (isset($this->specialParameters[$app][$text])) {
513
-			return $this->specialParameters[$app][$text];
514
-		}
515
-
516
-		if (!isset($this->specialParameters[$app])) {
517
-			$this->specialParameters[$app] = array();
518
-		}
519
-
520
-		foreach ($this->getExtensions() as $c) {
521
-			$specialParameter = $c->getSpecialParameterList($app, $text);
522
-			if (is_array($specialParameter)) {
523
-				$this->specialParameters[$app][$text] = $specialParameter;
524
-				return $specialParameter;
525
-			}
526
-		}
527
-
528
-		$this->specialParameters[$app][$text] = false;
529
-		return false;
530
-	}
531
-
532
-	/**
533
-	 * @param array $activity
534
-	 * @return integer|false
535
-	 */
536
-	public function getGroupParameter($activity) {
537
-		foreach ($this->getExtensions() as $c) {
538
-			$parameter = $c->getGroupParameter($activity);
539
-			if ($parameter !== false) {
540
-				return $parameter;
541
-			}
542
-		}
543
-
544
-		return false;
545
-	}
546
-
547
-	/**
548
-	 * Set the user we need to use
549
-	 *
550
-	 * @param string|null $currentUserId
551
-	 * @throws \UnexpectedValueException If the user is invalid
552
-	 */
553
-	public function setCurrentUserId($currentUserId) {
554
-		if (!is_string($currentUserId) && $currentUserId !== null) {
555
-			throw new \UnexpectedValueException('The given current user is invalid');
556
-		}
557
-		$this->currentUserId = $currentUserId;
558
-	}
559
-
560
-	/**
561
-	 * Get the user we need to use
562
-	 *
563
-	 * Either the user is logged in, or we try to get it from the token
564
-	 *
565
-	 * @return string
566
-	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
567
-	 */
568
-	public function getCurrentUserId() {
569
-		if ($this->currentUserId !== null) {
570
-			return $this->currentUserId;
571
-		} else if (!$this->session->isLoggedIn()) {
572
-			return $this->getUserFromToken();
573
-		} else {
574
-			return $this->session->getUser()->getUID();
575
-		}
576
-	}
577
-
578
-	/**
579
-	 * Get the user for the token
580
-	 *
581
-	 * @return string
582
-	 * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
583
-	 */
584
-	protected function getUserFromToken() {
585
-		$token = (string) $this->request->getParam('token', '');
586
-		if (strlen($token) !== 30) {
587
-			throw new \UnexpectedValueException('The token is invalid');
588
-		}
589
-
590
-		$users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
591
-
592
-		if (count($users) !== 1) {
593
-			// No unique user found
594
-			throw new \UnexpectedValueException('The token is invalid');
595
-		}
596
-
597
-		// Token found login as that user
598
-		return array_shift($users);
599
-	}
600
-
601
-	/**
602
-	 * @return array
603
-	 * @deprecated 11.0.0 - Use getFilters() instead
604
-	 */
605
-	public function getNavigation() {
606
-		$entries = array(
607
-			'apps' => array(),
608
-			'top' => array(),
609
-		);
610
-		foreach ($this->getExtensions() as $c) {
611
-			$additionalEntries = $c->getNavigation();
612
-			if (is_array($additionalEntries)) {
613
-				$entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
614
-				$entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
615
-			}
616
-		}
617
-
618
-		return $entries;
619
-	}
620
-
621
-	/**
622
-	 * @param string $filterValue
623
-	 * @return boolean
624
-	 * @deprecated 11.0.0 - Use getFilterById() instead
625
-	 */
626
-	public function isFilterValid($filterValue) {
627
-		if (isset($this->validFilters[$filterValue])) {
628
-			return $this->validFilters[$filterValue];
629
-		}
630
-
631
-		foreach ($this->getExtensions() as $c) {
632
-			if ($c->isFilterValid($filterValue) === true) {
633
-				$this->validFilters[$filterValue] = true;
634
-				return true;
635
-			}
636
-		}
637
-
638
-		$this->validFilters[$filterValue] = false;
639
-		return false;
640
-	}
641
-
642
-	/**
643
-	 * @param array $types
644
-	 * @param string $filter
645
-	 * @return array
646
-	 * @deprecated 11.0.0 - Use getFilterById()->filterTypes() instead
647
-	 */
648
-	public function filterNotificationTypes($types, $filter) {
649
-		if (!$this->isFilterValid($filter)) {
650
-			return $types;
651
-		}
652
-
653
-		foreach ($this->getExtensions() as $c) {
654
-			$result = $c->filterNotificationTypes($types, $filter);
655
-			if (is_array($result)) {
656
-				$types = $result;
657
-			}
658
-		}
659
-		return $types;
660
-	}
661
-
662
-	/**
663
-	 * @param string $filter
664
-	 * @return array
665
-	 * @deprecated 11.0.0 - Use getFilterById() instead
666
-	 */
667
-	public function getQueryForFilter($filter) {
668
-		if (!$this->isFilterValid($filter)) {
669
-			return [null, null];
670
-		}
671
-
672
-		$conditions = array();
673
-		$parameters = array();
674
-
675
-		foreach ($this->getExtensions() as $c) {
676
-			$result = $c->getQueryForFilter($filter);
677
-			if (is_array($result)) {
678
-				list($condition, $parameter) = $result;
679
-				if ($condition && is_array($parameter)) {
680
-					$conditions[] = $condition;
681
-					$parameters = array_merge($parameters, $parameter);
682
-				}
683
-			}
684
-		}
685
-
686
-		if (empty($conditions)) {
687
-			return array(null, null);
688
-		}
689
-
690
-		return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
691
-	}
692
-
693
-	/**
694
-	 * Will return additional notification types as specified by other apps
695
-	 *
696
-	 * @param string $languageCode
697
-	 * @return array
698
-	 * @deprecated 11.0.0 - Use getSettings() instead
699
-	 */
700
-	public function getNotificationTypes($languageCode) {
701
-		$notificationTypes = $sharingNotificationTypes = [];
702
-		foreach ($this->getExtensions() as $c) {
703
-			$result = $c->getNotificationTypes($languageCode);
704
-			if (is_array($result)) {
705
-				$notificationTypes = array_merge($notificationTypes, $result);
706
-			}
707
-		}
708
-
709
-		return array_merge($sharingNotificationTypes, $notificationTypes);
710
-	}
711
-
712
-	/**
713
-	 * @param string $method
714
-	 * @return array
715
-	 * @deprecated 11.0.0 - Use getSettings()->isDefaulEnabled<method>() instead
716
-	 */
717
-	public function getDefaultTypes($method) {
718
-		$defaultTypes = array();
719
-		foreach ($this->getExtensions() as $c) {
720
-			$types = $c->getDefaultTypes($method);
721
-			if (is_array($types)) {
722
-				$defaultTypes = array_merge($types, $defaultTypes);
723
-			}
724
-		}
725
-		return $defaultTypes;
726
-	}
43
+    /** @var IRequest */
44
+    protected $request;
45
+
46
+    /** @var IUserSession */
47
+    protected $session;
48
+
49
+    /** @var IConfig */
50
+    protected $config;
51
+
52
+    /** @var IValidator */
53
+    protected $validator;
54
+
55
+    /** @var string */
56
+    protected $formattingObjectType;
57
+
58
+    /** @var int */
59
+    protected $formattingObjectId;
60
+
61
+    /** @var bool */
62
+    protected $requirePNG;
63
+
64
+    /** @var string */
65
+    protected $currentUserId;
66
+
67
+    /**
68
+     * constructor of the controller
69
+     *
70
+     * @param IRequest $request
71
+     * @param IUserSession $session
72
+     * @param IConfig $config
73
+     * @param IValidator $validator
74
+     */
75
+    public function __construct(IRequest $request,
76
+                                IUserSession $session,
77
+                                IConfig $config,
78
+                                IValidator $validator) {
79
+        $this->request = $request;
80
+        $this->session = $session;
81
+        $this->config = $config;
82
+        $this->validator = $validator;
83
+    }
84
+
85
+    /** @var \Closure[] */
86
+    private $consumersClosures = array();
87
+
88
+    /** @var IConsumer[] */
89
+    private $consumers = array();
90
+
91
+    /** @var \Closure[] */
92
+    private $extensionsClosures = array();
93
+
94
+    /** @var IExtension[] */
95
+    private $extensions = array();
96
+
97
+    /** @var array list of filters "name" => "is valid" */
98
+    protected $validFilters = array(
99
+        'all'	=> true,
100
+        'by'	=> true,
101
+        'self'	=> true,
102
+    );
103
+
104
+    /** @var array list of type icons "type" => "css class" */
105
+    protected $typeIcons = array();
106
+
107
+    /** @var array list of special parameters "app" => ["text" => ["parameter" => "type"]] */
108
+    protected $specialParameters = array();
109
+
110
+    /**
111
+     * @return \OCP\Activity\IConsumer[]
112
+     */
113
+    protected function getConsumers() {
114
+        if (!empty($this->consumers)) {
115
+            return $this->consumers;
116
+        }
117
+
118
+        $this->consumers = [];
119
+        foreach($this->consumersClosures as $consumer) {
120
+            $c = $consumer();
121
+            if ($c instanceof IConsumer) {
122
+                $this->consumers[] = $c;
123
+            } else {
124
+                throw new \InvalidArgumentException('The given consumer does not implement the \OCP\Activity\IConsumer interface');
125
+            }
126
+        }
127
+
128
+        return $this->consumers;
129
+    }
130
+
131
+    /**
132
+     * @return \OCP\Activity\IExtension[]
133
+     */
134
+    protected function getExtensions() {
135
+        if (!empty($this->extensions)) {
136
+            return $this->extensions;
137
+        }
138
+
139
+        $this->extensions = [];
140
+        foreach($this->extensionsClosures as $extension) {
141
+            $e = $extension();
142
+            if ($e instanceof IExtension) {
143
+                $this->extensions[] = $e;
144
+            } else {
145
+                throw new \InvalidArgumentException('The given extension does not implement the \OCP\Activity\IExtension interface');
146
+            }
147
+        }
148
+
149
+        return $this->extensions;
150
+    }
151
+
152
+    /**
153
+     * Generates a new IEvent object
154
+     *
155
+     * Make sure to call at least the following methods before sending it to the
156
+     * app with via the publish() method:
157
+     *  - setApp()
158
+     *  - setType()
159
+     *  - setAffectedUser()
160
+     *  - setSubject()
161
+     *
162
+     * @return IEvent
163
+     */
164
+    public function generateEvent() {
165
+        return new Event($this->validator);
166
+    }
167
+
168
+    /**
169
+     * Publish an event to the activity consumers
170
+     *
171
+     * Make sure to call at least the following methods before sending an Event:
172
+     *  - setApp()
173
+     *  - setType()
174
+     *  - setAffectedUser()
175
+     *  - setSubject()
176
+     *
177
+     * @param IEvent $event
178
+     * @throws \BadMethodCallException if required values have not been set
179
+     */
180
+    public function publish(IEvent $event) {
181
+        if ($event->getAuthor() === '') {
182
+            if ($this->session->getUser() instanceof IUser) {
183
+                $event->setAuthor($this->session->getUser()->getUID());
184
+            }
185
+        }
186
+
187
+        if (!$event->getTimestamp()) {
188
+            $event->setTimestamp(time());
189
+        }
190
+
191
+        if (!$event->isValid()) {
192
+            throw new \BadMethodCallException('The given event is invalid');
193
+        }
194
+
195
+        foreach ($this->getConsumers() as $c) {
196
+            $c->receive($event);
197
+        }
198
+    }
199
+
200
+    /**
201
+     * @param string $app           The app where this event is associated with
202
+     * @param string $subject       A short description of the event
203
+     * @param array  $subjectParams Array with parameters that are filled in the subject
204
+     * @param string $message       A longer description of the event
205
+     * @param array  $messageParams Array with parameters that are filled in the message
206
+     * @param string $file          The file including path where this event is associated with
207
+     * @param string $link          A link where this event is associated with
208
+     * @param string $affectedUser  Recipient of the activity
209
+     * @param string $type          Type of the notification
210
+     * @param int    $priority      Priority of the notification
211
+     */
212
+    public function publishActivity($app, $subject, $subjectParams, $message, $messageParams, $file, $link, $affectedUser, $type, $priority) {
213
+        $event = $this->generateEvent();
214
+        $event->setApp($app)
215
+            ->setType($type)
216
+            ->setAffectedUser($affectedUser)
217
+            ->setSubject($subject, $subjectParams)
218
+            ->setMessage($message, $messageParams)
219
+            ->setObject('', 0, $file)
220
+            ->setLink($link);
221
+
222
+        $this->publish($event);
223
+    }
224
+
225
+    /**
226
+     * In order to improve lazy loading a closure can be registered which will be called in case
227
+     * activity consumers are actually requested
228
+     *
229
+     * $callable has to return an instance of OCA\Activity\IConsumer
230
+     *
231
+     * @param \Closure $callable
232
+     */
233
+    public function registerConsumer(\Closure $callable) {
234
+        $this->consumersClosures[] = $callable;
235
+        $this->consumers = [];
236
+    }
237
+
238
+    /**
239
+     * In order to improve lazy loading a closure can be registered which will be called in case
240
+     * activity consumers are actually requested
241
+     *
242
+     * $callable has to return an instance of OCA\Activity\IExtension
243
+     *
244
+     * @param \Closure $callable
245
+     */
246
+    public function registerExtension(\Closure $callable) {
247
+        $this->extensionsClosures[] = $callable;
248
+        $this->extensions = [];
249
+    }
250
+
251
+    /** @var string[] */
252
+    protected $filterClasses = [];
253
+
254
+    /** @var IFilter[] */
255
+    protected $filters = [];
256
+
257
+    /** @var bool */
258
+    protected $loadedLegacyFilters = false;
259
+
260
+    /**
261
+     * @param string $filter Class must implement OCA\Activity\IFilter
262
+     * @return void
263
+     */
264
+    public function registerFilter($filter) {
265
+        $this->filterClasses[$filter] = false;
266
+    }
267
+
268
+    /**
269
+     * @return IFilter[]
270
+     * @throws \InvalidArgumentException
271
+     */
272
+    public function getFilters() {
273
+        if (!$this->loadedLegacyFilters) {
274
+            $legacyFilters = $this->getNavigation();
275
+
276
+            foreach ($legacyFilters['top'] as $filter => $data) {
277
+                $this->filters[$filter] = new LegacyFilter(
278
+                    $this, $filter, $data['name'], true
279
+                );
280
+            }
281
+
282
+            foreach ($legacyFilters['apps'] as $filter => $data) {
283
+                $this->filters[$filter] = new LegacyFilter(
284
+                    $this, $filter, $data['name'], false
285
+                );
286
+            }
287
+            $this->loadedLegacyFilters = true;
288
+        }
289
+
290
+        foreach ($this->filterClasses as $class => $false) {
291
+            /** @var IFilter $filter */
292
+            $filter = \OC::$server->query($class);
293
+
294
+            if (!$filter instanceof IFilter) {
295
+                throw new \InvalidArgumentException('Invalid activity filter registered');
296
+            }
297
+
298
+            $this->filters[$filter->getIdentifier()] = $filter;
299
+
300
+            unset($this->filterClasses[$class]);
301
+        }
302
+        return $this->filters;
303
+    }
304
+
305
+    /**
306
+     * @param string $id
307
+     * @return IFilter
308
+     * @throws \InvalidArgumentException when the filter was not found
309
+     * @since 11.0.0
310
+     */
311
+    public function getFilterById($id) {
312
+        $filters = $this->getFilters();
313
+
314
+        if (isset($filters[$id])) {
315
+            return $filters[$id];
316
+        }
317
+
318
+        throw new \InvalidArgumentException('Requested filter does not exist');
319
+    }
320
+
321
+    /** @var string[] */
322
+    protected $providerClasses = [];
323
+
324
+    /** @var IProvider[] */
325
+    protected $providers = [];
326
+
327
+    /**
328
+     * @param string $provider Class must implement OCA\Activity\IProvider
329
+     * @return void
330
+     */
331
+    public function registerProvider($provider) {
332
+        $this->providerClasses[$provider] = false;
333
+    }
334
+
335
+    /**
336
+     * @return IProvider[]
337
+     * @throws \InvalidArgumentException
338
+     */
339
+    public function getProviders() {
340
+        foreach ($this->providerClasses as $class => $false) {
341
+            /** @var IProvider $provider */
342
+            $provider = \OC::$server->query($class);
343
+
344
+            if (!$provider instanceof IProvider) {
345
+                throw new \InvalidArgumentException('Invalid activity provider registered');
346
+            }
347
+
348
+            $this->providers[] = $provider;
349
+
350
+            unset($this->providerClasses[$class]);
351
+        }
352
+        return $this->providers;
353
+    }
354
+
355
+    /** @var string[] */
356
+    protected $settingsClasses = [];
357
+
358
+    /** @var ISetting[] */
359
+    protected $settings = [];
360
+
361
+    /** @var bool */
362
+    protected $loadedLegacyTypes = false;
363
+
364
+    /**
365
+     * @param string $setting Class must implement OCA\Activity\ISetting
366
+     * @return void
367
+     */
368
+    public function registerSetting($setting) {
369
+        $this->settingsClasses[$setting] = false;
370
+    }
371
+
372
+    /**
373
+     * @return ISetting[]
374
+     * @throws \InvalidArgumentException
375
+     */
376
+    public function getSettings() {
377
+        if (!$this->loadedLegacyTypes) {
378
+            $l = \OC::$server->getL10N('core');
379
+            $legacyTypes = $this->getNotificationTypes($l->getLanguageCode());
380
+            $streamTypes = $this->getDefaultTypes(IExtension::METHOD_STREAM);
381
+            $mailTypes = $this->getDefaultTypes(IExtension::METHOD_MAIL);
382
+            foreach ($legacyTypes as $type => $data) {
383
+                if (is_string($data)) {
384
+                    $desc = $data;
385
+                    $canChangeStream = true;
386
+                    $canChangeMail = true;
387
+                } else {
388
+                    $desc = $data['desc'];
389
+                    $canChangeStream = in_array(IExtension::METHOD_STREAM, $data['methods']);
390
+                    $canChangeMail = in_array(IExtension::METHOD_MAIL, $data['methods']);
391
+                }
392
+
393
+                $this->settings[$type] = new LegacySetting(
394
+                    $type, $desc,
395
+                    $canChangeStream, in_array($type, $streamTypes),
396
+                    $canChangeMail, in_array($type, $mailTypes)
397
+                );
398
+            }
399
+            $this->loadedLegacyTypes = true;
400
+        }
401
+
402
+        foreach ($this->settingsClasses as $class => $false) {
403
+            /** @var ISetting $setting */
404
+            $setting = \OC::$server->query($class);
405
+
406
+            if (!$setting instanceof ISetting) {
407
+                throw new \InvalidArgumentException('Invalid activity filter registered');
408
+            }
409
+
410
+            $this->settings[$setting->getIdentifier()] = $setting;
411
+
412
+            unset($this->settingsClasses[$class]);
413
+        }
414
+        return $this->settings;
415
+    }
416
+
417
+    /**
418
+     * @param string $id
419
+     * @return ISetting
420
+     * @throws \InvalidArgumentException when the setting was not found
421
+     * @since 11.0.0
422
+     */
423
+    public function getSettingById($id) {
424
+        $settings = $this->getSettings();
425
+
426
+        if (isset($settings[$id])) {
427
+            return $settings[$id];
428
+        }
429
+
430
+        throw new \InvalidArgumentException('Requested setting does not exist');
431
+    }
432
+
433
+    /**
434
+     * @param string $type
435
+     * @return string
436
+     */
437
+    public function getTypeIcon($type) {
438
+        if (isset($this->typeIcons[$type])) {
439
+            return $this->typeIcons[$type];
440
+        }
441
+
442
+        foreach ($this->getExtensions() as $c) {
443
+            $icon = $c->getTypeIcon($type);
444
+            if (is_string($icon)) {
445
+                $this->typeIcons[$type] = $icon;
446
+                return $icon;
447
+            }
448
+        }
449
+
450
+        $this->typeIcons[$type] = '';
451
+        return '';
452
+    }
453
+
454
+    /**
455
+     * @param string $type
456
+     * @param string $id
457
+     */
458
+    public function setFormattingObject($type, $id) {
459
+        $this->formattingObjectType = $type;
460
+        $this->formattingObjectId = (string) $id;
461
+    }
462
+
463
+    /**
464
+     * @return bool
465
+     */
466
+    public function isFormattingFilteredObject() {
467
+        return $this->formattingObjectType !== null && $this->formattingObjectId !== null
468
+            && $this->formattingObjectType === $this->request->getParam('object_type')
469
+            && $this->formattingObjectId === $this->request->getParam('object_id');
470
+    }
471
+
472
+    /**
473
+     * @param bool $status Set to true, when parsing events should not use SVG icons
474
+     */
475
+    public function setRequirePNG($status) {
476
+        $this->requirePNG = $status;
477
+    }
478
+
479
+    /**
480
+     * @return bool
481
+     */
482
+    public function getRequirePNG() {
483
+        return $this->requirePNG;
484
+    }
485
+
486
+    /**
487
+     * @param string $app
488
+     * @param string $text
489
+     * @param array $params
490
+     * @param boolean $stripPath
491
+     * @param boolean $highlightParams
492
+     * @param string $languageCode
493
+     * @return string|false
494
+     */
495
+    public function translate($app, $text, $params, $stripPath, $highlightParams, $languageCode) {
496
+        foreach ($this->getExtensions() as $c) {
497
+            $translation = $c->translate($app, $text, $params, $stripPath, $highlightParams, $languageCode);
498
+            if (is_string($translation)) {
499
+                return $translation;
500
+            }
501
+        }
502
+
503
+        return false;
504
+    }
505
+
506
+    /**
507
+     * @param string $app
508
+     * @param string $text
509
+     * @return array|false
510
+     */
511
+    public function getSpecialParameterList($app, $text) {
512
+        if (isset($this->specialParameters[$app][$text])) {
513
+            return $this->specialParameters[$app][$text];
514
+        }
515
+
516
+        if (!isset($this->specialParameters[$app])) {
517
+            $this->specialParameters[$app] = array();
518
+        }
519
+
520
+        foreach ($this->getExtensions() as $c) {
521
+            $specialParameter = $c->getSpecialParameterList($app, $text);
522
+            if (is_array($specialParameter)) {
523
+                $this->specialParameters[$app][$text] = $specialParameter;
524
+                return $specialParameter;
525
+            }
526
+        }
527
+
528
+        $this->specialParameters[$app][$text] = false;
529
+        return false;
530
+    }
531
+
532
+    /**
533
+     * @param array $activity
534
+     * @return integer|false
535
+     */
536
+    public function getGroupParameter($activity) {
537
+        foreach ($this->getExtensions() as $c) {
538
+            $parameter = $c->getGroupParameter($activity);
539
+            if ($parameter !== false) {
540
+                return $parameter;
541
+            }
542
+        }
543
+
544
+        return false;
545
+    }
546
+
547
+    /**
548
+     * Set the user we need to use
549
+     *
550
+     * @param string|null $currentUserId
551
+     * @throws \UnexpectedValueException If the user is invalid
552
+     */
553
+    public function setCurrentUserId($currentUserId) {
554
+        if (!is_string($currentUserId) && $currentUserId !== null) {
555
+            throw new \UnexpectedValueException('The given current user is invalid');
556
+        }
557
+        $this->currentUserId = $currentUserId;
558
+    }
559
+
560
+    /**
561
+     * Get the user we need to use
562
+     *
563
+     * Either the user is logged in, or we try to get it from the token
564
+     *
565
+     * @return string
566
+     * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
567
+     */
568
+    public function getCurrentUserId() {
569
+        if ($this->currentUserId !== null) {
570
+            return $this->currentUserId;
571
+        } else if (!$this->session->isLoggedIn()) {
572
+            return $this->getUserFromToken();
573
+        } else {
574
+            return $this->session->getUser()->getUID();
575
+        }
576
+    }
577
+
578
+    /**
579
+     * Get the user for the token
580
+     *
581
+     * @return string
582
+     * @throws \UnexpectedValueException If the token is invalid, does not exist or is not unique
583
+     */
584
+    protected function getUserFromToken() {
585
+        $token = (string) $this->request->getParam('token', '');
586
+        if (strlen($token) !== 30) {
587
+            throw new \UnexpectedValueException('The token is invalid');
588
+        }
589
+
590
+        $users = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
591
+
592
+        if (count($users) !== 1) {
593
+            // No unique user found
594
+            throw new \UnexpectedValueException('The token is invalid');
595
+        }
596
+
597
+        // Token found login as that user
598
+        return array_shift($users);
599
+    }
600
+
601
+    /**
602
+     * @return array
603
+     * @deprecated 11.0.0 - Use getFilters() instead
604
+     */
605
+    public function getNavigation() {
606
+        $entries = array(
607
+            'apps' => array(),
608
+            'top' => array(),
609
+        );
610
+        foreach ($this->getExtensions() as $c) {
611
+            $additionalEntries = $c->getNavigation();
612
+            if (is_array($additionalEntries)) {
613
+                $entries['apps'] = array_merge($entries['apps'], $additionalEntries['apps']);
614
+                $entries['top'] = array_merge($entries['top'], $additionalEntries['top']);
615
+            }
616
+        }
617
+
618
+        return $entries;
619
+    }
620
+
621
+    /**
622
+     * @param string $filterValue
623
+     * @return boolean
624
+     * @deprecated 11.0.0 - Use getFilterById() instead
625
+     */
626
+    public function isFilterValid($filterValue) {
627
+        if (isset($this->validFilters[$filterValue])) {
628
+            return $this->validFilters[$filterValue];
629
+        }
630
+
631
+        foreach ($this->getExtensions() as $c) {
632
+            if ($c->isFilterValid($filterValue) === true) {
633
+                $this->validFilters[$filterValue] = true;
634
+                return true;
635
+            }
636
+        }
637
+
638
+        $this->validFilters[$filterValue] = false;
639
+        return false;
640
+    }
641
+
642
+    /**
643
+     * @param array $types
644
+     * @param string $filter
645
+     * @return array
646
+     * @deprecated 11.0.0 - Use getFilterById()->filterTypes() instead
647
+     */
648
+    public function filterNotificationTypes($types, $filter) {
649
+        if (!$this->isFilterValid($filter)) {
650
+            return $types;
651
+        }
652
+
653
+        foreach ($this->getExtensions() as $c) {
654
+            $result = $c->filterNotificationTypes($types, $filter);
655
+            if (is_array($result)) {
656
+                $types = $result;
657
+            }
658
+        }
659
+        return $types;
660
+    }
661
+
662
+    /**
663
+     * @param string $filter
664
+     * @return array
665
+     * @deprecated 11.0.0 - Use getFilterById() instead
666
+     */
667
+    public function getQueryForFilter($filter) {
668
+        if (!$this->isFilterValid($filter)) {
669
+            return [null, null];
670
+        }
671
+
672
+        $conditions = array();
673
+        $parameters = array();
674
+
675
+        foreach ($this->getExtensions() as $c) {
676
+            $result = $c->getQueryForFilter($filter);
677
+            if (is_array($result)) {
678
+                list($condition, $parameter) = $result;
679
+                if ($condition && is_array($parameter)) {
680
+                    $conditions[] = $condition;
681
+                    $parameters = array_merge($parameters, $parameter);
682
+                }
683
+            }
684
+        }
685
+
686
+        if (empty($conditions)) {
687
+            return array(null, null);
688
+        }
689
+
690
+        return array(' and ((' . implode(') or (', $conditions) . '))', $parameters);
691
+    }
692
+
693
+    /**
694
+     * Will return additional notification types as specified by other apps
695
+     *
696
+     * @param string $languageCode
697
+     * @return array
698
+     * @deprecated 11.0.0 - Use getSettings() instead
699
+     */
700
+    public function getNotificationTypes($languageCode) {
701
+        $notificationTypes = $sharingNotificationTypes = [];
702
+        foreach ($this->getExtensions() as $c) {
703
+            $result = $c->getNotificationTypes($languageCode);
704
+            if (is_array($result)) {
705
+                $notificationTypes = array_merge($notificationTypes, $result);
706
+            }
707
+        }
708
+
709
+        return array_merge($sharingNotificationTypes, $notificationTypes);
710
+    }
711
+
712
+    /**
713
+     * @param string $method
714
+     * @return array
715
+     * @deprecated 11.0.0 - Use getSettings()->isDefaulEnabled<method>() instead
716
+     */
717
+    public function getDefaultTypes($method) {
718
+        $defaultTypes = array();
719
+        foreach ($this->getExtensions() as $c) {
720
+            $types = $c->getDefaultTypes($method);
721
+            if (is_array($types)) {
722
+                $defaultTypes = array_merge($types, $defaultTypes);
723
+            }
724
+        }
725
+        return $defaultTypes;
726
+    }
727 727
 }
Please login to merge, or discard this patch.
apps/files_versions/lib/Storage.php 2 patches
Indentation   +792 added lines, -792 removed lines patch added patch discarded remove patch
@@ -54,797 +54,797 @@
 block discarded – undo
54 54
 
55 55
 class Storage {
56 56
 
57
-	const DEFAULTENABLED=true;
58
-	const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota
59
-	const VERSIONS_ROOT = 'files_versions/';
60
-
61
-	const DELETE_TRIGGER_MASTER_REMOVED = 0;
62
-	const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1;
63
-	const DELETE_TRIGGER_QUOTA_EXCEEDED = 2;
64
-
65
-	// files for which we can remove the versions after the delete operation was successful
66
-	private static $deletedFiles = array();
67
-
68
-	private static $sourcePathAndUser = array();
69
-
70
-	private static $max_versions_per_interval = array(
71
-		//first 10sec, one version every 2sec
72
-		1 => array('intervalEndsAfter' => 10,      'step' => 2),
73
-		//next minute, one version every 10sec
74
-		2 => array('intervalEndsAfter' => 60,      'step' => 10),
75
-		//next hour, one version every minute
76
-		3 => array('intervalEndsAfter' => 3600,    'step' => 60),
77
-		//next 24h, one version every hour
78
-		4 => array('intervalEndsAfter' => 86400,   'step' => 3600),
79
-		//next 30days, one version per day
80
-		5 => array('intervalEndsAfter' => 2592000, 'step' => 86400),
81
-		//until the end one version per week
82
-		6 => array('intervalEndsAfter' => -1,      'step' => 604800),
83
-	);
84
-
85
-	/** @var \OCA\Files_Versions\AppInfo\Application */
86
-	private static $application;
87
-
88
-	/**
89
-	 * get the UID of the owner of the file and the path to the file relative to
90
-	 * owners files folder
91
-	 *
92
-	 * @param string $filename
93
-	 * @return array
94
-	 * @throws \OC\User\NoUserException
95
-	 */
96
-	public static function getUidAndFilename($filename) {
97
-		$uid = Filesystem::getOwner($filename);
98
-		$userManager = \OC::$server->getUserManager();
99
-		// if the user with the UID doesn't exists, e.g. because the UID points
100
-		// to a remote user with a federated cloud ID we use the current logged-in
101
-		// user. We need a valid local user to create the versions
102
-		if (!$userManager->userExists($uid)) {
103
-			$uid = User::getUser();
104
-		}
105
-		Filesystem::initMountPoints($uid);
106
-		if ( $uid !== User::getUser() ) {
107
-			$info = Filesystem::getFileInfo($filename);
108
-			$ownerView = new View('/'.$uid.'/files');
109
-			try {
110
-				$filename = $ownerView->getPath($info['fileid']);
111
-				// make sure that the file name doesn't end with a trailing slash
112
-				// can for example happen single files shared across servers
113
-				$filename = rtrim($filename, '/');
114
-			} catch (NotFoundException $e) {
115
-				$filename = null;
116
-			}
117
-		}
118
-		return [$uid, $filename];
119
-	}
120
-
121
-	/**
122
-	 * Remember the owner and the owner path of the source file
123
-	 *
124
-	 * @param string $source source path
125
-	 */
126
-	public static function setSourcePathAndUser($source) {
127
-		list($uid, $path) = self::getUidAndFilename($source);
128
-		self::$sourcePathAndUser[$source] = array('uid' => $uid, 'path' => $path);
129
-	}
130
-
131
-	/**
132
-	 * Gets the owner and the owner path from the source path
133
-	 *
134
-	 * @param string $source source path
135
-	 * @return array with user id and path
136
-	 */
137
-	public static function getSourcePathAndUser($source) {
138
-
139
-		if (isset(self::$sourcePathAndUser[$source])) {
140
-			$uid = self::$sourcePathAndUser[$source]['uid'];
141
-			$path = self::$sourcePathAndUser[$source]['path'];
142
-			unset(self::$sourcePathAndUser[$source]);
143
-		} else {
144
-			$uid = $path = false;
145
-		}
146
-		return array($uid, $path);
147
-	}
148
-
149
-	/**
150
-	 * get current size of all versions from a given user
151
-	 *
152
-	 * @param string $user user who owns the versions
153
-	 * @return int versions size
154
-	 */
155
-	private static function getVersionsSize($user) {
156
-		$view = new View('/' . $user);
157
-		$fileInfo = $view->getFileInfo('/files_versions');
158
-		return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
159
-	}
160
-
161
-	/**
162
-	 * store a new version of a file.
163
-	 */
164
-	public static function store($filename) {
165
-
166
-		// if the file gets streamed we need to remove the .part extension
167
-		// to get the right target
168
-		$ext = pathinfo($filename, PATHINFO_EXTENSION);
169
-		if ($ext === 'part') {
170
-			$filename = substr($filename, 0, strlen($filename) - 5);
171
-		}
172
-
173
-		// we only handle existing files
174
-		if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
175
-			return false;
176
-		}
177
-
178
-		list($uid, $filename) = self::getUidAndFilename($filename);
179
-
180
-		$files_view = new View('/'.$uid .'/files');
181
-		$users_view = new View('/'.$uid);
182
-
183
-		$eventDispatcher = \OC::$server->getEventDispatcher();
184
-		$id = $files_view->getFileInfo($filename)->getId();
185
-		$nodes = \OC::$server->getRootFolder()->getById($id);
186
-		foreach ($nodes as $node) {
187
-			$event = new CreateVersionEvent($node);
188
-			$eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event);
189
-			if ($event->shouldCreateVersion() === false) {
190
-				return false;
191
-			}
192
-		}
193
-
194
-		// no use making versions for empty files
195
-		if ($files_view->filesize($filename) === 0) {
196
-			return false;
197
-		}
198
-
199
-		// create all parent folders
200
-		self::createMissingDirectories($filename, $users_view);
201
-
202
-		self::scheduleExpire($uid, $filename);
203
-
204
-		// store a new version of a file
205
-		$mtime = $users_view->filemtime('files/' . $filename);
206
-		$users_view->copy('files/' . $filename, 'files_versions/' . $filename . '.v' . $mtime);
207
-		// call getFileInfo to enforce a file cache entry for the new version
208
-		$users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
209
-	}
210
-
211
-
212
-	/**
213
-	 * mark file as deleted so that we can remove the versions if the file is gone
214
-	 * @param string $path
215
-	 */
216
-	public static function markDeletedFile($path) {
217
-		list($uid, $filename) = self::getUidAndFilename($path);
218
-		self::$deletedFiles[$path] = array(
219
-			'uid' => $uid,
220
-			'filename' => $filename);
221
-	}
222
-
223
-	/**
224
-	 * delete the version from the storage and cache
225
-	 *
226
-	 * @param View $view
227
-	 * @param string $path
228
-	 */
229
-	protected static function deleteVersion($view, $path) {
230
-		$view->unlink($path);
231
-		/**
232
-		 * @var \OC\Files\Storage\Storage $storage
233
-		 * @var string $internalPath
234
-		 */
235
-		list($storage, $internalPath) = $view->resolvePath($path);
236
-		$cache = $storage->getCache($internalPath);
237
-		$cache->remove($internalPath);
238
-	}
239
-
240
-	/**
241
-	 * Delete versions of a file
242
-	 */
243
-	public static function delete($path) {
244
-
245
-		$deletedFile = self::$deletedFiles[$path];
246
-		$uid = $deletedFile['uid'];
247
-		$filename = $deletedFile['filename'];
248
-
249
-		if (!Filesystem::file_exists($path)) {
250
-
251
-			$view = new View('/' . $uid . '/files_versions');
252
-
253
-			$versions = self::getVersions($uid, $filename);
254
-			if (!empty($versions)) {
255
-				foreach ($versions as $v) {
256
-					\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
257
-					self::deleteVersion($view, $filename . '.v' . $v['version']);
258
-					\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
259
-				}
260
-			}
261
-		}
262
-		unset(self::$deletedFiles[$path]);
263
-	}
264
-
265
-	/**
266
-	 * Rename or copy versions of a file of the given paths
267
-	 *
268
-	 * @param string $sourcePath source path of the file to move, relative to
269
-	 * the currently logged in user's "files" folder
270
-	 * @param string $targetPath target path of the file to move, relative to
271
-	 * the currently logged in user's "files" folder
272
-	 * @param string $operation can be 'copy' or 'rename'
273
-	 */
274
-	public static function renameOrCopy($sourcePath, $targetPath, $operation) {
275
-		list($sourceOwner, $sourcePath) = self::getSourcePathAndUser($sourcePath);
276
-
277
-		// it was a upload of a existing file if no old path exists
278
-		// in this case the pre-hook already called the store method and we can
279
-		// stop here
280
-		if ($sourcePath === false) {
281
-			return true;
282
-		}
283
-
284
-		list($targetOwner, $targetPath) = self::getUidAndFilename($targetPath);
285
-
286
-		$sourcePath = ltrim($sourcePath, '/');
287
-		$targetPath = ltrim($targetPath, '/');
288
-
289
-		$rootView = new View('');
290
-
291
-		// did we move a directory ?
292
-		if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
293
-			// does the directory exists for versions too ?
294
-			if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) {
295
-				// create missing dirs if necessary
296
-				self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
297
-
298
-				// move the directory containing the versions
299
-				$rootView->$operation(
300
-					'/' . $sourceOwner . '/files_versions/' . $sourcePath,
301
-					'/' . $targetOwner . '/files_versions/' . $targetPath
302
-				);
303
-			}
304
-		} else if ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) {
305
-			// create missing dirs if necessary
306
-			self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
307
-
308
-			foreach ($versions as $v) {
309
-				// move each version one by one to the target directory
310
-				$rootView->$operation(
311
-					'/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'],
312
-					'/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version']
313
-				);
314
-			}
315
-		}
316
-
317
-		// if we moved versions directly for a file, schedule expiration check for that file
318
-		if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
319
-			self::scheduleExpire($targetOwner, $targetPath);
320
-		}
321
-
322
-	}
323
-
324
-	/**
325
-	 * Rollback to an old version of a file.
326
-	 *
327
-	 * @param string $file file name
328
-	 * @param int $revision revision timestamp
329
-	 * @return bool
330
-	 */
331
-	public static function rollback($file, $revision) {
332
-
333
-		// add expected leading slash
334
-		$file = '/' . ltrim($file, '/');
335
-		list($uid, $filename) = self::getUidAndFilename($file);
336
-		if ($uid === null || trim($filename, '/') === '') {
337
-			return false;
338
-		}
339
-
340
-		$users_view = new View('/'.$uid);
341
-		$files_view = new View('/'. User::getUser().'/files');
342
-
343
-		$versionCreated = false;
344
-
345
-		$fileInfo = $files_view->getFileInfo($file);
346
-
347
-		// check if user has the permissions to revert a version
348
-		if (!$fileInfo->isUpdateable()) {
349
-			return false;
350
-		}
351
-
352
-		//first create a new version
353
-		$version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
354
-		if (!$users_view->file_exists($version)) {
355
-			$users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
356
-			$versionCreated = true;
357
-		}
358
-
359
-		$fileToRestore =  'files_versions' . $filename . '.v' . $revision;
360
-
361
-		// Restore encrypted version of the old file for the newly restored file
362
-		// This has to happen manually here since the file is manually copied below
363
-		$oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion();
364
-		$oldFileInfo = $users_view->getFileInfo($fileToRestore);
365
-		$cache = $fileInfo->getStorage()->getCache();
366
-		$cache->update(
367
-			$fileInfo->getId(), [
368
-				'encrypted' => $oldVersion,
369
-				'encryptedVersion' => $oldVersion,
370
-				'size' => $oldFileInfo->getSize()
371
-			]
372
-		);
373
-
374
-		// rollback
375
-		if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) {
376
-			$files_view->touch($file, $revision);
377
-			Storage::scheduleExpire($uid, $file);
378
-			\OC_Hook::emit('\OCP\Versions', 'rollback', array(
379
-				'path' => $filename,
380
-				'revision' => $revision,
381
-			));
382
-			return true;
383
-		} else if ($versionCreated) {
384
-			self::deleteVersion($users_view, $version);
385
-		}
386
-
387
-		return false;
388
-
389
-	}
390
-
391
-	/**
392
-	 * Stream copy file contents from $path1 to $path2
393
-	 *
394
-	 * @param View $view view to use for copying
395
-	 * @param string $path1 source file to copy
396
-	 * @param string $path2 target file
397
-	 *
398
-	 * @return bool true for success, false otherwise
399
-	 */
400
-	private static function copyFileContents($view, $path1, $path2) {
401
-		/** @var \OC\Files\Storage\Storage $storage1 */
402
-		list($storage1, $internalPath1) = $view->resolvePath($path1);
403
-		/** @var \OC\Files\Storage\Storage $storage2 */
404
-		list($storage2, $internalPath2) = $view->resolvePath($path2);
405
-
406
-		$view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
407
-		$view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
408
-
409
-		// TODO add a proper way of overwriting a file while maintaining file ids
410
-		if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
411
-			$source = $storage1->fopen($internalPath1, 'r');
412
-			$target = $storage2->fopen($internalPath2, 'w');
413
-			list(, $result) = \OC_Helper::streamCopy($source, $target);
414
-			fclose($source);
415
-			fclose($target);
416
-
417
-			if ($result !== false) {
418
-				$storage1->unlink($internalPath1);
419
-			}
420
-		} else {
421
-			$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
422
-		}
423
-
424
-		$view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
425
-		$view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
426
-
427
-		return ($result !== false);
428
-	}
429
-
430
-	/**
431
-	 * get a list of all available versions of a file in descending chronological order
432
-	 * @param string $uid user id from the owner of the file
433
-	 * @param string $filename file to find versions of, relative to the user files dir
434
-	 * @param string $userFullPath
435
-	 * @return array versions newest version first
436
-	 */
437
-	public static function getVersions($uid, $filename, $userFullPath = '') {
438
-		$versions = array();
439
-		if (empty($filename)) {
440
-			return $versions;
441
-		}
442
-		// fetch for old versions
443
-		$view = new View('/' . $uid . '/');
444
-
445
-		$pathinfo = pathinfo($filename);
446
-		$versionedFile = $pathinfo['basename'];
447
-
448
-		$dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']);
449
-
450
-		$dirContent = false;
451
-		if ($view->is_dir($dir)) {
452
-			$dirContent = $view->opendir($dir);
453
-		}
454
-
455
-		if ($dirContent === false) {
456
-			return $versions;
457
-		}
458
-
459
-		if (is_resource($dirContent)) {
460
-			while (($entryName = readdir($dirContent)) !== false) {
461
-				if (!Filesystem::isIgnoredDir($entryName)) {
462
-					$pathparts = pathinfo($entryName);
463
-					$filename = $pathparts['filename'];
464
-					if ($filename === $versionedFile) {
465
-						$pathparts = pathinfo($entryName);
466
-						$timestamp = substr($pathparts['extension'], 1);
467
-						$filename = $pathparts['filename'];
468
-						$key = $timestamp . '#' . $filename;
469
-						$versions[$key]['version'] = $timestamp;
470
-						$versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp);
471
-						if (empty($userFullPath)) {
472
-							$versions[$key]['preview'] = '';
473
-						} else {
474
-							$versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]);
475
-						}
476
-						$versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
477
-						$versions[$key]['name'] = $versionedFile;
478
-						$versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
479
-						$versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile);
480
-					}
481
-				}
482
-			}
483
-			closedir($dirContent);
484
-		}
485
-
486
-		// sort with newest version first
487
-		krsort($versions);
488
-
489
-		return $versions;
490
-	}
491
-
492
-	/**
493
-	 * Expire versions that older than max version retention time
494
-	 * @param string $uid
495
-	 */
496
-	public static function expireOlderThanMaxForUser($uid){
497
-		$expiration = self::getExpiration();
498
-		$threshold = $expiration->getMaxAgeAsTimestamp();
499
-		$versions = self::getAllVersions($uid);
500
-		if (!$threshold || !array_key_exists('all', $versions)) {
501
-			return;
502
-		}
503
-
504
-		$toDelete = [];
505
-		foreach (array_reverse($versions['all']) as $key => $version) {
506
-			if (intval($version['version'])<$threshold) {
507
-				$toDelete[$key] = $version;
508
-			} else {
509
-				//Versions are sorted by time - nothing mo to iterate.
510
-				break;
511
-			}
512
-		}
513
-
514
-		$view = new View('/' . $uid . '/files_versions');
515
-		if (!empty($toDelete)) {
516
-			foreach ($toDelete as $version) {
517
-				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
518
-				self::deleteVersion($view, $version['path'] . '.v' . $version['version']);
519
-				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
520
-			}
521
-		}
522
-	}
523
-
524
-	/**
525
-	 * translate a timestamp into a string like "5 days ago"
526
-	 * @param int $timestamp
527
-	 * @return string for example "5 days ago"
528
-	 */
529
-	private static function getHumanReadableTimestamp($timestamp) {
530
-
531
-		$diff = time() - $timestamp;
532
-
533
-		if ($diff < 60) { // first minute
534
-			return  $diff . " seconds ago";
535
-		} elseif ($diff < 3600) { //first hour
536
-			return round($diff / 60) . " minutes ago";
537
-		} elseif ($diff < 86400) { // first day
538
-			return round($diff / 3600) . " hours ago";
539
-		} elseif ($diff < 604800) { //first week
540
-			return round($diff / 86400) . " days ago";
541
-		} elseif ($diff < 2419200) { //first month
542
-			return round($diff / 604800) . " weeks ago";
543
-		} elseif ($diff < 29030400) { // first year
544
-			return round($diff / 2419200) . " months ago";
545
-		} else {
546
-			return round($diff / 29030400) . " years ago";
547
-		}
548
-
549
-	}
550
-
551
-	/**
552
-	 * returns all stored file versions from a given user
553
-	 * @param string $uid id of the user
554
-	 * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename
555
-	 */
556
-	private static function getAllVersions($uid) {
557
-		$view = new View('/' . $uid . '/');
558
-		$dirs = array(self::VERSIONS_ROOT);
559
-		$versions = array();
560
-
561
-		while (!empty($dirs)) {
562
-			$dir = array_pop($dirs);
563
-			$files = $view->getDirectoryContent($dir);
564
-
565
-			foreach ($files as $file) {
566
-				$fileData = $file->getData();
567
-				$filePath = $dir . '/' . $fileData['name'];
568
-				if ($file['type'] === 'dir') {
569
-					$dirs[] = $filePath;
570
-				} else {
571
-					$versionsBegin = strrpos($filePath, '.v');
572
-					$relPathStart = strlen(self::VERSIONS_ROOT);
573
-					$version = substr($filePath, $versionsBegin + 2);
574
-					$relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart);
575
-					$key = $version . '#' . $relpath;
576
-					$versions[$key] = array('path' => $relpath, 'timestamp' => $version);
577
-				}
578
-			}
579
-		}
580
-
581
-		// newest version first
582
-		krsort($versions);
583
-
584
-		$result = array();
585
-
586
-		foreach ($versions as $key => $value) {
587
-			$size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']);
588
-			$filename = $value['path'];
589
-
590
-			$result['all'][$key]['version'] = $value['timestamp'];
591
-			$result['all'][$key]['path'] = $filename;
592
-			$result['all'][$key]['size'] = $size;
593
-
594
-			$result['by_file'][$filename][$key]['version'] = $value['timestamp'];
595
-			$result['by_file'][$filename][$key]['path'] = $filename;
596
-			$result['by_file'][$filename][$key]['size'] = $size;
597
-		}
598
-
599
-		return $result;
600
-	}
601
-
602
-	/**
603
-	 * get list of files we want to expire
604
-	 * @param array $versions list of versions
605
-	 * @param integer $time
606
-	 * @param bool $quotaExceeded is versions storage limit reached
607
-	 * @return array containing the list of to deleted versions and the size of them
608
-	 */
609
-	protected static function getExpireList($time, $versions, $quotaExceeded = false) {
610
-		$expiration = self::getExpiration();
611
-
612
-		if ($expiration->shouldAutoExpire()) {
613
-			list($toDelete, $size) = self::getAutoExpireList($time, $versions);
614
-		} else {
615
-			$size = 0;
616
-			$toDelete = [];  // versions we want to delete
617
-		}
618
-
619
-		foreach ($versions as $key => $version) {
620
-			if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) {
621
-				$size += $version['size'];
622
-				$toDelete[$key] = $version['path'] . '.v' . $version['version'];
623
-			}
624
-		}
625
-
626
-		return [$toDelete, $size];
627
-	}
628
-
629
-	/**
630
-	 * get list of files we want to expire
631
-	 * @param array $versions list of versions
632
-	 * @param integer $time
633
-	 * @return array containing the list of to deleted versions and the size of them
634
-	 */
635
-	protected static function getAutoExpireList($time, $versions) {
636
-		$size = 0;
637
-		$toDelete = array();  // versions we want to delete
638
-
639
-		$interval = 1;
640
-		$step = Storage::$max_versions_per_interval[$interval]['step'];
641
-		if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
642
-			$nextInterval = -1;
643
-		} else {
644
-			$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
645
-		}
646
-
647
-		$firstVersion = reset($versions);
648
-		$firstKey = key($versions);
649
-		$prevTimestamp = $firstVersion['version'];
650
-		$nextVersion = $firstVersion['version'] - $step;
651
-		unset($versions[$firstKey]);
652
-
653
-		foreach ($versions as $key => $version) {
654
-			$newInterval = true;
655
-			while ($newInterval) {
656
-				if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
657
-					if ($version['version'] > $nextVersion) {
658
-						//distance between two version too small, mark to delete
659
-						$toDelete[$key] = $version['path'] . '.v' . $version['version'];
660
-						$size += $version['size'];
661
-						\OCP\Util::writeLog('files_versions', 'Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, \OCP\Util::INFO);
662
-					} else {
663
-						$nextVersion = $version['version'] - $step;
664
-						$prevTimestamp = $version['version'];
665
-					}
666
-					$newInterval = false; // version checked so we can move to the next one
667
-				} else { // time to move on to the next interval
668
-					$interval++;
669
-					$step = Storage::$max_versions_per_interval[$interval]['step'];
670
-					$nextVersion = $prevTimestamp - $step;
671
-					if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
672
-						$nextInterval = -1;
673
-					} else {
674
-						$nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
675
-					}
676
-					$newInterval = true; // we changed the interval -> check same version with new interval
677
-				}
678
-			}
679
-		}
680
-
681
-		return array($toDelete, $size);
682
-	}
683
-
684
-	/**
685
-	 * Schedule versions expiration for the given file
686
-	 *
687
-	 * @param string $uid owner of the file
688
-	 * @param string $fileName file/folder for which to schedule expiration
689
-	 */
690
-	private static function scheduleExpire($uid, $fileName) {
691
-		// let the admin disable auto expire
692
-		$expiration = self::getExpiration();
693
-		if ($expiration->isEnabled()) {
694
-			$command = new Expire($uid, $fileName);
695
-			\OC::$server->getCommandBus()->push($command);
696
-		}
697
-	}
698
-
699
-	/**
700
-	 * Expire versions which exceed the quota.
701
-	 *
702
-	 * This will setup the filesystem for the given user but will not
703
-	 * tear it down afterwards.
704
-	 *
705
-	 * @param string $filename path to file to expire
706
-	 * @param string $uid user for which to expire the version
707
-	 * @return bool|int|null
708
-	 */
709
-	public static function expire($filename, $uid) {
710
-		$expiration = self::getExpiration();
711
-
712
-		if ($expiration->isEnabled()) {
713
-			// get available disk space for user
714
-			$user = \OC::$server->getUserManager()->get($uid);
715
-			if (is_null($user)) {
716
-				\OCP\Util::writeLog('files_versions', 'Backends provided no user object for ' . $uid, \OCP\Util::ERROR);
717
-				throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid);
718
-			}
719
-
720
-			\OC_Util::setupFS($uid);
721
-
722
-			if (!Filesystem::file_exists($filename)) {
723
-				return false;
724
-			}
725
-
726
-			if (empty($filename)) {
727
-				// file maybe renamed or deleted
728
-				return false;
729
-			}
730
-			$versionsFileview = new View('/'.$uid.'/files_versions');
731
-
732
-			$softQuota = true;
733
-			$quota = $user->getQuota();
734
-			if ( $quota === null || $quota === 'none' ) {
735
-				$quota = Filesystem::free_space('/');
736
-				$softQuota = false;
737
-			} else {
738
-				$quota = \OCP\Util::computerFileSize($quota);
739
-			}
740
-
741
-			// make sure that we have the current size of the version history
742
-			$versionsSize = self::getVersionsSize($uid);
743
-
744
-			// calculate available space for version history
745
-			// subtract size of files and current versions size from quota
746
-			if ($quota >= 0) {
747
-				if ($softQuota) {
748
-					$files_view = new View('/' . $uid . '/files');
749
-					$rootInfo = $files_view->getFileInfo('/', false);
750
-					$free = $quota - $rootInfo['size']; // remaining free space for user
751
-					if ($free > 0) {
752
-						$availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions
753
-					} else {
754
-						$availableSpace = $free - $versionsSize;
755
-					}
756
-				} else {
757
-					$availableSpace = $quota;
758
-				}
759
-			} else {
760
-				$availableSpace = PHP_INT_MAX;
761
-			}
762
-
763
-			$allVersions = Storage::getVersions($uid, $filename);
764
-
765
-			$time = time();
766
-			list($toDelete, $sizeOfDeletedVersions) = self::getExpireList($time, $allVersions, $availableSpace <= 0);
767
-
768
-			$availableSpace = $availableSpace + $sizeOfDeletedVersions;
769
-			$versionsSize = $versionsSize - $sizeOfDeletedVersions;
770
-
771
-			// if still not enough free space we rearrange the versions from all files
772
-			if ($availableSpace <= 0) {
773
-				$result = Storage::getAllVersions($uid);
774
-				$allVersions = $result['all'];
775
-
776
-				foreach ($result['by_file'] as $versions) {
777
-					list($toDeleteNew, $size) = self::getExpireList($time, $versions, $availableSpace <= 0);
778
-					$toDelete = array_merge($toDelete, $toDeleteNew);
779
-					$sizeOfDeletedVersions += $size;
780
-				}
781
-				$availableSpace = $availableSpace + $sizeOfDeletedVersions;
782
-				$versionsSize = $versionsSize - $sizeOfDeletedVersions;
783
-			}
784
-
785
-			foreach($toDelete as $key => $path) {
786
-				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
787
-				self::deleteVersion($versionsFileview, $path);
788
-				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
789
-				unset($allVersions[$key]); // update array with the versions we keep
790
-				\OCP\Util::writeLog('files_versions', "Expire: " . $path, \OCP\Util::INFO);
791
-			}
792
-
793
-			// Check if enough space is available after versions are rearranged.
794
-			// If not we delete the oldest versions until we meet the size limit for versions,
795
-			// but always keep the two latest versions
796
-			$numOfVersions = count($allVersions) -2 ;
797
-			$i = 0;
798
-			// sort oldest first and make sure that we start at the first element
799
-			ksort($allVersions);
800
-			reset($allVersions);
801
-			while ($availableSpace < 0 && $i < $numOfVersions) {
802
-				$version = current($allVersions);
803
-				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
804
-				self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']);
805
-				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
806
-				\OCP\Util::writeLog('files_versions', 'running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'] , \OCP\Util::INFO);
807
-				$versionsSize -= $version['size'];
808
-				$availableSpace += $version['size'];
809
-				next($allVersions);
810
-				$i++;
811
-			}
812
-
813
-			return $versionsSize; // finally return the new size of the version history
814
-		}
815
-
816
-		return false;
817
-	}
818
-
819
-	/**
820
-	 * Create recursively missing directories inside of files_versions
821
-	 * that match the given path to a file.
822
-	 *
823
-	 * @param string $filename $path to a file, relative to the user's
824
-	 * "files" folder
825
-	 * @param View $view view on data/user/
826
-	 */
827
-	private static function createMissingDirectories($filename, $view) {
828
-		$dirname = Filesystem::normalizePath(dirname($filename));
829
-		$dirParts = explode('/', $dirname);
830
-		$dir = "/files_versions";
831
-		foreach ($dirParts as $part) {
832
-			$dir = $dir . '/' . $part;
833
-			if (!$view->file_exists($dir)) {
834
-				$view->mkdir($dir);
835
-			}
836
-		}
837
-	}
838
-
839
-	/**
840
-	 * Static workaround
841
-	 * @return Expiration
842
-	 */
843
-	protected static function getExpiration(){
844
-		if (is_null(self::$application)) {
845
-			self::$application = new Application();
846
-		}
847
-		return self::$application->getContainer()->query('Expiration');
848
-	}
57
+    const DEFAULTENABLED=true;
58
+    const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota
59
+    const VERSIONS_ROOT = 'files_versions/';
60
+
61
+    const DELETE_TRIGGER_MASTER_REMOVED = 0;
62
+    const DELETE_TRIGGER_RETENTION_CONSTRAINT = 1;
63
+    const DELETE_TRIGGER_QUOTA_EXCEEDED = 2;
64
+
65
+    // files for which we can remove the versions after the delete operation was successful
66
+    private static $deletedFiles = array();
67
+
68
+    private static $sourcePathAndUser = array();
69
+
70
+    private static $max_versions_per_interval = array(
71
+        //first 10sec, one version every 2sec
72
+        1 => array('intervalEndsAfter' => 10,      'step' => 2),
73
+        //next minute, one version every 10sec
74
+        2 => array('intervalEndsAfter' => 60,      'step' => 10),
75
+        //next hour, one version every minute
76
+        3 => array('intervalEndsAfter' => 3600,    'step' => 60),
77
+        //next 24h, one version every hour
78
+        4 => array('intervalEndsAfter' => 86400,   'step' => 3600),
79
+        //next 30days, one version per day
80
+        5 => array('intervalEndsAfter' => 2592000, 'step' => 86400),
81
+        //until the end one version per week
82
+        6 => array('intervalEndsAfter' => -1,      'step' => 604800),
83
+    );
84
+
85
+    /** @var \OCA\Files_Versions\AppInfo\Application */
86
+    private static $application;
87
+
88
+    /**
89
+     * get the UID of the owner of the file and the path to the file relative to
90
+     * owners files folder
91
+     *
92
+     * @param string $filename
93
+     * @return array
94
+     * @throws \OC\User\NoUserException
95
+     */
96
+    public static function getUidAndFilename($filename) {
97
+        $uid = Filesystem::getOwner($filename);
98
+        $userManager = \OC::$server->getUserManager();
99
+        // if the user with the UID doesn't exists, e.g. because the UID points
100
+        // to a remote user with a federated cloud ID we use the current logged-in
101
+        // user. We need a valid local user to create the versions
102
+        if (!$userManager->userExists($uid)) {
103
+            $uid = User::getUser();
104
+        }
105
+        Filesystem::initMountPoints($uid);
106
+        if ( $uid !== User::getUser() ) {
107
+            $info = Filesystem::getFileInfo($filename);
108
+            $ownerView = new View('/'.$uid.'/files');
109
+            try {
110
+                $filename = $ownerView->getPath($info['fileid']);
111
+                // make sure that the file name doesn't end with a trailing slash
112
+                // can for example happen single files shared across servers
113
+                $filename = rtrim($filename, '/');
114
+            } catch (NotFoundException $e) {
115
+                $filename = null;
116
+            }
117
+        }
118
+        return [$uid, $filename];
119
+    }
120
+
121
+    /**
122
+     * Remember the owner and the owner path of the source file
123
+     *
124
+     * @param string $source source path
125
+     */
126
+    public static function setSourcePathAndUser($source) {
127
+        list($uid, $path) = self::getUidAndFilename($source);
128
+        self::$sourcePathAndUser[$source] = array('uid' => $uid, 'path' => $path);
129
+    }
130
+
131
+    /**
132
+     * Gets the owner and the owner path from the source path
133
+     *
134
+     * @param string $source source path
135
+     * @return array with user id and path
136
+     */
137
+    public static function getSourcePathAndUser($source) {
138
+
139
+        if (isset(self::$sourcePathAndUser[$source])) {
140
+            $uid = self::$sourcePathAndUser[$source]['uid'];
141
+            $path = self::$sourcePathAndUser[$source]['path'];
142
+            unset(self::$sourcePathAndUser[$source]);
143
+        } else {
144
+            $uid = $path = false;
145
+        }
146
+        return array($uid, $path);
147
+    }
148
+
149
+    /**
150
+     * get current size of all versions from a given user
151
+     *
152
+     * @param string $user user who owns the versions
153
+     * @return int versions size
154
+     */
155
+    private static function getVersionsSize($user) {
156
+        $view = new View('/' . $user);
157
+        $fileInfo = $view->getFileInfo('/files_versions');
158
+        return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
159
+    }
160
+
161
+    /**
162
+     * store a new version of a file.
163
+     */
164
+    public static function store($filename) {
165
+
166
+        // if the file gets streamed we need to remove the .part extension
167
+        // to get the right target
168
+        $ext = pathinfo($filename, PATHINFO_EXTENSION);
169
+        if ($ext === 'part') {
170
+            $filename = substr($filename, 0, strlen($filename) - 5);
171
+        }
172
+
173
+        // we only handle existing files
174
+        if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
175
+            return false;
176
+        }
177
+
178
+        list($uid, $filename) = self::getUidAndFilename($filename);
179
+
180
+        $files_view = new View('/'.$uid .'/files');
181
+        $users_view = new View('/'.$uid);
182
+
183
+        $eventDispatcher = \OC::$server->getEventDispatcher();
184
+        $id = $files_view->getFileInfo($filename)->getId();
185
+        $nodes = \OC::$server->getRootFolder()->getById($id);
186
+        foreach ($nodes as $node) {
187
+            $event = new CreateVersionEvent($node);
188
+            $eventDispatcher->dispatch('OCA\Files_Versions::createVersion', $event);
189
+            if ($event->shouldCreateVersion() === false) {
190
+                return false;
191
+            }
192
+        }
193
+
194
+        // no use making versions for empty files
195
+        if ($files_view->filesize($filename) === 0) {
196
+            return false;
197
+        }
198
+
199
+        // create all parent folders
200
+        self::createMissingDirectories($filename, $users_view);
201
+
202
+        self::scheduleExpire($uid, $filename);
203
+
204
+        // store a new version of a file
205
+        $mtime = $users_view->filemtime('files/' . $filename);
206
+        $users_view->copy('files/' . $filename, 'files_versions/' . $filename . '.v' . $mtime);
207
+        // call getFileInfo to enforce a file cache entry for the new version
208
+        $users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
209
+    }
210
+
211
+
212
+    /**
213
+     * mark file as deleted so that we can remove the versions if the file is gone
214
+     * @param string $path
215
+     */
216
+    public static function markDeletedFile($path) {
217
+        list($uid, $filename) = self::getUidAndFilename($path);
218
+        self::$deletedFiles[$path] = array(
219
+            'uid' => $uid,
220
+            'filename' => $filename);
221
+    }
222
+
223
+    /**
224
+     * delete the version from the storage and cache
225
+     *
226
+     * @param View $view
227
+     * @param string $path
228
+     */
229
+    protected static function deleteVersion($view, $path) {
230
+        $view->unlink($path);
231
+        /**
232
+         * @var \OC\Files\Storage\Storage $storage
233
+         * @var string $internalPath
234
+         */
235
+        list($storage, $internalPath) = $view->resolvePath($path);
236
+        $cache = $storage->getCache($internalPath);
237
+        $cache->remove($internalPath);
238
+    }
239
+
240
+    /**
241
+     * Delete versions of a file
242
+     */
243
+    public static function delete($path) {
244
+
245
+        $deletedFile = self::$deletedFiles[$path];
246
+        $uid = $deletedFile['uid'];
247
+        $filename = $deletedFile['filename'];
248
+
249
+        if (!Filesystem::file_exists($path)) {
250
+
251
+            $view = new View('/' . $uid . '/files_versions');
252
+
253
+            $versions = self::getVersions($uid, $filename);
254
+            if (!empty($versions)) {
255
+                foreach ($versions as $v) {
256
+                    \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
257
+                    self::deleteVersion($view, $filename . '.v' . $v['version']);
258
+                    \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
259
+                }
260
+            }
261
+        }
262
+        unset(self::$deletedFiles[$path]);
263
+    }
264
+
265
+    /**
266
+     * Rename or copy versions of a file of the given paths
267
+     *
268
+     * @param string $sourcePath source path of the file to move, relative to
269
+     * the currently logged in user's "files" folder
270
+     * @param string $targetPath target path of the file to move, relative to
271
+     * the currently logged in user's "files" folder
272
+     * @param string $operation can be 'copy' or 'rename'
273
+     */
274
+    public static function renameOrCopy($sourcePath, $targetPath, $operation) {
275
+        list($sourceOwner, $sourcePath) = self::getSourcePathAndUser($sourcePath);
276
+
277
+        // it was a upload of a existing file if no old path exists
278
+        // in this case the pre-hook already called the store method and we can
279
+        // stop here
280
+        if ($sourcePath === false) {
281
+            return true;
282
+        }
283
+
284
+        list($targetOwner, $targetPath) = self::getUidAndFilename($targetPath);
285
+
286
+        $sourcePath = ltrim($sourcePath, '/');
287
+        $targetPath = ltrim($targetPath, '/');
288
+
289
+        $rootView = new View('');
290
+
291
+        // did we move a directory ?
292
+        if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
293
+            // does the directory exists for versions too ?
294
+            if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) {
295
+                // create missing dirs if necessary
296
+                self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
297
+
298
+                // move the directory containing the versions
299
+                $rootView->$operation(
300
+                    '/' . $sourceOwner . '/files_versions/' . $sourcePath,
301
+                    '/' . $targetOwner . '/files_versions/' . $targetPath
302
+                );
303
+            }
304
+        } else if ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) {
305
+            // create missing dirs if necessary
306
+            self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
307
+
308
+            foreach ($versions as $v) {
309
+                // move each version one by one to the target directory
310
+                $rootView->$operation(
311
+                    '/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'],
312
+                    '/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version']
313
+                );
314
+            }
315
+        }
316
+
317
+        // if we moved versions directly for a file, schedule expiration check for that file
318
+        if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
319
+            self::scheduleExpire($targetOwner, $targetPath);
320
+        }
321
+
322
+    }
323
+
324
+    /**
325
+     * Rollback to an old version of a file.
326
+     *
327
+     * @param string $file file name
328
+     * @param int $revision revision timestamp
329
+     * @return bool
330
+     */
331
+    public static function rollback($file, $revision) {
332
+
333
+        // add expected leading slash
334
+        $file = '/' . ltrim($file, '/');
335
+        list($uid, $filename) = self::getUidAndFilename($file);
336
+        if ($uid === null || trim($filename, '/') === '') {
337
+            return false;
338
+        }
339
+
340
+        $users_view = new View('/'.$uid);
341
+        $files_view = new View('/'. User::getUser().'/files');
342
+
343
+        $versionCreated = false;
344
+
345
+        $fileInfo = $files_view->getFileInfo($file);
346
+
347
+        // check if user has the permissions to revert a version
348
+        if (!$fileInfo->isUpdateable()) {
349
+            return false;
350
+        }
351
+
352
+        //first create a new version
353
+        $version = 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename);
354
+        if (!$users_view->file_exists($version)) {
355
+            $users_view->copy('files'.$filename, 'files_versions'.$filename.'.v'.$users_view->filemtime('files'.$filename));
356
+            $versionCreated = true;
357
+        }
358
+
359
+        $fileToRestore =  'files_versions' . $filename . '.v' . $revision;
360
+
361
+        // Restore encrypted version of the old file for the newly restored file
362
+        // This has to happen manually here since the file is manually copied below
363
+        $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion();
364
+        $oldFileInfo = $users_view->getFileInfo($fileToRestore);
365
+        $cache = $fileInfo->getStorage()->getCache();
366
+        $cache->update(
367
+            $fileInfo->getId(), [
368
+                'encrypted' => $oldVersion,
369
+                'encryptedVersion' => $oldVersion,
370
+                'size' => $oldFileInfo->getSize()
371
+            ]
372
+        );
373
+
374
+        // rollback
375
+        if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) {
376
+            $files_view->touch($file, $revision);
377
+            Storage::scheduleExpire($uid, $file);
378
+            \OC_Hook::emit('\OCP\Versions', 'rollback', array(
379
+                'path' => $filename,
380
+                'revision' => $revision,
381
+            ));
382
+            return true;
383
+        } else if ($versionCreated) {
384
+            self::deleteVersion($users_view, $version);
385
+        }
386
+
387
+        return false;
388
+
389
+    }
390
+
391
+    /**
392
+     * Stream copy file contents from $path1 to $path2
393
+     *
394
+     * @param View $view view to use for copying
395
+     * @param string $path1 source file to copy
396
+     * @param string $path2 target file
397
+     *
398
+     * @return bool true for success, false otherwise
399
+     */
400
+    private static function copyFileContents($view, $path1, $path2) {
401
+        /** @var \OC\Files\Storage\Storage $storage1 */
402
+        list($storage1, $internalPath1) = $view->resolvePath($path1);
403
+        /** @var \OC\Files\Storage\Storage $storage2 */
404
+        list($storage2, $internalPath2) = $view->resolvePath($path2);
405
+
406
+        $view->lockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
407
+        $view->lockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
408
+
409
+        // TODO add a proper way of overwriting a file while maintaining file ids
410
+        if ($storage1->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage') || $storage2->instanceOfStorage('\OC\Files\ObjectStore\ObjectStoreStorage')) {
411
+            $source = $storage1->fopen($internalPath1, 'r');
412
+            $target = $storage2->fopen($internalPath2, 'w');
413
+            list(, $result) = \OC_Helper::streamCopy($source, $target);
414
+            fclose($source);
415
+            fclose($target);
416
+
417
+            if ($result !== false) {
418
+                $storage1->unlink($internalPath1);
419
+            }
420
+        } else {
421
+            $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
422
+        }
423
+
424
+        $view->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
425
+        $view->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
426
+
427
+        return ($result !== false);
428
+    }
429
+
430
+    /**
431
+     * get a list of all available versions of a file in descending chronological order
432
+     * @param string $uid user id from the owner of the file
433
+     * @param string $filename file to find versions of, relative to the user files dir
434
+     * @param string $userFullPath
435
+     * @return array versions newest version first
436
+     */
437
+    public static function getVersions($uid, $filename, $userFullPath = '') {
438
+        $versions = array();
439
+        if (empty($filename)) {
440
+            return $versions;
441
+        }
442
+        // fetch for old versions
443
+        $view = new View('/' . $uid . '/');
444
+
445
+        $pathinfo = pathinfo($filename);
446
+        $versionedFile = $pathinfo['basename'];
447
+
448
+        $dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']);
449
+
450
+        $dirContent = false;
451
+        if ($view->is_dir($dir)) {
452
+            $dirContent = $view->opendir($dir);
453
+        }
454
+
455
+        if ($dirContent === false) {
456
+            return $versions;
457
+        }
458
+
459
+        if (is_resource($dirContent)) {
460
+            while (($entryName = readdir($dirContent)) !== false) {
461
+                if (!Filesystem::isIgnoredDir($entryName)) {
462
+                    $pathparts = pathinfo($entryName);
463
+                    $filename = $pathparts['filename'];
464
+                    if ($filename === $versionedFile) {
465
+                        $pathparts = pathinfo($entryName);
466
+                        $timestamp = substr($pathparts['extension'], 1);
467
+                        $filename = $pathparts['filename'];
468
+                        $key = $timestamp . '#' . $filename;
469
+                        $versions[$key]['version'] = $timestamp;
470
+                        $versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp);
471
+                        if (empty($userFullPath)) {
472
+                            $versions[$key]['preview'] = '';
473
+                        } else {
474
+                            $versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]);
475
+                        }
476
+                        $versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
477
+                        $versions[$key]['name'] = $versionedFile;
478
+                        $versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
479
+                        $versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile);
480
+                    }
481
+                }
482
+            }
483
+            closedir($dirContent);
484
+        }
485
+
486
+        // sort with newest version first
487
+        krsort($versions);
488
+
489
+        return $versions;
490
+    }
491
+
492
+    /**
493
+     * Expire versions that older than max version retention time
494
+     * @param string $uid
495
+     */
496
+    public static function expireOlderThanMaxForUser($uid){
497
+        $expiration = self::getExpiration();
498
+        $threshold = $expiration->getMaxAgeAsTimestamp();
499
+        $versions = self::getAllVersions($uid);
500
+        if (!$threshold || !array_key_exists('all', $versions)) {
501
+            return;
502
+        }
503
+
504
+        $toDelete = [];
505
+        foreach (array_reverse($versions['all']) as $key => $version) {
506
+            if (intval($version['version'])<$threshold) {
507
+                $toDelete[$key] = $version;
508
+            } else {
509
+                //Versions are sorted by time - nothing mo to iterate.
510
+                break;
511
+            }
512
+        }
513
+
514
+        $view = new View('/' . $uid . '/files_versions');
515
+        if (!empty($toDelete)) {
516
+            foreach ($toDelete as $version) {
517
+                \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
518
+                self::deleteVersion($view, $version['path'] . '.v' . $version['version']);
519
+                \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
520
+            }
521
+        }
522
+    }
523
+
524
+    /**
525
+     * translate a timestamp into a string like "5 days ago"
526
+     * @param int $timestamp
527
+     * @return string for example "5 days ago"
528
+     */
529
+    private static function getHumanReadableTimestamp($timestamp) {
530
+
531
+        $diff = time() - $timestamp;
532
+
533
+        if ($diff < 60) { // first minute
534
+            return  $diff . " seconds ago";
535
+        } elseif ($diff < 3600) { //first hour
536
+            return round($diff / 60) . " minutes ago";
537
+        } elseif ($diff < 86400) { // first day
538
+            return round($diff / 3600) . " hours ago";
539
+        } elseif ($diff < 604800) { //first week
540
+            return round($diff / 86400) . " days ago";
541
+        } elseif ($diff < 2419200) { //first month
542
+            return round($diff / 604800) . " weeks ago";
543
+        } elseif ($diff < 29030400) { // first year
544
+            return round($diff / 2419200) . " months ago";
545
+        } else {
546
+            return round($diff / 29030400) . " years ago";
547
+        }
548
+
549
+    }
550
+
551
+    /**
552
+     * returns all stored file versions from a given user
553
+     * @param string $uid id of the user
554
+     * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename
555
+     */
556
+    private static function getAllVersions($uid) {
557
+        $view = new View('/' . $uid . '/');
558
+        $dirs = array(self::VERSIONS_ROOT);
559
+        $versions = array();
560
+
561
+        while (!empty($dirs)) {
562
+            $dir = array_pop($dirs);
563
+            $files = $view->getDirectoryContent($dir);
564
+
565
+            foreach ($files as $file) {
566
+                $fileData = $file->getData();
567
+                $filePath = $dir . '/' . $fileData['name'];
568
+                if ($file['type'] === 'dir') {
569
+                    $dirs[] = $filePath;
570
+                } else {
571
+                    $versionsBegin = strrpos($filePath, '.v');
572
+                    $relPathStart = strlen(self::VERSIONS_ROOT);
573
+                    $version = substr($filePath, $versionsBegin + 2);
574
+                    $relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart);
575
+                    $key = $version . '#' . $relpath;
576
+                    $versions[$key] = array('path' => $relpath, 'timestamp' => $version);
577
+                }
578
+            }
579
+        }
580
+
581
+        // newest version first
582
+        krsort($versions);
583
+
584
+        $result = array();
585
+
586
+        foreach ($versions as $key => $value) {
587
+            $size = $view->filesize(self::VERSIONS_ROOT.'/'.$value['path'].'.v'.$value['timestamp']);
588
+            $filename = $value['path'];
589
+
590
+            $result['all'][$key]['version'] = $value['timestamp'];
591
+            $result['all'][$key]['path'] = $filename;
592
+            $result['all'][$key]['size'] = $size;
593
+
594
+            $result['by_file'][$filename][$key]['version'] = $value['timestamp'];
595
+            $result['by_file'][$filename][$key]['path'] = $filename;
596
+            $result['by_file'][$filename][$key]['size'] = $size;
597
+        }
598
+
599
+        return $result;
600
+    }
601
+
602
+    /**
603
+     * get list of files we want to expire
604
+     * @param array $versions list of versions
605
+     * @param integer $time
606
+     * @param bool $quotaExceeded is versions storage limit reached
607
+     * @return array containing the list of to deleted versions and the size of them
608
+     */
609
+    protected static function getExpireList($time, $versions, $quotaExceeded = false) {
610
+        $expiration = self::getExpiration();
611
+
612
+        if ($expiration->shouldAutoExpire()) {
613
+            list($toDelete, $size) = self::getAutoExpireList($time, $versions);
614
+        } else {
615
+            $size = 0;
616
+            $toDelete = [];  // versions we want to delete
617
+        }
618
+
619
+        foreach ($versions as $key => $version) {
620
+            if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) {
621
+                $size += $version['size'];
622
+                $toDelete[$key] = $version['path'] . '.v' . $version['version'];
623
+            }
624
+        }
625
+
626
+        return [$toDelete, $size];
627
+    }
628
+
629
+    /**
630
+     * get list of files we want to expire
631
+     * @param array $versions list of versions
632
+     * @param integer $time
633
+     * @return array containing the list of to deleted versions and the size of them
634
+     */
635
+    protected static function getAutoExpireList($time, $versions) {
636
+        $size = 0;
637
+        $toDelete = array();  // versions we want to delete
638
+
639
+        $interval = 1;
640
+        $step = Storage::$max_versions_per_interval[$interval]['step'];
641
+        if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
642
+            $nextInterval = -1;
643
+        } else {
644
+            $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
645
+        }
646
+
647
+        $firstVersion = reset($versions);
648
+        $firstKey = key($versions);
649
+        $prevTimestamp = $firstVersion['version'];
650
+        $nextVersion = $firstVersion['version'] - $step;
651
+        unset($versions[$firstKey]);
652
+
653
+        foreach ($versions as $key => $version) {
654
+            $newInterval = true;
655
+            while ($newInterval) {
656
+                if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
657
+                    if ($version['version'] > $nextVersion) {
658
+                        //distance between two version too small, mark to delete
659
+                        $toDelete[$key] = $version['path'] . '.v' . $version['version'];
660
+                        $size += $version['size'];
661
+                        \OCP\Util::writeLog('files_versions', 'Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, \OCP\Util::INFO);
662
+                    } else {
663
+                        $nextVersion = $version['version'] - $step;
664
+                        $prevTimestamp = $version['version'];
665
+                    }
666
+                    $newInterval = false; // version checked so we can move to the next one
667
+                } else { // time to move on to the next interval
668
+                    $interval++;
669
+                    $step = Storage::$max_versions_per_interval[$interval]['step'];
670
+                    $nextVersion = $prevTimestamp - $step;
671
+                    if (Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'] === -1) {
672
+                        $nextInterval = -1;
673
+                    } else {
674
+                        $nextInterval = $time - Storage::$max_versions_per_interval[$interval]['intervalEndsAfter'];
675
+                    }
676
+                    $newInterval = true; // we changed the interval -> check same version with new interval
677
+                }
678
+            }
679
+        }
680
+
681
+        return array($toDelete, $size);
682
+    }
683
+
684
+    /**
685
+     * Schedule versions expiration for the given file
686
+     *
687
+     * @param string $uid owner of the file
688
+     * @param string $fileName file/folder for which to schedule expiration
689
+     */
690
+    private static function scheduleExpire($uid, $fileName) {
691
+        // let the admin disable auto expire
692
+        $expiration = self::getExpiration();
693
+        if ($expiration->isEnabled()) {
694
+            $command = new Expire($uid, $fileName);
695
+            \OC::$server->getCommandBus()->push($command);
696
+        }
697
+    }
698
+
699
+    /**
700
+     * Expire versions which exceed the quota.
701
+     *
702
+     * This will setup the filesystem for the given user but will not
703
+     * tear it down afterwards.
704
+     *
705
+     * @param string $filename path to file to expire
706
+     * @param string $uid user for which to expire the version
707
+     * @return bool|int|null
708
+     */
709
+    public static function expire($filename, $uid) {
710
+        $expiration = self::getExpiration();
711
+
712
+        if ($expiration->isEnabled()) {
713
+            // get available disk space for user
714
+            $user = \OC::$server->getUserManager()->get($uid);
715
+            if (is_null($user)) {
716
+                \OCP\Util::writeLog('files_versions', 'Backends provided no user object for ' . $uid, \OCP\Util::ERROR);
717
+                throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid);
718
+            }
719
+
720
+            \OC_Util::setupFS($uid);
721
+
722
+            if (!Filesystem::file_exists($filename)) {
723
+                return false;
724
+            }
725
+
726
+            if (empty($filename)) {
727
+                // file maybe renamed or deleted
728
+                return false;
729
+            }
730
+            $versionsFileview = new View('/'.$uid.'/files_versions');
731
+
732
+            $softQuota = true;
733
+            $quota = $user->getQuota();
734
+            if ( $quota === null || $quota === 'none' ) {
735
+                $quota = Filesystem::free_space('/');
736
+                $softQuota = false;
737
+            } else {
738
+                $quota = \OCP\Util::computerFileSize($quota);
739
+            }
740
+
741
+            // make sure that we have the current size of the version history
742
+            $versionsSize = self::getVersionsSize($uid);
743
+
744
+            // calculate available space for version history
745
+            // subtract size of files and current versions size from quota
746
+            if ($quota >= 0) {
747
+                if ($softQuota) {
748
+                    $files_view = new View('/' . $uid . '/files');
749
+                    $rootInfo = $files_view->getFileInfo('/', false);
750
+                    $free = $quota - $rootInfo['size']; // remaining free space for user
751
+                    if ($free > 0) {
752
+                        $availableSpace = ($free * self::DEFAULTMAXSIZE / 100) - $versionsSize; // how much space can be used for versions
753
+                    } else {
754
+                        $availableSpace = $free - $versionsSize;
755
+                    }
756
+                } else {
757
+                    $availableSpace = $quota;
758
+                }
759
+            } else {
760
+                $availableSpace = PHP_INT_MAX;
761
+            }
762
+
763
+            $allVersions = Storage::getVersions($uid, $filename);
764
+
765
+            $time = time();
766
+            list($toDelete, $sizeOfDeletedVersions) = self::getExpireList($time, $allVersions, $availableSpace <= 0);
767
+
768
+            $availableSpace = $availableSpace + $sizeOfDeletedVersions;
769
+            $versionsSize = $versionsSize - $sizeOfDeletedVersions;
770
+
771
+            // if still not enough free space we rearrange the versions from all files
772
+            if ($availableSpace <= 0) {
773
+                $result = Storage::getAllVersions($uid);
774
+                $allVersions = $result['all'];
775
+
776
+                foreach ($result['by_file'] as $versions) {
777
+                    list($toDeleteNew, $size) = self::getExpireList($time, $versions, $availableSpace <= 0);
778
+                    $toDelete = array_merge($toDelete, $toDeleteNew);
779
+                    $sizeOfDeletedVersions += $size;
780
+                }
781
+                $availableSpace = $availableSpace + $sizeOfDeletedVersions;
782
+                $versionsSize = $versionsSize - $sizeOfDeletedVersions;
783
+            }
784
+
785
+            foreach($toDelete as $key => $path) {
786
+                \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
787
+                self::deleteVersion($versionsFileview, $path);
788
+                \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
789
+                unset($allVersions[$key]); // update array with the versions we keep
790
+                \OCP\Util::writeLog('files_versions', "Expire: " . $path, \OCP\Util::INFO);
791
+            }
792
+
793
+            // Check if enough space is available after versions are rearranged.
794
+            // If not we delete the oldest versions until we meet the size limit for versions,
795
+            // but always keep the two latest versions
796
+            $numOfVersions = count($allVersions) -2 ;
797
+            $i = 0;
798
+            // sort oldest first and make sure that we start at the first element
799
+            ksort($allVersions);
800
+            reset($allVersions);
801
+            while ($availableSpace < 0 && $i < $numOfVersions) {
802
+                $version = current($allVersions);
803
+                \OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
804
+                self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']);
805
+                \OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
806
+                \OCP\Util::writeLog('files_versions', 'running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'] , \OCP\Util::INFO);
807
+                $versionsSize -= $version['size'];
808
+                $availableSpace += $version['size'];
809
+                next($allVersions);
810
+                $i++;
811
+            }
812
+
813
+            return $versionsSize; // finally return the new size of the version history
814
+        }
815
+
816
+        return false;
817
+    }
818
+
819
+    /**
820
+     * Create recursively missing directories inside of files_versions
821
+     * that match the given path to a file.
822
+     *
823
+     * @param string $filename $path to a file, relative to the user's
824
+     * "files" folder
825
+     * @param View $view view on data/user/
826
+     */
827
+    private static function createMissingDirectories($filename, $view) {
828
+        $dirname = Filesystem::normalizePath(dirname($filename));
829
+        $dirParts = explode('/', $dirname);
830
+        $dir = "/files_versions";
831
+        foreach ($dirParts as $part) {
832
+            $dir = $dir . '/' . $part;
833
+            if (!$view->file_exists($dir)) {
834
+                $view->mkdir($dir);
835
+            }
836
+        }
837
+    }
838
+
839
+    /**
840
+     * Static workaround
841
+     * @return Expiration
842
+     */
843
+    protected static function getExpiration(){
844
+        if (is_null(self::$application)) {
845
+            self::$application = new Application();
846
+        }
847
+        return self::$application->getContainer()->query('Expiration');
848
+    }
849 849
 
850 850
 }
Please login to merge, or discard this patch.
Spacing   +67 added lines, -67 removed lines patch added patch discarded remove patch
@@ -54,8 +54,8 @@  discard block
 block discarded – undo
54 54
 
55 55
 class Storage {
56 56
 
57
-	const DEFAULTENABLED=true;
58
-	const DEFAULTMAXSIZE=50; // unit: percentage; 50% of available disk space/quota
57
+	const DEFAULTENABLED = true;
58
+	const DEFAULTMAXSIZE = 50; // unit: percentage; 50% of available disk space/quota
59 59
 	const VERSIONS_ROOT = 'files_versions/';
60 60
 
61 61
 	const DELETE_TRIGGER_MASTER_REMOVED = 0;
@@ -69,17 +69,17 @@  discard block
 block discarded – undo
69 69
 
70 70
 	private static $max_versions_per_interval = array(
71 71
 		//first 10sec, one version every 2sec
72
-		1 => array('intervalEndsAfter' => 10,      'step' => 2),
72
+		1 => array('intervalEndsAfter' => 10, 'step' => 2),
73 73
 		//next minute, one version every 10sec
74
-		2 => array('intervalEndsAfter' => 60,      'step' => 10),
74
+		2 => array('intervalEndsAfter' => 60, 'step' => 10),
75 75
 		//next hour, one version every minute
76
-		3 => array('intervalEndsAfter' => 3600,    'step' => 60),
76
+		3 => array('intervalEndsAfter' => 3600, 'step' => 60),
77 77
 		//next 24h, one version every hour
78
-		4 => array('intervalEndsAfter' => 86400,   'step' => 3600),
78
+		4 => array('intervalEndsAfter' => 86400, 'step' => 3600),
79 79
 		//next 30days, one version per day
80 80
 		5 => array('intervalEndsAfter' => 2592000, 'step' => 86400),
81 81
 		//until the end one version per week
82
-		6 => array('intervalEndsAfter' => -1,      'step' => 604800),
82
+		6 => array('intervalEndsAfter' => -1, 'step' => 604800),
83 83
 	);
84 84
 
85 85
 	/** @var \OCA\Files_Versions\AppInfo\Application */
@@ -103,7 +103,7 @@  discard block
 block discarded – undo
103 103
 			$uid = User::getUser();
104 104
 		}
105 105
 		Filesystem::initMountPoints($uid);
106
-		if ( $uid !== User::getUser() ) {
106
+		if ($uid !== User::getUser()) {
107 107
 			$info = Filesystem::getFileInfo($filename);
108 108
 			$ownerView = new View('/'.$uid.'/files');
109 109
 			try {
@@ -153,7 +153,7 @@  discard block
 block discarded – undo
153 153
 	 * @return int versions size
154 154
 	 */
155 155
 	private static function getVersionsSize($user) {
156
-		$view = new View('/' . $user);
156
+		$view = new View('/'.$user);
157 157
 		$fileInfo = $view->getFileInfo('/files_versions');
158 158
 		return isset($fileInfo['size']) ? $fileInfo['size'] : 0;
159 159
 	}
@@ -171,13 +171,13 @@  discard block
 block discarded – undo
171 171
 		}
172 172
 
173 173
 		// we only handle existing files
174
-		if (! Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
174
+		if (!Filesystem::file_exists($filename) || Filesystem::is_dir($filename)) {
175 175
 			return false;
176 176
 		}
177 177
 
178 178
 		list($uid, $filename) = self::getUidAndFilename($filename);
179 179
 
180
-		$files_view = new View('/'.$uid .'/files');
180
+		$files_view = new View('/'.$uid.'/files');
181 181
 		$users_view = new View('/'.$uid);
182 182
 
183 183
 		$eventDispatcher = \OC::$server->getEventDispatcher();
@@ -202,10 +202,10 @@  discard block
 block discarded – undo
202 202
 		self::scheduleExpire($uid, $filename);
203 203
 
204 204
 		// store a new version of a file
205
-		$mtime = $users_view->filemtime('files/' . $filename);
206
-		$users_view->copy('files/' . $filename, 'files_versions/' . $filename . '.v' . $mtime);
205
+		$mtime = $users_view->filemtime('files/'.$filename);
206
+		$users_view->copy('files/'.$filename, 'files_versions/'.$filename.'.v'.$mtime);
207 207
 		// call getFileInfo to enforce a file cache entry for the new version
208
-		$users_view->getFileInfo('files_versions/' . $filename . '.v' . $mtime);
208
+		$users_view->getFileInfo('files_versions/'.$filename.'.v'.$mtime);
209 209
 	}
210 210
 
211 211
 
@@ -248,14 +248,14 @@  discard block
 block discarded – undo
248 248
 
249 249
 		if (!Filesystem::file_exists($path)) {
250 250
 
251
-			$view = new View('/' . $uid . '/files_versions');
251
+			$view = new View('/'.$uid.'/files_versions');
252 252
 
253 253
 			$versions = self::getVersions($uid, $filename);
254 254
 			if (!empty($versions)) {
255 255
 				foreach ($versions as $v) {
256
-					\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
257
-					self::deleteVersion($view, $filename . '.v' . $v['version']);
258
-					\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path . $v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
256
+					\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path.$v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
257
+					self::deleteVersion($view, $filename.'.v'.$v['version']);
258
+					\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path.$v['version'], 'trigger' => self::DELETE_TRIGGER_MASTER_REMOVED));
259 259
 				}
260 260
 			}
261 261
 		}
@@ -289,33 +289,33 @@  discard block
 block discarded – undo
289 289
 		$rootView = new View('');
290 290
 
291 291
 		// did we move a directory ?
292
-		if ($rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
292
+		if ($rootView->is_dir('/'.$targetOwner.'/files/'.$targetPath)) {
293 293
 			// does the directory exists for versions too ?
294
-			if ($rootView->is_dir('/' . $sourceOwner . '/files_versions/' . $sourcePath)) {
294
+			if ($rootView->is_dir('/'.$sourceOwner.'/files_versions/'.$sourcePath)) {
295 295
 				// create missing dirs if necessary
296
-				self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
296
+				self::createMissingDirectories($targetPath, new View('/'.$targetOwner));
297 297
 
298 298
 				// move the directory containing the versions
299 299
 				$rootView->$operation(
300
-					'/' . $sourceOwner . '/files_versions/' . $sourcePath,
301
-					'/' . $targetOwner . '/files_versions/' . $targetPath
300
+					'/'.$sourceOwner.'/files_versions/'.$sourcePath,
301
+					'/'.$targetOwner.'/files_versions/'.$targetPath
302 302
 				);
303 303
 			}
304
-		} else if ($versions = Storage::getVersions($sourceOwner, '/' . $sourcePath)) {
304
+		} else if ($versions = Storage::getVersions($sourceOwner, '/'.$sourcePath)) {
305 305
 			// create missing dirs if necessary
306
-			self::createMissingDirectories($targetPath, new View('/'. $targetOwner));
306
+			self::createMissingDirectories($targetPath, new View('/'.$targetOwner));
307 307
 
308 308
 			foreach ($versions as $v) {
309 309
 				// move each version one by one to the target directory
310 310
 				$rootView->$operation(
311
-					'/' . $sourceOwner . '/files_versions/' . $sourcePath.'.v' . $v['version'],
312
-					'/' . $targetOwner . '/files_versions/' . $targetPath.'.v'.$v['version']
311
+					'/'.$sourceOwner.'/files_versions/'.$sourcePath.'.v'.$v['version'],
312
+					'/'.$targetOwner.'/files_versions/'.$targetPath.'.v'.$v['version']
313 313
 				);
314 314
 			}
315 315
 		}
316 316
 
317 317
 		// if we moved versions directly for a file, schedule expiration check for that file
318
-		if (!$rootView->is_dir('/' . $targetOwner . '/files/' . $targetPath)) {
318
+		if (!$rootView->is_dir('/'.$targetOwner.'/files/'.$targetPath)) {
319 319
 			self::scheduleExpire($targetOwner, $targetPath);
320 320
 		}
321 321
 
@@ -331,14 +331,14 @@  discard block
 block discarded – undo
331 331
 	public static function rollback($file, $revision) {
332 332
 
333 333
 		// add expected leading slash
334
-		$file = '/' . ltrim($file, '/');
334
+		$file = '/'.ltrim($file, '/');
335 335
 		list($uid, $filename) = self::getUidAndFilename($file);
336 336
 		if ($uid === null || trim($filename, '/') === '') {
337 337
 			return false;
338 338
 		}
339 339
 
340 340
 		$users_view = new View('/'.$uid);
341
-		$files_view = new View('/'. User::getUser().'/files');
341
+		$files_view = new View('/'.User::getUser().'/files');
342 342
 
343 343
 		$versionCreated = false;
344 344
 
@@ -356,7 +356,7 @@  discard block
 block discarded – undo
356 356
 			$versionCreated = true;
357 357
 		}
358 358
 
359
-		$fileToRestore =  'files_versions' . $filename . '.v' . $revision;
359
+		$fileToRestore = 'files_versions'.$filename.'.v'.$revision;
360 360
 
361 361
 		// Restore encrypted version of the old file for the newly restored file
362 362
 		// This has to happen manually here since the file is manually copied below
@@ -372,7 +372,7 @@  discard block
 block discarded – undo
372 372
 		);
373 373
 
374 374
 		// rollback
375
-		if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) {
375
+		if (self::copyFileContents($users_view, $fileToRestore, 'files'.$filename)) {
376 376
 			$files_view->touch($file, $revision);
377 377
 			Storage::scheduleExpire($uid, $file);
378 378
 			\OC_Hook::emit('\OCP\Versions', 'rollback', array(
@@ -440,12 +440,12 @@  discard block
 block discarded – undo
440 440
 			return $versions;
441 441
 		}
442 442
 		// fetch for old versions
443
-		$view = new View('/' . $uid . '/');
443
+		$view = new View('/'.$uid.'/');
444 444
 
445 445
 		$pathinfo = pathinfo($filename);
446 446
 		$versionedFile = $pathinfo['basename'];
447 447
 
448
-		$dir = Filesystem::normalizePath(self::VERSIONS_ROOT . '/' . $pathinfo['dirname']);
448
+		$dir = Filesystem::normalizePath(self::VERSIONS_ROOT.'/'.$pathinfo['dirname']);
449 449
 
450 450
 		$dirContent = false;
451 451
 		if ($view->is_dir($dir)) {
@@ -465,7 +465,7 @@  discard block
 block discarded – undo
465 465
 						$pathparts = pathinfo($entryName);
466 466
 						$timestamp = substr($pathparts['extension'], 1);
467 467
 						$filename = $pathparts['filename'];
468
-						$key = $timestamp . '#' . $filename;
468
+						$key = $timestamp.'#'.$filename;
469 469
 						$versions[$key]['version'] = $timestamp;
470 470
 						$versions[$key]['humanReadableTimestamp'] = self::getHumanReadableTimestamp($timestamp);
471 471
 						if (empty($userFullPath)) {
@@ -473,9 +473,9 @@  discard block
 block discarded – undo
473 473
 						} else {
474 474
 							$versions[$key]['preview'] = \OC::$server->getURLGenerator('files_version.Preview.getPreview', ['file' => $userFullPath, 'version' => $timestamp]);
475 475
 						}
476
-						$versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'] . '/' . $filename);
476
+						$versions[$key]['path'] = Filesystem::normalizePath($pathinfo['dirname'].'/'.$filename);
477 477
 						$versions[$key]['name'] = $versionedFile;
478
-						$versions[$key]['size'] = $view->filesize($dir . '/' . $entryName);
478
+						$versions[$key]['size'] = $view->filesize($dir.'/'.$entryName);
479 479
 						$versions[$key]['mimetype'] = \OC::$server->getMimeTypeDetector()->detectPath($versionedFile);
480 480
 					}
481 481
 				}
@@ -493,7 +493,7 @@  discard block
 block discarded – undo
493 493
 	 * Expire versions that older than max version retention time
494 494
 	 * @param string $uid
495 495
 	 */
496
-	public static function expireOlderThanMaxForUser($uid){
496
+	public static function expireOlderThanMaxForUser($uid) {
497 497
 		$expiration = self::getExpiration();
498 498
 		$threshold = $expiration->getMaxAgeAsTimestamp();
499 499
 		$versions = self::getAllVersions($uid);
@@ -503,7 +503,7 @@  discard block
 block discarded – undo
503 503
 
504 504
 		$toDelete = [];
505 505
 		foreach (array_reverse($versions['all']) as $key => $version) {
506
-			if (intval($version['version'])<$threshold) {
506
+			if (intval($version['version']) < $threshold) {
507 507
 				$toDelete[$key] = $version;
508 508
 			} else {
509 509
 				//Versions are sorted by time - nothing mo to iterate.
@@ -511,11 +511,11 @@  discard block
 block discarded – undo
511 511
 			}
512 512
 		}
513 513
 
514
-		$view = new View('/' . $uid . '/files_versions');
514
+		$view = new View('/'.$uid.'/files_versions');
515 515
 		if (!empty($toDelete)) {
516 516
 			foreach ($toDelete as $version) {
517 517
 				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
518
-				self::deleteVersion($view, $version['path'] . '.v' . $version['version']);
518
+				self::deleteVersion($view, $version['path'].'.v'.$version['version']);
519 519
 				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_RETENTION_CONSTRAINT));
520 520
 			}
521 521
 		}
@@ -531,19 +531,19 @@  discard block
 block discarded – undo
531 531
 		$diff = time() - $timestamp;
532 532
 
533 533
 		if ($diff < 60) { // first minute
534
-			return  $diff . " seconds ago";
534
+			return  $diff." seconds ago";
535 535
 		} elseif ($diff < 3600) { //first hour
536
-			return round($diff / 60) . " minutes ago";
536
+			return round($diff / 60)." minutes ago";
537 537
 		} elseif ($diff < 86400) { // first day
538
-			return round($diff / 3600) . " hours ago";
538
+			return round($diff / 3600)." hours ago";
539 539
 		} elseif ($diff < 604800) { //first week
540
-			return round($diff / 86400) . " days ago";
540
+			return round($diff / 86400)." days ago";
541 541
 		} elseif ($diff < 2419200) { //first month
542
-			return round($diff / 604800) . " weeks ago";
542
+			return round($diff / 604800)." weeks ago";
543 543
 		} elseif ($diff < 29030400) { // first year
544
-			return round($diff / 2419200) . " months ago";
544
+			return round($diff / 2419200)." months ago";
545 545
 		} else {
546
-			return round($diff / 29030400) . " years ago";
546
+			return round($diff / 29030400)." years ago";
547 547
 		}
548 548
 
549 549
 	}
@@ -554,7 +554,7 @@  discard block
 block discarded – undo
554 554
 	 * @return array with contains two arrays 'all' which contains all versions sorted by age and 'by_file' which contains all versions sorted by filename
555 555
 	 */
556 556
 	private static function getAllVersions($uid) {
557
-		$view = new View('/' . $uid . '/');
557
+		$view = new View('/'.$uid.'/');
558 558
 		$dirs = array(self::VERSIONS_ROOT);
559 559
 		$versions = array();
560 560
 
@@ -564,7 +564,7 @@  discard block
 block discarded – undo
564 564
 
565 565
 			foreach ($files as $file) {
566 566
 				$fileData = $file->getData();
567
-				$filePath = $dir . '/' . $fileData['name'];
567
+				$filePath = $dir.'/'.$fileData['name'];
568 568
 				if ($file['type'] === 'dir') {
569 569
 					$dirs[] = $filePath;
570 570
 				} else {
@@ -572,7 +572,7 @@  discard block
 block discarded – undo
572 572
 					$relPathStart = strlen(self::VERSIONS_ROOT);
573 573
 					$version = substr($filePath, $versionsBegin + 2);
574 574
 					$relpath = substr($filePath, $relPathStart, $versionsBegin - $relPathStart);
575
-					$key = $version . '#' . $relpath;
575
+					$key = $version.'#'.$relpath;
576 576
 					$versions[$key] = array('path' => $relpath, 'timestamp' => $version);
577 577
 				}
578 578
 			}
@@ -613,13 +613,13 @@  discard block
 block discarded – undo
613 613
 			list($toDelete, $size) = self::getAutoExpireList($time, $versions);
614 614
 		} else {
615 615
 			$size = 0;
616
-			$toDelete = [];  // versions we want to delete
616
+			$toDelete = []; // versions we want to delete
617 617
 		}
618 618
 
619 619
 		foreach ($versions as $key => $version) {
620 620
 			if ($expiration->isExpired($version['version'], $quotaExceeded) && !isset($toDelete[$key])) {
621 621
 				$size += $version['size'];
622
-				$toDelete[$key] = $version['path'] . '.v' . $version['version'];
622
+				$toDelete[$key] = $version['path'].'.v'.$version['version'];
623 623
 			}
624 624
 		}
625 625
 
@@ -634,7 +634,7 @@  discard block
 block discarded – undo
634 634
 	 */
635 635
 	protected static function getAutoExpireList($time, $versions) {
636 636
 		$size = 0;
637
-		$toDelete = array();  // versions we want to delete
637
+		$toDelete = array(); // versions we want to delete
638 638
 
639 639
 		$interval = 1;
640 640
 		$step = Storage::$max_versions_per_interval[$interval]['step'];
@@ -656,9 +656,9 @@  discard block
 block discarded – undo
656 656
 				if ($nextInterval === -1 || $prevTimestamp > $nextInterval) {
657 657
 					if ($version['version'] > $nextVersion) {
658 658
 						//distance between two version too small, mark to delete
659
-						$toDelete[$key] = $version['path'] . '.v' . $version['version'];
659
+						$toDelete[$key] = $version['path'].'.v'.$version['version'];
660 660
 						$size += $version['size'];
661
-						\OCP\Util::writeLog('files_versions', 'Mark to expire '. $version['path'] .' next version should be ' . $nextVersion . " or smaller. (prevTimestamp: " . $prevTimestamp . "; step: " . $step, \OCP\Util::INFO);
661
+						\OCP\Util::writeLog('files_versions', 'Mark to expire '.$version['path'].' next version should be '.$nextVersion." or smaller. (prevTimestamp: ".$prevTimestamp."; step: ".$step, \OCP\Util::INFO);
662 662
 					} else {
663 663
 						$nextVersion = $version['version'] - $step;
664 664
 						$prevTimestamp = $version['version'];
@@ -713,8 +713,8 @@  discard block
 block discarded – undo
713 713
 			// get available disk space for user
714 714
 			$user = \OC::$server->getUserManager()->get($uid);
715 715
 			if (is_null($user)) {
716
-				\OCP\Util::writeLog('files_versions', 'Backends provided no user object for ' . $uid, \OCP\Util::ERROR);
717
-				throw new \OC\User\NoUserException('Backends provided no user object for ' . $uid);
716
+				\OCP\Util::writeLog('files_versions', 'Backends provided no user object for '.$uid, \OCP\Util::ERROR);
717
+				throw new \OC\User\NoUserException('Backends provided no user object for '.$uid);
718 718
 			}
719 719
 
720 720
 			\OC_Util::setupFS($uid);
@@ -731,7 +731,7 @@  discard block
 block discarded – undo
731 731
 
732 732
 			$softQuota = true;
733 733
 			$quota = $user->getQuota();
734
-			if ( $quota === null || $quota === 'none' ) {
734
+			if ($quota === null || $quota === 'none') {
735 735
 				$quota = Filesystem::free_space('/');
736 736
 				$softQuota = false;
737 737
 			} else {
@@ -745,7 +745,7 @@  discard block
 block discarded – undo
745 745
 			// subtract size of files and current versions size from quota
746 746
 			if ($quota >= 0) {
747 747
 				if ($softQuota) {
748
-					$files_view = new View('/' . $uid . '/files');
748
+					$files_view = new View('/'.$uid.'/files');
749 749
 					$rootInfo = $files_view->getFileInfo('/', false);
750 750
 					$free = $quota - $rootInfo['size']; // remaining free space for user
751 751
 					if ($free > 0) {
@@ -782,18 +782,18 @@  discard block
 block discarded – undo
782 782
 				$versionsSize = $versionsSize - $sizeOfDeletedVersions;
783 783
 			}
784 784
 
785
-			foreach($toDelete as $key => $path) {
785
+			foreach ($toDelete as $key => $path) {
786 786
 				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
787 787
 				self::deleteVersion($versionsFileview, $path);
788 788
 				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $path, 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
789 789
 				unset($allVersions[$key]); // update array with the versions we keep
790
-				\OCP\Util::writeLog('files_versions', "Expire: " . $path, \OCP\Util::INFO);
790
+				\OCP\Util::writeLog('files_versions', "Expire: ".$path, \OCP\Util::INFO);
791 791
 			}
792 792
 
793 793
 			// Check if enough space is available after versions are rearranged.
794 794
 			// If not we delete the oldest versions until we meet the size limit for versions,
795 795
 			// but always keep the two latest versions
796
-			$numOfVersions = count($allVersions) -2 ;
796
+			$numOfVersions = count($allVersions) - 2;
797 797
 			$i = 0;
798 798
 			// sort oldest first and make sure that we start at the first element
799 799
 			ksort($allVersions);
@@ -801,9 +801,9 @@  discard block
 block discarded – undo
801 801
 			while ($availableSpace < 0 && $i < $numOfVersions) {
802 802
 				$version = current($allVersions);
803 803
 				\OC_Hook::emit('\OCP\Versions', 'preDelete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
804
-				self::deleteVersion($versionsFileview, $version['path'] . '.v' . $version['version']);
804
+				self::deleteVersion($versionsFileview, $version['path'].'.v'.$version['version']);
805 805
 				\OC_Hook::emit('\OCP\Versions', 'delete', array('path' => $version['path'].'.v'.$version['version'], 'trigger' => self::DELETE_TRIGGER_QUOTA_EXCEEDED));
806
-				\OCP\Util::writeLog('files_versions', 'running out of space! Delete oldest version: ' . $version['path'].'.v'.$version['version'] , \OCP\Util::INFO);
806
+				\OCP\Util::writeLog('files_versions', 'running out of space! Delete oldest version: '.$version['path'].'.v'.$version['version'], \OCP\Util::INFO);
807 807
 				$versionsSize -= $version['size'];
808 808
 				$availableSpace += $version['size'];
809 809
 				next($allVersions);
@@ -829,7 +829,7 @@  discard block
 block discarded – undo
829 829
 		$dirParts = explode('/', $dirname);
830 830
 		$dir = "/files_versions";
831 831
 		foreach ($dirParts as $part) {
832
-			$dir = $dir . '/' . $part;
832
+			$dir = $dir.'/'.$part;
833 833
 			if (!$view->file_exists($dir)) {
834 834
 				$view->mkdir($dir);
835 835
 			}
@@ -840,7 +840,7 @@  discard block
 block discarded – undo
840 840
 	 * Static workaround
841 841
 	 * @return Expiration
842 842
 	 */
843
-	protected static function getExpiration(){
843
+	protected static function getExpiration() {
844 844
 		if (is_null(self::$application)) {
845 845
 			self::$application = new Application();
846 846
 		}
Please login to merge, or discard this patch.
apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php 1 patch
Indentation   +128 added lines, -128 removed lines patch added patch discarded remove patch
@@ -33,133 +33,133 @@
 block discarded – undo
33 33
 
34 34
 class BackupCodeStorage {
35 35
 
36
-	private static $CODE_LENGTH = 16;
37
-
38
-	/** @var BackupCodeMapper */
39
-	private $mapper;
40
-
41
-	/** @var IHasher */
42
-	private $hasher;
43
-
44
-	/** @var ISecureRandom */
45
-	private $random;
46
-
47
-	/** @var IManager */
48
-	private $activityManager;
49
-
50
-	/** @var ILogger */
51
-	private $logger;
52
-
53
-	/**
54
-	 * @param BackupCodeMapper $mapper
55
-	 * @param ISecureRandom $random
56
-	 * @param IHasher $hasher
57
-	 * @param IManager $activityManager
58
-	 * @param ILogger $logger
59
-	 */
60
-	public function __construct(BackupCodeMapper $mapper, ISecureRandom $random, IHasher $hasher,
61
-		IManager $activityManager, ILogger $logger) {
62
-		$this->mapper = $mapper;
63
-		$this->hasher = $hasher;
64
-		$this->random = $random;
65
-		$this->activityManager = $activityManager;
66
-		$this->logger = $logger;
67
-	}
68
-
69
-	/**
70
-	 * @param IUser $user
71
-	 * @return string[]
72
-	 */
73
-	public function createCodes(IUser $user, $number = 10) {
74
-		$result = [];
75
-
76
-		// Delete existing ones
77
-		$this->mapper->deleteCodes($user);
78
-
79
-		$uid = $user->getUID();
80
-		foreach (range(1, min([$number, 20])) as $i) {
81
-			$code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
82
-
83
-			$dbCode = new BackupCode();
84
-			$dbCode->setUserId($uid);
85
-			$dbCode->setCode($this->hasher->hash($code));
86
-			$dbCode->setUsed(0);
87
-			$this->mapper->insert($dbCode);
88
-
89
-			$result[] = $code;
90
-		}
91
-
92
-		$this->publishEvent($user, 'codes_generated');
93
-
94
-		return $result;
95
-	}
96
-
97
-	/**
98
-	 * Push an event the user's activity stream
99
-	 *
100
-	 * @param IUser $user
101
-	 * @param string $event
102
-	 */
103
-	private function publishEvent(IUser $user, $event) {
104
-		$activity = $this->activityManager->generateEvent();
105
-		$activity->setApp('twofactor_backupcodes')
106
-			->setType('security')
107
-			->setAuthor($user->getUID())
108
-			->setAffectedUser($user->getUID())
109
-			->setSubject($event);
110
-		try {
111
-			$this->activityManager->publish($activity);
112
-		} catch (BadMethodCallException $e) {
113
-			$this->logger->warning('could not publish backup code creation activity', ['app' => 'twofactor_backupcodes']);
114
-			$this->logger->logException($e, ['app' => 'twofactor_backupcodes']);
115
-		}
116
-	}
117
-
118
-	/**
119
-	 * @param IUser $user
120
-	 * @return bool
121
-	 */
122
-	public function hasBackupCodes(IUser $user) {
123
-		$codes = $this->mapper->getBackupCodes($user);
124
-		return count($codes) > 0;
125
-	}
126
-
127
-	/**
128
-	 * @param IUser $user
129
-	 * @return array
130
-	 */
131
-	public function getBackupCodesState(IUser $user) {
132
-		$codes = $this->mapper->getBackupCodes($user);
133
-		$total = count($codes);
134
-		$used = 0;
135
-		array_walk($codes, function (BackupCode $code) use (&$used) {
136
-			if (1 === (int) $code->getUsed()) {
137
-				$used++;
138
-			}
139
-		});
140
-		return [
141
-			'enabled' => $total > 0,
142
-			'total' => $total,
143
-			'used' => $used,
144
-		];
145
-	}
146
-
147
-	/**
148
-	 * @param IUser $user
149
-	 * @param string $code
150
-	 * @return bool
151
-	 */
152
-	public function validateCode(IUser $user, $code) {
153
-		$dbCodes = $this->mapper->getBackupCodes($user);
154
-
155
-		foreach ($dbCodes as $dbCode) {
156
-			if (0 === (int) $dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) {
157
-				$dbCode->setUsed(1);
158
-				$this->mapper->update($dbCode);
159
-				return true;
160
-			}
161
-		}
162
-		return false;
163
-	}
36
+    private static $CODE_LENGTH = 16;
37
+
38
+    /** @var BackupCodeMapper */
39
+    private $mapper;
40
+
41
+    /** @var IHasher */
42
+    private $hasher;
43
+
44
+    /** @var ISecureRandom */
45
+    private $random;
46
+
47
+    /** @var IManager */
48
+    private $activityManager;
49
+
50
+    /** @var ILogger */
51
+    private $logger;
52
+
53
+    /**
54
+     * @param BackupCodeMapper $mapper
55
+     * @param ISecureRandom $random
56
+     * @param IHasher $hasher
57
+     * @param IManager $activityManager
58
+     * @param ILogger $logger
59
+     */
60
+    public function __construct(BackupCodeMapper $mapper, ISecureRandom $random, IHasher $hasher,
61
+        IManager $activityManager, ILogger $logger) {
62
+        $this->mapper = $mapper;
63
+        $this->hasher = $hasher;
64
+        $this->random = $random;
65
+        $this->activityManager = $activityManager;
66
+        $this->logger = $logger;
67
+    }
68
+
69
+    /**
70
+     * @param IUser $user
71
+     * @return string[]
72
+     */
73
+    public function createCodes(IUser $user, $number = 10) {
74
+        $result = [];
75
+
76
+        // Delete existing ones
77
+        $this->mapper->deleteCodes($user);
78
+
79
+        $uid = $user->getUID();
80
+        foreach (range(1, min([$number, 20])) as $i) {
81
+            $code = $this->random->generate(self::$CODE_LENGTH, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
82
+
83
+            $dbCode = new BackupCode();
84
+            $dbCode->setUserId($uid);
85
+            $dbCode->setCode($this->hasher->hash($code));
86
+            $dbCode->setUsed(0);
87
+            $this->mapper->insert($dbCode);
88
+
89
+            $result[] = $code;
90
+        }
91
+
92
+        $this->publishEvent($user, 'codes_generated');
93
+
94
+        return $result;
95
+    }
96
+
97
+    /**
98
+     * Push an event the user's activity stream
99
+     *
100
+     * @param IUser $user
101
+     * @param string $event
102
+     */
103
+    private function publishEvent(IUser $user, $event) {
104
+        $activity = $this->activityManager->generateEvent();
105
+        $activity->setApp('twofactor_backupcodes')
106
+            ->setType('security')
107
+            ->setAuthor($user->getUID())
108
+            ->setAffectedUser($user->getUID())
109
+            ->setSubject($event);
110
+        try {
111
+            $this->activityManager->publish($activity);
112
+        } catch (BadMethodCallException $e) {
113
+            $this->logger->warning('could not publish backup code creation activity', ['app' => 'twofactor_backupcodes']);
114
+            $this->logger->logException($e, ['app' => 'twofactor_backupcodes']);
115
+        }
116
+    }
117
+
118
+    /**
119
+     * @param IUser $user
120
+     * @return bool
121
+     */
122
+    public function hasBackupCodes(IUser $user) {
123
+        $codes = $this->mapper->getBackupCodes($user);
124
+        return count($codes) > 0;
125
+    }
126
+
127
+    /**
128
+     * @param IUser $user
129
+     * @return array
130
+     */
131
+    public function getBackupCodesState(IUser $user) {
132
+        $codes = $this->mapper->getBackupCodes($user);
133
+        $total = count($codes);
134
+        $used = 0;
135
+        array_walk($codes, function (BackupCode $code) use (&$used) {
136
+            if (1 === (int) $code->getUsed()) {
137
+                $used++;
138
+            }
139
+        });
140
+        return [
141
+            'enabled' => $total > 0,
142
+            'total' => $total,
143
+            'used' => $used,
144
+        ];
145
+    }
146
+
147
+    /**
148
+     * @param IUser $user
149
+     * @param string $code
150
+     * @return bool
151
+     */
152
+    public function validateCode(IUser $user, $code) {
153
+        $dbCodes = $this->mapper->getBackupCodes($user);
154
+
155
+        foreach ($dbCodes as $dbCode) {
156
+            if (0 === (int) $dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) {
157
+                $dbCode->setUsed(1);
158
+                $this->mapper->update($dbCode);
159
+                return true;
160
+            }
161
+        }
162
+        return false;
163
+    }
164 164
 
165 165
 }
Please login to merge, or discard this patch.