PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/addons/MultiBlog.plugin/php/init.MultiBlog.php

http://github.com/openmelody/melody
PHP | 336 lines | 246 code | 30 blank | 60 comment | 79 complexity | ee7b00dec0c2896328b4d2f12ba2a449 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1
  1. <?php
  2. # Movable Type (r) Open Source (C) 2001-2009 Six Apart, Ltd.
  3. # This program is distributed under the terms of the
  4. # GNU General Public License, version 2.
  5. #
  6. # $Id$
  7. global $mt;
  8. $ctx = &$mt->context();
  9. # Check to see if MultiBlog is disabled...
  10. $switch = $mt->config('PluginSwitch');
  11. if (isset($switch) && isset($switch['MultiBlog/multiblog.pl'])) {
  12. if (!$switch['MultiBlog/multiblog.pl']) {
  13. define('MULTIBLOG_ENABLED', 0);
  14. return;
  15. }
  16. }
  17. define('MULTIBLOG_ENABLED', 1);
  18. define('MULTIBLOG_ACCESS_DENIED', 1);
  19. define('MULTIBLOG_ACCESS_ALLOWED', 2);
  20. # override handler for the following tags. the overridden version
  21. # will, in turn call the MT native handlers...
  22. global $multiblog_orig_handlers;
  23. $multiblog_orig_handlers = array();
  24. $multiblog_orig_handlers['mtblogpingcount']
  25. = $ctx->add_tag('blogpingcount', 'multiblog_MTBlogPingCount');
  26. $multiblog_orig_handlers['mtblogcommentcount']
  27. = $ctx->add_tag('blogcommentcount', 'multiblog_MTBlogCommentCount');
  28. $multiblog_orig_handlers['mtblogcategorycount']
  29. = $ctx->add_tag('blogcategorycount', 'multiblog_MTBlogCategoryCount');
  30. $multiblog_orig_handlers['mtblogentrycount']
  31. = $ctx->add_tag('blogentrycount', 'multiblog_MTBlogEntryCount');
  32. $multiblog_orig_handlers['mtauthors']
  33. = $ctx->add_container_tag('authors', 'multiblog_block_wrapper');
  34. $multiblog_orig_handlers['mtentries']
  35. = $ctx->add_container_tag('entries', 'multiblog_block_wrapper');
  36. $multiblog_orig_handlers['mtcomments']
  37. = $ctx->add_container_tag('comments', 'multiblog_block_wrapper');
  38. $multiblog_orig_handlers['mtcategories']
  39. = $ctx->add_container_tag('categories', 'multiblog_block_wrapper');
  40. $multiblog_orig_handlers['mtpages']
  41. = $ctx->add_container_tag('pages', 'multiblog_block_wrapper');
  42. $multiblog_orig_handlers['mtfolders']
  43. = $ctx->add_container_tag('folders', 'multiblog_block_wrapper');
  44. $multiblog_orig_handlers['mtpings']
  45. = $ctx->add_container_tag('pings', 'multiblog_block_wrapper');
  46. $multiblog_orig_handlers['mtblogs']
  47. = $ctx->add_container_tag('blogs', 'multiblog_block_wrapper');
  48. $multiblog_orig_handlers['mttags']
  49. = $ctx->add_container_tag('tags', 'multiblog_block_wrapper');
  50. $multiblog_orig_handlers['mtinclude']
  51. = $ctx->add_tag('include', 'multiblog_MTInclude');
  52. $multiblog_orig_handlers['mttagsearchlink']
  53. = $ctx->add_tag('tagsearchlink', 'multiblog_MTTagSearchLink');
  54. $ctx->add_conditional_tag('mtmultiblogiflocalblog');
  55. function multiblog_MTBlogCategoryCount($args, &$ctx) {
  56. return multiblog_function_wrapper('mtblogcategorycount', $args, $ctx);
  57. }
  58. function multiblog_MTBlogCommentCount($args, &$ctx) {
  59. return multiblog_function_wrapper('mtblogcommentcount', $args, $ctx);
  60. }
  61. function multiblog_MTBlogPingCount($args, &$ctx) {
  62. return multiblog_function_wrapper('mtblogpingcount', $args, $ctx);
  63. }
  64. function multiblog_MTBlogEntryCount($args, &$ctx) {
  65. return multiblog_function_wrapper('mtblogentrycount', $args, $ctx);
  66. }
  67. function multiblog_MTTagSearchLink($args, &$ctx) {
  68. return multiblog_function_wrapper('mttagsearchlink', $args, $ctx);
  69. }
  70. # Special handler for MTInclude
  71. function multiblog_MTInclude($args, &$ctx) {
  72. if (isset($args['blog_id'])) {
  73. if (!multiblog_filter_blogs_from_args($ctx, $args))
  74. return '';
  75. } else {
  76. # Explicitly set blog_id attribute to local blog.
  77. # so MTInclude is never affected by multiblog context
  78. $args['blog_id'] = $ctx->stash('local_blog_id');
  79. $args['blog_id'] or $args['blog_id'] = $ctx->mt->blog['blog_id'];
  80. }
  81. global $multiblog_orig_handlers;
  82. $fn = $multiblog_orig_handlers['mtinclude'];
  83. $result = $fn($args, $ctx);
  84. return $result;
  85. }
  86. # MultiBlog plugin wrapper for function tags (i.e. variable tags)
  87. function multiblog_function_wrapper($tag, $args, &$ctx) {
  88. $localvars = array('local_blog_id');
  89. $ctx->localize($localvars);
  90. # Filter blogs from multiblog tag attributes if any
  91. if (!multiblog_filter_blogs_from_args($ctx, $args)) {
  92. $result = 0;
  93. # Set multiblog tag context if applicable
  94. } elseif ($mode = $ctx->stash('multiblog_context')) {
  95. $args[$mode] = $ctx->stash('multiblog_blog_ids');
  96. }
  97. # Call original tag handler with new multiblog args
  98. global $multiblog_orig_handlers;
  99. $fn = $multiblog_orig_handlers[$tag];
  100. $result = $fn($args, $ctx);
  101. # Restore localized variables
  102. $ctx->restore($localvars);
  103. return $result;
  104. }
  105. # MultiBlog plugin wrapper for block tags (i.e. container/conditional)
  106. function multiblog_block_wrapper($args, $content, &$ctx, &$repeat) {
  107. $tag = $ctx->this_tag();
  108. $localvars = array('local_blog_id');
  109. if (!isset($content)) {
  110. $ctx->localize($localvars);
  111. # Filter blogs from multiblog tag attributes if any
  112. if (!multiblog_filter_blogs_from_args($ctx, $args)) {
  113. $repeat = false;
  114. $ctx->restore($localvars);
  115. return '';
  116. # Set multiblog tag context if applicable
  117. } elseif ($mode = $ctx->stash('multiblog_context')) {
  118. $args[$mode] = $ctx->stash('multiblog_blog_ids');
  119. }
  120. }
  121. # Fix for MTMultiBlogIfLocalBlog which should never return
  122. # true with MTTags block because tags are cross-blog
  123. if ($ctx->this_tag() == 'mttags')
  124. $ctx->stash('local_blog_id', 0);
  125. # Call original tag handler with new multiblog args
  126. global $multiblog_orig_handlers;
  127. $fn = $multiblog_orig_handlers[$tag];
  128. $result = $fn($args, $content, $ctx, $repeat);
  129. # Restore localized variables if last loop
  130. if (!$repeat)
  131. $ctx->restore($localvars);
  132. return $result;
  133. }
  134. function multiblog_filter_blogs_from_args(&$ctx, &$args) {
  135. # SANITY CHECK ON ARGUMENTS
  136. # Set and clean up working variables
  137. $incl = $args['include_blogs'];
  138. $incl or $incl = $args['blog_ids'];
  139. $incl or $incl = $args['blog_id'];
  140. $excl = $args['exclude_blogs'];
  141. # Remove spaces
  142. $incl = preg_replace('/\s+/', '', $incl);
  143. $excl = preg_replace('/\s+/', '', $excl);
  144. # If there are no multiblog arguments to filter, we don't need to be here
  145. if (! ($incl or $excl))
  146. return true;
  147. # Only one multiblog argument can be used
  148. $tagcount = 0;
  149. $possible_args = array( $args['include_blogs'],
  150. $args['blog_ids'],
  151. $args['blog_id'],
  152. $args['exclude_blogs']);
  153. foreach ($possible_args as $arg)
  154. $arg != '' and $tagcount++;
  155. if ($tagcount > 1)
  156. return false;
  157. # exclude_blogs="all" is not allowed
  158. if ($excl and ($excl == 'all'))
  159. return false;
  160. # blog_id attribute only accepts a single blog ID
  161. if ($args['blog_id'] and !preg_match('/^\d+$/', $args['blog_id']))
  162. return false;
  163. # Make sure include_blogs/exclude_blogs is valid
  164. if (($incl or $excl) != 'all'
  165. and !preg_match('/^\d+([,-]\d+)*$/', $incl or $excl)) {
  166. return false;
  167. }
  168. # Prepare for filter_blogs
  169. $blogs = array();
  170. $attr = $incl ? 'include_blogs' : 'exclude_blogs';
  171. $val = $incl ? $incl : $excl;
  172. if (preg_match('/-/', $val)) {
  173. # parse range blog ids out
  174. $list = preg_split('/\s*,\s*/', $val);
  175. foreach ($list as $item) {
  176. if (preg_match('/(\d+)-(\d+)/', $item, $matches)) {
  177. for ($i = $matches[1]; $i <= $matches[2]; $i++)
  178. $blogs[] = $i;
  179. } else {
  180. $blogs[] = $item;
  181. }
  182. }
  183. } else {
  184. $blogs = preg_split('/\s*,\s*/', $val);
  185. }
  186. # Filter the blogs using the MultiBlog access controls
  187. list($attr, $blogs) = multiblog_filter_blogs($ctx, $attr, $blogs);
  188. if (!($attr && count($blogs)))
  189. return false;
  190. # Rewrite the args to the modifed value
  191. if ($args['blog_ids'])
  192. unset($args['blog_ids']); // Deprecated
  193. if ($args['blog_id']) {
  194. $args['blog_id'] = $blogs[0];
  195. } else {
  196. unset($args['include_blogs']);
  197. unset($args['exclude_blogs']);
  198. $args[$attr] = implode(',', $blogs);
  199. }
  200. return true;
  201. }
  202. ## Get a mode (include/exclude) and list of blogs
  203. ## Process list using system default access setting and
  204. ## any blog-level overrides.
  205. ## Returns empty list if no blogs can be used
  206. function multiblog_filter_blogs(&$ctx, $is_include, $blogs) {
  207. # Set flag to indicate whether @blogs are to be included or excluded
  208. $is_include = $is_include == 'include_blogs' ? 1 : 0;
  209. # Set local blog
  210. $this_blog = $ctx->stash('blog_id');
  211. global $multiblog_system_config;
  212. global $mt;
  213. if (!$multiblog_system_config)
  214. $multiblog_system_config = $mt->db->fetch_plugin_config('MultiBlog', 'system');
  215. # Get the MultiBlog system config for default access and overrides
  216. if (isset($multiblog_system_config['default_access_allowed']))
  217. $default_access_allowed = $multiblog_system_config['default_access_allowed'];
  218. else
  219. $default_access_allowed = 1;
  220. $access_overrides =
  221. $multiblog_system_config['access_overrides'];
  222. if (!$access_overrides) $access_overrides = array();
  223. # System setting allows access by default
  224. if ($default_access_allowed) {
  225. # include_blogs="all"
  226. if ($is_include && ($blogs[0] == "all")) {
  227. # Check for any deny overrides.
  228. # If found, switch to exclude_blogs="..."
  229. $deny = array();
  230. foreach (array_keys($access_overrides) as $o) {
  231. if (($o != $this_blog) && (isset($access_overrides[$o]) && ($access_overrides[$o] == MULTIBLOG_ACCESS_DENIED)))
  232. $deny[] = $o;
  233. }
  234. return count($deny) ? array('exclude_blogs', $deny)
  235. : array('include_blogs', array('all'));
  236. }
  237. # include_blogs="1,2,3,4"
  238. elseif ($is_include && count($blogs)) {
  239. # Remove any included blogs that are specifically deny override
  240. # Return undef is all specified blogs are deny override
  241. $allow = array();
  242. foreach ($blogs as $b)
  243. if ($b == $this_blog || (!isset($access_overrides[$b])) || ($access_overrides[$b] == MULTIBLOG_ACCESS_ALLOWED))
  244. $allow[] = $b;
  245. return count($allow) ? array('include_blogs', $allow) : null;
  246. }
  247. # exclude_blogs="1,2,3,4"
  248. else {
  249. # Add any deny overrides blogs to the list and de-dupe
  250. foreach (array_keys($access_overrides) as $o)
  251. if (($o != $this_blog) && (isset($access_overrides[$o]) && ($access_overrides[$o] == MULTIBLOG_ACCESS_DENIED)))
  252. $blogs[] = $o;
  253. $seen = array();
  254. foreach ($blogs as $b)
  255. $seen[$b] = 1;
  256. $blogs = array_keys($seen);
  257. return array('exclude_blogs', $blogs);
  258. }
  259. }
  260. # System setting does not allow access by default
  261. else {
  262. # include_blogs="all"
  263. if ($is_include && ($blogs[0] == "all")) {
  264. # Enumerate blogs from allow override
  265. # Hopefully this is significantly smaller than @all_blogs
  266. $allow = array();
  267. foreach (array_keys($access_overrides) as $o)
  268. if (($o == $this_blog)
  269. || (isset($access_overrides[$o]) && ($access_overrides[$o] == MULTIBLOG_ACCESS_ALLOWED)))
  270. $allow[] = $o;
  271. if (!isset($access_overrides[$this_blog]))
  272. $allow[] = $this_blog;
  273. return count($allow) ? array('include_blogs', $allow) : null;
  274. }
  275. # include_blogs="1,2,3,4"
  276. elseif ($is_include && count($blogs)) {
  277. # Filter @blogs returning only those with allow override
  278. $allow = array();
  279. foreach ($blogs as $b)
  280. if ($b == $this_blog
  281. || (isset($access_overrides[$b]) && ($access_overrides[$b] == MULTIBLOG_ACCESS_ALLOWED)))
  282. $allow[] = $b;
  283. return count($allow) ? array('include_blogs', $allow) : null;
  284. }
  285. # exclude_blogs="1,2,3,4"
  286. else {
  287. # Get allow override blogs and then omit
  288. # the specified excluded blogs.
  289. $allow = array();
  290. foreach (array_keys($access_overrides) as $o)
  291. if (($o == $this_blog)
  292. || (isset($access_overrides[$o]) && ($access_overrides[$o] == MULTIBLOG_ACCESS_ALLOWED)))
  293. $allow[] = $o;
  294. if (!isset($access_overrides[$this_blog]))
  295. $allow[] = $this_blog;
  296. $seen = array();
  297. foreach ($blogs as $b)
  298. $seen[$b] = 1;
  299. $blogs = array();
  300. foreach ($allow as $a)
  301. if (!isset($seen[$a])) $blogs[] = $a;
  302. return count($blogs) ? array('include_blogs', $blogs) : null;
  303. }
  304. }
  305. }
  306. ?>