firewall_schedule_edit.php 41.3 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1 2
<?php
/*
3
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
	Copyright (C) 2004 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
	All rights reserved.

	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.

	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.
*/

30
require_once("guiconfig.inc");
31
require_once("filter.inc");
32 33


34 35 36 37 38 39 40
/****f* legacy/is_schedule_inuse
 * NAME
 *   checks to see if a schedule is currently in use by a rule
 * INPUTS
 *
 * RESULT
 *   true or false
41
 * NOTES
42
 *
43
 ******/
44 45 46 47 48 49
function is_schedule_inuse($schedule)
{
        global $config;

        if ($schedule == '') {
                return false;
50
        }
51 52 53 54 55 56 57 58

        /* loop through firewall rules looking for schedule in use */
        if (isset($config['filter']['rule'])) {
                foreach ($config['filter']['rule'] as $rule) {
                        if ($rule['sched'] == $schedule) {
                                return true;
                        }
                }
59
        }
60 61 62 63 64

        return false;
}


Ad Schellevis's avatar
Ad Schellevis committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
function schedulecmp($a, $b) {
	return strcmp($a['name'], $b['name']);
}

function schedule_sort(){
        global $g, $config;

        if (!is_array($config['schedules']['schedule']))
                return;

        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();

$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 (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) {
104

Ad Schellevis's avatar
Ad Schellevis committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
	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");
	}
119

Ad Schellevis's avatar
Ad Schellevis committed
120 121 122 123 124 125 126 127 128 129 130
	/* 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();
131

Ad Schellevis's avatar
Ad Schellevis committed
132
	$schedule['name'] = $_POST['name'];
133 134
	$schedule['descr'] = htmlentities($_POST['descr'], ENT_QUOTES, 'UTF-8');

Ad Schellevis's avatar
Ad Schellevis committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	$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];
153
			$timedescrstr = htmlentities($_POST['timedescr' . $x], ENT_QUOTES, 'UTF-8');
Ad Schellevis's avatar
Ad Schellevis committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
			$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 .= ",";
171
							$daystr .= ",";
Ad Schellevis's avatar
Ad Schellevis committed
172 173 174 175 176
						}
						$tempstr = "";
						$monthpos = strpos($currentselection, "m");
						$daypos = strpos($currentselection, "d");
						$monthstr .= substr($currentselection, $monthpos+1, $daypos-$monthpos-1);
177
						$daystr .=  substr($currentselection, $daypos+1);
Ad Schellevis's avatar
Ad Schellevis committed
178 179 180 181 182
						$firstprint = true;
					}
				}
				$timeparts['month'] = $monthstr;
				$timeparts['day'] = $daystr;
183
			}
Ad Schellevis's avatar
Ad Schellevis committed
184 185 186 187 188
			$timeparts['hour'] = $timehourstr;
			$timeparts['rangedescr'] = $timedescrstr;
			$schedule['timerange'][$x] = $timeparts;
		}
	}
189

Ad Schellevis's avatar
Ad Schellevis committed
190 191
	if (!$timerangeFound)
		$input_errors[] = gettext("The schedule must have at least one time range configured.");
192 193 194

	if (!$input_errors) {

Ad Schellevis's avatar
Ad Schellevis committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		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;
212

Ad Schellevis's avatar
Ad Schellevis committed
213 214 215 216 217 218 219 220 221 222 223
	}
	//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'];
224
	}
Ad Schellevis's avatar
Ad Schellevis committed
225 226 227 228 229

}
include("head.inc");

/* put your custom HTML head content here        */
230
/* using some of the new function calls */
Ad Schellevis's avatar
Ad Schellevis committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
$jscriptstr = <<<EOD
<script type="text/javascript">
//<![CDATA[
var daysSelected = "";
var month_array = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var day_array = ['Mon','Tues','Wed','Thur','Fri','Sat','Sun'];
var schCounter = 0;

function rgb2hex(rgb) {
	var parts = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	if (parts == null)
		return;
	function hex(x) {
		return ("0" + parseInt(x).toString(16)).slice(-2);
	}
	return ("#" + hex(parts[1]) + hex(parts[2]) + hex(parts[3])).toUpperCase();
}

function repeatExistingDays(){
	var tempstr, tempstrdaypos, week, daypos, dayposdone = "";
251

Ad Schellevis's avatar
Ad Schellevis committed
252 253 254 255 256 257
	var dayarray = daysSelected.split(",");
	for (i=0; i<=dayarray.length; i++){
		tempstr = dayarray[i];
		tempstrdaypos = tempstr.search("p");
		week = tempstr.substring(1,tempstrdaypos);
		week = parseInt(week);
258
		dashpos = tempstr.search("-");
Ad Schellevis's avatar
Ad Schellevis committed
259 260
		daypos = tempstr.substring(tempstrdaypos+1, dashpos);
		daypos = parseInt(daypos);
261

Ad Schellevis's avatar
Ad Schellevis committed
262 263 264 265 266 267 268 269 270 271
		daydone = dayposdone.search(daypos);
		tempstr = 'w' + week + 'p' + daypos;
		daycell = eval('document.getElementById(tempstr)');
		if (daydone == "-1"){
			if (rgb2hex(daycell.style.backgroundColor) == "#F08080")  // lightcoral
				daytogglerepeating(week,daypos,true);
			else
				daytogglerepeating(week,daypos,false);
			dayposdone += daypos + ",";
		}
272
	}
Ad Schellevis's avatar
Ad Schellevis committed
273 274 275 276 277
}

function daytogglerepeating(week,daypos,bExists){
	var tempstr, daycell, dayoriginal = "";
	for (j=1; j<=53; j++)
278
	{
Ad Schellevis's avatar
Ad Schellevis committed
279 280 281
		tempstr = 'w' + j + 'p' + daypos;
		daycell = eval('document.getElementById(tempstr)');
		dayoriginalpos =  daysSelected.indexOf(tempstr);
282

Ad Schellevis's avatar
Ad Schellevis committed
283
		//if bExists set to true, means cell is already select it
284 285
		//unselect it and remove original day from daysSelected string

Ad Schellevis's avatar
Ad Schellevis committed
286 287
		if (daycell != null)
		{
288
			if (bExists){
Ad Schellevis's avatar
Ad Schellevis committed
289 290 291 292
				daycell.style.backgroundColor = "#FFFFFF";  // white
			}
			else
			{
293 294 295
				daycell.style.backgroundColor = "#F08080";  // lightcoral
			}

Ad Schellevis's avatar
Ad Schellevis committed
296
			if (dayoriginalpos != "-1")
297
			{
Ad Schellevis's avatar
Ad Schellevis committed
298 299 300
				dayoriginalend = daysSelected.indexOf(',', dayoriginalpos);
				tempstr = daysSelected.substring(dayoriginalpos, dayoriginalend+1);
				daysSelected = daysSelected.replace(tempstr, "");
301 302 303 304

			}
		}
	}
Ad Schellevis's avatar
Ad Schellevis committed
305 306 307 308 309
}

function daytoggle(id) {
	var runrepeat, tempstr = "";
	var bFoundValid = false;
310

Ad Schellevis's avatar
Ad Schellevis committed
311 312 313 314
	iddashpos = id.search("-");
	var tempstrdaypos = id.search("p");
	var week = id.substring(1,tempstrdaypos);
	week = parseInt(week);
315

Ad Schellevis's avatar
Ad Schellevis committed
316 317 318 319
	if (iddashpos == "-1")
	{
		idmod = id;
		runrepeat = true;
320
		var daypos = id.substr(tempstrdaypos+1);
Ad Schellevis's avatar
Ad Schellevis committed
321 322
	}
	else
323
	{
Ad Schellevis's avatar
Ad Schellevis committed
324 325 326
		idmod = id.substring(0,iddashpos);
		var daypos = id.substring(tempstrdaypos+1,iddashpos);
	}
327

Ad Schellevis's avatar
Ad Schellevis committed
328
	daypos = parseInt(daypos);
329

Ad Schellevis's avatar
Ad Schellevis committed
330
	while (!bFoundValid){
331 332
		var daycell = document.getElementById(idmod);

Ad Schellevis's avatar
Ad Schellevis committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
		if (daycell != null){
			if (rgb2hex(daycell.style.backgroundColor) == "#FF0000"){  // red
				daycell.style.backgroundColor = "#FFFFFF";  // white
				str = id + ",";
				daysSelected = daysSelected.replace(str, "");
			}
			else if (rgb2hex(daycell.style.backgroundColor) == "#F08080")  // lightcoral
			{
				daytogglerepeating(week,daypos,true);
			}
			else //color is white cell
			{
				if (!runrepeat)
				{
					daycell.style.backgroundColor = "#FF0000";  // red
				}
				else
				{
					daycell.style.backgroundColor = "#F08080";  // lightcoral
352
					daytogglerepeating(week,daypos,false);
Ad Schellevis's avatar
Ad Schellevis committed
353 354 355 356 357 358 359 360 361 362
				}
				daysSelected += id + ",";
			}
			bFoundValid = true;
		}
		else
		{
			//we found an invalid cell when column was clicked, move up to the next week
			week++;
			tempstr = "w" + week + "p" + daypos;
363
			idmod = tempstr;
Ad Schellevis's avatar
Ad Schellevis committed
364 365 366 367 368 369 370 371 372 373 374
		}
	}
}

function update_month(){
	var indexNum = document.forms[0].monthsel.selectedIndex;
	var selected = document.forms[0].monthsel.options[indexNum].text;

	for (i=0; i<=11; i++){
		option = document.forms[0].monthsel.options[i].text;
		document.popupMonthLayer = eval('document.getElementById (option)');
375

Ad Schellevis's avatar
Ad Schellevis committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
		if(selected == option) {
			document.popupMonthLayer.style.display="block";
		}
		else
			document.popupMonthLayer.style.display="none";
	}
}

function checkForRanges(){
	if (daysSelected != "")
	{
		alert("You have not saved the specified time range. Please click 'Add Time' button to save the time range.");
		return false;
	}
	else
	{
		return true;
	}
}

function processEntries(){
	var tempstr, starttimehour, starttimemin, stoptimehour, stoptimemin, errors = "";
	var passedValidiation = true;
399

Ad Schellevis's avatar
Ad Schellevis committed
400 401 402 403 404 405 406
	//get time specified
	starttimehour = parseInt(document.getElementById("starttimehour").value);
	starttimemin = parseInt(document.getElementById("starttimemin").value);
	stoptimehour = parseInt(document.getElementById("stoptimehour").value);
	stoptimemin = parseInt(document.getElementById("stoptimemin").value);


407
	//do time checks
Ad Schellevis's avatar
Ad Schellevis committed
408 409 410 411
	if (starttimehour > stoptimehour)
	{
		errors = "Error: Start Hour cannot be greater than Stop Hour.";
		passedValidiation = false;
412

Ad Schellevis's avatar
Ad Schellevis committed
413 414 415 416 417 418 419
	}
	else if (starttimehour == stoptimehour)
	{
		if (starttimemin > stoptimemin){
			errors = "Error: Start Minute cannot be greater than Stop Minute.";
			passedValidiation = false;
		}
420 421
	}

Ad Schellevis's avatar
Ad Schellevis committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
	if (passedValidiation){
		addTimeRange();
	}
	else {
		if (errors != "")
			alert(errors);
	}
}

function addTimeRange(){
	var tempdayarray = daysSelected.split(",");
	var tempstr, tempFriendlyDay, starttimehour, starttimemin, stoptimehour, nrtempFriendlyTime, rtempFriendlyTime, nrtempID, rtempID = "";
	var stoptimemin, timeRange, tempstrdaypos, week, daypos, day, month, dashpos, nrtempTime, rtempTime, monthstr, daystr = "";
	rtempFriendlyTime = "";
	nrtempFriendlyTime = "";
	nrtempID = "";
	rtempID = "";
	nrtempTime = "";
	rtempTime = "";
441
	tempdayarray.sort();
Ad Schellevis's avatar
Ad Schellevis committed
442 443 444
	rtempFriendlyDay = "";
	monthstr = "";
	daystr = "";
445

Ad Schellevis's avatar
Ad Schellevis committed
446 447 448 449 450 451 452 453 454 455
	//check for existing entries
	var findCurrentCounter;
	for (u=0; u<99; u++){
		findCurrentCounter = document.getElementById("schedule" + u);
		if (!findCurrentCounter)
		{
			schCounter = u;
			break;
		}
	}
456

Ad Schellevis's avatar
Ad Schellevis committed
457 458 459 460 461 462
	if (daysSelected != ""){
		//get days selected
		for (i=0; i<tempdayarray.length; i++)
		{
			tempstr = tempdayarray[i];
			if (tempstr != "")
463
			{
Ad Schellevis's avatar
Ad Schellevis committed
464 465 466
				tempstrdaypos = tempstr.search("p");
				week = tempstr.substring(1,tempstrdaypos);
				week = parseInt(week);
467 468
				dashpos = tempstr.search("-");

Ad Schellevis's avatar
Ad Schellevis committed
469
				if (dashpos != "-1")
470
				{
Ad Schellevis's avatar
Ad Schellevis committed
471 472
					var nonrepeatingfound = true;
					daypos = tempstr.substring(tempstrdaypos+1, dashpos);
473 474
					daypos = parseInt(daypos);
					monthpos = tempstr.search("m");
Ad Schellevis's avatar
Ad Schellevis committed
475 476 477 478 479 480 481 482 483 484
					tempstrdaypos = tempstr.search("d");
					month = tempstr.substring(monthpos+1, tempstrdaypos);
					month = parseInt(month);
					day = tempstr.substring(tempstrdaypos+1);
					day = parseInt(day);
					monthstr += month + ",";
					daystr += day + ",";
					nrtempID += tempstr + ",";
				}
				else
485
				{
Ad Schellevis's avatar
Ad Schellevis committed
486 487
					var repeatingfound = true;
					daypos = tempstr.substr(tempstrdaypos+1);
488
					daypos = parseInt(daypos);
Ad Schellevis's avatar
Ad Schellevis committed
489
					rtempFriendlyDay += daypos + ",";
490 491 492 493 494
					rtempID += daypos + ",";
				}
			}
		}

Ad Schellevis's avatar
Ad Schellevis committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
		//code below spits out friendly look format for nonrepeating schedules
		var foundEnd = false;
		var firstDayFound = false;
		var firstprint = false;
		var tempFriendlyMonthArray = monthstr.split(",");
		var tempFriendlyDayArray = daystr.split(",");
		var currentDay, firstDay, nextDay, currentMonth, nextMonth, firstDay, firstMonth = "";
		for (k=0; k<tempFriendlyMonthArray.length; k++){
			tempstr = tempFriendlyMonthArray[k];
			if (tempstr != ""){
				if (!firstDayFound)
				{
					firstDay = tempFriendlyDayArray[k];
					firstDay = parseInt(firstDay);
					firstMonth = tempFriendlyMonthArray[k];
					firstMonth = parseInt(firstMonth);
					firstDayFound = true;
				}
				currentDay = tempFriendlyDayArray[k];
				currentDay = parseInt(currentDay);
				//get next day
				nextDay = tempFriendlyDayArray[k+1];
				nextDay = parseInt(nextDay);
				//get next month
519 520

				currentDay++;
Ad Schellevis's avatar
Ad Schellevis committed
521 522 523 524 525 526 527
				if ((currentDay != nextDay) || (tempFriendlyMonthArray[k] != tempFriendlyMonthArray[k+1])){
					if (firstprint)
						nrtempFriendlyTime += ", ";
					currentDay--;
					if (currentDay != firstDay)
						nrtempFriendlyTime += month_array[firstMonth-1] + " " + firstDay + "-" + currentDay;
					else
528 529 530
						nrtempFriendlyTime += month_array[firstMonth-1] + " " + currentDay;
					firstDayFound = false;
					firstprint = true;
Ad Schellevis's avatar
Ad Schellevis committed
531
				}
532 533 534
			}
		}

Ad Schellevis's avatar
Ad Schellevis committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
		//code below spits out friendly look format for repeating schedules
		foundEnd = false;
		firstDayFound = false;
		firstprint = false;
		tempFriendlyDayArray = rtempFriendlyDay.split(",");
		tempFriendlyDayArray.sort();
		currentDay, firstDay, nextDay = "";
		for (k=0; k<tempFriendlyDayArray.length; k++){
			tempstr = tempFriendlyDayArray[k];
			if (tempstr != ""){
				if (!firstDayFound)
				{
					firstDay = tempFriendlyDayArray[k];
					firstDay = parseInt(firstDay);
					firstDayFound = true;
				}
				currentDay = tempFriendlyDayArray[k];
				currentDay = parseInt(currentDay);
				//get next day
				nextDay = tempFriendlyDayArray[k+1];
				nextDay = parseInt(nextDay);
556
				currentDay++;
Ad Schellevis's avatar
Ad Schellevis committed
557 558 559 560 561 562 563 564
				if (currentDay != nextDay){
					if (firstprint)
						rtempFriendlyTime += ", ";
					currentDay--;
					if (currentDay != firstDay)
						rtempFriendlyTime += day_array[firstDay-1] + " - " + day_array[currentDay-1];
					else
						rtempFriendlyTime += day_array[firstDay-1];
565 566
					firstDayFound = false;
					firstprint = true;
Ad Schellevis's avatar
Ad Schellevis committed
567 568
				}
			}
569 570
		}

Ad Schellevis's avatar
Ad Schellevis committed
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
		//sort the tempID
		var tempsortArray = rtempID.split(",");
		var isFirstdone = false;
		tempsortArray.sort();
		//clear tempID
		rtempID = "";
		for (t=0; t<tempsortArray.length; t++)
		{
			if (tempsortArray[t] != ""){
				if (!isFirstdone){
					rtempID += tempsortArray[t];
					isFirstdone = true;
				}
				else
					rtempID += "," + tempsortArray[t];
			}
587 588 589
		}


Ad Schellevis's avatar
Ad Schellevis committed
590 591 592 593 594
		//get time specified
		starttimehour =  document.getElementById("starttimehour").value
		starttimemin = document.getElementById("starttimemin").value;
		stoptimehour = document.getElementById("stoptimehour").value;
		stoptimemin = document.getElementById("stoptimemin").value;
595

Ad Schellevis's avatar
Ad Schellevis committed
596 597
		timeRange = "||" + starttimehour + ":";
		timeRange += starttimemin + "-";
598 599 600
		timeRange += stoptimehour + ":";
		timeRange += stoptimemin;

Ad Schellevis's avatar
Ad Schellevis committed
601
		//get description for time range
602 603
		var tempdescr = document.getElementById("timerangedescr").value

Ad Schellevis's avatar
Ad Schellevis committed
604 605 606
		if (nonrepeatingfound){
			nrtempTime += nrtempID;
			//add time ranges
607
			nrtempTime += timeRange;
Ad Schellevis's avatar
Ad Schellevis committed
608 609 610 611
			//add description
			nrtempTime += "||" + tempdescr;
			insertElements(nrtempFriendlyTime, starttimehour, starttimemin, stoptimehour, stoptimemin, tempdescr, nrtempTime, nrtempID);
		}
612

Ad Schellevis's avatar
Ad Schellevis committed
613 614 615 616 617 618 619 620
		if (repeatingfound){
			rtempTime += rtempID;
			//add time ranges
			rtempTime += timeRange;
			//add description
			rtempTime += "||" + tempdescr;
			insertElements(rtempFriendlyTime, starttimehour, starttimemin, stoptimehour, stoptimemin, tempdescr, rtempTime, rtempID);
		}
621

Ad Schellevis's avatar
Ad Schellevis committed
622 623 624 625 626 627 628 629 630
	}
	else
	{
		//no days were selected, alert user
		alert ("You must select at least 1 day before adding time");
	}
}

function insertElements(tempFriendlyTime, starttimehour, starttimemin, stoptimehour, stoptimemin, tempdescr, tempTime, tempID){
631

Ad Schellevis's avatar
Ad Schellevis committed
632 633 634 635 636 637
		//add it to the schedule list
		d = document;
		tbody = d.getElementById("scheduletable").getElementsByTagName("tbody").item(0);
		tr = d.createElement("tr");
		td = d.createElement("td");
		td.innerHTML= "<span class='vexpl'>" + tempFriendlyTime + "<\/span>";
638 639
		tr.appendChild(td);

Ad Schellevis's avatar
Ad Schellevis committed
640 641 642
		td = d.createElement("td");
		td.innerHTML="<input type='text' readonly class='vexpl' name='starttime" + schCounter + "' id='starttime" + schCounter + "' style=' word-wrap:break-word; width:100%; border:0px solid;' value='" + starttimehour + ":" + starttimemin + "' />";
		tr.appendChild(td);
643

Ad Schellevis's avatar
Ad Schellevis committed
644 645 646
		td = d.createElement("td");
		td.innerHTML="<input type='text' readonly class='vexpl' name='stoptime" + schCounter + "' id='stoptime" + schCounter + "' style=' word-wrap:break-word; width:100%; border:0px solid;' value='" + stoptimehour + ":" + stoptimemin + "' />";
		tr.appendChild(td);
647

Ad Schellevis's avatar
Ad Schellevis committed
648 649 650
		td = d.createElement("td");
		td.innerHTML="<input type='text' readonly class='vexpl' name='timedescr" + schCounter + "' id='timedescr" + schCounter + "' style=' word-wrap:break-word; width:100%; border:0px solid;' value='" + tempdescr + "' />";
		tr.appendChild(td);
651

Ad Schellevis's avatar
Ad Schellevis committed
652
		td = d.createElement("td");
Ad Schellevis's avatar
Ad Schellevis committed
653
		td.innerHTML = "<a onclick='editRow(\"" + tempTime + "\",this); return false;' href='#' class=\"btn btn-default btn-xs\"><span class=\"glyphicon glyphicon-pencil\"></span></\a>";
Ad Schellevis's avatar
Ad Schellevis committed
654
		tr.appendChild(td);
655

Ad Schellevis's avatar
Ad Schellevis committed
656
		td = d.createElement("td");
Ad Schellevis's avatar
Ad Schellevis committed
657
		td.innerHTML = "<a onclick='removeRow(this); return false;' href='#' class=\"btn btn-default btn-xs\"><span class=\"glyphicon glyphicon-remove\"></span></\a>";
Ad Schellevis's avatar
Ad Schellevis committed
658
		tr.appendChild(td);
659 660 661

		td = d.createElement("td");
		td.innerHTML="<input type='hidden' id='schedule" + schCounter + "' name='schedule" + schCounter + "' value='" + tempID + "' />";
Ad Schellevis's avatar
Ad Schellevis committed
662 663
		tr.appendChild(td);
		tbody.appendChild(tr);
664

Ad Schellevis's avatar
Ad Schellevis committed
665
		schCounter++;
666

Ad Schellevis's avatar
Ad Schellevis committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
		//reset calendar and time and descr
		clearCalendar();
		clearTime();
		clearDescr();
}


function clearCalendar(){
	var tempstr, daycell = "";
	//clear days selected
	daysSelected = "";
	//loop through all 52 weeks
	for (j=1; j<=53; j++)
	{
		//loop through all 7 days
		for (k=1; k<8; k++){
			tempstr = 'w' + j + 'p' + k;
			daycell = eval('document.getElementById(tempstr)');
			if (daycell != null){
686 687
				daycell.style.backgroundColor = "#FFFFFF";  // white
			}
Ad Schellevis's avatar
Ad Schellevis committed
688
		}
689
	}
Ad Schellevis's avatar
Ad Schellevis committed
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
}

function clearTime(){
	document.getElementById("starttimehour").value = "0";
	document.getElementById("starttimemin").value = "00";
	document.getElementById("stoptimehour").value = "23";
	document.getElementById("stoptimemin").value = "59";
}

function clearDescr(){
	document.getElementById("timerangedescr").value = "";
}

function editRow(incTime, el) {
	var check = checkForRanges();
705 706 707

	if (check){

Ad Schellevis's avatar
Ad Schellevis committed
708 709 710
		//reset calendar and time
		clearCalendar();
		clearTime();
711 712 713

		var starttimehour, descr, days, tempstr, starttimemin, hours, stoptimehour, stoptimemin = "";

Ad Schellevis's avatar
Ad Schellevis committed
714
		tempArray = incTime.split ("||");
715

Ad Schellevis's avatar
Ad Schellevis committed
716 717 718
		days = tempArray[0];
		hours = tempArray[1];
		descr = tempArray[2];
719

Ad Schellevis's avatar
Ad Schellevis committed
720 721 722 723
		var tempdayArray = days.split(",");
		var temphourArray = hours.split("-");
		tempstr = temphourArray[0];
		var temphourArray2 = tempstr.split(":");
724

Ad Schellevis's avatar
Ad Schellevis committed
725
		document.getElementById("starttimehour").value = temphourArray2[0];
726 727
		document.getElementById("starttimemin").value = temphourArray2[1];

Ad Schellevis's avatar
Ad Schellevis committed
728 729
		tempstr = temphourArray[1];
		temphourArray2 = tempstr.split(":");
730

Ad Schellevis's avatar
Ad Schellevis committed
731 732
		document.getElementById("stoptimehour").value = temphourArray2[0];
		document.getElementById("stoptimemin").value = temphourArray2[1];
733

Ad Schellevis's avatar
Ad Schellevis committed
734
		document.getElementById("timerangedescr").value = descr;
735

Ad Schellevis's avatar
Ad Schellevis committed
736 737 738 739 740
		//toggle the appropriate days
		for (i=0; i<tempdayArray.length; i++)
		{
			if (tempdayArray[i]){
				var tempweekstr = tempdayArray[i];
741 742
				dashpos = tempweekstr.search("-");

Ad Schellevis's avatar
Ad Schellevis committed
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
				if (dashpos == "-1")
				{
					tempstr = "w2p" + tempdayArray[i];
				}
				else
				{
					tempstr = tempdayArray[i];
				}
				daytoggle(tempstr);
			}
		}
		removeRownoprompt(el);
	}
}

function removeRownoprompt(el) {
    var cel;
    while (el && el.nodeName.toLowerCase() != "tr")
	    el = el.parentNode;

    if (el && el.parentNode) {
	cel = el.getElementsByTagName("td").item(0);
	el.parentNode.removeChild(el);
    }
}


function removeRow(el) {
	var check = confirm ("Do you really want to delete this time range?");
	if (check){
	    var cel;
	    while (el && el.nodeName.toLowerCase() != "tr")
		    el = el.parentNode;
776

Ad Schellevis's avatar
Ad Schellevis committed
777 778 779 780 781 782 783 784 785 786 787
	    if (el && el.parentNode) {
		cel = el.getElementsByTagName("td").item(0);
		el.parentNode.removeChild(el);
	    }
	}
}
//]]>
</script>
EOD;
?>

788
<body>
Ad Schellevis's avatar
Ad Schellevis committed
789 790 791 792


<?php include("fbegin.inc");	echo $jscriptstr; ?>

Ad Schellevis's avatar
Ad Schellevis committed
793 794

	<section class="page-content-main">
795
		<div class="container-fluid">
Ad Schellevis's avatar
Ad Schellevis committed
796
			<div class="row">
797

798
				<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
Ad Schellevis's avatar
Ad Schellevis committed
799 800
				<div id="inputerrors"></div>

801

Ad Schellevis's avatar
Ad Schellevis committed
802
			    <section class="col-xs-12">
803 804 805 806 807 808 809 810 811 812 813

				<div class="content-box">

					 <header class="content-box-head container-fluid">
				        <h3><?=gettext("Schedule information");?></h3>
				    </header>

				    <div class="content-box-main">

						<form action="firewall_schedule_edit.php" method="post" name="iform" id="iform">

Ad Schellevis's avatar
Ad Schellevis committed
814 815
		                        <div class="table-responsive">
			                        <table class="table table-striped table-sort">
816 817
			                        <tbody>
								<tr>
Ad Schellevis's avatar
Ad Schellevis committed
818 819 820
										  <td width="15%" valign="top" class="vncellreq"><?=gettext("Schedule Name");?></td>
										  <td width="85%" class="vtable">
										  <?php if(is_schedule_inuse($pconfig['name']) == true): ?>
821
													<input name="name" type="hidden" id="name" size="40"  value="<?=htmlspecialchars($pconfig['name']);?>" />
Ad Schellevis's avatar
Ad Schellevis committed
822 823 824 825 826 827
												  <?php echo $pconfig['name']; ?>
												      <p>
												        <span class="vexpl"><?=gettext("NOTE: This schedule is in use so the name may not be modified!");?></span>
												      </p>
										<?php else: ?>
										  <input name="name" type="text" id="name" size="40" maxlength="40" class="form-control unknown" value="<?=htmlspecialchars($pconfig['name']);?>" /><br />
828 829 830 831
											<span class="vexpl">
											   <?=gettext("The name of the alias may only consist of the characters a-z, A-Z and 0-9");?>
											</span>
									<?php endif; ?>
Ad Schellevis's avatar
Ad Schellevis committed
832 833 834 835 836
										  </td>
										</tr>
										<tr>
											<td width="15%" valign="top" class="vncell"><?=gettext("Description");?></td>
											<td width="85%" class="vtable"><input name="descr" type="text" id="descr" size="40" maxlength="40" class="form-control unknown" value="<?=htmlspecialchars($pconfig['descr']);?>" /><br />
837 838 839 840
												<span class="vexpl">
												<?=gettext("You may enter a description here for your reference (not parsed).");?>
											</span>

Ad Schellevis's avatar
Ad Schellevis committed
841 842 843 844 845 846 847 848
											</td>
										</tr>
										<!-- tr>
										</tr -->
									    <tr>
										  <td width="15%" valign="top" class="vncellreq"><?=gettext("Month");?></td>
										  <td width="85%" class="vtable">
						                    <select name="monthsel" class="form-control" id="monthsel" onchange="update_month();">
849 850 851 852 853 854 855
									<?php
									$monthcounter = date("n");
									$monthlimit = $monthcounter + 12;
									$yearcounter = date("Y");
									for ($k=0; $k<12; $k++){?>
										<option value="<?php echo $monthcounter;?>"><?php echo date("F_y", mktime(0, 0, 0, date($monthcounter), 1, date($yearcounter)));?></option>
						                          <?php
Ad Schellevis's avatar
Ad Schellevis committed
856
						                          if ($monthcounter == 12)
Ad Schellevis's avatar
Ad Schellevis committed
857
													{
Ad Schellevis's avatar
Ad Schellevis committed
858 859
														$monthcounter = 1;
														$yearcounter++;
Ad Schellevis's avatar
Ad Schellevis committed
860
													}
Ad Schellevis's avatar
Ad Schellevis committed
861
													else
Ad Schellevis's avatar
Ad Schellevis committed
862
													{
Ad Schellevis's avatar
Ad Schellevis committed
863
														$monthcounter++;
864 865
													}
												} ?>
Ad Schellevis's avatar
Ad Schellevis committed
866
						                    </select><br /><br />
867 868 869 870 871
									<?php
									$firstmonth = TRUE;
									$monthcounter = date("n");
									$yearcounter = date("Y");
									for ($k=0; $k<12; $k++){
Ad Schellevis's avatar
Ad Schellevis committed
872 873 874
												$firstdayofmonth = date("w", mktime(0, 0, 0, date($monthcounter), 1, date($yearcounter)));
												if ($firstdayofmonth == 0)
													$firstdayofmonth = 7;
875

Ad Schellevis's avatar
Ad Schellevis committed
876 877 878 879 880
												$daycounter = 1;
												//number of day in month
												$numberofdays = date("t", mktime(0, 0, 0, date($monthcounter), 1, date($yearcounter)));
												$firstdayprinted = FALSE;
												$lasttr = FALSE;
881 882 883 884
												$positioncounter = 1;//7 for Sun, 1 for Mon, 2 for Tues, etc
												?>
							                        <div id="<?php echo date("F_y",mktime(0, 0, 0, date($monthcounter), 1, date($yearcounter)));?>" style=" position:relative; display:<?php if($firstmonth)echo "block";else echo "none";?>">
											<table border="1" cellspacing="1" cellpadding="1" id="calTable<?=$monthcounter . $yearcounter;?>" class="tabcont " summary="month">
Ad Schellevis's avatar
Ad Schellevis committed
885 886
														<thead>
														<tr><td colspan="7" align="center" class="listbg"><?php echo date("F_Y", mktime(0, 0, 0, date($monthcounter), 1, date($yearcounter)));?></td></tr>
887
														<tr>
Ad Schellevis's avatar
Ad Schellevis committed
888 889 890 891 892 893 894
															<td align="center" class="listhdrr" style="cursor: pointer;" onclick="daytoggle('w1p1');"><u><?=gettext("Mon");?></u></td>
															<td align="center" class="listhdrr" style="cursor: pointer;" onclick="daytoggle('w1p2');"><u><?=gettext("Tue");?></u></td>
															<td align="center" class="listhdrr" style="cursor: pointer;" onclick="daytoggle('w1p3');"><u><?=gettext("Wed");?></u></td>
															<td align="center" class="listhdrr" style="cursor: pointer;" onclick="daytoggle('w1p4');"><u><?=gettext("Thu");?></u></td>
															<td align="center" class="listhdrr" style="cursor: pointer;" onclick="daytoggle('w1p5');"><u><?=gettext("Fri");?></u></td>
															<td align="center" class="listhdrr" style="cursor: pointer;" onclick="daytoggle('w1p6');"><u><?=gettext("Sat");?></u></td>
															<td align="center" class="listhdrr" style="cursor: pointer;" onclick="daytoggle('w1p7');"><u><?=gettext("Sun");?></u></td>
Ad Schellevis's avatar
Ad Schellevis committed
895
														</tr>
Ad Schellevis's avatar
Ad Schellevis committed
896 897
														</thead>
														<tbody>
898 899
														<?php
														$firstmonth = FALSE;
Ad Schellevis's avatar
Ad Schellevis committed
900 901 902 903 904 905
														while ($daycounter<=$numberofdays){
															$weekcounter =  date("W", mktime(0, 0, 0, date($monthcounter), date($daycounter), date($yearcounter)));
															$weekcounter = ltrim($weekcounter, "0");
															if ($positioncounter == 1)
															{
																echo "<tr>";
906
															}
Ad Schellevis's avatar
Ad Schellevis committed
907 908 909 910 911 912 913 914 915 916 917 918 919
															if ($firstdayofmonth == $positioncounter){?>
																<td align="center" style="cursor: pointer;" class="listr" id="w<?=$weekcounter;?>p<?=$positioncounter;?>" onclick="daytoggle('w<?=$weekcounter;?>p<?=$positioncounter;?>-m<?=$monthcounter;?>d<?=$daycounter;?>');">
																<?php echo $daycounter;
																$daycounter++;
																$firstdayprinted = TRUE;
																echo "</td>";
															}
															elseif ($firstdayprinted == TRUE && $daycounter <= $numberofdays){?>
																<td align="center" style="cursor: pointer;" class="listr" id="w<?=$weekcounter;?>p<?=$positioncounter;?>" onclick="daytoggle('w<?=$weekcounter;?>p<?=$positioncounter;?>-m<?=$monthcounter;?>d<?=$daycounter;?>');">
																<?php echo $daycounter;
																$daycounter++;
																echo "</td>";
															}
Ad Schellevis's avatar
Ad Schellevis committed
920
															else
Ad Schellevis's avatar
Ad Schellevis committed
921 922 923
															{
																echo "<td align=\"center\" class=\"listr\"></td>";
															}
924

Ad Schellevis's avatar
Ad Schellevis committed
925 926 927 928 929 930 931
															if ($positioncounter == 7 || $daycounter > $numberofdays){
																$positioncounter = 1;
																echo "</tr>";
															}
															else{
																$positioncounter++;
															}
932 933

														}//end while loop?>
Ad Schellevis's avatar
Ad Schellevis committed
934
													</tbody>
Ad Schellevis's avatar
Ad Schellevis committed
935 936
													</table>
													</div>
937 938
											<?php

Ad Schellevis's avatar
Ad Schellevis committed
939 940 941 942 943 944 945 946
												if ($monthcounter == 12)
												{
													$monthcounter = 1;
													$yearcounter++;
												}
												else
												{
													$monthcounter++;
947
												}
Ad Schellevis's avatar
Ad Schellevis committed
948 949 950 951 952 953 954 955 956
											} //end for loop
											?>
													<br />
											<?=gettext("Click individual date to select that date only. Click the appropriate weekday Header to select all occurrences of that weekday.");?>
							                 </td>
										</tr>
										<tr>
										  <td width="15%" valign="top" class="vncellreq"><?=gettext("Time");?></td>
										  <td width="85%" class="vtable">
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
											<table cellspacing="2" class="tabcont" summary="time">
												<tr>
													<td class="listhdrr" align="center"><?=gettext("Start Time");?></td><td></td><td class="listhdrr" align="center"><?=gettext("Stop Time");?></td>
												</tr>
												<tr>
													<td>
														<select name="starttimehour" class="form-control" id="starttimehour">
															<?php
																for ($i=0; $i<24; $i++)
																{
																	echo "<option value=\"$i\">";
																	echo $i;
																	echo "</option>";
																}
															?>
														</select>&nbsp;<?=gettext("Hr"); ?>&nbsp;&nbsp;
														<select name="starttimemin" class="form-control" id="starttimemin">
															<option value="00">00</option>
															<option value="15">15</option>
															<option value="30">30</option>
															<option value="45">45</option>
															<option value="59">59</option>
														</select>&nbsp;<?=gettext("Min"); ?>
													</td>
													<td></td>
													<td>
														<select name="stoptimehour" class="form-control" id="stoptimehour">
														<?php
																for ($i=0; $i<24; $i++)
																{
																	if ($i==23)
																		$selected = "selected=\"selected\"";
																	else
																		$selected = "";

																	echo "<option value=\"$i\" $selected>";
																	echo $i;
																	echo "</option>";
																}
															?>
														</select>&nbsp;<?=gettext("Hr");?>&nbsp;&nbsp;
														<select name="stoptimemin" class="form-control" id="stoptimemin">
															<option value="00">00</option>
															<option value="15">15</option>
															<option value="30">30</option>
															<option value="45">45</option>
															<option value="59" selected="selected">59</option>
														</select>&nbsp;<?=gettext("Min");?>
													</td>
												</tr>
											</table><br />
Ad Schellevis's avatar
Ad Schellevis committed
1008 1009 1010 1011 1012 1013
						                   <?=gettext("Select the time range for the day(s) selected on the Month(s) above. A full day is 0:00-23:59.")?>
											</td>
										</tr>
										<tr>
											<td width="15%" valign="top" class="vncell"><?=gettext("Time Range Description")?></td>
											<td width="85%" class="vtable"><input name="timerangedescr" type="text" class="form-control unknown" id="timerangedescr" size="40" maxlength="40" /><br />
1014 1015 1016 1017
												<span class="vexpl">
												<?=gettext("You may enter a description here for your reference (not parsed).")?>
											</span>
										      </td>
Ad Schellevis's avatar
Ad Schellevis committed
1018 1019 1020 1021
										</tr>
										<tr>
										  <td width="22%" valign="top">&nbsp;</td>
										  <td width="78%">
1022 1023
											<input type="button" value="<?=gettext("Add Time");?>"  class="btn btn-default"  onclick="javascript:processEntries();" />&nbsp;&nbsp;&nbsp;
											<input type="button" value="<?=gettext("Clear Selection");?>" class="btn btn-default" onclick="javascript:clearCalendar(); clearTime(); clearDescr();" />
Ad Schellevis's avatar
Ad Schellevis committed
1024 1025 1026 1027
						                    </td>
										</tr>
										<tr>
										  <td width="15%" valign="top" class="vtable"></td>
Ad Schellevis's avatar
Ad Schellevis committed
1028
										  <td width="85%" class="vtable"></td>
1029
										</tr>
Ad Schellevis's avatar
Ad Schellevis committed
1030 1031 1032 1033
                                        </tbody>
								</table>
								<table class="table table-striped">
										<thead>
Ad Schellevis's avatar
Ad Schellevis committed
1034 1035
										<tr>
											<td colspan="2" valign="top" class="listtopic"><?=gettext("Schedule repeat");?></td>
Ad Schellevis's avatar
Ad Schellevis committed
1036 1037
										</tr>
										</thead>
1038
										<tbody>
Ad Schellevis's avatar
Ad Schellevis committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
										<tr>
											<td width="15%" valign="top" class="vncellreq"><?=gettext("Configured Ranges");?></td>
											<td width="85%">
												<table id="scheduletable" summary="range">
													<tbody>
														<tr>
															<td align="center" class="listbg" width="35%"><?=gettext("Day(s)");?></td>
															<td align="center" class="listbg" width="12%"><?=gettext("Start Time");?></td>
															<td align="center" class="listbg" width="11%"><?=gettext("Stop Time");?></td>
															<td align="center" class="listbg" width="42%"><?=gettext("Description");?></td>
														</tr>
														<?php
														if ($getSchedule){
															$counter = 0;
1053

Ad Schellevis's avatar
Ad Schellevis committed
1054 1055 1056 1057 1058 1059
															foreach($pconfig['timerange'] as $timerange) {
																$tempFriendlyTime = "";
																$tempID = "";
																if ($timerange){
																	$dayFriendly = "";
																	$tempFriendlyTime = "";
1060 1061
																	$timedescr = $timerange['rangedescr'];

Ad Schellevis's avatar
Ad Schellevis committed
1062 1063 1064
																	//get hours
																	$temptimerange = $timerange['hour'];
																	$temptimeseparator = strrpos($temptimerange, "-");
1065 1066 1067

																	$starttime = substr ($temptimerange, 0, $temptimeseparator);
																	$stoptime = substr ($temptimerange, $temptimeseparator+1);
Ad Schellevis's avatar
Ad Schellevis committed
1068 1069 1070 1071 1072
																	$currentDay = "";
																	$firstDay = "";
																	$nextDay = "";
																	$foundEnd = false;
																	$firstDayFound = false;
1073
																	$firstPrint = false;
Ad Schellevis's avatar
Ad Schellevis committed
1074
																	$firstprint2 = false;
1075

Ad Schellevis's avatar
Ad Schellevis committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
																	if ($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;
1087
																			}
Ad Schellevis's avatar
Ad Schellevis committed
1088
																			$weeknumber = date("W", mktime(0, 0, 0, date($month), date($day), date("Y")));
1089 1090
																			$weeknumber = ltrim($weeknumber, "0");

Ad Schellevis's avatar
Ad Schellevis committed
1091 1092 1093 1094 1095 1096
																			if ($firstPrint)
																			{
																				$tempID .= ",";
																			}
																			$tempID .= "w" . $weeknumber . "p" . $daypos . "-m" .  $month . "d" . $day;
																			$firstPrint = true;
1097

Ad Schellevis's avatar
Ad Schellevis committed
1098 1099 1100 1101 1102 1103
																			if (!$firstDayFound)
																			{
																				$firstDay = $day;
																				$firstmonth = $month;
																				$firstDayFound = true;
																			}
1104

Ad Schellevis's avatar
Ad Schellevis committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
																			$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;
1116
																				$firstDayFound = false;
Ad Schellevis's avatar
Ad Schellevis committed
1117
																				$firstprint2 = true;
1118 1119 1120 1121
																			}
																			$arraycounter++;
																		}

Ad Schellevis's avatar
Ad Schellevis committed
1122 1123 1124 1125 1126
																	}
																	else
																	{
																		$dayFriendly = $timerange['position'];
																		$tempID = $dayFriendly;
1127 1128
																	}

Ad Schellevis's avatar
Ad Schellevis committed
1129
																	$tempTime = $tempID . "||" . $starttime . "-" . $stoptime . "||" . $timedescr;
1130

Ad Schellevis's avatar
Ad Schellevis committed
1131 1132 1133 1134 1135 1136 1137 1138 1139
																	//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;
1140
																	if (!$timerange['month']){
Ad Schellevis's avatar
Ad Schellevis committed
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
																		foreach ($tempFriendlyDayArray as $day){
																			if ($day != ""){
																				if (!$firstDayFound)
																				{
																					$firstDay = $tempFriendlyDayArray[$i];
																					$firstDayFound = true;
																				}
																				$currentDay =$tempFriendlyDayArray[$i];
																				//get next day
																				$nextDay = $tempFriendlyDayArray[$i+1];
1151
																				$currentDay++;
Ad Schellevis's avatar
Ad Schellevis committed
1152 1153 1154 1155 1156 1157 1158 1159
																				if ($currentDay != $nextDay){
																					if ($firstprint)
																						$tempFriendlyTime .= ", ";
																					$currentDay--;
																					if ($currentDay != $firstDay)
																						$tempFriendlyTime .= $dayArray[$firstDay-1] . " - " . $dayArray[$currentDay-1];
																					else
																						$tempFriendlyTime .= $dayArray[$firstDay-1];
1160 1161
																					$firstDayFound = false;
																					$firstprint = true;
Ad Schellevis's avatar
Ad Schellevis committed
1162 1163 1164
																				}
																				$i++;
																			}
1165 1166 1167 1168
																		}
																	}


Ad Schellevis's avatar
Ad Schellevis committed
1169 1170
															?>
												          <tr>
1171 1172 1173
														<td>
															<span class="vexpl"><?php echo $tempFriendlyTime; ?></span>
														</td>
Ad Schellevis's avatar
Ad Schellevis committed
1174 1175 1176 1177
															<td>
												              <input type='text' readonly='readonly' class='vexpl' name='starttime<?php echo $counter; ?>' id='starttime<?php echo $counter; ?>' style=' word-wrap:break-word; width:100%; border:0px solid;' value='<?php echo $starttime; ?>' />
													        </td>
												            <td>
1178
												              <input type='text' readonly='readonly' class='vexpl' name='stoptime<?php echo $counter; ?>' id='stoptime<?php echo $counter; ?>' style=' word-wrap:break-word; width:100%; border:0px solid;' value='<?php echo $stoptime; ?>' />
Ad Schellevis's avatar
Ad Schellevis committed
1179 1180
													        </td>
													        <td>
1181
															<input type='text' readonly='readonly' class='vexpl' name='timedescr<?php echo $counter; ?>' id='timedescr<?php echo $counter; ?>' style=' word-wrap:break-word; width:100%; border:0px solid;' value='<?php echo $timedescr; ?>' />
Ad Schellevis's avatar
Ad Schellevis committed
1182
													        </td>
1183

Ad Schellevis's avatar
Ad Schellevis committed
1184
													        <td>
1185
															<a onclick='editRow("<?php echo $tempTime; ?>",this); return false;' href='#' class="btn btn-default"><span class="glyphicon glyphicon-edit"></span></a>
Ad Schellevis's avatar
Ad Schellevis committed
1186 1187
													        </td>
													        <td>
1188
															<a onclick='removeRow(this); return false;' href='#' class="btn btn-default"><span class="glyphicon glyphicon-remove"></span></a>
Ad Schellevis's avatar
Ad Schellevis committed
1189 1190
													        </td>
													        <td>
1191
															<input type='hidden' id='schedule<?php echo $counter; ?>' name='schedule<?php echo $counter; ?>' value='<?php echo $tempID; ?>' />
Ad Schellevis's avatar
Ad Schellevis committed
1192 1193 1194 1195
													        </td>
												          </tr>
															<?php
												        $counter++;
1196 1197 1198
															}//end if
												        } // end foreach
														}//end if
Ad Schellevis's avatar
Ad Schellevis committed
1199
														?>
1200 1201
													</tbody>
												</table>
Ad Schellevis's avatar
Ad Schellevis committed
1202 1203
											</td>
										</tr>
1204
										<tr>
Ad Schellevis's avatar
Ad Schellevis committed
1205 1206 1207 1208 1209 1210 1211 1212
										    <td width="15%" valign="top">&nbsp;</td>
										    <td width="85%">
										      <input id="submit" name="submit" type="submit" onclick="return checkForRanges();" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
										      <input type="button" class="btn btn-default" value="<?=gettext("Cancel");?>" onclick="window.location.href='<?=$referer;?>'" />
										      <?php if (isset($id) && $a_schedules[$id]): ?>
										      <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
										      <?php endif; ?>
										    </td>
1213
										</tr>
Ad Schellevis's avatar
Ad Schellevis committed
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
									</tbody>
								    </table>
								</div>
                            </form>
                        </div>
                    </div>
                </section>
            </div>
        </div>
    </section>
1224
<?php include("foot.inc"); ?>