123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676 |
- <?php
- if (!defined('SMF'))
- die('No direct access...');
- function db_packages_init()
- {
- global $smcFunc, $reservedTables, $db_package_log, $db_prefix;
- if (!isset($smcFunc['db_create_table']) || $smcFunc['db_create_table'] != 'smf_db_create_table')
- {
- $smcFunc += array(
- 'db_add_column' => 'smf_db_add_column',
- 'db_add_index' => 'smf_db_add_index',
- 'db_calculate_type' => 'smf_db_calculate_type',
- 'db_change_column' => 'smf_db_change_column',
- 'db_create_table' => 'smf_db_create_table',
- 'db_drop_table' => 'smf_db_drop_table',
- 'db_table_structure' => 'smf_db_table_structure',
- 'db_list_columns' => 'smf_db_list_columns',
- 'db_list_indexes' => 'smf_db_list_indexes',
- 'db_remove_column' => 'smf_db_remove_column',
- 'db_remove_index' => 'smf_db_remove_index',
- );
- $db_package_log = array();
- }
-
- $reservedTables = array('admin_info_files', 'approval_queue', 'attachments', 'ban_groups', 'ban_items',
- 'board_permissions', 'boards', 'calendar', 'calendar_holidays', 'categories', 'collapsed_categories',
- 'custom_fields', 'group_moderators', 'log_actions', 'log_activity', 'log_banned', 'log_boards',
- 'log_digest', 'log_errors', 'log_floodcontrol', 'log_group_requests', 'log_karma', 'log_mark_read',
- 'log_notify', 'log_online', 'log_packages', 'log_polls', 'log_reported', 'log_reported_comments',
- 'log_scheduled_tasks', 'log_search_messages', 'log_search_results', 'log_search_subjects',
- 'log_search_topics', 'log_topics', 'mail_queue', 'membergroups', 'members', 'message_icons',
- 'messages', 'moderators', 'package_servers', 'permission_profiles', 'permissions', 'personal_messages',
- 'pm_recipients', 'poll_choices', 'polls', 'scheduled_tasks', 'sessions', 'settings', 'smileys',
- 'themes', 'topics');
- foreach ($reservedTables as $k => $table_name)
- $reservedTables[$k] = strtolower($db_prefix . $table_name);
-
- db_extend('extra');
- }
- function smf_db_create_table($table_name, $columns, $indexes = array(), $parameters = array(), $if_exists = 'ignore', $error = 'fatal')
- {
- global $reservedTables, $smcFunc, $db_package_log, $db_prefix, $db_character_set;
-
- $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
-
- $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
-
- if (in_array(strtolower($table_name), $reservedTables))
- return false;
-
- $db_package_log[] = array('remove_table', $table_name);
-
- $tables = $smcFunc['db_list_tables']();
- if (in_array($full_table_name, $tables))
- {
-
- if ($if_exists == 'overwrite')
- $smcFunc['db_drop_table']($table_name);
- else
- return $if_exists == 'ignore';
- }
-
- $table_query = 'CREATE TABLE ' . $table_name . "\n" . '(';
- foreach ($columns as $column)
- $table_query .= "\n\t" . smf_db_create_query_column($column) . ',';
-
- foreach ($indexes as $index)
- {
- $columns = implode(',', $index['columns']);
-
- if (isset($index['type']) && $index['type'] == 'primary')
- $table_query .= "\n\t" . 'PRIMARY KEY (' . implode(',', $index['columns']) . '),';
- else
- {
- if (empty($index['name']))
- $index['name'] = implode('_', $index['columns']);
- $table_query .= "\n\t" . (isset($index['type']) && $index['type'] == 'unique' ? 'UNIQUE' : 'KEY') . ' ' . $index['name'] . ' (' . $columns . '),';
- }
- }
-
- if (substr($table_query, -1) == ',')
- $table_query = substr($table_query, 0, -1);
- $table_query .= ') ENGINE=MyISAM';
- if (!empty($db_character_set) && $db_character_set == 'utf8')
- $table_query .= ' DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci';
-
- $smcFunc['db_query']('', $table_query,
- array(
- 'security_override' => true,
- )
- );
- return true;
- }
- function smf_db_drop_table($table_name, $parameters = array(), $error = 'fatal')
- {
- global $reservedTables, $smcFunc, $db_prefix;
-
- $real_prefix = preg_match('~^(`?)(.+?)\\1\\.(.*?)$~', $db_prefix, $match) === 1 ? $match[3] : $db_prefix;
-
- $full_table_name = str_replace('{db_prefix}', $real_prefix, $table_name);
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
-
- if (in_array(strtolower($table_name), $reservedTables))
- return false;
-
- if (in_array($full_table_name, $smcFunc['db_list_tables']()))
- {
- $query = 'DROP TABLE ' . $table_name;
- $smcFunc['db_query']('',
- $query,
- array(
- 'security_override' => true,
- )
- );
- return true;
- }
-
- return false;
- }
- function smf_db_add_column($table_name, $column_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
- {
- global $smcFunc, $db_package_log, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
-
- $db_package_log[] = array('remove_column', $table_name, $column_info['name']);
-
- $columns = $smcFunc['db_list_columns']($table_name, false);
- foreach ($columns as $column)
- if ($column == $column_info['name'])
- {
-
- if ($if_exists == 'update')
- return $smcFunc['db_change_column']($table_name, $column_info['name'], $column_info);
- else
- return false;
- }
-
- $column_info['size'] = isset($column_info['size']) && is_numeric($column_info['size']) ? $column_info['size'] : null;
- list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
-
- $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
- if ($size !== null)
- $type = $type . '(' . $size . ')';
-
- $query = '
- ALTER TABLE ' . $table_name . '
- ADD ' . smf_db_create_query_column($column_info) . (empty($column_info['auto']) ? '' : ' primary key');
- $smcFunc['db_query']('', $query,
- array(
- 'security_override' => true,
- )
- );
- return true;
- }
- function smf_db_remove_column($table_name, $column_name, $parameters = array(), $error = 'fatal')
- {
- global $smcFunc, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
-
- $columns = $smcFunc['db_list_columns']($table_name, true);
- foreach ($columns as $column)
- if ($column['name'] == $column_name)
- {
- $smcFunc['db_query']('', '
- ALTER TABLE ' . $table_name . '
- DROP COLUMN ' . $column_name,
- array(
- 'security_override' => true,
- )
- );
- return true;
- }
-
- return false;
- }
- function smf_db_change_column($table_name, $old_column, $column_info, $parameters = array(), $error = 'fatal')
- {
- global $smcFunc, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
-
- $columns = $smcFunc['db_list_columns']($table_name, true);
- $old_info = null;
- foreach ($columns as $column)
- if ($column['name'] == $old_column)
- $old_info = $column;
-
- if ($old_info == null)
- return false;
-
- if (!isset($column_info['name']))
- $column_info['name'] = $old_column;
- if (!isset($column_info['default']))
- $column_info['default'] = $old_info['default'];
- if (!isset($column_info['null']))
- $column_info['null'] = $old_info['null'];
- if (!isset($column_info['auto']))
- $column_info['auto'] = $old_info['auto'];
- if (!isset($column_info['type']))
- $column_info['type'] = $old_info['type'];
- if (!isset($column_info['size']) || !is_numeric($column_info['size']))
- $column_info['size'] = $old_info['size'];
- if (!isset($column_info['unsigned']) || !in_array($column_info['type'], array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')))
- $column_info['unsigned'] = '';
- list ($type, $size) = $smcFunc['db_calculate_type']($column_info['type'], $column_info['size']);
-
- $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column_info['unsigned']) ? 'unsigned ' : '';
- if ($size !== null)
- $type = $type . '(' . $size . ')';
- $smcFunc['db_query']('', '
- ALTER TABLE ' . $table_name . '
- CHANGE COLUMN `' . $old_column . '` `' . $column_info['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (empty($column_info['null']) ? 'NOT NULL' : '') . ' ' .
- (!isset($column_info['default']) ? '' : 'default \'' . $smcFunc['db_escape_string']($column_info['default']) . '\'') . ' ' .
- (empty($column_info['auto']) ? '' : 'auto_increment') . ' ',
- array(
- 'security_override' => true,
- )
- );
- }
- function smf_db_add_index($table_name, $index_info, $parameters = array(), $if_exists = 'update', $error = 'fatal')
- {
- global $smcFunc, $db_package_log, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
-
- if (empty($index_info['columns']))
- return false;
- $columns = implode(',', $index_info['columns']);
-
- if (empty($index_info['name']))
- {
-
- if (isset($index_info['type']) && $index_info['type'] == 'primary')
- $index_info['name'] = '';
- else
- $index_info['name'] = implode('_', $index_info['columns']);
- }
- else
- $index_info['name'] = $index_info['name'];
-
- $db_package_log[] = array('remove_index', $table_name, $index_info['name']);
-
- $indexes = $smcFunc['db_list_indexes']($table_name, true);
-
- foreach ($indexes as $index)
- {
- if ($index['name'] == $index_info['name'] || ($index['type'] == 'primary' && isset($index_info['type']) && $index_info['type'] == 'primary'))
- {
-
- if ($if_exists != 'update' || $index['type'] == 'primary')
- return false;
- else
- $smcFunc['db_remove_index']($table_name, $index_info['name']);
- }
- }
-
- if (!empty($index_info['type']) && $index_info['type'] == 'primary')
- {
- $smcFunc['db_query']('', '
- ALTER TABLE ' . $table_name . '
- ADD PRIMARY KEY (' . $columns . ')',
- array(
- 'security_override' => true,
- )
- );
- }
- else
- {
- $smcFunc['db_query']('', '
- ALTER TABLE ' . $table_name . '
- ADD ' . (isset($index_info['type']) && $index_info['type'] == 'unique' ? 'UNIQUE' : 'INDEX') . ' ' . $index_info['name'] . ' (' . $columns . ')',
- array(
- 'security_override' => true,
- )
- );
- }
- }
- function smf_db_remove_index($table_name, $index_name, $parameters = array(), $error = 'fatal')
- {
- global $smcFunc, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
-
- $indexes = $smcFunc['db_list_indexes']($table_name, true);
- foreach ($indexes as $index)
- {
-
- if ($index['type'] == 'primary' && $index_name == 'primary')
- {
-
- $smcFunc['db_query']('', '
- ALTER TABLE ' . $table_name . '
- DROP PRIMARY KEY',
- array(
- 'security_override' => true,
- )
- );
- return true;
- }
- if ($index['name'] == $index_name)
- {
-
- $smcFunc['db_query']('', '
- ALTER TABLE ' . $table_name . '
- DROP INDEX ' . $index_name,
- array(
- 'security_override' => true,
- )
- );
- return true;
- }
- }
-
- return false;
- }
- function smf_db_calculate_type($type_name, $type_size = null, $reverse = false)
- {
-
- return array($type_name, $type_size);
- }
- function smf_db_table_structure($table_name, $parameters = array())
- {
- global $smcFunc, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
- return array(
- 'name' => $table_name,
- 'columns' => $smcFunc['db_list_columns']($table_name, true),
- 'indexes' => $smcFunc['db_list_indexes']($table_name, true),
- );
- }
- function smf_db_list_columns($table_name, $detail = false, $parameters = array())
- {
- global $smcFunc, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
- $result = $smcFunc['db_query']('', '
- SHOW FIELDS
- FROM {raw:table_name}',
- array(
- 'table_name' => substr($table_name, 0, 1) == '`' ? $table_name : '`' . $table_name . '`',
- )
- );
- $columns = array();
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- if (!$detail)
- {
- $columns[] = $row['Field'];
- }
- else
- {
-
- $auto = strpos($row['Extra'], 'auto_increment') !== false ? true : false;
-
- if (preg_match('~(.+?)\s*\((\d+)\)(?:(?:\s*)?(unsigned))?~i', $row['Type'], $matches) === 1)
- {
- $type = $matches[1];
- $size = $matches[2];
- if (!empty($matches[3]) && $matches[3] == 'unsigned')
- $unsigned = true;
- }
- else
- {
- $type = $row['Type'];
- $size = null;
- }
- $columns[$row['Field']] = array(
- 'name' => $row['Field'],
- 'null' => $row['Null'] != 'YES' ? false : true,
- 'default' => isset($row['Default']) ? $row['Default'] : null,
- 'type' => $type,
- 'size' => $size,
- 'auto' => $auto,
- );
- if (isset($unsigned))
- {
- $columns[$row['Field']]['unsigned'] = $unsigned;
- unset($unsigned);
- }
- }
- }
- $smcFunc['db_free_result']($result);
- return $columns;
- }
- function smf_db_list_indexes($table_name, $detail = false, $parameters = array())
- {
- global $smcFunc, $db_prefix;
- $table_name = str_replace('{db_prefix}', $db_prefix, $table_name);
- $result = $smcFunc['db_query']('', '
- SHOW KEYS
- FROM {raw:table_name}',
- array(
- 'table_name' => substr($table_name, 0, 1) == '`' ? $table_name : '`' . $table_name . '`',
- )
- );
- $indexes = array();
- while ($row = $smcFunc['db_fetch_assoc']($result))
- {
- if (!$detail)
- $indexes[] = $row['Key_name'];
- else
- {
-
- if ($row['Key_name'] == 'PRIMARY')
- $type = 'primary';
- elseif (empty($row['Non_unique']))
- $type = 'unique';
- elseif (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT')
- $type = 'fulltext';
- else
- $type = 'index';
-
- if (empty($indexes[$row['Key_name']]))
- {
- $indexes[$row['Key_name']] = array(
- 'name' => $row['Key_name'],
- 'type' => $type,
- 'columns' => array(),
- );
- }
-
- if (!empty($row['Sub_part']))
- $indexes[$row['Key_name']]['columns'][] = $row['Column_name'] . '(' . $row['Sub_part'] . ')';
- else
- $indexes[$row['Key_name']]['columns'][] = $row['Column_name'];
- }
- }
- $smcFunc['db_free_result']($result);
- return $indexes;
- }
- function smf_db_create_query_column($column)
- {
- global $smcFunc;
-
- if (!empty($column['auto']))
- {
- $default = 'auto_increment';
- }
- elseif (isset($column['default']) && $column['default'] !== null)
- $default = 'default \'' . $smcFunc['db_escape_string']($column['default']) . '\'';
- else
- $default = '';
-
- $column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
- list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']);
-
- $unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : '';
- if ($size !== null)
- $type = $type . '(' . $size . ')';
-
- return '`' .$column['name'] . '` ' . $type . ' ' . (!empty($unsigned) ? $unsigned : '') . (!empty($column['null']) ? '' : 'NOT NULL') . ' ' . $default;
- }
- ?>
|