Completed
Branch master (44537d)
by
unknown
14:30 queued 10:03
created
core/services/bootstrap/BootstrapRequestResponseObjects.php 2 patches
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -29,88 +29,88 @@
 block discarded – undo
29 29
  */
30 30
 class BootstrapRequestResponseObjects
31 31
 {
32
-    protected LegacyRequestInterface $legacy_request;
32
+	protected LegacyRequestInterface $legacy_request;
33 33
 
34
-    protected LoaderInterface        $loader;
34
+	protected LoaderInterface        $loader;
35 35
 
36
-    protected RequestInterface       $request;
36
+	protected RequestInterface       $request;
37 37
 
38
-    protected ResponseInterface      $response;
38
+	protected ResponseInterface      $response;
39 39
 
40 40
 
41
-    /**
42
-     * BootstrapRequestResponseObjects constructor.
43
-     *
44
-     * @param LoaderInterface $loader
45
-     */
46
-    public function __construct(LoaderInterface $loader)
47
-    {
48
-        $this->loader = $loader;
49
-    }
41
+	/**
42
+	 * BootstrapRequestResponseObjects constructor.
43
+	 *
44
+	 * @param LoaderInterface $loader
45
+	 */
46
+	public function __construct(LoaderInterface $loader)
47
+	{
48
+		$this->loader = $loader;
49
+	}
50 50
 
51 51
 
52
-    /**
53
-     * @return void
54
-     */
55
-    public function buildRequestResponse()
56
-    {
57
-        $email_validator   = new Basic();
58
-        $request_sanitizer = new RequestSanitizer($email_validator);
59
-        $server_sanitizer  = new ServerSanitizer();
60
-        $request_params    = new RequestParams($request_sanitizer, $_GET, $_POST);
61
-        $server_params     = new ServerParams($server_sanitizer, $_SERVER);
62
-        // load our Request and Response objects
63
-        $this->request  = apply_filters(
64
-            'FHEE___EventEspresso_core_services_bootstrap_BootstrapRequestResponseObjects__buildRequestResponse__request',
65
-            new Request($request_params, $server_params),
66
-            $request_params,
67
-            $server_params,
68
-            $request_sanitizer,
69
-            $server_sanitizer
70
-        );
71
-        $this->response = apply_filters(
72
-            'FHEE___EventEspresso_core_services_bootstrap_BootstrapRequestResponseObjects__buildRequestResponse__response',
73
-            new Response()
74
-        );
75
-        $this->loader->share(Basic::class, $email_validator);
76
-        $this->loader->share(RequestSanitizer::class, $request_sanitizer);
77
-        $this->loader->share(ServerSanitizer::class, $server_sanitizer);
78
-        $this->loader->share(RequestParams::class, $request_params);
79
-        $this->loader->share(ServerParams::class, $server_params);
80
-    }
52
+	/**
53
+	 * @return void
54
+	 */
55
+	public function buildRequestResponse()
56
+	{
57
+		$email_validator   = new Basic();
58
+		$request_sanitizer = new RequestSanitizer($email_validator);
59
+		$server_sanitizer  = new ServerSanitizer();
60
+		$request_params    = new RequestParams($request_sanitizer, $_GET, $_POST);
61
+		$server_params     = new ServerParams($server_sanitizer, $_SERVER);
62
+		// load our Request and Response objects
63
+		$this->request  = apply_filters(
64
+			'FHEE___EventEspresso_core_services_bootstrap_BootstrapRequestResponseObjects__buildRequestResponse__request',
65
+			new Request($request_params, $server_params),
66
+			$request_params,
67
+			$server_params,
68
+			$request_sanitizer,
69
+			$server_sanitizer
70
+		);
71
+		$this->response = apply_filters(
72
+			'FHEE___EventEspresso_core_services_bootstrap_BootstrapRequestResponseObjects__buildRequestResponse__response',
73
+			new Response()
74
+		);
75
+		$this->loader->share(Basic::class, $email_validator);
76
+		$this->loader->share(RequestSanitizer::class, $request_sanitizer);
77
+		$this->loader->share(ServerSanitizer::class, $server_sanitizer);
78
+		$this->loader->share(RequestParams::class, $request_params);
79
+		$this->loader->share(ServerParams::class, $server_params);
80
+	}
81 81
 
82 82
 
83
-    /**
84
-     * @return void
85
-     * @throws InvalidArgumentException
86
-     */
87
-    public function shareRequestResponse()
88
-    {
89
-        $this->loader->share(Request::class, $this->request);
90
-        $this->loader->share(Response::class, $this->response);
91
-        EE_Dependency_Map::instance()->setRequest($this->request);
92
-        EE_Dependency_Map::instance()->setResponse($this->response);
93
-    }
83
+	/**
84
+	 * @return void
85
+	 * @throws InvalidArgumentException
86
+	 */
87
+	public function shareRequestResponse()
88
+	{
89
+		$this->loader->share(Request::class, $this->request);
90
+		$this->loader->share(Response::class, $this->response);
91
+		EE_Dependency_Map::instance()->setRequest($this->request);
92
+		EE_Dependency_Map::instance()->setResponse($this->response);
93
+	}
94 94
 
95 95
 
96
-    /**
97
-     * @return void
98
-     * @throws InvalidArgumentException
99
-     * @throws EE_Error
100
-     */
101
-    public function setupLegacyRequest()
102
-    {
103
-        espresso_load_required(
104
-            'EE_Request',
105
-            EE_CORE . 'request_stack/EE_Request.core.php'
106
-        );
107
-        $this->legacy_request = new EE_Request($_GET, $_POST, $_COOKIE, $_SERVER);
108
-        $this->legacy_request->setRequest($this->request);
109
-        $this->legacy_request->admin      = $this->request->isAdmin();
110
-        $this->legacy_request->ajax       = $this->request->isAjax();
111
-        $this->legacy_request->front_ajax = $this->request->isFrontAjax();
112
-        EE_Dependency_Map::instance()->setLegacyRequest($this->legacy_request);
113
-        $this->loader->share('EE_Request', $this->legacy_request);
114
-        $this->loader->share('EventEspresso\core\services\request\LegacyRequestInterface', $this->legacy_request);
115
-    }
96
+	/**
97
+	 * @return void
98
+	 * @throws InvalidArgumentException
99
+	 * @throws EE_Error
100
+	 */
101
+	public function setupLegacyRequest()
102
+	{
103
+		espresso_load_required(
104
+			'EE_Request',
105
+			EE_CORE . 'request_stack/EE_Request.core.php'
106
+		);
107
+		$this->legacy_request = new EE_Request($_GET, $_POST, $_COOKIE, $_SERVER);
108
+		$this->legacy_request->setRequest($this->request);
109
+		$this->legacy_request->admin      = $this->request->isAdmin();
110
+		$this->legacy_request->ajax       = $this->request->isAjax();
111
+		$this->legacy_request->front_ajax = $this->request->isFrontAjax();
112
+		EE_Dependency_Map::instance()->setLegacyRequest($this->legacy_request);
113
+		$this->loader->share('EE_Request', $this->legacy_request);
114
+		$this->loader->share('EventEspresso\core\services\request\LegacyRequestInterface', $this->legacy_request);
115
+	}
116 116
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
         $request_params    = new RequestParams($request_sanitizer, $_GET, $_POST);
61 61
         $server_params     = new ServerParams($server_sanitizer, $_SERVER);
62 62
         // load our Request and Response objects
63
-        $this->request  = apply_filters(
63
+        $this->request = apply_filters(
64 64
             'FHEE___EventEspresso_core_services_bootstrap_BootstrapRequestResponseObjects__buildRequestResponse__request',
65 65
             new Request($request_params, $server_params),
66 66
             $request_params,
@@ -102,7 +102,7 @@  discard block
 block discarded – undo
102 102
     {
103 103
         espresso_load_required(
104 104
             'EE_Request',
105
-            EE_CORE . 'request_stack/EE_Request.core.php'
105
+            EE_CORE.'request_stack/EE_Request.core.php'
106 106
         );
107 107
         $this->legacy_request = new EE_Request($_GET, $_POST, $_COOKIE, $_SERVER);
108 108
         $this->legacy_request->setRequest($this->request);
Please login to merge, or discard this patch.
core/services/addon/api/IncompatibleAddonHandler.php 1 patch
Indentation   +74 added lines, -74 removed lines patch added patch discarded remove patch
@@ -6,80 +6,80 @@
 block discarded – undo
6 6
 
7 7
 class IncompatibleAddonHandler
8 8
 {
9
-    /**
10
-     * @return void
11
-     */
12
-    public function deactivateIncompatibleAddons()
13
-    {
14
-        static $done = false;
15
-        if ($done) {
16
-            return;
17
-        }
18
-        $this->deactivateIncompatibleAddon(
19
-            'Wait Lists',
20
-            'EE_WAIT_LISTS_VERSION',
21
-            '1.0.0.beta.074',
22
-            'load_espresso_wait_lists',
23
-            'EE_WAIT_LISTS_PLUGIN_FILE'
24
-        );
25
-        $this->deactivateIncompatibleAddon(
26
-            'Automated Upcoming Event Notifications',
27
-            'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_VERSION',
28
-            '1.0.0.beta.091',
29
-            'load_espresso_automated_upcoming_event_notification',
30
-            'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_PLUGIN_FILE'
31
-        );
32
-        // $this->deactivateIncompatibleAddon(
33
-        //     'WP Users Integration',
34
-        //     'EE_WPUSERS_VERSION',
35
-        //     '2.1.0.rc.003',
36
-        //     'load_ee_core_wpusers',
37
-        //     'EE_WPUSERS_PLUGIN_FILE'
38
-        // );
39
-        $done = true;
40
-    }
9
+	/**
10
+	 * @return void
11
+	 */
12
+	public function deactivateIncompatibleAddons()
13
+	{
14
+		static $done = false;
15
+		if ($done) {
16
+			return;
17
+		}
18
+		$this->deactivateIncompatibleAddon(
19
+			'Wait Lists',
20
+			'EE_WAIT_LISTS_VERSION',
21
+			'1.0.0.beta.074',
22
+			'load_espresso_wait_lists',
23
+			'EE_WAIT_LISTS_PLUGIN_FILE'
24
+		);
25
+		$this->deactivateIncompatibleAddon(
26
+			'Automated Upcoming Event Notifications',
27
+			'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_VERSION',
28
+			'1.0.0.beta.091',
29
+			'load_espresso_automated_upcoming_event_notification',
30
+			'EE_AUTOMATED_UPCOMING_EVENT_NOTIFICATION_PLUGIN_FILE'
31
+		);
32
+		// $this->deactivateIncompatibleAddon(
33
+		//     'WP Users Integration',
34
+		//     'EE_WPUSERS_VERSION',
35
+		//     '2.1.0.rc.003',
36
+		//     'load_ee_core_wpusers',
37
+		//     'EE_WPUSERS_PLUGIN_FILE'
38
+		// );
39
+		$done = true;
40
+	}
41 41
 
42 42
 
43
-    /**
44
-     * @param string $addon_name
45
-     * @param string $version_constant
46
-     * @param string $min_version_required
47
-     * @param string $load_callback
48
-     * @param string $plugin_file_constant
49
-     * @return void
50
-     */
51
-    private function deactivateIncompatibleAddon(
52
-        string $addon_name,
53
-        string $version_constant,
54
-        string $min_version_required,
55
-        string $load_callback,
56
-        string $plugin_file_constant
57
-    ) {
58
-        if (! defined($version_constant)) {
59
-            return;
60
-        }
61
-        $addon_version = constant($version_constant);
62
-        if ($addon_version && version_compare($addon_version, $min_version_required, '<')) {
63
-            remove_action('AHEE__EE_System__load_espresso_addons', $load_callback);
64
-            if (! function_exists('deactivate_plugins')) {
65
-                require_once ABSPATH . 'wp-admin/includes/plugin.php';
66
-            }
67
-            deactivate_plugins(plugin_basename(constant($plugin_file_constant)));
68
-            unset($_GET['activate'], $_REQUEST['activate'], $_GET['activate-multi'], $_REQUEST['activate-multi']);
69
-            EE_Error::add_error(
70
-                sprintf(
71
-                    esc_html__(
72
-                        'We\'re sorry, but the Event Espresso %1$s addon was deactivated because version %2$s or higher is required with this version of Event Espresso core.',
73
-                        'event_espresso'
74
-                    ),
75
-                    $addon_name,
76
-                    $min_version_required
77
-                ),
78
-                __FILE__,
79
-                __FUNCTION__ . "({$addon_name})",
80
-                __LINE__
81
-            );
82
-            EE_Error::get_notices(false, true);
83
-        }
84
-    }
43
+	/**
44
+	 * @param string $addon_name
45
+	 * @param string $version_constant
46
+	 * @param string $min_version_required
47
+	 * @param string $load_callback
48
+	 * @param string $plugin_file_constant
49
+	 * @return void
50
+	 */
51
+	private function deactivateIncompatibleAddon(
52
+		string $addon_name,
53
+		string $version_constant,
54
+		string $min_version_required,
55
+		string $load_callback,
56
+		string $plugin_file_constant
57
+	) {
58
+		if (! defined($version_constant)) {
59
+			return;
60
+		}
61
+		$addon_version = constant($version_constant);
62
+		if ($addon_version && version_compare($addon_version, $min_version_required, '<')) {
63
+			remove_action('AHEE__EE_System__load_espresso_addons', $load_callback);
64
+			if (! function_exists('deactivate_plugins')) {
65
+				require_once ABSPATH . 'wp-admin/includes/plugin.php';
66
+			}
67
+			deactivate_plugins(plugin_basename(constant($plugin_file_constant)));
68
+			unset($_GET['activate'], $_REQUEST['activate'], $_GET['activate-multi'], $_REQUEST['activate-multi']);
69
+			EE_Error::add_error(
70
+				sprintf(
71
+					esc_html__(
72
+						'We\'re sorry, but the Event Espresso %1$s addon was deactivated because version %2$s or higher is required with this version of Event Espresso core.',
73
+						'event_espresso'
74
+					),
75
+					$addon_name,
76
+					$min_version_required
77
+				),
78
+				__FILE__,
79
+				__FUNCTION__ . "({$addon_name})",
80
+				__LINE__
81
+			);
82
+			EE_Error::get_notices(false, true);
83
+		}
84
+	}
85 85
 }
Please login to merge, or discard this patch.
core/services/addon/AddonManager.php 1 patch
Indentation   +79 added lines, -79 removed lines patch added patch discarded remove patch
@@ -23,95 +23,95 @@
 block discarded – undo
23 23
  */
24 24
 class AddonManager
25 25
 {
26
-    private AddonCollection          $addons;
26
+	private AddonCollection          $addons;
27 27
 
28
-    private IncompatibleAddonHandler $incompatible_addon_handler;
28
+	private IncompatibleAddonHandler $incompatible_addon_handler;
29 29
 
30
-    private Psr4Autoloader           $psr4_loader;
30
+	private Psr4Autoloader           $psr4_loader;
31 31
 
32
-    private RegisterV1Addon          $register_v1_addon;
32
+	private RegisterV1Addon          $register_v1_addon;
33 33
 
34
-    private ThirdPartyPluginHandler  $third_party_plugin_handler;
34
+	private ThirdPartyPluginHandler  $third_party_plugin_handler;
35 35
 
36 36
 
37
-    /**
38
-     * AddonManager constructor.
39
-     *
40
-     * @param AddonCollection          $addons
41
-     * @param Psr4Autoloader           $psr4_loader
42
-     * @param RegisterV1Addon          $register_v1_addon
43
-     * @param IncompatibleAddonHandler $incompatible_addon_handler
44
-     * @param ThirdPartyPluginHandler  $third_party_plugin_handler
45
-     */
46
-    public function __construct(
47
-        AddonCollection $addons,
48
-        Psr4Autoloader $psr4_loader,
49
-        RegisterV1Addon $register_v1_addon,
50
-        IncompatibleAddonHandler $incompatible_addon_handler,
51
-        ThirdPartyPluginHandler $third_party_plugin_handler
52
-    ) {
53
-        $this->addons                     = $addons;
54
-        $this->psr4_loader                = $psr4_loader;
55
-        $this->register_v1_addon          = $register_v1_addon;
56
-        $this->incompatible_addon_handler = $incompatible_addon_handler;
57
-        $this->third_party_plugin_handler = $third_party_plugin_handler;
58
-    }
37
+	/**
38
+	 * AddonManager constructor.
39
+	 *
40
+	 * @param AddonCollection          $addons
41
+	 * @param Psr4Autoloader           $psr4_loader
42
+	 * @param RegisterV1Addon          $register_v1_addon
43
+	 * @param IncompatibleAddonHandler $incompatible_addon_handler
44
+	 * @param ThirdPartyPluginHandler  $third_party_plugin_handler
45
+	 */
46
+	public function __construct(
47
+		AddonCollection $addons,
48
+		Psr4Autoloader $psr4_loader,
49
+		RegisterV1Addon $register_v1_addon,
50
+		IncompatibleAddonHandler $incompatible_addon_handler,
51
+		ThirdPartyPluginHandler $third_party_plugin_handler
52
+	) {
53
+		$this->addons                     = $addons;
54
+		$this->psr4_loader                = $psr4_loader;
55
+		$this->register_v1_addon          = $register_v1_addon;
56
+		$this->incompatible_addon_handler = $incompatible_addon_handler;
57
+		$this->third_party_plugin_handler = $third_party_plugin_handler;
58
+	}
59 59
 
60 60
 
61
-    /**
62
-     * @throws Exception
63
-     */
64
-    public function initialize()
65
-    {
66
-        // set autoloaders for all of the classes implementing the legacy EEI_Plugin_API
67
-        // which provide helpers for EE plugin authors to more easily register certain components with EE.
68
-        EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api');
69
-    }
61
+	/**
62
+	 * @throws Exception
63
+	 */
64
+	public function initialize()
65
+	{
66
+		// set autoloaders for all of the classes implementing the legacy EEI_Plugin_API
67
+		// which provide helpers for EE plugin authors to more easily register certain components with EE.
68
+		EEH_Autoloader::register_autoloaders_for_each_file_in_folder(EE_LIBRARIES . 'plugin_api');
69
+	}
70 70
 
71 71
 
72
-    /**
73
-     * @throws Exception
74
-     * @throws Throwable
75
-     */
76
-    public function loadAddons()
77
-    {
78
-        try {
79
-            $this->incompatible_addon_handler->deactivateIncompatibleAddons();
80
-            // legacy add-on API
81
-            do_action('AHEE__EE_System__load_espresso_addons');
82
-            // new add-on API that uses versioning
83
-            do_action(
84
-                'AHEE__EventEspresso_core_services_addon_AddonManager__initialize__addons',
85
-                $this->addons,
86
-                espresso_version()
87
-            );
88
-            // addons are responsible for loading their AddonApiVersion into the AddonCollection
89
-            foreach ($this->addons as $addon) {
90
-                if ($addon instanceof AddonApiVersion) {
91
-                    $this->registerAddon($addon);
92
-                }
93
-            }
94
-            $this->third_party_plugin_handler->loadPlugins();
95
-            do_action('AHEE__EE_System__load_espresso_addons__complete');
96
-        } catch (Exception $exception) {
97
-            new ExceptionStackTraceDisplay($exception);
98
-        }
99
-    }
72
+	/**
73
+	 * @throws Exception
74
+	 * @throws Throwable
75
+	 */
76
+	public function loadAddons()
77
+	{
78
+		try {
79
+			$this->incompatible_addon_handler->deactivateIncompatibleAddons();
80
+			// legacy add-on API
81
+			do_action('AHEE__EE_System__load_espresso_addons');
82
+			// new add-on API that uses versioning
83
+			do_action(
84
+				'AHEE__EventEspresso_core_services_addon_AddonManager__initialize__addons',
85
+				$this->addons,
86
+				espresso_version()
87
+			);
88
+			// addons are responsible for loading their AddonApiVersion into the AddonCollection
89
+			foreach ($this->addons as $addon) {
90
+				if ($addon instanceof AddonApiVersion) {
91
+					$this->registerAddon($addon);
92
+				}
93
+			}
94
+			$this->third_party_plugin_handler->loadPlugins();
95
+			do_action('AHEE__EE_System__load_espresso_addons__complete');
96
+		} catch (Exception $exception) {
97
+			new ExceptionStackTraceDisplay($exception);
98
+		}
99
+	}
100 100
 
101 101
 
102
-    /**
103
-     * @param AddonApiVersion $addon
104
-     * @throws EE_Error
105
-     */
106
-    private function registerAddon(AddonApiVersion $addon)
107
-    {
108
-        // first register addon namespace so that FQCNs resolve correctly
109
-        $this->psr4_loader->addNamespace($addon->getNamespace(), dirname($addon->mainFile()) . '/src/');
110
-        // then allow add-on to perform any other setup that relied on PSR4 autoloading
111
-        $addon->initialize();
112
-        // now register each addon based on it's API version
113
-        if ($addon instanceof AddonApiV1) {
114
-            $this->register_v1_addon->register($addon);
115
-        }
116
-    }
102
+	/**
103
+	 * @param AddonApiVersion $addon
104
+	 * @throws EE_Error
105
+	 */
106
+	private function registerAddon(AddonApiVersion $addon)
107
+	{
108
+		// first register addon namespace so that FQCNs resolve correctly
109
+		$this->psr4_loader->addNamespace($addon->getNamespace(), dirname($addon->mainFile()) . '/src/');
110
+		// then allow add-on to perform any other setup that relied on PSR4 autoloading
111
+		$addon->initialize();
112
+		// now register each addon based on it's API version
113
+		if ($addon instanceof AddonApiV1) {
114
+			$this->register_v1_addon->register($addon);
115
+		}
116
+	}
117 117
 }
Please login to merge, or discard this patch.
core/services/loaders/ClassInterfaceCache.php 2 patches
Indentation   +187 added lines, -187 removed lines patch added patch discarded remove patch
@@ -16,191 +16,191 @@
 block discarded – undo
16 16
  */
17 17
 class ClassInterfaceCache
18 18
 {
19
-    /**
20
-     * array of interfaces indexed by FQCNs where values are arrays of interface FQNs
21
-     *
22
-     * @var string[][] $interfaces
23
-     */
24
-    private $interfaces = [];
25
-
26
-    /**
27
-     * @type string[][] $aliases
28
-     */
29
-    protected $aliases = [];
30
-
31
-
32
-    /**
33
-     * @return string[][]
34
-     */
35
-    public function getAliases(): array
36
-    {
37
-        return $this->aliases;
38
-    }
39
-
40
-
41
-    /**
42
-     * @param string|FullyQualifiedName $fqn
43
-     * @return string
44
-     */
45
-    public function getFqn($fqn): string
46
-    {
47
-        $fqn = $fqn instanceof FullyQualifiedName
48
-            ? $fqn->string()
49
-            : $fqn;
50
-        return ltrim((string) $fqn, '\\');
51
-    }
52
-
53
-
54
-    /**
55
-     * @param string|FullyQualifiedName $fqn
56
-     * @return array
57
-     */
58
-    public function getInterfaces($fqn): array
59
-    {
60
-        $fqn = $this->getFqn($fqn);
61
-        // have we already seen this FQCN ?
62
-        if (! array_key_exists($fqn, $this->interfaces)) {
63
-            $this->interfaces[ $fqn ] = [];
64
-            if (class_exists($fqn)) {
65
-                $this->interfaces[ $fqn ] = class_implements($fqn, false);
66
-                $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false
67
-                    ? $this->interfaces[ $fqn ]
68
-                    : [];
69
-            }
70
-        }
71
-        return $this->interfaces[ $fqn ];
72
-    }
73
-
74
-
75
-    /**
76
-     * @param string|FullyQualifiedName $fqn
77
-     * @param string                    $interface
78
-     * @return bool
79
-     */
80
-    public function hasInterface($fqn, string $interface): bool
81
-    {
82
-        $fqn        = $this->getFqn($fqn);
83
-        $interfaces = $this->getInterfaces($fqn);
84
-        return in_array($interface, $interfaces, true);
85
-    }
86
-
87
-
88
-    /**
89
-     * adds an alias for a classname
90
-     *
91
-     * @param string|FullyQualifiedName $fqn       the class name that should be used
92
-     *                                             (concrete class to replace interface)
93
-     * @param string|FullyQualifiedName $alias     the class name that would be type hinted for
94
-     *                                             (abstract parent or interface)
95
-     * @param string $for_class                    the class that has the dependency
96
-     *                                             (is type hinting for the interface)
97
-     * @throws InvalidAliasException
98
-     */
99
-    public function addAlias($fqn, $alias, string $for_class = '')
100
-    {
101
-        $fqn   = $this->getFqn($fqn);
102
-        $alias = $this->getFqn($alias);
103
-        if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) {
104
-            throw new InvalidAliasException($fqn, $alias);
105
-        }
106
-        // are we adding an alias for a specific class?
107
-        if ($for_class !== '') {
108
-            // make sure it's set up as an array
109
-            if (! isset($this->aliases[ $for_class ])) {
110
-                $this->aliases[ $for_class ] = [];
111
-            }
112
-            $this->aliases[ $for_class ][ $alias ] = $fqn;
113
-            return;
114
-        }
115
-        $this->aliases[ $alias ] = $fqn;
116
-    }
117
-
118
-
119
-    /**
120
-     * returns TRUE if the provided FQN is an alias
121
-     *
122
-     * @param string|FullyQualifiedName $fqn
123
-     * @param string                    $for_class
124
-     * @return bool
125
-     */
126
-    public function isAlias($fqn = '', string $for_class = ''): bool
127
-    {
128
-        $fqn = $this->getFqn($fqn);
129
-        if ($this->isAliasForClass($fqn, $for_class)) {
130
-            return true;
131
-        }
132
-        return $this->isDirectAlias($fqn);
133
-    }
134
-
135
-
136
-    /**
137
-     * returns TRUE if the provided FQN is an alias
138
-     *
139
-     * @param string $alias
140
-     * @return bool
141
-     */
142
-    protected function isDirectAlias(string $alias = ''): bool
143
-    {
144
-        return isset($this->aliases[ $alias ]) && ! is_array($this->aliases[ $alias ]);
145
-    }
146
-
147
-
148
-    /**
149
-     * returns TRUE if the provided FQN is an alias for the specified class
150
-     *
151
-     * @param string $alias
152
-     * @param string $for_class
153
-     * @return bool
154
-     */
155
-    protected function isAliasForClass(string $alias = '', string $for_class = ''): bool
156
-    {
157
-        return (
158
-            $for_class !== ''
159
-            && isset($this->aliases[ $for_class ][ $alias ])
160
-        );
161
-    }
162
-
163
-
164
-    /**
165
-     * returns FQN for provided alias if one exists, otherwise returns the original FQN
166
-     * functions recursively, so that multiple aliases can be used to drill down to a FQN
167
-     *  for example:
168
-     *      if the following two entries were added to the aliases array:
169
-     *          array(
170
-     *              'interface_alias'           => 'some\namespace\interface'
171
-     *              'some\namespace\interface'  => 'some\namespace\classname'
172
-     *          )
173
-     *      then one could use Loader::getNew( 'interface_alias' )
174
-     *      to load an instance of 'some\namespace\classname'
175
-     *
176
-     * @param string|FullyQualifiedName $alias
177
-     * @param string                    $for_class
178
-     * @return string
179
-     */
180
-    public function getFqnForAlias($alias = '', string $for_class = ''): string
181
-    {
182
-        $alias = $this->getFqn($alias);
183
-        if ($this->isAliasForClass($alias, $for_class)) {
184
-            return $this->getFqnForAlias($this->aliases[ $for_class ][ $alias ], $for_class);
185
-        }
186
-        if ($this->isDirectAlias($alias)) {
187
-            // note: changed '' to $for_class
188
-            return $this->getFqnForAlias($this->aliases[ $alias ], $for_class);
189
-        }
190
-        return $alias;
191
-    }
192
-
193
-
194
-    // public function debug($for_class = '')
195
-    // {
196
-    //     if ($for_class !== '') {
197
-    //         if ( ! isset($this->aliases[ $for_class ])) {
198
-    //             \EEH_Debug_Tools::printr('NOT FOUND', "aliases[ $for_class ]", __FILE__, __LINE__);
199
-    //             return;
200
-    //         }
201
-    //         \EEH_Debug_Tools::printr($this->aliases[ $for_class ], "aliases[ $for_class ]", __FILE__, __LINE__);
202
-    //         return;
203
-    //     }
204
-    //     \EEH_Debug_Tools::printr($this->aliases, '$this->aliases', __FILE__, __LINE__);
205
-    // }
19
+	/**
20
+	 * array of interfaces indexed by FQCNs where values are arrays of interface FQNs
21
+	 *
22
+	 * @var string[][] $interfaces
23
+	 */
24
+	private $interfaces = [];
25
+
26
+	/**
27
+	 * @type string[][] $aliases
28
+	 */
29
+	protected $aliases = [];
30
+
31
+
32
+	/**
33
+	 * @return string[][]
34
+	 */
35
+	public function getAliases(): array
36
+	{
37
+		return $this->aliases;
38
+	}
39
+
40
+
41
+	/**
42
+	 * @param string|FullyQualifiedName $fqn
43
+	 * @return string
44
+	 */
45
+	public function getFqn($fqn): string
46
+	{
47
+		$fqn = $fqn instanceof FullyQualifiedName
48
+			? $fqn->string()
49
+			: $fqn;
50
+		return ltrim((string) $fqn, '\\');
51
+	}
52
+
53
+
54
+	/**
55
+	 * @param string|FullyQualifiedName $fqn
56
+	 * @return array
57
+	 */
58
+	public function getInterfaces($fqn): array
59
+	{
60
+		$fqn = $this->getFqn($fqn);
61
+		// have we already seen this FQCN ?
62
+		if (! array_key_exists($fqn, $this->interfaces)) {
63
+			$this->interfaces[ $fqn ] = [];
64
+			if (class_exists($fqn)) {
65
+				$this->interfaces[ $fqn ] = class_implements($fqn, false);
66
+				$this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false
67
+					? $this->interfaces[ $fqn ]
68
+					: [];
69
+			}
70
+		}
71
+		return $this->interfaces[ $fqn ];
72
+	}
73
+
74
+
75
+	/**
76
+	 * @param string|FullyQualifiedName $fqn
77
+	 * @param string                    $interface
78
+	 * @return bool
79
+	 */
80
+	public function hasInterface($fqn, string $interface): bool
81
+	{
82
+		$fqn        = $this->getFqn($fqn);
83
+		$interfaces = $this->getInterfaces($fqn);
84
+		return in_array($interface, $interfaces, true);
85
+	}
86
+
87
+
88
+	/**
89
+	 * adds an alias for a classname
90
+	 *
91
+	 * @param string|FullyQualifiedName $fqn       the class name that should be used
92
+	 *                                             (concrete class to replace interface)
93
+	 * @param string|FullyQualifiedName $alias     the class name that would be type hinted for
94
+	 *                                             (abstract parent or interface)
95
+	 * @param string $for_class                    the class that has the dependency
96
+	 *                                             (is type hinting for the interface)
97
+	 * @throws InvalidAliasException
98
+	 */
99
+	public function addAlias($fqn, $alias, string $for_class = '')
100
+	{
101
+		$fqn   = $this->getFqn($fqn);
102
+		$alias = $this->getFqn($alias);
103
+		if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) {
104
+			throw new InvalidAliasException($fqn, $alias);
105
+		}
106
+		// are we adding an alias for a specific class?
107
+		if ($for_class !== '') {
108
+			// make sure it's set up as an array
109
+			if (! isset($this->aliases[ $for_class ])) {
110
+				$this->aliases[ $for_class ] = [];
111
+			}
112
+			$this->aliases[ $for_class ][ $alias ] = $fqn;
113
+			return;
114
+		}
115
+		$this->aliases[ $alias ] = $fqn;
116
+	}
117
+
118
+
119
+	/**
120
+	 * returns TRUE if the provided FQN is an alias
121
+	 *
122
+	 * @param string|FullyQualifiedName $fqn
123
+	 * @param string                    $for_class
124
+	 * @return bool
125
+	 */
126
+	public function isAlias($fqn = '', string $for_class = ''): bool
127
+	{
128
+		$fqn = $this->getFqn($fqn);
129
+		if ($this->isAliasForClass($fqn, $for_class)) {
130
+			return true;
131
+		}
132
+		return $this->isDirectAlias($fqn);
133
+	}
134
+
135
+
136
+	/**
137
+	 * returns TRUE if the provided FQN is an alias
138
+	 *
139
+	 * @param string $alias
140
+	 * @return bool
141
+	 */
142
+	protected function isDirectAlias(string $alias = ''): bool
143
+	{
144
+		return isset($this->aliases[ $alias ]) && ! is_array($this->aliases[ $alias ]);
145
+	}
146
+
147
+
148
+	/**
149
+	 * returns TRUE if the provided FQN is an alias for the specified class
150
+	 *
151
+	 * @param string $alias
152
+	 * @param string $for_class
153
+	 * @return bool
154
+	 */
155
+	protected function isAliasForClass(string $alias = '', string $for_class = ''): bool
156
+	{
157
+		return (
158
+			$for_class !== ''
159
+			&& isset($this->aliases[ $for_class ][ $alias ])
160
+		);
161
+	}
162
+
163
+
164
+	/**
165
+	 * returns FQN for provided alias if one exists, otherwise returns the original FQN
166
+	 * functions recursively, so that multiple aliases can be used to drill down to a FQN
167
+	 *  for example:
168
+	 *      if the following two entries were added to the aliases array:
169
+	 *          array(
170
+	 *              'interface_alias'           => 'some\namespace\interface'
171
+	 *              'some\namespace\interface'  => 'some\namespace\classname'
172
+	 *          )
173
+	 *      then one could use Loader::getNew( 'interface_alias' )
174
+	 *      to load an instance of 'some\namespace\classname'
175
+	 *
176
+	 * @param string|FullyQualifiedName $alias
177
+	 * @param string                    $for_class
178
+	 * @return string
179
+	 */
180
+	public function getFqnForAlias($alias = '', string $for_class = ''): string
181
+	{
182
+		$alias = $this->getFqn($alias);
183
+		if ($this->isAliasForClass($alias, $for_class)) {
184
+			return $this->getFqnForAlias($this->aliases[ $for_class ][ $alias ], $for_class);
185
+		}
186
+		if ($this->isDirectAlias($alias)) {
187
+			// note: changed '' to $for_class
188
+			return $this->getFqnForAlias($this->aliases[ $alias ], $for_class);
189
+		}
190
+		return $alias;
191
+	}
192
+
193
+
194
+	// public function debug($for_class = '')
195
+	// {
196
+	//     if ($for_class !== '') {
197
+	//         if ( ! isset($this->aliases[ $for_class ])) {
198
+	//             \EEH_Debug_Tools::printr('NOT FOUND', "aliases[ $for_class ]", __FILE__, __LINE__);
199
+	//             return;
200
+	//         }
201
+	//         \EEH_Debug_Tools::printr($this->aliases[ $for_class ], "aliases[ $for_class ]", __FILE__, __LINE__);
202
+	//         return;
203
+	//     }
204
+	//     \EEH_Debug_Tools::printr($this->aliases, '$this->aliases', __FILE__, __LINE__);
205
+	// }
206 206
 }
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -59,16 +59,16 @@  discard block
 block discarded – undo
59 59
     {
60 60
         $fqn = $this->getFqn($fqn);
61 61
         // have we already seen this FQCN ?
62
-        if (! array_key_exists($fqn, $this->interfaces)) {
63
-            $this->interfaces[ $fqn ] = [];
62
+        if ( ! array_key_exists($fqn, $this->interfaces)) {
63
+            $this->interfaces[$fqn] = [];
64 64
             if (class_exists($fqn)) {
65
-                $this->interfaces[ $fqn ] = class_implements($fqn, false);
66
-                $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false
67
-                    ? $this->interfaces[ $fqn ]
65
+                $this->interfaces[$fqn] = class_implements($fqn, false);
66
+                $this->interfaces[$fqn] = $this->interfaces[$fqn] !== false
67
+                    ? $this->interfaces[$fqn]
68 68
                     : [];
69 69
             }
70 70
         }
71
-        return $this->interfaces[ $fqn ];
71
+        return $this->interfaces[$fqn];
72 72
     }
73 73
 
74 74
 
@@ -106,13 +106,13 @@  discard block
 block discarded – undo
106 106
         // are we adding an alias for a specific class?
107 107
         if ($for_class !== '') {
108 108
             // make sure it's set up as an array
109
-            if (! isset($this->aliases[ $for_class ])) {
110
-                $this->aliases[ $for_class ] = [];
109
+            if ( ! isset($this->aliases[$for_class])) {
110
+                $this->aliases[$for_class] = [];
111 111
             }
112
-            $this->aliases[ $for_class ][ $alias ] = $fqn;
112
+            $this->aliases[$for_class][$alias] = $fqn;
113 113
             return;
114 114
         }
115
-        $this->aliases[ $alias ] = $fqn;
115
+        $this->aliases[$alias] = $fqn;
116 116
     }
117 117
 
118 118
 
@@ -141,7 +141,7 @@  discard block
 block discarded – undo
141 141
      */
142 142
     protected function isDirectAlias(string $alias = ''): bool
143 143
     {
144
-        return isset($this->aliases[ $alias ]) && ! is_array($this->aliases[ $alias ]);
144
+        return isset($this->aliases[$alias]) && ! is_array($this->aliases[$alias]);
145 145
     }
146 146
 
147 147
 
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
     {
157 157
         return (
158 158
             $for_class !== ''
159
-            && isset($this->aliases[ $for_class ][ $alias ])
159
+            && isset($this->aliases[$for_class][$alias])
160 160
         );
161 161
     }
162 162
 
@@ -181,11 +181,11 @@  discard block
 block discarded – undo
181 181
     {
182 182
         $alias = $this->getFqn($alias);
183 183
         if ($this->isAliasForClass($alias, $for_class)) {
184
-            return $this->getFqnForAlias($this->aliases[ $for_class ][ $alias ], $for_class);
184
+            return $this->getFqnForAlias($this->aliases[$for_class][$alias], $for_class);
185 185
         }
186 186
         if ($this->isDirectAlias($alias)) {
187 187
             // note: changed '' to $for_class
188
-            return $this->getFqnForAlias($this->aliases[ $alias ], $for_class);
188
+            return $this->getFqnForAlias($this->aliases[$alias], $for_class);
189 189
         }
190 190
         return $alias;
191 191
     }
Please login to merge, or discard this patch.
core/services/converters/date_time_formats/PhpToUnicode.php 1 patch
Indentation   +91 added lines, -91 removed lines patch added patch discarded remove patch
@@ -4,103 +4,103 @@
 block discarded – undo
4 4
 
5 5
 class PhpToUnicode
6 6
 {
7
-    /**
8
-     * array where keys are PHP date format parameters
9
-     * and values are Unicode Date Format substitutions
10
-     */
11
-    public static array $date_formats = [
12
-        // YEAR
13
-        'y'  => 'yy',    // 00, 01, ..., 99
14
-        'Y'  => 'yyyy',  // 2000, 2001, ..., 2099
15
-        'o'  => 'GGGG',  // ISO "week-numbering year" 2000, 2001, ..., 2099
16
-        // MONTH
17
-        'M'  => 'MMM',   // Jan, Feb, ..., Dec
18
-        'n'  => 'M',     // 1, 2, ..., 12
19
-        'm'  => 'MM',    // 01, 02, ..., 12
20
-        'F'  => 'MMMM',  // January, February, ..., December
21
-        // DAY
22
-        'd'  => 'dd',    // 01, 02, ..., 31
23
-        'D'  => 'eee',   // Sun, Mon, ..., Sat
24
-        'jS' => 'do',   // 1st, 2nd, ..., 31st
25
-        'j'  => 'd',     // 1, 2, ..., 31
26
-        'l'  => 'eeee',  // Sunday, Monday, ..., Saturday
27
-        'N'  => 'e',     // Day of week 0, 1, ..., 6
28
-        'w'  => 'i',     // ISO Day of week 1, 2, ..., 7
29
-        'z'  => 'D',   // day of the year 0 - 365 to 1 - 366
30
-        // WEEK
31
-        'W'  => 'w',
32
-        // CHARACTERS
33
-        '|'  => '',
34
-    ];
7
+	/**
8
+	 * array where keys are PHP date format parameters
9
+	 * and values are Unicode Date Format substitutions
10
+	 */
11
+	public static array $date_formats = [
12
+		// YEAR
13
+		'y'  => 'yy',    // 00, 01, ..., 99
14
+		'Y'  => 'yyyy',  // 2000, 2001, ..., 2099
15
+		'o'  => 'GGGG',  // ISO "week-numbering year" 2000, 2001, ..., 2099
16
+		// MONTH
17
+		'M'  => 'MMM',   // Jan, Feb, ..., Dec
18
+		'n'  => 'M',     // 1, 2, ..., 12
19
+		'm'  => 'MM',    // 01, 02, ..., 12
20
+		'F'  => 'MMMM',  // January, February, ..., December
21
+		// DAY
22
+		'd'  => 'dd',    // 01, 02, ..., 31
23
+		'D'  => 'eee',   // Sun, Mon, ..., Sat
24
+		'jS' => 'do',   // 1st, 2nd, ..., 31st
25
+		'j'  => 'd',     // 1, 2, ..., 31
26
+		'l'  => 'eeee',  // Sunday, Monday, ..., Saturday
27
+		'N'  => 'e',     // Day of week 0, 1, ..., 6
28
+		'w'  => 'i',     // ISO Day of week 1, 2, ..., 7
29
+		'z'  => 'D',   // day of the year 0 - 365 to 1 - 366
30
+		// WEEK
31
+		'W'  => 'w',
32
+		// CHARACTERS
33
+		'|'  => '',
34
+	];
35 35
 
36 36
 
37
-    /**
38
-     * array where keys are PHP time format parameters
39
-     * and values are Unicode Time Format substitutions
40
-     */
41
-    public static array $time_formats = [
42
-        // 'a' => 'a', // am, pm, no specific JS alternative
43
-        'A' => 'a', // AM, PM
44
-        // HOUR
45
-        // convert "g" to an intermediary format
46
-        // to avoid its result getting replaced by "h"
47
-        'g' => '@',     // 1, 2, ..., 12
48
-        'h' => 'hh',    // 01, 02, ..., 12
49
-        '@' => 'h',     // 1, 2, ..., 12
50
-        'G' => '#',     // 0, 1, ... 23
51
-        'H' => 'HH',    // 00, 01, ... 23
52
-        '#' => 'H',     // 0, 1, ... 23
53
-        // MINUTES & SECONDS
54
-        'i' => 'mm',    // minutes 00, 01, ..., 59
55
-        's' => 'ss',    // seconds 00, 01, ..., 59
56
-        'v' => 'SSS',   // milliseconds 000, 001, ..., 999
57
-        'u' => 'SSS',   // microseconds (not in unicode) 000, 001, ..., 999
58
-    ];
37
+	/**
38
+	 * array where keys are PHP time format parameters
39
+	 * and values are Unicode Time Format substitutions
40
+	 */
41
+	public static array $time_formats = [
42
+		// 'a' => 'a', // am, pm, no specific JS alternative
43
+		'A' => 'a', // AM, PM
44
+		// HOUR
45
+		// convert "g" to an intermediary format
46
+		// to avoid its result getting replaced by "h"
47
+		'g' => '@',     // 1, 2, ..., 12
48
+		'h' => 'hh',    // 01, 02, ..., 12
49
+		'@' => 'h',     // 1, 2, ..., 12
50
+		'G' => '#',     // 0, 1, ... 23
51
+		'H' => 'HH',    // 00, 01, ... 23
52
+		'#' => 'H',     // 0, 1, ... 23
53
+		// MINUTES & SECONDS
54
+		'i' => 'mm',    // minutes 00, 01, ..., 59
55
+		's' => 'ss',    // seconds 00, 01, ..., 59
56
+		'v' => 'SSS',   // milliseconds 000, 001, ..., 999
57
+		'u' => 'SSS',   // microseconds (not in unicode) 000, 001, ..., 999
58
+	];
59 59
 
60 60
 
61
-    /**
62
-     * array where keys are PHP timezone format parameters
63
-     * and values are Unicode Timezone Format substitutions
64
-     */
65
-    public static array $timezone_formats = [
66
-        'Z' => 'xx',    // -0100, +0000, ..., +1200
67
-        'e' => 'xxx',     // Timezone identifier UTC, GMT, Atlantic/Azores to -01:00, +00:00, ... +12:00
68
-        'T' => 'xxx',     // Timezone abbreviation EST, MDT to -01:00, +00:00, ... +12:00
69
-        'P' => 'xxx',     // -01:00, +00:00, ... +12:00
70
-        'O' => 'xx',    // -0100, +0000, ..., +1200
71
-    ];
61
+	/**
62
+	 * array where keys are PHP timezone format parameters
63
+	 * and values are Unicode Timezone Format substitutions
64
+	 */
65
+	public static array $timezone_formats = [
66
+		'Z' => 'xx',    // -0100, +0000, ..., +1200
67
+		'e' => 'xxx',     // Timezone identifier UTC, GMT, Atlantic/Azores to -01:00, +00:00, ... +12:00
68
+		'T' => 'xxx',     // Timezone abbreviation EST, MDT to -01:00, +00:00, ... +12:00
69
+		'P' => 'xxx',     // -01:00, +00:00, ... +12:00
70
+		'O' => 'xx',    // -0100, +0000, ..., +1200
71
+	];
72 72
 
73 73
 
74
-    /**
75
-     * @param string $date_format
76
-     * @return string
77
-     */
78
-    public function convertDateFormat(string $date_format): string
79
-    {
80
-        // replace backslashed characters and `: ` with empty string
81
-        $date_format = preg_replace('/(\\\\\w)*(:\s)*/', '', $date_format);
82
-        foreach (PhpToUnicode::$date_formats as $find => $replace) {
83
-            $date_format = (string) str_replace($find, $replace, $date_format);
84
-        }
85
-        return trim($date_format);
86
-    }
74
+	/**
75
+	 * @param string $date_format
76
+	 * @return string
77
+	 */
78
+	public function convertDateFormat(string $date_format): string
79
+	{
80
+		// replace backslashed characters and `: ` with empty string
81
+		$date_format = preg_replace('/(\\\\\w)*(:\s)*/', '', $date_format);
82
+		foreach (PhpToUnicode::$date_formats as $find => $replace) {
83
+			$date_format = (string) str_replace($find, $replace, $date_format);
84
+		}
85
+		return trim($date_format);
86
+	}
87 87
 
88 88
 
89
-    /**
90
-     * @param string $time_format
91
-     * @return string
92
-     */
93
-    public function convertTimeFormat(string $time_format): string
94
-    {
95
-        // replace backslashed characters and `: ` with empty string
96
-        $time_format = preg_replace('/(\\\\\w)*(:\s)*/', '', $time_format);
97
-        foreach (PhpToUnicode::$time_formats as $find => $replace) {
98
-            $time_format = (string) str_replace($find, $replace, $time_format);
99
-        }
100
-        // and just in case the timezone has been added
101
-        foreach (PhpToUnicode::$timezone_formats as $find => $replace) {
102
-            $time_format = (string) str_replace($find, $replace, $time_format);
103
-        }
104
-        return trim($time_format);
105
-    }
89
+	/**
90
+	 * @param string $time_format
91
+	 * @return string
92
+	 */
93
+	public function convertTimeFormat(string $time_format): string
94
+	{
95
+		// replace backslashed characters and `: ` with empty string
96
+		$time_format = preg_replace('/(\\\\\w)*(:\s)*/', '', $time_format);
97
+		foreach (PhpToUnicode::$time_formats as $find => $replace) {
98
+			$time_format = (string) str_replace($find, $replace, $time_format);
99
+		}
100
+		// and just in case the timezone has been added
101
+		foreach (PhpToUnicode::$timezone_formats as $find => $replace) {
102
+			$time_format = (string) str_replace($find, $replace, $time_format);
103
+		}
104
+		return trim($time_format);
105
+	}
106 106
 }
Please login to merge, or discard this patch.
core/services/helpers/datetime/PhpCompatGreaterFiveSixHelper.php 2 patches
Indentation   +87 added lines, -87 removed lines patch added patch discarded remove patch
@@ -10,99 +10,99 @@
 block discarded – undo
10 10
 
11 11
 class PhpCompatGreaterFiveSixHelper extends AbstractHelper
12 12
 {
13
-    /**
14
-     * PhpCompatGreaterFiveSixHelper constructor.
15
-     *
16
-     * @throws DomainException
17
-     */
18
-    public function __construct()
19
-    {
20
-        if (PHP_VERSION_ID < 50600) {
21
-            throw new DomainException(
22
-                sprintf(
23
-                    esc_html__(
24
-                        'The %1$s is only usable on php versions greater than 5.6.  You\'ll want to use %2$s instead.',
25
-                        'event_espresso'
26
-                    ),
27
-                    __CLASS__,
28
-                    'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper'
29
-                )
30
-            );
31
-        }
32
-    }
13
+	/**
14
+	 * PhpCompatGreaterFiveSixHelper constructor.
15
+	 *
16
+	 * @throws DomainException
17
+	 */
18
+	public function __construct()
19
+	{
20
+		if (PHP_VERSION_ID < 50600) {
21
+			throw new DomainException(
22
+				sprintf(
23
+					esc_html__(
24
+						'The %1$s is only usable on php versions greater than 5.6.  You\'ll want to use %2$s instead.',
25
+						'event_espresso'
26
+					),
27
+					__CLASS__,
28
+					'EventEspresso\core\services\helpers\datetime\PhpCompatLessFiveSixHelper'
29
+				)
30
+			);
31
+		}
32
+	}
33 33
 
34 34
 
35
-    /**
36
-     * Returns a timezone string for the provided gmt_offset.
37
-     * This is a valid timezone string that can be sent into DateTimeZone
38
-     *
39
-     * @param float|string $gmt_offset
40
-     * @return string
41
-     */
42
-    public function getTimezoneStringFromGmtOffset($gmt_offset = ''): string
43
-    {
44
-        $gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset);
45
-        return is_float($gmt_offset_or_timezone_string)
46
-            ? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string)
47
-            : $gmt_offset_or_timezone_string;
48
-    }
35
+	/**
36
+	 * Returns a timezone string for the provided gmt_offset.
37
+	 * This is a valid timezone string that can be sent into DateTimeZone
38
+	 *
39
+	 * @param float|string $gmt_offset
40
+	 * @return string
41
+	 */
42
+	public function getTimezoneStringFromGmtOffset($gmt_offset = ''): string
43
+	{
44
+		$gmt_offset_or_timezone_string = $this->sanitizeInitialIncomingGmtOffsetForGettingTimezoneString($gmt_offset);
45
+		return is_float($gmt_offset_or_timezone_string)
46
+			? $this->convertWpGmtOffsetForDateTimeZone($gmt_offset_or_timezone_string)
47
+			: $gmt_offset_or_timezone_string;
48
+	}
49 49
 
50 50
 
51
-    /**
52
-     * Returns a formatted offset for use as an argument for constructing DateTimeZone
53
-     *
54
-     * @param float $gmt_offset This should be a float representing the gmt_offset.
55
-     * @return string
56
-     */
57
-    protected function convertWpGmtOffsetForDateTimeZone(float $gmt_offset): string
58
-    {
59
-        $is_negative = $gmt_offset < 0;
60
-        $gmt_offset  *= 100;
61
-        $gmt_offset  = absint($gmt_offset);
62
-        // negative and need zero padding?
63
-        if (strlen($gmt_offset) < 4) {
64
-            $gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT);
65
-        }
66
-        $gmt_offset = $this->convertToTimeFraction($gmt_offset);
67
-        // return something like -1300, -0200 or +1300, +0200
68
-        return $is_negative
69
-            ? '-' . $gmt_offset
70
-            : '+' . $gmt_offset;
71
-    }
51
+	/**
52
+	 * Returns a formatted offset for use as an argument for constructing DateTimeZone
53
+	 *
54
+	 * @param float $gmt_offset This should be a float representing the gmt_offset.
55
+	 * @return string
56
+	 */
57
+	protected function convertWpGmtOffsetForDateTimeZone(float $gmt_offset): string
58
+	{
59
+		$is_negative = $gmt_offset < 0;
60
+		$gmt_offset  *= 100;
61
+		$gmt_offset  = absint($gmt_offset);
62
+		// negative and need zero padding?
63
+		if (strlen($gmt_offset) < 4) {
64
+			$gmt_offset = str_pad($gmt_offset, 4, '0', STR_PAD_LEFT);
65
+		}
66
+		$gmt_offset = $this->convertToTimeFraction($gmt_offset);
67
+		// return something like -1300, -0200 or +1300, +0200
68
+		return $is_negative
69
+			? '-' . $gmt_offset
70
+			: '+' . $gmt_offset;
71
+	}
72 72
 
73 73
 
74
-    /**
75
-     * Converts something like `1550` to `1530` or `0275` to `0245`
76
-     * Incoming offset should be a positive value, this will mutate negative values. Be aware!
77
-     *
78
-     * @param int $offset
79
-     * @return string
80
-     */
81
-    protected function convertToTimeFraction(int $offset): string
82
-    {
83
-        $first_part  = substr($offset, 0, 2);
84
-        $second_part = substr($offset, 2, 2);
85
-        $second_part = str_replace(['25', '50', '75'], ['15', '30', '45'], $second_part);
86
-        return $first_part . $second_part;
87
-    }
74
+	/**
75
+	 * Converts something like `1550` to `1530` or `0275` to `0245`
76
+	 * Incoming offset should be a positive value, this will mutate negative values. Be aware!
77
+	 *
78
+	 * @param int $offset
79
+	 * @return string
80
+	 */
81
+	protected function convertToTimeFraction(int $offset): string
82
+	{
83
+		$first_part  = substr($offset, 0, 2);
84
+		$second_part = substr($offset, 2, 2);
85
+		$second_part = str_replace(['25', '50', '75'], ['15', '30', '45'], $second_part);
86
+		return $first_part . $second_part;
87
+	}
88 88
 
89 89
 
90
-    /**
91
-     * Get Timezone offset for given timezone object
92
-     *
93
-     * @param DateTimeZone $date_time_zone
94
-     * @param null|int     $time
95
-     * @return int
96
-     * @throws Exception
97
-     */
98
-    public function getTimezoneOffset(DateTimezone $date_time_zone, $time = null): int
99
-    {
100
-        $time = is_int($time) || $time === null
101
-            ? $time
102
-            : (int) strtotime($time);
103
-        $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, (int) $time)
104
-            ? $time
105
-            : time();
106
-        return $date_time_zone->getOffset(new DateTime('@' . $time));
107
-    }
90
+	/**
91
+	 * Get Timezone offset for given timezone object
92
+	 *
93
+	 * @param DateTimeZone $date_time_zone
94
+	 * @param null|int     $time
95
+	 * @return int
96
+	 * @throws Exception
97
+	 */
98
+	public function getTimezoneOffset(DateTimezone $date_time_zone, $time = null): int
99
+	{
100
+		$time = is_int($time) || $time === null
101
+			? $time
102
+			: (int) strtotime($time);
103
+		$time = preg_match(EE_Datetime_Field::unix_timestamp_regex, (int) $time)
104
+			? $time
105
+			: time();
106
+		return $date_time_zone->getOffset(new DateTime('@' . $time));
107
+	}
108 108
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
     protected function convertWpGmtOffsetForDateTimeZone(float $gmt_offset): string
58 58
     {
59 59
         $is_negative = $gmt_offset < 0;
60
-        $gmt_offset  *= 100;
60
+        $gmt_offset *= 100;
61 61
         $gmt_offset  = absint($gmt_offset);
62 62
         // negative and need zero padding?
63 63
         if (strlen($gmt_offset) < 4) {
@@ -66,8 +66,8 @@  discard block
 block discarded – undo
66 66
         $gmt_offset = $this->convertToTimeFraction($gmt_offset);
67 67
         // return something like -1300, -0200 or +1300, +0200
68 68
         return $is_negative
69
-            ? '-' . $gmt_offset
70
-            : '+' . $gmt_offset;
69
+            ? '-'.$gmt_offset
70
+            : '+'.$gmt_offset;
71 71
     }
72 72
 
73 73
 
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
         $first_part  = substr($offset, 0, 2);
84 84
         $second_part = substr($offset, 2, 2);
85 85
         $second_part = str_replace(['25', '50', '75'], ['15', '30', '45'], $second_part);
86
-        return $first_part . $second_part;
86
+        return $first_part.$second_part;
87 87
     }
88 88
 
89 89
 
@@ -103,6 +103,6 @@  discard block
 block discarded – undo
103 103
         $time = preg_match(EE_Datetime_Field::unix_timestamp_regex, (int) $time)
104 104
             ? $time
105 105
             : time();
106
-        return $date_time_zone->getOffset(new DateTime('@' . $time));
106
+        return $date_time_zone->getOffset(new DateTime('@'.$time));
107 107
     }
108 108
 }
Please login to merge, or discard this patch.
core/services/helpers/WordPressHooks.php 2 patches
Indentation   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -4,35 +4,35 @@
 block discarded – undo
4 4
 
5 5
 class WordPressHooks
6 6
 {
7
-    /**
8
-     * returns an array of names for all classes that have methods registered as callbacks for the given action or
9
-     * filter hook
10
-     *
11
-     * @param string $hook
12
-     * @return array
13
-     */
14
-    public static function getClassNamesForAllCallbacksOnHook(string $hook): array
15
-    {
16
-        global $wp_filter;
17
-        $class_names = [];
18
-        // are any callbacks registered for this hook ?
19
-        if (isset($wp_filter[ $hook ])) {
20
-            // loop thru all of the callbacks attached to the deprecated hookpoint
21
-            foreach ($wp_filter[ $hook ] as $priority) {
22
-                foreach ($priority as $callback) {
23
-                    // is the callback a non-static class method ?
24
-                    if (isset($callback['function']) && is_array($callback['function'])) {
25
-                        if (isset($callback['function'][0]) && is_object($callback['function'][0])) {
26
-                            $class_names[] = get_class($callback['function'][0]);
27
-                        }
28
-                        // test for static method
29
-                    } elseif (strpos($callback['function'], '::') !== false) {
30
-                        $class         = explode('::', $callback['function']);
31
-                        $class_names[] = $class[0];
32
-                    }
33
-                }
34
-            }
35
-        }
36
-        return $class_names;
37
-    }
7
+	/**
8
+	 * returns an array of names for all classes that have methods registered as callbacks for the given action or
9
+	 * filter hook
10
+	 *
11
+	 * @param string $hook
12
+	 * @return array
13
+	 */
14
+	public static function getClassNamesForAllCallbacksOnHook(string $hook): array
15
+	{
16
+		global $wp_filter;
17
+		$class_names = [];
18
+		// are any callbacks registered for this hook ?
19
+		if (isset($wp_filter[ $hook ])) {
20
+			// loop thru all of the callbacks attached to the deprecated hookpoint
21
+			foreach ($wp_filter[ $hook ] as $priority) {
22
+				foreach ($priority as $callback) {
23
+					// is the callback a non-static class method ?
24
+					if (isset($callback['function']) && is_array($callback['function'])) {
25
+						if (isset($callback['function'][0]) && is_object($callback['function'][0])) {
26
+							$class_names[] = get_class($callback['function'][0]);
27
+						}
28
+						// test for static method
29
+					} elseif (strpos($callback['function'], '::') !== false) {
30
+						$class         = explode('::', $callback['function']);
31
+						$class_names[] = $class[0];
32
+					}
33
+				}
34
+			}
35
+		}
36
+		return $class_names;
37
+	}
38 38
 }
39 39
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -16,9 +16,9 @@
 block discarded – undo
16 16
         global $wp_filter;
17 17
         $class_names = [];
18 18
         // are any callbacks registered for this hook ?
19
-        if (isset($wp_filter[ $hook ])) {
19
+        if (isset($wp_filter[$hook])) {
20 20
             // loop thru all of the callbacks attached to the deprecated hookpoint
21
-            foreach ($wp_filter[ $hook ] as $priority) {
21
+            foreach ($wp_filter[$hook] as $priority) {
22 22
                 foreach ($priority as $callback) {
23 23
                     // is the callback a non-static class method ?
24 24
                     if (isset($callback['function']) && is_array($callback['function'])) {
Please login to merge, or discard this patch.
core/services/collections/iterators/CollectionFilterCallbackIterator.php 1 patch
Indentation   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -8,53 +8,53 @@
 block discarded – undo
8 8
 
9 9
 class CollectionFilterCallbackIterator extends FilterIterator
10 10
 {
11
-    /**
12
-     * Used for determining whether the iterated object in the Collection is "valid" or not.
13
-     *
14
-     * @var Closure
15
-     */
16
-    private Closure $acceptance_callback;
17
-
18
-
19
-    /**
20
-     * CollectionFilterCallbackIterator constructor.
21
-     *
22
-     * @param Collection $collection
23
-     * @param Closure    $acceptance_callback  The closure will receive an instance of whatever object is stored on the
24
-     *                                         collection when iterating over the collection and should return boolean.
25
-     */
26
-    public function __construct(Collection $collection, Closure $acceptance_callback)
27
-    {
28
-        $this->acceptance_callback = $acceptance_callback;
29
-        parent::__construct($collection);
30
-    }
31
-
32
-
33
-    /**
34
-     * Check whether the current element of the iterator is acceptable
35
-     *
36
-     * @link  http://php.net/manual/en/filteriterator.accept.php
37
-     * @return bool true if the current element is acceptable, otherwise false.
38
-     */
39
-    public function accept(): bool
40
-    {
41
-        $acceptance_callback = $this->acceptance_callback;
42
-        return $acceptance_callback($this->getInnerIterator()->current());
43
-    }
44
-
45
-
46
-    /**
47
-     * Returns a filtered array of objects from the collection using the provided acceptance callback
48
-     *
49
-     * @return array
50
-     */
51
-    public function getFiltered(): array
52
-    {
53
-        $filtered_array = [];
54
-        $this->rewind();
55
-        foreach ($this as $filtered_object) {
56
-            $filtered_array[] = $filtered_object;
57
-        }
58
-        return $filtered_array;
59
-    }
11
+	/**
12
+	 * Used for determining whether the iterated object in the Collection is "valid" or not.
13
+	 *
14
+	 * @var Closure
15
+	 */
16
+	private Closure $acceptance_callback;
17
+
18
+
19
+	/**
20
+	 * CollectionFilterCallbackIterator constructor.
21
+	 *
22
+	 * @param Collection $collection
23
+	 * @param Closure    $acceptance_callback  The closure will receive an instance of whatever object is stored on the
24
+	 *                                         collection when iterating over the collection and should return boolean.
25
+	 */
26
+	public function __construct(Collection $collection, Closure $acceptance_callback)
27
+	{
28
+		$this->acceptance_callback = $acceptance_callback;
29
+		parent::__construct($collection);
30
+	}
31
+
32
+
33
+	/**
34
+	 * Check whether the current element of the iterator is acceptable
35
+	 *
36
+	 * @link  http://php.net/manual/en/filteriterator.accept.php
37
+	 * @return bool true if the current element is acceptable, otherwise false.
38
+	 */
39
+	public function accept(): bool
40
+	{
41
+		$acceptance_callback = $this->acceptance_callback;
42
+		return $acceptance_callback($this->getInnerIterator()->current());
43
+	}
44
+
45
+
46
+	/**
47
+	 * Returns a filtered array of objects from the collection using the provided acceptance callback
48
+	 *
49
+	 * @return array
50
+	 */
51
+	public function getFiltered(): array
52
+	{
53
+		$filtered_array = [];
54
+		$this->rewind();
55
+		foreach ($this as $filtered_object) {
56
+			$filtered_array[] = $filtered_object;
57
+		}
58
+		return $filtered_array;
59
+	}
60 60
 }
Please login to merge, or discard this patch.
core/services/encryption/openssl/CipherMethod.php 1 patch
Indentation   +190 added lines, -190 removed lines patch added patch discarded remove patch
@@ -14,194 +14,194 @@
 block discarded – undo
14 14
  */
15 15
 class CipherMethod
16 16
 {
17
-    protected string $cipher_method_option_name = '';
18
-
19
-    /**
20
-     * list of cipher methods that we consider usable,
21
-     * essentially all of the installed_cipher_methods minus weak_algorithms
22
-     */
23
-    protected array  $cipher_methods        = [];
24
-
25
-    protected string $default_cipher_method = '';
26
-
27
-    /**
28
-     * list of ALL cipher methods available on the server
29
-     */
30
-    protected array $installed_cipher_methods = [];
31
-
32
-    /**
33
-     * the OpenSSL cipher method to use. default: AES-128-CBC
34
-     */
35
-    protected ?string $validated_cipher_method = null;
36
-
37
-    /**
38
-     * as early as Aug 2016, Openssl declared the following weak: RC2, RC4, DES, 3DES, MD5 based
39
-     * and ECB mode should be avoided
40
-     */
41
-    protected array $weak_algorithms = ['des', 'ecb', 'md5', 'rc2', 'rc4'];
42
-
43
-
44
-    /**
45
-     * @param string $default_cipher_method
46
-     * @param string $cipher_method_option_name
47
-     */
48
-    public function __construct(string $default_cipher_method, string $cipher_method_option_name)
49
-    {
50
-        $this->default_cipher_method     = $default_cipher_method;
51
-        $this->cipher_method_option_name = $cipher_method_option_name;
52
-        $this->installed_cipher_methods  = openssl_get_cipher_methods();
53
-    }
54
-
55
-
56
-    /**
57
-     * Returns a cipher method that has been verified to work.
58
-     * First checks if the cached cipher has been set already and if so, returns that.
59
-     * Then tests the incoming default and returns that if it's good.
60
-     * If not, then it retrieves the previously tested and saved cipher method.
61
-     * But if that doesn't exist, then calls getAvailableCipherMethod()
62
-     * to see what is available on the server, and returns the results.
63
-     *
64
-     * @param string $cipher_method
65
-     * @param bool   $load_alternate [optional] if TRUE, will load the default cipher method (or any strong algorithm)
66
-     *                               if the requested cipher method is not installed or invalid.
67
-     *                               if FALSE, will throw an exception if the requested cipher method is not valid.
68
-     * @return string
69
-     * @throws RuntimeException
70
-     */
71
-    public function getCipherMethod(string $cipher_method = '', bool $load_alternate = true): ?string
72
-    {
73
-        if (empty($cipher_method) && $this->validated_cipher_method !== null) {
74
-            return $this->validated_cipher_method;
75
-        }
76
-        // if nothing specific was requested and it's ok to load an alternate, then grab the system default
77
-        if (empty($cipher_method) && $load_alternate) {
78
-            $cipher_method = $this->default_cipher_method;
79
-        }
80
-        // verify that the cipher method can produce an initialization vector.
81
-        // but if the requested is invalid and we don't want to load an alternate, then throw an exception
82
-        $throw_exception = ! $load_alternate;
83
-        if ($this->validateCipherMethod($cipher_method, $throw_exception) === false) {
84
-            // nope? ... ok let's see what we can find
85
-            $cipher_method = $this->getAvailableCipherMethod();
86
-        }
87
-        // if nothing has been previously validated, then save the currently requested cipher which appears to be good
88
-        if ($this->validated_cipher_method === null) {
89
-            $this->validated_cipher_method = $cipher_method;
90
-        }
91
-        return $cipher_method;
92
-    }
93
-
94
-
95
-    /**
96
-     * returns true if the selected cipher method either uses Galois/Counter Mode (GCM)
97
-     * or Counter with CBC-MAC (CCM) authenticated encryption modes
98
-     * (also need to be using PHP 7.1 or greater to actually use authenticated encryption modes)
99
-     *
100
-     * @return bool
101
-     */
102
-    public function usesAuthenticatedEncryptionMode(): bool
103
-    {
104
-        return PHP_VERSION_ID >= 70100
105
-               && (
106
-                   stripos($this->validated_cipher_method, 'gcm') !== false
107
-                   || stripos($this->validated_cipher_method, 'ccm') !== false
108
-               );
109
-    }
110
-
111
-
112
-    /**
113
-     * @param string $cipher_method
114
-     * @return string
115
-     * @throws RuntimeException
116
-     */
117
-    protected function getAvailableCipherMethod(string $cipher_method = ''): ?string
118
-    {
119
-        // if nothing was supplied, the get what we found in the past to work
120
-        $cipher_method_to_test = $cipher_method
121
-            ?: get_option($this->cipher_method_option_name, '');
122
-        // verify that the incoming cipher method exists and can produce an initialization vector
123
-        if ($this->validateCipherMethod($cipher_method_to_test) === false) {
124
-            // what? there's no list?
125
-            if (empty($this->cipher_methods)) {
126
-                // generate that list and cache it
127
-                $this->cipher_methods = $this->getAvailableStrongCipherMethods();
128
-            }
129
-            // then grab the first item from the list (we'll add it back later if it is good)
130
-            $cipher_method_to_test = array_shift($this->cipher_methods);
131
-            if ($cipher_method_to_test === null) {
132
-                throw new RuntimeException(
133
-                    esc_html__(
134
-                        'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.',
135
-                        'event_espresso'
136
-                    )
137
-                );
138
-            }
139
-            // verify that the next cipher method works
140
-            return $this->getAvailableCipherMethod($cipher_method_to_test);
141
-        }
142
-        // if we've gotten this far, then we found an available cipher method that works
143
-        // so save that for next time, if it's not the same as what's there already
144
-        if ($cipher_method_to_test !== $cipher_method) {
145
-            update_option($this->cipher_method_option_name, $cipher_method_to_test);
146
-        }
147
-        // if we previously removed this cipher method from the list of valid ones, then let's put it back
148
-        if (! in_array($cipher_method_to_test, $this->cipher_methods, true)) {
149
-            array_unshift($this->cipher_methods, $cipher_method_to_test);
150
-        }
151
-        return $cipher_method_to_test;
152
-    }
153
-
154
-
155
-    /**
156
-     * @return array
157
-     */
158
-    protected function getAvailableStrongCipherMethods(): array
159
-    {
160
-        return array_filter($this->installed_cipher_methods, [$this, 'weakAlgorithmFilter']);
161
-    }
162
-
163
-
164
-    /**
165
-     * @param string $cipher_method
166
-     * @param bool   $throw_exception
167
-     * @return bool
168
-     */
169
-    protected function validateCipherMethod(string $cipher_method, bool $throw_exception = false): bool
170
-    {
171
-        // verify that the requested cipher method is actually installed and can produce an initialization vector
172
-        if (
173
-            in_array($cipher_method, $this->installed_cipher_methods, true)
174
-            && openssl_cipher_iv_length($cipher_method) !== false
175
-        ) {
176
-            return true;
177
-        }
178
-        if (! $throw_exception) {
179
-            return false;
180
-        }
181
-        throw new RuntimeException(
182
-            sprintf(
183
-                esc_html__(
184
-                    'The requested OpenSSL cipher method "%1$s" is invalid or not installed on the server. Please contact the server administrator.',
185
-                    'event_espresso'
186
-                ),
187
-                $cipher_method
188
-            )
189
-        );
190
-    }
191
-
192
-
193
-    /**
194
-     * @see https://www.php.net/manual/en/function.openssl-get-cipher-methods.php#example-890
195
-     * @param string $cipher_method
196
-     * @return bool
197
-     */
198
-    protected function weakAlgorithmFilter(string $cipher_method): bool
199
-    {
200
-        foreach ($this->weak_algorithms as $weak_algorithm) {
201
-            if (stripos($cipher_method, $weak_algorithm) !== false) {
202
-                return false;
203
-            }
204
-        }
205
-        return true;
206
-    }
17
+	protected string $cipher_method_option_name = '';
18
+
19
+	/**
20
+	 * list of cipher methods that we consider usable,
21
+	 * essentially all of the installed_cipher_methods minus weak_algorithms
22
+	 */
23
+	protected array  $cipher_methods        = [];
24
+
25
+	protected string $default_cipher_method = '';
26
+
27
+	/**
28
+	 * list of ALL cipher methods available on the server
29
+	 */
30
+	protected array $installed_cipher_methods = [];
31
+
32
+	/**
33
+	 * the OpenSSL cipher method to use. default: AES-128-CBC
34
+	 */
35
+	protected ?string $validated_cipher_method = null;
36
+
37
+	/**
38
+	 * as early as Aug 2016, Openssl declared the following weak: RC2, RC4, DES, 3DES, MD5 based
39
+	 * and ECB mode should be avoided
40
+	 */
41
+	protected array $weak_algorithms = ['des', 'ecb', 'md5', 'rc2', 'rc4'];
42
+
43
+
44
+	/**
45
+	 * @param string $default_cipher_method
46
+	 * @param string $cipher_method_option_name
47
+	 */
48
+	public function __construct(string $default_cipher_method, string $cipher_method_option_name)
49
+	{
50
+		$this->default_cipher_method     = $default_cipher_method;
51
+		$this->cipher_method_option_name = $cipher_method_option_name;
52
+		$this->installed_cipher_methods  = openssl_get_cipher_methods();
53
+	}
54
+
55
+
56
+	/**
57
+	 * Returns a cipher method that has been verified to work.
58
+	 * First checks if the cached cipher has been set already and if so, returns that.
59
+	 * Then tests the incoming default and returns that if it's good.
60
+	 * If not, then it retrieves the previously tested and saved cipher method.
61
+	 * But if that doesn't exist, then calls getAvailableCipherMethod()
62
+	 * to see what is available on the server, and returns the results.
63
+	 *
64
+	 * @param string $cipher_method
65
+	 * @param bool   $load_alternate [optional] if TRUE, will load the default cipher method (or any strong algorithm)
66
+	 *                               if the requested cipher method is not installed or invalid.
67
+	 *                               if FALSE, will throw an exception if the requested cipher method is not valid.
68
+	 * @return string
69
+	 * @throws RuntimeException
70
+	 */
71
+	public function getCipherMethod(string $cipher_method = '', bool $load_alternate = true): ?string
72
+	{
73
+		if (empty($cipher_method) && $this->validated_cipher_method !== null) {
74
+			return $this->validated_cipher_method;
75
+		}
76
+		// if nothing specific was requested and it's ok to load an alternate, then grab the system default
77
+		if (empty($cipher_method) && $load_alternate) {
78
+			$cipher_method = $this->default_cipher_method;
79
+		}
80
+		// verify that the cipher method can produce an initialization vector.
81
+		// but if the requested is invalid and we don't want to load an alternate, then throw an exception
82
+		$throw_exception = ! $load_alternate;
83
+		if ($this->validateCipherMethod($cipher_method, $throw_exception) === false) {
84
+			// nope? ... ok let's see what we can find
85
+			$cipher_method = $this->getAvailableCipherMethod();
86
+		}
87
+		// if nothing has been previously validated, then save the currently requested cipher which appears to be good
88
+		if ($this->validated_cipher_method === null) {
89
+			$this->validated_cipher_method = $cipher_method;
90
+		}
91
+		return $cipher_method;
92
+	}
93
+
94
+
95
+	/**
96
+	 * returns true if the selected cipher method either uses Galois/Counter Mode (GCM)
97
+	 * or Counter with CBC-MAC (CCM) authenticated encryption modes
98
+	 * (also need to be using PHP 7.1 or greater to actually use authenticated encryption modes)
99
+	 *
100
+	 * @return bool
101
+	 */
102
+	public function usesAuthenticatedEncryptionMode(): bool
103
+	{
104
+		return PHP_VERSION_ID >= 70100
105
+			   && (
106
+				   stripos($this->validated_cipher_method, 'gcm') !== false
107
+				   || stripos($this->validated_cipher_method, 'ccm') !== false
108
+			   );
109
+	}
110
+
111
+
112
+	/**
113
+	 * @param string $cipher_method
114
+	 * @return string
115
+	 * @throws RuntimeException
116
+	 */
117
+	protected function getAvailableCipherMethod(string $cipher_method = ''): ?string
118
+	{
119
+		// if nothing was supplied, the get what we found in the past to work
120
+		$cipher_method_to_test = $cipher_method
121
+			?: get_option($this->cipher_method_option_name, '');
122
+		// verify that the incoming cipher method exists and can produce an initialization vector
123
+		if ($this->validateCipherMethod($cipher_method_to_test) === false) {
124
+			// what? there's no list?
125
+			if (empty($this->cipher_methods)) {
126
+				// generate that list and cache it
127
+				$this->cipher_methods = $this->getAvailableStrongCipherMethods();
128
+			}
129
+			// then grab the first item from the list (we'll add it back later if it is good)
130
+			$cipher_method_to_test = array_shift($this->cipher_methods);
131
+			if ($cipher_method_to_test === null) {
132
+				throw new RuntimeException(
133
+					esc_html__(
134
+						'OpenSSL support appears to be enabled on the server, but no cipher methods are available. Please contact the server administrator.',
135
+						'event_espresso'
136
+					)
137
+				);
138
+			}
139
+			// verify that the next cipher method works
140
+			return $this->getAvailableCipherMethod($cipher_method_to_test);
141
+		}
142
+		// if we've gotten this far, then we found an available cipher method that works
143
+		// so save that for next time, if it's not the same as what's there already
144
+		if ($cipher_method_to_test !== $cipher_method) {
145
+			update_option($this->cipher_method_option_name, $cipher_method_to_test);
146
+		}
147
+		// if we previously removed this cipher method from the list of valid ones, then let's put it back
148
+		if (! in_array($cipher_method_to_test, $this->cipher_methods, true)) {
149
+			array_unshift($this->cipher_methods, $cipher_method_to_test);
150
+		}
151
+		return $cipher_method_to_test;
152
+	}
153
+
154
+
155
+	/**
156
+	 * @return array
157
+	 */
158
+	protected function getAvailableStrongCipherMethods(): array
159
+	{
160
+		return array_filter($this->installed_cipher_methods, [$this, 'weakAlgorithmFilter']);
161
+	}
162
+
163
+
164
+	/**
165
+	 * @param string $cipher_method
166
+	 * @param bool   $throw_exception
167
+	 * @return bool
168
+	 */
169
+	protected function validateCipherMethod(string $cipher_method, bool $throw_exception = false): bool
170
+	{
171
+		// verify that the requested cipher method is actually installed and can produce an initialization vector
172
+		if (
173
+			in_array($cipher_method, $this->installed_cipher_methods, true)
174
+			&& openssl_cipher_iv_length($cipher_method) !== false
175
+		) {
176
+			return true;
177
+		}
178
+		if (! $throw_exception) {
179
+			return false;
180
+		}
181
+		throw new RuntimeException(
182
+			sprintf(
183
+				esc_html__(
184
+					'The requested OpenSSL cipher method "%1$s" is invalid or not installed on the server. Please contact the server administrator.',
185
+					'event_espresso'
186
+				),
187
+				$cipher_method
188
+			)
189
+		);
190
+	}
191
+
192
+
193
+	/**
194
+	 * @see https://www.php.net/manual/en/function.openssl-get-cipher-methods.php#example-890
195
+	 * @param string $cipher_method
196
+	 * @return bool
197
+	 */
198
+	protected function weakAlgorithmFilter(string $cipher_method): bool
199
+	{
200
+		foreach ($this->weak_algorithms as $weak_algorithm) {
201
+			if (stripos($cipher_method, $weak_algorithm) !== false) {
202
+				return false;
203
+			}
204
+		}
205
+		return true;
206
+	}
207 207
 }
Please login to merge, or discard this patch.