/** * Write session handler. * * {@see http://php.net/manual/en/function.session-set-save-handler.php} * * NOTE: Do not write to output or throw any exceptions! * Hopefully the next page is going to display nice error or it recovers... * * @param string $sid * @param string $session_data * @return bool success */ public function handler_write($sid, $session_data) { if ($this->failed) { // Do not write anything back - we failed to start the session properly. return false; } $sessdata = base64_encode($session_data); // There might be some binary mess :-( $hash = sha1($sessdata); if ($hash === $this->lasthash) { return true; } try { if ($this->recordid) { $this->database->set_field('sessions', 'sessdata', $sessdata, array('id' => $this->recordid)); } else { // This happens in the first request when session record was just created in manager. $this->database->set_field('sessions', 'sessdata', $sessdata, array('sid' => $sid)); } } catch (\Exception $ex) { // Do not rethrow exceptions here, this should not happen. error_log('Unknown exception when writing database session data : ' . $sid . ' - ' . $ex->getMessage()); } return true; }
/** * Save the discussion to the DB * * @param object $discussion * @param upload_file $uploader */ public function save_discussion($discussion, upload_file $uploader) { $message = ''; $discussion->id = hsuforum_add_discussion($discussion, null, $message); $file = $uploader->process_file_upload($discussion->firstpost); if (!is_null($file)) { $this->db->set_field('hsuforum_posts', 'attachment', 1, array('id' => $discussion->firstpost)); } }
/** * Does all the grunt work for updating a post * * @param object $course * @param object $cm * @param object $forum * @param \context_module $context * @param object $discussion * @param object $post * @param array $deletefiles * @param array $options These override default post values, EG: set the post message with this * @return json_response */ public function handle_update_post($course, $cm, $forum, $context, $discussion, $post, array $deletefiles = array(), array $options) { $this->require_can_edit_post($forum, $context, $discussion, $post); $uploader = new upload_file(new attachments($forum, $context, $deletefiles), \mod_hsuforum_post_form::attachment_options($forum)); // Apply updates to the post. foreach ($options as $name => $value) { if (property_exists($post, $name)) { $post->{$name} = $value; } } $post->itemid = empty($options['itemid']) ? 0 : $options['itemid']; $errors = $this->validate_post($course, $cm, $forum, $context, $discussion, $post, $uploader); if (!empty($errors)) { return $this->create_error_response($errors); } $this->save_post($discussion, $post, $uploader); // If the user has access to all groups and they are changing the group, then update the post. if (empty($post->parent) && has_capability('mod/hsuforum:movediscussions', $context)) { $this->db->set_field('hsuforum_discussions', 'groupid', $options['groupid'], array('id' => $discussion->id)); } $this->trigger_post_updated($context, $forum, $discussion, $post); return new json_response((object) array('eventaction' => 'postupdated', 'discussionid' => (int) $discussion->id, 'postid' => (int) $post->id, 'livelog' => get_string('postwasupdated', 'hsuforum'), 'html' => $this->discussionservice->render_full_thread($discussion->id))); }
/** * Write session handler. * * {@see http://php.net/manual/en/function.session-set-save-handler.php} * * NOTE: Do not write to output or throw any exceptions! * Hopefully the next page is going to display nice error or it recovers... * * @param string $sid * @param string $session_data * @return bool success */ public function handler_write($sid, $session_data) { global $USER; // TODO: MDL-20625 we need to rollback all active transactions and log error if any open needed if ($this->failed) { // do not write anything back - we failed to start the session properly return false; } $userid = 0; if (!empty($USER->realuser)) { $userid = $USER->realuser; } else { if (!empty($USER->id)) { $userid = $USER->id; } } if (isset($this->record->id)) { $data = base64_encode($session_data); // There might be some binary mess :-( // Skip db update if nothing changed, // do not update the timemodified each second. $hash = sha1($data); if ($this->lasthash === $hash and $this->record->userid == $userid and time() - $this->record->timemodified < 20 and $this->record->lastip == getremoteaddr()) { // No need to update anything! return true; } $this->record->sessdata = $data; $this->record->userid = $userid; $this->record->timemodified = time(); $this->record->lastip = getremoteaddr(); try { $this->database->update_record_raw('sessions', $this->record); $this->lasthash = $hash; } catch (dml_exception $ex) { if ($this->database->get_dbfamily() === 'mysql') { try { $this->database->set_field('sessions', 'state', 9, array('id' => $this->record->id)); } catch (Exception $ignored) { } error_log('Can not write database session - please verify max_allowed_packet is at least 4M!'); } else { error_log('Can not write database session'); } return false; } catch (Exception $ex) { error_log('Can not write database session'); return false; } } else { // fresh new session try { $record = new stdClass(); $record->state = 0; $record->sid = $sid; $record->sessdata = base64_encode($session_data); // there might be some binary mess :-( $record->userid = $userid; $record->timecreated = $record->timemodified = time(); $record->firstip = $record->lastip = getremoteaddr(); $record->id = $this->database->insert_record_raw('sessions', $record); $this->record = $this->database->get_record('sessions', array('id' => $record->id)); $this->lasthash = sha1($record->sessdata); $this->database->get_session_lock($this->record->id, SESSION_ACQUIRE_LOCK_TIMEOUT); } catch (Exception $ex) { // this should not happen error_log('Can not write new database session or acquire session lock'); $this->failed = true; return false; } } return true; }