Exemplo n.º 1
0
function check_chart_public($id, $callback)
{
    $chart = ChartQuery::create()->findPK($id);
    $loggedUser = DatawrapperSession::getUser();
    if ($chart) {
        $user = $chart->getUser();
        if ($user->isAbleToPublish()) {
            if ($chart->isPublic() || $user == $loggedUser) {
                call_user_func($callback, $user, $chart);
            } else {
                if ($chart->_isDeleted()) {
                    error_chart_deleted();
                } else {
                    error_chart_not_published();
                }
            }
        } else {
            // no such chart
            error_not_allowed_to_publish();
        }
    } else {
        // no such chart
        error_chart_not_found($id);
    }
}
Exemplo n.º 2
0
 public function controller($app)
 {
     $plugin = $this;
     $app->get('/gallery(/?|/by/:key/:val)', function ($key = false, $val = false) use($app, $plugin) {
         disable_cache($app);
         $user = DatawrapperSession::getUser();
         $curPage = $app->request()->params('page');
         if (empty($curPage)) {
             $curPage = 0;
         }
         $perPage = 60;
         $filter = !empty($key) ? array($key => $val) : array();
         try {
             $charts = ChartQuery::create()->getGalleryCharts($filter, $curPage * $perPage, $perPage);
             $total = ChartQuery::create()->countGalleryCharts($filter);
         } catch (Exception $e) {
             // make sure bogus input for the filter doesn't kill the site
             $charts = array();
             $total = 0;
         }
         $page = array('charts' => $charts, 'byvis' => $plugin->nbChartsByType(), 'key' => $key, 'val' => $val);
         add_pagination_vars($page, $total, $curPage, $perPage);
         add_header_vars($page, 'gallery');
         $app->render('plugins/' . $plugin->getName() . '/gallery.twig', $page);
     });
 }
Exemplo n.º 3
0
function if_is_admin($callback)
{
    $user = DatawrapperSession::getUser();
    if ($user->isAdmin()) {
        call_user_func($callback);
    } else {
        error('access-denied', 'need admin privileges.');
    }
}
Exemplo n.º 4
0
/**
 * checks if a chart is writeable by the current user (or guest)
 *
 * @param chart_id
 * @param callback the function to be executed if chart is writable
 */
function if_chart_is_writable($chart_id, $callback)
{
    $chart = ChartQuery::create()->findPK($chart_id);
    if (!empty($chart)) {
        $user = DatawrapperSession::getUser();
        $res = $chart->isWritable($user);
        if ($res === true) {
            call_user_func($callback, $user, $chart);
        } else {
            error('access-denied', $res);
        }
    } else {
        error('no-such-chart', '');
    }
}
Exemplo n.º 5
0
 public function users($app, $page)
 {
     $page = array_merge($page, array('title' => __('Users'), 'q' => $app->request()->params('q', '')));
     $sort = $app->request()->params('sort', '');
     $user = DatawrapperSession::getUser();
     function getQuery($user)
     {
         global $app;
         $sort = $app->request()->params('sort', '');
         $query = UserQuery::create()->leftJoin('User.Chart')->withColumn('COUNT(Chart.Id)', 'NbCharts')->groupBy('User.Id')->filterByDeleted(false);
         $q = $app->request()->params('q');
         if ($q) {
             $query->where('email LIKE "%' . $q . '%" OR name LIKE "%' . $q . '%"');
         }
         if (!$user->isSysAdmin()) {
             $query->filterByRole('sysadmin', Criteria::NOT_EQUAL);
         }
         switch ($sort) {
             case 'name':
                 $query->orderByName('asc');
                 break;
             case 'email':
                 $query->orderByEmail('asc');
                 break;
             case 'charts':
                 $query->orderBy('NbCharts', 'desc');
                 break;
             case 'created_at':
             default:
                 $query->orderBy('createdAt', 'desc');
                 break;
         }
         return $query;
     }
     $curPage = $app->request()->params('page', 0);
     $total = getQuery($user)->count();
     $perPage = 50;
     $append = '';
     if ($page['q']) {
         $append = '&q=' . $page['q'];
     }
     if (!empty($sort)) {
         $append .= '&sort=' . $sort;
     }
     add_pagination_vars($page, $total, $curPage, $perPage, $append);
     $page['users'] = getQuery($user)->limit($perPage)->offset($curPage * $perPage)->find();
     $app->render('plugins/admin-users/admin-users.twig', $page);
 }
Exemplo n.º 6
0
function check_chart_writable($id, $callback)
{
    $chart = ChartQuery::create()->findPK($id);
    if ($chart) {
        $user = DatawrapperSession::getUser();
        if ($chart->isWritable($user) === true) {
            call_user_func($callback, $user, $chart);
        } else {
            // no such chart
            error_chart_not_writable();
        }
    } else {
        // no such chart
        error_chart_not_found($id);
    }
}
Exemplo n.º 7
0
 public function init()
 {
     $plugin = $this;
     // hook into chart publication
     DatawrapperHooks::register(DatawrapperHooks::GET_CHART_ACTIONS, function () use($plugin) {
         // no export possible without email
         $user = DatawrapperSession::getUser();
         if ($user->getEmail() == '') {
             return array();
         }
         return array('id' => 'export-image', 'title' => __("Export to static image for printing", $plugin->getName()), 'icon' => 'print');
     });
     // provide static assets files
     $this->declareAssets(array('export-image.js', 'export-image.css'), "|/chart/[^/]+/publish|");
     // hook into job execution
     DatawrapperHooks::register('export_image', array($this, 'exportImage'));
 }
Exemplo n.º 8
0
function user_charts($app, $user, $key, $val)
{
    $curPage = $app->request()->params('page');
    $q = $app->request()->params('q');
    if (empty($curPage)) {
        $curPage = 0;
    }
    $perPage = 12;
    $filter = !empty($key) ? array($key => $val) : array();
    if (!empty($q)) {
        $filter['q'] = $q;
    }
    $charts = ChartQuery::create()->getPublicChartsByUser($user, $filter, $curPage * $perPage, $perPage);
    $total = ChartQuery::create()->countPublicChartsByUser($user, $filter);
    $page = array('charts' => $charts, 'bymonth' => nbChartsByMonth($user), 'byvis' => nbChartsByType($user), 'bylayout' => nbChartsByLayout($user), 'bystatus' => nbChartsByStatus($user), 'key' => $key, 'val' => $val, 'search_query' => empty($q) ? '' : $q, 'mycharts_base' => '/mycharts');
    if (DatawrapperSession::getUser()->isAdmin() && $user != DatawrapperSession::getUser()) {
        $page['user2'] = $user;
        $page['mycharts_base'] = '/admin/charts/' . $user->getId();
        $page['all_users'] = UserQuery::create()->filterByDeleted(false)->orderByEmail()->find();
    }
    add_header_vars($page, 'mycharts');
    add_pagination_vars($page, $total, $curPage, $perPage, empty($q) ? '' : '&q=' . $q);
    $app->render('mycharts.twig', $page);
}
Exemplo n.º 9
0
<?php

//GET route
$app->get('/login', function () use($app) {
    disable_cache($app);
    if (DatawrapperSession::getUser()->isLoggedIn()) {
        $app->redirect('/');
    }
    $page = array('title' => 'Datawrapper', 'pageClass' => 'login', 'noHeader' => true, 'noFooter' => true, 'noSignup' => true);
    add_header_vars($page, '');
    $app->render('login-page.twig', $page);
});
//GET route
$app->get('/setup', function () use($app) {
    disable_cache($app);
    if (DatawrapperSession::getUser()->isLoggedIn() || UserQuery::create()->filterByRole(array('admin', 'sysadmin'))->count() > 0) {
        $app->redirect('/');
    }
    $page = array('title' => 'Datawrapper', 'pageClass' => 'setup', 'noHeader' => true, 'noFooter' => true, 'noSignup' => true, 'auth_salt' => DW_AUTH_SALT);
    add_header_vars($page, '');
    $app->render('setup.twig', $page);
});
/*
 * endpoint for final setup script
 */
$app->post('/setup', function () use($app) {
    $data = json_decode($app->request()->getBody());
    // check that there is no admin user yet (only true right after setup)
    if (UserQuery::create()->count() == 0) {
        $user = new User();
        $user->setCreatedAt(time());
Exemplo n.º 10
0
function get_theme_js($theme, $themeJS)
{
    $all = '';
    $org = DatawrapperSession::getUser()->getCurrentOrganization();
    if (!empty($org)) {
        $org = '/' . $org->getID();
    } else {
        $org = '';
    }
    $keys = DatawrapperHooks::execute(DatawrapperHooks::GET_PUBLISH_STORAGE_KEY);
    if (is_array($keys)) {
        $org .= '/' . join($keys, '/');
    }
    foreach ($themeJS as $js) {
        if (substr($js, 0, 7) != "http://" && substr($js, 0, 8) != "https://" && substr($js, 0, 2) != '//') {
            $all .= "\n\n\n" . file_get_contents(ROOT_PATH . 'www' . $js);
        }
    }
    $all = jsminify($all);
    $theme_js_md5 = md5($all . $org);
    $theme_path = 'theme/' . $theme['id'] . '-' . $theme_js_md5 . '.min.js';
    return array($theme_path, $all);
}
Exemplo n.º 11
0
function add_header_vars(&$page, $active = null)
{
    // define the header links
    global $app;
    $config = $GLOBALS['dw_config'];
    if (!isset($active)) {
        $active = explode('/', $app->request()->getResourceUri());
        $active = $active[1];
    }
    $user = DatawrapperSession::getUser();
    $headlinks = array();
    if ($user->isLoggedIn() || empty($config['prevent_guest_charts'])) {
        $headlinks[] = array('url' => '/chart/create', 'id' => 'chart', 'title' => __('Create Chart'), 'icon' => 'pencil');
    }
    if ($user->isLoggedIn() && $user->hasCharts()) {
        $headlinks[] = array('url' => '/mycharts/', 'id' => 'mycharts', 'title' => __('My Charts'), 'icon' => 'signal');
    } else {
        $headlinks[] = array('url' => '/gallery/', 'id' => 'gallery', 'title' => __('Gallery'), 'icon' => 'signal');
    }
    if (isset($config['navigation'])) {
        foreach ($config['navigation'] as $item) {
            $link = array('url' => str_replace('%lang%', substr(DatawrapperSession::getLanguage(), 0, 2), $item['url']), 'id' => $item['id'], 'title' => __($item['title']));
            if (!empty($item['icon'])) {
                $link['icon'] = $item['icon'];
            }
            $headlinks[] = $link;
        }
    }
    // language dropdown
    if (!empty($config['languages'])) {
        $langDropdown = array('url' => '', 'id' => 'lang', 'dropdown' => array(), 'title' => __('Language'), 'icon' => 'font');
        foreach ($config['languages'] as $lang) {
            $langDropdown['dropdown'][] = array('url' => '#lang-' . $lang['id'], 'title' => $lang['title']);
        }
        if (count($langDropdown['dropdown']) > 1) {
            $headlinks[] = $langDropdown;
        }
    }
    if ($user->isLoggedIn()) {
        $shortenedMail = $user->getEmail();
        $shortenedMail = strlen($shortenedMail) > 18 ? substr($shortenedMail, 0, 9) . '...' . substr($shortenedMail, strlen($shortenedMail) - 9) : $shortenedMail;
        $headlinks[] = array('url' => '#user', 'id' => 'user', 'title' => $shortenedMail, 'icon' => 'user', 'dropdown' => array(array('url' => '/account/settings', 'icon' => 'wrench', 'title' => __('Settings')), array('url' => '#logout', 'icon' => 'off', 'title' => __('Logout'))));
        if ($user->isAdmin()) {
            $headlinks[] = array('url' => '/admin', 'id' => 'admin', 'icon' => 'fire', 'title' => __('Admin'));
        }
    } else {
        $headlinks[] = array('url' => '#login', 'id' => 'login', 'title' => __('Login / Sign Up'), 'icon' => 'user');
    }
    foreach ($headlinks as $i => $link) {
        $headlinks[$i]['active'] = $headlinks[$i]['id'] == $active;
    }
    $page['headlinks'] = $headlinks;
    $page['user'] = DatawrapperSession::getUser();
    $page['language'] = substr(DatawrapperSession::getLanguage(), 0, 2);
    $page['locale'] = DatawrapperSession::getLanguage();
    $page['DW_DOMAIN'] = $config['domain'];
    $page['DW_VERSION'] = DATAWRAPPER_VERSION;
    $page['DW_CHART_CACHE_DOMAIN'] = $config['chart_domain'];
    $page['ADMIN_EMAIL'] = $config['email']['admin'];
    $page['config'] = $config;
    $page['invert_navbar'] = substr($config['domain'], -4) == '.pro';
    $uri = $app->request()->getResourceUri();
    $plugin_assets = DatawrapperHooks::execute(DatawrapperHooks::GET_PLUGIN_ASSETS, $uri);
    if (!empty($plugin_assets)) {
        $plugin_js_files = array();
        $plugin_css_files = array();
        foreach ($plugin_assets as $files) {
            if (!is_array($files)) {
                $files = array($files);
            }
            foreach ($files as $file) {
                if (substr($file, -3) == '.js') {
                    $plugin_js_files[] = $file;
                }
                if (substr($file, -4) == '.css') {
                    $plugin_css_files[] = $file;
                }
            }
        }
        $page['plugin_js'] = $plugin_js_files;
        $page['plugin_css'] = $plugin_css_files;
    }
    if (isset($config['piwik'])) {
        $page['PIWIK_URL'] = $config['piwik']['url'];
        $page['PIWIK_IDSITE'] = $config['piwik']['idSite'];
        if (isset($config['piwik']['idSiteNoCharts'])) {
            $page['PIWIK_IDSITE_NO_CHARTS'] = $config['piwik']['idSiteNoCharts'];
        }
    }
    if ($config['debug']) {
        if (file_exists('../.git')) {
            // parse git branch
            $head = file_get_contents('../.git/HEAD');
            $parts = explode("/", $head);
            $page['BRANCH'] = ' (' . trim($parts[count($parts) - 1]) . ')';
        }
    }
}
Exemplo n.º 12
0
    $app->get('/account/?', function () use($app, $pages) {
        $app->redirect('/account/' . $pages[0]['url'] . '/');
    });
    // redirect to settings
    $app->get('/settings/?', function () use($app) {
        $app->redirect('/account');
    });
    $user = DatawrapperSession::getUser();
    foreach ($pages as $page) {
        $context = array('title' => $page['title'], 'gravatar' => md5(strtolower(trim($user->getEmail()))), 'pages' => $pages, 'active' => $page['url'], 'user' => $user);
        add_header_vars($context, 'account');
        $app->get('/account/' . $page['url'] . '/?', function () use($app, $context, $page) {
            $u = DatawrapperSession::getUser();
            if ($u->isSysAdmin()) {
                if ($app->request()->get('uid') != null) {
                    $u = UserQuery::create()->findPk($app->request()->get('uid'));
                    if ($u) {
                        $context['user'] = $u;
                        $context['notme'] = $u != DatawrapperSession::getUser();
                        $context['gravatar'] = md5(strtolower(trim($u->getEmail())));
                        // update links to other pages
                        for ($i = 0; $i < count($context['pages']); $i++) {
                            $context['pages'][$i]['params'] = '?uid=' . $u->getId();
                        }
                    }
                }
            }
            call_user_func_array($page['controller']($app, $context), func_get_args());
        });
    }
});
Exemplo n.º 13
0
<?php

//GET route
$app->get('/login', function () use($app) {
    //disable_cache($app);
    if (DatawrapperSession::getUser()->isLoggedIn()) {
        $app->redirect('/');
    }
    $page = array('title' => 'Datawrapper', 'pageClass' => 'login', 'noHeader' => true, 'noFooter' => true, 'noSignup' => true);
    add_header_vars($page, '');
    $app->render('login-page.twig', $page);
});
//GET route
$app->get('/setup', function () use($app) {
    //disable_cache($app);
    if (DatawrapperSession::getUser()->isLoggedIn() || UserQuery::create()->count() > 0) {
        $app->redirect('/');
    }
    $page = array('title' => 'Datawrapper', 'pageClass' => 'setup', 'noHeader' => true, 'noFooter' => true, 'noSignup' => true, 'auth_salt' => DW_AUTH_SALT);
    add_header_vars($page, '');
    $app->render('setup.twig', $page);
});
/*
 * endpoint for final setup script
 */
$app->post('/setup', function () use($app) {
    $data = json_decode($app->request()->getBody());
    // check that there is no admin user yet (only true right after setup)
    if (UserQuery::create()->count() == 0) {
        $user = new User();
        $user->setCreatedAt(time());
Exemplo n.º 14
0
 public static function load()
 {
     if (defined('NO_SESSION')) {
         $plugins = PluginQuery::create()->distinct()->filterByEnabled(true)->filterByIsPrivate(false)->find();
     } else {
         $plugins = self::getUserPlugins(DatawrapperSession::getUser()->getId());
     }
     $not_loaded_yet = array();
     foreach ($plugins as $plugin) {
         if (!isset(self::$loaded[$plugin->getId()])) {
             $not_loaded_yet[] = $plugin;
         }
     }
     $could_not_install = array();
     $init_queue = array();
     $load_plugin = function ($plugin) use(&$init_queue) {
         $plugin_path = ROOT_PATH . 'plugins/' . $plugin->getName();
         // first if this plugin uses composer, require the autoloader
         if (file_exists($plugin_path . '/vendor/autoload.php')) {
             require_once $plugin_path . '/vendor/autoload.php';
         }
         if (file_exists($plugin_path . '/plugin.php')) {
             require_once $plugin_path . '/plugin.php';
             // init plugin class
             $className = $plugin->getClassName();
             $pluginClass = new $className();
         } else {
             $pluginClass = new DatawrapperPlugin($plugin->getName());
             if (file_exists($plugin_path . '/init.php')) {
                 $pluginClass->injectInitFunction(function ($plugin) use($plugin_path) {
                     include_once $plugin_path . '/init.php';
                 });
             }
         }
         // then, lets also load the libraries required by this lib
         // this is DEPRECATED, all new plugins should use the autoload
         // method using composer or similar
         foreach ($pluginClass->getRequiredLibraries() as $lib) {
             require_once $plugin_path . '/' . $lib;
         }
         $init_queue[] = $pluginClass;
         return $pluginClass;
     };
     while (count($not_loaded_yet) > 0) {
         $try = $not_loaded_yet;
         $not_loaded_yet = array();
         while (count($try) > 0) {
             $plugin = array_shift($try);
             $id = $plugin->getId();
             $deps = $plugin->getDependencies();
             unset($deps['core']);
             // ignore core dependency
             $can_load = true;
             if (is_array($deps)) {
                 foreach ($deps as $dep => $version) {
                     if (!isset(self::$loaded[$dep])) {
                         // dependency not loaded
                         $can_load = false;
                         if (!file_exists(ROOT_PATH . 'plugins/' . $dep) || isset($could_not_install[$dep])) {
                             // dependency does not exists, not good
                             $could_not_install[$id] = true;
                         }
                         break;
                     }
                 }
             }
             if (isset(self::$loaded[$id]) && self::$loaded[$id]) {
                 // plugin already loaded by now
                 continue;
             }
             if ($can_load) {
                 // load plugin
                 self::$loaded[$id] = true;
                 self::$instances[$id] = $load_plugin($plugin);
             } else {
                 if (!isset($could_not_install[$id])) {
                     $not_loaded_yet[] = $plugin;
                     // so try next time
                 }
             }
         }
     }
     // now initialize all plugins
     while (count($init_queue) > 0) {
         $pluginClass = array_shift($init_queue);
         $pluginClass->init();
     }
 }
Exemplo n.º 15
0
 public static function load()
 {
     $plugins = PluginQuery::create()->filterByEnabled(true);
     if (!defined('NO_SESSION')) {
         $user_id = DatawrapperSession::getUser()->getId();
         if (!empty($user_id)) {
             $plugins->where('Plugin.Id IN (SELECT plugin_id FROM plugin_organization WHERE organization_id IN (SELECT organization_id FROM user_organization WHERE user_id = ?))', $user_id)->_or();
         }
         $plugins = $plugins->where('Plugin.IsPrivate = FALSE');
     }
     $plugins = $plugins->find();
     $not_loaded_yet = array();
     foreach ($plugins as $plugin) {
         if (!isset(self::$loaded[$plugin->getId()])) {
             $not_loaded_yet[] = $plugin;
         }
     }
     $could_not_install = array();
     if (!function_exists('load_plugin')) {
         function load_plugin($plugin)
         {
             $plugin_path = ROOT_PATH . 'plugins/' . $plugin->getName() . '/plugin.php';
             if (file_exists($plugin_path)) {
                 require $plugin_path;
                 // init plugin class
                 $className = $plugin->getClassName();
                 $pluginClass = new $className();
             } else {
                 $pluginClass = new DatawrapperPlugin($plugin->getName());
             }
             // but before we load the libraries required by this lib
             foreach ($pluginClass->getRequiredLibraries() as $lib) {
                 require_once ROOT_PATH . 'plugins/' . $plugin->getName() . '/' . $lib;
             }
             $pluginClass->init();
             return $pluginClass;
         }
     }
     while (count($not_loaded_yet) > 0) {
         $try = $not_loaded_yet;
         $not_loaded_yet = array();
         while (count($try) > 0) {
             $plugin = array_shift($try);
             $id = $plugin->getId();
             $deps = $plugin->getDependencies();
             unset($deps['core']);
             // ignore core dependency
             $can_load = true;
             if (is_array($deps)) {
                 foreach ($deps as $dep => $version) {
                     if (!isset(self::$loaded[$dep])) {
                         // dependency not loaded
                         $can_load = false;
                         if (!file_exists(ROOT_PATH . 'plugins/' . $dep) || isset($could_not_install[$dep])) {
                             // dependency does not exists, not good
                             $could_not_install[$id] = true;
                         }
                         break;
                     }
                 }
             }
             if (isset(self::$loaded[$id]) && self::$loaded[$id]) {
                 // plugin already loaded by now
                 continue;
             }
             if ($can_load) {
                 // load plugin
                 self::$loaded[$id] = true;
                 self::$instances[$id] = load_plugin($plugin);
             } else {
                 if (!isset($could_not_install[$id])) {
                     $not_loaded_yet[] = $plugin;
                     // so try next time
                 }
             }
         }
     }
 }
Exemplo n.º 16
0
function add_header_vars(&$page, $active = null, $page_css = null)
{
    if (!function_exists('header_nav_hook')) {
        function header_nav_hook(&$headlinks, $part)
        {
            $links = DatawrapperHooks::execute('header_nav_' . $part);
            if (!empty($links)) {
                foreach ($links as $link) {
                    $headlinks[] = $link;
                }
            }
        }
    }
    // define the header links
    global $app;
    $config = $GLOBALS['dw_config'];
    if (!isset($active)) {
        $active = explode('/', $app->request()->getResourceUri());
        $active = $active[1];
    }
    if (!isset($config['prevent_guest_charts'])) {
        $config['prevent_guest_charts'] = false;
    }
    if (!isset($config['prevent_guest_access'])) {
        $config['prevent_guest_access'] = false;
    }
    $user = DatawrapperSession::getUser();
    $headlinks = array();
    if ($user->isLoggedIn()) {
        $headlinks[] = array('url' => '/chart/create', 'id' => 'chart', 'title' => __('New Chart'), 'icon' => 'fa fa-plus');
    }
    header_nav_hook($headlinks, 'create');
    if (isset($config['navigation'])) {
        foreach ($config['navigation'] as $item) {
            $link = array('url' => str_replace('%lang%', substr(DatawrapperSession::getLanguage(), 0, 2), $item['url']), 'id' => $item['id'], 'title' => __($item['title']));
            if (!empty($item['icon'])) {
                $link['icon'] = $item['icon'];
            }
            $headlinks[] = $link;
        }
    }
    if (!$user->isLoggedIn()) {
        header_nav_hook($headlinks, 'logged_out_nav');
    }
    header_nav_hook($headlinks, 'custom_nav');
    // language dropdown
    if (!empty($config['languages'])) {
        $langDropdown = array('url' => '', 'id' => 'lang', 'dropdown' => array(), 'title' => strtoupper(substr(DatawrapperSession::getLanguage(), 0, 2)), 'icon' => false, 'tooltip' => __('Switch language'));
        foreach ($config['languages'] as $lang) {
            $langDropdown['dropdown'][] = array('url' => '#lang-' . $lang['id'], 'title' => $lang['title']);
        }
        if (count($langDropdown['dropdown']) > 1) {
            $headlinks[] = $langDropdown;
        }
    }
    header_nav_hook($headlinks, 'languages');
    if ($user->isLoggedIn()) {
        $headlinks[] = 'divider';
        $username = $user->guessName();
        if ($username == $user->getEmail()) {
            $username = strlen($username) > 18 ? substr($username, 0, 9) . '…' . substr($username, strlen($username) - 9) : $username;
        } else {
            if (strlen($username) > 18) {
                $username = substr($username, 0, 16) . '…';
            }
        }
        $headlinks[] = array('url' => '/account/profile', 'id' => 'account', 'title' => '<img style="height:22px;position:relative;top:-2px;border-radius:7px;margin-right:7px" src="//www.gravatar.com/avatar/' . md5(strtolower(trim($user->getEmail()))) . '?s=44&amp;d=mm" /><b>' . htmlspecialchars($username, ENT_QUOTES, 'UTF-8') . '</b>');
        if ($user->hasCharts()) {
            // mycharts
            $mycharts = array('url' => '/mycharts/', 'id' => 'mycharts', 'title' => __('My Charts'), 'icon' => 'fa fa-bar-chart-o', 'dropdown' => array());
            foreach ($user->getRecentCharts(9) as $chart) {
                $mycharts['dropdown'][] = array('url' => '/chart/' . $chart->getId() . '/visualize#tell-the-story', 'title' => '<img width="30" src="' . ($chart->hasPreview() ? $chart->thumbUrl(true) : '') . '" class="icon" /> ' . '<span>' . strip_tags($chart->getTitle()) . '</span>');
            }
            $mycharts['dropdown'][] = 'divider';
            $mycharts['dropdown'][] = array('url' => '/mycharts/', 'title' => __('All charts'));
            $headlinks[] = $mycharts;
        }
        header_nav_hook($headlinks, 'mycharts');
        // the place where settings used to be
        header_nav_hook($headlinks, 'settings');
    } else {
        $headlinks[] = array('url' => '#login', 'id' => 'login', 'title' => $config['prevent_guest_access'] ? __('Login') : __('Login / Sign Up'), 'icon' => 'fa fa-sign-in');
    }
    if ($user->isLoggedIn()) {
        $headlinks[] = array('url' => '#logout', 'id' => 'signout', 'icon' => 'fa fa-sign-out', 'justicon' => true, 'tooltip' => __('Sign out'));
    }
    header_nav_hook($headlinks, 'user');
    // admin link
    if ($user->isLoggedIn() && $user->isAdmin() && DatawrapperHooks::hookRegistered(DatawrapperHooks::GET_ADMIN_PAGES)) {
        $headlinks[] = 'divider';
        $headlinks[] = array('url' => '/admin', 'id' => 'admin', 'icon' => 'fa fa-gears', 'justicon' => true, 'tooltip' => __('Admin'));
    }
    header_nav_hook($headlinks, 'admin');
    if (DatawrapperHooks::hookRegistered(DatawrapperHooks::CUSTOM_LOGO)) {
        $logos = DatawrapperHooks::execute(DatawrapperHooks::CUSTOM_LOGO);
        $page['custom_logo'] = $logos[0];
    }
    foreach ($headlinks as $i => $link) {
        if ($link == 'divider') {
            continue;
        }
        $headlinks[$i]['active'] = $headlinks[$i]['id'] == $active;
    }
    $page['headlinks'] = $headlinks;
    $page['user'] = DatawrapperSession::getUser();
    $page['language'] = substr(DatawrapperSession::getLanguage(), 0, 2);
    $page['locale'] = DatawrapperSession::getLanguage();
    $page['DW_DOMAIN'] = $config['domain'];
    $page['DW_VERSION'] = DATAWRAPPER_VERSION;
    $page['ASSET_DOMAIN'] = $config['asset_domain'];
    $page['DW_CHART_CACHE_DOMAIN'] = $config['chart_domain'];
    $page['SUPPORT_EMAIL'] = $config['email']['support'];
    $page['config'] = $config;
    $page['page_css'] = $page_css;
    $page['invert_navbar'] = isset($config['invert_header']) && $config['invert_header'] || substr($config['domain'], -4) == '.pro';
    $page['noSignup'] = $config['prevent_guest_access'];
    $page['alternative_signins'] = DatawrapperHooks::execute(DatawrapperHooks::ALTERNATIVE_SIGNIN);
    $page['footer'] = DatawrapperHooks::execute(DatawrapperHooks::GET_FOOTER);
    $uri = $app->request()->getResourceUri();
    $plugin_assets = DatawrapperHooks::execute(DatawrapperHooks::GET_PLUGIN_ASSETS, $uri);
    if (!empty($plugin_assets)) {
        $plugin_js_files = array();
        $plugin_css_files = array();
        foreach ($plugin_assets as $assets) {
            if (!is_array($assets)) {
                $assets = array($assets);
            }
            foreach ($assets as $asset) {
                $file = $asset[0];
                $plugin = $asset[1];
                if (substr($file, -3) == '.js') {
                    $plugin_js_files[] = $file . '?v=' . $plugin->getVersion();
                }
                if (substr($file, -4) == '.css') {
                    $plugin_css_files[] = $file . '?v=' . $plugin->getVersion();
                }
            }
        }
        $page['plugin_js'] = $plugin_js_files;
        $page['plugin_css'] = $plugin_css_files;
    }
    if (isset($config['piwik'])) {
        $page['PIWIK_URL'] = $config['piwik']['url'];
        $page['PIWIK_IDSITE'] = $config['piwik']['idSite'];
        if (isset($config['piwik']['idSiteNoCharts'])) {
            $page['PIWIK_IDSITE_NO_CHARTS'] = $config['piwik']['idSiteNoCharts'];
        }
    }
    if ($config['debug']) {
        if (file_exists('../.git')) {
            // parse git branch
            $head = file_get_contents('../.git/HEAD');
            $parts = explode("/", $head);
            $branch = trim($parts[count($parts) - 1]);
            $output = array();
            exec('git rev-parse HEAD', $output);
            $commit = $output[0];
            $page['BRANCH'] = ' (<a href="https://github.com/datawrapper/datawrapper/tree/' . $commit . '">' . $branch . '</a>)';
        }
    }
}
Exemplo n.º 17
0
        $user->setResetPasswordToken($token);
        $user->save();
        $protocol = get_current_protocol();
        $passwordResetLink = $protocol . '://' . $GLOBALS['dw_config']['domain'] . '/account/reset-password/' . $token;
        include ROOT_PATH . 'lib/templates/password-reset-email.php';
        dw_send_support_email($user->getEmail(), __('Datawrapper: You requested a reset of your password'), $password_reset_mail, array('name' => $user->guessName(), 'password_reset_link' => $passwordResetLink));
        ok(__('You should soon receive an email with further instructions.'));
    } else {
        error('login-email-unknown', __('The email is not registered yet.'));
    }
});
/*
 * endpoint for re-sending the activation link to a user
 */
$app->post('/account/resend-activation', function () use($app) {
    $user = DatawrapperSession::getUser();
    $token = $user->getActivateToken();
    if (!empty($token)) {
        // check how often the activation email has been send
        // we don't want to send it too often in order to prevent
        // mail spam coming from our server
        $r = ActionQuery::create()->filterByUser($user)->filterByKey('resend-activation')->find();
        if (count($r) > 2) {
            error('avoid-spam', str_replace('%support_email%', $GLOBALS['dw_config']['email']['support'], __('You already resent the activation mail three times, now. Please <a href="mailto:%support_email%">contact an administrator</a> to proceed with your account activation.')));
            return false;
        }
        // remember that we send the email
        Action::logAction($user, 'resend-activation', $token);
        // send email with activation key
        $domain = $GLOBALS['dw_config']['domain'];
        $protocol = get_current_protocol();
Exemplo n.º 18
0
 public function getConfig()
 {
     if (isset($GLOBALS['dw_config']['plugins'][$this->getName()])) {
         $cfg = $GLOBALS['dw_config']['plugins'][$this->getName()];
     } else {
         $cfg = array();
     }
     // apply organization-specific custom configuration
     $org = DatawrapperSession::getUser()->getCurrentOrganization();
     if (!empty($org)) {
         $pd = PluginDataQuery::create()->filterByPlugin($this->getPluginOM())->where('PluginData.Key LIKE ?', 'custom_config/' . $org->getId() . '/%')->find();
         foreach ($pd as $c) {
             $k = explode('/', $c->getKey());
             $k = explode('.', $k[2]);
             if (count($k) == 1) {
                 $cfg[$k[0]] = $c->getData();
             } else {
                 if (count($k) == 2) {
                     $cfg[$k[0]][$k[1]] = $c->getData();
                 } else {
                     if (count($k) == 3) {
                         $cfg[$k[0]][$k[1]][$k[2]] = $c->getData();
                     } else {
                         if (count($k) == 4) {
                             $cfg[$k[0]][$k[1]][$k[2]][$k[3]] = $c->getData();
                         }
                     }
                 }
             }
         }
     }
     return $cfg;
 }