PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/registry/objects/files.class.php

https://bitbucket.org/pooshonk/esw
PHP | 229 lines | 142 code | 46 blank | 41 comment | 22 complexity | 96e7feac9c666d6e79ba0536fa81f939 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Various file functions
  4. *
  5. *
  6. *
  7. */
  8. class Files {
  9. var $use_password = false;
  10. var $htuser;
  11. var $htpassword;
  12. function Files()
  13. {
  14. //
  15. }
  16. function create_folder( $name, $location )
  17. {
  18. //
  19. }
  20. function remove_folder( $path )
  21. {
  22. }
  23. /*
  24. * @broken
  25. */
  26. function getFolders( $path )
  27. {
  28. $handle = opendir( $path );
  29. $folders = array();
  30. while ( false !== ( $file = readdir( $handle ) ) )
  31. {
  32. if ( is_dir( $file ) )
  33. {
  34. $folders[] = $file;
  35. }
  36. }
  37. closedir($handle);
  38. return $folders;
  39. }
  40. function getFiles( $path, $subpath='', $traverseDirectories=false )
  41. {
  42. // Open a known directory, and proceed to read its contents
  43. $readPath = $path . $subpath;
  44. $files = array();
  45. if( is_dir( $readPath ) )
  46. {
  47. if ( $dh = opendir( $readPath ) )
  48. {
  49. while ( ($file = readdir($dh)) !== false )
  50. {
  51. if ( $file != "." && $file != ".." && $file != ".svn" )
  52. {
  53. $lowerdir = $path . $subpath . '/' . $file;
  54. $changed = date ("F d Y H:i:s.", filemtime( $readPath ) );
  55. if( ! is_dir( $lowerdir ) )
  56. {
  57. $files[] = array( 'name' => $file, 'type' => 'file', 'changed' => $changed );
  58. }
  59. elseif( $traverseDirectories == true )
  60. {
  61. $subfiles = $this->getFiles( $path, $subpath . $file, true );
  62. $files[] = array( 'name' => $file, 'type' => 'folder', 'files' => $subfiles, 'path' => $subpath . '/' . $file, 'changed' => $changed );
  63. }
  64. else
  65. {
  66. $files[] = array( 'name' => $file, 'type' => 'folder', 'changed' => $changed );
  67. }
  68. }
  69. }
  70. closedir($dh);
  71. }
  72. }
  73. return $files;
  74. }
  75. function remove_file( $path )
  76. {
  77. //
  78. }
  79. /**
  80. * This grabs the contents of a file
  81. * @param String file path / location / URL
  82. * @return void
  83. */
  84. function grab_contents( $file )
  85. {
  86. $file = str_replace( '&amp;', '&', $file );
  87. if( strpos($file, 'http://') === false && strpos($file, 'https://') === false )
  88. {
  89. return $this->open_file( $file );
  90. }
  91. else
  92. {
  93. return $this->grab_contents_fopen($file);
  94. }
  95. }
  96. /**
  97. * Sets if we will use .htaccess authentication for remote file access
  98. * @param boolean use - if we will use the authentication or not
  99. * @param String username
  100. * @param String password
  101. */
  102. function setuse_password( $use, $user, $password )
  103. {
  104. if ( $use )
  105. {
  106. $this->use_password = true;
  107. $this->htuser = $user;
  108. $this->htpassword = $password;
  109. }
  110. else
  111. {
  112. $this->use_password = false;
  113. }
  114. }
  115. /**
  116. * Grabs file contents using fopen
  117. * @param String file path
  118. * @return String the file contents
  119. */
  120. function grab_contents_fopen( $file )
  121. {
  122. // important if we are opening the same file a few times
  123. if ( ! @clearstatcache() )
  124. {
  125. // log error message
  126. }
  127. // clear it
  128. $temp = "";
  129. if ( $open_file = @fopen( $file, 'r') )
  130. {
  131. // while we are not at the end of file
  132. while ( ! feof( $open_file ) )
  133. {
  134. $temp .= fgets($open_file, 4096);
  135. }
  136. fclose($open_file);
  137. }
  138. return $temp;
  139. }
  140. /**
  141. * Grabs file cotents using sockets
  142. * @param String file path
  143. * @return String the file contents
  144. */
  145. function grabContentsSocket( $file )
  146. {
  147. // url checking
  148. $parsed = parse_url($file);
  149. $port = ( isset($parsed['port']) ) ? $parsed['port'] : 80;
  150. $path = ( empty($parsed['path']) ) ? '/' : $parsed['path'];
  151. $path .= ( empty($parsed['query']) ) ? '' : '?'.$parsed['query'];
  152. if ( !$sock = @fsockopen( $parsed['host'], $port, $error_number, $error_message ) )
  153. {
  154. PeacockCarterFrameworkRegistry::errorPage('unable to open file ' . $file);
  155. }
  156. else
  157. {
  158. $q = "";
  159. $q .= "GET " . $path . " HTTP/1.1\r\n";
  160. $q .= "Host: " . $parsed['host'] . "\r\n";
  161. if ( $this->use_password )
  162. {
  163. $q .= "Authorization: Basic " . base64_encode($this->htuser.':'.$this->htpassword) . "\r\n\r\n";
  164. }
  165. else
  166. {
  167. $q .= "\r\n";
  168. }
  169. // send it!
  170. fputs($sock,$q);
  171. $temp = "";
  172. while ( ! feof( $sock ) )
  173. {
  174. $temp .= fgets($sock, 4096);
  175. }
  176. $temp = explode("\r\n\r\n", $temp, 2);
  177. // close it
  178. fclose($sock);
  179. return $temp[1];
  180. }
  181. }
  182. }
  183. ?>