Exemple #1
0
function log_user($username, $text)
{
    $timestamp = null;
    // Store the UTC date with millisecond precision.
    $t = microtime(true);
    $micro = sprintf("%03d", ($t - floor($t)) * 1000);
    $timestamp = gmdate('Y-m-d H:i:s.', $t) . $micro;
    $file_name = __PATH_LOGS__ . 'log_' . $username . '.json';
    // Retrieve the messages
    $message_list = array();
    if (file_exists($file_name)) {
        // Retrieve the messages
        $str_json = file_get_contents($file_name);
        if (strlen($str_json) > 0) {
            $message_list = json_decode($str_json, true);
        }
    }
    // Prepare the message.
    $message = array();
    $message['timestamp'] = $timestamp;
    $message['username'] = $username;
    $message['text'] = $text;
    // Add the message to the list.
    array_push($message_list, $message);
    //var_dump($message_list);
    // Ensure the list is sorted by their timestamps before being stored.
    usort($message_list, function ($a, $b) {
        return strcmp($a['timestamp'], $b['timestamp']);
    });
    // Save the message list.
    $str_json = json_encode($message_list, 128);
    // 128 is code for JSON_PRETTY_PRINT
    $str_json = indent_json($str_json);
    file_put_contents($file_name, $str_json);
}
Exemple #2
0
function prety_json($obj)
{
    return indent_json(json_encode($obj));
}
Exemple #3
0
/**
 * Formats a raw json string as HTML.
 * @param string $json The json string to process
 */
function format_json($json)
{
    echo '<pre>';
    echo indent_json($json);
    echo '</pre>';
}
                             foreach ($export_path as $e_key => $e_path) {
                                 if (empty($export_path)) {
                                     $export_val = $parse_result->{$e_path};
                                 } else {
                                     $export_val = $export_val->{$e_path};
                                 }
                             }
                             ${$export_name} = $export_val = '';
                         }
                     }
                 }
                 $actions['parse_dump'] = array('data' => $parse_result, 'do' => true);
             }
             break;
         case 'indent_json':
             $indent_json = indent_json($vals['json']);
             $actions['raw_dump'] = array('data' => $indent_json, 'do' => true);
             break;
         case 'indent_xml':
             $indent_xml = indent_xml($vals['xml']);
             $actions['raw_dump'] = array('data' => $indent_xml, 'do' => true);
             break;
         case 'raw_dump':
             $the_raw_result = $vals['data'];
             break;
         case 'parse_dump':
             $the_parse_result = print_r($vals['data'], true);
             break;
     }
 }
 if ($clean === false) {