Commit | Line | Data |
---|---|---|
8c39c698 FB |
1 | <?php |
2 | ||
3 | class xorgPostPermsWidget { | |
4 | public static function behavior_adminPostFormSidebar($post) { | |
5 | $choices = array('public' => array('text' => 'Visible par tous', | |
6 | 'selected' => false), | |
7 | 'auth' => array('text' => 'Visible par les X', | |
8 | 'selected' => false)); | |
9 | $pos = 'public'; | |
dc51645d FB |
10 | global $core; |
11 | $type = $core->blog->settings->get('xorg_blog_type'); | |
12 | if ($type == 'group-admin' || $type == 'group-member') { | |
13 | $choices['group'] = array('text' => 'Visible par les membres du groupe', | |
14 | 'selected' => false); | |
15 | } | |
8c39c698 FB |
16 | if (!is_null($post)) { |
17 | $metas = unserialize($post->field('post_meta')); | |
18 | if (isset($metas['post_xorg_perms'])) { | |
19 | $pos = $metas['post_xorg_perms']; | |
20 | } | |
21 | } else { | |
8c39c698 FB |
22 | $pos = $core->auth->getOption('post_xorg_perms'); |
23 | if ($pos && !isset($choices[$pos])) { | |
24 | $pos = 'auth'; | |
25 | } | |
26 | } | |
27 | $choices[$pos]['selected'] = true; | |
28 | echo '<p><label>Visibilité du billet :' | |
29 | . ' <select name="post_xorg_perms">'; | |
30 | foreach ($choices as $val => $fields) { | |
31 | echo '<option value="' . $val . '"' . ($fields['selected'] ? ' selected="selected"' : '') . '>' | |
32 | . $fields['text'] . '</option>'; | |
33 | } | |
34 | echo ' </select>' | |
35 | . '</label></p>'; | |
36 | } | |
37 | ||
38 | private static function setPermsMeta(&$cur) { | |
39 | $meta = $cur->getField('post_meta'); | |
40 | if (is_string($meta)) { | |
41 | $meta = unserialize($meta); | |
42 | } | |
43 | if (!is_array($meta)) { | |
44 | $meta = array(); | |
45 | } | |
46 | $meta['post_xorg_perms'] = $_POST['post_xorg_perms']; | |
47 | $cur->setField('post_meta', serialize($meta)); | |
48 | } | |
49 | ||
50 | public static function behavior_adminBeforePostCreate(&$cur) { | |
51 | self::setPermsMeta($cur); | |
52 | } | |
53 | ||
54 | public static function behavior_adminBeforePostUpdate(&$cur, $post_id) { | |
55 | self::setPermsMeta($cur); | |
56 | } | |
57 | ||
58 | ||
59 | public static function behavior_adminPreferencesForm($core) { | |
60 | $levels = array('public' => array('text' => 'Visible par tous', | |
61 | 'selected' => false), | |
62 | 'auth' => array('text' => 'Visible par les X', | |
63 | 'selected' => false), | |
64 | 'group' => array('text' => 'Visible par les membres du groupe (1)', | |
65 | 'selected' => false)); | |
66 | $pos = $core->auth->getOption('post_xorg_perms'); | |
67 | if (!$pos || !isset($levels[$pos])) { | |
68 | $pos = 'public'; | |
69 | } | |
70 | $levels[$pos]['selected'] = true; | |
71 | echo '<p><label>Visibilité nouveaux billets par défaut :' | |
72 | . ' <select name="post_xorg_perms">'; | |
73 | foreach ($levels as $key => $fields) { | |
74 | echo '<option value="' . $key . '"' . ($fields['selected'] ? ' selected="selected"' : '') . '>' | |
75 | . $fields['text'] . '</option>'; | |
76 | } | |
77 | echo '</select>' | |
78 | . '(1) Ne concerne que les blogs de groupes X. Equivaut à "Visible par les X" sur les autres blogs"' | |
79 | . '</label></p>'; | |
80 | } | |
81 | ||
82 | public static function behavior_adminBeforeUserUpdate(&$cur, $user_id) { | |
83 | $opts = $cur->getField('user_options'); | |
84 | $opts['post_xorg_perms'] = $_POST['post_xorg_perms']; | |
85 | $cur->setField('user_options', $opts); | |
86 | } | |
d387689d FB |
87 | |
88 | public static function behavior_coreBlogGetPosts(&$rs) { | |
89 | $rs->extend('xorgPostPermsFilter'); | |
90 | } | |
91 | ||
92 | /* public static function behavior_coreBlogGetComments(&$rs) { | |
93 | $rs->extends('xorgCommentPermsFilter'); | |
94 | }*/ | |
95 | } | |
96 | ||
97 | if (class_exists('rsExtPostPublic')) { | |
98 | ||
99 | class xorgPostPermsFilter extends rsExtPostPublic { | |
100 | private static function canRead(&$rs) { | |
101 | $metas = unserialize($rs->field('post_meta')); | |
102 | global $core; | |
103 | if (!isset($metas['post_xorg_perms'])) { | |
104 | return true; | |
105 | } elseif ($metas['post_xorg_perms'] == 'public') { | |
106 | return true; | |
107 | } elseif ($metas['post_xorg_perms'] == 'auth' && $core->auth->userID()) { | |
108 | return true; | |
109 | } elseif ($metas['post_xorg_perms'] == 'group' && $core->auth->getInfo('xorg_group_member')) { | |
110 | return true; | |
111 | } | |
112 | return false; | |
113 | } | |
114 | ||
f4aeade7 FB |
115 | private static function showMessage(&$rs) { |
116 | $metas = unserialize($rs->field('post_meta')); | |
117 | global $core; | |
118 | $str = '<p class="error">' | |
119 | . 'Vous n\'avez pas les droits suffisants pour lire ce billet<br />'; | |
120 | if (!$core->auth->userID()) { | |
121 | $str .= 'Vous devez vous <a href="' . $core->blog->url . 'auth/Xorg?path=' . $_SERVER['REQUEST_URI'] .'">authentifier</a>'; | |
122 | } else { | |
123 | $str .= 'Tu dois être membre du groupe pour lire ce message'; | |
124 | } | |
125 | return $str . '</p>'; | |
126 | } | |
127 | ||
d387689d FB |
128 | public static function getContent(&$rs, $absolute_urls = false) { |
129 | if (self::canRead($rs)) { | |
130 | return parent::getContent(&$rs, $absolute_urls); | |
131 | } else if (!self::isExtended($rs)) { | |
f4aeade7 | 132 | return self::showMessage($rs); |
d387689d FB |
133 | } else { |
134 | return null; | |
135 | } | |
136 | } | |
137 | ||
138 | public static function getExcerpt(&$rs, $absolute_urls = false) { | |
139 | if (self::canRead($rs)) { | |
140 | return parent::getContent(&$rs, $absolute_urls); | |
141 | } else if (self::isExtended($rs)) { | |
f4aeade7 | 142 | return self::showMessage($rs); |
d387689d FB |
143 | } else { |
144 | return null; | |
145 | } | |
146 | } | |
147 | ||
148 | public static function commentsActive(&$rs) { | |
149 | return self::canRead($rs) && parent::commentsActive($rs); | |
150 | } | |
151 | ||
152 | public static function trackbacksActive(&$rs) { | |
153 | return self::canRead($rs) && parent::trackbacksActive($rs); | |
154 | } | |
155 | ||
156 | public static function hasComments(&$rs) { | |
157 | return self::canRead($rs) && parent::hasComments($rs); | |
158 | } | |
159 | } | |
8c39c698 FB |
160 | } |
161 | ?> |