Example #1
0
 function update_conversation()
 {
     global $db, $globals;
     $previous_ids = $db->get_col("select distinct conversation_to from conversations where conversation_type='post' and conversation_from={$this->id}");
     if ($previous_ids) {
         // Select users previous conversation to decrease in the new system
         $previous_users = $db->get_col("select distinct conversation_user_to from conversations where conversation_type='post' and conversation_from={$this->id}");
     } else {
         $previous_users = array();
     }
     //$db->query("delete from conversations where conversation_type='post' and conversation_from=$this->id");
     $seen_users = array();
     $seen_ids = array();
     $refs = 0;
     if (!$this->date) {
         $this->date = time();
     }
     if (preg_match_all(Post::REF_PREG, $this->content, $matches)) {
         foreach ($matches[2] as $reference) {
             $user = $db->escape(preg_replace('/,\\d+$/', '', $reference));
             $to = $db->get_var("select user_id from users where user_login = '******'");
             $id = intval(preg_replace('/[^\\s]+,(\\d+)$/', '$1', $reference));
             if (!$to > 0) {
                 continue;
             }
             if (!$id > 0) {
                 $id = (int) $db->get_var("select post_id from posts where post_user_id = {$to} and post_date < FROM_UNIXTIME({$this->date}) order by post_date desc limit 1");
             }
             if (!in_array($id, $previous_ids) && !in_array($id, $seen_ids)) {
                 if (User::friend_exists($to, $this->author) >= 0 && $refs < 10 && $this->author != $to && !in_array($to, $seen_users) && !in_array($to, $previous_users)) {
                     User::add_notification($to, 'post');
                 }
                 $db->query("insert into conversations (conversation_user_to, conversation_type, conversation_time, conversation_from, conversation_to) values ({$to}, 'post', from_unixtime({$this->date}), {$this->id}, {$id})");
             }
             $refs++;
             if (!in_array($id, $seen_ids)) {
                 $seen_ids[] = $id;
             }
             if (!in_array($to, $seen_users)) {
                 $seen_users[] = $to;
             }
         }
     }
     $to_delete = array_diff($previous_ids, $seen_ids);
     if ($to_delete) {
         $to_delete = implode(',', $to_delete);
         $db->query("delete from conversations where conversation_type='post' and conversation_from={$this->id} and conversation_to in ({$to_delete})");
     }
     $to_unnotify = array_diff($previous_users, $seen_users);
     foreach ($to_unnotify as $to) {
         User::add_notification($to, 'post', -1);
     }
 }
Example #2
0
function save_post($message_id)
{
    global $link, $db, $message, $current_user, $globals, $site_key;
    $message = new PrivateMessage();
    $to_user = User::get_valid_username($_POST['to_user']);
    if (!$to_user) {
        echo 'ERROR: ' . _('nombre de usuario erróneo');
        die;
    }
    $to = User::get_user_id($to_user);
    if (!$to > 0) {
        echo 'ERROR: ' . _('usuario erróneo');
        die;
    }
    if (!PrivateMessage::can_send($current_user->user_id, $to)) {
        echo 'ERROR: ' . _('el destinatario no lo tiene amigado');
        die;
    }
    $_POST['post'] = clean_text_with_tags($_POST['post'], 0, false, $globals['posts_len']);
    if (!empty($_FILES['image']['tmp_name'])) {
        $limit_exceded = Upload::current_user_limit_exceded($_FILES['image']['size']);
        if ($limit_exceded) {
            echo 'ERROR: ' . $limit_exceded;
            die;
        }
    }
    if (mb_strlen($_POST['post']) < 2) {
        echo 'ERROR: ' . _('texto muy corto');
        die;
    }
    if ($current_user->user_id != intval($_POST['author'])) {
        die;
    }
    // Check the post wasn't already stored
    $message->randkey = intval($_POST['key']);
    $message->author = $current_user->user_id;
    $message->to = $to;
    $message->content = $_POST['post'];
    $db->transaction();
    $dupe = intval($db->get_var("select count(*) from privates where user = {$current_user->user_id} and date > date_sub(now(), interval 5 minute) and randkey = {$message->randkey} FOR UPDATE"));
    if (!$dupe) {
        // Verify that there are a period of 1 minute between posts.
        if (intval($db->get_var("select count(*) from privates where user= {$current_user->user_id} and date > date_sub(now(), interval 15 second)")) > 0) {
            echo 'ERROR: ' . _('debe esperar 15 segundos entre mensajes');
            $db->rollback();
            die;
        }
        // Verify that there less than X messages from the same user in a day
        if (intval($db->get_var("select count(*) from privates where user= {$current_user->user_id} and date > date_sub(now(), interval 1 day)")) > 160) {
            echo 'ERROR: ' . _('demasiados mensajes en un día');
            die;
        }
        $db->commit();
        $message->store();
        notify_user($current_user->user_id, $to, $message->content);
        User::add_notification($message->to, 'private');
    } else {
        $db->commit();
        echo 'ERROR: ' . _('mensaje grabado previamente');
        die;
    }
    // Check image upload or delete
    if ($_POST['image_delete']) {
        $message->delete_image();
    } else {
        $message->store_image_from_form('image');
    }
    $message = PrivateMessage::from_db($message->id);
    // Reread the object
    $message->print_summary();
}
Example #3
0
 function update_conversation()
 {
     global $db, $globals, $current_user;
     $previous_ids = $db->get_col("select distinct conversation_to from conversations where conversation_type='comment' and conversation_from={$this->id}");
     if ($previous_ids) {
         // Select users previous conversation to decrease in the new system
         $previous_users = $db->get_col("select distinct conversation_user_to from conversations where conversation_type='comment' and conversation_from={$this->id}");
     } else {
         $previous_users = array();
     }
     $seen_users = array();
     $seen_ids = array();
     $refs = 0;
     $orders = array();
     if (preg_match_all('/(?:^|\\W)(#(?:\\d+)|@(?:[\\p{L}\\.][\\.\\d\\-_\\p{L}]+))\\b/u', $this->content, $matches)) {
         foreach ($matches[1] as $order) {
             $order = substr($order, 1);
             // Delete the # or @
             $orders[$order] += 1;
         }
     }
     if (!$this->date) {
         $this->date = time();
     }
     foreach ($orders as $order => $val) {
         if ($refs > 10) {
             // Limit the number of references to avoid abuses/spam
             syslog(LOG_NOTICE, "Meneame: too many references in comment: {$this->id} ({$current_user->user_login})");
             break;
         }
         if (!preg_match('/^\\d+/', $order)) {
             $username_to = $db->escape($order);
             $to = $db->get_row("select 0 as id, user_id from users where user_login = '******'");
         } elseif ($order == 0) {
             $to = $db->get_row("select 0 as id, link_author as user_id from links where link_id = {$this->link}");
         } else {
             $to = $db->get_row("select comment_id as id, comment_user_id as user_id from comments where comment_link_id = {$this->link} and comment_order={$order} and comment_type != 'admin'");
         }
         if (!$to) {
             continue;
         }
         if (!in_array($to->id, $previous_ids) && !in_array($to->id, $seen_ids)) {
             if (User::friend_exists($to->user_id, $this->author) >= 0 && $to->user_id != $this->author && !in_array($to->user_id, $seen_users) && !in_array($to->user_id, $previous_users)) {
                 User::add_notification($to->user_id, 'comment');
             }
             $db->query("insert into conversations (conversation_user_to, conversation_type, conversation_time, conversation_from, conversation_to) values ({$to->user_id}, 'comment', from_unixtime({$this->date}), {$this->id}, {$to->id})");
         }
         $refs++;
         if (!in_array($id, $seen_ids)) {
             $seen_ids[] = $to->id;
         }
         if (!in_array($to, $seen_users)) {
             $seen_users[] = $to->user_id;
         }
     }
     $to_delete = array_diff($previous_ids, $seen_ids);
     if ($to_delete) {
         $to_delete = implode(',', $to_delete);
         $db->query("delete from conversations where conversation_type='comment' and conversation_from={$this->id} and conversation_to in ({$to_delete})");
     }
     $to_unnotify = array_diff($previous_users, $seen_users);
     foreach ($to_unnotify as $to) {
         User::add_notification($to, 'comment', -1);
     }
 }