Issues (218)

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.

src/Console/DatabaseBackupCommand.php (4 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 Finder\Console\Commands;
4
5
use File;
6
use Carbon\Carbon;
7
use Illuminate\Console\Command;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class DatabaseBackupCommand extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'sitec:db:backup';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Backup and restore database.';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return void
30
     */
31
    public function fire()
32
    {
33
        // Create destination dir if it does not exist
34
        $this->destination = base_path('database/backups');
0 ignored issues
show
The property destination does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
        if(! File::exists($this->destination)) {
36
            File::makeDirectory($this->destination);
37
        }
38
39
        // Show list
40
        if($this->option('list')) {
41
            return $this->showListOfBackups();
42
        }
43
44
        // Get database connection to use
45
        if(! $this->getConnection($this->option('connection'))) {
46
            return;
47
        }
48
49
        // Either restore ...
50
        if($this->option('restore')) {
51
            return ($this->restore($this->option('restore'), $this->option('force'))) ? $this->info('OK') : $this->error('Unable to restore');
52
        }
53
54
        // ... or backup
55
        return ($this->backup()) ? $this->info('OK') : $this->error('Unable to backup');
56
    }
57
58
    /**
59
     * Show a list of exisisting backups.
60
     *
61
     * @return void
62
     */
63
    protected function showListOfBackups()
64
    {
65
        if(! $files = File::files($this->destination)) {
66
            return $this->error('No backup files found');
67
        }
68
69
        foreach($files as $key => $file)
70
        {
71
            $date = Carbon::createFromTimeStamp(File::lastModified($file));
0 ignored issues
show
$file is of type object<Symfony\Component..."string","3":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72
73
            $files[$key] = [
74
            basename($file),
75
            $date->toDateTimeString(),
76
            $date->diffForHumans(),
77
            $this->bytesToHuman(File::size($file)),
0 ignored issues
show
$file is of type object<Symfony\Component..."string","3":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
            ];
79
        }
80
81
        $this->table(['File', 'Date', 'Age', 'Size'], $files);
82
    }
83
84
    /**
85
     * Get the database connection.
86
     *
87
     * @param  string $name
88
     * @return array|string
89
     */
90
    protected function getConnection($name)
91
    {
92
        // Make sure connection exists
93
        if(! $connection = \Illuminate\Support\Facades\Config::get("database.connections.$name")) {
94
            $this->error("Unknown connection '$name'");
95
96
            return false;
97
        }
98
99
        // Make sure the connection is MySQL
100
        if($connection['driver'] !== 'mysql') {
101
            $this->error("Unsupported connection type '{$connection['driver']}'. Only 'mysql' connections are supported");
102
103
            return false;
104
        }
105
106
        $this->connection = $connection;
0 ignored issues
show
The property connection does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
107
108
        return true;
109
    }
110
111
    /**
112
     * Create a backup.
113
     *
114
     * @return bool
115
     */
116
    protected function backup()
117
    {
118
        $date = Carbon::now()->toDateTimeString();
119
        $file = $this->destination . DIRECTORY_SEPARATOR . $date . ' ' . $this->connection['database'] . '.sql';
120
121
        // Build backup command
122
        $command = sprintf(
123
            'mysqldump --host=%s --user=%s --password=%s %s > %s',
124
            escapeshellarg($this->connection['host']),
125
            escapeshellarg($this->connection['username']),
126
            escapeshellarg($this->connection['password']),
127
            escapeshellarg($this->connection['database']),
128
            escapeshellarg(str_replace([' ', ':'], ['_', '.'], $file))
129
        );
130
131
        // Exec backup command
132
        exec($command, $output, $returnValue);
133
134
        return ($returnValue === 0);
135
    }
136
137
    /**
138
     * Restore a backup.
139
     *
140
     * @param  string $file  File to restore
141
     * @param  bool   $force Do not prompt for confirmation
142
     * @return bool
143
     */
144
    protected function restore($file, $force = false)
145
    {
146
        // Check if file exist
147
        $file = $this->destination . DIRECTORY_SEPARATOR . $file;
148
        if(! File::exists($file)) {
149
            $this->error("File not found '$file'");
150
151
            return false;
152
        }
153
154
        // Promt for confirmation
155
        if(! $force and ! $this->confirm('This could delete existing data. Do you wish to continue?', false)) {
156
            $this->comment('Cancelled by user');
157
158
            return false;
159
        }
160
161
        // Build restore command
162
        $command = sprintf(
163
            'mysql --host=%s --user=%s --password=%s %s < %s',
164
            escapeshellarg($this->connection['host']),
165
            escapeshellarg($this->connection['username']),
166
            escapeshellarg($this->connection['password']),
167
            escapeshellarg($this->connection['database']),
168
            escapeshellarg($file)
169
        );
170
171
        // Exec restore command
172
        exec($command, $output, $returnValue);
173
174
        return ($returnValue === 0);
175
    }
176
177
    /**
178
     * Convert bytes to human friendly unit.
179
     *
180
     * @param  integer $bytes
181
     * @param  integer $decimals
182
     * @return string
183
     */
184
    protected function bytesToHuman($bytes, $decimals = 2)
185
    {
186
        $size   = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
187
        $factor = floor((strlen($bytes) - 1) / 3);
188
189
        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
190
    }
191
192
    /**
193
     * Get the console command options.
194
     *
195
     * @return array
196
     */
197
    protected function getOptions()
198
    {
199
        return [
200
        ['list', 'l', InputOption::VALUE_NONE, 'List saved backups'],
201
        ['restore', 'r', InputOption::VALUE_REQUIRED, 'Restore a backup'],
202
        ['force', 'f', InputOption::VALUE_NONE, 'Do not prompt for confimation when restoring a backup'],
203
        ['connection', 'c', InputOption::VALUE_REQUIRED, 'Database connection name', \Illuminate\Support\Facades\Config::get('database.default')],
204
        ];
205
    }
206
}
207