| 1 | <?php |
|---|
| 2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
|---|
| 3 | |
|---|
| 4 | /** |
|---|
| 5 | * Send comment to subscribers |
|---|
| 6 | * @param array comm |
|---|
| 7 | */ |
|---|
| 8 | function send_comment_to_subscribers($comm) |
|---|
| 9 | { |
|---|
| 10 | global $conf, $page, $user; |
|---|
| 11 | |
|---|
| 12 | $type= isset($comm['category_id']) ? 'category' : 'image'; |
|---|
| 13 | |
|---|
| 14 | // exclude current user |
|---|
| 15 | $exclude = null; |
|---|
| 16 | if (!empty($_POST['stc_mail'])) $exclude = pwg_db_real_escape_string($_POST['stc_mail']); |
|---|
| 17 | if (!is_a_guest()) $exclude = $user['email']; |
|---|
| 18 | |
|---|
| 19 | // get subscribers emails |
|---|
| 20 | $query = ' |
|---|
| 21 | SELECT |
|---|
| 22 | email |
|---|
| 23 | FROM '.SUBSCRIBE_TO_TABLE.' |
|---|
| 24 | WHERE |
|---|
| 25 | '.$type.'_id = '.$comm[$type.'_id'].' |
|---|
| 26 | AND validated = true |
|---|
| 27 | AND email != "'.$exclude.'" |
|---|
| 28 | '; |
|---|
| 29 | $emails = array_from_query($query, 'email'); |
|---|
| 30 | |
|---|
| 31 | set_make_full_url(); |
|---|
| 32 | switch ($type) |
|---|
| 33 | { |
|---|
| 34 | case 'image': |
|---|
| 35 | { |
|---|
| 36 | // get image infos |
|---|
| 37 | $query = ' |
|---|
| 38 | SELECT |
|---|
| 39 | id, |
|---|
| 40 | name, |
|---|
| 41 | file |
|---|
| 42 | FROM '.IMAGES_TABLE.' |
|---|
| 43 | WHERE id = '.$comm['image_id'].' |
|---|
| 44 | ;'; |
|---|
| 45 | $element = pwg_db_fetch_assoc(pwg_query($query)); |
|---|
| 46 | |
|---|
| 47 | if (empty($element['name'])) |
|---|
| 48 | { |
|---|
| 49 | $element['name'] = get_name_from_file($element['file']); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | $url_params = array('image_id' => $element['id']); |
|---|
| 53 | if (!empty($page['category'])) |
|---|
| 54 | { |
|---|
| 55 | $url_params['section'] = 'categories'; |
|---|
| 56 | $url_params['category'] = $page['category']; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $element['url'] = make_picture_url($url_params); |
|---|
| 60 | break; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | case 'category' : |
|---|
| 64 | { |
|---|
| 65 | // get category infos |
|---|
| 66 | $query = ' |
|---|
| 67 | SELECT |
|---|
| 68 | id, |
|---|
| 69 | name |
|---|
| 70 | FROM '.CATEGORIES_TABLE.' |
|---|
| 71 | WHERE id = '.$comm['category_id'].' |
|---|
| 72 | ;'; |
|---|
| 73 | $element = pwg_db_fetch_assoc(pwg_query($query)); |
|---|
| 74 | |
|---|
| 75 | $url_params['section'] = 'categories'; |
|---|
| 76 | $url_params['category'] = $element; |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | $element['url'] = make_index_url($url_params); |
|---|
| 80 | break; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | // get author name |
|---|
| 85 | if ($comm['author'] == 'guest') |
|---|
| 86 | { |
|---|
| 87 | $comm['author'] = l10n('guest'); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | // mail content |
|---|
| 91 | $mail_args = array( |
|---|
| 92 | 'subject' => '['.strip_tags($conf['gallery_title']).'] Re:'.$element['name'], |
|---|
| 93 | 'content_format' => 'text/html', |
|---|
| 94 | ); |
|---|
| 95 | |
|---|
| 96 | $generic_content = ' |
|---|
| 97 | <b>.'.trigger_event('render_comment_author', $comm['author']).'</b> wrote : |
|---|
| 98 | |
|---|
| 99 | <blockquote>'.trigger_event('render_comment_content', $comm['content']).'</blockquote> |
|---|
| 100 | |
|---|
| 101 | <a href="'.$element['url'].'#comment-'.$comm['id'].'">Link to comment</a> |
|---|
| 102 | <br><br> |
|---|
| 103 | ================================ |
|---|
| 104 | <br><br>'; |
|---|
| 105 | |
|---|
| 106 | foreach ($emails as $email) |
|---|
| 107 | { |
|---|
| 108 | $mail_args['content'] = $generic_content.' |
|---|
| 109 | <a href="'.make_stc_url('unsubscribe-'.$type, $email, $element['id']).'">Stop receiving notifications for this picture</a><br> |
|---|
| 110 | <a href="'.make_stc_url('unsubscribe-all', $email).'">Stop receiving all notifications</a><br> |
|---|
| 111 | '; |
|---|
| 112 | //<a href="'.make_stc_url('manage', $email).'">Manage my subscribtions</a> |
|---|
| 113 | pwg_mail($email, $mail_args); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | unset_make_full_url(); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /* |
|---|
| 120 | * add an email to subscribers list |
|---|
| 121 | * @param int (image|category)_id |
|---|
| 122 | * @param string email |
|---|
| 123 | * @param string type (image|category) |
|---|
| 124 | */ |
|---|
| 125 | function subscribe_to_comments($element_id, $email, $type='image') |
|---|
| 126 | { |
|---|
| 127 | global $page, $user, $conf, $template, $picture; |
|---|
| 128 | |
|---|
| 129 | $infos = $errors = array(); |
|---|
| 130 | |
|---|
| 131 | if ( is_a_guest() and empty($email) ) |
|---|
| 132 | { |
|---|
| 133 | array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.')); |
|---|
| 134 | return; |
|---|
| 135 | } |
|---|
| 136 | else if ( !is_a_guest() ) |
|---|
| 137 | { |
|---|
| 138 | $email = $user['email']; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | $query = ' |
|---|
| 142 | INSERT IGNORE INTO '.SUBSCRIBE_TO_TABLE.'( |
|---|
| 143 | email, |
|---|
| 144 | '.$type.'_id, |
|---|
| 145 | registration_date, |
|---|
| 146 | validated |
|---|
| 147 | ) |
|---|
| 148 | VALUES( |
|---|
| 149 | "'.pwg_db_real_escape_string($email).'", |
|---|
| 150 | '.$element_id.', |
|---|
| 151 | NOW(), |
|---|
| 152 | "'.(is_a_guest() ? "false" : "true").'" |
|---|
| 153 | ) |
|---|
| 154 | ;'; |
|---|
| 155 | pwg_query($query); |
|---|
| 156 | |
|---|
| 157 | if (is_a_guest() and pwg_db_insert_id() != 0) |
|---|
| 158 | { |
|---|
| 159 | $mail_args = array( |
|---|
| 160 | 'subject' => '['.strip_tags($conf['gallery_title']).'] Please confirm your subscribtion to comments', |
|---|
| 161 | 'content_format' => 'text/html', |
|---|
| 162 | ); |
|---|
| 163 | |
|---|
| 164 | $mail_args['content'] = ' |
|---|
| 165 | You requested to subscribe by email to comments on <b>'.$picture['current']['name'].'</b>.<br> |
|---|
| 166 | <br> |
|---|
| 167 | We care about your inbox, so we want to confirm this request. Please click the confirm link to activate the subscription.<br> |
|---|
| 168 | <br> |
|---|
| 169 | <a href="'.make_stc_url('validate-'.$type, $email, $element_id).'">Confirm subscription</a><br> |
|---|
| 170 | <br> |
|---|
| 171 | If you did not request this action please disregard this message. |
|---|
| 172 | '; |
|---|
| 173 | |
|---|
| 174 | pwg_mail($email, $mail_args); |
|---|
| 175 | array_push($infos, l10n('Please check your email inbox to confirm your subscription.')); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | if (!empty($infos)) |
|---|
| 179 | { |
|---|
| 180 | $template->assign('infos', array_merge($template->get_template_vars('infos'), $infos)); |
|---|
| 181 | } |
|---|
| 182 | if (!empty($errors)) |
|---|
| 183 | { |
|---|
| 184 | $template->assign('errors', array_merge($template->get_template_vars('errors'), $errors)); |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * create absolute url to subscriptions section |
|---|
| 190 | * @param string action |
|---|
| 191 | * @param string email |
|---|
| 192 | * @return string |
|---|
| 193 | */ |
|---|
| 194 | function make_stc_url($action, $email) |
|---|
| 195 | { |
|---|
| 196 | if ( empty($action) or empty($email) ) return null; |
|---|
| 197 | |
|---|
| 198 | global $conf; |
|---|
| 199 | set_make_full_url(); |
|---|
| 200 | |
|---|
| 201 | $url_params = array( |
|---|
| 202 | 'action' => $action, |
|---|
| 203 | 'email' => $email, |
|---|
| 204 | 'key' => crypt_value($action.$email, $conf['secret_key']), |
|---|
| 205 | ); |
|---|
| 206 | |
|---|
| 207 | if (func_num_args() > 2) |
|---|
| 208 | { |
|---|
| 209 | $url_params['param'] = func_get_arg(2); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | $url = add_url_params( |
|---|
| 213 | make_index_url( array('section' => 'subscriptions') ), |
|---|
| 214 | $url_params |
|---|
| 215 | ); |
|---|
| 216 | |
|---|
| 217 | unset_make_full_url(); |
|---|
| 218 | return $url; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * crypt a string using mcrypt extension or a binary method |
|---|
| 223 | * @param string value to crypt |
|---|
| 224 | * @param string key |
|---|
| 225 | * @return string |
|---|
| 226 | */ |
|---|
| 227 | function crypt_value($value, $key) |
|---|
| 228 | { |
|---|
| 229 | if (extension_loaded('mcrypt')) |
|---|
| 230 | { |
|---|
| 231 | $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|---|
| 232 | $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|---|
| 233 | $value = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $value, MCRYPT_MODE_ECB, $iv); |
|---|
| 234 | } |
|---|
| 235 | else |
|---|
| 236 | { |
|---|
| 237 | $value = $value ^ $key; // binary XOR operation |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | $value = base64url_encode($value); |
|---|
| 241 | return trim($value); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /** |
|---|
| 245 | * decrypt a string crypted with previous function |
|---|
| 246 | * @param string value to decrypt |
|---|
| 247 | * @param string key |
|---|
| 248 | * @return string |
|---|
| 249 | */ |
|---|
| 250 | function decrypt_value($value, $key) |
|---|
| 251 | { |
|---|
| 252 | $value = base64url_decode($value); |
|---|
| 253 | |
|---|
| 254 | if (extension_loaded('mcrypt')) |
|---|
| 255 | { |
|---|
| 256 | $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); |
|---|
| 257 | $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); |
|---|
| 258 | $value = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $value, MCRYPT_MODE_ECB, $iv); |
|---|
| 259 | } |
|---|
| 260 | else |
|---|
| 261 | { |
|---|
| 262 | $value = $value ^ $key; // binary XOR operation |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | return trim($value); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | /** |
|---|
| 269 | * variant of base64 functions usable into url |
|---|
| 270 | * http://fr.php.net/manual/fr/function.base64-encode.php#103849 |
|---|
| 271 | */ |
|---|
| 272 | function base64url_encode($data) |
|---|
| 273 | { |
|---|
| 274 | return rtrim(strtr(base64_encode($data), '+/', '-_'), '='); |
|---|
| 275 | } |
|---|
| 276 | function base64url_decode($data) |
|---|
| 277 | { |
|---|
| 278 | return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT)); |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | ?> |
|---|