PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Chive.class.php

https://github.com/advocaite/Stargate-Wars
PHP | 201 lines | 147 code | 8 blank | 46 comment | 2 complexity | 3879add0f8e12295c9f4b38693df9600 MD5 | raw file
  1. <?php
  2. // Base::Chive.class.php
  3. class Chive
  4. {
  5. // General Info
  6. /**
  7. * Name of the class
  8. *
  9. * @var String $name
  10. */
  11. var $name = null;
  12. /**
  13. * Database table prefix
  14. *
  15. * @var String $db_prefix
  16. */
  17. var $db_prefix = null;
  18. /**
  19. * Database server location
  20. *
  21. * @var String $db_server
  22. */
  23. var $db_server = null;
  24. /**
  25. * Database name
  26. *
  27. * @var String $db_name
  28. */
  29. var $db_name = null;
  30. /**
  31. * Database username
  32. *
  33. * @var String $db_username
  34. */
  35. var $db_username = null;
  36. /**
  37. * Database password
  38. *
  39. * @var String $db_password
  40. */
  41. var $db_password = null;
  42. /**
  43. * MySQL Resource link for database connections
  44. *
  45. * @var MySQL_Resource $db_link
  46. */
  47. var $db_link = null;
  48. var $queryCount = 0;
  49. /**
  50. * Contructor for Chive
  51. * @param String $name Name of the class
  52. *
  53. */
  54. function Chive($name = "")
  55. {
  56. global $conf;
  57. $this->name = $name;
  58. $this->db_server = $conf['db_server'];
  59. $this->db_name = $conf['db_name'];
  60. $this->db_username = $conf['db_username'];
  61. $this->db_password = $conf['db_password'];
  62. $this->db_prefix = $conf['db_prefix'];
  63. Debug::printMsg(__CLASS__, __FUNCTION__, "Class created with <b>\$name</b> ".$this->name);
  64. }
  65. /**
  66. * Creates a MySQL Resource link to $db_link
  67. *
  68. */
  69. function connectToDB()
  70. {
  71. Debug::printMsg(__CLASS__, __FUNCTION__, "Connecting to DB...");
  72. $this->db_link = mysql_connect($this->db_server, $this->db_username, $this->db_password);
  73. if($this->db_link)
  74. {
  75. Debug::printMsg(__CLASS__, __FUNCTION__, "Connected to database");
  76. } else {
  77. Debug::printMsg(__CLASS__, __FUNCTION__, "Couldn't connect to DB ".mysql_error());
  78. }
  79. if(mysql_select_db($this->db_name, $this->db_link))
  80. {
  81. Debug::printMsg(__CLASS__, __FUNCTION__, "Selected database ".$this->db_name);
  82. } else {
  83. Debug::printMsg(__CLASS__, __FUNCTION__, "Database not selected - <b>ERROR:</b> '".mysql_error()."'");
  84. }
  85. }
  86. /**
  87. * Checks if a connection to the database server has been made
  88. *
  89. * @return bool
  90. */
  91. function connected()
  92. {
  93. if($this->db_link)
  94. {
  95. return true;
  96. }
  97. return false;
  98. }
  99. /**
  100. * Cleans $string for a MySQL statement
  101. *
  102. * @param String $string
  103. * @return String
  104. */
  105. function clean_sql($string, $quotes = 1)
  106. {
  107. if(!$this->connected()) $this->connectToDB();
  108. // Stripslashes
  109. if (get_magic_quotes_gpc())
  110. {
  111. $string = stripslashes($string);
  112. }
  113. // Quote if not integer
  114. if (!is_numeric($string) && $quotes)
  115. {
  116. $string = "'".mysql_real_escape_string($string)."'";
  117. }
  118. return $string;
  119. }
  120. /**
  121. * Queries Database.
  122. * Returns true or false depending on success
  123. *
  124. * @param String $query
  125. * @return resource
  126. */
  127. function query($query)
  128. {
  129. if(!$this->connected()) $this->connectToDB();
  130. $r = mysql_query($query);
  131. if($r)
  132. {
  133. Debug::printMsg(__CLASS__, __FUNCTION__, "Query successful: ".$query."\r\n");
  134. $this->queryCount++;
  135. return $r;
  136. }
  137. Debug::printMsg(__CLASS__, __FUNCTION__, "Query unsuccessful - <b>ERROR:</b> ".mysql_error()." FROM QUERY - \"".$query."\"\n");
  138. $this->queryCount++;
  139. return false;
  140. }
  141. }
  142. class page_gen {
  143. //
  144. // PRIVATE CLASS VARIABLES
  145. //
  146. var $_start_time;
  147. var $_stop_time;
  148. var $_gen_time;
  149. //
  150. // USER DEFINED VARIABLES
  151. //
  152. var $round_to;
  153. //
  154. // CLASS CONSTRUCTOR
  155. //
  156. function page_gen() {
  157. if (!isset($this->round_to)) {
  158. $this->round_to = 4;
  159. }
  160. }
  161. //
  162. // FIGURE OUT THE TIME AT THE BEGINNING OF THE PAGE
  163. //
  164. function start() {
  165. $microstart = explode(' ',microtime());
  166. $this->_start_time = $microstart[0] + $microstart[1];
  167. }
  168. //
  169. // FIGURE OUT THE TIME AT THE END OF THE PAGE
  170. //
  171. function stop() {
  172. $microstop = explode(' ',microtime());
  173. $this->_stop_time = $microstop[0] + $microstop[1];
  174. }
  175. //
  176. // CALCULATE THE DIFFERENCE BETWEEN THE BEGINNNG AND THE END AND RETURN THE VALUE
  177. //
  178. function gen() {
  179. $this->_gen_time = round($this->_stop_time - $this->_start_time,$this->round_to);
  180. return $this->_gen_time;
  181. }
  182. }
  183. ?>