-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_generateCode.php
More file actions
145 lines (122 loc) · 3.85 KB
/
class_generateCode.php
File metadata and controls
145 lines (122 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
/*
* @file class_generateCode
* @desc class to generate codes
* @author Tomasz Leszczynński <tomekleszczynski94@gmail.com>
* @url https://github.com/leszcz/generate-codes
* @version 1.0
*/
class generateCode
{
/* Cache folder localization */
private $cacheLocalization = "cache";
/* Generating random string (code)
* @params $length - length of a generated code
* @return Generated code
*/
private function generateString($length = 6)
{
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charsLength = strlen($chars);
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $chars[rand(0, $charsLength - 1)];
}
return $string;
}
/* Console mode (PHP-CLI) to generate codes
* @params array from user console
* @return Filename
*/
public function cli( array $params )
{
$checkParamsAndSave = $this->checkParamsAndSave( getopt($params) );
return "Nazwa pliku: ".$checkParamsAndSave;
}
/* Form in web browser (PHP-CGI) to generate codes
* @params array $_POST
* @return URL to file or web form
*/
public function cgi( array $params )
{
if( isset($params['s']) )
{
$checkParamsAndSave = $this->checkParamsAndSave( $params );
return '<a href="'.$checkParamsAndSave.'" title="Lista kodów" alt="Lista kodów">Pobierz liste kodów</a>';
} else {
return $this->getForm();
}
}
/* Function to generate a list of codes
* @params length and count of generated codes
* @return list of codes
*/
private function generateCodes( $length = 6, $count = 100 )
{
$codes = [];
for($i = 0; $i<$count; $i++)
{
$code = $this->generateString($length);
if(!in_array($code, $codes))
{
$codes[] = $code;
}
}
return $codes;
}
/* Save file with generated codes
* @params array $_POST
* @return URL to file or form
*/
private function saveFileWithCodes( $filename, $count, $length )
{
$checksum = md5($length.$count);
$cache = $this->cacheLocalization."/".$checksum;
if( !file_exists($filename) )
{
$getCodes = $this->generateCodes($length, $count); // generate Codes
if( file_exists($cache) ) {
$saveFile = file_put_contents($filename, file_get_contents($cache), FILE_APPEND); // save file with cache content
} else if( !file_exists($cache) ) {
$saveFile = file_put_contents($filename, implode(PHP_EOL, $getCodes), FILE_APPEND); // save file with generated codes
$saveCache = file_put_contents($cache, $filename, FILE_APPEND); // save cache file
}
if( !$saveFile || !$saveCache ) {
die("Błąd zapisu pliku");
} else {
return $filename;
}
}
}
/* Checking parameters and save a file
* @params array from CGI / CLI
* @return file
*/
private function checkParamsAndSave( array $params )
{
if( !is_array($params) )
die("Parametry nie zostały dostarczone prawidłowo. Potrzebna jest tablica [array]");
$length = intval($params['l']);
$count = intval($params['c']);
$filename = basename($params['f']);
if( $length == 0 || $count == 0 || empty($filename) ) {
echo "Popraw parametry!";
} else {
return $this->saveFileWithCodes($filename, $count, $length);
}
}
/* Open form in web browser (PHP-CGI) to generate codes */
protected function getForm()
{
return '
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<label for="c">Ilość kodów</label>
<input type="number" name="c" min="1" max="100000"><br />
<label for="l">Długość kodów</label>
<input type="number" name="l" min="1" max="10"><br />
<label for="f">Nazwa pliku:</label>
<input type="text" name="f" placeholder="kody.txt"><br />
<input type="submit" name="s" value="Wygeneruj i zapisz">
</form>';
}
}