{"id":574,"date":"2012-10-18T14:05:16","date_gmt":"2012-10-18T14:05:16","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=574"},"modified":"2016-02-03T17:05:00","modified_gmt":"2016-02-03T17:05:00","slug":"web-interfaces","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/tcpip\/web-interfaces","title":{"rendered":".General PHP Code Tricks"},"content":{"rendered":"<h4>\nIssuing Commands From Web Pages<br \/>\n<\/h4>\n<p>\nIf you <a href=\"https:\/\/raspberry-projects.com\/pi\/pi-operating-systems\/raspbian\/web-servers\/phpapache\" target=\"_blank\">install a PHP web server on your RPi<\/a> you can issue commands using the PHP system() function.\n<\/p>\n<p>\nExamples:\n<\/p>\n<pre>\r\n<code>\r\n\/\/Echo some system status:\r\n\techo &#39;&lt;pre&gt;&#39;;\r\n\tsystem(&#39;netstat -a&#39;);\r\n\techo &#39;&lt;\/pre&gt;&#39;;\r\n\/\/Run an executable file:\r\n\techo &#39;&lt;pre&gt;&#39;;\r\n\tsystem(&quot;sudo \/home\/pi\/projects\/my_project.a&quot;);\t\t\/\/system() will echo the output of the executable, use shell_exec() instead if you don&#39;t want this\r\n\techo &#39;&lt;\/pre&gt;&#39;;\r\n<\/code>\r\n<\/pre>\n<p>\n<strong><em>WARNING!!&nbsp;When allowing user-supplied data to be passed use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.<\/em><\/strong>\n<\/p>\n<p>\nsystem()\n<\/p>\n<p style=\"margin-left: 40px;\">\nAlso echo&#39;s the output of the command to the web page\n<\/p>\n<p>\nshell_exec()\n<\/p>\n<p style=\"margin-left: 40px;\">\nDoes not echo the output of the command and&nbsp;returns&nbsp;you all of the&nbsp;output\n<\/p>\n<p>\nexec()\n<\/p>\n<p style=\"margin-left: 40px;\">\nDoes not echo the output of the command but returns&nbsp;only the last line of any output\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\nIf it doesn&#39;t work\n<\/p>\n<p style=\"margin-left: 40px;\">\nTry adding this to the end of your command:&nbsp;2&gt;&amp;1<br \/>\nFor example:&nbsp;sudo fping -c1 -t750 192.168.1.1 2&gt;&amp;1<br \/>\nThis will cause error text to be returned\n<\/p>\n<p>\nIssuing commands in the background\n<\/p>\n<p>\nYou can&#39;t just add the &#39;&amp;&#39; character to the end of commands&nbsp;In PHP, because all calls are always waiting for the command to return. &nbsp;There&#39;s a good post in the issue here:&nbsp;<a href=\"http:\/\/phaq.phunsites.net\/2012\/01\/18\/run-command-in-background-from-php\/\">http:\/\/phaq.phunsites.net\/2012\/01\/18\/run-command-in-background-from-php\/<\/a>\n<\/p>\n<p>\nTo fix it you need to direct the console&nbsp;output&nbsp;to a file instead and just adding this to the end of a command will do that:&nbsp;&nbsp;&gt; \/dev\/null 2&gt;&amp;1 &amp; echo $!\n<\/p>\n<p>\nThe following&nbsp;example issues a reboot command after a 2 second delay (you need to add the command to sudoeres for it to work &#8211; see below) and does it in the background so that the page is loaded before the reboot is actioned:\n<\/p>\n<pre>\r\n<code>\r\n&lt;?php\r\n\t\/\/----- DO THE REBOOT -----\r\n\t\t\/\/THIS SUDO COMMAND HAS AUTHORISED FOR APACHE TO USE IN THE FILE: sudo nano \/etc\/sudoers\r\n\t\t\/\/\t# Special for this system - let apache run exes we use in the web interface\r\n\t\t\/\/\twww-data ALL=NOPASSWD: \/sbin\/reboot\r\n\techo &#39;&lt;pre&gt;&#39;;\r\n\tsystem(&quot;(sleep 2 ; sudo \/sbin\/reboot ) &gt; \/dev\/null 2&gt;&amp;1 &amp; echo $!&quot;);\r\n\techo &#39;&lt;\/pre&gt;&#39;;\r\n?&gt;\r\n<\/code><\/pre>\n<h5>\nIssuing sudo commands<br \/>\n<\/h5>\n<p>\nApache will typically run using account www-data and this account is not permitted to use sudo. &nbsp;If you need to use sudo you need to enable this, but you don&#39;t want to simply give www-data global sudo access.&nbsp;If a malicious script is uploaded to your web server and gets executed this opens a massive security risk. The more secure approach is&nbsp;to restrict it to only the commands that you actually use in your own scripts.\n<\/p>\n<p>\nYou can use this command to open the sudoers file:\n<\/p>\n<pre>\r\n<code>\r\nsudo nano \/etc\/sudoers\r\n<\/code><\/pre>\n<p>\nBE VERY CAREFUL TO COPY THIS FILE BEFORE YOU CHANGE IT &#8211; IF YOU MAKE AN ERROR YOU CAN STOP YOURSELF BEING ABLE TO OPEN THE FILE USING&nbsp;SUDO (changes to it are instant)!\n<\/p>\n<p>\nAn example enabling it to run a single executible, add this to the end of the file:\n<\/p>\n<pre>\r\n<code>\r\nwww-data ALL=NOPASSWD: \/home\/pi\/some_executable_name\r\n<\/code><\/pre>\n<p>\nAn example enabling it to run several different&nbsp;executables, add this to the end of the file:\n<\/p>\n<pre>\r\n<code>\r\nwww-data ALL=NOPASSWD: \/home\/pi\/some_executable_name, \/home\/pi\/some_executable_name some_option, \/home\/pi\/some_other_executable_name\r\n<\/code><\/pre>\n<h5>\nPassing $_POST Data To A Running Application<br \/>\n<\/h5>\n<p>\nThere is a really useful way of using the PHP system function to pass data to a running application on your RPi (e.g. your own application&nbsp;doing something with the IO) if you <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/pipes\/named-pipes-fifos\">create a FIFO within it<\/a>. &nbsp;Then you can use use a web page like this to pass it data from a form or $_POST input:\n<\/p>\n<pre>\r\n<code>\r\n-&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot; \/&gt;\r\n&lt;title&gt;My Page&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;?php\r\n\tif ($_POST['Command'])\r\n\t{\r\n\t\t$Command = trim($_POST['Command']);\r\n\t\techo &quot;Command received: $Command&lt;br \/&gt;&quot;;\r\n\t\tsystem(&quot;sudo sh -c &#39;echo \\&quot;&quot;. escapeshellarg($Command) . &quot;\\&quot; &gt; \/tmp\/my_fifo&#39;&quot;);\r\n\t}\r\n?&gt;\r\n\r\n&lt;div align=&quot;center&quot;&gt;\r\n  &lt;form id=&quot;form1&quot; name=&quot;form1&quot; method=&quot;post&quot; action=&quot;&quot;&gt;\r\n    &lt;p&gt;\r\n      &lt;label for=&quot;Command&quot;&gt;Command:&lt;\/label&gt;\r\n      &lt;input type=&quot;text&quot; name=&quot;Command&quot; id=&quot;Command&quot; size=&quot;80&quot; \/&gt;\r\n      &lt;input type=&quot;submit&quot; name=&quot;Send&quot; id=&quot;Send&quot; value=&quot;Send&quot; \/&gt;\r\n    &lt;\/p&gt;\r\n  &lt;\/form&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n\r\n<\/code><\/pre>\n<h5>\nEscaping user supplied data &#8211; IMPORTANT!<br \/>\n<\/h5>\n<p>\nWhen allowing user-supplied data to be passed to the system() function, ensure you use escapeshellarg() or escapeshellcmd() so that users can&#39;t trick the system into executing arbitrary commands.\n<\/p>\n<h4>\nConfirming which user apache is running as<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n&lt;?php\r\necho exec(&#39;whoami&#39;);\r\n?&gt;\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Issuing Commands From Web Pages If you install a PHP web server on your RPi you can issue commands using the PHP system() function. Examples: \/\/Echo some system status: echo &#39;&lt;pre&gt;&#39;; system(&#39;netstat -a&#39;); echo &#39;&lt;\/pre&gt;&#39;; \/\/Run an executable file: echo &#39;&lt;pre&gt;&#39;; system(&quot;sudo \/home\/pi\/projects\/my_project.a&quot;); \/\/system() will echo the output of the executable, use shell_exec() instead if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123,31,34],"tags":[],"class_list":["post-574","post","type-post","status-publish","format-standard","hentry","category-php-code-bits","category-tcpip","category-web-servers"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=574"}],"version-history":[{"count":26,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":2512,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/574\/revisions\/2512"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}