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

/phpmyfaq/admin/record.add.php

https://github.com/cyrke/phpMyFAQ
PHP | 286 lines | 207 code | 38 blank | 41 comment | 28 complexity | 68862f4bde773a38272a03dff6f7af5a MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Adds a record in the database, handles the preview and checks for missing
  4. * category entries.
  5. *
  6. * PHP Version 5.3
  7. *
  8. * This Source Code Form is subject to the terms of the Mozilla Public License,
  9. * v. 2.0. If a copy of the MPL was not distributed with this file, You can
  10. * obtain one at http://mozilla.org/MPL/2.0/.
  11. *
  12. * @category phpMyFAQ
  13. * @package Administration
  14. * @author Thorsten Rinne <thorsten@phpmyfaq.de>
  15. * @copyright 2003-2012 phpMyFAQ Team
  16. * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
  17. * @link http://www.phpmyfaq.de
  18. * @since 2003-02-23
  19. */
  20. if (!defined('IS_VALID_PHPMYFAQ')) {
  21. header('Location: http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']));
  22. exit();
  23. }
  24. // Re-evaluate $user
  25. $user = PMF_User_CurrentUser::getFromSession($faqConfig);
  26. if ($permission['editbt']|| $permission['addbt']) {
  27. // FAQ data
  28. $dateStart = PMF_Filter::filterInput(INPUT_POST, 'dateStart', FILTER_SANITIZE_STRING);
  29. $dateEnd = PMF_Filter::filterInput(INPUT_POST, 'dateEnd', FILTER_SANITIZE_STRING);
  30. $question = PMF_Filter::filterInput(INPUT_POST, 'question', FILTER_SANITIZE_STRING);
  31. $categories = PMF_Filter::filterInputArray(INPUT_POST, array('rubrik' => array('filter' => FILTER_VALIDATE_INT,
  32. 'flags' => FILTER_REQUIRE_ARRAY)));
  33. $record_lang = PMF_Filter::filterInput(INPUT_POST, 'lang', FILTER_SANITIZE_STRING);
  34. $tags = PMF_Filter::filterInput(INPUT_POST, 'tags', FILTER_SANITIZE_STRING);
  35. $active = PMF_Filter::filterInput(INPUT_POST, 'active', FILTER_SANITIZE_STRING);
  36. $sticky = PMF_Filter::filterInput(INPUT_POST, 'sticky', FILTER_SANITIZE_STRING);
  37. $content = PMF_Filter::filterInput(INPUT_POST, 'answer', FILTER_SANITIZE_SPECIAL_CHARS);
  38. $keywords = PMF_Filter::filterInput(INPUT_POST, 'keywords', FILTER_SANITIZE_STRING);
  39. $author = PMF_Filter::filterInput(INPUT_POST, 'author', FILTER_SANITIZE_STRING);
  40. $email = PMF_Filter::filterInput(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
  41. $comment = PMF_Filter::filterInput(INPUT_POST, 'comment', FILTER_SANITIZE_STRING);
  42. $record_id = PMF_Filter::filterInput(INPUT_POST, 'id', FILTER_VALIDATE_INT);
  43. $solution_id = PMF_Filter::filterInput(INPUT_POST, 'solution_id', FILTER_VALIDATE_INT);
  44. $revision_id = PMF_Filter::filterInput(INPUT_POST, 'revision_id', FILTER_VALIDATE_INT);
  45. $changed = PMF_Filter::filterInput(INPUT_POST, 'changed', FILTER_SANITIZE_STRING);
  46. // Permissions
  47. $user_permission = PMF_Filter::filterInput(INPUT_POST, 'userpermission', FILTER_SANITIZE_STRING);
  48. $restricted_users = ('all' == $user_permission) ? -1 : PMF_Filter::filterInput(INPUT_POST, 'restricted_users', FILTER_VALIDATE_INT);
  49. $group_permission = PMF_Filter::filterInput(INPUT_POST, 'grouppermission', FILTER_SANITIZE_STRING);
  50. $restricted_groups = ('all' == $group_permission) ? -1 : PMF_Filter::filterInput(INPUT_POST, 'restricted_groups', FILTER_VALIDATE_INT);
  51. if (!isset($categories['rubrik'])) {
  52. $categories['rubrik'] = array();
  53. }
  54. if (!is_null($question) && !is_null($categories['rubrik'])) {
  55. // new entry
  56. $logging = new PMF_Logging($faqConfig);
  57. $logging->logAdmin($user, 'Beitragcreatesave');
  58. printf("<h2>%s</h2>\n", $PMF_LANG['ad_entry_aor']);
  59. $category = new PMF_Category($faqConfig, false);
  60. $category->setUser($currentAdminUser);
  61. $category->setGroups($currentAdminGroups);
  62. $tagging = new PMF_Tags($faqConfig);
  63. $recordData = array(
  64. 'lang' => $record_lang,
  65. 'active' => $active,
  66. 'sticky' => (!is_null($sticky) ? 1 : 0),
  67. 'thema' => html_entity_decode($question),
  68. 'content' => html_entity_decode($content),
  69. 'keywords' => $keywords,
  70. 'author' => $author,
  71. 'email' => $email,
  72. 'comment' => (!is_null($comment) ? 'y' : 'n'),
  73. 'date' => date('YmdHis'),
  74. 'dateStart' => (empty($dateStart) ? '00000000000000' : str_replace('-', '', $dateStart) . '000000'),
  75. 'dateEnd' => (empty($dateEnd) ? '99991231235959' : str_replace('-', '', $dateEnd) . '235959'),
  76. 'linkState' => '',
  77. 'linkDateCheck' => 0);
  78. // Add new record and get that ID
  79. $record_id = $faq->addRecord($recordData);
  80. if ($record_id) {
  81. // Create ChangeLog entry
  82. $faq->createChangeEntry($record_id, $user->getUserId(), nl2br($changed), $recordData['lang']);
  83. // Create the visit entry
  84. $visits = new PMF_Visits($faqConfig);
  85. $visits->add($record_id);
  86. // Insert the new category relations
  87. $faq->addCategoryRelations($categories['rubrik'], $record_id, $recordData['lang']);
  88. // Insert the tags
  89. if ($tags != '') {
  90. $tagging->saveTags($record_id, explode(',',$tags));
  91. }
  92. // Add user permissions
  93. $faq->addPermission('user', $record_id, $restricted_users);
  94. $category->addPermission('user', $categories['rubrik'], $restricted_users);
  95. // Add group permission
  96. if ($faqConfig->get('security.permLevel') != 'basic') {
  97. $faq->addPermission('group', $record_id, $restricted_groups);
  98. $category->addPermission('group', $categories['rubrik'], $restricted_groups);
  99. }
  100. printf('<p class="alert alert-success">%s</p>', $PMF_LANG['ad_entry_savedsuc']);
  101. // Open question answered
  102. $openQuestionId = PMF_Filter::filterInput(INPUT_POST, 'openQuestionId', FILTER_VALIDATE_INT);
  103. if (null !== $openQuestionId) {
  104. if ($faqConfig->get('records.enableDeleteQuestion')) { // deletes question
  105. $faq->deleteQuestion($openQuestionId);
  106. } else { // adds this faq record id to the related open question
  107. $faq->updateQuestionAnswer($openQuestionId, $record_id, $categories['rubrik'][0]);
  108. }
  109. $url = sprintf(
  110. '%s?action=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
  111. $faqConfig->get('main.referenceURL'),
  112. $categories['rubrik'][0],
  113. $record_id,
  114. $record_lang
  115. );
  116. $oLink = new PMF_Link($url, $faqConfig);
  117. // notify the user who added the question
  118. $notifyEmail = PMF_Filter::filterInput(INPUT_POST, 'notifyEmail', FILTER_SANITIZE_EMAIL);
  119. $notifyUser = PMF_Filter::filterInput(INPUT_POST, 'notifyUser', FILTER_SANITIZE_STRING);
  120. $notification = new PMF_Notification($faqConfig);
  121. $notification->sendOpenQuestionAnswered($notifyEmail, $notifyUser, $oLink->toString());
  122. }
  123. // Call Link Verification
  124. PMF_Helper_Linkverifier::linkOndemandJavascript($record_id, $recordData['lang']);
  125. // Callback to Twitter if enabled
  126. if ($faqConfig->get('socialnetworks.enableTwitterSupport')) {
  127. require '../inc/libs/twitteroauth/twitteroauth.php';
  128. $connection = new TwitterOAuth($faqConfig->get('socialnetworks.twitterConsumerKey'),
  129. $faqConfig->get('socialnetworks.twitterConsumerSecret'),
  130. $faqConfig->get('socialnetworks.twitterAccessTokenKey'),
  131. $faqConfig->get('socialnetworks.twitterAccessTokenSecret'));
  132. $link = PMF_Link::getSystemRelativeUri() .
  133. sprintf('?action=artikel&amp;cat=%d&amp;id=%d&amp;artlang=%s',
  134. $category,
  135. $record_id,
  136. $record_lang);
  137. $link = $faqConfig->get('main.referenceURL') . str_replace('/admin/','/', $link);
  138. $oLink = new PMF_Link($link, $faqConfig);
  139. $oLink->itemTitle = $question;
  140. $link = $oLink->toString();
  141. if ($connection) {
  142. $twitter = new PMF_Services_Twitter($connection);
  143. $twitter->addPost($question, $tags, $link);
  144. }
  145. }
  146. // All the other translations
  147. $languages = PMF_Filter::filterInput(INPUT_POST, 'used_translated_languages', FILTER_SANITIZE_STRING);
  148. if ($faqConfig->get('main.enableGoogleTranslation') === true && !empty($languages)) {
  149. $linkverifier = new PMF_Linkverifier($faqConfig, $user->getLogin());
  150. $languages = explode(",", $languages);
  151. foreach ($languages as $translated_lang) {
  152. if ($translated_lang == $record_lang) {
  153. continue;
  154. }
  155. $translated_question = PMF_Filter::filterInput(INPUT_POST, 'question_translated_' . $translated_lang, FILTER_SANITIZE_STRING);
  156. $translated_answer = PMF_Filter::filterInput(INPUT_POST, 'answer_translated_' . $translated_lang, FILTER_SANITIZE_SPECIAL_CHARS);
  157. $translated_keywords = PMF_Filter::filterInput(INPUT_POST, 'keywords_translated_' . $translated_lang, FILTER_SANITIZE_STRING);
  158. $recordData = array_merge($recordData, array(
  159. 'id' => $record_id,
  160. 'lang' => $translated_lang,
  161. 'thema' => html_entity_decode($translated_question),
  162. 'content' => html_entity_decode($translated_answer),
  163. 'keywords' => $translated_keywords,
  164. 'author' => 'Google Translate',
  165. 'email' => $faqConfig->get('main.administrationMail')));
  166. // Create ChangeLog entry
  167. $faq->createChangeEntry($record_id, $user->getUserId(), nl2br($changed), $translated_lang);
  168. // save or update the FAQ record
  169. if ($faq->isAlreadyTranslated($record_id, $translated_lang)) {
  170. $faq->updateRecord($recordData);
  171. } else {
  172. $faq->addRecord($recordData, false);
  173. }
  174. // delete category relations
  175. $faq->deleteCategoryRelations($record_id, $translated_lang);
  176. // save or update the category relations
  177. $faq->addCategoryRelations($categories['rubrik'], $record_id, $translated_lang);
  178. // Copy Link Verification
  179. $linkverifier->markEntry($record_id, $translated_lang);
  180. // add faqvisit entry
  181. $visits->add($record_id, $translated_lang);
  182. // Set attachment relations
  183. $attachments = PMF_Attachment_Factory::fetchByRecordId($faqConfig, $record_id);
  184. foreach ($attachments as $attachment) {
  185. if ($attachment instanceof PMF_Attachment_Abstract) {
  186. $attachment->setId(null);
  187. $attachment->setRecordLang($translated_lang);
  188. $attachment->saveMeta();
  189. }
  190. }
  191. }
  192. }
  193. ?>
  194. <script type="text/javascript">
  195. $(document).ready(function(){
  196. setTimeout(function() {
  197. window.location = "index.php?action=editentry&id=<?php print $record_id; ?>&lang=<?php print $recordData['lang'] ?>";
  198. }, 5000);
  199. });
  200. </script>
  201. <?php
  202. } else {
  203. printf(
  204. '<p class="alert alert-error">%s</p>',
  205. $PMF_LANG['ad_entry_savedfail'] . $faqConfig->getDb()->error()
  206. );
  207. }
  208. } else {
  209. printf("<h2>%s</h2>\n", $PMF_LANG['ad_entry_aor']);
  210. printf('<p class="alert alert-error">%s</p>', $PMF_LANG['ad_entryins_fail']);
  211. ?>
  212. <form action="?action=editpreview" method="post">
  213. <input type="hidden" name="question" value="<?php print PMF_String::htmlspecialchars($question); ?>" />
  214. <input type="hidden" name="content" class="mceNoEditor" value="<?php print PMF_String::htmlspecialchars($content); ?>" />
  215. <input type="hidden" name="lang" value="<?php print $record_lang; ?>" />
  216. <input type="hidden" name="keywords" value="<?php print $keywords; ?>" />
  217. <input type="hidden" name="tags" value="<?php print $tags; ?>" />
  218. <input type="hidden" name="author" value="<?php print $author; ?>" />
  219. <input type="hidden" name="email" value="<?php print $email; ?>" />
  220. <?php
  221. if (is_array($categories['rubrik'])) {
  222. foreach ($categories['rubrik'] as $key => $_categories) {
  223. print ' <input type="hidden" name="rubrik['.$key.']" value="'.$_categories.'" />';
  224. }
  225. }
  226. ?>
  227. <input type="hidden" name="solution_id" value="<?php print $solution_id; ?>" />
  228. <input type="hidden" name="revision" value="<?php print $revision_id; ?>" />
  229. <input type="hidden" name="active" value="<?php print $active; ?>" />
  230. <input type="hidden" name="changed" value="<?php print $changed; ?>" />
  231. <input type="hidden" name="comment" value="<?php print $comment; ?>" />
  232. <input type="hidden" name="dateStart" value="<?php print $dateStart; ?>" />
  233. <input type="hidden" name="dateEnd" value="<?php print $dateEnd; ?>" />
  234. <input type="hidden" name="userpermission" value="<?php print $user_permission; ?>" />
  235. <input type="hidden" name="restricted_users" value="<?php print $restricted_users; ?>" />
  236. <input type="hidden" name="grouppermission" value="<?php print $group_permission; ?>" />
  237. <input type="hidden" name="restricted_group" value="<?php print $restricted_groups; ?>" />
  238. <p align="center">
  239. <button class="btn btn-primary" type="submit" name="submit">
  240. <?php print $PMF_LANG['ad_entry_back']; ?>
  241. </button>
  242. </p>
  243. </form>
  244. <?php
  245. }
  246. } else {
  247. print $PMF_LANG['err_NotAuth'];
  248. }