Menu="NetworkSettings:1000"
Title="_(Wireless)_ wlan0"
Tag="fa-wifi"
Cond="file_exists('/sys/class/net/wlan0')"
---
<?PHP
/* Copyright 2005-2025, Lime Technology
 * Copyright 2012-2025, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$title   = _('Connect to WiFi network');
$cfg     = '/boot/config/wireless.cfg';
$regions = file("$docroot/webGui/include/regions.key", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (file_exists($cfg)) $wlan0 = parse_ini_file($cfg,true)['wlan0'];
?>
<form markdown="1" name="wifi_settings" method="POST" action="/update.php" target="progressFrame" onsubmit="enable_wifi(this.WIFI.value)">
<input type="hidden" name="#file" value="<?=$cfg?>"/>
<input type="hidden" name="#section" value="wlan0"/>
<input type="hidden" name="#command" value="/webGui/scripts/wireless">
<input type="hidden" name="#arg[1]" value=""/>

<div markdown="1">
_(Wi-Fi)_:
: <select name="WIFI">
  <?=mk_option(_var($wlan0,'WIFI'), "no", _("Disabled"))?>
  <?=mk_option(_var($wlan0,'WIFI'), "yes", _("Enabled"))?>
  </select>
</div>

<div markdown="1">
_(Regulatory region)_:
: <select name="REGION" onchange="showManual(this.value)">
  <?=mk_option(_var($wlan0,'REGION'), "", _("Automatic"))?>
  <?=mk_option(_var($wlan0,'REGION'), "00", _("Manual"))?>
  <?=mk_option(_var($wlan0,'REGION'), "--", "--------", 'disabled')?>
  <?foreach ($regions as $region):?>
    <?[$code, $country] = my_explode('|', $region);?>
    <?=mk_option(_var($wlan0,'REGION'), $code, _($country))?>
  <?endforeach;?>
  </select>
</div>

<div markdown="1" id="manual" class="hide">
_(Manual input)_ [XX]:
: <input type="text" name="REGION_XX" value="<?=_var($wlan0,'REGION_XX')?>" minlength="2" maxlength="2" title="_(Two letter country code)_ (ISO 3166)">
</div>

&nbsp;
: <span class="flex flex-row flex-wrap items-center gap-2">
    <input type="submit" value="_(Apply)_" disabled>
    <input type="button" value="_(Done)_" onclick="done()">
  </span>
</form>

<div id="wifi" class="hide">
<div markdown="1" id="connected" class="shade">
_(Connected)_:
: <span class="inline-block">
    <i class="fa fa-spin fa-circle-o-notch"></i>
  </span>

</div>
<div markdown="1" id="my_networks" class="shade">
_(My networks)_:
: <span class="inline-block">
    <i class="fa fa-spin fa-circle-o-notch"></i>
  </span>

</div>
<div markdown="1" id="other_networks" class="shade">
_(Other networks)_:
: <span class="inline-block">
    <i class="fa fa-spin fa-circle-o-notch"></i>
  </span>

</div>

<div markdown="1" class="shade">
_(Add network)_:
: <span>
    <input name="CUSTOM" type="text" class="narrow" maxlength="32" autocomplete="off" spellcheck="false" value="">
    <i class="fa fa-wifi hand grey-text" onclick="manage_wifi(encodeURIComponent($('input[name=CUSTOM]').val()),3)" title="<?=$title?>"></i>
  </span>

</div>
</div>

<script>
function enable_wifi(state) {
  $('input[name="#arg[1]"]').val(state);
}

function update_wifi(load) {
  $.post('/webGui/include/Wireless.php',{cmd:'list',load:load},function(text) {
    if (text.length > 0) {
      var wifi = JSON.parse(text);
      $('#connected').html(wifi.active);
      $('#my_networks').html(wifi.saved);
      if (typeof wifi.other == 'string') $('#other_networks').html(wifi.other);
    }
  });
  timers.wifi = setTimeout(update_wifi,6000);
}

function manage_wifi(ssid,task) {
  if (task == 2) {
    clearTimeout(timers.wifi);
    $.post('/webGui/include/Wireless.php',{cmd:'forget',ssid:ssid},function(){
      swal.close();
      setTimeout(function(){update_wifi(1);});
    });
    return;
  }
  if (ssid) {
    $.post('/webGui/include/Wireless.php',{cmd:'join',ssid:ssid,task:task},function(text) {
      swal({title:decodeURIComponent(ssid),text:text,animation:'none',html:true,confirmButtonText:"_(Join this network)_",showCancelButton:true,cancelButtonText:"_(Cancel)_"},function(join){
        if (join) $('form[name=wifi]').submit();
      });
    });
  } else {
    swal({title:"_(Missing Network Name)_",animation:'none',html:true,type:'error',confirmButtonText:"_(Ok)_"});
  }
}

function showManual(val) {
  if (val == '00') {$('#manual').show(); $('input[name="REGION_XX"]').focus()} else $('#manual').hide();
}

function showDHCP(val, v) {
  if (val == 'no') {
    $('tr.static'+v).show();
    $('select[name=DNS'+v+']').val('yes');
    $('select[name=DNS'+v+'] option:eq(0)').prop('disabled',true);
    if (v == '6') $('tr.dns6').show();
    $('tr.server'+v).show();
  } else if (val == 'yes') {
    $('tr.static'+v).hide();
    $('select[name=DNS'+v+']').val('no');
    $('select[name=DNS'+v+'] option:eq(0)').prop('disabled',false);
    if (v == '6') $('tr.dns6').show();
    $('tr.server'+v).hide();
  } else {
    $('tr.static'+v).hide();
    $('tr.dns'+v).hide();
    $('tr.server'+v).hide();
  }
}

function showDNS(val, v) {
  if (val == 'no') $('tr.server'+v).hide();
  if (val == 'yes') $('tr.server'+v).show();
}

function showPassword() {
  if ($('#showPass').hasClass('checked')) {
    $('#showPass').removeClass('checked fa-eye-slash').addClass('fa-eye');
    var view = 'password';
  } else {
    $('#showPass').addClass('checked fa-eye-slash').removeClass('fa-eye');
    var view = 'text';
  }
  $('input[name="PASSWORD"]').attr('type',view);
}

function showSecurity(val) {
  if (val == 'open') {
    $('#username,#password').hide();
  } else if (val.substr(0,4) == 'IEEE' || val == 'auto') {
    $('#username,#password').show();
  } else {
    $('#username').hide();
    $('#password').show();
  }
}

<?if (_var($wlan0,'WIFI')=='yes'):?>
$(function() {
  $('#wifi').show();
  update_wifi(1);
});
<?endif;?>
</script>
