Completed
Push — master ( 94ab2e...2cd79a )
by Lukas
12:30
created

JSResourceLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 5
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Template;
27
28
class JSResourceLocator extends ResourceLocator {
29
30
	/** @var JSCombiner */
31
	protected $jsCombiner;
32
33
	public function __construct(\OCP\ILogger $logger, $theme, array $core_map, array $party_map, JSCombiner $JSCombiner) {
34
		parent::__construct($logger, $theme, $core_map, $party_map);
35
36
		$this->jsCombiner = $JSCombiner;
37
	}
38
39
	/**
40
	 * @param string $script
41
	 */
42
	public function doFind($script) {
43
		$theme_dir = 'themes/'.$this->theme.'/';
44
		if (strpos($script, '3rdparty') === 0
45
			&& $this->appendIfExist($this->thirdpartyroot, $script.'.js')) {
46
			return;
47
		}
48
49
		if (strpos($script, '/l10n/') !== false) {
50
			// For language files we try to load them all, so themes can overwrite
51
			// single l10n strings without having to translate all of them.
52
			$found = 0;
53
			$found += $this->appendIfExist($this->serverroot, 'core/'.$script.'.js');
54
			$found += $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js');
55
			$found += $this->appendIfExist($this->serverroot, $script.'.js');
56
			$found += $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js');
57
			$found += $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js');
58
59
			if ($found) {
60
				return;
61
			}
62
		} else if ($this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js')
63
			|| $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js')
64
			|| $this->appendIfExist($this->serverroot, $script.'.js')
65
			|| $this->cacheAndAppendCombineJsonIfExist($this->serverroot, $script.'.json')
66
			|| $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js')
67
			|| $this->appendIfExist($this->serverroot, 'core/'.$script.'.js')
68
			|| $this->cacheAndAppendCombineJsonIfExist($this->serverroot, 'core/'.$script.'.json')
69
		) {
70
			return;
71
		}
72
73
		$app = substr($script, 0, strpos($script, '/'));
74
		$script = substr($script, strpos($script, '/')+1);
75
		$app_path = \OC_App::getAppPath($app);
76
		$app_url = \OC_App::getAppWebPath($app);
77
78
		// missing translations files fill be ignored
79
		if (strpos($script, 'l10n/') === 0) {
80
			$this->appendIfExist($app_path, $script . '.js', $app_url);
0 ignored issues
show
Security Bug introduced by
It seems like $app_path defined by \OC_App::getAppPath($app) on line 75 can also be of type false; however, OC\Template\ResourceLocator::appendIfExist() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $app_url defined by \OC_App::getAppWebPath($app) on line 76 can also be of type false; however, OC\Template\ResourceLocator::appendIfExist() does only seem to accept string|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
81
			return;
82
		}
83
		if (!$this->cacheAndAppendCombineJsonIfExist($app_path, $script.'.json', $app)) {
84
			$this->append($app_path, $script . '.js', $app_url);
0 ignored issues
show
Security Bug introduced by
It seems like $app_path defined by \OC_App::getAppPath($app) on line 75 can also be of type false; however, OC\Template\ResourceLocator::append() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $app_url defined by \OC_App::getAppWebPath($app) on line 76 can also be of type false; however, OC\Template\ResourceLocator::append() does only seem to accept string|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
85
		}
86
	}
87
88
	/**
89
	 * @param string $script
90
	 */
91
	public function doFindTheme($script) {
92
	}
93
94
	protected function cacheAndAppendCombineJsonIfExist($root, $file, $app = 'core') {
95
		if (is_file($root.'/'.$file)) {
96
			if ($this->jsCombiner->process($root, $file, $app)) {
97
				$this->append($this->serverroot, $this->jsCombiner->getCachedJS($app, $file), false, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
			} else {
99
				// Add all the files from the json
100
				$files = $this->jsCombiner->getContent($root, $file);
101
				$app_url = \OC_App::getAppWebPath($app);
102
103
				foreach ($files as $jsFile) {
104
					$this->append($root, $jsFile, $app_url);
0 ignored issues
show
Security Bug introduced by
It seems like $app_url defined by \OC_App::getAppWebPath($app) on line 101 can also be of type false; however, OC\Template\ResourceLocator::append() does only seem to accept string|null, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
105
				}
106
			}
107
			return true;
108
		}
109
110
		return false;
111
	}
112
}
113