Sql.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\FileReader\Reader;
  3. use ModulesGarden\ProxmoxAddon\Core\ServiceLocator;
  4. use ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  5. use Illuminate\Database\Capsule\Manager as DB;
  6. /**
  7. * Description of Sql
  8. *
  9. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  10. */
  11. class Sql extends AbstractType
  12. {
  13. protected function loadFile()
  14. {
  15. $return = '';
  16. try
  17. {
  18. if (file_exists($this->path . DS . $this->file))
  19. {
  20. $collation = $this->getWHMCSTablesCollation();
  21. $charset = $this->getWHMCSTablesCharset();
  22. $return = file_get_contents($this->path . DS . $this->file);
  23. $return = str_replace("#collation#", $collation, $return);
  24. $return = str_replace("#charset#", $charset, $return);
  25. $return = str_replace("#prefix#", ModuleConstants::getPrefixDataBase(), $return);
  26. foreach ($this->renderData as $key => $value)
  27. {
  28. $return = str_replace("#$key#", $value, $return);
  29. }
  30. }
  31. }
  32. catch (\Exception $e)
  33. {
  34. ServiceLocator::call('errorManager')->addError(self::class, $e->getMessage(), $e->getTrace());
  35. }
  36. $this->data = $return;
  37. }
  38. protected function getWHMCSTablesCollation()
  39. {
  40. $pdo = \Illuminate\Database\Capsule\Manager::connection()->getPdo();
  41. $query = $pdo->prepare("SHOW TABLE STATUS WHERE name = 'tblclients'");
  42. $query->execute();
  43. $result = $query->fetchObject();
  44. return $result->Collation;
  45. }
  46. protected function getWHMCSTablesCharset()
  47. {
  48. $pdo = \Illuminate\Database\Capsule\Manager::connection()->getPdo();
  49. $query = $pdo->prepare("SELECT CCSA.character_set_name as Charset FROM information_schema.`TABLES` T,
  50. information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
  51. WHERE CCSA.collation_name = T.table_collation
  52. AND T.table_schema = '{$GLOBALS['db_name']}'
  53. AND T.table_name = 'tblclients';");
  54. $query->execute();
  55. $result = $query->fetchObject();
  56. if($result->Charset){
  57. return $result->Charset;
  58. }
  59. return 'utf8';
  60. }
  61. }