· 8 years ago · Jan 02, 2018, 05:24 PM
1<?php
2
3/*
4
5 2012 - Seth A. Robinson
6 www.rtsoft.com
7
8 NOTES: Requires PHP5
9
10 The class should only be used by other PHP files, it should never be called by the user directly. (nothing would happen)
11
12*/
13
14include_once('MiscUtils.php');
15
16class GrowtopiaNotifications
17{
18 private $dbTableName;
19 private $dbcnx;
20 private $dbHost;
21 private $dbName;
22 private $dbUserName;
23 private $dbPassword;
24
25 const C_MAX_KEY_LENGTH = 64;
26 const C_MAX_TEXT_LENGTH = 16;
27
28 public function __construct()
29 {
30 $this->dbTableName = 'cosmo_notifications';
31
32 //to test locally
33 $this->dbHost = $GLOBALS['C_RT_DB_HOST'];
34 $this->dbUserName = $GLOBALS['C_RT_DB_USERNAME'];
35 $this->dbPassword = $GLOBALS['C_RT_DB_PASSWORD'];
36 $this->dbName = $GLOBALS['C_RT_DB_NAME'];
37
38 $this->dbcnx = mysql_connect($this->dbHost, $this->dbUserName, $this->dbPassword);
39 if (!$this->dbcnx)
40 {
41 die( '<p>Unable to connect to the database server at this time.</p>' );
42 }
43
44 if (!mysql_select_db($this->dbName) )
45 {
46 die('Unable to connect to select database:'.mysql_error());
47 }
48 $this->CreateTableIfNeeded();
49 }
50
51 public function __destruct()
52 {
53 }
54
55 //uuid snippet by mimec - http://fr.php.net/manual/en/function.uniqid.php
56
57 private function GetUUID()
58 {
59 return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
60 mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
61 mt_rand( 0, 0x0fff ) | 0x4000,
62 mt_rand( 0, 0x3fff ) | 0x8000,
63 mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
64 }
65
66
67 public function LogMsg($msg)
68 {
69 echo ('<p>' . $msg . '</p>');
70 }
71
72 private function CreateTableIfNeeded()
73 {
74 //first see if the table exists.. there's probably a better way then this...
75 $sql = "SHOW TABLES LIKE '$this->dbTableName'";
76 $response = mysql_query($sql);
77 if ($response && mysql_fetch_array($response) > 0)
78 {
79 //the table exists, we don't need to create it
80 return;
81 }
82
83
84 //let's create the database we need
85 $sql = "CREATE TABLE $this->dbTableName (
86ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
87GrowtopiaID INT NOT NULL,
88KeyName CHAR(".self::C_MAX_KEY_LENGTH."),
89Password CHAR(".self::C_MAX_TEXT_LENGTH."),
90Date DATE NOT NULL,
91Mode INT DEFAULT 0
92)";
93
94 if ( @mysql_query($sql) )
95 {
96 // echo('<p>FIRST TIME RUN: link table successfully created!</p>');
97 } else
98 {
99 //echo('<p>Error creating table: '. mysql_error(). '</p>');
100 }
101 }
102
103public function DeleteAll() //completely destroys the database, causing it to be reformed at next use
104 {
105 $sql = "drop table $this->dbTableName";
106
107 if (@mysql_query($sql))
108 {
109 echo ("Database wiped. I hope you meant to do that.");
110 } else
111 {
112 echo('<p>Can\'t destroy table $this->dbTableName : '. mysql_error(). '</p>');
113 }
114
115 //create it again
116 $this->CreateTableIfNeeded();
117 }
118
119 public function AddNameChangeRequest($GrowtopiaAccountNum, $newpass, &$downloadKeyOut)
120 {
121
122 $downloadKeyOut = $this->GetUUID();
123 $downloadKeySafe = addslashes($downloadKeyOut);
124 //Add to database
125$sql = "INSERT INTO $this->dbTableName SET
126GrowtopiaID = $GrowtopiaAccountNum,
127KeyName = '$downloadKeySafe',
128Password = '$newpass',
129Date = SYSDATE()
130";
131 if (@mysql_query($sql))
132 {
133 } else
134 {
135 echo('<p>Error with NameKey insert: ' . mysql_error() . '</p>');
136 return false;
137 }
138
139 $idOut = mysql_insert_id();
140 return true;
141 }
142
143 public function PrintDataBaseStatistics()
144 {
145 $totalRecs = 0;
146
147 $sql = "SELECT COUNT(*) FROM $this->dbTableName";
148
149 $response = mysql_query($sql);
150 if (!$response)
151 {
152 echo('<p>Error PrintDataBaseStatistics '. mysql_error(). '</p>');
153 return false;
154 }
155
156 $row = mysql_fetch_array($response);
157 $count = $row[0];
158 echo("There are <b>$count</b> GrowtopiaNotification records .<BR>");
159 }
160
161 public function SentEmailToThisGuyRecently($UserID)
162 {
163
164 $sql = "SELECT COUNT(*) FROM $this->dbTableName where GrowtopiaID = $UserID AND Date > DATE_SUB(SYSDATE(), INTERVAL 1 DAY)";
165 $response = mysql_query($sql);
166
167 if (!$response)
168 {
169 echo('<p>Error SentEmailToThisGuyRecently( '. mysql_error(). '</p>');
170 return false;
171 }
172
173 $row = mysql_fetch_array($response);
174 $count = $row[0];
175 return ($count > 0);
176 }
177
178
179 public function DeleteOldNotifications($daysOldToKeep)
180 {
181 $sql = "DELETE FROM $this->dbTableName WHERE Date < DATE_SUB(NOW(), INTERVAL ".$daysOldToKeep." DAY)";
182 $response = mysql_query($sql);
183 if (!$response)
184 {
185 echo('<p>Error DeleteAllNoficationsToThisGrowtopiaID( '. mysql_error(). '</p>');
186 }
187
188 return;
189 }
190
191 public function DeleteAllNoficationsToThisGrowtopiaID($GrowtopiaID)
192 {
193 $sql = "DELETE FROM $this->dbTableName WHERE GrowtopiaID = $GrowtopiaID";
194 $response = mysql_query($sql);
195 if (!$response)
196 {
197 echo('<p>Error DeleteAllNoficationsToThisGrowtopiaID( '. mysql_error(). '</p>');
198 }
199
200 return;
201 }
202
203 public function GetNotificationFromKeyAndGrowtopiaID($key, $GrowtopiaID, &$keyInfoOut)
204 {
205
206 $sql = "SELECT * FROM $this->dbTableName WHERE GrowtopiaID = $GrowtopiaID AND KeyName = '$key'";
207 $response = mysql_query($sql);
208 if (!$response)
209 {
210 //echo('<p>Error GetNotificationFromKeyAndGrowtopiaID( '. mysql_error(). '</p>');
211 return 0;
212 }
213 $rows = mysql_numrows($response);
214 if ($rows < 1)
215 {
216 return false;
217 }
218
219 $keyInfoOut = mysql_fetch_array($response);
220 return true;
221 }
222
223}
224?>