/
code-server.php
149 lines (123 loc) · 3.63 KB
/
code-server.php
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
146
147
148
149
<?php
/* code-server.php
*
* Copyright © 2012 Zac Sturgeon (2012/01/22 22:33:14)
*
* Generates permutations of sets of three alpha-numeric characters,
* and returns a random, unique code, re-shuffling after all codes
* are exhausted (every 16'120 runs in it's current configuration).
*
* I am not a PHP developer and have never written any more than what's
* here, so please have mercy on my soul. The two files below should
* be stored somewhere OUTSIDE the webserver's path somewhere which
* the server has authority to read, write, and create these two files
* in. Exposing the list and index would make sequences predictable,
* making it theoretically possible to forge parent codes.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
$index = 'codes.index'; //file to store the current line number
$codes = 'codes.txt';
function generate($file) {
// Generate formats for ANA, NAN, and AAN (X = alpha, N = number)
$alpha = range('A', 'Z');
$numbers = range(0, 9);
$fh = fopen($file, 'w') or die("Can't open file");
foreach ($alpha as $i) {
foreach ($numbers as $j) {
foreach ($alpha as $k) {
fwrite($fh, $i . $j . $k . "\n");
}
}
}
foreach ($numbers as $i) {
foreach ($alpha as $j) {
foreach ($numbers as $k) {
fwrite($fh, $i . $j . $k . "\n");
}
}
}
foreach ($alpha as $i) {
foreach ($alpha as $j) {
foreach ($numbers as $k) {
fwrite($fh, $i . $j . $k . "\n");
}
}
}
fclose($fh);
}
//shuffle function
function pc_array_shuffle($array) {
$i = count($array);
while(--$i) {
$j = mt_rand(0, $i);
if ($i != $j) {
// swap elements
$tmp = $array[$j];
$array[$j] = $array[$i];
$array[$i] = $tmp;
}
}
return $array;
}
// shufle the line order of a file.
function randomize($file) {
$lines = file($file);
$lines = pc_array_shuffle($lines);
$fh = fopen($file, 'w') or die("Can't open file");
foreach($lines as $line) {
fwrite($fh, $line);
}
fclose($fh);
}
function getIndex($index) {
if ( file_exists($index) ) {
$f = file($index);
} else { //file doesn't exist; create it and store '1'.
setIndex($index, '0');
$f = array('0');
}
return $f[0];
}
function getRecord($codes, $line) {
$f = file($codes) or die("Can't open codes: $codes");
return $f[$line];
}
function setIndex($index, $value) {
$fh = fopen($index, 'w') or die("Can't open index: $index");
fwrite($fh, $value);
fclose($fh);
}
// if either codes.txt or codes.index doesn't exist, generate new codes.
if ( ! file_exists($index) || ! file_exists($codes) ) {
generate($codes);
randomize($codes);
}
/* begin main program */
$i = getIndex($index);
$lines = count(file($codes));
if ( $i == $lines ) {
// shuffle file and reset the count
randomize($codes);
setIndex($index, '0');
$i = 0;
}
$read = file($codes); // read all the codes into $read
echo $read[$i]; // output the $i-th line
$i++; //incrament the counter and save it
setIndex($index, $i);
?>