voucher.inc 16.9 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4 5
    Copyright (C) 2010-2012 Ermal Luci <eri@pfsense.org>
    Copyright (C) 2010 Scott Ullrich <sullrich@gmail.com>
Ad Schellevis's avatar
Ad Schellevis committed
6 7
    Copyright (C) 2007 Marcel Wiget <mwiget@mac.com>
    All rights reserved.
8

Ad Schellevis's avatar
Ad Schellevis committed
9 10
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
11

Ad Schellevis's avatar
Ad Schellevis committed
12 13
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
14

Ad Schellevis's avatar
Ad Schellevis committed
15 16 17
    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.
18

Ad Schellevis's avatar
Ad Schellevis committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32
    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.

*/

/* include all configuration functions */
33
require_once("captiveportal.inc");
Ad Schellevis's avatar
Ad Schellevis committed
34 35 36 37

function voucher_expire($voucher_received) {
	global $g, $config, $cpzone;

38 39
	$cpdb = new OPNsense\CaptivePortal\DB($cpzone);
	$cpc = new OPNsense\CaptivePortal\CPClient();
40

Ad Schellevis's avatar
Ad Schellevis committed
41 42 43 44 45 46 47 48 49 50 51
	// read rolls into assoc array with rollid as key and minutes as value
	$tickets_per_roll = array();
	$minutes_per_roll = array();
	if (is_array($config['voucher'][$cpzone]['roll'])) {
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
		}
	}

	// split into an array. Useful for multiple vouchers given
52
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
Ad Schellevis's avatar
Ad Schellevis committed
53 54 55 56 57 58 59 60 61 62
	$active_dirty = false;

	// go through all received vouchers, check their valid and extract
	// Roll# and Ticket# using the external readvoucher binary
	foreach ($a_vouchers_received as $voucher) {
		$v = escapeshellarg($voucher);
		if (strlen($voucher) < 3)
			continue;   // seems too short to be a voucher!

		unset($output);
63
		$_gb = exec("/usr/local/bin/voucher -c /var/etc/voucher_{$cpzone}.cfg -k /var/etc/voucher_{$cpzone}.public -- $v", $output);
Ad Schellevis's avatar
Ad Schellevis committed
64 65
		list($status, $roll, $nr) = explode(" ", $output[0]);
		if ($status == "OK") {
66
			// check if we have this ticket on a registered roll for this ticket
Ad Schellevis's avatar
Ad Schellevis committed
67
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
68
				// voucher is from a registered roll.
Ad Schellevis's avatar
Ad Schellevis committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
				if (!isset($active_vouchers[$roll]))
					$active_vouchers[$roll] = voucher_read_active_db($roll);
				// valid voucher. Store roll# and ticket#
				if (!empty($active_vouchers[$roll][$voucher])) {
					$active_dirty = true;
					unset($active_vouchers[$roll][$voucher]);
				}
				// check if voucher already marked as used
				if (!isset($bitstring[$roll]))
					$bitstring[$roll] = voucher_read_used_db($roll);
				$pos = $nr >> 3; // divide by 8 -> octet
				$mask = 1 << ($nr % 8);
				// mark bit for this voucher as used
				if (!(ord($bitstring[$roll][$pos]) & $mask))
					$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
				captiveportal_syslog("{$voucher} ({$roll}/{$nr}) forced to expire");

				/* Check if this voucher has any active sessions */
87 88 89
				$clients  = $cpdb->listClients(array("username"=>$voucher),null, null);
				foreach($clients as $client ){
					$cpc->disconnect($cpzone,$client->sessionid);
Ad Schellevis's avatar
Ad Schellevis committed
90
				}
91

Ad Schellevis's avatar
Ad Schellevis committed
92 93 94 95 96 97 98 99 100
			} else
				captiveportal_syslog("$voucher ($roll/$nr): not found on any registererd Roll");
		} else
			// hmm, thats weird ... not what I expected
			captiveportal_syslog("$voucher invalid: {$output[0]}!!");
	}

	// Refresh active DBs
	if ($active_dirty == true) {
101
		foreach ($active_vouchers as $roll => $active) {
Ad Schellevis's avatar
Ad Schellevis committed
102
			voucher_write_active_db($roll, $active);
103
		}
Ad Schellevis's avatar
Ad Schellevis committed
104 105
		unset($active_vouchers);

106 107
		/* trigger a sync of the vouchers on config */
		voucher_save_db_to_config();
Ad Schellevis's avatar
Ad Schellevis committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	}

	// Write back the used DB's
	if (is_array($bitstring)) {
		foreach ($bitstring as $roll => $used) {
			if(is_array($used)) {
				foreach($used as $u)
					voucher_write_used_db($roll, base64_encode($u));
			} else {
				voucher_write_used_db($roll, base64_encode($used));
			}
		}
		unset($bitstring);
	}

123 124 125
	unset($cpdb);
	unset($cpc);

Ad Schellevis's avatar
Ad Schellevis committed
126 127 128 129

	return true;
}

130
/*
Ad Schellevis's avatar
Ad Schellevis committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
 * Authenticate a voucher and return the remaining time credit in minutes
 * if $test is set, don't mark the voucher as used nor add it to the list
 * of active vouchers
 * If $test is set, simply test the voucher. Don't change anything
 * but return a more verbose error and result message back
 */
function voucher_auth($voucher_received, $test = 0) {
	global $g, $config, $cpzone, $dbc;

	if (!isset($config['voucher'][$cpzone]['enable']))
		return 0;

	// read rolls into assoc array with rollid as key and minutes as value
	$tickets_per_roll = array();
	$minutes_per_roll = array();
	if (is_array($config['voucher'][$cpzone]['roll'])) {
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
			$tickets_per_roll[$rollent['number']] = $rollent['count'];
			$minutes_per_roll[$rollent['number']] = $rollent['minutes'];
		}
	}

	// split into an array. Useful for multiple vouchers given
154
	$a_vouchers_received = preg_split("/[\t\n\r ]+/s", $voucher_received);
Ad Schellevis's avatar
Ad Schellevis committed
155 156 157 158 159 160 161 162 163 164 165 166 167
	$error = 0;
	$test_result = array();     // used to display for voucher test option in GUI
	$total_minutes = 0;
	$first_voucher = "";
	$first_voucher_roll = 0;

	// go through all received vouchers, check their valid and extract
	// Roll# and Ticket# using the external readvoucher binary
	foreach ($a_vouchers_received as $voucher) {
		$v = escapeshellarg($voucher);
		if (strlen($voucher) < 3)
			continue;   // seems too short to be a voucher!

168
		$result = exec("/usr/local/bin/voucher -c /var/etc/voucher_{$cpzone}.cfg -k /var/etc/voucher_{$cpzone}.public -- $v");
Ad Schellevis's avatar
Ad Schellevis committed
169 170 171 172 173 174 175
		list($status, $roll, $nr) = explode(" ", $result);
		if ($status == "OK") {
			if (!$first_voucher) {
				// store first voucher. Thats the one we give the timecredit
				$first_voucher = $voucher;
				$first_voucher_roll = $roll;
			}
176
			// check if we have this ticket on a registered roll for this ticket
Ad Schellevis's avatar
Ad Schellevis committed
177
			if ($tickets_per_roll[$roll] && ($nr <= $tickets_per_roll[$roll])) {
178
				// voucher is from a registered roll.
Ad Schellevis's avatar
Ad Schellevis committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
				if (!isset($active_vouchers[$roll]))
					$active_vouchers[$roll] = voucher_read_active_db($roll);
				// valid voucher. Store roll# and ticket#
				if (!empty($active_vouchers[$roll][$voucher])) {
					list($timestamp,$minutes) = explode(",", $active_vouchers[$roll][$voucher]);
					// we have an already active voucher here.
					$remaining = intval((($timestamp + (60*$minutes)) - time())/60);
					$test_result[] = sprintf(gettext('%1$s (%2$s/%3$s) active and good for %4$d Minutes'), $voucher, $roll, $nr, $remaining);
					$total_minutes += $remaining;
				} else {
					// voucher not used. Check if ticket Id is on the roll (not too high)
					// and if the ticket is marked used.
					// check if voucher already marked as used
					if (!isset($bitstring[$roll]))
						$bitstring[$roll] = voucher_read_used_db($roll);
					$pos = $nr >> 3; // divide by 8 -> octet
					$mask = 1 << ($nr % 8);
					if (ord($bitstring[$roll][$pos]) & $mask) {
						$test_result[] = "$voucher ($roll/$nr) already used and expired";
						captiveportal_syslog("$voucher ($roll/$nr) already used and expired");
						$total_minutes = -1;    // voucher expired
						$error++;
					} else {
						// mark bit for this voucher as used
						$bitstring[$roll][$pos] = chr(ord($bitstring[$roll][$pos]) | $mask);
						$test_result[] = "$voucher ($roll/$nr) good for {$minutes_per_roll[$roll]} Minutes";
						$total_minutes += $minutes_per_roll[$roll];
					}
				}
			} else {
				$test_result[] = "$voucher ($roll/$nr): not found on any registererd Roll";
				captiveportal_syslog("$voucher ($roll/$nr): not found on any registererd Roll");
			}
		} else {
			// hmm, thats weird ... not what I expected
			$test_result[] = "$voucher invalid: $result !!";
			captiveportal_syslog("$voucher invalid: $result !!");
			$error++;
		}
	}

	// if this was a test call, we're done. Return the result.
	if ($test) {
		if ($error) {
			$test_result[] = gettext("Access denied!");
		} else {
			$test_result[] = sprintf(gettext("Access granted for %d Minutes in total."),$total_minutes);
		}

		return $test_result;
	}

	// if we had an error (one of the vouchers is invalid), return 0.
	// Discussion: we could return the time remaining for good vouchers, but then
	// the user wouldn't know that he used at least one invalid voucher.
	if ($error) {
		if ($total_minutes > 0)     // probably not needed, but want to make sure
			$total_minutes = 0;     // we only report -1 (expired) or 0 (no access)
		return $total_minutes;       // well, at least one voucher had errors. Say NO ACCESS
	}

	// All given vouchers were valid and this isn't simply a test.
	// Write back the used DB's
	if (is_array($bitstring)) {
		foreach ($bitstring as $roll => $used) {
			if(is_array($used)) {
				foreach($used as $u)
					voucher_write_used_db($roll, base64_encode($u));
			} else {
				voucher_write_used_db($roll, base64_encode($used));
			}
		}
	}

	// Active DB: we only add the first voucher if multiple given
	// and give that one all the time credit. This allows the user to logout and
	// log in later using just the first voucher. It also keeps username limited
	// to one voucher and that voucher shows the correct time credit in 'active vouchers'
	if (!empty($active_vouchers[$first_voucher_roll][$first_voucher])) {
		list($timestamp, $minutes) = explode(",", $active_vouchers[$first_voucher_roll][$first_voucher]);
	} else {
		$timestamp = time();    // new voucher
		$minutes = $total_minutes;
	}

	$active_vouchers[$first_voucher_roll][$first_voucher] = "$timestamp,$minutes";
	voucher_write_active_db($first_voucher_roll, $active_vouchers[$first_voucher_roll]);

267 268
	/* trigger a sync of the vouchers on config */
	voucher_save_db_to_config();
Ad Schellevis's avatar
Ad Schellevis committed
269 270 271 272

	return $total_minutes;
}

273 274 275
function voucher_configure($sync = false)
{
	global $config, $cpzone;
Ad Schellevis's avatar
Ad Schellevis committed
276

277 278 279 280 281 282 283 284 285 286 287
	$ret = true;

	if (!is_array($config['voucher'])) {
		return $ret;
	}

	foreach ($config['voucher'] as $voucherzone => $vcfg) {
		$cpzone = $voucherzone;
		$error = voucher_configure_zone($sync);
		if ($error) {
			$ret = false;
Ad Schellevis's avatar
Ad Schellevis committed
288 289
		}
	}
290 291

	return $ret;
Ad Schellevis's avatar
Ad Schellevis committed
292 293
}

294 295
function voucher_configure_zone($sync = false)
{
Ad Schellevis's avatar
Ad Schellevis committed
296 297
	global $config, $g, $cpzone;

298
	if (!isset($config['voucher'][$cpzone]['enable'])) {
Ad Schellevis's avatar
Ad Schellevis committed
299
		return 0;
300
	}
Ad Schellevis's avatar
Ad Schellevis committed
301 302 303

	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);

304 305
	/* write public key used to verify vouchers */
	$pubkey = base64_decode($config['voucher'][$cpzone]['publickey']);
306
	$fd = fopen("/var/etc/voucher_{$cpzone}.public", "w");
307 308 309 310 311 312 313
	if (!$fd) {
		captiveportal_syslog("Voucher error: cannot write voucher.public\n");
		unlock($voucherlck);
		return 1;
	}
	fwrite($fd, $pubkey);
	fclose($fd);
314
	@chmod("/var/etc/voucher_{$cpzone}.public", 0600);
315 316

	/* write config file used by voucher binary to decode vouchers */
317
	$fd = fopen("/var/etc/voucher_{$cpzone}.cfg", "w");
318 319 320 321 322 323 324
	if (!$fd) {
		captiveportal_syslog(gettext("Error: cannot write voucher.cfg") . "\n");
		unlock($voucherlck);
		return 1;
	}
	fwrite($fd, "{$config['voucher'][$cpzone]['rollbits']},{$config['voucher'][$cpzone]['ticketbits']},{$config['voucher'][$cpzone]['checksumbits']},{$config['voucher'][$cpzone]['magic']},{$config['voucher'][$cpzone]['charset']}\n");
	fclose($fd);
325
	@chmod("/var/etc/voucher_{$cpzone}.cfg", 0600);
Ad Schellevis's avatar
Ad Schellevis committed
326 327
	unlock($voucherlck);

328 329 330 331 332
	if (!$sync) {
		return 0;
	}

	captiveportal_syslog('Writing voucher db from sync data...');
Ad Schellevis's avatar
Ad Schellevis committed
333

334
	if (isset($config['voucher'][$cpzone]['roll'])) {
Ad Schellevis's avatar
Ad Schellevis committed
335 336
		$voucherlck = lock("voucher{$cpzone}", LOCK_EX);

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
		// create active and used DB per roll on ramdisk from config
		foreach ($config['voucher'][$cpzone]['roll'] as $rollent) {
			$roll = $rollent['number'];
			voucher_write_used_db($roll, $rollent['used']);
			$minutes = $rollent['minutes'];
			$active_vouchers = array();
			$a_active = &$rollent['active'];
			if (is_array($a_active)) {
				foreach ($a_active as $activent) {
					$voucher = $activent['voucher'];
					$timestamp = $activent['timestamp'];
					$minutes = $activent['minutes'];
					// its tempting to check for expired timestamps, but during
					// bootup, we most likely don't have the correct time time.
					$active_vouchers[$voucher] = "$timestamp,$minutes";
				}
			}

			voucher_write_active_db($roll, $active_vouchers);
		}
Ad Schellevis's avatar
Ad Schellevis committed
357 358

		unlock($voucherlck);
359
	}
Ad Schellevis's avatar
Ad Schellevis committed
360 361 362 363

	return 0;
}

364
/* write bitstring of used vouchers to ramdisk.
Ad Schellevis's avatar
Ad Schellevis committed
365 366
 * Bitstring must already be base64_encoded!
 */
367 368 369 370 371
function voucher_write_used_db($roll, $vdb)
{
	global $cpzone;

	$fn = "/var/db/voucher_{$cpzone}_used_{$roll}.db";
Ad Schellevis's avatar
Ad Schellevis committed
372

373
	$fd = fopen($fn, 'w');
Ad Schellevis's avatar
Ad Schellevis committed
374 375 376
	if ($fd) {
		fwrite($fd, $vdb . "\n");
		fclose($fd);
377 378 379
	} else {
		voucher_log(LOG_ERR, sprintf(gettext('Can\'t write %s'), $fn));
	}
Ad Schellevis's avatar
Ad Schellevis committed
380 381 382
}

/* return assoc array of active vouchers with activation timestamp
383
 * voucher is index.
Ad Schellevis's avatar
Ad Schellevis committed
384 385 386 387 388 389
 */
function voucher_read_active_db($roll) {
	global $g, $cpzone;

	$active = array();
	$dirty = 0;
390
	$file = "/var/db/voucher_{$cpzone}_active_{$roll}.db";
Ad Schellevis's avatar
Ad Schellevis committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404
	if (file_exists($file)) {
		$fd = fopen($file, "r");
		if ($fd) {
			while (!feof($fd)) {
				$line = trim(fgets($fd));
				if ($line) {
					list($voucher,$timestamp,$minutes) = explode(",", $line); // voucher,timestamp
					if ((($timestamp + (60*$minutes)) - time()) > 0)
						$active[$voucher] = "$timestamp,$minutes";
					else
						$dirty=1;
				}
			}
			fclose($fd);
405 406
			if ($dirty) {
				/* if we found expired entries, lets save our snapshot */
Ad Schellevis's avatar
Ad Schellevis committed
407
				voucher_write_active_db($roll, $active);
408 409
				/* trigger a sync of the vouchers on config */
				voucher_save_db_to_config();
Ad Schellevis's avatar
Ad Schellevis committed
410 411 412 413 414 415 416 417 418 419 420 421
			}
		}
	}
	return $active;
}

/* store array of active vouchers back to DB */
function voucher_write_active_db($roll, $active) {
    global $g, $cpzone;

	if (!is_array($active))
		return;
422
    $fd = fopen("/var/db/voucher_{$cpzone}_active_{$roll}.db", "w");
Ad Schellevis's avatar
Ad Schellevis committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    if ($fd) {
        foreach($active as $voucher => $value)
            fwrite($fd, "$voucher,$value\n");
        fclose($fd);
    }
}

/* return how many vouchers are marked used on a roll */
function voucher_used_count($roll) {
    global $g, $cpzone;

    $bitstring = voucher_read_used_db($roll);
    $max = strlen($bitstring) * 8;
    $used = 0;
    for ($i = 1; $i <= $max; $i++) {
438
        // check if ticket already used or not.
Ad Schellevis's avatar
Ad Schellevis committed
439 440 441 442 443 444 445 446 447 448
        $pos = $i >> 3;            // divide by 8 -> octet
        $mask = 1 << ($i % 8);  // mask to test bit in octet
        if (ord($bitstring[$pos]) & $mask)
            $used++;
    }
    unset($bitstring);

    return $used;
}

449 450 451
function voucher_read_used_db($roll)
{
	global $cpzone;
Ad Schellevis's avatar
Ad Schellevis committed
452

453 454 455 456
	$fn = "/var/db/voucher_{$cpzone}_used_{$roll}.db";
	$vdb = '';

        $fd = fopen($fn, 'r');
Ad Schellevis's avatar
Ad Schellevis committed
457
        if ($fd) {
458 459 460 461 462 463 464
		$vdb = trim(fgets($fd));
		fclose($fd);
	} else {
		voucher_log(LOG_ERR, sprintf(gettext('Can\'t read %s'), $fn));
	}

	return base64_decode($vdb);
Ad Schellevis's avatar
Ad Schellevis committed
465 466
}

467 468 469 470 471 472
function voucher_unlink_db($roll)
{
	global $cpzone;

	@unlink("/var/db/voucher_{$cpzone}_used_{$roll}.db");
	@unlink("/var/db/voucher_{$cpzone}_active_{$roll}.db");
Ad Schellevis's avatar
Ad Schellevis committed
473 474 475
}

/* we share the log with captiveportal for now */
476 477
function voucher_log($priority, $message)
{
Ad Schellevis's avatar
Ad Schellevis committed
478 479 480 481 482 483
    $message = trim($message);
    openlog("logportalauth", LOG_PID, LOG_LOCAL4);
    syslog($priority, sprintf(gettext("Voucher: %s"),$message));
    closelog();
}

484 485 486
/*
 * Save active and used voucher DB into XML config and write it to config
 * Called during reboot and every active voucher change
Ad Schellevis's avatar
Ad Schellevis committed
487
 */
488 489 490
function voucher_save_db_to_config()
{
	global $config, $cpzone;
Ad Schellevis's avatar
Ad Schellevis committed
491

492 493 494 495 496 497 498 499 500 501 502 503 504
	if (!isset($config['voucher'])) {
		return;
	}

	$needs_write = 0;

	foreach ($config['voucher'] as $voucherzone => $vcfg) {
		$cpzone = $voucherzone;
		$needs_write += voucher_save_db_to_config_zone();
	}

	if ($needs_write) {
		write_config("Backing up vouchers");
Ad Schellevis's avatar
Ad Schellevis committed
505 506 507
	}
}

508 509 510
function voucher_save_db_to_config_zone()
{
	global $config, $cpzone;
511

512 513 514 515
	if (!isset($config['voucher'][$cpzone]['enable'])) {
		// no vouchers or don't want to save DB's
		return 0;
	}
Ad Schellevis's avatar
Ad Schellevis committed
516

517 518 519
	if (!isset($config['voucher'][$cpzone]['roll'])) {
		return 0;
	}
Ad Schellevis's avatar
Ad Schellevis committed
520

521
	$voucherlck = lock("voucher{$cpzone}", LOCK_EX);
Ad Schellevis's avatar
Ad Schellevis committed
522

523 524 525 526 527 528 529 530 531
	// walk all active rolls and save runtime DBs
	$a_roll = &$config['voucher'][$cpzone]['roll'];
	while (list($key, $value) = each($a_roll)) {
		$rollent = &$a_roll[$key];
		$roll = $rollent['number'];
		$bitmask = voucher_read_used_db($roll);
		$rollent['used'] = base64_encode($bitmask);
		$active_vouchers = voucher_read_active_db($roll);
		$db = array();
Ad Schellevis's avatar
Ad Schellevis committed
532 533
		$dbi = 1;

534 535 536 537 538 539 540 541
		foreach($active_vouchers as $voucher => $line) {
			list($timestamp, $minutes) = explode(',', $line);
			$activent['voucher'] = $voucher;
			$activent['timestamp'] = $timestamp;
			$activent['minutes'] = $minutes;
			$db["v{$dbi}"] = $activent;
			$dbi++;
		}
Ad Schellevis's avatar
Ad Schellevis committed
542

543 544 545
		$rollent['active'] = $db;
		unset($active_vouchers);
	}
Ad Schellevis's avatar
Ad Schellevis committed
546

547 548 549 550
	unlock($voucherlck);

	return 1;
}