root/modules/branches/2.9/fax/install.php

Revision 10002, 9.9 kB (checked in by p_lindheimer, 3 years ago)

Merged revisions 10001 via svnmerge from
http://svn.freepbx.org/modules/branches/2.7

........

r10001 | p_lindheimer | 2010-06-29 21:26:24 -0700 (Tue, 29 Jun 2010) | 1 line


don't try to execute sql statements if the insert_array is empty, pear throws a distracting warning

........

Line 
1 <?php
2
3 //for translation only
4 if (false) {
5 _("Dial System FAX");
6 }
7
8 if (! function_exists("out")) {
9     function out($text) {
10         echo $text."<br />";
11     }
12 }
13
14 if (! function_exists("outn")) {
15     function outn($text) {
16         echo $text;
17     }
18 }
19
20 global $db;
21
22 $sql[]='CREATE TABLE IF NOT EXISTS `fax_details` (
23   `key` varchar(50) default NULL,
24   `value` varchar(510) default NULL,
25   UNIQUE KEY `key` (`key`)
26 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;';
27
28
29 $sql[]='CREATE TABLE IF NOT EXISTS `fax_incoming` (
30   `cidnum` varchar(20) default NULL,
31   `extension` varchar(50) default NULL,
32   `detection` varchar(20) default NULL,
33   `detectionwait` varchar(5) default NULL,
34   `destination` varchar(50) default NULL,
35   `legacy_email` varchar(50) default NULL
36 )';
37
38 $sql[]='CREATE TABLE IF NOT EXISTS `fax_users` (
39   `user` varchar(15) default NULL,
40   `faxenabled` varchar(10) default NULL,
41   `faxemail` varchar(50) default NULL,
42   UNIQUE KEY `user` (`user`)
43 )';
44
45
46 foreach ($sql as $statement){
47     $check = $db->query($statement);
48     if (DB::IsError($check)){
49         die_freepbx( "Can not execute $statement : " . $check->getMessage() .  "\n");
50     }
51 }
52
53 //check for 2.6-style tables
54 $sql='describe fax_incoming';
55 $fields=$db->getAssoc($sql);
56 if(array_key_exists('faxdestination',$fields)){
57     out(_('Migrating fax_incoming table...'));
58     $sql='alter table fax_incoming
59                 change faxdetection detection varchar(20) default NULL,
60                 change faxdetectionwait detectionwait varchar(5) default NULL,
61                 change faxdestination destination varchar(50) default NULL,
62                 add legacy_email varchar(50) default NULL,
63                 drop faxenabled,
64                 modify extension varchar(50)';
65     $q=$db->query($sql);
66     if(DB::IsError($q)){
67     out(_('WARINING: fax_incoming table may still be using the 2.6 schema!'));
68   } else {
69     out(_('Sucsessfuly migraded fax_incoming table!'));
70   }
71 }
72 unset($sql);
73
74 /* migrate simu_fax from core to fax module, including in miscdests module in case it is being used as a destination.
75    this migration is a bit "messy" but assures that any simu_fax settings or destinations being used in the dialplan
76    will migrate silently and continue to work.
77  */
78 outn(_("Moving simu_fax feature code from core.."));
79 $check = $db->query("UPDATE featurecodes set modulename = 'fax' WHERE modulename = 'core' AND featurename = 'simu_fax'");
80 if (DB::IsError($check)){
81   if ($check->getCode() == DB_ERROR_ALREADY_EXISTS) {
82     outn(_("duplicate, removing old from core.."));
83     $check = $db->query("DELETE FROM featurecodes WHERE modulename = 'core' AND featurename = 'simu_fax'");
84     if (DB::IsError($check)){
85       out(_("unknown error"));
86     } else {
87       out(_("removed"));
88     }
89   } else {
90     out(_("unknown error"));
91   }
92 } else {
93   out(_("done"));
94 }
95 outn(_("Updating simu_fax in miscdest table.."));
96 $check = $db->query("UPDATE miscdests set destdial = '{fax:simu_fax}' WHERE destdial = '{core:simu_fax}'");
97 if (DB::IsError($check)){
98   out(_("not needed"));
99 } else {
100   out(_("done"));
101 }
102 $fcc = new featurecode('fax', 'simu_fax');
103 $fcc->setDescription('Dial System FAX');
104 $fcc->setDefault('666');
105 $fcc->update();
106 unset($fcc);
107
108 //check to make sure that min/maxrate and ecm are set; if not set them to defaults
109 $settings=sql('SELECT * FROM fax_details', 'getAssoc', 'DB_FETCHMODE_ASSOC');
110 foreach($settings as $setting => $value){$set[$setting]=$value['0'];}
111 if(!is_array($set)){$set=array();}//never return a null value
112 if(!$set['minrate']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("minrate","14400")';}
113 if(!$set['maxrate']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("maxrate","14400")';}
114 if(!$set['ecm']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("ecm","yes")';}
115 if(!$set['legacy_mode']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("legacy_mode","no")';}
116 if(!$set['force_detection']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("force_detection","no")';}
117  
118 if(isset($sql)){
119     foreach ($sql as $statement){
120         $check = $db->query($statement);
121         if (DB::IsError($check)){
122             die_freepbx( "Can not execute $statement : " . $check->getMessage() .  "\n");
123         }
124     }
125 }
126 /*
127 incoming columns:
128
129 faxexten: disabled
130           default (check what global is)
131           device_num
132
133 determine what default is, if a device then treat as that default device, if system
134 then treat as it was system here, and if disabled then treat as that.
135
136 legacy_email:
137   null -> not in legacy mode
138   blank or value -> in legacy mode
139
140 */
141 outn(_("Checking if legacy fax needs migrating.."));
142 $sql = "SELECT `extension`, `cidnum`, `faxexten`, `faxemail`, `wait`, `answer` FROM `incoming`";
143 $legacy_settings = $db->getAll($sql, DB_FETCHMODE_ASSOC);
144 if(!DB::IsError($legacy_settings)) {
145     out(_("starting migration"));
146
147   // First step, need to get global settings and if not present use defaults
148   //
149   $sql = "SELECT variable, value FROM globals WHERE variable IN ('FAX_RX', 'FAX_RX_EMAIL', 'FAX_RX_FROM')";
150   $globalvars = $db->getAll($sql, DB_FETCHMODE_ASSOC);
151
152   foreach ($globalvars as $globalvar) {
153       $global[trim($globalvar['variable'])] = $globalvar['value'];   
154   }
155   $fax_rx =          isset($global['FAX_RX'])       ? $global['FAX_RX'] : 'disabled';
156   $fax_rx_email =    isset($global['FAX_RX_EMAIL']) ? $global['FAX_RX_EMAIL'] : '';
157   $sender_address  = isset($global['FAX_RX_FROM'])  ? $global['FAX_RX_FROM'] : '';
158
159   // Now some sanity settings, can't email the fax if no email present
160   if ($fax_rx_email == '') {
161     $fax_rx = 'disabled';
162   }
163
164   // TODO Update Module Defaults Here
165   // insert_general_values()
166   //
167   $global_migrate = array();
168   $global_migrate[] = array('sender_address',$sender_address);
169   $global_migrate[] = array('fax_rx_email',$fax_rx_email);
170
171     outn(_("migrating defaults.."));
172     $compiled = $db->prepare("REPLACE INTO `fax_details` (`key`, `value`) VALUES (?,?)");
173     $result = $db->executeMultiple($compiled,$global_migrate);
174     if(DB::IsError($result)) {
175     out(_("failed"));
176         die_freepbx( "Fatal error during migration: " . $result->getMessage() .  "\n");
177     } else {
178     out(_("migrated"));
179   }
180
181     $detection_type = array(0 => 'dahdi', 1 => 'dahdi', 2 => 'nvfax');
182     $non_converts = array();
183
184   if (count($legacy_settings)) {
185     foreach($legacy_settings as $row) {
186       $legacy_email = null;
187       if ($row['faxexten'] == 'default') {
188         $row['faxexten'] = $fax_rx;
189       } else if ($row['faxexten'] == '') {
190         $row['faxexten'] = 'disabled';
191             }
192             if ($row['wait'] < 2) {
193         $detectionwait = '2';
194       } elseif ($row['wait'] > 10) {
195         $detectionwait = '10';
196       } else {
197         $detectionwait = $row['wait'];
198       }
199       $detection = $detection_type[$row['answer']];
200       switch ($row['faxexten']) {
201         case 'disabled':
202           continue; // go back to foreach for now
203         break;
204
205         case 'system':
206           $legacy_email = $row['faxemail'] ? $row['faxemail'] : $fax_rx_email;
207
208           // Now some sanity, if faxemail is blank then it won't work and we treat as disabled
209           //
210           if (!$legacy_email) {
211             continue;
212           }
213           $destination = '';
214                 $insert_array[] = array($row['extension'], $row['cidnum'], $detection, $detectionwait, $destination, $legacy_email);
215         break;
216
217         default:
218           if (ctype_digit($row['faxexten'])) {
219             $sql = "SELECT `user` FROM `devices` WHERE `id` = '".$row['faxexten']."'";
220             $user = $db->getOne($sql);
221             if (ctype_digit($user)) {
222               $destination = "from-did-direct,$user,1";
223             } else {
224                             $non_converts[] = array('extension' => $row['extension'], 'cidnum' => $row['cidnum'], 'device' => $row['faxexten'], 'user' => $user);
225               continue;
226             }
227           }
228               $insert_array[] = array($row['extension'], $row['cidnum'], $detection, $detectionwait, $destination, $legacy_email);
229         break;
230       }
231     }
232
233     if (!empty($insert_array)) {
234           $compiled = $db->prepare("INSERT INTO `fax_incoming` (`extension`, `cidnum`, `detection`, `detectionwait`, `destination`, `legacy_email`) VALUES (?,?,?,?,?,?)");
235           $result = $db->executeMultiple($compiled,$insert_array);
236     }
237         if(!empty($insert_array) && DB::IsError($result)) {
238       out("Fatal error migrating to fax module..legacy data retained in incoming and globals tables");
239           die_freepbx( "Fatal error during migration: " . $result->getMessage() .  "\n");
240         } else {
241             $migrate_array = array('faxexten', 'faxemail', 'wait', 'answer');
242             foreach ($migrate_array as $field) {
243                 outn(sprintf(_("Removing field %s from incoming table.."),$field));
244                 $sql = "ALTER TABLE `incoming` DROP `".$field."`";
245                 $results = $db->query($sql);
246                 if (DB::IsError($results)) {
247                     out(_("not present"));
248                 } else {
249                     out(_("removed"));
250                 }
251             }
252             outn(_("Removing old globals.."));
253       $sql = "DELETE FROM globals WHERE variable IN ('FAX_RX', 'FAX_RX_EMAIL', 'FAX_RX_FROM')";
254
255             $results = $db->query($sql);
256             if (DB::IsError($results)) {
257                 out(_("failed"));
258             } else {
259                 out(_("removed"));
260             }
261
262         $failed_faxes = count($non_converts);
263       outn(_("Checking for failed migrations.."));
264         if ($failed_faxes) {
265         $notifications = notifications::create($db);
266             $extext = _("The following Inbound Routes had FAX processing that failed migration because they were accessing a device with no associated user. They have been disabled and will need to be updated. Click delete icon on the right to remove this notice.")."<br />";
267             foreach ($non_converts as $did) {
268           $didval = trim($did['extension']) == '' ? _("blank") : $did['extension'];
269           $cidval = trim($did['cidnum']) == '' ? _("blank") : $did['cidnum'];
270                 $extext .= "DID: ".$didval." CIDNUM: ".$cidval." PREVIOUS DEVICE: ".$did['device']."<br />";
271             }
272             $notifications->add_error('fax', 'FAXMIGRATE', sprintf(_('%s FAX Migrations Failed'),$failed_faxes), $extext, '', true, true);
273         out(sprintf(_('%s FAX Migrations Failed, check notification panel for details'),$failed_faxes));
274         } else {
275         out(_("all migrations succeeded successfully"));
276       }
277         }
278   } else {
279       out(_("No Inbound Routes to migrate"));
280   }
281 } else {
282     out(_("already done"));
283 }
284 ?>
285
Note: See TracBrowser for help on using the browser.