/**
 * writes a new log entry in the specified category.
 *
 * @global Clgs_DB $clgs_db
 * @global Clgs_Last_Log $clgs_last_log
 *
 * @param string $category a registered category name
 * @param string $text the logged message, can contain HTML same as comments
 * (filtered by wp_kses_data)
 * @param int $severity one of defined severity levels (see above); if missing
 * defaults to CLGS_NOCATEGORY
 * @param mixed $user user id, slug or WP user object are aceptable; if missing
 * defaults to current user (or a placeholder if none is logged in)
 * @param int $blog_id blog id; if missing defaults to current blog
 * @param mixed $date a UNIX timestamp or a string recognized by strtotime();
 * if missing defaults to current time
 *
 * @return boolean false if entering the log failed.
 */
function clgs_log($category, $text, $severity = null, $user = null, $blog_id = null, $date = null)
{
    global $clgs_db, $clgs_last_log;
    // category must pass validation
    if (is_null($date)) {
        $date = time();
    }
    $rules = array('category' => array('sanitize' => 'string', 'validate' => 'registered'), 'text' => array('sanitize' => 'kses_string', 'validate' => 'length'), 'date' => array('sanitize' => 'time', 'validate' => 'sanitation'));
    $must = clgs_evaluate(compact('category', 'text', 'date'), $rules, 'block');
    if (!$must) {
        return false;
    }
    // all others have a default
    $rules = array('user' => array('sanitize_function' => 'clgs_to_user', 'validate' => 'exists', 'default' => is_user_logged_in() ? wp_get_current_user()->display_name : ' — '), 'blog_id' => array('sanitize' => 'int', 'validate' => 'positive', 'default' => get_current_blog_id()), 'severity' => array('sanitize' => 'int', 'validate' => 'severity', 'default' => CLGS_NOSEVERITY));
    $sane = clgs_evaluate(compact('date', 'user', 'blog_id', 'severity'), $rules);
    // get blog name
    if (clgs_is_network_mode()) {
        switch_to_blog($sane['blog_id']);
        $blog_name = get_bloginfo('name');
        restore_current_blog();
    } else {
        $blog_name = get_bloginfo('name');
    }
    $data = array('category' => $must['category'], 'blog_id' => $sane['blog_id'], 'blog_name' => $blog_name, 'date' => $must['date'], 'user_name' => $sane['user'], 'text' => $must['text'], 'severity' => $sane['severity']);
    if ($clgs_last_log->compare($data)) {
        $clgs_last_log->write();
        $first = $clgs_last_log->data['date'];
        $data['text'] = '(' . $clgs_last_log->count . '× ' . __('since ', 'custom-logging-service') . '<span data-date="' . $first . '"></span>):<br/>' . $data['text'];
        $ok = (bool) $clgs_db->update_entry($clgs_last_log->entry_id, $data);
    } else {
        $entry_id = $clgs_db->insert_entry($data);
        $ok = (bool) $entry_id;
        if ($ok) {
            $clgs_last_log->set($data, $entry_id);
        }
    }
    return $ok;
}
Esempio n. 2
0
/**
 * render log page
 *
 * @global string $pagenow
 * @global Clgs_DB $clgs_db
 *
 * @return void
 */
function clgs_manager_page()
{
    global $pagenow, $clgs_db;
    extract(clgs_get_settings());
    /*** Input sanitation ***/
    $attrs = clgs_evaluate($_REQUEST, array('min_severity' => array('sanitize' => 'int', 'validate' => 'severity', 'default' => $def_severity_filter), 'seen' => array('sanitize' => 'bool', 'validate' => 'exists', 'default' => false), 'category' => array('sanitize' => 'string', 'validate' => 'registered'), 'entry_id' => array('sanitize' => 'int', 'validate' => 'positive'), 'orderby' => array('sanitize' => 'string', 'validate_array' => ['date', 'category', 'user_name', 'blog_name', 'severity'], 'default' => 'date'), 'order' => array('sanitize' => 'toupper_string', 'validate_array' => ['ASC', 'DESC'], 'default' => 'ASC')));
    extract($attrs);
    //var_dump( $attrs );
    /*** Render ***/
    $table = new Clgs_Manager_Table();
    $pageurl = add_query_arg('page', CLGS_LOG_PAGE, $pagenow);
    $pageurl = add_query_arg(compact('seen', 'min_severity'), $pageurl);
    // Show a single entry or a list?
    if (isset($entry_id)) {
        unset($attrs['category']);
    }
    ?>
    <div class="wrap">
        <h1><?php 
    _e('Application logs', 'custom-logging-service');
    ?>
</h1>
<?php 
    if (isset($attrs['category'])) {
        // single log category
        $log = $clgs_db->get_log($attrs['category']);
        $actionurl = wp_nonce_url(add_query_arg('category', urlencode($attrs['category']), $pageurl), 'bulk-category');
        ?>
        <h2><?php 
        echo __('Log category', 'custom-logging-service') . ': ' . $log->category;
        ?>
</h2>
        <p><?php 
        echo $log->description;
        ?>
</p>
        <p>
            <a href="<?php 
        echo $actionurl . '&action=mark-category';
        ?>
" title="<?php 
        _e("Mark whole category as read", 'custom-logging-service');
        ?>
" ><?php 
        _e('Mark whole category as read', 'custom-logging-service');
        ?>
</a> |
            <a href="<?php 
        echo $actionurl . '&action=clear';
        ?>
" title="<?php 
        _e("Remove all log entries from this category", 'custom-logging-service');
        ?>
"><?php 
        _e('Clear', 'custom-logging-service');
        ?>
</a> |
            <a href="<?php 
        echo $actionurl . '&action=unregister';
        ?>
" title="<?php 
        _e("Delete this log category permanently (with all entries)", 'custom-logging-service');
        ?>
" ><?php 
        _e('Delete', 'custom-logging-service');
        ?>
</a> |
            <a href="<?php 
        echo $pageurl;
        ?>
"><?php 
        _e('Show all categories', 'custom-logging-service');
        ?>
</a>
        </p>
        <form method="get">
            <input type="hidden" name="page" value="<?php 
        echo CLGS_LOG_PAGE;
        ?>
" />
            <input type="hidden" name="action" value="view" />
            <input type="hidden" name="category" value="<?php 
        echo $log->category;
        ?>
" />
<?php 
    } else {
        ?>
        <h2><?php 
        _e("New log entries from all categories", 'custom-logging-service');
        ?>
</h2>
        <form method="get">
            <input type="hidden" name="page" value="<?php 
        echo CLGS_LOG_PAGE;
        ?>
" />
            <input type="hidden" name="action" value="view" />
<?php 
    }
    $table->set_attributes($attrs);
    $table->prepare_items();
    $table->display();
    ?>
        </form>
<?php 
    if (!isset($attrs['category'])) {
        // include a log category overview
        ?>
        <h2><?php 
        _e('Log categories', 'custom-logging-service');
        ?>
</h2>
        <div id="clgs-log-list"><table class="wp-list-table widefat fixed striped">
<?php 
        foreach ($clgs_db->get_logs() as $id => $log) {
            $caturl = add_query_arg('category', urlencode($log->category), $pageurl);
            $actionurl = wp_nonce_url($caturl, 'bulk-category');
            ?>
            <tr class="<?php 
            echo $id % 2 === 0 ? 'alternate' : '';
            ?>
">
            <td class="column-primary">
                <span><a href="<?php 
            echo $caturl;
            ?>
"><?php 
            echo $log->category;
            ?>
</a></span>
                <div class="row-actions visible">
                <a href="<?php 
            echo $actionurl . '&action=mark-category';
            ?>
" title="<?php 
            _e("Mark whole category as read", 'custom-logging-service');
            ?>
" ><?php 
            _e('Mark as read', 'custom-logging-service');
            ?>
</a> |
                <a href="<?php 
            echo $actionurl . '&action=clear';
            ?>
" title="<?php 
            _e("Remove all log entries from this category", 'custom-logging-service');
            ?>
"><?php 
            _e('Clear', 'custom-logging-service');
            ?>
</a> |
                <a href="<?php 
            echo $actionurl . '&action=unregister';
            ?>
" title="<?php 
            _e("Delete this log category permanently (with all entries)", 'custom-logging-service');
            ?>
" ><?php 
            _e('Delete', 'custom-logging-service');
            ?>
</a>
                </div>
                <button class="toggle-row" type="button"><span class="screen-reader-text"><?php 
            _e('Show more details');
            ?>
</span></button>
            </td>
            <td><?php 
            echo esc_attr($log->description);
            ?>
</td>
<?php 
        }
        ?>
        </table></div>
<?php 
    }
    ?>
    </div>
<?php 
}
Esempio n. 3
0
/**
 * sanitation function for settings page
 *
 * @global array $clgs_settings_structure
 *
 * @return array sane settings, unaltered in case of an error
 */
function clgs_sanitize($input)
{
    global $clgs_settings_structure;
    $original = clgs_get_settings();
    $result = clgs_evaluate($input, $clgs_settings_structure, 'hold');
    if ('string' == gettype($result)) {
        $offending = __($clgs_settings_structure[$result]['desc'], 'custom-logging-service');
        $message = sprintf(__('The setting %s was invalid, nothing saved.', 'custom-logging-service'), '<em>"' . $offending . '"</em>');
        add_settings_error(CLGS_SETTINGS, 'clgs_error', $message);
        return $original;
    }
    return array_merge($original, $result);
}