postcomments.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php namespace HashOver;
  2. // Copyright (C) 2015-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. }
  23. }
  24. // Do some standard HashOver setup work
  25. require ('standard-setup.php');
  26. require ('javascript-setup.php');
  27. require ('oop-setup.php');
  28. try {
  29. // Mode is based on whether request is AJAX
  30. $mode = isset ($_POST['ajax']) ? 'javascript' : 'php';
  31. $data = null;
  32. // Instantiate HashOver class
  33. $hashover = new \HashOver ($mode);
  34. $hashover->setup->setPageURL ('request');
  35. $hashover->setup->setPageTitle ('request');
  36. $hashover->initiate ();
  37. $hashover->finalize ();
  38. // Instantiate class for writing and editing comments
  39. $write_comments = new WriteComments (
  40. $hashover->setup,
  41. $hashover->readComments
  42. );
  43. // Various POST data actions
  44. $post_actions = array (
  45. 'login',
  46. 'logout',
  47. 'post',
  48. 'edit',
  49. 'delete'
  50. );
  51. // Execute an action (write/edit/login/etc)
  52. foreach ($post_actions as $action) {
  53. if (empty ($_POST[$action])) {
  54. continue;
  55. }
  56. switch ($action) {
  57. case 'login': {
  58. if ($hashover->setup->allowsLogin !== false) {
  59. $write_comments->login ();
  60. }
  61. break;
  62. }
  63. case 'logout': {
  64. $write_comments->logout ();
  65. break;
  66. }
  67. case 'post': {
  68. $data = $write_comments->postComment ();
  69. break;
  70. }
  71. case 'edit': {
  72. $data = $write_comments->editComment ();
  73. break;
  74. }
  75. case 'delete': {
  76. $write_comments->deleteComment ();
  77. break;
  78. }
  79. }
  80. break;
  81. }
  82. // Returns comment being saved as JSON
  83. if (isset ($_POST['ajax']) and is_array ($data)) {
  84. // Slit file into parts
  85. $file = $data['file'];
  86. $key_parts = explode ('-', $file);
  87. // Parsed comment data
  88. $comment = $data['comment'];
  89. $parsed = $hashover->commentParser->parse ($comment, $file, $key_parts);
  90. // Echo JSON array
  91. echo json_encode (array (
  92. 'comment' => $parsed,
  93. 'count' => $hashover->getCommentCount ()
  94. ));
  95. }
  96. } catch (\Exception $error) {
  97. $misc = new Misc (isset ($_POST['ajax']) ? 'json' : 'php');
  98. $misc->displayError ($error->getMessage ());
  99. }