DatabaseController.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @name eolinker ams open source,eolinker开源版本
  4. * @link https://www.eolinker.com/
  5. * @package eolinker
  6. * @author www.eolinker.com 广州银云信息科技有限公司 ©2015-2018
  7. * eoLinker是目前全球领先、国内最大的在线API接口管理平台,提供自动生成API文档、API自动化测试、Mock测试、团队协作等功能,旨在解决由于前后端分离导致的开发效率低下问题。
  8. * 如在使用的过程中有任何问题,欢迎加入用户讨论群进行反馈,我们将会以最快的速度,最好的服务态度为您解决问题。
  9. *
  10. * eoLinker AMS开源版的开源协议遵循Apache License 2.0,如需获取最新的eolinker开源版以及相关资讯,请访问:https://www.eolinker.com/#/os/download
  11. *
  12. * 官方网站:https://www.eolinker.com/
  13. * 官方博客以及社区:http://blog.eolinker.com/
  14. * 使用教程以及帮助:http://help.eolinker.com/
  15. * 商务合作邮箱:market@eolinker.com
  16. * 用户讨论QQ群:284421832
  17. */
  18. class DatabaseController
  19. {
  20. // return an json object
  21. //返回Json类型
  22. private $returnJson = array('type' => 'database');
  23. /**
  24. * Checkout login status
  25. * 检查登录状态
  26. */
  27. public function __construct()
  28. {
  29. //identity authentication
  30. // 身份验证
  31. $server = new GuestModule;
  32. if (!$server->checkLogin()) {
  33. $this->returnJson['statusCode'] = '120005';
  34. exitOutput($this->returnJson);
  35. }
  36. }
  37. /**
  38. * Add database
  39. * 添加数据库
  40. */
  41. public function addDatabase()
  42. {
  43. $nameLen = mb_strlen(quickInput('dbName'), 'utf8');
  44. $dbName = securelyInput('dbName');
  45. $dbVersion = securelyInput('dbVersion');
  46. if (!($nameLen >= 1 && $nameLen <= 32)) {
  47. // illegal database name length
  48. // 数据库名长度不合法
  49. $this->returnJson['statusCode'] = '220001';
  50. } elseif (!(is_float(floatval($dbVersion)) && intval($dbVersion))) {
  51. // illegal database version
  52. // 数据库版本不合法
  53. $this->returnJson['statusCode'] = '220002';
  54. } else {
  55. $service = new DatabaseModule;
  56. $result = $service->addDatabase($dbName, $dbVersion);
  57. if ($result) {
  58. $this->returnJson['statusCode'] = '000000';
  59. $this->returnJson['dbID'] = $result;
  60. } else {
  61. $this->returnJson['statusCode'] = '220003';
  62. }
  63. }
  64. exitOutput($this->returnJson);
  65. }
  66. /**
  67. * Delete database
  68. * 删除数据库
  69. */
  70. public function deleteDatabase()
  71. {
  72. $dbID = securelyInput('dbID');
  73. $module = new DatabaseModule();
  74. $userType = $module->getUserType($dbID);
  75. if ($userType < 0 || $userType > 1) {
  76. $this->returnJson['statusCode'] = '120007';
  77. exitOutput($this->returnJson);
  78. }
  79. // illegal database ID
  80. //数据库ID格式非法
  81. if (!preg_match('/^[0-9]{1,11}$/', $dbID)) {
  82. $this->returnJson['statusCode'] = '220004';
  83. } else {
  84. $service = new DatabaseModule;
  85. $result = $service->deleteDatabase($dbID);
  86. if ($result) {
  87. $this->returnJson['statusCode'] = '000000';
  88. } else {
  89. $this->returnJson['statusCode'] = '220005';
  90. }
  91. }
  92. exitOutput($this->returnJson);
  93. }
  94. /**
  95. * Get all database list
  96. * 获取数据库列表
  97. */
  98. public function getDatabase()
  99. {
  100. $service = new DatabaseModule;
  101. $result = $service->getDatabase();
  102. if ($result) {
  103. $this->returnJson['statusCode'] = '000000';
  104. $this->returnJson['databaseList'] = $result;
  105. } else {
  106. $this->returnJson['statusCode'] = '220006';
  107. }
  108. exitOutput($this->returnJson);
  109. }
  110. /**
  111. * Edit database
  112. * 修改数据库
  113. */
  114. public function editDatabase()
  115. {
  116. $dbID = securelyInput('dbID');
  117. $module = new DatabaseModule();
  118. $userType = $module->getUserType($dbID);
  119. if ($userType < 0 || $userType > 2) {
  120. $this->returnJson['statusCode'] = '120007';
  121. exitOutput($this->returnJson);
  122. }
  123. $nameLen = mb_strlen(quickInput('dbName'), 'utf8');
  124. $dbName = securelyInput('dbName');
  125. $dbVersion = securelyInput('dbVersion');
  126. // illegal database ID
  127. //数据库ID格式非法
  128. if (!preg_match('/^[0-9]{1,11}$/', $dbID)) {
  129. $this->returnJson['statusCode'] = '220004';
  130. } elseif (!($nameLen >= 1 && $nameLen <= 32)) {
  131. // illegal database name length
  132. // 数据库名长度不合法
  133. $this->returnJson['statusCode'] = '220001';
  134. } elseif (!(is_float(floatval($dbVersion)) && intval($dbVersion))) {
  135. // illegal database version
  136. // 数据库版本不合法
  137. $this->returnJson['statusCode'] = '220002';
  138. } else {
  139. $service = new DatabaseModule;
  140. $result = $service->editDatabase($dbID, $dbName, $dbVersion);
  141. if ($result) {
  142. $this->returnJson['statusCode'] = '000000';
  143. } else {
  144. $this->returnJson['statusCode'] = '220007';
  145. }
  146. }
  147. exitOutput($this->returnJson);
  148. }
  149. /**
  150. * Export database's data
  151. * 导出数据库
  152. */
  153. public function exportDatabase()
  154. {
  155. $dbID = quickInput('dbID');
  156. $module = new DatabaseModule();
  157. $userType = $module->getUserType($dbID);
  158. if ($userType < 0 || $userType > 1) {
  159. $this->returnJson['statusCode'] = '120007';
  160. exitOutput($this->returnJson);
  161. }
  162. // illegal database ID
  163. //数据库ID格式非法
  164. if (!preg_match('/^[0-9]{1,11}$/', $dbID)) {
  165. $this->returnJson['statusCode'] = '220004';
  166. } else {
  167. $service = new DatabaseModule;
  168. $fileName = $service->exportDatabase($dbID);
  169. if ($fileName) {
  170. $this->returnJson['statusCode'] = '000000';
  171. $this->returnJson['fileName'] = $fileName;
  172. } else {
  173. //export fail
  174. //导出失败
  175. $this->returnJson['statusCode'] = '220000';
  176. }
  177. }
  178. exitOutput($this->returnJson);
  179. }
  180. /**
  181. * Import database table which export from mysql
  182. * 导入SQL格式数据表
  183. */
  184. public function importDatabase()
  185. {
  186. $dbID = securelyInput('dbID');
  187. $module = new DatabaseModule();
  188. $userType = $module->getUserType($dbID);
  189. if ($userType < 0 || $userType > 2) {
  190. $this->returnJson['statusCode'] = '120007';
  191. exitOutput($this->returnJson);
  192. }
  193. $dumpSql = quickInput('dumpSql');
  194. //illegal database ID
  195. //数据库ID格式非法
  196. if (!preg_match('/^[0-9]{1,11}$/', $dbID)) {
  197. $this->returnJson['statusCode'] = '230001';
  198. } else {
  199. $tables = array();
  200. //match all statement blocks which create tables using regex
  201. //正则匹配出所有创建表的语句块
  202. preg_match_all('/CREATE.*?TABLE[\\s\\S]+?;/', $dumpSql, $sql);
  203. if (empty($sql)) {
  204. preg_match_all('/create.*?table[\\s\\S]+?;/', $dumpSql, $sql);
  205. }
  206. preg_match_all('/ALTER.*?TABLE[\\s\\S]+?PRIMARY.+?\\)/', $dumpSql, $primaryKeys);
  207. if (empty($primaryKeys)) {
  208. preg_match_all('/alter.*?table[\\s\\S]+?primary.+?\\)/', $dumpSql, $primaryKeys);
  209. }
  210. foreach ($sql[0] as $tableSql) {
  211. // get table name from the sql
  212. //正则提取表名,结果为array,取索引为1
  213. preg_match('/`(.*?)`/', $tableSql, $tableName);
  214. preg_match("/COMMENT='(.*?)'/", $tableSql, $tableDesc);
  215. if (empty($tableDesc)) {
  216. preg_match("/comment='(.*?)'/", $tableSql, $tableDesc);
  217. }
  218. // get table fields from the sql
  219. //截取表的字段信息
  220. $tableField = substr(substr($tableSql, strpos($tableSql, '(') + 1), 0, strlen($tableSql) - strpos($tableSql, '(') - 9);
  221. if ($primaryKeys[0] != NULL) {
  222. $key = '';
  223. foreach ($primaryKeys[0] as $primaryKey) {
  224. if (strpos($primaryKey, $tableName[1]) !== FALSE) {
  225. $key = substr($primaryKey, strpos($primaryKey, '('));
  226. break;
  227. }
  228. }
  229. }
  230. $tables[] = array(
  231. 'tableName' => $tableName[1],
  232. 'tableDesc' => $tableDesc[1],
  233. 'tableField' => $tableField,
  234. 'primaryKey' => $key
  235. );
  236. }
  237. if (count($tables) > 0) {
  238. $service = new DatabaseModule;
  239. $result = $service->importDatabase($dbID, $tables);
  240. if ($result) {
  241. $this->returnJson['statusCode'] = '000000';
  242. //$this -> returnJson['result'] =$result;
  243. } else {
  244. //import fail
  245. //导入失败
  246. $this->returnJson['statusCode'] = '220008';
  247. }
  248. } else {
  249. //cannot find any create table statement block in the sql data
  250. //导入的sql文件未匹配到创建表的语句块
  251. $this->returnJson['statusCode'] = '220009';
  252. }
  253. }
  254. exitOutput($this->returnJson);
  255. }
  256. /**
  257. * Import database by database's data which export from the api named exportDatabase
  258. * 导入数据库
  259. */
  260. public function importDatabseByJson()
  261. {
  262. $json = quickInput('data');
  263. $data = json_decode($json, TRUE);
  264. if (empty($data)) {
  265. //empty data
  266. //数据为空
  267. $this->returnJson['statusCode'] = '220010';
  268. } else {
  269. $service = new DatabaseModule;
  270. $result = $service->importDatabseByJson($data);
  271. if ($result) {
  272. $this->returnJson['statusCode'] = '000000';
  273. } else {
  274. $this->returnJson['statusCode'] = '220000';
  275. }
  276. }
  277. exitOutput($this->returnJson);
  278. }
  279. }
  280. ?>