<commit_before><?php
/**
 * [Router description]
 * @uses Altorouter
 */
class Router
{

  public static function run()
  {
    $get = [];

    $router = new AltoRouter();

    $router->setBasePath(cfg::get('rewriteBase') === '/' ? false : cfg::get('rewriteBase'));

    $controller = new Controller($_GET, $_POST, $_REQUEST);

    $router->map( 'GET', "/controller.php", function() use ($controller) {
      $controller->route();
      return false;
    }, 'controller');

    $router->map( 'GET', "/robots.txt", function() use ($controller) {
      $controller->route('seo_ctrl', 'robots');
      return false;
    }, 'robots');

    $router->map( 'GET', "/sitemap.xml", function() use ($controller) {
      $controller->route('seo_ctrl', 'sitemap');
      return false;
    }, 'sitemap');

    $router->map( 'GET', "/feed/rss", function() use ($controller) {
      $controller->route('feeds_ctrl', 'rss2');
      return false;
    }, 'rss');

    $router->map( 'GET', "/feed/atom", function() use ($controller) {
      $controller->route('feeds_ctrl', 'atom');
      return false;
    }, 'atom');

    $router->map( 'GET', "/oai", function() use ($controller) {
      $controller->route('OAI_ctrl', 'run');
      return false;
    }, 'oai');

    $router->map( 'GET', "/api", function() use ($controller) {
      $controller->route('api_ctrl', 'run');
      return false;
    }, 'api');

    $router->map( 'GET', "/download/[*:querystring]", function($querystring) use ($controller) {
      $controller->route('download_ctrl', 'go', ['file' => $querystring]);
      return false;
    }, 'download');

    $router->map( 'GET', "/admin", function() {
      require __DIR__ . '/../admin.php';
    }, 'admin');

    $router->addMatchTypes(array('lng' => '[a-z]{2}'));
    $router->addMatchTypes(array('art' => '[a-zA-Z0-9-_\/]*'));
    $router->addMatchTypes(array('string' => '[a-zA-Z0-9-_]*'));

    $router->map( 'GET', "/[lng:lng]?/?", function($lng = false) {
      return [
        'lang' => $lng
      ];
    }, 'home');

    $router->map( 'GET', "/[lng:lng]?/[art:art]", function($lng = false, $art) {
      return [
        'lang' => $lng,
        'art_title' => $art
      ];
    }, 'article');

    $router->map( 'GET', "/[lng:lng]?/[art:art].draft", function($lng = false, $art) {
      return [
        'lang' => $lng,
        'art_title' => $art,
        'draft' => true
      ];
    }, 'draft');

    $router->map( 'GET', "/[lng:lng]?/[tag:tags].all/[i:pg]?", function($lng = false, $tags, $pg = false) {
      return [
        'lang' => $lng,
        'tags' => $tags,
        'page' => $pg
      ];
    }, 'tags');

    $router->map( 'GET', "/[lng:lng]?/search:[:string]/[i:pg]?", function($lng = false, $string, $pg = false) {
      return [
        'lang' => $lng,
        'search' => $string,
        'page' => $pg
      ];
    }, 'search');

    $router->map( 'GET', "/[lng:lng]?/?[**:requestedUrl]", function($lng = false, $requestedUrl) {
      return [
        'lang' => $lng,
        'art_title' => $requestedUrl,
        'default' => true
      ];
    }, 'default');


    $match = $router->match();

    if( $match && is_callable( $match['target'] ) ) {
      $get = call_user_func_array( $match['target'], $match['params'] );

      if(!$get) {
        return;
      }
    } else {

      // $get = [ 'art_title' => 'not_found' ];
      header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
    }

    echo self::frontend($get);
  }






  private static function frontend($get)
  {
    $outHtml = new OutHtml($get, $_SESSION['lang']);

    $settings = unserialize(CACHE);
    $settings['autoescape'] = false;

    $twig = new Twig_Environment(
      new Twig_Loader_Filesystem('./sites/default' . ($_SESSION['sandbox'] ? '/' . $_SESSION['sandbox'] : '')),
      $settings
    );

    if ($_SESSION['debug'])
    {
      $twig->addExtension(new Twig_Extension_Debug());
    }
    // TODO: document intersect
    $intersect = new Twig_SimpleFunction('intersect', function () {
      return array_values(call_user_func_array('array_intersect',func_get_args()));
    });
    $twig->addFunction($intersect);

    $fn_file_exists = new Twig_SimpleFunction('file_exists', function ($file) {
      return file_exists($file);
    });
    $twig->addFunction($fn_file_exists);

    $filter = new Twig_SimpleFilter('parseTags', function ($string) {
        return customTags::parseContent($string, $outHtml);
      });

    $twig->addFilter($filter);

    return $twig->render('index.twig', array(
        'html'=>$outHtml
    ));
  }
}
/*
#sitemap
# RewriteRule ^sitemap.xml$ controller.php?obj=seo_ctrl&method=sitemap

# www to non-www url syntax
# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

#safe site, js and css, tiny_mce
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule (css|sites|js|img|xsl|fonts)/(.+) $1/$2

#admin
# RewriteRule ^admin/?$ admin.php [L]

#safe home page
# RewriteRule ^([a-z]{2})/?$ index.php?lang=$1  [L]

#tags: (ln/)some-tag.all !paginate
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_+~]+)\.all(/[0-9]{1,2})?$ index.php?lang=$1&tags=$2&page=$3 [QSA,L]
# RewriteRule ^([a-zA-Z0-9-_+~]+)\.all(/[0-9]{1,2})?$ index.php?tags=$1&page=$2 [QSA,L]

#article title: (ln/)sometitle
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_]+)/?$ index.php?lang=$1&art_title=$2 [QSA,L]
# RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?art_title=$1 [QSA,L]

#Drafts: article title: (ln/)sometitle.draft
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_]+).draft$ index.php?lang=$1&art_title=$2&draft=1 [L]
# RewriteRule ^([a-zA-Z0-9-_]+).draft$ index.php?art_title=$1&draft=true [L]

#search string: (ln/)search:some string !paginate
# RewriteRule ^([a-z]{2})/search:([^\/]+)(/[0-9]{1,2})?$ index.php?lang=$1&search=$2&page=$3 [QSA,L]
# RewriteRule ^search:([^\/]+)(/[0-9]{1,2})?$ index.php?search=$1&page=$2 [QSA,L]

#search tags: (ln/)some-tags.search !paginate
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_+~]+).search(/[0-9]{1,2})?$ index.php?lang=$1&tags=$2&is_search=1&page=$3 [QSA,L]
# RewriteRule ^([a-zA-Z0-9-_+~]+).search(/[0-9]{1,2})?$ index.php?tags=$1&is_search=1&page=$2 [QSA,L]

#all non existing, non image content to index.php http://jrgns.net/redirect_request_to_index/
# RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule . index.php?art_title=not_found [L]
#ErrorDocument 404 /index.php?art_title=not_found

<FilesMatch "\.(htaccess|htpasswd|ini|log|inc|bak|sqlite|json)$">
	order allow,deny
	deny from all
</FilesMatch>

<FilesMatch "\.(png|jpg|jpeg|gif)$">
	Header set Access-Control-Allow-Origin "*"
</FilesMatch>

<IfModule mod_deflate.c>

# Force compression for mangled headers.
# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping<commit_msg>Added support for + in articles and ~ in tags<commit_after><?php
/**
 * [Router description]
 * @uses Altorouter
 */
class Router
{

  public static function run()
  {
    $get = [];

    $router = new AltoRouter();

    $router->setBasePath(cfg::get('rewriteBase') === '/' ? false : cfg::get('rewriteBase'));

    $controller = new Controller($_GET, $_POST, $_REQUEST);

    $router->map( 'GET', "/controller.php", function() use ($controller) {
      $controller->route();
      return false;
    }, 'controller');

    $router->map( 'GET', "/robots.txt", function() use ($controller) {
      $controller->route('seo_ctrl', 'robots');
      return false;
    }, 'robots');

    $router->map( 'GET', "/sitemap.xml", function() use ($controller) {
      $controller->route('seo_ctrl', 'sitemap');
      return false;
    }, 'sitemap');

    $router->map( 'GET', "/feed/rss", function() use ($controller) {
      $controller->route('feeds_ctrl', 'rss2');
      return false;
    }, 'rss');

    $router->map( 'GET', "/feed/atom", function() use ($controller) {
      $controller->route('feeds_ctrl', 'atom');
      return false;
    }, 'atom');

    $router->map( 'GET', "/oai", function() use ($controller) {
      $controller->route('OAI_ctrl', 'run');
      return false;
    }, 'oai');

    $router->map( 'GET', "/api", function() use ($controller) {
      $controller->route('api_ctrl', 'run');
      return false;
    }, 'api');

    $router->map( 'GET', "/download/[*:querystring]", function($querystring) use ($controller) {
      $controller->route('download_ctrl', 'go', ['file' => $querystring]);
      return false;
    }, 'download');

    $router->map( 'GET', "/admin", function() {
      require __DIR__ . '/../admin.php';
    }, 'admin');

    $router->addMatchTypes(array('lng' => '[a-z]{2}'));
    $router->addMatchTypes(array('tag' => '[a-zA-Z0-9-_~]*'));
    $router->addMatchTypes(array('art' => '[a-zA-Z0-9-_\/]*'));
    $router->addMatchTypes(array('string' => '[a-zA-Z0-9-_+]*'));

    $router->map( 'GET', "/[lng:lng]?/?", function($lng = false) {
      return [
        'lang' => $lng
      ];
    }, 'home');

    $router->map( 'GET', "/[lng:lng]?/[art:art]", function($lng = false, $art) {
      return [
        'lang' => $lng,
        'art_title' => $art
      ];
    }, 'article');

    $router->map( 'GET', "/[lng:lng]?/[art:art].draft", function($lng = false, $art) {
      return [
        'lang' => $lng,
        'art_title' => $art,
        'draft' => true
      ];
    }, 'draft');

    $router->map( 'GET', "/[lng:lng]?/[tag:tags].all/[i:pg]?", function($lng = false, $tags, $pg = false) {
      return [
        'lang' => $lng,
        'tags' => $tags,
        'page' => $pg
      ];
    }, 'tags');

    $router->map( 'GET', "/[lng:lng]?/search:[:string]/[i:pg]?", function($lng = false, $string, $pg = false) {
      return [
        'lang' => $lng,
        'search' => $string,
        'page' => $pg
      ];
    }, 'search');

    $router->map( 'GET', "/[lng:lng]?/?[**:requestedUrl]", function($lng = false, $requestedUrl) {
      return [
        'lang' => $lng,
        'art_title' => $requestedUrl,
        'default' => true
      ];
    }, 'default');


    $match = $router->match();

    if( $match && is_callable( $match['target'] ) ) {
      $get = call_user_func_array( $match['target'], $match['params'] );

      if(!$get) {
        return;
      }
    } else {

      // $get = [ 'art_title' => 'not_found' ];
      header( $_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
    }

    echo self::frontend($get);
  }






  private static function frontend($get)
  {
    $outHtml = new OutHtml($get, $_SESSION['lang']);

    $settings = unserialize(CACHE);
    $settings['autoescape'] = false;

    $twig = new Twig_Environment(
      new Twig_Loader_Filesystem('./sites/default' . ($_SESSION['sandbox'] ? '/' . $_SESSION['sandbox'] : '')),
      $settings
    );

    if ($_SESSION['debug'])
    {
      $twig->addExtension(new Twig_Extension_Debug());
    }
    // TODO: document intersect
    $intersect = new Twig_SimpleFunction('intersect', function () {
      return array_values(call_user_func_array('array_intersect',func_get_args()));
    });
    $twig->addFunction($intersect);

    $fn_file_exists = new Twig_SimpleFunction('file_exists', function ($file) {
      return file_exists($file);
    });
    $twig->addFunction($fn_file_exists);

    $filter = new Twig_SimpleFilter('parseTags', function ($string) {
        return customTags::parseContent($string, $outHtml);
      });

    $twig->addFilter($filter);

    return $twig->render('index.twig', array(
        'html'=>$outHtml
    ));
  }
}
/*
#sitemap
# RewriteRule ^sitemap.xml$ controller.php?obj=seo_ctrl&method=sitemap

# www to non-www url syntax
# RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
# RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

#safe site, js and css, tiny_mce
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule (css|sites|js|img|xsl|fonts)/(.+) $1/$2

#admin
# RewriteRule ^admin/?$ admin.php [L]

#safe home page
# RewriteRule ^([a-z]{2})/?$ index.php?lang=$1  [L]

#tags: (ln/)some-tag.all !paginate
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_+~]+)\.all(/[0-9]{1,2})?$ index.php?lang=$1&tags=$2&page=$3 [QSA,L]
# RewriteRule ^([a-zA-Z0-9-_+~]+)\.all(/[0-9]{1,2})?$ index.php?tags=$1&page=$2 [QSA,L]

#article title: (ln/)sometitle
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_]+)/?$ index.php?lang=$1&art_title=$2 [QSA,L]
# RewriteRule ^([a-zA-Z0-9-_]+)/?$ index.php?art_title=$1 [QSA,L]

#Drafts: article title: (ln/)sometitle.draft
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_]+).draft$ index.php?lang=$1&art_title=$2&draft=1 [L]
# RewriteRule ^([a-zA-Z0-9-_]+).draft$ index.php?art_title=$1&draft=true [L]

#search string: (ln/)search:some string !paginate DROP SUPPORT
# RewriteRule ^([a-z]{2})/search:([^\/]+)(/[0-9]{1,2})?$ index.php?lang=$1&search=$2&page=$3 [QSA,L]
# RewriteRule ^search:([^\/]+)(/[0-9]{1,2})?$ index.php?search=$1&page=$2 [QSA,L]

#search tags: (ln/)some-tags.search !paginate
# RewriteRule ^([a-z]{2})/([a-zA-Z0-9-_+~]+).search(/[0-9]{1,2})?$ index.php?lang=$1&tags=$2&is_search=1&page=$3 [QSA,L]
# RewriteRule ^([a-zA-Z0-9-_+~]+).search(/[0-9]{1,2})?$ index.php?tags=$1&is_search=1&page=$2 [QSA,L]

#all non existing, non image content to index.php http://jrgns.net/redirect_request_to_index/
# RewriteCond %{REQUEST_URI} !\.(gif|jpg|png)$
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule . index.php?art_title=not_found [L]
#ErrorDocument 404 /index.php?art_title=not_found

<FilesMatch "\.(htaccess|htpasswd|ini|log|inc|bak|sqlite|json)$">
	order allow,deny
	deny from all
</FilesMatch>

<FilesMatch "\.(png|jpg|jpeg|gif)$">
	Header set Access-Control-Allow-Origin "*"
</FilesMatch>

<IfModule mod_deflate.c>

# Force compression for mangled headers.
# http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping
{'fraction_non_alphanumeric': 0.2360433801586724, 'fraction_numerical': 0.014120387218866002, 'mean_word_length': 3.455252918287938, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7340706', 'n_tokens_mistral': 5380, 'n_tokens_neox': 5145, 'n_words': 870}