PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyfaq/ajaxservice.php

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