Ticket #3291: retrieve_op_conf_from_mysql.3.php

File retrieve_op_conf_from_mysql.3.php, 23.5 kB (added by Nick_Lewis, 2 years ago)
Line 
1 #!/usr/bin/php
2 <?php
3
4 # Retrieves the sip user/peer entries from the database for the flash operator panel
5
6 //include("/var/www/html/admin/functions.inc.php");
7 //include("DB.php");
8 require_once("/var/www/html/admin/functions.inc.php");
9 require_once("DB.php");
10
11 ################### BEGIN OF CONFIGURATION ####################
12
13 if ($argc == 2)
14 {
15   $amportalconf = $argv[0];
16   $zapataconf = $argv[1]."/zapata.conf";
17   $zapataautoconf = $argv[1]."/zapata-auto.conf";
18 } else
19 {
20   $amportalconf = "/etc/amportal.conf";
21   $zapataconf="/etc/asterisk/zapata.conf";
22   $zapataautoconf="/etc/asterisk/zapata-auto.conf";
23 }
24
25 ######## LAYOUT INFO #########
26
27 # NOTE - These values may be overwritten by values in a table in the freepbx database named "panel"
28
29 # structure is - ID, Legend, startpos, stoppos, color1, color2
30 $rectangle1 = array("trunk","Trunks", 53, 80, "10ff10", "009900");
31 $rectangle2 = array("extension","Extensions", 1, 40, "1010ff", "99cccc");
32 $rectangle3 = array("parking","Parking lots", 49, 72, "ffff10", "cc9933");
33 $rectangle4 = array("conference","Conferences", 45, 68, "006666", "00a010");
34 $rectangle5 = array("queue","Queues", 41, 64, "ff1010", "a01000");
35 $rectangles = array($rectangle1,$rectangle2,$rectangle3,$rectangle4,$rectangle5);
36
37 $rectmarginx = 1;
38 $rectmarginy = 1;
39 $legendoffsetx = 3;
40 $legendoffsety = 1;
41
42 # $layoutbuttonsonly = 0 : allow display of buttons even if no corresponding layout info
43 # $layoutbuttonsonly = 1 : suppress display of buttons if no corresponding layout info
44 $layoutbuttonsonly = 1;
45
46 ######## BUTTON INFO #########
47 $buttonsizex = 246; # 1+244+1 from information in op_style.cfg
48 $buttonsizey = 28; # 1+26+1 from information in op_style.cfg
49 $numbuttonsx = 4;
50 $numbuttonsy = 20;
51 $buttonsoriginx = -1;
52 $buttonsoriginy = 32;
53
54
55 ######## STYLE INFO #########
56
57 # NOTE - These values may be overwritten by the syleinfo function with values generated from the layout info
58
59 $extenpos="2-40";
60 $trunkpos="53-60,72-80";
61 $parkingpos="50-51,69-71";
62 $confepos="46-48,65-68";
63 $queuepos="42-44,61-64";
64
65
66
67 # Remove or add Zap trunks as needed
68 # Note: ZAP/* will match any ZAP channel that *is not referenced* in another button (ie: extensions)
69 $zaplines=array(); # zap channel, description
70 #array_push($zaplines,array( "Zap/*","PSTN" ));
71 #array_push($zaplines,array( "Zap/1","Zap 1" ));
72 #array_push($zaplines,array( "Zap/2","Zap 2" ));
73 #array_push($zaplines,array( "Zap/3","Zap 3" ));
74 #array_push($zaplines,array( "Zap/4","Zap 4" ));
75
76 if (file_exists($zapataconf)) {
77   parse_zapata($zaplines,$zapataconf);
78 }
79 // Now no need to parse other files as include declarations are followed
80 //if (file_exists($zapataautoconf)) {
81 //  parse_zapata($zaplines,$zapataautoconf);
82 //}
83
84 function parse_zapata(&$zaplines,$conffile) {
85   # LETS PARSE zapata.conf
86   # Allowed format options
87   # %c Zap Channel number
88   # %n Line number
89   # %N Line number, but restart counter
90   # Example:
91   # ;AMPLABEL:Channel %c - Button %n
92
93   //global $zaplines; passed by reference instead
94   global $ampwildcard;
95   global $zaplabel;
96   global $istrunk;
97   global $lastlabelnum;
98
99   if (!isset($ampwildcard)) {$ampwildcard=0;}
100   if (!isset($zaplabel)) {$zaplabel="Zap %c";}
101   if (!isset($istrunk)) {$istrunk=0;}
102   if (!isset($lastlabelnum)) {$lastlabelnum=0;}
103
104   $filearray = file($conffile);
105   if ($filearray === false) {die("Cannot open config file: $conffile\n");}
106   foreach ($filearray as $line) {
107     $line = trim($line);
108     if ($line == '') {continue;} //was next in perl version
109
110     $temparray = @explode(";AMPWILDCARDLABEL(",$line,2);
111     if (count($temparray) == 2) {
112       $temparray = @explode("):",$temparray[1],2);
113       if (count($temparray) != 2)  {continue;}
114       array_push($zaplines,array("Zap/*",trim($temparray[1]),($temparray[0])));
115       $ampwildcard=1;
116       continue;
117     }
118
119     $temparray = @explode(";AMPLABEL:",$line,2);
120     if (count($temparray) == 2) {
121       $zaplabel=trim($temparray[1]);
122       $ampwildcard=0;
123       if (strpos($temparray[1],"%N") === false) {continue;}
124       $lastlabelnum=0;
125       continue;
126     }
127
128     # remove comments
129     list($line,$comment) = explode(";",$line,2);
130         $line = trim($line);
131     if ($line == "") {continue;}
132
133     #normalize whitespace
134     $line = str_replace("\t"," ",$line);
135     $line = preg_replace("/(\040)+/", " ", $line);
136
137         # check if an include declaration
138     @list($command,$include) = explode("#include ",$line,2);
139     if (isset($include))  {
140       parse_zapata(&$zaplines,trim($include));
141       continue;
142         }
143
144     #normalize assignment operator
145     $line = str_replace("=>","=",$line);
146     $line = str_replace(" ","",$line);
147
148     echo "Debug: " . $line . "\n";
149
150         # check if trunk or extension
151         //note that some versions of php do not support !== but only ===
152         if (!(strpos($line,"context=from-pstn") === false)) {
153           $istrunk=1;
154           continue;
155         }
156         if (!(strpos($line,"context=from-zaptel") === false)) {
157           $istrunk=1;
158           continue;
159         }
160         if (!(strpos($line,"context=from-internal") === false)) {
161           $istrunk=0;
162           continue;
163         }
164
165
166     $temparray = @explode("channel=",$line,2);
167     if ((count($temparray) == 2) and ($ampwildcard == 0))  {
168       $temparray = @explode(",",$temparray[1],2);
169       foreach ($temparray as $range) {
170         list($start,$end) = @explode("-",$range,2);
171         $start = intval($start); //crudely maps non-integers to zero
172         if (!isset($end)) {$end = $start;}
173         for ($i = $start; $i <= $end; $i++) {
174           $lastlabelnum++;
175           $newlabel=$zaplabel;
176           $newlabel=str_replace("%c",$i,$newlabel);
177           $newlabel=str_replace("%n",$lastlabelnum,$newlabel);
178           $newlabel=str_replace("%N",$lastlabelnum,$newlabel);
179
180           # only add if A) this is a trunk
181           # and B) we have not already defined any zaplines at the top of the file
182           # (I use this to customize it so instead of saying "Zap 1" it will
183           # say something more useful -- like the phone # of the line)
184
185           if($istrunk == 1) {
186             $inzaplines=false;
187             foreach ($zaplines as $tempvalue) {
188               if($tempvalue[0] == "Zap/$i") {
189                 $inzaplines=true;
190                 break;
191               }
192             }
193             if (!($inzaplines)) {
194               array_push($zaplines,array( "Zap/$i","$newlabel" ));
195             }
196           } //istrunk
197         } //for $i
198       } //foreach $range
199     } // if "channel="
200   } //foreach $line
201   return $zaplines;
202 }
203 #Finished parsing zapata.conf
204
205
206 # Conference Rooms not yet implemented in AMP config
207 $conferences=array();   #### ext#, description
208 #array_push($conferences,array( "810","Conf.10" ));
209 #array_push($conferences,array( "811","Conf.11" ));
210
211 # cool hack by Julien BLACHE <jblache@debian.org>
212 $ampconf = parse_amportal_conf( $amportalconf );
213 # WARNING: this file will be substituted by the output of this program
214 $op_conf = $ampconf["AMPWEBROOT"]."/panel/op_buttons_additional.cfg";
215 # username to connect to the database
216 $username = $ampconf["AMPDBUSER"];
217 # password to connect to the database
218 $password = $ampconf["AMPDBPASS"];
219 # the name of the box the MySQL database is running on
220 $hostname = $ampconf["AMPDBHOST"];
221 # the name of the database our tables are kept
222 $database = $ampconf["AMPDBNAME"];
223 #sort option: extension or lastname
224 $sortoption = $ampconf["FOPSORT"];
225
226 # the engine to be used for the SQL queries,
227 # if none supplied, backfall to mysql
228 $db_engine = "mysql";
229 if (isset($ampconf["AMPDBENGINE"])){
230   $db_engine = $ampconf["AMPDBENGINE"];
231 }
232 ################### END OF CONFIGURATION #######################
233
234 $warning_banner =
235 "; do not edit this file, this is an auto-generated file by freepbx
236 ; all modifications must be done from the web gui
237 ";
238
239 switch ($db_engine)
240 {
241         case "pgsql":
242         case "mysql":
243                 /* datasource in in this style:
244                 dbengine://username:password@host/database */
245
246                 $datasource = $db_engine.'://'.$username.':'.$password.'@'.$hostname.'/'.$database;
247                 $db = DB::connect($datasource); // attempt connection
248                 break;
249
250         case "sqlite":
251                 require_once('DB/sqlite.php');
252
253                 if (!isset($amp_conf["AMPDBFILE"]))
254                         die("You must setup properly AMPDBFILE in /etc/amportal.conf");
255
256                 if (isset($amp_conf["AMPDBFILE"]) == "")
257                         die("AMPDBFILE in /etc/amportal.conf cannot be blank");
258
259                 $DSN = array (
260                         "database" => $amp_conf["AMPDBFILE"],
261                         "mode" => 0666
262                 );
263
264                 $db = new DB_sqlite();
265                 $db->connect( $DSN );
266                 break;
267
268         default:
269                 die( "Unknown SQL engine: [$db_engine]");
270 }
271
272 if(DB::isError($db)) {
273        echo("FAILED\n");
274        echo("Cannot connect to database\n");
275      die($db->getMessage());
276
277 }
278
279 # Get layout-info from a "panel" table in the freepbx database
280 if (table_exists($db,"panel")) {
281
282   $statement = "SELECT id, legend, startpos, stoppos, color1, color2 from panel";
283   $results = $db->getAll($statement);
284   if(DB::IsError($results)) {
285      die($results->getMessage());
286   }
287   if (count($results) < 1) {
288     print "Notice: no panel defined\n";
289   }
290   $rectangles = $results;
291 }
292
293 // pass layout info to function explicitly rather than via globals
294 $layoutinfo = array('rectangles'=>$rectangles,'numbuttonsx'=>$numbuttonsx,'numbuttonsy'=>$numbuttonsy);
295
296 # Automated generation of style-info from layout-info
297 $autoextenpos=get_styleinfo("extension",$layoutinfo);
298 $autotrunkpos=get_styleinfo("trunk",$layoutinfo);
299 $autoparkingpos=get_styleinfo("parking",$layoutinfo);
300 $autoconfepos=get_styleinfo("conference",$layoutinfo);
301 $autoqueuepos=get_styleinfo("queue",$layoutinfo);
302
303 if ($layoutbuttonsonly == 1) {$extenpos = ""; $trunkpos = ""; $parkingpos = ""; $confepos = ""; $queuepos = "";}
304
305 if (isset($autoextenpos)) {$extenpos = $autoextenpos;}
306 if (isset($autotrunkpos)) {$trunkpos = $autotrunkpos;}
307 if (isset($autoparkingpos)) {$parkingpos = $autoparkingpos;}
308 if (isset($autoconfepos)) {$confepos = $autoconfepos;}
309 if (isset($autoqueuepos)) {$queuepos = $autoqueuepos;}
310
311
312 $fhandle = fopen($op_conf,"w" );
313 if ($fhandle === false) {die("Cannot create/overwrite config file: $op_conf \n");}
314 fwrite($fhandle, $warning_banner);
315
316 #First, populate extensions
317
318 $extensionlist=array();
319
320 if (table_exists($db,"devices")) {
321   $statement = "SELECT description,id,dial,tech from devices";
322   $results = $db->getAll($statement);
323   if(DB::IsError($results)) {
324      die($results->getMessage());
325   }
326   if (count($results) < 1) {
327     print "Notice: no Devices defined\n";
328   }
329   $extensionlist = $results;
330 }
331 else { print "Table does not exist: devices\n"; }
332
333 # sort the extensions
334 foreach ($extensionlist as $key=>$extension) {
335   $temparray = explode(" ",$extension[0]);
336   $lastname[$key] = end($temparray);
337   $extnum[$key] = $extension[1];
338 }
339
340 if  (isset($sortoption) && ($sortoption == "lastname")) {
341   array_multisort($lastname,$extensionlist);
342 } else {
343   array_multisort($extnum,SORT_STRING,$extensionlist);
344 }
345
346 #Next, populate queues
347 $queues=array();
348 if (table_exists($db,"queues_config")) {
349   $statement = "SELECT extension,descr from queues_config order by extension";
350   $results = $db->getAll($statement);
351   if(DB::IsError($results)) {
352      die($results->getMessage());
353   }
354   if (count($results) < 1) {
355     print "Notice: no Queues defined\n";
356   }
357   $queues = $results;
358 }
359 else { print "Table does not exist: queues_config\n"; }
360
361
362 ## SME server chnges
363
364 #Next, populate conferences
365 $conferences=array();
366 if(table_exists($db,"meetme")) {
367   $statement = "SELECT exten,description FROM meetme ORDER BY exten";
368   $results = $db->getAll($statement);
369   if(DB::IsError($results)) {
370      die($results->getMessage());
371   }
372   if (count($results) < 1) {
373     print "Notice: no Conferences defined\n";
374   }
375   $conferences = $results;
376 }
377 else { print "Table does not exist: meetme\n"; }
378
379
380 #Next, populate parkings
381 $parkings=array();
382 if(table_exists($db,"parkinglot")) {
383   $statement = "SELECT keyword,data FROM parkinglot";
384   $results = $db->getAll($statement);
385   if(DB::IsError($results)) {
386      die($results->getMessage());
387   }
388   if (count($results) < 1) {
389     print "Notice: no Parking Lots defined\n";
390   }
391   $parkings = $results;
392 }
393 else { print "Table does not exist: parkinglot\n"; }
394
395 ## End of changes
396 #Next, populate trunks (sip and iax)
397 $trunklist=array();
398 $tables = array("sip","iax");
399 foreach ($tables as $table) {
400   if (table_exists($db,$table)) {
401     $statement = "SELECT data,id,'$table' from $table where keyword='account' and flags <> 1 and id>99990 group by data order by id";
402     $results = $db->getAll($statement);
403     if(DB::IsError($results)) {
404        die($results->getMessage());
405     }
406     if (count($results) < 1) {
407       print "Notice: no $table trunks defined\n";
408     }
409     $trunklist = array_merge($trunklist,$results);
410 }
411 else { print "Table does not exist: $table \n"; }
412 }
413
414 #Determine AMP Users
415 $ampusers=array(array("default","0","0"));
416 if (table_exists($db,"ampusers")) {
417   $statement = 'SELECT deptname,extension_low,extension_high from ampusers WHERE NOT extension_low = "" AND NOT extension_high = ""';
418     $results = $db->getAll($statement);
419     if(DB::IsError($results)) {
420        die($results->getMessage());
421     }
422     if (count($results) < 1) {
423       print "Notice: no AMP Users defined\n";
424     }
425     else {
426       $ampusers = $results;
427     }
428 }
429 else { print "Table does not exist: ampusers\n"; }
430
431 #Write a separate panel context from each AMP User department
432 foreach ($ampusers as $pcontext) {
433   $exten_low = $pcontext[1];
434   $exten_high = $pcontext[2];
435   $panelcontext = $pcontext[0];
436   if ($panelcontext == "") { $panelcontext = $exten_low."to".$exten_high; }
437
438
439   # WRITE EXTENSIONS
440
441   $btn=0;
442   if ($exten_low != 0 && $exten_high != 0) {  #display only allowed range of extensions for panel_contexts
443     $extensionrange = array();
444     foreach ($extensionlist as $value) {
445       if (!is_numeric($value)) {array_push($extensionrange,$value);}
446       if (($value >= $exten_low) && ($value <= $exten_high))  {array_push($extensionrange,$value);}
447     }
448   } else {
449     $extensionrange = $extensionlist;
450   }
451   foreach ( $extensionrange as $row ) {
452     $description = $row[0];
453     $id = $row[1];
454     $dial = $row[2];
455
456
457     # Support for real mailbox settings -
458     $tech = $row[3];
459     # some sensible defaults for voicemail ext and context
460     $vmext = $row[1];
461     $vmcontext = "default";
462     # the device tech table should also have a dial context - if not assume from-internal
463     $context = "from-internal";
464     # database table name for iax2 is just iax but sip and zap are ok
465     if ($tech == "iax2") {$tech = "iax";}
466     # get mailbox setting from relevant tech table and split into ext and content
467     if (table_exists($db,$tech)) {
468       $statement = "SELECT data from $tech WHERE id = '$id' AND keyword = 'mailbox' ";
469       $results = $db->getAll($statement);
470       if(DB::IsError($results)) {
471          die($results->getMessage());
472       }
473       if (count($results) < 1) {
474         print "Notice: no Mailboxes defined\n";
475       }
476       else {
477       $mailbox = $results[0][0];
478       $values = @explode('@', $mailbox,2);
479       if (strlen($values[0]) > 0) {$vmext = $values[0];}
480       if (strlen($values[1]) > 0) {$vmcontext = $values[1];}
481       }
482       #while in this table lets get the dial context as well
483       $statement = "SELECT data from $tech WHERE id = '$id' AND keyword = 'context' ";
484       $results = $db->getAll($statement);
485       if(DB::IsError($results)) {
486          die($results->getMessage());
487       }
488       if (count($results) < 1) {
489         print "Notice: no Context defined\n";
490       }
491       else {
492       $context = $results[0][0];
493       }
494     } else { print "Table does not exist: $tech\n"; }
495     # - Support for real mailbox settings
496
497
498     # Support for real VM_PREFIX -
499     $vmprefix = "*";
500     if (table_exists($db,"globals")) {
501       $statement = "SELECT value from globals WHERE variable = 'VM_PREFIX' ";
502       $results = $db->getAll($statement);
503       if(DB::IsError($results)) {
504          die($results->getMessage());
505       }
506       if (count($results) < 1) {
507         print "Notice: no VM Prefix defined\n";
508       }
509       else {
510       $vmprefix = $results[0][0];
511       }
512     } else { print "Table does not exist: global\n"; }
513     # - Support for real VM_PREFIX
514
515     $btn=get_next_btn($extenpos,$btn);
516     $icon='4';
517     fwrite($fhandle, "[$dial]\nPosition=$btn\nLabel=\"$id : $description\"\nExtension=$id\nContext=$context\nIcon=$icon\nVoicemail_Context=$vmcontext\nVoiceMailExt=$vmprefix$vmext@$context\nPanel_Context=$panelcontext\n");
518   }
519
520
521   ### NOW WRITE TRUNKS.. WE START WITH ZAP TRUNKS DEFINED ABOVE
522
523
524
525
526   $btn=0;
527
528   foreach ($zaplines as $row) {
529     $zapdef=$row[0];
530     $zapdesc=$row[1];
531     $icon='3';
532     # zaplines and trunklist share the trunk positions so need to store previous btn on overflow from zaplines
533     $previousbtn = $btn;
534     $btn=get_next_btn($trunkpos,$btn);
535     if ($btn == 0) {$btn = $previousbtn; last;}
536     if ($zapdef == "Zap/*") {
537       $numbuttons=$row[2]-1;
538       fwrite($fhandle, "[$zapdef]\nLabel=\"$zapdesc\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\nPosition=".$btn);
539       while($numbuttons-->0) {
540         $btn=get_next_btn($trunkpos,$btn);
541         fwrite($fhandle, ",".$btn);
542       }
543
544       fwrite($fhandle, "\n");
545     } else {
546       fwrite($fhandle, "[$zapdef]\nPosition=$btn\nLabel=\"$zapdesc\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\n");
547     }
548   }
549
550
551   foreach ($trunklist as $row) {
552     $account = $row[0];
553     $id = $row[1];
554     $table = $row[2];
555     if ($account == "") {continue;};
556     $btn=get_next_btn($trunkpos,$btn);
557     if ($btn == 0) {last;}
558     if (table_exists($db,$table)) {
559     $statement = "SELECT keyword,data from $table where id=$id and keyword <> 'account' and flags <> 1 order by keyword";
560       $results = $db->getAll($statement);
561       if(DB::IsError($results)) {
562          die($results->getMessage());
563       }
564       if (count($results) < 1) {
565         print "Notice: no Trunks defined\n";
566       }
567     } else { print "Table does not exist: $table \n"; }
568
569     if ($table == "sip") {$tech="SIP";}
570     if ($table == "iax") {$tech="IAX2";}
571     #if ($table == "zap") {$tech="ZAP";} #no zap trunks in db
572
573     $callerid = $account;  #default callerid to account
574
575     foreach ($results as $drow) {
576       if ( $drow[0] == "callerid" ) {
577         $callerid = $drow[1];
578         $fields = explode("<",$callerid);
579         $callerid=$fields[1] ." ". $fields[0];
580         $callerid = str_replace("\t","",$callerid);
581         $callerid = str_replace("\"","",$callerid);
582         $callerid = str_replace("<","",$callerid);
583         $callerid = str_replace(">","",$callerid);
584       }
585     }
586     $icon='3';
587     fwrite($fhandle, "[$tech/$account]\nPosition=$btn\nLabel=\"$callerid\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\n");
588   }
589
590
591   ## SME server changes
592
593
594
595         ### Write Parkings lots
596   $btn=0;
597   $parken="" ;
598   $extpark ;
599   $parkcontext ;
600   $numberlots ;
601   $maxparkingslots ;
602
603   foreach ($parkings as $row) {
604     if ($row[0] == "parkingenabled") {
605       $parken = $row[1] ;
606     }
607     if ($row[0] == "parkext") {
608       $extpark = $row[1] ;
609     }
610     if ($row[0] == "parkingcontext") {
611       $parkcontext = $row[1] ;
612     }
613     if ($row[0] == "numslots") {
614       $numberlots = $row[1] ;
615     }
616   }
617   if ($parken == "s") {
618     for ($i = 1 ; $i <= $numberlots ; $i++ ) {
619       $btn=get_next_btn($parkingpos,$btn);
620       if ($btn == 0) {last;}
621       $parknum = $extpark + $i ;
622       $icon='1';
623       fwrite($fhandle, "[PARK$parknum]\nPosition=$btn\nLabel=\"Parked ($parknum)\"\nExtension=$parknum\nContext=$parkcontext\nIcon=$icon\nPanel_Context=$panelcontext\n");
624     }
625   }
626
627   ## End of chagnes
628   ### Write conferences (meetme)
629
630   $btn=0;
631   if ($exten_low != 0 && $exten_high != 0) {  #display only allowed range of extensions for panel_contexts
632     $confrange = array();
633     foreach ($conferences as $value) {
634       if (!is_numeric($value)) {array_push($confrange,$value);}
635       if (($value >= $exten_low) && ($value <= $exten_high))  {array_push($confrange,$value);}
636     }
637   } else {
638     $confrange = $conferences;
639   }
640   foreach ($confrange as $row) {
641     $btn=get_next_btn($confepos,$btn);
642     if ($btn == 0) {last;}
643     $confenum=$row[0];
644     $confedesc=$row[1];
645     $icon='6';
646     fwrite($fhandle, "[$confenum]\nPosition=$btn\nLabel=\"$confedesc\"\nExtension=$confenum\nContext=from-internal\nIcon=$icon\nPanel_Context=$panelcontext\n");
647   }
648
649   ### Write Queues
650
651   $btn=0;
652   if ($exten_low != 0 && $exten_high != 0) {  #display only allowed range of extensions for panel_contexts
653     $queuerange = array();
654     foreach ($queues as $value) {
655       if (!is_numeric($value)) {array_push($queuerange,$value);}
656       if (($value >= $exten_low) && ($value <= $exten_high))  {array_push($queuerange,$value);}
657     }
658   } else {
659     $queuerange = $queues;
660   }
661   foreach ($queuerange as $row) {
662     $btn=get_next_btn($queuepos,$btn);
663     if ($btn == 0) {last;}
664     $queuename=$row[0];
665     $queuedesc=$row[1];
666     $icon='5';
667     fwrite($fhandle, "[QUEUE/$queuename]\nPosition=$btn\nLabel=\"$queuedesc\"\nExtension=-1\nContext=from-internal\nIcon=$icon\nPanel_Context=$panelcontext\n");
668   }
669
670   ### Write rectangles
671
672   foreach ($rectangles as $rect) {
673     $comment = $rect[0];
674     $color1 = $rect[4];
675     $color2 = $rect[5];
676     $start = $rect[2];
677     $stop = $rect[3];
678
679     $xposition = $buttonsoriginx + $buttonsizex * floor(($start-1)/$numbuttonsy);
680     $yposition = $buttonsoriginy + $buttonsizey * (($start-1)%$numbuttonsy);
681     $xsize = $buttonsizex * (1 + floor(($stop-1)/$numbuttonsy) - floor(($start-1)/$numbuttonsy));
682     $ysize = $buttonsizey * (1 + (($stop-1)%$numbuttonsy) - (($start-1)%$numbuttonsy));
683
684     if (($xsize <= 0) || ($ysize <= 0)) {continue;}
685
686     $xposition += $rectmarginx;
687     $yposition += $rectmarginy;
688     $xsize -= 2 * $rectmarginx;
689     $ysize -= 2 * $rectmarginy;
690
691     fwrite($fhandle, "\n; $comment\n[rectangle]\nx=$xposition\ny=$yposition\nwidth=$xsize\nheight=$ysize\nline_width=0\nline_color=$color1\nfade_color1=$color1\nfade_color2=$color2\nrnd_border=2\nalpha=20\nlayer=bottom\n");
692   }
693
694   ### Write legends
695
696   foreach ($rectangles as $legend) {
697     $text = $legend[1];
698     $start = $legend[2];
699     $stop = $legend[3];
700
701     $xposition = $buttonsoriginx + $buttonsizex * floor(($start-1)/$numbuttonsy);
702     $yposition = $buttonsoriginy + $buttonsizey * (($start-1)%$numbuttonsy);
703     $xsize = $buttonsizex * (1 + floor(($stop-1)/$numbuttonsy) - floor(($start-1)/$numbuttonsy));
704     $ysize = $buttonsizey * (1 + (($stop-1)%$numbuttonsy) - (($start-1)%$numbuttonsy));
705
706     if (($xsize <= 0) || ($ysize <= 0)) {continue;}
707
708     $xposition += $legendoffsetx;
709     $yposition += $legendoffsety;
710
711     fwrite($fhandle, "\n[LEGEND]\nx=$xposition\ny=$yposition\ntext=$text\nfont_size=18\nfont_family=Arial\nuse_embed_fonts=1\n");
712   }
713
714 }
715
716 function get_next_btn($data,$last) {
717   $rangelist=explode(",",$data);
718   foreach ($rangelist as $range) {
719     $rangeval=explode("-",$range,2);
720     if ($last < $rangeval[0]) {return $rangeval[0] ;}
721     if (isset($rangeval[1]) && ($last < $rangeval[1])) {return $last+1;}
722     #Need to try another range def...
723   }
724   #If we get here, we ran out of positions :(
725   return 0; #?????
726 }
727 #this sub checks for the existance of a table
728 function table_exists($db,$table) {
729          $result = mysql_query("SHOW TABLES LIKE '" . $table . "'");
730          if(mysql_fetch_row($result) === false) {return(false);}
731          return(true);
732          }
733
734 function get_styleinfo($id,$layoutinfo) {
735 // do not use globals - instead pass layout info into function explicitly
736 //  global $rectangles;
737 //  global $numbuttonsx;
738 //  global $numbuttonsy;
739 $rectangles = $layoutinfo['rectangles'];
740 $numbuttonsx = $layoutinfo['numbuttonsx'];
741 $numbuttonsy = $layoutinfo['numbuttonsy'];
742
743
744   foreach ($rectangles as $rect) {
745     if ($id == $rect[0]) {
746
747       $start = $rect[2];
748       $stop = $rect[3];
749
750       $xposition = floor(($start-1)/$numbuttonsy);
751       $yposition = (($start-1)%$numbuttonsy);
752       $xsize = 1 + floor(($stop-1)/$numbuttonsy) - floor(($start-1)/$numbuttonsy);
753       $ysize = 1 + (($stop-1)%$numbuttonsy) - (($start-1)%$numbuttonsy);
754
755       if (($xsize <= 0) || ($ysize <= 0)) {print "Warning: rectange '$id' has negative area\n"; last;}
756       $styleinfo = "";
757       if ($ysize > 2) {
758         $styleinfo .= ($start + 1) . "-" . ($start + $ysize - 1) . ",";
759       }
760       elseif ($ysize == 2) {
761         $styleinfo .= ($start + 1) . ",";
762       }
763
764       for ($i = 1 ; $i < $xsize ; $i++ ) {
765         if ($ysize > 1) {
766           $styleinfo .= (($i + $xposition) * $numbuttonsy + $yposition + 1) . "-" . (($i + $xposition) * $numbuttonsy + $yposition + $ysize) . ",";
767         }
768         else {
769           $styleinfo .= (($i + $xposition) * $numbuttonsy + $yposition + 1) . ",";
770         }
771       }
772       $retval = $styleinfo;
773       last;
774     }
775   }
776   return $retval;
777 }
778 ?>