invoke('session_name', [$name]); $this->invoke('session_cache_limiter', ['']); try { $this->startSession(); } catch (\Exception $e) { setcookie($this->invoke('session_name'), '', -1, \OC::$WEBROOT ?: '/'); } restore_error_handler(); if (!isset($_SESSION)) { throw new \Exception('Failed to start session'); } } /** * @param string $key * @param integer $value */ public function set(string $key, $value) { $reopened = $this->reopen(); $_SESSION[$key] = $value; if ($reopened) { $this->close(); } } /** * @param string $key * @return mixed */ public function get(string $key) { if (!$this->exists($key)) { return null; } return $_SESSION[$key]; } /** * @param string $key * @return bool */ public function exists(string $key): bool { return isset($_SESSION[$key]); } /** * @param string $key */ public function remove(string $key) { if (isset($_SESSION[$key])) { unset($_SESSION[$key]); } } public function clear() { $this->reopen(); $this->invoke('session_unset'); $this->regenerateId(); $this->invoke('session_write_close'); $this->startSession(true); $_SESSION = []; } public function close() { $this->invoke('session_write_close'); parent::close(); } /** * Wrapper around session_regenerate_id * * @param bool $deleteOldSession Whether to delete the old associated session file or not. * @param bool $updateToken Whether to update the associated auth token * @return void */ public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) { $this->reopen(); $oldId = null; if ($updateToken) { // Get the old id to update the token try { $oldId = $this->getId(); } catch (SessionNotAvailableException $e) { // We can't update a token if there is no previous id $updateToken = false; } } try { @session_regenerate_id($deleteOldSession); } catch (\Error $e) { $this->trapError($e->getCode(), $e->getMessage()); } if ($updateToken) { // Get the new id to update the token $newId = $this->getId(); /** @var IProvider $tokenProvider */ $tokenProvider = \OCP\Server::get(IProvider::class); try { $tokenProvider->renewSessionToken($oldId, $newId); } catch (InvalidTokenException $e) { // Just ignore } } } /** * Wrapper around session_id * * @return string * @throws SessionNotAvailableException * @since 9.1.0 */ public function getId(): string { $id = $this->invoke('session_id', [], true); if ($id === '') { throw new SessionNotAvailableException(); } return $id; } /** * @throws \Exception */ public function reopen(): bool { if ($this->sessionClosed) { $this->startSession(false, false); $this->sessionClosed = false; return true; } return false; } /** * @param int $errorNumber * @param string $errorString * @throws \ErrorException */ public function trapError(int $errorNumber, string $errorString) { if ($errorNumber & E_ERROR) { throw new \ErrorException($errorString); } } /** * @param string $functionName the full session_* function name * @param array $parameters * @param bool $silence whether to suppress warnings * @throws \ErrorException via trapError * @return mixed */ private function invoke(string $functionName, array $parameters = [], bool $silence = false) { try { $timeBefore = microtime(true); if ($silence) { $result = @call_user_func_array($functionName, $parameters); } else { $result = call_user_func_array($functionName, $parameters); } $timeAfter = microtime(true); $timeSpent = $timeAfter - $timeBefore; if ($timeSpent > 0.1) { $logLevel = match (true) { $timeSpent > 25 => ILogger::ERROR, $timeSpent > 10 => ILogger::WARN, $timeSpent > 0.5 => ILogger::INFO, default => ILogger::DEBUG, }; $this->logger?->log( $logLevel, "Slow session operation $functionName detected", [ 'parameters' => $parameters, 'timeSpent' => $timeSpent, ], ); } return $result; } catch (\Error $e) { $this->trapError($e->getCode(), $e->getMessage()); } } private function startSession(bool $silence = false, bool $readAndClose = true) { $sessionParams = ['cookie_samesite' => 'Lax']; if (\OC::hasSessionRelaxedExpiry()) { $sessionParams['read_and_close'] = $readAndClose; } $this->invoke('session_start', [$sessionParams], $silence); } } phics FOP: https://github.com/apache/xmlgraphics-fopwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/rtf/rtflib/rtfdoc/RtfSection.java
blob: 88c7e599b584a15e9c0d7af2b8a4cef2f82ff39c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.rtf.rtflib.rtfdoc;

/*
 * This file is part of the RTF library of the FOP project, which was originally
 * created by Bertrand Delacretaz <bdelacretaz@codeconsult.ch> and by other
 * contributors to the jfor project (www.jfor.org), who agreed to donate jfor to
 * the FOP project.
 */

import java.io.Writer;
import java.io.IOException;

/**  Models a section in an RTF document
 *  @author Bertrand Delacretaz bdelacretaz@codeconsult.ch
 */

public class RtfSection
extends RtfContainer
implements
    IRtfParagraphContainer,
    IRtfTableContainer,
    IRtfListContainer,
    IRtfExternalGraphicContainer,
    IRtfBeforeContainer,
    IRtfParagraphKeepTogetherContainer,
    IRtfAfterContainer,
    IRtfJforCmdContainer,
    IRtfTextrunContainer {
    private RtfParagraph paragraph;
    private RtfTable table;
    private RtfList list;
    private RtfExternalGraphic externalGraphic;
    private RtfBefore before;
    private RtfAfter after;
    private RtfJforCmd jforCmd;

    /** Create an RTF container as a child of given container */
    RtfSection(RtfDocumentArea parent, Writer w) throws IOException {
        super(parent, w);
    }

    /**
     * Start a new external graphic after closing current paragraph, list and table
     * @return new RtfExternalGraphic object
     * @throws IOException for I/O problems
     */
    public RtfExternalGraphic newImage() throws IOException {
        closeAll();
        externalGraphic = new RtfExternalGraphic(this, writer);
        return externalGraphic;
    }

    /**
     * Start a new paragraph after closing current paragraph, list and table
     * @param attrs attributes for new RtfParagraph
     * @return new RtfParagraph object
     * @throws IOException for I/O problems
     */
    public RtfParagraph newParagraph(RtfAttributes attrs) throws IOException {
        closeAll();
        paragraph = new RtfParagraph(this, writer, attrs);
        return paragraph;
    }

    /**
     * Close current paragraph if any and start a new one with default attributes
     * @return new RtfParagraph
     * @throws IOException for I/O problems
     */
    public RtfParagraph newParagraph() throws IOException {
        return newParagraph(null);
    }

    /**
     * Close current paragraph if any and start a new one
     * @return new RtfParagraphKeepTogether
     * @throws IOException for I/O problems
     */
    public RtfParagraphKeepTogether newParagraphKeepTogether() throws IOException {
        return new RtfParagraphKeepTogether(this, writer);
    }

    /**
     * Start a new table after closing current paragraph, list and table
     * @param tc Table context used for number-columns-spanned attribute (added by
     * Boris Poudérous on july 2002)
     * @return new RtfTable object
     * @throws IOException for I/O problems
     */
    public RtfTable newTable(ITableColumnsInfo tc) throws IOException {
        closeAll();
        table = new RtfTable(this, writer, tc);
        return table;
    }

    /**
     * Start a new table after closing current paragraph, list and table
     * @param attrs attributes of new RtfTable
     * @param tc Table context used for number-columns-spanned attribute (added by
     * Boris Poudérous on july 2002)
     * @return new RtfTable object
     * @throws IOException for I/O problems
     */
    public RtfTable newTable(RtfAttributes attrs, ITableColumnsInfo tc) throws IOException {
        closeAll();
        table = new RtfTable(this, writer, attrs, tc);
        return table;
    }

    /**
     * Start a new list after closing current paragraph, list and table
     * @param attrs attributes of new RftList object
     * @return new RtfList
     * @throws IOException for I/O problems
     */
    public RtfList newList(RtfAttributes attrs) throws IOException {
        closeAll();
        list = new RtfList(this, writer, attrs);
        return list;
    }

    /**
     * IRtfBeforeContainer
     * @param attrs attributes of new RtfBefore object
     * @return new RtfBefore object
     * @throws IOException for I/O problems
     */
    public RtfBefore newBefore(RtfAttributes attrs) throws IOException {
        closeAll();
        before = new RtfBefore(this, writer, attrs);
        return before;
    }

    /**
     * IRtfAfterContainer
     * @param attrs attributes of new RtfAfter object
     * @return new RtfAfter object
     * @throws IOException for I/O problems
     */
    public RtfAfter newAfter(RtfAttributes attrs) throws IOException {
        closeAll();
        after = new RtfAfter(this, writer, attrs);
        return after;
    }

    /**
     *
     * @param attrs attributes of new RtfJforCmd
     * @return the new RtfJforCmd
     * @throws IOException for I/O problems
     */
    public RtfJforCmd newJforCmd(RtfAttributes attrs) throws IOException {
        jforCmd  = new RtfJforCmd(this, writer, attrs);
        return jforCmd;
    }



    /**
     * Can be overridden to write RTF prefix code, what comes before our children
     * @throws IOException for I/O problems
     */
    protected void writeRtfPrefix() throws IOException {
        writeAttributes(attrib, RtfPage.PAGE_ATTR);
        newLine();
        writeControlWord("sectd");
    }

    /**
     * Can be overridden to write RTF suffix code, what comes after our children
     * @throws IOException for I/O problems
     */
    protected void writeRtfSuffix() throws IOException {
        writeControlWord("sect");
    }

    private void closeCurrentTable() throws IOException {
        if (table != null) {
            table.close();
        }
    }

    private void closeCurrentParagraph() throws IOException {
        if (paragraph != null) {
            paragraph.close();
        }
    }

    private void closeCurrentList() throws IOException {
        if (list != null) {
            list.close();
        }
    }

    private void closeCurrentExternalGraphic() throws IOException {
        if (externalGraphic != null) {
            externalGraphic.close();
        }
    }

    private void closeCurrentBefore() throws IOException {
        if (before != null) {
            before.close();
        }
    }

    private void closeAll()
    throws IOException {
        closeCurrentTable();
        closeCurrentParagraph();
        closeCurrentList();
        closeCurrentExternalGraphic();
        closeCurrentBefore();
    }
    
    /**
     * Returns the current RtfTextrun.
     * @return Current RtfTextrun
     * @throws IOException Thrown when an IO-problem occurs.
     */
    public RtfTextrun getTextrun()
    throws IOException {
        return RtfTextrun.getTextrun(this, writer, null);
    }
}