ILIAS  release_8 Revision v8.24
class.ilShellUtil.php
Go to the documentation of this file.
1<?php
2
33{
44 public static function resizeImage(
45 string $a_from,
46 string $a_to,
47 int $a_width,
48 int $a_height,
49 bool $a_constrain_prop = false
50 ): void {
51 if ($a_constrain_prop) {
52 $size = " -geometry " . $a_width . "x" . $a_height . " ";
53 } else {
54 $size = " -resize " . $a_width . "x" . $a_height . "! ";
55 }
56 $convert_cmd = ilShellUtil::escapeShellArg($a_from) . " " . $size . ilShellUtil::escapeShellArg($a_to);
57
58 ilShellUtil::execConvert($convert_cmd);
59 }
60
61 public static function escapeShellArg(string $a_arg): string
62 {
63 setlocale(
64 LC_CTYPE,
65 "UTF8",
66 "en_US.UTF-8"
67 ); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
68 // see also ilias bug 5630
69 return escapeshellarg($a_arg);
70 }
71
78 protected static function processConvertVersion(string $a_version): int
79 {
80 if (preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)([\.|\-]([0-9]+))?/", $a_version, $match)) {
81 $version = str_pad($match[1], 2, "0", STR_PAD_LEFT) .
82 str_pad($match[2], 2, "0", STR_PAD_LEFT) .
83 str_pad($match[3], 2, "0", STR_PAD_LEFT) .
84 str_pad($match[5], 2, "0", STR_PAD_LEFT);
85 return (int) $version;
86 }
87 return 0;
88 }
89
93 public static function escapeShellCmd(string $a_arg): string
94 {
95 if (ini_get('safe_mode') == 1) {
96 return $a_arg;
97 }
98 setlocale(
99 LC_CTYPE,
100 "UTF8",
101 "en_US.UTF-8"
102 ); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
103 return escapeshellcmd($a_arg);
104 }
105
109 public static function execQuoted(string $cmd, ?string $args = null): array
110 {
111 global $DIC;
112
113 if (ilUtil::isWindows() && strpos($cmd, " ") !== false && substr($cmd, 0, 1) !== '"') {
114 // cmd won't work without quotes
115 $cmd = '"' . $cmd . '"';
116 if ($args) {
117 // args are also quoted, workaround is to quote the whole command AGAIN
118 // was fixed in php 5.2 (see php bug #25361)
119 if (version_compare(phpversion(), "5.2", "<") && strpos($args, '"') !== false) {
120 $cmd = '"' . $cmd . " " . $args . '"';
121 } // args are not quoted or php is fixed, just append
122 else {
123 $cmd .= " " . $args;
124 }
125 }
126 } // nothing todo, just append args
127 elseif ($args) {
128 $cmd .= " " . $args;
129 }
130 $arr = [];
131 exec($cmd, $arr);
132 $DIC->logger()->root()->debug("ilUtil::execQuoted: " . $cmd . ".");
133 return $arr;
134 }
135
142 public static function isConvertVersionAtLeast(string $a_version): bool
143 {
144 $current_version = ilShellUtil::execQuoted(PATH_TO_CONVERT, "--version");
145 $current_version = self::processConvertVersion($current_version[0]);
146 $version = self::processConvertVersion($a_version);
147 if ($current_version >= $version) {
148 return true;
149 }
150 return false;
151 }
152
161 public static function getConvertCmd(): string
162 {
163 return PATH_TO_CONVERT;
164 }
165
175 public static function convertImage(
176 string $a_from,
177 string $a_to,
178 string $a_target_format = "",
179 string $a_geometry = "",
180 string $a_background_color = ""
181 ): void {
182 $format_str = ($a_target_format != "")
183 ? strtoupper($a_target_format) . ":"
184 : "";
185 $geometry = "";
186 if ($a_geometry != "") {
187 if (is_int(strpos($a_geometry, "x"))) {
188 $geometry = " -geometry " . $a_geometry . " ";
189 } else {
190 $geometry = " -geometry " . $a_geometry . "x" . $a_geometry . " ";
191 }
192 }
193
194 $bg_color = ($a_background_color != "")
195 ? " -background color " . $a_background_color . " "
196 : "";
197 $convert_cmd = ilShellUtil::escapeShellArg($a_from) . " " . $bg_color . $geometry . ilShellUtil::escapeShellArg(
198 $format_str . $a_to
199 );
200 ilShellUtil::execConvert($convert_cmd);
201 }
202
210 public static function execConvert(string $args): void
211 {
212 $args = self::escapeShellCmd($args);
213 ilShellUtil::execQuoted(PATH_TO_CONVERT, $args);
214 }
215}
$version
Definition: plugin.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static convertImage(string $a_from, string $a_to, string $a_target_format="", string $a_geometry="", string $a_background_color="")
convert image
static escapeShellArg(string $a_arg)
static escapeShellCmd(string $a_arg)
static isConvertVersionAtLeast(string $a_version)
Compare convert version numbers.
static execConvert(string $args)
execute convert command
static processConvertVersion(string $a_version)
Parse convert version string, e.g.
static execQuoted(string $cmd, ?string $args=null)
static getConvertCmd()
get convert command
static resizeImage(string $a_from, string $a_to, int $a_width, int $a_height, bool $a_constrain_prop=false)
resize image
static isWindows()
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28