/**
  * Displays frontend logs list
  *
  * @return void
  */
 private function show_template_view()
 {
     onapp_debug(__CLASS__ . ' :: ' . __FUNCTION__);
     $page = onapp_get_arg('page');
     if (!$page) {
         $page = 1;
     }
     if (count(scandir(ONAPP_PATH . ONAPP_DS . ONAPP_LOG_DIRECTORY)) < 4) {
         $files_list = NULL;
         $pages_quantity = NULL;
     } else {
         $list = onapp_scan_dir(ONAPP_PATH . ONAPP_DS . ONAPP_LOG_DIRECTORY);
         foreach ($list as $file) {
             if (!preg_match("/^" . ONAPP_DEBUG_FILE_NAME . "|^index/", $file)) {
                 $files_list[substr($file, 6, -4)]['date'] = date('Y-m-d H:i:s', filemtime(ONAPP_PATH . ONAPP_DS . ONAPP_LOG_DIRECTORY . ONAPP_DS . $file));
                 $files_list[substr($file, 6, -4)]['size'] = filesize(ONAPP_PATH . ONAPP_DS . ONAPP_LOG_DIRECTORY . ONAPP_DS . $file);
             }
         }
         if (isset($files_list) && count($files_list) > 1) {
             arsort($files_list);
         }
         $items_per_page = 15;
         $j = 0;
         $i = 1;
         if (isset($files_list) && is_array($files_list)) {
             foreach ($files_list as $key => $value) {
                 $files_list_array[$i][$key] = $value;
                 $j++;
                 if ($j > $items_per_page) {
                     $j = 0;
                     $i++;
                 }
             }
             $pages_quantity = count($files_list_array);
             $files_list = $files_list_array[$page];
         } else {
             $pages_quantity = NULL;
             $files_list = NULL;
         }
     }
     $params = array('alias' => 'error_logs', 'page' => $page, 'pages_quantity' => $pages_quantity, 'files_list' => $files_list, 'title' => onapp_string('ERROR_LOGS'), 'info_title' => onapp_string('ERROR_LOGS'), 'info_body' => onapp_string('ERROR_LOGS_INFO'));
     onapp_show_template('errorLogs_view', $params);
 }
 /**
  * Displays mail template edit page
  *
  * @return void
  */
 public function show_template_create()
 {
     onapp_debug(__METHOD__);
     $events_list = onapp_scan_dir(ONAPP_PATH . ONAPP_DS . 'events' . ONAPP_DS);
     sort($events_list, SORT_STRING);
     $params = array('classes_fields' => $this->get_classes_fields(), 'events_list' => $events_list, 'title' => onapp_string('СREATE_EMAIL_TEMPLATE'), 'info_title' => onapp_string('CREATE_EMAIL_TEMPLATE'), 'info_body' => onapp_string('CREATE_EMAIL_TEMPLATE_INFO'));
     onapp_show_template('emailTemplates_create', $params);
 }
/**
 * 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 error logs files
 *
 * @return void
 */
function onapp_rotate_error_log()
{
    onapp_debug(__METHOD__);
    $log_directory = ONAPP_PATH . ONAPP_DS . ONAPP_LOG_DIRECTORY;
    if (count(scandir($log_directory)) < 4) {
        onapp_debug('Nothing to rotate');
        return;
    }
    $list = onapp_scan_dir($log_directory);
    foreach ($list as $file) {
        $file_path = $log_directory . ONAPP_DS . $file;
        if (filemtime($file_path) < time() - 24 * 60 * 60 * ONAPP_LOG_ROTATION_DAYS) {
            chmod($file_path, 0666);
            unlink($file_path);
        }
    }
}