Advertisement:

Author Topic: [MOD][WIP]MemBox  (Read 5494 times)

Offline nend

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,755
  • 2 deep n2 the code
    • sicommnend on GitHub
    • SIComm.us
[MOD][WIP]MemBox
« on: December 31, 2015, 12:49:11 AM »
I am developing this modification because I wanted a chat box for one of my sites without all the db calls which I find useless for something with temporary data.

The system will work sort of like a shoutbox but instead data will be stored in the servers cache system.

Just started developing the backend, you know validation and trying to keep things optimized.

So forth
Code: [Select]
<?php

define
('SMF'1);
require_once(
dirname(__FILE__) . '/Settings.php');
require_once(
$sourcedir '/Load.php');

if (isset(
$_COOKIE[$cookiename])) {
if (preg_match('~^a:[34]:\{i:0;(i:\d{1,6}|s:[1-8]:"\d{1,8}");i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d{1,14};(i:3;i:\d;)?\}$~i'$_COOKIE[$cookiename]) == 1) {
list ($id_member$password) = safe_unserialize($_COOKIE[$cookiename]);
$id_member = !empty($id_member) && strlen($password) > ? (int) $id_member 0;
}
}

if (
$id_member != && ($user_settings cache_get_data('user_settings-'.$id_member.'smf'60)) == null) {
mysql_select_db($db_namemysql_connect($db_server$db_user$db_passwd));
$result mysql_query('
SELECT mem.*, IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type
FROM '
.$db_prefix.'members AS mem
LEFT JOIN '
.$db_prefix.'attachments AS a ON (a.id_member = '.$id_member.')
WHERE mem.id_member = '
.$id_member.'
LIMIT 1'
);
$user_settings mysql_fetch_assoc($result);
mysql_free_result($result);
// This is a pure memory based chat, we want to limit our database connections
// Doing it the SMF way but we are not checking cache level.
cache_put_data('user_settings-'.$id_member.'smf'$user_settings60);
}

// Here will pass $user_settings to a function if it and $id_member is set, if not then just end the script, bout to get wild with the server memory.

function safe_unserialize($data) {
if (preg_match('/(^|;|{|})O:([0-9]|\+|\-)+/'$data) === 0)
return @unserialize($data);
}
?>

What do you think, good/bad idea? Any thoughts and opinions?

Offline JBlaze

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 12,152
    • @fragicide on Twitter
Re: [MOD][WIP]MemBox
« Reply #1 on: December 31, 2015, 08:28:29 AM »
One thing I noticed right away is that you are using mysql_*() functions which are soon to be deprecated in favor of PDO. Also, you don't need to re-declare safe_unserialize() since it is declared in Subs.php, and will be loaded as Load.php is called.

Offline Suki

  • Customizer
  • SMF Super Hero
  • *
  • Posts: 15,084
  • Kaizoku Jotei
    • MissAllSunday on GitHub
    • SMF mods
Re: [MOD][WIP]MemBox
« Reply #2 on: January 02, 2016, 10:03:57 AM »
Just keep in mind that due to the free nature of SMF it gets installed on limited servers with no access to proper cache tools, meaning you will end up using the default file cache system SMF has which will be pretty limited and it gets cumbersome if you try to write/get large amounts of data to/from it.

Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline nend

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 1,755
  • 2 deep n2 the code
    • sicommnend on GitHub
    • SIComm.us
Re: [MOD][WIP]MemBox
« Reply #3 on: February 01, 2016, 10:01:53 PM »
Sorry for the late reply, I think I will not continue development of this modification.

It is more of a hack and will only work on one setup, which would happen to be a single server setup up with memory caching. I am on a multiple server setup, so not all caches have the same data in them so this causes inconsistencies in my situation.

However there is good news, I finished this up somewhat and it is a single file that would just need to be placed in the root folder of a forum. I say somewhat because it isn't polished or had any real in debt testing.

I will share this code with anyone that would like to continue development or may find it useful. I am releasing this code as is, and any express or implied warranties, including but not limited to of merchant ability and fitness for a particular purpose are disclaimed. In no way anyone shall be held responsible for any incidental damages caused by this code. I am adding this disclaimer because this code is as is and not really tested.

Code: [Select]
<?php

$time 
microtime();
$time explode(' '$time);
$time $time[1] + $time[0];
$start $time;

define('SMF'1);
require_once(
dirname(__FILE__) . '/Settings.php');
require_once(
$sourcedir '/Load.php');

if (isset(
$_COOKIE[$cookiename])) {
if (preg_match('~^a:[34]:\{i:0;(i:\d{1,6}|s:[1-8]:"\d{1,8}");i:1;s:(0|40):"([a-fA-F0-9]{40})?";i:2;[id]:\d{1,14};(i:3;i:\d;)?\}$~i'$_COOKIE[$cookiename]) == 1) {
list ($id_member$password) = safe_unserialize($_COOKIE[$cookiename]);
$id_member = !empty($id_member) && strlen($password) > ? (int) $id_member 0;
}
}

if (
$id_member != && ($user_settings cache_get_data('user_settings-'.$id_member.'smf'60)) == null) {
mysql_select_db($db_namemysql_connect($db_server$db_user$db_passwd));
$result mysql_query('
SELECT mem.*, IFNULL(a.id_attach, 0) AS id_attach, a.filename, a.attachment_type
FROM '
.$db_prefix.'members AS mem
LEFT JOIN '
.$db_prefix.'attachments AS a ON (a.id_member = '.$id_member.')
WHERE mem.id_member = '
.$id_member.'
LIMIT 1'
);
$user_settings mysql_fetch_assoc($result);
mysql_free_result($result);
// This is a pure memory based chat, we want to limit our database connections
// Doing it the SMF way but we are not checking cache level.
cache_put_data('user_settings-'.$id_member.'smf'$user_settings60);
}

if (
$id_member != 0) {chIin();} else {echo 'access denied<br />';}

$time microtime();
$time explode(' '$time);
$time $time[1] + $time[0];
$finish $time;
$total_time round(($finish $start), 4);
echo 
'Page generated in '.$total_time.' seconds.';

function 
chIin() {

$action = array(
'get' => 'chatGet',
'send' => 'chatSend',
);

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); 
header("Last-Modified: " gmdate"D, d M Y H:i:s" ) . "GMT" ); 
header("Cache-Control: no-cache, must-revalidate" ); 
header("Pragma: no-cache" );

if(array_key_exists($_REQUEST['act'],$action)) {
header("Content-Type: text/xml; charset=utf-8");
call_user_func($action[$_REQUEST['act']]);
} else {
chatHome();
}
}

function 
chatHome() {

echo '<html>
<head>
<title>Membox</title>
<script language="JavaScript" type="text/javascript">
var timer;
function chatStart() {
document.getElementById(\'message\').focus();
chatGet();
}
function chatGet() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "?act=get", true);
xmlhttp.send();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("chat").innerHTML = xmlhttp.responseText;
document.getElementById("chat").scrollTop = document.getElementById("chat").scrollHeight;
}
};
timer = setTimeout(\'chatGet();\',2000);
}
function chatSend() {
if(document.getElementById(\'message\').value == \'\') {
return;
}
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "?act=send", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var param = \'msg=\' + document.getElementById(\'message\').value;
xmlhttp.send(param);
document.getElementById(\'message\').value = \'\';
clearInterval(timer);
chatGet();
}
function chatSubmit() {
chatSend();
return false;
}
</script>
</head>
<body onLoad="javascript:chatStart();">
<div id="chat" style="height:300px;width:500px;overflow:auto;"></div>
<form onSubmit="return chatSubmit();">
<input type="button" value="Refresh Chat" onClick="javascript:chatGet();" />
<input type="text" id="message" name="message" style="width:500px;" />
<input type="button" value="Send" onClick="javascript:chatSend();" />
</form>
</body>
</html>'
;

}

function 
chatGet() {
if (($data cache_get_data('membox'600)) != null) {
$msgs array_reverse($data['msg']);
foreach ($msgs as &$msg)
echo $msg.'<br />';
} else
echo 'No Chat Data.';
}

function 
chatSend() {

global $user_settings;

if (($data cache_get_data('membox'600)) == null) {
$data = array(
'time_start' => time(),
'msg' => array(),
);
} else {
$data['msg'] = array_slice($data['msg'], 020);
}

if (isset($_REQUEST['msg'])) {
$post str_replace(array("\r\n""\r""\n"), ""$_REQUEST['msg']);
array_unshift($data['msg'], '<b>'.$user_settings['member_name'].'</b> - '.htmlspecialchars(strip_tags($post)));
}
cache_put_data('membox'$data600);
}

function 
safe_unserialize($data) {
if (preg_match('/(^|;|{|})O:([0-9]|\+|\-)+/'$data) === 0)
return @unserialize($data);
}
?>