From 972737ec181f32dee92e828d3888e42624e4aee4 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Tue, 25 Aug 2015 18:46:25 +0000 Subject: [PATCH] (legacy) refactor firewall_schedule_edit.php --- src/www/firewall_schedule_edit.php | 2035 ++++++++++++++-------------- 1 file changed, 989 insertions(+), 1046 deletions(-) diff --git a/src/www/firewall_schedule_edit.php b/src/www/firewall_schedule_edit.php index 524f6ee31..315b67aef 100644 --- a/src/www/firewall_schedule_edit.php +++ b/src/www/firewall_schedule_edit.php @@ -1,30 +1,30 @@ . - All rights reserved. + Copyright (C) 2014-2015 Deciso B.V. + Copyright (C) 2004 Scott Ullrich + Copyright (C) 2003-2004 Manuel Kasper . + All rights reserved. - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ require_once("guiconfig.inc"); @@ -63,182 +63,158 @@ function is_schedule_inuse($schedule) function schedulecmp($a, $b) { - return strcmp($a['name'], $b['name']); + return strcmp($a['name'], $b['name']); } function schedule_sort(){ - global $g, $config; + global $g, $config; - if (!is_array($config['schedules']['schedule'])) - return; + if (!is_array($config['schedules']['schedule'])) + return; - usort($config['schedules']['schedule'], "schedulecmp"); + usort($config['schedules']['schedule'], "schedulecmp"); } -$pgtitle = array(gettext("Firewall"),gettext("Schedules"),gettext("Edit")); - -$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/firewall_schedule.php'); - $dayArray = array (gettext('Mon'),gettext('Tues'),gettext('Wed'),gettext('Thur'),gettext('Fri'),gettext('Sat'),gettext('Sun')); $monthArray = array (gettext('January'),gettext('February'),gettext('March'),gettext('April'),gettext('May'),gettext('June'),gettext('July'),gettext('August'),gettext('September'),gettext('October'),gettext('November'),gettext('December')); -if (!is_array($config['schedules']['schedule'])) - $config['schedules']['schedule'] = array(); +if (!isset($config['schedules']['schedule'])) { + $config['schedules']['schedule'] = array(); +} $a_schedules = &$config['schedules']['schedule']; -if (is_numericint($_GET['id'])) - $id = $_GET['id']; -if (isset($_POST['id']) && is_numericint($_POST['id'])) - $id = $_POST['id']; +if ($_SERVER['REQUEST_METHOD'] === 'GET') { + // input record id, if valid + if (!empty($_GET['name'])) { + foreach ($a_schedules as $i => $sched) { + if ($sched['name'] == $_GET['name']) { + $id = $i; + break; + } + } + } elseif (isset($_GET['dup']) && isset($a_schedules[$_GET['dup']])) { + $configId = $_GET['dup']; + } elseif (isset($_GET['id']) && isset($a_schedules[$_GET['id']])) { + $id = $_GET['id']; + $configId = $id; + } + $pconfig['name'] = $a_schedules[$configId]['name']; + $pconfig['descr'] = $a_schedules[$configId]['descr']; + $pconfig['timerange'] = isset($a_schedules[$configId]['timerange']) ? $a_schedules[$configId]['timerange'] : array(); + $pconfig['schedlabel'] = isset($a_schedules[$configId]['schedlabel']) ? $a_schedules[$configId]['schedlabel'] : uniqid(); +} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (isset($_POST['id']) && isset($a_schedules[$_POST['id']])) { + $id = $_POST['id']; + } + $pconfig = $_POST; -// quick hack, cleanup follows -if (!empty($_GET['name'])) { - foreach ($a_schedules as $i => $sched) { - if ($sched['name'] == $_GET['name']) { - $id = $i; - break; - } - } + // validate + if(strtolower($pconfig['name']) == "lan") + $input_errors[] = gettext("Schedule may not be named LAN."); + if(strtolower($pconfig['name']) == "wan") + $input_errors[] = gettext("Schedule may not be named WAN."); + if(strtolower($pconfig['name']) == "") + $input_errors[] = gettext("Schedule name cannot be blank."); + $x = is_validaliasname($pconfig['name']); + if (!isset($x)) { + $input_errors[] = gettext("Reserved word used for schedule name."); + } elseif ($x == false) { + $input_errors[] = gettext("The schedule name may only consist of the characters a-z, A-Z, 0-9"); + } + /* check for name conflicts */ + foreach ($a_schedules as $schedId => $schedule) { + if ( $schedId != $id && $schedule['name'] == $pconfig['name']) { + $input_errors[] = gettext("A Schedule with this name already exists."); + break; + } + } + + // parse time ranges + $pconfig['timerange'] = array(); + + $timerangeFound = false; + for ($x=0; $x<99; $x++){ + if($pconfig['schedule' . $x]) { + if (!preg_match('/^[0-9]+:[0-9]+$/', $pconfig['starttime' . $x])) { + $input_errors[] = sprintf(gettext("Invalid start time - '%s'"), $pconfig['starttime' . $x]); + continue; + } + if (!preg_match('/^[0-9]+:[0-9]+$/', $pconfig['stoptime' . $x])) { + $input_errors[] = sprintf(gettext("Invalid stop time - '%s'"), $pconfig['stoptime' . $x]); + continue; + } + $timerangeFound = true; + $timeparts = array(); + $firstprint = false; + $timestr = $pconfig['schedule' . $x]; + $timehourstr = $pconfig['starttime' . $x]; + $timehourstr .= "-"; + $timehourstr .= $pconfig['stoptime' . $x]; + $timedescrstr = htmlentities($pconfig['timedescr' . $x], ENT_QUOTES, 'UTF-8'); + $dashpos = strpos($timestr, '-'); + if ($dashpos === false) { + $timeparts['position'] = $timestr; + } else { + $tempindarray = array(); + $monthstr = ""; + $daystr = ""; + $tempindarray = explode(",", $timestr); + foreach ($tempindarray as $currentselection) { + if ($currentselection){ + if ($firstprint) { + $monthstr .= ","; + $daystr .= ","; + } + $tempstr = ""; + $monthpos = strpos($currentselection, "m"); + $daypos = strpos($currentselection, "d"); + $monthstr .= substr($currentselection, $monthpos+1, $daypos-$monthpos-1); + $daystr .= substr($currentselection, $daypos+1); + $firstprint = true; + } + } + $timeparts['month'] = $monthstr; + $timeparts['day'] = $daystr; + } + $timeparts['hour'] = $timehourstr; + $timeparts['rangedescr'] = $timedescrstr; + $pconfig['timerange'][$x] = $timeparts; + } + } + + if (count($pconfig['timerange']) == 0) { + $input_errors[] = gettext("The schedule must have at least one time range configured."); + } + + if (count($input_errors) == 0) { + $schedule = array(); + $schedule['name'] = $pconfig['name']; + $schedule['descr'] = $pconfig['descr']; + $schedule['timerange'] = $pconfig['timerange']; + $schedule['schedlabel'] = $pconfig['schedlabel']; + + if (isset($id)) { + $a_schedules[$id] = $schedule; + } else { + $a_schedules[] = $schedule; + } + schedule_sort(); + if (write_config()) { + filter_configure(); + } + + header("Location: firewall_schedule.php"); + exit; + } } -if (isset($id) && $a_schedules[$id]) { - $pconfig['name'] = $a_schedules[$id]['name']; - $pconfig['descr'] = html_entity_decode($a_schedules[$id]['descr']); - $pconfig['timerange'] = $a_schedules[$id]['timerange']; - $pconfig['schedlabel'] = $a_schedules[$id]['schedlabel']; - $getSchedule = true; -} -if ($_POST) { +$pgtitle = array(gettext("Firewall"),gettext("Schedules"),gettext("Edit")); +legacy_html_escape_form_data($pconfig); - if(strtolower($_POST['name']) == "lan") - $input_errors[] = gettext("Schedule may not be named LAN."); - if(strtolower($_POST['name']) == "wan") - $input_errors[] = gettext("Schedule may not be named WAN."); - if(strtolower($_POST['name']) == "") - $input_errors[] = gettext("Schedule name cannot be blank."); - - $x = is_validaliasname($_POST['name']); - if (!isset($x)) { - $input_errors[] = gettext("Reserved word used for schedule name."); - } else { - if (is_validaliasname($_POST['name']) == false) - $input_errors[] = gettext("The schedule name may only consist of the characters a-z, A-Z, 0-9"); - } - - /* check for name conflicts */ - foreach ($a_schedules as $schedule) { - if (isset($id) && ($a_schedules[$id]) && ($a_schedules[$id] === $schedule)) - continue; - - if ($schedule['name'] == $_POST['name']) { - $input_errors[] = gettext("A Schedule with this name already exists."); - break; - } - } - $schedule = array(); - - $schedule['name'] = $_POST['name']; - $schedule['descr'] = htmlentities($_POST['descr'], ENT_QUOTES, 'UTF-8'); - - $timerangeFound = false; - for ($x=0; $x<99; $x++){ - if($_POST['schedule' . $x]) { - if (!preg_match('/^[0-9]+:[0-9]+$/', $_POST['starttime' . $x])) { - $input_errors[] = sprintf(gettext("Invalid start time - '%s'"), $_POST['starttime' . $x]); - continue; - } - if (!preg_match('/^[0-9]+:[0-9]+$/', $_POST['stoptime' . $x])) { - $input_errors[] = sprintf(gettext("Invalid stop time - '%s'"), $_POST['stoptime' . $x]); - continue; - } - $timerangeFound = true; - $timeparts = array(); - $firstprint = false; - $timestr = $_POST['schedule' . $x]; - $timehourstr = $_POST['starttime' . $x]; - $timehourstr .= "-"; - $timehourstr .= $_POST['stoptime' . $x]; - $timedescrstr = htmlentities($_POST['timedescr' . $x], ENT_QUOTES, 'UTF-8'); - $dashpos = strpos($timestr, '-'); - if ($dashpos === false) - { - $timeparts['position'] = $timestr; - } - else - { - $tempindarray = array(); - $monthstr = ""; - $daystr = ""; - $tempindarray = explode(",", $timestr); - foreach ($tempindarray as $currentselection) - { - if ($currentselection){ - if ($firstprint) - { - $monthstr .= ","; - $daystr .= ","; - } - $tempstr = ""; - $monthpos = strpos($currentselection, "m"); - $daypos = strpos($currentselection, "d"); - $monthstr .= substr($currentselection, $monthpos+1, $daypos-$monthpos-1); - $daystr .= substr($currentselection, $daypos+1); - $firstprint = true; - } - } - $timeparts['month'] = $monthstr; - $timeparts['day'] = $daystr; - } - $timeparts['hour'] = $timehourstr; - $timeparts['rangedescr'] = $timedescrstr; - $schedule['timerange'][$x] = $timeparts; - } - } - - if (!$timerangeFound) - $input_errors[] = gettext("The schedule must have at least one time range configured."); - - if (!$input_errors) { - - if (!empty($pconfig['schedlabel'])) - $schedule['schedlabel'] = $pconfig['schedlabel']; - else - $schedule['schedlabel'] = uniqid(); - - if (isset($id) && $a_schedules[$id]){ - $a_schedules[$id] = $schedule; - } - else{ - $a_schedules[] = $schedule; - } - schedule_sort(); - if (write_config()) - filter_configure(); - - header("Location: firewall_schedule.php"); - exit; - - } - //we received input errors, copy data to prevent retype - else - { - if (!$_POST['schedule0']) - $getSchedule = false; - else - $getSchedule = true; - $pconfig['name'] = $schedule['name']; - $pconfig['descr'] = $schedule['descr']; - $pconfig['timerange'] = $schedule['timerange']; - } - -} include("head.inc"); - -/* put your custom HTML head content here */ -/* using some of the new function calls */ -$jscriptstr = << -EOD; -?> + +
+
+
+ 0) print_input_errors($input_errors); ?> +
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ + + +

+ +

+ + + + +
+
+ +
+ +

+ +
" style=" position:relative; display:"> + + + + + + + + + + + + + + +"; + } + if ($firstdayofmonth == $positioncounter){?> + "; + } + if ($positioncounter == 7 || $daycounter > $numberofdays) { + $positioncounter = 1; + echo ""; + } else { + $positioncounter++; + } + }//end while loop?> + +
+ "; + } elseif ($firstdayprinted == TRUE && $daycounter <= $numberofdays){?> + + "; + } else { + echo "
+
+ + +
+ + + + + + + + + +
+
+ + +
+
+
+ + +
+
+ +
+ + +
  + " class="btn btn-default" onclick="javascript:processEntries();" />    + " class="btn btn-default" onclick="javascript:clearCalendar(); clearTime(); clearDescr();" /> +
+ + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + $starttime = substr ($temptimerange, 0, $temptimeseparator); + $stoptime = substr ($temptimerange, $temptimeseparator+1); + $currentDay = ""; + $firstDay = ""; + $nextDay = ""; + $foundEnd = false; + $firstDayFound = false; + $firstPrint = false; + $firstprint2 = false; + if (!empty($timerange['month'])){ + $tempmontharray = explode(",", $timerange['month']); + $tempdayarray = explode(",",$timerange['day']); + $arraycounter = 0; + foreach ($tempmontharray as $monthtmp){ + $month = $tempmontharray[$arraycounter]; + $day = $tempdayarray[$arraycounter]; + $daypos = date("w", mktime(0, 0, 0, date($month), date($day), date("Y"))); + //if sunday, set position to 7 to get correct week number. This is due to php limitations on ISO-8601. When we move to php5.1 we can change this. + if ($daypos == 0){ + $daypos = 7; + } + $weeknumber = date("W", mktime(0, 0, 0, date($month), date($day), date("Y"))); + $weeknumber = ltrim($weeknumber, "0"); + if ($firstPrint) { + $tempID .= ","; + } + $tempID .= "w" . $weeknumber . "p" . $daypos . "-m" . $month . "d" . $day; + $firstPrint = true; + if (!$firstDayFound) { + $firstDay = $day; + $firstmonth = $month; + $firstDayFound = true; + } -
-
-
+ $currentDay = $day; + $nextDay = $tempdayarray[$arraycounter+1]; + $currentDay++; + if (($currentDay != $nextDay) || ($tempmontharray[$arraycounter] != $tempmontharray[$arraycounter+1])){ + if ($firstprint2) { + $tempFriendlyTime .= ", "; + } + $currentDay--; + if ($currentDay != $firstDay) { + $tempFriendlyTime .= $monthArray[$firstmonth-1] . " " . $firstDay . " - " . $currentDay ; + } else { + $tempFriendlyTime .= $monthArray[$month-1] . " " . $day; + } + $firstDayFound = false; + $firstprint2 = true; + } + $arraycounter++; + } + } else { + $dayFriendly = $timerange['position']; + $tempID = $dayFriendly; + } + $tempTime = $tempID . "||" . $starttime . "-" . $stoptime . "||" . $timedescr; - 0) print_input_errors($input_errors); ?> -
- - -
- -
- -
-

-
- -
- - - -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - -

- -

- -
- - - - -

- - - - -
-

- -
" style=" position:relative; display:"> - - - - - - - - - - - - - - - "; - } - if ($firstdayofmonth == $positioncounter){?> - "; - } - - if ($positioncounter == 7 || $daycounter > $numberofdays){ - $positioncounter = 1; - echo ""; - } - else{ - $positioncounter++; - } - - }//end while loop?> - -
- "; - } - elseif ($firstdayprinted == TRUE && $daycounter <= $numberofdays){?> - - "; - } - else - { - echo "
-
- -
- -
- - - - - - - - - -
-     -   - -     -   -

- -

- - - -
  - " class="btn btn-default" onclick="javascript:processEntries();" />    - " class="btn btn-default" onclick="javascript:clearCalendar(); clearTime(); clearDescr();" /> -
- - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - -
-
  - " /> - " onclick="window.location.href=''" /> - - - -
- - - - - + //following code makes the days friendly appearing, IE instead of Mon, Tues, Wed it will show Mon - Wed + $foundEnd = false; + $firstDayFound = false; + $firstprint = false; + $tempFriendlyDayArray = explode(",", $dayFriendly); + $currentDay = ""; + $firstDay = ""; + $nextDay = ""; + $i = 0; + if (empty($timerange['month'])) { + foreach ($tempFriendlyDayArray as $day){ + if ($day != ""){ + if (!$firstDayFound) { + $firstDay = $tempFriendlyDayArray[$i]; + $firstDayFound = true; + } + $currentDay =$tempFriendlyDayArray[$i]; + //get next day + $nextDay = $tempFriendlyDayArray[$i+1]; + $currentDay++; + if ($currentDay != $nextDay){ + if ($firstprint){ + $tempFriendlyTime .= ", "; + } + $currentDay--; + if ($currentDay != $firstDay) { + $tempFriendlyTime .= $dayArray[$firstDay-1] . " - " . $dayArray[$currentDay-1]; + } else { + $tempFriendlyTime .= $dayArray[$firstDay-1]; + } + $firstDayFound = false; + $firstprint = true; + } + $i++; + } + } + } +?> +
+ + + + + + + + + + + + + +
+ + + +   + + " /> + " onclick="window.location.href=''" /> + + + + + + + +
+
+
+