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