PageRenderTime 60ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/php/comment.php

https://gitlab.com/billyprice1/source
PHP | 289 lines | 213 code | 70 blank | 6 comment | 32 complexity | 30ec4483525ec2a2560db5b93905777c MD5 | raw file
  1. <?php
  2. require "common.php";
  3. function translate($data) {
  4. // Based on
  5. // http://ankwebprogramming.wordpress.com/2011/10/08/translating-text-using-the-google-translate-api-and-php-json-and-curl/
  6. // Kartik Rangholiya
  7. global $privateconfig;
  8. $ENDPOINT = 'https://www.googleapis.com/language/translate/v2';
  9. $values = array(
  10. 'key' => $privateconfig["google_translate"]["key"],
  11. 'target' => $privateconfig["google_translate"]["language"],
  12. 'q' => $data
  13. );
  14. $formData = http_build_query($values);
  15. $ch = curl_init($ENDPOINT);
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
  18. curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
  19. $json = curl_exec($ch);
  20. curl_close($ch);
  21. $data = json_decode($json, true);
  22. if (!is_array($data) || !array_key_exists('data', $data)) {
  23. return "n/a";
  24. }
  25. if (!array_key_exists('translations', $data['data'])) {
  26. return "n/a";
  27. }
  28. if (!is_array($data['data']['translations'])) {
  29. return "n/a";
  30. }
  31. foreach ($data['data']['translations'] as $translation) {
  32. return $translation['translatedText'];
  33. }
  34. return "n/a";
  35. }
  36. function chunk_of_text($title,$chunk,$translate) {
  37. global $mirrorconfig;
  38. global $privateconfig;
  39. $chunk_e = htmlentities($chunk);
  40. $html = "<div>";
  41. $html .= " <div class=heading><span style='background-color: black; color: white; width: 100%;'>$title</span></div>";
  42. $html .= " <div class=userinput><pre><code style='font-size: 125%'>$chunk_e</code></pre></div>";
  43. if ($translate) {
  44. if ($privateconfig["google_translate"]["enable"]) {
  45. $chunk = translate($chunk);
  46. $html .= chunk_of_text("$title (translated)",$chunk,0);
  47. }
  48. }
  49. return $html;
  50. }
  51. function chunk_of_hash($title,$json) {
  52. global $mirrorconfig;
  53. global $privateconfig;
  54. $r = json_decode($json,true);
  55. $y = yaml_emit($r);
  56. return chunk_of_text($title,$y,0);
  57. }
  58. function chunk_of_results($title,$json) {
  59. global $mirrorconfig;
  60. global $privateconfig;
  61. $r = json_decode($json,true);
  62. $n = Array();
  63. $n["tests"] = Array();
  64. foreach ($r["tests"] as $key=>$array) {
  65. $n["tests"][$key] = $array["status"] . " " . sprintf('%.03f',$array["time_ms"]/1000.0);
  66. }
  67. $n["tokens"]=$r["tokens"];
  68. $y=yaml_emit($n);
  69. return chunk_of_text($title,$y,0);
  70. }
  71. function get_css($filename) {
  72. $buf = file_get_contents($filename);
  73. if (strlen($buf)) {
  74. return "<style>$buf</style>";
  75. } else {
  76. return "";
  77. }
  78. }
  79. function store_data_html() {
  80. global $mirrorconfig;
  81. global $privateconfig;
  82. $cookie = fetch_cookie(); # Validated for certain safety measures.
  83. $tokens = param_val("tokens","/^[a-zA-Z0-9 ,]+\$/");
  84. if ($_POST["nobots"] != "serious") {
  85. header("HTTP/1.1 500 Internal server error");
  86. print htmlentities("nobots value wrong, received \"". $_POST["nobots"] . "\"");
  87. exit(1);
  88. }
  89. if ($_POST["purpose"] == "-") {
  90. header("HTTP/1.1 400 Bad Request");
  91. print htmlentities("'purpose' must be specified to post this comment.");
  92. exit(1);
  93. }
  94. $base = htmlentities($_POST["subdomain"]);
  95. # $css = get_css("index.css");
  96. $message = <<<HEREDOC
  97. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  98. <html>
  99. <head>
  100. <base href="http://$base">
  101. <title>feedback</title>
  102. <link REL="SHORTCUT ICON" HREF="http://test-ipv6.com/images/favicon.ico">
  103. <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  104. </head>
  105. <body>
  106. HEREDOC;
  107. $message .= "<div>site name: $base</div>";
  108. $message .= "<div>";
  109. $message .= $_POST["form_tab_main"];
  110. $message .= "</div>";
  111. $ua = $_SERVER["HTTP_USER_AGENT"];
  112. $message .= chunk_of_text("user agent",$ua,0);
  113. $message .= "<hr/>";
  114. $purpose = "notes";
  115. if (preg_match('/^[a-z0-9 -]+$/i',$_POST["purpose"])) {
  116. $purpose = $_POST["purpose"];
  117. }
  118. $message .= chunk_of_text($purpose,$_POST["notes"],1);
  119. $message.= chunk_of_text("comments",$_POST["comments"],0);
  120. $message .= chunk_of_results("results",$_POST["form_results"],0);
  121. $message .= chunk_of_hash("config",$_POST["form_config"],0);
  122. $contact = trim($_POST["contact"]);
  123. $result = filter_var($contact, FILTER_VALIDATE_EMAIL);
  124. if ($result) {
  125. $replyto = "Reply-to: $contact\r\n";
  126. } else {
  127. $replyto = ""; $contact="";
  128. }
  129. $to = $mirrorconfig["site"]["mailto"];
  130. $subject = $mirrorconfig["site"]["name"] . " feedback [$contact]";
  131. $headers = $replyto . "Content-type: text/html; charset=utf-8";
  132. $headers = $headers . "\nContent-Transfer-Encoding: base64";
  133. $message64 = base64_encode($message);
  134. mail($to,$subject,$message64,$headers);
  135. print "Feedback sent; thank you for your assistance.<p>";
  136. print "If you included contact details, you may be contacted for further information, by " . $mirrorconfig["site"]["contact"] . "</hr>";
  137. }
  138. function store_data_text() {
  139. global $mirrorconfig;
  140. global $privateconfig;
  141. $cookie = fetch_cookie(); # Validated for certain safety measures.
  142. $tokens = param_val("tokens","/^[a-zA-Z0-9 ,]+\$/");
  143. if ($_POST["nobots"] != "serious") {
  144. header("HTTP/1.1 500 Internal server error");
  145. print htmlentities("nobots value wrong, received \"". $_POST["nobots"] . "\"");
  146. exit(1);
  147. }
  148. if ($_POST["purpose"] == "-") {
  149. header("HTTP/1.1 400 Bad Request");
  150. print htmlentities("'purpose' must be specified to post this comment.");
  151. exit(1);
  152. }
  153. $message = sprintf("%-15s: %s\n", "contact", $_POST["contact"]);
  154. $message .= sprintf("%-15s: %s\n", "purpose", $_POST["purpose"]);
  155. $message .= sprintf("%-15s: %s\n", "tokens", $_POST["tokens"]);
  156. $message .= sprintf("%-15s: %s\n", "score_transition", $_POST["score_transition"]);
  157. $message .= sprintf("%-15s: %s\n", "score_strict", $_POST["score_strict"]);
  158. $message .= sprintf("%-15s: %s\n", "a", $_POST["a"]);
  159. $message .= sprintf("%-15s: %s\n", "aaaa", $_POST["aaaa"]);
  160. if (preg_match("/ok|slow/",$_POST["ds4"])) {
  161. $message .= sprintf("%-15s: %s (via ipv4)\n", "ds", $_POST["ds4"]);
  162. } else if (preg_match("/ok|slow/",$_POST["ds6"])) {
  163. $message .= sprintf("%-15s: %s (via ipv6)\n", "ds", $_POST["ds6"]);
  164. } else {
  165. $message .= sprintf("%-15s: %s (via ipv4)\n", "ds", $_POST["ds4"]);
  166. $message .= sprintf("%-15s: %s (via ipv6)\n", "ds", $_POST["ds6"]);
  167. }
  168. $message .= sprintf("%-15s: %s\n", "dsmtu", $_POST["dsmtu"]);
  169. $message .= sprintf("%-15s: %s\n", "ipv4", $_POST["ipv4"]);
  170. $message .= sprintf("%-15s: %s\n", "ipv6", $_POST["ipv6"]);
  171. $message .= sprintf("%-15s: %s\n", "v6mtu", $_POST["v6mtu"]);
  172. $message .= sprintf("%-15s: %s\n", "v6ns", $_POST["v6ns"]);
  173. $message .= "----------------------------\n";
  174. $message .= sprintf("%-15s: %s\n", "ip4", $_POST["ip4"]);
  175. $message .= sprintf("%-15s: %s %s\n", "ip6", $_POST["ip6"], $_POST["ip6subtype"]);
  176. $message .= sprintf("%-15s: %s\n", "remote_addr", remote_addr());
  177. $message .= sprintf("%-15s: %s\n", "user-agent", $_SERVER["HTTP_USER_AGENT"]);
  178. $message .= sprintf("%-15s: %s\n", "referer", $_SERVER["HTTP_REFERER"]);
  179. $message .= sprintf("%-15s: %s\n", "subdomain", $_POST["subdomain"]);
  180. $message .= "\n\nNotes\n--------------\n" . $_POST["notes"];
  181. if ($privateconfig["google_translate"]["enable"]) {
  182. $message .= "\n\nTranslated\n---------\n" . translate( $_POST["notes"]);
  183. }
  184. $message .= "\n\nComments\n--------------\n" . $_POST["comments"];
  185. # print_r($_SERVER);
  186. $charset = "UTF-8";
  187. # header("Content-type: text/html; charset=$charset");
  188. ?>
  189. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  190. <html>
  191. <head>
  192. <title>Test your IPv6.</title>
  193. <link REL="SHORTCUT ICON" HREF="http://test-ipv6.com/images/favicon.ico">
  194. <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  195. </head>
  196. <body>
  197. <?php
  198. $contact = trim($_POST["contact"]);
  199. $result = filter_var($contact, FILTER_VALIDATE_EMAIL);
  200. if ($result) {
  201. $replyto = "Reply-to: $contact\r\n";
  202. } else {
  203. $replyto = ""; $contact="";
  204. }
  205. mail($mirrorconfig["site"]["mailto"],$mirrorconfig["site"]["name"] . " feedback [$contact]", $message,$replyto . "Content-type: text/plain; charset=$charset");
  206. print "Feedback sent; thank you for your assistance.<p>";
  207. print "If you included contact details, you may be contacted for further information, by " . $mirrorconfig["site"]["contact"] . "</hr>";
  208. print "<pre><code>";
  209. print "To: " . $mirrorconfig["site"]["mailto"] . "\n\n";
  210. print htmlentities($message,ENT_COMPAT,"UTF-8");
  211. print "</code></pre>";
  212. print "</body>";
  213. }
  214. if ($mirrorconfig["options"]["comment_html"]) {
  215. store_data_html();
  216. } else {
  217. store_data_text();
  218. }
  219. ?>