Changeset 679


Ignore:
Timestamp:
May 14, 2019 2:17:07 AM (5 years ago)
Author:
anonymous
Message:

Fix minor bugs. Detect http port and add to site_port, site_url, and page_url params of App.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/examples/_config.inc.php

    r665 r679  
    6262    'images_path' => '/i',
    6363
     64    'php_timezone' => null,
    6465    'date_format' => 'd M Y',
     66    'time_format' => 'H:i',
    6567    'sql_date_format' => '%e %b %Y',
    6668    'sql_time_format' => '%k:%i',
  • trunk/js/Msg.js

    r636 r679  
    8383    var msghash = Strangecode.Msg.hash(message);
    8484    if (!$(options.container).find('[data-msghash="' + msghash + '"]').length) {
    85         $(options.container).append($('<div data-alert class="alert-box sc-js-msg ' + msg_class + '" data-msghash="' + msghash + '"></div>').hide().html(message).append('<a href="#" class="close">×</a>'));
     85        $(options.container).append($('<div data-alert class="alert-box sc-js-msg ' + msg_class + '" data-msghash="' + msghash + '"></div>').hide().html(message).append('<a href="#" class="close">×</a>')).show();
    8686        $('.sc-js-msg').slideDown('fast');
    8787        if ($.fn.foundation) {
     
    107107    }
    108108
    109     if (options.gotohash) {
    110         $(document.body).animate({
     109    if (options.gotohash && $('html, body').scrollTop() > $(options.container).offset().top) {
     110        $('html, body').animate({
    111111            'scrollTop': $(options.container).offset().top
    112112        }, 'fast');
  • trunk/js/Utilities.js

    r672 r679  
    3939*/
    4040if (!String.prototype.format) {
    41     String.prototype.format = function() {
     41    String.prototype.format = function () {
    4242        var args = arguments;
    43         return this.replace(/{(\d+)}/g, function(match, number) {
     43        return this.replace(/{(\d+)}/g, function (match, number) {
    4444            return typeof args[number-1] != 'undefined' ? args[number-1] : match;
    4545        });
     
    6161* @since    30 Jun 2008 12:32:19
    6262*/
    63 jQuery.fn.nospam = function() {
    64     return this.each(function(){
     63jQuery.fn.nospam = function () {
     64    return this.each(function (){
    6565        $(this).text($(this).text().replace(' at ', '@').replace(' dot ', '.'));
    6666        if (this.href) {
     
    131131* @since    30 Jun 2013
    132132*/
    133 $.fn.slug = function() {
     133$.fn.slug = function () {
    134134    str = this.text().trim().toLowerCase();
    135135    var from = 'áéíóúàÚìòùÀëïöÌÁÉÍÓÚÀÈÌÒÙÄËÏÖÜâêîÎûÂÊÎÔÛñçÇ@·/_,:;';
     
    173173*/
    174174if (!String.prototype.ucfirst) {
    175     String.prototype.ucfirst = function() {
     175    String.prototype.ucfirst = function () {
    176176        return this.charAt(0).toUpperCase() + this.slice(1);
    177177    };
     
    193193* @since    06 Mar 2019
    194194*/
    195 Strangecode.humanTime = function(seconds, max_unit) {
     195Strangecode.humanTime = function (seconds, max_unit) {
    196196    // Units: array of seconds in the unit, singular and plural unit names.
    197197    var units = {
     
    227227// https://github.com/benjamingr/RegExp.escape
    228228if(!RegExp.escape){
    229     RegExp.escape = function(s){
     229    RegExp.escape = function (s){
    230230        return String(s).replace(/[\\^$*+?.()|[\]{}]/g, '\\$&');
    231231    };
    232232}
     233
     234// Throttle will ensure that a function is called at most once
     235// in a specified time period (for instance, once per 10 seconds). This means
     236// throttling will prevent a function from running if it has run “recently”.
     237// Throttling also ensures a function is run regularly at a fixed rate.
     238// https://blog.bitsrc.io/understanding-throttling-and-debouncing-973131c1ba07
     239Strangecode.throttle = function (f, t) {
     240    return function (args) {
     241        let previousCall = this.lastCall;
     242        this.lastCall = Date.now();
     243        if (typeof previousCall === 'undefined' || (this.lastCall - previousCall) > t) {
     244            // Throttle time has elapsed.
     245            f(args);
     246        }
     247    }
     248}
     249
     250// Debounce will ignore all calls to it until the calls have stopped for a
     251// specified time period. Only then will it call the original function. For
     252// instance, if we specify the time as two seconds, and the debounced function is
     253// called 10 times with an interval of one second between each call, the function
     254// will not call the original function until two seconds after the last (tenth)
     255// call.
     256// https://blog.bitsrc.io/understanding-throttling-and-debouncing-973131c1ba07
     257Strangecode.debounce = function (f, t) {
     258    return function (args) {
     259        let previousCall = this.lastCall;
     260        this.lastCall = Date.now();
     261        if (previousCall && ((this.lastCall - previousCall) <= t)) {
     262            clearTimeout(this.lastCallTimer);
     263        }
     264        this.lastCallTimer = setTimeout(() => f(args), t);
     265    }
     266}
  • trunk/lib/App.inc.php

    r676 r679  
    7878        'site_name' => null,
    7979        'site_email' => '', // Set to no-reply@HTTP_HOST if not set here.
    80         'site_hostname' => '', // The hostname of this application (if not set, use a cleaned HTTP_HOST environment variable).
     80        'site_hostname' => '', // The hostname of this application (if not set, derived from HTTP_HOST).
     81        'site_port' => '', // The hostname of this application (if not set, derived from HTTP_HOST).
    8182        'site_url' => '', // URL to the root of the site (created during App->start()).
    8283        'page_url' => '', // URL to the current page (created during App->start()).
     
    146147        'db_pass' => null,
    147148
    148         // And for CLI scripts, which should include a JSON file at this specified location in the include path.
     149        // CLI scripts will need this JSON file in the include path.
    149150        'db_auth_file' => 'db_auth.json',
    150151
     
    479480         */
    480481
    481         // To get a safe hostname, remove host port and invalid hostname characters.
     482        // To get a safe hostname, remove port and invalid hostname characters.
    482483        $safe_http_host = preg_replace('/[^a-z\d.:-]/', '', strtok(getenv('HTTP_HOST'), ':'));
     484        // If strtok() matched a ':' in the previous line, the rest of the string contains the port number (or FALSE)
     485        $safe_http_port = preg_replace('/[^0-9]/', '', strtok(''));
    483486        if ('' != $safe_http_host && '' == $this->getParam('site_hostname')) {
    484487            $this->setParam(array('site_hostname' => $safe_http_host));
     488        }
     489        if ('' != $safe_http_port && '' == $this->getParam('site_port')) {
     490            $this->setParam(array('site_port' => $safe_http_port));
    485491        }
    486492
     
    489495        // Not available on CLI scripts obviously.
    490496        if ('' != $safe_http_host && '' == $this->getParam('site_url')) {
    491             $this->setParam(array('site_url' => sprintf('%s://%s', (getenv('HTTPS') ? 'https' : 'http'), $safe_http_host)));
     497            $this->setParam(array('site_url' => sprintf('%s://%s%s', (getenv('HTTPS') ? 'https' : 'http'), $safe_http_host, (preg_match('/^(|80|443)$/', $safe_http_port) ? '' : ':' . $safe_http_port))));
    492498        }
    493499
     
    495501        // Also not available on CLI scripts obviously.
    496502        if ('' != $safe_http_host) {
    497             $this->setParam(array('page_url' => sprintf('%s://%s%s', (getenv('HTTPS') ? 'https' : 'http'), $safe_http_host, getenv('REQUEST_URI'))));
     503            $this->setParam(array('page_url' => sprintf('%s%s', $this->getParam('site_url'), getenv('REQUEST_URI'))));
    498504        }
    499505
     
    13961402
    13971403        if (preg_match('!^/!', $url)) {
    1398             // If relative URL is given, prepend correct local hostname.
    1399             $scheme = getenv('HTTPS') ? 'https' : 'http';
    1400             $host = $this->getParam('site_hostname');
    1401             $url = sprintf('%s://%s%s', $scheme, $host, $url);
     1404            // If relative URL is given, prepend site_url, which contains: scheme://hostname[:port]
     1405            $url = sprintf('%s%s', $this->getParam('site_url'), $url);
    14021406        }
    14031407
     
    17051709
    17061710    /*
    1707     * Set timezone used internally by PHP.
     1711    * Set timezone used internally by PHP. See full list at https://www.php.net/manual/en/timezones.php
    17081712    *
    17091713    * @access   public
  • trunk/lib/DB.inc.php

    r665 r679  
    339339        $debugqry = preg_replace("/\n[\t ]+/", "\n", $query);
    340340        if ($this->getParam('db_always_debug') || $debug) {
    341             echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n";
     341            if ($debug > 1) {
     342                dump($debugqry, true, SC_DUMP_PRINT_R, __FILE__, __LINE__);
     343            } else {
     344                echo "<!-- ----------------- Query $this->_query_count ---------------------\n$debugqry\n-->\n";
     345            }
    342346        }
    343347
  • trunk/lib/Utilities.inc.php

    r672 r679  
    14521452    $ch = curl_init($url);
    14531453    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
     1454    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    14541455    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    1455     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     1456    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
    14561457    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");
    14571458    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Don't pass through data to the browser.
Note: See TracChangeset for help on using the changeset viewer.