123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- class DatabaseModule
- {
- public function __construct()
- {
- @session_start();
- }
-
- public function getUserType(&$dbID)
- {
- $dao = new AuthorizationDao();
- $result = $dao->getDatabaseUserType($_SESSION['userID'], $dbID);
- if ($result === FALSE) {
- return -1;
- } else {
- return $result;
- }
- }
-
- public function addDatabase(&$dbName, &$dbVersion = "1.0")
- {
- $databaseDao = new DatabaseDao;
- return $databaseDao->addDatabase($dbName, $dbVersion, $_SESSION['userID']);
- }
-
- public function deleteDatabase(&$dbID)
- {
- $databaseDao = new DatabaseDao;
- if ($dbID = $databaseDao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
- return $databaseDao->deleteDatabase($dbID);
- } else
- return FALSE;
- }
-
- public function getDatabase()
- {
- $databaseDao = new DatabaseDao;
- return $databaseDao->getDatabase($_SESSION['userID']);
- }
-
- public function editDatabase(&$dbID, &$dbName, &$dbVersion)
- {
- $databaseDao = new DatabaseDao;
- if ($dbID = $databaseDao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
- return $databaseDao->editDatabase($dbID, $dbName, $dbVersion);
- } else
- return FALSE;
- }
-
- public function importDatabase(&$dbID, &$tables)
- {
- $userID = $_SESSION['userID'];
- $databaseDao = new DatabaseDao;
- $databaseTableDao = new DatabaseTableDao;
- if ($dbID = $databaseDao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
- $tableList = array();
- foreach ($tables as $table) {
- $fieldList = array();
-
- preg_match_all('/.+?[\r\n]+/s', $table['tableField'], $fields);
- $primaryKeys = '';
- foreach ($fields[0] as $field) {
- $field = trim($field);
-
- if (strpos($field, '`') === 0) {
- $fieldName = substr($field, 1, strpos(substr($field, 1), '`'));
-
- preg_match('/`\\s(.+?)\\s/', $field, $type);
- preg_match("/COMMENT.*'(.*?)'/", $field, $fieldDesc);
- if (empty($fieldDesc)) {
- preg_match("/comment.*'(.*?)'/", $field, $fieldDesc);
- }
- if (!$type[1]) {
- $type[1] = substr($field, strlen($fieldName) + 3, strpos(substr($field, strlen($fieldName) + 3), ','));
- }
- if (!$type[1]) {
- $type[1] = substr($field, strlen($fieldName) + 3);
- }
- if (strpos($type[1], '(')) {
- $fieldType = substr($type[1], 0, strpos($type[1], '('));
- if (preg_match('/\([0-9]{1,10}/', $type[1], $match)) {
-
- $fieldLength = substr($match[0], 1);
- } else {
- $fieldLength = '0';
- }
- } else {
- $fieldType = $type[1];
-
- $fieldLength = '0';
- }
- if (strpos($field, 'NOT NULL') !== FALSE) {
- $isNotNull = 1;
- } else
- $isNotNull = 0;
- $fieldList[] = array(
- 'fieldName' => $fieldName,
- 'fieldDesc' => $fieldDesc[1],
- 'fieldType' => $fieldType,
- 'fieldLength' => $fieldLength,
- 'isNotNull' => $isNotNull
- );
- }
-
- if (strpos($field, 'PRIMARY') !== FALSE) {
- $table['primaryKey'] = $table['primaryKey'] . substr($field, strpos($field, '('));
- }
- }
-
- foreach ($fieldList as &$tableField) {
- if (strpos($table['primaryKey'], $tableField['fieldName']) !== FALSE) {
- $tableField['isPrimaryKey'] = 1;
- } else {
- $tableField['isPrimaryKey'] = 0;
- }
- }
- $tableList[] = array(
- 'tableName' => $table['tableName'],
- 'tableDesc' => $table['tableDesc'],
- 'fieldList' => $fieldList
- );
- unset($fieldList);
- }
- if (isset($tableList[0]))
- return $databaseDao->importDatabase($dbID, $tableList);
- else
- return FALSE;
- } else
- return FALSE;
- }
-
- public function importDatabseByJson(&$data)
- {
- $user_id = $_SESSION['userID'];
- $service = new DatabaseDao;
- $result = $service->importDatabaseByJson($user_id, $data);
- if ($result)
- return TRUE;
- else
- return FALSE;
- }
-
- public function exportDatabase(&$dbID)
- {
- $userID = $_SESSION['userID'];
- $dao = new DatabaseDao;
- if ($dao->checkDatabasePermission($dbID, $_SESSION['userID'])) {
- $dumpJson = json_encode($dao->getDatabaseInfo($dbID));
- $fileName = 'eolinker_export_' . $_SESSION['userName'] . '_' . time() . '.export';
- if (file_put_contents(realpath('./dump') . DIRECTORY_SEPARATOR . $fileName, $dumpJson)) {
- return $fileName;
- } else {
- return FALSE;
- }
- } else
- return FALSE;
- }
- }
- ?>
|