* @author Bart Visscher * @author duritong * @author Georg Ehrke * @author Joas Schilling * @author Juan Pablo Villafáñez * @author Lukas Reschke * @author Michael Gapczynski * @author Morris Jobke * @author Phiber2000 * @author Robin Appelman * @author Roeland Jago Douma * @author Roger Szabo * @author Thomas Müller * @author Thomas Pulzer * @author Vincent Petry * * @license AGPL-3.0 * * This code is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License, version 3, * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License, version 3, * along with this program. If not, see * */ namespace OC\Log; /** * logging utilities * * Log is saved at data/nextcloud.log (on default) */ class File { static protected $logFile; /** * Init class data */ public static function init() { $systemConfig = \OC::$server->getSystemConfig(); $defaultLogFile = $systemConfig->getValue("datadirectory", \OC::$SERVERROOT.'/data').'/nextcloud.log'; self::$logFile = $systemConfig->getValue("logfile", $defaultLogFile); /** * Fall back to default log file if specified logfile does not exist * and can not be created. */ if (!file_exists(self::$logFile)) { if(!is_writable(dirname(self::$logFile))) { self::$logFile = $defaultLogFile; } else { if(!touch(self::$logFile)) { self::$logFile = $defaultLogFile; } } } } /** * write a message in the log * @param string $app * @param string $message * @param int $level */ public static function write($app, $message, $level) { $config = \OC::$server->getSystemConfig(); // default to ISO8601 $format = $config->getValue('logdateformat', \DateTime::ATOM); $logTimeZone = $config->getValue('logtimezone', 'UTC'); try { $timezone = new \DateTimeZone($logTimeZone); } catch (\Exception $e) { $timezone = new \DateTimeZone('UTC'); } $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", "")); if ($time === false) { $time = new \DateTime(null, $timezone); } else { // apply timezone if $time is created from UNIX timestamp $time->setTimezone($timezone); } $request = \OC::$server->getRequest(); $reqId = $request->getId(); $remoteAddr = $request->getRemoteAddress(); // remove username/passwords from URLs before writing the to the log file $time = $time->format($format); $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--'; $method = is_string($request->getMethod()) ? $request->getMethod() : '--'; if($config->getValue('installed', false)) { $user = \OC_User::getUser() ? \OC_User::getUser() : '--'; } else { $user = '--'; } $userAgent = $request->getHeader('User-Agent'); if ($userAgent === '') { $userAgent = '--'; } $version = $config->getValue('version', ''); $entry = compact( 'reqId', 'level', 'time', 'remoteAddr', 'user', 'app', 'method', 'url', 'message', 'userAgent', 'version' ); // PHP's json_encode only accept proper UTF-8 strings, loop over all // elements to ensure that they are properly UTF-8 compliant or convert // them manually. foreach($entry as $key => $value) { if(is_string($value)) { $testEncode = json_encode($value); if($testEncode === false) { $entry[$key] = utf8_encode($value); } } } $entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR); $handle = @fopen(self::$logFile, 'a'); if ((fileperms(self::$logFile) & 0777) != 0640) { @chmod(self::$logFile, 0640); } if ($handle) { fwrite($handle, $entry."\n"); fclose($handle); } else { // Fall back to error_log error_log($entry); } if (php_sapi_name() === 'cli-server') { error_log($message, 4); } } /** * get entries from the log in reverse chronological order * @param int $limit * @param int $offset * @return array */ public static function getEntries($limit=50, $offset=0) { self::init(); $minLevel = \OC::$server->getSystemConfig()->getValue("loglevel", \OCP\Util::WARN); $entries = array(); $handle = @fopen(self::$logFile, 'rb'); if ($handle) { fseek($handle, 0, SEEK_END); $pos = ftell($handle); $line = ''; $entriesCount = 0; $lines = 0; // Loop through each character of the file looking for new lines while ($pos >= 0 && ($limit === null ||$entriesCount < $limit)) { fseek($handle, $pos); $ch = fgetc($handle); if ($ch == "\n" || $pos == 0) { if ($line != '') { // Add the first character if at the start of the file, // because it doesn't hit the else in the loop if ($pos == 0) { $line = $ch.$line; } $entry = json_decode($line); // Add the line as an entry if it is passed the offset and is equal or above the log level if ($entry->level >= $minLevel) { $lines++; if ($lines > $offset) { $entries[] = $entry; $entriesCount++; } } $line = ''; } } else { $line = $ch.$line; } $pos--; } fclose($handle); } return $entries; } /** * @return string */ public static function getLogFilePath() { return self::$logFile; } } : italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
import { jQuery } from "../core.js";
import { rsingleTag } from "./var/rsingleTag.js";
import { buildFragment } from "../manipulation/buildFragment.js";
import { isObviousHtml } from "./isObviousHtml.js";

// Argument "data" should be string of html or a TrustedHTML wrapper of obvious HTML
// context (optional): If specified, the fragment will be created in this context,
// defaults to document
// keepScripts (optional): If true, will include scripts passed in the html string
jQuery.parseHTML = function( data, context, keepScripts ) {
	if ( typeof data !== "string" && !isObviousHtml( data + "" ) ) {
		return [];
	}
	if ( typeof context === "boolean" ) {
		keepScripts = context;
		context = false;
	}

	var parsed, scripts;

	if ( !context ) {

		// Stop scripts or inline event handlers from being executed immediately
		// by using DOMParser
		context = ( new window.DOMParser() )
			.parseFromString( "", "text/html" );
	}

	parsed = rsingleTag.exec( data );
	scripts = !keepScripts && [];

	// Single tag
	if ( parsed ) {
		return [ context.createElement( parsed[ 1 ] ) ];
	}

	parsed = buildFragment( [ data ], context, scripts );

	if ( scripts && scripts.length ) {
		jQuery( scripts ).remove();
	}

	return jQuery.merge( [], parsed.childNodes );
};