0

[updated again!] stop changing my question it became unprecise.

I have kdeconnect 1.4.0 installed on Ubuntu 20.04.2 LTS Focal and it is functional. I am able to text from the command line using the user that I used installed kdeconnect with.

My current task is to have the command "kdeconnect-cli" executable by Apache2 version 2.4.41 with PHP version 7.4.

I thought it would be as simple as adding the path to the command to the PHP.INI, but that does nothing.

Apache2 runs as the user www-data and I have made these changes to grant the privilege and run the command. But it still does not work.

  • I used shell_exec and it returns blank. I have tested this and get no output

$command = "kdeconnect-cli --send-sms $message --destination $phoneNumber --device 26de31sdfbc6b97f"; $output = shell_exec($command); echo "<pre>$output</pre>";

  • I added the command to the /etc/php/7.4/cli/php.ini file

include_path = ".:/usr/bin"

  • I added this line to the sudoers file

www-data ALL=(KDEUSER) NOPASSWD: /usr/bin/kdeconnect_cli

  • I've tried making a .sh file that run the kdeconnect-cli and it works in the terminal, but it does not if run by 'www-data' in PHP

My ultimate goal is to run kdeconnect-cli with PHP, what am I missing?

Thanks in advance.

P.S. Don't edit my question.

6
  • PHP's shell_exec is made for that.
    – Raffa
    Jun 17, 2021 at 6:33
  • I have tested this and get no output $command = "kdeconnect-cli --send-sms $message --destination $phoneNumber --device 26de31sdfbc6b97f"; $output = shell_exec($command); echo "<pre>$output</pre>"; Jun 17, 2021 at 6:44
  • This site is so hard to get text to show correctly, its my first time. Please bare with me. Jun 17, 2021 at 6:45
  • Then don't say "P.S. Don't edit my question." when people who do know the correct way to format things try to fix them for you. This site is quite encouraging of people editing others' posts, so learn to accept them. I don't see where I made anything "unprecise".
    – muru
    Jun 17, 2021 at 7:56
  • Bullets and attached text were changed to code blocks. Bullets are ways to order text and making them code blocks when it was not code makes it unprecise and unreadable. Jun 17, 2021 at 8:02

1 Answer 1

0

One way

There is a built-in function for that in PHP called shell_exec

(PHP 4, PHP 5, PHP 7, PHP 8)

shell_exec — Execute command via shell and return the complete output as a string

Another way

Save your PHP form data to a file on your system and parse it immediately in bash. The steps are easy:

  • Create a directory with read and write permissions for both the user that will run the PHP web-page e.g. www-data and the user that will run the bash script e.g. you.

  • Run a bash script in the background as a process to monitor for newly created files in the directory, parse them to send the messages and then delete them afterwords... utilizing inotify-tools like so:

    #!/bin/bash
    
    # Change the path to directory and the device accordingly
    
    path_to_directory="/full/path/to/the/directory/to/save/messages/"
    device="26de31sdfbc6b97f" 
    
    inotifywait -m "$path_to_directory" -e create |
    while IFS=' ' read path action file;
        do
        echo "$path$file was created"
        while IFS='|' read -r n m
            do      
            echo "Sending: $m ---> $n via $device"
            kdeconnect-cli --send-sms "$m" --destination "$n" --device "$device"
            done < "$path$file"
        rm "$path$file"
        echo "$path$file was deleted"
        done
    

    Install inotify-tools with sudo apt install inotify-tools if it is not already installed. After that save the above code to a script file like send.sh, make the script file executable with chmod +x send.sh then run it with bash send.sh and keep it running.

  • Make your PHP page save your form data (the number and the message) to a file in the same directory and in one line with a delimiter like | between them... and generate the file name randomly to avoid overwriting the same file. Take the following PHP snippet as an example:

    <?PHP
    //Modify the following three lines.
    //You can dynamically assign your form number to $number and
    //your form message to $message
    
    $number="0096650123456";
    $message="This is a test message.";
    $path_to_directory="/full/path/to/the/directory/to/save/messages/";
    
    
    //Do not modify beloww this line
    
    $content=$number . "|" . $message . "\n";
    $filename=$path_to_directory . rand(0000, 9999) . ".msg";
    
    file_put_contents($filename, $content);
    
8
  • I've used it and it does not work and does not return any error. I have tested this and get no output \r\n $command = "kdeconnect-cli --send-sms $message --destination $phoneNumber --device 26de31sdfbc6b97f"; \r\n $output = shell_exec($command); \r\n echo "<pre>$output</pre>"; Jun 17, 2021 at 6:47
  • How to you create a carriage return in these comments so comments make since? Jun 17, 2021 at 6:48
  • It seemed this was the solution but it did not work either. stackoverflow.com/questions/48331794/… Jun 17, 2021 at 7:47
  • @newtoallthis shell_exec is not the only solution for your requirement… saving the message and phone number to a file and parsing/processing it with a bash script is another way .., I will update my answer with that when I get back to my computer as I am commenting from my phone now… It will be solved :)
    – Raffa
    Jun 17, 2021 at 9:06
  • One of the requirements to this project is that the message and the phone number both originate from PHP. I wish I could stay with bash script, I would have been done. Thanks Raffa, I appreciate your help. Jun 17, 2021 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.