public function log_json($level, $msg, $json) { if ($level > $this->loglevel) { return; } $this->log($level, $msg, Feediron_Json::format($json)); }
public static function format($json) { $result = ''; $pos = 0; $strLen = strlen($json); $indentStr = ' '; $newLine = "\n"; $prevChar = ''; $outOfQuotes = true; $currentline = 0; $possible_errors = array(',]' => 'Additional comma before ] (%s)', '""' => 'Missing seperator between after " (%s)', ',}' => 'Additional comma before } (%s)', ',:' => 'Comma before :(%s)', ']:' => '] before :(%s)', '}:' => '} before :(%s)', '[:' => '[ before :(%s)', '{:' => '{ before :(%s)'); for ($i = 0; $i <= $strLen; $i++) { // Grab the next character in the string. $char = substr($json, $i, 1); if ($char == $newLine) { $currentline++; continue; } if ($char == ' ' && $outOfQuotes) { continue; } if (array_key_exists($prevChar . $char, $possible_errors)) { self::$json_error = sprintf($possible_errors[$prevChar . $char] . ' in line %s', substr($result, self::strrpos_count($result, $newLine, 3)), $currentline); } // Are we inside a quoted string? if ($char == '"' && $prevChar != '\\') { $outOfQuotes = !$outOfQuotes; // If this character is the end of an element, // output a new line and indent the next line. } else { if (($char == '}' || $char == ']') && $outOfQuotes) { $result .= $newLine; $pos--; for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } } // Add the character to the result string. $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { $result .= $newLine; if ($char == '{' || $char == '[') { $pos++; } for ($j = 0; $j < $pos; $j++) { $result .= $indentStr; } } else { if ($char == ':' && $outOfQuotes) { $result .= ' '; } } $prevChar = $char; } return $result; }
function test() { Feediron_Logger::get()->set_log_level($_POST['verbose'] ? Feediron_Logger::LOG_VERBOSE : Feediron_Logger::LOG_TEST); $test_url = $_POST['test_url']; Feediron_Logger::get()->log(Feediron_Logger::LOG_TTRSS, "Test url: {$test_url}"); if (isset($_POST['test_conf']) && trim($_POST['test_conf']) != '') { $json_conf = $_POST['test_conf']; $json_reply = array(); Feediron_Json::format($json_conf); header('Content-Type: application/json'); if (is_null(json_decode($json_conf))) { $json_reply['success'] = false; $json_reply['errormessage'] = __('Invalid JSON! ') . json_last_error_msg(); $json_reply['json_error'] = Feediron_Json::get_error(); echo json_encode($json_reply); return false; } $config = $this->getConfigSection($test_url); Feediron_Logger::get()->log_object(Feediron_Logger::LOG_TEST, "config found: ", $config); $newconfig = json_decode($_POST['test_conf'], true); Feediron_Logger::get()->log_object(Feediron_Logger::LOG_TEST, "config posted: ", $newconfig); Feediron_Logger::get()->log_object(Feediron_Logger::LOG_TEST, "config diff", $this->arrayRecursiveDiff($config, $newconfig)); if (count($this->arrayRecursiveDiff($newconfig, $config)) != 0) { Feediron_Logger::get()->log(Feediron_Logger::LOG_TEST, "Save test config"); $this->host->set($this, 'test_conf', Feediron_Json::format(json_encode($config))); } $config = json_decode($_POST['test_conf'], true); } else { $config = $this->getConfigSection($test_url); } Feediron_Logger::get()->log_object(Feediron_Logger::LOG_TEST, "Using config", $config); $test_url = $this->reformatUrl($test_url, $config); Feediron_Logger::get()->log(Feediron_Logger::LOG_TTRSS, "Url after reformat: {$test_url}"); header('Content-Type: application/json'); $reply = array(); if ($config === FALSE) { $reply['success'] = false; $reply['errormessage'] = "URL did not match"; $reply['log'] = Feediron_Logger::get()->get_testlog(); echo json_encode($reply); return false; } else { $reply['success'] = true; $reply['url'] = $test_url; $reply['content'] = $this->getNewContent($test_url, $config); $reply['config'] = Feediron_Json::format(json_encode($config)); if ($reply['config'] == null) { $reply['config'] = $_POST['test_conf']; } $reply['log'] = Feediron_Logger::get()->get_testlog(); echo json_encode($reply); } }