Code

603dd1319a4c125907dc3ada674656eb5979a999
[gosa.git] / gosa-plugins / systems / admin / systems / class_filterSYSTEMS.inc
1 <?php
3 class filterSYSTEMS {
5   static function query($parent,$base, $scope, $filter, $attributes, $category, $objectStorage= array())
6   {
7     $ui = get_userinfo();
8     global $config;
10     // Check whether the arp handling active or not
11     $arp_handling_active = ($config->search("ArpNewDevice","CLASS",array('tabs')) != "");
13     $config= session::global_get('config');
14     $ldap= $config->get_ldap_link(TRUE);
15     $flag= ($scope == "sub")?GL_SUBSEARCH:0;
16     $entries = filterSYSTEMS::get_list($parent,$base, $filter, $attributes, $category, $objectStorage, $flag | GL_SIZELIMIT);
18     foreach($entries as $key => $entry){
19       if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'),'/')."/i", $entry['dn'])){
21         // Detect incoming types
22         if(in_array('gotoWorkstation', $entry['objectClass'])){
23           $entries[$key]['objectClass'][] = 'FAKE_OC_NewWorkstation';
24           $entries[$key]['cn'][0].= " <i>("._("New workstation").")</i>";
25         }elseif(in_array('gotoTerminal', $entry['objectClass'])){
26           $entries[$key]['objectClass'][] = 'FAKE_OC_NewTerminal';
27           $entries[$key]['cn'][0].= " <i>("._("New terminal").")</i>";
28         }elseif($arp_handling_active && 
29             in_array('GOhard', $entry['objectClass']) && 
30             !isset($entry['gotoMode'][0])){ 
31           $entries[$key]['objectClass'][] = 'FAKE_OC_ArpNewDevice';
32           $entries[$key]['cn'][0].= " <i>("._("New unknown device").")</i>";
33         }elseif(in_array('GOhard', $entry['objectClass'])){
34           $entries[$key]['objectClass'][] = 'FAKE_OC_NewDevice';
35           $entries[$key]['cn'][0].= " <i>("._("New device").")</i>";
36         }
38         // Detect SAMBA machines
39       }elseif(in_array('sambaSamAccount', $entry['objectClass']) && preg_match("/\$$/", $entry['cn'][0])){
40         $entries[$key]['objectClass'][] = 'sambaSamAccount';
42         // Detect Workstation & templates
43       }elseif(in_array('gotoWorkstation', $entry['objectClass'])){
44         if(!isset($entry['macAddress'][0]) || $entry['macAddress'][0] == "-"){
45           continue; // We do not need templates anymore, we've ogroups now.
46           //$entries[$key]['objectClass'][] = 'FAKE_OC_WorkstationTemplate';
47         }
48         $pwd_acl = $ui->get_permissions($entry['dn'],'workstation/workgeneric','userPassword');
49         if(preg_match("/w/",$pwd_acl)){
50           $entries[$key]['objectClass'][] = 'FAKE_OC_PWD_changeAble';
51         }
53         // Detect Terminal & templates
54       }elseif(in_array('gotoTerminal', $entry['objectClass'])){
55         if(!isset($entry['macAddress'][0]) || $entry['macAddress'][0] == "-"){
56           continue; // We do not need templates anymore, we've ogroups now.
57           //$entries[$key]['objectClass'][] = 'FAKE_OC_TerminalTemplate';
58         }
59         $pwd_acl = $ui->get_permissions($entry['dn'],'terminal/termgeneric','userPassword');
60         if(preg_match("/w/",$pwd_acl)){
61           $entries[$key]['objectClass'][] = 'FAKE_OC_PWD_changeAble';
62         }
64         // Detect Server & templates
65       }elseif(in_array('goServer', $entry['objectClass'])){
66         if(!isset($entry['macAddress'][0]) || $entry['macAddress'][0] == "-"){
67           continue; // We do not need templates anymore, we've ogroups now.
68           //$entries[$key]['objectClass'][] = 'FAKE_OC_ServerTemplate';
69         }
70         $pwd_acl = $ui->get_permissions($entry['dn'],'server/servgeneric','userPassword');
71         if(preg_match("/w/",$pwd_acl)){
72           $entries[$key]['objectClass'][] = 'FAKE_OC_PWD_changeAble';
73         }
75         // Detect ieee802Device
76       }elseif(in_array('ieee802Device', $entry['objectClass'])){
77         $pwd_acl = $ui->get_permissions($entry['dn'],'component/componentGeneric','userPassword');
78         if(preg_match("/w/",$pwd_acl)){
79           $entries[$key]['objectClass'][] = 'FAKE_OC_PWD_changeAble';
80         }
81       }
83       // Detect object states
84       foreach(array('gotoWorkstation','gotoTerminal','goServer') as $type){
85         if(in_array($type, $entry['objectClass'])){
87           if(isset($entry['gotoMode'][0]) && preg_match("/locked/i", $entry['gotoMode'][0])){
88             $entries[$key]['objectClass'][] = $type."__IS_LOCKED";
89           }elseif(isset($entry['FAIstate'][0])){
90             switch (preg_replace('/:.*$/', '', $entry['FAIstate'][0])) {
91               case 'error':
92                 $state= '__IS_ERROR';
93                 break;
94               case 'installing':
95               case 'install':
96               case 'sysinfo':
97               case 'softupdate':
98               case 'scheduledupdate':
99                 $state= '__IS_BUSY';
100                 break;
101               default: $state ="";
102             }
103             if(!empty($state)) $entries[$key]['objectClass'][] = $type.$state;
104           }
105         }
106       }
107     }
109     // Append opsi stuff ...
110     global $config;
111     $ui = get_userinfo();
112     if(preg_match("/FAIstate=__NETBOOT__/", $filter) && 
113         class_available('opsi') &&
114         $base == $config->current['BASE']){
116       // Get filter regex ...
117       $regex = "";
118       if(preg_match("/\(cn=[^\)\$]*\)/", $filter)){
119         $regex = trim(preg_replace("/^.*\(cn=([^\)\$]*)\).*$/","\\1", $filter),"* ");
120       }
122       // Check ACLs and opsi handle
123       $opsi_acl = $ui->get_permissions($base,"opsi/opsiProperties");
124       $opsi = new opsi($config);
125       $objects = array();
126       if($opsi instanceof opsi && $opsi->enabled() && preg_match("/r/",$opsi_acl)){
128         // Get list of opsi hosts 
129         $opsi_clients = $opsi->get_hosts_for_system_management();
130         if($opsi->is_error()){
131           msg_dialog::display(_("Error"),msgPool::siError($opsi->get_error()),ERROR_DIALOG);
132         }else{
133           foreach($opsi_clients as $entry){
134             if(empty($regex) || preg_match('/'.preg_quote($regex,'/').'/i',$entry['cn'][0])){
135               $entry['objectClass'] = array("FAKE_OC_OpsiHost");
136               $item = array('count' => count($entry));
137               foreach($entry as  $name => $value){
138                 $item[] = $name;
139                 $item[$name] = $value;
140               }
141               $entries[] = $item;
142             }
143           }
144         }
145       }
146     }
147     return($entries);
148   }
151   static function get_list($parent,$base, $filter, $attributes, $category, $objectStorage, $flags= GL_SUBSEARCH)
152   {
153     $ui= session::global_get('ui');
154     $config= session::global_get('config');
156     // Move to arrays for category and objectStorage
157     if (!is_array($category)) {
158       $category= array($category);
159     }
161     // Store in base - i.e. is a rdn value empty?
162     $storeOnBase= count($objectStorage) == 1 && empty($objectStorage[0]);
164     $method= ($storeOnBase && !($flags & GL_SUBSEARCH))?"ls":"search";
166     // Initialize search bases
167     $bases= array();
169     // Get list of sub bases to search on
170     if ($storeOnBase) {
171       $bases[$base]= "";
172     } else {
173       foreach ($objectStorage as $oc) {
174         $oc= preg_replace('/,$/', '', $oc);
175         $tmp= explode(',', $oc);
176         if (count($tmp) == 1) {
177           preg_match('/([^=]+)=(.*)$/', $oc, $m);
178           if ($flags & GL_SUBSEARCH) {
179             $bases[$base][]= $m[1].":dn:=".$m[2];
180           } else {
181             $bases["$oc,$base"][]= $m[1].":dn:=".$m[2];
182           }
183         } else {
184           // No, there's no \, in pre defined RDN values
185           preg_match('/^([^,]+),(.*)$/', $oc, $matches);
186           preg_match('/([^=]+)=(.*)$/', $matches[1], $m);
187           if ($flags & GL_SUBSEARCH) {
188             $bases[$base][]= $m[1].":dn:=".$m[2];
189           } else {
190             $bases[$matches[2].",$base"][]= $m[1].":dn:=".$m[2];
191           }
192         }
193       }
194     }
195     // Get LDAP link
196     $ldap= $config->get_ldap_link($flags & GL_SIZELIMIT);
198     // Do search for every base
199     $result= array();
200     $limit_exceeded = FALSE;
201     foreach($bases as $base => $dnFilters) {
203       // Break if the size limit is exceeded
204       if($limit_exceeded){
205         return($result);
206       }
208       // Switch to new base and search
209       if (is_array($dnFilters)){
210         $dnFilter= "(|";
211         foreach ($dnFilters as $df) {
212           $dnFilter.= "($df)";
213         }
214         $dnFilter.= ")";
215       } else {
216         $dnFilter= "";
217       }
218       $ldap->cd($base);
219       if ($method == "ls") {
220         $ldap->ls("(&$filter$dnFilter)", $base, $attributes);
221       } else {
222         $ldap->search("(&$filter$dnFilter)", $attributes);
223       }
225       // Check for size limit exceeded messages for GUI feedback
226       if (preg_match("/size limit/i", $ldap->get_error())){
227         session::set('limit_exceeded', TRUE);
228         $limit_exceeded = TRUE;
229       }
231       /* Crawl through result entries and perform the migration to the
232          result array */
233       while($attrs = $ldap->fetch()) {
234         $dn= $ldap->getDN();
236         /* Convert dn into a printable format */
237         if ($flags & GL_CONVERT){
238           $attrs["dn"]= convert_department_dn($dn);
239         } else {
240           $attrs["dn"]= $dn;
241         }
243         /* Skip ACL checks if we are forced to skip those checks */
244         if($flags & GL_NO_ACL_CHECK){
245           $result[]= $attrs;
246         }else{
248           if(preg_match("/".preg_quote(get_ou('systemIncomingRDN'),'/')."/i",$dn)){
249             $attrs['objectClass'][] = "gotoTerminal";
250           }
251           $obj = $parent->headpage->getObjectType($parent->headpage->objectTypes, $attrs['objectClass']);
253           if(isset($obj['category'])){
254             $o = $obj['category']."/".$obj['class'];
255             if(preg_match("/r/",$ui->get_permissions($dn,$o))){
256               $result[]= $attrs;
257             }
258           }
259         }
260       }
262     }
264     return $result;
265   }
268 ?>