Score:0

How to use kdeconnect-cli with Apache2 and PHP

us flag

[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.

Raffa avatar
jp flag
PHP's [`shell_exec`](https://www.php.net/manual/en/function.shell-exec.php) is made for that.
newtoallthis avatar
us flag
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>";`
newtoallthis avatar
us flag
This site is so hard to get text to show correctly, its my first time. Please bare with me.
muru avatar
us flag
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".
newtoallthis avatar
us flag
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.
muru avatar
us flag
The lists were kept as lists. On this site, we prefer formatting file contents, like configuration as code blocks. That is the only way to accurately and *precisely* render them.
Score:0
jp flag

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);
    
newtoallthis avatar
us flag
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>";`
newtoallthis avatar
us flag
How to you create a carriage return in these comments so comments make since?
newtoallthis avatar
us flag
It seemed this was the solution but it did not work either. https://stackoverflow.com/questions/48331794/php-shell-exec-doesnt-work-but-commands-work-from-console
Raffa avatar
jp flag
@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 :)
newtoallthis avatar
us flag
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.
Raffa avatar
jp flag
@newtoallthis That is what I meant … they will originate from PHP and then saved to file… that file will be instantly processed by bash script running in the background as a process… message sent and file deleted afterwards… and so on… is that ok with you?
newtoallthis avatar
us flag
Sure, everything I've tried is locked down by 'user-www' or permission issues. So, if you have an idea, I'll wait till you get to your computer. Thanks
Raffa avatar
jp flag
@newtoallthis Done that... I hope it works for you.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.