Beispiel #1
2
 public function fetch($delete = false)
 {
     $oImap = imap_open('{' . $this->mail_server . ':993/imap/ssl/notls/novalidate-cert}', $this->username, $this->password);
     $oMailboxStatus = imap_check($oImap);
     $aMessages = imap_fetch_overview($oImap, "1:{$oMailboxStatus->Nmsgs}");
     $validMessages = array();
     foreach ($aMessages as $oMessage) {
         print "Trying message '" . $oMessage->subject . "'";
         $fileContent = $fileType = '';
         $geocoder = factory::create('geocoder');
         $postCode = $geocoder->extract_postcode($oMessage->subject);
         $fromName = null;
         $fromEmail = null;
         if (strpos($oMessage->from, '<')) {
             $split = split('<', $oMessage->from);
             //name - make sure name not an email address
             $fromName = trim($split[0]);
             if (valid_email($fromName)) {
                 $fromName = null;
             }
             //email
             $fromEmail = trim(str_replace('>', '', $split[1]));
         } else {
             $fromEmail = $oMessage->from;
         }
         $images = array();
         $messageStructure = imap_fetchstructure($oImap, $oMessage->msgno);
         if (isset($messageStructure->parts)) {
             $partNumber = 0;
             foreach ($messageStructure->parts as $oPart) {
                 $partNumber++;
                 if ($oPart->subtype == 'PLAIN' && !$postCode) {
                     $messageContent = imap_fetchbody($oImap, $oMessage->msgno, $partNumber);
                     if ($oPart->encoding == 4) {
                         $messageContent = quoted_printable_decode($messageContent);
                     }
                     $postCode = geocoder::extract_postcode($messageContent);
                 } elseif ($oPart->encoding == 3 && in_array($oPart->subtype, array('JPEG', 'PNG'))) {
                     $oImage = null;
                     $encodedBody = imap_fetchbody($oImap, $oMessage->msgno, $partNumber);
                     $fileContent = base64_decode($encodedBody);
                     $oImage = imagecreatefromstring($fileContent);
                     if (imagesx($oImage) > $this->min_import_size && imagesy($oImage) > $this->min_import_size) {
                         array_push($images, $oImage);
                     }
                     $fileType = strtolower($oPart->subtype);
                 }
             }
         }
         //add to the messages array
         array_push($validMessages, array('postcode' => $postCode, 'images' => $images, 'file_type' => $fileType, 'from_address' => $fromAddress, 'from_email' => $fromEmail, 'from_name' => $fromName));
         if ($delete) {
             imap_delete($oImap, $oMessage->msgno);
         }
     }
     imap_close($oImap, CL_EXPUNGE);
     $this->messages = $validMessages;
 }
 public static function example_postcode()
 {
     $election_id = get_election_id($election_id);
     $search = factory::create('search');
     $result = $search->search("australian_postcode", array(array('election_id', '=', $election_id)), null, null, null, 1);
     return $result[0]->postcode;
 }
 private function lookup_constituency($postcode)
 {
     $cache = cache::factory();
     $cached = $cache->get('twfy' . $postcode);
     if (isset($cached) && $cached !== false && $cached != '') {
         return $cached;
     } else {
         if (COUNTRY_CODE_TLD == "uk") {
             $twfy = factory::create('twfy');
             $twfy_constituency = $twfy->query('getConstituency', array('output' => 'php', 'postcode' => $postcode, 'future' => 'yes_please'));
             $twfy_constituency = unserialize($twfy_constituency);
             $success = $cache->set('twfy' . $postcode, $twfy_constituency);
             $twfy_constituency = array($twfy_constituency["name"]);
         } else {
             if (COUNTRY_CODE_TLD == "au") {
                 $australian_postcode = factory::create("australian_postcode");
                 return $australian_postcode->lookup_constituency_names($postcode);
             } else {
                 $success = false;
             }
         }
         if ($success && isset($twfy_constituency) && $twfy_constituency != '' && $twfy_constituency != false) {
             return $twfy_constituency;
         } else {
             return false;
         }
     }
 }
function get_leaflets($email_alert)
{
    if (ALERT_DEBUG > 0) {
        print $email_alert->type . "\n";
        print "frequency : " . $email_alert->frequency_hours . "\n";
    }
    $results = array();
    $search = factory::create('search');
    $time = time() - 60 * 60 * $email_alert->frequency_hours;
    $time = mysql_date($time);
    //do we have any matching leaflets?
    if ($email_alert->type == 'attack') {
        $results = $search->search('leaflet', array(array('leaflet_party_attack.party_id', '=', $email_alert->parent_id), array('leaflet.date_uploaded', '>=', $time), array('leaflet.live', '=', 1)), 'AND', array(array('leaflet_party_attack', 'inner')));
    } else {
        if ($email_alert->type == 'party') {
            $results = $search->search('leaflet', array(array('leaflet.publisher_party_id', '=', $email_alert->parent_id), array('leaflet.date_uploaded', '>=', $time), array('leaflet.live', '=', 1)));
        } else {
            if ($email_alert->type == 'constituency') {
                $results = $search->search('leaflet', array(array('leaflet_constituency.constituency_id', '=', $email_alert->parent_id), array('leaflet.date_uploaded', '>=', $time), array('leaflet.live', '=', 1)), 'AND', array(array('leaflet_constituency', 'inner')));
            } else {
                if ($email_alert->type == 'category') {
                    $results = $search->search('leaflet', array(array('leaflet_category.category_id', '=', $email_alert->parent_id), array('leaflet.date_uploaded', '>=', $time), array('leaflet.live', '=', 1)), 'AND', array(array('leaflet_category', 'inner')));
                }
            }
        }
    }
    return $results;
}
Beispiel #5
0
 function bind()
 {
     //get the leaflet
     $search = factory::create("search");
     $result = $search->search("leaflet", array(array("leaflet_id", "=", $this->leaflet_id), array('live', '=', 1)), 'AND', array(array("party", "inner")));
     $leaflet = null;
     if (count($result) != 1) {
         throw_404();
     } else {
         $leaflet = $result[0];
     }
     //get images
     $leaflet_images = $search->search("leaflet_image", array(array("leaflet_id", "=", $this->leaflet_id)), 'AND', null, array(array("sequence", "ASC")));
     //get categories
     $leaflet_categories = $search->search("category", array(array("leaflet_category.leaflet_id", "=", $this->leaflet_id)), 'AND', array(array("leaflet_category", 'inner')));
     //get tags
     $leaflet_tags = $search->search("tag", array(array("leaflet_tag.leaflet_id", "=", $this->leaflet_id)), 'AND', array(array("leaflet_tag", 'inner')));
     //get parties attacked
     $leaflet_parties_attacked = $search->search("party", array(array("leaflet_party_attack.leaflet_id", "=", $this->leaflet_id)), 'AND', array(array("leaflet_party_attack", 'inner')));
     //get constituencies
     $leaflet_constituencies = $search->search("constituency", array(array("leaflet_constituency.leaflet_id", "=", $this->leaflet_id)), 'AND', array(array("leaflet_constituency", 'inner')));
     //js
     $this->onloadscript = "showMap('" . MAP_PROVIDER . "', " . number_format($leaflet->lng, 2) . ", " . number_format($leaflet->lat, 2) . ");";
     //assign
     $this->page_title = $leaflet->title . " (an election leaflet published by " . $leaflet->party_name . ")";
     $this->has_map = true;
     $this->assign("leaflet", $leaflet);
     $this->assign("leaflet_images", $leaflet_images);
     $this->assign("leaflet_categories", $leaflet_categories);
     $this->assign("leaflet_tags", $leaflet_tags);
     $this->assign("constituency", $leaflet_constituencies[0]);
     $this->assign("leaflet_parties_attacked", $leaflet_parties_attacked);
 }
Beispiel #6
0
 function bind()
 {
     $this->page_title = "Election leaflets by category";
     $search = factory::create('search');
     //get top parties
     $categories = $search->search_cached("category", array(array("1", "=", "1")), "AND", null, array(array("name", "ASC")));
     $this->assign("categories", $categories);
 }
 private function set_application()
 {
     $application = session_read('application');
     if (!isset($application)) {
         $application = factory::create('application');
     }
     $this->application = $application;
 }
Beispiel #8
0
 function bind()
 {
     $this->page_title = "Election leaflets by category";
     $search = factory::create('search');
     $election_id = get_election_id();
     // Get categories
     $categories = $search->search_cached("category", array(array("category_election.election_id", "=", $election_id)), 'AND', array(array("category_election", "inner")), array(array('name', "ASC")));
     $this->assign("categories", $categories);
 }
Beispiel #9
0
 function bind()
 {
     $this->page_title = "Election leaflets by party";
     $search = factory::create('search');
     //get top parties
     $election_id = get_election_id();
     $parties = $search->search_cached("party", array(array("party_election.election_id", "=", $election_id)), "AND", array(array("party_election", "inner")), array(array("name", "ASC")));
     $this->assign("parties", $parties);
 }
Beispiel #10
0
 function bind()
 {
     $this->page_title = "Election leaflets by tag";
     $search = factory::create('search');
     $tag = factory::create("tag");
     $tag_count = 250;
     $weighted_tags = $tag->get_weighted_tags($tag_count);
     $this->assign("weighted_tags", $weighted_tags);
 }
function get_constituency_type()
{
    $return = false;
    $search = factory::create('search');
    $results = $search->search('constituency_type', array(array("name", "=", 'UK Parliament Constituency')));
    if (count($results) == 1) {
        $return = $results[0];
    }
    return $return;
}
 public static function lookup_constituency_names($postcode)
 {
     $search = factory::create('search');
     $constituencies = $search->search("australian_postcode", array(array('postcode', '=', $postcode)), 'AND', null, array(array('constituency', "ASC")));
     $result = array();
     foreach ($constituencies as $c) {
         $result[] = $c->constituency;
     }
     return $result;
 }
 function process()
 {
     // Start transaction
     $db = new DB_DataObject();
     $db->query('BEGIN');
     // Get existing constituencies
     $search = factory::create('search');
     $results = $search->search("constituency", array(array("election_id", "=", $this->election_id)), 'AND', array(array("constituency_election", "inner")), array(array('name', "ASC")));
     // Delete existing constituencies
     foreach ($results as $result) {
         if (!$result->delete()) {
             $db->query('ROLLBACK');
             die("Unable to delete constituency. Transaction rolled back.");
         }
     }
     // Delete many-to-many joins
     $query_string = 'DELETE FROM `constituency_election`
                      WHERE `election_id`=' . $this->election_id;
     if ($db->query($query_string) === false) {
         $db->query('ROLLBACK');
         die("Unable to delete constituency links. Transaction rolled back.");
     }
     // Add user supplied constituencies
     $supplied_constituencies = explode("\n", $this->data['txtConstituencies']);
     foreach ($supplied_constituencies as $constituency_name) {
         $constituency_name = trim($constituency_name);
         if ($constituency_name == '') {
             continue;
         }
         // Check for an existing constituency (from another election)
         $existing_constituency = $search->search("constituency", array(array("name", "=", $constituency_name)));
         if (count($existing_constituency) == 1) {
             $constituency = $existing_constituency[0];
         } else {
             // Create constituency
             $constituency = factory::create('constituency');
             $constituency->name = $constituency_name;
             if (!$constituency->insert()) {
                 $db->query('ROLLBACK');
                 die("Unable to add constituency. Transaction rolled back.");
             }
         }
         // Create join
         $constituency_election = factory::create('constituency_election');
         $constituency_election->constituency_id = $constituency->constituency_id;
         $constituency_election->election_id = $this->election_id;
         if (!$constituency_election->insert()) {
             $db->query('ROLLBACK');
             die("Unable to add constituency. Transaction rolled back.");
         }
     }
     $db->query('COMMIT');
     $this->bind();
     $this->render();
 }
Beispiel #14
0
 function bind()
 {
     $this->page_title = "Election leaflets stats by party";
     $this->assign("date_since", $this->date_since);
     $search = factory::create('search');
     $parties = $search->search("party", array(array("major", "=", 1)));
     $party_rates = array();
     foreach ($parties as $party) {
         $this->assign($party->url_id . "_party_rates", $party->get_rate_values($this->date_since));
     }
 }
 function process()
 {
     // TODO: Duplicate detection
     if ($this->data['txtCategoryName']) {
         $category = factory::create('category');
         $category->name = $this->data['txtCategoryName'];
         if (!$category->insert()) {
             trigger_error("Unable to save category");
         }
     }
     $this->bind();
     $this->render();
 }
Beispiel #16
0
 public function insert()
 {
     $return = false;
     $search = factory::create('search');
     $result = $search->search("tag", array(array("tag", "=", $this->tag)));
     if (count($result) > 0) {
         $this->tag = $result[0]->tag;
         $this->tag_id = $result[0]->tag_id;
         $return = true;
     } else {
         $return = parent::insert();
     }
     return $return;
 }
Beispiel #17
0
 private function get_leaflet_count()
 {
     $return = 0;
     $cache = cache::factory();
     $cached = $cache->get("total_leaflet_count");
     if ($cached !== false && isset($cached)) {
         $return = $cached;
     } else {
         $leaflet = factory::create('leaflet');
         $return = $leaflet->count();
         $cache->set("total_leaflet_count", $return);
     }
     return $return;
 }
 function bind()
 {
     //get the leaflet
     $search = factory::create("search");
     $result = $search->search("leaflet", array(array("leaflet_id", "=", $this->viewstate['leaflet_id'])));
     $leaflet = null;
     if (count($result) != 1) {
         throw_404();
     } else {
         $leaflet = $result[0];
     }
     $this->assign("leaflet", $leaflet);
     $this->assign("message_sent", $this->viewstate['message_sent']);
 }
Beispiel #19
0
 function load()
 {
     $upload_key = get_http_var('q');
     if (!isset($upload_key) || $upload_key == null) {
         throw_404();
     } else {
         $search = factory::create('search');
         $image_que_items = $search->search("image_que", array(array("upload_key", "=", $upload_key)), "AND", null, array(array("uploaded_date", "ASC")));
         if (count($image_que_items) > 0) {
             redirect(WWW_SERVER . '/addinfo.php?key=' . urlencode($upload_key));
         } else {
             throw_404();
         }
     }
 }
Beispiel #20
0
 function setup()
 {
     $this->confirm_id = get_http_var('q');
     if (!isset($this->confirm_id)) {
         throw_404();
     } else {
         //make sure ID exists
         $search = factory::create('search');
         $results = $search->search("email_alert", array(array("confirm_id", "=", $this->confirm_id), array("activated", '=', 0)));
         if (count($results) != 1) {
             throw_404();
         } else {
             $this->email_alert = $results[0];
         }
     }
 }
Beispiel #21
0
 function unbind()
 {
     //get images
     foreach ($_FILES as $key => $value) {
         $upload_key = $this->viewstate['upload_key'];
         if (isset($_FILES[$key]) && $_FILES[$key]['name'] != '' && isset($upload_key) && $upload_key != '') {
             $temp_file = $this->upload_image($key);
             if ($temp_file !== false) {
                 $image_que = factory::create('image_que');
                 $image_que->upload_key = $upload_key;
                 $image_que->save_image($temp_file);
                 $image_que->insert();
             }
         }
     }
 }
Beispiel #22
0
 public function get_category_count($date_since, $limit = 10, $cache = true)
 {
     $return = array();
     $category = factory::create('category');
     $sql = "SELECT COUNT(leaflet_category.leaflet_category_id) as count, category.name, category.category_id FROM category ";
     $sql .= "INNER JOIN leaflet_category ON category.category_id = leaflet_category.category_id ";
     $sql .= "INNER JOIN leaflet ON leaflet_category.leaflet_id = leaflet.leaflet_id ";
     $sql .= "WHERE date_delivered > '{$date_since}' AND leaflet.live=1 ";
     $sql .= "GROUP BY category.name, category.category_id ORDER BY COUNT(leaflet_category.leaflet_category_id) DESC LIMIT " . $limit;
     if ($cache) {
         $return = $category->execute_cached($sql);
     } else {
         $return = $category->execute($sql);
     }
     return $return;
 }
Beispiel #23
0
 function bind()
 {
     $this->page_title = "Browse election leaflets";
     //get top parties
     $parties_counts = tableclass_party::get_party_count(STAT_ZERO_DATE);
     $this->assign("parties_counts", $parties_counts);
     //get categories
     $categories_counts = tableclass_category::get_category_count(STAT_ZERO_DATE);
     $this->assign("categories_counts", $categories_counts);
     //get electorates
     $constituencies_counts = tableclass_constituency::get_constituency_count(STAT_ZERO_DATE);
     $this->assign("constituencies_counts", $constituencies_counts);
     //get top tags
     $tag = factory::create("tag");
     $weighted_tags = $tag->get_weighted_tags();
     $this->assign("weighted_tags", $weighted_tags);
 }
Beispiel #24
0
 public static function user_count($email, $cache = true)
 {
     $return = 0;
     $rate_value = factory::create('rate_value');
     $sql = "select count(distinct leaflet_id) as count from rate_value where user_email = '" . $rate_value->escape($email) . "'";
     if ($cache) {
         $return = $rate_value->execute_cached($sql);
     } else {
         $return = $rate_value->execute($sql);
     }
     if (count($return) == 1) {
         $return = $return[0]->count;
     } else {
         $return = 0;
     }
     return $return;
 }
Beispiel #25
0
 function process()
 {
     if ($this->validate()) {
         $election = factory::create('election');
         $election->name = trim($this->data['txtName']);
         $election->vote_date = DB_DataObject_Cast::date($this->data['txtDate']);
         if ($election->insert()) {
             $this->bind();
             $this->render();
         } else {
             trigger_error("Unable to save election");
         }
     } else {
         $this->bind();
         $this->render();
     }
 }
 public static function get_constituency_count($date_since, $limit = 10, $cache = true)
 {
     $return = array();
     $constituency = factory::create('constituency');
     $sql = "SELECT COUNT(leaflet_constituency.leaflet_constituency_id) AS count, constituency.name, constituency.url_id, constituency.constituency_id FROM constituency ";
     $sql .= "INNER JOIN leaflet_constituency ON constituency.constituency_id = leaflet_constituency.constituency_id ";
     $sql .= "INNER JOIN leaflet ON leaflet_constituency.leaflet_id = leaflet.leaflet_id ";
     $sql .= "WHERE date_delivered > '{$date_since}' AND leaflet.live=1 ";
     $sql .= "GROUP By constituency.name, constituency.constituency_id ";
     $sql .= "ORDER BY count(leaflet_constituency.leaflet_constituency_id) desc limit " . $limit;
     if ($cache) {
         $return = $constituency->execute_cached($sql);
     } else {
         $return = $constituency->execute($sql);
     }
     return $return;
 }
Beispiel #27
0
 function load()
 {
     $this->confirm_id = get_http_var('q');
     if (!isset($this->confirm_id)) {
         throw_404();
     } else {
         if ($this->viewstate['state'] != 'deleted') {
             //make sure ID exists
             $search = factory::create('search');
             $results = $search->search("email_alert", array(array("confirm_id", "=", $this->confirm_id)));
             if (count($results) != 1) {
                 throw_404();
             } else {
                 $this->email_alert = $results[0];
             }
         }
     }
 }
 function bind()
 {
     //get the leaflet
     $search = factory::create("search");
     $result = $search->search("leaflet", array(array("leaflet_id", "=", $this->leaflet_id)), 'AND', array(array("party", "inner")));
     $leaflet = null;
     if (count($result) != 1) {
         throw_404();
     } else {
         $leaflet = $result[0];
     }
     //get images
     $leaflet_images = $search->search("leaflet_image", array(array("leaflet_id", "=", $this->leaflet_id)), 'AND', null, array(array("sequence", "ASC")));
     //assign
     $this->page_title = "Full image of " . $leaflet->title . " ( election leaflet published by " . $leaflet->party_name . ")";
     $this->assign("leaflet", $leaflet);
     $this->assign("leaflet_images", $leaflet_images);
 }
function lookup_constituency($postcode)
{
    $cache = cache::factory();
    $cached = $cache->get('twfy' . $postcode);
    if (isset($cached) && $cached !== false && $cached != '') {
        return $cached;
    } else {
        $twfy = factory::create('twfy');
        $twfy_constituency = $twfy->query('getConstituency', array('output' => 'php', 'postcode' => $postcode, 'future' => 'yes_please'));
        $twfy_constituency = unserialize($twfy_constituency);
        $success = $cache->set('twfy' . $postcode, $twfy_constituency);
        if ($success && isset($twfy_constituency) && $twfy_constituency != '' && $twfy_constituency != false) {
            return $twfy_constituency;
        } else {
            return false;
        }
    }
}
Beispiel #30
0
 function process()
 {
     //get rate types
     $search = factory::create("search");
     $rate_types = $search->search("rate_type", array(array("1", "=", "1")));
     $rate_values = array();
     //get leaflet_id
     $leaflet_id = $this->data['hidLeafletId'];
     if (isset($leaflet_id) && $leaflet_id > 0 && $leaflet_id != '') {
         $name = session_read('analysis_name');
         $email = session_read('analysis_email');
         //loop through types
         foreach ($rate_types as $rate_type) {
             if (isset($this->data['hidRateValue_' . $rate_type->rate_type_id])) {
                 $rate_value = factory::create('rate_value');
                 $rate_value->rate_type_id = $rate_type->rate_type_id;
                 $rate_value->leaflet_id = $leaflet_id;
                 $rate_value->value = $this->data['hidRateValue_' . $rate_type->rate_type_id];
                 $rate_value->user_name = $name;
                 $rate_value->user_email = $email;
                 array_push($rate_values, $rate_value);
             } else {
                 trigger_error("Unknown rate type on rate page");
             }
         }
     } else {
         trigger_error("No leaflet ID set on rate page");
     }
     //save rate values
     foreach ($rate_values as $rate_value) {
         $rate_value->insert();
     }
     //anything interesting?
     $interesting = trim($this->data['txtInteresting']);
     if (isset($interesting) && $interesting != '' && strlen($interesting) > 30) {
         $rate_interesting = factory::create('rate_interesting');
         $rate_interesting->leaflet_id = $leaflet_id;
         $rate_interesting->description = $interesting;
         $rate_interesting->user_name = $name;
         $rate_interesting->user_email = $email;
         $rate_interesting->insert();
     }
     redirect(WWW_SERVER . "/rate.php");
 }