Ejemplo n.º 1
0
function get_chart_content($chart, $user, $published = false, $debug = false)
{
    if (!function_exists('unique_scripts')) {
        function unique_scripts($scripts)
        {
            $exist = array();
            $out = array();
            foreach ($scripts as $s) {
                $src = is_array($s) ? $s['src'] : $s;
                if (isset($exist[$src])) {
                    continue;
                }
                $exist[$src] = true;
                $out[] = is_array($s) ? $s : array('src' => $s);
            }
            return $out;
        }
    }
    $theme_css = array();
    $theme_js = array();
    $protocol = get_current_protocol();
    $next_theme_id = $chart->getTheme();
    $locale = DatawrapperSession::getLanguage();
    if ($chart->getLanguage() != '') {
        $locale = $chart->getLanguage();
    }
    $static_path = $GLOBALS['dw_config']['static_path'];
    $abs = $protocol . '://' . $GLOBALS['dw_config']['domain'];
    if ($static_path == 'static/') {
        $static_path = $abs . $static_path;
    }
    while (!empty($next_theme_id)) {
        $theme = DatawrapperTheme::get($next_theme_id);
        // $theme_static_path = str_replace('/static/', $static_path . '/', $theme['__static_path']);
        $theme_static_path = $theme['__static_path'];
        $theme_js[] = $theme_static_path . $next_theme_id . '.js';
        if ($theme['hasStyles']) {
            $theme_css[] = $theme_static_path . $next_theme_id . '.css';
        }
        $next_theme_id = $theme['extends'];
    }
    $abs = $protocol . '://' . $GLOBALS['dw_config']['domain'];
    $debug = $GLOBALS['dw_config']['debug'] == true || $debug;
    $culture = str_replace('_', '-', $locale);
    if ($published && !empty($GLOBALS['dw_config']['asset_domain'])) {
        $base_js = array('//' . $GLOBALS['dw_config']['asset_domain'] . '/globalize.min.js', '//' . $GLOBALS['dw_config']['asset_domain'] . '/cultures/globalize.culture.' . $culture . '.js', '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js', '//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js');
    } else {
        // use "local" assets
        $base_js = array($abs . '/static/vendor/globalize/globalize.min.js', $abs . '/static/vendor/globalize/cultures/globalize.culture.' . $culture . '.js', $abs . '/static/vendor/underscore/underscore-min.js', $abs . '/static/vendor/jquery/jquery.min.js');
    }
    $vis_js = array();
    $vis_css = array();
    $next_vis_id = $chart->getType();
    $vis_libs = array();
    $vis_libs_cdn = array();
    $vis_libs_local = array();
    $vis_locale = array();
    // visualizations may define localized strings, e.g. "other"
    while (!empty($next_vis_id)) {
        $vis = DatawrapperVisualization::get($next_vis_id);
        // $vis_static_path = str_replace('/static/', $static_path . '/', $vis['__static_path']);
        $vis_static_path = $vis['__static_path'];
        $vjs = array();
        if (!empty($vis['libraries'])) {
            foreach (array_reverse($vis['libraries']) as $script) {
                if (!is_array($script)) {
                    $script = array("local" => $script, "cdn" => false);
                }
                if (!empty($script['cdn'])) {
                    $script['src'] = $script['cdn'];
                    $vis_libs_cdn[] = $script;
                }
                // at first we check if the library lives in ./lib of the vis module
                if (file_exists(ROOT_PATH . 'www' . $vis['__static_path'] . $script['local'])) {
                    $u = $vis_static_path . $script['local'];
                } else {
                    if (file_exists(ROOT_PATH . 'www/static/vendor/' . $script['local'])) {
                        $u = '/static/vendor/' . $script['local'];
                    } else {
                        print ROOT_PATH . 'www' . $vis['__static_path'] . $script['local'];
                        die("could not find required library " . $script["local"]);
                    }
                }
                $script['src'] = $u;
                $vis_libs[] = $script;
                if (empty($url['cdn'])) {
                    $vis_libs_local[] = $script;
                }
            }
        }
        if (!empty($vis['locale']) && is_array($vis['locale'])) {
            foreach ($vis['locale'] as $term => $translations) {
                if (!isset($vis_locale[$term])) {
                    $vis_locale[$term] = $translations;
                }
            }
        }
        $vjs[] = $vis_static_path . $vis['id'] . '.js';
        $vis_js = array_merge($vis_js, array_reverse($vjs));
        if ($vis['hasCSS']) {
            $vis_css[] = $vis_static_path . $vis['id'] . '.css';
        }
        $next_vis_id = !empty($vis['extends']) ? $vis['extends'] : null;
    }
    $stylesheets = array_merge(array('/static/css/chart.base.css'), $vis_css, array_reverse($theme_css));
    $the_vis = DatawrapperVisualization::get($chart->getType());
    $the_vis['locale'] = $vis_locale;
    $the_theme = DatawrapperTheme::get($chart->getTheme());
    $l10n__domain = $the_theme['__static_path'];
    $the_vis_js = get_vis_js($the_vis, array_merge(array_reverse($vis_js), $vis_libs_local));
    $the_theme_js = get_theme_js($the_theme, array_reverse($theme_js));
    $the_chart_js = get_chart_js();
    if ($published) {
        $scripts = array_merge($base_js, $vis_libs_cdn, array('/lib/' . $the_vis_js[0], '/lib/' . $the_theme_js[0], '/lib/' . $the_chart_js[0]));
        $stylesheets = array($chart->getID() . '.all.css');
        // NOTE: replace `/static/` by `assets/` in the `__static_path` value,
        //       since vis assets are handle by DatawrapperVisualization
        $replace_in = $the_vis['__static_path'];
        $replace_by = 'assets/';
        $replace = '/static/';
        $the_vis['__static_path'] = substr_replace($replace_in, $replace_by, strrpos($replace_in, $replace), strlen($replace));
        // length
        $the_theme['__static_path'] = '';
    } else {
        $scripts = unique_scripts(array_merge($base_js, array($static_path . '/js/dw-2.0' . ($debug ? '' : '.min') . '.js'), array_reverse($theme_js), array_reverse($vis_js), array_reverse($vis_libs), array($static_path . '/js/dw/chart.base.js')));
    }
    $cfg = $GLOBALS['dw_config'];
    $published_urls = DatawrapperHooks::execute(DatawrapperHooks::GET_PUBLISHED_URL, $chart);
    if (empty($published_urls)) {
        $chart_url = $protocol . '://' . $cfg['chart_domain'] . '/' . $chart->getID() . '/';
    } else {
        $chart_url = $published_urls[0];
        // ignore urls except from the first one
    }
    $page = array('chartData' => $chart->loadData(), 'chart' => $chart, 'lang' => strtolower(substr($locale, 0, 2)), 'metricPrefix' => get_metric_prefix($locale), 'l10n__domain' => $l10n__domain, 'origin' => !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '', 'DW_DOMAIN' => $protocol . '://' . $cfg['domain'] . '/', 'DW_CHART_DATA' => $protocol . '://' . $cfg['domain'] . '/chart/' . $chart->getID() . '/data.csv', 'ASSET_PATH' => $published ? '' : $the_theme['__static_path'], 'published' => $published, 'chartUrl' => $chart_url, 'embedCode' => '<iframe src="' . $chart_url . '" frameborder="0" allowtransparency="true" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen width="' . $chart->getMetadata('publish.embed-width') . '" height="' . $chart->getMetadata('publish.embed-height') . '"></iframe>', 'chartUrlFs' => strpos($chart_url, '.html') > 0 ? str_replace('index.html', 'fs.html', $chart_url) : $chart_url . '?fs=1', 'stylesheets' => $stylesheets, 'scripts' => $scripts, 'visualization' => $the_vis, 'theme' => $the_theme, 'chartLocale' => str_replace('_', '-', $locale), 'vis_js' => $the_vis_js, 'theme_js' => $the_theme_js, 'chart_js' => $the_chart_js);
    return $page;
}
Ejemplo n.º 2
0
/*
 * endpoint for sending a new invitation to a user
 *
 * expects payload { "email": "*****@*****.**" }
 */
$app->post('/account/resend-invitation', function () use($app) {
    $payload = json_decode($app->request()->getBody());
    $user = UserQuery::create()->findOneByEmail($payload->email);
    $token = $user->getActivateToken();
    if (!empty($user)) {
        if (empty($token)) {
            return error("token-invalid", __("This activation token is invalid. Your email address is probably already activated."));
        }
        // variables for `templates/invitation-email.php`
        $domain = $GLOBALS['dw_config']['domain'];
        $protocol = get_current_protocol();
        $invitationLink = $protocol . '://' . $domain . '/account/invite/' . $token;
        $name = $user->getEmail();
        include '../../lib/templates/invitation-email.php';
        $from = $GLOBALS['dw_config']['email']['invite'];
        dw_send_support_email($user->getEmail(), __('You have been invited to Datawrapper!'), $invitation_mail, array('name' => $user->guessName(), 'invitation_link' => $invitationLink));
        ok(__('You should soon receive an email with further instructions.'));
    } else {
        error('login-email-unknown', __('The email is not registered yet.'));
    }
});
/*
 * endpoint for validating an invitation. The user sends his new password
 */
$app->post('/account/invitation/:token', function ($token) use($app) {
    $data = json_decode($app->request()->getBody());
Ejemplo n.º 3
0
function publish_html($user, $chart)
{
    $cdn_files = array();
    $static_path = get_static_path($chart);
    $seckey = sha1(isset($GLOBALS['dw_config']['secure_auth_key']) ? $GLOBALS['dw_config']['secure_auth_key'] : '');
    $protocol = get_current_protocol();
    $url = $protocol . "://" . $GLOBALS['dw_config']['domain'] . '/chart/' . $chart->getID() . '/preview?minify=1&seckey=' . $seckey;
    $outf = $static_path . '/index.html';
    $chart->setPublishedAt(time() + 5);
    $chart->setLastEditStep(5);
    $chart->save();
    download($url, $outf);
    download($url . '&plain=1', $static_path . '/plain.html');
    download($url . '&fs=1', $static_path . '/fs.html');
    $cdn_files[] = array($outf, $chart->getCDNPath() . 'index.html', 'text/html');
    $cdn_files[] = array($static_path . '/plain.html', $chart->getCDNPath() . 'plain.html', 'text/html');
    $cdn_files[] = array($static_path . '/fs.html', $chart->getCDNPath() . 'fs.html', 'text/html');
    // copy empty image as placeholder for nojs.png
    file_put_contents($static_path . '/nojs.png', file_get_contents(ROOT_PATH . 'www/static/img/nojs.png'));
    return $cdn_files;
}
Ejemplo n.º 4
0
function get_base_url($support_https = false)
{
    global $pun_config;
    static $base_url;
    if (!$support_https) {
        return $pun_config['o_base_url'];
    }
    if (!isset($base_url)) {
        // Make sure we are using the correct protocol
        $base_url = str_replace(array('http://', 'https://'), get_current_protocol() . '://', $pun_config['o_base_url']);
    }
    return $base_url;
}
Ejemplo n.º 5
0
         $user->setPwd($payload->pwd);
         Action::logAction($curUser, 'change-password', array('user' => $user->getId()));
     } else {
         Action::logAction($curUser, 'change-password-failed', array('user' => $user->getId(), 'reason' => 'old password is wrong'));
         $errors[] = __('The password could not be changed because your old password was not entered correctly.');
     }
 }
 if (!empty($payload->email) && $payload->email != $user->getEmail()) {
     if (check_email($payload->email) || $curUser->isAdmin()) {
         if (!email_exists($payload->email)) {
             if ($curUser->isAdmin()) {
                 $user->setEmail($payload->email);
             } else {
                 // non-admins need to confirm new emails addresses
                 $token = hash_hmac('sha256', $user->getEmail() . '/' . $payload->email . '/' . time(), DW_TOKEN_SALT);
                 $token_link = get_current_protocol() . '://' . $GLOBALS['dw_config']['domain'] . '/account/profile?token=' . $token;
                 // send email with token
                 require ROOT_PATH . 'lib/templates/email-change-email.php';
                 dw_send_support_email($payload->email, __('Datawrapper: You requested a change of your email address'), $email_change_mail, array('name' => $user->guessName(), 'email_change_token_link' => $token_link, 'old_email' => $user->getEmail(), 'new_email' => $payload->email));
                 // log action for later confirmation
                 Action::logAction($curUser, 'email-change-request', array('old-email' => $user->getEmail(), 'new-email' => $payload->email, 'token' => $token));
                 $messages[] = __('To complete the change of your email address, you need to confirm that you have access to it. Therefor we sent an email with the confirmation link to your new address. Your new email will be set right after you clicked that link.');
             }
         } else {
             $errors[] = sprintf(__('The email address <b>%s</b> already exists.'), $payload->email);
         }
     } else {
         $errors[] = sprintf(__('The email address <b>%s</b> is invalid.'), $payload->email);
     }
 }
 if (!empty($payload->name)) {
Ejemplo n.º 6
0
function get_current_url($max_length = 0)
{
    $protocol = get_current_protocol();
    $port = isset($_SERVER['SERVER_PORT']) && ($_SERVER['SERVER_PORT'] != '80' && $protocol == 'http' || $_SERVER['SERVER_PORT'] != '443' && $protocol == 'https') && strpos($_SERVER['HTTP_HOST'], ':') === false ? ':' . $_SERVER['SERVER_PORT'] : '';
    $url = urldecode($protocol . '://' . $_SERVER['HTTP_HOST'] . $port . $_SERVER['REQUEST_URI']);
    if (strlen($url) <= $max_length || $max_length == 0) {
        return $url;
    }
    // We can't find a short enough url
    return null;
}
Ejemplo n.º 7
0
 public function getLocalUrl()
 {
     return get_current_protocol() . '://' . $GLOBALS['dw_config']['chart_domain'] . '/' . $this->getID() . '/index.html';
 }