Issues (197)

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/GenerateCode.php (1 issue)

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 Imanghafoori\LaravelMicroscope;
4
5
use Illuminate\Support\Str;
6
use Imanghafoori\LaravelMicroscope\Analyzers\NamespaceCorrector;
7
use Imanghafoori\LaravelMicroscope\Analyzers\Refactor;
8
use Imanghafoori\LaravelMicroscope\LaravelPaths\FilePath;
9
use Imanghafoori\LaravelMicroscope\Stubs\ServiceProviderStub;
10
11
class GenerateCode
12
{
13
    /**
14
     * Get all of the listeners and their corresponding events.
15
     *
16
     * @param  iterable  $paths
17
     * @param  $composerPath
18
     * @param  $composerNamespace
19
     * @param  $command
20
     *
21
     * @return void
22
     */
23
    public static function serviceProvider($paths, $composerPath, $composerNamespace, $command)
24
    {
25
        foreach ($paths as $classFilePath) {
26
            /**
27
             * @var $classFilePath \Symfony\Component\Finder\SplFileInfo
28
             */
29
            if (! Str::endsWith($classFilePath->getFilename(), ['ServiceProvider.php'])) {
30
                continue;
31
            }
32
            $absFilePath = $classFilePath->getRealPath();
33
            $content = file_get_contents($absFilePath);
34
35
            if (strlen(\trim($content)) > 10) {
36
                // file is not empty
37
                continue;
38
            }
39
40
            $relativePath = FilePath::getRelativePath($absFilePath);
41
            $correctNamespace = NamespaceCorrector::calculateCorrectNamespace($relativePath, $composerPath, $composerNamespace);
42
43
            $className = \str_replace('.php', '', $classFilePath->getFilename());
44
            $answer = self::ask($command, $correctNamespace.'\\'.$className);
45
            if (! $answer) {
46
                continue;
47
            }
48
            $prefix = strtolower(str_replace('ServiceProvider', '', $className));
49
            file_put_contents($absFilePath, ServiceProviderStub::providerContent($correctNamespace, $className, $prefix));
50
51
            self::generateFolderStructure($classFilePath, $correctNamespace, $prefix);
52
            self::addToProvidersArray($correctNamespace.'\\'.$className);
53
        }
54
    }
55
56
    /**
57
     * Build the directory for the class if necessary.
58
     *
59
     * @param  string  $path
60
     * @return string
61
     */
62
    protected static function makeDirectory($path)
63
    {
64
        if (! is_dir($path)) {
65
            @mkdir($path, 0777, 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...
66
        }
67
68
        return $path;
69
    }
70
71
    private static function ask($command, $name)
72
    {
73
        return $command->getOutput()->confirm('Do you want to generate a service provider: '.$name, true);
74
    }
75
76
    private static function isProvidersKey($tokens, $i)
77
    {
78
        $token = $tokens[$i];
79
80
        return $token[0] == T_CONSTANT_ENCAPSED_STRING &&
81
            \trim($token[1], '\'\"') == 'providers' &&
82
            \in_array(T_DOUBLE_ARROW, [$tokens[$i + 1][0], $tokens[$i + 2][0]]);
83
    }
84
85
    private static function addToProvidersArray($providerPath)
86
    {
87
        $tokens = token_get_all(file_get_contents(config_path('app.php')));
88
89
        foreach ($tokens as $i => $token) {
90
            if (! self::isProvidersKey($tokens, $i)) {
91
                continue;
92
            }
93
            $closeBracketIndex = Analyzers\TokenManager::readBody($tokens, $i + 15, ']')[1];
94
95
            $j = $closeBracketIndex;
96
            while ($tokens[--$j][0] == T_WHITESPACE && $tokens[--$j][0] == T_COMMENT) {
97
            }
98
99
            // put a comma at the end of the array if it is not there
100
            $tokens[$j] !== ',' && array_splice($tokens, $j + 1, 0, [[',']]);
101
102
            array_splice($tokens, (int) $closeBracketIndex, 0, [["\n        ".$providerPath.'::class,'."\n    "]]);
103
            file_put_contents(config_path('app.php'), Refactor::toString($tokens));
104
        }
105
106
        return $tokens;
107
    }
108
109
    protected static function generateFolderStructure($classFilePath, $namespace, $prefix)
110
    {
111
        $_basePath = $classFilePath->getPath().DIRECTORY_SEPARATOR;
112
        file_put_contents($_basePath.$prefix.'_routes.php', self::routeContent($namespace));
113
        self::makeDirectory($_basePath.'Database'.DIRECTORY_SEPARATOR.'migrations');
114
        self::makeDirectory($_basePath.'views');
115
        self::makeDirectory($_basePath.'Http');
116
        self::makeDirectory($_basePath.'Database'.DIRECTORY_SEPARATOR.'Models');
117
    }
118
119
    protected static function routeContent($namespace)
120
    {
121
        return "<?php
122
123
use Illuminate\Support\Facades\Route;
124
125
Route::group(['middleware' => ['web'], 'namespace' => '$namespace\Http'], function () {
126
127
});";
128
    }
129
}
130