This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
scorerender-utils.inc
248 lines (216 loc) · 6.86 KB
/
scorerender-utils.inc
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
<?php
/**
* ScoreRender documentation
* @package ScoreRender
* @version 0.3.50
* @author Abel Cheung
* @copyright Copyright (C) 2006 Chris Lamb
* @copyright Copyright (C) 2007, 2008, 2009, 2010 Abel Cheung
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU AGPL v3
*/
// Backported function: sys_get_temp_dir
// http://www.phpit.net/article/creating-zip-tar-archives-dynamically-php/2/
//
if (!function_exists ('sys_get_temp_dir')) /* {{{ */
{
/**
* @ignore
*/
function sys_get_temp_dir ()
{
// Try to get from environment variable
if ( !empty($_ENV['TMP']) )
return realpath( $_ENV['TMP'] );
elseif ( !empty($_ENV['TMPDIR']) )
return realpath( $_ENV['TMPDIR'] );
elseif ( !empty($_ENV['TEMP']) )
return realpath( $_ENV['TEMP'] );
else
{
// Detect by creating a temporary file and pick its dir name
$temp_file = tempnam( md5(uniqid(rand(), TRUE)), '' );
if ( !$temp_file) return false;
unlink( $temp_file );
return realpath( dirname($temp_file) );
}
}
} /* }}} */
// Backported function: array_intersect_key
// http://www.php.net/manual/en/function.array-intersect-key.php#68179
//
if (!function_exists ('array_intersect_key')) /* {{{ */
{
/**
* @ignore
*/
function array_intersect_key ()
{
$arrs = func_get_args ();
$result = array_shift ($arrs);
foreach ($arrs as $array)
foreach (array_keys ($result) as $key)
if (!array_key_exists ($key, $array))
unset ($result[$key]);
return $result;
}
} /* }}} */
/**
* Convenience function: Check if OS is Windows
*
* @since 0.3
* return boolean True if OS is Windows, false otherwise.
*/
function is_windows ()
{
return (substr(PHP_OS, 0, 3) == 'WIN');
}
/**
* Transform path string to Windows or Unix presentation
*
* @since 0.3
* @param string $path The path to be transformed
* @param boolean $is_internal Whether to always transform into Unix format, which is used for storing values into database. FALSE means using OS native representation.
* @uses is_windows()
* @return string $path The resulting path, with appropriate slashes or backslashes
*/
function get_path_presentation ($path, $is_internal) /* {{{ */
{
if (is_windows () && ! $is_internal)
return preg_replace ('#/+#', '\\', $path);
// TODO: Check how CJK chars are handled in paths
return preg_replace ('#\\\\+#', '/', $path);
} /* }}} */
/**
* Create temporary directory
*
* Inspired from PHP tempnam documentation comment
*
* @uses sys_get_temp_dir()
* @param string $dir Base directory on which temp folder is created
* @param string $prefix Prefix of temp directory
* @param integer $mode Access mode of temp directory
* @return string Full path of created temp directory, or FALSE on failure
*/
function create_temp_dir ($dir = '', $prefix = '', $mode = 0700) /* {{{ */
{
if ( !is_dir ($dir) || !is_writable ($dir) )
$dir = sys_get_temp_dir ();
if (!empty ($dir)) $dir = trailingslashit ($dir);
// Not secure indeed. But PHP doesn't provide facility to create temp folder anyway.
$chars = str_split ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
$i = 0;
do {
$path = $dir . $prefix . sprintf ("%s%s%s%s%s%s",
$chars[mt_rand(0,51)], $chars[mt_rand(0,51)], $chars[mt_rand(0,51)],
$chars[mt_rand(0,51)], $chars[mt_rand(0,51)], $chars[mt_rand(0,51)]);
}
while (!@mkdir ($path, $mode) && (++$i < 100));
return ($i < 100) ? $path : FALSE;
} /* }}} */
/**
* Search for executables in system and return full path of its location
*
* It will try to search in PATH envioronment variable first; after failing that,
* use glob() to search for all paths supplied by user, and finally some more
* guess work among common apps locations.
*
* @since 0.3.50
* @uses is_windows()
*
* @param string $prog The program name to be searched
* @param array|string $extra_win_paths Array containing paths to be searched,
* also accepts a %PATH%-like string
* @param array|string $extra_unix_paths Similar to previous argument, but
* conforms to unix $PATH style if specified as string
*
* @return string|boolean Full path of program if it is found, FALSE otherwise
*/
function search_prog ($binary_name, $extra_win_paths = array(), $extra_unix_paths = array()) /* {{{ */
{
if ( is_windows() ) $binary_name .= '.exe';
foreach ( explode( PATH_SEPARATOR, getenv('PATH')) as $dir )
{
$fullpath = realpath ($dir . DIRECTORY_SEPARATOR . $binary_name);
if ( is_executable ( $fullpath ) ) return $fullpath;
}
// game over if glob() is disabled
if ( !function_exists ('glob') ) return $fullpath;
if ( is_windows() )
{
if ( is_string ($extra_win_paths) )
$extra_paths = explode ( PATH_SEPARATOR, $extra_win_paths );
elseif ( !is_array ($extra_win_paths) )
$extra_paths = array();
$extra_paths[] = "C:\\Program Files";
$extra_paths[] = "C:\\Program Files\\*";
$extra_paths[] = "C:\\cygwin*\\bin";
}
else
{
if ( is_string ($extra_unix_paths) )
$extra_paths = explode ( PATH_SEPARATOR, $extra_unix_paths );
elseif ( !is_array ($extra_unix_paths) )
$extra_paths = array();
$extra_paths[] = "/usr/local/bin";
$extra_paths[] = "/opt/bin";
$extra_paths[] = "/opt/*/bin";
}
foreach ( array_values ($extra_paths) as $dir )
{
$fullpath = glob ($dir . DIRECTORY_SEPARATOR . $binary_name);
if ( !empty ($fullpath) )
{
foreach ($fullpath as $prog)
{
$prog = realpath ($prog);
if ( is_executable ($prog) ) return $prog;
}
}
}
return false;
} /* }}} */
/**
* Transform all path related options in ScoreRender settings
*
* @since 0.3
* @uses get_path_presentation()
* @uses scorerender_get_def_settings()
* @param array $setting The settings to be transformed, either from
* existing setting or from newly submitted setting
* @param boolean $is_internal Whether to always transform into Unix format,
* which is used for storing values into database.
* FALSE means using OS native representation.
*/
function transform_paths (&$setting, $is_internal) /* {{{ */
{
if (!is_array ($setting)) return;
$default_settings = scorerender_get_def_settings(SrNotationBase::TYPES_ONLY);
// Transform path and program settings to unix presentation
foreach ($default_settings as $key => $type)
if ( in_array ( $type, array ('path', 'prog', 'midiprog') ) && isset( $setting[$key] ) )
$setting[$key] = get_path_presentation ($setting[$key], $is_internal);
} /* }}} */
/**
* Convert linebreak to appropriate one depending on OS
*
* @since 0.3.50
* @uses is_windows()
* @param string $content Original content to be converted
* @return string Content with appropriate linebreak
*/
function normalize_linebreak ($content) /* {{{ */
{
if ( is_windows() )
{
$content = preg_replace ("/(?<!\r)\n/", "\r\n", $content);
$content = preg_replace ("/\r(?!\n)/", "\r\n", $content);
}
else
{
$content = preg_replace ("/\r\n/", "\n", $content);
$content = preg_replace ("/\r/", "\n", $content);
}
return $content;
} /* }}} */
/* vim: set cindent foldmethod=marker : */
?>