1
0
mirror of https://github.com/moodle/moodle.git synced 2025-05-13 03:36:09 +02:00

Merge branch 'w13_MDL-44106_m27_requestorig' of git://github.com/skodak/moodle

This commit is contained in:
Marina Glancy 2014-03-25 14:15:19 +08:00
commit dd043de942
14 changed files with 101 additions and 41 deletions

@ -59,6 +59,8 @@ trait buffered_writer {
* @return void
*/
public function write(\core\event\base $event) {
global $PAGE;
if ($this->is_event_ignored($event)) {
return;
}
@ -68,16 +70,10 @@ trait buffered_writer {
// snapshots and custom objects may be garbage collected.
$entry = $event->get_data();
$entry['other'] = serialize($entry['other']);
if (CLI_SCRIPT) {
$entry['origin'] = 'cli';
$entry['ip'] = null;
} else {
$entry['origin'] = 'web';
$entry['ip'] = getremoteaddr();
}
$entry['origin'] = $PAGE->requestorigin;
$entry['ip'] = $PAGE->requestip;
$entry['realuserid'] = \core\session\manager::is_loggedinas() ? $_SESSION['USER']->realuser : null;
$this->buffer[] = $entry;
$this->count++;

@ -0,0 +1,34 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Restore controller hackery.
*
* @package tool_log
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
class logstore_standard_restore extends restore_controller {
public static function hack_executing($state) {
self::$executing = $state;
}
}

@ -25,6 +25,7 @@
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__ . '/fixtures/event.php');
require_once(__DIR__ . '/fixtures/restore_hack.php');
class logstore_standard_store_testcase extends advanced_testcase {
public function test_log_writing() {
@ -55,6 +56,7 @@ class logstore_standard_store_testcase extends advanced_testcase {
$stores = $manager->get_readers();
$this->assertCount(1, $stores);
$this->assertEquals(array('logstore_standard'), array_keys($stores));
/** @var \logstore_standard\log\store $store */
$store = $stores['logstore_standard'];
$this->assertInstanceOf('logstore_standard\log\store', $store);
$this->assertInstanceOf('tool_log\log\writer', $store);
@ -87,9 +89,11 @@ class logstore_standard_store_testcase extends advanced_testcase {
\core\session\manager::loginas($user1->id, context_system::instance());
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
logstore_standard_restore::hack_executing(1);
$event2 = \logstore_standard\event\unittest_executed::create(
array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
$event2->trigger();
logstore_standard_restore::hack_executing(0);
$_SESSION['SESSION'] = new \stdClass();
$this->setUser(0);
@ -100,13 +104,14 @@ class logstore_standard_store_testcase extends advanced_testcase {
array_shift($logs);
$log2 = array_shift($logs);
$this->assertSame('\core\event\user_loggedinas', $log2->eventname);
$this->assertSame('cli', $log2->origin);
$log3 = array_shift($logs);
unset($log3->id);
$log3->other = unserialize($log3->other);
$log3 = (array)$log3;
$data = $event2->get_data();
$data['origin'] = 'cli';
$data['origin'] = 'restore';
$data['ip'] = null;
$data['realuserid'] = 2;
$this->assertEquals($data, $log3);

@ -87,6 +87,8 @@ defined('MOODLE_INTERNAL') || die();
* @property-read string $pagetype The page type string, should be used as the id for the body tag in the theme.
* @property-read int $periodicrefreshdelay The periodic refresh delay to use with meta refresh
* @property-read page_requirements_manager $requires Tracks the JavaScript, CSS files, etc. required by this page.
* @property-read string $requestip The IP address of the current request, null if unknown.
* @property-read string $requestorigin The type of request 'web', 'ws', 'cli', 'restore', etc.
* @property-read settings_navigation $settingsnav The settings navigation
* @property-read int $state One of the STATE_... constants
* @property-read string $subpage The subpage identifier, if any.
@ -719,6 +721,38 @@ class moodle_page {
return $this->_settingsnav;
}
/**
* Returns request IP address.
*
* @return string IP address or null if unknown
*/
protected function magic_get_requestip() {
return getremoteaddr(null);
}
/**
* Returns the origin of current request.
*
* Note: constants are not required because we need to use these values in logging and reports.
*
* @return string 'web', 'ws', 'cli', 'restore', etc.
*/
protected function magic_get_requestorigin() {
if (class_exists('restore_controller', false) && restore_controller::is_executing()) {
return 'restore';
}
if (WS_SERVER) {
return 'ws';
}
if (CLI_SCRIPT) {
return 'cli';
}
return 'web';
}
/**
* PHP overloading magic to make the $PAGE->course syntax work by redirecting
* it to the corresponding $PAGE->magic_get_course() method if there is one, and

@ -304,6 +304,11 @@ if (defined('WEB_CRON_EMULATED_CLI')) {
}
}
// All web service requests have WS_SERVER == true.
if (!defined('WS_SERVER')) {
define('WS_SERVER', false);
}
// Detect CLI maintenance mode - this is useful when you need to mess with database, such as during upgrades
if (file_exists("$CFG->dataroot/climaintenance.html")) {
if (!CLI_SCRIPT) {
@ -745,6 +750,10 @@ if (CLI_SCRIPT) {
// no sessions in CLI scripts possible
define('NO_MOODLE_COOKIES', true);
} else if (WS_SERVER) {
// No sessions possible in web services.
define('NO_MOODLE_COOKIES', true);
} else if (!defined('NO_MOODLE_COOKIES')) {
if (empty($CFG->version) or $CFG->version < 2009011900) {
// no session before sessions table gets created

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
// Make sure OPcache does not strip comments, we need them for Zend!
if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
// Make sure OPcache does not strip comments, we need them for Zend!
if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
require('../../config.php');
require_once("$CFG->dirroot/webservice/rest/locallib.php");

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
require('../../config.php');
require_once("$CFG->dirroot/webservice/rest/locallib.php");

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
// Make sure OPcache does not strip comments, we need them for Zend!
if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
// Make sure OPcache does not strip comments, we need them for Zend!
if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {

@ -3,6 +3,12 @@ information provided here is intended especially for developers.
This information is intended for authors of webservices, not people writing webservice clients.
=== 2.7 ===
* All webservice server.php and simpleserver.php scripts must define('WS_SERVER', true)
before including config.php file.
=== 2.6 ===
* webservice/upload.php

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
// Make sure OPcache does not strip comments, we need them for Zend!
if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {

@ -28,10 +28,7 @@
*/
define('NO_DEBUG_DISPLAY', true);
/**
* NO_MOODLE_COOKIES - no cookies with web service
*/
define('NO_MOODLE_COOKIES', true);
define('WS_SERVER', true);
// Make sure OPcache does not strip comments, we need them for Zend!
if (ini_get('opcache.enable') and strtolower(ini_get('opcache.enable')) !== 'off') {