root/contributed_modules/modules/_ep_phone_modules/modules/endpoint_config_gen.class.inc

Revision 10153, 14.4 kB (checked in by tm1000, 3 years ago)

More Phone Changes

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1 <?PHP
2 abstract class endpoint_config_gen {
3    
4   public static $brand_name = "undefined";
5   public static $family_line = "undefined";
6
7   public $brand;
8   public $family;
9
10   public $mac;
11   public $ext;
12   public $secret;
13   public $description;
14   public $srvip;
15   public $timezone;
16   public $lines;
17   public $config_files_override;
18  
19  
20   //Open configuration files and return the data from the file
21   function open_config_file($cfg_file){   
22     //if there is no configuration file over ridding the default then load up $contents with the file's information, where $key is the name of the default configuration file
23     if(!isset($this->config_files_override[$cfg_file])) {
24       $hd_file=PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/".$cfg_file;     
25       //always use 'rb' says php.net
26       $handle = fopen($hd_file, "rb");
27       $contents = fread($handle, filesize($hd_file));
28       fclose($handle);
29       return($contents);
30     } else {     
31       return($this->config_files_override[$cfg_file]);
32     }
33   }
34  
35   function parse_config_file($file_contents,$keep_unknown=FALSE) {
36     $family_data = $this->xml2array(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/family_data.xml");
37
38     if(is_array($family_data['data']['model_list'])) {
39       $key = $this->arraysearchrecursive($this->model, $family_data, "model");
40       $line_total = $family_data['data']['model_list'][$key[2]]['lines'];
41     } else {
42       $line_total = $family_data['data']['model_list']['lines'];
43     }
44    
45     if(!($line_total > 0)) {
46       die("INVALID MODEL");
47     }
48        
49     $data = $this->parse_lines($line_total,$file_contents,$keep_unknown=FALSE);
50     $data = $this->parse_config_values($data);
51    
52     return $data;
53   }
54
55   function parse_lines($line_total,$file_contents,$keep_unknown=FALSE) {
56     //Find line looping data   
57     $pattern = "/{line_loop}(.*?){\/line_loop}/si";
58       while(preg_match($pattern, $file_contents, $matches)) {
59       $i = 1;
60       $parsed = "";
61       while($i <= $line_total) {
62         if(isset($this->ext['line'][$i])) {           
63           $parsed_2 = $this->replace_static_variables($matches[1],$i,TRUE);
64           $parsed .= $this->parse_config_values($parsed_2,TRUE,$i);
65         }
66         $i++;
67       }
68       $file_contents = preg_replace($pattern, $parsed, $file_contents, 1);
69     }
70    
71    
72    
73     $i = 1;
74     while($i <= $line_total) {
75       if(isset($this->ext['line'][$i])) {
76         $file_contents = $this->replace_static_variables($file_contents,$i,FALSE);
77       }
78       $i++;
79     }
80    
81     $file_contents = $this->replace_static_variables($file_contents,"GLOBAL");
82        
83     return($file_contents);
84   }
85
86   /*
87   -Send this function the contents of a text file to $file_contents and then send an array to $custom_cfg_data
88   The key of the array is the name of the variable in the text file between "{$" and "}"
89   Then return the contents of the file with the values merged into it
90   -The $keep_unknown variable tells this function to ignore(TRUE) or blank out(FALSE) variables that it finds in $file_contents but can not find in $variables_array
91   */
92   function parse_config_values($file_contents,$keep_unknown=FALSE,$line="GLOBAL") {   
93     $family_data = $this->xml2array(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/family_data.xml");
94
95     if(is_array($family_data['data']['model_list'])) {
96       $key = $this->arraysearchrecursive($this->model, $family_data, "model");
97       $template_data_list = $family_data['data']['model_list'][$key[2]]['template_data'];
98     } else {
99       $template_data_list = $family_data['data']['model_list']['template_data'];
100     }
101    
102     $template_data = array();
103     if(is_array($template_data_list['files'])) {
104       foreach($template_data_list['files'] as $files) {
105         if(file_exists(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/".$files)) {
106           $template_data_multi = $this->xml2array(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/".$files);
107           $template_data_multi = $this->fix_single_array_keys($template_data_multi['template_data']['item']);         
108           $template_data = array_merge($template_data, $template_data_multi);
109         }
110       }
111     } else {
112       if(file_exists(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/".$template_data_list['files'])) {
113         $template_data = $this->xml2array(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/".$template_data_list['files']);
114         $template_data = $this->fix_single_array_keys($template_data_multi['template_data']['item']);
115       }
116      
117     }
118    
119     if(file_exists(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/template_data_custom.xml")) {
120       $template_data_multi = $this->xml2array(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/template_data_custom.xml");
121       $template_data_multi = $this->fix_single_array_keys($template_data_multi['template_data']['item']);
122       $template_data = array_merge($template_data, $template_data_multi);
123     }
124    
125     if(file_exists(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/template_data_".$this->model."_custom.xml")) {
126       $template_data_multi = $this->xml2array(PHONE_MODULES_PATH. static::$brand_name ."/". static::$family_line ."/template_data_".$this->model."_custom.xml");
127       $template_data_multi = $this->fix_single_array_keys($template_data_multi['template_data']['item']);
128       $template_data = array_merge($template_data, $template_data_multi);
129     }   
130    
131
132    
133     //Find all matched variables in the text file between "{$" and "}"
134     preg_match_all('/[{\$](.*?)[}]/i',$file_contents,$match);
135     //Result without brackets (but with the $ variable identifier)
136     $no_brackets = array_values(array_unique($match[1]));
137     //Result with brackets
138     $brackets = array_values(array_unique($match[0]));
139    
140    
141     //loop though each variable found in the text file
142     foreach($no_brackets as $variables) {
143       $variables = str_replace("$","",$variables);
144            
145       //Users can set defaults within template files with pipes, they will over-ride whatever is in the XML file.
146       if(strstr($variables,"|")) {
147         $variables = explode("|",$variables);
148         $default = $variables[1];
149         $variables = $variables[0];
150       } else {
151         unset($default);
152       }
153      
154       if(strstr($variables,".")) {
155         $variables = explode(".",$variables);
156         $line = $variables[2];
157         $variables = $variables[0];
158       }
159            
160       //If the variable we found in the text file exists in the variables array then replace the variable in the text file with the value under our key
161       if (($line == "GLOBAL") AND (isset($this->xml_variables['line']['global'][$variables]['value']))) {
162         $this->xml_variables['line']['global'][$variables]['value'] = htmlspecialchars($this->xml_variables['line']['global'][$variables]['value']);
163
164         $this->xml_variables['line']['global'][$variables]['value'] = $this->replace_static_variables($this->xml_variables['line']['global'][$variables]['value']);
165      
166         if (isset($default)) {
167           $file_contents=str_replace('{$'.$variables.'|'.$default.'}', $this->xml_variables['line']['global'][$variables]['value'],$file_contents);
168         } else {
169           $file_contents=str_replace('{$'.$variables.'}', $this->xml_variables['line']['global'][$variables]['value'],$file_contents);
170         }
171    
172       } elseif(($line != "GLOBAL") AND (isset($this->xml_variables['line'][$line][$variables]['value']))) {
173         $this->xml_variables['line'][$line][$variables]['value'] = htmlspecialchars($this->xml_variables['line'][$line][$variables]['value']);
174
175         $this->xml_variables['line'][$line][$variables]['value'] = $this->replace_static_variables($this->xml_variables['line'][$line][$variables]['value']);
176      
177         if (isset($default)) {
178           $file_contents=str_replace('{$'.$variables.'|'.$default.'}', $this->xml_variables['line'][$line][$variables]['value'],$file_contents);
179         } else {
180           $file_contents=str_replace('{$'.$variables.'}', $this->xml_variables['line'][$line][$variables]['value'],$file_contents);
181         }
182       } else {
183         if(!$keep_unknown) {
184           //read default template values here, blank unknowns or arrays (which are blanks anyways)
185           $key1 = $this->arraysearchrecursive('$'.$variables, $template_data, 'variable');
186
187           if (isset($default)) {
188             $file_contents=str_replace('{$'.$variables.'|'.$default.'}', $default,$file_contents);
189           }elseif((isset($template_data[$key1[0]]['default_value'])) AND (!is_array($template_data[$key1[0]]['default_value']))) {
190             $file_contents=str_replace('{$'.$variables.'}', $template_data[$key1[0]]['default_value'],$file_contents);
191           } else {
192             $file_contents=str_replace('{$'.$variables.'}', "",$file_contents);
193           }
194         }
195       }
196     }
197    
198    
199     return $file_contents;
200    
201   }
202  
203   function replace_static_variables($contents,$line="GLOBAL",$looping=TRUE) {
204     $contents=str_replace('{$srvip}', $this->srvip, $contents,$count);   
205     $contents=str_replace('{$mac}', $this->mac, $contents,$count);
206     $contents=str_replace('{$model}', $this->model, $contents,$count);
207     $contents=str_replace('{$gmtoff}', $this->timezone, $contents,$count);
208     $contents=str_replace('{$gmthr}', $this->timezone, $contents,$count);
209     $contents=str_replace('{$timezone}', $this->timezone, $contents,$count);
210    
211     if(($line != "GLOBAL") AND ($looping == TRUE)) {
212       $contents = str_replace('{$line}',$line,$contents,$count);
213       $contents=str_replace('{$ext}', $this->ext['line'][$line], $contents,$count);
214       $contents=str_replace('{$displayname}', $this->displayname['line'][$line], $contents,$count);
215       $contents=str_replace('{$secret}', $this->secret['line'][$line], $contents,$count);
216       $contents=str_replace('{$pass}', $this->secret['line'][$line], $contents,$count);
217     } elseif (($line != "GLOBAL") AND ($looping == FALSE)) {
218       $contents=str_replace('{$line.line.'.$line.'}',$line,$contents,$count);
219       $contents=str_replace('{$ext.line.'.$line.'}', $this->ext['line'][$line], $contents,$count);
220       $contents=str_replace('{$displayname.line.'.$line.'}', $this->displayname['line'][$line], $contents,$count);
221       $contents=str_replace('{$secret.line.'.$line.'}', $this->secret['line'][$line], $contents,$count);
222       $contents=str_replace('{$pass.line.'.$line.'}', $this->secret['line'][$line], $contents,$count);
223     }
224
225     return($contents);
226   }
227  
228   function fix_single_array_keys($array) {
229     if(empty($array[0])) {
230       $array_n[0] = $array;
231       return($array_n);
232     } else {
233       return($array);
234     }
235    
236   }
237  
238   function arraysearchrecursive($Needle,$Haystack,$NeedleKey="",$Strict=false,$Path=array()) {
239     if(!is_array($Haystack))
240       return false;
241     foreach($Haystack as $Key => $Val) {
242       if(is_array($Val)&&
243          $SubPath=$this->arraysearchrecursive($Needle,$Val,$NeedleKey,$Strict,$Path)) {
244         $Path=array_merge($Path,Array($Key),$SubPath);
245         return $Path;
246       }
247       elseif((!$Strict&&$Val==$Needle&&
248               $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))||
249               ($Strict&&$Val===$Needle&&
250                $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))) {
251         $Path[]=$Key;
252         return $Path;
253       }
254     }
255     return false;
256   }
257  
258   function xml2array($url, $get_attributes = 1, $priority = 'tag')
259   {
260     $contents = "";
261     if (!function_exists('xml_parser_create'))
262     {
263       return array ();
264     }
265     $parser = xml_parser_create('');
266     if(!($fp = @ fopen($url, 'rb')))
267     {
268       return array ();
269     }
270     while(!feof($fp))
271     {
272       $contents .= fread($fp, 8192);
273     }
274     fclose($fp);
275     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
276     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
277     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
278     xml_parse_into_struct($parser, trim($contents), $xml_values);
279     xml_parser_free($parser);
280     if(!$xml_values)
281     {
282       return; //Hmm...
283     }
284     $xml_array = array ();
285     $parents = array ();
286     $opened_tags = array ();
287     $arr = array ();
288     $current = & $xml_array;
289     $repeated_tag_index = array ();
290     foreach ($xml_values as $data)
291     {
292       unset ($attributes, $value);
293       extract($data);
294       $result = array ();
295       $attributes_data = array ();
296       if (isset ($value))
297       {
298         if($priority == 'tag')
299         {
300           $result = $value;
301         }
302         else
303         {
304           $result['value'] = $value;
305         }
306       }
307       if(isset($attributes) and $get_attributes)
308       {
309         foreach($attributes as $attr => $val)
310         {
311           if($priority == 'tag')
312           {
313             $attributes_data[$attr] = $val;
314           }
315           else
316           {
317             $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
318           }
319         }
320       }
321       if ($type == "open")
322       {
323         $parent[$level -1] = & $current;
324         if(!is_array($current) or (!in_array($tag, array_keys($current))))
325         {
326           $current[$tag] = $result;
327           if($attributes_data)
328           {
329             $current[$tag . '_attr'] = $attributes_data;
330           }
331           $repeated_tag_index[$tag . '_' . $level] = 1;
332           $current = & $current[$tag];
333         }
334         else
335         {
336           if (isset ($current[$tag][0]))
337           {
338             $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
339             $repeated_tag_index[$tag . '_' . $level]++;
340           }
341           else
342           {
343             $current[$tag] = array($current[$tag],$result);
344             $repeated_tag_index[$tag . '_' . $level] = 2;
345             if(isset($current[$tag . '_attr']))
346             {
347               $current[$tag]['0_attr'] = $current[$tag . '_attr'];
348               unset ($current[$tag . '_attr']);
349             }
350           }
351           $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
352           $current = & $current[$tag][$last_item_index];
353         }
354       }
355       else if($type == "complete")
356       {
357         if(!isset ($current[$tag]))
358         {
359           $current[$tag] = $result;
360           $repeated_tag_index[$tag . '_' . $level] = 1;
361           if($priority == 'tag' and $attributes_data)
362           {
363             $current[$tag . '_attr'] = $attributes_data;
364           }
365         }
366         else
367         {
368           if (isset ($current[$tag][0]) and is_array($current[$tag]))
369           {
370             $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
371             if ($priority == 'tag' and $get_attributes and $attributes_data)
372             {
373               $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
374             }
375             $repeated_tag_index[$tag . '_' . $level]++;
376           }
377           else
378           {
379             $current[$tag] = array($current[$tag],$result);
380             $repeated_tag_index[$tag . '_' . $level] = 1;
381             if ($priority == 'tag' and $get_attributes)
382             {
383               if (isset ($current[$tag . '_attr']))
384               {
385                 $current[$tag]['0_attr'] = $current[$tag . '_attr'];
386                 unset ($current[$tag . '_attr']);
387               }
388               if ($attributes_data)
389               {
390                 $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
391               }
392             }
393             $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
394           }
395         }
396       }
397       else if($type == 'close')
398       {
399         $current = & $parent[$level -1];
400       }
401     }
402     return ($xml_array);
403   }
404  
405 }
406
407 ?>
Note: See TracBrowser for help on using the browser.