Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: add support for IP based macro #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,49 @@ function _get_language_of_wiki($id, $parent_id) {
}
return cleanID($result);
}

/**
* calc ip-adress to in_addr-representation
* @link http://www.php.net/manual/de/function.inet-pton.php#93501 source and idea
*/
function ip2pton($ipaddr) {

// Strip out the netmask, if there is one.
$cx = strpos($ipaddr, '/');
if ($cx)
{
$subnet = (int)(substr($ipaddr, $cx+1));
$ipaddr = substr($ipaddr, 0, $cx);
}
else $subnet = null; // No netmask present

// Convert address to packed format
$addr = inet_pton($ipaddr);

// Convert the netmask
if (is_integer($subnet))
{
// Maximum netmask length = same as packed address
$len = 8*strlen($addr);
if ($subnet > $len) $subnet = $len;

// Create a hex expression of the subnet mask
$mask = str_repeat('f', $subnet>>2);
switch($subnet & 3)
{
case 3: $mask .= 'e'; break;
case 2: $mask .= 'c'; break;
case 1: $mask .= '8'; break;
}
$mask = str_pad($mask, $len>>2, '0');

// Packed representation of netmask
$mask = pack('H*', $mask);
}

// Return logical and of addr and mask
return ($addr & $mask);
}

/**
* Makes user or date dependent includes possible
*/
Expand Down Expand Up @@ -876,6 +918,22 @@ function _apply_macro($id, $parent_id) {
$id = preg_replace('/@DATE(\w+)@/','', $id);
}

if(preg_match('/@IP_(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?@/',$id,$matches)) {

$ipadd=$matches[1];
$mask='24';//default mask
if(count($matches)>2){
$mask=$matches[3];
}
//dbglog('$ipadd='.$ipadd.' $mask='.$mask);

if($this->ip2pton($ipadd.'/'.$mask) === $this->ip2pton(clientIP(true).'/'.$mask)){
$id = preg_replace('/@IP_(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?@/',$ipadd.'_'.$mask, $id);
//dbglog('found!!! '.$id);
}

}

$replace = array(
'@USER@' => cleanID($user),
'@NAME@' => cleanID($INFO['userinfo']['name']),
Expand Down