PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyfaq/admin/ajax.config.php

http://github.com/thorsten/phpMyFAQ
PHP | 237 lines | 183 code | 37 blank | 17 comment | 38 complexity | a8dfb5a540f68caaeb2707478cd9c1c0 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * AJAX: handling of Ajax configuration calls.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public License,
  6. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. * obtain one at http://mozilla.org/MPL/2.0/.
  8. *
  9. * @package phpMyFAQ
  10. * @author Anatoliy Belsky <anatoliy.belsky@mayflower.de>
  11. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  12. * @copyright 2009-2021 phpMyFAQ Team
  13. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  14. * @link https://www.phpmyfaq.de
  15. * @since 2009-04-01
  16. */
  17. use phpMyFAQ\Database;
  18. use phpMyFAQ\Entity\MetaEntity as MetaEntity;
  19. use phpMyFAQ\Filter;
  20. use phpMyFAQ\Helper\HttpHelper;
  21. use phpMyFAQ\Instance;
  22. use phpMyFAQ\Instance\Client;
  23. use phpMyFAQ\Instance\Setup;
  24. use phpMyFAQ\Language;
  25. use phpMyFAQ\Meta;
  26. use phpMyFAQ\Stopwords;
  27. use phpMyFAQ\User;
  28. if (!defined('IS_VALID_PHPMYFAQ')) {
  29. http_response_code(400);
  30. exit();
  31. }
  32. $ajaxAction = Filter::filterInput(INPUT_GET, 'ajaxaction', FILTER_UNSAFE_RAW);
  33. $instanceId = Filter::filterInput(INPUT_GET, 'instanceId', FILTER_VALIDATE_INT);
  34. $stopwordId = Filter::filterInput(INPUT_GET, 'stopword_id', FILTER_VALIDATE_INT);
  35. $stopword = Filter::filterInput(INPUT_GET, 'stopword', FILTER_UNSAFE_RAW);
  36. $stopwordsLang = Filter::filterInput(INPUT_GET, 'stopwords_lang', FILTER_UNSAFE_RAW);
  37. $csrfToken = Filter::filterInput(INPUT_GET, 'csrf', FILTER_UNSAFE_RAW);
  38. $http = new HttpHelper();
  39. $stopwords = new Stopwords($faqConfig);
  40. switch ($ajaxAction) {
  41. case 'add_instance':
  42. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  43. $http->setStatus(400);
  44. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  45. exit(1);
  46. }
  47. $url = Filter::filterInput(INPUT_GET, 'url', FILTER_UNSAFE_RAW);
  48. $instance = Filter::filterInput(INPUT_GET, 'instance', FILTER_UNSAFE_RAW);
  49. $comment = Filter::filterInput(INPUT_GET, 'comment', FILTER_UNSAFE_RAW);
  50. $email = Filter::filterInput(INPUT_GET, 'email', FILTER_VALIDATE_EMAIL);
  51. $admin = Filter::filterInput(INPUT_GET, 'admin', FILTER_UNSAFE_RAW);
  52. $password = Filter::filterInput(INPUT_GET, 'password', FILTER_UNSAFE_RAW);
  53. if (empty($url) || empty($instance) || empty($comment) || empty($email) || empty($admin) || empty($password)) {
  54. $http->setStatus(400);
  55. $http->sendJsonWithHeaders(['error' => 'Cannot create instance.']);
  56. exit(1);
  57. }
  58. $data = [
  59. 'url' => 'https://' . $url . '.' . $_SERVER['SERVER_NAME'],
  60. 'instance' => $instance,
  61. 'comment' => $comment,
  62. ];
  63. $faqInstance = new Instance($faqConfig);
  64. $instanceId = $faqInstance->addInstance($data);
  65. $faqInstanceClient = new Client($faqConfig);
  66. $faqInstanceClient->createClient($faqInstance);
  67. $urlParts = parse_url($data['url']);
  68. $hostname = $urlParts['host'];
  69. if ($faqInstanceClient->createClientFolder($hostname)) {
  70. $clientDir = PMF_ROOT_DIR . '/multisite/' . $hostname;
  71. $clientSetup = new Setup();
  72. $clientSetup->setRootDir($clientDir);
  73. try {
  74. $faqInstanceClient->copyConstantsFile($clientDir . '/constants.php');
  75. } catch (\phpMyFAQ\Core\Exception $e) {
  76. }
  77. $dbSetup = [
  78. 'dbServer' => $DB['server'],
  79. 'dbPort' => $DB['port'],
  80. 'dbUser' => $DB['user'],
  81. 'dbPassword' => $DB['password'],
  82. 'dbDatabaseName' => $DB['db'],
  83. 'dbPrefix' => substr($hostname, 0, strpos($hostname, '.')),
  84. 'dbType' => $DB['type'],
  85. 'dbPort' => $DB['port']
  86. ];
  87. $clientSetup->createDatabaseFile($dbSetup, '');
  88. $faqInstanceClient->setClientUrl('https://' . $hostname);
  89. $faqInstanceClient->createClientTables($dbSetup['dbPrefix']);
  90. Database::setTablePrefix($dbSetup['dbPrefix']);
  91. // add admin account and rights
  92. $instanceAdmin = new User($faqConfig);
  93. $instanceAdmin->createUser($admin, $password, '', 1);
  94. $instanceAdmin->setStatus('protected');
  95. $instanceAdminData = [
  96. 'display_name' => '',
  97. 'email' => $email,
  98. ];
  99. $instanceAdmin->setUserData($instanceAdminData);
  100. // Add anonymous user account
  101. $clientSetup->createAnonymousUser($faqConfig);
  102. Database::setTablePrefix($DB['prefix']);
  103. } else {
  104. $faqInstance->removeInstance($instanceId);
  105. $http->setStatus(400);
  106. $payload = ['error' => 'Cannot create instance.'];
  107. }
  108. if (0 !== $instanceId) {
  109. $http->setStatus(200);
  110. $payload = ['added' => $instanceId, 'url' => $data['url']];
  111. } else {
  112. $http->setStatus(400);
  113. $payload = ['error' => $instanceId];
  114. }
  115. $http->sendJsonWithHeaders($payload);
  116. break;
  117. case 'delete_instance':
  118. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  119. $http->setStatus(400);
  120. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  121. exit(1);
  122. }
  123. if (null !== $instanceId) {
  124. $client = new Client($faqConfig);
  125. $clientData = $client->getInstanceById($instanceId);
  126. if (
  127. 1 !== $instanceId &&
  128. $client->deleteClientFolder($clientData->url) &&
  129. $client->removeInstance($instanceId)
  130. ) {
  131. $http->setStatus(200);
  132. $payload = ['deleted' => $instanceId];
  133. } else {
  134. $http->setStatus(400);
  135. $payload = ['error' => $instanceId];
  136. }
  137. $http->sendJsonWithHeaders($payload);
  138. }
  139. break;
  140. case 'load_stop_words_by_lang':
  141. if (Language::isASupportedLanguage($stopwordsLang)) {
  142. $stopwordsList = $stopwords->getByLang($stopwordsLang);
  143. $payload = $stopwordsList;
  144. $http->sendJsonWithHeaders($payload);
  145. }
  146. break;
  147. case 'delete_stop_word':
  148. if (null != $stopwordId && Language::isASupportedLanguage($stopwordsLang)) {
  149. $stopwords->setLanguage($stopwordsLang);
  150. $stopwords->remove((int)$stopwordId);
  151. }
  152. break;
  153. case 'save_stop_word':
  154. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  155. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  156. exit(1);
  157. }
  158. if (null != $stopword && Language::isASupportedLanguage($stopwordsLang)) {
  159. $stopwords->setLanguage($stopwordsLang);
  160. if (null !== $stopwordId && -1 < $stopwordId) {
  161. echo $stopwords->update((int)$stopwordId, $stopword);
  162. } elseif (!$stopwords->match($stopword)) {
  163. echo $stopwords->add($stopword);
  164. }
  165. }
  166. break;
  167. case 'add_meta':
  168. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  169. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  170. exit(1);
  171. }
  172. $meta = new Meta($faqConfig);
  173. $entity = new MetaEntity();
  174. $entity
  175. ->setPageId(Filter::filterInput(INPUT_GET, 'page_id', FILTER_UNSAFE_RAW))
  176. ->setType(Filter::filterInput(INPUT_GET, 'type', FILTER_UNSAFE_RAW))
  177. ->setContent(Filter::filterInput(INPUT_GET, 'content', FILTER_SANITIZE_SPECIAL_CHARS));
  178. $metaId = $meta->add($entity);
  179. if (0 !== $metaId) {
  180. $payload = ['added' => $metaId];
  181. } else {
  182. $payload = ['error' => $metaId];
  183. }
  184. $http->sendJsonWithHeaders($payload);
  185. break;
  186. case 'delete_meta':
  187. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $csrfToken) {
  188. $http->sendJsonWithHeaders(['error' => $PMF_LANG['err_NotAuth']]);
  189. exit(1);
  190. }
  191. $meta = new Meta($faqConfig);
  192. $metaId = Filter::filterInput(INPUT_GET, 'meta_id', FILTER_UNSAFE_RAW);
  193. if ($meta->delete((int)$metaId)) {
  194. $payload = ['deleted' => $metaId];
  195. } else {
  196. $payload = ['error' => $metaId];
  197. }
  198. $http->sendJsonWithHeaders($payload);
  199. break;
  200. }