quinta-feira, 19 de maio de 2016

Apache + PHP + Virtual Host


Vi como grande vantagem vhosts em meus projetos PHP, sendo uma excelente forma de criar subdominios para cada projeto e evitar riscos de varreduras indesejadas a outros sistemas no diretório padrão do apache.
Suponhamos que o domínio para o sistema seja subdominio.dominio.com.br

Podemos usar o diretório padrão do apache ou outro, como por exemplo /srv/www, fique ao seu critério.

Estou tomando como base a estrutura de diretórios do Debian, sistema operacional que uso.

Em /var/www:

$ sudo mkdir subdominio.dominio.com.br 

Em /etc/apache2/sites-available:

$ sudo vim subdominio.conf 

Dentro de subdominio.conf cole:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
ServerName subdominio.dominio.com.br
        DocumentRoot /home/lucio/sites/subdominio.dominio.com.br
        <Directory /home/lucio/sites/subdominio.dominio.com.br/>
                Options Indexes FollowSymLinks MultiViews Includes ExecCGI
                AllowOverride All
                Order allow,deny
                allow from all
                Require all granted
            AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/subdominio.dominio.com.br.error.log
        LogLevel warn
        CustomLog ${APACHE_LOG_DIR}/subdominio.dominio.com.br.access.log combined
</VirtualHost>

Veja que coloquei em Document Root e Directory o seguinte diretório:
/home/lucio/sites/subdominio.dominio.com.br

Criei um diretório sites, e dentro dele crio links simbólicos das minhas vhosts. Isso é um capricho meu, apenas para organização. Fica como sugestão, mas caso não queira pode colocar o caminho real do projeto.


Após toda configuração, entre com os comandos:

$ sudo a2ensite subdominio.conf 
$ sudo service apache2 restart 


That's all folks!

sexta-feira, 15 de abril de 2016

Cannot set LC_ALL to default locale: No such file or directory


lucio@workstation:~$ locale
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=pt_BR.UTF-8
LC_TIME=pt_BR.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=pt_BR.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=pt_BR.UTF-8
LC_NAME=pt_BR.UTF-8
LC_ADDRESS=pt_BR.UTF-8
LC_TELEPHONE=pt_BR.UTF-8
LC_MEASUREMENT=pt_BR.UTF-8
LC_IDENTIFICATION=pt_BR.UTF-8
LC_ALL=

Para corrigir essa mensagem de erro no Debian, execute os seguintes comandos:

$ locale-gen en_US en_US.UTF-8 pt_BR.UTF-8
$ dpkg-reconfigure locales

Após isso, faça o login novamente e execute o comando locale para certificar de que foi o problema foi corrigido.

Até!

sábado, 9 de abril de 2016

Legendas .srt + Smart TV Sony Bravia + interrogações


Se você está tendo problemas de legendas com sua TV Sony Bravia, e ao invés de aparecer os textos aparecem interrogações (????), fique tranquilo pois é fácil de resolver esse problema.

Com toda certeza, seu arquivo de legenda (.srt) não está com a codificação correta. A codificação certa tem que ser a Unicode Big Endian, em outras palavras UTF-16 BE.

Eu usei o Sublime Text para mudar a codificação. Simplesmente fui em File >> Save with Encoding >> UTF-16 BE .  

Pronto, volte lá para sua TV e bom filme!

quinta-feira, 3 de março de 2016

PHP + MySQL blob fields and UTF-8

I had problems with graphic accentuation using a blob field in MySQL. When I was a select, the accentuation was not recognized by PHP, but in the others fields, like VARCHAR, CHAR or TEXT were recognized.

The solution was use the PHP function utf8_encode.

 $query = mysql_query("SELECT blob_field FROM table");  
 $arr = mysql_fetch_array($query);  
 echo $arr['field'];  
 >> m�e  
 echo utf8_encode($arr['blob_field']);  
 >> mãe  


segunda-feira, 22 de fevereiro de 2016

Python and Google Gmail API


Hi guys, I will try write this post in english, sorry about some erros, cause I'm studying and improving bit a bit.

Firstly, let me tell you what I intended to do. I'd like built a bot to monitor prices in some sites, if the bot find prices that wourthwhile it send me an email. I could do this using smtplib library, see below:

 import smtplib  
 gmail_user = 'emailFrom@gmail.com'  
 gmail_pwd = 'emailFrom@gmail.com password'  
 FROM = 'emailFrom@gmail.com'  
 TO = ['emailTo@gmail.com'] #recipient if type(recipient) is list else [recipient]  
 SUBJECT = 'subject text'  
 TEXT = 'body text'  
 # Prepare actual message  
 message = """\From: %s\nTo: %s\nSubject: %s\n\n%s""" % (FROM, ", ".join(TO), SUBJECT, TEXT)  
 try:  
     server = smtplib.SMTP('smtp.gmail.com', 587)  
     server.ehlo()  
     server.starttls()  
     server.login(gmail_user, gmail_pwd)  
     server.sendmail(FROM, TO, message)  
     server.close()  
     print 'Successfully sent the mail to emailTo@gmail'  
 except:  
     print 'Failed to send mail'  

But, you need enable access for less secure apps. I didn't like to do this, feeling me unsafe and I would want to use what the Google recomends, and I went study how could use the Google API for Gmail.

I started following the process step by step, as this tutorial Google asks to do. The quickstart script inside this tutorial enable you only read emails, not send or make other things. When I tried to send an email I received this answer:

HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/messages/send?alt=json returned "Insufficient Permission"

Reading many forums looking for the answer to that message that I could know what was happening.

Note that in quickstart.py, at the line SCOPES = 'https://www.googleapis.com/auth/gmail.readonly', makes it the only permission is for reading, for another thing you do not have permission.


Here begins the purpose of this post !!!


For it works, follow the steps...

In your Google Developers Console, click in Credentials >> Create credentials and choice OAuth client ID. Check de option Other and put any Name, like gmail. Click Create and click OK to dismiss the resulting dialog. Click the to download (Download JSON) button to the right of the client ID and move this file to your working directory and rename it gmail.json.

Note that this step is very similar to Google tutorial.

Now, following the script to send e-mail, it is a mix between quickstart.py and other Google script:


 import httplib2  
 import os  
 from httplib2 import Http  
   
 from apiclient import discovery  
 from apiclient import errors  
   
 import oauth2client  
 from oauth2client import client  
 from oauth2client import tools  
   
 import base64  
 from email.mime.audio import MIMEAudio  
 from email.mime.base import MIMEBase  
 from email.mime.image import MIMEImage  
 from email.mime.multipart import MIMEMultipart  
 from email.mime.text import MIMEText  
 import mimetypes  
   
 try:  
   import argparse  
   flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()  
 except ImportError:  
   flags = None  
   
 # If modifying these scopes, delete your previously saved credentials  
 # at ~/.credentials/gmail-python-quickstart.json  
 SCOPES = 'https://mail.google.com/'  
 #SCOPES = 'https://mail.google.com/'  
 CLIENT_SECRET_FILE = 'gmail.json'  
 APPLICATION_NAME = 'Gmail API Python'  
   
   
 def SendMessage(service, user_id, message):  
     """Send an email message.  
   
     Args:  
         service: Authorized Gmail API service instance.  
         user_id: User's email address. The special value "me"  
         can be used to indicate the authenticated user.  
         message: Message to be sent.  
   
     Returns:  
         Sent Message.  
     """  
     try:  
         message = (service.users().messages().send(userId=user_id, body=message).execute())  
         print ('Message Id: %s' %message['id'])  
         return message  
     except errors.HttpError, error:  
         print ('An error occurred: %s' % error)  
   
   
 def CreateMessage(sender, to, subject, message_text):  
     """Create a message for an email.  
   
     Args:  
         sender: Email address of the sender.  
         to: Email address of the receiver.  
         subject: The subject of the email message.  
         message_text: The text of the email message.  
   
     Returns:  
         An object containing a base64 encoded email object.  
     """  
     message = MIMEText(message_text)  
     message['to'] = to  
     message['from'] = sender  
     message['subject'] = subject  
     return {'raw': base64.b64encode(message.as_string())}  
   
 def get_credentials():  
     """Gets valid user credentials from storage.  
   
     If nothing has been stored, or if the stored credentials are invalid,  
     the OAuth2 flow is completed to obtain the new credentials.  
   
     Returns:  
         Credentials, the obtained credential.  
     """  
     home_dir = os.path.expanduser('~')  
     credential_dir = os.path.join(home_dir, '.credentials')  
     if not os.path.exists(credential_dir):  
         os.makedirs(credential_dir)  
     credential_path = os.path.join(credential_dir, 'gmail-python-gmail.json')  
   
     store = oauth2client.file.Storage(credential_path)  
     credentials = store.get()  
     if not credentials or credentials.invalid:  
         flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)  
         flow.user_agent = APPLICATION_NAME  
         if flags:  
             credentials = tools.run_flow(flow, store, flags)  
         else: # Needed only for compatibility with Python 2.6  
             credentials = tools.run(flow, store)  
         print('Storing credentials to ' + credential_path)  
     return credentials  
   
 def main():  
     credentials = get_credentials()  
     http = credentials.authorize(httplib2.Http())  
     service = discovery.build('gmail', 'v1', http=http)  
   
     msg = CreateMessage('emailFrom@gmail.com', 'emailTo@gmail.com', 'subject', 'body email')  
     print SendMessage(service, 'me', msg)  
   
 if __name__ == '__main__':  
     main()  


When running for the first time this script opens a tab in your browser requesting permission for this new json. Next time do not need anymore.

I hope this helps, and if it did not work, talk to me.

May the force be with you!