PageRenderTime 29ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/editor/add_comment.php

https://github.com/lasseron/Saurus-CMS-Community-Edition
PHP | 404 lines | 262 code | 64 blank | 78 comment | 73 complexity | 5f88808d45c091d49c82851b3b8c73e5 MD5 | raw file
  1. <?php
  2. /**
  3. * This source file is is part of Saurus CMS content management software.
  4. * It is licensed under MPL 1.1 (http://www.opensource.org/licenses/mozilla1.1.php).
  5. * Copyright (C) 2000-2010 Saurused Ltd (http://www.saurus.info/).
  6. * Redistribution of this file must retain the above copyright notice.
  7. *
  8. * Please note that the original authors never thought this would turn out
  9. * such a great piece of software when the work started using Perl in year 2000.
  10. * Due to organic growth, you may find parts of the software being
  11. * a bit (well maybe more than a bit) old fashioned and here's where you can help.
  12. * Good luck and keep your open source minds open!
  13. *
  14. * @package SaurusCMS
  15. * @copyright 2000-2010 Saurused Ltd (http://www.saurus.info/)
  16. * @license Mozilla Public License 1.1 (http://www.opensource.org/licenses/mozilla1.1.php)
  17. *
  18. */
  19. ##############################
  20. # Add a comment into database
  21. # : is FORM action value for comment forms
  22. # : will redirect back to the calling page
  23. # : is independent script, not for including, new Site is generated
  24. ##############################
  25. global $site, $leht;
  26. ##############################
  27. # function big_string_remove
  28. function big_string_remove( $input ) {
  29. global $site;
  30. $limit = $site->CONF['comment_max_chars'] ? $site->CONF['comment_max_chars'] : 50;
  31. $output = "";
  32. $sybol = array ("(","{","[","]","}",")");
  33. for ( $i=0; $i<=strlen($input); $i++) {
  34. if ($input[$i]!= " ") {
  35. $y++;
  36. } else {
  37. $y=0;
  38. }
  39. if (in_array ($input[$i], $sybol)){
  40. $x++;
  41. } else {
  42. $x=0;
  43. }
  44. $output .= $input[$i];
  45. if ( $y >= $limit ) {
  46. $y=0;
  47. $output .= " ";
  48. }
  49. if ( $x >= $limit ) {
  50. $x=0;
  51. $output .= "\n";
  52. }
  53. }
  54. return $output;
  55. }
  56. # / function big_string_remove
  57. ##############################
  58. preg_match('/\/(admin|editor)\//i', $_SERVER["REQUEST_URI"], $matches);
  59. $class_path = $matches[1] == "editor" ? "../classes/" : "./classes/";
  60. include($class_path."port.inc.php");
  61. include($class_path."mail.class.php"); # for f-n encodeHeader()
  62. $site = new Site(array(
  63. on_debug=>0,
  64. ));
  65. if(!isset($_SESSION['keel']))
  66. {
  67. //no session started, prolly a bot, exit
  68. header('Location: index.php');
  69. exit;
  70. }
  71. if($site->CONF['allow_commenting'] == 0)
  72. {
  73. header('Location: index.php');
  74. exit;
  75. }
  76. if($site->CONF['check_for_captcha'] == 1)
  77. {
  78. if(isset($_SESSION['scms_captcha']) && is_array($_SESSION['scms_captcha']))
  79. {
  80. $captcha = array_keys($_SESSION['scms_captcha']);
  81. $captcha['name'] = $captcha[0];
  82. $captcha['text'] = $_SESSION['scms_captcha'][$captcha['name']];
  83. if(strtolower($_POST['captcha_'.$captcha['name']]) == strtolower($captcha['text']))
  84. {
  85. $capthca_check_failed = false;
  86. }
  87. else
  88. {
  89. $capthca_check_failed = true;
  90. }
  91. }
  92. else
  93. {
  94. $capthca_check_failed = true;
  95. }
  96. }
  97. unset($_SESSION['scms_captcha']);
  98. if($capthca_check_failed)
  99. {
  100. // let's save data from form to cookie if there is captcha error
  101. $error_data = $site->fdat['nimi'].'|'.$site->fdat['email'].'|'.$site->fdat['url'].'|'.$site->fdat['text'].'|'.$site->fdat['pealkiri'];
  102. setcookie("addcomment_captcha_error", $error_data);
  103. // or I know: to the session!
  104. $_SESSION['scms_last_comment'] = $site->fdat;
  105. if ($site->fdat['redirect_url'])
  106. {
  107. header('Location: '.urldecode(preg_replace("!\r|\n.*!s", "", $_POST['redirect_url'])).'&lisa_alert=2');
  108. exit;
  109. }
  110. else
  111. {
  112. //protocol check ...
  113. header('Location: '.(empty($_SERVER['HTTPS']) ? 'http://': 'https://').$site->CONF['hostname'].$site->CONF['wwwroot'].($site->in_editor?'/editor':'').'/?'.(($site->fdat['tpl'] || $site->fdat['c_tpl'])&&!$site->fdat['inserted_id']&&!$site->fdat['jump_to_parent']?'tpl='.$site->fdat['tpl'].'&c_tpl='.$site->fdat['c_tpl'].'&':'').'id='.$site->fdat['id'].'&lisa_alert=2');
  114. exit;
  115. }
  116. }
  117. $tyyp_id = 14;
  118. $site->debug->print_hash($site->fdat,1,"FDAT");
  119. $leht = new Leht(array(
  120. id => $site->fdat['id'] ? $site->fdat['id'] : $site->alias("rub_home_id"),
  121. ));
  122. $objekt = new Objekt(array(
  123. objekt_id => $site->fdat['id'],
  124. on_sisu=>1,
  125. ));
  126. if(!$objekt->objekt_id)
  127. {
  128. //redirect 404 lehele
  129. header('Location: index.php?id='.$site->alias(array('key' => '404error')));
  130. exit;
  131. }
  132. $obj_conf = new CONFIG($objekt->all['ttyyp_params']);
  133. if ($site->fdat['output_device'] == 'pda') {
  134. if (strlen($site->fdat['text']) < 2 || strlen($site->fdat['nimi']) < 2) {
  135. myRedirect($site->fdat['redirect_url']);
  136. exit;
  137. }
  138. $name = trim($site->user->all['firstname'] . ' ' . $site->user->all['lastname']);
  139. $nimi = trim($site->fdat['nimi']);
  140. if ($name != $nimi)
  141. $site->fdat['nimi'] .= ' (nimi muudetud)';
  142. }
  143. $already = 0;
  144. ############ get all parent object: trail
  145. $trail_objs = $leht->parents->list;
  146. #oldfor ($y=-1;$y>-10;$y--){
  147. $i = 0;
  148. foreach ($trail_objs as $i => $myobj) {
  149. # skip the first array element - itself
  150. //if($i == 0) { continue; }
  151. if (($myobj->all[ttyyp_id]==40 || $myobj->all[ttyyp_id]>1000) && !$already){
  152. $already=1;
  153. $par_rubobj = $myobj; # get parent section object
  154. ############################
  155. # CONFIGURATION PARAMETERS - reading parameters values of object
  156. $leht->debug->msg("PARAMS ".$par_rubobj->all[ttyyp_params]);
  157. $conf = new CONFIG($par_rubobj->all[ttyyp_params]);
  158. $faq_mode = ($conf->get("faq_mode") ? 1:0);
  159. $conf->debug->print_msg();
  160. if (!$faq_mode) {
  161. $leht->debug->msg("set default forum view");
  162. } else {
  163. $leht->debug->msg("set forum view to FAQ-mode");
  164. }
  165. # / CONFIGURATION PARAMETERS
  166. ############################
  167. };
  168. }
  169. ########################
  170. # if article then check if commenting is allowed for this article;
  171. # allow unlimited commenting for all other content objects (Bug #2656)
  172. if (($objekt->all[klass] == "artikkel" && $objekt->all['on_foorum']) || $objekt->all[klass] != "artikkel") {
  173. ########################
  174. # if access is allowed
  175. # Bug #2133
  176. if (!($objekt->all[klass] == "kommentaar" && $faq_mode && !$site->in_editor)){
  177. # kui FAQ-mode ja pole editor, siis pane avaldatud=NO (Bug #2133)
  178. if ($faq_mode && !$site->in_editor){
  179. $publish=0;
  180. } else {$publish=1;}
  181. # Kui admin vastab kirjale, siis teeme parent avaldatud:
  182. if ($faq_mode && $site->in_editor && $objekt->all[klass] == "kommentaar" && is_numeric($site->fdat['id'])){
  183. $sql = $site->db->prepare(
  184. "UPDATE objekt SET on_avaldatud=?, last_modified=? WHERE objekt_id=?",
  185. 1,
  186. time(),
  187. $site->fdat['id']
  188. );
  189. $sth = new SQL ($sql);
  190. $site->debug->msg($sth->debug->get_msgs());
  191. }
  192. #####################
  193. # insert into objekt:
  194. $sql = $site->db->prepare("INSERT INTO objekt (pealkiri, tyyp_id, on_avaldatud, keel, kesk, pealkiri_strip, sisu_strip, aeg, check_in, last_modified, created_user_id, created_user_name, created_time) values (?, ?, ?, ?, ?, ?, ?, ".$site->db->unix2db_datetime(time()).", ?, ?, ?, ?, ?)",
  195. big_string_remove(strip_tags($site->fdat['pealkiri'])),
  196. $tyyp_id,
  197. $publish,
  198. $site->keel,
  199. 0,
  200. big_string_remove(strip_tags($site->fdat['pealkiri'])),
  201. big_string_remove(strip_tags($site->fdat['text'])),
  202. time(),
  203. 0,
  204. $site->user->id,
  205. $site->user->name,
  206. date("Y-m-d H:i:s")
  207. );
  208. $sth = new SQL ($sql);
  209. $site->debug->msg($sth->debug->get_msgs());
  210. $id = $sth->insert_id;
  211. #####################
  212. # insert into objekt_objekt:
  213. $sql = "SELECT MAX(sorteering) FROM objekt_objekt";
  214. $sth = new SQL ($sql);
  215. $site->debug->msg($sth->debug->get_msgs());
  216. $sorteering=$sth->fetchsingle();
  217. $sql = $site->db->prepare("INSERT INTO objekt_objekt (objekt_id, parent_id, sorteering) VALUES (?,?,?)",
  218. $id,
  219. $site->fdat['id'],
  220. $sorteering+1
  221. );
  222. $sth = new SQL($sql);
  223. $site->debug->msg($sth->debug->get_msgs());
  224. $name = big_string_remove(strip_tags($site->fdat['nimi']));
  225. $email = strip_tags($site->fdat['email']);
  226. $blog_url = strip_tags($site->fdat['url']);
  227. #####################
  228. # insert into obj_kommentaar:
  229. $sql = $site->db->prepare("INSERT INTO obj_kommentaar (objekt_id, nimi, email, on_saada_email, on_peida_email, ip, text, kasutaja_id, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
  230. $id,
  231. $name,
  232. $email,
  233. $site->fdat['on_saada_email'] ? 1 : 0,
  234. $site->fdat['on_peida_email'] ? 1 : 0,
  235. $_SERVER["REMOTE_ADDR"],
  236. big_string_remove(strip_tags($site->fdat['text'])),
  237. $site->user->user_id,
  238. $blog_url
  239. );
  240. $sth = new SQL($sql);
  241. $site->debug->msg($sth->debug->get_msgs());
  242. $comment_inserted = $sth->rows;
  243. #####################
  244. # UPDATE LAST_COMMENTED_TIME, COMMENT_COUNT
  245. # get comment count for object:
  246. $alamlist_count = new Alamlist(array(
  247. parent => $site->fdat['id'],
  248. klass => "kommentaar",
  249. asukoht => 0,
  250. on_counter => 1
  251. ));
  252. $comment_count = $alamlist_count->rows;
  253. $sql = $site->db->prepare("UPDATE objekt SET last_commented_time=".$site->db->unix2db_datetime(time()).", comment_count=? WHERE objekt_id=?",
  254. $comment_count,
  255. $site->fdat['id']
  256. );
  257. $sth = new SQL($sql);
  258. $site->debug->msg($sth->debug->get_msgs());
  259. #################
  260. # kui kommentaar edukalt tabelisse lisatud
  261. if ($comment_inserted){
  262. ####### find TO e-mail saved in topic's editor or in template configuration
  263. if(is_object($obj_conf) && $obj_conf->get('email')){
  264. $conf_email = $obj_conf->get('email');
  265. } elseif(is_object($conf)){
  266. $conf_email = $conf->get("email");
  267. }
  268. #####################
  269. # kui e-maili vaja saata ja e-maili formaat OK
  270. if (($objekt->all[on_saada_email]==1 && preg_match("/^[\w\d\-\&\.]+\@[\w\d\-\&\.]+$/",$objekt->all[email])) || ($conf_email != '' && preg_match("/^[\w\d\-\&\.]+\@[\w\d\-\&\.]+$/",$conf_email))
  271. ){
  272. if (preg_match("/^[\w\d\-\&\.]+\@[\w\d\-\&\.]+$/",$site->fdat[email])){
  273. $from = $site->fdat['email'];
  274. } else {
  275. $from = $site->CONF['from_email'];
  276. };
  277. $url = "/?".($site->fdat[tpl]?"tpl=".$site->fdat[tpl]."&":"").($site->fdat[c_tpl]?"c_tpl=".$site->fdat[c_tpl]."&":"")."id=".($site->fdat['inserted_id'] ? $id : $site->fdat[id]);
  278. $messagebody = ($site->fdat['message_text'] ? str_replace("\\n", "\n", strip_tags($site->fdat['message_text'])) : strip_tags($site->fdat['text']))."\n\n\nURL: ".(empty($_SERVER['HTTPS']) ? 'http://': 'https://').$site->CONF[hostname].$site->CONF[wwwroot].($site->fdat['mail_to_admin'] || ($faq_mode && $publish == 0) ? "/editor" : "")."/?".($site->fdat[tpl]?"tpl=".$site->fdat[tpl]."&":"").($site->fdat[c_tpl]?"c_tpl=".$site->fdat[c_tpl]."&":"")."id=".($id ? $id : $site->fdat[id]);
  279. mail(
  280. email::encodeHeader(($objekt->all[email] ? $objekt->all[email] : $conf_email), $site->encoding),
  281. email::encodeHeader(strip_tags($site->fdat['pealkiri']), $site->encoding),
  282. $messagebody,
  283. "From: ". email::encodeHeader($from, $site->encoding) .(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "\r\n" : "\n").
  284. "MIME-Version: 1.0" .(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "\r\n" : "\n").
  285. "Content-Type: text/plain; charset=\"".$site->encoding."\"" .(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "\r\n" : "\n"). # Bug #2121
  286. "Content-Transfer-Encoding: 8bit".(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? "\r\n" : "\n")
  287. );
  288. if ($site->on_debug){
  289. echo "<hr>Saadan meil siia:".($objekt->all[email] ? $objekt->all[email] : $conf_email).", from: ".$from;
  290. };
  291. };
  292. # / kui e-maili vaja saata ja e-maili formaat OK
  293. #####################
  294. # ------------------------
  295. # Kustutame chache-ist
  296. # ------------------------
  297. clear_cache("ALL");
  298. }
  299. # / kui kommentaar edukalt tabelisse lisatud
  300. #################
  301. #########################
  302. # debug info
  303. $site->debug->print_msg();
  304. #########################
  305. # redirect
  306. if (!$site->on_debug){
  307. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); //To fool old browsers
  308. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  309. header("Cache-Control: no-store, no-cache, must-revalidate");
  310. header("Cache-Control: post-check=0, pre-check=0", false);
  311. header("Pragma: no-cache");
  312. # show javascript message "Forum alert: Your question has been sent"
  313. if ($faq_mode && !$site->in_editor){$tmp_lisa_alert="&lisa_alert=1";} # Bug #2133
  314. if ($site->fdat['redirect_url']){
  315. header("Location: ".urldecode($site->fdat['redirect_url']));
  316. } else { # Bug #1953
  317. header("Location: ".(empty($_SERVER['HTTPS']) ? 'http://': 'https://').$site->CONF[hostname].$site->CONF[wwwroot].($site->in_editor?"/editor":"")."/?".(($site->fdat[tpl] || $site->fdat[c_tpl])&&!$site->fdat['inserted_id']&&!$site->fdat['jump_to_parent']?"tpl=".$site->fdat[tpl]."&c_tpl=".$site->fdat[c_tpl]."&":"")."id=".($site->fdat['jump_to_parent'] ? $objekt->parent_id : $objekt->objekt_id).$tmp_lisa_alert);
  318. }
  319. } # not debug
  320. }
  321. else {
  322. echo "<font size=2>Access denied.</font>";
  323. }
  324. # / if access is allowed
  325. ########################
  326. }
  327. else {
  328. $site->debug->msg("Object adding denied - not correct class:".$objekt->all[klass]);
  329. $site->debug->print_msg();
  330. }
  331. # / double check object class: is it correct?
  332. ########################
  333. function myRedirect($url) {
  334. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); //To fool old browsers
  335. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  336. header("Cache-Control: no-store, no-cache, must-revalidate");
  337. header("Cache-Control: post-check=0, pre-check=0", false);
  338. header("Pragma: no-cache");
  339. header("Location: " . urldecode($url));
  340. }