Completed
Push — master ( 35a619...c36a21 )
by Robbert
100:00 queued 90:25
created

ldap_compiler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 4 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
  include_once($store->get_config("code")."stores/modules/sql_compiler.php");
3
4
  class ldap_compiler extends sql_compiler {
5
	function __construct(&$store, $mappings) {
6
		debug("ldap_compiler($tbl_prefix)", "store");
0 ignored issues
show
Bug introduced by
The variable $tbl_prefix does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
7
		$this->tbl_prefix=$tbl_prefix;
0 ignored issues
show
Bug introduced by
The property tbl_prefix does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
8
		$this->store=$store;
9
		$this->buildlist=array();
0 ignored issues
show
Bug introduced by
The property buildlist does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
10
		$this->mappings=$mappings;
0 ignored issues
show
Bug introduced by
The property mappings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
11
	}
12
13
	function compile_tree(&$node, $arguments=null) {
14
15
		if ($arguments) {
16
			extract($arguments);
17
		}
18
19
		switch ((string)$node["id"]) {
20
			case 'implements':
21
				$result="prop_object.AR_implements";
22
			break;
23
			case 'property':
24
				$table=$node["table"];
25
				$field=$node["field"];
26
				$result="$table.$field";
27
			break;
28
			case 'ident':
29
				$table=$node["table"];
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
				$field=$node["field"];
31
				$result="prop_object.AR_$field";
32
			break;
33
			case 'custom':
34
				$table="prop_custom";
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
				$field=$node["field"];
36
				$result="prop_my.AR_$field";
37
			break;
38
			case 'string':
39
				// LDAP filters don't have quotes around strings
40
				$result=$node["value"];
41
				$result=substr($result, 1, -1);
42
			break;
43
			case 'float':
44
			case 'int':
45
				$result=$node["value"];
46
			break;
47 View Code Duplication
			case 'and':
48
				if (preg_match("/^&[0-9]+$/", $this->buildlist[sizeof($this->buildlist)-1]) &&
49
				    !$this->groupingflag) {
50
					$this->buildlist[sizeof($this->buildlist)-1]="&".
51
				          (substr($this->buildlist[sizeof($this->buildlist)-1], 1)+1);
52
				} else {
53
					array_push($this->buildlist, '&2');
54
					$this->groupingflag=false;
0 ignored issues
show
Bug introduced by
The property groupingflag does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
				}
56
				$this->compile_tree($node["left"]);
57
				$this->compile_tree($node["right"]);
58
			break;
59 View Code Duplication
			case 'or':
60
				if (preg_match("/^\|[0-9]+/$", $this->buildlist[sizeof($this->buildlist)-1]) &&
61
				    !$this->groupingflag) {
62
					$this->buildlist[sizeof($this->buildlist)-1]="|".
63
				          (substr($this->buildlist[sizeof($this->buildlist)-1], 1)+1);
64
				} else {
65
					array_push($this->buildlist, '|2');
66
					$this->groupingflag=false;
67
				}
68
				$this->compile_tree($node["left"]);
69
				$this->compile_tree($node["right"]);
70
			break;
71
			case 'cmp':
72
				$not=false;
73
				$joker=false;
74
				$operator=$node["operator"];
75
				switch ($operator) {
76
					case '!~':
77
					case '!~~':
0 ignored issues
show
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
78
						$not=true;
79
 					case '~=':
80
					case '=~':
81
					case '=~~':
82
						$joker=true;
83
						$ecompare = '=';
84
						break;
85
					case '!=':
86
						$not=true;
87
						// fall through
88
					case '=':
89
					case '==':
90
						$ecompare = '=';
91
						break;
92
					case '>':
93
						$not=true;
94
						// fall through
95
					case '<=':
96
						$ecompare = '<=';
97
						break;
98
					case '<':
99
						$not=true;
100
						// fall through
101
					case '>=':
102
						$ecompare = '>=';
103
						break;
104
				}
105
				$left=$this->compile_tree($node["left"]);
106
				$right=$this->compile_tree($node["right"]);
107
108
				// Quote the characters that have a special meaning in LDAP filters
109
				// as per RFC 2254
110
				$ldapspecial=array("\\", "*", "(", ")", "\x00");
111
				$ldapreplace=array("\\5c", "\\2a", "\\28", "\\29", "\\00");
112
				$evalue=str_replace($ldapspecial, $ldapreplace, $right);
113
114
				if ($joker) {
115
					// Replace '%' with '*' for the LDAP filter
116
					$evalue=str_replace('%', '*', $evalue);
117
				}
118
119
				if (isset($this->mappings["$left$operator$right"])) {
120
					// Complete translation (property name and value)
121
					$result=$this->mappings["$left$operator$right"];
122
				} elseif (isset($this->mappings["$left"])) {
123
					// Only the property name should be translated
124
					$result=$this->mappings["$left"].$ecompare.$evalue;
125
				} elseif (substr($left, 0, 13)=="prop_ldap.AR_") {
126
					// Literal ldap attribute name given
127
					$result=substr($left, 13).$ecompare.$evalue;
128
				} else {
129
					// Hmm, unknown property, maybe we should throw
130
					// an error here? (TODO)
131
					$result="$left$ecompare$evalue";
132
				}
133
134
				// Ignore the parent property, is it makes
135
				// no sense here (objectclass=* matches
136
				// every LDAP object)
137
				if ($left=='prop_object.AR_parent') {
138
					$result='objectclass=*';
139
				}
140
141
				if ($not) {
142
					$result="!($result)";
143
				}
144
145
				array_push($this->buildlist, $result);
146
			break;
147
			case 'group':
148
				$this->groupingflag=true;
149
				$left=$this->compile_tree($node["left"]);
0 ignored issues
show
Unused Code introduced by
$left is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
150
			break;
151
152 View Code Duplication
			case 'orderby':
153
				$result=$this->compile_tree($node["left"]);
154
				$this->orderby_s=$this->compile_tree($node["right"]);
0 ignored issues
show
Bug introduced by
The property orderby_s does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
155
			break;
156
157
			case 'orderbyfield':
158
				$this->in_orderby = true;
0 ignored issues
show
Bug introduced by
The property in_orderby does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
159
				$left=$this->compile_tree($node["left"]);
0 ignored issues
show
Unused Code introduced by
$left is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
160
				$right=$this->compile_tree($node["right"]);
161
				if (isset($this->mappings["$right"])) {
162
					// Property name should be translated
163
					$right=$this->mappings["$right"];
164
				} elseif (substr($right, 0, 13)=="prop_ldap.AR_") {
165
					// Literal ldap attribute name given
166
					$right=substr($right, 13);
167
				} else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
168
					// Hmm, unknown property, maybe we should throw
169
					// an error here? (TODO)
170
				}
171
				$this->orderbyfield=$right;
0 ignored issues
show
Bug introduced by
The property orderbyfield does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
172
			break;
173
174
			case 'limit':
175
				$this->compile_tree($node["left"]);
176
				// Ignore limit and offset as LDAP does
177
				// not support it as complete as we need it
178
			break;
179
		}
180
181
		return $result;
182
	}
183
184
	// ldap specific compiler function
185
	function priv_sql_compile($tree) {
186
		$this->compile_tree($tree);
187
188
		//print implode(" - ", $this->buildlist)."<br>\n";
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
189
190
		$query="";
191
		$groupstack=array();
192
		foreach ($this->buildlist as $item) {
193
			if (sizeof($groupstack)>0) {
194
				$groupstack[sizeof($groupstack)-1]--;
195
			}
196
			if (preg_match("/^[&\|][0-9]+$/", $item)) {
197
				array_push($groupstack, substr($item, 1));
198
				$query.="(".substr($item, 0, 1);
199
			} else {
200
				$query.="(".$item.")";
201
			}
202
			if (sizeof($groupstack)>0 &&
203
			    $groupstack[sizeof($groupstack)-1]==0) {
204
				array_pop($groupstack);
205
				$query.=")";
206
			}
207
		}
208
209
		if (sizeof($groupstack)) {
210
			$query.=")";
211
		}
212
213
		return $query;
214
	}
215
216
  }
217