Passed
Push — master ( d5dea1...e207b9 )
by Jan-Christoph
13:52 queued 10s
created
lib/public/AppFramework/OCS/OCSNotFoundException.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -32,14 +32,14 @@
 block discarded – undo
32 32
  * @since 9.1.0
33 33
  */
34 34
 class OCSNotFoundException extends OCSException {
35
-	/**
36
-	 * OCSNotFoundException constructor.
37
-	 *
38
-	 * @param string $message
39
-	 * @param Exception|null $previous
40
-	 * @since 9.1.0
41
-	 */
42
-	public function __construct($message = '', Exception $previous = null) {
43
-		parent::__construct($message, Http::STATUS_NOT_FOUND, $previous);
44
-	}
35
+    /**
36
+     * OCSNotFoundException constructor.
37
+     *
38
+     * @param string $message
39
+     * @param Exception|null $previous
40
+     * @since 9.1.0
41
+     */
42
+    public function __construct($message = '', Exception $previous = null) {
43
+        parent::__construct($message, Http::STATUS_NOT_FOUND, $previous);
44
+    }
45 45
 }
Please login to merge, or discard this patch.
lib/public/AppFramework/OCS/OCSForbiddenException.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -32,14 +32,14 @@
 block discarded – undo
32 32
  * @since 9.1.0
33 33
  */
34 34
 class OCSForbiddenException extends OCSException {
35
-	/**
36
-	 * OCSForbiddenException constructor.
37
-	 *
38
-	 * @param string $message
39
-	 * @param Exception|null $previous
40
-	 * @since 9.1.0
41
-	 */
42
-	public function __construct($message = '', Exception $previous = null) {
43
-		parent::__construct($message, Http::STATUS_FORBIDDEN, $previous);
44
-	}
35
+    /**
36
+     * OCSForbiddenException constructor.
37
+     *
38
+     * @param string $message
39
+     * @param Exception|null $previous
40
+     * @since 9.1.0
41
+     */
42
+    public function __construct($message = '', Exception $previous = null) {
43
+        parent::__construct($message, Http::STATUS_FORBIDDEN, $previous);
44
+    }
45 45
 }
Please login to merge, or discard this patch.
lib/public/AppFramework/OCS/OCSBadRequestException.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -32,14 +32,14 @@
 block discarded – undo
32 32
  * @since 9.1.0
33 33
  */
34 34
 class OCSBadRequestException extends OCSException {
35
-	/**
36
-	 * OCSBadRequestException constructor.
37
-	 *
38
-	 * @param string $message
39
-	 * @param Exception|null $previous
40
-	 * @since 9.1.0
41
-	 */
42
-	public function __construct($message = '', Exception $previous = null) {
43
-		parent::__construct($message, Http::STATUS_BAD_REQUEST, $previous);
44
-	}
35
+    /**
36
+     * OCSBadRequestException constructor.
37
+     *
38
+     * @param string $message
39
+     * @param Exception|null $previous
40
+     * @since 9.1.0
41
+     */
42
+    public function __construct($message = '', Exception $previous = null) {
43
+        parent::__construct($message, Http::STATUS_BAD_REQUEST, $previous);
44
+    }
45 45
 }
Please login to merge, or discard this patch.
lib/public/AppFramework/Http/FileDisplayResponse.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -51,7 +51,7 @@  discard block
 block discarded – undo
51 51
 		$this->file = $file;
52 52
 		$this->setStatus($statusCode);
53 53
 		$this->setHeaders(array_merge($this->getHeaders(), $headers));
54
-		$this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
54
+		$this->addHeader('Content-Disposition', 'inline; filename="'.rawurldecode($file->getName()).'"');
55 55
 
56 56
 		$this->setETag($file->getEtag());
57 57
 		$lastModified = new \DateTime();
@@ -65,7 +65,7 @@  discard block
 block discarded – undo
65 65
 	 */
66 66
 	public function callback(IOutput $output) {
67 67
 		if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
68
-			$output->setHeader('Content-Length: ' . $this->file->getSize());
68
+			$output->setHeader('Content-Length: '.$this->file->getSize());
69 69
 			$output->setOutput($this->file->getContent());
70 70
 		}
71 71
 	}
Please login to merge, or discard this patch.
Indentation   +32 added lines, -32 removed lines patch added patch discarded remove patch
@@ -32,40 +32,40 @@
 block discarded – undo
32 32
  */
33 33
 class FileDisplayResponse extends Response implements ICallbackResponse {
34 34
 
35
-	/** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */
36
-	private $file;
35
+    /** @var \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile */
36
+    private $file;
37 37
 
38
-	/**
39
-	 * FileDisplayResponse constructor.
40
-	 *
41
-	 * @param \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile $file
42
-	 * @param int $statusCode
43
-	 * @param array $headers
44
-	 * @since 11.0.0
45
-	 */
46
-	public function __construct($file, $statusCode = Http::STATUS_OK,
47
-								$headers = []) {
48
-		parent::__construct();
38
+    /**
39
+     * FileDisplayResponse constructor.
40
+     *
41
+     * @param \OCP\Files\File|\OCP\Files\SimpleFS\ISimpleFile $file
42
+     * @param int $statusCode
43
+     * @param array $headers
44
+     * @since 11.0.0
45
+     */
46
+    public function __construct($file, $statusCode = Http::STATUS_OK,
47
+                                $headers = []) {
48
+        parent::__construct();
49 49
 
50
-		$this->file = $file;
51
-		$this->setStatus($statusCode);
52
-		$this->setHeaders(array_merge($this->getHeaders(), $headers));
53
-		$this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
50
+        $this->file = $file;
51
+        $this->setStatus($statusCode);
52
+        $this->setHeaders(array_merge($this->getHeaders(), $headers));
53
+        $this->addHeader('Content-Disposition', 'inline; filename="' . rawurldecode($file->getName()) . '"');
54 54
 
55
-		$this->setETag($file->getEtag());
56
-		$lastModified = new \DateTime();
57
-		$lastModified->setTimestamp($file->getMTime());
58
-		$this->setLastModified($lastModified);
59
-	}
55
+        $this->setETag($file->getEtag());
56
+        $lastModified = new \DateTime();
57
+        $lastModified->setTimestamp($file->getMTime());
58
+        $this->setLastModified($lastModified);
59
+    }
60 60
 
61
-	/**
62
-	 * @param IOutput $output
63
-	 * @since 11.0.0
64
-	 */
65
-	public function callback(IOutput $output) {
66
-		if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
67
-			$output->setHeader('Content-Length: ' . $this->file->getSize());
68
-			$output->setOutput($this->file->getContent());
69
-		}
70
-	}
61
+    /**
62
+     * @param IOutput $output
63
+     * @since 11.0.0
64
+     */
65
+    public function callback(IOutput $output) {
66
+        if ($output->getHttpResponseCode() !== Http::STATUS_NOT_MODIFIED) {
67
+            $output->setHeader('Content-Length: ' . $this->file->getSize());
68
+            $output->setOutput($this->file->getContent());
69
+        }
70
+    }
71 71
 }
Please login to merge, or discard this patch.
lib/public/AppFramework/Http/DownloadResponse.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -43,7 +43,7 @@
 block discarded – undo
43 43
 		$this->filename = $filename;
44 44
 		$this->contentType = $contentType;
45 45
 
46
-		$this->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
46
+		$this->addHeader('Content-Disposition', 'attachment; filename="'.$filename.'"');
47 47
 		$this->addHeader('Content-Type', $contentType);
48 48
 	}
49 49
 }
Please login to merge, or discard this patch.
Indentation   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -30,22 +30,22 @@
 block discarded – undo
30 30
  * @since 7.0.0
31 31
  */
32 32
 class DownloadResponse extends Response {
33
-	private $filename;
34
-	private $contentType;
33
+    private $filename;
34
+    private $contentType;
35 35
 
36
-	/**
37
-	 * Creates a response that prompts the user to download the file
38
-	 * @param string $filename the name that the downloaded file should have
39
-	 * @param string $contentType the mimetype that the downloaded file should have
40
-	 * @since 7.0.0
41
-	 */
42
-	public function __construct($filename, $contentType) {
43
-		parent::__construct();
36
+    /**
37
+     * Creates a response that prompts the user to download the file
38
+     * @param string $filename the name that the downloaded file should have
39
+     * @param string $contentType the mimetype that the downloaded file should have
40
+     * @since 7.0.0
41
+     */
42
+    public function __construct($filename, $contentType) {
43
+        parent::__construct();
44 44
 
45
-		$this->filename = $filename;
46
-		$this->contentType = $contentType;
45
+        $this->filename = $filename;
46
+        $this->contentType = $contentType;
47 47
 
48
-		$this->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
49
-		$this->addHeader('Content-Type', $contentType);
50
-	}
48
+        $this->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
49
+        $this->addHeader('Content-Type', $contentType);
50
+    }
51 51
 }
Please login to merge, or discard this patch.
lib/public/AppFramework/Http/JSONResponse.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@
 block discarded – undo
68 68
 	public function render() {
69 69
 		$response = json_encode($this->data, JSON_HEX_TAG);
70 70
 		if ($response === false) {
71
-			throw new \Exception(sprintf('Could not json_encode due to invalid ' .
71
+			throw new \Exception(sprintf('Could not json_encode due to invalid '.
72 72
 				'non UTF-8 characters in the array: %s', var_export($this->data, true)));
73 73
 		}
74 74
 
Please login to merge, or discard this patch.
Indentation   +50 added lines, -50 removed lines patch added patch discarded remove patch
@@ -41,64 +41,64 @@
 block discarded – undo
41 41
  */
42 42
 class JSONResponse extends Response {
43 43
 
44
-	/**
45
-	 * response data
46
-	 * @var array|object
47
-	 */
48
-	protected $data;
44
+    /**
45
+     * response data
46
+     * @var array|object
47
+     */
48
+    protected $data;
49 49
 
50 50
 
51
-	/**
52
-	 * constructor of JSONResponse
53
-	 * @param array|object $data the object or array that should be transformed
54
-	 * @param int $statusCode the Http status code, defaults to 200
55
-	 * @since 6.0.0
56
-	 */
57
-	public function __construct($data = [], $statusCode = Http::STATUS_OK) {
58
-		parent::__construct();
51
+    /**
52
+     * constructor of JSONResponse
53
+     * @param array|object $data the object or array that should be transformed
54
+     * @param int $statusCode the Http status code, defaults to 200
55
+     * @since 6.0.0
56
+     */
57
+    public function __construct($data = [], $statusCode = Http::STATUS_OK) {
58
+        parent::__construct();
59 59
 
60
-		$this->data = $data;
61
-		$this->setStatus($statusCode);
62
-		$this->addHeader('Content-Type', 'application/json; charset=utf-8');
63
-	}
60
+        $this->data = $data;
61
+        $this->setStatus($statusCode);
62
+        $this->addHeader('Content-Type', 'application/json; charset=utf-8');
63
+    }
64 64
 
65 65
 
66
-	/**
67
-	 * Returns the rendered json
68
-	 * @return string the rendered json
69
-	 * @since 6.0.0
70
-	 * @throws \Exception If data could not get encoded
71
-	 */
72
-	public function render() {
73
-		$response = json_encode($this->data, JSON_HEX_TAG);
74
-		if ($response === false) {
75
-			throw new \Exception(sprintf('Could not json_encode due to invalid ' .
76
-				'non UTF-8 characters in the array: %s', var_export($this->data, true)));
77
-		}
66
+    /**
67
+     * Returns the rendered json
68
+     * @return string the rendered json
69
+     * @since 6.0.0
70
+     * @throws \Exception If data could not get encoded
71
+     */
72
+    public function render() {
73
+        $response = json_encode($this->data, JSON_HEX_TAG);
74
+        if ($response === false) {
75
+            throw new \Exception(sprintf('Could not json_encode due to invalid ' .
76
+                'non UTF-8 characters in the array: %s', var_export($this->data, true)));
77
+        }
78 78
 
79
-		return $response;
80
-	}
79
+        return $response;
80
+    }
81 81
 
82
-	/**
83
-	 * Sets values in the data json array
84
-	 * @param array|object $data an array or object which will be transformed
85
-	 *                             to JSON
86
-	 * @return JSONResponse Reference to this object
87
-	 * @since 6.0.0 - return value was added in 7.0.0
88
-	 */
89
-	public function setData($data) {
90
-		$this->data = $data;
82
+    /**
83
+     * Sets values in the data json array
84
+     * @param array|object $data an array or object which will be transformed
85
+     *                             to JSON
86
+     * @return JSONResponse Reference to this object
87
+     * @since 6.0.0 - return value was added in 7.0.0
88
+     */
89
+    public function setData($data) {
90
+        $this->data = $data;
91 91
 
92
-		return $this;
93
-	}
92
+        return $this;
93
+    }
94 94
 
95 95
 
96
-	/**
97
-	 * Used to get the set parameters
98
-	 * @return array the data
99
-	 * @since 6.0.0
100
-	 */
101
-	public function getData() {
102
-		return $this->data;
103
-	}
96
+    /**
97
+     * Used to get the set parameters
98
+     * @return array the data
99
+     * @since 6.0.0
100
+     */
101
+    public function getData() {
102
+        return $this->data;
103
+    }
104 104
 }
Please login to merge, or discard this patch.
lib/private/Archive/ZIP.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 			$result = $this->zip->addFromString($path, $source);
73 73
 		}
74 74
 		if ($result) {
75
-			$this->zip->close();//close and reopen to save the zip
75
+			$this->zip->close(); //close and reopen to save the zip
76 76
 			$this->zip->open($this->path);
77 77
 		}
78 78
 		return $result;
@@ -130,7 +130,7 @@  discard block
 block discarded – undo
130 130
 	public function getFiles() {
131 131
 		$fileCount = $this->zip->numFiles;
132 132
 		$files = [];
133
-		for ($i = 0;$i < $fileCount;$i++) {
133
+		for ($i = 0; $i < $fileCount; $i++) {
134 134
 			$files[] = $this->zip->getNameIndex($i);
135 135
 		}
136 136
 		return $files;
@@ -204,7 +204,7 @@  discard block
 block discarded – undo
204 204
 				$this->extractFile($path, $tmpFile);
205 205
 			}
206 206
 			$handle = fopen($tmpFile, $mode);
207
-			return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
207
+			return CallbackWrapper::wrap($handle, null, null, function() use ($path, $tmpFile) {
208 208
 				$this->writeBack($tmpFile, $path);
209 209
 			});
210 210
 		}
Please login to merge, or discard this patch.
Indentation   +192 added lines, -192 removed lines patch added patch discarded remove patch
@@ -36,199 +36,199 @@
 block discarded – undo
36 36
 use OCP\ILogger;
37 37
 
38 38
 class ZIP extends Archive {
39
-	/**
40
-	 * @var \ZipArchive zip
41
-	 */
42
-	private $zip = null;
43
-	private $path;
39
+    /**
40
+     * @var \ZipArchive zip
41
+     */
42
+    private $zip = null;
43
+    private $path;
44 44
 
45
-	/**
46
-	 * @param string $source
47
-	 */
48
-	public function __construct($source) {
49
-		$this->path = $source;
50
-		$this->zip = new \ZipArchive();
51
-		if ($this->zip->open($source, \ZipArchive::CREATE)) {
52
-		} else {
53
-			\OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN);
54
-		}
55
-	}
56
-	/**
57
-	 * add an empty folder to the archive
58
-	 * @param string $path
59
-	 * @return bool
60
-	 */
61
-	public function addFolder($path) {
62
-		return $this->zip->addEmptyDir($path);
63
-	}
64
-	/**
65
-	 * add a file to the archive
66
-	 * @param string $path
67
-	 * @param string $source either a local file or string data
68
-	 * @return bool
69
-	 */
70
-	public function addFile($path, $source = '') {
71
-		if ($source and $source[0] == '/' and file_exists($source)) {
72
-			$result = $this->zip->addFile($source, $path);
73
-		} else {
74
-			$result = $this->zip->addFromString($path, $source);
75
-		}
76
-		if ($result) {
77
-			$this->zip->close();//close and reopen to save the zip
78
-			$this->zip->open($this->path);
79
-		}
80
-		return $result;
81
-	}
82
-	/**
83
-	 * rename a file or folder in the archive
84
-	 * @param string $source
85
-	 * @param string $dest
86
-	 * @return boolean|null
87
-	 */
88
-	public function rename($source, $dest) {
89
-		$source = $this->stripPath($source);
90
-		$dest = $this->stripPath($dest);
91
-		$this->zip->renameName($source, $dest);
92
-	}
93
-	/**
94
-	 * get the uncompressed size of a file in the archive
95
-	 * @param string $path
96
-	 * @return int
97
-	 */
98
-	public function filesize($path) {
99
-		$stat = $this->zip->statName($path);
100
-		return $stat['size'];
101
-	}
102
-	/**
103
-	 * get the last modified time of a file in the archive
104
-	 * @param string $path
105
-	 * @return int
106
-	 */
107
-	public function mtime($path) {
108
-		return filemtime($this->path);
109
-	}
110
-	/**
111
-	 * get the files in a folder
112
-	 * @param string $path
113
-	 * @return array
114
-	 */
115
-	public function getFolder($path) {
116
-		$files = $this->getFiles();
117
-		$folderContent = [];
118
-		$pathLength = strlen($path);
119
-		foreach ($files as $file) {
120
-			if (substr($file, 0, $pathLength) == $path and $file != $path) {
121
-				if (strrpos(substr($file, 0, -1), '/') <= $pathLength) {
122
-					$folderContent[] = substr($file, $pathLength);
123
-				}
124
-			}
125
-		}
126
-		return $folderContent;
127
-	}
128
-	/**
129
-	 * get all files in the archive
130
-	 * @return array
131
-	 */
132
-	public function getFiles() {
133
-		$fileCount = $this->zip->numFiles;
134
-		$files = [];
135
-		for ($i = 0;$i < $fileCount;$i++) {
136
-			$files[] = $this->zip->getNameIndex($i);
137
-		}
138
-		return $files;
139
-	}
140
-	/**
141
-	 * get the content of a file
142
-	 * @param string $path
143
-	 * @return string
144
-	 */
145
-	public function getFile($path) {
146
-		return $this->zip->getFromName($path);
147
-	}
148
-	/**
149
-	 * extract a single file from the archive
150
-	 * @param string $path
151
-	 * @param string $dest
152
-	 * @return boolean|null
153
-	 */
154
-	public function extractFile($path, $dest) {
155
-		$fp = $this->zip->getStream($path);
156
-		file_put_contents($dest, $fp);
157
-	}
158
-	/**
159
-	 * extract the archive
160
-	 * @param string $dest
161
-	 * @return bool
162
-	 */
163
-	public function extract($dest) {
164
-		return $this->zip->extractTo($dest);
165
-	}
166
-	/**
167
-	 * check if a file or folder exists in the archive
168
-	 * @param string $path
169
-	 * @return bool
170
-	 */
171
-	public function fileExists($path) {
172
-		return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false);
173
-	}
174
-	/**
175
-	 * remove a file or folder from the archive
176
-	 * @param string $path
177
-	 * @return bool
178
-	 */
179
-	public function remove($path) {
180
-		if ($this->fileExists($path.'/')) {
181
-			return $this->zip->deleteName($path.'/');
182
-		} else {
183
-			return $this->zip->deleteName($path);
184
-		}
185
-	}
186
-	/**
187
-	 * get a file handler
188
-	 * @param string $path
189
-	 * @param string $mode
190
-	 * @return bool|resource
191
-	 */
192
-	public function getStream($path, $mode) {
193
-		if ($mode == 'r' or $mode == 'rb') {
194
-			return $this->zip->getStream($path);
195
-		} else {
196
-			//since we can't directly get a writable stream,
197
-			//make a temp copy of the file and put it back
198
-			//in the archive when the stream is closed
199
-			if (strrpos($path, '.') !== false) {
200
-				$ext = substr($path, strrpos($path, '.'));
201
-			} else {
202
-				$ext = '';
203
-			}
204
-			$tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
205
-			if ($this->fileExists($path)) {
206
-				$this->extractFile($path, $tmpFile);
207
-			}
208
-			$handle = fopen($tmpFile, $mode);
209
-			return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
210
-				$this->writeBack($tmpFile, $path);
211
-			});
212
-		}
213
-	}
45
+    /**
46
+     * @param string $source
47
+     */
48
+    public function __construct($source) {
49
+        $this->path = $source;
50
+        $this->zip = new \ZipArchive();
51
+        if ($this->zip->open($source, \ZipArchive::CREATE)) {
52
+        } else {
53
+            \OCP\Util::writeLog('files_archive', 'Error while opening archive '.$source, ILogger::WARN);
54
+        }
55
+    }
56
+    /**
57
+     * add an empty folder to the archive
58
+     * @param string $path
59
+     * @return bool
60
+     */
61
+    public function addFolder($path) {
62
+        return $this->zip->addEmptyDir($path);
63
+    }
64
+    /**
65
+     * add a file to the archive
66
+     * @param string $path
67
+     * @param string $source either a local file or string data
68
+     * @return bool
69
+     */
70
+    public function addFile($path, $source = '') {
71
+        if ($source and $source[0] == '/' and file_exists($source)) {
72
+            $result = $this->zip->addFile($source, $path);
73
+        } else {
74
+            $result = $this->zip->addFromString($path, $source);
75
+        }
76
+        if ($result) {
77
+            $this->zip->close();//close and reopen to save the zip
78
+            $this->zip->open($this->path);
79
+        }
80
+        return $result;
81
+    }
82
+    /**
83
+     * rename a file or folder in the archive
84
+     * @param string $source
85
+     * @param string $dest
86
+     * @return boolean|null
87
+     */
88
+    public function rename($source, $dest) {
89
+        $source = $this->stripPath($source);
90
+        $dest = $this->stripPath($dest);
91
+        $this->zip->renameName($source, $dest);
92
+    }
93
+    /**
94
+     * get the uncompressed size of a file in the archive
95
+     * @param string $path
96
+     * @return int
97
+     */
98
+    public function filesize($path) {
99
+        $stat = $this->zip->statName($path);
100
+        return $stat['size'];
101
+    }
102
+    /**
103
+     * get the last modified time of a file in the archive
104
+     * @param string $path
105
+     * @return int
106
+     */
107
+    public function mtime($path) {
108
+        return filemtime($this->path);
109
+    }
110
+    /**
111
+     * get the files in a folder
112
+     * @param string $path
113
+     * @return array
114
+     */
115
+    public function getFolder($path) {
116
+        $files = $this->getFiles();
117
+        $folderContent = [];
118
+        $pathLength = strlen($path);
119
+        foreach ($files as $file) {
120
+            if (substr($file, 0, $pathLength) == $path and $file != $path) {
121
+                if (strrpos(substr($file, 0, -1), '/') <= $pathLength) {
122
+                    $folderContent[] = substr($file, $pathLength);
123
+                }
124
+            }
125
+        }
126
+        return $folderContent;
127
+    }
128
+    /**
129
+     * get all files in the archive
130
+     * @return array
131
+     */
132
+    public function getFiles() {
133
+        $fileCount = $this->zip->numFiles;
134
+        $files = [];
135
+        for ($i = 0;$i < $fileCount;$i++) {
136
+            $files[] = $this->zip->getNameIndex($i);
137
+        }
138
+        return $files;
139
+    }
140
+    /**
141
+     * get the content of a file
142
+     * @param string $path
143
+     * @return string
144
+     */
145
+    public function getFile($path) {
146
+        return $this->zip->getFromName($path);
147
+    }
148
+    /**
149
+     * extract a single file from the archive
150
+     * @param string $path
151
+     * @param string $dest
152
+     * @return boolean|null
153
+     */
154
+    public function extractFile($path, $dest) {
155
+        $fp = $this->zip->getStream($path);
156
+        file_put_contents($dest, $fp);
157
+    }
158
+    /**
159
+     * extract the archive
160
+     * @param string $dest
161
+     * @return bool
162
+     */
163
+    public function extract($dest) {
164
+        return $this->zip->extractTo($dest);
165
+    }
166
+    /**
167
+     * check if a file or folder exists in the archive
168
+     * @param string $path
169
+     * @return bool
170
+     */
171
+    public function fileExists($path) {
172
+        return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false);
173
+    }
174
+    /**
175
+     * remove a file or folder from the archive
176
+     * @param string $path
177
+     * @return bool
178
+     */
179
+    public function remove($path) {
180
+        if ($this->fileExists($path.'/')) {
181
+            return $this->zip->deleteName($path.'/');
182
+        } else {
183
+            return $this->zip->deleteName($path);
184
+        }
185
+    }
186
+    /**
187
+     * get a file handler
188
+     * @param string $path
189
+     * @param string $mode
190
+     * @return bool|resource
191
+     */
192
+    public function getStream($path, $mode) {
193
+        if ($mode == 'r' or $mode == 'rb') {
194
+            return $this->zip->getStream($path);
195
+        } else {
196
+            //since we can't directly get a writable stream,
197
+            //make a temp copy of the file and put it back
198
+            //in the archive when the stream is closed
199
+            if (strrpos($path, '.') !== false) {
200
+                $ext = substr($path, strrpos($path, '.'));
201
+            } else {
202
+                $ext = '';
203
+            }
204
+            $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
205
+            if ($this->fileExists($path)) {
206
+                $this->extractFile($path, $tmpFile);
207
+            }
208
+            $handle = fopen($tmpFile, $mode);
209
+            return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
210
+                $this->writeBack($tmpFile, $path);
211
+            });
212
+        }
213
+    }
214 214
 
215
-	/**
216
-	 * write back temporary files
217
-	 */
218
-	public function writeBack($tmpFile, $path) {
219
-		$this->addFile($path, $tmpFile);
220
-		unlink($tmpFile);
221
-	}
215
+    /**
216
+     * write back temporary files
217
+     */
218
+    public function writeBack($tmpFile, $path) {
219
+        $this->addFile($path, $tmpFile);
220
+        unlink($tmpFile);
221
+    }
222 222
 
223
-	/**
224
-	 * @param string $path
225
-	 * @return string
226
-	 */
227
-	private function stripPath($path) {
228
-		if (!$path || $path[0] == '/') {
229
-			return substr($path, 1);
230
-		} else {
231
-			return $path;
232
-		}
233
-	}
223
+    /**
224
+     * @param string $path
225
+     * @return string
226
+     */
227
+    private function stripPath($path) {
228
+        if (!$path || $path[0] == '/') {
229
+            return substr($path, 1);
230
+        } else {
231
+            return $path;
232
+        }
233
+    }
234 234
 }
Please login to merge, or discard this patch.
lib/private/IntegrityCheck/Iterator/ExcludeFoldersByPathFilterIterator.php 2 patches
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -35,17 +35,17 @@
 block discarded – undo
35 35
 		}
36 36
 
37 37
 		$excludedFolders = [
38
-			rtrim($root . '/data', '/'),
39
-			rtrim($root . '/themes', '/'),
40
-			rtrim($root . '/config', '/'),
41
-			rtrim($root . '/apps', '/'),
42
-			rtrim($root . '/assets', '/'),
43
-			rtrim($root . '/lost+found', '/'),
38
+			rtrim($root.'/data', '/'),
39
+			rtrim($root.'/themes', '/'),
40
+			rtrim($root.'/config', '/'),
41
+			rtrim($root.'/apps', '/'),
42
+			rtrim($root.'/assets', '/'),
43
+			rtrim($root.'/lost+found', '/'),
44 44
 			// Ignore folders generated by updater since the updater is replaced
45 45
 			// after the integrity check is run.
46 46
 			// See https://github.com/owncloud/updater/issues/318#issuecomment-212497846
47
-			rtrim($root . '/updater', '/'),
48
-			rtrim($root . '/_oc_upgrade', '/'),
47
+			rtrim($root.'/updater', '/'),
48
+			rtrim($root.'/_oc_upgrade', '/'),
49 49
 		];
50 50
 		$customDataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', '');
51 51
 		if ($customDataDir !== '') {
Please login to merge, or discard this patch.
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -28,45 +28,45 @@
 block discarded – undo
28 28
 namespace OC\IntegrityCheck\Iterator;
29 29
 
30 30
 class ExcludeFoldersByPathFilterIterator extends \RecursiveFilterIterator {
31
-	private $excludedFolders;
31
+    private $excludedFolders;
32 32
 
33
-	public function __construct(\RecursiveIterator $iterator, $root = '') {
34
-		parent::__construct($iterator);
33
+    public function __construct(\RecursiveIterator $iterator, $root = '') {
34
+        parent::__construct($iterator);
35 35
 
36
-		$appFolders = \OC::$APPSROOTS;
37
-		foreach ($appFolders as $key => $appFolder) {
38
-			$appFolders[$key] = rtrim($appFolder['path'], '/');
39
-		}
36
+        $appFolders = \OC::$APPSROOTS;
37
+        foreach ($appFolders as $key => $appFolder) {
38
+            $appFolders[$key] = rtrim($appFolder['path'], '/');
39
+        }
40 40
 
41
-		$excludedFolders = [
42
-			rtrim($root . '/data', '/'),
43
-			rtrim($root . '/themes', '/'),
44
-			rtrim($root . '/config', '/'),
45
-			rtrim($root . '/apps', '/'),
46
-			rtrim($root . '/assets', '/'),
47
-			rtrim($root . '/lost+found', '/'),
48
-			// Ignore folders generated by updater since the updater is replaced
49
-			// after the integrity check is run.
50
-			// See https://github.com/owncloud/updater/issues/318#issuecomment-212497846
51
-			rtrim($root . '/updater', '/'),
52
-			rtrim($root . '/_oc_upgrade', '/'),
53
-		];
54
-		$customDataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', '');
55
-		if ($customDataDir !== '') {
56
-			$excludedFolders[] = rtrim($customDataDir, '/');
57
-		}
41
+        $excludedFolders = [
42
+            rtrim($root . '/data', '/'),
43
+            rtrim($root . '/themes', '/'),
44
+            rtrim($root . '/config', '/'),
45
+            rtrim($root . '/apps', '/'),
46
+            rtrim($root . '/assets', '/'),
47
+            rtrim($root . '/lost+found', '/'),
48
+            // Ignore folders generated by updater since the updater is replaced
49
+            // after the integrity check is run.
50
+            // See https://github.com/owncloud/updater/issues/318#issuecomment-212497846
51
+            rtrim($root . '/updater', '/'),
52
+            rtrim($root . '/_oc_upgrade', '/'),
53
+        ];
54
+        $customDataDir = \OC::$server->getConfig()->getSystemValue('datadirectory', '');
55
+        if ($customDataDir !== '') {
56
+            $excludedFolders[] = rtrim($customDataDir, '/');
57
+        }
58 58
 
59
-		$this->excludedFolders = array_merge($excludedFolders, $appFolders);
60
-	}
59
+        $this->excludedFolders = array_merge($excludedFolders, $appFolders);
60
+    }
61 61
 
62
-	/**
63
-	 * @return bool
64
-	 */
65
-	public function accept() {
66
-		return !\in_array(
67
-			$this->current()->getPathName(),
68
-			$this->excludedFolders,
69
-			true
70
-		);
71
-	}
62
+    /**
63
+     * @return bool
64
+     */
65
+    public function accept() {
66
+        return !\in_array(
67
+            $this->current()->getPathName(),
68
+            $this->excludedFolders,
69
+            true
70
+        );
71
+    }
72 72
 }
Please login to merge, or discard this patch.
lib/private/App/CodeChecker/NodeVisitor.php 2 patches
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -214,28 +214,28 @@  discard block
 block discarded – undo
214 214
 		$alias = strtolower($alias);
215 215
 
216 216
 		foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) {
217
-			if (strpos($blackListedClassName, $name . '\\') === 0) {
217
+			if (strpos($blackListedClassName, $name.'\\') === 0) {
218 218
 				$aliasedClassName = str_replace($name, $alias, $blackListedClassName);
219 219
 				$this->blackListedClassNames[$aliasedClassName] = $blackListedClassName;
220 220
 			}
221 221
 		}
222 222
 
223 223
 		foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) {
224
-			if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) {
224
+			if (strpos($blackListedConstant, $name.'\\') === 0 || strpos($blackListedConstant, $name.'::') === 0) {
225 225
 				$aliasedConstantName = str_replace($name, $alias, $blackListedConstant);
226 226
 				$this->blackListedConstants[$aliasedConstantName] = $blackListedConstant;
227 227
 			}
228 228
 		}
229 229
 
230 230
 		foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) {
231
-			if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) {
231
+			if (strpos($blackListedFunction, $name.'\\') === 0 || strpos($blackListedFunction, $name.'::') === 0) {
232 232
 				$aliasedFunctionName = str_replace($name, $alias, $blackListedFunction);
233 233
 				$this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction;
234 234
 			}
235 235
 		}
236 236
 
237 237
 		foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) {
238
-			if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) {
238
+			if (strpos($blackListedMethod, $name.'\\') === 0 || strpos($blackListedMethod, $name.'::') === 0) {
239 239
 				$aliasedMethodName = str_replace($name, $alias, $blackListedMethod);
240 240
 				$this->blackListedMethods[$aliasedMethodName] = $blackListedMethod;
241 241
 			}
@@ -256,7 +256,7 @@  discard block
 block discarded – undo
256 256
 	}
257 257
 
258 258
 	private function checkBlackListConstant($class, $constantName, Node $node) {
259
-		$name = $class . '::' . $constantName;
259
+		$name = $class.'::'.$constantName;
260 260
 		$lowerName = strtolower($name);
261 261
 
262 262
 		if (isset($this->blackListedConstants[$lowerName])) {
@@ -270,7 +270,7 @@  discard block
 block discarded – undo
270 270
 	}
271 271
 
272 272
 	private function checkBlackListFunction($class, $functionName, Node $node) {
273
-		$name = $class . '::' . $functionName;
273
+		$name = $class.'::'.$functionName;
274 274
 		$lowerName = strtolower($name);
275 275
 
276 276
 		if (isset($this->blackListedFunctions[$lowerName])) {
@@ -284,7 +284,7 @@  discard block
 block discarded – undo
284 284
 	}
285 285
 
286 286
 	private function checkBlackListMethod($class, $functionName, Node $node) {
287
-		$name = $class . '::' . $functionName;
287
+		$name = $class.'::'.$functionName;
288 288
 		$lowerName = strtolower($name);
289 289
 
290 290
 		if (isset($this->blackListedMethods[$lowerName])) {
Please login to merge, or discard this patch.
Indentation   +276 added lines, -276 removed lines patch added patch discarded remove patch
@@ -30,280 +30,280 @@
 block discarded – undo
30 30
 use PhpParser\NodeVisitorAbstract;
31 31
 
32 32
 class NodeVisitor extends NodeVisitorAbstract {
33
-	/** @var ICheck */
34
-	protected $list;
35
-
36
-	/** @var string */
37
-	protected $blackListDescription;
38
-	/** @var string[] */
39
-	protected $blackListedClassNames;
40
-	/** @var string[] */
41
-	protected $blackListedConstants;
42
-	/** @var string[] */
43
-	protected $blackListedFunctions;
44
-	/** @var string[] */
45
-	protected $blackListedMethods;
46
-	/** @var bool */
47
-	protected $checkEqualOperatorUsage;
48
-	/** @var string[] */
49
-	protected $errorMessages;
50
-
51
-	/**
52
-	 * @param ICheck $list
53
-	 */
54
-	public function __construct(ICheck $list) {
55
-		$this->list = $list;
56
-
57
-		$this->blackListedClassNames = [];
58
-		foreach ($list->getClasses() as $class => $blackListInfo) {
59
-			if (is_numeric($class) && is_string($blackListInfo)) {
60
-				$class = $blackListInfo;
61
-				$blackListInfo = null;
62
-			}
63
-
64
-			$class = strtolower($class);
65
-			$this->blackListedClassNames[$class] = $class;
66
-		}
67
-
68
-		$this->blackListedConstants = [];
69
-		foreach ($list->getConstants() as $constantName => $blackListInfo) {
70
-			$constantName = strtolower($constantName);
71
-			$this->blackListedConstants[$constantName] = $constantName;
72
-		}
73
-
74
-		$this->blackListedFunctions = [];
75
-		foreach ($list->getFunctions() as $functionName => $blackListInfo) {
76
-			$functionName = strtolower($functionName);
77
-			$this->blackListedFunctions[$functionName] = $functionName;
78
-		}
79
-
80
-		$this->blackListedMethods = [];
81
-		foreach ($list->getMethods() as $functionName => $blackListInfo) {
82
-			$functionName = strtolower($functionName);
83
-			$this->blackListedMethods[$functionName] = $functionName;
84
-		}
85
-
86
-		$this->checkEqualOperatorUsage = $list->checkStrongComparisons();
87
-
88
-		$this->errorMessages = [
89
-			CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "%s class must not be extended",
90
-			CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "%s interface must not be implemented",
91
-			CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of %s class must not be called",
92
-			CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of %s class must not not be fetched",
93
-			CodeChecker::CLASS_NEW_NOT_ALLOWED => "%s class must not be instantiated",
94
-			CodeChecker::CLASS_USE_NOT_ALLOWED => "%s class must not be imported with a use statement",
95
-			CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of %s class must not be called",
96
-
97
-			CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged",
98
-		];
99
-	}
100
-
101
-	/** @var array */
102
-	public $errors = [];
103
-
104
-	public function enterNode(Node $node) {
105
-		if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\Equal) {
106
-			$this->errors[] = [
107
-				'disallowedToken' => '==',
108
-				'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
109
-				'line' => $node->getLine(),
110
-				'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
111
-			];
112
-		}
113
-		if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\NotEqual) {
114
-			$this->errors[] = [
115
-				'disallowedToken' => '!=',
116
-				'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
117
-				'line' => $node->getLine(),
118
-				'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
119
-			];
120
-		}
121
-		if ($node instanceof Node\Stmt\Class_) {
122
-			if (!is_null($node->extends)) {
123
-				$this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node);
124
-			}
125
-			foreach ($node->implements as $implements) {
126
-				$this->checkBlackList($implements->toString(), CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED, $node);
127
-			}
128
-		}
129
-		if ($node instanceof Node\Expr\StaticCall) {
130
-			if (!is_null($node->class)) {
131
-				if ($node->class instanceof Name) {
132
-					$this->checkBlackList($node->class->toString(), CodeChecker::STATIC_CALL_NOT_ALLOWED, $node);
133
-
134
-					$this->checkBlackListFunction($node->class->toString(), $node->name, $node);
135
-					$this->checkBlackListMethod($node->class->toString(), $node->name, $node);
136
-				}
137
-
138
-				if ($node->class instanceof Node\Expr\Variable) {
139
-					/**
140
-					 * TODO: find a way to detect something like this:
141
-					 *       $c = "OC_API";
142
-					 *       $n = $c::call();
143
-					 */
144
-					// $this->checkBlackListMethod($node->class->..., $node->name, $node);
145
-				}
146
-			}
147
-		}
148
-		if ($node instanceof Node\Expr\MethodCall) {
149
-			if (!is_null($node->var)) {
150
-				if ($node->var instanceof Node\Expr\Variable) {
151
-					/**
152
-					 * TODO: find a way to detect something like this:
153
-					 *       $c = new OC_API();
154
-					 *       $n = $c::call();
155
-					 *       $n = $c->call();
156
-					 */
157
-					// $this->checkBlackListMethod($node->var->..., $node->name, $node);
158
-				}
159
-			}
160
-		}
161
-		if ($node instanceof Node\Expr\ClassConstFetch) {
162
-			if (!is_null($node->class)) {
163
-				if ($node->class instanceof Name) {
164
-					$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, $node);
165
-				}
166
-				if ($node->class instanceof Node\Expr\Variable || $node->class instanceof Node\Expr\PropertyFetch) {
167
-					/**
168
-					 * TODO: find a way to detect something like this:
169
-					 *       $c = "OC_API";
170
-					 *       $n = $i::ADMIN_AUTH;
171
-					 */
172
-				} else {
173
-					$this->checkBlackListConstant($node->class->toString(), $node->name, $node);
174
-				}
175
-			}
176
-		}
177
-		if ($node instanceof Node\Expr\New_) {
178
-			if (!is_null($node->class)) {
179
-				if ($node->class instanceof Name) {
180
-					$this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_NOT_ALLOWED, $node);
181
-				}
182
-				if ($node->class instanceof Node\Expr\Variable) {
183
-					/**
184
-					 * TODO: find a way to detect something like this:
185
-					 *       $c = "OC_API";
186
-					 *       $n = new $i;
187
-					 */
188
-				}
189
-			}
190
-		}
191
-		if ($node instanceof Node\Stmt\UseUse) {
192
-			$this->checkBlackList($node->name->toString(), CodeChecker::CLASS_USE_NOT_ALLOWED, $node);
193
-			if ($node->alias) {
194
-				$this->addUseNameToBlackList($node->name->toString(), $node->alias);
195
-			} else {
196
-				$this->addUseNameToBlackList($node->name->toString(), $node->name->getLast());
197
-			}
198
-		}
199
-	}
200
-
201
-	/**
202
-	 * Check whether an alias was introduced for a namespace of a blacklisted class
203
-	 *
204
-	 * Example:
205
-	 * - Blacklist entry:      OCP\AppFramework\IApi
206
-	 * - Name:                 OCP\AppFramework
207
-	 * - Alias:                OAF
208
-	 * =>  new blacklist entry:  OAF\IApi
209
-	 *
210
-	 * @param string $name
211
-	 * @param string $alias
212
-	 */
213
-	private function addUseNameToBlackList($name, $alias) {
214
-		$name = strtolower($name);
215
-		$alias = strtolower($alias);
216
-
217
-		foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) {
218
-			if (strpos($blackListedClassName, $name . '\\') === 0) {
219
-				$aliasedClassName = str_replace($name, $alias, $blackListedClassName);
220
-				$this->blackListedClassNames[$aliasedClassName] = $blackListedClassName;
221
-			}
222
-		}
223
-
224
-		foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) {
225
-			if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) {
226
-				$aliasedConstantName = str_replace($name, $alias, $blackListedConstant);
227
-				$this->blackListedConstants[$aliasedConstantName] = $blackListedConstant;
228
-			}
229
-		}
230
-
231
-		foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) {
232
-			if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) {
233
-				$aliasedFunctionName = str_replace($name, $alias, $blackListedFunction);
234
-				$this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction;
235
-			}
236
-		}
237
-
238
-		foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) {
239
-			if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) {
240
-				$aliasedMethodName = str_replace($name, $alias, $blackListedMethod);
241
-				$this->blackListedMethods[$aliasedMethodName] = $blackListedMethod;
242
-			}
243
-		}
244
-	}
245
-
246
-	private function checkBlackList($name, $errorCode, Node $node) {
247
-		$lowerName = strtolower($name);
248
-
249
-		if (isset($this->blackListedClassNames[$lowerName])) {
250
-			$this->errors[] = [
251
-				'disallowedToken' => $name,
252
-				'errorCode' => $errorCode,
253
-				'line' => $node->getLine(),
254
-				'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode)
255
-			];
256
-		}
257
-	}
258
-
259
-	private function checkBlackListConstant($class, $constantName, Node $node) {
260
-		$name = $class . '::' . $constantName;
261
-		$lowerName = strtolower($name);
262
-
263
-		if (isset($this->blackListedConstants[$lowerName])) {
264
-			$this->errors[] = [
265
-				'disallowedToken' => $name,
266
-				'errorCode' => CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED,
267
-				'line' => $node->getLine(),
268
-				'reason' => $this->buildReason($this->blackListedConstants[$lowerName], CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED)
269
-			];
270
-		}
271
-	}
272
-
273
-	private function checkBlackListFunction($class, $functionName, Node $node) {
274
-		$name = $class . '::' . $functionName;
275
-		$lowerName = strtolower($name);
276
-
277
-		if (isset($this->blackListedFunctions[$lowerName])) {
278
-			$this->errors[] = [
279
-				'disallowedToken' => $name,
280
-				'errorCode' => CodeChecker::STATIC_CALL_NOT_ALLOWED,
281
-				'line' => $node->getLine(),
282
-				'reason' => $this->buildReason($this->blackListedFunctions[$lowerName], CodeChecker::STATIC_CALL_NOT_ALLOWED)
283
-			];
284
-		}
285
-	}
286
-
287
-	private function checkBlackListMethod($class, $functionName, Node $node) {
288
-		$name = $class . '::' . $functionName;
289
-		$lowerName = strtolower($name);
290
-
291
-		if (isset($this->blackListedMethods[$lowerName])) {
292
-			$this->errors[] = [
293
-				'disallowedToken' => $name,
294
-				'errorCode' => CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED,
295
-				'line' => $node->getLine(),
296
-				'reason' => $this->buildReason($this->blackListedMethods[$lowerName], CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED)
297
-			];
298
-		}
299
-	}
300
-
301
-	private function buildReason($name, $errorCode) {
302
-		if (isset($this->errorMessages[$errorCode])) {
303
-			$desc = $this->list->getDescription($errorCode, $name);
304
-			return sprintf($this->errorMessages[$errorCode], $desc);
305
-		}
306
-
307
-		return "$name usage not allowed - error: $errorCode";
308
-	}
33
+    /** @var ICheck */
34
+    protected $list;
35
+
36
+    /** @var string */
37
+    protected $blackListDescription;
38
+    /** @var string[] */
39
+    protected $blackListedClassNames;
40
+    /** @var string[] */
41
+    protected $blackListedConstants;
42
+    /** @var string[] */
43
+    protected $blackListedFunctions;
44
+    /** @var string[] */
45
+    protected $blackListedMethods;
46
+    /** @var bool */
47
+    protected $checkEqualOperatorUsage;
48
+    /** @var string[] */
49
+    protected $errorMessages;
50
+
51
+    /**
52
+     * @param ICheck $list
53
+     */
54
+    public function __construct(ICheck $list) {
55
+        $this->list = $list;
56
+
57
+        $this->blackListedClassNames = [];
58
+        foreach ($list->getClasses() as $class => $blackListInfo) {
59
+            if (is_numeric($class) && is_string($blackListInfo)) {
60
+                $class = $blackListInfo;
61
+                $blackListInfo = null;
62
+            }
63
+
64
+            $class = strtolower($class);
65
+            $this->blackListedClassNames[$class] = $class;
66
+        }
67
+
68
+        $this->blackListedConstants = [];
69
+        foreach ($list->getConstants() as $constantName => $blackListInfo) {
70
+            $constantName = strtolower($constantName);
71
+            $this->blackListedConstants[$constantName] = $constantName;
72
+        }
73
+
74
+        $this->blackListedFunctions = [];
75
+        foreach ($list->getFunctions() as $functionName => $blackListInfo) {
76
+            $functionName = strtolower($functionName);
77
+            $this->blackListedFunctions[$functionName] = $functionName;
78
+        }
79
+
80
+        $this->blackListedMethods = [];
81
+        foreach ($list->getMethods() as $functionName => $blackListInfo) {
82
+            $functionName = strtolower($functionName);
83
+            $this->blackListedMethods[$functionName] = $functionName;
84
+        }
85
+
86
+        $this->checkEqualOperatorUsage = $list->checkStrongComparisons();
87
+
88
+        $this->errorMessages = [
89
+            CodeChecker::CLASS_EXTENDS_NOT_ALLOWED => "%s class must not be extended",
90
+            CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED => "%s interface must not be implemented",
91
+            CodeChecker::STATIC_CALL_NOT_ALLOWED => "Static method of %s class must not be called",
92
+            CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED => "Constant of %s class must not not be fetched",
93
+            CodeChecker::CLASS_NEW_NOT_ALLOWED => "%s class must not be instantiated",
94
+            CodeChecker::CLASS_USE_NOT_ALLOWED => "%s class must not be imported with a use statement",
95
+            CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED => "Method of %s class must not be called",
96
+
97
+            CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED => "is discouraged",
98
+        ];
99
+    }
100
+
101
+    /** @var array */
102
+    public $errors = [];
103
+
104
+    public function enterNode(Node $node) {
105
+        if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\Equal) {
106
+            $this->errors[] = [
107
+                'disallowedToken' => '==',
108
+                'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
109
+                'line' => $node->getLine(),
110
+                'reason' => $this->buildReason('==', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
111
+            ];
112
+        }
113
+        if ($this->checkEqualOperatorUsage && $node instanceof Node\Expr\BinaryOp\NotEqual) {
114
+            $this->errors[] = [
115
+                'disallowedToken' => '!=',
116
+                'errorCode' => CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED,
117
+                'line' => $node->getLine(),
118
+                'reason' => $this->buildReason('!=', CodeChecker::OP_OPERATOR_USAGE_DISCOURAGED)
119
+            ];
120
+        }
121
+        if ($node instanceof Node\Stmt\Class_) {
122
+            if (!is_null($node->extends)) {
123
+                $this->checkBlackList($node->extends->toString(), CodeChecker::CLASS_EXTENDS_NOT_ALLOWED, $node);
124
+            }
125
+            foreach ($node->implements as $implements) {
126
+                $this->checkBlackList($implements->toString(), CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED, $node);
127
+            }
128
+        }
129
+        if ($node instanceof Node\Expr\StaticCall) {
130
+            if (!is_null($node->class)) {
131
+                if ($node->class instanceof Name) {
132
+                    $this->checkBlackList($node->class->toString(), CodeChecker::STATIC_CALL_NOT_ALLOWED, $node);
133
+
134
+                    $this->checkBlackListFunction($node->class->toString(), $node->name, $node);
135
+                    $this->checkBlackListMethod($node->class->toString(), $node->name, $node);
136
+                }
137
+
138
+                if ($node->class instanceof Node\Expr\Variable) {
139
+                    /**
140
+                     * TODO: find a way to detect something like this:
141
+                     *       $c = "OC_API";
142
+                     *       $n = $c::call();
143
+                     */
144
+                    // $this->checkBlackListMethod($node->class->..., $node->name, $node);
145
+                }
146
+            }
147
+        }
148
+        if ($node instanceof Node\Expr\MethodCall) {
149
+            if (!is_null($node->var)) {
150
+                if ($node->var instanceof Node\Expr\Variable) {
151
+                    /**
152
+                     * TODO: find a way to detect something like this:
153
+                     *       $c = new OC_API();
154
+                     *       $n = $c::call();
155
+                     *       $n = $c->call();
156
+                     */
157
+                    // $this->checkBlackListMethod($node->var->..., $node->name, $node);
158
+                }
159
+            }
160
+        }
161
+        if ($node instanceof Node\Expr\ClassConstFetch) {
162
+            if (!is_null($node->class)) {
163
+                if ($node->class instanceof Name) {
164
+                    $this->checkBlackList($node->class->toString(), CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED, $node);
165
+                }
166
+                if ($node->class instanceof Node\Expr\Variable || $node->class instanceof Node\Expr\PropertyFetch) {
167
+                    /**
168
+                     * TODO: find a way to detect something like this:
169
+                     *       $c = "OC_API";
170
+                     *       $n = $i::ADMIN_AUTH;
171
+                     */
172
+                } else {
173
+                    $this->checkBlackListConstant($node->class->toString(), $node->name, $node);
174
+                }
175
+            }
176
+        }
177
+        if ($node instanceof Node\Expr\New_) {
178
+            if (!is_null($node->class)) {
179
+                if ($node->class instanceof Name) {
180
+                    $this->checkBlackList($node->class->toString(), CodeChecker::CLASS_NEW_NOT_ALLOWED, $node);
181
+                }
182
+                if ($node->class instanceof Node\Expr\Variable) {
183
+                    /**
184
+                     * TODO: find a way to detect something like this:
185
+                     *       $c = "OC_API";
186
+                     *       $n = new $i;
187
+                     */
188
+                }
189
+            }
190
+        }
191
+        if ($node instanceof Node\Stmt\UseUse) {
192
+            $this->checkBlackList($node->name->toString(), CodeChecker::CLASS_USE_NOT_ALLOWED, $node);
193
+            if ($node->alias) {
194
+                $this->addUseNameToBlackList($node->name->toString(), $node->alias);
195
+            } else {
196
+                $this->addUseNameToBlackList($node->name->toString(), $node->name->getLast());
197
+            }
198
+        }
199
+    }
200
+
201
+    /**
202
+     * Check whether an alias was introduced for a namespace of a blacklisted class
203
+     *
204
+     * Example:
205
+     * - Blacklist entry:      OCP\AppFramework\IApi
206
+     * - Name:                 OCP\AppFramework
207
+     * - Alias:                OAF
208
+     * =>  new blacklist entry:  OAF\IApi
209
+     *
210
+     * @param string $name
211
+     * @param string $alias
212
+     */
213
+    private function addUseNameToBlackList($name, $alias) {
214
+        $name = strtolower($name);
215
+        $alias = strtolower($alias);
216
+
217
+        foreach ($this->blackListedClassNames as $blackListedAlias => $blackListedClassName) {
218
+            if (strpos($blackListedClassName, $name . '\\') === 0) {
219
+                $aliasedClassName = str_replace($name, $alias, $blackListedClassName);
220
+                $this->blackListedClassNames[$aliasedClassName] = $blackListedClassName;
221
+            }
222
+        }
223
+
224
+        foreach ($this->blackListedConstants as $blackListedAlias => $blackListedConstant) {
225
+            if (strpos($blackListedConstant, $name . '\\') === 0 || strpos($blackListedConstant, $name . '::') === 0) {
226
+                $aliasedConstantName = str_replace($name, $alias, $blackListedConstant);
227
+                $this->blackListedConstants[$aliasedConstantName] = $blackListedConstant;
228
+            }
229
+        }
230
+
231
+        foreach ($this->blackListedFunctions as $blackListedAlias => $blackListedFunction) {
232
+            if (strpos($blackListedFunction, $name . '\\') === 0 || strpos($blackListedFunction, $name . '::') === 0) {
233
+                $aliasedFunctionName = str_replace($name, $alias, $blackListedFunction);
234
+                $this->blackListedFunctions[$aliasedFunctionName] = $blackListedFunction;
235
+            }
236
+        }
237
+
238
+        foreach ($this->blackListedMethods as $blackListedAlias => $blackListedMethod) {
239
+            if (strpos($blackListedMethod, $name . '\\') === 0 || strpos($blackListedMethod, $name . '::') === 0) {
240
+                $aliasedMethodName = str_replace($name, $alias, $blackListedMethod);
241
+                $this->blackListedMethods[$aliasedMethodName] = $blackListedMethod;
242
+            }
243
+        }
244
+    }
245
+
246
+    private function checkBlackList($name, $errorCode, Node $node) {
247
+        $lowerName = strtolower($name);
248
+
249
+        if (isset($this->blackListedClassNames[$lowerName])) {
250
+            $this->errors[] = [
251
+                'disallowedToken' => $name,
252
+                'errorCode' => $errorCode,
253
+                'line' => $node->getLine(),
254
+                'reason' => $this->buildReason($this->blackListedClassNames[$lowerName], $errorCode)
255
+            ];
256
+        }
257
+    }
258
+
259
+    private function checkBlackListConstant($class, $constantName, Node $node) {
260
+        $name = $class . '::' . $constantName;
261
+        $lowerName = strtolower($name);
262
+
263
+        if (isset($this->blackListedConstants[$lowerName])) {
264
+            $this->errors[] = [
265
+                'disallowedToken' => $name,
266
+                'errorCode' => CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED,
267
+                'line' => $node->getLine(),
268
+                'reason' => $this->buildReason($this->blackListedConstants[$lowerName], CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED)
269
+            ];
270
+        }
271
+    }
272
+
273
+    private function checkBlackListFunction($class, $functionName, Node $node) {
274
+        $name = $class . '::' . $functionName;
275
+        $lowerName = strtolower($name);
276
+
277
+        if (isset($this->blackListedFunctions[$lowerName])) {
278
+            $this->errors[] = [
279
+                'disallowedToken' => $name,
280
+                'errorCode' => CodeChecker::STATIC_CALL_NOT_ALLOWED,
281
+                'line' => $node->getLine(),
282
+                'reason' => $this->buildReason($this->blackListedFunctions[$lowerName], CodeChecker::STATIC_CALL_NOT_ALLOWED)
283
+            ];
284
+        }
285
+    }
286
+
287
+    private function checkBlackListMethod($class, $functionName, Node $node) {
288
+        $name = $class . '::' . $functionName;
289
+        $lowerName = strtolower($name);
290
+
291
+        if (isset($this->blackListedMethods[$lowerName])) {
292
+            $this->errors[] = [
293
+                'disallowedToken' => $name,
294
+                'errorCode' => CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED,
295
+                'line' => $node->getLine(),
296
+                'reason' => $this->buildReason($this->blackListedMethods[$lowerName], CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED)
297
+            ];
298
+        }
299
+    }
300
+
301
+    private function buildReason($name, $errorCode) {
302
+        if (isset($this->errorMessages[$errorCode])) {
303
+            $desc = $this->list->getDescription($errorCode, $name);
304
+            return sprintf($this->errorMessages[$errorCode], $desc);
305
+        }
306
+
307
+        return "$name usage not allowed - error: $errorCode";
308
+    }
309 309
 }
Please login to merge, or discard this patch.