File Coverage

blib/lib/PHP/Functions/File.pm
Criterion Covered Total %
statement 39 110 35.4
branch 0 40 0.0
condition 0 3 0.0
subroutine 13 16 81.2
pod 0 3 0.0
total 52 172 30.2


line stmt bran cond sub pod time code
1 1     1   36271 use constant FILE_USE_INCLUDE_PATH => 1;
  1         3  
  1         99  
2 1     1   7 use constant LOCK_EX => 2;
  1         2  
  1         50  
3 1     1   5 use constant FILE_APPEND => 8;
  1         6  
  1         572  
4            
5             package PHP::Functions::File;
6            
7 1     1   9 use strict;
  1         2  
  1         1112  
8 1     1   10 use warnings;
  1         2  
  1         180  
9            
10 1     1   7 use vars qw(@ISA @EXPORT_OK $VERSION);
  1         3  
  1         150  
11            
12             require Exporter;
13             @ISA = qw(Exporter);
14             @EXPORT_OK = qw(file_get_contents file_put_contents);
15             $VERSION = '0.04';
16            
17 1     1   5 use Carp qw(carp croak);
  1         2  
  1         725  
18            
19             sub file_get_contents {
20 0     0 0   my ($filename, $use_include_path, $context, $offset, $maxlen) = @_;
21            
22 0 0         if(!defined($filename)) {
23 0           return return_warn("first arg is required");
24             }
25            
26 0 0         $use_include_path = defined($use_include_path) ? $use_include_path : 0;
27 0 0         $offset = defined($offset) ? $offset : -1;
28            
29 0           my $protocol = "default";
30 0 0         if($filename =~ /^([^:]+):\/\/(.+)/) {
31 0           $protocol = lc $1;
32 0           $filename = $2;
33             }
34            
35 0           my $return;
36            
37 0 0         if($protocol eq "file") {
38 0 0         if($use_include_path) {
39 1     1   10 use File::Spec;
  1         2  
  1         213  
40 0           my $filepath;
41 0           foreach my $ip (@INC) {
42 0           $filepath = File::Spec->catfile($ip, $filename);
43 0           my $break = 0;
44 0 0         if( -e $filepath)
45             {
46 0           $filename = $filepath;
47 0           last;
48             }
49             }
50             }
51 0           $protocol = "default";
52             }
53            
54 0 0 0       if($protocol eq "default") {
    0          
    0          
55 0           open(IN, $filename);
56 0           while()
57             {
58 0           $return .= $_;
59             }
60 0           close(IN);
61            
62 0           return $return;
63             }
64             elsif($protocol eq "http" || $protocol eq "https" ) {
65 1     1   3794 use LWP::UserAgent;
  1         119135  
  1         44  
66 1     1   12 use HTTP::Request;
  1         1  
  1         26  
67 1     1   5 use HTTP::Response;
  1         3  
  1         208  
68            
69 0           my $proxy = new LWP::UserAgent;
70 0           my $req = HTTP::Request->new('GET', $protocol . "://". $filename);
71 0           my $res = $proxy->request($req);
72            
73 0 0         if($res->is_success) {
74 0           return $res->content;
75             }
76             else {
77 0           return return_warn("file_get_contents(" . $res->base . "): failed to open stream: HTTP request failed! " . $res->code . " " . $res->message);
78             }
79             }
80             elsif ($protocol eq "zlib") {
81             #@todo using gzip is firstest way. use gzip with pipe in case gzip is supported on OS.
82 1     1   1141 use Compress::Zlib;
  1         171365  
  1         625  
83            
84 0           my $line;
85            
86 0 0         my $gz = gzopen($filename, "rb") or return return_warn("Cannot open $filename: $!");
87 0           while ($gz->gzreadline($line)) {
88 0           $return .= $line;
89             }
90 0           $gz->gzclose;
91            
92 0           return $return;
93             }
94             =cut_start
95             elsif ($protocol eq "ftp" || $protocol eq "ftps") {
96             use IO::Socket;
97            
98             my $username = "anonymous";
99             my $password = "anonymous";
100             my $hostname;
101            
102             if($filename =~ /^([^:]+):([^@]+)@([^\/]+)(.+)/) {
103             $username = $1;
104             $password = $2;
105             $hostname = $3;
106             $filename = $4;
107             }
108            
109             my $socket;
110            
111             #@todo error handling
112             if($protocol eq "ftp") {
113             $socket = IO::Socket::INET->new(PeerAddr => $hostname,
114             PeerPort => 20,
115             Proto => 'tcp',
116             );
117             }
118             #@todo ftps support. IO::Socket::SSL
119            
120             if(!$socket) {
121             return return_warn("cannot open socket for ftp.");
122             }
123             print $socket "USER " . $username;
124             print $socket "PASS " . $password;
125             print $socket "SYST";
126             #@todo TYPE, SIZE , PORT / PASV, RETR, MDTM
127             print $socket "QUIT";
128             $socket->flush();
129             $socket->close();
130             }
131             =cut
132             else {
133 0           return return_warn("Protocol not supported");
134             }
135             }
136            
137             sub file_put_contents {
138 0     0 0   my ($filename, $data, $flags, $context) = @_;
139            
140 0 0         if(!defined($filename)) {
141 0           return return_warn("first arg is required");
142             }
143            
144 0           my $use_include_path;
145             my $file_append;
146 0           my $lock_ex;
147            
148 0           my $protocol = "default";
149 0 0         if($filename =~ /^([^:]+):\/\/(.+)/) {
150 0           $protocol = lc $1;
151 0           $filename = $2;
152             }
153            
154 0 0         if($protocol eq "file") {
155 0 0         if($use_include_path) {
156 1     1   11 use File::Spec;
  1         2  
  1         315  
157 0           my $filepath;
158 0           foreach my $ip (@INC) {
159 0           $filepath = File::Spec->catfile($ip, $filename);
160 0           my $break = 0;
161 0 0         if( -e $filepath)
162             {
163 0           $filename = $filepath;
164 0           last;
165             }
166             }
167             }
168 0           $protocol = "default";
169             }
170            
171 0 0         if($protocol eq "default") {
    0          
172 0           open(OUT, ">".$filename);
173 0           print OUT $data;
174 0           close(IN);
175 0           return -s $filename;
176             }
177             elsif ($protocol eq "zlib") {
178             #@todo using gzip is firstest way. use gzip with pipe in case gzip is supported on OS.
179 0 0         my $gz = gzopen($filename, "wb") or return return_warn("Cannot open $filename: $!");
180            
181 0           $gz->gzwrite($data);
182 0           $gz->gzclose;
183            
184 0           return 1;
185             }
186             =cut_start
187             elsif ($protocol eq "ftp" || $protocol eq "ftps") {
188             #@todo ftp/ftps support
189             }
190             =cut
191             else {
192 0           return return_warn("Protocol not supported");
193             }
194             }
195            
196             sub return_warn {
197 0     0 0   my ($message) = @_;
198 0           warn $message . "\n";
199 0           return 0;
200             }
201            
202             1;
203             __END__