Issues (280)

Security Analysis    not enabled

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.

classes/autoptimizeCriticalCSSBase.php (9 issues)

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
 * Critical CSS base file (initializes all ccss files).
4
 */
5
6
if ( ! defined( 'ABSPATH' ) ) {
7
    exit;
8
}
9
10
class autoptimizeCriticalCSSBase {
11
    /**
12
     * Main plugin filepath.
13
     * Used for activation/deactivation/uninstall hooks.
14
     *
15
     * @var string
16
     */
17
    protected $filepath = null;
18
19
    public function __construct()
20
    {
21
        // define constant, but only once.
22
        if ( ! defined( 'AO_CCSS_DIR' ) ) {
23
            // Define a constant with the directory to store critical CSS in.
24
            if ( is_multisite() ) {
25
                $blog_id = get_current_blog_id();
26
                define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' . $blog_id . '/' );
27
            } else {
28
                define( 'AO_CCSS_DIR', WP_CONTENT_DIR . '/uploads/ao_ccss/' );
29
            }
30
        }
31
        if ( ! defined( 'AO_CCSS_VER' ) ) {
32
            // Define plugin version.
33
            define( 'AO_CCSS_VER', 'AO_' . AUTOPTIMIZE_PLUGIN_VERSION );
34
35
            // Define constants for criticalcss.com base path and API endpoints.
36
            // fixme: AO_CCSS_URL should be read from the autoptimize availability json stored as option.
37
            define( 'AO_CCSS_URL', 'https://criticalcss.com' );
38
            define( 'AO_CCSS_API', AO_CCSS_URL . '/api/premium/' );
39
            define( 'AO_CCSS_SLEEP', 10 );
40
        }
41
42
        // Define support files locations, in case they are not already defined.
43
        if ( ! defined( 'AO_CCSS_LOCK' ) ) {
44
            define( 'AO_CCSS_LOCK', AO_CCSS_DIR . 'queue.lock' );
45
        }
46
        if ( ! defined( 'AO_CCSS_LOG' ) ) {
47
            define( 'AO_CCSS_LOG', AO_CCSS_DIR . 'queuelog.html' );
48
        }
49
        if ( ! defined( 'AO_CCSS_DEBUG' ) ) {
50
            define( 'AO_CCSS_DEBUG', AO_CCSS_DIR . 'queue.json' );
51
        }
52
53
        $this->filepath = __FILE__;
54
55
        $this->setup();
56
        $this->load_requires();
57
    }
58
59
    public function setup()
60
    {
61
        // get all options.
62
        $all_options = $this->fetch_options();
0 ignored issues
show
Are you sure the assignment to $all_options is correct as $this->fetch_options() (which targets autoptimizeCriticalCSSBase::fetch_options()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
63
        foreach ( $all_options as $option => $value ) {
0 ignored issues
show
The expression $all_options of type null is not traversable.
Loading history...
64
            ${$option} = $value;
65
        }
66
67
        // make sure the 10 minutes cron schedule is added.
68
        add_filter( 'cron_schedules', array( $this, 'ao_ccss_interval' ) );
69
70
        // check if we need to upgrade.
71
        $this->check_upgrade();
72
73
        // make sure ao_ccss_queue is scheduled OK if an API key is set.
74 View Code Duplication
        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) {
0 ignored issues
show
The variable $ao_ccss_key seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
This code seems to be duplicated across your project.

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

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

Loading history...
75
            wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' );
76
        }
77
    }
78
79
    public function load_requires() {
80
        // Required libs, core is always needed.
81
        $criticalcss_core = new autoptimizeCriticalCSSCore();
0 ignored issues
show
$criticalcss_core is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
83
        if ( defined( 'WP_CLI' ) || defined( 'DOING_CRON' ) || is_admin() ) {
84
            // TODO: also include if overridden somehow to force queue processing to be executed?
85
            $criticalcss_cron = new autoptimizeCriticalCSSCron();
0 ignored issues
show
$criticalcss_cron is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
        }
87
88
        if ( is_admin() ) {
89
            $criticalcss_settings = new autoptimizeCriticalCSSSettings();
0 ignored issues
show
$criticalcss_settings is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
        } else {
91
            // enqueuing only done when not wp-admin.
92
            $criticalcss_enqueue = new autoptimizeCriticalCSSEnqueue();
0 ignored issues
show
$criticalcss_enqueue is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
        }
94
    }
95
96
    public static function fetch_options() {
97
        static $autoptimize_ccss_options = null;
98
99
        if ( null === $autoptimize_ccss_options ) {
100
            // not cached yet, fetching from WordPress options.
101
            $autoptimize_ccss_options['ao_css_defer']          = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer' );
102
            $autoptimize_ccss_options['ao_css_defer_inline']   = autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer_inline' );
103
            $autoptimize_ccss_options['ao_ccss_rules_raw']     = get_option( 'autoptimize_ccss_rules', false );
104
            $autoptimize_ccss_options['ao_ccss_additional']    = get_option( 'autoptimize_ccss_additional' );
105
            $autoptimize_ccss_options['ao_ccss_queue_raw']     = get_option( 'autoptimize_ccss_queue', false );
106
            $autoptimize_ccss_options['ao_ccss_viewport']      = get_option( 'autoptimize_ccss_viewport', false );
107
            $autoptimize_ccss_options['ao_ccss_finclude']      = get_option( 'autoptimize_ccss_finclude', false );
108
            $autoptimize_ccss_options['ao_ccss_rtimelimit']    = get_option( 'autoptimize_ccss_rtimelimit', '30' );
109
            $autoptimize_ccss_options['ao_ccss_noptimize']     = get_option( 'autoptimize_ccss_noptimize', false );
110
            $autoptimize_ccss_options['ao_ccss_debug']         = get_option( 'autoptimize_ccss_debug', false );
111
            $autoptimize_ccss_options['ao_ccss_key']           = get_option( 'autoptimize_ccss_key' );
112
            $autoptimize_ccss_options['ao_ccss_keyst']         = get_option( 'autoptimize_ccss_keyst' );
113
            $autoptimize_ccss_options['ao_ccss_loggedin']      = get_option( 'autoptimize_ccss_loggedin', '1' );
114
            $autoptimize_ccss_options['ao_ccss_forcepath']     = get_option( 'autoptimize_ccss_forcepath', '1' );
115
            $autoptimize_ccss_options['ao_ccss_servicestatus'] = get_option( 'autoptimize_service_availablity' );
116
            $autoptimize_ccss_options['ao_ccss_deferjquery']   = get_option( 'autoptimize_ccss_deferjquery', false );
117
            $autoptimize_ccss_options['ao_ccss_domain']        = get_option( 'autoptimize_ccss_domain' );
118
            $autoptimize_ccss_options['ao_ccss_unloadccss']    = get_option( 'autoptimize_ccss_unloadccss', false );
119
120
            if ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) === false && strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'uggc' ) === 0 ) {
121
                $autoptimize_ccss_options['ao_ccss_domain'] = str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] );
122
            } elseif ( strpos( $autoptimize_ccss_options['ao_ccss_domain'], 'http' ) !== false ) {
123
                // not rot13'ed yet, do so now (goal; avoid migration plugins change the bound domain).
124
                update_option( 'autoptimize_ccss_domain', str_rot13( $autoptimize_ccss_options['ao_ccss_domain'] ) );
125
            }
126
127
            // Setup the rules array.
128
            if ( empty( $autoptimize_ccss_options['ao_ccss_rules_raw'] ) ) {
129
                $autoptimize_ccss_options['ao_ccss_rules']['paths'] = array();
130
                $autoptimize_ccss_options['ao_ccss_rules']['types'] = array();
131
            } else {
132
                $autoptimize_ccss_options['ao_ccss_rules'] = json_decode( $autoptimize_ccss_options['ao_ccss_rules_raw'], true );
133
            }
134
135
            // Setup the queue array.
136
            if ( empty( $autoptimize_ccss_options['ao_ccss_queue_raw'] ) ) {
137
                $autoptimize_ccss_options['ao_ccss_queue'] = array();
138
            } else {
139
                $autoptimize_ccss_options['ao_ccss_queue'] = json_decode( $autoptimize_ccss_options['ao_ccss_queue_raw'], true );
140
            }
141
142
            // Override API key if constant is defined.
143
            if ( defined( 'AUTOPTIMIZE_CRITICALCSS_API_KEY' ) ) {
144
                $autoptimize_ccss_options['ao_ccss_key'] = AUTOPTIMIZE_CRITICALCSS_API_KEY;
145
            }
146
        }
147
148
        return $autoptimize_ccss_options;
149
    }
150
151
    public function on_upgrade() {
152
        global $ao_ccss_key;
153
154
        // Create the cache directory if it doesn't exist already.
155
        if ( ! file_exists( AO_CCSS_DIR ) ) {
156
            mkdir( AO_CCSS_DIR, 0755, true );
157
        }
158
159
        // Create a scheduled event for the queue.
160 View Code Duplication
        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_queue' ) ) {
0 ignored issues
show
This code seems to be duplicated across your project.

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

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

Loading history...
161
            wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' );
162
        }
163
164
        // Create a scheduled event for log maintenance.
165
        if ( isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) && ! wp_next_scheduled( 'ao_ccss_maintenance' ) ) {
166
            wp_schedule_event( time(), 'twicedaily', 'ao_ccss_maintenance' );
167
        }
168
    }
169
170
    public function check_upgrade() {
171
        $db_version = get_option( 'autoptimize_ccss_version', '' );
172
        if ( AO_CCSS_VER !== $db_version ) {
173
            // check schedules & re-schedule if needed.
174
            $this->on_upgrade();
175
            // and update db_version.
176
            update_option( 'autoptimize_ccss_version', AO_CCSS_VER );
177
        }
178
    }
179
180
    public function ao_ccss_interval( $schedules ) {
181
        // Let interval be configurable.
182
        if ( ! defined( 'AO_CCSS_DEBUG_INTERVAL' ) ) {
183
            $intsec = 600;
184
        } else {
185
            $intsec = AO_CCSS_DEBUG_INTERVAL;
186
            if ( $intsec >= 120 ) {
187
                $inttxt = $intsec / 60 . ' minutes';
188
            } else {
189
                $inttxt = $intsec . ' second(s)';
190
            }
191
            autoptimizeCriticalCSSCore::ao_ccss_log( 'Using custom WP-Cron interval of ' . $inttxt, 3 );
192
        }
193
194
        // Attach interval to schedule.
195
        $schedules['ao_ccss'] = array(
196
            'interval' => $intsec,
197
            'display'  => __( 'Autoptimize CriticalCSS' ),
198
        );
199
        return $schedules;
200
    }
201
}
202