/package/phpcms/modules/dbsource/dbsource_admin.php

https://github.com/zstulc/myproject · PHP · 127 lines · 115 code · 11 blank · 1 comment · 48 complexity · 7792837b8839c3c18ca52769b38ec8b6 MD5 · raw file

  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. pc_base::load_app_class('admin', 'admin', 0);
  4. class dbsource_admin extends admin {
  5. private $db;
  6. public function __construct() {
  7. $this->db = pc_base::load_model('dbsource_model');
  8. parent::__construct();
  9. pc_base::load_app_func('global');
  10. }
  11. public function init() {
  12. $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
  13. $list = $this->db->listinfo(array('siteid'=>$this->get_siteid()), '', $page, 20);
  14. $pages = $this->db->pages;
  15. $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=dbsource&c=dbsource_admin&a=add\', title:\''.L('added_external_data_source').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('added_external_data_source'));
  16. include $this->admin_tpl('dbsource_list');
  17. }
  18. public function add() {
  19. if (isset($_POST['dosubmit'])) {
  20. $name = isset($_POST['name']) && trim($_POST['name']) ? trim($_POST['name']) : showmessage(L('dbsource_name').L('empty'));
  21. $host = isset($_POST['host']) && trim($_POST['host']) ? trim($_POST['host']) : showmessage(L('server_address').L('empty'));
  22. $port = isset($_POST['port']) && intval($_POST['port']) ? intval($_POST['port']) : showmessage(L('server_port').L('empty'));
  23. $username = isset($_POST['username']) && trim($_POST['username']) ? trim($_POST['username']) : showmessage(L('username').L('empty'));
  24. $password = isset($_POST['password']) && trim($_POST['password']) ? trim($_POST['password']) : showmessage(L('password').L('empty'));
  25. $dbname = isset($_POST['dbname']) && trim($_POST['dbname']) ? trim($_POST['dbname']) : showmessage(L('database').L('empty'));
  26. $dbtablepre = isset($_POST['dbtablepre']) && trim($_POST['dbtablepre']) ? trim($_POST['dbtablepre']) : '';
  27. $charset = isset($_POST['charset']) && in_array(trim($_POST['charset']), array('gbk','utf8', 'gb2312', 'latin1')) ? trim($_POST['charset']) : showmessage(L('charset').L('illegal_parameters'));
  28. $siteid = $this->get_siteid();
  29. if (!preg_match('/^\\w+$/i', $name)) {
  30. showmessage(L('data_source_of_the_letters_and_figures'));
  31. }
  32. //检察数据源名是否已经存在
  33. if ($this->db->get_one(array('siteid'=>$siteid, 'name'=>$name), 'id')) {
  34. showmessage(L('dbsource_name').L('exists'));
  35. }
  36. if ($this->db->insert(array('siteid'=>$siteid, 'name'=>$name,'host'=>$host,'port'=>$port,'username'=>$username,'password'=>$password,'dbname'=>$dbname,'dbtablepre'=>$dbtablepre,'charset'=>$charset))) {
  37. dbsource_cache();
  38. showmessage('', '', '', 'add');
  39. } else {
  40. showmessage(L('operation_failure'));
  41. }
  42. } else {
  43. pc_base::load_sys_class('form', '', 0);
  44. $show_header = $show_validator = true;
  45. include $this->admin_tpl('dbsource_add');
  46. }
  47. }
  48. public function edit() {
  49. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : showmessage('ID'.L('empty'));
  50. $data = $this->db->get_one(array('id'=>$id));
  51. if (!$data) {
  52. showmessage(L('notfound'));
  53. }
  54. if (isset($_POST['dosubmit'])) {
  55. $host = isset($_POST['host']) && trim($_POST['host']) ? trim($_POST['host']) : showmessage(L('server_address').L('empty'));
  56. $port = isset($_POST['port']) && intval($_POST['port']) ? intval($_POST['port']) : showmessage(L('server_port').L('empty'));
  57. $username = isset($_POST['username']) && trim($_POST['username']) ? trim($_POST['username']) : showmessage(L('username').L('empty'));
  58. $password = isset($_POST['password']) && trim($_POST['password']) ? trim($_POST['password']) : showmessage(L('password').L('empty'));
  59. $dbname = isset($_POST['dbname']) && trim($_POST['dbname']) ? trim($_POST['dbname']) : showmessage(L('database').L('empty'));
  60. $dbtablepre = isset($_POST['dbtablepre']) && trim($_POST['dbtablepre']) ? trim($_POST['dbtablepre']) : '';
  61. $charset = isset($_POST['charset']) && in_array(trim($_POST['charset']), array('gbk','utf8', 'gb2312', 'latin1')) ? trim($_POST['charset']) : showmessage(L('charset').L('illegal_parameters'));
  62. $siteid = $this->get_siteid();
  63. $sql = array('siteid'=>$siteid, 'host'=>$host,'port'=>$port,'username'=>$username,'password'=>$password,'dbname'=>$dbname, 'dbtablepre'=>$dbtablepre, 'charset'=>$charset);
  64. if ($this->db->update($sql, array('id'=>$id))) {
  65. dbsource_cache();
  66. showmessage('', '', '', 'edit');
  67. } else {
  68. showmessage(L('operation_failure'));
  69. }
  70. } else {
  71. pc_base::load_sys_class('form', '', 0);
  72. $show_header = $show_validator = true;
  73. include $this->admin_tpl('dbsource_edit');
  74. }
  75. }
  76. public function del() {
  77. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : '';
  78. if ($this->db->get_one(array('id'=>$id))) {
  79. if ($this->db->delete(array('id'=>$id))) {
  80. dbsource_cache();
  81. showmessage(L('operation_success'), '?m=dbsource&c=dbsource_admin&a=init');
  82. } else {
  83. showmessage(L('operation_failure'), '?m=dbsource&c=dbsource_admin&a=init');
  84. }
  85. } else {
  86. showmessage(L('notfound'), '?m=dbsource&c=dbsource_admin&a=init');
  87. }
  88. }
  89. public function public_name() {
  90. $name = isset($_GET['name']) && trim($_GET['name']) ? (pc_base::load_config('system', 'charset') == 'gbk' ? iconv('utf-8', 'gbk', trim($_GET['name'])) : trim($_GET['name'])) : exit('0');
  91. $id = isset($_GET['id']) && intval($_GET['id']) ? intval($_GET['id']) : '';
  92. $data = array();
  93. if ($id) {
  94. $data = $this->db->get_one(array('id'=>$id), 'name');
  95. if (!empty($data) && $data['name'] == $name) {
  96. exit('1');
  97. }
  98. }
  99. if ($this->db->get_one(array('name'=>$name), 'id')) {
  100. exit('0');
  101. } else {
  102. exit('1');
  103. }
  104. }
  105. public function public_test_mysql_connect() {
  106. $host = isset($_GET['host']) && trim($_GET['host']) ? trim($_GET['host']) : exit('0');
  107. $password = isset($_GET['password']) && trim($_GET['password']) ? trim($_GET['password']) : exit('0');
  108. $port = isset($_GET['port']) && intval($_GET['port']) ? intval($_GET['port']) : exit('0');
  109. $username = isset($_GET['username']) && trim($_GET['username']) ? trim($_GET['username']) : exit('0');
  110. if (@mysql_connect($host.':'.$port, $username, $password)) {
  111. exit('1');
  112. } else {
  113. exit('0');
  114. }
  115. }
  116. }