Issues (5)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/ShortcodeManager.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Bright Nucleus Shortcode Component.
4
 *
5
 * @package   BrightNucleus\Shortcode
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Shortcode;
13
14
use BrightNucleus\Config\ConfigInterface;
15
use BrightNucleus\Config\ConfigTrait;
16
use BrightNucleus\Dependency\DependencyManagerInterface as DependencyManager;
17
use BrightNucleus\Exception\RuntimeException;
18
use BrightNucleus\Invoker\InstantiatorTrait;
19
use BrightNucleus\Shortcode\Exception\FailedToInstantiateObject;
20
use BrightNucleus\View\ViewBuilder;
21
use BrightNucleus\Views;
22
use Exception;
23
24
/**
25
 * Shortcode Manager.
26
 *
27
 * This class manages all the shortcodes that it gets passed within a
28
 * ConfigInterface.
29
 *
30
 * @since   0.1.0
31
 *
32
 * @package BrightNucleus\Shortcode
33
 * @author  Alain Schlesser <[email protected]>
34
 */
35
class ShortcodeManager implements ShortcodeManagerInterface {
36
37
	use ConfigTrait;
38
	use InstantiatorTrait;
39
40
	/*
41
	 * The delimiter that is used to express key-subkey relations in the config.
42
	 */
43
	const CONFIG_SEPARATOR = '/';
44
45
	/*
46
	 * Default classes that are used when omitted from the config.
47
	 */
48
	const DEFAULT_SHORTCODE             = 'BrightNucleus\Shortcode\Shortcode';
49
	const DEFAULT_SHORTCODE_ATTS_PARSER = 'BrightNucleus\Shortcode\ShortcodeAttsParser';
50
	const DEFAULT_SHORTCODE_UI          = 'BrightNucleus\Shortcode\ShortcodeUI';
51
52
	/*
53
	 * The names of the configuration keys.
54
	 */
55
	const KEY_CUSTOM_ATTS_PARSER = 'custom_atts_parser';
56
	const KEY_CUSTOM_CLASS       = 'custom_class';
57
	const KEY_CUSTOM_UI          = 'custom_ui';
58
	const KEY_UI                 = 'ui';
59
	/**
60
	 * Collection of ShortcodeInterface objects.
61
	 *
62
	 * @since 0.1.0
63
	 *
64
	 * @var ShortcodeInterface[]
65
	 */
66
	protected $shortcodes = [];
67
68
	/**
69
	 * DependencyManagerInterface implementation.
70
	 *
71
	 * @since 0.1.0
72
	 *
73
	 * @var DependencyManager
74
	 */
75
	protected $dependencies;
76
77
	/**
78
	 * View builder instance to use for rendering views.
79
	 *
80
	 * @since 0.4.0
81
	 *
82
	 * @var ViewBuilder
83
	 */
84
	protected $view_builder;
85
86
	/**
87
	 * Collection of ShortcodeUIInterface objects.
88
	 *
89
	 * @since 0.1.0
90
	 *
91
	 * @var ShortcodeUIInterface[]
92
	 */
93
	protected $shortcode_uis = [];
94
95
	/**
96
	 * External injector to use.
97
	 *
98
	 * @var object
99
	 */
100
	protected $injector;
101
102
	/**
103
	 * Instantiate a ShortcodeManager object.
104
	 *
105
	 * @since 0.1.0
106
	 * @since 0.4.0 Add optional $viewBuilder argument.
107
	 *
108
	 * @param ConfigInterface        $config       Configuration to set up the
109
	 *                                             shortcodes.
110
	 * @param DependencyManager|null $dependencies Optional. Dependencies that
111
	 *                                             are needed by the
112
	 *                                             shortcodes.
113
	 * @param ViewBuilder|null       $view_builder Optional. View builder
114
	 *                                             instance to use for
115
	 *                                             rendering views.
116
	 *
117
	 * @throws RuntimeException If the config could not be processed.
118
	 */
119
	public function __construct(
120
		ConfigInterface $config,
121
		DependencyManager $dependencies = null,
122
		ViewBuilder $view_builder = null
123
	) {
124
		$this->processConfig( $config );
125
		$this->dependencies = $dependencies;
126
		$this->view_builder = $view_builder ?? Views::getViewBuilder();
127
	}
128
129
	/**
130
	 * Use an external injector to instantiate the different classes.
131
	 *
132
	 * The injector will
133
	 * @param object $injector Injector to use.
134
	 */
135
	public function with_injector( $injector ) {
136
		if ( ! method_exists( $injector, 'make' ) ) {
137
			throw new RuntimeException(
138
				'Invalid injector provided, it does not have a make() method.'
139
			);
140
		}
141
142
		$this->injector = $injector;
143
	}
144
145
	/**
146
	 * Initialize the Shortcode Manager.
147
	 *
148
	 * @since 0.1.0
149
	 */
150
	protected function init_shortcodes() {
151
		foreach ( $this->getConfigKeys() as $tag ) {
152
			$this->init_shortcode( $tag );
153
		}
154
	}
155
156
	/**
157
	 * Initialize a single shortcode.
158
	 *
159
	 * @since 0.1.0
160
	 *
161
	 * @param string $tag The tag of the shortcode to register.
162
	 *
163
	 * @throws FailedToInstantiateObject If the Shortcode Atts Parser object
164
	 *                                   could not be instantiated.
165
	 * @throws FailedToInstantiateObject If the Shortcode object could not be
166
	 *                                   instantiated.
167
	 */
168
	protected function init_shortcode( $tag ) {
169
		$shortcode_class       = $this->get_shortcode_class( $tag );
170
		$shortcode_atts_parser = $this->get_shortcode_atts_parser_class( $tag );
171
172
		$atts_parser = $this->instantiate(
173
			ShortcodeAttsParserInterface::class,
174
			$shortcode_atts_parser,
175
			[ 'config' => $this->config->getSubConfig( $tag ) ]
176
		);
177
178
		$this->shortcodes[] = $this->instantiate(
179
			ShortcodeInterface::class,
180
			$shortcode_class,
181
			[
182
				'shortcode_tag' => $tag,
183
				'config'        => $this->config->getSubConfig( $tag ),
184
				'atts_parser'   => $atts_parser,
185
				'dependencies'  => $this->dependencies,
186
				'view_builder'  => $this->view_builder,
187
			]
188
		);
189
190
		if ( $this->hasConfigKey( $tag, self::KEY_UI ) ) {
191
			$this->init_shortcode_ui( $tag );
192
		}
193
	}
194
195
	/**
196
	 * Get the class name of an implementation of the ShortcodeInterface.
197
	 *
198
	 * @since 0.1.0
199
	 *
200
	 * @param string $tag Shortcode tag to get the class for.
201
	 *
202
	 * @return string Class name of the Shortcode.
203
	 */
204
	protected function get_shortcode_class( $tag ) {
205
		$shortcode_class = $this->hasConfigKey( $tag, self::KEY_CUSTOM_CLASS )
206
			? $this->getConfigKey( $tag, self::KEY_CUSTOM_CLASS )
207
			: self::DEFAULT_SHORTCODE;
208
209
		return $shortcode_class;
210
	}
211
212
	/**
213
	 * Get the class name of an implementation of the
214
	 * ShortcodeAttsParsersInterface.
215
	 *
216
	 * @since 0.1.0
217
	 *
218
	 * @param string $tag Shortcode tag to get the class for.
219
	 *
220
	 * @return string Class name of the ShortcodeAttsParser.
221
	 */
222
	protected function get_shortcode_atts_parser_class( $tag ) {
223
		$atts_parser = $this->hasConfigKey( $tag, self::KEY_CUSTOM_ATTS_PARSER )
224
			? $this->getConfigKey( $tag, self::KEY_CUSTOM_ATTS_PARSER )
225
			: self::DEFAULT_SHORTCODE_ATTS_PARSER;
226
227
		return $atts_parser;
228
	}
229
230
	/**
231
	 * Initialize the Shortcode UI for a single shortcode.
232
	 *
233
	 * @since 0.1.0
234
	 *
235
	 * @param string $tag                The tag of the shortcode to register
236
	 *                                   the UI for.
237
	 *
238
	 * @throws FailedToInstantiateObject If the Shortcode UI object could not
239
	 *                                   be instantiated.
240
	 */
241
	protected function init_shortcode_ui( $tag ) {
242
		$shortcode_ui_class = $this->get_shortcode_ui_class( $tag );
243
244
		$this->shortcode_uis[] = $this->instantiate(
245
			ShortcodeUIInterface::class,
246
			$shortcode_ui_class,
247
			[
248
				'shortcode_tag' => $tag,
249
				'config'        => $this->config->getSubConfig( $tag, self::KEY_UI ),
0 ignored issues
show
The call to ConfigInterface::getSubConfig() has too many arguments starting with self::KEY_UI.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
250
				'dependencies'  => $this->dependencies,
251
			]
252
		);
253
	}
254
255
	/**
256
	 * Get the class name of an implementation of the ShortcodeUIInterface.
257
	 *
258
	 * @since 0.1.0
259
	 *
260
	 * @param string $tag Configuration settings.
261
	 *
262
	 * @return string Class name of the ShortcodeUI.
263
	 */
264
	protected function get_shortcode_ui_class( $tag ) {
265
		$ui_class = $this->hasConfigKey( $tag, self::KEY_CUSTOM_UI )
266
			? $this->getConfigKey( $tag, self::KEY_CUSTOM_UI )
267
			: self::DEFAULT_SHORTCODE_UI;
268
269
		return $ui_class;
270
	}
271
272
	/**
273
	 * Register all of the shortcode handlers.
274
	 *
275
	 * @since 0.1.0
276
	 *
277
	 * @param mixed $context Optional. Context information to pass to shortcode.
278
	 *
279
	 * @return void
280
	 */
281
	public function register( $context = null ) {
282
		$this->init_shortcodes();
283
284
		$context                  = $this->validate_context( $context );
285
		$context['page_template'] = $this->get_page_template();
286
287
		array_walk( $this->shortcodes,
288
			function ( ShortcodeInterface $shortcode ) use ( $context ) {
289
				$shortcode->register( $context );
290
			} );
291
292
		// This hook only gets fired when Shortcode UI plugin is active.
293
		\add_action(
294
			'register_shortcode_ui',
295
			[ $this, 'register_shortcode_ui', ]
296
		);
297
	}
298
299
	/**
300
	 * Validate the context to make sure it is an array.
301
	 *
302
	 * @since 0.2.3
303
	 *
304
	 * @param mixed $context The context as passed in by WordPress.
305
	 *
306
	 * @return array Validated context.
307
	 */
308
	protected function validate_context( $context ) {
309
		if ( is_string( $context ) ) {
310
			return [ 'wp_context' => $context ];
311
		}
312
313
		return (array) $context;
314
	}
315
316
	/**
317
	 * Get the name of the page template.
318
	 *
319
	 * @since 0.1.0
320
	 *
321
	 * @return string Name of the page template.
322
	 */
323
	protected function get_page_template() {
324
		$template = str_replace(
325
			\trailingslashit( \get_stylesheet_directory() ),
326
			'',
327
			\get_page_template()
328
		);
329
330
		return $template;
331
	}
332
333
	/**
334
	 * Register the shortcode UI handlers.
335
	 *
336
	 * @since 0.1.0
337
	 */
338
	public function register_shortcode_ui() {
339
		$template = $this->get_page_template();
340
		$context  = [ 'page_template' => $template ];
341
342
		array_walk( $this->shortcode_uis,
343
			function ( ShortcodeUIInterface $shortcode_ui ) use ( $context ) {
344
				$shortcode_ui->register( $context );
345
			}
346
		);
347
	}
348
349
	/**
350
	 * Execute a specific shortcode directly from code.
351
	 *
352
	 * @since 0.2.4
353
	 *
354
	 * @param string $tag     Tag of the shortcode to execute.
355
	 * @param array  $atts    Array of attributes to pass to the shortcode.
356
	 * @param null   $content Inner content to pass to the shortcode.
357
	 *
358
	 * @return string|false Rendered HTML.
359
	 */
360
	public function do_tag( $tag, array $atts = [], $content = null ) {
361
		return \BrightNucleus\Shortcode\do_tag( $tag, $atts, $content );
362
	}
363
364
	/**
365
	 * Instantiate a new object through either a class name or a factory method.
366
	 *
367
	 * @since 0.3.0
368
	 *
369
	 * @param string          $interface Interface the object needs to
370
	 *                                   implement.
371
	 * @param callable|string $class     Fully qualified class name or factory
372
	 *                                   method.
373
	 * @param array           $args      Arguments passed to the constructor or
374
	 *                                   factory method.
375
	 *
376
	 * @return object Object that implements the interface.
377
	 * @throws FailedToInstantiateObject If no valid object could be
378
	 *                                   instantiated.
379
	 */
380
	protected function instantiate( $interface, $class, array $args ) {
381
		try {
382
			if ( is_callable( $class ) ) {
383
				$class = call_user_func_array( $class, $args );
384
			}
385
386
			if ( is_string( $class ) ) {
387
				if ( null !== $this->injector ) {
388
					$class = $this->injector->make( $class, $args );
389
				} else {
390
					$class = $this->instantiateClass( $class, $args );
391
				}
392
			}
393
		} catch ( Exception $exception ) {
394
			throw FailedToInstantiateObject::fromFactory(
395
				$class,
396
				$interface,
397
				$exception
398
			);
399
		}
400
401
		if ( ! is_subclass_of( $class, $interface ) ) {
402
			throw FailedToInstantiateObject::fromInvalidObject(
403
				$class,
404
				$interface
405
			);
406
		}
407
408
		return $class;
409
	}
410
}
411