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); }); }
public function init() { $plugin = $this; // register plugin controller DatawrapperHooks::register(DatawrapperHooks::GET_ADMIN_PAGES, function () use($plugin) { return array('url' => '/users', 'title' => __('Users', $plugin->getName()), 'controller' => array($plugin, 'users'), 'order' => '2'); }); $this->declareAssets(array('vendor/serious-toolkit/serious-widget.js', 'dw.admin.users.js', 'users.css'), "|/admin/users|"); $user = DatawrapperSession::getUser(); if ($user->isAdmin()) { $this->registerController(function ($app) use($plugin) { $app->get('/admin/users/:user_id', function ($uid) use($app, $plugin) { $theUser = UserQuery::create()->findPk($uid); $page = array('title' => 'Users » ' . $theUser->guessName()); // manually add the admin nav menu vars global $__dw_admin_pages; foreach ($__dw_admin_pages as $adm_pg) { $page['adminmenu'][$adm_pg['url']] = $adm_pg['title']; } add_header_vars($page, 'admin'); $page['the_user'] = $theUser; $page['userPlugins'] = DatawrapperPluginManager::getUserPlugins($theUser->getId(), false); $app->render('plugins/admin-users/admin-user-detail.twig', $page); }); }); } }
/** * */ function error_page($step, $title, $message, $options = false, $status = 500) { global $app; $tmpl = array('title' => $title, 'message' => $message, 'options' => $options); $app->status($status); add_header_vars($tmpl, $step); $app->render('error.twig', $tmpl); }
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); }
<?php require_once ROOT_PATH . 'lib/utils/themes.php'; require_once ROOT_PATH . 'vendor/jsmin/jsmin.php'; /* * PUBLISH STEP - shows progress of publishing action and thumbnail generation * forwards to /chart/:id/finish */ $app->get('/chart/:id/publish', function ($id) use($app) { disable_cache($app); check_chart_writable($id, function ($user, $chart) use($app) { $cfg = $GLOBALS['dw_config']; $page = array('title' => $chart->getID() . ' :: ' . __('Publish'), 'chartData' => $chart->loadData(), 'chart' => $chart, 'visualizations' => DatawrapperVisualization::all(), 'vis' => DatawrapperVisualization::get($chart->getType()), 'chartUrl' => $chart->getPublicUrl(), 'chartUrlLocal' => '/chart/' . $chart->getID() . '/preview', 'themes' => DatawrapperTheme::all(), 'exportStaticImage' => !empty($cfg['phantomjs']), 'chartActions' => DatawrapperHooks::execute(DatawrapperHooks::GET_CHART_ACTIONS, $chart), 'estExportTime' => ceil(JobQuery::create()->estimatedTime('export_image') / 60)); add_header_vars($page, 'chart', 'chart-editor/publish.css'); add_editor_nav($page, 4); if ($user->isAbleToPublish() && ($chart->getLastEditStep() == 3 || $app->request()->get('republish') == 1)) { // actual publish process $chart->publish(); $page['chartUrl'] = $chart->getPublicUrl(); // generate thumbnails $page['publish'] = true; $page['republish'] = $app->request()->get('republish') == 1; } $app->render('chart/publish.twig', $page); }); });
<?php /* * VISUALIZE STEP */ $app->get('/chart/:id/visualize', function ($id) use($app) { disable_cache($app); check_chart_writable($id, function ($user, $chart) use($app) { $page = array('title' => $chart->getID() . ' :: ' . __('Visualize'), 'chartData' => $chart->loadData(), 'chart' => $chart, 'visualizations_deps' => DatawrapperVisualization::all('dependencies'), 'visualizations' => DatawrapperVisualization::all(), 'vis' => DatawrapperVisualization::get($chart->getType()), 'themes' => DatawrapperTheme::all(), 'theme' => DatawrapperTheme::get($chart->getTheme()), 'debug' => !empty($GLOBALS['dw_config']['debug_export_test_cases']) ? '1' : '0'); add_header_vars($page, 'chart'); add_editor_nav($page, 3); $app->render('chart/visualize.twig', $page); }); });
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()); $user->setEmail($data->email); $user->setRole('admin'); $user->setPwd(secure_password($data->pwd)); $user->setLanguage(DatawrapperSession::getLanguage());
* reloads the header menu after login/logout */ $app->get('/xhr/header/:page', function ($active) use($app) { disable_cache($app); $page = array(); add_header_vars($page, $active); $res = $app->response(); $res['Cache-Control'] = 'max-age=0'; $app->render('header.twig', $page); }); /** * reloads the header menu after login/logout */ $app->get('/xhr/home-login', function () use($app) { $page = array(); add_header_vars($page); $res = $app->response(); $res['Cache-Control'] = 'max-age=0'; $app->render('home-login.twig', $page); }); /** * reloads visualization specific options after the user * changed the visualization type */ require_once '../lib/utils/themes.php'; $app->get('/xhr/:chartid/vis-options', function ($id) use($app) { disable_cache($app); check_chart_writable($id, function ($user, $chart) use($app) { $page = array('vis' => DatawrapperVisualization::get($chart->getType()), 'theme' => DatawrapperTheme::get($chart->getTheme()), 'language' => substr(DatawrapperSession::getLanguage(), 0, 2)); $app->render('vis-options.twig', $page); });
<?php /* * DESCRIBE STEP */ $app->get('/chart/:id/describe', function ($id) use($app) { disable_cache($app); check_chart_writable($id, function ($user, $chart) use($app) { $page = array('title' => $chart->getID() . ' :: ' . __('Check & Describe'), 'chartData' => $chart->loadData(), 'chart' => $chart); add_header_vars($page, 'chart', 'chart-editor/describe.css'); add_editor_nav($page, 2); switch (substr(DatawrapperSession::getLanguage(), 0, 2)) { case 'de': $k = '.'; $d = ','; break; case 'fr': $k = ' '; $d = ','; break; default: $k = ','; $d = '.'; } $page['columntypes'] = array('text' => 'Text', 'number' => 'Number', 'date' => 'Date'); $page['numberformats'] = array('n3' => '3 (' . number_format(1234.56789, 3, $d, $k) . ')', 'n2' => '2 (' . number_format(1234.56789, 2, $d, $k) . ')', 'n1' => '1 (' . number_format(1234.56789, 1, $d, $k) . ')', 'n0' => '0 (' . number_format(1234.56789, 0, $d, $k) . ')'); $page['significantdigits'] = array('s6' => '6 (' . number_format(1234.56789, 2, $d, $k) . ')', 's5' => '5 (' . number_format(123.456789, 2, $d, $k) . ')', 's4' => '4 (' . number_format(12.34, 2, $d, $k) . ')', 's3' => '3 (' . number_format(1.23, 2, $d, $k) . ')', 's2' => '2 (' . number_format(0.12, 2, $d, $k) . ')', 's1' => '1 (' . number_format(0.01, 2, $d, $k) . ')'); $app->render('chart/describe.twig', $page); }); });
$user->save(); // notify plugins about the newly activated user DatawrapperHooks::execute(DatawrapperHooks::USER_ACTIVATED, $user); $params = '?t=s&m=' . urlencode(sprintf(__('Your email address %s has been successfully activated!'), $user->getEmail())); } } $app->redirect('/' . $params); }); /* * check invitation token and show invited page */ $app->get('/account/invite/:token', function ($token) use($app) { disable_cache($app); _checkInviteTokenAndExec($token, function ($user) use($app) { $page = array('email' => $user->getEmail(), 'auth_salt' => DW_AUTH_SALT); add_header_vars($page, 'about', 'account/invite.css'); $app->render('account/invite.twig', $page); }); }); /* * store new password, clear invitation token and login */ $app->post('/account/invite/:token', function ($token) use($app) { _checkInviteTokenAndExec($token, function ($user) use($app) { $data = json_decode($app->request()->getBody()); $user->setPwd($data->pwd); $user->setActivateToken(''); $user->save(); // notify plugins about the newly activated user DatawrapperHooks::execute(DatawrapperHooks::USER_ACTIVATED, $user); DatawrapperSession::login($user);
<?php //GET route $app->get('/account/settings/', function () use($app) { disable_cache($app); $page = array('title' => __('Settings'), 'api_user' => 'current'); add_header_vars($page, 'user'); $user = DatawrapperSession::getUser(); if ($user->getRole() == 'guest') { error_settings_need_login(); return; } if ($user->isAdmin()) { // admins can edit settings for other users $req = $app->request(); if ($req->get('uid') != null) { $u = UserQuery::create()->findPk($req->get('uid')); if ($u) { $user = $page['user'] = $u; $page['api_user'] = $user->getId(); } } } if ($app->request()->get('token')) { // look for action with this token $t = ActionQuery::create()->filterByUser($user)->filterByKey('email-change-request')->orderByActionTime('desc')->findOne(); if (!empty($t)) { // check if token is valid $params = json_decode($t->getDetails(), true); if (!empty($params['token']) && $params['token'] == $app->request()->get('token')) { // token matches
} } usort($pages, function ($a, $b) { return $a['order'] - $b['order']; }); $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(); } } }
<?php if (DatawrapperHooks::hookRegistered(DatawrapperHooks::GET_ADMIN_PAGES)) { // pull admin pages from plugins $__dw_admin_pages = DatawrapperHooks::execute(DatawrapperHooks::GET_ADMIN_PAGES); // order admin pages by index "order" usort($__dw_admin_pages, function ($a, $b) { return (isset($a['order']) ? $a['order'] : 9999) - (isset($b['order']) ? $b['order'] : 9999); }); foreach ($__dw_admin_pages as $admin_page) { $app->map('/admin' . $admin_page['url'], function () use($app, $admin_page, $__dw_admin_pages) { disable_cache($app); $user = DatawrapperSession::getUser(); if ($user->isAdmin()) { $page_vars = array('title' => $admin_page['title'], 'adminmenu' => array(), 'adminactive' => $admin_page['url']); // add admin pages to menu foreach ($__dw_admin_pages as $adm_pg) { $page_vars['adminmenu'][$adm_pg['url']] = $adm_pg['title']; } add_header_vars($page_vars, 'admin'); call_user_func_array($admin_page['controller'], array($app, $page_vars)); } else { $app->notFound(); } })->via('GET', 'POST'); } }
<?php //GET route $app->get('/account/reset-password/:token', function ($token) use($app) { disable_cache($app); $page = array(); add_header_vars($page, 'account'); if (!empty($token)) { $users = UserQuery::create()->filterByResetPasswordToken($token)->find(); if (count($users) != 1) { $page['alert'] = array('type' => 'error', 'message' => 'This activation token is invalid.'); error_invalid_password_reset_token(); } else { $user = $users[0]; // $user->setResetPasswordToken(''); // $user->save(); $page['token'] = $token; $app->render('account/reset-password.twig', $page); } } });
$sql = "SELECT type, COUNT(*) c FROM chart WHERE show_in_gallery = 1 AND deleted = 0 GROUP BY type ORDER BY c DESC ;"; $rs = $con->query($sql); $res = array(); $max = 0; foreach ($rs as $r) { $vis = DatawrapperVisualization::get($r['type']); $lang = substr(DatawrapperSession::getLanguage(), 0, 2); $res[] = array('count' => $r['c'], 'id' => $r['type'], 'name' => $vis['title']); $max = max($max, $r['c']); } foreach ($res as $c => $r) { $res[$c]['bar'] = round($r['count'] / $max * 80); } return $res; } $app->get('/gallery(/?|/by/:key/:val)', function ($key = false, $val = false) use($app) { disable_cache($app); $user = DatawrapperSession::getUser(); $curPage = $app->request()->params('page'); if (empty($curPage)) { $curPage = 0; } $perPage = 12; $filter = !empty($key) ? array($key => $val) : array(); $charts = ChartQuery::create()->getGalleryCharts($filter, $curPage * $perPage, $perPage); $total = ChartQuery::create()->countGalleryCharts($filter); $page = array('charts' => $charts, 'bymonth' => gal_nbChartsByMonth(), 'byvis' => gal_nbChartsByType(), 'key' => $key, 'val' => $val); add_pagination_vars($page, $total, $curPage, $perPage); add_header_vars($page, 'gallery'); $app->render('gallery.twig', $page); });
<?php //GET route $app->get('/account/set-password/:token', function ($token) use($app) { $page = array(); add_header_vars($page, 'about'); if (!empty($token)) { $users = UserQuery::create()->filterByActivateToken($token)->find(); $page['salt'] = DW_AUTH_SALT; if (count($users) != 1) { $page['alert'] = array('type' => 'error', 'message' => _('This activation token is invalid. Your email address is probably already activated.')); } } $app->render('set-password.twig', $page); });