[Xaren_Client_IP]

Description

Link: [Xaren_Client_IP]
Author: Adam Randall
Category: Client
Version: 8.x
License: Public Domain
Posted: May. 13, 2008
Updated: May. 13, 2008
More by this author...
The client_ip type that is provided with Lasso Professional 8 and beyond is lacking in the amount of control you have over an IP. You can only check if an IP is in a 32, 24, 16, or 8 subnet. You should be able to validate against any CIDR or subnet notation. That's what this tag provides.

Parameters

-ip string, optional Manual IP instead of the current client IP

Sample Usage

// returns true for IPs 10.0.1.0 through 10.0.1.31
xaren_client_ip == '10.0.1.0/27'
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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
[

	namespace_using( '_global_Xaren_Client_' );

		log_critical( 'Loading Namespace: Xaren_Client_' );

		define_type( 'IP' );

			local( 'ip' );

			define_tag( 'onCreate',
				-optional = 'ip' );

				if( ! local_defined( 'ip' ) );
					self->'ip' = ( _client_ip_old->size ? _client_ip_old | false );
				else( #ip->type == 'string' && ! self->aton( #ip ) );
					self->'ip' = false;
				else( #ip->type == 'string' );
					self->'ip' = #ip;
				else( #ip->type == 'integer' );
					local( 'ntoa' = self->ntoa( #ip ) );
					self->'ip' = ( #ntoa === false ? false | #ntoa );
				else;
					self->'ip' = false;
				/if;

			/define_tag;

			define_tag( 'onConvert' );
				self->'ip' === false ?
					return( false );
				return( params->get(1) == 'integer' ? self->aton( self->'ip' ) | string( self->'ip' ) );
			/define_tag;

			define_tag( 'onCompare' );
				self->'ip' === false ?
					return( true );

				local(
					'i'    = null,
					't'    = null,
					'rhs'  = params->get(1),
					'ip'   = null,
					'cidr' = null );

				// make sure all characters are okay
				string_replaceregexp( -find = '[.0-9/]+', -replace = '', #rhs )->size ?
					return( true );

				if( #rhs->find( '/' ) > 0 );
					#t = #rhs->split( '/' );
					#t->size > 2 ? return( true ); // return if the input is invalid

					! string_isdigit( #t->get(2) ) ?
						#t->get(2) = self->subnettocidr( #t->get(2) );

					#t->get(2) === false ? return( true );

					return( ! self->within( #t->get(1), integer( #t->get(2) ) ) );
				else;
					return( ! self->within( #rhs, 32) );
				/if;

				return( true );
			/define_tag;


			define_tag( 'aton',
				-optional = 'ip',
				-type     = 'string' );

				if( ! local_defined( 'ip' ) );
					self->'ip' === false ?
						return( false );

					local( 'ip' = self->'ip' );
				else( string_replaceregexp( -find = '[.0-9]', -replace = '', #ip )->size > 0 );
					return( false );
				/if;

				local(
					'parts' = #ip->split( '.' ),
					'n'     = null );

				#parts->size < 4 ? return( false );

				#n = ( integer( #parts->get(1) ) * 256 * 256 * 256 ) +
						 ( integer( #parts->get(2) ) * 256 * 256 ) +
						 ( integer( #parts->get(3) ) * 256 ) +
						 integer( #parts->get(4) );

				return( #n > 4294967295 ? false | #n );

			/define_tag;


			define_tag( 'ntoa',
				-required = 'long',
				-type     = 'integer',
				-copy );

				if( #long < 0 || #long > 4294967295 ?
					return( false );

				local( 'ip' = '' );

				loop( 4 );
					#ip = string( integer( #long )->bitand( 255 ) & ) + ( loop_count > 1 ? '.' #ip );
					#long->bitshiftright( 8 );
				/loop;

				return( #ip );

			/define_tag;


			define_tag( 'subnettocidr',
				-required = 'ip' );

				#ip->isa( 'integer' ) ? return( @#ip );

				// set this up in case we are doing a lot of lookups
				! var_defined( '__cidrs__' ) ?
					var(
						'__cidrs__' = map(
							'255.255.255.255' = 32,
							'255.255.255.254' = 31,
							'255.255.255.252' = 30,
							'255.255.255.248' = 29,
							'255.255.255.240' = 28,
							'255.255.255.224' = 27,
							'255.255.255.192' = 26,
							'255.255.255.128' = 25,
							'255.255.255.0'   = 24,
							'255.255.254.0'   = 23,
							'255.255.252.0'   = 22,
							'255.255.248.0'   = 21,
							'255.255.240.0'   = 20,
							'255.255.224.0'   = 19,
							'255.255.192.0'   = 18,
							'255.255.128.0'   = 17,
							'255.255.0.0'     = 16,
							'255.254.0.0'     = 15,
							'255.252.0.0'     = 14,
							'255.248.0.0'     = 13,
							'255.240.0.0'     = 12,
							'255.224.0.0'     = 11,
							'255.192.0.0'     = 10,
							'255.128.0.0'     =  9,
							'255.0.0.0'       =  8,
							'254.0.0.0'       =  7,
							'252.0.0.0'       =  6,
							'248.0.0.0'       =  5,
							'240.0.0.0'       =  4,
							'224.0.0.0'       =  3,
							'192.0.0.0'       =  2,
							'128.0.0.0'       =  1,
							'0.0.0.0'         =  0 ) );

				local( 'cidr' = $__cidrs__->find( #ip ) );

				return( #cidr->isa( 'integer' ) ? #cidr | false );

			/define_tag;


			define_tag( 'within',
				-required = 'net',
				-type     = 'string',
				-copy,
				-required = 'cidr',
				-type     = 'integer' );

				self->'ip' === false ?
					return( false );

				#cidr < 0 || #cidr > 32 ?
					return( false );

				local(
					'ip'        = self->aton( self->'ip' ),
					'rede'      = self->aton( #net ),
					'real_mask' = null,
					'netmask'   = null,
					'res'       = null );

				#real_mask = 0;
				#netmask   = #rede;
				#res       = #ip;

				#rede === false ?
					return( false );

				#real_mask->bitnot & bitshiftleft( 32 - #cidr );

				#netmask->bitand( #real_mask );
				#res->bitand( #real_mask );

				return( #res == #netmask );

			/define_tag;

		/define_type;

	/namespace_using;
]

 

Comments

none

Email:


Password:



Newest

Most Popular