PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file trax.php

Documentation is available at trax.php

  1. <?php
  2. /**
  3. * Create Trax application work area
  4. *
  5. * (PHP 5)
  6. *
  7. * @package PHPonTrax
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @copyright (c) Walter O. Haas 2006
  10. * @version $Id$
  11. * @author Walt Haas <haas@xmission.com>
  12. */
  13.  
  14. /**
  15. * Define where to find files to copy to the work area
  16. *
  17. * Set automatically by the Pear installer when you install Trax with
  18. * the <b>pear install</b> command. If you are prevented from using
  19. * <b>pear install</b>, change "@DATA-DIR@/PHPonTrax" by hand to the
  20. * full filesystem path of the location where you installed the Trax
  21. * distribution
  22. */
  23. define("SOURCE_DIR", "@DATA-DIR@/PHPonTrax/data/");
  24.  
  25. /**
  26. * Symbol substitution tables
  27. *
  28. * $search and $replace below are used to perform substitutions of
  29. * symbols in a file being copied. $search is an array of
  30. * Perl-compatible regular expressions, and $replace is a congruent
  31. * array of replacements for RE matches. So everywhere that the RE
  32. * in, for example, $search[3] is matched in a file, the matching
  33. * string is replaced by the contents of $replace[3].
  34. */
  35. $search = array(
  36. '/@TRAX-CONFIG@/' // symbol for the full filesystem path
  37. // to the Trax config/ directory in
  38. // the user's work area
  39. );
  40.  
  41. $replace = array(
  42. '' // actual value of the full filesystem
  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;
  51.  
  52. // Get command line argument, if any
  53. if (!array_key_exists('argc',$GLOBALS)
  54. || ($GLOBALS['argc'] < 2)) {
  55. usage(); // print Usage message and exit
  56. }
  57.  
  58. // Check for excess arguments
  59. if ($GLOBALS['argc'] > 2) {
  60. echo "unrecognized command argument ".$GLOBALS['argv'][2]."\n";
  61. usage();
  62. }
  63.  
  64. // Destination directory on command line
  65. $dstdir = $GLOBALS['argv'][1];
  66.  
  67. // Guarantee it ends with DIRECTORY_SEPARATOR
  68. if (substr($dstdir,-1,1) != DIRECTORY_SEPARATOR) {
  69. $dstdir .= DIRECTORY_SEPARATOR;
  70. }
  71. if (!create_dir($dstdir)) {
  72. return;
  73. }
  74.  
  75. // Assign real values for symbol substitution
  76. $replace[0] = realpath($dstdir).'/config'; // actual value of
  77. // the full filesystem path to the
  78. // Trax config/ directory in the
  79. // user's work area
  80.  
  81. $srcdir = SOURCE_DIR;
  82. // copy source directory to destination directory
  83. copy_dir($srcdir,$dstdir);
  84. }
  85.  
  86. /**
  87. * Copy a directory with all its contents
  88. *
  89. * When a file whose filename ends '.log' is created, its permissions
  90. * are set to be world writable.
  91. * @param string $src_path Path to source directory
  92. * @param string $dst_path Path to destination directory
  93. * @return boolean true=>success, false=>failure.
  94. */
  95. function copy_dir($src_path,$dst_path) {
  96.  
  97. // Make sure we have directories as arguments
  98. if (!is_dir($src_path)) {
  99. echo $src_path." is not a directory\n";
  100. return false;
  101. }
  102. if (!is_dir($dst_path)) {
  103. echo $dst_path." is not a directory\n";
  104. return false;
  105. }
  106.  
  107. // Open the source directory
  108. $src_handle = opendir($src_path);
  109. if (!$src_handle) {
  110. echo "unable to open $src_path\n";
  111. return false;
  112. }
  113.  
  114. // Copy contents of source directory
  115. while (false !== ($src_file = readdir($src_handle))) {
  116. if (!is_dir($src_path . $src_file)) {
  117.  
  118. // If this file exists only to make the directory
  119. // non-empty so that PackageFileManager will add it to
  120. // the installable package, don't bother to copy it.
  121. if ($src_file == '.delete_this_file') {
  122. continue;
  123. }
  124.  
  125. // This is a regular file, need to copy it
  126. if (file_exists( $dst_path . $src_file )) {
  127.  
  128. // A destination file or directory with this name exists
  129. if (is_file( $dst_path . $src_file )) {
  130.  
  131. // A regular destination file with this name exists.
  132. // Check whether it's different from source.
  133. $src_content = file_get_contents($src_path . $src_file);
  134. $dst_content = file_get_contents($dst_path . $src_file);
  135. if ($src_content == $dst_content) {
  136. // Source and destination are identical
  137. echo "$dst_path$src_file exists\n";
  138. continue;
  139. }
  140. }
  141.  
  142. // New and old files differ. Save the old file.
  143. $stat = stat($dst_path.$src_file);
  144. $new_name = $dst_path.$src_file.'.'.$stat[9];
  145. if (!rename($dst_path.$src_file,$new_name)) {
  146. echo "unable to rename $dst_path$src_file to $new_name\n";
  147. return false;
  148. }
  149. echo "renamed $dst_path$src_file to $new_name\n";
  150. }
  151.  
  152. // Destination file does not exist. Create it
  153. if (!copy_file($src_path . $src_file, $dst_path . $src_file)) {
  154. return false;
  155. }
  156.  
  157. // Log files need to be world writeable
  158. if (substr($src_file,-4,4) == '.log') {
  159. chmod($dst_path . $src_file, 0666);
  160. }
  161. echo "$dst_path$src_file created\n";
  162. } else {
  163.  
  164. // This is a directory. Ignore '.' and '..'
  165. if ( ($src_file == '.') || ($src_file == '..') ) {
  166. continue;
  167. }
  168. // This directory needs to be copied.
  169. if (!create_dir( $dst_path . $src_file )) {
  170. return false;
  171. }
  172.  
  173. // Recursive call to copy directory
  174. if (!copy_dir($src_path . $src_file . DIRECTORY_SEPARATOR,
  175. $dst_path . $src_file . DIRECTORY_SEPARATOR)) {
  176. return false;
  177. }
  178. }
  179. }
  180. closedir($src_handle);
  181. return true;
  182. } // function copy_dir()
  183.  
  184. /**
  185. * Create a directory if it doesn't exist
  186. * @param string $dst_dir Path of directory to create
  187. * @return boolean true=>success, false=>failed
  188. */
  189. function create_dir($dst_dir) {
  190.  
  191. // Does a directory of this name exist?
  192. if (file_exists( $dst_dir )) {
  193.  
  194. // A destination file or directory with this name exists
  195. if (is_dir( $dst_dir )) {
  196.  
  197. // A destination directory with this name exists.
  198. echo "$dst_dir".DIRECTORY_SEPARATOR." exists\n";
  199. return true;
  200. }
  201.  
  202. // There is an old destination file with the same
  203. // name as the new destination directory.
  204. // Save the old file.
  205. $stat = stat($dst_dir);
  206. $new_name = $dst_dir.'.'.$stat[9];
  207. if (!rename($dst_dir,$new_name)) {
  208. echo "unable to rename $dst_dir to $new_name\n";
  209. return false;
  210. }
  211. echo "renamed $dst_dir to $new_name\n";
  212. }
  213.  
  214. // Destination directory does not exist. Create it
  215. if (!mkdir($dst_dir,0775,true)) {
  216. return false;
  217. }
  218. echo "$dst_dir".DIRECTORY_SEPARATOR." created\n";
  219. return true;
  220. }
  221.  
  222. /**
  223. * Copy a Trax file into user's work area, substituting @TRAX-...@
  224. *
  225. * @param string $src_path Path to source file
  226. * @param string $dst_path Path to destination file
  227. * @return boolean true=>success, false=>failure.
  228. */
  229. function copy_file($src_path, $dst_path) {
  230.  
  231. global $search, $replace;
  232.  
  233. // Read source file into a string
  234. if (!$file = file_get_contents($src_path)) {
  235. return false;
  236. }
  237.  
  238. // Substitute @TRAX-...@ symbols for appropriate values
  239. $file = preg_replace($search, $replace, $file);
  240.  
  241. // Write out file contents
  242. @file_put_contents($dst_path, $file);
  243. return true;
  244. }
  245.  
  246. /**
  247. * Output a Usage message and exit
  248. */
  249. function usage() {
  250. echo "Usage: @BIN-DIR@".DIRECTORY_SEPARATOR."trax"
  251. ." ".DIRECTORY_SEPARATOR."path".DIRECTORY_SEPARATOR."to"
  252. .DIRECTORY_SEPARATOR."your".DIRECTORY_SEPARATOR."app
  253.  
  254. Description:
  255. The 'trax' command creates a new Trax application with a default
  256. directory structure and configuration at the path you specify.
  257.  
  258. Example:
  259. trax ".DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www"
  260. .DIRECTORY_SEPARATOR."html
  261.  
  262. This generates a skeletal Trax installation in "
  263. .DIRECTORY_SEPARATOR."var".DIRECTORY_SEPARATOR."www"
  264. .DIRECTORY_SEPARATOR."html.
  265. See the README in the newly created application to get going.
  266. \n";
  267. exit;
  268. }
  269.  
  270. /**
  271. * Main program
  272. */
  273. trax();
  274.  
  275. // -- set Emacs parameters --
  276. // Local variables:
  277. // mode: php
  278. // tab-width: 4
  279. // c-basic-offset: 4
  280. // c-hanging-comment-ender-p: nil
  281. // indent-tabs-mode: nil
  282. // End:
  283.  
  284. ?>

Documentation generated on Thu, 04 May 2006 19:47:56 -0600 by phpDocumentor 1.3.0RC4