17 require_once SQ_CORE_PACKAGE_PATH.
'/system/conditions/condition/condition_edit_fns.inc';
18 require_once SQ_FUDGE_PATH.
'/csv/csv.inc';
19 require_once dirname(__FILE__).
'/condition_user_ip.inc';
46 parent::__construct();
64 if (!isset($condition_data[
'user_ip_ranges'])) {
65 $condition_data[
'user_ip_ranges'] = Array();
68 if (!isset($condition_data[
'default_grant'])) {
69 $condition_data[
'default_grant'] = FALSE;
73 $condition_data[
'user_ip_ranges'][
'new'] = Array(
'ip_address' => NULL,
'subnet' => NULL,
'grant' => 1);
76 $grant_options = Array(1 => translate(
'grant'), 0 => translate(
'deny'));
78 if (!$write_access && empty($condition_data[
'user_ip_ranges'])) {
79 ?><em><?php echo translate(
'condition_user_ip_no_conditions_set'); ?></em><?php
82 <table style=
"width: auto" class=
"sq-backend-table">
84 <?php
if ($write_access) {
90 <th><?php echo translate(
'network_ip_address'); ?></th>
92 <th><?php echo translate(
'subnet_mask'); ?></th>
93 <th><?php echo translate(
'action'); ?></th>
94 <?php
if ($write_access) {
96 <th><?php echo translate(
'delete_question'); ?></th>
101 <?php
foreach ($condition_data[
'user_ip_ranges'] as $index => $ranges) {
104 <?php
if ($write_access) {
108 if ($index ===
'new') {
109 echo
'<strong>'.translate(
'new_question').
'</strong>';
119 self::paintIPAddressField($prefix.
'['.$index.
'][ip_address]', $ranges[
'ip_address']);
121 echo $ranges[
'ip_address'];
131 self::paintSubnetMaskField($prefix.
'['.$index.
'][subnet]', $ranges[
'subnet']);
133 echo $ranges[
'subnet'];
141 combo_box($prefix.
'['.$index.
'][grant]', $grant_options, FALSE, Array($ranges[
'grant']));
144 ?><strong style=
"color: <?php echo $ranges['grant'] ? 'green' : 'red' ?>"><?php
145 echo $grant_options[$ranges[
'grant']];
149 <td style=
"text-align: center">
151 if ($write_access && ($index !==
'new')) {
152 check_box($prefix.
'['.$index.
'][delete]', FALSE);
166 echo translate(
'condition_user_ip_default_grant').
': ';
168 combo_box($prefix.
'[default_grant]', $grant_options, FALSE, Array($condition_data[
'default_grant']));
170 ?><strong style=
"color: <?php echo $condition_data['default_grant'] ? 'green' : 'red' ?>"><?php
171 echo $grant_options[$condition_data[
'default_grant']];
178 echo
'<b>'.translate(
'condition_user_ip_import_from_file').
':</b> ';
179 file_upload($prefix.
'_import');
180 echo
'<br/>'.translate(
'condition_user_ip_import_from_file_explain').
'</p>';
196 if (isset($_FILES[$prefix.
'_import']) && ($_FILES[$prefix.
'_import'][
'error'] == 0)) {
197 $file_name = $_FILES[$prefix.
'_import'][
'tmp_name'];
198 if (is_uploaded_file($file_name)) {
199 $condition_data = self::importRulesFromCSV($file_name);
200 if ($condition_data !== FALSE) {
201 $condition_data[
'default_grant'] = $_POST[$prefix][
'default_grant'];
202 return $condition_data;
204 trigger_localised_error(
'CORE0249', E_USER_WARNING);
212 $ip_ranges = Array();
214 foreach ($_POST[$prefix] as $index => $post_data) {
215 if (!isset($post_data[
'delete'])) {
217 'ip_address' => self::processIPAddressField($post_data[
'ip_address']),
218 'subnet' => self::processSubnetMaskField($post_data[
'subnet']),
219 'grant' => $post_data[
'grant'],
223 if (empty($this_row[
'ip_address']))
continue;
226 if (in_array($this_row, $ip_ranges))
continue;
230 $this_row[
'grant'] = !$this_row[
'grant'];
231 if (in_array($this_row, $ip_ranges)) {
232 trigger_localised_error(
'CORE0250', E_USER_WARNING, $this_row[
'ip_address'], $this_row[
'subnet']);
238 if (self::_doesIPBeginSubnet($this_row[
'ip_address'], $this_row[
'subnet'])) {
240 $this_row[
'grant'] = $post_data[
'grant'];
241 $ip_ranges[] = $this_row;
243 trigger_localised_error(
'CORE0248', E_USER_WARNING, $this_row[
'ip_address'], $this_row[
'subnet']);
249 'user_ip_ranges' => $ip_ranges,
250 'default_grant' => $_POST[$prefix][
'default_grant'],
270 if ($value == NULL) $value =
'...';
271 $value = explode(
'.', $value);
273 text_box($prefix.
'[0]', $value[0], 3, 0, 3);
275 text_box($prefix.
'[1]', $value[1], 3, 0, 3);
277 text_box($prefix.
'[2]', $value[2], 3, 0, 3);
279 text_box($prefix.
'[3]', $value[3], 3, 0, 3);
295 for ($i = 0; $i <= 3; $i++) {
296 if ($octets[$i] ==
'')
return FALSE;
300 $octets[0] = (int)$octets[0];
301 $octets[1] = (int)$octets[1];
302 $octets[2] = (int)$octets[2];
303 $octets[3] = (int)$octets[3];
305 for ($i = 0; $i <= 3; $i++) {
306 if (($octets[$i] < 0) || ($octets[$i] > 255)) {
311 return implode(
'.', $octets);
329 $possible_bitmasks = self::_getPossibleSubnetOctets();
331 if ($value == NULL) $value =
'255.255.255.255';
332 $value = explode(
'.', $value);
334 combo_box($prefix.
'[0]', $possible_bitmasks, FALSE, Array($value[0]));
336 combo_box($prefix.
'[1]', $possible_bitmasks, FALSE, Array($value[1]));
338 combo_box($prefix.
'[2]', $possible_bitmasks, FALSE, Array($value[2]));
340 combo_box($prefix.
'[3]', $possible_bitmasks, FALSE, Array($value[3]));
356 $octets[0] = (int)$octets[0];
357 $octets[1] = (int)$octets[1];
358 $octets[2] = (int)$octets[2];
359 $octets[3] = (int)$octets[3];
363 $ip_address = implode(
'.', $octets);
365 if (self::_isValidSubnetMask($ip_address)) {
368 trigger_error(
'Subnet mask entered is not valid', E_USER_WARNING);
391 if (!self::_isValidSubnetMask($subnet_ip)) {
395 $octets = explode(
'.', $subnet_ip);
398 for ($i = 0; $i <= 3; $i++) {
399 if ($octets[$i] == 0)
break;
400 while ($octets[$i] > 0) {
401 if ($octets[$i] % 2 == 1) $cidr_value++;
426 $cidr_value = (int)$cidr_value;
427 if (($cidr_value < 0) || ($cidr_value > 32)) {
431 $octets = Array(0, 0, 0, 0);
434 while ($cidr_value > 0) {
435 if ($cidr_value > 8) {
438 $octets[$current_octet] = 255;
442 for ($i = 0; $i < 8; $i++) {
443 $octets[$current_octet] <<= 1;
444 if ($cidr_value > 0) {
445 $octets[$current_octet]++;
452 return implode(
'.', $octets);
470 public static function isInSubnet($ip_address, $network_ip, $subnet_ip)
472 $ip_octets = explode(
'.', $ip_address);
473 $network_octets = explode(
'.', $network_ip);
474 $subnet_octets = explode(
'.', $subnet_ip);
476 for ($i = 0; $i <= 3; $i++) {
477 $masked_octet = (int)$ip_octets[$i] & (
int)$subnet_octets[$i];
478 if ($masked_octet != (
int)$network_octets[$i]) {
498 $bitmasks = self::_getPossibleSubnetOctets();
500 $octets = explode(
'.', $subnet_ip);
501 $zero_bit_found = FALSE;
503 for ($i = 0; $i <= 3; $i++) {
504 if ($zero_bit_found && ($octets[$i] != 0)) {
508 }
else if ($octets[$i] != 255) {
510 $zero_bit_found = TRUE;
514 if (!isset($bitmasks[$octets[$i]]))
return FALSE;
532 $ip_ranges_by_cidr = Array();
534 foreach ($ip_ranges as $ip_range) {
535 $cidr_value = self::convertSubnetIPtoCIDR($ip_range[
'subnet']);
536 $ip_ranges_by_cidr[$cidr_value][] = $ip_range;
539 ksort($ip_ranges_by_cidr);
541 $ip_ranges = Array();
542 foreach ($ip_ranges_by_cidr as $cidr_ip_range) {
543 $ip_ranges = array_merge($ip_ranges, $cidr_ip_range);
589 $ip_octets = explode(
'.', $ip_address);
590 $subnet_octets = explode(
'.', $subnet_ip);
592 for ($i = 0; $i <= 3; $i++) {
593 if (($ip_octets[$i] & (~(
int)$subnet_octets[$i])) != 0) {
616 $min_values = Array(0, 0, 0, 0, 1, 0);
617 $max_values = Array(255, 255, 255, 255, 32, 1);
619 $condition_data = Array(
'user_ip_ranges' => Array());
620 $condition_data[
'default_grant'] = 0;
623 $csv->setFilepath($file_name);
625 $lines = $csv->values;
628 $ip_ranges = Array();
630 foreach ($lines as $line_fields) {
632 if (count($line_fields) != 6)
return FALSE;
634 foreach ($line_fields as $index => $value) {
635 if (!(is_numeric($value) && ($value >= $min_values[$index]) && ($value <= $max_values[$index]))) {
640 $ip_address = implode(
'.', array_slice($line_fields, 0, 4));
641 $subnet_mask = self::convertCIDRtoSubnetIP($line_fields[4]);
642 $grant = $line_fields[5];
644 $ip_range_field = Array(
645 'ip_address' => $ip_address,
646 'subnet' => $subnet_mask,
651 if (in_array($ip_range_field, $ip_ranges))
continue;
655 $ip_range_field[
'grant'] = !$ip_range_field[
'grant'];
656 if (in_array($ip_range_field, $ip_ranges)) {
657 trigger_localised_error(
'CORE0250', E_USER_WARNING, $ip_range_field[
'ip_address'], $ip_range_field[
'subnet']);
662 if (!self::_doesIPBeginSubnet($ip_range_field[
'ip_address'], $ip_range_field[
'subnet'])) {
663 trigger_localised_error(
'CORE0248', E_USER_WARNING, $ip_range_field[
'ip_address'], $ip_range_field[
'subnet']);
668 $ip_range_field[
'grant'] = !$ip_range_field[
'grant'];
669 $ip_ranges[] = $ip_range_field;
672 $condition_data[
'user_ip_ranges'] = $ip_ranges;
674 return $condition_data;