PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/spellcheck.php

https://gitlab.com/tutaalexandr/bot_local
PHP | 345 lines | 270 code | 22 blank | 53 comment | 34 complexity | 4bbc857a0abce20fd6a0fddcb0abdc6a MD5 | raw file
  1. <?php
  2. /***************************************
  3. * http://www.program-o.com
  4. * PROGRAM O
  5. * Version: 2.4.8
  6. * FILE: spellcheck.php
  7. * AUTHOR: Elizabeth Perreau and Dave Morton
  8. * DATE: 12-09-2014
  9. * DETAILS: Displays the admin page for the spellcheck plugin and provides access to various features
  10. ***************************************/
  11. $msg = '';
  12. $upperScripts = <<<endScript
  13. <script type="text/javascript" src="scripts/tablesorter.js"></script>
  14. <script type="text/javascript">
  15. <!--
  16. var state = 'hidden';
  17. function showhide(layer_ref) {
  18. if (state == 'visible') {
  19. state = 'hidden';
  20. }
  21. else {
  22. state = 'visible';
  23. }
  24. if (document.all) { //IS IE 4 or 5 (or 6 beta)
  25. eval( "document.all." + layer_ref + ".style.visibility = state");
  26. }
  27. if (document.layers) { //IS NETSCAPE 4 or below
  28. document.layers[layer_ref].visibility = state;
  29. }
  30. if (document.getElementById && !document.all) {
  31. maxwell_smart = document.getElementById(layer_ref);
  32. maxwell_smart.style.visibility = state;
  33. }
  34. }
  35. //-->
  36. </script>
  37. endScript;
  38. $post_vars = filter_input_array(INPUT_POST);
  39. $get_vars = filter_input_array(INPUT_GET);
  40. $group = (isset($get_vars['group'])) ? $get_vars['group'] : 1;
  41. $content = $template->getSection('SearchSpellForm');
  42. $sc_action = isset($_REQUEST['action']) ? strtolower($_REQUEST['action']) : '';
  43. $sc_id = isset($_REQUEST['id']) ? $_REQUEST['id'] : -1;
  44. if (!empty($sc_action)) {
  45. switch($sc_action) {
  46. case 'search':
  47. $content .= runSpellSearch();
  48. $content .= spellCheckForm();
  49. break;
  50. case 'update':
  51. $x = updateSpell();
  52. $content .= spellCheckForm();
  53. break;
  54. case 'delete':
  55. $content .= ($sc_id >= 0) ? delSpell($sc_id) . spellCheckForm() : spellCheckForm();
  56. break;
  57. case 'edit':
  58. $content .= ($sc_id >= 0) ? editSpellForm($sc_id) : spellCheckForm();
  59. break;
  60. case 'add':
  61. $x = insertSpell();
  62. $content .= spellCheckForm();
  63. break;
  64. default:
  65. $content .= spellCheckForm();
  66. }
  67. }
  68. else {
  69. $content .= spellCheckForm();
  70. }
  71. $content = str_replace('[group]', $group, $content);
  72. $sc_enabled = (USE_SPELL_CHECKER) ? 'enabled' : 'disabled';
  73. $topNav = $template->getSection('TopNav');
  74. $leftNav = $template->getSection('LeftNav');
  75. $main = $template->getSection('Main');
  76. $navHeader = $template->getSection('NavHeader');
  77. $rightNav = $template->getSection('RightNav');
  78. $rightNavLinks = getMisspelledWords();
  79. $FooterInfo = getFooter();
  80. $errMsgClass = (!empty($msg)) ? "ShowError" : "HideError";
  81. $errMsgStyle = $template->getSection($errMsgClass);
  82. $noLeftNav = '';
  83. $noTopNav = '';
  84. $noRightNav = '';
  85. $headerTitle = 'Actions:';
  86. $pageTitle = 'My-Program O - Spellcheck Editor';
  87. $mainContent = $content;
  88. $mainTitle = 'Spellcheck Editor';
  89. $mainContent = str_replace('[spellCheckForm]', spellCheckForm(), $mainContent);
  90. $mainContent = str_replace('[sc_enabled]', $sc_enabled, $mainContent);
  91. $rightNav = str_replace('[rightNavLinks]', $rightNavLinks, $rightNav);
  92. $rightNav = str_replace('[navHeader]', $navHeader, $rightNav);
  93. $rightNav = str_replace('[headerTitle]', scPaginate(), $rightNav);
  94. /**
  95. * Function scPaginate
  96. *
  97. *
  98. * @return string
  99. */
  100. function scPaginate()
  101. {
  102. global $dbConn, $get_vars;
  103. $sql = "select count(*) from `spellcheck` where 1";
  104. $row = db_fetch($sql, null, __FILE__, __FUNCTION__, __LINE__);
  105. $rowCount = $row['count(*)'];
  106. $lastPage = intval($rowCount / 50);
  107. $remainder = ($rowCount / 50) - $lastPage;
  108. if ($remainder > 0) $lastPage++;
  109. $out = "Missspelled Words<br />\n50 words per page:<br />\n";
  110. $link=" - <a class=\"paginate\" href=\"index.php?page=spellcheck&amp;group=[group]\">[label]</a>";
  111. $curStart = (isset($get_vars['group'])) ? $get_vars['group'] : 1;
  112. $firstPage = 1;
  113. $prev = ($curStart > ($firstPage + 1)) ? $curStart - 1 : -1;
  114. $next = ($lastPage > ($curStart + 1)) ? $curStart + 1 : -1;
  115. $firstLink = ($firstPage != $curStart) ? str_replace('[group]', '1', $link) : '';
  116. $prevLink = ($prev > 0) ? str_replace('[group]', $prev, $link) : '';
  117. $curLink = "- $curStart ";
  118. if (empty($firstLink) and empty($prevLink)) $curLink = $curStart;
  119. $nextLink = ($next > 0) ? str_replace('[group]', $next, $link) : '';
  120. $lastLink = ($lastPage > $curStart) ? str_replace('[group]', $lastPage, $link) : '';
  121. $firstLink = str_replace('[label]', 'first', $firstLink);
  122. $prevLink = str_replace('[label]', '&lt;&lt;', $prevLink);
  123. $nextLink = str_replace('[label]', '&gt;&gt;', $nextLink);
  124. $lastLink = str_replace('[label]', 'last', $lastLink);
  125. $out .= ltrim("$firstLink\n$prevLink\n$curLink\n$nextLink\n$lastLink", " - ");
  126. return $out;
  127. }
  128. /**
  129. * Function getMisspelledWords
  130. *
  131. *
  132. * @return string
  133. */
  134. function getMisspelledWords() {
  135. global $dbConn, $template, $get_vars;
  136. # pagination variables
  137. $group = (isset($get_vars['group'])) ? $get_vars['group'] : 1;
  138. $_SESSION['poadmin']['group'] = $group;
  139. $startEntry = ($group - 1) * 50;
  140. $end = $group + 50;
  141. $_SESSION['poadmin']['page_start'] = $group;
  142. $curID = (isset($get_vars['id'])) ? $get_vars['id'] : -1;
  143. $sql = "select `id`,`missspelling` from `spellcheck` where 1 order by abs(`id`) asc limit $startEntry, 50;";
  144. $baseLink = $template->getSection('NavLink');
  145. $links = ' <div class="userlist">' . "\n";
  146. $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
  147. $count = 0;
  148. foreach ($result as $row) {
  149. $linkId = $row['id'];
  150. $linkClass = ($linkId == $curID) ? 'selected' : 'noClass';
  151. $missspelling = $row['missspelling'];
  152. $tmpLink = str_replace('[linkClass]', " class=\"$linkClass\"", $baseLink);
  153. $linkHref = " href=\"index.php?page=spellcheck&amp;action=edit&amp;id=$linkId&amp;group=$group#$linkId\" name=\"$linkId\"";
  154. $tmpLink = str_replace('[linkHref]', $linkHref, $tmpLink);
  155. $tmpLink = str_replace('[linkOnclick]', '', $tmpLink);
  156. $tmpLink = str_replace('[linkTitle]', " title=\"Edit spelling correction for the word '$missspelling'\"", $tmpLink);
  157. $tmpLink = str_replace('[linkLabel]', $missspelling, $tmpLink);
  158. $links .= "$tmpLink\n";
  159. $count++;
  160. }
  161. $page_count = intval($count / 50);
  162. $_SESSION['poadmin']['page_count'] = $page_count + (($count / 50) > $page_count) ? 1 : 0;
  163. $links .= "\n </div>\n";
  164. return $links;
  165. }
  166. /**
  167. * Function spellCheckForm
  168. *
  169. *
  170. * @return mixed|string
  171. */
  172. function spellCheckForm() {
  173. global $template, $get_vars;
  174. $out = $template->getSection('SpellcheckForm');
  175. $group = (isset($get_vars['group'])) ? $get_vars['group'] : 1;
  176. $out = str_replace('[group]', $group, $out);
  177. return $out;
  178. }
  179. /**
  180. * Function insertSpell
  181. *
  182. *
  183. * @return string
  184. */
  185. function insertSpell() {
  186. global $dbConn, $template, $msg, $post_vars;
  187. $correction = trim($post_vars['correction']);
  188. $missspell = trim($post_vars['missspell']);
  189. if(($correction == "") || ($missspell == "")) {
  190. $msg = ' <div id="errMsg">You must enter a spelling mistake and the correction.</div>' . "\n";
  191. }
  192. else {
  193. $sql = "INSERT INTO `spellcheck` VALUES (NULL,'$missspell','$correction')";
  194. $params = array(
  195. ':missspell' => $missspell,
  196. ':correction' => $correction
  197. );
  198. $affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
  199. if($affectedRows > 0) {
  200. $msg = '<div id="successMsg">Correction added.</div>';
  201. }
  202. else {
  203. $msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
  204. }
  205. }
  206. return $msg;
  207. }
  208. /**
  209. * Function delSpell
  210. *
  211. * * @param $id
  212. * @return void
  213. */
  214. function delSpell($id) {
  215. global $dbConn, $template, $msg;
  216. if($id=="") {
  217. $msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
  218. }
  219. else {
  220. $sql = "DELETE FROM `spellcheck` WHERE `id` = :id LIMIT 1";
  221. $params = array(':id' => $id);
  222. $affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
  223. if($affectedRows > 0) {
  224. $msg = '<div id="successMsg">Correction deleted.</div>';
  225. }
  226. else {
  227. $msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
  228. }
  229. }
  230. }
  231. /**
  232. * Function runSpellSearch
  233. *
  234. *
  235. * @return string
  236. */
  237. function runSpellSearch() {
  238. global $dbConn, $template, $post_vars;
  239. $i=0;
  240. $search = trim($post_vars['search']);
  241. $sql = "SELECT * FROM `spellcheck` WHERE `missspelling` LIKE '%$search%' OR `correction` LIKE '%$search%' LIMIT 50";
  242. $result = db_fetchAll($sql, null, __FILE__, __FUNCTION__, __LINE__);
  243. $htmltbl = '<table>
  244. <thead>
  245. <tr>
  246. <th class="sortable">missspelling</th>
  247. <th class="sortable">Correction</th>
  248. <th>Action</th>
  249. </tr>
  250. </thead>
  251. <tbody>';
  252. foreach ($result as $row) {
  253. $i++;
  254. $misspell = strtoupper($row['missspelling']);
  255. $correction = strtoupper($row['correction']);
  256. $id = $row['id'];
  257. $group = round(($id / 50));
  258. $action = "<a href=\"index.php?page=spellcheck&amp;action=edit&amp;id=$id&amp;group=$group#$id\"><img src=\"images/edit.png\" border=0 width=\"15\" height=\"15\" alt=\"Edit this entry\" title=\"Edit this entry\" /></a>
  259. <a href=\"index.php?page=spellcheck&amp;action=del&amp;id=$id&amp;group=$group#$id\" onclick=\"return confirm('Do you really want to delete this missspelling? You will not be able to undo this!')\";><img src=\"images/del.png\" border=0 width=\"15\" height=\"15\" alt=\"Edit this entry\" title=\"Edit this entry\" /></a>";
  260. $htmltbl .= "<tr valign=top>
  261. <td>$misspell</td>
  262. <td>$correction</td>
  263. <td align=center>$action</td>
  264. </tr>";
  265. }
  266. $htmltbl .= "</tbody></table>";
  267. if($i >= 50) {
  268. $msg = "Found more than 50 results for '<b>$search</b>', please refine your search further";
  269. }
  270. elseif($i == 0) {
  271. $msg = "Found 0 results for '<b>$search</b>'. You can use the form below to add that entry.";
  272. $htmltbl="";
  273. }
  274. else {
  275. $msg = "Found $i results for '<b>$search</b>'";
  276. }
  277. $htmlresults = "<div id=\"pTitle\">$msg</div>".$htmltbl;
  278. return $htmlresults;
  279. }
  280. /**
  281. * Function editSpellForm
  282. *
  283. * * @param $id
  284. * @return mixed|string
  285. */
  286. function editSpellForm($id) {
  287. global $dbConn, $template, $get_vars;
  288. $group = (isset($get_vars['group'])) ? $get_vars['group'] : 1;
  289. $form = $template->getSection('EditSpellForm');
  290. $sql = "SELECT * FROM `spellcheck` WHERE `id` = '$id' LIMIT 1";
  291. $row = db_fetch($sql, null, __FILE__, __FUNCTION__, __LINE__);
  292. $uc_missspelling = (IS_MB_ENABLED) ? mb_strtoupper($row['missspelling']) : strtoupper($row['missspelling']);
  293. $uc_correction = (IS_MB_ENABLED) ? mb_strtoupper($row['correction']) : strtoupper($row['correction']);
  294. $form = str_replace('[id]', $row['id'], $form);
  295. $form = str_replace('[missspelling]', $uc_missspelling, $form);
  296. $form = str_replace('[correction]', $uc_correction, $form);
  297. $form = str_replace('[group]', $group, $form);
  298. return $form;
  299. }
  300. function updateSpell() {
  301. global $dbConn, $template, $msg, $post_vars;
  302. $missspelling = trim($post_vars['missspelling']);
  303. $correction = trim($post_vars['correction']);
  304. $id = trim($post_vars['id']);
  305. if(($id=="")||($missspelling=="")||($correction=="")) {
  306. $msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
  307. }
  308. else {
  309. $sql = "UPDATE `spellcheck` SET `missspelling` = :missspelling,`correction` = :correction WHERE `id` = :id LIMIT 1";
  310. $params = array(
  311. ':missspelling' => $missspelling,
  312. ':correction' => $correction,
  313. ':id' => $id
  314. );
  315. $affectedRows = db_write($sql, $params, false, __FILE__, __FUNCTION__, __LINE__);
  316. if($affectedRows > 0) {
  317. $msg = '<div id="successMsg">Correction edited.</div>';
  318. }
  319. else {
  320. $msg = '<div id="errMsg">There was a problem editing the correction - no changes made.</div>';
  321. }
  322. }
  323. }