/trunk/registry/objects/files.class.php
PHP | 229 lines | 142 code | 46 blank | 41 comment | 22 complexity | 96e7feac9c666d6e79ba0536fa81f939 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- /**
- * Various file functions
- *
- *
- *
- */
- class Files {
-
- var $use_password = false;
- var $htuser;
- var $htpassword;
-
- function Files()
- {
- //
- }
-
- function create_folder( $name, $location )
- {
- //
- }
-
- function remove_folder( $path )
- {
-
- }
-
- /*
- * @broken
- */
- function getFolders( $path )
- {
- $handle = opendir( $path );
- $folders = array();
- while ( false !== ( $file = readdir( $handle ) ) )
- {
- if ( is_dir( $file ) )
- {
- $folders[] = $file;
- }
- }
- closedir($handle);
- return $folders;
-
- }
-
- function getFiles( $path, $subpath='', $traverseDirectories=false )
- {
- // Open a known directory, and proceed to read its contents
- $readPath = $path . $subpath;
- $files = array();
- if( is_dir( $readPath ) )
- {
- if ( $dh = opendir( $readPath ) )
- {
- while ( ($file = readdir($dh)) !== false )
- {
- if ( $file != "." && $file != ".." && $file != ".svn" )
- {
- $lowerdir = $path . $subpath . '/' . $file;
- $changed = date ("F d Y H:i:s.", filemtime( $readPath ) );
- if( ! is_dir( $lowerdir ) )
- {
- $files[] = array( 'name' => $file, 'type' => 'file', 'changed' => $changed );
- }
- elseif( $traverseDirectories == true )
- {
- $subfiles = $this->getFiles( $path, $subpath . $file, true );
- $files[] = array( 'name' => $file, 'type' => 'folder', 'files' => $subfiles, 'path' => $subpath . '/' . $file, 'changed' => $changed );
-
- }
- else
- {
- $files[] = array( 'name' => $file, 'type' => 'folder', 'changed' => $changed );
- }
-
- }
- }
- closedir($dh);
- }
- }
- return $files;
- }
-
- function remove_file( $path )
- {
- //
- }
-
- /**
- * This grabs the contents of a file
- * @param String file path / location / URL
- * @return void
- */
- function grab_contents( $file )
- {
- $file = str_replace( '&', '&', $file );
-
- if( strpos($file, 'http://') === false && strpos($file, 'https://') === false )
- {
- return $this->open_file( $file );
- }
- else
- {
- return $this->grab_contents_fopen($file);
-
- }
- }
-
- /**
- * Sets if we will use .htaccess authentication for remote file access
- * @param boolean use - if we will use the authentication or not
- * @param String username
- * @param String password
- */
- function setuse_password( $use, $user, $password )
- {
- if ( $use )
- {
- $this->use_password = true;
- $this->htuser = $user;
- $this->htpassword = $password;
- }
- else
- {
- $this->use_password = false;
- }
- }
-
- /**
- * Grabs file contents using fopen
- * @param String file path
- * @return String the file contents
- */
- function grab_contents_fopen( $file )
- {
- // important if we are opening the same file a few times
- if ( ! @clearstatcache() )
- {
- // log error message
- }
-
- // clear it
- $temp = "";
-
- if ( $open_file = @fopen( $file, 'r') )
- {
- // while we are not at the end of file
- while ( ! feof( $open_file ) )
- {
- $temp .= fgets($open_file, 4096);
- }
-
- fclose($open_file);
- }
-
- return $temp;
- }
-
- /**
- * Grabs file cotents using sockets
- * @param String file path
- * @return String the file contents
- */
- function grabContentsSocket( $file )
- {
-
- // url checking
-
- $parsed = parse_url($file);
-
- $port = ( isset($parsed['port']) ) ? $parsed['port'] : 80;
-
- $path = ( empty($parsed['path']) ) ? '/' : $parsed['path'];
-
- $path .= ( empty($parsed['query']) ) ? '' : '?'.$parsed['query'];
-
- if ( !$sock = @fsockopen( $parsed['host'], $port, $error_number, $error_message ) )
- {
- PeacockCarterFrameworkRegistry::errorPage('unable to open file ' . $file);
- }
- else
- {
-
- $q = "";
- $q .= "GET " . $path . " HTTP/1.1\r\n";
- $q .= "Host: " . $parsed['host'] . "\r\n";
-
- if ( $this->use_password )
- {
- $q .= "Authorization: Basic " . base64_encode($this->htuser.':'.$this->htpassword) . "\r\n\r\n";
- }
- else
- {
- $q .= "\r\n";
- }
-
- // send it!
- fputs($sock,$q);
- $temp = "";
- while ( ! feof( $sock ) )
- {
- $temp .= fgets($sock, 4096);
- }
-
- $temp = explode("\r\n\r\n", $temp, 2);
-
-
- // close it
- fclose($sock);
-
- return $temp[1];
-
- }
- }
-
-
- }
- ?>