{"id":399,"date":"2016-02-16T19:08:14","date_gmt":"2016-02-16T19:08:14","guid":{"rendered":"http:\/\/www.xn--nrdoteket-l8a.dk\/?page_id=399"},"modified":"2016-03-17T09:02:33","modified_gmt":"2016-03-17T09:02:33","slug":"gds-iot-kildekode-control-php","status":"publish","type":"page","link":"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/","title":{"rendered":"GDS-IOT kildekode: control.php"},"content":{"rendered":"<p>Denne artikel er en st\u00f8tteartikel til serien om GDS-IOT, indeholdende kildekode.<\/p>\n<h1>control.php<\/h1>\n<script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- wp-inpost-add -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-7793103161450012\"\r\n     data-ad-slot=\"1741650889\"\r\n     data-ad-format=\"auto\"><\/ins>\r\n<script>\r\n(adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\n<blockquote><p>Serverside koden der implementerer API&#8217;et<\/p><\/blockquote>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n\/*\r\n * -----------------------------------------------------------------------------------\r\n * &quot;THE BEER-WARE LICENSE&quot; (Revision 42):\r\n * &lt;steen@ieee.org&gt; wrote this file.  As long as you retain this notice you\r\n * can do whatever you want with this stuff. If we meet some day, and you think\r\n * this stuff is worth it, you can buy me a beer in return.   Steen Garbers Enevoldsen\r\n * -----------------------------------------------------------------------------------\r\n *\/\r\n\r\n&lt;?php $realm = 'gds-iot'; $users = array('TypeUsernameHere' =&gt; 'TypePasswordHere');\r\n\r\nif (empty($_SERVER&#x5B;'PHP_AUTH_DIGEST'])) {\r\n  header('HTTP\/1.1 401 Unauthorized');\r\n    header('WWW-Authenticate: Digest realm=&quot;'.$realm.\r\n     '&quot;,qop=&quot;auth&quot;,nonce=&quot;'.uniqid().'&quot;,opaque=&quot;'.md5($realm).'&quot;');\r\n\r\n  die('UNAUTHORIZED');\r\n  }\r\n\r\n\/\/ analyze the PHP_AUTH_DIGEST variable\r\nif (!($data = http_digest_parse($_SERVER&#x5B;'PHP_AUTH_DIGEST'])) ||\r\n    !isset($users&#x5B;$data&#x5B;'username']]))\r\n      die('WRONG_CREDENTIALS');\r\n\r\n\/\/ generate the valid response\r\n$A1 = md5($data&#x5B;'username'] . ':' . $realm . ':' . $users&#x5B;$data&#x5B;'username']]);\r\n$A2 = md5($_SERVER&#x5B;'REQUEST_METHOD'].':'.$data&#x5B;'uri']);\r\n$valid_response = md5($A1.':'.$data&#x5B;'nonce'].':'.$data&#x5B;'nc'].':'.$data&#x5B;'cnonce'].':'.$data&#x5B;'qop'].':'.$A2);\r\n\r\nif ($data&#x5B;'response'] != $valid_response)\r\n  die('WRONG_CREDENTIALS');\r\n\r\n\/\/ ok, valid username &amp; password\r\n\r\n\/\/ function to parse the http auth header\r\nfunction http_digest_parse($txt)\r\n{\r\n  \/\/ protect against missing data\r\n    $needed_parts = array('nonce'=&gt;1, 'nc'=&gt;1, 'cnonce'=&gt;1, 'qop'=&gt;1, 'username'=&gt;1, 'uri'=&gt;1, 'response'=&gt;1);\r\n      $data = array();\r\n        $keys = implode('|', array_keys($needed_parts));\r\n\t  preg_match_all('@(' . $keys . ')=(?:(&#x5B;\\'&quot;])(&#x5B;^\\2]+?)\\2|(&#x5B;^\\s,]+))@', $txt, $matches, PREG_SET_ORDER);\r\n\r\n  foreach ($matches as $m) {\r\n    $data&#x5B;$m&#x5B;1]] = $m&#x5B;3] ? $m&#x5B;3] : $m&#x5B;4];\r\n    unset($needed_parts&#x5B;$m&#x5B;1]]);\r\n  }\r\n\r\n  return $needed_parts ? false : $data;\r\n}\r\n\r\n\/\/ User validated, now do the real stuff\r\n\r\n$action = $_GET&#x5B;'action'];\r\n$port = $_GET&#x5B;'device'];\r\n\r\n\/\/ We use GPIO#17 and GPIO#27 for controlling the relays\r\n\/\/ Configure the ports as output.\r\n$setmode17 = shell_exec(&quot;\/usr\/local\/bin\/gpio -g mode 17 out&quot;);\r\n$setmode27 = shell_exec(&quot;\/usr\/local\/bin\/gpio -g mode 27 out&quot;);\r\n\r\nswitch ($port)\r\n{\r\ncase 1:\r\n    $portreal=&quot;27&quot;; \/\/ Device#1 maps to GPIO#27\r\n    break;\r\ncase 2:\r\n    $portreal=&quot;17&quot;; \/\/ Device#2 maps to GPIO#17\r\n    break;\r\ndefault:\r\n    die('INVALID_DEVICE');\r\n    break;\r\n}\r\n\r\nswitch($action) \/\/ What are we asked to do?\r\n  {\r\n  case 'ON':\r\n    $gpio_on = shell_exec(&quot;\/usr\/local\/bin\/gpio -g write &quot; . $portreal . &quot; 1&quot;); \/\/ Turn on the desired GPIO\r\n    $gpio_stat = shell_exec(&quot;\/usr\/local\/bin\/gpio -g read &quot; . $portreal); \/\/ Read status of port\r\n    if ($gpio_stat &lt;&gt; 1)\r\n      {\r\n      echo &quot;SYSTEM_ERROR&quot;; \/\/ We turned on the port, but it reads as off, return System Error\r\n      }\r\n    else\r\n      {\r\n      echo &quot;DEVICE_&quot; . $port . &quot;_ON&quot;; \/\/ All is good, return OK\r\n      }\r\n    break;\r\n  case 'OFF':\r\n    $gpio_off = shell_exec(&quot;\/usr\/local\/bin\/gpio -g write &quot; . $portreal . &quot; 0&quot;); \/\/ Turn off the desired GPIO\r\n    $gpio_stat = shell_exec(&quot;\/usr\/local\/bin\/gpio -g read &quot; . $portreal); \/\/ Read port status\r\n    if ($gpio_stat &lt;&gt; 0)\r\n      {\r\n      echo &quot;SYSTEM_ERROR&quot;; \/\/ We asked to turn off, but port is still on, return system error.\r\n      }\r\n    else\r\n      {\r\n      echo &quot;DEVICE_&quot; . $port . &quot;_OFF&quot;; \/\/ All good, return confirm\r\n      }\r\n    break;\r\n  case 'STATUS': \/\/ We're asked to return status of port\r\n    $gpio_stat = shell_exec(&quot;\/usr\/local\/bin\/gpio -g read &quot; . $portreal); \/\/ Read status of port\r\n    switch ($gpio_stat)\r\n      {\r\n      case 0:\r\n      echo &quot;DEVICE_&quot; . $port . &quot;_OFF&quot;;\r\n      \/\/   echo &quot;OFF&quot;;\r\n      break;\r\n      case 1:\r\n      echo &quot;DEVICE_&quot; . $port . &quot;_ON&quot;;\r\n      \/\/   echo &quot;ON&quot;;\r\n      break;\r\n      default:\r\n      echo &quot;SYSTEM_ERROR&quot;;\r\n      }\r\n\r\n    break;\r\n  default:\r\n    echo &quot;API_ERROR&quot;;\r\n    break;\r\n  }\r\n\r\n?&gt;\r\n<\/pre>\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\r\n<!-- wp-ad-rect -->\r\n<ins class=\"adsbygoogle\"\r\n     style=\"display:block\"\r\n     data-ad-client=\"ca-pub-7793103161450012\"\r\n     data-ad-slot=\"8706514482\"\r\n     data-ad-format=\"auto\"><\/ins>\r\n<script>\r\n     (adsbygoogle = window.adsbygoogle || []).push({});\r\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Denne artikel er en st\u00f8tteartikel til serien om GDS-IOT, indeholdende kildekode. control.php Serverside koden der implementerer API&#8217;et \/* * &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; * &quot;THE BEER-WARE LICENSE&quot; (Revision 42): * &lt;steen@ieee.org&gt; wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you &hellip; <a href=\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/\" class=\"more-link\">L\u00e6s videre<span class=\"screen-reader-text\"> &#8220;GDS-IOT kildekode: control.php&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-399","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>GDS-IOT kildekode: control.php - N\u00f8rd&#039;o&#039;teket<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/\" \/>\n<meta property=\"og:locale\" content=\"da_DK\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"GDS-IOT kildekode: control.php - N\u00f8rd&#039;o&#039;teket\" \/>\n<meta property=\"og:description\" content=\"Denne artikel er en st\u00f8tteartikel til serien om GDS-IOT, indeholdende kildekode. control.php Serverside koden der implementerer API&#8217;et \/* * ----------------------------------------------------------------------------------- * &quot;THE BEER-WARE LICENSE&quot; (Revision 42): * &lt;steen@ieee.org&gt; wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you &hellip; L\u00e6s videre &quot;GDS-IOT kildekode: control.php&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/\" \/>\n<meta property=\"og:site_name\" content=\"N\u00f8rd&#039;o&#039;teket\" \/>\n<meta property=\"article:modified_time\" content=\"2016-03-17T09:02:33+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimeret l\u00e6setid\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutter\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/\",\"url\":\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/\",\"name\":\"GDS-IOT kildekode: control.php - N\u00f8rd&#039;o&#039;teket\",\"isPartOf\":{\"@id\":\"https:\/\/noerdoteket.dk\/#website\"},\"datePublished\":\"2016-02-16T19:08:14+00:00\",\"dateModified\":\"2016-03-17T09:02:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/#breadcrumb\"},\"inLanguage\":\"da-DK\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Hjem\",\"item\":\"https:\/\/noerdoteket.dk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"GDS-IOT kildekode: control.php\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/noerdoteket.dk\/#website\",\"url\":\"https:\/\/noerdoteket.dk\/\",\"name\":\"N\u00f8rd&#039;o&#039;teket\",\"description\":\"L\u00f8st og fast om teknologi og dimser\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/noerdoteket.dk\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"da-DK\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"GDS-IOT kildekode: control.php - N\u00f8rd&#039;o&#039;teket","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/","og_locale":"da_DK","og_type":"article","og_title":"GDS-IOT kildekode: control.php - N\u00f8rd&#039;o&#039;teket","og_description":"Denne artikel er en st\u00f8tteartikel til serien om GDS-IOT, indeholdende kildekode. control.php Serverside koden der implementerer API&#8217;et \/* * ----------------------------------------------------------------------------------- * &quot;THE BEER-WARE LICENSE&quot; (Revision 42): * &lt;steen@ieee.org&gt; wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you &hellip; L\u00e6s videre \"GDS-IOT kildekode: control.php\"","og_url":"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/","og_site_name":"N\u00f8rd&#039;o&#039;teket","article_modified_time":"2016-03-17T09:02:33+00:00","twitter_card":"summary_large_image","twitter_misc":{"Estimeret l\u00e6setid":"3 minutter"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/","url":"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/","name":"GDS-IOT kildekode: control.php - N\u00f8rd&#039;o&#039;teket","isPartOf":{"@id":"https:\/\/noerdoteket.dk\/#website"},"datePublished":"2016-02-16T19:08:14+00:00","dateModified":"2016-03-17T09:02:33+00:00","breadcrumb":{"@id":"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/#breadcrumb"},"inLanguage":"da-DK","potentialAction":[{"@type":"ReadAction","target":["https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/noerdoteket.dk\/index.php\/gds-iot-kildekode-control-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Hjem","item":"https:\/\/noerdoteket.dk\/"},{"@type":"ListItem","position":2,"name":"GDS-IOT kildekode: control.php"}]},{"@type":"WebSite","@id":"https:\/\/noerdoteket.dk\/#website","url":"https:\/\/noerdoteket.dk\/","name":"N\u00f8rd&#039;o&#039;teket","description":"L\u00f8st og fast om teknologi og dimser","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/noerdoteket.dk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"da-DK"}]}},"_links":{"self":[{"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/pages\/399","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/comments?post=399"}],"version-history":[{"count":3,"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/pages\/399\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/pages\/399\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/noerdoteket.dk\/index.php\/wp-json\/wp\/v2\/media?parent=399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}