Issues (18)

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.

commands/behaviors/LockUnLockBehavior.php (3 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
namespace yiicod\cron\commands\behaviors;
4
5
use Yii;
6
use yii\base\Behavior;
7
use yii\console\Controller;
8
use yii\helpers\Console;
9
10
/**
11
 * Exclude duplicate console command run.
12
 */
13
class LockUnLockBehavior extends Behavior
14
{
15
    /**
16
     * File time live. Default 28800 seconds (8 hour)
17
     */
18
    public $timeLock = 28800;
19
20
    /**
21
     * File path
22
     */
23
    protected $lockFilePath;
24
25
    /**
26
     * Declares events and the corresponding event handler methods.
27
     * If you override this method, make sure you merge the parent result to the return value.
28
     *
29
     * @return array events (array keys) and the corresponding event handler methods (array values)
30
     *
31
     * @see CBehavior::events
32
     */
33
    public function events()
34
    {
35
        return [
36
            Controller::EVENT_BEFORE_ACTION => 'beforeAction',
37
            Controller::EVENT_AFTER_ACTION => 'afterAction',
38
        ];
39
    }
40
41
    /**
42
     * Parses the command line arguments and determines which action to perform.
43
     *
44
     * @param array $args command line arguments
45
     *
46
     * @return array the action name, named options (name=>value), and unnamed options
47
     *
48
     * @since 1.1.5
49
     */
50
    protected function resolveRequest($args)
51
    {
52
        $options = []; // named parameters
53
        $params = []; // unnamed parameters
54
        foreach ($args as $arg) {
55
            if (preg_match('/^--(\w+)(=(.*))?$/', $arg, $matches)) {  // an option
56
                $name = $matches[1];
57
                $value = isset($matches[3]) ? $matches[3] : true;
58
                if (isset($options[$name])) {
59
                    if (!is_array($options[$name])) {
60
                        $options[$name] = [$options[$name]];
61
                    }
62
                    $options[$name][] = $value;
63
                } else {
64
                    $options[$name] = $value;
65
                }
66
            } elseif (isset($action)) {
67
                $params[] = $arg;
68
            } else {
69
                $action = $arg;
70
            }
71
        }
72
        if (!isset($action)) {
73
            $action = $this->defaultAction;
0 ignored issues
show
The property defaultAction does not exist on object<yiicod\cron\comma...ors\LockUnLockBehavior>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
        }
75
76
        //Change "/" for "." if action not default (like "controller/action")
77
        $action = str_replace('/', '.', $action);
78
79
        return [$action, $options, $params];
80
    }
81
82
    /**
83
     * @param $event
84
     *
85
     * @return bool
86
     */
87
    public function beforeAction($event)
88
    {
89
        $this->prepareLockFilePath();
90
91
        if (false === $this->lock()) {
92
            $event->isValid = false;
93
            $this->owner->stdout("Cron has run\n", Console::FG_RED);
94
        }
95
    }
96
97
    /**
98
     * @param $event
99
     */
100
    public function afterAction($event)
0 ignored issues
show
The parameter $event is not used and could be removed.

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

Loading history...
101
    {
102
        $this->unLock();
103
    }
104
105
    /**
106
     * Prepare lock file path
107
     */
108
    protected function prepareLockFilePath()
109
    {
110
        $filePath = sprintf('%s/runtime/locks', Yii::$app->basePath);
111
        if (false === is_dir($filePath)) {
112
            @mkdir($filePath, 0755, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
113
        }
114
        $argv = array_diff($_SERVER['argv'], ['yii']);
115
        list($action, $options, $args) = $this->resolveRequest($argv);
116
        $this->lockFilePath = mb_strtolower(sprintf('%s/%s.bin', $filePath, $action . preg_replace('/[^A-Za-z0-9-]+/', '_', trim(implode(' ', $args)) . trim(implode(' ', $options)))));
117
    }
118
119
    /**
120
     * Check the end of the process.
121
     * If a thread is not locked, it will be locked and started command.
122
     *
123
     * @return bool
124
     */
125
    protected function lock()
126
    {
127
        $lockFilePath = $this->lockFilePath;
128
129
        // current time
130
        if (false === file_exists($lockFilePath)) {
131
            file_put_contents($lockFilePath, time());
132
133
            return true;
134
        } else {
135
            $timeSec = time();
136
            // time change file
137
            $timeFile = @filemtime($lockFilePath) ? @filemtime($lockFilePath) : time();
138
139
            // Now find out how much time has passed (in seconds)
140
            if (($timeSec - $timeFile) > $this->timeLock) {
141
                $this->unLock();
142
                file_put_contents($lockFilePath, time());
143
144
                return true;
145
            }
146
147
            return false;
148
        }
149
    }
150
151
    /**
152
     * Unlocking the process
153
     *
154
     * @return bool
155
     */
156
    protected function unLock()
157
    {
158
        $lockFilePath = $this->lockFilePath;
159
160
        if (true === file_exists($lockFilePath)) {
161
            return unlink($lockFilePath);
162
        } else {
163
            return true;
164
        }
165
    }
166
}
167