sexta-feira, 19 de setembro de 2014

Acesso Remoto em banco de dados Mysql

Por padrão o mysql vem habilitado para aceitar conexões locais, para conseguir conectar remotamente é necessario fazer algumas modificações.
Abrir o arquivo:
/etc/my.cnf e comentar a linha  " bind-address = 127.0.0.1 "

PARA ACESSO DE QUALQUER IP

grant all privileges on *.* to root@'%' identified by 'aqui a senha' with grant option;

PARA ACESSO DE HOST ESPECIFICO

GRANT ALL PRIVILEGES ON *.* TO user_name@host IDENTIFIED BY 'pass_word';

E POR FIM

flush privileges;

Agenda para os telefones IP Grandstream

Primeiramente crie uma página PHP com o seguinte conteúdo:

<?php

//PHP PARA GERAR LISTA CONTATOS EM XML PARA APARELHOS DA GRANDSTREAM
//CRIADO POR JUNIOR VIEIRA
//EMAIL: JUNIORDJV@GMAIL.COM

 //Criando a conexão com a base
    $db = mysql_connect("host","user","senha");
    if (!$db) {
        die('Could not connect to db: ' . mysql_error());
    }

    //Selecionando a base de dados
    mysql_select_db("asterisk",$db);

    //Select para buscar os dados para agenda
    $result = mysql_query("SELECT * FROM devices ORDER BY description ASC", $db);

    //Criando SimpleXMLElement object
    $xml = new SimpleXMLElement('<AddressBook/>');
    $index = 0;
   
while($row = mysql_fetch_assoc($result)) {
$mydata = $xml->addChild('Contact');
        $mydata->addChild('LastName',$row['description']);
$mydata->addChild('FirstName',' ');
$mydata = $mydata->addChild('Phone');
$mydata->addChild('phonenumber',$row['user']);
$mydata->addChild('accountindex',$index);

}
mysql_close($db);


    //Criando o arquivo XML
    $fp = fopen("informe o seu diretório de gravação","wb");

    //Escrevendo o XML
    fwrite($fp,$xml->asXML());

    //Informando que o arquivo foi gravado

    $dataHora = date("d/m/Y h:i:s");
    echo $dataHora ;
    echo " - Arquivo gravado com sucesso em: /tftpboot/gs_phonebook.xml";
   
    //Fechando a conexao com a base de dados
    fclose($fp);

?>

<br><button type="button" onclick="location.href='download.php'">Download XML</button></br>


Agora crieu uma página download.php para download do XML gerado:

<?php
    // abaixo informe o diretorio que foi gravado
    $file = "/tftpboot/gs_phonebook.xml";

    if(!file_exists($file)) die("Arquivo Inexistente.");

    $type = filetype($file);
    // Get a date and timestamp
    $today = date("F j, Y, g:i a");
    $time = time();
    // Send file headers
    header('Content-type: text/xml');
    header('Content-Disposition: attachment; filename="gs_phonebook.xml"');
    header("Content-Transfer-Encoding: binary");
    header('Pragma: no-cache');
    header('Expires: 0');
    // Send the file contents.
    set_time_limit(0);
    readfile($file);
?>

Feito isto é só executar o php, que o arquivo será gerado no diretório informado, ou baixe pelo botão.