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

Revision 10609, 10.0 kB (checked in by p_lindheimer, 2 years ago)

have destinations provided for these featurecodes re #4653 (and making paging codes enabled by default)

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->setProvideDest();
106 $fcc->update();
107 unset($fcc);
108
109 //check to make sure that min/maxrate and ecm are set; if not set them to defaults
110 $settings=sql('SELECT * FROM fax_details', 'getAssoc', 'DB_FETCHMODE_ASSOC');
111 foreach($settings as $setting => $value){$set[$setting]=$value['0'];}
112 if(!is_array($set)){$set=array();}//never return a null value
113 if(!$set['minrate']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("minrate","14400")';}
114 if(!$set['maxrate']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("maxrate","14400")';}
115 if(!$set['ecm']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("ecm","yes")';}
116 if(!$set['legacy_mode']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("legacy_mode","no")';}
117 if(!$set['force_detection']){$sql[]='REPLACE INTO fax_details (`key`, `value`) VALUES ("force_detection","no")';}
118  
119 if(isset($sql)){
120     foreach ($sql as $statement){
121         $check = $db->query($statement);
122         if (DB::IsError($check)){
123             die_freepbx( "Can not execute $statement : " . $check->getMessage() .  "\n");
124         }
125     }
126 }
127 /*
128 incoming columns:
129
130 faxexten: disabled
131           default (check what global is)
132           device_num
133
134 determine what default is, if a device then treat as that default device, if system
135 then treat as it was system here, and if disabled then treat as that.
136
137 legacy_email:
138   null -> not in legacy mode
139   blank or value -> in legacy mode
140
141 */
142 outn(_("Checking if legacy fax needs migrating.."));
143 $sql = "SELECT `extension`, `cidnum`, `faxexten`, `faxemail`, `wait`, `answer` FROM `incoming`";
144 $legacy_settings = $db->getAll($sql, DB_FETCHMODE_ASSOC);
145 if(!DB::IsError($legacy_settings)) {
146     out(_("starting migration"));
147
148   // First step, need to get global settings and if not present use defaults
149   //
150   $sql = "SELECT variable, value FROM globals WHERE variable IN ('FAX_RX', 'FAX_RX_EMAIL', 'FAX_RX_FROM')";
151   $globalvars = $db->getAll($sql, DB_FETCHMODE_ASSOC);
152
153   foreach ($globalvars as $globalvar) {
154       $global[trim($globalvar['variable'])] = $globalvar['value'];   
155   }
156   $fax_rx =          isset($global['FAX_RX'])       ? $global['FAX_RX'] : 'disabled';
157   $fax_rx_email =    isset($global['FAX_RX_EMAIL']) ? $global['FAX_RX_EMAIL'] : '';
158   $sender_address  = isset($global['FAX_RX_FROM'])  ? $global['FAX_RX_FROM'] : '';
159
160   // Now some sanity settings, can't email the fax if no email present
161   if ($fax_rx_email == '') {
162     $fax_rx = 'disabled';
163   }
164
165   // TODO Update Module Defaults Here
166   // insert_general_values()
167   //
168   $global_migrate = array();
169   $global_migrate[] = array('sender_address',$sender_address);
170   $global_migrate[] = array('fax_rx_email',$fax_rx_email);
171
172     outn(_("migrating defaults.."));
173     $compiled = $db->prepare("REPLACE INTO `fax_details` (`key`, `value`) VALUES (?,?)");
174     $result = $db->executeMultiple($compiled,$global_migrate);
175     if(DB::IsError($result)) {
176     out(_("failed"));
177         die_freepbx( "Fatal error during migration: " . $result->getMessage() .  "\n");
178     } else {
179     out(_("migrated"));
180   }
181
182     $detection_type = array(0 => 'dahdi', 1 => 'dahdi', 2 => 'nvfax');
183     $non_converts = array();
184
185   if (count($legacy_settings)) {
186     foreach($legacy_settings as $row) {
187       $legacy_email = null;
188       if ($row['faxexten'] == 'default') {
189         $row['faxexten'] = $fax_rx;
190       } else if ($row['faxexten'] == '') {
191         $row['faxexten'] = 'disabled';
192             }
193             if ($row['wait'] < 2) {
194         $detectionwait = '2';
195       } elseif ($row['wait'] > 10) {
196         $detectionwait = '10';
197       } else {
198         $detectionwait = $row['wait'];
199       }
200       $detection = $detection_type[$row['answer']];
201       switch ($row['faxexten']) {
202         case 'disabled':
203           continue; // go back to foreach for now
204         break;
205
206         case 'system':
207           $legacy_email = $row['faxemail'] ? $row['faxemail'] : $fax_rx_email;
208
209           // Now some sanity, if faxemail is blank then it won't work and we treat as disabled
210           //
211           if (!$legacy_email) {
212             continue;
213           }
214           $destination = '';
215                 $insert_array[] = array($row['extension'], $row['cidnum'], $detection, $detectionwait, $destination, $legacy_email);
216         break;
217
218         default:
219           if (ctype_digit($row['faxexten'])) {
220             $sql = "SELECT `user` FROM `devices` WHERE `id` = '".$row['faxexten']."'";
221             $user = $db->getOne($sql);
222             if (ctype_digit($user)) {
223               $destination = "from-did-direct,$user,1";
224             } else {
225                             $non_converts[] = array('extension' => $row['extension'], 'cidnum' => $row['cidnum'], 'device' => $row['faxexten'], 'user' => $user);
226               continue;
227             }
228           }
229               $insert_array[] = array($row['extension'], $row['cidnum'], $detection, $detectionwait, $destination, $legacy_email);
230         break;
231       }
232     }
233
234     if (!empty($insert_array)) {
235           $compiled = $db->prepare("INSERT INTO `fax_incoming` (`extension`, `cidnum`, `detection`, `detectionwait`, `destination`, `legacy_email`) VALUES (?,?,?,?,?,?)");
236           $result = $db->executeMultiple($compiled,$insert_array);
237     }
238         if(!empty($insert_array) && DB::IsError($result)) {
239       out("Fatal error migrating to fax module..legacy data retained in incoming and globals tables");
240           die_freepbx( "Fatal error during migration: " . $result->getMessage() .  "\n");
241         } else {
242             $migrate_array = array('faxexten', 'faxemail', 'wait', 'answer');
243             foreach ($migrate_array as $field) {
244                 outn(sprintf(_("Removing field %s from incoming table.."),$field));
245                 $sql = "ALTER TABLE `incoming` DROP `".$field."`";
246                 $results = $db->query($sql);
247                 if (DB::IsError($results)) {
248                     out(_("not present"));
249                 } else {
250                     out(_("removed"));
251                 }
252             }
253             outn(_("Removing old globals.."));
254       $sql = "DELETE FROM globals WHERE variable IN ('FAX_RX', 'FAX_RX_EMAIL', 'FAX_RX_FROM')";
255
256             $results = $db->query($sql);
257             if (DB::IsError($results)) {
258                 out(_("failed"));
259             } else {
260                 out(_("removed"));
261             }
262
263         $failed_faxes = count($non_converts);
264       outn(_("Checking for failed migrations.."));
265         if ($failed_faxes) {
266         $notifications = notifications::create($db);
267             $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 />";
268             foreach ($non_converts as $did) {
269           $didval = trim($did['extension']) == '' ? _("blank") : $did['extension'];
270           $cidval = trim($did['cidnum']) == '' ? _("blank") : $did['cidnum'];
271                 $extext .= "DID: ".$didval." CIDNUM: ".$cidval." PREVIOUS DEVICE: ".$did['device']."<br />";
272             }
273             $notifications->add_error('fax', 'FAXMIGRATE', sprintf(_('%s FAX Migrations Failed'),$failed_faxes), $extext, '', true, true);
274         out(sprintf(_('%s FAX Migrations Failed, check notification panel for details'),$failed_faxes));
275         } else {
276         out(_("all migrations succeeded successfully"));
277       }
278         }
279   } else {
280       out(_("No Inbound Routes to migrate"));
281   }
282 } else {
283     out(_("already done"));
284 }
285 ?>
286
Note: See TracBrowser for help on using the browser.