Advertisement
Guest User

Website.php

a guest
Sep 17th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. <?php
  2. if(!defined('INITIALIZED'))
  3. exit;
  4.  
  5. class Website extends WebsiteErrors
  6. {
  7. public static $serverConfig;
  8. public static $websiteConfig;
  9. public static $vocations;
  10. public static $groups;
  11. public static $SQL;
  12. public static $passwordsEncryptions = array(
  13. 'plain' => 'plain',
  14. 'md5' => 'md5',
  15. 'sha1' => 'sha1',
  16. 'sha256' => 'sha256',
  17. 'sha512' => 'sha512',
  18. 'vahash' => 'vahash'
  19. );
  20. private static $passwordsEncryption;
  21.  
  22. public static function setDatabaseDriver($value)
  23. {
  24. self::$SQL = null;
  25.  
  26. switch($value)
  27. {
  28. case Database::DB_MYSQL:
  29. self::$SQL = new Database_MySQL();
  30. break;
  31. case Database::DB_SQLITE:
  32. self::$SQL = new Database_SQLite();
  33. break;
  34. }
  35. }
  36.  
  37. public static function getDBHandle()
  38. {
  39. if(isset(self::$SQL))
  40. return self::$SQL;
  41. else
  42. new Error_Critic('#C-9', 'ERROR: <b>#C-9</b> : Class::Website - getDBHandle(), database driver not set.');
  43. }
  44.  
  45. public static function loadWebsiteConfig()
  46. {
  47. self::$websiteConfig = new ConfigPHP();
  48. global $config;
  49. self::$websiteConfig->setConfig($config['site']);
  50. }
  51.  
  52. public static function getWebsiteConfig()
  53. {
  54. if(!isset(self::$websiteConfig))
  55. self::loadWebsiteConfig();
  56.  
  57. return self::$websiteConfig;
  58. }
  59.  
  60. public static function loadServerConfig()
  61. {
  62. self::$serverConfig = new ConfigPHP();
  63. global $config;
  64. self::$serverConfig->setConfig($config['server']);
  65. }
  66.  
  67. public static function getServerConfig()
  68. {
  69. if(!isset(self::$serverConfig))
  70. self::loadServerConfig();
  71.  
  72. return self::$serverConfig;
  73. }
  74.  
  75. public static function getConfig($fileNameArray)
  76. {
  77. $fileName = implode('_', $fileNameArray);
  78.  
  79. if(Functions::isValidFolderName($fileName))
  80. {
  81. $_config = new ConfigPHP('./config/' . $fileName . '.php');
  82. return $_config;
  83. }
  84. else
  85. new Error_Critic('', __METHOD__ . ' - invalid folder/file name <b>' . htmlspecialchars('./config/' . $fileName . '.php') . '</b>');
  86. }
  87.  
  88. public static function getFileContents($path)
  89. {
  90. $file = file_get_contents($path);
  91.  
  92. if($file === false)
  93. new Error_Critic('', __METHOD__ . ' - Cannot read from file: <b>' . htmlspecialchars($path) . '</b>');
  94.  
  95. return $file;
  96. }
  97.  
  98. public static function putFileContents($path, $data, $append = false)
  99. {
  100. if($append)
  101. $status = file_put_contents($path, $data, FILE_APPEND);
  102. else
  103. $status = file_put_contents($path, $data);
  104.  
  105. if($status === false)
  106. new Error_Critic('', __METHOD__ . ' - Cannot write to: <b>' . htmlspecialchars($path) . '</b>');
  107.  
  108. return $status;
  109. }
  110.  
  111. public static function deleteFile($path)
  112. {
  113. unlink($path);
  114. }
  115.  
  116. public static function fileExists($path)
  117. {
  118. return file_exists($path);
  119. }
  120.  
  121. public static function setPasswordsEncryption($encryption)
  122. {
  123. if(isset(self::$passwordsEncryptions[strtolower($encryption)]))
  124. self::$passwordsEncryption = strtolower($encryption);
  125. else
  126. new Error_Critic('#C-12', 'Invalid passwords encryption ( ' . htmlspecialchars($encryption) . '). Must be one of these: ' . implode(', ', self::$passwordsEncryptions));
  127. }
  128.  
  129. public static function getPasswordsEncryption()
  130. {
  131. return self::$passwordsEncryption;
  132. }
  133.  
  134. public static function validatePasswordsEncryption($encryption)
  135. {
  136. if(isset(self::$passwordsEncryptions[strtolower($encryption)]))
  137. return true;
  138. else
  139. return false;
  140. }
  141.  
  142. public static function encryptPassword($password, $account = null)
  143. {
  144. // add SALT for 0.4
  145. if(isset(self::$passwordsEncryption))
  146. if(self::$passwordsEncryption == 'plain')
  147. return $password;
  148. else
  149. return hash(self::$passwordsEncryption, $password);
  150. else
  151. new Error_Critic('#C-13', 'You cannot use Website::encryptPassword(\$password) when password encryption is not set.');
  152. }
  153.  
  154. public static function loadVocations()
  155. {
  156. $path = self::getWebsiteConfig()->getValue('serverPath');
  157. self::$vocations = new Vocations($path . 'data/XML/vocations.xml');
  158. }
  159.  
  160. public static function getVocations()
  161. {
  162. if(!isset(self::$vocations))
  163. self::loadVocations();
  164.  
  165. return self::$vocations;
  166. }
  167.  
  168. public static function getVocationName($id, $promotion)
  169. {
  170. if(!isset(self::$vocations))
  171. self::loadVocations();
  172.  
  173. return self::$vocations->getVocationName($id, $promotion);
  174. }
  175.  
  176. public static function loadGroups()
  177. {
  178. $path = self::getWebsiteConfig()->getValue('serverPath');
  179. self::$groups = new Groups($path . 'data/XML/groups.xml');
  180. }
  181.  
  182. public static function getGroups()
  183. {
  184. if(!isset(self::$groups))
  185. self::loadGroups();
  186.  
  187. return self::$groups;
  188. }
  189.  
  190. public static function getGroupName($id)
  191. {
  192. if(!isset(self::$groups))
  193. self::loadGroups();
  194.  
  195. return self::$groups->getGroupName($id);
  196. }
  197.  
  198. public static function getCountryCode($IP)
  199. {
  200. $a = explode(".",$IP);
  201. if($a[0] == 10) // IPs 10.0.0.0 - 10.255.255.255 = private network, so can't geolocate
  202. return 'unknown';
  203. if($a[0] == 127) // IPs 127.0.0.0 - 127.255.255.255 = local network, so can't geolocate
  204. return 'unknown';
  205. if($a[0] == 172 && ($a[1] >= 16 && $a[1] <= 31)) // IPs 172.16.0.0 - 172.31.255.255 = private network, so can't geolocate
  206. return 'unknown';
  207. if($a[0] == 192 && $a[1] == 168) // IPs 192.168.0.0 - 192.168.255.255 = private network, so can't geolocate
  208. return 'unknown';
  209. if($a[0] >= 224) // IPs over 224.0.0.0 are not assigned, so can't geolocate
  210. return 'unknown';
  211. $longIP = $a[0] * 256 * 256 * 256 + $a[1] * 256 * 256 + $a[2] * 256 + $a[3]; // we need unsigned value
  212. if(!file_exists('cache/flags/flag' . $a[0]))
  213. {
  214. $flagData = @file_get_contents('http://country-flags.ots.me/flag' . $a[0]);
  215. if($flagData === false)
  216. return 'unknown';
  217. if(@file_put_contents('cache/flags/flag' . $a[0], $flagData) === false)
  218. return 'unknown';
  219. }
  220. $countries = unserialize(file_get_contents('cache/flags/flag' . $a[0])); // load file
  221. $lastCountryCode = 'unknown';
  222. foreach($countries as $fromLong => $countryCode)
  223. {
  224. if($fromLong > $longIP)
  225. break;
  226. $lastCountryCode = $countryCode;
  227. }
  228. return $lastCountryCode;
  229. }
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement