Guest User

Untitled

a guest
Jan 22nd, 2019
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @author bailz777
  5. *
  6. *
  7. */
  8. class db_interface {
  9.  
  10. public $ServerName = 'localhost:3306'; //hostname:port
  11. public $UserName = '******'; //mysql user
  12. public $Password = '******'; //mysql password
  13. public $DataBase = '******'; //database name
  14. public $Domain = 'test.com'; //Full domain name (no host)
  15. public $con = '';
  16.  
  17. function __construct() {
  18.  
  19. //on construction, we must ensure that the domain is registered in our system
  20. //first check if it was defined locally to avoid extra DataBase Work
  21. var_dump(defined('DOMAIN_REGISTERED'));
  22. if(!defined('DOMAIN_REGISTERED')) {
  23. $this->db_connect();
  24. $result = $this->validate_domain();
  25. if($result) {
  26. echo "<p>Domain Validated!!</p>";
  27. }
  28. $this->db_disconnect();
  29. }
  30. else {
  31. echo "<p>Domain Validated!!</p>";
  32. }
  33. }
  34.  
  35. /**
  36. *
  37. */
  38. function __destruct() {
  39.  
  40. }
  41.  
  42. /**
  43. *
  44. * @param unknown_type $domain
  45. * @return boolean
  46. */
  47. private function validate_domain() {
  48. $constants = get_defined_constants();
  49. // return $this->con;
  50. // print_r($constants);
  51. var_dump(defined('DOMAIN_REGISTERED'));
  52. if(defined('DOMAIN_REGISTERED')) {//Check DOMAIN_REGISTERED to avoid unnecessary db work
  53. return TRUE;
  54. }
  55. elseif (!defined('DOMAIN_REGISTERED')) {//Check the domain is in the db
  56. echo '<p>Domain was not locally registered, checking DataBase</p>';
  57. $query = "SELECT `name` FROM `$this->DataBase`.`registered_domains` WHERE `name` = '$this->Domain'";
  58. $result = mysql_query($query,$this->con);
  59. //var_dump($result);
  60. if(!$result) {
  61. die('No result found : ' . mysql_error());
  62. }
  63. elseif (mysql_num_rows($result)==0) { //if no rows returned, then domain is not in DataBase
  64. $domain_exists = FALSE;
  65. }
  66. elseif (mysql_num_rows($result)>0) { //if rows returned, then domain is in DataBase
  67. $domain_exists = TRUE;
  68. //If a domain does not exist, a mysql will be passed, use @ to suppress the error
  69. //The domain will be written to the db and on the next run of this function, the
  70. //constant will be defined
  71. }
  72. if($domain_exists) {//If it exists Then assign CONSTANT DOMAIN_REGISTERED to TRUE
  73. echo '<p>Domain Found in DataBase</p>';
  74. echo '<p>Registering domain locally</p>';
  75. define("DOMAIN_REGISTERED", TRUE);
  76. if(DOMAIN_REGISTERED) {
  77. echo '<p>Successfully registered domain locally</p>';
  78. }
  79. //var_dump(defined('DOMAIN_REGISTERED'));
  80. //echo DOMAIN_REGISTERED;
  81. return TRUE;
  82. }
  83. elseif(!$domain_exists) {//If it does not exist then add it to the registered_domains table, and assign CONSTANT __DOMAIN_TRUE__ to TRUE
  84. echo '<p>Domain not found in DataBase</p>';
  85. echo '<p>Now Registering Domain</p>';
  86. $query = "INSERT INTO `$this->DataBase`.`registered_domains` (`name`) VALUES ('$this->Domain')";
  87. $result = mysql_query($query);
  88. if(!$result) {
  89. die('Domain not added : ' . mysql_error());
  90. }
  91. else{
  92. define("DOMAIN_REGISTERED", TRUE);
  93. return TRUE;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. //Connect to mysql and define the active database
  100. private function db_connect() {
  101. $this->con = $con = mysql_connect($this->ServerName,$this->UserName,$this->Password);
  102. if (!$con) {
  103. die('Could not connect: ' . mysql_error());
  104. }
  105. else {
  106. echo 'Successfully connected to MySQL<br />';
  107. //define active database
  108. $this->db = mysql_select_db($this->DataBase);
  109. if(!$this->db) {
  110. die('Could not connect to Database : ' . mysql_error());
  111. }
  112. else {
  113. echo 'Successfully connected to DataBase<br />';
  114. }
  115. }
  116. }
  117.  
  118. //disconnect from mysql
  119. private function db_disconnect() {
  120. $close = mysql_close($this->con);
  121. if($close) {
  122. echo 'Successfully disconnected from MySQL<br />';
  123. }
  124. }
  125.  
  126. public function add_record($fname,$lname,$email) {
  127. $ip = $_SERVER['REMOTE_ADDR'];
  128. $authorized_date = time();
  129.  
  130. }
  131. }
  132.  
  133. ?>
  134.  
  135. <?php
  136.  
  137. class pizza
  138. {
  139. const SIZE_SMALL = 1;
  140. const SIZE_MEDIUM = 2;
  141. const SIZE_LARGE = 3;
  142.  
  143. public function getCookingTime($size)
  144. {
  145. if ($size == self::SIZE_SMALL) {
  146. $time = 10;
  147. } else if ($size == self::SIZE_MEDIUM || $size == self::SIZE_LARGE) {
  148. $time = 15;
  149. }
  150. return $size;
  151. }
  152. }
  153.  
  154. $pizza = new pizza();
  155. $time = $pizza->getCookingTime(3);
  156.  
  157. // or more usefull:
  158.  
  159. $time = $pizza->getCookingTime(pizza::SIZE_SMALL);
  160.  
  161. ?>
  162.  
  163. class mydb
  164. {
  165. private $host;
  166. private $database;
  167. ..
  168.  
  169. public function __construct($host, $database ..)
  170. {
  171. $this->host = $host;
  172. $this->database = $database;
  173. }
  174. }
  175.  
  176. $mydb = new mydb('localhost', 'mydatabase', ..);
  177.  
  178. class mydb
  179. {
  180. private $host;
  181. private $database;
  182. ..
  183.  
  184. public function __construct($host, $database ..)
  185. {
  186. $this->host = $host;
  187. $this->database = $database;
  188. }
  189.  
  190. public function Register( $domainname )
  191. {
  192. //connect to the database and do register stuff
  193. }
  194. }
  195.  
  196. $mydb = new mydb('localhost', 'mydatabase', ..);
  197. $mydb->Register('MyDomain');
Add Comment
Please, Sign In to add comment