Index: openacs-4/packages/cookie-consent/cookie-consent.info =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/cookie-consent/cookie-consent.info,v diff -u -r1.3 -r1.4 --- openacs-4/packages/cookie-consent/cookie-consent.info 22 Dec 2017 17:31:35 -0000 1.3 +++ openacs-4/packages/cookie-consent/cookie-consent.info 2 Aug 2018 13:05:22 -0000 1.4 @@ -9,18 +9,17 @@ f f - + Gustaf Neumann Cookie Consent Widget based on the free Cookie Consent Library 2017-12-13 MIT License 0 - + - + Index: openacs-4/packages/cookie-consent/tcl/cookie-consent-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/cookie-consent/tcl/cookie-consent-procs.tcl,v diff -u -r1.9 -r1.10 --- openacs-4/packages/cookie-consent/tcl/cookie-consent-procs.tcl 13 Jun 2018 11:39:24 -0000 1.9 +++ openacs-4/packages/cookie-consent/tcl/cookie-consent-procs.tcl 2 Aug 2018 13:05:22 -0000 1.10 @@ -238,7 +238,6 @@ -package_id $subsite_id \ -parameter CookieConsentEnabled \ -default 0] - # # Just do real initialization, when the cookie is NOT set. # When more complex interactions are defined, this has to be @@ -280,46 +279,51 @@ } - ad_proc version_info { + ad_proc resource_info { {-version ""} } { Get information about available version(s) of the cookieconsent packages, either from the local file system, or from CDN. + @return dict containing cdn, cdnHost, resourceDir, prefix, cssFiles, jsFiles and extraFiles. } { # # If no version of the cookie consent library was specified, # use the name-spaced variable as default. # if {$version eq ""} { - set version ${::cookieconsent::version} + set version $::cookieconsent::version } # # Provide paths for loading either via resources or CDN # - set resource_prefix [acs_package_root_dir cookie-consent/www/resources] - set cdn "//cdnjs.cloudflare.com/ajax/libs" + set resourceDir [acs_package_root_dir cookie-consent/www/resources] + set cdn "//cdnjs.cloudflare.com/ajax/libs" # # If the resources are not available locally, these will be # loaded via CDN and the CDN host is set (necessary for CSP). # The returned "prefix" indicates the place, from where the # resource will be loaded. # - if {[file exists $resource_prefix/$version]} { + if {[file exists $resourceDir/$version]} { set prefix /resources/cookie-consent/$version/ + set cdnHost "" } else { set prefix $cdn/$version/ - lappend result host "cdnjs.cloudflare.com" + set cdnHost cdnjs.cloudflare.com } lappend result \ + resourceDir $resourceDir \ cdn $cdn \ + cdnHost $cdnHost \ prefix $prefix \ - cssFiles [list cookieconsent.min.css] \ - jsFiles [list cookieconsent.min.js] + cssFiles {cookieconsent.min.css} \ + jsFiles {cookieconsent.min.js} \ + extraFiles {} return $result } @@ -332,98 +336,24 @@ Add the necessary CSS, JavaScript and CSP to the current page. } { - set version_info [version_info -version $version] + set resource_info [resource_info -version $version] - if {[dict exists $version_info host]} { - security::csp::require script-src [dict get $version_info host] - security::csp::require style-src [dict get $version_info host] + if {[dict exists $resource_info cdnHost] && [dict get $resource_info cdnHost] ne ""} { + security::csp::require script-src [dict get $resource_info cdnHost] + security::csp::require style-src [dict get $resource_info cdnHost] } - set prefix [dict get $version_info prefix] + set prefix [dict get $resource_info prefix] - foreach cssFile [dict get $version_info cssFiles] { + foreach cssFile [dict get $resource_info cssFiles] { template::head::add_css -href $prefix/$cssFile } - foreach jsFile [dict get $version_info jsFiles] { + foreach jsFile [dict get $resource_info jsFiles] { template::head::add_javascript -src $prefix/$jsFile } ::template::add_body_script -script [$object render_js] } - - ad_proc -private download_file {url} { - # - # Helper to download from a URL and to raise exception, when - # download fails. - # - } { - set result [util::http::get -url $url -spool] - if {[dict get $result status] == 200} { - set fn [dict get $result file] - return $fn - } else { - error "download from $url failed: $result" - } - } - - - ad_proc download { - {-version ""} - } { - - Download a version of the cookie consent library - it into a directory structure similar to the CDN structure to - allow installation of multiple versions. When the local - structure is available, it will be used by initialize_widget. - - } { - # - # If no version is explicitly specified, use the name-spaced - # variable as default. - # - if {$version eq ""} { - set version ${::cookieconsent::version} - } - - set version_info [version_info -version $version] - - set download_prefix https:[dict get $version_info cdn] - set resource_prefix [acs_package_root_dir cookie-consent/www/resources/] - - # - # Do we have a writable output directory under resources? - # - if {![file isdirectory $resource_prefix/$version]} { - file mkdir $resource_prefix/$version - } - if {![file writable $resource_prefix/$version]} { - error "directory $resource_prefix/$version is not writable" - } - - # - # Do we have gzip installed? - # - set gzip [::util::which gzip] - - # - # So far, everything is fine, download the - # files. "download_file" will raise an exception, when the - # download fails. - # - foreach file [concat \ - [dict get $version_info cssFiles] \ - [dict get $version_info jsFiles]] { - set fn [download_file $download_prefix/$version/$file] - file rename -force -- $fn $resource_prefix/$version/$file - - # - # When gzip is available, produce a static compressed file as well - # - if {$gzip ne ""} { - exec $gzip -9 -k $resource_prefix/$version/$file - } - } - } } # Local variables: Index: openacs-4/packages/cookie-consent/www/sitewide-admin/download.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/cookie-consent/www/sitewide-admin/download.tcl,v diff -u -r1.1 -r1.2 --- openacs-4/packages/cookie-consent/www/sitewide-admin/download.tcl 17 Dec 2017 19:55:03 -0000 1.1 +++ openacs-4/packages/cookie-consent/www/sitewide-admin/download.tcl 2 Aug 2018 13:05:22 -0000 1.2 @@ -6,5 +6,11 @@ {version:word,notnull ""} } -::cookieconsent::download -version $version +if {$version eq ""} { + set version $::cookieconsent::version +} +::util::resources::download \ + -version_dir $version \ + -resource_info [::cookieconsent::resource_info] + ad_returnredirect . Index: openacs-4/packages/cookie-consent/www/sitewide-admin/index.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/cookie-consent/www/sitewide-admin/index.tcl,v diff -u -r1.4 -r1.5 --- openacs-4/packages/cookie-consent/www/sitewide-admin/index.tcl 11 Jun 2018 09:41:40 -0000 1.4 +++ openacs-4/packages/cookie-consent/www/sitewide-admin/index.tcl 2 Aug 2018 13:05:22 -0000 1.5 @@ -1,46 +1,48 @@ +ad_page_contract { + @author Gustaf Neumann + + @creation-date Dec 22, 2017 +} { +} + set title "Cookie Consent Sitewide Admin" set context [list $title] -set resource_prefix [acs_package_root_dir cookie-consent/www/resources] set what "Cookie Consent Widget" set version $::cookieconsent::version - -if {$::tcl_version eq "8.5"} { - # - # In Tcl 8.5, "::try" was not yet a built-in of Tcl - # - package require try -} - - # # Get version info about the resource files of this package. If not # locally installed, offer a link for download. # -set version_info [::cookieconsent::version_info] -set first_css [lindex [dict get $version_info cssFiles] 0] -set cdn [dict get $version_info cdn] +set resource_info [::cookieconsent::resource_info] +set resource_dir [dict get $resource_info resourceDir] +set cdn [dict get $resource_info cdn] -set writable 1 -if {![file isdirectory $resource_prefix]} { - try { - file mkdir $resource_prefix - } on error {errorMsg} { - set writable 0 - } -} +# +# Check, if the resources are already installed. +# +set is_installed [::util::resources::is_installed_locally \ + -resource_info $resource_info \ + -version_dir $version ] +if {$is_installed} { + # + # Tell the users, where the resources are installed. + # + set resources $resource_dir/$version -if {$writable} { - if {[file exists $resource_prefix/$version/$first_css]} { - set resources $resource_prefix/$version - } - set path $resource_prefix/$version - if {![file exists $path]} { - catch {file mkdir $path} - } - set writable [file writable $path] } else { - set path $resource_prefix + # + # Check, if we can install the resources locally. + # + set writable [util::resources::can_install_locally \ + -resource_info $resource_info \ + -version_dir $version] + if {!$writable} { + # + # If we cannot install locally, tell the user were we want to + # install. + # + set path $resource_dir/$version + } } -