1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of PHPOffice Common |
4
|
|
|
* |
5
|
|
|
* PHPOffice Common is free software distributed under the terms of the GNU Lesser |
6
|
|
|
* General Public License version 3 as published by the Free Software Foundation. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. For the full list of |
10
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
11
|
|
|
* |
12
|
|
|
* @link https://github.com/PHPOffice/Common |
13
|
|
|
* @copyright 2009-2016 PHPOffice Common contributors |
14
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace PhpOffice\Common; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Drawing |
21
|
|
|
*/ |
22
|
|
|
class File |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Verify if a file exists |
26
|
|
|
* |
27
|
|
|
* @param string $pFilename Filename |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
2 |
|
public static function fileExists($pFilename) |
31
|
|
|
{ |
32
|
|
|
// Sick construction, but it seems that |
33
|
|
|
// file_exists returns strange values when |
34
|
|
|
// doing the original file_exists on ZIP archives... |
35
|
2 |
|
if (strtolower(substr($pFilename, 0, 3)) == 'zip') { |
36
|
|
|
// Open ZIP file and verify if the file exists |
37
|
2 |
|
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); |
38
|
2 |
|
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1); |
39
|
|
|
|
40
|
2 |
|
$zip = new \ZipArchive(); |
41
|
2 |
|
if ($zip->open($zipFile) === true) { |
42
|
2 |
|
$returnValue = ($zip->getFromName($archiveFile) !== false); |
43
|
2 |
|
$zip->close(); |
44
|
|
|
|
45
|
2 |
|
return $returnValue; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Regular file_exists |
52
|
2 |
|
return file_exists($pFilename); |
53
|
|
|
} |
54
|
|
|
/** |
55
|
|
|
* Returns the content of a file |
56
|
|
|
* |
57
|
|
|
* @param string $pFilename Filename |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
1 |
|
public static function fileGetContents($pFilename) |
61
|
|
|
{ |
62
|
1 |
|
if (!self::fileExists($pFilename)) { |
63
|
1 |
|
return false; |
|
|
|
|
64
|
|
|
} |
65
|
1 |
|
if (strtolower(substr($pFilename, 0, 3)) == 'zip') { |
66
|
|
|
// Open ZIP file and verify if the file exists |
67
|
1 |
|
$zipFile = substr($pFilename, 6, strpos($pFilename, '#') - 6); |
68
|
1 |
|
$archiveFile = substr($pFilename, strpos($pFilename, '#') + 1); |
69
|
|
|
|
70
|
1 |
|
$zip = new \ZipArchive(); |
71
|
1 |
|
if ($zip->open($zipFile) === true) { |
72
|
1 |
|
$returnValue = $zip->getFromName($archiveFile); |
73
|
1 |
|
$zip->close(); |
74
|
1 |
|
return $returnValue; |
75
|
|
|
} |
76
|
|
|
return false; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
// Regular file contents |
79
|
1 |
|
return file_get_contents($pFilename); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns canonicalized absolute pathname, also for ZIP archives |
84
|
|
|
* |
85
|
|
|
* @param string $pFilename |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
1 |
|
public static function realpath($pFilename) |
89
|
|
|
{ |
90
|
|
|
// Try using realpath() |
91
|
1 |
|
$returnValue = realpath($pFilename); |
92
|
|
|
|
93
|
|
|
// Found something? |
94
|
1 |
|
if ($returnValue == '' || is_null($returnValue)) { |
95
|
1 |
|
$pathArray = explode('/', $pFilename); |
96
|
1 |
|
while (in_array('..', $pathArray) && $pathArray[0] != '..') { |
97
|
1 |
|
$numPathArray = count($pathArray); |
98
|
1 |
|
for ($i = 0; $i < $numPathArray; ++$i) { |
99
|
1 |
|
if ($pathArray[$i] == '..' && $i > 0) { |
100
|
1 |
|
unset($pathArray[$i]); |
101
|
1 |
|
unset($pathArray[$i - 1]); |
102
|
1 |
|
break; |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
1 |
|
} |
106
|
1 |
|
$returnValue = implode('/', $pathArray); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
|
|
// Return |
110
|
1 |
|
return $returnValue; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.