ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
class.ilShellUtil.php
Go to the documentation of this file.
1 <?php
2 
34 {
45  public static function resizeImage(
46  string $a_from,
47  string $a_to,
48  int $a_width,
49  int $a_height,
50  bool $a_constrain_prop = false
51  ): void {
52  if ($a_constrain_prop) {
53  $size = " -geometry " . $a_width . "x" . $a_height . " ";
54  } else {
55  $size = " -resize " . $a_width . "x" . $a_height . "! ";
56  }
57  $convert_cmd = ilShellUtil::escapeShellArg($a_from) . " " . $size . ilShellUtil::escapeShellArg($a_to);
58 
59  ilShellUtil::execConvert($convert_cmd);
60  }
61 
62  public static function escapeShellArg(string $a_arg): string
63  {
64  setlocale(
65  LC_CTYPE,
66  "UTF8",
67  "en_US.UTF-8"
68  ); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
69  // see also ilias bug 5630
70  return escapeshellarg($a_arg);
71  }
72 
79  protected static function processConvertVersion(string $a_version): int
80  {
81  if (preg_match("/([0-9]+)\.([0-9]+)\.([0-9]+)([\.|\-]([0-9]+))?/", $a_version, $match)) {
82  $version = str_pad($match[1], 2, "0", STR_PAD_LEFT) .
83  str_pad($match[2], 2, "0", STR_PAD_LEFT) .
84  str_pad($match[3], 2, "0", STR_PAD_LEFT) .
85  str_pad($match[5], 2, "0", STR_PAD_LEFT);
86  return (int) $version;
87  }
88  return 0;
89  }
90 
94  public static function escapeShellCmd(string $a_arg): string
95  {
96  if (ini_get('safe_mode') == 1) {
97  return $a_arg;
98  }
99  setlocale(
100  LC_CTYPE,
101  "UTF8",
102  "en_US.UTF-8"
103  ); // fix for PHP escapeshellcmd bug. See: http://bugs.php.net/bug.php?id=45132
104  return escapeshellcmd($a_arg);
105  }
106 
110  public static function execQuoted(string $cmd, ?string $args = null): array
111  {
112  global $DIC;
113 
114  if (ilUtil::isWindows() && strpos($cmd, " ") !== false && substr($cmd, 0, 1) !== '"') {
115  // cmd won't work without quotes
116  $cmd = '"' . $cmd . '"';
117  if ($args) {
118  // args are also quoted, workaround is to quote the whole command AGAIN
119  // was fixed in php 5.2 (see php bug #25361)
120  if (version_compare(phpversion(), "5.2", "<") && strpos($args, '"') !== false) {
121  $cmd = '"' . $cmd . " " . $args . '"';
122  } // args are not quoted or php is fixed, just append
123  else {
124  $cmd .= " " . $args;
125  }
126  }
127  } // nothing todo, just append args
128  elseif ($args) {
129  $cmd .= " " . $args;
130  }
131  $arr = [];
132  exec($cmd, $arr);
133  $DIC->logger()->root()->debug("ilUtil::execQuoted: " . $cmd . ".");
134  return $arr;
135  }
136 
140  public static function isConvertVersionAtLeast(string $a_version): bool
141  {
142  $current_version = ilShellUtil::execQuoted(PATH_TO_CONVERT, "--version");
143  $current_version = self::processConvertVersion($current_version[0]);
144  $version = self::processConvertVersion($a_version);
145  if ($current_version >= $version) {
146  return true;
147  }
148  return false;
149  }
150 
154  public static function getConvertCmd(): string
155  {
156  return PATH_TO_CONVERT;
157  }
158 
162  public static function convertImage(
163  string $a_from,
164  string $a_to,
165  string $a_target_format = "",
166  string $a_geometry = "",
167  string $a_background_color = ""
168  ): void {
169  $format_str = ($a_target_format != "")
170  ? strtoupper($a_target_format) . ":"
171  : "";
172  $geometry = "";
173  if ($a_geometry != "") {
174  if (is_int(strpos($a_geometry, "x"))) {
175  $geometry = " -geometry " . $a_geometry . " ";
176  } else {
177  $geometry = " -geometry " . $a_geometry . "x" . $a_geometry . " ";
178  }
179  }
180 
181  $bg_color = ($a_background_color != "")
182  ? " -background color " . $a_background_color . " "
183  : "";
184  $convert_cmd = ilShellUtil::escapeShellArg($a_from) . " " . $bg_color . $geometry . ilShellUtil::escapeShellArg(
185  $format_str . $a_to
186  );
187  ilShellUtil::execConvert($convert_cmd);
188  }
189 
197  public static function execConvert(string $args): void
198  {
199  $args = self::escapeShellCmd($args);
200  ilShellUtil::execQuoted(PATH_TO_CONVERT, $args);
201  }
202 }
$version
Definition: plugin.php:24
static isConvertVersionAtLeast(string $a_version)
static processConvertVersion(string $a_version)
Parse convert version string, e.g.
static escapeShellArg(string $a_arg)
static isWindows()
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:26
static convertImage(string $a_from, string $a_to, string $a_target_format="", string $a_geometry="", string $a_background_color="")
static execQuoted(string $cmd, ?string $args=null)
static escapeShellCmd(string $a_arg)
static getConvertCmd()
static execConvert(string $args)
execute convert command
static resizeImage(string $a_from, string $a_to, int $a_width, int $a_height, bool $a_constrain_prop=false)
resize image