0

I have a python file for sending automatic sms, as I show here:

myresult = cursor.fetchall()

for linha in myresult:
 PrimeiroNome = linha[0]
 UltimoNome = linha[1]
 respons = linha[2]
 contact = linha[3]
 Data = linha[4]
 Datasms = linha[5]
 Hora = linha[6]
 title = linha[7]

 if __name__ == "__main__":
    url ="xxxxxxxxxxxxxxxxxxxxxxxxx"
    usrPass = "xxxxxxxxxxxxxxxxxxxx"
    data = json.dumps({
    u"to":[contact],
    u"from":u"xxxxxxxxxxx",
    u"message":u"Informamos que tem visita agendada no dia {} às {}, com o Utente {} {}. Caso não possa comparecer, avise-nos pf.".format(
        Datasms, Hora, PrimeiroNome, UltimoNome),
    u"encoding":u"gsm-pt",
    u"parts":u"2"
    })
    b64Val = base64.b64encode(usrPass)
    headers=["Accept:Application/json","Authorization:Basic %s"%b64Val]
    c = pycurl.Curl()
    c.setopt(pycurl.URL, url)
    c.setopt(pycurl.HTTPHEADER,headers)
    c.setopt(pycurl.POST, 1)
    c.setopt(pycurl.POSTFIELDS, data)
    c.setopt(pycurl.SSL_VERIFYHOST, 0)
    c.setopt(pycurl.SSL_VERIFYPEER, 0)
    c.perform()
    http_code = c.getinfo(pycurl.HTTP_CODE)

The file path is /var/www/html/wp-content/themes/sparkling/smsvistadia.py. I intend that when inserting into the database in php that you execute this file and send the sms if you fulfill the condition.

I'm trying this way:

$sql = "INSERT INTO AgendaVisitas(title, respons, contact, title1, contact1, titl1, contac1, start, end, Acamado, DataRegisto, colaborador, week) values  ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

$sth = $conn->prepare($sql )->execute([$title, $respons, $contact, $title1, $contact1, $titl, $contac, $start, $end, $Acamd, $DataRegisto, $colaborador, $week]);

if(DATE($DataRegisto) == DATE($start)){     
    $command = escapeshellcmd('/var/www/html/wp-conten/themes/sparkling/smsvistadia.py');
    $output = shell_exec($command);
}

But when I do the insert in order to fulfill the condition it does not execute the action of the python file which is to send the sms. Can you help?

Bruno
  • 801
  • 5
  • 11
  • You could do the insert from python, no? This may be open to SQL injections as is. Your prepared statement usage is incorrect. `var_dump(DATE($DataRegisto) == DATE($start))` gives back what? – user3783243 May 24 '21 at 16:28
  • @user3783243 Ok I'm going to fix this part of the code – Bruno May 24 '21 at 16:29
  • @user3783243 I already fixed the sql part. But is it possible to do it the way I am trying? – Bruno May 24 '21 at 16:34
  • Is there a `#!/somepath/python` or `#!/usr/bin/env python` or something at the top of `smsvistadia.py`? If not you have to call it like `/somepath/python /path/to/smsvistadia.py` – AbraCadaver May 24 '21 at 16:47
  • Does this help https://stackoverflow.com/a/19736494/4882134 ? – endeavour May 24 '21 at 16:49
  • @AbraCadaver At the top of the python file I have: `#! /usr/bin/env python`, `# -*- coding: utf-8 -*-`, `import MySQLdb`, `import pycurl`, `import base64`, `import json`, `import datetime`, `import smtplib` – Bruno May 24 '21 at 16:52
  • Does `$output` contain anything? – AbraCadaver May 24 '21 at 16:54
  • @endeavour What I did followed me through that link you put, but you are not running the python file – Bruno May 24 '21 at 16:54
  • @AbraCadaver I have already tested the python file and it is sending the sms. I'm not getting through php. I will check if the $ output returns anything – Bruno May 24 '21 at 16:56
  • The variables in the `execute` should not be quoted. – user3783243 May 24 '21 at 16:58
  • @user3783243 You can put an example with the best way to do it – Bruno May 24 '21 at 17:05
  • What you've done is right, just no quotes. `->execute(['$title',` should be `->execute([$title,` in quotes it is literal. The driver handles the quoting – user3783243 May 24 '21 at 21:47
  • @user3783243 Okay, thanks for the tip. I still haven't been able to solve my question problem – Bruno May 24 '21 at 23:16
  • what are the permissions of the pyton file? is it executable? – Ron May 24 '21 at 23:21
  • What did the `var_dump` give back? – user3783243 May 24 '21 at 23:34
  • @user3783243 Var_dump doesn't return anything, but I know the python file is correct, because if you run python directly on ubuntu it sends the message – Bruno May 25 '21 at 08:09
  • @Ron How do I see it from permissions and if it is executable? I know the file has the correct code – Bruno May 25 '21 at 08:10
  • @Pinto `var_dump` always returns something. Either you have a fatal error or the place you put that didn't execute. – user3783243 May 25 '21 at 09:56
  • to make sure the file is executable, you can do `chmod +x /var/www/html/wp-conten/themes/sparkling/smsvistadia.py`; to check the permissions of a file, use `stat /var/www/html/wp-conten/themes/sparkling/smsvistadia.py` , you should research for linux file permissions... OR in your code you need (as stated above) to prepend like so: `'python /var/www/html/wp-conten/themes/sparkling/smsvistadia.py'` which will use the default `python` in your distribution.. – Ron May 25 '21 at 23:42

0 Answers0