-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
177 lines (144 loc) · 4.51 KB
/
Copy pathfunctions.php
File metadata and controls
177 lines (144 loc) · 4.51 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
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
<?php
/**
* Saves a polysyllable that the visitor chosen to ignore.
*/
function save_ignored_poly($word) {
oawc_ensure_db();
if (empty($word)) {
return;
}
$query = "REPLACE INTO oawc_poly_ignore
(`word`, `session_id`)
VALUES (
'" . mysql_real_escape_string($word) . "',
'" . mysql_real_escape_string(get_session_id()) . "'
);";
mysql_query($query);
}
/**
* Saves a word replacement that the visitor has selected.
*/
function save_replaced_poly($original, $word) {
oawc_ensure_db();
if (empty($word) || empty($original)) {
return;
}
// Check if this word is already saved.
$query = "SELECT word, replacement FROM oawc_replacements
WHERE word = '" . mysql_real_escape_string($original) . "' AND replacement = '" . mysql_real_escape_string($word) . "' ";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
if ($row) {
$query = "UPDATE oawc_replacements SET `count` = `count`+1
WHERE word = '" . mysql_real_escape_string($original) . "' AND replacement = '" . mysql_real_escape_string($word) . "' ";
}
else {
$query = "INSERT INTO oawc_replacements
(`word`, `replacement`, `count`)
VALUES (
'" . mysql_real_escape_string($original) . "',
'" . mysql_real_escape_string($word) . "',
1
)
";
}
mysql_query($query);
}
/**
* Returns a list of polysyllables that the current visitor has already ignored.
*/
function get_ignored_polys() {
oawc_ensure_db();
$query = "SELECT word FROM oawc_poly_ignore
WHERE session_id = '*' OR session_id = '" . mysql_real_escape_string(get_session_id()) . "'";
$result = mysql_query($query);
$words = array();
while ($row = mysql_fetch_assoc($result)) {
$words[] = $row['word'];
}
return $words;
}
/**
* Returns the session id.
*/
function get_session_id() {
return (session_id() ? session_id() : $_SERVER['REMOTE_ADDR']);
}
/**
* Logs an error messages to the log file.
*/
function log_error($text) {
global $log_file;
file_put_contents($log_file, '[' . date('Y/m/d@G:i:s') . ']' . $text . "\n", FILE_APPEND);
}
/**
* Counts the syllables in a word.
*/
function count_syllables($word) {
$word = strtolower($word);
// Regex Patterns Needed
$triples = "dn't|eau|iou|ouy|you|bl$";
$doubles = "ai|ae|ay|au|ea|ee|ei|eu|ey|ie|ii|io|oa|oe|oi|oo|ou|oy|ue|uy|ya|ye|yi|yo|yu";
$singles = "a|e|i|o|u|y";
$vowels = "/(" . $triples . "|" . $doubles . "|" . $singles . ")/";
$trailing_e = "/e$/";
$trailing_s = "/s$/";
// Cleaning up word endings
$word = preg_replace($trailing_s, "", $word);
$word = preg_replace($trailing_e, "", $word);
// Count # of "vowels"
preg_match_all($vowels, $word, $matches);
$syl_count = count($matches[0]);
return $syl_count;
};
/**
* Replaces encoded characters.
*/
function remove_smart_quotes($file) {
// First, replace UTF-8 characters.
$file = str_replace(array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"), array("'", "'", '"', '"', '-', '--', '...'), $file);
// Next, replace their Windows-1252 equivalents.
$file = str_replace(array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)), array("'", "'", '"', '"', '-', '--', '...'), $file);
return $file;
};
/**
* Establishes database connection.
* Configuration is loaded from conf.php.
*/
function oawc_ensure_db() {
global $db_hostname, $db_username, $db_password, $db_name, $db_resource;
if (!empty($db_resource)) {
// Connection already established.
return TRUE;
}
$db_resource = mysql_connect($db_hostname, $db_username, $db_password);
if ($db_resource) {
if (!mysql_select_db($db_name, $db_resource)) {
print("Unable to select database: " . mysql_error());
return FALSE;
}
}
else {
print("Unable to open database: $db_hostname " . mysql_error());
return FALSE;
}
return TRUE;
}
/**
* Returns simpler alternative suggestions.
*/
function get_word_suggestions() {
$handle = fopen('writeclearly-thesaurus.csv', 'r');
$bad_words = array();
$good_words = array();
if ($handle !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if (count($data) > 1) {
$bad_words[] = trim($data[0]);
$good_words[] = trim($data[1]);
}
}
fclose($handle);
}
return array($bad_words, $good_words);
}