blib/lib/PHP/Functions/File.pm

filter: Statementstmt 38.2% 44 / 115 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.2% 13 / 16 Podpod 0.0% 0 / 3 totaltotal 31.1% 57 / 183 CC 26 SCAR 45.7
CC
26
Cov
27%
file_get_contents
178.4
file_put_contents
92.4
return_warn
2.0
CRAP 96.9
↑ Summary
1 1 ▶use constant FILE_USE_INCLUDE_PATH => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use constant LOCK_EX => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use constant FILE_APPEND => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 package PHP::Functions::File;
6
7 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 1 ▶ use vars qw(@ISA @EXPORT_OK $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 1 require Exporter;
13 1 @ISA = qw(Exporter);
14 1 @EXPORT_OK = qw(file_get_contents file_put_contents);
15 1 $VERSION = '0.04';
16
17 1 ▶ use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 0 ▶ sub file_get_contents {
Subroutine file_get_contents: not called CC=17 SCAR=51.8
Pod undocumented
20 0 my ($filename, $use_include_path, $context, $offset, $maxlen) = @_;
21
22 0 ▶ if(!defined($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 0 0
23 0 return return_warn("first arg is required");
24 }
25
26 0 ▶ $use_include_path = defined($use_include_path) ? $use_include_path : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $use_include_path ? : 0 0 0
27 0 ▶ $offset = defined($offset) ? $offset : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $offset ? : 0 0 0
28
29 0 my $protocol = "default";
30 0 ▶ if($filename =~ /^([^:]+):\/\/(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[^([^:]+)://(.+)]) 0 0 0
31 0 $protocol = lc $1;
32 0 $filename = $2;
33 }
34
35 0 my $return;
36
37 0 ▶ if($protocol eq "file") {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq "file") 0 0 0
38 0 ▶ if($use_include_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_include_path) 0 0 0
39 1 ▶ use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ if( -e $filepath)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filepath) 0 0 0
45 {
46 0 $filename = $filepath;
47 0 last;
48 }
49 }
50 }
51 0 $protocol = "default";
52 }
53
54 0 ▶ if($protocol eq "default") {
Branch: 3 branches
BranchTrueFalseTotal
if ($protocol eq 'default') { } 0 0 0
elsif ($protocol eq 'http' or $protocol eq 'https') { } 0 0 0
elsif ($protocol eq 'zlib') { } 0 0 0
55 0 open(IN, $filename);
56 0 ▶ while(<IN>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
57 {
58 0 $return .= $_;
59 }
60 0 close(IN);
61
62 0 return $return;
63 }
64 ▶ elsif($protocol eq "http" || $protocol eq "https" ) {
Condition0%0/3
$protocol eq "http" or $protocol eq "https"
l!l&&r!l&&!r
000
MC/DC0%0/2
$protocol eq "http" or $protocol eq "https"
$protocol eq "http" $protocol eq "https"
Truth table0%0/3
A or B
A= $protocol eq "http"B= $protocol eq "https"
ABresult
1X1
011
000
65 1 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶ use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶ use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ if($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
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 ▶ use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
83
84 0 my $line;
85
86 0 ▶ my $gz = gzopen($filename, "rb") or return return_warn("Cannot open $filename: $!");
Branch: 1 branch
BranchTrueFalseTotal
my $gz = &gzopen($filename, "rb") or return return_warn("Cannot open ${filename}: $!") 0 0 0
87 0 ▶ while ($gz->gzreadline($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gz->gzreadline($line)) 0 0 0
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 0 ▶ sub file_put_contents {
Subroutine file_put_contents: not called CC=10 SCAR=45.3
Pod undocumented
138 0 my ($filename, $data, $flags, $context) = @_;
139
140 0 ▶ if(!defined($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $filename) 0 0 0
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 ▶ if($filename =~ /^([^:]+):\/\/(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[^([^:]+)://(.+)]) 0 0 0
150 0 $protocol = lc $1;
151 0 $filename = $2;
152 }
153
154 0 ▶ if($protocol eq "file") {
Branch: 1 branch
BranchTrueFalseTotal
if ($protocol eq "file") 0 0 0
155 0 ▶ if($use_include_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_include_path) 0 0 0
156 1 ▶ use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶ if( -e $filepath)
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filepath) 0 0 0
162 {
163 0 $filename = $filepath;
164 0 last;
165 }
166 }
167 }
168 0 $protocol = "default";
169 }
170
171 0 ▶ if($protocol eq "default") {
Branch: 2 branches
BranchTrueFalseTotal
if ($protocol eq 'default') { } 0 0 0
elsif ($protocol eq 'zlib') { } 0 0 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 ▶ my $gz = gzopen($filename, "wb") or return return_warn("Cannot open $filename: $!");
Branch: 1 branch
BranchTrueFalseTotal
my $gz = &gzopen($filename, "wb") or return return_warn("Cannot open ${filename}: $!") 0 0 0
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 0 ▶ sub return_warn {
Subroutine return_warn: not called CC=1 SCAR=6.9
Pod undocumented
197 0 my ($message) = @_;
198 0 warn $message . "\n";
199 0 return 0;
200 }
201
202 1 1;
203 __END__
↑ Summary