<?php
/**
* AkoComment - A User Comments Component
* @version 2.0
* @package AkoComment
* @copyright (C) 2004 by Arthur Konze - All rights reserved!
* @license http://www.konze.de/content/view/8/26/ Copyrighted Commercial Software
*******************************
* editd by zwiastun: added language variable and changed line: 277  
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPrepareContent', 'botAkoComment' );

function botAkoComment( $published, &$row, &$params, $page=0 ) {

  if (!$published) {
    $row->text = str_replace( "{moscomment}", "", $row->text );
    return;
  }

  # Variables - Don't change anything here!!!
  global $database, $mainframe, $option, $task, $mosConfig_lang, $mosConfig_absolute_path, $mosConfig_live_site, $my, $Itemid;

  # STOP! THE REMOVAL OF THE POWERED BY LINE IS NOT ALLOWED.
  # IF YOU WANT TO REMOVE IT, CONTACT ME AT www.konze.de FOR DETAILS!
  $akocpversion = "<p align='center'><span class='small'>Powered by <a href='http://www.mamboportal.com/' target='_blank'>AkoComment</a></p>";

  # Load configuration file
  require($mosConfig_absolute_path."/administrator/components/com_akocomment/config.akocomment.php");
  $seclistarray = explode (",", $ac_sectionlist);

  if ((($ac_mainmode == "0") AND (strpos($row->text, "{moscomment}") !== false)) OR (($ac_mainmode == "1") AND (in_array ($row->sectionid, $seclistarray)))) {

    # Prepare smiley array
    $smiley[':)']     = "sm_smile.gif";    $smiley[':grin']  = "sm_biggrin.gif";
    $smiley[';)']     = "sm_wink.gif";     $smiley['8)']     = "sm_cool.gif";
    $smiley[':p']     = "sm_razz.gif";     $smiley[':roll']  = "sm_rolleyes.gif";
    $smiley[':eek']   = "sm_bigeek.gif";   $smiley[':upset'] = "sm_upset.gif";
    $smiley[':zzz']   = "sm_sleep.gif";    $smiley[':sigh']  = "sm_sigh.gif";
    $smiley[':?']     = "sm_confused.gif"; $smiley[':cry']   = "sm_cry.gif";
    $smiley[':(']     = "sm_mad.gif";      $smiley[':x']     = "sm_dead.gif";

    # Get the right language if it exists
    if (file_exists($mosConfig_absolute_path.'/components/com_akocomment/languages/'.$mosConfig_lang.'.php')) {
      include_once($mosConfig_absolute_path.'/components/com_akocomment/languages/'.$mosConfig_lang.'.php');
    } else {
      include_once($mosConfig_absolute_path.'/components/com_akocomment/languages/english.php');
    }

    # Load function library
    require_once($mosConfig_absolute_path.'/components/com_akocomment/functions.akocomment.php');

    # Check number of comments
    $database->setQuery( "SELECT count(*) FROM #__akocomment WHERE contentid='$row->id' AND published='1'" );
    $total = $database->loadResult();
    echo $database->getErrorMsg();
    if ($option=='com_content' AND $task=='view') {

      # Check for Editor rights
      $is_editor = (strtolower($my->usertype) == 'editor' || strtolower($my->usertype) == 'publisher' || strtolower($my->usertype) == 'manager' || strtolower($my->usertype) == 'administrator' || strtolower($my->usertype) == 'super administrator' );
      $is_user   = (strtolower($my->usertype) <> '');

      $akocomments = "<div class=\"article_separator\"></div>";
      if ($total>=0) {
        # Show Comments if exist
        $akocomments .= "<table width='100%' border='0' cellspacing='1' cellpadding='4'>";
        $akocomments .= "<tr><td class='contentheading'>"._AKOCOMMENT_COMMENTS."</td></tr>";
        $line=1;
        $database->setQuery( "SELECT id as acid, title as actitle, name as acname, date as acdate, comment as accomment FROM #__akocomment WHERE contentid='$row->id' AND published='1' ORDER BY id $ac_sorting" );
        $acrows = $database->loadObjectList();
        if ($database->getErrorNum()) {
          echo $database->stderr();
          return false;
        }
        for ($i=0, $n=count( $acrows ); $i < $n; $i++) {
          $acrow = &$acrows[$i];
          $linecolor = ($line % 2) + 1;
          
          //Filter Bad words
          require_once( 'components/com_badwords2/class.badwords2.php' );
          $badword_flag = Badword::filter ( "$acrow->actitle $acrow->accomment", $my );
          if (!$is_editor && $badword_flag) {
             $acrow->actitle = Badword::flush($acrow->actitle, $my);
             $acrow->accomment = Badword::flush($acrow->accomment, $my);
          }

          $akocomments .= "<tr class='sectiontableentry".$linecolor."'><td valign='top'><a name='akocomment".$acrow->acid."'></a>";
          if ($acrow->actitle) {
            $acrow->actitle = stripslashes($acrow->actitle);
            $akocomments .= "<b>$acrow->actitle</b><br />";
          }

          //Throw Badword message
          if ($badword_flag) {
             $akocomments .=  _BW2_MODERATED_ENTRY ;
             if ( $is_editor ) {$akocomments .=  " " . _BW2_EDITOR_VIEW ;}
             $akocomments .=  "<br />";
          }
          
          $acrow->acname = stripslashes($acrow->acname);
          $akocomments .= "<span class='small'>"._AKOCOMMENT_WRITTENBY." $acrow->acname "._AKOCOMMENT_ON." $acrow->acdate</span><hr />";
          $parsedcomment= AkoParse($acrow->accomment, $smiley, $ac_bbcodesupport, $ac_picsupport, $ac_smiliesupport, $mosConfig_live_site);
          $parsedcomment= textwrap($parsedcomment, $width = 60);
          $akocomments .= "$parsedcomment";
          $akocomments .= "</td></tr>";
          $line++;
        }
        $akocomments .= "</table><div class=\"article_separator\"></div>";
      }

      # Check if Registered Users only
      if (!$ac_anonentry AND !$is_user) {
        $akoinputforum = "<p>"._AKOCOMMENT_ONLYREGISTERED."</p>";
      } else {
        # Javascript for Form Check
        $akoinputforum = "";
        $akoinputforum .= "<script language=\"Javascript\">";
        $akoinputforum .= "  function x () {";
        $akoinputforum .= "    return;";
        $akoinputforum .= "  }";
        $akoinputforum .= "  function ac_smilie(thesmile) {";
        $akoinputforum .= "    document.akocommentform.comment.value += \" \"+thesmile+\" \";";
        $akoinputforum .= "    document.akocommentform.comment.focus();";
        $akoinputforum .= "  }";
        $akoinputforum .= "  function validate(){";
        $akoinputforum .= "    if (document.akocommentform.comment.value==''){";
        $akoinputforum .= "      alert(\""._AKOCOMMENT_FORMVALIDATE."\");";
        $akoinputforum .= "    } else {";
        $akoinputforum .= "      document.akocommentform.action = 'index.php';";
        $akoinputforum .= "      document.akocommentform.submit();";
        $akoinputforum .= "    }";
        $akoinputforum .= "  }";
        if ($ac_bbcodesupport) {
          $akoinputforum .= "function DoPrompt(action) {";
          $akoinputforum .= "  var revisedMessage;";
          $akoinputforum .= "  var currentMessage = document.akocommentform.comment.value;";
          $akoinputforum .= "  if (action == \"url\") {";
          $akoinputforum .= "    var thisURL = prompt(\""._A_AKOCOM_B_WWW_URL_MSG."\", \"http://\");";
          $akoinputforum .= "    var thisTitle = prompt(\""._A_AKOCOM_B_WWW_TITLE_MSG."\", \""._A_AKOCOM_B_WWW_TITLE_."\");";
          $akoinputforum .= "    var urlBBCode = \"[URL=\"+thisURL+\"]\"+thisTitle+\"[/URL]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+urlBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"email\") {";
          $akoinputforum .= "    var thisEmail = prompt(\""._A_AKOCOM_B_EMAIL."\", \"\");";
          $akoinputforum .= "    var emailBBCode = \"[EMAIL]\"+thisEmail+\"[/EMAIL]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+emailBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"bold\") {";
          $akoinputforum .= "    var thisBold = prompt(\""._A_AKOCOM_B_BOLD_MSG."\", \"\");";
          $akoinputforum .= "    var boldBBCode = \"[B]\"+thisBold+\"[/B]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+boldBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"italic\") {";
          $akoinputforum .= "    var thisItal = prompt(\""._A_AKOCOM_B_ITALIC_MSG."\", \"\");";
          $akoinputforum .= "    var italBBCode = \"[I]\"+thisItal+\"[/I]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+italBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"underline\") {";
          $akoinputforum .= "    var thisUndl = prompt(\""._A_AKOCOM_B_UNDERL_MSG."\", \"\");";
          $akoinputforum .= "    var undlBBCode = \"[U]\"+thisUndl+\"[/U]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+undlBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"image\") {";
          $akoinputforum .= "    var thisImage = prompt(\""._A_AKOCOM_B_IMG_URL."\", \"http://\");";
          $akoinputforum .= "    var imageBBCode = \"[IMG]\"+thisImage+\"[/IMG]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+imageBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"quote\") {";
          $akoinputforum .= "    var quoteBBCode = \"[QUOTE]  [/QUOTE]\";";
                $akoinputforum .= "    var thisQuote = prompt(\""._A_AKOCOM_B_QUOTE_MSG."\", \"\");";
          $akoinputforum .= "    var quoteBBCode = \"[QUOTE]\"+thisQuote+\"[/QUOTE]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+quoteBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"code\") {";
          $akoinputforum .= "    var codeBBCode = \"[CODE]  [/CODE]\";";
                $akoinputforum .= "    var thisCode = prompt(\""._A_AKOCOM_B_CODE_MSG."\", \"\");";
          $akoinputforum .= "    var codeBBCode = \"[CODE]\"+thisCode+\"[/CODE]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+codeBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"listopen\") {";
          $akoinputforum .= "    var liststartBBCode = \"[LIST]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+liststartBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"listclose\") {";
          $akoinputforum .= "    var listendBBCode = \"[/LIST]\";";
          $akoinputforum .= "    revisedMessage = currentMessage+listendBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "  if (action == \"listitem\") {";
          $akoinputforum .= "    var thisItem = prompt(\""._A_AKOCOM_B_LIST_MSG."\", \"\");";
          $akoinputforum .= "    var itemBBCode = \"[*]\"+thisItem;";
          $akoinputforum .= "    revisedMessage = currentMessage+itemBBCode;";
          $akoinputforum .= "    document.akocommentform.comment.value=revisedMessage;";
          $akoinputforum .= "    document.akocommentform.comment.focus();";
          $akoinputforum .= "    return;";
          $akoinputforum .= "  }";
          $akoinputforum .= "}";
        }
        $akoinputforum .= "</SCRIPT>";
        $akoinputforum .= "<br /><br /><hr /><TABLE align='center' width='100%' CELLPADDING='4' CELLSPACING='1' BORDER='0'>";
        $akoinputforum .= "<tr><td colspan='2' class='sectiontableheader'>"._AKOCOMMENT_WRITECOMMENT."</td></tr>";
        $akoinputforum .= "<FORM NAME='akocommentform' ACTION='index.php' TARGET=_top METHOD='POST'>";
        $akoinputforum .= "<INPUT TYPE='hidden' NAME='option' value='com_akocomment'/>";
        $akoinputforum .= "<INPUT TYPE='hidden' NAME='acitemid' value='$Itemid'/>";
        $akoinputforum .= "<INPUT TYPE='hidden' NAME='contentid' value='$row->id'/>";
        $akoinputforum .= "<INPUT TYPE='hidden' NAME='func' value='entry'/>";
        $akoinputforum .= "<TR class='sectiontableentry1'><TD style=\"width:150px;\">"._AKOCOMMENT_ENTERNAME."</TD><TD>";
        if ($my->username) {
          $akoinputforum .= "$my->username<INPUT TYPE='hidden' NAME='acname' value='$my->username'>";
        } else {
          $akoinputforum .= _AKOCOMMENT_GUESTNAME."<INPUT TYPE='hidden' NAME='acname' value='"._AKOCOMMENT_GUESTNAME."'>";
        }
        $akoinputforum .= "</TD></TR>";
        $akoinputforum .= "<TR class='sectiontableentry1'><TD>"._AKOCOMMENT_ENTERTITLE."</TD><TD><INPUT TYPE='text' NAME='title' style='width:75%;' class='inputbox'/></TD></TR>";
        # Switch for BB Code support
        if ($ac_bbcodesupport) {
          $akoinputforum .= "<TR class='sectiontableentry1'><TD WIDTH='130'>BBCode:</TD><TD>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"url\");'><IMG src='components/com_akocomment/images/bburl.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_WEB_ADDRESS."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"email\");'><IMG src='components/com_akocomment/images/bbemail.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_EMAIL_ADDRESS."'/></A>";
          if ($ac_picsupport) $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"image\");'><IMG src='components/com_akocomment/images/bbimgsrc.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_IMAGE_LOAD."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"bold\");'><IMG src='components/com_akocomment/images/bbbold.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_BOLD_TEXT."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"italic\");'><IMG src='components/com_akocomment/images/bbitalic.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_ITALIC_TEXT."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"underline\");'><IMG src='components/com_akocomment/images/bbunderline.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_UNDERL_TEXT."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"quote\");'><IMG src='components/com_akocomment/images/bbquote.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_QUOTE."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"code\");'><IMG src='components/com_akocomment/images/bbcode.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_CODE."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"listopen\");'><IMG src='components/com_akocomment/images/bblistopen.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_OPEN_LIST."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"listitem\");'><IMG src='components/com_akocomment/images/bblistitem.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_ITEM_LIST."'/></A>";
          $akoinputforum .= "<A href='javascript: x()' onClick='DoPrompt(\"listclose\");'><IMG src='components/com_akocomment/images/bblistclose.gif' hspace='1' border='0' alt='"._A_AKOCOM_B_CLOSE_LIST."'/></A>";
          $akoinputforum .= "</TD></TR>";
        }
        $akoinputforum .= "<TR class='sectiontableentry1'><TD valign='top'>"._AKOCOMMENT_ENTERTEXT."<br /><br />";
        # Switch for Smilie Support
          if ($ac_smiliesupport) {
            $count=1;
            foreach ($smiley as $i=>$sm) {
              $akoinputforum .= "<a href=\"javascript:ac_smilie('$i')\" title='$i'><img src='$mosConfig_live_site/components/com_akocomment/images/$sm' border='0'/></a> ";
              if ($count%5==0) $akoinputforum .= "<br/>";
              $count++;
            }
          }
        $akoinputforum .= "</TD><TD valign='top'><TEXTAREA style='width:75%;' ROWS='8' NAME='comment' class='inputbox' wrap='VIRTUAL'></TEXTAREA></TD></TR>";
        $akoinputforum .= "<TR class='sectiontableentry1'><TD> </TD><TD align='left'><INPUT TYPE='button' NAME='send' VALUE='"._AKOCOMMENT_SENDFORM."' class='button' onClick='validate()'></TD></TR></TABLE></FORM>";
        $akoinputforum .= "";
      }

      # Check for right placement
      if ($ac_formposition) {
        $akocomreplace = "$akoinputforum$akocomments$akocpversion";
      } else {
        $akocomreplace = "$akocomments$akoinputforum$akocpversion";
      }

      if ($ac_mainmode == "0") {
        $row->text = str_replace( "{moscomment}", $akocomreplace, $row->text );
      } elseif (in_array ($row->sectionid, $seclistarray)) {
        $row->text = str_replace( "{moscomment}", "", $row->text );
        $row->text = $row->text.$akocomreplace;
      }

    # If we are not on the content page
    } else {
      $replacementlink = ($ac_openingmode) ? "<a class=\"readon\" href=\"javascript:void window.open('".sefRelToAbs("index2.php?option=com_content&task=view&id=$row->id&pop=1&page=0")."', 'win2', 'status=no,toolbar=no,scrollbars=yes,titlebar=no,menubar=no,resizable=yes,width=640,height=480,directories=no,location=no');\">" : "<a class=\"readon\" href='".sefRelToAbs("index.php?option=com_content&task=view&id=$row->id&Itemid=$Itemid")."'>";
      if ($ac_mainmode == "0") {
        $row->text = str_replace( "{moscomment}", "<p><img src='$mosConfig_live_site/components/com_akocomment/images/comment.png' hspace='5' align='absmiddle'>$replacementlink"._AKOCOMMENT_WRITECOMMENT." ($total "._AKOCOMMENT_COMMENTS.")</a></p>", $row->text );
      } elseif (in_array ($row->sectionid, $seclistarray)) {
        $row->text = str_replace( "{moscomment}", "", $row->text );
        $row->text = $row->text."$replacementlink"._AKOCOMMENT_WRITECOMMENT." ($total "._AKOCOMMENT_COMMENTS.")</a>";
      }
    }
  }

  return true;
}

?>
