PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/ajaxservice.php

https://github.com/NHLH-ITM/phpMyFAQ-kindeditor
PHP | 850 lines | 649 code | 141 blank | 60 comment | 141 complexity | 968434af3588833e9e1c8fabf6c50ca2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * The Ajax Service Layer
  4. *
  5. * PHP Version 5.4
  6. *
  7. * This Source Code Form is subject to the terms of the Mozilla Public License,
  8. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  9. * obtain one at http://mozilla.org/MPL/2.0/.
  10. *
  11. * @category phpMyFAQ
  12. * @package Ajax
  13. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  14. * @copyright 2010-2014 phpMyFAQ Team
  15. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  16. * @link http://www.phpmyfaq.de
  17. * @since 2010-09-15
  18. */
  19. use Symfony\Component\HttpFoundation\JsonResponse;
  20. use PMF\Helper\ResponseWrapper;
  21. define('IS_VALID_PHPMYFAQ', null);
  22. //
  23. // Bootstrapping
  24. //
  25. require 'inc/Bootstrap.php';
  26. $action = PMF_Filter::filterInput(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
  27. $ajaxlang = PMF_Filter::filterInput(INPUT_POST, 'lang', FILTER_SANITIZE_STRING);
  28. $code = PMF_Filter::filterInput(INPUT_POST, 'captcha', FILTER_SANITIZE_STRING);
  29. $currentToken = PMF_Filter::filterInput(INPUT_POST, 'csrf', FILTER_SANITIZE_STRING);
  30. $Language = new PMF_Language($faqConfig);
  31. $languageCode = $Language->setLanguage($faqConfig->get('main.languageDetection'), $faqConfig->get('main.language'));
  32. require_once 'lang/language_en.php';
  33. $faqConfig->setLanguage($Language);
  34. if (PMF_Language::isASupportedLanguage($ajaxlang)) {
  35. $languageCode = trim($ajaxlang);
  36. require_once 'lang/language_' . $languageCode . '.php';
  37. } else {
  38. $languageCode = 'en';
  39. require_once 'lang/language_en.php';
  40. }
  41. //Load plurals support for selected language
  42. $plr = new PMF_Language_Plurals($PMF_LANG);
  43. //
  44. // Initalizing static string wrapper
  45. //
  46. PMF_String::init($languageCode);
  47. // Check captcha
  48. $captcha = new PMF_Captcha($faqConfig);
  49. $captcha->setSessionId(
  50. PMF_Filter::filterInput(INPUT_COOKIE, PMF_Session::PMF_COOKIE_NAME_SESSIONID, FILTER_VALIDATE_INT)
  51. );
  52. // Prepare response
  53. $response = new JsonResponse;
  54. $responseWrapper = new ResponseWrapper($response);
  55. $responseWrapper->addCommonHeaders();
  56. // Set session
  57. $faqsession = new PMF_Session($faqConfig);
  58. $network = new PMF_Network($faqConfig);
  59. $stopwords = new PMF_Stopwords($faqConfig);
  60. if (!$network->checkIp($_SERVER['REMOTE_ADDR'])) {
  61. $message = array('error' => $PMF_LANG['err_bannedIP']);
  62. }
  63. // Check, if user is logged in
  64. $user = PMF_User_CurrentUser::getFromSession($faqConfig);
  65. if ($user instanceof PMF_User_CurrentUser) {
  66. $isLoggedIn = true;
  67. } else {
  68. $isLoggedIn = false;
  69. }
  70. if ('savevoting' !== $action && 'saveuserdata' !== $action && 'changepassword' !== $action &&
  71. !$captcha->checkCaptchaCode($code) && !$isLoggedIn) {
  72. $message = array('error' => $PMF_LANG['msgCaptcha']);
  73. }
  74. if (isset($message['error'])) {
  75. $response->setData($message)->send();
  76. exit;
  77. }
  78. // Save user generated content
  79. switch ($action) {
  80. // Comments
  81. case 'savecomment':
  82. if (!$faqConfig->get('records.allowCommentsForGuests') &&
  83. $user->perm->checkRight($user->getUserId(), 'addcomment')) {
  84. $message = array('error' => $PMF_LANG['err_NotAuth']);
  85. break;
  86. }
  87. $faq = new PMF_Faq($faqConfig);
  88. $oComment = new PMF_Comment($faqConfig);
  89. $type = PMF_Filter::filterInput(INPUT_POST, 'type', FILTER_SANITIZE_STRING);
  90. $faqid = PMF_Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT, 0);
  91. $newsid = PMF_Filter::filterInput(INPUT_POST, 'newsid', FILTER_VALIDATE_INT);
  92. $username = PMF_Filter::filterInput(INPUT_POST, 'user', FILTER_SANITIZE_STRING);
  93. $mail = PMF_Filter::filterInput(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL);
  94. $comment = PMF_Filter::filterInput(INPUT_POST, 'comment_text', FILTER_SANITIZE_SPECIAL_CHARS);
  95. switch ($type) {
  96. case 'news':
  97. $id = $newsid;
  98. break;
  99. case 'faq';
  100. $id = $faqid;
  101. break;
  102. }
  103. // If e-mail address is set to optional
  104. if (!$faqConfig->get('main.optionalMailAddress') && is_null($mail)) {
  105. $mail = $faqConfig->get('main.administrationMail');
  106. }
  107. if (!is_null($username) && !empty($username) && !empty($mail) && !is_null($mail) && !is_null($comment) &&
  108. !empty($comment) && $stopwords->checkBannedWord($comment) && !$faq->commentDisabled($id, $languageCode, $type)) {
  109. try {
  110. $faqsession->userTracking('save_comment', $id);
  111. } catch (PMF_Exception $e) {
  112. // @todo handle the exception
  113. }
  114. $commentData = array(
  115. 'record_id' => $id,
  116. 'type' => $type,
  117. 'username' => $username,
  118. 'usermail' => $mail,
  119. 'comment' => nl2br($comment),
  120. 'date' => $_SERVER['REQUEST_TIME'],
  121. 'helped' => '');
  122. if ($oComment->addComment($commentData)) {
  123. $emailTo = $faqConfig->get('main.administrationMail');
  124. $urlToContent = '';
  125. if ('faq' == $type) {
  126. $faq->getRecord($id);
  127. if ($faq->faqRecord['email'] != '') {
  128. $emailTo = $faq->faqRecord['email'];
  129. }
  130. $faqUrl = sprintf(
  131. '%s?action=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
  132. $faqConfig->get('main.referenceURL'),
  133. 0,
  134. $faq->faqRecord['id'],
  135. $faq->faqRecord['lang']
  136. );
  137. $oLink = new PMF_Link($faqUrl, $faqConfig);
  138. $oLink->itemTitle = $faq->faqRecord['title'];
  139. $urlToContent = $oLink->toString();
  140. } else {
  141. $oNews = new PMF_News($faqConfig);
  142. $news = $oNews->getNewsEntry($id);
  143. if ($news['authorEmail'] != '') {
  144. $emailTo = $news['authorEmail'];
  145. }
  146. $link = sprintf('%s?action=news&amp;newsid=%d&amp;newslang=%s',
  147. $faqConfig->get('main.referenceURL'),
  148. $news['id'],
  149. $news['lang']
  150. );
  151. $oLink = new PMF_Link($link, $faqConfig);
  152. $oLink->itemTitle = $news['header'];
  153. $urlToContent = $oLink->toString();
  154. }
  155. $commentMail =
  156. 'User: ' . $commentData['username'] . ', mailto:'. $commentData['usermail'] . "\n".
  157. 'New comment posted on: ' . $urlToContent .
  158. "\n\n" .
  159. wordwrap($comment, 72);
  160. $send = [];
  161. $mail = new PMF_Mail($faqConfig);
  162. $mail->setReplyTo($commentData['usermail'], $commentData['username']);
  163. $mail->addTo($emailTo);
  164. $send[$emailTo] = 1;
  165. // Let the admin get a copy of the message
  166. if (!isset($send[$faqConfig->get('main.administrationMail')])) {
  167. $mail->addCc($faqConfig->get('main.administrationMail'));
  168. $send[$faqConfig->get('main.administrationMail')] = 1;
  169. }
  170. // Let the category owner get a copy of the message
  171. $category = new PMF_Category($faqConfig, $current_groups);
  172. $categories = $category->getCategoryIdsFromArticle($faq->faqRecord['id']);
  173. foreach ($categories as $_category) {
  174. $userId = $category->getCategoryUser($_category);
  175. $catUser = new PMF_User($faqConfig);
  176. $catUser->getUserById($userId);
  177. $catOwnerEmail = $catUser->getUserData('email');
  178. if ($catOwnerEmail != '') {
  179. if (!isset($send[$catOwnerEmail])) {
  180. $mail->addCc($catOwnerEmail);
  181. $send[$catOwnerEmail] = 1;
  182. }
  183. }
  184. }
  185. $mail->subject = '%sitename%';
  186. $mail->message = strip_tags($commentMail);
  187. $result = $mail->send();
  188. unset($mail);
  189. $message = array('success' => $PMF_LANG['msgCommentThanks']);
  190. } else {
  191. try {
  192. $faqsession->userTracking('error_save_comment', $id);
  193. } catch (PMF_Exception $e) {
  194. // @todo handle the exception
  195. }
  196. $message = array('error' => $PMF_LANG['err_SaveComment']);
  197. }
  198. } else {
  199. $message = array('error' => 'Please add your name, your e-mail address and a comment!');
  200. }
  201. break;
  202. case 'savefaq':
  203. if (!$faqConfig->get('records.allowNewFaqsForGuests') &&
  204. $user->perm->checkRight($user->getUserId(), 'addfaq')) {
  205. $message = array('error' => $PMF_LANG['err_NotAuth']);
  206. break;
  207. }
  208. $faq = new PMF_Faq($faqConfig);
  209. $category = new PMF_Category($faqConfig);
  210. $name = PMF_Filter::filterInput(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
  211. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  212. $faqid = PMF_Filter::filterInput(INPUT_POST, 'faqid', FILTER_VALIDATE_INT);
  213. $faqlanguage = PMF_Filter::filterInput(INPUT_POST, 'faqlanguage', FILTER_SANITIZE_STRING);
  214. $question = PMF_Filter::filterInput(INPUT_POST, 'question', FILTER_SANITIZE_STRIPPED);
  215. if ($faqConfig->get('main.enableWysiwygEditorFrontend')) {
  216. $answer = PMF_Filter::filterInput(INPUT_POST, 'answer', FILTER_SANITIZE_SPECIAL_CHARS);
  217. $answer = html_entity_decode($answer);
  218. } else {
  219. $answer = PMF_Filter::filterInput(INPUT_POST, 'answer', FILTER_SANITIZE_STRIPPED);
  220. $answer = nl2br($answer);
  221. }
  222. $translation = PMF_Filter::filterInput(INPUT_POST, 'translated_answer', FILTER_SANITIZE_STRING);
  223. $contentlink = PMF_Filter::filterInput(INPUT_POST, 'contentlink', FILTER_VALIDATE_URL);
  224. $keywords = PMF_Filter::filterInput(INPUT_POST, 'keywords', FILTER_SANITIZE_STRIPPED);
  225. $categories = PMF_Filter::filterInputArray(
  226. INPUT_POST,
  227. array(
  228. 'rubrik' => array(
  229. 'filter' => FILTER_VALIDATE_INT,
  230. 'flags' => FILTER_REQUIRE_ARRAY
  231. )
  232. )
  233. );
  234. // Check on translation
  235. if (empty($answer) && !is_null($translation)) {
  236. $answer = $translation;
  237. }
  238. if (!is_null($name) && !empty($name) && !is_null($email) && !empty($email) &&
  239. !is_null($question) && !empty($question) && $stopwords->checkBannedWord(strip_tags($question)) &&
  240. !is_null($answer) && !empty($answer) && $stopwords->checkBannedWord(strip_tags($answer)) &&
  241. ((is_null($faqid) && !is_null($categories['rubrik'])) || (!is_null($faqid) && !is_null($faqlanguage) &&
  242. PMF_Language::isASupportedLanguage($faqlanguage)))) {
  243. $isNew = true;
  244. if (!is_null($faqid)) {
  245. $isNew = false;
  246. try {
  247. $faqsession->userTracking('save_new_translation_entry', 0);
  248. } catch (PMF_Exception $e) {
  249. // @todo handle the exception
  250. }
  251. } else {
  252. try {
  253. $faqsession->userTracking('save_new_entry', 0);
  254. } catch (PMF_Exception $e) {
  255. // @todo handle the exception
  256. }
  257. }
  258. $isTranslation = false;
  259. if (!is_null($faqlanguage)) {
  260. $isTranslation = true;
  261. $newLanguage = $faqlanguage;
  262. }
  263. if (PMF_String::substr($contentlink, 7) != "") {
  264. $answer = sprintf(
  265. '%s<br /><div id="newFAQContentLink">%s<a href="http://%s" target="_blank">%s</a></div>',
  266. $answer,
  267. $PMF_LANG['msgInfo'],
  268. PMF_String::substr($contentlink, 7),
  269. $contentlink
  270. );
  271. }
  272. $autoActivate = $faqConfig->get('records.defaultActivation');
  273. $newData = array(
  274. 'lang' => ($isTranslation == true ? $newLanguage : $languageCode),
  275. 'thema' => $question,
  276. 'active' => ($autoActivate ? FAQ_SQL_ACTIVE_YES : FAQ_SQL_ACTIVE_NO),
  277. 'sticky' => 0,
  278. 'content' => $answer,
  279. 'keywords' => $keywords,
  280. 'author' => $name,
  281. 'email' => $email,
  282. 'comment' => FAQ_SQL_YES,
  283. 'date' => date('YmdHis'),
  284. 'dateStart' => '00000000000000',
  285. 'dateEnd' => '99991231235959',
  286. 'linkState' => '',
  287. 'linkDateCheck' => 0);
  288. if ($isNew) {
  289. $categories = $categories['rubrik'];
  290. } else {
  291. $newData['id'] = $faqid;
  292. $categories = $category->getCategoryIdsFromArticle($newData['id']);
  293. }
  294. $recordId = $faq->addRecord($newData, $isNew);
  295. $faq->addCategoryRelations($categories, $recordId, $newData['lang']);
  296. $openQuestionId = PMF_Filter::filterInput(INPUT_POST, 'openQuestionID', FILTER_VALIDATE_INT);
  297. if ($openQuestionId) {
  298. if ($faqConfig->get('records.enableDeleteQuestion')) {
  299. $faq->deleteQuestion($openQuestionId);
  300. } else { // adds this faq record id to the related open question
  301. $faq->updateQuestionAnswer($openQuestionId, $recordId, $categories[0]);
  302. }
  303. }
  304. // Activate visits
  305. $visits = new PMF_Visits($faqConfig);
  306. $visits->add($recordId, $newData['lang']);
  307. if ($autoActivate) {
  308. // Add user permissions
  309. $faq->addPermission('user', $recordId, array(-1));
  310. $category->addPermission('user', $categories['rubrik'], array(-1));
  311. // Add group permission
  312. if ($faqConfig->get('security.permLevel') != 'basic') {
  313. $faq->addPermission('group', $recordId, array(-1));
  314. $category->addPermission('group', $categories['rubrik'], array(-1));
  315. }
  316. }
  317. // Let the PMF Administrator and the Category Owner to be informed by email of this new entry
  318. $send = [];
  319. $mail = new PMF_Mail($faqConfig);
  320. $mail->setReplyTo($email, $name);
  321. $mail->addTo($faqConfig->get('main.administrationMail'));
  322. $send[$faqConfig->get('main.administrationMail')] = 1;
  323. foreach ($categories as $_category) {
  324. $userId = $category->getCategoryUser($_category);
  325. // @todo Move this code to Category.php
  326. $oUser = new PMF_User($faqConfig);
  327. $oUser->getUserById($userId);
  328. $catOwnerEmail = $oUser->getUserData('email');
  329. // Avoid to send multiple emails to the same owner
  330. if (!isset($send[$catOwnerEmail])) {
  331. $mail->addCc($catOwnerEmail);
  332. $send[$catOwnerEmail] = 1;
  333. }
  334. }
  335. $mail->subject = '%sitename%';
  336. // @todo let the email contains the faq article both as plain text and as HTML
  337. $mail->message = html_entity_decode(
  338. $PMF_LANG['msgMailCheck']) . "\n\n" .
  339. $faqConfig->get('main.titleFAQ') . ": " .
  340. $faqConfig->get('main.referenceURL') . '/admin/';
  341. $result = $mail->send();
  342. unset($mail);
  343. $message = array(
  344. 'success' => ($isNew ? $PMF_LANG['msgNewContentThanks'] : $PMF_LANG['msgNewTranslationThanks'])
  345. );
  346. } else {
  347. $message = array('error' => $PMF_LANG['err_SaveEntries']);
  348. }
  349. break;
  350. case 'savequestion':
  351. if (!$faqConfig->get('records.allowQuestionsForGuests') &&
  352. $user->perm->checkRight($user->getUserId(), 'addquestion')) {
  353. $message = array('error' => $PMF_LANG['err_NotAuth']);
  354. break;
  355. }
  356. $faq = new PMF_Faq($faqConfig);
  357. $cat = new PMF_Category($faqConfig);
  358. $categories = $cat->getAllCategories();
  359. $name = PMF_Filter::filterInput(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
  360. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  361. $ucategory = PMF_Filter::filterInput(INPUT_POST, 'category', FILTER_VALIDATE_INT);
  362. $question = PMF_Filter::filterInput(INPUT_POST, 'question', FILTER_SANITIZE_STRIPPED);
  363. $save = PMF_Filter::filterInput(INPUT_POST, 'save', FILTER_VALIDATE_INT, 0);
  364. // If e-mail address is set to optional
  365. if (!$faqConfig->get('main.optionalMailAddress') && is_null($email)) {
  366. $email = $faqConfig->get('main.administrationMail');
  367. }
  368. if (!is_null($name) && !empty($name) && !is_null($email) && !empty($email) &&
  369. !is_null($question) && !empty($question) && $stopwords->checkBannedWord(PMF_String::htmlspecialchars($question))) {
  370. if ($faqConfig->get('records.enableVisibilityQuestions')) {
  371. $visibility = 'N';
  372. } else {
  373. $visibility = 'Y';
  374. }
  375. $questionData = array(
  376. 'username' => $name,
  377. 'email' => $email,
  378. 'category_id' => $ucategory,
  379. 'question' => $question,
  380. 'is_visible' => $visibility
  381. );
  382. if (1 != $save) {
  383. $cleanQuestion = $stopwords->clean($question);
  384. $user = new PMF_User_CurrentUser($faqConfig);
  385. $faqSearch = new PMF_Search($faqConfig);
  386. $faqSearchResult = new PMF_Search_Resultset($user, $faq, $faqConfig);
  387. $searchResult = [];
  388. $mergedResult = [];
  389. foreach ($cleanQuestion as $word) {
  390. $searchResult[] = $faqSearch->search($word);
  391. }
  392. foreach ($searchResult as $resultSet) {
  393. foreach($resultSet as $result) {
  394. $mergedResult[] = $result;
  395. }
  396. }
  397. $faqSearchResult->reviewResultset($mergedResult);
  398. if (0 < $faqSearchResult->getNumberOfResults()) {
  399. $response = sprintf('<p>%s</p>',
  400. $plr->GetMsg('plmsgSearchAmount', $faqSearchResult->getNumberOfResults()));
  401. $response .= '<ul>';
  402. foreach ($faqSearchResult->getResultset() as $result) {
  403. $url = sprintf(
  404. '%s/index.php?action=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
  405. $faqConfig->get('main.referenceURL'),
  406. $result->category_id,
  407. $result->id,
  408. $result->lang
  409. );
  410. $oLink = new PMF_Link($url, $faqConfig);
  411. $oLink->text = PMF_Utils::chopString($result->question, 15);
  412. $oLink->itemTitle = $result->question;
  413. $response .= sprintf('<li>%s<br /><div class="searchpreview">%s...</div></li>',
  414. $oLink->toHtmlAnchor(),
  415. PMF_Utils::chopString(strip_tags($result->answer), 10)
  416. );
  417. }
  418. $response .= '</ul>';
  419. $message = array('result' => $response);
  420. } else {
  421. $faq->addQuestion($questionData);
  422. $questionMail = "User: " . $questionData['username'] .
  423. ", mailto:".$questionData['email'] . "\n" . $PMF_LANG["msgCategory"] .
  424. ": " . $categories[$questionData['category_id']]["name"] . "\n\n" .
  425. wordwrap($question, 72) . "\n\n" .
  426. $faqConfig->get('main.referenceURL') . '/admin/';
  427. $userId = $cat->getCategoryUser($questionData['category_id']);
  428. $oUser = new PMF_User($faqConfig);
  429. $oUser->getUserById($userId);
  430. $userEmail = $oUser->getUserData('email');
  431. $mainAdminEmail = $faqConfig->get('main.administrationMail');
  432. $mail = new PMF_Mail($faqConfig);
  433. $mail->setReplyTo($questionData['email'], $questionData['username']);
  434. $mail->addTo($mainAdminEmail);
  435. // Let the category owner get a copy of the message
  436. if ($userEmail && $mainAdminEmail != $userEmail) {
  437. $mail->addCc($userEmail);
  438. }
  439. $mail->subject = '%sitename%';
  440. $mail->message = $questionMail;
  441. $mail->send();
  442. unset($mail);
  443. $message = array('success' => $PMF_LANG['msgAskThx4Mail']);
  444. }
  445. } else {
  446. $faq->addQuestion($questionData);
  447. $questionMail = "User: " . $questionData['username'] .
  448. ", mailto:".$questionData['email'] . "\n" . $PMF_LANG["msgCategory"] .
  449. ": " . $categories[$questionData['category_id']]["name"] . "\n\n" .
  450. wordwrap($question, 72) . "\n\n" .
  451. $faqConfig->get('main.referenceURL') . '/admin/';
  452. $userId = $cat->getCategoryUser($questionData['category_id']);
  453. $oUser = new PMF_User($faqConfig);
  454. $oUser->getUserById($userId);
  455. $userEmail = $oUser->getUserData('email');
  456. $mainAdminEmail = $faqConfig->get('main.administrationMail');
  457. $mail = new PMF_Mail($faqConfig);
  458. $mail->setReplyTo($questionData['email'], $questionData['username']);
  459. $mail->addTo($mainAdminEmail);
  460. // Let the category owner get a copy of the message
  461. if ($userEmail && $mainAdminEmail != $userEmail) {
  462. $mail->addCc($userEmail);
  463. }
  464. $mail->subject = '%sitename%';
  465. $mail->message = $questionMail;
  466. $mail->send();
  467. unset($mail);
  468. $message = array('success' => $PMF_LANG['msgAskThx4Mail']);
  469. }
  470. } else {
  471. $message = array('error' => $PMF_LANG['err_SaveQuestion']);
  472. }
  473. break;
  474. case 'saveregistration':
  475. $realname = PMF_Filter::filterInput(INPUT_POST, 'realname', FILTER_SANITIZE_STRING);
  476. $loginname = PMF_Filter::filterInput(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
  477. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  478. if (!is_null($loginname) && !empty($loginname) && !is_null($email) && !empty($email) &&
  479. !is_null($realname) && !empty($realname)) {
  480. $message = [];
  481. $user = new PMF_User($faqConfig);
  482. // Create user account (login and password)
  483. // Note: password be automatically generated and sent by email as soon if admin switch user to "active"
  484. if (!$user->createUser($loginname, '')) {
  485. $message = array('error' => $user->error());
  486. } else {
  487. $user->userdata->set(
  488. array('display_name', 'email'),
  489. array($realname, $email)
  490. );
  491. // set user status
  492. $user->setStatus('blocked');
  493. $text = sprintf(
  494. "New user has been registrated:\n\nName: %s\nLogin name: %s\n\n" .
  495. "To activate this user do please use the administration interface at %s.",
  496. $realname,
  497. $loginname,
  498. $faqConfig->get('main.referenceURL')
  499. );
  500. $mail = new PMF_Mail($faqConfig);
  501. $mail->setReplyTo($email, $realname);
  502. $mail->addTo($faqConfig->get('main.administrationMail'));
  503. $mail->subject = PMF_Utils::resolveMarkers($PMF_LANG['emailRegSubject'], $faqConfig);
  504. $mail->message = $text;
  505. $result = $mail->send();
  506. unset($mail);
  507. $message = array(
  508. 'success' => trim($PMF_LANG['successMessage']) .
  509. ' ' .
  510. trim($PMF_LANG['msgRegThankYou'])
  511. );
  512. }
  513. } else {
  514. $message = array('error' => $PMF_LANG['err_sendMail']);
  515. }
  516. break;
  517. case 'savevoting':
  518. $faq = new PMF_Faq($faqConfig);
  519. $type = PMF_Filter::filterInput(INPUT_POST, 'type', FILTER_SANITIZE_STRING, 'faq');
  520. $recordId = PMF_Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT, 0);
  521. $vote = PMF_Filter::filterInput(INPUT_POST, 'vote', FILTER_VALIDATE_INT);
  522. $userIp = PMF_Filter::filterVar($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP);
  523. if (isset($vote) && $faq->votingCheck($recordId, $userIp) && $vote > 0 && $vote < 6) {
  524. try {
  525. $faqsession->userTracking('save_voting', $recordId);
  526. } catch (PMF_Exception $e) {
  527. // @todo handle the exception
  528. }
  529. $votingData = array(
  530. 'record_id' => $recordId,
  531. 'vote' => $vote,
  532. 'user_ip' => $userIp);
  533. if (!$faq->getNumberOfVotings($recordId)) {
  534. $faq->addVoting($votingData);
  535. } else {
  536. $faq->updateVoting($votingData);
  537. }
  538. $faqRating = new PMF_Rating($faqConfig);
  539. $message = array(
  540. 'success' => $PMF_LANG['msgVoteThanks'],
  541. 'rating' => $faqRating->getVotingResult($recordId)
  542. );
  543. } elseif (!$faq->votingCheck($recordId, $userIp)) {
  544. try {
  545. $faqsession->userTracking('error_save_voting', $recordId);
  546. } catch (PMF_Exception $e) {
  547. // @todo handle the exception
  548. }
  549. $message = array('error' => $PMF_LANG['err_VoteTooMuch']);
  550. } else {
  551. try {
  552. $faqsession->userTracking('error_save_voting', $recordId);
  553. } catch (PMF_Exception $e) {
  554. // @todo handle the exception
  555. }
  556. $message = array('error' => $PMF_LANG['err_noVote']);
  557. }
  558. break;
  559. // Send user generated mails
  560. case 'sendcontact':
  561. $name = PMF_Filter::filterInput(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
  562. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  563. $question = PMF_Filter::filterInput(INPUT_POST, 'question', FILTER_SANITIZE_STRIPPED);
  564. // If e-mail address is set to optional
  565. if (!$faqConfig->get('main.optionalMailAddress') && is_null($email)) {
  566. $email = $faqConfig->get('main.administrationMail');
  567. }
  568. if (!is_null($name) && !empty($name) && !is_null($email) && !empty($email) && !is_null($question) &&
  569. !empty($question) && $stopwords->checkBannedWord(PMF_String::htmlspecialchars($question))) {
  570. $question = sprintf(
  571. "%s %s\n%s %s\n\n %s",
  572. $PMF_LANG["msgNewContentName"],
  573. $name,
  574. $PMF_LANG["msgNewContentMail"],
  575. $email,
  576. $question
  577. );
  578. $mail = new PMF_Mail($faqConfig);
  579. $mail->setReplyTo($email, $name);
  580. $mail->addTo($faqConfig->get('main.administrationMail'));
  581. $mail->subject = 'Feedback: %sitename%';;
  582. $mail->message = $question;
  583. $result = $mail->send();
  584. unset($mail);
  585. $message = array('success' => $PMF_LANG['msgMailContact']);
  586. } else {
  587. $message = array('error' => $PMF_LANG['err_sendMail']);
  588. }
  589. break;
  590. // Send mails to friends
  591. case 'sendtofriends':
  592. $name = PMF_Filter::filterInput(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
  593. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  594. $link = PMF_Filter::filterInput(INPUT_POST, 'link', FILTER_VALIDATE_URL);
  595. $attached = PMF_Filter::filterInput(INPUT_POST, 'message', FILTER_SANITIZE_STRIPPED);
  596. $mailto = PMF_Filter::filterInputArray(INPUT_POST,
  597. array('mailto' =>
  598. array('filter' => FILTER_VALIDATE_EMAIL,
  599. 'flags' => FILTER_REQUIRE_ARRAY | FILTER_NULL_ON_FAILURE
  600. )
  601. )
  602. );
  603. if (!is_null($name) && !empty($name) && !is_null($email) && !empty($email) &&
  604. is_array($mailto) && !empty($mailto['mailto'][0]) &&
  605. $stopwords->checkBannedWord(PMF_String::htmlspecialchars($attached))) {
  606. foreach($mailto['mailto'] as $recipient) {
  607. $recipient = trim(strip_tags($recipient));
  608. if (!empty($recipient)) {
  609. $mail = new PMF_Mail($faqConfig);
  610. $mail->setReplyTo($email, $name);
  611. $mail->addTo($recipient);
  612. $mail->subject = $PMF_LANG["msgS2FMailSubject"].$name;
  613. $mail->message = sprintf("%s\r\n\r\n%s\r\n%s\r\n\r\n%s",
  614. $faqConfig->get('main.send2friendText'),
  615. $PMF_LANG['msgS2FText2'],
  616. $link,
  617. $attached);
  618. // Send the email
  619. $result = $mail->send();
  620. unset($mail);
  621. usleep(250);
  622. }
  623. }
  624. $message = array('success' => $PMF_LANG['msgS2FThx']);
  625. } else {
  626. $message = array('error' => $PMF_LANG['err_sendMail']);
  627. }
  628. break;
  629. // Save user data from UCP
  630. case 'saveuserdata':
  631. if (!isset($_SESSION['phpmyfaq_csrf_token']) || $_SESSION['phpmyfaq_csrf_token'] !== $currentToken) {
  632. $message = array('error' => $PMF_LANG['ad_msg_noauth']);
  633. break;
  634. }
  635. $userId = PMF_Filter::filterInput(INPUT_POST, 'userid', FILTER_VALIDATE_INT);
  636. $name = PMF_Filter::filterInput(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
  637. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  638. $password = PMF_Filter::filterInput(INPUT_POST, 'password', FILTER_SANITIZE_STRING);
  639. $confirm = PMF_Filter::filterInput(INPUT_POST, 'password_confirm', FILTER_SANITIZE_STRING);
  640. $user = PMF_User_CurrentUser::getFromSession($faqConfig);
  641. if ($userId !== $user->getUserId()) {
  642. $message = array('error' => 'User ID mismatch!');
  643. break;
  644. }
  645. if ($password !== $confirm) {
  646. $message = array('error' => $PMF_LANG['ad_user_error_passwordsDontMatch']);
  647. break;
  648. }
  649. $userData = array(
  650. 'display_name' => $name,
  651. 'email' => $email);
  652. $success = $user->setUserData($userData);
  653. if (0 !== strlen($password) && 0 !== strlen($confirm)) {
  654. foreach ($user->getAuthContainer() as $name => $auth) {
  655. if ($auth->setReadOnly()) {
  656. continue;
  657. }
  658. if (!$auth->changePassword($user->getLogin(), $password)) {
  659. $message = array('error' => $auth->error());
  660. $success = false;
  661. } else {
  662. $success = true;
  663. }
  664. }
  665. }
  666. if ($success) {
  667. $message = array('success' => $PMF_LANG['ad_entry_savedsuc']);
  668. } else {
  669. $message = array('error' => $PMF_LANG['ad_entry_savedfail']);
  670. }
  671. break;
  672. case 'changepassword':
  673. $username = PMF_Filter::filterInput(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
  674. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  675. if (!is_null($username) && !is_null($email)) {
  676. $user = new PMF_User_CurrentUser($faqConfig);
  677. $loginExist = $user->getUserByLogin($username);
  678. if ($loginExist && ($email == $user->getUserData('email'))) {
  679. $consonants = array(
  680. 'b','c','d','f','g','h','j','k','l','m','n','p','r','s','t','v','w','x','y','z'
  681. );
  682. $vowels = array(
  683. 'a','e','i','o','u'
  684. );
  685. $newPassword = '';
  686. srand((double)microtime()*1000000);
  687. for ($i = 1; $i <= 4; $i++) {
  688. $newPassword .= $consonants[rand(0,19)];
  689. $newPassword .= $vowels[rand(0,4)];
  690. }
  691. $user->changePassword($newPassword);
  692. $text = $PMF_LANG['lostpwd_text_1']."\nUsername: ".$username."\nNew Password: ".$newPassword."\n\n".$PMF_LANG["lostpwd_text_2"];
  693. $mail = new PMF_Mail($faqConfig);
  694. $mail->addTo($email);
  695. $mail->subject = '[%sitename%] Username / password request';
  696. $mail->message = $text;
  697. $result = $mail->send();
  698. unset($mail);
  699. // Trust that the email has been sent
  700. $message = array('success' => $PMF_LANG['lostpwd_mail_okay']);
  701. } else {
  702. $message = array('error' => $PMF_LANG['lostpwd_err_1']);
  703. }
  704. } else {
  705. $message = array('error' => $PMF_LANG['lostpwd_err_2']);
  706. }
  707. break;
  708. }
  709. $response->setData($message)->send();