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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
<?php
namespace EasyRdf;
/**
* EasyRdf
*
* LICENSE
*
* Copyright (c) 2009-2014 Nicholas J Humfrey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2014 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/
/**
* Class containing static utility functions
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2014 Nicholas J Humfrey
* @license https://www.opensource.org/licenses/bsd-license.php
*/
class Utils
{
/**
* Convert a string into CamelCase
*
* A capital letter is inserted for any non-letter (including userscore).
* For example:
* 'hello world' becomes HelloWorld
* 'rss-tag-soup' becomes RssTagSoup
* 'FOO//BAR' becomes FooBar
*
* @param string $str The input string
*
* @return string The input string converted to CamelCase
*/
public static function camelise($str)
{
$cc = '';
foreach (preg_split('/[\W_]+/', $str) as $part) {
$cc .= ucfirst(strtolower($part));
}
return $cc;
}
/**
* Check if something is an associative array
*
* Note: this method only checks the key of the first value in the array.
*
* @param mixed $param The variable to check
*
* @return bool true if the variable is an associative array
*/
public static function isAssociativeArray($param)
{
if (is_array($param)) {
$keys = array_keys($param);
if ($keys[0] === 0) {
return false;
} else {
return true;
}
} else {
return false;
}
}
/**
* Remove the fragment from a URI (if it has one)
*
* @param mixed $uri A URI
*
* @return string The same URI with the fragment removed
*/
public static function removeFragmentFromUri($uri)
{
$pos = strpos($uri, '#');
if ($pos === false) {
return $uri;
} else {
return substr($uri, 0, $pos);
}
}
/** Return pretty-print view of a resource URI
*
* This method is mainly intended for internal use and is used by
* EasyRdf\Graph and EasyRdf\Sparql\Result to format a resource
* for display.
*
* @param mixed $resource An EasyRdf\Resource object or an associative array
* @param string $format Either 'html' or 'text'
* @param string $color The colour of the text
*
* @throws \InvalidArgumentException
* @return string
*/
public static function dumpResourceValue($resource, $format = 'html', $color = 'blue')
{
if (!preg_match('/^#?[-\w]+$/', $color)) {
throw new \InvalidArgumentException(
"\$color must be a legal color code or name"
);
}
if (is_object($resource)) {
$resource = strval($resource);
} elseif (is_array($resource)) {
$resource = $resource['value'];
}
$short = RdfNamespace::shorten($resource);
if ($format == 'html') {
$escaped = htmlentities($resource, ENT_QUOTES);
if (substr($resource, 0, 2) == '_:') {
$href = '#' . $escaped;
} else {
$href = $escaped;
}
if ($short) {
return "<a href='$href' style='text-decoration:none;color:$color'>$short</a>";
} else {
return "<a href='$href' style='text-decoration:none;color:$color'>$escaped</a>";
}
} else {
if ($short) {
return $short;
} else {
return $resource;
}
}
}
/** Return pretty-print view of a literal
*
* This method is mainly intended for internal use and is used by
* EasyRdf\Graph and EasyRdf\Sparql\Result to format a literal
* for display.
*
* @param mixed $literal An EasyRdf\Literal object or an associative array
* @param string $format Either 'html' or 'text'
* @param string $color The colour of the text
*
* @throws \InvalidArgumentException
* @return string
*/
public static function dumpLiteralValue($literal, $format = 'html', $color = 'black')
{
if (!preg_match('/^#?[-\w]+$/', $color)) {
throw new \InvalidArgumentException(
"\$color must be a legal color code or name"
);
}
if (is_object($literal)) {
$literal = $literal->toRdfPhp();
} elseif (!is_array($literal)) {
$literal = array('value' => $literal);
}
$text = '"'.$literal['value'].'"';
if (isset($literal['lang'])) {
$text .= '@' . $literal['lang'];
}
if (isset($literal['datatype'])) {
$short = RdfNamespace::shorten($literal['datatype']);
if ($short) {
$text .= "^^$short";
} else {
$text .= "^^<".$literal['datatype'].">";
}
}
if ($format == 'html') {
return "<span style='color:$color'>".
htmlentities($text, ENT_COMPAT, "UTF-8").
"</span>";
} else {
return $text;
}
}
/** Clean up and split a mime-type up into its parts
*
* @param string $mimeType A MIME Type, optionally with parameters
*
* @return array $type, $parameters
*/
public static function parseMimeType($mimeType)
{
$parts = explode(';', strtolower($mimeType));
$type = trim(array_shift($parts));
$params = array();
foreach ($parts as $part) {
if (preg_match('/^\s*(\w+)\s*=\s*(.+?)\s*$/', $part, $matches)) {
$params[$matches[1]] = $matches[2];
}
}
return array($type, $params);
}
/** Execute a command as a pipe
*
* The proc_open() function is used to open a pipe to a
* a command line process, writing $input to STDIN, returning STDOUT
* and throwing an exception if anything is written to STDERR or the
* process returns non-zero.
*
* @param string $command The command to execute
* @param array $args Optional list of arguments to pass to the command
* @param string $input Optional buffer to send to the command
* @param string $dir Path to directory to run command in (defaults to /tmp)
*
* @throws Exception
* @return string The result of the command, printed to STDOUT
*/
public static function execCommandPipe($command, $args = null, $input = null, $dir = null)
{
$descriptorspec = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('pipe', 'w')
);
// Use the system tmp directory by default
if (!$dir) {
$dir = sys_get_temp_dir();
}
if (is_array($args)) {
$fullCommand = implode(
' ',
array_map('escapeshellcmd', array_merge(array($command), $args))
);
} else {
$fullCommand = escapeshellcmd($command);
if ($args) {
$fullCommand .= ' '.escapeshellcmd($args);
}
}
$process = proc_open($fullCommand, $descriptorspec, $pipes, $dir);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// 2 => readable handle connected to child stderr
if ($input) {
fwrite($pipes[0], $input);
}
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$error = stream_get_contents($pipes[2]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$returnValue = proc_close($process);
if ($returnValue) {
throw new Exception(
"Error while executing command $command: ".$error
);
}
} else {
throw new Exception(
"Failed to execute command $command"
);
}
return $output;
}
}
|