~quam-plures-core/quam-plures/qp5_colls-blogs_chaps-cats

« back to all changes in this revision

Viewing changes to qp_inc/_blog_main.inc.php

  • Committer: EdB
  • Date: 2013-03-12 06:26:03 UTC
  • Revision ID: 1912webworks@gmail.com-20130312062603-tnlb5zco5mglydqj
lots of changes in this branch. tested and functional, but not going into merge just yet ...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
2
/**
3
 
 * This file loads and initializes the blog to be displayed.
4
 
 *
5
 
 * This file is part of the Quam Plures project - {@link http://quamplures.net/}.
6
 
 * See also {@link https://launchpad.net/quam-plures}.
7
 
 *
8
 
 * @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
9
 
 * @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}.
10
 
 * Parts of this file are copyright (c)2004-2005 by Daniel HAHLER - {@link http://thequod.de/contact}.
11
 
 *
12
 
 * @license http://quamplures.net/license.html GNU General Public License (GPL)
13
 
 *
14
 
 * {@internal Open Source relicensing agreement:
15
 
 * Daniel HAHLER grants Francois PLANQUE the right to license
16
 
 * Daniel HAHLER's contributions to this file and the b2evolution project
17
 
 * under any OSI approved OSS license (http://www.opensource.org/licenses/).
18
 
 * }}
19
 
 *
20
 
 * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
21
 
 * @author blueyed: Daniel HAHLER
22
 
 * @author fplanque: Francois PLANQUE
23
 
 *
24
 
 * @package pond
 
3
 * This file loads and initializes the blog to be displayed
 
4
 *
 
5
 * @author {@link http://wonderwinds.com/ Ed Bennett}
 
6
 * @author {@link http://daniel.hahler.de/ Daniel HAHLER}
 
7
 * @author {@link http://fplanque.net/ Francois PLANQUE}
 
8
 * @copyright (c) 2009 by {@link http://quamplures.net/ the Quam Plures project}
 
9
 * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v3
 
10
 * @package core
25
11
 */
26
 
 
27
 
if( !defined('QP_CONFIG_LOADED') ) die( 'Please, do not access this page directly.' );
 
12
if(!defined('QP_CONFIG_LOADED')) die('fail');
28
13
 
29
14
/**
30
 
 * Initialize everything:
 
15
 * Initialize everything
31
16
 */
32
17
require_once dirname(__FILE__).'/_main.inc.php';
33
18
 
34
 
load_funcs('templates/_template.funcs.php');
35
 
load_class('items/model/_itemlist.class.php');
 
19
load_funcs( 'templates/_template.funcs.php' );
36
20
 
37
21
$Timer->start( '_blog_main.inc' );
38
22
 
39
 
 
40
 
/*
41
 
 * blog ID. This is a little bit special.
42
 
 *
43
 
 * In most cases $blog should be set by a stub file and the param() call below will just check that it's an integer.
44
 
 *
45
 
 * Note we do NOT memorize the param as we don't want it in regenerate_url() calls.
46
 
 * Whenever we do, index.php will already have called param() with memorize=true
47
 
 *
48
 
 * In some cases $blog will not have been set before and it will be set with the param() call below.
49
 
 * Currently, this only happens with the old /qp_srvc/ RSS stubs.
50
 
 */
 
23
// blog ID. This is a little bit special. In most cases $blog should be set by a
 
24
// stub file and the param() call below will just check that it's an integer. Note
 
25
// we do NOT memorize the param as we don't want it in regenerate_url() calls. Whenever
 
26
// we do, index.php will already have called param() with memorize=true In some cases
 
27
// $blog will not have been set before and it will be set with the param() call below.
 
28
// Currently, this only happens with the old /qp_srvc/ RSS stubs.
51
29
param( 'blog', 'integer', '', false );
52
30
 
53
 
// Getting current blog info:
 
31
// Getting current blog info
54
32
$BlogCache = & get_Cache( 'BlogCache' );
55
 
/**
56
 
 * @var Blog
57
 
 */
58
33
$Blog = & $BlogCache->get_by_ID( $blog, false, false );
59
34
if( empty( $Blog ) )
60
35
{
61
 
        require $templates_path.'_404_blog_not_found.main.php'; // error & exit
62
 
        // EXIT.
 
36
        // error & exit
 
37
        require $templates_path.'_404_blog_not_found.main.php';
63
38
}
64
39
 
65
 
 
66
40
// Init $disp
67
41
param( 'disp', 'string', 'posts', true );
68
42
$disp_detail = '';
69
43
 
70
 
 
71
 
/*
72
 
 * _______________________________ Locale / Charset for the Blog _________________________________
73
 
 *
74
 
        TODO: blueyed>> This should get moved as default to the locale detection in _main.inc.php,
75
 
                as we only want to activate the I/O charset, which is probably the user's..
76
 
                It prevents using a locale/charset in the front office, apart from the one given as default for the blog!!
77
 
fp>there is no blog defined in _main and there should not be any
78
 
blueyed> Sure, but that means we should either split it, or use the locale here only, if there's no-one given with higher priority.
79
 
*/
80
 
// Activate matching locale:
81
 
$Debuglog->add( 'Activating blog locale: '.$Blog->get('locale'), 'locale' );
 
44
// Locale / Charset for the Blog - activate matching locale
82
45
locale_activate( $Blog->get('locale') );
83
46
 
84
 
 
85
 
// Re-Init charset handling, in case current_charset has changed:
 
47
// Re-Init charset handling, in case current_charset has changed
86
48
if( init_charsets( $current_charset ) )
87
49
{
88
 
  // Reload Blog(s) (for encoding of name, tagline etc):
89
 
  $BlogCache->clear();
90
 
 
91
 
  $Blog = & $BlogCache->get_by_ID( $blog );
92
 
}
93
 
 
94
 
 
95
 
/*
96
 
 * _____________________________ Extra path info decoding ________________________________
97
 
 *
98
 
 * This will translate extra path into 'regular' params.
99
 
 *
100
 
 * Decoding should try to work like this:
101
 
 *
102
 
 * baseurl/blog-urlname/junk/.../junk/post-title    -> points to a single post (no ending slash)
103
 
 * baseurl/blog-urlname/junk/.../junk/p142          -> points to a single post
104
 
 * baseurl/blog-urlname/2006/                       -> points to a yearly archive because of ending slash + 4 digits
105
 
 * baseurl/blog-urlname/2006/12/                    -> points to a monthly archive
106
 
 * baseurl/blog-urlname/2006/12/31/                 -> points to a daily archive
107
 
 * baseurl/blog-urlname/2006/w53/                   -> points to a weekly archive
108
 
 * baseurl/blog-urlname/junk/.../junk/chap-urlname/ -> points to a single chapter/category (because of ending slash)
109
 
 * Note: category names cannot be named like this [a-z][0-9]+
110
 
 */
111
 
if( ! isset( $resolve_extra_path ) ) { $resolve_extra_path = true; }
 
50
        // Reload Blog(s) (for encoding of name, tagline etc)
 
51
        $BlogCache->clear();
 
52
        $Blog = & $BlogCache->get_by_ID( $blog );
 
53
}
 
54
 
 
55
// Extra path info decoding. This will translate extra path into 'regular' params.
 
56
// Decoding should try to work like this:
 
57
// - baseurl/blog-urlname/junk/.../junk/post-title -> points to a single post (no ending slash)
 
58
// - baseurl/blog-urlname/junk/.../junk/p142 -> points to a single post
 
59
// - baseurl/blog-urlname/2006/ -> points to a yearly archive because of ending slash + 4 digits
 
60
// - baseurl/blog-urlname/2006/12/ -> points to a monthly archive
 
61
// - baseurl/blog-urlname/2006/12/31/ -> points to a daily archive
 
62
// - baseurl/blog-urlname/2006/w53/ -> points to a weekly archive
 
63
// - baseurl/blog-urlname/junk/.../junk/cat-urlname/ -> points to a single category (because of ending slash)
 
64
// Note: category names cannot be named like this [a-z][0-9]+
 
65
if( ! isset( $resolve_extra_path ) )
 
66
{
 
67
        $resolve_extra_path = true;
 
68
}
 
69
 
112
70
if( $resolve_extra_path )
113
71
{
114
 
        // Check and Remove blog base URI from ReqPath:
 
72
        // Check and Remove blog base URI from ReqPath
115
73
        $blog_baseuri = substr( $Blog->gen_baseurl(), strlen( $Blog->get('baseurlroot') ) );
116
 
        $Debuglog->add( 'blog_baseuri: "'.$blog_baseuri.'"', 'params' );
117
74
 
118
 
        // Remove trailer:
 
75
        // Remove trailer
119
76
        $blog_baseuri_regexp = preg_replace( '�(\.php[0-9]?)?/?$�', '', $blog_baseuri );
120
 
        // Readd possibilities in order to get a broad match:
 
77
        // Read possibilities in order to get a broad match
121
78
        $blog_baseuri_regexp = '�^'.preg_quote( $blog_baseuri_regexp ).'(\.php[0-9]?)?/(.+)$�';
122
79
 
123
80
        if( preg_match( $blog_baseuri_regexp, $ReqPath, $matches ) )
124
 
        { // We have extra path info
 
81
        {
 
82
                // We have extra path info
125
83
                $path_string = $matches[2];
126
 
 
127
 
                $Debuglog->add( 'Extra path info found! path_string=' . $path_string , 'params' );
128
 
 
129
84
                // Replace encoded ";" and ":" with regular chars (used for tags)
130
 
                // TODO: dh> why not urldecode it altogether? fp> would prolly make sense but requires testing -- note: check with tags (move urldecode from tags up here)
131
 
                // TODO: PHP5: use str_ireplace
132
85
                $path_string = str_replace(
133
 
                        array('%3b', '%3B', '%3a', '%3A'),
134
 
                        array(';', ';', ':', ':'),
 
86
                        array( '%3b', '%3B', '%3a', '%3A' ),
 
87
                        array( ';', ';', ':', ':' ),
135
88
                        $path_string );
136
89
 
137
 
                // Slice the path:
 
90
                // Slice the path
138
91
                $path_elements = preg_split( '~/~', $path_string, 20, PREG_SPLIT_NO_EMPTY );
139
92
 
140
93
                if( isset( $path_elements[0] )
141
94
                        && ( $path_elements[0] == $Blog->stub
142
95
                                || $path_elements[0] == $Blog->urlname
143
96
                                || $path_elements[0] == 'index.php' ) )
144
 
                { // Ignore stub files, blog url names, and index.php
 
97
                {
 
98
                        // Ignore stub files, blog url names, and index.php
145
99
                        array_shift( $path_elements );
146
 
                        $Debuglog->add( 'Ignoring stub filename, blog urlname, or index.php in extra path info' , 'params' );
147
100
                }
148
101
 
149
102
                if( isset( $path_elements[0] )
150
103
                        && ( substr_count( $path_elements[0], '.' ) != 0 )
151
104
                        && is_file( $basepath.$path_elements[0] ) )
152
 
                { // Ignore filenames that exist in root
 
105
                {
 
106
                        // Ignore filenames that exist in root
153
107
                        array_shift( $path_elements );
154
 
                        $Debuglog->add( 'Ignoring filenames that exist as installation root files in extra path info' , 'params' );
155
108
                }
156
109
 
157
110
                // Do we still have extra path info to decode?
158
111
                if( count($path_elements) )
159
112
                {
160
 
                        // TODO: dh> add plugin hook here, which would allow to handle path elements (name spaces in clean URLs), and to override internal functionality (e.g. handle tags in a different way).
161
113
                        // Is this a tag ("prefix-only" mode)?
162
 
                        if( $Blog->get_setting('tag_links') == 'prefix-only'
 
114
                        if( $Blog->get_setting( 'tag_links' ) == 'prefix-only'
163
115
                                && count($path_elements) == 2
164
 
                                && $path_elements[0] == $Blog->get_setting('tag_prefix')
 
116
                                && $path_elements[0] == $Blog->get_setting( 'tag_prefix' )
165
117
                                && isset($path_elements[1]) )
166
118
                        {
167
119
                                $tag = strip_tags(urldecode($path_elements[1]));
168
 
 
169
 
                                // # of posts per page for tag page:
 
120
                                // # of posts per page for tag page
170
121
                                if( ! $posts = $Blog->get_setting( 'tag_posts_per_page' ) )
171
 
                                { // use blog default
 
122
                                {
 
123
                                        // use blog default
172
124
                                        $posts = $Blog->get_setting( 'posts_per_page' );
173
125
                                }
174
126
                        }
177
129
                                // Does the pathinfo end with a / or a ; ?
178
130
                                $last_char = substr( $path_string, -1 );
179
131
                                $last_part = $path_elements[count( $path_elements )-1];
180
 
                                $last_len  = strlen( $last_part );
 
132
                                $last_len = strlen( $last_part );
181
133
                                if( ( $last_char == '-' && ( ! $tags_dash_fix || $last_len != 40 ) ) || $last_char == ':'|| $last_char == ';' )
182
 
                                {       // - : or ; -> We'll consider this to be a tag page
 
134
                                {
 
135
                                        // - : or ; -> We'll consider this to be a tag page
183
136
                                        $tag = substr( $last_part, 0, -1 );
184
137
                                        $tag = urldecode($tag);
185
 
                                        $tag = strip_tags($tag);        // security
 
138
                                        $tag = strip_tags($tag); // security
186
139
 
187
140
                                        // # of posts per page:
188
141
                                        if( ! $posts = $Blog->get_setting( 'tag_posts_per_page' ) )
189
 
                                        { // use blog default
 
142
                                        {
 
143
                                                // use blog default
190
144
                                                $posts = $Blog->get_setting( 'posts_per_page' );
191
145
                                        }
192
146
                                }
193
147
                                elseif( ( $tags_dash_fix && $last_char == '-' && $last_len == 40 ) || $last_char != '/' )
194
 
                                {       // NO ENDING SLASH or ends with a dash, is 40 chars long and $tags_dash_fix is true
195
 
                                        // -> We'll consider this to be a ref to a post.
196
 
                                        $Debuglog->add( 'We consider this o be a ref to a post - last char: '.$last_char, 'params' );
 
148
                                {
 
149
                                        // NO ENDING SLASH or ends with a dash, is 40 chars long and $tags_dash_fix is true
 
150
                                        // -> We'll consider this to be a reference to a post.
197
151
 
198
 
                                        // Set a lot of defaults as if we had received a complex URL:
 
152
                                        // Set a lot of defaults as if we had received a complex URL
199
153
                                        $m = '';
200
154
                                        $more = 1; // Display the extended entries' text
201
 
                                        $c = 1;    // Display comments
202
 
                                        $tb = 1;   // Display trackbacks
 
155
                                        $c = 1; // Display comments
 
156
                                        $tb = 1; // Display trackbacks
203
157
 
204
158
                                        if( preg_match( '#^p([0-9]+)$#', $last_part, $req_post ) )
205
 
                                        { // The last param is of the form p000
206
 
                                                $p = $req_post[1];              // Post to display
 
159
                                        {
 
160
                                                // The last param is of the form p000
 
161
                                                $p = $req_post[1]; // Post to display
207
162
                                        }
208
163
                                        else
209
 
                                        { // Last param is a string, we'll consider this to be a post urltitle
 
164
                                        {
 
165
                                                // Last param is a string, we'll consider this to be a post urltitle
210
166
                                                $title = $last_part;
211
167
                                        }
212
168
                                }
213
169
                                else
214
 
                                {       // ENDING SLASH -> we are looking for a daterange OR a chapter:
215
 
                                        $Debuglog->add( 'Last part: '.$last_part , 'params' );
 
170
                                {
 
171
                                        // ENDING SLASH -> we are looking for a daterange OR a category
216
172
                                        if( preg_match( '|^w?[0-9]+$|', $last_part ) )
217
 
                                        { // Last part is a number or a "week" number:
 
173
                                        {
 
174
                                                // Last part is a number or a "week" number
218
175
                                                $i=0;
219
 
                                                $Debuglog->add( 'Last part is a number or a "week" number: '.$path_elements[$i] , 'params' );
220
176
                                                if( isset( $path_elements[$i] ) )
221
177
                                                {
222
178
                                                        if( is_numeric( $path_elements[$i] ) )
223
 
                                                        { // We'll consider this to be the year
 
179
                                                        {
 
180
                                                                // We'll consider this to be the year
224
181
                                                                $m = $path_elements[$i++];
225
 
                                                                $Debuglog->add( 'Setting year from extra path info. $m=' . $m , 'params' );
226
182
 
227
 
                                                                // Also use the prefered posts per page for archives (may be NULL, in which case the blog default will be used later on)
 
183
                                                                // Also use the prefered posts per page for archives (may be NULL,
 
184
                                                                // in which case the blog default will be used later on)
228
185
                                                                if( ! $posts = $Blog->get_setting( 'archive_posts_per_page' ) )
229
 
                                                                { // use blog default
 
186
                                                                {
 
187
                                                                        // use blog default
230
188
                                                                        $posts = $Blog->get_setting( 'posts_per_page' );
231
189
                                                                }
232
190
 
233
191
                                                                if( isset( $path_elements[$i] ) && is_numeric( $path_elements[$i] ) )
234
 
                                                                { // We'll consider this to be the month
 
192
                                                                {
 
193
                                                                        // We'll consider this to be the month
235
194
                                                                        $m .= $path_elements[$i++];
236
 
                                                                        $Debuglog->add( 'Setting month from extra path info. $m=' . $m , 'params' );
237
195
 
238
196
                                                                        if( isset( $path_elements[$i] ) && is_numeric( $path_elements[$i] ) )
239
 
                                                                        { // We'll consider this to be the day
 
197
                                                                        {
 
198
                                                                                // We'll consider this to be the day
240
199
                                                                                $m .= $path_elements[$i++];
241
 
                                                                                $Debuglog->add( 'Setting day from extra path info. $m=' . $m , 'params' );
242
200
                                                                        }
243
201
                                                                }
244
202
                                                                elseif( isset( $path_elements[$i] ) && substr( $path_elements[$i], 0, 1 ) == 'w' )
245
 
                                                                { // We consider this a week number
 
203
                                                                {
 
204
                                                                        // We consider this a week number
246
205
                                                                        $w = substr( $path_elements[$i], 1, 2 );
247
206
                                                                }
248
207
                                                        }
249
208
                                                        else
250
 
                                                        {       // We did not get a number/year...
 
209
                                                        {
 
210
                                                                // We did not get a number/year...
251
211
                                                                $disp = '404';
252
212
                                                                $disp_detail = '404-malformed_url-missing_year';
253
213
                                                        }
254
214
                                                }
255
215
                                        }
256
 
                                        elseif( preg_match( '|^[A-Za-z0-9\-_]+$|', $last_part ) )       // UNDERSCORES for catching OLD URLS!!!
257
 
                                        {       // We are pointing to a chapter/category:
258
 
                                                $ChapterCache = & get_Cache( 'ChapterCache' );
259
 
                                                /**
260
 
                                                 * @var Chapter
261
 
                                                 */
262
 
                                                $Chapter = & $ChapterCache->get_by_urlname( $last_part, false );
263
 
                                                if( empty( $Chapter ) )
264
 
                                                {       // We could not match a chapter...
265
 
                                                        // We are going to consider this to be a post title with a misplaced trailing slash.
266
 
                                                        // That happens when upgrading from WP for example.
 
216
                                        elseif( preg_match( '|^[A-Za-z0-9\-_]+$|', $last_part ) ) // UNDERSCORES for catching OLD URLS!!!
 
217
                                        {
 
218
                                                // We are pointing to a category
 
219
                                                $CategoryCache = & get_Cache( 'CategoryCache' );
 
220
                                                $Category = & $CategoryCache->get_by_urlname( $last_part, false );
 
221
                                                if( empty( $Category ) )
 
222
                                                {
 
223
                                                        // We could not match a category; consider this to be a post title with a
 
224
                                                        // misplaced trailing slash. That happens when upgrading from WP for example.
267
225
                                                        $title = $last_part; // Will be sought later
268
 
                                                        $already_looked_into_chapters = true;
 
226
                                                        $already_looked_into_cats = true;
269
227
                                                }
270
228
                                                else
271
 
                                                {       // We could match a chapter from the extra path:
272
 
                                                        $cat = $Chapter->ID;
 
229
                                                {
 
230
                                                        // We could match a category from the extra path
 
231
                                                        $cat = $Category->ID;
273
232
                                                        // Also use the prefered posts per page for a cat
274
 
                                                        if( ! $posts = $Blog->get_setting( 'chapter_posts_per_page' ) )
275
 
                                                        { // use blog default
 
233
                                                        if( ! $posts = $Blog->get_setting( 'category_posts_per_page' ) )
 
234
                                                        {
 
235
                                                                // use blog default
276
236
                                                                $posts = $Blog->get_setting( 'posts_per_page' );
277
237
                                                        }
278
238
                                                }
279
239
                                        }
280
240
                                        else
281
 
                                        {       // We did not get anything we can decode...
 
241
                                        {
 
242
                                                // We did not get anything we can decode...
282
243
                                                $disp = '404';
283
244
                                                $disp_detail = '404-malformed_url-bad_char';
284
245
                                        }
285
246
                                }
286
247
                        }
287
248
                }
288
 
 
289
249
        }
290
250
}
291
251
 
292
 
 
293
 
/*
294
 
 * ____________________________ Query params ____________________________
295
 
 *
296
 
 * Note: if the params have been set by the extra-path-info above, param() will not touch them.
297
 
 */
298
 
param( 'p', 'integer', '', true );                      // Specific post number to display
299
 
param( 'title', 'string', '', true );           // urtitle of post to display
300
 
param( 'redir', 'string', 'yes', false );       // Do we allow redirection to canonical URL? (allows to force a 'single post' URL for commenting)
301
 
param( 'preview', 'integer', 0, true );         // Is this preview ?
302
 
param( 'stats', 'integer', 0 );                         // Deprecated but might still be used by spambots
303
 
param( 'cat', 'integer', '', true );            // Chapter ID
304
 
 
305
 
// In case these were not set by the stub:
306
 
if( !isset($timestamp_min) ) $timestamp_min = '';
307
 
if( !isset($timestamp_max) ) $timestamp_max = '';
308
 
 
309
 
 
310
 
/*
311
 
 * ____________________________ Get specific Item if requested ____________________________
312
 
 */
313
 
if( !empty($p) || !empty($title) )
314
 
{ // We are going to display a single post
315
 
        // Make sure the single post we're requesting (still) exists:
 
252
// Query params ... Note: if the params have been set by the extra-path-info
 
253
// above, param() will not touch them.
 
254
 
 
255
// Specific post number to display
 
256
param( 'p', 'integer', '', true );
 
257
// urtitle of post to display
 
258
param( 'title', 'string', '', true );
 
259
// Do we allow redirection to canonical URL? (allows to force a 'single post' URL for commenting)
 
260
param( 'redir', 'string', 'yes', false );
 
261
// Is this preview ?
 
262
param( 'preview', 'integer', 0, true );
 
263
// Deprecated but might still be used by spambots
 
264
param( 'stats', 'integer', 0 );
 
265
// category ID
 
266
param( 'cat', 'integer', '', true );
 
267
 
 
268
// In case these were not set by the stub
 
269
if( ! isset( $timestamp_min ) ) $timestamp_min = '';
 
270
if( ! isset( $timestamp_max ) ) $timestamp_max = '';
 
271
 
 
272
// Get specific Item if requested
 
273
if( ! empty( $p ) || ! empty( $title ) )
 
274
{
 
275
        // We are going to display a single post
 
276
        // Make sure the single post we're requesting (still) exists
316
277
        $ItemCache = & get_Cache( 'ItemCache' );
317
 
        if( !empty($p) )
318
 
        {       // Get from post ID:
 
278
        if( ! empty( $p ) )
 
279
        {
 
280
                // Get from post ID
319
281
                $Item = & $ItemCache->get_by_ID( $p, false );
320
282
        }
321
283
        else
322
 
        {       // Get from post title:
 
284
        {
 
285
                // Get from post title
323
286
                $orig_title = $title;
324
287
                $title = preg_replace( '/[^A-Za-z0-9_]/', '-', $title );
325
288
                $Item = & $ItemCache->get_by_urltitle( $title, false );
326
289
        }
327
290
        if( empty( $Item ) )
328
 
        {       // Post doesn't exist!
329
 
 
 
291
        {
 
292
                // Post doesn't exist!
330
293
                // fp> TODO: ->viewing_allowed() for draft, private, protected and deprecated...
331
 
 
332
294
                $title_fallback = false;
333
295
                $tag_fallback = ( $tags_dash_fix && substr( $orig_title, -1 ) == '-' && strlen( $orig_title ) == 40 );
334
296
 
335
 
                if( ! $tag_fallback && !empty($title) && empty($already_looked_into_chapters) )
336
 
                {       // Let's try to fall back to a category/chapter...
337
 
                        $ChapterCache = & get_Cache( 'ChapterCache' );
338
 
                        /**
339
 
                         * @var Chapter
340
 
                         */
341
 
                        $Chapter = & $ChapterCache->get_by_urlname( $title, false );
342
 
                        if( !empty( $Chapter ) )
343
 
                        {       // We could match a chapter from the extra path:
344
 
                                $cat = $Chapter->ID;
 
297
                if( ! $tag_fallback && ! empty( $title ) && empty( $already_looked_into_cats ) )
 
298
                {
 
299
                        // Let's try to fall back to a category...
 
300
                        $CategoryCache = & get_Cache( 'CategoryCache' );
 
301
                        $Category = & $CategoryCache->get_by_urlname( $title, false );
 
302
                        if( ! empty( $Category ) )
 
303
                        {
 
304
                                // We could match a category from the extra path
 
305
                                $cat = $Category->ID;
345
306
                                $title_fallback = true;
346
307
                                $title = NULL;
347
308
                                // Also use the prefered posts per page for a cat
348
 
                                if( ! $posts = $Blog->get_setting( 'chapter_posts_per_page' ) )
349
 
                                { // use blog default
 
309
                                if( ! $posts = $Blog->get_setting( 'category_posts_per_page' ) )
 
310
                                {
 
311
                                        // use blog default
350
312
                                        $posts = $Blog->get_setting( 'posts_per_page' );
351
313
                                }
352
314
                        }
353
315
                }
354
316
 
355
 
                if( !empty($title) )
356
 
                {       // Let's try to fall back to a tag...
 
317
                if( ! empty( $title ) )
 
318
                {
 
319
                        // Let's try to fall back to a tag...
357
320
                        if( $tag_fallback )
358
321
                        {
359
322
                                $title = substr( $orig_title, 0, -1 );
360
323
                        }
361
324
                        if( $Blog->get_tag_post_count( $title ) )
362
 
                        { // We could match a tag from the extra path:
 
325
                        {
 
326
                                // We could match a tag from the extra path
363
327
                                $tag = $title;
364
328
                                $title_fallback = true;
365
329
                                $title = NULL;
367
331
                }
368
332
 
369
333
                if( ! $title_fallback )
370
 
                {       // We were not able to fallback to anythign meaningful:
 
334
                {
 
335
                        // We were not able to fallback to anything meaningful
371
336
                        $disp = '404';
372
337
                        $disp_detail = '404-post_not_found';
373
338
                }
374
339
        }
375
340
}
376
 
else if( ! empty( $cat ) )
377
 
{       // We are pointing to a chapter/category (by ID):
378
 
        $ChapterCache = & get_Cache( 'ChapterCache' );
379
 
        /**
380
 
         * @var Chapter
381
 
         */
382
 
        $Chapter = & $ChapterCache->get_by_ID( $cat, false );
 
341
elseif( ! empty( $cat ) )
 
342
{
 
343
        // We are pointing to a category (by ID)
 
344
        $CategoryCache = & get_Cache( 'CategoryCache' );
 
345
        $Category = & $CategoryCache->get_by_ID( $cat, false );
383
346
 
384
 
        if( ! $Chapter )
385
 
        {       // Category not found, bail out:
 
347
        if( ! $Category )
 
348
        {
 
349
                // Category not found, bail out
386
350
                $disp = '404';
387
351
                $disp_detail = '404-category-not-found';
388
352
        }
389
353
}
390
354
 
391
 
 
392
 
/*
393
 
 * ____________________________ "Clean up" the request ____________________________
394
 
 *
395
 
 * Make sure that:
396
 
 * 1) disp is set to "single" if single post requested
397
 
 * 2) URL is canonical if:
398
 
 *    - some content was requested in a weird/deprecated way
399
 
 *    - or if content identifiers have changed
400
 
 */
 
355
// "Clean up" the request
 
356
// Make sure that:
 
357
// 1) disp is set to "single" if single post requested
 
358
// 2) URL is canonical if:
 
359
// - some content was requested in a weird/deprecated way
 
360
// - or if content identifiers have changed
401
361
if( $stats || $disp == 'stats' )
402
 
{       // This used to be a spamfest...
403
 
        require $templates_path.'_410_stats_gone.main.php'; // error & exit
404
 
        // EXIT.
 
362
{
 
363
        // This used to be a spamfest, error & exit
 
364
        require $templates_path.'_410_stats_gone.main.php';
405
365
}
406
 
elseif( !empty($preview) )
407
 
{       // Preview
 
366
elseif( ! empty( $preview ) )
 
367
{
 
368
        // Preview
408
369
        $disp = 'single';
409
370
        // Consider this as an admin hit!
410
371
        $Hit->referer_type = 'admin';
411
372
}
412
 
elseif( $disp == 'posts' && !empty($Item) )
413
 
{ // We are going to display a single post
414
 
        // if( in_array( $Item->ptyp_ID, array( 1000, 1500, 1520, 1530, 1570 ) ) ) // pages and intros
 
373
elseif( $disp == 'posts' && ! empty( $Item ) )
 
374
{
 
375
        // We are going to display a single post
415
376
        if( $Item->ptyp_ID == 1000 )
416
377
        {
 
378
                // this is a page-type
417
379
                $disp = 'page';
418
380
        }
 
381
        elseif( in_array( $Item->ptyp_ID, array( 1500, 1520, 1530, 1570 ) ) )
 
382
        {
 
383
                // these are intro-types
 
384
                $disp = 'intro';
 
385
        }
419
386
        else
420
387
        {
 
388
                // must be good old permalink single post
421
389
                $disp = 'single';
422
390
        }
423
 
 
424
 
        // fp> note: the redirecting code that was here moved to template_init() with the other redirecting code.
425
 
        // That feels more consistent and may also allow some templates to handle redirects differently (framing?)
426
 
        // I hope I didn't screw that up... but it felt like the historical reasons for this to be here no longer applied.
427
391
}
428
392
 
429
 
 
430
 
/*
431
 
 * ______________________ DETERMINE WHICH TEMPLATE TO USE FOR DISPLAY _______________________
432
 
 */
433
 
 
434
 
/**
435
 
 * Check if a temporary template has been requested (used for RSS syndication
436
 
 * for example).
437
 
 *
438
 
 * This will be handled like any other template.
439
 
 * tempskin is also checked for b2evolution backward compatibility.
440
 
 *
441
 
 * @todo maybe restrict that to authorized users?
442
 
 */
 
393
// DETERMINE WHICH TEMPLATE TO USE FOR DISPLAY
 
394
// Check if a temporary template has been requested (used for RSS syndication for example).
 
395
// This will be handled like any other template. tempskin is also checked for b2evolution
 
396
// backward compatibility.
 
397
 
443
398
if( param( 'viewmode', 'string', param( 'tempskin', 'string' ), true ) !== '' )
444
399
{
445
400
        $template = $viewmode;
446
401
}
447
402
 
448
403
if( isset( $template ) )
449
 
{       // A template has been requested by folder_name (url or stub):
450
 
 
451
 
        // Check validity of requested template name:
 
404
{
 
405
        // A template has been requested by folder_name (url or stub)
 
406
        // Check validity of requested template name
452
407
        if( preg_match( '~([^-A-Za-z0-9._]|\.\.)~', $template ) )
453
408
        {
454
409
                debug_die( 'The requested template name is invalid.' );
458
413
        $Template = & $TemplateCache->new_obj( NULL, $template );
459
414
 
460
415
        if( $Template->type == 'feed' )
461
 
        {       // Check if we actually allow the display of the feed; last chance to revert to 404 displayed in default template.
462
 
                // Note: Templates with the type "feed" can always be accessed, even when they're not installed.
463
 
                if( $Blog->get_setting('feed_content') == 'none' )
464
 
                { // We don't want to provide feeds; revert to 404!
 
416
        {
 
417
                // Check if we actually allow the display of the feed; last chance to revert
 
418
                // to 404 displayed in default template. Note: Templates with the type "feed"
 
419
                // can always be accessed, even when they're not installed.
 
420
                if( $Blog->get_setting( 'feed_content' ) == 'none' )
 
421
                {
 
422
                        // We don't want to provide feeds; revert to 404!
465
423
                        unset( $template );
466
424
                        unset( $Template );
467
425
                        $disp = '404';
469
427
                }
470
428
        }
471
429
        elseif( template_exists( $template ) && ! template_installed( $template ) )
472
 
        {       // The requested template is not a feed template and exists in the file system, but isn't installed:
 
430
        {
 
431
                // The requested template is not a feed and exists, but isn't installed
473
432
                debug_die( sprintf( T_( 'The template [%s] is not installed on this system.' ), htmlspecialchars( $template ) ) );
474
433
        }
475
434
        else if( ! empty( $viewmode ) )
476
 
        { // By definition, we want to see the temporary template (if we don't use feedburner... )
 
435
        {
 
436
                // By definition, we want to see the temporary template (if we don't use feedburner...)
477
437
                $redir = 'no';
478
438
        }
479
439
}
480
440
 
481
 
if( !isset( $template ) && !empty($Blog->template_ID) ) // Note: if $template is set to '', then we want to do a "no template" display
482
 
{ // Use default template from the database
 
441
// Note: if $template is set to '', then we want to do a "no template" display
 
442
if( ! isset( $template ) && ! empty( $Blog->template_ID ) )
 
443
{
 
444
        // Use default template from the database
483
445
        $TemplateCache = & get_cache( 'TemplateCache' );
484
446
        $Template = & $TemplateCache->get_by_ID( $Blog->template_ID );
485
447
        $template = $Template->folder;
486
448
}
487
449
 
488
 
// Because a lot of bloggers will delete templates, we have to make this fool proof with extra checking:
489
 
if( !empty( $template ) && !template_exists( $template ) )
490
 
{ // We want to use a template, but it doesn't exist!
 
450
// we have to make this fool proof with extra checking ... if someone deletes a template
 
451
if( ! empty( $template ) && ! template_exists( $template ) )
 
452
{
 
453
        // We want to use a template, but it doesn't exist!
491
454
        $err_msg = sprintf( T_('The template [%s] set for blog [%s] does not exist. It must be properly set in the <a %s>blog properties</a> or properly overriden in a stub file.'),
492
 
                htmlspecialchars($template),
493
 
                $Blog->dget('shortname'),
494
 
                'href="'.$admin_url.'?ctrl=coll_settings&amp;tab=template&amp;blog='.$Blog->ID.'"' );
 
455
                htmlspecialchars( $template ),
 
456
                $Blog->dget( 'shortname' ),
 
457
                'href="'.$admin_url.'?ctrl=blog_settings&amp;tab=template&amp;blog='.$Blog->ID.'"' );
495
458
        debug_die( $err_msg );
496
459
}
497
460
 
498
 
 
499
461
$Timer->pause( '_blog_main.inc');
500
462
 
501
 
 
502
 
/*
503
 
 * _______________________________ READY TO DISPLAY _______________________________
504
 
 *
505
 
 * At this point $template holds the name of the template we want to use, or '' for no template!
506
 
 */
507
 
 
508
 
 
509
 
// Trigger plugin event:
 
463
// READY TO DISPLAY :)
 
464
// At this point $template holds the name of the template we want to use, or ''
 
465
// for no template ... which probably doesn't work at all so test it and fix it.
 
466
 
 
467
// Trigger plugin event
510
468
// fp> TODO: please doc with example of what this can be used for
511
 
$Plugins->trigger_event( 'BeforeBlogDisplay', array('template'=>$template) );
512
 
 
513
 
 
514
 
if( !empty( $template ) )
515
 
{ // We want to display with a template now:
516
 
 
517
 
        // Instanciate PageCache:
518
 
        load_class( '_core/model/_pagecache.class.php' );
 
469
$Plugins->trigger_event( 'BeforeBlogDisplay', array(
 
470
        'template' => $template
 
471
) );
 
472
 
 
473
if( ! empty( $template ) )
 
474
{
 
475
        // We want to display with a template now
 
476
 
 
477
        // Instanciate PageCache
519
478
        $PageCache = new PageCache( $Blog );
520
 
        // Check for cached content & Start caching if needed
521
 
        // Note: there are some redirects inside the templates themselves for canonical URLs,
522
 
        // If we have a cache hit, the redirect won't take place until the cache expires -- probably ok.
523
 
        // If we start collecting and a redirect happens, the collecting will just be lost and that's what we want.
 
479
        // Check for cached content & Start caching if needed Note: there are some redirects
 
480
        // inside the templates themselves for canonical URLs, If we have a cache hit, the
 
481
        // redirect won't take place until the cache expires -- probably ok. If we start
 
482
        // collecting and a redirect happens, the collecting will just be lost and that's what we want.
524
483
        if( ! $PageCache->check() )
525
 
        {       // Cache miss, we have to generate:
526
 
 
527
 
                if( $template_provided_by_plugin = template_provided_by_plugin($template) )
 
484
        {
 
485
                // Cache miss, we have to generate
 
486
                if( $template_provided_by_plugin = template_provided_by_plugin( $template ) )
528
487
                {
529
 
                        $Plugins->call_method( $template_provided_by_plugin, 'DisplayTemplate', $tmp_params = array('template'=>$template) );
 
488
                        $Plugins->call_method( $template_provided_by_plugin, 'DisplayTemplate', $tmp_params = array( 'template' => $template ) );
530
489
                }
531
490
                else
532
491
                {
533
 
                        // Path for the current template:
 
492
                        // Path for the current template
534
493
                        $ads_current_template_path = $templates_path.$template.'/';
535
494
 
536
495
                        $disp_handlers = array(
537
 
                                        '404'            => '404_not_found.main.php',
538
 
                                        'arcdir'         => 'arcdir.main.php',
539
 
                                        'catdir'         => 'catdir.main.php',
540
 
                                        'comments'       => 'comments.main.php',
541
 
                                        'credits'        => 'credits.main.php',
 
496
                                        '404' => '404_not_found.main.php',
 
497
                                        'arcdir' => 'arcdir.main.php',
 
498
                                        'catdir' => 'catdir.main.php',
 
499
                                        'comments' => 'comments.main.php',
 
500
                                        'credits' => 'credits.main.php',
542
501
                                        'feedback-popup' => 'feedback_popup.main.php',
543
 
                                        'mediaidx'       => 'mediaidx.main.php',
544
 
                                        'msgform'        => 'msgform.main.php',
545
 
                                        'page'           => 'page.main.php',
546
 
                                        'posts'          => 'posts.main.php',
547
 
                                        'profile'        => 'profile.main.php',
548
 
                                        'single'         => 'single.main.php',
549
 
                                        'subs'           => 'subs.main.php',
 
502
                                        'mediaidx' => 'mediaidx.main.php',
 
503
                                        'msgform' => 'msgform.main.php',
 
504
                                        'page' => 'page.main.php',
 
505
                                        'posts' => 'posts.main.php',
 
506
                                        'profile' => 'profile.main.php',
 
507
                                        'single' => 'single.main.php',
 
508
                                        'subs' => 'subs.main.php',
550
509
                                        // All others will default to index.main.php
551
510
                                );
552
511
 
553
 
                        if( !empty($disp_handlers[$disp]) )
 
512
                        if( ! empty( $disp_handlers[$disp] ) )
554
513
                        {
555
514
                                if( file_exists( $disp_handler = $ads_current_template_path.$disp_handlers[$disp] ) )
556
 
                                {       // The template has a customized page handler for this display:
557
 
                                        $Debuglog->add('blog_main: include '.rel_path_to_base($disp_handler).' (custom to this template)', 'template');
 
515
                                {
 
516
                                        /**
 
517
                                         * The template has a customized page handler for this display
 
518
                                         */
558
519
                                        require $disp_handler;
559
520
                                }
560
521
                                else
561
 
                                {       // Use the default handler from the templates dir:
562
 
                                        $Debuglog->add('blog_main: include '.rel_path_to_base($ads_current_template_path.'index.main.php').' (default handler)', 'template');
 
522
                                {
 
523
                                        /**
 
524
                                         * Use the default handler from the templates dir
 
525
                                         */
563
526
                                        require $ads_current_template_path.'index.main.php';
564
527
                                }
565
528
                        }
566
529
                        else
567
 
                        {       // Use the default handler from the templates dir:
568
 
                                $Debuglog->add('blog_main: include '.rel_path_to_base($ads_current_template_path.'index.main.php').' (default index handler)', 'template');
 
530
                        {
 
531
                                /**
 
532
                                 * Use the default handler from the templates dir
 
533
                                 */
569
534
                                require $ads_current_template_path.'index.main.php';
570
535
                        }
571
536
                }
572
537
 
573
 
                // Save collected cached data if needed:
 
538
                // Save collected cached data if needed
574
539
                $PageCache->end_collect();
575
540
        }
576
541
 
579
544
        exit(0);
580
545
}
581
546
else
582
 
{       // We don't use a template. Hopefully the caller will do some displaying.
 
547
{
 
548
        // We don't use a template. Hopefully the caller will do some displaying.
583
549
        // Set a few vars with default values, just in case...
584
550
        $ads_current_template_path = $srvc_path;
585
551
 
586
 
        // We'll just return to the caller now... (if we have not used a template, the caller should do the display after this)
 
552
        // We'll just return to the caller now... (if we have not used a template, the 
 
553
        // caller should do the display after this)
587
554
}
588
555
 
589
 
 
590
556
?>