OSDN Git Service

9a3467e88d94705488e46b7ec6a4d0347f1faff4
[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                         $manager->notify(
120                                 'GenerateURL',
121                                 array(
122                                         'type' => $type,
123                                         'params' => $params,
124                                         'completed' => &$created,
125                                         'url' => &$url
126                                 )
127                         );
128                 }
129         
130                 // if a plugin created the URL, return it
131                 if ($created)
132                 {
133                         return $url;
134                 }
135         
136                 // default implementation
137                 switch ($type) {
138                         case 'item':
139                                 if ($usePathInfo) {
140                                         $url = $CONF['ItemURL'] . '/' . $CONF['ItemKey'] . '/' . $params['itemid'];
141                                 } else {
142                                         $url = $CONF['ItemURL'] . '?itemid=' . $params['itemid'];
143                                 }
144                                 break;
145         
146                         case 'member':
147                                 if ($usePathInfo) {
148                                         $url = $CONF['MemberURL'] . '/' . $CONF['MemberKey'] . '/' . $params['memberid'];
149                                 } else {
150                                         $url = $CONF['MemberURL'] . '?memberid=' . $params['memberid'];
151                                 }
152                                 break;
153         
154                         case 'category':
155                                 if ($usePathInfo) {
156                                         $url = $CONF['CategoryURL'] . '/' . $CONF['CategoryKey'] . '/' . $params['catid'];
157                                 } else {
158                                         $url = $CONF['CategoryURL'] . '?catid=' . $params['catid'];
159                                 }
160                                 break;
161         
162                         case 'archivelist':
163                                 if (!$params['blogid']) {
164                                         $params['blogid'] = $CONF['DefaultBlog'];
165                                 }
166         
167                                 if ($usePathInfo) {
168                                         $url = $CONF['ArchiveListURL'] . '/' . $CONF['ArchivesKey'] . '/' . $params['blogid'];
169                                 } else {
170                                         $url = $CONF['ArchiveListURL'] . '?archivelist=' . $params['blogid'];
171                                 }
172                                 break;
173         
174                         case 'archive':
175                                 if ($usePathInfo) {
176                                         $url = $CONF['ArchiveURL'] . '/' . $CONF['ArchiveKey'] . '/'.$params['blogid'].'/' . $params['archive'];
177                                 } else {
178                                         $url = $CONF['ArchiveURL'] . '?blogid='.$params['blogid'].'&amp;archive=' . $params['archive'];
179                                 }
180                                 break;
181         
182                         case 'blog':
183                                 if ($usePathInfo) {
184                                         $url = $CONF['BlogURL'] . '/' . $CONF['BlogKey'] . '/' . $params['blogid'];
185                                 } else {
186                                         $url = $CONF['BlogURL'] . '?blogid=' . $params['blogid'];
187                                 }
188                                 break;
189                 }
190         
191                 return Link::add_link_params($url, (isset($params['extra'])? $params['extra'] : null));
192         }
193         
194         static private function add_link_params($link, $params)
195         {
196                 global $CONF;
197         
198                 if (is_array($params) ) {
199         
200                         if ($CONF['URLMode'] == 'pathinfo') {
201         
202                                 foreach ($params as $param => $value) {
203                                         // change in 3.63 to fix problem where URL generated with extra params mike look like category/4/blogid/1
204                                         // but they should use the URL keys like this: category/4/blog/1
205                                         // if user wants old urls back, set $CONF['NoURLKeysInExtraParams'] = 1; in config.php
206                                         if (isset($CONF['NoURLKeysInExtraParams']) && $CONF['NoURLKeysInExtraParams'] == 1) 
207                                         {
208                                                 $link .= '/' . $param . '/' . urlencode($value);
209                                         } else {
210                                                 switch ($param) {
211                                                         case 'itemid':
212                                                                 $link .= '/' . $CONF['ItemKey'] . '/' . urlencode($value);
213                                                         break;
214                                                         case 'memberid':
215                                                                 $link .= '/' . $CONF['MemberKey'] . '/' . urlencode($value);
216                                                         break;
217                                                         case 'catid':
218                                                                 $link .= '/' . $CONF['CategoryKey'] . '/' . urlencode($value);
219                                                         break;
220                                                         case 'archivelist':
221                                                                 $link .= '/' . $CONF['ArchivesKey'] . '/' . urlencode($value);
222                                                         break;
223                                                         case 'archive':
224                                                                 $link .= '/' . $CONF['ArchiveKey'] . '/' . urlencode($value);
225                                                         break;
226                                                         case 'blogid':
227                                                                 $link .= '/' . $CONF['BlogKey'] . '/' . urlencode($value);
228                                                         break;
229                                                         default:
230                                                                 $link .= '/' . $param . '/' . urlencode($value);
231                                                         break;
232                                                 }
233                                         }
234                                 }
235         
236                         } else {
237         
238                                 foreach ($params as $param => $value) {
239                                         $link .= '&amp;' . $param . '=' . urlencode($value);
240                                 }
241         
242                         }
243                 }
244         
245                 return $link;
246         }
247
248         /**
249          * Link::create_blog_link()
250          * Create an link to a blog
251          * 
252          * This function considers the URLMode of the blog
253          * 
254          * @static
255          * @param $url          url
256          * @param $params       parameters
257          */
258         static public function create_blog_link($url, $params) {
259                 global $CONF;
260                 if ($CONF['URLMode'] == 'normal') {
261                         if (i18n::strpos($url, '?') === FALSE && is_array($params)) {
262                                 $fParam = reset($params);
263                                 $fKey   = key($params);
264                                 array_shift($params);
265                                 $url .= '?' . $fKey . '=' . $fParam;
266                         }
267                 } elseif ($CONF['URLMode'] == 'pathinfo' && i18n::substr($url, -1) == '/') {
268                         $url = i18n::substr($url, 0, -1);
269                 }
270                 return addLinkParams($url, $params);
271         }
272
273 }