GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c5bb66...d445c4 )
by Alexey
11:04
created

connector.php ➔ logger()   C

Complexity

Conditions 13
Paths 3

Size

Total Lines 46
Code Lines 32

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 13
eloc 32
nc 3
nop 4
dl 0
loc 46
rs 5.1118

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
set_time_limit(0); // just in case it too long, not recommended for production
4
error_reporting(E_ALL | E_STRICT); // Set E_ALL for debuging
5
// error_reporting(0);
6
ini_set('max_file_uploads', 50);   // allow uploading up to 50 files at once
7
8
// needed for case insensitive search to work, due to broken UTF-8 support in PHP
9
ini_set('mbstring.internal_encoding', 'UTF-8');
10
ini_set('mbstring.func_overload', 2);
11
12
if (function_exists('date_default_timezone_set')) {
13
	date_default_timezone_set('Europe/Moscow');
14
}
15
16
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderConnector.class.php';
17
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinder.class.php';
18
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeDriver.class.php';
19
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeLocalFileSystem.class.php';
20
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeMySQL.class.php';
21
include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'elFinderVolumeFTP.class.php';
22
23
function debug($o) {
24
	echo '<pre>';
25
	print_r($o);
26
}
27
28
29
30
/**
31
 * Smart logger function
32
 * Demonstrate how to work with elFinder event api
33
 *
34
 * @param  string   $cmd       command name
35
 * @param  array    $result    command result
36
 * @param  array    $args      command arguments from client
37
 * @param  elFinder $elfinder  elFinder instance
38
 * @return void|true
39
 * @author Troex Nevelin
40
 **/
41
function logger($cmd, $result, $args, $elfinder) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $elfinder is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
43
	
44
	$log = sprintf("[%s] %s: %s \n", date('r'), strtoupper($cmd), var_export($result, true));
45
	$logfile = '../files/temp/log.txt';
46
	$dir = dirname($logfile);
47
	if (!is_dir($dir) && !mkdir($dir)) {
48
		return;
49
	}
50
	if (($fp = fopen($logfile, 'a'))) {
51
		fwrite($fp, $log);
52
		fclose($fp);
53
	}
54
	return;
55
56
	foreach ($result as $key => $value) {
0 ignored issues
show
Unused Code introduced by
foreach ($result as $key...mplode(', ', $data)); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
57
		if (empty($value)) {
58
			continue;
59
		}
60
		$data = array();
61
		if (in_array($key, array('error', 'warning'))) {
62
			array_push($data, implode(' ', $value));
63
		} else {
64
			if (is_array($value)) { // changes made to files
65
				foreach ($value as $file) {
66
					$filepath = (isset($file['realpath']) ? $file['realpath'] : $elfinder->realpath($file['hash']));
67
					array_push($data, $filepath);
68
				}
69
			} else { // other value (ex. header)
70
				array_push($data, $value);
71
			}
72
		}
73
		$log .= sprintf(' %s(%s)', $key, implode(', ', $data));
74
	}
75
	$log .= "\n";
76
77
	$logfile = '../files/temp/log.txt';
78
	$dir = dirname($logfile);
79
	if (!is_dir($dir) && !mkdir($dir)) {
0 ignored issues
show
Bug introduced by
The variable $dir seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
80
		return;
81
	}
82
	if (($fp = fopen($logfile, 'a'))) {
0 ignored issues
show
Bug introduced by
The variable $logfile seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
83
		fwrite($fp, $log);
84
		fclose($fp);
85
	}
86
}
87
88
89
/**
90
 * Simple logger function.
91
 * Demonstrate how to work with elFinder event api.
92
 *
93
 * @package elFinder
94
 * @author Dmitry (dio) Levashov
95
 **/
96
class elFinderSimpleLogger {
97
	
98
	/**
99
	 * Log file path
100
	 *
101
	 * @var string
102
	 **/
103
	protected $file = '';
104
	
105
	/**
106
	 * constructor
107
	 *
108
	 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
109
	 * @author Dmitry (dio) Levashov
110
	 **/
111
	public function __construct($path) {
112
		$this->file = $path;
113
		$dir = dirname($path);
114
		if (!is_dir($dir)) {
115
			mkdir($dir);
116
		}
117
	}
118
	
119
	/**
120
	 * Create log record
121
	 *
122
	 * @param  string   $cmd       command name
123
	 * @param  array    $result    command result
124
	 * @param  array    $args      command arguments from client
125
	 * @param  elFinder $elfinder  elFinder instance
126
	 * @return void|true
127
	 * @author Dmitry (dio) Levashov
128
	 **/
129
	public function log($cmd, $result, $args, $elfinder) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
130
		$log = $cmd.' ['.date('d.m H:s')."]\n";
131
		
132 View Code Duplication
		if (!empty($result['error'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
			$log .= "\tERROR: ".implode(' ', $result['error'])."\n";
134
		}
135
		
136 View Code Duplication
		if (!empty($result['warning'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
			$log .= "\tWARNING: ".implode(' ', $result['warning'])."\n";
138
		}
139
		
140
		if (!empty($result['removed'])) {
141
			foreach ($result['removed'] as $file) {
142
				// removed file contain additional field "realpath"
143
				$log .= "\tREMOVED: ".$file['realpath']."\n";
144
			}
145
		}
146
		
147 View Code Duplication
		if (!empty($result['added'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
			foreach ($result['added'] as $file) {
149
				$log .= "\tADDED: ".$elfinder->realpath($file['hash'])."\n";
150
			}
151
		}
152
		
153 View Code Duplication
		if (!empty($result['changed'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
			foreach ($result['changed'] as $file) {
155
				$log .= "\tCHANGED: ".$elfinder->realpath($file['hash'])."\n";
156
			}
157
		}
158
		
159
		$this->write($log);
160
	}
161
	
162
	/**
163
	 * Write log into file
164
	 *
165
	 * @param  string  $log  log record
166
	 * @return void
167
	 * @author Dmitry (dio) Levashov
168
	 **/
169
	protected function write($log) {
170
		
171
		if (($fp = @fopen($this->file, 'a'))) {
172
			fwrite($fp, $log."\n");
173
			fclose($fp);
174
		}
175
	}
176
	
177
	
178
} // END class 
179
180
181
/**
182
 * Simple function to demonstrate how to control file access using "accessControl" callback.
183
 * This method will disable accessing files/folders starting from  '.' (dot)
184
 *
185
 * @param  string  $attr  attribute name (read|write|locked|hidden)
186
 * @param  string  $path  file path relative to volume root directory started with directory separator
187
 * @return bool|null
188
 **/
189
function access($attr, $path, $data, $volume) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $volume is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
The function access() has been defined more than once; this definition is ignored, only the first definition in protected/extensions/elF...p/connector.minimal.php (L23-27) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
190
	return strpos(basename($path), '.') === 0       // if file/folder begins with '.' (dot)
191
		? !($attr == 'read' || $attr == 'write')    // set read+write to false, other (locked+hidden) set to true
192
		:  null;                                    // else elFinder decide it itself
193
}
194
195
/**
196
 * Access control example class
197
 *
198
 * @author Dmitry (dio) Levashov
199
 **/
200
class elFinderTestACL {
201
	
202
	/**
203
	 * make dotfiles not readable, not writable, hidden and locked
204
	 *
205
	 * @param  string  $attr  attribute name (read|write|locked|hidden)
206
	 * @param  string  $path  file path. Attention! This is path relative to volume root directory started with directory separator.
207
	 * @param  mixed   $data  data which seted in 'accessControlData' elFinder option
208
	 * @param  elFinderVolumeDriver  $volume  volume driver
209
	 * @return bool
210
	 * @author Dmitry (dio) Levashov
211
	 **/
212
	public function fsAccess($attr, $path, $data, $volume) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
		
214
		if ($volume->name() == 'localfilesystem') {
0 ignored issues
show
Bug introduced by
The method name() does not seem to exist on object<elFinderVolumeDriver>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
215
			return strpos(basename($path), '.') === 0
216
				? !($attr == 'read' || $attr == 'write')
217
				: $attr == 'read' || $attr == 'write';
218
		}
219
		
220
		return true;
221
	}
222
	
223
} // END class 
224
225
$acl = new elFinderTestACL();
226
227
function validName($name) {
228
	return strpos($name, '.') !== 0;
229
}
230
231
232
$logger = new elFinderSimpleLogger('../files/temp/log.txt');
233
234
235
236
$opts = array(
237
	'locale' => 'en_US.UTF-8',
238
	'bind' => array(
239
		// '*' => 'logger',
240
		'mkdir mkfile rename duplicate upload rm paste' => 'logger'
241
	),
242
	'debug' => true,
243
	'roots' => array(
244
		array(
245
			'driver'     => 'LocalFileSystem',
246
			'path'       => '../files/',
247
			'startPath'  => '../files/test/',
248
			'URL'        => dirname($_SERVER['PHP_SELF']) . '/../files/',
249
			// 'treeDeep'   => 3,
250
			// 'alias'      => 'File system',
251
			'mimeDetect' => 'internal',
252
			'tmbPath'    => '.tmb',
253
			'utf8fix'    => true,
254
			'tmbCrop'    => false,
255
			'tmbBgColor' => 'transparent',
256
			'accessControl' => 'access',
257
			'acceptedName'    => '/^[^\.].*$/',
258
			// 'disabled' => array('extract', 'archive'),
259
			// 'tmbSize' => 128,
260
			'attributes' => array(
261
				array(
262
					'pattern' => '/\.js$/',
263
					'read' => true,
264
					'write' => false
265
				),
266
				array(
267
					'pattern' => '/^\/icons$/',
268
					'read' => true,
269
					'write' => false
270
				)
271
			)
272
			// 'uploadDeny' => array('application', 'text/xml')
273
		),
274
		// array(
275
		// 	'driver'     => 'LocalFileSystem',
276
		// 	'path'       => '../files2/',
277
		// 	// 'URL'        => dirname($_SERVER['PHP_SELF']) . '/../files2/',
278
		// 	'alias'      => 'File system',
279
		// 	'mimeDetect' => 'internal',
280
		// 	'tmbPath'    => '.tmb',
281
		// 	'utf8fix'    => true,
282
		// 	'tmbCrop'    => false,
283
		// 	'startPath'  => '../files/test',
284
		// 	// 'separator' => ':',
285
		// 	'attributes' => array(
286
		// 		array(
287
		// 			'pattern' => '~/\.~',
288
		// 			// 'pattern' => '/^\/\./',
289
		// 			'read' => false,
290
		// 			'write' => false,
291
		// 			'hidden' => true,
292
		// 			'locked' => false
293
		// 		),
294
		// 		array(
295
		// 			'pattern' => '~/replace/.+png$~',
296
		// 			// 'pattern' => '/^\/\./',
297
		// 			'read' => false,
298
		// 			'write' => false,
299
		// 			// 'hidden' => true,
300
		// 			'locked' => true
301
		// 		)
302
		// 	),
303
		// 	// 'defaults' => array('read' => false, 'write' => true)
304
		// ),
305
		
306
		// array(
307
		// 	'driver' => 'FTP',
308
		// 	'host' => '192.168.1.38',
309
		// 	'user' => 'dio',
310
		// 	'pass' => 'hane',
311
		// 	'path' => '/Users/dio/Documents',
312
		// 	'tmpPath' => '../files/ftp',
313
		// 	'utf8fix' => true,
314
		// 	'attributes' => array(
315
		// 		array(
316
		// 			'pattern' => '~/\.~',
317
		// 			'read' => false,
318
		// 			'write' => false,
319
		// 			'hidden' => true,
320
		// 			'locked' => false
321
		// 		),
322
		// 		
323
		// 	)
324
		// ),
325
		array(
326
			'driver' => 'FTP',
327
			'host' => 'work.std42.ru',
328
			'user' => 'dio',
329
			'pass' => 'wallrus',
330
			'path' => '/',
331
			'tmpPath' => '../files/ftp',
332
		),
333
		// array(
334
		// 	'driver' => 'FTP',
335
		// 	'host' => '10.0.1.3',
336
		// 	'user' => 'frontrow',
337
		// 	'pass' => 'frontrow',
338
		// 	'path' => '/',
339
		// 	'tmpPath' => '../files/ftp',
340
		// ),
341
		
342
		// array(
343
		// 	'driver'     => 'LocalFileSystem',
344
		// 	'path'       => '../files2/',
345
		// 	'URL'        => dirname($_SERVER['PHP_SELF']) . '/../files2/',
346
		// 	'alias'      => 'Files',
347
		// 	'mimeDetect' => 'internal',
348
		// 	'tmbPath'    => '.tmb',
349
		// 	// 'copyOverwrite' => false,
350
		// 	'utf8fix'    => true,
351
		// 	'attributes' => array(
352
		// 		array(
353
		// 			'pattern' => '~/\.~',
354
		// 			// 'pattern' => '/^\/\./',
355
		// 			// 'read' => false,
356
		// 			// 'write' => false,
357
		// 			'hidden' => true,
358
		// 			'locked' => false
359
		// 		),
360
		// 	)
361
		// ),
362
		
363
		// array(
364
		// 	'driver' => 'MySQL',
365
		// 	'path' => 1,
366
		// 	// 'treeDeep' => 2,
367
		// 	// 'socket'          => '/opt/local/var/run/mysql5/mysqld.sock',
368
		// 	'user' => 'root',
369
		// 	'pass' => 'hane',
370
		// 	'db' => 'elfinder',
371
		// 	'user_id' => 1,
372
		// 	// 'accessControl' => 'access',
373
		// 	// 'separator' => ':',
374
		// 	'tmbCrop'         => true,
375
		// 	// thumbnails background color (hex #rrggbb or 'transparent')
376
		// 	'tmbBgColor'      => '#000000',
377
		// 	'files_table' => 'elfinder_file',
378
		// 	// 'imgLib' => 'imagick',
379
		// 	// 'uploadOverwrite' => false,
380
		// 	// 'copyTo' => false,
381
		// 	// 'URL'    => 'http://localhost/git/elfinder',
382
		// 	'tmpPath' => '../filesdb/tmp',
383
		// 	'tmbPath' => '../filesdb/tmb',
384
		// 	'tmbURL' => dirname($_SERVER['PHP_SELF']) . '/../filesdb/tmb/',
385
		// 	// 'attributes' => array(
386
		// 	// 	array(),
387
		// 	// 	array(
388
		// 	// 		'pattern' => '/\.jpg$/',
389
		// 	// 		'read' => false,
390
		// 	// 		'write' => false,
391
		// 	// 		'locked' => true,
392
		// 	// 		'hidden' => true
393
		// 	// 	)
394
		// 	// )
395
		// 	
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
396
		// )
397
	)
398
		
399
);
400
401
402
403
// sleep(3);
404
header('Access-Control-Allow-Origin: *');
405
$connector = new elFinderConnector(new elFinder($opts), true);
406
$connector->run();
407
408
// echo '<pre>';
409
// print_r($connector);
410