Skip to content

Commit

Permalink
Migrate from ODBC to PDO extension.
Browse files Browse the repository at this point in the history
Fix wrong column in character module.
Question during installation on changing the database collate.
  • Loading branch information
Vaflan committed Apr 1, 2023
1 parent b26578c commit b1605f9
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 64 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Created: 2009-03-17
Repository: https://github.com/Vaflan/MyMuWeb
Author: Ruslans Jermakovics <Vaflan>
Contact: https://github.com/Vaflan
Contact: http://mymuweb.ru


## Instruction
Expand All @@ -13,10 +13,11 @@ The first step, you need PHP settings:
* On Windows system ~ C:\WINDOWS\php.ini (extensions with .dll)
* On Linux system ~ /etc/php/phpX.X/php.ini


extension = gd2
extension = odbc
error_reporting = E_ALL & ~E_NOTICE
extension = gd2
extension = pdo
extension = odbc
extension = pdo_odbc
error_reporting = E_ALL & ~E_NOTICE

*********************************************************************
The second step, add library if database uses MD5
Expand All @@ -41,4 +42,5 @@ The third step is to change socks
*********************************************************************


### Thanks for using MyMuWeb by Vaflan!
### Thanks for using MyMuWeb by Vaflan!
Special thanks to the **x-Mu** Community: https://x-mu.net/?board=84.0
1 change: 1 addition & 0 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
$mmw['sql']['user'] = 'USER'; // Login SQL
$mmw['sql']['pass'] = 'PASSWORD'; // Pass SQL
$mmw['sql']['database'] = 'MuOnline'; // DataBase SQL
// Example: $mmw['sql']['driver'] = 'dblib:host=%host%;dbname=%dbnm%';


// MyMuWeb Config
Expand Down
3 changes: 2 additions & 1 deletion includes/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
}


$result = mssql_query("SELECT mc.c_id,
$result = mssql_query("SELECT
mc.c_id,
mc.c_char,
mc.c_text,
mc.c_date,
Expand Down
11 changes: 8 additions & 3 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,12 @@ function default_img($src)
/////// Start BBCode Formats ///////
function bbcode($text)
{
global $flash_body_size;
if (empty($flash_body_size)) {
$flash_body_size = 426;
}
$aspectRatioHeight = round($flash_body_size * 9 / 16);

$bbCode = array(
'/\[br\]/is' => '<br>',
'/\[hr\]/is' => '<hr>',
Expand All @@ -287,10 +293,9 @@ function bbcode($text)
'/\[font\=(.*?)\](.*?)\[\/font\]/is' => '<span style="font:$1">$2</span>',
'/\[size\=(.*?)\](.*?)\[\/size\]/is' => '<span style="font-size:$1 pt;">$2</span>',
'/\[url\=(.*?)\](.*?)\[\/url\]/is' => '<a target="_blank" href="$1">$2</a>',
'/\[video\].*youtube.com\/watch[^=]+=(.*?)\[\/video\]/is' => '<iframe width="416" height="234" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture" allowfullscreen></iframe>',
'/\[video\].*youtube.com\/watch[^=]+=(.*?)\[\/video\]/is' => '<iframe width="' . $flash_body_size . '" height="' . $aspectRatioHeight . '" src="https://www.youtube.com/embed/$1" frameborder="0" allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture" allowfullscreen></iframe>',
);
$text = preg_replace(array_keys($bbCode), array_values($bbCode), $text);
return $text;
return preg_replace(array_keys($bbCode), array_values($bbCode), $text);
}

/////// END BBCode Formats ///////
Expand Down
112 changes: 82 additions & 30 deletions includes/mmw_sql.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/*
It's bridge from mssql to odbc
It's bridge from mssql to pdo
Copyright by Ruslans Jermakovics [Vaflan]
Version of bridge 2.1 - 23:39 12.02.2023
Version of bridge 3.0 - 00:00 08.03.2023
Insert to config.php: require_once __DIR__ . '/includes/mmwsql.php';
*/

Expand All @@ -18,18 +18,21 @@
function mssql_connect($servername = null, $username = null, $password = null, $new_link = false)
{
global $mmwsql;

$mmwsql = array(
'connect' => true,
'host' => $servername,
'user' => $username,
'pass' => $password,
'driver' => $mmwsql['driver'] ?: 'odbc:Driver={SQL Server};Server={%host%};Database={%dbnm%};',
);
// Example Linux with dblib: $mmwsql['driver'] = 'dblib:host=%host%;dbname=%dbnm%';

if (!extension_loaded('odbc')) {
throw new Exception('ODBC extension not loaded. Please open php.ini and add odbc extension (extension=odbc)');
if (!extension_loaded('pdo')) {
throw new Exception('PDO extension not loaded. Please open php.ini and add pdo extension');
}

return true;
return $mmwsql['connect'];
}

/**
Expand All @@ -39,11 +42,24 @@ function mssql_connect($servername = null, $username = null, $password = null, $
function mssql_select_db($database_name, $link_identifier = null)
{
global $mmwsql;

$mmwsql['dbnm'] = $database_name;
if ($link_identifier) {
return $mmwsql['connect'] = odbc_connect('Driver={SQL Server};Server={' . $mmwsql['host'] . '};Database={' . $mmwsql['dbnm'] . '};', $mmwsql['user'], $mmwsql['pass']);
$mmwsql['connect'] = new PDO(
str_replace(
array_map(static function ($value) {
return '%' . $value . '%';
}, array_keys($mmwsql)),
array_values($mmwsql),
$mmwsql['driver']
),
$mmwsql['user'],
$mmwsql['pass']
);
$mmwsql['list'] = array();
}
return false;

return $mmwsql['connect'];
}

/**
Expand All @@ -53,15 +69,22 @@ function mssql_select_db($database_name, $link_identifier = null)
function mssql_query($query, $link_identifier = null, $batch_size = 0)
{
global $mmwsql;

$lastQuery = end($mmwsql['list']);
if ($lastQuery['id']) {
$lastQuery['id']->closeCursor();
}

$mmwsql['last_query'] = $query;
$mmwsql['list'][] = array(
'id' => @odbc_exec($link_identifier ? $link_identifier : $mmwsql['connect'], $query),
'id' => $mmwsql['connect']->query($query),
'query' => $query,
);
$resourceId = end($mmwsql['list'])['id'];
if ($resourceId === false) {
throw new Exception(odbc_errormsg() . PHP_EOL . $query);
throw new Exception(mssql_get_last_message() . PHP_EOL . $query);
}

return $resourceId;
}

Expand All @@ -71,7 +94,6 @@ function mssql_query($query, $link_identifier = null, $batch_size = 0)
*/
function mssql_fetch_row($result)
{
/* ODBC fetch_row have issue, hack by Vaflan .!.. */
return mssql_fetch_array($result, MSSQL_NUM);
}

Expand All @@ -81,9 +103,13 @@ function mssql_fetch_row($result)
*/
function mssql_fetch_assoc($result_id)
{
if ($result = odbc_fetch_array($result_id)) {
return $result;
/** @var $result_id PDOStatement */

// Important BOTH! mssql_fetch_assoc have issue, hack by Vaflan .!..
if ($data = $result_id->fetch(PDO::FETCH_BOTH)) {
return $data;
}

return false;
}

Expand All @@ -93,18 +119,19 @@ function mssql_fetch_assoc($result_id)
*/
function mssql_fetch_array($result, $result_type = MSSQL_BOTH)
{
$rows = mssql_fetch_assoc($result);
if ($rows) {
switch ($result_type) {
case MSSQL_BOTH:
return array_merge($rows, array_values($rows));
case MSSQL_NUM:
return array_values($rows);
case MSSQL_ASSOC:
return $rows;
}
/** @var $result PDOStatement */
switch ($result_type) {
case MSSQL_NUM:
$pdoFetch = PDO::FETCH_NUM;
break;
case MSSQL_ASSOC:
$pdoFetch = PDO::FETCH_ASSOC;
break;
default:
$pdoFetch = PDO::FETCH_BOTH;
}
return false;

return $result->fetch($pdoFetch) ?: false;
}

/**
Expand All @@ -113,7 +140,22 @@ function mssql_fetch_array($result, $result_type = MSSQL_BOTH)
*/
function mssql_num_rows($result)
{
return odbc_num_rows($result);
global $mmwsql;

/** @var $result PDOStatement */
$count = $result->rowCount();
if ($count === -1) {
//trigger_error('[mssql_num_rows] PDO cant return correct row count');
$result->closeCursor();
$countQuery = preg_replace('/^SELECT(.*?)FROM/is', 'SELECT COUNT(*) FROM', $result->queryString);
if (strpos($countQuery, 'ORDER BY') !== false) {
$countQuery = substr($countQuery, 0, strpos($countQuery, 'ORDER BY'));
}
$count = (int)$mmwsql['connect']->query($countQuery)->fetchColumn();
$result->execute();
}

return $count;
}

/**
Expand All @@ -122,9 +164,11 @@ function mssql_num_rows($result)
*/
function mssql_result($result, $row = 0, $field = 0)
{
/* ODBC result haven't rows params, hack by Vaflan .!.. */
/** @var $result PDOStatement */

/* PDO result haven't rows params, hack by Vaflan .!.. */
$i = 0;
while ($rows = @odbc_fetch_array($result)) {
while ($rows = $result->fetch(PDO::FETCH_BOTH)) {
if ($i == $row) {
foreach ($rows as $k => $v) {
if ($k == $field) {
Expand All @@ -134,6 +178,7 @@ function mssql_result($result, $row = 0, $field = 0)
}
$i++;
}

return false;
}

Expand All @@ -144,7 +189,10 @@ function mssql_result($result, $row = 0, $field = 0)
function mssql_get_last_message()
{
global $mmwsql;
return odbc_errormsg($mmwsql['connect']);

$errorInfo = $mmwsql['connect']->errorInfo();

return $errorInfo[2] ?: reset($errorInfo);
}

/**
Expand All @@ -153,7 +201,8 @@ function mssql_get_last_message()
*/
function mssql_num_fields($result)
{
return odbc_num_fields($result);
/** @var $result PDOStatement */
return $result->columnCount();
}

/**
Expand All @@ -163,7 +212,10 @@ function mssql_num_fields($result)
function mssql_close($link_identifier)
{
global $mmwsql;
odbc_close($mmwsql['connect']);
return $link_identifier === $mmwsql['connect'];

$isCurrentConnection = ($link_identifier === $mmwsql['connect']);
unset($mmwsql['connect']);

return $isCurrentConnection;
}
}
5 changes: 4 additions & 1 deletion includes/theme_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ function theme()
function menu($style = null)
{
global $mmw;
require __DIR__ . '/menu.php';
if (!isset($mmw['menu'])) {
require_once __DIR__ . '/menu.php';
}

if (empty($style)) {
$style = '<a href="$1">$2</a><br>';
}
Expand Down
32 changes: 20 additions & 12 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@
: array('Database does not support MD5!', 'No');
?>
<form name="set_md5" method="post" action="?page=3">
<label>
Change database COLLATE
<select name="collate" title="Set COLLATE" style="margin:0;padding:0;">
<option value="false">No</option>
<option value="true">Yes</option>
</select>
</label><br>
<label>
In "config.php" Now MD5:
<select name="md5" title="Set MD5 Option" style="margin:0;padding:0;">
<option value="true"<?php echo $md5Selector[true]; ?>>Yes</option>
<option value="false"<?php echo $md5Selector[false]; ?>>No</option>
<option value="true"<?php echo $md5Selector[true]; ?>>Yes</option>
</select>
</label><br>
<b style="color:red;"><?php echo $md5Info[0]; ?></b><br>
Expand All @@ -68,15 +75,14 @@
if (($_POST['md5'] === 'true' && !$mmw['md5']) || ($_POST['md5'] === 'false' && $mmw['md5'])) {
$configFile = 'config.php';
$configData = file_get_contents($configFile);
$configData = preg_replace('/\$mmw\[\'md5\'] = (true|false);/', "\$mmw['md5'] = {$_POST['md5']}22;", $configData);
$configData = preg_replace('/\$mmw\[\'md5\'] = (true|false);/', "\$mmw['md5'] = {$_POST['md5']};", $configData);
file_put_contents($configFile, $configData);
$mmw['md5'] = $_POST['md5'];
}
@file_put_contents('includes/installed.php', "<?php\n// MyMuWeb Installed Date\n\$mmw['installed'] = '" . time() . "';\n");
?>
<b>Tables and columns install end!</b> [<a href="#"
onclick="document.getElementById('install_log').style.display=''">Show</a>]
<br>
<b>Tables and columns install end!</b>
[<a href="#" onclick="document.getElementById('install_log').style.display=''">Show</a>]<br>
On the next page you can choose the site administrator<br>
<div class="buttonFlow">
<button onclick="window.location.href='?page=4'">Next &#8594;</button>
Expand All @@ -89,12 +95,14 @@
<?php
$queryList = array();

// DECODE DATABASE
$queryList['decode_database'] = array(
"ALTER DATABASE {$mmw['sql']['database']} SET SINGLE_USER WITH ROLLBACK IMMEDIATE",
"ALTER DATABASE {$mmw['sql']['database']} COLLATE SQL_Latin1_General_CP1_CI_AS",
"ALTER DATABASE {$mmw['sql']['database']} SET MULTI_USER"
);
if ($_POST['collate'] === 'true') {
// DECODE DATABASE
$queryList['decode_database'] = array(
"ALTER DATABASE {$mmw['sql']['database']} SET SINGLE_USER WITH ROLLBACK IMMEDIATE",
"ALTER DATABASE {$mmw['sql']['database']} COLLATE SQL_Latin1_General_CP1_CI_AS",
"ALTER DATABASE {$mmw['sql']['database']} SET MULTI_USER"
);
}

// CREAT TABLES
$queryList['load_db_mmw'] = @file_get_contents('includes/db_mmw.sql');
Expand Down Expand Up @@ -126,7 +134,7 @@
"ALTER TABLE MEMB_INFO ADD mmw_coin int not null default 0",
"ALTER TABLE warehouse ADD extMoney bigint null default 0",
"ALTER TABLE Character ADD {$mmw['reset_column']} int not null default 0",
"ALTER TABLE Character ADD leadership int not null default 0",
"ALTER TABLE Character ADD Leadership int not null default 0",
"ALTER TABLE Guild ADD G_Union int not null default 0",
"ALTER TABLE GuildMember ADD G_Status tinyint not null default 0",
);
Expand Down
2 changes: 1 addition & 1 deletion modules/character.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<?php endif; ?>
<tr>
<td style="text-align:right"><?php echo mmw_lang_status; ?>:</td>
<td><?php echo ctlCode($info[16]); ?></td>
<td><?php echo ctlCode($info[15]); ?></td>
</tr>
<tr>
<td style="text-align:right"><?php echo mmw_lang_class; ?>:</td>
Expand Down
Loading

0 comments on commit b1605f9

Please sign in to comment.