Issues (3)

Security Analysis    no request data  

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/RegistryManager.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
 * CRM library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Crm;
7
8
use Slince\Crm\Exception\InvalidArgumentException;
9
use Slince\Crm\Exception\RegistryNotExistsException;
10
use Slince\Crm\Exception\RuntimeException;
11
12
class RegistryManager
13
{
14
    /**
15
     * registry collection
16
     * @var RegistryCollection
17
     */
18
    protected $registries;
19
20
    public function __construct()
21
    {
22
        $this->registries = new RegistryCollection();
23
    }
24
25
    /**
26
     * Read repositories from file
27
     * @param $file
28
     */
29
    public function readRegistriesFromFile($file)
30
    {
31
        $this->registries = RegistryCollection::fromArray(Utils::readJsonFile($file));
32
    }
33
34
    /**
35
     * Dump all registries to file
36
     * @param $file
37
     */
38
    public function dumpRepositoriesToFile($file)
39
    {
40
        $registries = $this->registries->toArray();
41
        Utils::getFilesystem()->dumpFile($file, json_encode($registries, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
42
    }
43
44
    /**
45
     * @return RegistryCollection
46
     */
47
    public function getRegistries()
48
    {
49
        return $this->registries;
50
    }
51
52
    /**
53
     * Add one registry
54
     * @param string $name
55
     * @param string $url
56
     * @throws InvalidArgumentException
57
     * @return Registry
58
     */
59
    public function addRegistry($name, $url)
60
    {
61
        try {
62
            $this->findRegistry($name);
63
            throw new InvalidArgumentException(sprintf("Registry [%s] already exists", $name));
64
        } catch (RegistryNotExistsException $exception) {
65
            $registry = Registry::create([
66
                'name' => $name,
67
                'url' => $url
68
            ]);
69
            $this->registries->add($registry);
70
        }
71
        return $registry;
72
    }
73
74
    /**
75
     * Remove registry
76
     * @param string $name
77
     */
78
    public function removeRegistry($name)
79
    {
80
        try {
81
            $registry = $this->findRegistry($name);
82
            $this->registries->remove($registry);
83
        } catch (RegistryNotExistsException $exception) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
84
    }
85
86
    /**
87
     * Find registry
88
     * @param $name
89
     * @return Registry
90
     * @throws RegistryNotExistsException
91
     */
92
    public function findRegistry($name)
93
    {
94
        foreach ($this->registries as $registry) {
95
            if (strcasecmp($name, $registry->getName()) == 0) {
96
                return $registry;
97
            }
98
        }
99
        throw new RegistryNotExistsException($name);
100
    }
101
102
    /**
103
     * Use Registry
104
     * @param Registry $registry
105
     * @param boolean $isCurrent
106
     * @return void
107
     * @codeCoverageIgnore
108
     */
109
    public function useRegistry(Registry $registry, $isCurrent = false)
110
    {
111
        $command = $this->makeUseRegistryCommand($registry, $isCurrent);
112
        $this->runSystemCommand($command);
113
    }
114
115
    /**
116
     * Get Current Registry
117
     * @param boolean $isCurrent
118
     * @throws RuntimeException
119
     * @return Registry
120
     * @codeCoverageIgnore
121
     */
122
    public function getCurrentRegistry($isCurrent = false)
123
    {
124
        $rawOutput = $this->runSystemCommand($isCurrent
125
            ? "composer config repo.packagist.org"
126
            : "composer config -g repo.packagist.org");
127
        $registryData = json_decode($rawOutput, true);
128
        if (json_last_error()) {
129
            throw new RuntimeException(sprintf("Can not find current registry, error: %s", json_last_error_msg()));
130
        }
131
        foreach ($this->registries as $registry) {
132
            if (strcasecmp($registry->getUrl(), $registryData['url']) == 0) {
133
                return $registry;
134
            }
135
        }
136
        return Registry::create([
137
            'url' => $registryData['url'],
138
            'name' => 'unknown'
139
        ]);
140
    }
141
142
    /**
143
     * Make change registry command string
144
     * @param Registry $registry
145
     * @param boolean $isCurrent
146
     * @return string
147
     * @codeCoverageIgnore
148
     */
149
    protected function makeUseRegistryCommand(Registry $registry, $isCurrent)
150
    {
151
        $command = $isCurrent
152
            ? "composer config repo.packagist composer %s"
153
            : "composer config -g repo.packagist composer %s";
154
        return sprintf($command, $registry->getUrl());
155
    }
156
157
    /**
158
     * Run command
159
     * @param string $command
160
     * @throws RuntimeException
161
     * @return string
162
     * @codeCoverageIgnore
163
     */
164
    protected function runSystemCommand($command)
165
    {
166
        $return = exec($command, $output, $returnVar);
167
        if ($returnVar !== 0) {
168
            throw new RuntimeException(sprintf('Execute the command "%s" failed, "composer" must be set for crm to run correctly', $command));
169
        }
170
        return $return;
171
    }
172
}
173