<?php
class links_broken
{
	var $mydirname;
	var $controller_name;
	var $Links;

	/** Constructor
	 *
	**/
	function __construct($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();
	}

	function index($lid = 0)
	{
		$this->form($lid);
	}

	function form($lid = 0)
	{
		if ($link = $this->Links->getDataByLid($lid)) {

			$bread = $this->Links->getBreadcrumbs();
			$bread[] = array('name' => $link['title']);
			$data = array(
				'link'              => $link,
				'xoops_breadcrumbs' => $bread,
				'gticket'           => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__, 1800, 'links_broken'),
			);
			$template = $this->Links->getTemplatePrefix() . '_broken.html';
			$this->Links->view($template, $data);

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function post()
	{
		$js_check = TRUE;
		$this->Links->basicPostCheck('links_broken', $js_check);

		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		$return_msg = $this->_post_common($lid);

		if (empty($return_msg)) {
			$this->Links->linksRedirectHeader($this->Links->makeUrl('index', 'item', array('lid' => $lid)), 2, _MD_THANKSFORINFO);
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, $return_msg);
		}
	}

	function _post_common($lid)
	{
		$msg = '';

		if ($link = $this->Links->getDataByLid($lid)) {

			$this->db_model = $this->Links->setDbModel($this->controller_name);

			//DB Check
			if ($this->Links->uid != 0) {
				$this->db_model->setWhere('lid', $lid, '=');
				$this->db_model->setWhere('sender', $this->Links->uid, '=');
			//	$this->db_model->setWhere("lid=$lid AND sender=". $this->Links->uid);
				$count = $this->db_model->getCount();
				if ($count > 0) {
					$msg = _MD_ALREADYREPORTED;
					return $msg;
				}
			} else {
				$ip = getenv("REMOTE_ADDR");

				if (!empty($ip)) {
					$this->db_model->setWhere('lid', $lid, '=');
					$this->db_model->setWhere('ip', $ip, '=');
				//	$this->db_model->setWhere("lid=$lid AND ip='$ip'");
					$count = $this->db_model->getCount();
					if ($count > 0) {
						$msg = _MD_ALREADYREPORTED;
						return $msg;
					}
				}
			}

			if ($this->db_model->insert($lid)) {
				return $msg;
			} else {
				$msg = _ERRORS;
				return $msg;
			}

		} else {
			$msg = _MD_CANT_GET_DATA;
			return $msg;
		}
	}

	// for javascript
	function post_j()
	{
		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		//Check Referer
		if (! (strpos(xoops_getenv('HTTP_REFERER'),XOOPS_URL)===0)) {
			die('Illegal Referer');
		}

		if (! $GLOBALS['xoopsGTicket']->check(TRUE, 'links_broken', FALSE)) {
			$new_gticket = $GLOBALS['xoopsGTicket']->issue(__LINE__,1800,'links_broken');
			echo $this->Links->returnEnc("[{'status':'ERR','lid':'". $lid ."','msg':'". str_replace("\n","",$GLOBALS['xoopsGTicket']->getErrors()) ."','mode':'broken','gticket':'". $new_gticket ."'}]");
			exit();
		}

		$return_msg = $this->_post_common($lid);

		$new_gticket = $GLOBALS['xoopsGTicket']->issue(__LINE__,1800,'links_broken');
		if (empty($return_msg)) {
			echo $this->Links->returnEnc("[{'status':'OK','lid':'". $lid ."','msg':'". _MD_THANKSFORINFO ."','mode':'broken','gticket':'". $new_gticket ."'}]");
		} else {
			echo $this->Links->returnEnc("[{'status':'ERR','lid':'". $lid ."','msg':'". $return_msg ."','mode':'broken','gticket':'". $new_gticket ."'}]");
		}

	}

}
