phpmode.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php namespace HashOver;
  2. // Copyright (C) 2010-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. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class PHPMode
  27. {
  28. public $setup;
  29. public $html;
  30. public $locale;
  31. public $templater;
  32. public $markdown;
  33. public $comments;
  34. protected $trimTagRegexes = array (
  35. 'blockquote' => '/(<blockquote>)([\s\S]*?)(<\/blockquote>)/i',
  36. 'ul' => '/(<ul>)([\s\S]*?)(<\/ul>)/i',
  37. 'ol' => '/(<ol>)([\s\S]*?)(<\/ol>)/i'
  38. );
  39. protected $linkRegex = '/((http|https|ftp):\/\/[a-z0-9-@:;%_\+.~#?&\/=]+) {0,1}/i';
  40. protected $codeTagCount = 0;
  41. protected $codeTags = array ();
  42. protected $preTagCount = 0;
  43. protected $preTags = array ();
  44. protected $paragraphRegex = '/(?:\r\n|\r|\n){2}/';
  45. protected $lineRegex = '/(?:\r\n|\r|\n)/';
  46. public function __construct (Setup $setup, HTMLOutput $html, array $comments)
  47. {
  48. $this->setup = $setup;
  49. $this->html = $html;
  50. $this->locale = new Locale ($setup);
  51. $this->templater = new Templater ($setup->usage['mode'], $setup);
  52. $this->markdown = new Markdown ();
  53. $this->comments = $comments;
  54. }
  55. protected function fileFromPermalink ($permalink)
  56. {
  57. $file = substr ($permalink, 1);
  58. $file = str_replace ('r', '-', $file);
  59. $file = str_replace ('-pop', '', $file);
  60. return $file;
  61. }
  62. protected function replyCheck ($permalink)
  63. {
  64. if (empty ($_GET['hashover-reply'])) {
  65. return;
  66. }
  67. if ($_GET['hashover-reply'] === $permalink) {
  68. $file = $this->fileFromPermalink ($permalink);
  69. $form = new HTMLTag ('form', array (
  70. 'id' => 'hashover-reply-' . $permalink,
  71. 'class' => 'hashover-reply-form',
  72. 'method' => 'post',
  73. 'action' => $this->setup->httpScripts . '/postcomments.php'
  74. ));
  75. $form->innerHTML ($this->html->replyForm ($permalink, $file));
  76. return $form->asHTML ();
  77. }
  78. }
  79. protected function editCheck ($comment)
  80. {
  81. if (empty ($_GET['hashover-edit'])) {
  82. return;
  83. }
  84. $permalink = !empty ($comment['permalink']) ? $comment['permalink'] : '';
  85. if ($_GET['hashover-edit'] === $permalink) {
  86. $file = $this->fileFromPermalink ($permalink);
  87. $body = $comment['body'];
  88. $body = preg_replace ($this->linkRegex, '\\1', $body);
  89. $status = !empty ($comment['status']) ? $comment['status'] : 'approved';
  90. $name = !empty ($comment['name']) ? $comment['name'] : '';
  91. $website = !empty ($comment['website']) ? $comment['website'] : '';
  92. $subscribed = isset ($comment['subscribed']);
  93. $form = new HTMLTag ('form', array (
  94. 'id' => 'hashover-edit-' . $permalink,
  95. 'class' => 'hashover-edit-form',
  96. 'method' => 'post',
  97. 'action' => $this->setup->httpScripts . '/postcomments.php'
  98. ), false);
  99. $edit_form = $this->html->editForm ($permalink, $file, $name, $website, $body, $status, $subscribed);
  100. $form->innerHTML ($edit_form);
  101. return $form->asHTML ();
  102. }
  103. }
  104. protected function codeTagReplace ($grp)
  105. {
  106. $codePlaceholder = $grp[1] . 'CODE_TAG[' . $this->codeTagCount . ']' . $grp[3];
  107. $this->codeTags[$this->codeTagCount] = trim ($grp[2], "\r\n");
  108. $this->codeTagCount++;
  109. return $codePlaceholder;
  110. }
  111. protected function codeTagReturn ($grp) {
  112. return $this->codeTags[($grp[1])];
  113. }
  114. protected function preTagReplace ($grp)
  115. {
  116. $prePlaceholder = $grp[1] . 'PRE_TAG[' . $this->preTagCount . ']' . $grp[3];
  117. $this->preTags[$this->preTagCount] = trim ($grp[2], "\r\n");
  118. $this->preTagCount++;
  119. return $prePlaceholder;
  120. }
  121. protected function preTagReturn ($grp) {
  122. return $this->preTags[($grp[1])];
  123. }
  124. // Returns the permalink of a comment's parent
  125. protected function getParentPermalink ($permalink)
  126. {
  127. $permalink_parts = explode ('r', $permalink);
  128. array_pop ($permalink_parts);
  129. return implode ('r', $permalink_parts);
  130. }
  131. // Find a comment by its permalink
  132. protected function findByPermalink ($permalink, $comments)
  133. {
  134. // Loop through all comments
  135. foreach ($comments as $comment) {
  136. // Return comment if its permalink matches
  137. if ($comment['permalink'] === $permalink) {
  138. return $comment;
  139. }
  140. // Recursively check replies when present
  141. if (!empty ($comment['replies'])) {
  142. $reply = $this->findByPermalink ($permalink, $comment['replies']);
  143. if ($reply !== null) {
  144. return $reply;
  145. }
  146. }
  147. }
  148. // Otherwise return null
  149. return null;
  150. }
  151. public function parseComment (array $comment, $parent = null, $popular = false)
  152. {
  153. $template = array ();
  154. $nameClass = 'hashover-name-plain';
  155. $permalink = $comment['permalink'];
  156. $template['permalink'] = $permalink;
  157. $is_reply = ($parent !== null);
  158. $this->codeTagCount = 0;
  159. $this->codeTags = array ();
  160. $this->preTagCount = 0;
  161. $this->preTags = array ();
  162. // Text for avatar image alt attribute
  163. $permatext = substr ($permalink, 1);
  164. $permatext = explode ('r', $permatext);
  165. $permatext = array_pop ($permatext);
  166. // Wrapper element for each comment
  167. $comment_wrapper = $this->html->commentWrapper ($permalink);
  168. // Get parent comment via permalink
  169. if ($is_reply === false and strpos ($permalink, 'r') !== false) {
  170. $parent_permalink = $this->getParentPermalink ($permalink);
  171. $parent = $this->findByPermalink ($parent_permalink, $this->comments['comments']);
  172. $is_reply = ($parent !== null);
  173. }
  174. // Check if this comment is a popular comment
  175. if ($popular === true) {
  176. // Remove "-pop" from text for avatar
  177. $permatext = str_replace ('-pop', '', $permatext);
  178. } else {
  179. // Check if comment is a reply
  180. if ($is_reply === true) {
  181. // Append class to indicate comment is a reply
  182. $comment_wrapper->appendAttribute ('class', 'hashover-reply');
  183. }
  184. }
  185. // Add avatar image to template
  186. $template['avatar'] = $this->html->userAvatar ($permatext, $permalink, $comment['avatar']);
  187. if (!isset ($comment['notice'])) {
  188. $name = !empty ($comment['name']) ? $comment['name'] : $this->setup->defaultName;
  189. $is_twitter = false;
  190. // Check if user's name is a Twitter handle
  191. if ($name[0] === '@') {
  192. $name = mb_substr ($name, 1);
  193. $nameClass = 'hashover-name-twitter';
  194. $is_twitter = true;
  195. $nameLength = mb_strlen ($name);
  196. // Check if Twitter handle is valid length
  197. if ($nameLength > 1 and $nameLength <= 30) {
  198. // Set website to Twitter profile if a specific website wasn't given
  199. if (empty ($comment['website'])) {
  200. $comment['website'] = 'http://twitter.com/' . $name;
  201. }
  202. }
  203. }
  204. // Check whether user gave a website
  205. if (!empty ($comment['website'])) {
  206. if ($is_twitter === false) {
  207. $nameClass = 'hashover-name-website';
  208. }
  209. // If so, display name as a hyperlink
  210. $nameLink = $this->html->nameElement ('a', $name, $permalink, $comment['website']);
  211. } else {
  212. // If not, display name as plain text
  213. $nameLink = $this->html->nameElement ('span', $name, $permalink);
  214. }
  215. // Add "Top of Thread" hyperlink to template
  216. if ($is_reply === true) {
  217. $parent_name = !empty ($parent['name']) ? $parent['name'] : $this->setup->defaultName;
  218. $template['thread-link'] = $this->html->threadLink ($parent['permalink'], $parent['permalink'], $parent_name);
  219. }
  220. if (isset ($comment['user-owned'])) {
  221. // Append class to indicate comment is from logged in user
  222. $comment_wrapper->appendAttribute ('class', 'hashover-user-owned');
  223. // Define "Reply" link with original poster title
  224. $replyTitle = $this->locale->get ('commenter-tip');
  225. $replyClass = 'hashover-no-email';
  226. // Add "Reply" hyperlink to template
  227. if (!empty ($_GET['hashover-edit']) and $_GET['hashover-edit'] === $permalink) {
  228. $template['edit-link'] = $this->html->cancelLink ($permalink, 'edit');
  229. } else {
  230. $template['edit-link'] = $this->html->formLink ('edit', $permalink);
  231. }
  232. } else {
  233. // Check if commenter is subscribed
  234. if (isset ($comment['subscribed'])) {
  235. // If so, set subscribed title
  236. $replyTitle = $name . ' ' . $this->locale->text['subscribed-tip'];
  237. $replyClass = 'hashover-has-email';
  238. } else{
  239. // If not, set unsubscribed title
  240. $replyTitle = $name . ' ' . $this->locale->text['unsubscribed-tip'];
  241. $replyClass = 'hashover-no-email';
  242. }
  243. }
  244. // Get number of likes, append "Like(s)" locale
  245. if (isset ($comment['likes'])) {
  246. $plural = ($comment['likes'] === 1 ? 0 : 1);
  247. $likeCount = $comment['likes'] . ' ' . $this->locale->text['like'][$plural];
  248. } else {
  249. $likeCount = '';
  250. }
  251. // Add like count to HTML template
  252. $template['like-count'] = $this->html->likeCount ('likes', $permalink, $likeCount);
  253. // Get number of dislikes, append "Dislike(s)" locale
  254. if ($this->setup->allowsDislikes === true) {
  255. if (isset ($comment['dislikes'])) {
  256. $plural = ($comment['dislikes'] === 1 ? 0 : 1);
  257. $dislikeCount = $comment['dislikes'] . ' ' . $this->locale->text['dislike'][$plural];
  258. } else {
  259. $dislikeCount = '';
  260. }
  261. // Add dislike count to HTML template
  262. $template['dislike-count'] = $this->html->likeCount ('dislikes', $permalink, $dislikeCount);
  263. }
  264. // Add name HTML to template
  265. $template['name'] = $this->html->nameWrapper ($nameLink, $nameClass);
  266. // Add date permalink hyperlink to template
  267. $template['date'] = $this->html->dateLink ($permalink, $comment['date']);
  268. // Add "Reply" hyperlink to template
  269. if (!empty ($_GET['hashover-reply']) and $_GET['hashover-reply'] === $permalink) {
  270. $template['reply-link'] = $this->html->cancelLink ($permalink, 'reply', $replyClass);
  271. } else {
  272. $template['reply-link'] = $this->html->formLink ('reply', $permalink, $replyClass, $replyTitle);
  273. }
  274. // Add edit form HTML to template
  275. if (isset ($comment['user-owned'])) {
  276. $template['edit-form'] = $this->editCheck ($comment);
  277. }
  278. // Add reply form HTML to template
  279. $template['reply-form'] = $this->replyCheck ($permalink);
  280. // Add reply count to template
  281. if (!empty ($comment['replies'])) {
  282. $template['reply-count'] = count ($comment['replies']);
  283. if ($template['reply-count'] > 0) {
  284. if ($template['reply-count'] !== 1) {
  285. $template['reply-count'] .= ' ' . $this->locale->get ('replies');
  286. } else {
  287. $template['reply-count'] .= ' ' . $this->locale->get ('reply');
  288. }
  289. }
  290. }
  291. // Add comment data to template
  292. $template['comment'] = $comment['body'];
  293. // Remove [img] tags
  294. $template['comment'] = preg_replace ('/\[(img|\/img)\]/i', '', $template['comment']);
  295. // Add HTML anchor tag to URLs (hyperlinks)
  296. $template['comment'] = preg_replace ($this->linkRegex, '<a href="\\1" rel="noopener noreferrer" target="_blank">\\1</a>', $template['comment']);
  297. // Parse markdown in comment
  298. $template['comment'] = $this->markdown->parseMarkdown ($template['comment']);
  299. // Check for code tags
  300. if (mb_strpos ($template['comment'], '<code>') !== false) {
  301. // Replace code tags with placeholder text
  302. $template['comment'] = preg_replace_callback ('/(<code>)([\s\S]*?)(<\/code>)/i', 'self::codeTagReplace', $template['comment']);
  303. }
  304. // Check for pre tags
  305. if (mb_strpos ($template['comment'], '<pre>') !== false) {
  306. // Replace pre tags with placeholder text
  307. $template['comment'] = preg_replace_callback ('/(<pre>)([\s\S]*?)(<\/pre>)/i', 'self::preTagReplace', $template['comment']);
  308. }
  309. // Check for various multi-line tags
  310. foreach ($this->trimTagRegexes as $tag => $trimTagRegex) {
  311. if (mb_strpos ($template['comment'], '<' . $tag . '>') !== false) {
  312. // Trim leading and trailing whitespace
  313. $template['comment'] = preg_replace_callback ($trimTagRegex, function ($grp) {
  314. return $grp[1] . trim ($grp[2], "\r\n") . $grp[3];
  315. }, $template['comment']);
  316. }
  317. }
  318. // Break comment into paragraphs
  319. $paragraphs = preg_split ($this->paragraphRegex, $template['comment']);
  320. $pd_comment = '';
  321. for ($i = 0, $il = count ($paragraphs); $i < $il; $i++) {
  322. // Wrap comment in paragraph tag, replace single line breaks with break tags
  323. $pd_comment .= '<p>' . preg_replace ($this->lineRegex, '<br>', $paragraphs[$i]) . '</p>' . PHP_EOL;
  324. }
  325. // Replace code tag placeholders with original code tag HTML
  326. if ($this->codeTagCount > 0) {
  327. $pd_comment = preg_replace_callback ('/CODE_TAG\[([0-9]+)\]/', 'self::codeTagReturn', $pd_comment);
  328. }
  329. // Replace pre tag placeholders with original pre tag HTML
  330. if ($this->preTagCount > 0) {
  331. $pd_comment = preg_replace_callback ('/PRE_TAG\[([0-9]+)\]/', 'self::preTagReturn', $pd_comment);
  332. }
  333. // Add paragraph'd comment data to template
  334. $template['comment'] = $pd_comment;
  335. } else {
  336. // Append notice class
  337. $comment_wrapper->appendAttribute ('class', 'hashover-notice');
  338. $comment_wrapper->appendAttribute ('class', $comment['notice-class']);
  339. // Add notice to template
  340. $template['comment'] = $comment['notice'];
  341. // Set name to 'Comment Deleted!'
  342. $template['name'] = $this->html->nameWrapper ($comment['title'], $nameClass);
  343. }
  344. // Comment HTML template
  345. $comment_wrapper->innerHTML ($this->templater->parseTemplate ($template));
  346. // Recursively parse replies
  347. if (!empty ($comment['replies'])) {
  348. foreach ($comment['replies'] as $reply) {
  349. $comment_wrapper->appendInnerHTML ($this->parseComment ($reply, $comment));
  350. }
  351. }
  352. return $comment_wrapper->asHTML ();
  353. }
  354. }