Funzione php non viene richiamata!

hackersatifal

Utente Attivo
9 Ago 2012
238
0
0
Salve a tutti,
ho uno script nel quale ho inserito una funzione php str_replace che viene richiamata, ma quando vado a vedere se alla variabile viene applicata mi spunta: ".(qui il testo che voglio rimpiazzare).". La funzione serve per la gestione delle emoticon e per la sostituzione dell'emoticon scritta con la rispettiva immagine, ma questo non avviene. Ecco lo script:
PHP:
<?php

define ('DBPATH','localhost');
define ('DBUSER','****');
define ('DBPASS','*******');
define ('DBNAME','miodatabase');

session_start();

global $dbh;
$dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
mysql_selectdb(DBNAME,$dbh);

if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } 
if ($_GET['action'] == "sendchat") { sendChat(); } 
if ($_GET['action'] == "closechat") { closeChat(); } 
if ($_GET['action'] == "startchatsession") { startChatSession(); } 

if (!isset($_SESSION['chatHistory'])) {
	$_SESSION['chatHistory'] = array();	
}

if (!isset($_SESSION['openChatBoxes'])) {
	$_SESSION['openChatBoxes'] = array();	
}
function faccine($items){
  $items_ok = str_replace(":)", "<img src='smile.gif'>", $items);
  $items_ok = str_replace(":-)", "<img src='smile.gif'>", $items_ok);
  $items_ok = str_replace(":(", "<img src=\"triste.gif\">", $items_ok);
  $items_ok = str_replace(":-(", "<img src=\"triste.gif\">", $items_ok);
  $items_ok = str_replace(":P", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":p", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":-p", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":-P", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":D", "<img src=\"grandesmile.gif\">", $items_ok);
  $items_ok = str_replace(":-D", "<img src=\"grandesmile.gif\">", $items_ok);
  $items_ok = str_replace(":o", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":O", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":-o", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":-O", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(";)", "<img src=\"occhiolino.gif\">", $items_ok);
  $items_ok = str_replace(";-)", "<img src=\"occhiolino.gif\">", $items_ok);
  $items_ok = str_replace(":v", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":V", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":-v", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":-V", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(">:( ", "<img src=\"occhistrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-(", "<img src=\"occhistrizzati.gif\">", $items_ok);
  $items_ok = str_replace(":/", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":-/", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":'(", "<img src=\"lacrima.gif\">", $items_ok);
  $items_ok= str_replace(":'-(", "<img src=\"lacrima.gif\">", $items_ok);
  $items_ok = str_replace("8)", "<img src=\"occhialichiari.gif\">", $items_ok);
  $items_ok = str_replace("8-)", "<img src=\"occhialichiari.gif\">", $items_ok);
  $items_ok = str_replace("B|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("B-|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("b|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("b-|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("<3", "<img src=\"cuore.gif\">", $items_ok);
  $items_ok = str_replace("3:)", "<img src=\"diavoletto.gif\">", $items_ok);
  $items_ok = str_replace("3:-)", "<img src=\"diavoletto.gif\">", $items_ok);
  $items_ok = str_replace("o:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("O:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("0:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("o:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("O:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("0:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("o.O", "<img src=\"occhiopiùgrande.gif\">", $items_ok);
  $items_ok = str_replace(">:o", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:O", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-o", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-O", "<img src=\"felicissimostrizzati.gfi\">", $items_ok);
  $items_ok = str_replace(":3", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":-3", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace("*_*", "<img src=\"indifferente.gif\">", $items_ok);
  $items_ok = str_replace("*-*", "<img src=\"indifferente.gif\">", $items_ok);
  $items_ok = str_replace("-_-", "<img src=\"indifferente.gif\">", $items_ok);
  
  return $items_ok;
}
function chatHeartbeat() {
	
	$sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' AND recd = 0) order by id ASC";
	$query = mysql_query($sql);
	$items = '';

	$chatBoxes = array();

	while ($chat = mysql_fetch_array($query)) {

		if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
			$items = $_SESSION['chatHistory'][$chat['from']];
		}

		$chat['message'] = sanitize($chat['message']);

		$items .= <<<EOD
					   {
			"s": "0",
			"f": "{$chat['from']}",
			"m": "{$chat['message']}"
	   },
EOD;

	if (!isset($_SESSION['chatHistory'][$chat['from']])) {
		$_SESSION['chatHistory'][$chat['from']] = '';
	}

	$_SESSION['chatHistory'][$chat['from']] .= <<<EOD
						   {
			"s": "0",
			"f": "{$chat['from']}",
			"m": "{$chat['message']}"
	   },
EOD;
		
		unset($_SESSION['tsChatBoxes'][$chat['from']]);
		$_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
	}

	if (!empty($_SESSION['openChatBoxes'])) {
	foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
		if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
			$now = time()-strtotime($time);
			$time = date('g:iA M dS', strtotime($time));

			$message = "Inviato: $time";
			if ($now > 180) {
				$items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

	if (!isset($_SESSION['chatHistory'][$chatbox])) {
		$_SESSION['chatHistory'][$chatbox] = '';
	}

	$_SESSION['chatHistory'][$chatbox] .= <<<EOD
		{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
			$_SESSION['tsChatBoxes'][$chatbox] = 1;
		}
		}
	}
}

	$sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' and recd = 0";
	$query = mysql_query($sql);

	if ($items != '') {
		$items = substr($items, 0, -1);
	}
header('Content-type: application/json');
?>
{
		"items": [
			<?php echo '".faccine($items)."';?>
        ]
}

<?php
			exit(0);
}

function chatBoxSession($chatbox) {
	
	$items = '';
	
	if (isset($_SESSION['chatHistory'][$chatbox])) {
		$items = $_SESSION['chatHistory'][$chatbox];
	}

	return 'faccine.($items).';
}

function startChatSession() {
	$items = '';
	if (!empty($_SESSION['openChatBoxes'])) {
		foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
			$items .= chatBoxSession($chatbox);
		}
	}


	if ($items != '') {
		$items = substr($items, 0, -1);
	}

header('Content-type: application/json');
?>
{
		"username": "<?php echo $_SESSION['utente'];?>",
		"items": [
			<?php echo '".faccine($items)."';?>
        ]
}

<?php


	exit(0);
}

function sendChat() {
	$from = $_SESSION['utente'];
	$to = $_POST['to'];
	$message = $_POST['message'];

	$_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
	
	$messagesan = sanitize($message);

	if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
		$_SESSION['chatHistory'][$_POST['to']] = '';
	}

	$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
					   {
			"s": "1",
			"f": "{$to}",
			"m": "{$messagesan}"
	   },
EOD;


	unset($_SESSION['tsChatBoxes'][$_POST['to']]);

	$sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
	$query = mysql_query($sql);
	echo "1";
	exit(0);
}

function closeChat() {

	unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
	
	echo "1";
	exit(0);
}

function sanitize($text) {
	$text = htmlspecialchars($text, ENT_QUOTES);
	$text = str_replace("\n\r","\n",$text);
	$text = str_replace("\r\n","\n",$text);
	$text = str_replace("\n","<br>",$text);
	return $text;
}
?>
Grazie a tutti in anticipo per il vostro aiuto,
hackersatifal.
 

alessandro1997

Utente Attivo
6 Ott 2009
5.302
1
0
23
Roma
alessandro1997.netsons.org
Prova se questo funziona. Ho fatto diverse modifiche:
PHP:
<?php
define('DB_HOST', 'localhost');
define('DB_USER', null);
define('DB_PASS', null);
define('DB_NAME', null);

session_start();

$dbh = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME, $dbh);

switch ($_GET['action']) {
    case 'chatheartbeat':
        chatHeartbeat();
    break;

    case 'sendchat':
        sendChat();
    break;

    case 'closechat':
        closeChat();
    break;

    case 'startchatsession':
        startChatSession();
    break;
}

if (!isset($_SESSION['chatHistory'])) {
    $_SESSION['chatHistory'] = array();
}

if (!isset($_SESSION['openChatBoxes'])) {
    $_SESSION['openChatBoxes'] = array();
}

function emoticons($text)
{
    $conversions = array(
        'smile'                => array(':)', ':-)'),
        'triste'               => array(':(', ':-('),
        'linguaccia'           => array(':P', ':p', ':-P', ':-p'),
        'grandesmile'          => array(':D', ':-D'),
        'stupito'              => array(':o', ':0', ':-o', ':-0'),
        'occhiolino'           => array(';)', ';-)'),
        'pacman'               => array(':v', ':V', ':-v', ':-V'),
        'occhistrizzati'       => array('<:(', '<:-('),
        'perplesso'            => array(':/', ':-/'),
        'lacrima'              => array(':\'(', ':\'-('),
        'occhialichiari'       => array('8)', '8-)'),
        'occhialiscuri'        => array('B|', 'B-|', 'b|', 'b-|'),
        'cuore'                => array('<3'),
        'diavoletto'           => array('3:)', '3:-)'),
        'angioletto'           => array('o:)', 'O:)', '0:)', 'o:-)', 'O:-)', '0:-)'),
        'occhiopiùgrande'      => array('o.O'),
        'felicissimostrizzati' => array('>:o', '>:O', '>:-o', '>:-O'),
        'perplesso'            => array(':3', ':-3'),
        'indifferente'         => array('*_*', '*-*', '-_-'),
    );

    foreach ($conversions as $image => $smiles) {
        $text = str_replace($smiles, "<img src=\"{$image}.gif\" />", $text);
    }

    return $text;
}

function chatHeartbeat()
{
    $sql = 'SELECT * FROM chat WHERE to = \'%s\' AND recd = 0 ORDER BY id ASC';
    $sql = sprintf($sql, mysql_real_escape_string($_SESSION['utente']));

    $query = mysql_query($sql);

    $chatBoxes = array();

    while ($chat = mysql_fetch_assoc($query)) {
        if (!isset($_SESSION['openChatboxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
            $items = $_SESSION['chatHistory'][$chat['from']];
        }

        $chat['message'] = sanitize($chat['message']);

        $item .= json_encode(array(
            's' => 0,
            'f' => $chat['from'],
            'm' => $chat['message'],
        )) . ',';

        $items .= $item;

        if (!isset($_SESSION['chatHistory'][$chat['from']])) {
            $_SESSION['chatHistory'][$chat['from']] = '';
        }

        $_SESSION['chatHistory'][$chat['from']] .= $item;

        unset($_SESSION['tsChatBoxes'][$chat['from']]);
        $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
    }

    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
            if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
                $now = time() - strtotime($time);
                $time = date('g:iA M dS', strtotime($time));

                $message = "Inviato: {$time}";

                if ($now > 180) {
                    $item = json_encode(array(
                        's' => 2,
                        'f' => $chatbox,
                        'm' => $message,
                    )) . ',';

                    $items .= $item;

                    if (!isset($_SESSION['chatHistory'][$chatbox])) {
                        $_SESSION['chatHistory'][$chatbox] = '';
                    }

                    $_SESSION['chatHistory'][$chatbox] .= $item;

                    $_SESSION['tsChatBoxes'][$chatbox] = 1;
                }
            }
        }
    }

    $sql = 'UPDATE chat SET recd = 1 WHERE to = \'%s\' AND recd = 0';
    $sql = sprintf($sql, mysql_real_escape_string($_SESSION['utente']));

    $query = mysql_query($sql);

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

    header('Content-type: application/json');

    $items = emoticons($items);

    exit("{'items':[{$items}]}");
}

function chatBoxSession($chatbox)
{
    $items = '';

    if (isset($_SESSION['chatHistory'][$chatbox])) {
        $items = $_SESSION['chatHistory'][$chatbox];
    }

    return faccine($items);
}

function startChatSession()
{
    $items = '';

    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
            $items .= chatBoxSession($chatbox);
        }
    }

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

    header('Content-type: application/json');

    $items = emoticons($items);

    die("{'username':'{$_SESSION['utente']}', 'items':{$items}}");
}

function sendChat()
{
    $from    = $_SESSION['utente'];
    $to      = $_POST['to'];
    $message = $_POST['message'];

    $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());

    $messagesan = sanitize($message);

    if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
        $_SESSION['chatHistory'][$_POST['to']] = '';
    }

    $_SESSION['chatHistory'][$_POST['to']] .= json_encode(array(
        's' => 1,
        'f' => $to,
        'm' => $messagesan,
    )) . ',';

    unset($_SESSION['tsChatBoxes'][$_POST['to']]);

    $sql = 'INSERT INTO chat (from, to, message, sent) VALUES (\'%s\', \'%s\', \'%s\', NOW())';
    $sql = sprintf($sql, mysql_real_escape_string($from), mysql_real_escape_string($to), mysql_real_escape_string($message));

    $query = mysql_query($sql);

    exit('1');
}

function closeChat()
{
    unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);

    exit('1');
}

function sanitize($text)
{
    $text = htmlspecialchars($text, ENT_QUOTES);

    $text = str_replace(array("\r\n", "\n\r"), "\n", $text);
    $text = nl2br($text);

    return $text;
}
?>
 
Ultima modifica:

hackersatifal

Utente Attivo
9 Ago 2012
238
0
0
Ho rivisto il tuo codice e dovrebbe funzionare quindi, secondo te, quale potrebbe essere il problema?
 

hackersatifal

Utente Attivo
9 Ago 2012
238
0
0
Non è che tramite Javascript viene applicato qualche filtro? Se la chat funziona con Ajax, usa un plugin del browser (Firebug su Firefox) per vedere il risultato della richiesta. Se lì l'emoticon compare sotto forma di codice HTML, allora il filtro viene applicato dal client.
Ho ispezionato con Firebug e mi spunta questo codice html
HTML:
<span class="chatboxmessagecontent">".(:))."</span>
quindi non spunta l'img nemmeno nel codice html.
 

alessandro1997

Utente Attivo
6 Ott 2009
5.302
1
0
23
Roma
alessandro1997.netsons.org
Ho fatto qualche leggera modifica:
PHP:
 <?php
define('DB_HOST', 'localhost');
define('DB_USER', null);
define('DB_PASS', null);
define('DB_NAME', null);

session_start();

$dbh = mysql_connect(DB_HOST, DB_USER, DB_PASS);
mysql_select_db(DB_NAME, $dbh);

switch ($_GET['action']) {
    case 'chatheartbeat':
        chatHeartbeat();
    break;

    case 'sendchat':
        sendChat();
    break;

    case 'closechat':
        closeChat();
    break;

    case 'startchatsession':
        startChatSession();
    break;
}

if (!isset($_SESSION['chatHistory'])) {
    $_SESSION['chatHistory'] = array();
}

if (!isset($_SESSION['openChatBoxes'])) {
    $_SESSION['openChatBoxes'] = array();
}

function emoticons($text)
{
    $conversions = array(
        'smile'                => array(':)', ':-)'),
        'triste'               => array(':(', ':-('),
        'linguaccia'           => array(':P', ':p', ':-P', ':-p'),
        'grandesmile'          => array(':D', ':-D'),
        'stupito'              => array(':o', ':0', ':-o', ':-0'),
        'occhiolino'           => array(';)', ';-)'),
        'pacman'               => array(':v', ':V', ':-v', ':-V'),
        'occhistrizzati'       => array('<:(', '<:-('),
        'perplesso'            => array(':/', ':-/'),
        'lacrima'              => array(':\'(', ':\'-('),
        'occhialichiari'       => array('8)', '8-)'),
        'occhialiscuri'        => array('B|', 'B-|', 'b|', 'b-|'),
        'cuore'                => array('<3'),
        'diavoletto'           => array('3:)', '3:-)'),
        'angioletto'           => array('o:)', 'O:)', '0:)', 'o:-)', 'O:-)', '0:-)'),
        'occhiopiùgrande'      => array('o.O'),
        'felicissimostrizzati' => array('>:o', '>:O', '>:-o', '>:-O'),
        'perplesso'            => array(':3', ':-3'),
        'indifferente'         => array('*_*', '*-*', '-_-'),
    );

    foreach ($conversions as $image => $smiles) {
        $text = str_replace($smiles, "<img src=\"{$image}.gif\" />", $text);
    }

    return $text;
}

function chatHeartbeat()
{
    $sql = 'SELECT * FROM chat WHERE to = \'%s\' AND recd = 0 ORDER BY id ASC';
    $sql = sprintf($sql, mysql_real_escape_string($_SESSION['utente']));

    $query = mysql_query($sql);

    $chatBoxes = array();

    while ($chat = mysql_fetch_assoc($query)) {
        if (!isset($_SESSION['openChatboxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
            $items = $_SESSION['chatHistory'][$chat['from']];
        }

        $chat['message'] = sanitize($chat['message']);

        $item .= json_encode(array(
            's' => 0,
            'f' => $chat['from'],
            'm' => $chat['message'],
        )) . ',';

        $items .= $item;

        if (!isset($_SESSION['chatHistory'][$chat['from']])) {
            $_SESSION['chatHistory'][$chat['from']] = '';
        }

        $_SESSION['chatHistory'][$chat['from']] .= $item;

        unset($_SESSION['tsChatBoxes'][$chat['from']]);
        $_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
    }

    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
            if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
                $now = time() - strtotime($time);
                $time = date('g:iA M dS', strtotime($time));

                $message = "Inviato: {$time}";

                if ($now > 180) {
                    $item = json_encode(array(
                        's' => 2,
                        'f' => $chatbox,
                        'm' => emoticons($message),
                    )) . ',';

                    $items .= $item;

                    if (!isset($_SESSION['chatHistory'][$chatbox])) {
                        $_SESSION['chatHistory'][$chatbox] = '';
                    }

                    $_SESSION['chatHistory'][$chatbox] .= $item;

                    $_SESSION['tsChatBoxes'][$chatbox] = 1;
                }
            }
        }
    }

    $sql = 'UPDATE chat SET recd = 1 WHERE to = \'%s\' AND recd = 0';
    $sql = sprintf($sql, mysql_real_escape_string($_SESSION['utente']));

    $query = mysql_query($sql);

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

    header('Content-type: application/json');

    exit("{'items':[{$items}]}");
}

function chatBoxSession($chatbox)
{
    $items = '';

    if (isset($_SESSION['chatHistory'][$chatbox])) {
        $items = $_SESSION['chatHistory'][$chatbox];
    }

    return faccine($items);
}

function startChatSession()
{
    $items = '';

    if (!empty($_SESSION['openChatBoxes'])) {
        foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
            $items .= chatBoxSession($chatbox);
        }
    }

    if ($items != '') {
        $items = substr($items, 0, -1);
    }

    header('Content-type: application/json');

    die("{'username':'{$_SESSION['utente']}', 'items':[{$items}]}");
}

function sendChat()
{
    $from    = $_SESSION['utente'];
    $to      = $_POST['to'];
    $message = $_POST['message'];

    $_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());

    $messagesan = sanitize($message);

    if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
        $_SESSION['chatHistory'][$_POST['to']] = '';
    }

    $_SESSION['chatHistory'][$_POST['to']] .= json_encode(array(
        's' => 1,
        'f' => $to,
        'm' => $messagesan,
    )) . ',';

    unset($_SESSION['tsChatBoxes'][$_POST['to']]);

    $sql = 'INSERT INTO chat (from, to, message, sent) VALUES (\'%s\', \'%s\', \'%s\', NOW())';
    $sql = sprintf($sql, mysql_real_escape_string($from), mysql_real_escape_string($to), mysql_real_escape_string($message));

    $query = mysql_query($sql);

    exit('1');
}

function closeChat()
{
    unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);

    exit('1');
}

function sanitize($text)
{
    $text = htmlspecialchars($text, ENT_QUOTES);

    $text = str_replace(array("\r\n", "\n\r"), "\n", $text);
    $text = nl2br($text);

    return $text;
}
?>
 

hackersatifal

Utente Attivo
9 Ago 2012
238
0
0
Ho provato il tuo codice, ma non mi funziona bene e non visualizza le emoticon, quindi ho modificato un file javascript e questo php e ora non me lo sostituisce, ma mi dà :) quindi senza ".()."
Secondo te dove può essere il problema?
Ecco il mio codice php:
PHP:
<?php

define ('DBPATH','localhost');
define ('DBUSER','****');
define ('DBPASS','*********');
define ('DBNAME','miodatabase');

session_start();

global $dbh;
$dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
mysql_selectdb(DBNAME,$dbh);

if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } 
if ($_GET['action'] == "sendchat") { sendChat(); } 
if ($_GET['action'] == "closechat") { closeChat(); } 
if ($_GET['action'] == "startchatsession") { startChatSession(); } 

if (!isset($_SESSION['chatHistory'])) {
	$_SESSION['chatHistory'] = array();	
}

if (!isset($_SESSION['openChatBoxes'])) {
	$_SESSION['openChatBoxes'] = array();	
}
function faccine($items){
  $items_ok = str_replace(":)", "<img src='smile.gif'>", $items);
  $items_ok = str_replace(":-)", "<img src='smile.gif'>", $items_ok);
  $items_ok = str_replace(":(", "<img src=\"triste.gif\">", $items_ok);
  $items_ok = str_replace(":-(", "<img src=\"triste.gif\">", $items_ok);
  $items_ok = str_replace(":P", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":p", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":-p", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":-P", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":D", "<img src=\"grandesmile.gif\">", $items_ok);
  $items_ok = str_replace(":-D", "<img src=\"grandesmile.gif\">", $items_ok);
  $items_ok = str_replace(":o", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":O", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":-o", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":-O", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(";)", "<img src=\"occhiolino.gif\">", $items_ok);
  $items_ok = str_replace(";-)", "<img src=\"occhiolino.gif\">", $items_ok);
  $items_ok = str_replace(":v", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":V", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":-v", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":-V", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(">:( ", "<img src=\"occhistrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-(", "<img src=\"occhistrizzati.gif\">", $items_ok);
  $items_ok = str_replace(":/", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":-/", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":'(", "<img src=\"lacrima.gif\">", $items_ok);
  $items_ok= str_replace(":'-(", "<img src=\"lacrima.gif\">", $items_ok);
  $items_ok = str_replace("8)", "<img src=\"occhialichiari.gif\">", $items_ok);
  $items_ok = str_replace("8-)", "<img src=\"occhialichiari.gif\">", $items_ok);
  $items_ok = str_replace("B|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("B-|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("b|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("b-|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("<3", "<img src=\"cuore.gif\">", $items_ok);
  $items_ok = str_replace("3:)", "<img src=\"diavoletto.gif\">", $items_ok);
  $items_ok = str_replace("3:-)", "<img src=\"diavoletto.gif\">", $items_ok);
  $items_ok = str_replace("o:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("O:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("0:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("o:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("O:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("0:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("o.O", "<img src=\"occhiopiùgrande.gif\">", $items_ok);
  $items_ok = str_replace(">:o", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:O", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-o", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-O", "<img src=\"felicissimostrizzati.gfi\">", $items_ok);
  $items_ok = str_replace(":3", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":-3", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace("*_*", "<img src=\"indifferente.gif\">", $items_ok);
  $items_ok = str_replace("*-*", "<img src=\"indifferente.gif\">", $items_ok);
  $items_ok = str_replace("-_-", "<img src=\"indifferente.gif\">", $items_ok);
  
  return $items_ok;
}
function chatHeartbeat() {
	
	$sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' AND recd = 0) order by id ASC";
	$query = mysql_query($sql);
	$items = '';

	$chatBoxes = array();

	while ($chat = mysql_fetch_array($query)) {

		if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
			$items = $_SESSION['chatHistory'][$chat['from']];
		}

		$chat['message'] = sanitize($chat['message']);

		$items .= <<<EOD
					   {
			"s": "0",
			"f": "{$chat['from']}",
			"m": "{$chat['message']}"
	   },
EOD;

	if (!isset($_SESSION['chatHistory'][$chat['from']])) {
		$_SESSION['chatHistory'][$chat['from']] = '';
	}

	$_SESSION['chatHistory'][$chat['from']] .= <<<EOD
						   {
			"s": "0",
			"f": "{$chat['from']}",
			"m": "{$chat['message']}"
	   },
EOD;
		
		unset($_SESSION['tsChatBoxes'][$chat['from']]);
		$_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
	}

	if (!empty($_SESSION['openChatBoxes'])) {
	foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
		if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
			$now = time()-strtotime($time);
			$time = date('g:iA M dS', strtotime($time));

			$message = "Inviato: $time";
			if ($now > 180) {
				$items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

	if (!isset($_SESSION['chatHistory'][$chatbox])) {
		$_SESSION['chatHistory'][$chatbox] = '';
	}

	$_SESSION['chatHistory'][$chatbox] .= <<<EOD
		{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
			$_SESSION['tsChatBoxes'][$chatbox] = 1;
		}
		}
	}
}

	$sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' and recd = 0";
	$query = mysql_query($sql);

	if ($items != '') {
		$items = substr($items, 0, -1);
	}
header('Content-type: application/json');
?>
{
		"items": [
			<?php echo ".faccine($items).";?>
        ]
}

<?php
			exit(0);
}

function chatBoxSession($chatbox) {
	
	$items = '';
	
	if (isset($_SESSION['chatHistory'][$chatbox])) {
		$items = $_SESSION['chatHistory'][$chatbox];
	}

	return "faccine.($items).";
}

function startChatSession() {
	$items = '';
	if (!empty($_SESSION['openChatBoxes'])) {
		foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
			$items .= chatBoxSession($chatbox);
		}
	}


	if ($items != '') {
		$items = substr($items, 0, -1);
	}

header('Content-type: application/json');
?>
{
		"username": "<?php echo $_SESSION['utente'];?>",
		"items": [
			<?php echo ".faccine($items).";?>
        ]
}

<?php


	exit(0);
}

function sendChat() {
	$from = $_SESSION['utente'];
	$to = $_POST['to'];
	$message = $_POST['message'];

	$_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
	
	$messagesan = sanitize($message);

	if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
		$_SESSION['chatHistory'][$_POST['to']] = '';
	}

	$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
					   {
			"s": "1",
			"f": "{$to}",
			"m": "{$messagesan}"
	   },
EOD;


	unset($_SESSION['tsChatBoxes'][$_POST['to']]);

	$sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
	$query = mysql_query($sql);
	echo "1";
	exit(0);
}

function closeChat() {

	unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
	
	echo "1";
	exit(0);
}

function sanitize($text) {
	$text = htmlspecialchars($text, ENT_QUOTES);
	$text = str_replace("\n\r","\n",$text);
	$text = str_replace("\r\n","\n",$text);
	$text = str_replace("\n","<br>",$text);
	return $text;
}
?>
 

hackersatifal

Utente Attivo
9 Ago 2012
238
0
0
Ho provato il tuo codice, ma non mi funziona bene e non visualizza le emoticon, quindi ho modificato un file javascript e questo php e ora non me lo sostituisce, ma mi dà :) quindi senza ".()."
Secondo te dove può essere il problema?
Ecco il mio codice php:
PHP:
<?php

define ('DBPATH','localhost');
define ('DBUSER','****');
define ('DBPASS','*********');
define ('DBNAME','miodatabase');

session_start();

global $dbh;
$dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
mysql_selectdb(DBNAME,$dbh);

if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); } 
if ($_GET['action'] == "sendchat") { sendChat(); } 
if ($_GET['action'] == "closechat") { closeChat(); } 
if ($_GET['action'] == "startchatsession") { startChatSession(); } 

if (!isset($_SESSION['chatHistory'])) {
	$_SESSION['chatHistory'] = array();	
}

if (!isset($_SESSION['openChatBoxes'])) {
	$_SESSION['openChatBoxes'] = array();	
}
function faccine($items){
  $items_ok = str_replace(":)", "<img src='smile.gif'>", $items);
  $items_ok = str_replace(":-)", "<img src='smile.gif'>", $items_ok);
  $items_ok = str_replace(":(", "<img src=\"triste.gif\">", $items_ok);
  $items_ok = str_replace(":-(", "<img src=\"triste.gif\">", $items_ok);
  $items_ok = str_replace(":P", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":p", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":-p", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":-P", "<img src=\"linguaccia.gif\">", $items_ok);
  $items_ok = str_replace(":D", "<img src=\"grandesmile.gif\">", $items_ok);
  $items_ok = str_replace(":-D", "<img src=\"grandesmile.gif\">", $items_ok);
  $items_ok = str_replace(":o", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":O", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":-o", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(":-O", "<img src=\"stupito.gif\">", $items_ok);
  $items_ok = str_replace(";)", "<img src=\"occhiolino.gif\">", $items_ok);
  $items_ok = str_replace(";-)", "<img src=\"occhiolino.gif\">", $items_ok);
  $items_ok = str_replace(":v", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":V", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":-v", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(":-V", "<img src=\"pacman.gif\">", $items_ok);
  $items_ok = str_replace(">:( ", "<img src=\"occhistrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-(", "<img src=\"occhistrizzati.gif\">", $items_ok);
  $items_ok = str_replace(":/", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":-/", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":'(", "<img src=\"lacrima.gif\">", $items_ok);
  $items_ok= str_replace(":'-(", "<img src=\"lacrima.gif\">", $items_ok);
  $items_ok = str_replace("8)", "<img src=\"occhialichiari.gif\">", $items_ok);
  $items_ok = str_replace("8-)", "<img src=\"occhialichiari.gif\">", $items_ok);
  $items_ok = str_replace("B|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("B-|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("b|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("b-|", "<img src=\"occhialiscuri.gif\">", $items_ok);
  $items_ok = str_replace("<3", "<img src=\"cuore.gif\">", $items_ok);
  $items_ok = str_replace("3:)", "<img src=\"diavoletto.gif\">", $items_ok);
  $items_ok = str_replace("3:-)", "<img src=\"diavoletto.gif\">", $items_ok);
  $items_ok = str_replace("o:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("O:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("0:)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("o:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("O:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("0:-)", "<img src=\"angioletto.gif\">", $items_ok);
  $items_ok = str_replace("o.O", "<img src=\"occhiopiùgrande.gif\">", $items_ok);
  $items_ok = str_replace(">:o", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:O", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-o", "<img src=\"felicissimostrizzati.gif\">", $items_ok);
  $items_ok = str_replace(">:-O", "<img src=\"felicissimostrizzati.gfi\">", $items_ok);
  $items_ok = str_replace(":3", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace(":-3", "<img src=\"perplesso.gif\">", $items_ok);
  $items_ok = str_replace("*_*", "<img src=\"indifferente.gif\">", $items_ok);
  $items_ok = str_replace("*-*", "<img src=\"indifferente.gif\">", $items_ok);
  $items_ok = str_replace("-_-", "<img src=\"indifferente.gif\">", $items_ok);
  
  return $items_ok;
}
function chatHeartbeat() {
	
	$sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' AND recd = 0) order by id ASC";
	$query = mysql_query($sql);
	$items = '';

	$chatBoxes = array();

	while ($chat = mysql_fetch_array($query)) {

		if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
			$items = $_SESSION['chatHistory'][$chat['from']];
		}

		$chat['message'] = sanitize($chat['message']);

		$items .= <<<EOD
					   {
			"s": "0",
			"f": "{$chat['from']}",
			"m": "{$chat['message']}"
	   },
EOD;

	if (!isset($_SESSION['chatHistory'][$chat['from']])) {
		$_SESSION['chatHistory'][$chat['from']] = '';
	}

	$_SESSION['chatHistory'][$chat['from']] .= <<<EOD
						   {
			"s": "0",
			"f": "{$chat['from']}",
			"m": "{$chat['message']}"
	   },
EOD;
		
		unset($_SESSION['tsChatBoxes'][$chat['from']]);
		$_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
	}

	if (!empty($_SESSION['openChatBoxes'])) {
	foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
		if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
			$now = time()-strtotime($time);
			$time = date('g:iA M dS', strtotime($time));

			$message = "Inviato: $time";
			if ($now > 180) {
				$items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

	if (!isset($_SESSION['chatHistory'][$chatbox])) {
		$_SESSION['chatHistory'][$chatbox] = '';
	}

	$_SESSION['chatHistory'][$chatbox] .= <<<EOD
		{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
			$_SESSION['tsChatBoxes'][$chatbox] = 1;
		}
		}
	}
}

	$sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['utente'])."' and recd = 0";
	$query = mysql_query($sql);

	if ($items != '') {
		$items = substr($items, 0, -1);
	}
header('Content-type: application/json');
?>
{
		"items": [
			<?php echo ".faccine($items).";?>
        ]
}

<?php
			exit(0);
}

function chatBoxSession($chatbox) {
	
	$items = '';
	
	if (isset($_SESSION['chatHistory'][$chatbox])) {
		$items = $_SESSION['chatHistory'][$chatbox];
	}

	return "faccine.($items).";
}

function startChatSession() {
	$items = '';
	if (!empty($_SESSION['openChatBoxes'])) {
		foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
			$items .= chatBoxSession($chatbox);
		}
	}


	if ($items != '') {
		$items = substr($items, 0, -1);
	}

header('Content-type: application/json');
?>
{
		"username": "<?php echo $_SESSION['utente'];?>",
		"items": [
			<?php echo ".faccine($items).";?>
        ]
}

<?php


	exit(0);
}

function sendChat() {
	$from = $_SESSION['utente'];
	$to = $_POST['to'];
	$message = $_POST['message'];

	$_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());
	
	$messagesan = sanitize($message);

	if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
		$_SESSION['chatHistory'][$_POST['to']] = '';
	}

	$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
					   {
			"s": "1",
			"f": "{$to}",
			"m": "{$messagesan}"
	   },
EOD;


	unset($_SESSION['tsChatBoxes'][$_POST['to']]);

	$sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
	$query = mysql_query($sql);
	echo "1";
	exit(0);
}

function closeChat() {

	unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);
	
	echo "1";
	exit(0);
}

function sanitize($text) {
	$text = htmlspecialchars($text, ENT_QUOTES);
	$text = str_replace("\n\r","\n",$text);
	$text = str_replace("\r\n","\n",$text);
	$text = str_replace("\n","<br>",$text);
	return $text;
}
?>
Volevo dire che mi spunta :) senza immagine e quindi non funziona, ma ora mi spunta senza gli apici, i punti e le parentesi.
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.042
146
63
PR
www.borgo-italia.it
ciao
perche dici che non funzia, ho provato
PHP:
<?php
$items = "nel mezzo di :) camin di ns vita mi ritrovai per una-) selva oscura";
$items_ok =str_replace(":)","<img src='smile.gif'>",$items);
$items_ok =str_replace("-)","<img src='smile.gif'>",$items_ok);
echo $items_ok;
?>
e mi da le faccine (anzi il classico X rosso perche non ho i gif) al posto giusto
 

hackersatifal

Utente Attivo
9 Ago 2012
238
0
0
ciao
perche dici che non funzia, ho provato
PHP:
<?php
$items = "nel mezzo di :) camin di ns vita mi ritrovai per una-) selva oscura";
$items_ok =str_replace(":)","<img src='smile.gif'>",$items);
$items_ok =str_replace("-)","<img src='smile.gif'>",$items_ok);
echo $items_ok;
?>
e mi da le faccine (anzi il classico X rosso perche non ho i gif) al posto giusto
A me invece spunta solamente : ) senza immagine :dipser:
 

hackersatifal

Utente Attivo
9 Ago 2012
238
0
0
Ho provato anche come mi hai detto tu ma non funziona: sto impazzendo e non so più che prove fare :dipser:
 

borgo italia

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
4 Feb 2008
16.042
146
63
PR
www.borgo-italia.it
ciao
fai una prova: ti fai un file php (es. prova.php) e ci metti solo la funzione delle faccine e metti qualcosa in $items faccine comprese e prova con
PHP:
<?php
function faccine($items){
  $items_ok = str_replace(":)", "<img src='smile.gif'>", $items);
  $items_ok = str_replace(":-)", "<img src='smile.gif'>", $items_ok);
  $items_ok = str_replace(":(", "<img src=\"triste.gif\">", $items_ok);
  //eccetera.....
 
  return $items_ok;
}
$items="pinco :) pallo eccetera.....";
echo faccine($items);
?>
se così funzia l'errore non è su faccine
p.s.
sono giusti i percorsi?