fork download
  1. importPackage(java.io);
  2. importPackage(java.lang);
  3.  
  4. <?php
  5. /**
  6.  * JComments - Joomla Comment System
  7.  *
  8.  * @version 3.0
  9.  * @package JComments
  10.  * @author Sergey M. Litvinov (smart@joomlatune.ru)
  11.  * @copyright (C) 2006-2013 by Sergey M. Litvinov (http://w...content-available-to-author-only...e.ru)
  12.  * @license GNU/GPL: http://w...content-available-to-author-only...u.org/copyleft/gpl.html
  13.  */
  14.  
  15. defined('_JEXEC') or die;
  16.  
  17.  
  18. if (!defined('JOOMLATUNE_AJAX')) {
  19. require_once (JCOMMENTS_LIBRARIES.'/joomlatune/ajax.php');
  20. }
  21.  
  22. if ((version_compare(phpversion(), '5.1.0') >= 0)) {
  23. }
  24.  
  25. JTable::addIncludePath(JCOMMENTS_TABLES);
  26.  
  27. /**
  28.  * Frontend event handler
  29.  */
  30. class JCommentsAJAX
  31. {
  32. public static function prepareValues( &$values )
  33. {
  34. foreach ($values as $k => $v) {
  35. if ($k == 'comment') {
  36. // strip all HTML except [code]
  37. $m = array();
  38. preg_match_all('#(\[code\=?([a-z0-9]*?)\].*\[\/code\])#isUu', trim($v), $m);
  39.  
  40. $tmp = array();
  41. $key = '';
  42.  
  43. foreach ($m[1] as $code) {
  44. $key = '{' . md5($code.$key). '}';
  45. $tmp[$key] = $code;
  46. $v = preg_replace('#' . preg_quote($code, '#') . '#isUu', $key, $v);
  47. }
  48.  
  49. $v = trim(strip_tags($v));
  50.  
  51. // handle magic quotes compatibility
  52. if (get_magic_quotes_gpc() == 1) {
  53. $v = stripslashes($v);
  54. }
  55. $v = JCommentsText::nl2br($v);
  56.  
  57. foreach ($tmp as $key => $code) {
  58. if (get_magic_quotes_gpc() == 1) {
  59. $code = str_replace('\"', '"', $code);
  60. $code = str_replace('\'', "'", $code);
  61. }
  62. $v = preg_replace('#' . preg_quote($key, '#') . '#isUu', $code, $v);
  63. }
  64. unset($tmp, $m);
  65. $values[$k] = $v;
  66. } else {
  67. $values[$k] = trim(strip_tags($v));
  68.  
  69. // handle magic quotes compatibility
  70. if (get_magic_quotes_gpc() == 1) {
  71. $values[$k] = stripslashes($values[$k]);
  72. }
  73.  
  74. }
  75. }
  76.  
  77. return $values;
  78. }
  79.  
  80. public static function escapeMessage($message)
  81. {
  82. $message = str_replace("\n", '\n', $message);
  83. $message = str_replace('\n', '<br />', $message);
  84. $message = JCommentsText::jsEscape($message);
  85. return $message;
  86. }
  87.  
  88. public static function showErrorMessage($message, $name = '', $target = '')
  89. {
  90. $message = self::escapeMessage($message);
  91. $response = JCommentsFactory::getAjaxResponse();
  92. $response->addScript("jcomments.error('$message','$target','$name');");
  93. }
  94.  
  95. public static function showInfoMessage($message, $target = '')
  96. {
  97. $message = self::escapeMessage($message);
  98. $response = JCommentsFactory::getAjaxResponse();
  99. $response->addScript("jcomments.message('$message', '$target');");
  100. }
  101.  
  102. public static function showForm( $object_id, $object_group, $target )
  103. {
  104. if (JCommentsSecurity::badRequest() == 1) {
  105. JCommentsSecurity::notAuth();
  106. }
  107.  
  108. $response = JCommentsFactory::getAjaxResponse();
  109. $object_group = JCommentsSecurity::clearObjectGroup($object_group);
  110.  
  111. $form = JComments::getCommentsForm($object_id, $object_group);
  112. $response->addAssign($target, 'innerHTML', $form);
  113. return $response;
  114. }
  115.  
  116. public static function showReportForm($id, $target)
  117. {
  118. if (JCommentsSecurity::badRequest() == 1) {
  119. JCommentsSecurity::notAuth();
  120. }
  121.  
  122. $config = JCommentsFactory::getConfig();
  123. if ($config->getInt('report_reason_required') == 0) {
  124. JFactory::getApplication()->input->post->set('commentid', (int) $id);
  125. $response = JCommentsFactory::getAjaxResponse();
  126. $response->addAssign($target, 'innerHTML', '<div id="comments-report-form"></div>');
  127. return self::reportComment();
  128. } else {
  129. $response = JCommentsFactory::getAjaxResponse();
  130.  
  131. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  132. if ($comment->load($id)) {
  133. $form = JComments::getCommentsReportForm($id, $comment->object_id, $comment->object_group);
  134. $response->addAssign($target, 'innerHTML', $form);
  135. }
  136. return $response;
  137. }
  138. }
  139.  
  140. public static function addComment($values = array())
  141. {
  142. if (JCommentsSecurity::badRequest() == 1) {
  143. JCommentsSecurity::notAuth();
  144. }
  145.  
  146. $user = JFactory::getUser();
  147. $acl = JCommentsFactory::getACL();
  148. $config = JCommentsFactory::getConfig();
  149. $response = JCommentsFactory::getAjaxResponse();
  150.  
  151. if ($acl->canComment()) {
  152. $values = self::prepareValues($_POST);
  153.  
  154. $object_group = isset($values['object_group']) ? JCommentsSecurity::clearObjectGroup($values['object_group']) : '';
  155. $object_id = isset($values['object_id']) ? intval($values['object_id']) : '';
  156.  
  157. if ($object_group == '' || $object_id == '') {
  158. // TODO: add appropriate error message
  159. return $response;
  160. }
  161.  
  162. $commentsPerObject = $config->getInt('max_comments_per_object');
  163. if ($commentsPerObject > 0) {
  164. $commentsCount = JComments::getCommentsCount($object_id, $object_group);
  165. if ($commentsCount >= $commentsPerObject) {
  166. $message = $config->get('message_locked');
  167. if (empty($message)) {
  168. $message = $config->get('ERROR_CANT_COMMENT');
  169. }
  170. $message = self::escapeMessage($message);
  171. $response->addAlert($message);
  172. return $response;
  173. }
  174. }
  175.  
  176. $userIP = $acl->getUserIP();
  177.  
  178. if (!$user->id) {
  179. $noErrors = false;
  180.  
  181. if (isset($values['userid']) && intval($values['userid']) > 0) {
  182. // TODO: we need more correct way to detect login timeout
  183. self::showErrorMessage(JText::_('ERROR_SESSION_EXPIRED'));
  184. } else if (($config->getInt('author_name', 2) == 2) && empty($values['name'])) {
  185. self::showErrorMessage(JText::_('ERROR_EMPTY_NAME'), 'name');
  186. } else if (JCommentsSecurity::checkIsRegisteredUsername($values['name']) == 1) {
  187. self::showErrorMessage(JText::_('ERROR_NAME_EXISTS'), 'name');
  188. } else if (JCommentsSecurity::checkIsForbiddenUsername($values['name']) == 1) {
  189. self::showErrorMessage(JText::_('ERROR_FORBIDDEN_NAME'), 'name');
  190. } else if (preg_match('/[\"\'\[\]\=\<\>\(\)\;]+/', $values['name'])) {
  191. self::showErrorMessage(JText::_('ERROR_INVALID_NAME'), 'name');
  192. } else if (($config->get('username_maxlength') != 0)
  193. && (JCommentsText::strlen($values['name']) > $config->get('username_maxlength'))) {
  194. self::showErrorMessage(JText::_('ERROR_TOO_LONG_USERNAME'), 'name');
  195. } else if (($config->getInt('author_email') == 2) && empty($values['email'])) {
  196. self::showErrorMessage(JText::_('ERROR_EMPTY_EMAIL'), 'email');
  197. } else if (!empty($values['email']) && (!preg_match(_JC_REGEXP_EMAIL2, $values['email']))) {
  198. self::showErrorMessage(JText::_('ERROR_INCORRECT_EMAIL'), 'email');
  199. } else if (($config->getInt('author_email') != 0) && JCommentsSecurity::checkIsRegisteredEmail($values['email']) == 1) {
  200. self::showErrorMessage(JText::_('ERROR_EMAIL_EXISTS'), 'email');
  201. } else if (($config->getInt('author_homepage') == 2) && empty($values['homepage'])) {
  202. self::showErrorMessage(JText::_('ERROR_EMPTY_HOMEPAGE'), 'homepage');
  203. } else {
  204. $noErrors = true;
  205. }
  206.  
  207. if (!$noErrors) {
  208. return $response;
  209. }
  210. }
  211.  
  212. if (($acl->check('floodprotection') == 1) && (JCommentsSecurity::checkFlood($userIP))) {
  213. self::showErrorMessage(JText::_('ERROR_TOO_QUICK'));
  214. } else if (empty($values['homepage']) && ($config->get('author_homepage') == 3)) {
  215. self::showErrorMessage(JText::_('ERROR_EMPTY_HOMEPAGE'), 'homepage');
  216. } else if (empty($values['title']) && ($config->get('comment_title') == 3)) {
  217. self::showErrorMessage(JText::_('ERROR_EMPTY_TITLE'), 'title');
  218. } else if (empty($values['comment'])) {
  219. self::showErrorMessage(JText::_('ERROR_EMPTY_COMMENT'), 'comment');
  220. } else if (($config->getInt('comment_maxlength') != 0)
  221. && ($acl->check('enable_comment_length_check') == 1)
  222. && (JCommentsText::strlen($values['comment']) > $config->get('comment_maxlength'))) {
  223. self::showErrorMessage(JText::_('ERROR_YOUR_COMMENT_IS_TOO_LONG'), 'comment');
  224. } else if (($config->getInt('comment_minlength', 0) != 0)
  225. && ($acl->check('enable_comment_length_check') == 1)
  226. && (JCommentsText::strlen($values['comment']) < $config->get('comment_minlength'))) {
  227. self::showErrorMessage(JText::_('ERROR_YOUR_COMMENT_IS_TOO_SHORT'), 'comment');
  228. } else {
  229. if ($acl->check('enable_captcha') == 1) {
  230.  
  231. $captchaEngine = $config->get('captcha_engine', 'kcaptcha');
  232.  
  233. if ($captchaEngine == 'kcaptcha') {
  234. require_once( JCOMMENTS_SITE.'/jcomments.captcha.php' );
  235.  
  236. if (!JCommentsCaptcha::check($values['captcha_refid'])) {
  237. self::showErrorMessage(JText::_('ERROR_CAPTCHA'), 'captcha');
  238. JCommentsCaptcha::destroy();
  239. $response->addScript("jcomments.clear('captcha');");
  240. return $response;
  241. }
  242. } else {
  243. $result = JCommentsEventHelper::trigger('onJCommentsCaptchaVerify', array($values['captcha_refid'], &$response));
  244. // if all plugins returns false
  245. if (!in_array(true, $result, true)) {
  246. self::showErrorMessage(JText::_('ERROR_CAPTCHA'));
  247. return $response;
  248. }
  249. }
  250. }
  251.  
  252. $db = JFactory::getDbo();
  253.  
  254. // small fix (by default $my has empty 'name' and 'email' field)
  255. if ($user->id) {
  256. $currentUser = JFactory::getUser($user->id);
  257. $user->name = $currentUser->name;
  258. $user->username = $currentUser->username;
  259. $user->email = $currentUser->email;
  260. unset($currentUser);
  261. }
  262.  
  263. if (empty($values['name'])) {
  264. $values['name'] = 'Guest'; // JText::_('Guest');
  265. }
  266.  
  267. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  268.  
  269. $comment->id = 0;
  270. $comment->name = $user->id ? $user->name : preg_replace("/[\'\"\>\<\(\)\[\]]?+/i", '', $values['name']);
  271. $comment->username = $user->id ? $user->username : $comment->name;
  272. $comment->email = $user->id ? $user->email : (isset($values['email']) ? $values['email'] : '');
  273.  
  274. if (($config->getInt('author_homepage') != 0)
  275. && !empty($values['homepage'])) {
  276. $comment->homepage = JCommentsText::url($values['homepage']);
  277. }
  278.  
  279. $comment->comment = $values['comment'];
  280.  
  281. // filter forbidden bbcodes
  282. $bbcode = JCommentsFactory::getBBCode();
  283. $comment->comment = $bbcode->filter( $comment->comment );
  284.  
  285. if ($comment->comment != '') {
  286. if ($config->getInt('enable_custom_bbcode')) {
  287. // filter forbidden custom bbcodes
  288. $commentLength = strlen($comment->comment);
  289. $customBBCode = JCommentsFactory::getCustomBBCode();
  290. $comment->comment = $customBBCode->filter( $comment->comment );
  291.  
  292. if (strlen($comment->comment) == 0 && $commentLength > 0) {
  293. self::showErrorMessage(JText::_('ERROR_YOU_HAVE_NO_RIGHTS_TO_USE_THIS_TAG'), 'comment');
  294. return $response;
  295. }
  296. }
  297. }
  298.  
  299. if ($comment->comment == '') {
  300. self::showErrorMessage(JText::_('ERROR_EMPTY_COMMENT'), 'comment');
  301. return $response;
  302. }
  303.  
  304. $commentWithoutQuotes = $bbcode->removeQuotes($comment->comment);
  305. if ($commentWithoutQuotes == '') {
  306. self::showErrorMessage(JText::_('ERROR_NOTHING_EXCEPT_QUOTES'), 'comment');
  307. return $response;
  308. } else if (($config->getInt('comment_minlength', 0) != 0)
  309. && ($acl->check('enable_comment_length_check') == 1)
  310. && (JCommentsText::strlen($commentWithoutQuotes) < $config->get('comment_minlength'))) {
  311. self::showErrorMessage(JText::_('ERROR_YOUR_COMMENT_IS_TOO_SHORT'), 'comment');
  312. return $response;
  313. }
  314.  
  315. $values['subscribe'] = isset($values['subscribe']) ? (int) $values['subscribe'] : 0;
  316.  
  317. if ($values['subscribe'] == 1 && $comment->email == '') {
  318. self::showErrorMessage(JText::_('ERROR_SUBSCRIPTION_EMAIL'), 'email');
  319. return $response;
  320. }
  321.  
  322. $comment->object_id = (int) $object_id;
  323. $comment->object_group = $object_group;
  324. $comment->title = isset($values['title']) ? $values['title'] : '';
  325. $comment->parent = isset($values['parent']) ? intval($values['parent']) : 0;
  326. $comment->lang = JCommentsMultilingual::getLanguage();
  327. $comment->ip = $userIP;
  328. $comment->userid = $user->id ? $user->id : 0;
  329. $comment->published = $acl->check('autopublish');
  330. $comment->date = JFactory::getDate()->toSql();
  331.  
  332. $query = "SELECT COUNT(*) "
  333. . "\nFROM #__jcomments "
  334. . "\nWHERE comment = '" . $db->escape($comment->comment) . "'"
  335. . "\n AND ip = '" . $db->escape($comment->ip) . "'"
  336. . "\n AND name = '" . $db->escape($comment->name) . "'"
  337. . "\n AND userid = '" . $comment->userid . "'"
  338. . "\n AND object_id = " . $comment->object_id
  339. . "\n AND parent = " . $comment->parent
  340. . "\n AND object_group = '" . $db->escape($comment->object_group) . "'"
  341. . (JCommentsMultilingual::isEnabled() ? "\nAND lang = '" . JCommentsMultilingual::getLanguage() . "'" : "")
  342. ;
  343. $db->setQuery($query);
  344. $found = $db->loadResult();
  345.  
  346. // if duplicates is not found
  347. if ($found == 0) {
  348. $result = JCommentsEventHelper::trigger('onJCommentsCommentBeforeAdd', array(&$comment));
  349.  
  350. if (in_array(false, $result, true)) {
  351. return $response;
  352. }
  353.  
  354. // save comments subscription
  355. if ($values['subscribe']) {
  356. require_once (JCOMMENTS_SITE.'/jcomments.subscription.php');
  357. $manager = JCommentsSubscriptionManager::getInstance();
  358. $manager->subscribe($comment->object_id, $comment->object_group, $comment->userid, $comment->email, $comment->name, $comment->lang);
  359. }
  360.  
  361. $merged = false;
  362. $merge_time = $config->getInt('merge_time', 0);
  363.  
  364. // merge comments from same author
  365. if ($user->id && $merge_time > 0) {
  366. // load previous comment for same object and group
  367. $prevComment = JCommentsModel::getLastComment($comment->object_id, $comment->object_group, $comment->parent);
  368.  
  369. if ($prevComment != null) {
  370. // if previous comment from same author and it currently not edited
  371. // by any user - we'll update comment, else - insert new record to database
  372. if (($prevComment->userid == $comment->userid)
  373. && ($prevComment->parent == $comment->parent)
  374. && (!$acl->isLocked($prevComment))) {
  375.  
  376. $newText = $prevComment->comment . '<br /><br />' . $comment->comment;
  377. $timeDiff = strtotime($comment->date) - strtotime($prevComment->date);
  378.  
  379. if ($timeDiff < $merge_time) {
  380.  
  381. $maxlength = $config->getInt('comment_maxlength');
  382. $needcheck = $acl->check('enable_comment_length_check');
  383.  
  384. // validate new comment text length and if it longer than specified -
  385. // disable union current comment with previous
  386. if (($needcheck == 0) || (($needcheck == 1) && ($maxlength != 0)
  387. && (JCommentsText::strlen($newText) <= $maxlength))) {
  388. $comment->id = $prevComment->id;
  389. $comment->comment = $newText;
  390. $merged = true;
  391. }
  392. }
  393. }
  394. unset($prevComment);
  395. }
  396. }
  397.  
  398. // save new comment to database
  399. if (!$comment->store()) {
  400. $response->addScript("jcomments.clear('comment');");
  401.  
  402. if ($acl->check('enable_captcha') == 1 && $config->get('captcha_engine', 'kcaptcha') == 'kcaptcha') {
  403. JCommentsCaptcha::destroy();
  404. $response->addScript("jcomments.clear('captcha');");
  405. }
  406. return $response;
  407. }
  408.  
  409. // store/update information about commented object
  410. JCommentsObjectHelper::storeObjectInfo($comment->object_id, $comment->object_group, $comment->lang);
  411.  
  412. JCommentsEventHelper::trigger('onJCommentsCommentAfterAdd', array(&$comment));
  413.  
  414. // send notification to administrators
  415. if ($config->getInt('enable_notification') == 1) {
  416. if ($config->check('notification_type', 1) == true) {
  417. JComments::sendNotification($comment, true);
  418. }
  419. }
  420.  
  421. // if comment published we need update comments list
  422. if ($comment->published) {
  423. // send notification to comment subscribers
  424. JComments::sendToSubscribers($comment, true);
  425.  
  426. if ($merged) {
  427. $commentText = $comment->comment;
  428. $html = JCommentsText::jsEscape(JComments::getCommentItem($comment));
  429. $response->addScript("jcomments.updateComment(".$comment->id.", '$html');");
  430. $comment->comment = $commentText;
  431. } else {
  432. $count = JComments::getCommentsCount($comment->object_id, $comment->object_group);
  433.  
  434. if ($config->get('template_view') == 'tree') {
  435. if ($count > 1) {
  436. $html = JComments::getCommentListItem($comment);
  437. $html = JCommentsText::jsEscape($html);
  438. $mode = ($config->getInt('comments_tree_order') == 1
  439. || ($config->getInt('comments_tree_order') == 2 && $comment->parent > 0)) ? 'b' : 'a';
  440. $response->addScript("jcomments.updateTree('$html','$comment->parent','$mode');");
  441. } else {
  442. $html = JComments::getCommentsTree($comment->object_id, $comment->object_group);
  443. $html = JCommentsText::jsEscape($html);
  444. $response->addScript("jcomments.updateTree('$html',null);");
  445. }
  446. } else {
  447. // if pagination disabled and comments count > 1...
  448. if ($config->getInt('comments_per_page') == 0 && $count > 1) {
  449. // update only added comment
  450. $html = JComments::getCommentListItem($comment);
  451. $html = JCommentsText::jsEscape($html);
  452.  
  453. if ($config->get('comments_list_order') == 'DESC') {
  454. $response->addScript("jcomments.updateList('$html','p');");
  455. } else {
  456. $response->addScript("jcomments.updateList('$html','a');");
  457. }
  458. } else {
  459. // update comments list
  460. $html = JComments::getCommentsList($comment->object_id, $comment->object_group, JComments::getCommentPage($comment->object_id, $comment->object_group, $comment->id));
  461. $html = JCommentsText::jsEscape($html);
  462. $response->addScript("jcomments.updateList('$html','r');");
  463. }
  464.  
  465. // scroll to first comment
  466. if ($config->get('comments_list_order') == 'DESC') {
  467. $response->addScript("jcomments.scrollToList();");
  468. }
  469. }
  470. }
  471. self::showInfoMessage(JText::_('THANK_YOU_FOR_YOUR_SUBMISSION'));
  472. } else {
  473. self::showInfoMessage(JText::_('THANK_YOU_YOUR_COMMENT_WILL_BE_PUBLISHED_ONCE_REVIEWED'));
  474. }
  475.  
  476. // clear comments textarea & update comment length counter if needed
  477. $response->addScript("jcomments.clear('comment');");
  478.  
  479. if ($acl->check('enable_captcha') == 1 && $config->get('captcha_engine', 'kcaptcha') == 'kcaptcha') {
  480. require_once( JCOMMENTS_SITE.'/jcomments.captcha.php' );
  481. JCommentsCaptcha::destroy();
  482. $response->addScript("jcomments.clear('captcha');");
  483. }
  484. } else {
  485. self::showErrorMessage(JText::_('ERROR_DUPLICATE_COMMENT'), 'comment');
  486. }
  487. }
  488. } else {
  489. $message = $config->get('ERROR_CANT_COMMENT');
  490. if ($acl->getUserBlocked()) {
  491. $bannedMessage = $config->get('message_banned');
  492. if (!empty($bannedMessage)) {
  493. $message = self::escapeMessage($bannedMessage);
  494. }
  495. }
  496. $response->addAlert($message);
  497. }
  498. return $response;
  499. }
  500.  
  501. public static function deleteComment($id)
  502. {
  503. if (JCommentsSecurity::badRequest() == 1) {
  504. JCommentsSecurity::notAuth();
  505. }
  506.  
  507. $acl = JCommentsFactory::getACL();
  508. $config = JCommentsFactory::getConfig();
  509. $response = JCommentsFactory::getAjaxResponse();
  510.  
  511. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  512.  
  513. if ($comment->load((int) $id)) {
  514. if ($acl->isLocked($comment)) {
  515. $response->addAlert(JText::_('ERROR_BEING_EDITTED'));
  516. } else if ($acl->canDelete($comment)) {
  517.  
  518. $object_id = $comment->object_id;
  519. $object_group = $comment->object_group;
  520.  
  521. $result = JCommentsEventHelper::trigger('onJCommentsCommentBeforeDelete', array(&$comment));
  522.  
  523. if (!in_array(false, $result, true)) {
  524. if ($config->getInt('delete_mode') == 0) {
  525. $comment->delete();
  526. $count = JComments::getCommentsCount($object_id, $object_group, '', true);
  527.  
  528. if ($config->get('template_view') == 'tree') {
  529. if ($count > 0) {
  530. $response->addScript("jcomments.updateComment('$id','');");
  531. } else {
  532. $response->addScript("jcomments.updateTree('',null);");
  533. }
  534. } else {
  535. if ($count > 0) {
  536. if ($config->getInt('comments_per_page') > 0) {
  537. require_once (JCOMMENTS_HELPERS.'/pagination.php');
  538. $pagination = new JCommentsPagination($object_id, $object_group);
  539. $pagination->setCommentsCount($count);
  540. $currentPage = $pagination->getCommentPage($object_id, $object_group, $id);
  541. $currentPage = min($currentPage, $pagination->getTotalPages());
  542.  
  543. $html = JComments::getCommentsList($object_id, $object_group, $currentPage);
  544. $html = JCommentsText::jsEscape($html);
  545. $response->addScript("jcomments.updateList('$html','r');");
  546. } else {
  547. $response->addScript("jcomments.updateComment('$id','');");
  548. }
  549. } else {
  550. $response->addScript("jcomments.updateList('','r');");
  551. }
  552. }
  553. } else {
  554. $comment->markAsDeleted();
  555. $html = JCommentsText::jsEscape(JComments::getCommentItem($comment));
  556. $response->addScript("jcomments.updateComment(" . $comment->id . ", '$html');");
  557. }
  558.  
  559. JCommentsEventHelper::trigger('onJCommentsCommentAfterDelete', array(&$comment));
  560. }
  561. } else {
  562. $response->addAlert(JText::_('ERROR_CANT_DELETE'));
  563. }
  564. }
  565. return $response;
  566. }
  567.  
  568. public static function publishComment($id)
  569. {
  570. if (JCommentsSecurity::badRequest() == 1) {
  571. JCommentsSecurity::notAuth();
  572. }
  573.  
  574. $acl = JCommentsFactory::getACL();
  575. $response = JCommentsFactory::getAjaxResponse();
  576.  
  577. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  578.  
  579. if ($comment->load((int) $id)) {
  580. if ($acl->isLocked($comment)) {
  581. $response->addAlert(JText::_('ERROR_BEING_EDITTED'));
  582. } else if ($acl->canPublish($comment)) {
  583.  
  584. $object_id = $comment->object_id;
  585. $object_group = $comment->object_group;
  586. $page = JComments::getCommentPage($object_id, $object_group, $comment->id);
  587. $comment->published = !$comment->published;
  588.  
  589. $result = JCommentsEventHelper::trigger('onJCommentsCommentBeforePublish', array(&$comment));
  590.  
  591. if (!in_array(false, $result, true)) {
  592. if ($comment->store()) {
  593. JCommentsEventHelper::trigger('onJCommentsCommentAfterPublish', array(&$comment));
  594. if ($comment->published) {
  595. JComments::sendToSubscribers($comment, true);
  596. }
  597. self::updateCommentsList($response, $object_id, $object_group, $page);
  598. }
  599. }
  600. } else {
  601. $response->addAlert(JText::_('ERROR_CANT_PUBLISH'));
  602. }
  603. }
  604. return $response;
  605. }
  606.  
  607. public static function cancelComment($id)
  608. {
  609. if (JCommentsSecurity::badRequest() == 1) {
  610. JCommentsSecurity::notAuth();
  611. }
  612.  
  613. $response = JCommentsFactory::getAjaxResponse();
  614. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  615.  
  616. if ($comment->load((int) $id)) {
  617. $acl = JCommentsFactory::getACL();
  618.  
  619. if (!$acl->isLocked($comment)) {
  620. $comment->checkin();
  621. }
  622. }
  623. return $response;
  624. }
  625.  
  626. public static function editComment($id, $loadForm = 0)
  627. {
  628. if (JCommentsSecurity::badRequest() == 1) {
  629. JCommentsSecurity::notAuth();
  630. }
  631.  
  632. $user = JFactory::getUser();
  633. $response = JCommentsFactory::getAjaxResponse();
  634. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  635.  
  636. if ($comment->load((int) $id)) {
  637. $acl = JCommentsFactory::getACL();
  638.  
  639. if ($acl->isLocked($comment)) {
  640. $response->addAlert(JText::_('ERROR_BEING_EDITTED'));
  641. } else if ($acl->canEdit($comment)) {
  642. $comment->checkout($user->id);
  643.  
  644. $name = ($comment->userid) ? '' : JCommentsText::jsEscape($comment->name);
  645. $email = ($comment->userid) ? '' : JCommentsText::jsEscape($comment->email);
  646. $homepage = JCommentsText::jsEscape($comment->homepage);
  647. $text = JCommentsText::jsEscape(JCommentsText::br2nl($comment->comment));
  648. $title = JCommentsText::jsEscape(str_replace("\n", '', JCommentsText::br2nl($comment->title)));
  649.  
  650. if (intval($loadForm) == 1) {
  651. $form = JComments::getCommentsForm($comment->object_id, $comment->object_group, true);
  652. $response->addAssign('comments-form-link', 'innerHTML', $form);
  653. }
  654. $response->addScript("jcomments.showEdit(" . $comment->id . ", '$name', '$email', '$homepage', '$title', '$text');");
  655. } else {
  656. $response->addAlert(JText::_('ERROR_CANT_EDIT'));
  657. }
  658. }
  659. return $response;
  660. }
  661.  
  662. public static function saveComment($values = array())
  663. {
  664. if (JCommentsSecurity::badRequest() == 1) {
  665. JCommentsSecurity::notAuth();
  666. }
  667.  
  668. $config = JCommentsFactory::getConfig();
  669.  
  670. $response = JCommentsFactory::getAjaxResponse();
  671. $values = self::prepareValues($_POST);
  672. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  673. $id = (int) $values['id'];
  674.  
  675. if ($comment->load($id)) {
  676. $acl = JCommentsFactory::getACL();
  677.  
  678. if ($acl->canEdit($comment)) {
  679. if ($values['comment'] == '') {
  680. self::showErrorMessage(JText::_('ERROR_EMPTY_COMMENT'), 'comment');
  681. } else if (($config->getInt('comment_maxlength') != 0)
  682. && ($acl->check('enable_comment_length_check') == 1)
  683. && (JCommentsText::strlen($values['comment']) > $config->getInt('comment_maxlength'))) {
  684. self::showErrorMessage(JText::_('ERROR_YOUR_COMMENT_IS_TOO_LONG'), 'comment');
  685. } else if (($config->getInt('comment_minlength') != 0)
  686. && ($acl->check('enable_comment_length_check') == 1)
  687. && (JCommentsText::strlen($values['comment']) < $config->getInt('comment_minlength'))) {
  688. self::showErrorMessage(JText::_('ERROR_YOUR_COMMENT_IS_TOO_SHORT'), 'comment');
  689. } else {
  690. $bbcode = JCommentsFactory::getBBCode();
  691.  
  692. $comment->comment = $values['comment'];
  693. $comment->comment = $bbcode->filter($comment->comment);
  694. $comment->published = $acl->check('autopublish');
  695.  
  696.  
  697. if (($config->getInt('comment_title') != 0) && isset($values['title'])) {
  698. $comment->title = stripslashes((string)$values['title']);
  699. }
  700.  
  701. if (($config->getInt('author_homepage') == 1) && isset($values['homepage'])) {
  702. $comment->homepage = JCommentsText::url($values['homepage']);
  703. } else {
  704. $comment->homepage = '';
  705. }
  706.  
  707. $result = JCommentsEventHelper::trigger('onJCommentsCommentBeforeChange', array(&$comment));
  708.  
  709. if (in_array(false, $result, true)) {
  710. return $response;
  711. }
  712.  
  713. $comment->store();
  714. $comment->checkin();
  715.  
  716. JCommentsEventHelper::trigger('onJCommentsCommentAfterChange', array(&$comment));
  717.  
  718. if ($config->getInt('enable_notification') == 1) {
  719. if ($config->check('notification_type', 1) == true) {
  720. JComments::sendNotification($comment, false);
  721. }
  722. }
  723. $html = JCommentsText::jsEscape(JComments::getCommentItem($comment));
  724. $response->addScript("jcomments.updateComment(" . $comment->id . ", '$html');");
  725. }
  726. } else {
  727. $response->addAlert(JText::_('ERROR_CANT_EDIT'));
  728. }
  729. }
  730. return $response;
  731. }
  732.  
  733. public static function quoteComment($id, $loadForm = 0)
  734. {
  735. if (JCommentsSecurity::badRequest() == 1) {
  736. JCommentsSecurity::notAuth();
  737. }
  738.  
  739. $acl = JCommentsFactory::getACL();
  740. $config = JCommentsFactory::getConfig();
  741. $response = JCommentsFactory::getAjaxResponse();
  742. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  743.  
  744. if ($comment->load((int) $id)) {
  745. $comment_name = JComments::getCommentAuthorName($comment);
  746. $comment_text = JCommentsText::br2nl($comment->comment);
  747.  
  748. if ($config->getInt('enable_nested_quotes') == 0) {
  749. $bbcode = JCommentsFactory::getBBCode();
  750. $comment_text = $bbcode->removeQuotes($comment_text);
  751. }
  752.  
  753. if ($config->getInt('enable_custom_bbcode')) {
  754. $customBBCode = JCommentsFactory::getCustomBBCode();
  755. $comment_text = $customBBCode->filter($comment_text, true);
  756. }
  757.  
  758. if ($acl->getUserId() == 0) {
  759. $bbcode = JCommentsFactory::getBBCode();
  760. $comment_text = $bbcode->removeHidden($comment_text);
  761. }
  762.  
  763. if ($comment_text != '') {
  764. if ($acl->check('enable_autocensor')) {
  765. $comment_text = JCommentsText::censor($comment_text);
  766. }
  767.  
  768. if (intval($loadForm) == 1) {
  769. $form = JComments::getCommentsForm($comment->object_id, $comment->object_group, true);
  770. $response->addAssign('comments-form-link', 'innerHTML', $form);
  771. }
  772.  
  773. $comment_name = JCommentsText::jsEscape($comment_name);
  774. $comment_text = JCommentsText::jsEscape($comment_text);
  775. $text = '[quote name="' . $comment_name . '"]' . $comment_text . '[/quote]\n';
  776. $response->addScript("jcomments.insertText('" . $text . "');");
  777. } else {
  778. $response->addAlert(JText::_('ERROR_NOTHING_TO_QUOTE'));
  779. }
  780. }
  781. return $response;
  782. }
  783.  
  784. public static function updateCommentsList(&$response, $object_id, $object_group, $page)
  785. {
  786. $config = JCommentsFactory::getConfig();
  787.  
  788. if ($config->get('template_view') == 'tree') {
  789. $html = JComments::getCommentsTree($object_id, $object_group, $page);
  790. $html = JCommentsText::jsEscape($html);
  791. $response->addScript("jcomments.updateTree('$html',null);");
  792. } else {
  793. $html = JComments::getCommentsList($object_id, $object_group, $page);
  794. $html = JCommentsText::jsEscape($html);
  795. $response->addScript("jcomments.updateList('$html','r');");
  796. }
  797. }
  798.  
  799. public static function showPage($object_id, $object_group, $page)
  800. {
  801. $response = JCommentsFactory::getAjaxResponse();
  802.  
  803. $object_id = (int) $object_id;
  804. $object_group = strip_tags($object_group);
  805. $object_group = JCommentsSecurity::clearObjectGroup($object_group);
  806. $page = (int) $page;
  807.  
  808. self::updateCommentsList($response, $object_id, $object_group, $page);
  809. return $response;
  810. }
  811.  
  812. public static function showComment($id)
  813. {
  814. $response = JCommentsFactory::getAjaxResponse();
  815. $acl = JCommentsFactory::getACL();
  816. $config = JCommentsFactory::getConfig();
  817. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  818.  
  819. if ($comment->load((int) $id) && ($acl->canPublish($comment) || $comment->published)) {
  820. if ($config->get('template_view') == 'tree') {
  821. $page = 0;
  822. } else {
  823. $page = JComments::getCommentPage($comment->object_id, $comment->object_group, $comment->id);
  824. }
  825. self::updateCommentsList($response, $comment->object_id, $comment->object_group, $page);
  826. $response->addScript("jcomments.scrollToComment('$id');");
  827. } else {
  828. $response->addAlert(JText::_('ERROR_NOT_FOUND'));
  829. }
  830. return $response;
  831. }
  832.  
  833. public static function jump2email($id, $hash)
  834. {
  835. $response = JCommentsFactory::getAjaxResponse();
  836. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  837. $hash = preg_replace('#[\(\)\'\"]#is', '', strip_tags($hash));
  838.  
  839. if ((strlen($hash) == 32) && ($comment->load( (int) $id))) {
  840. $matches = array();
  841. preg_match_all(_JC_REGEXP_EMAIL, $comment->comment, $matches);
  842. foreach ($matches[0] as $email) {
  843. if (md5((string) $email) == $hash) {
  844. $response->addScript("window.location='mailto:$email';");
  845. }
  846. }
  847. }
  848. return $response;
  849. }
  850.  
  851. public static function subscribeUser($object_id, $object_group)
  852. {
  853. $user = JFactory::getUser();
  854. $response = JCommentsFactory::getAjaxResponse();
  855.  
  856. if ($user->id) {
  857. require_once (JCOMMENTS_SITE.'/jcomments.subscription.php');
  858.  
  859. $manager = JCommentsSubscriptionManager::getInstance();
  860. $result = $manager->subscribe($object_id, $object_group, $user->id);
  861.  
  862. if ($result) {
  863. $response->addScript("jcomments.updateSubscription(true, '" . JText::_('BUTTON_UNSUBSCRIBE') . "');");
  864. } else {
  865. $errors = $manager->getErrors();
  866. if (count($errors)) {
  867. $response->addAlert(implode('\n', $errors));
  868. }
  869. }
  870. }
  871. return $response;
  872. }
  873.  
  874. public static function unsubscribeUser($object_id, $object_group)
  875. {
  876. $user = JFactory::getUser();
  877. $response = JCommentsFactory::getAjaxResponse();
  878. $object_group = JCommentsSecurity::clearObjectGroup($object_group);
  879.  
  880. if ($user->id) {
  881. require_once (JCOMMENTS_SITE.'/jcomments.subscription.php');
  882.  
  883. $manager = JCommentsSubscriptionManager::getInstance();
  884. $result = $manager->unsubscribe($object_id, $object_group, $user->id);
  885.  
  886. if ($result) {
  887. $response->addScript("jcomments.updateSubscription(false, '" . JText::_('BUTTON_SUBSCRIBE') . "');");
  888. } else {
  889. $errors = $manager->getErrors();
  890. $response->addAlert(implode('\n', $errors));
  891. }
  892. }
  893. return $response;
  894. }
  895.  
  896. public static function voteComment($id, $value)
  897. {
  898. $acl = JCommentsFactory::getACL();
  899. $db = JFactory::getDbo();
  900. $response = JCommentsFactory::getAjaxResponse();
  901.  
  902. $id = (int) $id;
  903. $value = (int) $value;
  904. $value = ($value > 0) ? 1 : -1;
  905.  
  906. $ip = $acl->getUserIP();
  907.  
  908. $query = 'SELECT COUNT(*) FROM `#__jcomments_votes` WHERE commentid = ' . $id;
  909.  
  910. if ($acl->getUserId()) {
  911. $query .= ' AND userid = ' . $acl->getUserId();
  912. } else {
  913. $query .= ' AND userid = 0 AND ip = "' . $ip . '"';
  914. }
  915. $db->setQuery($query);
  916. $voted = $db->loadResult();
  917.  
  918. if ($voted == 0) {
  919. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  920.  
  921. if ($comment->load($id)) {
  922. if ($acl->canVote($comment)) {
  923.  
  924. $result = JCommentsEventHelper::trigger('onJCommentsCommentBeforeVote', array(&$comment, &$value));
  925.  
  926. if (!in_array(false, $result, true)) {
  927.  
  928. if ($value > 0) {
  929. $comment->isgood++;
  930. } else {
  931. $comment->ispoor++;
  932. }
  933. $comment->store();
  934.  
  935. $query = "INSERT INTO `#__jcomments_votes`(`commentid`,`userid`,`ip`,`date`,`value`)"
  936. . "VALUES('".$comment->id."', '".$acl->getUserId()."','".$db->escape($ip)."', now(), ".$value.")";
  937. $db->setQuery($query);
  938. $db->execute();
  939.  
  940. JCommentsEventHelper::trigger('onJCommentsCommentAfterVote', array(&$comment, $value));
  941. }
  942.  
  943. $tmpl = JCommentsFactory::getTemplate();
  944. $tmpl->load('tpl_comment');
  945. $tmpl->addVar('tpl_comment', 'get_comment_vote', 1);
  946. $tmpl->addObject('tpl_comment', 'comment', $comment);
  947.  
  948. $html = $tmpl->renderTemplate('tpl_comment');
  949. $html = JCommentsText::jsEscape($html);
  950. $response->addScript("jcomments.updateVote('" . $comment->id . "','$html');");
  951. } else {
  952. $response->addAlert(JText::_('ERROR_CANT_VOTE'));
  953. }
  954. } else {
  955. $response->addAlert(JText::_('ERROR_NOT_FOUND'));
  956. }
  957. } else {
  958. $response->addAlert(JText::_('ERROR_ALREADY_VOTED'));
  959. }
  960. return $response;
  961. }
  962.  
  963. public static function reportComment()
  964. {
  965. if (JCommentsSecurity::badRequest() == 1) {
  966. JCommentsSecurity::notAuth();
  967. }
  968.  
  969. $acl = JCommentsFactory::getACL();
  970. $db = JFactory::getDbo();
  971. $config = JCommentsFactory::getConfig();
  972. $response = JCommentsFactory::getAjaxResponse();
  973. $values = self::prepareValues($_POST);
  974.  
  975. $id = (int) $values['commentid'];
  976. $reason = trim(strip_tags($values['reason']));
  977. $name = trim(strip_tags($values['name']));
  978. $ip = $acl->getUserIP();
  979.  
  980. if (empty($reason)) {
  981. if ($config->getInt('report_reason_required') == 1) {
  982. self::showErrorMessage(JText::_('ERROR_NO_REASON_FOR_REPORT'), '', 'comments-report-form');
  983. return $response;
  984. } else {
  985. $reason = JText::_('REPORT_REASON_UNKNOWN_REASON');
  986. }
  987. }
  988.  
  989. $query = 'SELECT COUNT(*) FROM `#__jcomments_reports` WHERE commentid = ' . $id;
  990. if ($acl->getUserId()) {
  991. $query .= ' AND userid = ' . $acl->getUserId();
  992. } else {
  993. $query .= ' AND userid = 0 AND ip = "' . $ip . '"';
  994. }
  995. $db->setQuery( $query );
  996. $reported = $db->loadResult();
  997.  
  998. if (!$reported) {
  999. $maxReportsPerComment = $config->getInt('reports_per_comment', 1);
  1000. $maxReportsBeforeUnpublish = $config->getInt('reports_before_unpublish', 0);
  1001. $db->setQuery('SELECT COUNT(*) FROM `#__jcomments_reports` WHERE commentid = ' . $id);
  1002. $reported = $db->loadResult();
  1003. if ($reported < $maxReportsPerComment || $maxReportsPerComment == 0) {
  1004. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  1005. if ($comment->load($id)) {
  1006. if ($acl->canReport($comment)) {
  1007. if ($acl->getUserId()) {
  1008. $user = JFactory::getUser();
  1009. $name = $user->name;
  1010. } else {
  1011. if (empty($name)) {
  1012. $name = 'Guest'; // JText::_('Guest');
  1013. }
  1014. }
  1015.  
  1016. $report = JTable::getInstance('Report', 'JCommentsTable');
  1017. $report->commentid = $comment->id;
  1018. $report->date = JFactory::getDate()->toSql();
  1019. $report->userid = $acl->getUserId();
  1020. $report->ip = $ip;
  1021. $report->name = $name;
  1022. $report->reason = $reason;
  1023.  
  1024. $html = '';
  1025. $result = JCommentsEventHelper::trigger('onJCommentsCommentBeforeReport', array(&$comment, &$report));
  1026.  
  1027. if (!in_array(false, $result, true)) {
  1028. if ($report->store()) {
  1029. JCommentsEventHelper::trigger('onJCommentsCommentAfterReport', array(&$comment, $report));
  1030.  
  1031. if ($config->getInt('enable_notification') == 1) {
  1032. if ($config->check('notification_type', 2)) {
  1033. JComments::sendReport($comment, $name, $reason);
  1034. }
  1035. }
  1036.  
  1037. // unpublish comment if reports count is enough
  1038. if ($maxReportsBeforeUnpublish > 0 && $reported >= $maxReportsBeforeUnpublish) {
  1039. $comment->published = 0;
  1040. $comment->store();
  1041. }
  1042.  
  1043. $html = JText::_('REPORT_SUCCESSFULLY_SENT');
  1044. $html = str_replace("\n", '\n', $html);
  1045. $html = str_replace('\n', '<br />', $html);
  1046. $html = JCommentsText::jsEscape($html);
  1047. }
  1048. }
  1049. $response->addScript("jcomments.closeReport('$html');");
  1050. } else {
  1051. self::showErrorMessage(JText::_('ERROR_YOU_HAVE_NO_RIGHTS_TO_REPORT'), '', 'comments-report-form');
  1052. }
  1053. } else {
  1054. $response->addAlert(JText::_('ERROR_NOT_FOUND'));
  1055. }
  1056. } else {
  1057. self::showErrorMessage(JText::_('ERROR_COMMENT_ALREADY_REPORTED'), '', 'comments-report-form');
  1058. }
  1059. } else {
  1060. self::showErrorMessage(JText::_('ERROR_YOU_CAN_NOT_REPORT_THE_SAME_COMMENT_MORE_THAN_ONCE'), '', 'comments-report-form');
  1061. }
  1062. return $response;
  1063. }
  1064.  
  1065. public static function BanIP($id)
  1066. {
  1067. if (JCommentsSecurity::badRequest() == 1) {
  1068. JCommentsSecurity::notAuth();
  1069. }
  1070.  
  1071. $acl = JCommentsFactory::getACL();
  1072. $response = JCommentsFactory::getAjaxResponse();
  1073. if ($acl->canBan()) {
  1074. $config = JCommentsFactory::getConfig();
  1075. if ($config->getInt('enable_blacklist') == 1) {
  1076. $id = (int) $id;
  1077.  
  1078. $comment = JTable::getInstance('Comment', 'JCommentsTable');
  1079. if ($comment->load($id)) {
  1080. // we will not ban own IP ;)
  1081. if ($comment->ip != $acl->getUserIP()) {
  1082. $options = array();
  1083. $options['ip'] = $comment->ip;
  1084. // check if this IP already banned
  1085. if (JCommentsSecurity::checkBlacklist($options)) {
  1086.  
  1087. $result = JCommentsEventHelper::trigger('onJCommentsUserBeforeBan', array(&$comment, &$options));
  1088.  
  1089. if (!in_array(false, $result, true)) {
  1090. $blacklist = JTable::getInstance('Blacklist', 'JCommentsTable');
  1091. $blacklist->ip = $comment->ip;
  1092. $blacklist->created = JFactory::getDate()->toSql();
  1093. $blacklist->created_by = $acl->getUserId();
  1094.  
  1095. if ($blacklist->store()) {
  1096. JCommentsEventHelper::trigger('onJCommentsUserAfterBan', array(&$comment, $options));
  1097. self::showInfoMessage(JText::_('SUCCESSFULLY_BANNED'), 'comment-item-' . $id);
  1098. }
  1099. }
  1100. } else {
  1101. self::showErrorMessage(JText::_('ERROR_IP_ALREADY_BANNED'), '', 'comment-item-' . $id);
  1102. }
  1103. } else {
  1104. self::showErrorMessage(JText::_('ERROR_YOU_CAN_NOT_BAN_YOUR_IP'), '', 'comment-item-' . $id);
  1105. }
  1106. }
  1107. }
  1108. }
  1109. return $response;
  1110. }
  1111.  
  1112. public static function refreshObjectsAjax()
  1113. {
  1114. $app = JFactory::getApplication();
  1115.  
  1116. $hash = $app->input->post->get('hash', '');
  1117. $step = $app->input->post->getInt('step');
  1118. $lang = $app->input->post->get('lang', '');
  1119.  
  1120. $count = 50;
  1121.  
  1122. if ($hash === md5($app->getCfg('secret'))) {
  1123. $db = JFactory::getDBO();
  1124.  
  1125. if ($step == 0) {
  1126. if ($app->getCfg('caching') != 0) {
  1127. // clean cache for all object groups
  1128. $db->setQuery('SELECT DISTINCT object_group FROM #__jcomments_objects');
  1129. $rows = $db->loadColumn();
  1130. foreach ($rows as $row) {
  1131. $cache = JFactory::getCache('com_jcomments_objects_'.strtolower($row));
  1132. $cache->clean();
  1133. }
  1134. }
  1135.  
  1136. $db->setQuery('TRUNCATE TABLE #__jcomments_objects');
  1137. $db->execute();
  1138. }
  1139.  
  1140. $where = array();
  1141. $where[] = 'IFNULL(c.lang, "") <> ""';
  1142.  
  1143. // count objects without information
  1144. $query = "SELECT COUNT(DISTINCT c.object_id, c.object_group, c.lang)"
  1145. . " FROM #__jcomments AS c"
  1146. . (count($where) ? ("\nWHERE " . implode(' AND ', $where)) : "")
  1147. ;
  1148.  
  1149. $db->setQuery($query);
  1150. $total = (int) $db->loadResult();
  1151.  
  1152. $count = 0;
  1153.  
  1154. if ($total > 0) {
  1155. $where[] = 'NOT EXISTS (SELECT o.id FROM #__jcomments_objects AS o WHERE o.object_id = c.object_id AND o.object_group = c.object_group AND o.lang = c.lang)';
  1156.  
  1157. // get list of first objects without information
  1158. $query = "SELECT DISTINCT c.object_id, c.object_group, c.lang"
  1159. . " FROM #__jcomments AS c"
  1160. . (count($where) ? ("\nWHERE " . implode(' AND ', $where)) : "")
  1161. . " ORDER BY c.object_group, c.lang"
  1162. ;
  1163.  
  1164. $db->setQuery($query, 0, $count);
  1165. $rows = $db->loadObjectList();
  1166.  
  1167. $i = 0;
  1168. $multilanguage = JCommentsMultilingual::isEnabled();
  1169.  
  1170. $nextLanguage = $lang;
  1171. if (count($rows)) {
  1172. foreach ($rows as $row) {
  1173. if ($nextLanguage != $row->lang && $multilanguage) {
  1174. $nextLanguage = $row->lang;
  1175. break;
  1176. }
  1177.  
  1178. // retrieve and store object information
  1179. JCommentsObjectHelper::storeObjectInfo($row->object_id, $row->object_group, $row->lang, false, true);
  1180. $i++;
  1181. }
  1182. }
  1183.  
  1184. if ($i > 0) {
  1185. $db->setQuery("SELECT COUNT(*) FROM #__jcomments_objects");
  1186. $count = (int) $db->loadResult();
  1187. }
  1188.  
  1189. $percent = ceil(($count / $total) * 100);
  1190. $percent = min($percent, 100);
  1191. } else {
  1192. $percent = 100;
  1193. }
  1194.  
  1195. $step++;
  1196.  
  1197. $lang_codes = JLanguageHelper::getLanguages('lang_code');
  1198. $language_sef = isset($lang_codes[$nextLanguage]) ? $lang_codes[$nextLanguage]->sef : $nextLanguage;
  1199.  
  1200. $data = array('count' => $count,
  1201. 'total' => $total,
  1202. 'percent' => $percent,
  1203. 'step' => $step,
  1204. 'hash' => $hash,
  1205. 'object_group' => null,
  1206. 'lang' => $nextLanguage,
  1207. 'lang_sef' => $language_sef,
  1208. );
  1209.  
  1210. echo json_encode($data);
  1211. }
  1212.  
  1213. $app->close();
  1214. }
  1215. }
  1216.  
  1217. $result = ob_get_contents();
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
importPackage(java.io);
importPackage(java.lang);