<?
   // echo "funs.php 成功載入<BR>";
   require_once ("./etc/init.php");
	
   class W_user
   {
      var $operator = "";	 // 信件類別 identification、new_password)
      var $user = "";		 // 使用者帳號
      var $email = "";		 // 使用者註冊 e-mail
      var $new_password = "";	 // 新密碼
      var $activate_code = "";	 // 認證碼
      var $hadoop_user = "";	 // hadoop 帳號
      var $hadoop_password = ""; // hadoop 密碼
      var $reg_date = "";	 // 註冊日期

      // 取得現在時間
      function get_current_date()
      {
	 return date("Y-m-d, H:i:s");
      }

      // 重新導向
      function redirect_to($w_url, $message)
      {
	 header ("refresh:5 ;url=$w_url");
         echo '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">';
         echo "<BR> 此網頁將於 5 秒後重新導向 <BR>";
	 echo "<BR> ======== System Message ========";
	 echo "<BR>" . $message;
      }

      // 取得認證信內容
      function get_activate_mailbody()
      {
	 global $w_localhost;	// 載入主機位址環境變數
	 $str = '
		  <br>
		  <br>您好：
		  <br>
		  <br>請點選以下認證碼啟動您的 Hadoop 帳號：
		  <br>
		  <br>
		  ';
	 $str .= "<br><A href=" . "$w_localhost" . "/check_activate_code.php?user=" .
	 $this->user . "&" . "act=" . $this->activate_code . ">啟動 Hadoop 帳號</A>";
	 return $str;
      }

      // 取得密碼信內容
      function get_new_password_mailbody()
      {
	 global $w_localhost;        // 載入主機位址環境變數
	 $str = '
		  <br>
		  <br>您好：
		  <br>
		  <br>以下為您新的密碼：
		  <br>
		  <br>
		  ';
	 $str .= "
		  <br>請使用以下設定
		  <br>帳號：$this->user
		  <br>密碼：$this->new_password
		  <br>登入：$w_localhost
		  ";
	 return $str;
      }

      // 取得 hadoop 帳號密碼信內容
      function get_hadoop_user_password_mailbody()
      {
	 global $w_localhost;       // 載入主機位址環境變數
	 global $w_ssh_server;	    // 載入 ssh 主機位址
	 $str = '
		  <br>
		  <br>您好：
		  <br>
		  <br>以下為您所申請的 Hadoop 帳號及密碼：
		  <br>
		  <br>
		  ';
	 $str .= "
		  <br>位址：$w_localhost
		  <br>帳號：$this->hadoop_user
		  <br>密碼：$this->hadoop_password
		  <br>
		  <br>請用<a href=\"http://www.csie.ntu.edu.tw/~piaip/pietty/\">SSH Client</a>登入使用。
		  <br>Ex. ssh $w_ssh_server -l $this->hadoop_user
		  ";
	 return $str;
      }

      // 取得認證碼
      function get_activate_code()
      {
	 return md5($this->user . microtime() );	
      }

      // 取得新密碼
      function get_new_password()
      {
	 return rand();
      }
   }

   class W_mysql
   {
      function connect()
      {
	 require ("init.php");
	 // 連結資料庫
	 $link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error());
	 mysql_select_db($mysql_database, $link) or die(mysql_error() );
	 mysql_query("SET NAMES 'utf8'");
	 return $link;
      }

      function count_is_active_user($link)
      {
	 require ("init.php");
	 $str = sprintf("SELECT COUNT(*)
			 FROM `%s` 
			 WHERE `is_activate` = 1
			 GROUP BY `is_activate`
			",$mysql_table);
	 $result = mysql_query($str , $link) or die(mysql_error() );
	 $row = mysql_fetch_row($result);
	 return $row[0]; 
      }
      function close($link)
      {
	 mysql_close($link);
      }
   }

   function test()
   {
      echo "<br>========";
      echo "<br>this is in W_user->test() <br>";
      $str = "<br><A href=" . "$w_localhost" . "/check_activate_code.php?user=" .
      $this->user . "&" . "act=" . $this->activate_code . ">啟動 Hadoop 帳號</A>";
      //echo $this->user;
      //echo $this->activate_code;
   }
?>
