<?php
if( ! defined( 'XOOPS_ROOT_PATH' ) ) exit ;
require_once dirname(__FILE__) . '/links_Link.class.php';

class links_Base extends links_Link {

	var $mytrustdirname;
	var $mytrustdirpath;
	var $myurl;
	var $mydirname;

	var $module_config;
	var $module_id;
	var $module_name;

	//DATABASE
	var $table_cat;
	var $table_links;
	var $table_text;

	//OBJCT
	var $Categories;

	var $per_page;
	var $allkeycid;
	var $allcid;
	var $bread;
	var $uri_option_arr;
	var $screen_shot;
	var $screen_shot_service   = NULL;
	var $screenshot_cache_time;
	var $category_selector;
	var $uid      = 0;
	var $uname    = '';
	var $realname = '';
	var $isadmin  = FALSE;
	var $groups   = '';
	var $delegate_name   = NULL;
	var $delegate_method = NULL;
	var $is_robot =FALSE;

	var $post_perm     = FALSE;
	var $post_approval = FALSE;
	var $edit_perm     = FALSE;
	var $edit_approval = FALSE;
	var $del_perm      = FALSE;
	var $del_approval  = FALSE;
	var $request_perm  = FALSE;

	var $cache_dir = NULL;
	var $cache_url = NULL;

	/** Constructor
	 *
	**/
	function __construct($mydirname)
	{

		parent::__construct();

		$this->mydirname = $mydirname;
		$this->mytrustdirname = basename(dirname(dirname(__FILE__)));
		$this->mytrustdirpath = XOOPS_TRUST_PATH . '/modules/' . $this->mytrustdirname;
		$this->myurl = XOOPS_URL .'/modules/'. $this->mydirname;

		// Load Files
		$this->_require_files();

		// ADD language file
		$this->_set_language();

		// category
		$this->_set_categories();

		// set DB table
		$this->_set_db_table();

		// Basic setting
		$this->_setting();

		// Permission Check
		$this->_permission_check();

		// Cache Directory Check
		if ($this->screen_shot == 1 && $this->screenshot_cache_time > 0) {
			$this->_check_cache_dir();
		}



	}

	static function getInstance($mydirname)
	{
		static $instance = array();
		if (!isset($instance[$mydirname])) {
			$instance[$mydirname] = new links_Base($mydirname);
		}
		return $instance[$mydirname];
	}

	function _require_files()
	{
		require_once $this->mytrustdirpath . '/class/gtickets.php';
		require_once $this->mytrustdirpath . '/include/functions.php';
		require_once $this->mytrustdirpath . '/include/version.php';
		require_once $this->mytrustdirpath . '/class/categories.class.php';
		require_once XOOPS_TRUST_PATH . '/libs/altsys/class/D3LanguageManager.class.php';
		require_once $this->mytrustdirpath . '/class/links_pagenav.php';
	}

	function _set_language()
	{
		$langman = D3LanguageManager::getInstance() ;
		$langman->read('main.php', $this->mydirname, $this->mytrustdirname) ;
		if (defined('LINKS_ADMIN_AREA') && LINKS_ADMIN_AREA == 1) {
			$langman->read('admin.php', $this->mydirname, $this->mytrustdirname);
			$langman->read('modinfo.php', $this->mydirname, $this->mytrustdirname);
		}
	}

	function _set_categories()
	{
		$this->Categories = new linksCategoriesClass($this->mydirname);
		$this->allcid = $this->Categories->getAllcatKeyCid("corder,cid");
		$this->allkeycid = array_keys($this->allcid);
	}

	function _set_db_table()
	{
		$db = icms_db_Factory::instance();
		$this->table_links = $db->prefix($this->mydirname . '_links');
		$this->table_cat   = $db->prefix($this->mydirname . '_cat');
		$this->table_text  = $db->prefix($this->mydirname . '_text');
	}

	function _setting()
	{
		//Module Config
		$xoopsModule = $this->getXoopsModule();
		$config_handler =& xoops_gethandler('config') ;
		$this->module_id = $xoopsModule->getVar('mid');
		$this->module_config =& $config_handler->getConfigsByCat(0, $this->module_id);

		// module name
		$this->module_name = $xoopsModule->getVar('name');

		//1:multiple, 0:single(js)
		$this->category_selector = $this->module_config['category_selector'] == 1 ? TRUE: FALSE ;

		$this->uri_option_arr = array(
			'url_approach' => intval($this->module_config['url_approach']),
			'url_suffix'   => htmlspecialchars($this->module_config['url_suffix'], ENT_QUOTES, _CHARSET),
		);

		$this->per_page = intval($this->module_config['perpage']);
		$this->screen_shot = intval($this->module_config['screenshot']);
		$this->screenshot_cache_time = intval($this->module_config['screenshot_cache_time']);
		$this->_screen_Shot_Service_Check($this->screen_shot);
	}

	function _permission_check()
	{
		global $xoopsUser, $xoopsConfig;

		if (is_object($xoopsUser)) {
			$this->uid      = $xoopsUser->uid() ;
			$this->uname    = $xoopsUser->uname();
			$this->realname = $xoopsUser->name() ? $xoopsUser->name() : '' ;
			$this->isadmin  = $xoopsUser->isAdmin($this->module_id);
			$this->groups   = $xoopsUser->getGroups();
		} else {
			$this->uid      = 0 ;
			$this->uname    = isset($xoopsConfig) ? htmlspecialchars($xoopsConfig['anonymous'], ENT_QUOTES, _CHARSET) : '' ;
			$this->realname = isset($xoopsConfig) ? htmlspecialchars($xoopsConfig['anonymous'], ENT_QUOTES, _CHARSET) : '' ;
			$this->isadmin  = FALSE ;
			$this->groups   = XOOPS_GROUP_ANONYMOUS;
		}

		$gperm =& xoops_gethandler('groupperm');
		$this->post_perm     = ($gperm->checkRight('links_perm',1,$this->groups,$this->module_id) || $this->isadmin) ? TRUE: FALSE;
		$this->post_approval = ($gperm->checkRight('links_perm',2,$this->groups,$this->module_id) || $this->isadmin) ? TRUE: FALSE;
		$this->edit_perm     = (($gperm->checkRight('links_perm',3,$this->groups,$this->module_id) && is_object($xoopsUser)) || $this->isadmin) ? TRUE: FALSE;
		$this->edit_approval = (($gperm->checkRight('links_perm',4,$this->groups,$this->module_id) && is_object($xoopsUser)) || $this->isadmin) ? TRUE: FALSE;
		$this->del_perm      = (($gperm->checkRight('links_perm',5,$this->groups,$this->module_id) && is_object($xoopsUser)) || $this->isadmin) ? TRUE: FALSE;
		$this->del_approval  = (($gperm->checkRight('links_perm',6,$this->groups,$this->module_id) && is_object($xoopsUser)) || $this->isadmin) ? TRUE: FALSE;
		$this->request_perm  = (is_object($xoopsUser) || $this->module_config['request_perm']) ? TRUE: FALSE;
	}

	function _check_cache_dir()
	{
		$this->cache_dir = XOOPS_ROOT_PATH .'/uploads/'. $this->mydirname .'_capture_cache';
		$this->cache_url = XOOPS_URL .'/uploads/'. $this->mydirname .'_capture_cache';

		$safe_mode = ini_get('safe_mode');

		// check or make dir
		if (!is_dir($this->cache_dir)) {
			if ($safe_mode) {
				$this->linksRedirectHeader(XOOPS_URL ."/", 5, "At first create & chmod 777 '". $this->cache_dir ."' by ftp or shell.");
				exit;
			}

			if (!mkdir($this->cache_dir, 0777)) {
				$this->linksRedirectHeader(XOOPS_URL ."/", 5, $this->cache_dir ." is not a directory");
				exit;
			} else {
				@chmod($this->cache_dir, 0777);
			}
		}

		// check or set permissions of dir
		if (!is_writable($this->cache_dir) || !is_readable($this->cache_dir)) {
			if (!chmod($this->cache_dir, 0777)) {
				$this->linksRedirectHeader(XOOPS_URL ."/", 5, "chmod 0777 into ". $this->cache_dir ." failed");
				exit;
			}
		}
	}

	function _screen_Shot_Service_Check( $useSSS )
	{
		if ($useSSS) {
			if ( check_URI('http://capture.heartrails.com/')==200 ){
				$this->screen_shot_service = 'http://capture.heartrails.com/128x128/shadow?' ;
			} else if ( check_URI('http://mozshot.nemui.org/')==200 ) {
				/* 128x128: http://mozshot.nemui.org/shot?
				   256x256: http://mozshot.nemui.org/shot/large?
				   64x64  : http://mozshot.nemui.org/shot/small?  */
				$this->screen_shot_service = 'http://mozshot.nemui.org/shot?' ;
			} else if ( check_URI('http://img.simpleapi.net/small/')==200 ){
				$this->screen_shot_service = 'http://img.simpleapi.net/small/' ;
			}
		}
	}

}
