00001 <?
00012 abstract class BaseTicket extends MyModule
00013 {
00017 private $hash;
00018
00022 private $url;
00023
00027 private $user;
00028
00032 private $table;
00033
00039 function __construct($hash = null, $table = 'tickets')
00040 {
00041 $this->hash = $hash;
00042 $this->table = $table;
00043
00044 parent::__construct();
00045 }
00046
00052 function getPagesXml()
00053 {
00054 $xml = parent::getPagesXml();
00055 $xml .= "<page name=\"gate\">\n";
00056 $xml .= "\t<param name=\"ticket\" required=\"1\" regex=\"/^[0-9a-f]{16}$/\" />\n";
00057 $xml .= "\t<param name=\"url\" />\n";
00058 $xml .= "</page>";
00059
00060 return $xml;
00061 }
00062
00063 function getCreateTableSql()
00064 {
00065 return <<<SQL
00066 CREATE TABLE `$this->table` (
00067 `hash` varchar(16) NOT NULL default '',
00068 `user_id` int(11) NOT NULL default '0',
00069 `url` varchar(255) NOT NULL default '',
00070 `expire` datetime NOT NULL default '0000-00-00 00:00:00',
00071 PRIMARY KEY (`hash`),
00072 KEY `user_id` (`user_id`)
00073 ) ENGINE=InnoDB;
00074 SQL;
00075 return $sql;
00076 }
00077
00084 public function initGatePage()
00085 {
00086 $this->hash = $this->params('ticket');
00087
00088 if (!$this->process())
00089 {
00090 if ($this->params('url'))
00091 Util::redirect(base64_decode($this->params('url')));
00092 else
00093 throw new PageError("No fallback url found. You must use the entire link contained in the email.");
00094 }
00095 }
00096
00100 public function drawMainPage()
00101 {
00102 global $me;
00103 }
00104
00110 public function verify()
00111 {
00112 $hashRs = dbQuery("
00113 SELECT *
00114 FROM $this->table
00115 WHERE hash = '$this->hash'
00116 ");
00117 if (dbGetNumRows($hashRs))
00118 return dbFetchAssoc($hashRs);
00119 return false;
00120 }
00121
00131 public function generate($url, $user, $expire = null)
00132 {
00133
00134 $this->hash = substr(sha1(mt_rand()), 0, 16);
00135 $this->url = $url;
00136 $this->user = $user;
00137
00138
00139 if ($expire === null)
00140 $this->expire = date('Y-m-d H:i:s', strtotime('+2 month'));
00141
00142
00143 dbExecute("
00144 INSERT INTO $this->table
00145 (user_id, hash, url, expire)
00146 VALUES
00147 ('$user->id', '$this->hash', '$this->url', '$this->expire')
00148 ");
00149
00150 return $hash;
00151 }
00152
00158 public function url()
00159 {
00160
00161 return $this->getUrl(".gate?ticket=$this->hash&url=" . base64_encode($this->url), null, true);
00162 }
00163
00169 public function process()
00170 {
00171 if ($hash = $this->verify())
00172 {
00173
00174 $user = new User($hash['user_id']);
00175 $user->doLogin();
00176
00177
00178 $this->destroy();
00179
00180
00181 Util::redirect($hash['url']);
00182
00183 return true;
00184 }
00185
00186 return false;
00187 }
00188
00192 public function destroy()
00193 {
00194 dbExecute("
00195 DELETE FROM $this->table
00196 WHERE hash = '$this->hash'
00197 ");
00198 }
00199 }
00200 ?>