OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / LINK.php
1 <?php 
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2011 The Nucleus Group
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * (see nucleus/documentation/index.html#license for more info)
11  */
12 /**
13  * This class is a collections of functions that produce links
14  * 
15  * All functions in this clss should only be called statically,
16  * for example: Link::create_item_link(...)
17  * 
18  * @license http://nucleuscms.org/license.txt GNU General Public License
19  * @copyright Copyright (C) 2002-2011 The Nucleus Group
20  * @version $Id: LINK.php 1721 2012-03-31 10:18:25Z sakamocchi $
21  */
22 class Link
23 {
24
25         /**
26          * Link::create_item_link()
27          * Create a link to an item
28          * @static
29          * @param $itemid       item id
30          * @param $extra        extra parameter
31          */
32         static public function create_item_link($itemid, $extra = '') {
33                 return self::create_link('item', array('itemid' => $itemid, 'extra' => $extra) );
34         }
35
36         /**
37          * Link::create_member_link()
38          * Create a link to a member
39          * 
40          * @static
41          * @param $memberid     member id
42          * @param $extra        extra parameter
43          */
44         static public function create_member_link($memberid, $extra = '') {
45                 return self::create_link('member', array('memberid' => $memberid, 'extra' => $extra) );
46         }
47         
48         /**
49          * Link::create_category_link()
50          * Create a link to a category
51          * 
52          * @static
53          * @param $catid        category id
54          * @param $extra        extra parameter
55          */
56         static public function create_category_link($catid, $extra = '') {
57                 return self::create_link('category', array('catid' => $catid, 'extra' => $extra) );
58         }
59
60         /**
61          * Link::cteate_archive_link()
62          * Create a link to an archive
63          * 
64          * @static
65          * @param $blogid       blog id
66          * @param $archive      archive identifier
67          * @param $extra        extra parameter
68          */
69         static public function create_archive_link($blogid, $archive, $extra = '') {
70                 return self::create_link('archive', array('blogid' => $blogid, 'archive' => $archive, 'extra' => $extra) );
71         }
72
73         /**
74          * Link::create_archivelist_link()
75          * Create a link to an archive list
76          * 
77          * @static
78          * @param $blogid       blog id
79          * @param $extra        extra parameter
80          */
81         static public function create_archivelist_link($blogid = '', $extra = '') {
82                 return self::create_link('archivelist', array('blogid' => $blogid, 'extra' => $extra) );
83         }
84
85         /**
86          * Link::create_blogid_link()
87          * Create a link to a blog
88          * 
89          * @static
90          * @param $blogid       blog id
91          * @param $extra        extra parameter
92          */
93         static public function create_blogid_link($blogid, $params = '') {
94                 return self::create_link('blog', array('blogid' => $blogid, 'extra' => $params) );
95         }
96
97         /**
98          * Link::create_link()
99          * Create a link
100          * 
101          * Universell function that creates link of different types (like item, blog ...)
102          * and with an array of parameters
103          * 
104          * @static
105          * @param $type         type of the link
106          * @param $params       array with parameters
107          */
108         static public function create_link($type, $params) {
109                 global $manager, $CONF;
110         
111                 $generatedURL = '';
112                 $usePathInfo = ($CONF['URLMode'] == 'pathinfo');
113         
114                 // ask plugins first
115                 $created = false;
116         
117                 if ($usePathInfo)
118                 {
119                         $data = array(
120                                 'type'          =>  $type,
121                                 'params'        =>  $params,
122                                 'completed'     => &$created,
123                                 'url'           => &$url
124                         );
125                         $manager->notify('GenerateURL', $data);
126                 }
127         
128                 // if a plugin created the URL, return it
129                 if ($created)
130                 {
131                         return $url;
132                 }
133         
134                 // default implementation
135                 switch ($type) {
136                         case 'item':
137                                 if ($usePathInfo) {
138                                         $url = $CONF['ItemURL'] . '/' . $CONF['ItemKey'] . '/' . $params['itemid'];
139                                 } else {
140                                         $url = $CONF['ItemURL'] . '?itemid=' . $params['itemid'];
141                                 }
142                                 break;
143         
144                         case 'member':
145                                 if ($usePathInfo) {
146                                         $url = $CONF['MemberURL'] . '/' . $CONF['MemberKey'] . '/' . $params['memberid'];
147                                 } else {
148                                         $url = $CONF['MemberURL'] . '?memberid=' . $params['memberid'];
149                                 }
150                                 break;
151         
152                         case 'category':
153                                 if ($usePathInfo) {
154                                         $url = $CONF['CategoryURL'] . '/' . $CONF['CategoryKey'] . '/' . $params['catid'];
155                                 } else {
156                                         $url = $CONF['CategoryURL'] . '?catid=' . $params['catid'];
157                                 }
158                                 break;
159         
160                         case 'archivelist':
161                                 if (!$params['blogid']) {
162                                         $params['blogid'] = $CONF['DefaultBlog'];
163                                 }
164         
165                                 if ($usePathInfo) {
166                                         $url = $CONF['ArchiveListURL'] . '/' . $CONF['ArchivesKey'] . '/' . $params['blogid'];
167                                 } else {
168                                         $url = $CONF['ArchiveListURL'] . '?archivelist=' . $params['blogid'];
169                                 }
170                                 break;
171         
172                         case 'archive':
173                                 if ($usePathInfo) {
174                                         $url = $CONF['ArchiveURL'] . '/' . $CONF['ArchiveKey'] . '/'.$params['blogid'].'/' . $params['archive'];
175                                 } else {
176                                         $url = $CONF['ArchiveURL'] . '?blogid='.$params['blogid'].'&amp;archive=' . $params['archive'];
177                                 }
178                                 break;
179         
180                         case 'blog':
181                                 if ($usePathInfo) {
182                                         $url = $CONF['BlogURL'] . '/' . $CONF['BlogKey'] . '/' . $params['blogid'];
183                                 } else {
184                                         $url = $CONF['BlogURL'] . '?blogid=' . $params['blogid'];
185                                 }
186                                 break;
187                 }
188         
189                 return Link::add_link_params($url, (isset($params['extra'])? $params['extra'] : null));
190         }
191         
192         static private function add_link_params($link, $params)
193         {
194                 global $CONF;
195         
196                 if (is_array($params) ) {
197         
198                         if ($CONF['URLMode'] == 'pathinfo') {
199         
200                                 foreach ($params as $param => $value) {
201                                         // change in 3.63 to fix problem where URL generated with extra params mike look like category/4/blogid/1
202                                         // but they should use the URL keys like this: category/4/blog/1
203                                         // if user wants old urls back, set $CONF['NoURLKeysInExtraParams'] = 1; in config.php
204                                         if (isset($CONF['NoURLKeysInExtraParams']) && $CONF['NoURLKeysInExtraParams'] == 1) 
205                                         {
206                                                 $link .= '/' . $param . '/' . urlencode($value);
207                                         } else {
208                                                 switch ($param) {
209                                                         case 'itemid':
210                                                                 $link .= '/' . $CONF['ItemKey'] . '/' . urlencode($value);
211                                                         break;
212                                                         case 'memberid':
213                                                                 $link .= '/' . $CONF['MemberKey'] . '/' . urlencode($value);
214                                                         break;
215                                                         case 'catid':
216                                                                 $link .= '/' . $CONF['CategoryKey'] . '/' . urlencode($value);
217                                                         break;
218                                                         case 'archivelist':
219                                                                 $link .= '/' . $CONF['ArchivesKey'] . '/' . urlencode($value);
220                                                         break;
221                                                         case 'archive':
222                                                                 $link .= '/' . $CONF['ArchiveKey'] . '/' . urlencode($value);
223                                                         break;
224                                                         case 'blogid':
225                                                                 $link .= '/' . $CONF['BlogKey'] . '/' . urlencode($value);
226                                                         break;
227                                                         default:
228                                                                 $link .= '/' . $param . '/' . urlencode($value);
229                                                         break;
230                                                 }
231                                         }
232                                 }
233         
234                         } else {
235         
236                                 foreach ($params as $param => $value) {
237                                         $link .= '&amp;' . $param . '=' . urlencode($value);
238                                 }
239         
240                         }
241                 }
242         
243                 return $link;
244         }
245
246         /**
247          * Link::create_blog_link()
248          * Create an link to a blog
249          * 
250          * This function considers the URLMode of the blog
251          * 
252          * @static
253          * @param $url          url
254          * @param $params       parameters
255          */
256         static public function create_blog_link($url, $params) {
257                 global $CONF;
258                 if ($CONF['URLMode'] == 'normal') {
259                         if (i18n::strpos($url, '?') === FALSE && is_array($params)) {
260                                 $fParam = reset($params);
261                                 $fKey   = key($params);
262                                 array_shift($params);
263                                 $url .= '?' . $fKey . '=' . $fParam;
264                         }
265                 } elseif ($CONF['URLMode'] == 'pathinfo' && i18n::substr($url, -1) == '/') {
266                         $url = i18n::substr($url, 0, -1);
267                 }
268                 return addLinkParams($url, $params);
269         }
270
271 }