public function exec($params)
 {
     $data = array();
     $module = isset($params['module']) ? $params['module'] : 'servers';
     $view = isset($params['view']) ? $params['view'] : 'index';
     $data['module'] = $module;
     $data['view'] = $view;
     $data['title'] = '';
     $data['uri'] = '/' . $module . '/' . $view;
     $data['uriArray'] = array('/', $module, $view);
     switch ($module) {
         case 'domains':
             $domains = R::find('domain');
             $data['title'] = 'Domains';
             $data['domains'] = $domains;
             $data['template'] = 'design/desktop/templates/domains.tpl.php';
             break;
         case 'accounts':
             $data['title'] = 'Domains to accounts';
             $data['domains'] = getUnrelatedMainDomains();
             $data['template'] = 'design/desktop/templates/accounts.tpl.php';
             break;
         case 'servers':
             $data['title'] = 'Servers';
             $data['hasFieldSelector'] = true;
             $data['avaliableFields'] = getAvaliableFields('servers');
             $data['enabledFields'] = getEnabledFields('servers');
             $data['serversGrouped'] = getGroupedByType();
             $data['template'] = 'design/desktop/templates/servers_list.tpl.php';
             break;
         case 'search':
             $data['title'] = 'Search';
             $data['template'] = 'design/desktop/templates/search.tpl.php';
             break;
         case 'cleanup':
             $data['title'] = 'Cleanup';
             $data['template'] = 'design/desktop/templates/cleanup.tpl.php';
             break;
         default:
             $data['title'] = '404 Page not found';
             $data['template'] = 'design/desktop/templates/error.tpl.php';
             $data['error'] = array('code' => '404', 'msg' => 'Page not found');
             break;
     }
     mvc\render('design/desktop/templates/header.tpl.php', $data);
     mvc\render('design/desktop/templates/top_menu.tpl.php', $data);
     if (isset($data['hasFieldSelector']) && $data['hasFieldSelector']) {
         mvc\render('design/desktop/templates/field_selector.tpl.php', $data);
     }
     if (mvc\retrieve('debug')) {
         $data['values']['params'] = $params;
         mvc\render('design/desktop/templates/debug.tpl.php', $data);
     }
     mvc\render($data['template'], $data);
     mvc\render('design/desktop/templates/footer.tpl.php', $data);
 }
Exemple #2
0
/**
 * Returns an array contaning which fields is enabled for a given view 
 *
 * @param string $view
 * @return array 
 * @author Henrik Farre <*****@*****.**>
 **/
function getEnabledFields($view)
{
    if (isset($_COOKIE['enabledFields'])) {
        $enabledFields = unserialize($_COOKIE['enabledFields']);
        if (isset($enabledFields[$view])) {
            return $enabledFields[$view];
        }
    }
    // Return all
    $availableFields = getAvaliableFields($view);
    return $availableFields;
}
 public function exec($params)
 {
     register_shutdown_function(array($this, 'outPutJson'));
     $this->setJsonMsg(array('msg' => 'No data returned'), self::ERROR);
     switch ($params['action']) {
         case 'accountsToDomains':
             if (empty($_GET['account_id']) || empty($_GET['domains'])) {
                 $this->setJsonMsg(array('msg' => 'Some fields are missing'), self::ERROR);
             } else {
                 $owner = false;
                 $owner = R::findOne('owner', 'account_id=?', array($_GET['account_id']));
                 if (!$owner instanceof RedBean_OODBBean) {
                     $owner = R::dispense("owner");
                     $owner->name = $_GET['account_name'];
                     $owner->account_id = $_GET['account_id'];
                     $id = R::store($owner);
                 }
                 foreach ($_GET['domains'] as $id) {
                     $domain = R::load('domain', $id);
                     R::associate($owner, $domain);
                     /*$otherDomainsDefinedInVhost = R::find("domain","vhost_id = ?",array($domain->vhost_id));
                       foreach ($otherDomainsDefinedInVhost as $otherDomain) 
                       {
                         if ( in_array( $otherDomain->id, $_GET['domains'] ) )
                         {
                           continue;
                         }
                         R::associate( $owner, $otherDomain );
                       }*/
                 }
                 $domains = getUnrelatedMainDomains();
                 $html = '';
                 foreach ($domains as $domain) {
                     $html .= '<option value="' . $domain->id . '">' . $domain->name . '</option>';
                 }
                 $this->setJsonMsg(array('msg' => $owner->name . ' set as owner of domains', 'content' => $html));
             }
             break;
         case 'getDomains':
             // TODO: use search
             $serverID = (int) $_GET['serverID'];
             $server = R::load("server", $serverID);
             $linker = mvc\retrieve('beanLinker');
             $vhostEntries = R::find('vhostEntry', 'server_id = ?', array($server->id));
             if (!empty($vhostEntries)) {
                 $html = '<h1>Domains in vhosts on server (excluding www aliases)</h1><div class="domains list">';
                 foreach ($vhostEntries as $entry) {
                     $domain = R::load('domain', $entry->domain_id);
                     // ignore www aliases
                     if ($domain->sub == 'www' && $domain->type == 'alias') {
                         continue;
                     }
                     $html .= '<div class="domain">
           <div class="status">' . (!$entry->is_valid ? '<img src="/design/desktop/images/error.png" title="' . $entry->note . '" class="icon"/>' : '') . '</div>
           <div class="name' . ($domain->is_active ? '' : ' inactive') . '">' . ($entry->type == 'alias' ? '- ' : '') . '<a href="http://' . $domain->getFQDN() . '">' . $domain->name . '</a></div>
           <br class="cls">
           </div>';
                 }
                 $html .= '</div>';
                 $this->setJsonMsg(array('msg' => 'ok', 'content' => $html));
             } else {
                 $this->setJsonMsg(array('msg' => 'No domains found on server'), self::WARNING);
             }
             break;
         case 'search':
             if (isset($_GET['query']) && isset($_GET['type'])) {
                 $type = $_GET['type'];
                 $query = $_GET['query'];
                 $query = $query . '%';
                 if (isset($_GET['wildcards']) && $_GET['wildcards'] == 'both') {
                     $query = '%' . $query;
                 }
                 $handler = new searchHandler($type);
                 $json = $handler->query($query);
                 $this->setJsonData($json);
             } else {
                 $this->setJsonMsg(array('msg' => 'Missing paramaters'), self::ERROR);
             }
             break;
         case 'missingDomRelation':
             $servers = R::find("server", "type = ?", array('xenu'));
             $content = array();
             foreach ($servers as $server) {
                 $dom0 = false;
                 $dom0 = R::getParent($server);
                 if (!$dom0 instanceof RedBean_OODBBean || empty($dom0->name)) {
                     $content[] = $server->name;
                 }
             }
             if (empty($content)) {
                 $this->setJsonMsg(array('msg' => 'No servers found', 'msg_type' => self::OK), self::ERROR);
             } else {
                 $this->setJsonMsg(array('msg' => 'ok', 'content' => implode('<br>', $content)));
             }
             break;
         case 'missingFieldsOnServer':
             $servers = R::find("server");
             $content = array();
             $allowedMissing = array('comment');
             foreach ($servers as $server) {
                 $missingFields = array();
                 foreach ($server->getIterator() as $key => $value) {
                     if (in_array($key, $allowedMissing)) {
                         continue;
                     }
                     if (empty($value) || is_null($value)) {
                         $missingFields[] = $key;
                     }
                 }
                 if (!empty($missingFields)) {
                     $content[] = 'Id: ' . $server->id . (!empty($server->name) ? ' (' . $server->name . ')' : '') . ' is missing: ' . implode(', ', $missingFields);
                 }
             }
             $this->setJsonMsg(array('msg' => 'ok', 'content' => implode('<br>', $content)));
             break;
         case 'inactiveDomains':
             $domains = R::find("domain", "is_active=?", array(false));
             $content = array();
             foreach ($domains as $domain) {
                 $server = R::load("server", $domain->server_id);
                 $content[] = $domain->name . ' on ' . $server->name . ' last updated ' . date('d-m-Y H:i:s', $domain->updated);
             }
             if (empty($content)) {
                 $this->setJsonMsg(array('msg' => 'No inactive domains found', 'msg_type' => self::OK), self::ERROR);
             } else {
                 $this->setJsonMsg(array('msg' => 'ok', 'content' => implode('<br>', $content)));
             }
             break;
         case 'notUpdatedRecently':
             $content = array();
             $type = $params['segments'][0][0];
             $ts = mktime();
             switch ($type) {
                 case 'domains':
                     $results = R::find("domain", "updated < ?", array($ts));
                     foreach ($results as $domain) {
                         $content[] = $domain->getFQDN();
                     }
                     break;
                 case 'servers':
                     $results = R::find("server", "updated < ?", array($ts));
                     foreach ($results as $result) {
                         $content[] = $result->name;
                     }
                     break;
             }
             if (empty($content)) {
                 $this->setJsonMsg(array('msg' => 'No ' . $type . ' not updated the last 3 days', 'msg_type' => self::OK), self::ERROR);
             } else {
                 $this->setJsonMsg(array('msg' => 'ok', 'content' => implode('<br>', $content)));
             }
             break;
         case 'domainsWithForeignDNS':
             $dnsServers = mvc\retrieve('config')->dnsServers;
             if (is_array($dnsServers)) {
                 $content = array();
                 $ipAddresses = R::find('ip_address', " type = 'NS' AND value NOT IN ('" . implode("', '", $dnsServers) . "') ");
                 foreach ($ipAddresses as $ip) {
                     $domains = R::related($ip, 'domain');
                     foreach ($domains as $domain) {
                         $content[$domain->name] = $domain->name . ' (' . $ip->value . ')';
                     }
                 }
                 $this->setJsonMsg(array('msg' => 'ok', 'content' => implode('<br>', $content)));
             } else {
                 $this->setJsonMsg(array('msg' => 'Please set "dnsServers" as an array in the config file'), self::ERROR);
             }
             break;
             // TODO: should be generalized to allow editing of other fields
         // TODO: should be generalized to allow editing of other fields
         case 'editServerComment':
             $serverID = (int) $_GET['serverID'];
             $server = R::load("server", $serverID);
             if ($server instanceof RedBean_OODBBean) {
                 $content = '<form action="/service/ajax/saveServerComment/?serverID=' . $serverID . '" method="post" id="serverCommentForm">
         <p>
         <textarea name="comment" rows="10" cols="50">' . $server->comment . '</textarea><br>
         <input type="submit" name="serverCommentSaveAction" value="Save" />
         </p>
         </form';
                 $this->setJsonMsg(array('msg' => 'ok', 'content' => $content));
             } else {
                 $this->setJsonMsg(array('msg' => 'Unknown server'), self::ERROR);
             }
             break;
             // TODO: should be generalized to allow editing of other fields
         // TODO: should be generalized to allow editing of other fields
         case 'saveServerComment':
             $comment = $_GET['comment'];
             $serverID = (int) $_GET['serverID'];
             $server = R::load("server", $serverID);
             $server->comment = $comment;
             R::store($server);
             $this->setJsonMsg(array('msg' => 'Comment stored'));
             break;
         case 'setEnabledFields':
             $type = $_GET['type'];
             $availableFields = getAvaliableFields($type);
             $enabledFields = array();
             foreach ($_GET['field'] as $key) {
                 if (isset($availableFields[$key])) {
                     $enabledFields[$key] = $availableFields[$key];
                 }
             }
             setcookie('enabledFields', serialize(array($type => $enabledFields)), time() + 36000, '/');
             $this->setJsonMsg(array('msg' => 'Enabled fields set'));
             break;
         case 'getServerList':
             $data['hasFieldSelector'] = true;
             $data['avaliableFields'] = getAvaliableFields('servers');
             $data['enabledFields'] = getEnabledFields('servers');
             $data['serversGrouped'] = getGroupedByType();
             $data['template'] = 'design/desktop/templates/servers_list.tpl.php';
             ob_start();
             mvc\render($data['template'], $data);
             $content = ob_get_clean();
             $this->setJsonMsg(array('msg' => 'ok', 'content' => $content));
             break;
         case 'getFieldList':
             // TODO: should not be hardcoded
             $avaliableFields = getAvaliableFields('servers');
             $enabledFields = getEnabledFields('servers');
             $avaliableLi = '';
             $enabledLi = '';
             $enabledFieldKeys = array_keys($enabledFields);
             foreach ($avaliableFields as $key => $prettyName) {
                 if (in_array($key, $enabledFieldKeys)) {
                     $enabledLi .= '<li id="field=' . $key . '">' . $prettyName . '</li>';
                 } else {
                     $avaliableLi .= '<li id="field=' . $key . '">' . $prettyName . '</li>';
                 }
             }
             $content = '<div class="sortableListContainer first">
       <h2>Enabled fields</h2>
       <ul class="connectedSortable" id="enabledFields">
       ' . $enabledLi . '
       </ul>
       </div>
       <div class="sortableListContainer last">
       <h2>Avaliable fields</h2>
       <ul class="connectedSortable" id="avaliableFields">
       ' . $avaliableLi . '
       </ul>
       </div>';
             $this->setJsonMsg(array('msg' => 'ok', 'content' => $content));
             break;
         default:
             $this->setJsonMsg(array('msg' => 'Unknown action'), self::ERROR);
             break;
     }
 }