Ticket #4185: retrieve_op_conf_from_mysql.php.ek

File retrieve_op_conf_from_mysql.php.ek, 22.7 kB (added by ekennedy, 2 years ago)

Updated File

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