r69: Removing our nasty iff() function in place of the beautiful ternary operator
[bugdar.git] / docs / phrasetools.php
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
6 <title>Phrase Tools</title>
7 </head>
8 <body>
9
10 <?php
11
12 error_reporting(E_ALL & ~E_NOTICE);
13
14 // phrase tools for managing DB phrase system
15 // SVN: $Id$
16
17 chdir('./../');
18 require_once('./global.php');
19
20 define('SELECTED', ' selected="selected"');
21 define('CHECKED', ' checked="checked"');
22
23 sanitize(array('varname' => STR, 'phrasetext' => STR, 'matchmethod' => STR, 'do' => STR, 'doneinsert' => INT, 'oldvarname' => STR));
24 $use['varname'] = (bool)$_REQUEST['use']['varname'];
25 $use['phrasetext'] = (bool)$_REQUEST['use']['phrasetext'];
26 $use['and'] = (($vars['matchmethod'] == 'and') ? true : false);
27 $use['or'] = (($use['and']) ? false : true);
28 $use['matcher'] = (($use['and']) ? 'AND' : 'OR');
29
30 $select['or'] = (($use['or']) ? SELECTED : '');
31 $select['and'] = (($use['and']) ? SELECTED : '');
32 $select['search'] = (($vars['do'] == 'search') ? SELECTED : '');
33 $select['insert'] = (($vars['do'] == 'insert' OR $vars['doneinsert']) ? SELECTED : '');
34 $select['delete'] = (($vars['do'] == 'delete') ? SELECTED : '');
35
36 function sanitize_name($name)
37 {
38 $name = preg_replace('#[^a-zA-Z0-9_]#', '_', $name);
39 $name = preg_replace('#_{1,}#', '_', $name);
40 $name = preg_replace('#(^_|_$)#', '', $name);
41 return $name;
42 }
43
44 // ###################################################################
45
46 echo <<<HTML
47 <h2>Search</h2>
48
49 <form action="phrasetools.php" method="post" name="newphrase">
50 <div><strong><u>V</u>arname:</strong> <input type="text" name="varname" value="$vars[varname]" size="35" accesskey="v" /> <input type="checkbox" name="use[varname]" value="1" checked="checked" /></div>
51 <div><strong>Phrase <u>t</u>ext:</strong> <input type="checkbox" name="use[phrasetext]" value="1" /></div>
52 <div><textarea name="phrasetext" rows="7" cols="75" accesskey="t">$vars[phrasetext]</textarea></div>
53 <div>
54 <strong>Action:</strong>
55 <select name="do"><option value="search"$select[search]>Search</option><option value="insert"$select[insert]>Insert</option><option value="delete"$select[delete]>Delete</option></select>
56 </div>
57 <div>
58 <strong>Match Method:</strong>
59 <select name="matchmethod"><option value="or"$select[or]>* OR *</option><option value="and"$select[and]>+ AND +</option></select>
60 <input type="submit" name="submit" value=" Continue " accesskey="s" />
61 <input type="reset" name="reset" value=" Reset " />
62 </div>
63 </form>
64 HTML;
65
66 if ($_REQUEST['do'])
67 {
68 echo "\n\n<hr />\n\n";
69 }
70
71 // ###################################################################
72
73 if ($_REQUEST['do'] == 'kill')
74 {
75 $db->query("DELETE FROM " . TABLE_PREFIX . "phrase WHERE varname = '" . addslasheslike($vars['varname']) . "'");
76 header("Location: phrasetools.php");
77 }
78
79 // ###################################################################
80
81 if ($_REQUEST['do'] == 'delete')
82 {
83 $phrase = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "phrase WHERE varname = '" . addslasheslike($vars['varname']) . "'");
84 if (!$phrase)
85 {
86 echo 'Not a valid phrase!';
87 exit;
88 }
89
90 echo "<h2>Delete</h2>\n\n<pre><a href=\"phrasetools.php?do=kill&amp;varname=$phrase[varname]\">Found: <strong>$phrase[varname]</strong> ===> $phrase[phrasetext]</a></pre>";
91 }
92
93 // ###################################################################
94
95 if ($_POST['do'] == 'insert')
96 {
97 $vars['varname'] = str_replace(' ', '_', $vars['varname']);
98 $db->query("
99 INSERT INTO " . TABLE_PREFIX . "phrase
100 (varname, phrasetext)
101 VALUES
102 ('" . addslasheslike(sanitize_name($vars['varname'])) . "', '" . addslasheslike($vars['phrasetext']) . "'
103 )"
104 );
105 header("Location: phrasetools.php?do=edit&doneinsert=1&varname=$vars[varname]");
106 }
107
108 // ###################################################################
109
110 if ($_POST['do'] == 'update')
111 {
112 $db->query("
113 UPDATE " . TABLE_PREFIX . "phrase
114 SET varname = '" . addslasheslike(sanitize_name($vars['varname'])) . "',
115 phrasetext = '" . addslasheslike($vars['phrasetext']) . "'
116 WHERE varname = '" . addslasheslike($vars['oldvarname']) . "'"
117 );
118 header("Location: phrasetools.php?do=edit&varname=$vars[varname]");
119 }
120
121 // ###################################################################
122
123 if ($_REQUEST['do'] == 'edit')
124 {
125 $phrase = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "phrase WHERE varname = '" . addslasheslike($vars['varname']) . "'");
126 if (!$phrase)
127 {
128 echo 'Not a valid phrase!';
129 exit;
130 }
131
132 echo <<<HTML
133 <h2>Edit</h2>
134
135 <form action="phrasetools.php" method="post" name="editphrase">
136 <input type="hidden" name="do" value="update" />
137 <input type="hidden" name="oldvarname" value="$phrase[varname]" />
138 <div><strong>Varname:</strong> <input type="text" name="varname" value="$phrase[varname]" size="35" /></div>
139 <div><strong>Phrase text:</strong></div>
140 <div><textarea name="phrasetext" rows="7" cols="75">$phrase[phrasetext]</textarea></div>
141 <div>
142 <input type="submit" name="submit" value=" Continue " accesskey="e" />
143 <input type="reset" name="reset" value=" Reset " />
144 </div>
145 </form>
146 HTML;
147 }
148
149 // ###################################################################
150
151 if ($_REQUEST['do'] == 'search')
152 {
153 if ($use['varname'] AND !$use['phrasetext'])
154 {
155 $where = "varname LIKE '%$vars[varname]%'";
156 }
157 else if (!$use['varname'] AND $use['phrasetext'])
158 {
159 $where = "phrasetext LIKE '%$vars[phrasetext]%'";
160 }
161 else
162 {
163 $where = "varname LIKE '%$vars[varname]%' $use[matcher] phrasetext LIKE '%$vars[phrasetext]%'";
164 }
165
166 $phrases = $db->query("SELECT * FROM " . TABLE_PREFIX . "phrase WHERE $where");
167 $numrows = $db->num_rows($phrases);
168
169 if ($numrows < 1)
170 {
171 echo 'No results found!';
172 exit;
173 }
174
175 if ($numrows > 2)
176 {
177 while ($phrase = $db->fetch_array($phrases))
178 {
179 echo "<pre><div>\$bugsys->language['<strong><a href=\"phrasetools.php?do=edit&amp;varname=$phrase[varname]\">$phrase[varname]</a></strong>'] =======> " . htmlspecialcharslike($phrase['phrasetext']) . "</div></pre>";
180 }
181 }
182 else
183 {
184 $phrase = $db->fetch_array($phrases);
185 header("Location: phrasetools.php?do=edit&varname=$phrase[varname]");
186 }
187 }
188
189 ?>
190
191 </body>
192 </html>