| | 143 | sub download_theme { |
|---|
| | 144 | my $app = shift; |
|---|
| | 145 | my ($url) = @_; |
|---|
| | 146 | |
|---|
| | 147 | my $static_path = $app->static_file_path; |
|---|
| | 148 | my $themeroot = File::Spec->catdir($static_path, 'support', 'themes'); |
|---|
| | 149 | my $ua = $app->new_ua(); |
|---|
| | 150 | my $filemgr = file_mgr() |
|---|
| | 151 | or return; |
|---|
| | 152 | |
|---|
| | 153 | # Break up the css url in to a couple useful pieces |
|---|
| | 154 | my @url = split( /\//, $url ); |
|---|
| | 155 | |
|---|
| | 156 | my $theme_url = join(q{/}, @url[0..-2]) . '/'; |
|---|
| | 157 | my ($basename, $extension) = split( /\./, $url[-1] ); |
|---|
| | 158 | if ($basename eq 'screen' || $basename eq 'style') { |
|---|
| | 159 | $basename = $url[-2]; |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | # Pick up the stylesheet |
|---|
| | 163 | my $stylesheet_res = $ua->get($url); |
|---|
| | 164 | |
|---|
| | 165 | # Parse out image filenames in the css and then write out the css file |
|---|
| | 166 | # and thumbnails to our theme folder |
|---|
| | 167 | my $stylesheet = $stylesheet_res->content; |
|---|
| | 168 | $stylesheet =~ s!/\*.*?\*/!!gs; # strip all comments first |
|---|
| | 169 | my @images = $stylesheet =~ m{ |
|---|
| | 170 | \b url\( \s* # opening url() reference |
|---|
| | 171 | ( [\w\.-]+\.(?:gif|jpe?g|png) ) # a filename ending in an image extension |
|---|
| | 172 | \s* \) # close of url() reference |
|---|
| | 173 | }xmsgi; |
|---|
| | 174 | |
|---|
| | 175 | my $theme_path = File::Spec->catdir($themeroot, $basename); |
|---|
| | 176 | if (!$filemgr->mkpath($theme_path)) { |
|---|
| | 177 | my $error = $app->translate("Could not create [_1] folder - Check that your 'themes' folder is webserver-writable.", |
|---|
| | 178 | $basename); |
|---|
| | 179 | return $app->json_error($error); |
|---|
| | 180 | } |
|---|
| | 181 | |
|---|
| | 182 | $filemgr->put_data( $stylesheet_res->content, |
|---|
| | 183 | File::Spec->catfile($theme_path, $basename . '.css') ); |
|---|
| | 184 | |
|---|
| | 185 | # Pick up the images we parsed earlier and write them to the theme folder |
|---|
| | 186 | my @thumbs = ("thumbnail.gif", "thumbnail-large.gif"); |
|---|
| | 187 | IMAGE: for my $rel_url (@thumbs, @images) { |
|---|
| | 188 | # Is this safe to get? |
|---|
| | 189 | my $full_url = URI->new_abs($rel_url, $theme_url); |
|---|
| | 190 | next IMAGE if !$full_url; |
|---|
| | 191 | next IMAGE if $full_url->as_string() !~ m{ \A \Q$theme_url\E }xms; |
|---|
| | 192 | |
|---|
| | 193 | my $res = $ua->get($full_url->as_string()); |
|---|
| | 194 | |
|---|
| | 195 | # Skip images that don't download; we were accidentally doing so already. |
|---|
| | 196 | next IMAGE if !$res->is_success(); |
|---|
| | 197 | |
|---|
| | 198 | my $canon_rel_url = URI->new($rel_url)->rel($theme_url); |
|---|
| | 199 | my @image_path = split /\//, $canon_rel_url->as_string(); |
|---|
| | 200 | my $image_filename = File::Spec->catfile($theme_path, @image_path); |
|---|
| | 201 | $filemgr->put_data($res->content, $image_filename, 'upload') |
|---|
| | 202 | or return $app->json_error( $filemgr->errstr ); |
|---|
| | 203 | } |
|---|
| | 204 | |
|---|
| | 205 | return $basename; |
|---|
| | 206 | } |
|---|
| | 207 | |
|---|
| 175 | | my $filemgr = file_mgr() |
|---|
| 176 | | or return $app->json_error( MT::FileMgr->errstr ); |
|---|
| 177 | | |
|---|
| 178 | | if ( $url !~ m/^(\Q$webthemeroot\E|\Q$mtthemeroot\E)/ ) { |
|---|
| 179 | | my $new_url = ''; |
|---|
| 180 | | |
|---|
| 181 | | for (0..(scalar(@url)-2)) { |
|---|
| 182 | | $new_url .= $url[$_] . '/'; |
|---|
| 183 | | } |
|---|
| 184 | | my ( $basename, $extension ) = split( /\./, $url[-1] ); |
|---|
| 185 | | if ($basename eq 'screen') { |
|---|
| 186 | | $basename = $url[-2]; |
|---|
| 187 | | } |
|---|
| 188 | | |
|---|
| 189 | | # Pick up the stylesheet |
|---|
| 190 | | my $user_agent = $app->new_ua; |
|---|
| 191 | | my $css_request = HTTP::Request->new( GET => $url ); |
|---|
| 192 | | my $response = $user_agent->request($css_request); |
|---|
| 193 | | |
|---|
| 194 | | # Pick up the thumbnail and thumbnail-large |
|---|
| 195 | | my $thumbnail_request = |
|---|
| 196 | | HTTP::Request->new( GET => $new_url . "thumbnail.gif" ); |
|---|
| 197 | | my $thumbnail_response = $user_agent->request($thumbnail_request); |
|---|
| 198 | | my $thumbnail_large_request = |
|---|
| 199 | | HTTP::Request->new( GET => $new_url . "thumbnail-large.gif" ); |
|---|
| 200 | | my $thumbnail_large_response = |
|---|
| 201 | | $user_agent->request($thumbnail_large_request); |
|---|
| 202 | | |
|---|
| 203 | | # Parse out image filenames in the css and then write out the css file |
|---|
| 204 | | # and thumbnails to our theme folder |
|---|
| 205 | | my $content = $response->content; |
|---|
| 206 | | $content =~ s!/\*.*?\*/!!gs; # strip all comments first |
|---|
| 207 | | my @images = $content =~ |
|---|
| 208 | | m/\b(?:url\(\s*)([a-zA-Z0-9_.-]+\.(?:gif|jpe?g|png))(?:\s*?\))/gi; |
|---|
| 209 | | $filemgr->mkpath( File::Spec->catdir( $themeroot, $basename ) ) |
|---|
| 210 | | or return $app->json_error( |
|---|
| 211 | | $app->translate( |
|---|
| 212 | | "Could not create [_1] folder - Check that your 'themes' folder is webserver-writable.", |
|---|
| 213 | | $basename |
|---|
| 214 | | ) |
|---|
| 215 | | ); |
|---|
| 216 | | $filemgr->put_data( $response->content, |
|---|
| 217 | | File::Spec->catfile( $themeroot, $basename, $basename . '.css' ) ); |
|---|
| 218 | | if (($thumbnail_response->code >= 200) && ($thumbnail_response->code < 400)) { |
|---|
| 219 | | $filemgr->put_data( $thumbnail_response->content, |
|---|
| 220 | | File::Spec->catfile( $themeroot, $basename, "thumbnail.gif" ), |
|---|
| 221 | | 'upload' ); |
|---|
| 222 | | } else { |
|---|
| 223 | | return $app->json_error($app->translate("Error downloading image: [_1]", $new_url . 'thumbnail.gif')) |
|---|
| 224 | | } |
|---|
| 225 | | if (($thumbnail_large_response->code >= 200) && ($thumbnail_large_response->code < 400)) { |
|---|
| 226 | | $filemgr->put_data( |
|---|
| 227 | | $thumbnail_large_response->content, |
|---|
| 228 | | File::Spec->catfile( $themeroot, $basename, "thumbnail-large.gif" ), |
|---|
| 229 | | 'upload' |
|---|
| 230 | | ); |
|---|
| 231 | | } else { |
|---|
| 232 | | return $app->json_error($app->translate("Error downloading image: [_1]", $new_url . 'thumbnail-large.gif')) |
|---|
| 233 | | } |
|---|
| 234 | | |
|---|
| 235 | | # Pick up the images we parsed earlier and write them to the theme folder |
|---|
| 236 | | for my $image_url (@images) { |
|---|
| 237 | | my $image_request = |
|---|
| 238 | | HTTP::Request->new( GET => $new_url . $image_url ); |
|---|
| 239 | | my $image_response = $user_agent->request($image_request); |
|---|
| 240 | | |
|---|
| 241 | | my @image_url = split( /\//, $image_url ); |
|---|
| 242 | | my $image_filename = $image_url[-1]; |
|---|
| 243 | | |
|---|
| 244 | | if (($response->code >= 200) && ($response->code < 400)) { |
|---|
| 245 | | $filemgr->put_data( $image_response->content, |
|---|
| 246 | | File::Spec->catfile( $themeroot, $basename, $image_filename ), |
|---|
| 247 | | 'upload' ) |
|---|
| 248 | | or return $app->json_error( $filemgr->errstr ); |
|---|
| 249 | | } else { |
|---|
| 250 | | return $app->json_error($app->translate("Error downloading image: [_1]", $new_url . $image_url)); |
|---|
| 251 | | } |
|---|
| 252 | | } |
|---|
| 253 | | $url = "$webthemeroot$basename/$basename.css"; |
|---|
| 254 | | } |
|---|
| 255 | | |
|---|
| 256 | | |
|---|
| | 231 | my $static_url = $app->static_path; |
|---|
| | 232 | if ( $url !~ m{ \A \Q$static_url\E (?:support/)? themes/ }xms ) { |
|---|
| | 233 | my $basename = download_theme($app, $url) |
|---|
| | 234 | or return; |
|---|
| | 235 | $url = "${static_url}support/themes/$basename/$basename.css"; |
|---|
| | 236 | } |
|---|
| | 237 | |
|---|
| | 238 | my $mtthemebase = $app->static_path . 'themes-base/'; |
|---|