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

Last change on this file since 2588 was 2299, checked in by plg, 17 years ago

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

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