public function run()
 {
     onapp_debug(__METHOD__);
     global $_ALIASES, $_SCREEN_IDS;
     $route = onapp_get_arg('route');
     onapp_debug('route => ' . $route);
     if (array_key_exists($route, $_SCREEN_IDS)) {
         $method_name = $_SCREEN_IDS[$route]['method'];
         $class_name = $_SCREEN_IDS[$route]['class'];
     } else {
         if (is_null($route)) {
             onapp_debug('$route is null!, Redirecting :' . ONAPP_BASE_URL . '/' . $_ALIASES[ONAPP_DEFAULT_ALIAS]);
             onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES[ONAPP_DEFAULT_ALIAS]);
         } else {
             onapp_debug('There is no such page, Redirecting Base Url');
             trigger_error('There is no such page => ' . ONAPP_BASE_URL . $route);
             $_SESSION['message'] = 'THERE_IS_NO_SUCH_PAGE';
             onapp_redirect(ONAPP_BASE_URL);
         }
     }
     $file_path = ONAPP_PATH . ONAPP_DS . 'controllers' . ONAPP_DS . ONAPP_CONTROLLERS . ONAPP_DS . strtolower('c_' . $class_name . '') . '.php';
     if (file_exists($file_path)) {
         require_once "{$file_path}";
     } else {
         onapp_die("Could not find file {$file_path}");
     }
     onapp_debug("ONAPP_Controller->run: args => " . print_r(array('route' => $route, 'file' => $file_path, 'class' => $class_name, 'method' => $method_name), true));
     $new_class = new $class_name();
     $new_class->{$method_name}();
 }
 /**
  * Checks permission for displaying MENU item
  *
  * @return boolean if has permission to see menu item
  */
 static function access()
 {
     onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
     $return = onapp_has_permission(array('roles'));
     onapp_debug('return => ' . $return);
     return $return;
 }
 /**
  * Checks permission for displaying MENU item
  *
  * @return boolean if has permission to see menu item
  */
 static function access()
 {
     onapp_debug(__METHOD__);
     $return = onapp_has_permission(array('roles'));
     onapp_debug('return => ' . $return);
     return $return;
 }
 /**
  * Calculates backups' quantity and sizes
  *
  * @param object $vm_backup_obj Virtual Machine Backup object
  * @return array Virtual Machine backups' quantity and sizes
  */
 private function calculateBackups($vm_backup_obj)
 {
     onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
     $backups_quantity = 0;
     $backups_total_size = 0;
     if ($vm_backup_obj && is_array($vm_backup_obj)) {
         foreach ($vm_backup_obj as $value) {
             $backups_quantity++;
             $backups_total_size += $value->_backup_size;
         }
         $backups_total_size = round($backups_total_size / 1024);
     } else {
         if (count($vm_backup_obj) == 1 && !is_array($vm_backup_obj) && $vm_backup_obj->_id) {
             $backups_quantity = 1;
             $backups_total_size = round($vm_backup_obj->_backup_size / 1024);
         }
     }
     return $size_and_quantity = array('size' => $backups_total_size, 'quantity' => $backups_quantity);
 }
 /**
  * Checks permission for displaying MENU item
  *
  * @return boolean if has permission to see menu item
  */
 static function access()
 {
     onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
     $return = onapp_has_permission(array('log_items', 'log_items.list', 'log_items.list.own'));
     onapp_debug('return => ' . $return);
     return $return;
 }
 /**
  * Edits user group
  *
  * @global array $_ALIASES menu page aliases
  * @param integer user group id
  * @return void
  */
 private function group_edit($id)
 {
     global $_ALIASES;
     onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
     onapp_debug('id => ' . $id);
     onapp_permission(array('groups', 'groups.update'));
     $group = onapp_get_arg('group');
     if (is_null($group)) {
         $this->show_template_group_edit($id);
     } else {
         $onapp = $this->get_factory();
         $group_obj = $onapp->factory('UserGroup', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
         foreach ($group as $key => $value) {
             $group_obj->{$key} = $value;
         }
         $group_obj->_id = $id;
         //TODO delete this when fixed Ticket #2511
         $group_obj->_tagRoot = 'pack';
         //*****************************
         $group_obj->save();
         onapp_debug('group_obj =>' . print_r($group_obj, true));
         if (is_null($group_obj->error)) {
             onapp_event_exec('group_edit', array($group_obj->_obj));
             $_SESSION['message'] = 'GROUP_HAS_BEEN_UPDATED_SUCCESSFULLY';
             onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['users_and_groups'] . '?action=groups');
         } else {
             onapp_event_exec('group_edit_failed', array($group_obj->_obj));
             trigger_error(print_r($group_obj->error, true));
             $this->show_template_groups($group_obj->error);
         }
     }
 }
 /**
  * Loads OnApp user profile into the session
  *
  * @param mixed $onapp instance of ONAPP_Factory class
  * @return void
  */
 private function _load_profile($onapp)
 {
     onapp_debug(__METHOD__);
     $profile = $onapp->factory('Profile');
     $profile_obj = $profile->load();
     foreach ($profile_obj->_roles as $role) {
         foreach ($role->_permissions as $permission) {
             $_SESSION['permissions'][] = $permission->_identifier;
         }
     }
     //http://e-mats.org/2008/07/fatal-error-exception-thrown-without-a-stack-frame-in-unknown-on-line-0/
     unset($profile_obj->_used_ip_addresses);
     $_SESSION['profile_obj'] = $profile_obj;
     onapp_debug('SESSION[permissions]' . print_r($_SESSION['permissions'], true));
 }
/**
 * Event manager function
 * 
 * @param string $event_name Event name
 * @param mixed $objects_array Objects array
 * @param string $url
 */
function onapp_event_exec($event_name, $objects_array = NULL, $url = NULL)
{
    //print('<pre>'); print_r($_SESSION['profile_obj']); die();
    onapp_debug(__METHOD__);
    $event_directory = ONAPP_PATH . ONAPP_DS . 'events' . ONAPP_DS . $event_name . ONAPP_DS;
    if (!(count(scandir($event_directory . 'script')) == 2)) {
    }
    if (!(count(scandir($event_directory . 'exec')) == 2)) {
    }
    if (!(count(scandir($event_directory . 'mail')) == 2)) {
        $mail_directory = $event_directory . 'mail' . ONAPP_DS;
        $mails = onapp_scan_dir($mail_directory);
        //print('<pre>');print_r($mails); die();
        $content = '';
        $mail_count = 1;
        $mails_array = array();
        foreach ($mails as $mail) {
            $handle = @fopen($mail_directory . $mail, "r");
            if ($handle) {
                $mails_array[$mail_count]['message'] = '';
                while (($buffer = fgets($handle, 4096)) !== false) {
                    if (preg_match("/^:from:/", $buffer)) {
                        $mails_array[$mail_count]['from'] = trim(str_replace(':from:', '', $buffer));
                    } elseif (preg_match("/^:from_name:/", $buffer)) {
                        $mails_array[$mail_count]['from_name'] = trim(str_replace(':from_name:', '', $buffer));
                    } elseif (preg_match("/^:to:/", $buffer)) {
                        $mails_array[$mail_count]['to'] = trim(str_replace(':to:', '', $buffer));
                    } elseif (preg_match("/^:subject:/", $buffer)) {
                        $mails_array[$mail_count]['subject'] = trim(str_replace(':subject:', '', $buffer));
                    } elseif (!preg_match('/^:/', $buffer)) {
                        $mails_array[$mail_count]['message'] .= $buffer;
                    } elseif (preg_match('/^:copy/', $buffer)) {
                        $mails_array[$mail_count]['copy'] = trim(str_replace(':copy:', '', $buffer));
                    }
                }
            } else {
                die('Unable to open file ' . $mail_directory . $mail);
            }
            fclose($handle);
            $mail_count++;
        }
        // print('<pre>');print_r($mails_array ); die();
        require_once ONAPP_PATH . ONAPP_DS . 'libs' . ONAPP_DS . 'smarty' . ONAPP_DS . 'Smarty.class.php';
        $smarty = new Smarty();
        if ($objects_array) {
            foreach ($objects_array as $object) {
                // print('<pre>');print_r($object); die();
                if ($object) {
                    // print('<pre>');print_r($object); die();
                    $name = str_replace('OnApp_', '', $object->getClassName());
                    // echo $name; die();
                    foreach ($object->getClassFields() as $field => $value) {
                        $field = '_' . $field;
                        $smarty->assign($name . $field, $object->{$field});
                        // $sma[$name][$name . $field] =$object->$field;
                    }
                }
            }
        }
        // print('<pre>');print_r($sma); die();
        $profile = $_SESSION['profile_obj'];
        $smarty->assign('responsible_name', $profile->_first_name . ' ' . $profile->_last_name);
        $smarty->assign('responsible_email', $profile->_email);
        //echo $smarty->fetch('string:'. $email['message']); die(); //print(***************************
        foreach ($mails_array as $email) {
            try {
                $to_f = $smarty->fetch('string:' . $email['to']);
                $from_f = $smarty->fetch('string:' . $email['from']);
                $subject_f = $smarty->fetch('string:' . $email['subject']);
                $message_f = $smarty->fetch('string:' . $email['message']);
                $from_name_f = $smarty->fetch('string:' . $email['from_name']);
                $copy_f = $smarty->fetch('string:' . $email['copy']);
            } catch (Exception $e) {
                trigger_error('Smarty Syntax Error  in Email Template <br />' . $e, E_USER_ERROR);
                break;
            }
            $sent = onapp_send_email($to_f, $from_f, $subject_f, $message_f, $from_name_f, $copy_f);
            if (!$sent) {
                trigger_error('Failed to send email to' . $email['to']);
            }
        }
    }
}
/**
 * Rotates debug logs files
 *
 * @return void
 */
function onapp_rotate_debug_log()
{
    onapp_debug(__METHOD__);
    $file = ONAPP_PATH . ONAPP_DS . ONAPP_LOG_DIRECTORY . ONAPP_DS . ONAPP_DEBUG_FILE_NAME;
    if (!file_exists($file)) {
        onapp_debug('Debug file doesn\'t exists');
        return;
    }
    $size = filesize($file) / 1024 / 1024;
    if ($size >= ONAPP_LOG_ROTATION_SIZE) {
        for ($i = 6; $i > 0; $i--) {
            $old_name = $file . '.' . $i;
            $new_name = $file . '.' . ($i + 1);
            if (file_exists($old_name) && $i != 6) {
                rename($old_name, $new_name);
            }
        }
        $renamed = rename($file, $file . '.1');
    }
}
 /**
  * Writes log settings changes to onapp frontend configuration file
  *
  * @param array onapp frontend configurations array
  * @param string path to onapp frontend configuration file
  * @return boolean true on success
  */
 private function write_config($config_array, $path)
 {
     onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
     onapp_debug('params : $config_array => ' . print_r($config_array, true) . '$path => ' . $path);
     $content = '';
     foreach ($config_array as $key => $value) {
         $content .= "{$key}={$value}" . "\n";
     }
     onapp_debug('New config file content => ' . $content);
     if (!($handle = fopen($path, 'w'))) {
         $error = 'CONFIG_FILE_NOT_WRITABLE';
     } else {
         if (!fwrite($handle, $content)) {
             $error = 'CONFIG_FILE_NOT_WRITABLE';
         } else {
             fclose($handle);
         }
     }
     if (isset($error)) {
         return $error;
     }
 }
 /**
  * Edits hypervisor's params
  *
  * @global array $_ALIASES menu page aliases
  * @param integer hypervisor id
  * @return void
  */
 private function edit($id)
 {
     global $_ALIASES;
     onapp_debug(__METHOD__);
     onapp_debug('id => ' . $id);
     $hypervisor = onapp_get_arg('hypervisor');
     onapp_permission(array('hypervisors.update', 'hypervisors'));
     if (is_null($hypervisor)) {
         $this->show_template_edit($id);
     } else {
         $onapp = $this->get_factory();
         $id = onapp_get_arg('id');
         $hypervisor_obj = $onapp->factory('Hypervisor', ONAPP_WRAPPER_LOG_REPORT_ENABLE);
         foreach ($hypervisor as $key => $value) {
             $hypervisor_obj->{$key} = $value;
         }
         $hypervisor_obj->_id = $id;
         $hypervisor_obj->save();
         onapp_debug('hypervisor_obj =>' . print_r($hypervisor_obj, true));
         if (is_null($hypervisor_obj->error)) {
             onapp_event_exec('hypervisor_edit', array($hypervisor_obj->_obj));
             $_SESSION['message'] = 'HYPERVISOR_HAS_BEEN_UPDATED_SUCCESSFULLY';
             onapp_redirect(ONAPP_BASE_URL . '/' . $_ALIASES['hypervisors']);
         } else {
             onapp_event_exec('hypervisor_edit_failed', array($hypervisor_obj->_obj));
             trigger_error(print_r($hypervisor_obj->error, true));
             $this->show_template_view($hypervisor_obj->error);
         }
     }
 }