PageRenderTime 57ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/campsite/src/admin-files/articles/comments/do_add_comment.php

https://github.com/joechrysler/Campsite
PHP | 160 lines | 117 code | 18 blank | 25 comment | 23 complexity | a1ad854a0ad59994ccf3910cd6b607f3 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. require_once($GLOBALS['g_campsiteDir']."/include/phorum_load.php");
  3. require_once($GLOBALS['g_campsiteDir'].'/classes/DbReplication.php');
  4. require_once($GLOBALS['g_campsiteDir'].'/classes/Input.php');
  5. require_once($GLOBALS['g_campsiteDir'].'/classes/Phorum_forum.php');
  6. require_once($GLOBALS['g_campsiteDir'].'/classes/Phorum_message.php');
  7. require_once($GLOBALS['g_campsiteDir'].'/classes/Phorum_user.php');
  8. require_once($GLOBALS['g_campsiteDir'].'/classes/ArticleComment.php');
  9. require_once($GLOBALS['g_campsiteDir']."/$ADMIN_DIR/pub/pub_common.php");
  10. $f_language_id = Input::Get('f_language_id', 'int', 0, true);
  11. $f_article_number = Input::Get('f_article_number', 'int', 0);
  12. $f_language_selected = Input::Get('f_language_selected', 'int', 0);
  13. $f_comment_nickname = Input::Get('f_comment_nickname', 'string', '', true);
  14. $f_comment_subject = Input::Get('f_comment_subject', 'string', '', true);
  15. $f_comment_body = Input::Get('f_comment_body');
  16. $f_comment_parent_id = Input::Get('f_comment_id', 'int', 0, true);
  17. if (!Input::IsValid()) {
  18. camp_html_display_error(getGS('Invalid input: $1', Input::GetErrorString()), $BackLink);
  19. exit;
  20. }
  21. // Check that the article exists.
  22. $articleObj = new Article($f_language_id, $f_article_number);
  23. if (!$articleObj->exists()) {
  24. exit;
  25. }
  26. if (!$articleObj->commentsEnabled() || $articleObj->commentsLocked()) {
  27. camp_html_goto_page(camp_html_article_url($articleObj, $f_language_selected, "edit.php"));
  28. }
  29. if (SystemPref::Get("UseDBReplication") == 'Y') {
  30. $dbReplicationObj = new DbReplication();
  31. $connectedToOnlineServer = $dbReplicationObj->connect();
  32. if ($connectedToOnlineServer == false) {
  33. camp_html_add_msg(getGS("Comments Disabled: you are either offline or not able to reach the Online server"));
  34. camp_html_goto_page(camp_html_article_url($articleObj, $f_language_selected, "edit.php"));
  35. }
  36. }
  37. // Add the user if it doesnt exist in the Phorum user table
  38. $phorumUser = new Phorum_user($g_user->getUserId());
  39. if (!$phorumUser->CampUserExists($g_user->getUserId())) {
  40. $success = $phorumUser->create($g_user->getUserName(),
  41. $g_user->getPassword(),
  42. $g_user->getEmail(),
  43. $g_user->getUserId());
  44. }
  45. // Get the forum ID.
  46. $publicationObj = new Publication($articleObj->getPublicationId());
  47. $forumId = $publicationObj->getForumId();
  48. // Exit if the forum hasnt been created (this should never happen).
  49. if (!$forumId) {
  50. camp_html_goto_page(camp_html_article_url($articleObj, $f_language_selected, "edit.php")."#add_comment");
  51. }
  52. // Create/get first post.
  53. $firstPost = camp_comment_first_post($articleObj, $forumId);
  54. // Exit if the forum hasnt been created (this should never happen).
  55. if (!$firstPost->exists()) {
  56. camp_html_goto_page(camp_html_article_url($articleObj, $f_language_selected, "edit.php")."#add_comment");
  57. }
  58. $threadId = $firstPost->getThreadId();
  59. // If reply isnt specified, then its a reply to the base message.
  60. if ($f_comment_parent_id == 0) {
  61. $f_comment_parent_id = $firstPost->getMessageId();
  62. }
  63. // Create the comment
  64. $commentObj = new Phorum_message();
  65. $commentObj->create($forumId,
  66. $f_comment_subject,
  67. $f_comment_body,
  68. $threadId,
  69. $f_comment_parent_id,
  70. $f_comment_nickname,
  71. $g_user->getEmail(),
  72. $g_user->getUserId());
  73. $commentObj->setStatus(PHORUM_STATUS_APPROVED);
  74. // Link the message to the article
  75. $isFirstMessage = ($threadId == 0);
  76. ArticleComment::Link($f_article_number, $f_language_id, $commentObj->getMessageId(), $isFirstMessage);
  77. camp_html_goto_page(camp_html_article_url($articleObj, $f_language_selected, "edit.php")."#add_comment");
  78. /**
  79. * Create the first message for an article, which is a blank message
  80. * with the title of the article as the subject.
  81. *
  82. * @param Article $p_article
  83. * @param int $p_forumId
  84. * @return mixed
  85. * The comment created (or the one that already exists) on success,
  86. * or false on error.
  87. */
  88. function camp_comment_first_post($p_article, $p_forumId)
  89. {
  90. // Check if the first post already exists.
  91. $articleNumber = $p_article->getArticleNumber();
  92. $languageId = $p_article->getLanguageId();
  93. $firstPostId = ArticleComment::GetCommentThreadId($articleNumber, $languageId);
  94. if ($firstPostId) {
  95. $firstPost = new Phorum_message($firstPostId);
  96. if ($firstPost->exists()) {
  97. return $firstPost;
  98. }
  99. // Unlink the message from the current article.
  100. ArticleComment::Unlink($articleNumber, $languageId, $firstPostId);
  101. }
  102. // Get article creator
  103. $user = new User($p_article->getCreatorId());
  104. if ($user->exists()) {
  105. $userId = $user->getUserId();
  106. $userEmail = $user->getEmail();
  107. $userPasswd = $user->getPassword();
  108. $userName = $user->getUserName();
  109. $userRealName = $user->getRealName();
  110. // Create phorum user if necessary
  111. $phorumUser = Phorum_user::GetByUserName($userName);
  112. if (!is_object($phorumUser)) {
  113. $phorumUser = new Phorum_user();
  114. }
  115. if (!$phorumUser->CampUserExists($userId)
  116. && !$phorumUser->create($userName, $userPasswd, $userEmail, $userId)) {
  117. return false;
  118. }
  119. } else {
  120. $userId = null;
  121. $userEmail = '';
  122. $userRealName = '';
  123. }
  124. // Create the comment.
  125. $title = $p_article->getTitle();
  126. $commentObj = new Phorum_message();
  127. if ($commentObj->create($p_forumId,
  128. $title,
  129. '',
  130. 0,
  131. 0,
  132. $userRealName,
  133. $userEmail,
  134. is_null($userId) ? 0 : $userId)) {
  135. // Link the message to the current article.
  136. ArticleComment::Link($articleNumber, $languageId, $commentObj->getMessageId(), true);
  137. return $commentObj;
  138. } else {
  139. return false;
  140. }
  141. } // fn camp_comment_first_post
  142. ?>