Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- 2012 - Seth A. Robinson
- www.rtsoft.com
- NOTES: Requires PHP5
- The class should only be used by other PHP files, it should never be called by the user directly. (nothing would happen)
- */
- include_once('MiscUtils.php');
- class GrowtopiaNotifications
- {
- private $dbTableName;
- private $dbcnx;
- private $dbHost;
- private $dbName;
- private $dbUserName;
- private $dbPassword;
- const C_MAX_KEY_LENGTH = 64;
- const C_MAX_TEXT_LENGTH = 16;
- public function __construct()
- {
- $this->dbTableName = 'cosmo_notifications';
- //to test locally
- $this->dbHost = $GLOBALS['C_RT_DB_HOST'];
- $this->dbUserName = $GLOBALS['C_RT_DB_USERNAME'];
- $this->dbPassword = $GLOBALS['C_RT_DB_PASSWORD'];
- $this->dbName = $GLOBALS['C_RT_DB_NAME'];
- $this->dbcnx = mysql_connect($this->dbHost, $this->dbUserName, $this->dbPassword);
- if (!$this->dbcnx)
- {
- die( '<p>Unable to connect to the database server at this time.</p>' );
- }
- if (!mysql_select_db($this->dbName) )
- {
- die('Unable to connect to select database:'.mysql_error());
- }
- $this->CreateTableIfNeeded();
- }
- public function __destruct()
- {
- }
- //uuid snippet by mimec - http://fr.php.net/manual/en/function.uniqid.php
- private function GetUUID()
- {
- return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
- mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
- mt_rand( 0, 0x0fff ) | 0x4000,
- mt_rand( 0, 0x3fff ) | 0x8000,
- mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
- }
- public function LogMsg($msg)
- {
- echo ('<p>' . $msg . '</p>');
- }
- private function CreateTableIfNeeded()
- {
- //first see if the table exists.. there's probably a better way then this...
- $sql = "SHOW TABLES LIKE '$this->dbTableName'";
- $response = mysql_query($sql);
- if ($response && mysql_fetch_array($response) > 0)
- {
- //the table exists, we don't need to create it
- return;
- }
- //let's create the database we need
- $sql = "CREATE TABLE $this->dbTableName (
- ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
- GrowtopiaID INT NOT NULL,
- KeyName CHAR(".self::C_MAX_KEY_LENGTH."),
- Password CHAR(".self::C_MAX_TEXT_LENGTH."),
- Date DATE NOT NULL,
- Mode INT DEFAULT 0
- )";
- if ( @mysql_query($sql) )
- {
- // echo('<p>FIRST TIME RUN: link table successfully created!</p>');
- } else
- {
- //echo('<p>Error creating table: '. mysql_error(). '</p>');
- }
- }
- public function DeleteAll() //completely destroys the database, causing it to be reformed at next use
- {
- $sql = "drop table $this->dbTableName";
- if (@mysql_query($sql))
- {
- echo ("Database wiped. I hope you meant to do that.");
- } else
- {
- echo('<p>Can\'t destroy table $this->dbTableName : '. mysql_error(). '</p>');
- }
- //create it again
- $this->CreateTableIfNeeded();
- }
- public function AddNameChangeRequest($GrowtopiaAccountNum, $newpass, &$downloadKeyOut)
- {
- $downloadKeyOut = $this->GetUUID();
- $downloadKeySafe = addslashes($downloadKeyOut);
- //Add to database
- $sql = "INSERT INTO $this->dbTableName SET
- GrowtopiaID = $GrowtopiaAccountNum,
- KeyName = '$downloadKeySafe',
- Password = '$newpass',
- Date = SYSDATE()
- ";
- if (@mysql_query($sql))
- {
- } else
- {
- echo('<p>Error with NameKey insert: ' . mysql_error() . '</p>');
- return false;
- }
- $idOut = mysql_insert_id();
- return true;
- }
- public function PrintDataBaseStatistics()
- {
- $totalRecs = 0;
- $sql = "SELECT COUNT(*) FROM $this->dbTableName";
- $response = mysql_query($sql);
- if (!$response)
- {
- echo('<p>Error PrintDataBaseStatistics '. mysql_error(). '</p>');
- return false;
- }
- $row = mysql_fetch_array($response);
- $count = $row[0];
- echo("There are <b>$count</b> GrowtopiaNotification records .<BR>");
- }
- public function SentEmailToThisGuyRecently($UserID)
- {
- $sql = "SELECT COUNT(*) FROM $this->dbTableName where GrowtopiaID = $UserID AND Date > DATE_SUB(SYSDATE(), INTERVAL 1 DAY)";
- $response = mysql_query($sql);
- if (!$response)
- {
- echo('<p>Error SentEmailToThisGuyRecently( '. mysql_error(). '</p>');
- return false;
- }
- $row = mysql_fetch_array($response);
- $count = $row[0];
- return ($count > 0);
- }
- public function DeleteOldNotifications($daysOldToKeep)
- {
- $sql = "DELETE FROM $this->dbTableName WHERE Date < DATE_SUB(NOW(), INTERVAL ".$daysOldToKeep." DAY)";
- $response = mysql_query($sql);
- if (!$response)
- {
- echo('<p>Error DeleteAllNoficationsToThisGrowtopiaID( '. mysql_error(). '</p>');
- }
- return;
- }
- public function DeleteAllNoficationsToThisGrowtopiaID($GrowtopiaID)
- {
- $sql = "DELETE FROM $this->dbTableName WHERE GrowtopiaID = $GrowtopiaID";
- $response = mysql_query($sql);
- if (!$response)
- {
- echo('<p>Error DeleteAllNoficationsToThisGrowtopiaID( '. mysql_error(). '</p>');
- }
- return;
- }
- public function GetNotificationFromKeyAndGrowtopiaID($key, $GrowtopiaID, &$keyInfoOut)
- {
- $sql = "SELECT * FROM $this->dbTableName WHERE GrowtopiaID = $GrowtopiaID AND KeyName = '$key'";
- $response = mysql_query($sql);
- if (!$response)
- {
- //echo('<p>Error GetNotificationFromKeyAndGrowtopiaID( '. mysql_error(). '</p>');
- return 0;
- }
- $rows = mysql_numrows($response);
- if ($rows < 1)
- {
- return false;
- }
- $keyInfoOut = mysql_fetch_array($response);
- return true;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement