| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 27 | 0 | ▶ | $offset = defined($offset) ? $offset : -1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | my $protocol = "default"; | |||||||||||||||||||
| 30 | 0 | ▶ | if($filename =~ /^([^:]+):\/\/(.+)/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 38 | 0 | ▶ | if($use_include_path) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 55 | 0 | open(IN, $filename); | |||||||||||||||||||
| 56 | 0 | ▶ | while(<IN>) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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"
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"
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 87 | 0 | ▶ | while ($gz->gzreadline($line)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 150 | 0 | $protocol = lc $1; | |||||||||||||||||||
| 151 | 0 | $filename = $2; | |||||||||||||||||||
| 152 | } | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | 0 | ▶ | if($protocol eq "file") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 155 | 0 | ▶ | if($use_include_path) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||