root/contributed_modules/modules/a2billing/install.php

Revision 10405, 5.6 kB (checked in by mbrevda, 3 years ago)

new a2billing module, based off a2b 1.8.1. This mod is really limited in scope and ability, as of now handels intial install, using freepbx credentials.

Line 
1 <?php
2 $path = dirname(__FILE__) ;
3
4 if (! function_exists("out")) {
5     function out($text) {
6         if (php_sapi_name() == 'cli') {
7             echo $text ."\n";
8         } else {
9             echo $text."<br />";
10         }
11     }
12 }
13
14
15 //check if web dir's parent is writeable by the current apache user and
16 //attempt to create the dir beofre anything else - no need to go through the installation if it will fail anyway
17 $a2bweb = $amp_conf['AMPWEBROOT'] . '/a2b';
18
19 if (is_dir($a2bweb) && is_writable($a2bweb) || @mkdir($a2bweb, 0755)) {
20      //were good here, move along
21 } else {
22     out(_('The a2billing web directory isn\'t wrtiable. Please execute the following commands:'));
23     out(_( 'mkdir ' . $a2bweb ));
24     out(_( 'chown '
25         . (isset($amp_conf['AMPASTERISKWEBUSER']) ? $amp_conf['AMPASTERISKWEBUSER'] : 'asterisk')
26         . ':'
27         . (isset($amp_conf['AMPASTERISKWEBGROUP']) ? $amp_conf['AMPASTERISKWEBGROUP'] : 'asterisk')
28         . ' ' .$a2bweb
29         ));
30     out(_( 'chmod 0755 ' . $a2bweb));
31     exit(1);
32 }
33
34 //ensure that were using mysql, a2billing dosent support sqlite
35 if ($amp_conf['AMPDBENGINE'] != 'mysql') {
36     die_freepbx(_('A2Billing only support a mysql DB'));
37 }
38
39 //Check if there is already an a2b database, dont re-create it if it exists.
40 out(_("Checking if DB exists..."));
41
42 $sql = 'SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "mya2billing"';
43 $q = $db->getOne($sql);
44
45 if ($q == 'mya2billing') { //TODO: make sure this test works
46     out(_("A2Billing DB already exists. Assuming proper install and using current data. ..."));
47 } else {
48     out(_("A2Billing tables are missing..."));
49
50     $msg = 'The database for A2Billing needs to be set up. Please log in to mysql and execture the following commands.'
51         . ' When done, rerun the instalation of this module:'
52         . "\n"
53         . 'GRANT ALL PRIVILEGES ON mya2billing.* TO "' . $amp_conf['AMPDBUSER'] . '"@"' . $amp_conf['AMPDBHOST'] . '" IDENTIFIED BY "' . $amp_conf['AMPDBPASS'] . '";'
54         . "\n"
55         . 'CREATE DATABASE IF NOT EXISTS `mya2billing`;'
56         . "\n"
57         . 'exit'
58         . "\n"
59         . 'Then, do the following from the command line:'
60         . "\n"
61         . 'mysql -u<privlaged user> -p <password> -Dmya2billing < '
62         . $path . '/a2b/DataBase/mysql-5.x/a2billing-mysql-schema-v1.7.0.sql'
63         . "\n";
64     out(_($msg));
65     exit(1);
66
67
68 }
69
70 //install files
71 //agi files
72 system('ln -sf ' . $path . '/AGI/' . $amp_conf['AMPWEBROOT'] . '/admin/modules/a2billing/ag-bin');
73
74 //web files
75 system('ln -sf ' . $path . '/a2b/admin/ '     . $a2bweb . '/admin');
76 system('ln -sf ' . $path . '/a2b/agent/ '     . $a2bweb . '/agent');
77 system('ln -sf ' . $path . '/a2b/customer/ ' . $a2bweb . '/customer');
78 system('ln -sf ' . $path . '/a2b/common/ '     . $a2bweb . '/common');
79
80 //symlink soundfiles
81 system('ln -sf ' . $path . '/a2b/addons/sounds ' . $path . '/sounds');
82
83 //set a2billing to use freepbx's asterisk manager credentials
84 $dsn = array(
85         'phptype'  => $amp_conf['AMPDBENGINE'],
86         'username' => $amp_conf['AMPDBUSER'],
87         'password' => $amp_conf['AMPDBPASS'],
88         'hostspec' => $amp_conf['AMPDBHOST'],
89         'database' => 'mya2billing'
90     );
91
92 $a2bdb = DB::connect($dsn);
93
94 $settings = array(
95             array('id' => 8, 'val' => $amp_conf["ASTMANAGERHOST"]),
96             array('id' => 9, 'val' => $amp_conf["AMPMGRUSER"]),
97             array('id' => 10, 'val' => $amp_conf["AMPMGRPASS"])
98         );
99
100 foreach($settings as $set) {
101     $sql = 'UPDATE cc_config set config_value = ? WHERE id = ? ';
102     $q = $a2bdb->query($sql, array($set['val'],$set['id']));
103     if (DB::isError($q)) {
104         die_freepbx($q->getMessage());
105     }
106 }
107
108
109 //
110 $ini = parse_ini_file($path . '/a2b/a2billing.conf', true);
111
112 $ini['database']['hostname']    = $amp_conf['AMPDBHOST'];
113 $ini['database']['user']        = $amp_conf['AMPDBUSER'];
114 $ini['database']['password']    = $amp_conf['AMPDBPASS'];
115 $ini['database']['dbname']        = 'mya2billing';
116 $ini['database']['dbtype']        = $amp_conf['AMPDBENGINE'];
117 $ini['handler_FileHandler']['args']            = '(' . $ini['handler_FileHandler']['args'] . ')'; //fix for php ini parser oddities
118 $ini['handler_consoleHandler']['args']        = '(' . $ini['handler_consoleHandler']['args'] . ')';
119
120 if (!function_exists('write_ini_file')) {
121     function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {
122         $content = "";
123
124         if ($has_sections) {
125             foreach ($assoc_arr as $key=>$elem) {
126                 $content .= "[".$key."]\n";
127                 foreach ($elem as $key2=>$elem2)
128                 {
129                     if(is_array($elem2))
130                     {
131                         for($i=0;$i<count($elem2);$i++)
132                         {
133                             //$content .= $key2."[] = \"".$elem2[$i]."\"\n";
134                              $content .= $key2."[] = ".$elem2[$i]."\n";
135                         }
136                     }
137                     else if($elem2=="") $content .= $key2." = \n";
138                     //else $content .= $key2." = \"".$elem2."\"\n";
139                     else $content .= $key2." = ".$elem2."\n";
140                 }
141             }
142         }
143         else {
144             foreach ($assoc_arr as $key=>$elem) {
145                 if(is_array($elem))
146                 {
147                     for($i=0;$i<count($elem);$i++)
148                     {
149                         //$content .= $key2."[] = \"".$elem[$i]."\"\n";
150                         $content .= $key2."[] = ".$elem[$i]."\n";
151                     }
152                 }
153                 else if($elem=="") $content .= $key2." = \n";
154                 //else $content .= $key2." = \"".$elem."\"\n";
155                 else $content .= $key2." = ".$elem."\n";
156             }
157         }
158
159         if (!$handle = fopen($path, 'w')) {
160             return false;
161         }
162         if (!fwrite($handle, $content)) {
163             return false;
164         }
165         fclose($handle);
166
167         return true;
168     }
169 }
170
171 write_ini_file($ini, '/etc/asterisk/a2billing.conf', true);
172
173 ?>
Note: See TracBrowser for help on using the browser.