+// URI type enum
+/** Relative path. */
+define('PKWK_URI_RELATIVE', 0);
+/** Root relative URI. */
+define('PKWK_URI_ROOT', 1);
+/** Absolute URI. */
+define('PKWK_URI_ABSOLUTE', 2);
+
+function pkwk_log($message)
+{
+ $log_filepath = 'log/error.log.php';
+ static $dateTimeExists;
+ if (!isset($dateTimeExists)) {
+ $dateTimeExists = class_exists('DateTime');
+ error_log("<?php\n", 3, $log_filepath);
+ }
+ if ($dateTimeExists) {
+ // for PHP5.2+
+ $d = \DateTime::createFromFormat('U.u', sprintf('%6F', microtime(true)));
+ $timestamp = substr($d->format('Y-m-d H:i:s.u'), 0, 23);
+ } else {
+ $timestamp = date('Y-m-d H:i:s');
+ }
+ error_log($timestamp . ' ' . $message . "\n", 3, $log_filepath);
+}
+
+/*
+ * Get LTSV safe string - Remove tab and newline chars.
+ *
+ * @param $s target string
+ */
+function get_ltsv_value($s) {
+ if (!$s) {
+ return '';
+ }
+ return preg_replace('#[\t\r\n]#', '', $s);
+}
+
+/**
+ * Write update_log on updating contents.
+ *
+ * @param $page page name
+ * @param $diff_content diff expression
+ */
+function pkwk_log_updates($page, $diff_content) {
+ global $auth_user, $logging_updates, $logging_updates_log_dir;
+ $log_dir = $logging_updates_log_dir;
+ $timestamp = time();
+ $ymd = gmdate('Ymd', $timestamp);
+ $difflog_file = $log_dir . '/diff.' . $ymd . '.log';
+ $ltsv_file = $log_dir . '/update.' . $ymd . '.log';
+ $d = array(
+ 'time' => gmdate('Y-m-d H:i:s', $timestamp),
+ 'uri' => $_SERVER['REQUEST_URI'],
+ 'method' => $_SERVER['REQUEST_METHOD'],
+ 'remote_addr' => $_SERVER['REMOTE_ADDR'],
+ 'user_agent' => $_SERVER['HTTP_USER_AGENT'],
+ 'page' => $page,
+ 'user' => $auth_user,
+ 'diff' => $diff_content
+ );
+ if (file_exists($log_dir) && defined('JSON_UNESCAPED_UNICODE')) {
+ // require: PHP5.4+
+ $line = json_encode($d, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
+ file_put_contents($difflog_file, $line, FILE_APPEND | LOCK_EX);
+ $keys = array('time', 'uri', 'method', 'remote_addr', 'user_agent',
+ 'page', 'user');
+ $ar2 = array();
+ foreach ($keys as $k) {
+ $ar2[] = $k . ':' . get_ltsv_value($d[$k]);
+ }
+ $ltsv = join($ar2, "\t") . "\n";
+ file_put_contents($ltsv_file, $ltsv, FILE_APPEND | LOCK_EX);
+ }
+}
+
+/**
+ * ctype_digit that supports PHP4+.
+ *
+ * PHP official document says PHP4 has ctype_digit() function.
+ * But sometimes it doen't exists on PHP 4.1.
+ */
+function pkwk_ctype_digit($s) {
+ static $ctype_digit_exists;
+ if (!isset($ctype_digit_exists)) {
+ $ctype_digit_exists = function_exists('ctype_digit');
+ }
+ if ($ctype_digit_exists) {
+ return ctype_digit($s);
+ }
+ return preg_match('/^[0-9]+$/', $s) ? true : false;
+}
+