|
| 1 | +<?php |
| 2 | +/*========================================================================= |
| 3 | +Program: MIDAS Server |
| 4 | +Language: PHP/HTML/Java/Javascript/SQL |
| 5 | +Date: $Date$ |
| 6 | +Version: $Revision$ |
| 7 | +
|
| 8 | +Copyright (c) Kitware Inc. 28 Corporate Drive. All rights reserved. |
| 9 | +Clifton Park, NY, 12065, USA. |
| 10 | +
|
| 11 | +See Copyright.txt for details. |
| 12 | +This software is distributed WITHOUT ANY WARRANTY; without even |
| 13 | +the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | +PURPOSE. See the above copyright notices for more information. |
| 15 | +=========================================================================*/ |
| 16 | +?> |
| 17 | +<?php |
| 18 | +/** |
| 19 | + * globally useful utility functions. |
| 20 | + */ |
| 21 | +class KWUtils |
| 22 | +{ |
| 23 | + |
| 24 | + CONST DEFAULT_MKDIR_MODE = 0775; |
| 25 | + |
| 26 | + /** |
| 27 | + * @method mkDir |
| 28 | + * @TODO what to do with errors in a way that is consistent with error reporting |
| 29 | + * Will create the directory $dir and set the filemode so that the newly |
| 30 | + * created dir is writable by the current user. |
| 31 | + * @return true on success, false otherwise |
| 32 | + */ |
| 33 | + public static function mkDir($dir, $mode = self::DEFAULT_MKDIR_MODE) |
| 34 | + { |
| 35 | + if(!file_exists($dir) && !mkdir($dir, $mode, true)) |
| 36 | + { |
| 37 | + return false; |
| 38 | + } |
| 39 | + // change file mode |
| 40 | + // even though we are swallowing the error messages, we return false |
| 41 | + // if the operation can't be completed |
| 42 | + if(!is_writeable($dir) || @!chmod($dir, $mode)) |
| 43 | + { |
| 44 | + return false; |
| 45 | + } |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @method createSubDirectories recursively create subdirectories starting at |
| 51 | + * baseDirectory, sequentially creating each of the directories in the |
| 52 | + * subDirectories array, according to the passed in mode. |
| 53 | + * @param $baseDirectory the first directory to create |
| 54 | + * @param $subDirectories an array of directories that will be created in a |
| 55 | + * recursive fashion, each one appending to the last as a deeper subdirectory |
| 56 | + * of baseDirectory |
| 57 | + * @param the mode to create the new directories |
| 58 | + */ |
| 59 | + public static function createSubDirectories($baseDirectory, $subDirectories, $mode = self::DEFAULT_MKDIR_MODE) |
| 60 | + { |
| 61 | + if(!file_exists($baseDirectory) ) |
| 62 | + { |
| 63 | + throw new Zend_Exception($baseDirectory . ' does not exist'); |
| 64 | + } |
| 65 | + $relpath = ''; |
| 66 | + foreach($subDirectories as $directory) |
| 67 | + { |
| 68 | + $relpath .= $directory . "/"; |
| 69 | + |
| 70 | + if(!KwUtils::mkDir($baseDirectory . $relpath, $mode)) |
| 71 | + { |
| 72 | + throw new Zend_Exception($baseDirectory . $relpath . ' could not be created'); |
| 73 | + } |
| 74 | + } |
| 75 | + return $baseDirectory . $relpath; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @method isWindows() |
| 80 | + * @return True if the current platform is windows |
| 81 | + */ |
| 82 | + public static function isWindows() |
| 83 | + { |
| 84 | + return (strtolower(substr(PHP_OS, 0, 3)) == "win"); |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * @method escapeCommand |
| 90 | + * will escape a command respecting the format of the current platform |
| 91 | + * @param $command, the command to be escaped |
| 92 | + * @return the $command, $escaped for the current platform |
| 93 | + * @TODO, how to test this? |
| 94 | + */ |
| 95 | + public static function escapeCommand($command ) |
| 96 | + { |
| 97 | + // if windows platform, add extra double-quote |
| 98 | + // See http://www.mail-archive.com/internals@lists.php.net/msg29874.html |
| 99 | + if(KWUtils::isWindows() ) |
| 100 | + { |
| 101 | + $command = '"'.$command.'"'; |
| 102 | + } |
| 103 | + |
| 104 | + return $command; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @method appendStringIfNot will append the string $ext to |
| 109 | + * $subject if it is not already a suffix of $subject |
| 110 | + * @param $subject, the string to be appended to |
| 111 | + * @param $ext, the extension to check for and append |
| 112 | + * @return $subject, will end with the suffix $ext |
| 113 | + */ |
| 114 | + public static function appendStringIfNot($subject, $ext) |
| 115 | + { |
| 116 | + if(!(substr($subject, strlen($subject) - strlen($ext)) === $ext) ) |
| 117 | + { |
| 118 | + $subject .= $ext; |
| 119 | + } |
| 120 | + return $subject; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @method exec |
| 125 | + * will execute a command, respecting the format of the current platform. |
| 126 | + * @param $command to be executed, with all arguments, and formatted correctly |
| 127 | + * @param $output, a reference to put the output of the command |
| 128 | + * @param $chdir, the dir to change to for execution, if any |
| 129 | + * @param $return_val, a reference to put the return value of the command |
| 130 | + * the temporary work dir |
| 131 | + */ |
| 132 | + public static function exec($command, &$output = null, $chdir = "", &$return_val = null) |
| 133 | + { |
| 134 | + if(!empty($chdir) && is_dir($chdir)) |
| 135 | + { |
| 136 | + if(!chdir($chdir)) |
| 137 | + { |
| 138 | + throw new Zend_Exception("Failed to change directory: [".$chdir."]"); |
| 139 | + } |
| 140 | + } |
| 141 | + // on Linux need to add redirection to handle stderr |
| 142 | + $redirect_error = KWUtils::isLinux() ? " 2>&1" : ""; |
| 143 | + exec(KWUtils::escapeCommand($command) . $redirect_error, $output, $return_val); |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + /** |
| 148 | + * @method isLinux() |
| 149 | + * @return True if the current platform is Linux |
| 150 | + */ |
| 151 | + public static function isLinux() |
| 152 | + { |
| 153 | + return (strtolower(substr(PHP_OS, 0, 5)) == "linux"); |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + /** |
| 159 | + * @method prepareExecCommand |
| 160 | + * will prepare an executable application and params for command line |
| 161 | + * execution, including escaping and quoting arguments. |
| 162 | + * @param $app_name, the application to be executed |
| 163 | + * @param $params, an array of arguments to the application |
| 164 | + * @return the full command line command, escaped and quoted, will throw a |
| 165 | + * Zend_Exception if the app is not in the path and not executable |
| 166 | + */ |
| 167 | + public static function prepareExecCommand($app_name, $params = array()) |
| 168 | + { |
| 169 | + // Check if application is executable, if not, see if you can find it |
| 170 | + // in the path |
| 171 | + if(!KWUtils::isExecutable($app_name, false)) |
| 172 | + { |
| 173 | + $app_name = KWUtils::findApp($app_name, true); |
| 174 | + } |
| 175 | + |
| 176 | + // escape parameters |
| 177 | + $escapedParams = array(); |
| 178 | + foreach($params as $param) |
| 179 | + { |
| 180 | + $escapedParams[] = escapeshellarg($param); |
| 181 | + } |
| 182 | + |
| 183 | + // glue together app_name and params using spaces |
| 184 | + return escapeshellarg($app_name)." ".implode(" ", $escapedParams); |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + /** |
| 189 | + * @method isExecutable will return true if the app can be found and is |
| 190 | + * executable, can optionally look in the path. |
| 191 | + * @param string $app_name, the app to check |
| 192 | + * @param boolean $check_in_path, if true, will search in path for app |
| 193 | + * @return True if app_name is found and executable, False otherwise |
| 194 | + */ |
| 195 | + public static function isExecutable($app_name, $check_in_path = false) |
| 196 | + { |
| 197 | + if(!is_executable($app_name )) |
| 198 | + { |
| 199 | + if($check_in_path) |
| 200 | + { |
| 201 | + try |
| 202 | + { |
| 203 | + if(KWUtils::findApp($app_name, true)) |
| 204 | + { |
| 205 | + return true; |
| 206 | + } |
| 207 | + } |
| 208 | + catch(Zend_Exception $ze) |
| 209 | + { |
| 210 | + return false; |
| 211 | + } |
| 212 | + } |
| 213 | + return false; |
| 214 | + } |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
| 218 | + /** |
| 219 | + * @method findApp will return the absolute path of an application |
| 220 | + * @param $app_name, the name of the application |
| 221 | + * @param $check_execution_flag, whether to include in the check that the |
| 222 | + * application is executable |
| 223 | + * @return the path to the application, throws a Zend_Exception if the app |
| 224 | + * can't be found, or if $check_execution_flag is set and the app is not |
| 225 | + * executable. |
| 226 | + */ |
| 227 | + public static function findApp($app_name, $check_execution_flag ) |
| 228 | + { |
| 229 | + $PHP_PATH_SEPARATOR = ":"; |
| 230 | + // split path |
| 231 | + $path_list = explode($PHP_PATH_SEPARATOR, getenv("PATH")); |
| 232 | + |
| 233 | + // loop through paths |
| 234 | + foreach($path_list as $path) |
| 235 | + { |
| 236 | + $status = false; |
| 237 | + $path_to_app = KWUtils::appendStringIfNot($path, DIRECTORY_SEPARATOR).$app_name; |
| 238 | + if($check_execution_flag) |
| 239 | + { |
| 240 | + if(is_executable($path_to_app)) |
| 241 | + { |
| 242 | + $status = true; |
| 243 | + break; |
| 244 | + } |
| 245 | + } |
| 246 | + else |
| 247 | + { |
| 248 | + if(file_exists($path_to_app)) |
| 249 | + { |
| 250 | + $status = true; |
| 251 | + break; |
| 252 | + } |
| 253 | + } |
| 254 | + } |
| 255 | + if(!$status) |
| 256 | + { |
| 257 | + throw new Zend_Exception("Failed to locate the application: [".$app_name."] [check_execution_flag:".$check_execution_flag."]"); |
| 258 | + } |
| 259 | + return $path_to_app; |
| 260 | + } |
| 261 | + |
| 262 | + |
| 263 | + |
| 264 | + |
| 265 | + /** |
| 266 | + * @method formatAppName |
| 267 | + * Format the application name according to the platform. |
| 268 | + */ |
| 269 | + public static function formatAppName($app_name) |
| 270 | + { |
| 271 | + if(substr(PHP_OS, 0, 3) == "WIN") |
| 272 | + { |
| 273 | + $app_name = KWUtils::appendStringIfNot($app_name, ".exe"); |
| 274 | + } |
| 275 | + return $app_name; |
| 276 | + } |
| 277 | + |
| 278 | + |
| 279 | +} |
0 commit comments