Example #1
0
 public function add()
 {
     if (preg_match(':^admin/reports/edit:', url::current())) {
         // We replace this because we want to add our configureables in the same
         // section.
         Event::replace('ushahidi_action.report_form_admin_after_time', array(new endtime(), '_report_form'), array($this, '_report_form'));
         // Hook into the report_submit_admin (post_POST) event right before saving
         Event::replace('ushahidi_action.report_submit_admin', array(new endtime(), '_report_validate'), array($this, '_report_validate'));
         // Hook into the report_edit (post_SAVE) event
         Event::replace('ushahidi_action.report_edit', array(new endtime(), '_report_form_submit'), array($this, '_report_form_submit'));
     } else {
         if (preg_match(':^decayimage:', url::current())) {
             Event::add('ushahidi_filter.header_js', array($this, 'decayimage_ushahidi_filter_header_js'));
         } else {
             if (preg_match(':admin/manage:', url::current())) {
                 Event::add('ushahidi_action.category_save', array($this, 'decayimage_ushahidi_filter_category_save'));
             }
         }
     }
 }
Example #2
0
 public function receive_event(Event $event)
 {
     global $config, $database, $page, $user;
     if (is_null($this->theme)) {
         $this->theme = get_theme_object($this);
     }
     if ($event instanceof InitExtEvent) {
         if ($config->get_int("ext_numeric_score_version", 0) < 1) {
             $this->install();
         }
     }
     if ($event instanceof DisplayingImageEvent) {
         if (!$user->is_anonymous()) {
             $html = $this->theme->get_voter_html($event->image);
             $page->add_block(new Block("Image Score", $html, "left", 20));
         }
     }
     if ($event instanceof UserPageBuildingEvent) {
         $html = $this->theme->get_nuller_html($event->display_user);
         $page->add_block(new Block("Votes", $html, "main", 60));
     }
     if ($event instanceof PageRequestEvent) {
         if ($event->page_matches("numeric_score_votes")) {
             $image_id = int_escape($event->get_arg(0));
             $x = $database->get_all("SELECT users.name as username, user_id, score \n\t\t\t\t\tFROM numeric_score_votes \n\t\t\t\t\tJOIN users ON numeric_score_votes.user_id=users.id\n\t\t\t\t\tWHERE image_id=?", array($image_id));
             $html = "<table>";
             foreach ($x as $vote) {
                 $html .= "<tr><td>";
                 $html .= "<a href='/user/{$vote['username']}'>{$vote['username']}</a>";
                 $html .= "</td><td>";
                 $html .= $vote['score'];
                 $html .= "</td></tr>";
             }
             die($html);
         }
         if ($event->page_matches("numeric_score_vote") && $user->check_auth_token()) {
             if (!$user->is_anonymous()) {
                 $image_id = int_escape($_POST['image_id']);
                 $char = $_POST['vote'];
                 $score = null;
                 if ($char == "up") {
                     $score = 1;
                 } else {
                     if ($char == "null") {
                         $score = 0;
                     } else {
                         if ($char == "down") {
                             $score = -1;
                         }
                     }
                 }
                 if (!is_null($score) && $image_id > 0) {
                     send_event(new NumericScoreSetEvent($image_id, $user, $score));
                 }
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/view/{$image_id}"));
             }
         }
         if ($event->page_matches("numeric_score/remove_votes_on") && $user->check_auth_token()) {
             if ($user->is_admin()) {
                 $image_id = int_escape($_POST['image_id']);
                 $database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($image_id));
                 $database->execute("UPDATE images SET numeric_score=0 WHERE id=?", array($image_id));
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link("post/view/{$image_id}"));
             }
         }
         if ($event->page_matches("numeric_score/remove_votes_by") && $user->check_auth_token()) {
             if ($user->is_admin()) {
                 $user_id = int_escape($_POST['user_id']);
                 $image_ids = $database->get_col("SELECT image_id FROM numeric_score_votes WHERE user_id=?", array($user_id));
                 $database->execute("DELETE FROM numeric_score_votes WHERE user_id=? AND image_id IN ?", array($user_id, $image_ids));
                 $database->execute("UPDATE images SET numeric_score=(SELECT SUM(score) FROM numeric_score_votes WHERE image_id=images.id) WHERE images.id IN ?", array($image_ids));
                 $page->set_mode("redirect");
                 $page->set_redirect(make_link());
             }
         }
     }
     if ($event instanceof NumericScoreSetEvent) {
         log_info("numeric_score", "Rated Image #{$event->image_id} as {$event->score}");
         $this->add_vote($event->image_id, $user->id, $event->score);
     }
     if ($event instanceof ImageDeletionEvent) {
         $database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($event->image->id));
     }
     if ($event instanceof ParseLinkTemplateEvent) {
         $event->replace('$score', $event->image->numeric_score);
     }
     if ($event instanceof SearchTermParseEvent) {
         $matches = array();
         if (preg_match("/^score(<|<=|=|>=|>)(\\d+)\$/", $event->term, $matches)) {
             $cmp = $matches[1];
             $score = $matches[2];
             $event->add_querylet(new Querylet("numeric_score {$cmp} {$score}"));
         }
         if (preg_match("/^upvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=1)", array($duser->id)));
         }
         if (preg_match("/^downvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=-1)", array($duser->id)));
         }
         if (preg_match("/^upvoted_by_id=(\\d+)\$/", $event->term, $matches)) {
             $iid = int_escape($matches[1]);
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=1)", array($iid)));
         }
         if (preg_match("/^downvoted_by_id=(\\d+)\$/", $event->term, $matches)) {
             $iid = int_escape($matches[1]);
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=-1)", array($iid)));
         }
     }
 }
Example #3
0
 public function receive_event(Event $event)
 {
     global $config, $database, $page, $user;
     if (is_null($this->theme)) {
         $this->theme = get_theme_object($this);
     }
     if ($event instanceof InitExtEvent) {
         if ($config->get_int("ext_numeric_score_version", 0) < 1) {
             $this->install();
         }
     }
     if ($event instanceof DisplayingImageEvent) {
         if (!$user->is_anonymous()) {
             $html = $this->theme->get_voter_html($event->image);
             $page->add_block(new Block("Image Score", $html, "left", 20));
         }
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("numeric_score_vote")) {
         if (!$user->is_anonymous()) {
             $image_id = int_escape($_POST['image_id']);
             $char = $_POST['vote'];
             $score = 0;
             if ($char == "up") {
                 $score = 1;
             } else {
                 if ($char == "down") {
                     $score = -1;
                 }
             }
             if ($score != 0) {
                 send_event(new NumericScoreSetEvent($image_id, $user, $score));
             }
             $page->set_mode("redirect");
             $page->set_redirect(make_link("post/view/{$image_id}"));
         }
     }
     if ($event instanceof NumericScoreSetEvent) {
         $this->add_vote($event->image_id, $user->id, $event->score);
     }
     if ($event instanceof ImageDeletionEvent) {
         $database->execute("DELETE FROM numeric_score_votes WHERE image_id=?", array($event->image->id));
     }
     if ($event instanceof ParseLinkTemplateEvent) {
         $event->replace('$score', $event->image->numeric_score);
     }
     if ($event instanceof SearchTermParseEvent) {
         $matches = array();
         if (preg_match("/^score(<|<=|=|>=|>)(\\d+)\$/", $event->term, $matches)) {
             $cmp = $matches[1];
             $score = $matches[2];
             $event->add_querylet(new Querylet("numeric_score {$cmp} {$score}"));
         }
         if (preg_match("/^upvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=1)", array($duser->id)));
         }
         if (preg_match("/^downvoted_by=(.*)\$/", $event->term, $matches)) {
             $duser = User::by_name($matches[1]);
             if (is_null($duser)) {
                 throw new SearchTermParseException("Can't find the user named " . html_escape($matches[1]));
             }
             $event->add_querylet(new Querylet("images.id in (SELECT image_id FROM numeric_score_votes WHERE user_id=? AND score=-1)", array($duser->id)));
         }
     }
 }
Example #4
0
 public function receive_event(Event $event)
 {
     global $config, $database, $page, $user;
     if (is_null($this->theme)) {
         $this->theme = get_theme_object($this);
     }
     if ($event instanceof AdminBuildingEvent) {
         $this->theme->display_bulk_rater();
     }
     if ($event instanceof PageRequestEvent && $event->page_matches("admin/bulk_rate")) {
         global $database, $user, $page;
         if (!$user->is_admin()) {
             throw PermissionDeniedException();
         } else {
             $n = 0;
             while (true) {
                 $images = Image::find_images($n, 100, Tag::explode($_POST["query"]));
                 if (count($images) == 0) {
                     break;
                 }
                 foreach ($images as $image) {
                     send_event(new RatingSetEvent($image, $user, $_POST['rating']));
                 }
                 $n += 100;
             }
             #$database->execute("
             #	update images set rating=? where images.id in (
             #		select image_id from image_tags join tags
             #		on image_tags.tag_id = tags.id where tags.tag = ?);
             #	", array($_POST["rating"], $_POST["tag"]));
             $page->set_mode("redirect");
             $page->set_redirect(make_link("admin"));
         }
     }
     if ($event instanceof InitExtEvent) {
         if ($config->get_int("ext_ratings2_version") < 2) {
             $this->install();
         }
         $config->set_default_string("ext_rating_anon_privs", 'squ');
         $config->set_default_string("ext_rating_user_privs", 'sqeu');
         $config->set_default_string("ext_rating_admin_privs", 'sqeu');
     }
     if ($event instanceof RatingSetEvent) {
         $this->set_rating($event->image->id, $event->rating);
     }
     if ($event instanceof ImageInfoBoxBuildingEvent) {
         if ($this->can_rate()) {
             $event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating), 80);
         }
     }
     if ($event instanceof ImageInfoSetEvent) {
         if ($this->can_rate() && isset($_POST["rating"])) {
             send_event(new RatingSetEvent($event->image, $user, $_POST['rating']));
         }
     }
     if ($event instanceof SetupBuildingEvent) {
         $privs = array();
         $privs['Safe Only'] = 's';
         $privs['Safe and Unknown'] = 'su';
         $privs['Safe and Questionable'] = 'sq';
         $privs['Safe, Questionable, Unknown'] = 'squ';
         $privs['All'] = 'sqeu';
         $sb = new SetupBlock("Image Ratings");
         $sb->add_choice_option("ext_rating_anon_privs", $privs, "Anonymous: ");
         $sb->add_choice_option("ext_rating_user_privs", $privs, "<br>Users: ");
         $sb->add_choice_option("ext_rating_admin_privs", $privs, "<br>Admins: ");
         $event->panel->add_block($sb);
     }
     if ($event instanceof ParseLinkTemplateEvent) {
         $event->replace('$rating', $this->theme->rating_to_name($event->image->rating));
     }
     if ($event instanceof SearchTermParseEvent) {
         $matches = array();
         if (is_null($event->term) && $this->no_rating_query($event->context)) {
             $set = Ratings::privs_to_sql(Ratings::get_user_privs($user));
             $event->add_querylet(new Querylet("rating IN ({$set})"));
         }
         if (preg_match("/^rating=([sqeu]+)\$/", $event->term, $matches)) {
             $sqes = $matches[1];
             $arr = array();
             for ($i = 0; $i < strlen($sqes); $i++) {
                 $arr[] = "'" . $sqes[$i] . "'";
             }
             $set = join(', ', $arr);
             $event->add_querylet(new Querylet("rating IN ({$set})"));
         }
         if (preg_match("/^rating=(safe|questionable|explicit|unknown)\$/", strtolower($event->term), $matches)) {
             $text = $matches[1];
             $char = $text[0];
             $event->add_querylet(new Querylet("rating = ?", array($char)));
         }
     }
 }