/* $Id$ * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ package org.apache.fop.layout; // FOP import org.apache.fop.datatypes.*; import org.apache.fop.fo.flow.Marker; import org.apache.fop.layout.inline.InlineSpace; // Java import java.util.Vector; import java.util.Hashtable; abstract public class Area extends Box { /* nominal font size and nominal font family incorporated in fontState */ protected FontState fontState; protected BorderAndPadding bp=null; protected Vector children = new Vector(); /* max size in line-progression-direction */ protected int maxHeight; /** * Total height of content of this area. */ protected int currentHeight = 0; // used to keep track of the current x position within a table. Required for drawing rectangle links. protected int tableCellXOffset = 0; // used to keep track of the absolute height on the page. Required for drawing rectangle links. private int absoluteHeight = 0; protected int contentRectangleWidth; protected int allocationWidth; /* the page this area is on */ protected Page page; protected ColorType backgroundColor; private IDReferences idReferences; protected Vector markers; // as defined in Section 6.1.1 protected org.apache.fop.fo.FObj generatedBy; // corresponds to 'generated-by' trait protected Hashtable returnedBy; // as defined in Section 6.1.1 protected String areaClass; // as defined in Section 4.2.2 protected boolean isFirst = false; protected boolean isLast = false; /* author : Seshadri G ** the fo which created it */ // This is deprecated and should be phased out in // favour of using 'generatedBy' public org.apache.fop.fo.FObj foCreator; public Area (FontState fontState) { setFontState(fontState); this.markers = new Vector(); this.returnedBy = new Hashtable(); } /** * Creates a new Area instance. * * @param fontState a FontState value * @param allocationWidth the inline-progression dimension of the content * rectangle of the Area * @param maxHeight the maximum block-progression dimension available * for this Area (its allocation rectangle) */ public Area (FontState fontState, int allocationWidth, int maxHeight) { setFontState(fontState); this.allocationWidth = allocationWidth; this.contentRectangleWidth = allocationWidth; this.maxHeight = maxHeight; this.markers = new Vector(); this.returnedBy = new Hashtable(); } private void setFontState(FontState fontState) { // fontState.setFontInfo(this.page.getFontInfo()); this.fontState = fontState; } public void addChild(Box child) { this.children.addElement(child); child.parent = this; } public void addChildAtStart(Box child) { this.children.insertElementAt(child, 0); child.parent = this; } public void addDisplaySpace(int size) { this.addChild(new DisplaySpace(size)); this.absoluteHeight += size; this.currentHeight += size; } public void addInlineSpace(int size) { this.addChild(new InlineSpace(size)); // other adjustments... } public FontInfo getFontInfo() { return this.page.getFontInfo(); } public void end() { } public int getAllocationWidth() { /* ATTENTION: this may change your output!! (Karen Lease, 4mar2001) return this.allocationWidth - getPaddingLeft() - getPaddingRight() - getBorderLeftWidth() - getBorderRightWidth(); */ return this.allocationWidth ; } /** * Set the allocation width. * @param w The new allocation width. * This sets content width to the same value. * Currently only called during layout of Table to set the width * to the total width of all the columns. Note that this assumes the * column widths are explicitly specified. */ public void setAllocationWidth(int w) { this.allocationWidth = w; this.contentRectangleWidth = this.allocationWidth; } public Vector getChildren() { return this.children; } public boolean hasChildren() { return (this.children.size() != 0); } public int getContentWidth() { /* ATTENTION: this may change your output!! (Karen Lease, 4mar2001) return contentRectangleWidth - getPaddingLeft() - getPaddingRight() - getBorderLeftWidth() - getBorderRightWidth(); */ return contentRectangleWidth ; } public FontState getFontState() { return this.fontState; } /** * Returns content height of the area. * * @return Content height in millipoints */ public int getContentHeight() { return this.currentHeight; } /** * Returns allocation height of this area. * The allocation height is the sum of the content height plus border * and padding in the vertical direction. * * @return allocation height in millipoints */ public int getHeight() { return this.currentHeight + getPaddingTop() + getPaddingBottom() + getBorderTopWidth() + getBorderBottomWidth(); } public int getMaxHeight() { // Change KDL: return max height of content rectangle return this.maxHeight; /* return this.maxHeight - getPaddingTop() - getPaddingBottom() - getBorderTopWidth() - getBorderBottomWidth(); */ } public Page getPage() { return this.page; } public ColorType getBackgroundColor() { return this.backgroundColor; } // Must handle conditionality here, depending on isLast/isFirst public int getPaddingTop() { return (bp==null? 0 : bp.getPaddingTop(false)); } public int getPaddingLeft() { return(bp==null? 0 : bp.getPaddingLeft(false)); } public int getPaddingBottom() { return (bp==null? 0 : bp.getPaddingBottom(false)); } public int getPaddingRight() { return (bp==null? 0 : bp.getPaddingRight(false)); } // Handle border-width, including conditionality // For now, just pass false everywhere! public int getBorderTopWidth() { return (bp==null? 0 : bp.getBorderTopWidth(false)); } public int getBorderRightWidth() { return (bp==null? 0 : bp.getBorderRightWidth(false)); } public int getBorderLeftWidth() { return (bp==null? 0 : bp.getBorderLeftWidth(false)); } public int getBorderBottomWidth() { return (bp==null? 0 : bp.getBorderBottomWidth(false)); } public int getTableCellXOffset() { return tableCellXOffset; } public void setTableCellXOffset(int offset) { tableCellXOffset = offset; } public int getAbsoluteHeight() { return absoluteHeight; } public void setAbsoluteHeight(int value) { absoluteHeight = value; } public void increaseAbsoluteHeight(int value) { absoluteHeight += value; } public void increaseHeight(int amount) { this.currentHeight += amount; this.absoluteHeight += amount; } // Remove allocation height of child public void removeChild(Area area) { this.currentHeight -= area.getHeight(); this.absoluteHeight -= area.getHeight(); this.children.removeElement(area); } public void removeChild(DisplaySpace spacer) { this.currentHeight -= spacer.getSize(); this.absoluteHeight -= spacer.getSize(); this.children.removeElement(spacer); } public void remove() { this.parent.removeChild(this); } public void setPage(Page page) { this.page = page; } public void setBackgroundColor(ColorType bgColor) { this.backgroundColor = bgColor; } public void setBorderAndPadding(BorderAndPadding bp) { this.bp = bp; } /** * Return space remaining in the vertical direction (height). * This returns maximum available space - current content height * Note: content height should be based on allocation height of content! * @return space remaining in base units (millipoints) */ public int spaceLeft() { return maxHeight - currentHeight; } public void start() { } /** * Set the content height to the passed value if that value is * larger than current content height. If the new content height * is greater than the maximum available height, set the content height * to the max. available (!!!) * * @param height allocation height of content in millipoints */ public void setHeight(int height) { int prevHeight = currentHeight; if (height > currentHeight) { currentHeight = height; } if (currentHeight > getMaxHeight()) { currentHeight = getMaxHeight(); } absoluteHeight += (currentHeight - prevHeight); } public void setMaxHeight(int height) { this.maxHeight = height; } public Area getParent() { return this.parent; } public void setParent(Area parent) { this.parent = parent; } public void setIDReferences(IDReferences idReferences) { this.idReferences = idReferences; } public IDReferences getIDReferences() { return idReferences; } /* Author seshadri */ public org.apache.fop.fo.FObj getfoCreator() { return this.foCreator; } // Function not currently used! (KLease, 16mar01) public AreaContainer getNearestAncestorAreaContainer() { Area area = this.getParent(); while (!(area instanceof AreaContainer)) { area = area.getParent(); } return (AreaContainer)area; } public BorderAndPadding getBorderAndPadding() { return bp; } public void addMarker(Marker marker) { markers.addElement(marker); } public void addMarkers(Vector markers) { markers.addAll(markers); } public void addLineagePair(org.apache.fop.fo.FObj fo, int areaPosition) { returnedBy.put(fo, new Integer(areaPosition)); } public Vector getMarkers() { return markers; } public void setGeneratedBy(org.apache.fop.fo.FObj generatedBy) { this.generatedBy = generatedBy; } public org.apache.fop.fo.FObj getGeneratedBy() { return generatedBy; } public void isFirst(boolean isFirst) { this.isFirst = isFirst; } public boolean isFirst() { return isFirst; } public void isLast(boolean isLast) { this.isLast = isLast; } public boolean isLast() { return isLast; } } alue='artonge/fix/vue_app_names'>artonge/fix/vue_app_names Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/LargeFileHelper.php
blob: 238fb0790b8b7e6c48ddbfe884a1b1bc50bc9f59 (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
<?php
/**
 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-only
 */
namespace OC;

use bantu\IniGetWrapper\IniGetWrapper;

/**
 * Helper class for large files on 32-bit platforms.
 */
class LargeFileHelper {
	/**
	 * pow(2, 53) as a base-10 string.
	 * @var string
	 */
	public const POW_2_53 = '9007199254740992';

	/**
	 * pow(2, 53) - 1 as a base-10 string.
	 * @var string
	 */
	public const POW_2_53_MINUS_1 = '9007199254740991';

	/**
	 * @brief Checks whether our assumptions hold on the PHP platform we are on.
	 *
	 * @throws \RuntimeException if our assumptions do not hold on the current
	 *                           PHP platform.
	 */
	public function __construct() {
		$pow_2_53 = (float)self::POW_2_53_MINUS_1 + 1.0;
		if ($this->formatUnsignedInteger($pow_2_53) !== self::POW_2_53) {
			throw new \RuntimeException(
				'This class assumes floats to be double precision or "better".'
			);
		}
	}

	/**
	 * @brief Formats a signed integer or float as an unsigned integer base-10
	 *        string. Passed strings will be checked for being base-10.
	 *
	 * @param int|float|string $number Number containing unsigned integer data
	 *
	 * @throws \UnexpectedValueException if $number is not a float, not an int
	 *                                   and not a base-10 string.
	 *
	 * @return string Unsigned integer base-10 string
	 */
	public function formatUnsignedInteger(int|float|string $number): string {
		if (is_float($number)) {
			// Undo the effect of the php.ini setting 'precision'.
			return number_format($number, 0, '', '');
		} elseif (is_string($number) && ctype_digit($number)) {
			return $number;
		} elseif (is_int($number)) {
			// Interpret signed integer as unsigned integer.
			return sprintf('%u', $number);
		} else {
			throw new \UnexpectedValueException(
				'Expected int, float or base-10 string'
			);
		}
	}

	/**
	 * @brief Tries to get the size of a file via various workarounds that
	 *        even work for large files on 32-bit platforms.
	 *
	 * @param string $filename Path to the file.
	 *
	 * @return int|float Number of bytes as number (float or int)
	 */
	public function getFileSize(string $filename): int|float {
		$fileSize = $this->getFileSizeViaCurl($filename);
		if (!is_null($fileSize)) {
			return $fileSize;
		}
		$fileSize = $this->getFileSizeViaExec($filename);
		if (!is_null($fileSize)) {
			return $fileSize;
		}
		return $this->getFileSizeNative($filename);
	}

	/**
	 * @brief Tries to get the size of a file via a CURL HEAD request.
	 *
	 * @param string $fileName Path to the file.
	 *
	 * @return null|int|float Number of bytes as number (float or int) or
	 *                        null on failure.
	 */
	public function getFileSizeViaCurl(string $fileName): null|int|float {
		if (\OC::$server->get(IniGetWrapper::class)->getString('open_basedir') === '') {
			$encodedFileName = rawurlencode($fileName);
			$ch = curl_init("file:///$encodedFileName");
			curl_setopt($ch, CURLOPT_NOBODY, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HEADER, true);
			$data = curl_exec($ch);
			curl_close($ch);
			if ($data !== false) {
				$matches = [];
				preg_match('/Content-Length: (\d+)/', $data, $matches);
				if (isset($matches[1])) {
					return 0 + $matches[1];
				}
			}
		}
		return null;
	}

	/**
	 * @brief Tries to get the size of a file via an exec() call.
	 *
	 * @param string $filename Path to the file.
	 *
	 * @return null|int|float Number of bytes as number (float or int) or
	 *                        null on failure.
	 */
	public function getFileSizeViaExec(string $filename): null|int|float {
		if (\OCP\Util::isFunctionEnabled('exec')) {
			$os = strtolower(php_uname('s'));
			$arg = escapeshellarg($filename);
			$result = null;
			if (str_contains($os, 'linux')) {
				$result = $this->exec("stat -c %s $arg");
			} elseif (str_contains($os, 'bsd') || str_contains($os, 'darwin')) {
				$result = $this->exec("stat -f %z $arg");
			}
			return $result;
		}
		return null;
	}

	/**
	 * @brief Gets the size of a file via a filesize() call and converts
	 *        negative signed int to positive float. As the result of filesize()
	 *        will wrap around after a file size of 2^32 bytes = 4 GiB, this
	 *        should only be used as a last resort.
	 *
	 * @param string $filename Path to the file.
	 *
	 * @return int|float Number of bytes as number (float or int).
	 */
	public function getFileSizeNative(string $filename): int|float {
		$result = filesize($filename);
		if ($result < 0) {
			// For file sizes between 2 GiB and 4 GiB, filesize() will return a
			// negative int, as the PHP data type int is signed. Interpret the
			// returned int as an unsigned integer and put it into a float.
			return (float)sprintf('%u', $result);
		}
		return $result;
	}

	/**
	 * Returns the current mtime for $fullPath
	 */
	public function getFileMtime(string $fullPath): int {
		try {
			$result = filemtime($fullPath) ?: -1;
		} catch (\Exception $e) {
			$result = - 1;
		}
		if ($result < 0) {
			if (\OCP\Util::isFunctionEnabled('exec')) {
				$os = strtolower(php_uname('s'));
				if (str_contains($os, 'linux')) {
					return (int)($this->exec('stat -c %Y ' . escapeshellarg($fullPath)) ?? -1);
				}
			}
		}
		return $result;
	}

	protected function exec(string $cmd): null|int|float {
		$result = trim(exec($cmd));
		return ctype_digit($result) ? 0 + $result : null;
	}
}