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

Last change on this file since 8728 was 8728, checked in by plg, 13 years ago

Happy new year 2011

Change "Piwigo - a PHP based picture gallery" into "Piwigo - a PHP based photo gallery"

  • Property svn:eol-style set to LF
File size: 6.2 KB
RevLine 
[1866]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[8728]5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
[2297]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// +-----------------------------------------------------------------------+
[1866]23
[2047]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.'
[6550]31  WHERE permalink=\''.$permalink.'\'';
[2047]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
[6664]50  WHERE op.permalink=\''.$permalink.'\'
[2047]51  LIMIT 1';
52  $result = pwg_query($query);
53  $cat_id = null;
[4325]54  if ( pwg_db_num_rows($result) )
55    list( $cat_id ) = pwg_db_fetch_row($result);
[2047]56  return $cat_id;
57}
58
59
[1866]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.'
[6550]72  WHERE id=\''.$cat_id.'\'
[1866]73;';
74  $result = pwg_query($query);
[4325]75  if ( pwg_db_num_rows($result) )
[1866]76  {
[4325]77    list($permalink) = pwg_db_fetch_row($result);
[1866]78  }
79  if ( !isset($permalink) )
80  {// no permalink; nothing to do
81    return true;
82  }
83  if ($save)
84  {
[2047]85    $old_cat_id = get_cat_id_from_old_permalink($permalink);
[1866]86    if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
87    {
88      $page['errors'][] = 
89        sprintf( 
[6993]90          l10n('Permalink %s has been previously used by album %s. Delete from the permalink history first'),
[1866]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()
[6550]111  WHERE cat_id='.$cat_id.' AND permalink=\''.$permalink.'\'';
[1866]112    }
113    else
114    {
115      $query = '
116INSERT INTO '.OLD_PERMALINKS_TABLE.'
117  (permalink, cat_id, date_deleted)
118VALUES
[6550]119  ( \''.$permalink.'\','.$cat_id.',NOW() )';
[1866]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 
[2047]137  $sanitized_permalink = preg_replace( '#[^a-zA-Z0-9_/-]#', '' ,$permalink);
138  $sanitized_permalink = trim($sanitized_permalink, '/');
139  $sanitized_permalink = str_replace('//', '/', $sanitized_permalink);
[1866]140  if ( $sanitized_permalink != $permalink 
[1873]141      or preg_match( '#^(\d)+(-.*)?$#', $permalink) )
[1866]142  {
[5207]143    $page['errors'][] = l10n('The permalink name must be composed of a-z, A-Z, 0-9, "-", "_" or "/". It must not be numeric or start with number followed by "-"');
[1866]144    return false;
145  }
146 
[1873]147  // check if the new permalink is actively used
[1866]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( 
[6993]159          l10n('Permalink %s is already used by album %s'),
[1866]160          $permalink, $existing_cat_id 
161        );
162      return false;
163    }
164  }
165
[1873]166  // check if the new permalink was historically used
[2047]167  $old_cat_id = get_cat_id_from_old_permalink($permalink);
[1873]168  if ( isset($old_cat_id) and $old_cat_id!=$cat_id )
[1866]169  {
[1873]170    $page['errors'][] = 
171      sprintf( 
[6993]172        l10n('Permalink %s has been previously used by album %s. Delete from the permalink history first'),
[1873]173        $permalink, $old_cat_id
174      );
175    return false;
[1866]176  }
[1873]177
[1866]178  if ( !delete_cat_permalink($cat_id, $save ) )
179  {
180    return false;
181  }
182
[1873]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.'
[6550]188  WHERE cat_id='.$old_cat_id.' AND permalink=\''.$permalink.'\'';
[1873]189    pwg_query($query);
190  }
191 
[1866]192  $query = '
193UPDATE '.CATEGORIES_TABLE.'
[6550]194  SET permalink=\''.$permalink.'\'
[4833]195  WHERE id='.$cat_id;
196  //  LIMIT 1';
[1866]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.