root/contributed_modules/modules/smartroutes/bin/clean_calltracking.php

Revision 12077, 8.4 kB (checked in by escape2mtns, 2 years ago)

fix func redefinition compatibility with FreePBX 2.9

  • Property svn:executable set to *
Line 
1 #!/usr/bin/php -q
2 <?php
3 /**
4  * FreePBX SmartRoutes Module
5  *
6  * Copyright (c) 2011, VoiceNation, LLC.
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License Version 2.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19 */
20
21 // include bootstrap
22 $bootstrap_settings['freepbx_auth'] = false;
23 if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
24   @include_once('/etc/asterisk/freepbx.conf');
25 }
26
27 if(!isset($amp_conf)) {
28     // FREEPBX 2.9+ BOOTSTRAP NOT DONE
29     // initialize FreePBX in pre-2.9 fashion
30     initLegacyFreePBX();
31 }
32
33 // =============================================               
34 // **** OUR CODE TO CLEAN CALL TRACKING DB) ****
35 // =============================================
36 // not sure this is needed but it's a good idea either way
37 // could get fancy, conect to AMI and get a list of current calls to comprare but for now....
38 // This will remove any entries that might have been orphaned (calls over an hour old)   
39 // update freepbx cron job to cleanup the calltracking (as required)
40 $sql = "DELETE FROM smartroute_currentcalls WHERE calldate < DATE_SUB(NOW(), INTERVAL 1 HOUR)";
41 $db->query($sql);
42
43 exit(0);
44
45 // =============================               
46 // **** DONE WITH CRON TASK ****
47 // =============================
48
49 /********************************************************************************************************************/
50 /* FREEPBX PRE-2.9 INITIALIZATION/BOOTSTRAP                                                                         */
51 /********************************************************************************************************************/
52 function initLegacyFreePBX() {
53     global $amportalconf;
54     global $db_engine;
55     global $amp_conf;
56     global $asterisk_conf;
57     global $db;
58     global $amp_conf_defaults;
59     
60     // taken from freepbx-cron-scheduler (so we can connect with FreePBX asterisk DB)
61     define("AMP_CONF", "/etc/amportal.conf");
62     $amportalconf = AMP_CONF;
63
64     // **** Make sure we have STDIN etc
65
66     // from  ben-php dot net at efros dot com   at  php.net/install.unix.commandline
67     if (version_compare(phpversion(),'4.3.0','<') || !defined("STDIN")) {
68           define('STDIN',fopen("php://stdin","r"));
69           define('STDOUT',fopen("php://stdout","r"));
70           define('STDERR',fopen("php://stderr","r"));
71           register_shutdown_function( create_function( '' , 'fclose(STDIN); fclose(STDOUT); fclose(STDERR); return true;' ) );
72     }
73
74     // **** Make sure we have PEAR's DB.php, and include it
75
76     outn(_("Checking for PEAR DB.."));
77     if (! @ include('DB.php')) {
78           out(_("FAILED"));
79           fatal(_("PEAR Missing"),sprintf(_("PEAR must be installed (requires DB.php). Include path: %s "), ini_get("include_path")));
80     }
81     out(_("OK"));
82
83     // **** Check for amportal.conf
84
85     outn(sprintf(_("Checking for %s "), $amportalconf)._(".."));
86     if (!file_exists($amportalconf)) {
87           fatal(_("amportal.conf access problem: "),sprintf(_("The %s file does not exist, or is inaccessible"), $amportalconf));
88     }
89     out(_("OK"));
90
91     // **** read amportal.conf
92
93     outn(sprintf(_("Bootstrapping %s .."), $amportalconf));
94     $amp_conf = parse_amportal_conf_bootstrap($amportalconf);
95     if (count($amp_conf) == 0) {
96           fatal(_("amportal.conf parsing failure"),sprintf(_("no entries found in %s"), $amportalconf));
97     }
98     out(_("OK"));
99
100     outn(sprintf(_("Parsing %s .."), $amportalconf));
101     require_once($amp_conf['AMPWEBROOT']."/admin/functions.inc.php");
102     $amp_conf = parse_amportal_conf($amportalconf);
103     if (count($amp_conf) == 0) {
104           fatal(_("amportal.conf parsing failure"),sprintf(_("no entries found in %s"), $amportalconf));
105     }
106     out(_("OK"));
107
108     $asterisk_conf_file = $amp_conf["ASTETCDIR"]."/asterisk.conf";
109     outn(sprintf(_("Parsing %s .."), $asterisk_conf_file));
110     $asterisk_conf = parse_asterisk_conf($asterisk_conf_file);
111     if (count($asterisk_conf) == 0) {
112           fatal(_("asterisk.conf parsing failure"),sprintf(_("no entries found in %s"), $asterisk_conf_file));
113     }
114     out(_("OK"));
115
116     // **** Connect to database
117
118     outn(_("Connecting to database.."));
119
120     # the engine to be used for the SQL queries,
121     # if none supplied, backfall to mysql
122     $db_engine = "mysql";
123     if (isset($amp_conf["AMPDBENGINE"])){
124           $db_engine = $amp_conf["AMPDBENGINE"];
125     }
126
127     // Define the notification class for logging to the dashboard
128     //
129     $nt = notifications::create($db);
130
131     switch ($db_engine)
132     {
133       case "pgsql":
134       case "mysql":
135         /* datasource in in this style:
136         dbengine://username:password@host/database */
137
138         $db_user = $amp_conf["AMPDBUSER"];
139         $db_pass = $amp_conf["AMPDBPASS"];
140         $db_host = $amp_conf["AMPDBHOST"];
141         $db_name = $amp_conf["AMPDBNAME"];
142     
143         $datasource = $db_engine.'://'.$db_user.':'.$db_pass.'@'.$db_host.'/'.$db_name;
144         $db = DB::connect($datasource); // attempt connection
145         break;
146     
147       case "sqlite":
148         require_once('DB/sqlite.php');
149     
150         if (!isset($amp_conf["AMPDBFILE"]))
151           fatal(_("AMPDBFILE not setup properly"),sprintf(_("You must setup properly AMPDBFILE in %s "), $amportalconf));
152     
153         if (isset($amp_conf["AMPDBFILE"]) == "")
154           fatal(_("AMPDBFILE not setup properly"),sprintf(_("AMPDBFILE in %s cannot be blank"), $amportalconf));
155     
156         $DSN = array (
157           "database" => $amp_conf["AMPDBFILE"],
158           "mode" => 0666
159         );
160     
161         $db = new DB_sqlite();
162         $db->connect( $DSN );
163         break;
164     
165       case "sqlite3":
166         if (!isset($amp_conf["AMPDBFILE"]))
167           fatal("You must setup properly AMPDBFILE in $amportalconf");
168     
169         if (isset($amp_conf["AMPDBFILE"]) == "")
170           fatal("AMPDBFILE in $amportalconf cannot be blank");
171     
172         require_once('DB/sqlite3.php');
173         $datasource = "sqlite3:///" . $amp_conf["AMPDBFILE"] . "?mode=0666";
174         $db = DB::connect($datasource);
175         break;
176     
177       default:
178         fatal( "Unknown SQL engine: [$db_engine]");
179     }
180     
181     if(DB::isError($db)) {
182       out(_("FAILED"));
183       debug($db->userinfo);
184       fatal(_("database connection failure"),("failed trying to connect to the configured database"));
185     
186     }
187     out(_("OK"));   
188 }
189
190
191 /********************************************************************************************************************/
192 /* FUNCTIONS USED IN FREEPBX PRE-2.9 INITIALIZATION */
193 /********************************************************************************************************************/
194 // Emulate gettext extension functions if gettext is not available
195 if (!function_exists('_')) {
196   function _($str) {
197     return $str;
198   }
199 }
200
201 if (!function_exists('out')) {
202     function out($text) {
203       //echo $text."\n";
204     }
205 }
206     
207 if (!function_exists('outn')) {   
208     function outn($text) {
209       //echo $text;
210     }
211 }
212     
213 if (!function_exists('error')) {
214     function error($text) {
215       echo "[ERROR] ".$text."\n";
216     }
217 }
218     
219 if (!function_exists('fatal')) {
220     function fatal($text, $extended_text="", $type="FATAL") {
221       global $db;
222     
223       echo "[$type] ".$text." ".$extended_text."\n";
224     
225       if(!DB::isError($db)) {
226         $nt = notifications::create($db);
227         $nt->add_critical('cron_manager', $type, $text, $extended_text);
228       }
229     
230       exit(1);
231     }
232 }
233     
234 if (!function_exists('debug')) {
235     function debug($text) {
236       global $debug;
237     
238       if ($debug) echo "[DEBUG-preDB] ".$text."\n";
239     }
240 }
241
242 // bootstrap retrieve_conf by getting the AMPWEBROOT since that is currently where the necessary
243 // functions.inc.php resides, and then use that parser to properly parse the file and get all
244 // the defaults as needed.
245 //
246 function parse_amportal_conf_bootstrap($filename) {
247   $file = file($filename);
248   foreach ($file as $line) {
249     if (preg_match("/^\s*([\w]+)\s*=\s*\"?([\w\/\:\.\*\%-]*)\"?\s*([;#].*)?/",$line,$matches)) {
250       $conf[ $matches[1] ] = $matches[2];
251     }
252   }
253   if ( !isset($conf["AMPWEBROOT"]) || ($conf["AMPWEBROOT"] == "")) {
254     $conf["AMPWEBROOT"] = "/var/www/html";
255   } else {
256     $conf["AMPWEBROOT"] = rtrim($conf["AMPWEBROOT"],'/');
257   }
258
259   return $conf;
260 }
261
262 /********************************************************************************************************************/
263
264
265 ?>
Note: See TracBrowser for help on using the browser.