Ignore:
Timestamp:
Apr 26, 2007, 5:07:50 AM (17 years ago)
Author:
rvelices
Message:

put some code from the huge section_init to 2 functions

  • parse_section_url does the exact opposite of make_section_in_url
  • parse_well_known_params_url does the exact opposite of add_well_known_params_in_url

section_init is more readable and plugins can have their own views with the same url style
this is not a bug correction but I will merge it to branch 1_7 to take advantage for a plugin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_url.inc.php

    r1956 r1980  
    431431
    432432/**
     433 * the reverse of make_section_in_url
     434 * returns the 'section' (categories/tags/...) and the data associated with it
     435 *
     436 * Depending on section, other parameters are returned (category/tags/list/...)
     437 *
     438 * @param array of url tokens to parse
     439 * @param int the index in the array of url tokens; in/out
     440 * @return array
     441 */
     442function parse_section_url( $tokens, &$next_token)
     443{
     444  $page=array();
     445  if (0 === strpos(@$tokens[$next_token], 'categor'))
     446  {
     447    $page['section'] = 'categories';
     448    $next_token++;
     449
     450    if (isset($tokens[$next_token]) )
     451    {
     452      if (preg_match('/^(\d+)(?:-(.+))?$/', $tokens[$next_token], $matches))
     453      {
     454        if ( isset($matches[2]) )
     455          $page['hit_by']['cat_url_name'] = $matches[2];
     456        $page['category'] = $matches[1];
     457        $next_token++;
     458      }
     459      else
     460      {
     461        if ( strpos($tokens[$next_token], 'created-')!==0
     462            and strpos($tokens[$next_token], 'posted-')!==0
     463            and strpos($tokens[$next_token], 'start-')!==0
     464            and $tokens[$next_token] != 'flat')
     465        {// try a permalink
     466          $cat_id = get_cat_id_from_permalink($tokens[$next_token]);
     467          if ( !isset($cat_id) )
     468          {//try old permalink
     469            $cat_id = get_cat_id_from_old_permalink($tokens[$next_token], true);
     470          }
     471          if ( isset($cat_id) )
     472          {
     473            $page['category'] = $cat_id;
     474            $page['hit_by']['cat_permalink'] = $tokens[$next_token];
     475          }
     476          else
     477          {
     478            page_not_found('Permalink for album not found');
     479          }
     480          $next_token++;
     481        }
     482       }
     483    }
     484
     485    if (isset($page['category']))
     486    {
     487      $result = get_cat_info($page['category']);
     488      if (empty($result))
     489      {
     490        page_not_found('Requested category does not exist' );
     491      }
     492      $page['category']=$result;
     493    }
     494  }
     495  else if (0 === strpos(@$tokens[$next_token], 'tag'))
     496  {
     497    $page['section'] = 'tags';
     498    $page['tags'] = array();
     499
     500    $next_token++;
     501    $i = $next_token;
     502
     503    $requested_tag_ids = array();
     504    $requested_tag_url_names = array();
     505
     506    while (isset($tokens[$i]))
     507    {
     508      if ( preg_match('/^(created-|posted-|start-(\d)+)/', $tokens[$i]) )
     509        break;
     510
     511      if ( preg_match('/^(\d+)(?:-(.*))?/', $tokens[$i], $matches) )
     512      {
     513        array_push($requested_tag_ids, $matches[1]);
     514      }
     515      else
     516      {
     517        array_push($requested_tag_url_names, $tokens[$i]);
     518      }
     519      $i++;
     520    }
     521    $next_token = $i;
     522
     523    if ( empty($requested_tag_ids) && empty($requested_tag_url_names) )
     524    {
     525      bad_request('at least one tag required');
     526    }
     527
     528    $page['tags'] = find_tags($requested_tag_ids, $requested_tag_url_names);
     529    if ( empty($page['tags']) )
     530    {
     531      page_not_found('Requested tag does not exist', get_root_url().'tags.php' );
     532    }
     533  }
     534  else if (0 === strpos(@$tokens[$next_token], 'fav'))
     535  {
     536    $page['section'] = 'favorites';
     537    $next_token++;
     538  }
     539  else if ('most_visited' == @$tokens[$next_token])
     540  {
     541    $page['section'] = 'most_visited';
     542    $next_token++;
     543  }
     544  else if ('best_rated' == @$tokens[$next_token])
     545  {
     546    $page['section'] = 'best_rated';
     547    $next_token++;
     548  }
     549  else if ('recent_pics' == @$tokens[$next_token])
     550  {
     551    $page['section'] = 'recent_pics';
     552    $next_token++;
     553  }
     554  else if ('recent_cats' == @$tokens[$next_token])
     555  {
     556    $page['section'] = 'recent_cats';
     557    $next_token++;
     558  }
     559  else if ('search' == @$tokens[$next_token])
     560  {
     561    $page['section'] = 'search';
     562    $next_token++;
     563
     564    preg_match('/(\d+)/', @$tokens[$next_token], $matches);
     565    if (!isset($matches[1]))
     566    {
     567      bad_request('search identifier is missing');
     568    }
     569    $page['search'] = $matches[1];
     570    $next_token++;
     571  }
     572  else if ('list' == @$tokens[$next_token])
     573  {
     574    $page['section'] = 'list';
     575    $next_token++;
     576
     577    $page['list'] = array();
     578
     579    // No pictures
     580    if (empty($tokens[$next_token]))
     581    {
     582      // Add dummy element list
     583      array_push($page['list'], -1);
     584    }
     585    // With pictures list
     586    else
     587    {
     588      if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
     589      {
     590        bad_request('wrong format on list GET parameter');
     591      }
     592      foreach (explode(',', $tokens[$next_token]) as $image_id)
     593      {
     594        array_push($page['list'], $image_id);
     595      }
     596    }
     597    $next_token++;
     598  }
     599  return $page;
     600}
     601
     602/**
     603 * the reverse of add_well_known_params_in_url
     604 * parses start, flat and chronology from url tokens
     605*/
     606function parse_well_known_params_url($tokens, $i)
     607{
     608  $page = array();
     609  while (isset($tokens[$i]))
     610  {
     611    if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
     612    {
     613      $page['start'] = $matches[1];
     614    }
     615
     616    if ( 'flat' == $tokens[$i] )
     617    {
     618      // indicate a special list of images
     619      $page['flat'] = true;
     620    }
     621
     622    if (preg_match('/^(posted|created)/', $tokens[$i] ))
     623    {
     624      $chronology_tokens = explode('-', $tokens[$i] );
     625
     626      $page['chronology_field'] = $chronology_tokens[0];
     627
     628      array_shift($chronology_tokens);
     629      $page['chronology_style'] = $chronology_tokens[0];
     630
     631      array_shift($chronology_tokens);
     632      if ( count($chronology_tokens)>0 )
     633      {
     634        if ('list'==$chronology_tokens[0] or
     635            'calendar'==$chronology_tokens[0])
     636        {
     637          $page['chronology_view'] = $chronology_tokens[0];
     638          array_shift($chronology_tokens);
     639        }
     640        $page['chronology_date'] = $chronology_tokens;
     641      }
     642    }
     643    $i++;
     644  }
     645  return $page;
     646}
     647
     648/**
    433649 * Indicate to build url with full path
    434650 *
Note: See TracChangeset for help on using the changeset viewer.