<?php

include_once( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
include_once( PLOG_CLASS_PATH."class/view/plugintemplatedview.class.php" );

include_once( PLOG_CLASS_PATH."plugins/shoutbox/class/dao/shoutbox.class.php" );
include_once( PLOG_CLASS_PATH."class/view/errorview.class.php" );
include_once( PLOG_CLASS_PATH."class/view/redirectview.class.php" );

/**
  * shows a form with the current configuration
*/
class PluginShoutboxNewShoutAction extends BlogAction {
    
  function PluginShoutboxNewShoutAction( $actionInfo, $request ) {
      $this->BlogAction( $actionInfo, $request );
  }
  
  function perform() {  
    // load some configuration settings
    $blogSettings = $this->_blogInfo->getSettings();
    
    $locale = Locales::getPluginLocale('shoutbox', $blogSettings->getValue('locale'));

    // Get the shoutbox entries
    $s = new Shoutbox($this->_blogInfo->getId(), $blogSettings);
    
    $error = false;
    if(!$s->shout_enabled) {
      $error = $locale->tr("temp_disabled");
    }

    //$nickname = $this->_request->getValue('nickname');
    $nickname = $this->_request->getValue( 'shoutbox_nickname' );
    $nickname = strip_tags($nickname);
    $protected_nicks = $s->protected_nicks;
    $protection_password = $s->protection_password;
  
    //echo("Controllo :$protection_password contro $nickname<br>");
    $usesProtectedNick = false;
    $parts = explode(' ', $s->protected_nicks);
    //echo("PN: $protected_nicks <pre>" . print_r($parts) ."</pre>");
  
    foreach($parts as $part) {
        //echo("Controllo $part contro $nickname<br>");
        if(preg_match("/$part/i", $nickname)) {
            $usesProtectedNick = true;
            continue;
        }
    }
  
    if($usesProtectedNick && !$error) {
        if(!preg_match("/:$protection_password\$/", $nickname)) {
            $error = $locale->tr("nickname_is_protected");
            if($s->mail_alert) {
                $s->notifyUser($nickname, $shoutText, "alert");
            }
        }
        else {
            $nickname = preg_replace("/:$protection_password\$/", "", $nickname);
        }
    }
    
  
    //$shoutText = $this->_request->getValue('shoutText');
    $shoutText = $this->_request->getValue( 'shoutbox_text' );
    $shoutText = strip_tags($shoutText, '<a><br><b><i><strong>');
    if (!get_magic_quotes_gpc()) {
        $nickname = addslashes($nickname);
        $shoutText = addslashes($shoutText);
    }
    if(strlen($nickname) == 0 || strlen($shoutText) == 0) {
        if(!$error)
          $error = $locale->tr("missing_content");
    }
    else if($nickname == $s->default_nick || $shoutText == $s->default_shout) {
        if(!$error)
          $error = $locale->tr("default_shouted");
    }
    else if(strlen($shoutText) > $s->max_length) {
        if(!$error)
          $error = $locale->tr("too_long_shout");
    }
    else if(strlen($nickname) > 20) {
        if(!$error)
          $error = $locale->tr("too_long_nick");
    }

    
    if($error) {
      $this->_view = new ErrorView($this->_blogInfo );
      $this->_view->setErrorMessage($error);
      $this->setCommonData();
      return true;
    }

    $s->addShout($nickname, $shoutText);
    
    // create a view and export the settings to the template
    /*
    $this->_view = new PluginTemplatedView($this->_blogInfo, "shoutbox", "shoutbox_newshout" );
    $this->_view->setValue("returnPage", $_POST['shoutbox_return']);
    */

    if (array_key_exists( 'HTTP_REFERER', $_SERVER )) {
		$this->_view = new RedirectView( $_SERVER['HTTP_REFERER'] );
	}
    else {
    	$this->_view = new InfoView($this->_blogInfo );
    	$this->_view->setInfoMessage("shout_added");
	}
      
    
    $this->setCommonData();
    
    // Now reset this blog's cache
    CacheControl::resetBlogCache($this->_blogInfo->getId());
    
    return true;
  }
}


define( "INFO_TEMPLATE", "info" );
class InfoView extends BlogView  {
  function InfoView( $blogInfo, $message = null ) {
      $this->BlogView( $blogInfo, INFO_TEMPLATE, SMARTY_VIEW_CACHE_DISABLED );

        $this->_message = $message;
    }

  function setInfoMessage( $message ) {
    $this->setValue( "message", $message );
    
    return( parent::setErrorMessage( $message ));
  }

  function render() {
      if(!empty($this->_message))
        $this->setValue( "message", $this->_message );
    
    parent::render();
  }
}
?>