PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/php/comments.php

https://github.com/davidturner/Wee_
PHP | 209 lines | 176 code | 27 blank | 6 comment | 51 complexity | 5db6cca6a00485705c0d0b920b632232 MD5 | raw file
  1. <?php
  2. if($site->akismet != ''){
  3. $WordPressAPIKey = $site->akismet;
  4. $MyBlogURL = $site->url;
  5. $akismet = new Akismet($site->url ,$site->akismet);
  6. }
  7. $comments->post = 'categories'.$site->query;
  8. $comments->dir = $comments->post.'comments/';
  9. $md = new Markdownify_Extra;
  10. $post['name'] = '';
  11. $post['email'] = '';
  12. $post['site-url'] = '';
  13. $post['comment'] = '';
  14. $commentMsg = '';
  15. $allow = array('important-input');
  16. if(count(array_filter($_POST)) == 0){
  17. $site->process = 0;
  18. } else {
  19. foreach ($_POST as $key => $value) {
  20. $post[$key] = trim($value);
  21. if($value == '' && !in_array($key, $allow)) { $site->process = 0; }
  22. }
  23. if($key == 'important-input' && $value != ''){
  24. $site->process = 0;
  25. }
  26. }
  27. if($site->process){
  28. $akismet->setCommentAuthor($post["name"]);
  29. $akismet->setCommentAuthorEmail($post["email"]);
  30. $akismet->setCommentAuthorURL($post["site-url"]);
  31. $akismet->setCommentContent($post["comment"]);
  32. $akismet->setPermalink($site->url.$site->query);
  33. $finalcomment["name"] = strip_tags($post["name"]);
  34. $finalcomment["email"] = strip_tags($post["email"]);
  35. $finalcomment["gravatar-url"] = "http://www.gravatar.com/avatar/".md5($finalcomment["email"]).'?d='.urlencode( $site->url.'/avatars/unknown-commenter.png' );
  36. ;
  37. $finalcomment["site-url"] = strip_tags($post["site-url"]);
  38. $finalcomment["comment"] = '<p>'.str_replace("\n\n", '</p><p>', $post["comment"]).'</p>';
  39. $finalcomment["comment"] = str_replace("\n", '<br />', $post["comment"]);
  40. $finalcomment["comment"] = $md->parseString($finalcomment["comment"]);
  41. if($akismet->isCommentSpam()){
  42. // Probably Spam, but better to keep it just in case. Flag spam files in the naming structure too (spam.date.md)
  43. $finalcomment["flagged"] = "true";
  44. $commentMsg = '<p class="comment-flagged">Unfortunately your comment has been flagged as spam. The comment has still been saved, but won\'t show until it is determined to be an actual comment.</p>';
  45. }else{
  46. $finalcomment["flagged"] = "false";
  47. $commentMsg = '<p class="comment-success">Your post has been successfully added. Check it out above.</p>';
  48. $site->purge = 'cache/'.str_replace("/","-",substr($site->query, 1, -1)).'.html';
  49. }
  50. $time = time();
  51. $finalcomment["comment"] = str_replace("<","&lt;",str_replace(">", "&gt;", stripslashes($finalcomment["comment"])));
  52. $finalcomment["comment"] = str_replace('&lt;a', '<a', $finalcomment["comment"]);
  53. $finalcomment["comment"] = str_replace('&lt;/a&gt;','</a>',$finalcomment["comment"]);
  54. $finalcomment["comment"] = str_replace('"&gt;', '">', $finalcomment["comment"]);
  55. $finalcomment["comment"] = str_replace('&lt;strong&gt;', '<strong>', $finalcomment["comment"]);
  56. $finalcomment["comment"] = str_replace('&lt;/strong&gt;', '</strong>', $finalcomment["comment"]);
  57. $finalcomment["comment"] = str_replace('&lt;b&gt;', '<b>', $finalcomment["comment"]);
  58. $finalcomment["comment"] = str_replace('&lt;/b&gt;', '</b>', $finalcomment["comment"]);
  59. $finalcomment["comment"] = str_replace('&lt;em&gt;', '<em>', $finalcomment["comment"]);
  60. $finalcomment["comment"] = str_replace('&lt;/em&gt;', '</em>', $finalcomment["comment"]);
  61. $finalcomment["comment"] = str_replace('&lt;i&gt;', '<i>', $finalcomment["comment"]);
  62. $finalcomment["comment"] = str_replace('&lt;/i&gt;', '</i>', $finalcomment["comment"]);
  63. $finalcomment["comment"] = str_replace('&lt;del&gt;', '<del>', $finalcomment["comment"]);
  64. $finalcomment["comment"] = str_replace('&lt;/del&gt;', '</del>', $finalcomment["comment"]);
  65. $finalcomment["comment"] = str_replace('&lt;ins&gt;', '<ins>', $finalcomment["comment"]);
  66. $finalcomment["comment"] = str_replace('&lt;/ins&gt;', '</ins>', $finalcomment["comment"]);
  67. $finalcomment["comment"] = str_replace('&lt;blockquote&gt;', '<blockquote>', $finalcomment["comment"]);
  68. $finalcomment["comment"] = str_replace('&lt;/blockquote&gt;', '</blockquote>', $finalcomment["comment"]);
  69. $finalcomment["comment"] = str_replace('&lt;pre&gt;', '<pre>', $finalcomment["comment"]);
  70. $finalcomment["comment"] = str_replace('&lt;/pre&gt;', '</pre>', $finalcomment["comment"]);
  71. $finalcomment["comment"] = str_replace('&lt;code&gt;', '<code>', $finalcomment["comment"]);
  72. $finalcomment["comment"] = str_replace('&lt;/code&gt;', '</code>', $finalcomment["comment"]);
  73. $finalcomment["comment"] = str_replace('&lt;cite&gt;', '<cite>', $finalcomment["comment"]);
  74. $finalcomment["comment"] = str_replace('&lt;/cite&gt;', '</cite>', $finalcomment["comment"]);
  75. $finalcomment["comment"] = str_replace('&lt;q&gt;', '<q>', $finalcomment["comment"]);
  76. $finalcomment["comment"] = str_replace('&lt;/q&gt;', '</q>', $finalcomment["comment"]);
  77. $fullcomment = "Comment Author: ".$finalcomment["name"]."\n";
  78. $fullcomment .= "Email: ".$finalcomment["email"]."\n";
  79. $fullcomment .= "Gravatar: ".$finalcomment["gravatar-url"]."\n";
  80. $fullcomment .= "URL: ".$finalcomment["site-url"]."\n";
  81. $fullcomment .= "Posted on: ".$time."\n";
  82. $fullcomment .= "No-Follow: true\n";
  83. $fullcomment .= "Flagged: ".$finalcomment["flagged"]."\n";
  84. $fullcomment .= "=-=-=\n";
  85. $fullcomment .= $finalcomment["comment"];
  86. if($finalcomment["flagged"] == "true"){
  87. $file = $comments->dir."spam-".$time.".md";
  88. }else{
  89. $file = $comments->dir.$time.".md";
  90. }
  91. if($finalcomment["flagged"] == "true" && $site->comments == 'all' || $finalcomment["flagged"] == "false"){
  92. $handle = fopen($file, 'w') or die('Cannot open file: '.$file);
  93. fwrite($handle, $fullcomment);
  94. $commenttxt = "<h2>Comment submitted on your site!</h2><hr />";
  95. $commenttxt .= "<p>Name: ".$finalcomment["name"]."<br />Email: ".$finalcomment["email"]."<br />Comment: </p>";
  96. $commenttxt .= Markdown($finalcomment["comment"]);
  97. $commenttxt .= '<p>You can view this comment on your site <a href="'.$site->url.$site->query.'#comment-'.$time.'">here</a>.</p>';
  98. $to = $site->author->email;
  99. $subject = "Comment posted by ".$finalcomment["name"];
  100. $message = "<html><body>".$commenttxt."</body></html>";
  101. $headers = 'From: '.$finalcomment["name"].' <'.$finalcomment["email"].'> '."\n";
  102. $headers .= 'Reply-To: '.$finalcomment["email"].''."\n";
  103. $headers .= "MIME-Version: 1.0\n";
  104. $headers .= "Content-Type: text/html; charset=ISO-8859-1\n";
  105. $headers .= 'X-Mailer: PHP/' . phpversion();
  106. mail($to, $subject, $message, $headers);
  107. unlink($site->cachefile);
  108. header('Location: '.$site->query.'#comment-'.$time);
  109. die;
  110. }
  111. }
  112. # If comments folder doesn't exist, make it. Make it naow!
  113. if(!is_dir($comments->dir) && is_dir($comments->post)){
  114. mkdir($comments->dir, 0777);
  115. }
  116. $comments->exist = 0;
  117. $comments->comments = scandir($comments->dir);
  118. //print_r($comments->comments);
  119. echo '<section id="comments-area">';
  120. if(!isset($page->closecomments) && !isset($site->closecomments) && $site->closecomments == 1){
  121. echo '<h1>Comments</h1>';
  122. }
  123. foreach($comments->comments as $comment){
  124. if($comment != '.' && $comment != '..' && $comment != '.DS_Store' && !strlen(strstr($comment,"spam"))>0){
  125. $commentText = $comments->dir.$comment;
  126. if(file_exists($commentText)){
  127. $comments->exist = 1;
  128. $singleComment = getComment($commentText);
  129. if($singleComment["URL"] != ""){
  130. $commenter = '<a href="'.$singleComment["URL"].'">'.$singleComment["Comment Author"].'</a>';
  131. if($singleComment["No-Follow"] == "true"){
  132. $commenter = str_replace("<a", '<a rel="nofollow"', $commenter);
  133. }
  134. }else{
  135. $commenter = $singleComment["Comment Author"];
  136. }
  137. if($singleComment["No-Follow"] == "true"){
  138. $singleComment["post"] = str_replace("<a ", '<a rel="nofollow" ', $singleComment["post"]);
  139. }
  140. echo '<article class="post-comment clearfix" id="comment-'.$singleComment["Posted on"].'">'."\n";
  141. echo '<img class="gravatar" src="'.$singleComment["Gravatar"].'" alt="'.$singleComment["Comment Author"].'\'s Avatar" />'."\n";
  142. echo '<div class="comment-content">'."\n";
  143. echo '<p>Comment by '.$commenter.' on <time datetime="'.date("c",$singleComment["Posted on"]).'">'.date('F \t\h\e jS, Y',$singleComment["Posted on"]).' at '.date("g:ia",$singleComment["Posted on"]).'</time></p>'."\n";
  144. echo $singleComment["post"].'</div>'."\n";
  145. echo '</article>'."\n\n";
  146. }
  147. }
  148. }
  149. if(!$comments->exist && !isset($page->closecomments) && !isset($site->closecomments) && $site->closecomments == 1){
  150. echo '<p>No comments have been made on this article just yet. Would you like yours to <a href="#comment-form">be the first</a>?</p>';
  151. }
  152. /*
  153. Comment Form pl0x
  154. */
  155. if(!isset($page->closecomments) && !$site->closecomments){ ?>
  156. <h2>Have Your Say</h2>
  157. <form id="comment-form" action="#comment-form" method="post">
  158. <?=$commentMsg;?>
  159. <div class="commenter-data">
  160. <input type="hidden" name="important-input" id="important-input" />
  161. <label for="name">Name (required):</label>
  162. <input type="text" id="name" name="name" placeholder="Your Name..." value="<?php if(!$site->process){ echo $post["name"];} ?>" required />
  163. <label for="email">Email Address (required, never shared):</label>
  164. <input type="email" id="email" name="email" placeholder="Your Email Address..." value="<?php if(!$site->process){ echo $post["email"];} ?>" required />
  165. <label for="site-url">Site URL:</label>
  166. <input type="url" id="site-url" name="site-url" placeholder="Your Site's URL..." value="<?php if(!$site->process){ echo $post["site-url"];} ?>" />
  167. </div>
  168. <div class="comment-message">
  169. <label for="comment" class="comment-label">Your Comment (required, supports <a href="http://daringfireball.net/projects/markdown/syntax/">markdown</a> formatting):</label>
  170. <textarea name="comment" id="comment" cols="30" rows="10" placeholder="Your Comment..." required><?php if(!$site->process){ echo $post["comment"];} ?></textarea>
  171. <input type="submit" value="Add Comment" />
  172. </div>
  173. </form>
  174. <?php } elseif(isset($page->closecomments)) { ?>
  175. <h2>Comments are closed</h2>
  176. <p>Unfortunately comments on this post are now closed.</p>
  177. <?php } else { ?>
  178. <h2>Comments on this site are Closed</h2>
  179. <p>Comments have now been closed throughout the site. If you would like to share your thoughts on this post feel free to write something on your own site<?php if(isset($site->author->twitter)){ echo ' or get in touch with me on <a href="http://twitter.com/'.$site->author->twitter.'">twitter</a>'; } ?>.</p>
  180. <?php
  181. }
  182. echo '</section>';