/
controller.php
191 lines (157 loc) · 4.34 KB
/
controller.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
//error_reporting(E_ALL); # Report Errors, Warnings, and Notices
//ini_set('display_errors', 1); # Display errors on page (instead of a log file)
//$words = file_get_contents("http://www.paulnoll.com/Books/Clear-English/words-01-02-hundred.html");
$handle = fopen("words.txt", "r") or die("Unable to open file!");;
// create an array to store all the words
$wordList = Array();
while (!feof($handle))
{
$word = fgets($handle);
array_push($wordList, $word);
}
fclose($handle);
/*
$wordList = Array('Harvard',
'Monkey',
'Violin',
'Apple',
'Goldfish',
'Swimming',
'Computer',
'Magic',
'Coffee',
'Disney');
*/
$numberOfWords = 0;
$userMessage = "";
$passwordString = "";
$includeNumber = FALSE;
$specialCharacter = FALSE;
// make sure the numberOfWord is greater than 0 and less than or equal to 4
if (isset($_POST["numberOfWords"]))
{
$numberOfWords = $_POST["numberOfWords"];
if ($numberOfWords <= 0 || $numberOfWords > 4)
{
$userMessage = "Please enter a valid number for words!";
}
else
{
$numberOfWords = $_POST["numberOfWords"];
}
}
// get the values of the checkboxes
if (isset($_POST["number"]))
{
$includeNumber = TRUE;
}
if (isset($_POST["specialCharacter"]))
{
$specialCharacter = TRUE;
}
// create an array to store the words
$wordsPicked = Array();
// make sure that the $numberOfWords is valid
if ($numberOfWords <= 0 || $numberOfWords > 4)
{
return;
}
// handle each different case
else if ($includeNumber && $specialCharacter)
{
generatePassword($numberOfWords, $wordList);
$wordsPickedAndNumber = addNumber($wordsPicked);
$wordsPickedFinal = addCharacter($wordsPickedAndNumber);
$passwordString = printPassword($wordsPickedFinal, $passwordString);
}
else if ($includeNumber)
{
generatePassword($numberOfWords, $wordList);
$wordsPicked = addNumber($wordsPicked);
$passwordString = printPassword($wordsPicked, $passwordString);
}
else if ($specialCharacter)
{
generatePassword($numberOfWords, $wordList);
$wordsPicked = addCharacter($wordsPicked);
$passwordString = printPassword($wordsPicked, $passwordString);
}
else
{
//$wordsPicked = generatePassword($numberOfWords, $wordsPicked, $wordList);
generatePassword($numberOfWords, $wordList);
$passwordString = printPassword($wordsPicked, $passwordString);
}
/*
* This is a function that generates random words inside an array.
* @param: $numberOfWords, $array, $wordList
* @return: N/A
*/
function generatePassword($numberOfWords, $wordList)
{
global $wordsPicked;
// pick random numbers that matches the number user inputs
for ($i = 0; $i < $numberOfWords; $i++)
{
$randomNumber = rand(0, count($wordList) - 1);
/*
This is loop that checks to make sure the same number isn't picked
again. It is unnecessary here since the wordList is so big.
It was useful in debugging with an array of 10 items.
*/
/*
for ($j = 0; $j < count($randomNumbers); $j++)
{
while ($randomNumbers[$j] == $randomNumber)
{
$randomNumber = rand(0, count($wordList) - 1);
print $randomNumber . "<br>";
}
}
*/
// push into array
array_push($wordsPicked, $wordList[$randomNumber]);
}
}
/*
* This is a function that loops over an array and prints the password string
* @param: $wordsPicked, $passwordString
* @return: $passwordString
*/
function printPassword($wordsPicked, $passwordString)
{
for ($i = 0; $i < count($wordsPicked); $i++)
{
$passwordString .= $wordsPicked[$i] . " ";
}
return $passwordString;
}
/*
* This is a function that adds a number to the array
* @param: $wordsPicked
* @return: $wordsPicked
*/
function addNumber($wordsPicked)
{
$keyNumber = rand(0, 9);
array_push($wordsPicked, $keyNumber);
shuffle($wordsPicked);
return $wordsPicked;
}
/*
* This is a function that adds a character to the array
* @param: $wordsPicked
* @return: $wordsPicked
*/
function addCharacter($wordsPicked)
{
// create an array for special character
$specialCharacters = Array("#", "$", "&", "*", "@");
// add a number to the words picked
$specialCharacter = rand(0, count($specialCharacters) - 1);
array_push($wordsPicked, $specialCharacters[$specialCharacter]);
shuffle($wordsPicked);
return $wordsPicked;
}
?>