yiicod /
yii2-cron
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
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
|
|||
| 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
|
|||
| 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
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 |
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@propertyannotation to your class or interface to document the existence of this variable.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.