Issues (7)

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/Snowflake.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
/*
4
 * This file is part of the godruoyi/php-snowflake.
5
 *
6
 * (c) Godruoyi <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled.
9
 */
10
11
namespace Godruoyi\Snowflake;
12
13
class Snowflake
14
{
15
    const MAX_TIMESTAMP_LENGTH = 41;
16
17
    const MAX_DATACENTER_LENGTH = 5;
18
19
    const MAX_WORKID_LENGTH = 5;
20
21
    const MAX_SEQUENCE_LENGTH = 12;
22
23
    const MAX_FIRST_LENGTH = 1;
24
25
    /**
26
     * The data center id.
27
     *
28
     * @var int
29
     */
30
    protected $datacenter;
31
32
    /**
33
     * The worker id.
34
     *
35
     * @var int
36
     */
37
    protected $workerid;
38
39
    /**
40
     * The Sequence Resolver instance.
41
     *
42
     * @var \Godruoyi\Snowflake\SequenceResolver|null
43
     */
44
    protected $sequence;
45
46
    /**
47
     * The start timestamp.
48
     *
49
     * @var int
50
     */
51
    protected $startTime;
52
53
    /**
54
     * Default sequence resolver.
55
     *
56
     * @var \Godruoyi\Snowflake\SequenceResolver|null
57
     */
58
    protected $defaultSequenceResolver;
59
60
    /**
61
     * Build Snowflake Instance.
62
     *
63
     * @param int $datacenter
64
     * @param int $workerid
65
     */
66 13
    public function __construct(int $datacenter = null, int $workerid = null)
67
    {
68 13
        $maxDataCenter = -1 ^ (-1 << self::MAX_DATACENTER_LENGTH);
69 13
        $maxWorkId = -1 ^ (-1 << self::MAX_WORKID_LENGTH);
70
71
        // If not set datacenter or workid, we will set a default value to use.
72 13
        $this->datacenter = $datacenter > $maxDataCenter || $datacenter < 0 ? mt_rand(0, 31) : $datacenter;
73 13
        $this->workerid = $workerid > $maxWorkId || $workerid < 0 ? mt_rand(0, 31) : $workerid;
74 13
    }
75
76
    /**
77
     * Get snowflake id.
78
     *
79
     * @return string
80
     */
81 6
    public function id()
82
    {
83 6
        $currentTime = $this->getCurrentMicrotime();
84 6
        while (($sequence = $this->callResolver($currentTime)) > (-1 ^ (-1 << self::MAX_SEQUENCE_LENGTH))) {
85
            usleep(1);
86
            $currentTime = $this->getCurrentMicrotime();
87
        }
88
89 6
        $workerLeftMoveLength = self::MAX_SEQUENCE_LENGTH;
90 6
        $datacenterLeftMoveLength = self::MAX_WORKID_LENGTH + $workerLeftMoveLength;
91 6
        $timestampLeftMoveLength = self::MAX_DATACENTER_LENGTH + $datacenterLeftMoveLength;
92
93 6
        return (string) ((($currentTime - $this->getStartTimeStamp()) << $timestampLeftMoveLength)
94 6
            | ($this->datacenter << $datacenterLeftMoveLength)
95 6
            | ($this->workerid << $workerLeftMoveLength)
96 6
            | ($sequence));
97
    }
98
99
    /**
100
     * Parse snowflake id.
101
     */
102 3
    public function parseId(string $id, $transform = false): array
103
    {
104 3
        $id = decbin($id);
105
106
        $data = [
107 3
            'timestamp' => substr($id, 0, -22),
108 3
            'sequence' => substr($id, -12),
109 3
            'workerid' => substr($id, -17, 5),
110 3
            'datacenter' => substr($id, -22, 5),
111
        ];
112
113
        return $transform ? array_map(function ($value) {
114 3
            return bindec($value);
115 3
        }, $data) : $data;
116
    }
117
118
    /**
119
     * Get current microtime timestamp.
120
     *
121
     * @return int
122
     */
123 15
    public function getCurrentMicrotime()
124
    {
125 15
        return floor(microtime(true) * 1000) | 0;
126
    }
127
128
    /**
129
     * Set start time (millisecond).
130
     */
131 3 View Code Duplication
    public function setStartTimeStamp(int $startTime)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133 3
        $missTime = $this->getCurrentMicrotime() - $startTime;
134
135 3
        if ($missTime < 0) {
136
            throw new \Exception('The start time cannot be greater than the current time');
137
        }
138
139 3
        $maxTimeDiff = -1 ^ (-1 << self::MAX_TIMESTAMP_LENGTH);
140
141 3
        if ($missTime > $maxTimeDiff) {
142 2
            throw new \Exception(sprintf('The current microtime - starttime is not allowed to exceed -1 ^ (-1 << %d), You can reset the start time to fix this', self::MAX_TIMESTAMP_LENGTH));
143
        }
144
145 3
        $this->startTime = $startTime;
146
147 3
        return $this;
148
    }
149
150
    /**
151
     * Get start timestamp (millisecond), If not set default to 2019-08-08 08:08:08.
152
     *
153
     * @return int
154
     */
155 10
    public function getStartTimeStamp()
156
    {
157 10
        if ($this->startTime > 0) {
158 3
            return $this->startTime;
159
        }
160
161
        // We set a default start time if you not set.
162 9
        $defaultTime = '2019-08-08 08:08:08';
163
164 9
        return strtotime($defaultTime) * 1000;
165
    }
166
167
    /**
168
     * Set Sequence Resolver.
169
     *
170
     * @param SequenceResolver|callable $sequence
171
     */
172 4
    public function setSequenceResolver($sequence)
173
    {
174 4
        $this->sequence = $sequence;
175
176 4
        return $this;
177
    }
178
179
    /**
180
     * Get Sequence Resolver.
181
     *
182
     * @return \Godruoyi\Snowflake\SequenceResolver|callable|null
183
     */
184 10
    public function getSequenceResolver()
185
    {
186 10
        return $this->sequence;
187
    }
188
189
    /**
190
     * Get Default Sequence Resolver.
191
     *
192
     * @return \Godruoyi\Snowflake\SequenceResolver
193
     */
194 8
    public function getDefaultSequenceResolver(): SequenceResolver
195
    {
196 8
        return $this->defaultSequenceResolver ?: $this->defaultSequenceResolver = new RandomSequenceResolver();
197
    }
198
199
    /**
200
     * Call resolver.
201
     *
202
     * @param callable|\Godruoyi\Snowflake\SequenceResolver $resolver
203
     * @param int                                           $maxSequence
204
     *
205
     * @return int
206
     */
207 8
    protected function callResolver($currentTime)
208
    {
209 8
        $resolver = $this->getSequenceResolver();
210
211 8
        if (is_callable($resolver)) {
212 2
            return $resolver($currentTime);
213
        }
214
215 6
        return is_null($resolver) || !($resolver instanceof SequenceResolver)
216 6
            ? $this->getDefaultSequenceResolver()->sequence($currentTime)
217 6
            : $resolver->sequence($currentTime);
218
    }
219
}
220