source: trunk/admin/include/functions_permalinks.php @ 2047

Last change on this file since 2047 was 2047, checked in by rvelices, 17 years ago

feature 713: allow permalinks to contain the slash ("/") character

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
5// +-----------------------------------------------------------------------+
6// | file          : $Id: admin/function_permalinks.inc.php$
7// | last update   : $Date: 2007-06-28 02:16:03 +0000 (Thu, 28 Jun 2007) $
8// | last modifier : $Author: rvelices $
9// | revision      : $Revision: 2047 $
10// +-----------------------------------------------------------------------+
11// | This program is free software; you can redistribute it and/or modify  |
12// | it under the terms of the GNU General Public License as published by  |
13// | the Free Software Foundation                                          |
14// |                                                                       |
15// | This program is distributed in the hope that it will be useful, but   |
16// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
17// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
18// | General Public License for more details.                              |
19// |                                                                       |
20// | You should have received a copy of the GNU General Public License     |
21// | along with this program; if not, write to the Free Software           |
22// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
23// | USA.                                                                  |
24// +-----------------------------------------------------------------------+
25
26/** returns a category id that corresponds to the given permalink (or null)
27 * @param string permalink
28 */
29function get_cat_id_from_permalink( $permalink )
30{
31  $query ='
32SELECT id FROM '.CATEGORIES_TABLE.'
33  WHERE permalink="'.$permalink.'"';
34  $ids = array_from_query($query, 'id');
35  if (!empty($ids))
36  {
37    return $ids[0];
38  }
39  return null;
40}
41
42/** returns a category id that has used before this permalink (or null)
43 * @param string permalink
44 * @param boolean is_hit if true update the usage counters on the old permalinks
45 */
46function get_cat_id_from_old_permalink($permalink)
47{
48  $query='
49SELECT c.id
50  FROM '.OLD_PERMALINKS_TABLE.' op INNER JOIN '.CATEGORIES_TABLE.' c
51    ON op.cat_id=c.id
52  WHERE op.permalink="'.$permalink.'"
53  LIMIT 1';
54  $result = pwg_query($query);
55  $cat_id = null;
56  if ( mysql_num_rows($result) )
57    list( $cat_id ) = mysql_fetch_array($result);
58  return $cat_id;
59}
60
61
62/** deletes the permalink associated with a category
63 * returns true on success
64 * @param int cat_id the target category id
65 * @param boolean save if true, the current category-permalink association
66 * is saved in the old permalinks table in case external links hit it
67 */
68function delete_cat_permalink( $cat_id, $save )
69{
70  global $page, $cache;
71  $query = '
72SELECT permalink
73  FROM '.CATEGORIES_TABLE.'
74  WHERE id="'.$cat_id.'"
75;';
76  $result = pwg_query($query);
77  if ( mysql_num_rows($result) )
78  {
79    list($permalink) = mysql_fetch_array($result);
80  }
81  if ( !isset($permalink) )
82  {// no permalink; nothing to do
83    return true;
84  }
85  if ($save)
86  {
87    $old_cat_id = get_cat_id_from_old_permalink($permalink);
88    if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
89    {
90      $page['errors'][] = 
91        sprintf( 
92          l10n('Permalink_%s_histo_used_by_%s'),
93          $permalink, $old_cat_id
94        );
95      return false;
96    }
97  }
98  $query = '
99UPDATE '.CATEGORIES_TABLE.'
100  SET permalink=NULL
101  WHERE id='.$cat_id.'
102  LIMIT 1';
103  pwg_query($query);
104 
105  unset( $cache['cat_names'] ); //force regeneration
106  if ($save)
107  {
108    if ( isset($old_cat_id) )
109    {
110      $query = '
111UPDATE '.OLD_PERMALINKS_TABLE.'
112  SET date_deleted=NOW()
113  WHERE cat_id='.$cat_id.' AND permalink="'.$permalink.'"';
114    }
115    else
116    {
117      $query = '
118INSERT INTO '.OLD_PERMALINKS_TABLE.'
119  (permalink, cat_id, date_deleted)
120VALUES
121  ( "'.$permalink.'",'.$cat_id.',NOW() )';
122    }
123    pwg_query( $query );
124  }
125  return true;
126}
127
128/** sets a new permalink for a category
129 * returns true on success
130 * @param int cat_id the target category id
131 * @param string permalink the new permalink
132 * @param boolean save if true, the current category-permalink association
133 * is saved in the old permalinks table in case external links hit it
134 */
135function set_cat_permalink( $cat_id, $permalink, $save )
136{
137  global $page, $cache;
138 
139  $sanitized_permalink = preg_replace( '#[^a-zA-Z0-9_/-]#', '' ,$permalink);
140  $sanitized_permalink = trim($sanitized_permalink, '/');
141  $sanitized_permalink = str_replace('//', '/', $sanitized_permalink);
142  if ( $sanitized_permalink != $permalink 
143      or preg_match( '#^(\d)+(-.*)?$#', $permalink) )
144  {
145    $page['errors'][] = l10n('Permalink_name_rule');
146    return false;
147  }
148 
149  // check if the new permalink is actively used
150  $existing_cat_id = get_cat_id_from_permalink( $permalink );
151  if ( isset($existing_cat_id) )
152  {
153    if ( $existing_cat_id==$cat_id )
154    {// no change required
155      return true;
156    }
157    else
158    {
159      $page['errors'][] = 
160        sprintf( 
161          l10n('Permalink %s is already used by category %s'),
162          $permalink, $existing_cat_id 
163        );
164      return false;
165    }
166  }
167
168  // check if the new permalink was historically used
169  $old_cat_id = get_cat_id_from_old_permalink($permalink);
170  if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
171  {
172    $page['errors'][] = 
173      sprintf( 
174        l10n('Permalink_%s_histo_used_by_%s'),
175        $permalink, $old_cat_id
176      );
177    return false;
178  }
179
180  if ( !delete_cat_permalink($cat_id, $save ) )
181  {
182    return false;
183  }
184
185  if ( isset($old_cat_id) )
186  {// the new permalink must not be active and old at the same time
187    assert( $old_cat_id==$cat_id );
188    $query = '
189DELETE FROM '.OLD_PERMALINKS_TABLE.'
190  WHERE cat_id='.$old_cat_id.' AND permalink="'.$permalink.'"';
191    pwg_query($query);
192  }
193 
194  $query = '
195UPDATE '.CATEGORIES_TABLE.'
196  SET permalink="'.$permalink.'"
197  WHERE id='.$cat_id.'
198  LIMIT 1';
199  pwg_query($query);
200
201  unset( $cache['cat_names'] ); //force regeneration
202 
203  return true;
204}
205
206?>
Note: See TracBrowser for help on using the repository browser.