Ticket #4185: retrieve_op_conf_from_mysql.php.original

File retrieve_op_conf_from_mysql.php.original, 22.5 kB (added by ekennedy, 2 years ago)

Original 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               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 # WARNING: this file will be substituted by the output of this program
213 $op_conf = $amp_conf["AMPWEBROOT"]."/panel/op_buttons_additional.cfg";
214 # username to connect to the database
215 $username = $amp_conf["AMPDBUSER"];
216 # password to connect to the database
217 $password = $amp_conf["AMPDBPASS"];
218 # the name of the box the MySQL database is running on
219 $hostname = $amp_conf["AMPDBHOST"];
220 # the name of the database our tables are kept
221 $database = $amp_conf["AMPDBNAME"];
222 #sort option: extension or lastname
223 $sortoption = $amp_conf["FOPSORT"];
224
225 ################### END OF CONFIGURATION #######################
226
227 $warning_banner =
228 "; do not edit this file, this is an auto-generated file by freepbx
229 ; all modifications must be done from the web gui
230 ";
231
232 # Get layout-info from a "panel" table in the freepbx database
233 if (table_exists($db,"panel")) {
234
235   $statement = "SELECT id, legend, startpos, stoppos, color1, color2 from panel";
236   $results = $db->getAll($statement);
237   if(DB::IsError($results)) {
238      die($results->getMessage());
239   }
240   if (count($results) < 1) {
241     print "Notice: no panel defined\n";
242   }
243   $rectangles = $results;
244 }
245
246 // pass layout info to function explicitly rather than via globals
247 $layoutinfo = array('rectangles'=>$rectangles,'numbuttonsx'=>$numbuttonsx,'numbuttonsy'=>$numbuttonsy);
248
249 # Automated generation of style-info from layout-info
250 $autoextenpos=get_styleinfo("extension",$layoutinfo);
251 $autotrunkpos=get_styleinfo("trunk",$layoutinfo);
252 $autoparkingpos=get_styleinfo("parking",$layoutinfo);
253 $autoconfepos=get_styleinfo("conference",$layoutinfo);
254 $autoqueuepos=get_styleinfo("queue",$layoutinfo);
255
256 if ($layoutbuttonsonly == 1) {$extenpos = ""; $trunkpos = ""; $parkingpos = ""; $confepos = ""; $queuepos = "";}
257
258 if (isset($autoextenpos)) {$extenpos = $autoextenpos;}
259 if (isset($autotrunkpos)) {$trunkpos = $autotrunkpos;}
260 if (isset($autoparkingpos)) {$parkingpos = $autoparkingpos;}
261 if (isset($autoconfepos)) {$confepos = $autoconfepos;}
262 if (isset($autoqueuepos)) {$queuepos = $autoqueuepos;}
263
264
265 $fhandle = fopen($op_conf,"w" );
266 if ($fhandle === false) {die("Cannot create/overwrite config file: $op_conf \n");}
267 fwrite($fhandle, $warning_banner);
268
269 #First, populate extensions
270
271 $extensionlist=array();
272
273 if (table_exists($db,"devices")) {
274   $statement = "SELECT description,id,dial,tech from devices";
275   $results = $db->getAll($statement);
276   if(DB::IsError($results)) {
277      die($results->getMessage());
278   }
279   if (count($results) < 1) {
280     print "Notice: no Devices defined\n";
281   }
282   $extensionlist = $results;
283 }
284 else { print "Table does not exist: devices\n"; }
285
286 # sort the extensions
287 foreach ($extensionlist as $key=>$extension) {
288   $temparray = explode(" ",$extension[0]);
289   $lastname[$key] = end($temparray);
290   $extnum[$key] = $extension[1];
291 }
292
293 if  (isset($sortoption) && ($sortoption == "lastname")) {
294   array_multisort($lastname,$extensionlist);
295 } else {
296   array_multisort($extnum,SORT_STRING,$extensionlist);
297 }
298
299 #Next, populate queues
300 $queues=array();
301 if (table_exists($db,"queues_config")) {
302   $statement = "SELECT extension,descr from queues_config order by extension";
303   $results = $db->getAll($statement);
304   if(DB::IsError($results)) {
305      die($results->getMessage());
306   }
307   if (count($results) < 1) {
308     print "Notice: no Queues defined\n";
309   }
310   $queues = $results;
311 }
312 else { print "Table does not exist: queues_config\n"; }
313
314
315 ## SME server chnges
316
317 #Next, populate conferences
318 $conferences=array();
319 if(table_exists($db,"meetme")) {
320   $statement = "SELECT exten,description FROM meetme ORDER BY exten";
321   $results = $db->getAll($statement);
322   if(DB::IsError($results)) {
323      die($results->getMessage());
324   }
325   if (count($results) < 1) {
326     print "Notice: no Conferences defined\n";
327   }
328   $conferences = $results;
329 }
330 else { print "Table does not exist: meetme\n"; }
331
332
333 #Next, populate parkings
334 $parkings=array();
335 if(table_exists($db,"parkinglot")) {
336   $statement = "SELECT keyword,data FROM parkinglot";
337   $results = $db->getAll($statement);
338   if(DB::IsError($results)) {
339      die($results->getMessage());
340   }
341   if (count($results) < 1) {
342     print "Notice: no Parking Lots defined\n";
343   }
344   $parkings = $results;
345 }
346 else { print "Table does not exist: parkinglot\n"; }
347
348 ## End of changes
349 #Next, populate trunks (sip and iax)
350 $trunklist=array();
351 $tables = array("sip","iax");
352 foreach ($tables as $table) {
353   if (table_exists($db,$table)) {
354     $statement = "SELECT data,id,'$table' from $table where keyword='account' and flags <> 1 and id LIKE 'tr-%' group by data order by id";
355     $results = $db->getAll($statement);
356     if(DB::IsError($results)) {
357        die($results->getMessage());
358     }
359     if (count($results) < 1) {
360       print "Notice: no $table trunks defined\n";
361     }
362     $trunklist = array_merge($trunklist,$results);
363 }
364 else { print "Table does not exist: $table \n"; }
365 }
366
367 #Determine AMP Users
368 $ampusers=array();
369 if (table_exists($db,"ampusers")) {
370   $statement = 'SELECT deptname,extension_low,extension_high from ampusers WHERE NOT extension_low = "" AND NOT extension_high = ""';
371     $results = $db->getAll($statement);
372     if(DB::IsError($results)) {
373        die($results->getMessage());
374     }
375     if (count($results) < 1) {
376       print "Notice: no AMP Users defined\n";
377     }
378     else {
379       $ampusers = $results;
380     }
381 }
382 else { print "Table does not exist: ampusers\n"; }
383 array_push($ampusers,array("default","0","0")); //add a default panelcontext that can see all extensions
384
385 #Write a separate panel context from each AMP User department
386 foreach ($ampusers as $pcontext) {
387   $exten_low = $pcontext[1];
388   $exten_high = $pcontext[2];
389   $panelcontext = $pcontext[0];
390   if ($panelcontext == "") { $panelcontext = $exten_low."to".$exten_high; }
391
392 fwrite($fhandle, "\n\n; Panel Context: " . $panelcontext . "\n");
393
394   # WRITE EXTENSIONS
395
396   $btn=0;
397   if ($exten_low != 0 && $exten_high != 0) {  #display only allowed range of extensions for panel_contexts
398     $extensionrange = array();
399     foreach ($extensionlist as $value) {
400       if (!is_numeric($value[1])) {array_push($extensionrange,$value);}
401       if (($value[1] >= $exten_low) && ($value[1] <= $exten_high))  {array_push($extensionrange,$value);}
402     }
403   } else {
404     $extensionrange = $extensionlist;
405   }
406
407   foreach ( $extensionrange as $row ) {
408     $description = $row[0];
409     $id = $row[1];
410     $dial = $row[2];
411
412
413     # Support for real mailbox settings -
414     $tech = $row[3];
415     # some sensible defaults for voicemail ext and context
416     $vmext = $row[1];
417     $vmcontext = "default";
418     # the device tech table should also have a dial context - if not assume from-internal
419     $context = "from-internal";
420     # database table name for iax2 is just iax but sip and zap are ok
421     if ($tech == "iax2") {$tech = "iax";}
422     # get mailbox setting from relevant tech table and split into ext and content
423     if (table_exists($db,$tech)) {
424       $statement = "SELECT data from $tech WHERE id = '$id' AND keyword = 'mailbox' ";
425       $results = $db->getAll($statement);
426       if(DB::IsError($results)) {
427          die($results->getMessage());
428       }
429       if (count($results) < 1) {
430         print "Notice: no Mailboxes defined\n";
431       }
432       else {
433       $mailbox = $results[0][0];
434       $values = @explode('@', $mailbox,2);
435       if (strlen($values[0]) > 0) {$vmext = $values[0];}
436       if (strlen($values[1]) > 0) {$vmcontext = $values[1];}
437       }
438       #while in this table lets get the dial context as well
439       $statement = "SELECT data from $tech WHERE id = '$id' AND keyword = 'context' ";
440       $results = $db->getAll($statement);
441       if(DB::IsError($results)) {
442          die($results->getMessage());
443       }
444       if (count($results) < 1) {
445         print "Notice: no Context defined\n";
446       }
447       else {
448       $context = $results[0][0];
449       }
450     } else { print "Table does not exist: $tech\n"; }
451     # - Support for real mailbox settings
452
453
454     # Support for real VM_PREFIX -
455     $vmprefix = "*";
456     if (table_exists($db,"globals")) {
457       $statement = "SELECT value from globals WHERE variable = 'VM_PREFIX' ";
458       $results = $db->getAll($statement);
459       if(DB::IsError($results)) {
460          die($results->getMessage());
461       }
462       if (count($results) < 1) {
463         print "Notice: no VM Prefix defined\n";
464       }
465       else {
466       $vmprefix = $results[0][0];
467       }
468     } else { print "Table does not exist: global\n"; }
469     # - Support for real VM_PREFIX
470
471     $btn=get_next_btn($extenpos,$btn);
472     $icon='4';
473     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");
474   }
475
476
477   ### NOW WRITE TRUNKS.. WE START WITH ZAP TRUNKS DEFINED ABOVE
478
479
480
481
482   $btn=0;
483
484   foreach ($zaplines as $row) {
485     $zapdef=$row[0];
486     $zapdesc=$row[1];
487     $icon='3';
488     # zaplines and trunklist share the trunk positions so need to store previous btn on overflow from zaplines
489     $previousbtn = $btn;
490     $btn=get_next_btn($trunkpos,$btn);
491     if ($btn == 0) {$btn = $previousbtn; break;}
492     if ($zapdef == "Zap/*") {
493       $numbuttons=$row[2]-1;
494       fwrite($fhandle, "\n[$zapdef]\nLabel=\"$zapdesc\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\nPosition=".$btn);
495       while($numbuttons-->0) {
496         $btn=get_next_btn($trunkpos,$btn);
497         fwrite($fhandle, ",".$btn);
498       }
499
500       fwrite($fhandle, "\n");
501     } else {
502       fwrite($fhandle, "\n[$zapdef]\nPosition=$btn\nLabel=\"$zapdesc\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\n");
503     }
504   }
505
506
507   foreach ($trunklist as $row) {
508     $account = $row[0];
509     $id = $row[1];
510     $table = $row[2];
511     if ($account == "") {continue;};
512     $btn=get_next_btn($trunkpos,$btn);
513     if ($btn == 0) {break;}
514     if (table_exists($db,$table)) {
515     $statement = "SELECT keyword,data from $table where id='$id' and keyword <> 'account' and flags <> 1 order by keyword";
516       $results = $db->getAll($statement);
517       if(DB::IsError($results)) {
518          die($results->getMessage());
519       }
520       if (count($results) < 1) {
521         print "Notice: no Trunks defined\n";
522       }
523     } else { print "Table does not exist: $table \n"; }
524
525     if ($table == "sip") {$tech="SIP";}
526     if ($table == "iax") {$tech="IAX2";}
527     #if ($table == "zap") {$tech="ZAP";} #no zap trunks in db
528
529     $callerid = $account;  #default callerid to account
530
531     foreach ($results as $drow) {
532       if ( $drow[0] == "callerid" ) {
533         $callerid = $drow[1];
534         $fields = explode("<",$callerid);
535         $callerid=$fields[1] ." ". $fields[0];
536         $callerid = str_replace("\t","",$callerid);
537         $callerid = str_replace("\"","",$callerid);
538         $callerid = str_replace("<","",$callerid);
539         $callerid = str_replace(">","",$callerid);
540       }
541     }
542     $icon='3';
543     fwrite($fhandle, "\n[$tech/$account]\nPosition=$btn\nLabel=\"$callerid\"\nExtension=-1\nIcon=$icon\nPanel_Context=$panelcontext\n");
544   }
545
546
547   ## SME server changes
548
549
550
551         ### Write Parkings lots
552   $btn=0;
553   $parken="" ;
554   $extpark ;
555   $parkcontext ;
556   $numberlots ;
557   $maxparkingslots ;
558
559   foreach ($parkings as $row) {
560     if ($row[0] == "parkingenabled") {
561       $parken = $row[1] ;
562     }
563     if ($row[0] == "parkext") {
564       $extpark = $row[1] ;
565     }
566     if ($row[0] == "parkingcontext") {
567       $parkcontext = $row[1] ;
568     }
569     if ($row[0] == "numslots") {
570       $numberlots = $row[1] ;
571     }
572   }
573   if ($parken == "s") {
574     for ($i = 1 ; $i <= $numberlots ; $i++ ) {
575       $btn=get_next_btn($parkingpos,$btn);
576       if ($btn == 0) {break;}
577       $parknum = $extpark + $i ;
578       $icon='1';
579       fwrite($fhandle, "\n[PARK$parknum]\nPosition=$btn\nLabel=\"Parked ($parknum)\"\nExtension=$parknum\nContext=$parkcontext\nIcon=$icon\nPanel_Context=$panelcontext\n");
580     }
581   }
582
583   ## End of chagnes
584   ### Write conferences (meetme)
585
586   $btn=0;
587   if ($exten_low != 0 && $exten_high != 0) {  #display only allowed range of extensions for panel_contexts
588     $confrange = array();
589     foreach ($conferences as $value) {
590       if (!is_numeric($value)) {array_push($confrange,$value);}
591       if (($value >= $exten_low) && ($value <= $exten_high))  {array_push($confrange,$value);}
592     }
593   } else {
594     $confrange = $conferences;
595   }
596   foreach ($confrange as $row) {
597     $btn=get_next_btn($confepos,$btn);
598     if ($btn == 0) {break;}
599     $confenum=$row[0];
600     $confedesc=$row[1];
601     $icon='6';
602     fwrite($fhandle, "\n[$confenum]\nPosition=$btn\nLabel=\"$confedesc\"\nExtension=$confenum\nContext=from-internal\nIcon=$icon\nPanel_Context=$panelcontext\n");
603   }
604
605   ### Write Queues
606
607   $btn=0;
608   if ($exten_low != 0 && $exten_high != 0) {  #display only allowed range of extensions for panel_contexts
609     $queuerange = array();
610     foreach ($queues as $value) {
611       if (!is_numeric($value)) {array_push($queuerange,$value);}
612       if (($value >= $exten_low) && ($value <= $exten_high))  {array_push($queuerange,$value);}
613     }
614   } else {
615     $queuerange = $queues;
616   }
617   foreach ($queuerange as $row) {
618     $btn=get_next_btn($queuepos,$btn);
619     if ($btn == 0) {break;}
620     $queuename=$row[0];
621     $queuedesc=$row[1];
622     $icon='5';
623     fwrite($fhandle, "\n[QUEUE/$queuename]\nPosition=$btn\nLabel=\"$queuedesc\"\nExtension=-1\nContext=from-internal\nIcon=$icon\nPanel_Context=$panelcontext\n");
624   }
625
626   ### Write rectangles
627
628   foreach ($rectangles as $rect) {
629     $comment = $rect[0];
630     $color1 = $rect[4];
631     $color2 = $rect[5];
632     $start = $rect[2];
633     $stop = $rect[3];
634
635     $xposition = $buttonsoriginx + $buttonsizex * floor(($start-1)/$numbuttonsy);
636     $yposition = $buttonsoriginy + $buttonsizey * (($start-1)%$numbuttonsy);
637     $xsize = $buttonsizex * (1 + floor(($stop-1)/$numbuttonsy) - floor(($start-1)/$numbuttonsy));
638     $ysize = $buttonsizey * (1 + (($stop-1)%$numbuttonsy) - (($start-1)%$numbuttonsy));
639
640     if (($xsize <= 0) || ($ysize <= 0)) {continue;}
641
642     $xposition += $rectmarginx;
643     $yposition += $rectmarginy;
644     $xsize -= 2 * $rectmarginx;
645     $ysize -= 2 * $rectmarginy;
646
647     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");
648   }
649
650   ### Write legends
651
652   foreach ($rectangles as $legend) {
653     $text = $legend[1];
654     $start = $legend[2];
655     $stop = $legend[3];
656
657     $xposition = $buttonsoriginx + $buttonsizex * floor(($start-1)/$numbuttonsy);
658     $yposition = $buttonsoriginy + $buttonsizey * (($start-1)%$numbuttonsy);
659     $xsize = $buttonsizex * (1 + floor(($stop-1)/$numbuttonsy) - floor(($start-1)/$numbuttonsy));
660     $ysize = $buttonsizey * (1 + (($stop-1)%$numbuttonsy) - (($start-1)%$numbuttonsy));
661
662     if (($xsize <= 0) || ($ysize <= 0)) {continue;}
663
664     $xposition += $legendoffsetx;
665     $yposition += $legendoffsety;
666
667     fwrite($fhandle, "\n[LEGEND]\nx=$xposition\ny=$yposition\ntext=$text\nfont_size=18\nfont_family=Arial\nuse_embed_fonts=1\nPanel_Context=$panelcontext\n");
668   }
669
670 }
671
672 function get_next_btn($data,$last) {
673   $rangelist=explode(",",$data);
674   foreach ($rangelist as $range) {
675     $rangeval=explode("-",$range,2);
676     if ($last < $rangeval[0]) {return $rangeval[0] ;}
677     if (isset($rangeval[1]) && ($last < $rangeval[1])) {return $last+1;}
678     #Need to try another range def...
679   }
680   #If we get here, we ran out of positions :(
681   return 0; #?????
682 }
683 #this sub checks for the existance of a table
684 function table_exists($db,$table) {
685          $result = mysql_query("SHOW TABLES LIKE '" . $table . "'");
686          if(mysql_fetch_row($result) === false) {return(false);}
687          return(true);
688          }
689
690 function get_styleinfo($id,$layoutinfo) {
691 // do not use globals - instead pass layout info into function explicitly
692 //  global $rectangles;
693 //  global $numbuttonsx;
694 //  global $numbuttonsy;
695 $rectangles = $layoutinfo['rectangles'];
696 $numbuttonsx = $layoutinfo['numbuttonsx'];
697 $numbuttonsy = $layoutinfo['numbuttonsy'];
698
699
700   foreach ($rectangles as $rect) {
701     if ($id == $rect[0]) {
702
703       $start = $rect[2];
704       $stop = $rect[3];
705
706       $xposition = floor(($start-1)/$numbuttonsy);
707       $yposition = (($start-1)%$numbuttonsy);
708       $xsize = 1 + floor(($stop-1)/$numbuttonsy) - floor(($start-1)/$numbuttonsy);
709       $ysize = 1 + (($stop-1)%$numbuttonsy) - (($start-1)%$numbuttonsy);
710
711       if (($xsize <= 0) || ($ysize <= 0)) {print "Warning: rectange '$id' has negative area\n"; break;}
712       $styleinfo = "";
713       if ($ysize > 2) {
714         $styleinfo .= ($start + 1) . "-" . ($start + $ysize - 1) . ",";
715       }
716       elseif ($ysize == 2) {
717         $styleinfo .= ($start + 1) . ",";
718       }
719
720       for ($i = 1 ; $i < $xsize ; $i++ ) {
721         if ($ysize > 1) {
722           $styleinfo .= (($i + $xposition) * $numbuttonsy + $yposition + 1) . "-" . (($i + $xposition) * $numbuttonsy + $yposition + $ysize) . ",";
723         }
724         else {
725           $styleinfo .= (($i + $xposition) * $numbuttonsy + $yposition + 1) . ",";
726         }
727       }
728       $retval = $styleinfo;
729       break;
730     }
731   }
732   return $retval;
733 }
734 ?>