Beispiel #1
0
 public function __construct()
 {
     $db = FreePBX::Database();
     $dash = FreePBX::Dashboard();
     if (!is_object($db)) {
         throw new Exception("DB isn't a Database?");
     }
     if (!is_object($dash)) {
         throw new Exception("Dash isn't an Object?");
     }
     $this->db = $db;
     $this->dash = $dash;
     // Generate a temporary 'what's next' array for lookups.
     foreach ($this->periods as $p => $i) {
         if ($this->last) {
             $this->pnext[$this->last] = $p;
             $this->last = $p;
         } else {
             $this->last = $p;
         }
     }
 }
Beispiel #2
0
 /**
  * Get the feed from cache or retrieve it
  * @param  string $feed The feed URL
  * @return object       Reader object
  */
 private function getFeed($feed)
 {
     $reader = new Reader();
     $d = \FreePBX::Dashboard();
     $etag = $d->getConfig($feed, "etag");
     $last_modified = $d->getConfig($feed, "last_modified");
     try {
         $resource = $reader->download($feed, $last_modified, $etag);
         if ($resource->isModified()) {
             $parser = $reader->getParser($resource->getUrl(), $resource->getContent(), $resource->getEncoding());
             $content = $parser->execute();
             $etag = $resource->getEtag();
             $last_modified = $resource->getLastModified();
             $d->setConfig($feed, $content, "content");
             $d->setConfig($feed, $etag, "etag");
             $d->setConfig($feed, $last_modified, "last_modified");
         } else {
             $content = $d->getConfig($feed, "content");
         }
     } catch (\PicoFeed\PicoFeedException $e) {
         $content = $d->getConfig($feed, "content");
     }
     return $content;
 }
Beispiel #3
0
 private function getURL($url)
 {
     // Check to see if we've already grabbed this recently
     $d = \FreePBX::Dashboard();
     $res = $d->getConfig($url, "Blogs");
     // 3 Hours.
     $expired = time() - 10800;
     // Has this expired, or is it new?
     if (!$res || $res['timestamp'] < $expired) {
         $contents = file_get_contents($url);
         $res['timestamp'] = time();
         $res['contents'] = $contents;
         $d->setConfig($url, $res, "Blogs");
     }
     return $res['contents'];
 }
 private function genAlertGlyphicon($res, $tt = null)
 {
     return \FreePBX::Dashboard()->genStatusIcon($res, $tt);
 }
<?php

// vim: set ai ts=4 sw=4 ft=phtml:
// New Dashboard
//	License for all code of this FreePBX module can be found in the license file inside the module directory
//	Copyright 2013 Schmooze Com Inc.
//
if (!class_exists('DashboardHooks')) {
    include 'classes/DashboardHooks.class.php';
}
$allhooks = DashboardHooks::genHooks(FreePBX::Dashboard()->getConfig('visualorder'));
FreePBX::Dashboard()->setConfig('allhooks', $allhooks);
show_view(__DIR__ . '/views/main.php', array("brand" => FREEPBX::Config()->get('DASHBOARD_FREEPBX_BRAND')));
Beispiel #6
0
 public function get_connections()
 {
     // Grab our list of extensions.
     $sql = "SELECT `id` FROM `devices` WHERE `tech` <> 'custom'";
     $alldevices = FreePBX::create()->Database->query($sql)->fetchAll(PDO::FETCH_COLUMN, 0);
     $devices = array_flip($alldevices);
     $protocols = array("sip", "iax2", "pjsip");
     $vars = array("users_online", "users_offline", "users_total", "trunks_online", "trunks_offline", "trunks_total", "registrations_online", "registrations_offline", "registrations_total");
     // Build array to return
     foreach ($protocols as $p) {
         foreach ($vars as $v) {
             $retarr[$p . "_" . $v] = 0;
         }
     }
     // Add Totals
     foreach ($vars as $v) {
         $retarr[$v] = 0;
     }
     if (!$this->astman) {
         return $retarr;
     }
     $response = $this->astman->send_request('Command', array('Command' => "sip show peers"));
     $astout = explode("\n", $response['data']);
     $blacklist = \FreePBX::Dashboard()->extIgnoreList();
     foreach ($astout as $line) {
         // Previous bug IRT trunks starting or ending with /'s here. Investigate.
         $exploded = preg_split('/\\s+/', $line);
         if (strpos($exploded[0], '/') === false) {
             $name = $exploded[0];
         } else {
             list($name, $null) = explode('/', $exploded[0]);
         }
         //prefix blacklist
         foreach ($blacklist as $num) {
             if (substr($name, 0, $num['length']) == $num['value'] && $name !== $num['value']) {
                 continue;
             }
         }
         // How to we see if a trunk is down?
         if ($exploded[1] == "(Unspecified)" || $exploded[5] == "UNREACHABLE" || $exploded[6] == "UNREACHABLE") {
             // This is a device that's down
             if (!isset($devices[$name])) {
                 // It is, actually a TRUNK that's down.
                 $retarr['sip_trunks_offline']++;
             } else {
                 $retarr['sip_users_offline']++;
             }
         } elseif (filter_var($exploded[1], FILTER_VALIDATE_IP)) {
             // This is a device that's up.
             if (!isset($devices[$name])) {
                 $retarr['sip_trunks_online']++;
             } else {
                 $retarr['sip_users_online']++;
             }
         }
         // else it's not a device.
     }
     $response = $this->astman->send_request('Command', array('Command' => "sip show registry"));
     $astout = explode("\n", $response['data']);
     $pos = false;
     foreach ($astout as $line) {
         if (trim($line) != '') {
             if ($pos === false) {
                 // find the position of "State" in the first line
                 $pos = strpos($line, "State");
             } else {
                 // subsequent lines, check if it says "Registered" at that position
                 if (substr($line, $pos, 10) == "Registered") {
                     $retarr['sip_registrations_online']++;
                 } elseif (strlen($line) > $pos) {
                     $retarr['sip_registrations_offline']++;
                 }
             }
         }
     }
     $response = $this->astman->send_request('Command', array('Command' => "iax2 show peers"));
     $astout = explode("\n", $response['data']);
     foreach ($astout as $line) {
         if (preg_match('/^(([a-z0-9\\-_]+)(\\/([a-z0-9\\-_]+))?)\\s+(\\([a-z]+\\)|\\d{1,3}(\\.\\d{1,3}){3})/i', $line, $matches)) {
             //matches: [2] = name, [4] = username, [5] = host, [6] = part of ip (if IP)
             // have an IP address listed, so its online
             $online = !empty($matches[6]);
             if (!isset($devices[$matches[2]])) {
                 // this is a trunk
                 //TODO match trunk tech as well?
                 $retarr['iax2_trunks_' . ($online ? 'online' : 'offline')]++;
             } else {
                 $retarr['iax2_users_' . ($online ? 'online' : 'offline')]++;
             }
         }
     }
     $response = $this->astman->send_request('Command', array('Command' => "iax2 show registry"));
     $astout = explode("\n", $response['data']);
     $pos = false;
     foreach ($astout as $line) {
         if (trim($line) != '') {
             if ($pos === false) {
                 // find the position of "State" in the first line
                 $pos = strpos($line, "State");
             } else {
                 // subsequent lines, check if it syas "Registered" at that position
                 if (substr($line, $pos, 10) == "Registered") {
                     $retarr['iax2_registrations_online']++;
                 } elseif (strlen($line) > $pos) {
                     $retarr['iax2_registrations_offline']++;
                 }
             }
         }
     }
     $response = $this->astman->send_request('Command', array('Command' => "pjsip show endpoints"));
     // This is an amazingly awful format to parse.
     $lines = explode("\n", $response['data']);
     $inheader = true;
     $istrunk = $isendpoint = false;
     foreach ($lines as $l) {
         if ($inheader) {
             if (isset($l[1]) && $l[1] == "=") {
                 // Last line of the header.
                 $inheader = false;
             }
             continue;
         }
         $l = trim($l);
         if (!$l) {
             continue;
         }
         // If we have a line starting with 'Endpoint:' then we found one!
         if (strpos($l, "Endpoint:") === 0) {
             if (preg_match("/Endpoint:\\s+(.+)\\/(.+?)\\b\\s+(.+)/", $l, $out)) {
                 // Found a device
                 $isendpoint = $out[1];
                 $istrunk = false;
                 if (isset($out[3]) && strpos($out[3], "Unavail") === 0) {
                     // Unavailable endpoint.
                     $retarr['pjsip_users_offline']++;
                 } else {
                     $retarr['pjsip_users_online']++;
                 }
                 continue;
             } elseif (preg_match("/Endpoint:\\s+(.+?)\\b/", $l, $out)) {
                 // Found a trunk
                 $isendpoint = false;
                 $istrunk = $out[1];
                 continue;
             } else {
                 throw new \Exception("Unable to parse endpoint {$l}");
             }
         }
         // If we have a Contact: line, then that's something that's registered!
         if (strpos($l, "Contact:") === 0) {
             if ($isendpoint !== false) {
                 // This is a registered endpoint
                 $retarr['pjsip_registrations_online']++;
             } elseif ($istrunk !== false) {
                 // Trunk status... Check for 'avail'
                 if (strpos($l, "Avail ") === false) {
                     // Trunk down.
                     $retarr['pjsip_trunks_offline']++;
                 } else {
                     $retarr['pjsip_trunks_online']++;
                 }
             } else {
                 throw new \Exception("Found a contact before I figured out what it is!");
             }
         }
     }
     // Now figure out the totals.
     foreach ($protocols as $p) {
         $users = $retarr[$p . "_users_online"] + $retarr[$p . "_users_offline"];
         $retarr[$p . "_users_total"] = $users;
         $trunks = $retarr[$p . "_trunks_online"] + $retarr[$p . "_trunks_offline"];
         $retarr[$p . "_trunks_total"] = $trunks;
         $regs = $retarr[$p . "_registrations_online"] + $retarr[$p . "_registrations_offline"];
         $retarr[$p . "_registrations_total"] = $regs;
     }
     foreach ($vars as $v) {
         foreach ($protocols as $p) {
             $retarr[$v] += $retarr[$p . "_" . $v];
         }
     }
     return $retarr;
 }
// Copyright 2013 Schmooze Com Inc.
//
// Dashboard Scheduler.
// Runs every minute.
//
// Start quickly.
$bootstrap_settings['freepbx_auth'] = false;
// Just in case.
$restrict_mods = true;
// Takes startup from 0.2 seconds to 0.07 seconds.
include '/etc/freepbx.conf';
$astrundir = \FreePBX::Config()->get('ASTRUNDIR');
if (!is_dir($astrundir) || !is_writable($astrundir)) {
    echo "Asterisk Run Dir [" . $astrundir . "] is missing or not writable! Is Asterisk running?\n";
    exit(1);
}
$lockfile = $astrundir . "/scheduler.lock";
// Sleep to fix crazy issues with large VM hosting providers
sleep(mt_rand(1, 30));
// Create a lock to make sure no more than one instance of this
// program can be running on a machine at a time
$fh = fopen($lockfile, "a");
if (!$fh || !flock($fh, LOCK_EX | LOCK_NB)) {
    // Unable to lock, we're already running.
    exit;
}
// Run the trigger
\FreePBX::Dashboard()->runTrigger();
// remove lockfile, and then close handle to release kernel lock
unlink($lockfile);
fclose($fh);