| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
define("SOURCE_DIR", "@DATA-DIR@/PHPonTrax/data/"); |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
$search = array( |
|---|
| 36 |
'/@TRAX-CONFIG@/' |
|---|
| 37 |
// to the Trax config/ directory in |
|---|
| 38 |
// the user's work area |
|---|
| 39 |
); |
|---|
| 40 |
|
|---|
| 41 |
$replace = array( |
|---|
| 42 |
'' |
|---|
| 43 |
// path to the Trax config/ directory |
|---|
| 44 |
// in the user's work area |
|---|
| 45 |
); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
function trax() { |
|---|
| 49 |
|
|---|
| 50 |
global $search, $replace, $quiet; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
if (!array_key_exists('argc',$GLOBALS) || ($GLOBALS['argc'] < 2)) { |
|---|
| 54 |
usage(); |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
$first_param_is_option = (substr($GLOBALS['argv'][1], 0, 1) == "-") ? true : false; |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
if ($GLOBALS['argc'] > 3 && !$first_param_is_option) { |
|---|
| 61 |
echo "unrecognized command argument ".$GLOBALS['argv'][2]."\n"; |
|---|
| 62 |
usage(); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
if($first_param_is_option) { |
|---|
| 66 |
foreach($GLOBALS['argv'] as $arg) { |
|---|
| 67 |
if($arg == '-v' || $arg == '--version') { |
|---|
| 68 |
include(dirname(__FILE__)."/vendor/trax/trax.php"); |
|---|
| 69 |
echo "Trax ".Trax::version()."\n"; |
|---|
| 70 |
exit; |
|---|
| 71 |
} elseif($arg == '-h' || $arg == '--help') { |
|---|
| 72 |
usage(); |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
} else { |
|---|
| 76 |
|
|---|
| 77 |
$dstdir = $GLOBALS['argv'][1]; |
|---|
| 78 |
if($GLOBALS['argv'][2] == '-q' || $GLOBALS['argv'][2] == '--quiet') { |
|---|
| 79 |
$quiet = true; |
|---|
| 80 |
} |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
if (substr($dstdir,-1,1) != DIRECTORY_SEPARATOR) { |
|---|
| 85 |
$dstdir .= DIRECTORY_SEPARATOR; |
|---|
| 86 |
} |
|---|
| 87 |
if (!create_dir($dstdir)) { |
|---|
| 88 |
return; |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
$replace[0] = realpath($dstdir).'/config'; |
|---|
| 93 |
// the full filesystem path to the |
|---|
| 94 |
// Trax config/ directory in the |
|---|
| 95 |
// user's work area |
|---|
| 96 |
|
|---|
| 97 |
$srcdir = SOURCE_DIR; |
|---|
| 98 |
|
|---|
| 99 |
copy_dir($srcdir, $dstdir); |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
function copy_dir($src_path, $dst_path) { |
|---|
| 112 |
|
|---|
| 113 |
global $quiet; |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
if (!is_dir($src_path)) { |
|---|
| 117 |
echo $src_path." is not a directory\n"; |
|---|
| 118 |
return false; |
|---|
| 119 |
} |
|---|
| 120 |
if (!is_dir($dst_path)) { |
|---|
| 121 |
echo $dst_path." is not a directory\n"; |
|---|
| 122 |
return false; |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
$src_handle = opendir($src_path); |
|---|
| 127 |
if (!$src_handle) { |
|---|
| 128 |
echo "unable to open $src_path\n"; |
|---|
| 129 |
return false; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
while (false !== ($src_file = readdir($src_handle))) { |
|---|
| 134 |
if (!is_dir($src_path . $src_file)) { |
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
// non-empty so that PackageFileManager will add it to |
|---|
| 138 |
// the installable package, don't bother to copy it. |
|---|
| 139 |
if ($src_file == '.delete_this_file') { |
|---|
| 140 |
continue; |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
if (file_exists( $dst_path . $src_file )) { |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
if (is_file( $dst_path . $src_file )) { |
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
// Check whether it's different from source. |
|---|
| 151 |
$src_content = file_get_contents($src_path . $src_file); |
|---|
| 152 |
$dst_content = file_get_contents($dst_path . $src_file); |
|---|
| 153 |
if ($src_content == $dst_content) { |
|---|
| 154 |
|
|---|
| 155 |
if(!$quiet) echo "\texists $dst_path$src_file\n"; |
|---|
| 156 |
continue; |
|---|
| 157 |
} |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
$stat = stat($dst_path.$src_file); |
|---|
| 162 |
$new_name = $dst_path.$src_file.'.'.$stat[9]; |
|---|
| 163 |
if (!rename($dst_path.$src_file,$new_name)) { |
|---|
| 164 |
echo "unable to rename $dst_path$src_file to $new_name\n"; |
|---|
| 165 |
return false; |
|---|
| 166 |
} |
|---|
| 167 |
if(!$quiet) echo "\trenamed $src_file to ".($src_file.'.'.$stat[9])."\n"; |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
if (!copy_file($src_path . $src_file, $dst_path . $src_file)) { |
|---|
| 172 |
return false; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
if (substr($src_file,-4,4) == '.log') { |
|---|
| 177 |
chmod($dst_path . $src_file, 0666); |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
if ($src_file == 'generate.php' || $src_file == 'console.php') { |
|---|
| 182 |
chmod($dst_path . $src_file, 0754); |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
if(!$quiet) echo "\tcreate $dst_path$src_file\n"; |
|---|
| 186 |
} else { |
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
if ( ($src_file == '.') || ($src_file == '..') ) { |
|---|
| 190 |
continue; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
if (!create_dir( $dst_path . $src_file )) { |
|---|
| 195 |
return false; |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
if (!copy_dir($src_path . $src_file . DIRECTORY_SEPARATOR, |
|---|
| 200 |
$dst_path . $src_file . DIRECTORY_SEPARATOR)) { |
|---|
| 201 |
return false; |
|---|
| 202 |
} |
|---|
| 203 |
} |
|---|
| 204 |
} |
|---|
| 205 |
closedir($src_handle); |
|---|
| 206 |
return true; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
function create_dir($dst_dir) { |
|---|
| 216 |
|
|---|
| 217 |
global $quiet; |
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
if (file_exists( $dst_dir )) { |
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
if (is_dir( $dst_dir )) { |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
if(!$quiet) echo "\texists $dst_dir\n"; |
|---|
| 227 |
return true; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
// name as the new destination directory. |
|---|
| 232 |
// Save the old file. |
|---|
| 233 |
$stat = stat($dst_dir); |
|---|
| 234 |
$new_name = $dst_dir.'.'.$stat[9]; |
|---|
| 235 |
if (!rename($dst_dir, $new_name)) { |
|---|
| 236 |
echo "unable to rename $dst_dir to $new_name\n"; |
|---|
| 237 |
return false; |
|---|
| 238 |
} |
|---|
| 239 |
if(!$quiet) echo "\trenamed $dst_dir to $new_name\n"; |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
if (!mkdir($dst_dir, 0775, true)) { |
|---|
| 244 |
return false; |
|---|
| 245 |
} |
|---|
| 246 |
if(!$quiet) echo "\tcreate ".$dst_dir."\n"; |
|---|
| 247 |
return true; |
|---|
| 248 |
} |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
function copy_file($src_path, $dst_path) { |
|---|
| 258 |
|
|---|
| 259 |
global $search, $replace; |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
if (!$file = file_get_contents($src_path)) { |
|---|
| 263 |
return false; |
|---|
| 264 |
} |
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
$file = preg_replace($search, $replace, $file); |
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
@file_put_contents($dst_path, $file); |
|---|
| 271 |
return true; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
function usage() { |
|---|
| 278 |
echo "Usage: @BIN-DIR@".DIRECTORY_SEPARATOR."trax" |
|---|
| 279 |
." ".DIRECTORY_SEPARATOR."path".DIRECTORY_SEPARATOR."to" |
|---|
| 280 |
.DIRECTORY_SEPARATOR."your".DIRECTORY_SEPARATOR."app [options] |
|---|
| 281 |
|
|---|
| 282 |
Options: |
|---|
| 283 |
-q, --quiet Suppress normal output. |
|---|
| 284 |
-v, --version Show the Trax version number and quit. |
|---|
| 285 |
-h, --help Show this help message and quit. |
|---|
| 286 |
|
|---|
| 287 |
Description: |
|---|
| 288 |
The 'trax' command creates a new Trax application with a default |
|---|
| 289 |
directory structure and configuration at the path you specify. |
|---|
| 290 |
|
|---|
| 291 |
Example: |
|---|
| 292 |
trax ".DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www" |
|---|
| 293 |
.DIRECTORY_SEPARATOR."html |
|---|
| 294 |
|
|---|
| 295 |
This generates a skeletal Trax installation in " |
|---|
| 296 |
.DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www" |
|---|
| 297 |
.DIRECTORY_SEPARATOR."html. |
|---|
| 298 |
See the README in the newly created application to get going. |
|---|
| 299 |
\n"; |
|---|
| 300 |
exit; |
|---|
| 301 |
} |
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
trax(); |
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
?> |
|---|