forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LogoService.php
137 lines (119 loc) · 3.92 KB
/
LogoService.php
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
<?php
/**
* Login Service class.
*
* Business logic for the login page
*
* @package OpenEMR
* @subpackage Login
* @author Robert Down <[email protected]>
* @copyright Copyright (c) 2023 Robert Down
* @copyright Providence Healthtech
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
namespace OpenEMR\Services;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
class LogoService
{
/**
* Finder class
*
* @var Finder
*/
private $finder;
/**
* Filesystem class
*
* @var Filesystem
*/
private $fs;
public function __construct()
{
// Ensure a finder object exists
$this->resetFinder();
$this->fs = new Filesystem();
}
private function resetFinder()
{
$this->finder = new Finder();
}
public function reset()
{
$this->resetFinder();
}
/**
* Get a logo, if one exists. Ignores any rendering options, just returns a filepath
*
* $type matches the filepath of the logo, (i.e.) core/login/primary or
* core/login/secondary. The fallback paths are automatically matched here,
* concatenating the $type argument with the images_static_absolute path for
* all logos.
*
* @param string $type
* @return string
*/
public function getLogo(string $type, string $filename = "logo.*"): string
{
$siteDir = "{$GLOBALS['OE_SITE_DIR']}/images/logos/{$type}/";
$publicDir = "{$GLOBALS['images_static_absolute']}/logos/{$type}/";
$paths = [];
if ($this->fs->exists($publicDir)) {
$paths[] = $publicDir;
}
if ($this->fs->exists($siteDir)) {
$paths[] = $siteDir;
}
try {
$logo = $this->findLogo($paths, $filename);
} catch (\Exception $e) {
error_log($e->getMessage());
$logo = "";
}
// This is critical, the finder must be completely reinstantiated to ensure the proper directories are searched next time.
$this->resetFinder();
return $this->convertToWebPath($logo);
}
/**
* Convert a path between absolute and web-friendly paths for sites and images paths
*
* @param string $path
* @return string
*/
private function convertToWebPath(string $path): string
{
$paths = [
$GLOBALS['OE_SITE_DIR'] => $GLOBALS['OE_SITE_WEBROOT'],
$GLOBALS['images_static_absolute'] => $GLOBALS['images_static_relative'],
];
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$path = str_replace('\\', '/', $path);
}
return str_replace(array_keys($paths), array_values($paths), $path);
}
/**
* Search in the given directories for a filename
*
* By default, will search in the directory array for any file named "logo" (extension agnostic). If found, only
* the last file found will be returned. By default, will append a query string for time modified to cache bust.
*
* @param array $directory Array of directories to search
* @param string $filename File to look for
* @param boolean $timestamp Will return with a query string of the last modified time
* @return string|null String of real path or null if no file found
*/
private function findLogo(array $directory, string $filename = 'logo.*', $timestamp = true): string
{
$this->finder->files()->in($directory)->name($filename);
if ($this->finder->hasResults()) {
// There is at least 1 file in the sites directory for the given logo
foreach ($this->finder as $f) {
$return = $f->getRealPath();
$return = ($timestamp) ? $return . "?t=" . $f->getMTime() : $return;
}
} else {
$return = "";
}
return $return;
}
}