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

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

permalinks: admin fix on some tricky cases when setting a new permalinks.inc.php
permalinks: added ability to sort the permalink tables in the admin
added new action in category_cats.inc.php

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.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-03-06 02:07:15 +0000 (Tue, 06 Mar 2007) $
8// | last modifier : $Author: rvelices $
9// | revision      : $Revision: 1873 $
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/** deletes the permalink associated with a category
27 * returns true on success
28 * @param int cat_id the target category id
29 * @param boolean save if true, the current category-permalink association
30 * is saved in the old permalinks table in case external links hit it
31 */
32function delete_cat_permalink( $cat_id, $save )
33{
34  global $page, $cache;
35  $query = '
36SELECT permalink
37  FROM '.CATEGORIES_TABLE.'
38  WHERE id="'.$cat_id.'"
39;';
40  $result = pwg_query($query);
41  if ( mysql_num_rows($result) )
42  {
43    list($permalink) = mysql_fetch_array($result);
44  }
45  if ( !isset($permalink) )
46  {// no permalink; nothing to do
47    return true;
48  }
49  if ($save)
50  {
51    $old_cat_id = get_cat_id_from_old_permalink($permalink, false);
52    if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
53    {
54      $page['errors'][] = 
55        sprintf( 
56          l10n('Permalink_%s_histo_used_by_%s'),
57          $permalink, $old_cat_id
58        );
59      return false;
60    }
61  }
62  $query = '
63UPDATE '.CATEGORIES_TABLE.'
64  SET permalink=NULL
65  WHERE id='.$cat_id.'
66  LIMIT 1';
67  pwg_query($query);
68 
69  unset( $cache['cat_names'] ); //force regeneration
70  if ($save)
71  {
72    if ( isset($old_cat_id) )
73    {
74      $query = '
75UPDATE '.OLD_PERMALINKS_TABLE.'
76  SET date_deleted=NOW()
77  WHERE cat_id='.$cat_id.' AND permalink="'.$permalink.'"';
78    }
79    else
80    {
81      $query = '
82INSERT INTO '.OLD_PERMALINKS_TABLE.'
83  (permalink, cat_id, date_deleted)
84VALUES
85  ( "'.$permalink.'",'.$cat_id.',NOW() )';
86    }
87    pwg_query( $query );
88  }
89  return true;
90}
91
92/** sets a new permalink for a category
93 * returns true on success
94 * @param int cat_id the target category id
95 * @param string permalink the new permalink
96 * @param boolean save if true, the current category-permalink association
97 * is saved in the old permalinks table in case external links hit it
98 */
99function set_cat_permalink( $cat_id, $permalink, $save )
100{
101  global $page, $cache;
102 
103  $sanitized_permalink = preg_replace( '#[^a-zA-Z0-9_-]#', '' ,$permalink);
104  if ( $sanitized_permalink != $permalink 
105      or preg_match( '#^(\d)+(-.*)?$#', $permalink) )
106  {
107    $page['errors'][] = l10n('Permalink_name_rule');
108    return false;
109  }
110 
111  // check if the new permalink is actively used
112  $existing_cat_id = get_cat_id_from_permalink( $permalink );
113  if ( isset($existing_cat_id) )
114  {
115    if ( $existing_cat_id==$cat_id )
116    {// no change required
117      return true;
118    }
119    else
120    {
121      $page['errors'][] = 
122        sprintf( 
123          l10n('Permalink %s is already used by category %s'),
124          $permalink, $existing_cat_id 
125        );
126      return false;
127    }
128  }
129
130  // check if the new permalink was historically used
131  $old_cat_id = get_cat_id_from_old_permalink($permalink, false);
132  if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
133  {
134    $page['errors'][] = 
135      sprintf( 
136        l10n('Permalink_%s_histo_used_by_%s'),
137        $permalink, $old_cat_id
138      );
139    return false;
140  }
141
142  if ( !delete_cat_permalink($cat_id, $save ) )
143  {
144    return false;
145  }
146
147  if ( isset($old_cat_id) )
148  {// the new permalink must not be active and old at the same time
149    assert( $old_cat_id==$cat_id );
150    $query = '
151DELETE FROM '.OLD_PERMALINKS_TABLE.'
152  WHERE cat_id='.$old_cat_id.' AND permalink="'.$permalink.'"';
153    pwg_query($query);
154  }
155 
156  $query = '
157UPDATE '.CATEGORIES_TABLE.'
158  SET permalink="'.$permalink.'"
159  WHERE id='.$cat_id.'
160  LIMIT 1';
161  pwg_query($query);
162
163  unset( $cache['cat_names'] ); //force regeneration
164 
165  return true;
166}
167
168?>
Note: See TracBrowser for help on using the repository browser.