14
« en: 02 de Diciembre de 2012, 22:02:18 pm »
Igualmente aqui os dejo el resto del codigo ya que el chat me sale sin estar logeado y no se lo que hay mal.
Intento hacerlo todo muy bien, tal y como me han enseñado, algo bastante "avanzado", con rapidez, sin muchos recursos...
functions.php
<?php
if(isset($_COOKIE[COOKIE]) || mysql_num_rows($current_user = mysql_query('SELECT * FROM users WHERE id_extreme = \''.MySQL_Clean($_COOKIE[COOKIE]).'\''))){
$current_user = mysql_fetch_assoc($current_user);
if(mysql_num_rows($ban = mysql_query('SELECT reason FROM bans WHERE user = \''.$current_user['id'].'\''))){
$ban = mysql_fetch_row($ban);
exit('Has sido baneado por '.htmlentities($ban[0]));
}
}else{
unset($current_user);
}
function isLogged(){
global $current_user;
return (count($current_user) > 1 ? true: false);
}
function MySQL_Clean($string){
if(get_magic_quotes_gpc()){
$string = stripslashes($string);
}
return mysql_real_escape_string($string);
}
function BBCode($string){
global $current_user;
$string = nl2br($string);
$string = htmlentities($string);
$string = str_replace('[tu]', $current_user['user'], $string);
$string = preg_replace('/(http:\/\/|www\.|http:\/\/www\.)([a-z0-9\-_\.\/^"]+)/si', '<a target="_blank" href="\\1\\2">\\1\\2</a>', $string); // URL
$string = preg_replace('/\[b\](.+)\[\/b\]/Usi', '<b>\\1</b>', $string); // Negrita
$string = preg_replace('/\[i\](.+)\[\/i\]/Usi', '<i>\\1</i>', $string); // Cursiva
$string = preg_replace('/\[u\](.+)\[\/u\]/Usi', '<u>\\1</u>', $string); // Subrayada
$censored = array('*****s', '*****s', 'mierda');
$string = str_ireplace($censored, '****', $string); // La I de Ireplace es para no distinguir entre minúsculas y mayúsculas.
return $string;
}
function isAllowedTo($permissions){
global $current_user;
$query = mysql_fetch_row(mysql_query('SELECT permissions FROM ranks WHERE id = \''.$current_user['rank'].'\''));
$query = explode(',', $query[0]);
return (in_array($permissions, $query) ? true : false);
}
?>
index.php
<?php
include('mysql.php');
include('functions.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="iso-8859-1">
<title>Documento sin título</title>
</head>
<style type="text/css">
body{
font-family: Helvetica, Arial, Sans-serif;
padding: 10px;
}
/* fix ie */
img{ border: none; }
/* estilos generales */
.center{ text-align: center; }
.floatL{ float: left; }
.floatR{ float: right; }
.clearBoth{ clear: both; }
.h2{ font-size: 18pt; }
.admin_note{
padding: 0.2em;
border: 2px solid red;
background-color: #ffc;
color: red;
display: block;
margin: 5px auto;
text-align: center;
max-width: 203px;
font-size: 0.8em;
}
span.h2 > span{ font-size: 15pt; }
a, a:visited, a:active, a:hover{ color: blue; text-decoration: none; }
#ao1, #ao2, #ao3, #ao4{ display: none; }
.delete{
width: 16px;
height: 16px;
background: transparent url(delete.png) no-repeat;
display: inline-block;
cursor: pointer;
}
.message{
border: 1px solid rgb(255, 201, 14);
border-left: none;
border-right: none;
background-color: rgb(255, 236, 170);
padding: 10px;
text-align: center;
display: block;
width: 100%;
margin: auto 10px;
}
.message a{
text-decoration: none;
}
#nmsg > input[type="text"]{
width: 130px;
_width: 110px;
border: 1px solid;
border-radius: 5px;
outline: none;
text-indent: 5px;
}
.refresh{
background: transparent url(images/refresh.png) no-repeat;
opacity: 0.5;
filter: alpha(opacity=50);
width: 16px;
height: 16px;
cursor: pointer;
font-size: 0;
text-indent: 20px;
}
.refresh:hover{
opacity: 1;
filter: alpha(opacity=100);
font-size:11px;
}
.register_form{
margin-top: 50px;
}
.cont{
width: 205px;
_width: 235px;
border: 1px solid;
border-radius: 7px;
}
.box_title{
height: 26px;
line-height: 26px;
border-radius: 7px 7px 0 0;
width: 205px;
background-color: rgb(221, 171, 0);
text-align: center;
color: #fff;
font-size: 12px;
}
.box_submenu{
height: 18px;
line-height: 18px;
font-size: 12px;
background-color: #000;
color: #fff;
text-indent: 10px;
cursor: pointer;
-webkit-user-select: none;
}
.box_body{
width: 205px;
overflow-x: auto;
word-wrap: break-word;
height: auto;
max-height: 300px;
font-size: 12px;
}
.box_send{
background-color: rgb(183, 183, 183);
height: 37px;
padding-top: 10px;
border-top: 1px solid;
text-align: center;
}
.box_footer, .box_footer a{
height: 16px;
line-height: 16px;
border-radius: 0 0 7px 7px;
border-top: 1px solid #000;
width: 205px;
background-color: rgb(221, 171, 0);
text-align: center;
color: #fff;
font-size: 12px;
}
</style>
<body>
<?php
if(!isLogged()){
if(!isset($_POST['usuario'])){
?>
hola
<?php
}else{
foreach($_POST as $field => $value){
$value = trim($value);
if(empty($value)){
exit('Falta el campo '.$field);
}
if(strlen($_POST['user']) > 32 || strlen($_POST['user']) < 6 || !preg_match('/[a-z0-9_]/i', $_POST['user'])){
exit('El nombre no es válido, es demasiado largo.');
}
if(mysql_num_rows(mysql_query('SELECT id FROM users WHERE user = \''.MySQL_Clean($_POST['user']).'\''))){
exit('El nombre de usuario ya esta registrado.');
}
if(strlen($_POST['password']) < 6){
exit('La contraseña es demasiado corta.');
}
if(!preg_match('/^[a-z0-9_\.\-]{1,64}@[a-z0-9_\.\-]{1,255}\.([a-z]{2,3})+$/i', $_POST['email'])){
exit('El email no es válido.');
}
if(mysql_num_rows(mysql_query('SELECT id FROM users WHERE email = \''.MySQL_Clean($_POST['email']).'\''))){
exit('El email ya esta registrado.');
}
$id_extreme = md5(uniqid(mt_rand(), true)); // id_extreme
$date = date('d/m/Y');
mysql_query('INSERT INTO users (id_extreme, ip_register, ip_actual, user, password, email, rank, register) VALUES (\''.$id_extreme.'\', \''.$_SERVER['REMOTE_ADDR'].'\', \''.$_SERVER['REMOTE_ADDR'].'\', \''.MySQL_Clean($_POST['user']).'\', \''.md5($_POST['password']).'\', \''.MySQL_Clean($_POST['email']).'\', \'1\', \''.$_POST['date'].'\')') or exit(mysql_error());
exit('<script>$.get("ajax/login.php", "user='.$_POST['user'].'&password='.$_POST['password'].'");</script>
Has sido registrado correctamente.
<meta http-equiv="refresh" content="2">');
}
}
?>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<div class="float_left">
</div>
<div class="floatL">
<div class="cont">
<div class="box_title" style="cursor: pointer;" onclick="$('#mc').slideToggle()">Mensajes</div>
<div id="mc">
<div class="box_submenu" onclick="$('#bb').slideToggle();aquo(this)">« Ver mensajes</div>
<div class="box_body" id="bb"></div>
<div class="box_submenu" onclick="$('#nmsg').slideToggle();aquo(this)">« Enviar mensaje</div>
<div class="box_send" id="nmsg">
<input type="text" id="newm" /><input type="submit" id="sendmsg" value="Enviar">
</div>
</div>
<div class="box_footer"><?=(isAllowedTo('delmsg') ? '<a href="#" onclick="deleteall()">Borrar todos los mensajes</a>' : '')?></div>
</div>
» <a href="#" onclick="$.cookie('<?=COOKIE;?>', null);location.reload();return;">Salir</a>
</div>
<br /><br />IE8+ Compatible
</div>
<div class="clearBoth"></div>
<script type="text/javascript">
$('#sendmsg').bind('click', function(){ //enviar mensaje
if(!$('#newm').val()){ return false; }
$.get('ajax/new-message.php', 'msg=' + $('#newm').val(), function(a){ if(a.charAt(0) == '0'){ alert(a.substring(1));return false; }$('#newm').val('').focus(); });
});
$('#newm').bind('keypress', function(event){ if(event.keyCode == 13){ $('#sendmsg').click(); } }); //enter
$('#button_ban').bind('click', function(){ //banear
if(!$('#banuser').val()){ alert('Selecciona un usuario');return; }
var reason = prompt('Razon de suspension:');
if(reason == '' || !reason){ return; }
$.get('ajax/action-user.php', 'sa=ban&val=' + $('#banuser').val() + '&reason=' + reason, function(a){
alert('El usuario ' + (a.charAt(0) == 1 ? 'ha sido baneado' : 'no ha podido ser baneado: ' + a.substring(1)));
});
});
$('#button_desban').bind('click', function(){
if(!$('#desbanuser').val()){ alert('Selecciona un usuario');return false; }
$.get('ajax/action-user.php', 'sa=desban&val=' + parseInt($('#banuser').val()), function(a){
alert('El usuario ' + (a.charAt(0) == 1 ? 'ha sido reactivado' : 'no ha podido ser reactivado: ' + a.substring(1)));
});
});
$('#change_user_rank').bind('click', function(){
if(!$('#cr').val() || !$('#nr').val()){ alert('Selecciona un usuario y su nuevo rango');return false; }
$.get('ajax/action-user.php', 'sa=rank&val=' + $('#cr').val() + '&nr=' + parseInt($('#nr').val()), function(a){
alert('Rango ' + (a.charAt(0) == 1 ? 'cambiado' : 'no cambiado: ' + a.substring(1)));
});
});
$('#du').bind('keypress', function(event){
if(event.keyCode == 13){ $('#bdu').click(); }
});
function update_msg(){
$.get('ajax/last-messages.php', 'rnd=' + Math.random() * 99999, function(a){ $('#bb').html(a); });
}
function del_msg(id){
id = parseInt(id);
$.get('ajax/delete-message.php', 'id=' + id, function(a){ if(a.charAt(0) == '0'){ alert(a.substring(1)); } });
}
function deleteall(){
$.get('ajax/delete-message.php', function(a){ if(a.charAt(0) == '0'){ alert(a.substring(1)); } });
}
function del_user(who){
if(!confirm('Si borras al usuario, borras TODOS sus datos de la BD!')){ return false; }
$.get('ajax/action-user.php', 'sa=delete&val=' + who, function(a){
if(a == 1){ alert('Usuario borrado'); }else{ alert(a.substring(1)); }
});
}
function aquo(o){
o.innerHTML = (o.innerHTML.charAt(0) == '»' ? '«' : '»') + o.innerHTML.substring(1);
}
setInterval('update_msg()', 1000);
</script>
<?php } ?>
</body>
</html>
mysql.php
<?php
$MySQL = array(
'db_host' => 'localhost',
'db_username' => 'root',
'db_name' => 'chat',
'db_password' => '',
'page_title' => 'Chat',
'cookie_url' => '/chat',
'anti_flood' => 3,
'maintenance' => false
);
define('COOKIE', 'chat');
$MySQL_Connect = mysql_connect($MySQL['db_host'], $MySQL['db_username'], $MySQL['db_password']) or exit(mysql_error());
mysql_select_db($MySQL['db_name'], $MySQL_Connect) or exit(mysql_error());
?>
Un saludo.
Agradezco vuestra ayuda, gracias.