Example #1
0
 /**
  * Returns the ID of the user with the provided email address. If the user
  * account doesn't exist, it is created. If the user account exists but is
  * inactive, an exception is thrown.
  * 
  * @param string $email
  * @param string|null $name If this is a new user, $name will be used as the
  *                          user's name.
  * @return array
  * @throws Yadda_Model_Exception
  */
 public static function findByEmail($email, $name = null)
 {
     $userDb = Yadda_Db_Table::getInstance('user');
     $user = $userDb->fetchRow(array('email = ?' => $email));
     if ($user !== null) {
         if ($user->status != 'active') {
             throw new Yadda_Model_Exception('Your account is no longer active.');
         }
     } else {
         $userId = $userDb->insert(array('email' => $email, 'name' => $name, 'created' => Yadda_Db::now()));
         $user = $userDb->fetchRow(array('email = ?' => $email));
     }
     return self::toArray($user);
 }
Example #2
0
 /**
  * Returns all images matching the given parameters.
  * 
  * @param array $params
  * @return array
  */
 public static function all(array $params)
 {
     $imageDb = Yadda_Db_Table::getInstance('image');
     $select = $imageDb->select()->from('image')->order('id DESC');
     // check parameters
     if (isset($params['deal'])) {
         $select->where('deal_id = ?', $params['deal']);
     }
     // fetch
     $images = $imageDb->fetchAll($select);
     $return = array();
     foreach ($images as $image) {
         $return[] = self::toArray($image);
     }
     return $return;
 }
Example #3
0
 public function sitemapAction()
 {
     // first get together all the URLs
     $urls = array('/', '/contact', '/map');
     $regionDb = Yadda_Db_Table::getInstance('region');
     $select = $regionDb->select()->from('region', array('id'))->order('name');
     $regions = $regionDb->fetchAll($select);
     foreach ($regions as $region) {
         $urls[] = $this->view->url(array(), 'search') . '?region=' . urlencode($region['id']);
     }
     // generate XML
     $xml = new DOMDocument('1.0', 'utf-8');
     $urlset = $xml->createElement('urlset');
     $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
     $xml->appendChild($urlset);
     foreach ($urls as $url) {
         $loc = $xml->createElement('loc', 'http://' . $_SERVER['HTTP_HOST'] . $url);
         $url = $xml->createElement('url');
         $url->appendChild($loc);
         $urlset->appendChild($url);
     }
     $this->getResponse()->setHeader('Content-Type', 'text/xml');
     $this->getResponse()->setBody($xml->saveXML(null, LIBXML_NOEMPTYTAG));
     $this->getResponse()->sendResponse();
     die;
     /*
     <?xml version="1.0" encoding="UTF-8"?>
     <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
             xmlns:image="http://www.sitemaps.org/schemas/sitemap-image/1.1"
             xmlns:video="http://www.sitemaps.org/schemas/sitemap-video/1.1">
       <url> 
         <loc>http://www.example.com/foo.html</loc> 
         <image:image>
            <image:loc>http://example.com/image.jpg</image:loc> 
         </image:image>
         <video:video>     
           <video:content_loc>http://www.example.com/video123.flv</video:content_loc>
           <video:thumbnail_loc>http://www.example.com/thumbs/123.jpg</video:thumbnail_loc>
           <video:player_loc allow_embed="yes" autoplay="ap=1">http://www.example.com/videoplayer.swf?video=123</video:player_loc>
           <video:title>Grilling steaks for summer</video:title>  
           <video:description>Get perfectly done steaks every time</video:description>
         </video:video>
       </url>
     </urlset>
     */
 }
Example #4
0
 public static function unsubscribe($id, $hash)
 {
     // validate - id
     if (empty($id)) {
         throw new Yadda_Model_Exception('Please provide a subscription ID.');
     }
     $id = (int) $id;
     $subscriptionDb = Yadda_Db_Table::getInstance('subscription');
     $subscription = $subscriptionDb->fetchRow(array('id = ?' => $id, 'status = ?' => 'active'));
     if ($subscription === null) {
         throw new Yadda_Model_Exception('There is no subscription with the provided ID.');
     }
     // validate - hash
     if (empty($hash)) {
         throw new Yadda_Model_Exception('Please provide an authentication hash.');
     }
     $config = Zend_Registry::get('config');
     if ($hash != md5($id . $config->secret)) {
         throw new Yadda_Model_Exception('Please provide a valid authentication hash.');
     }
     // ok all good -- unsub
     $subscriptionDb->update(array('status' => 'deleted', 'modified' => Yadda_Db::now()), array('id = ?' => $id));
 }
Example #5
0
<?php

include_once '../bootstrap.php';
ini_set('memory_limit', '1024M');
set_time_limit(0);
// create logger
$logger = Yadda_Log::getInstance();
// get all feeds that have last_fetch = null or last_fetch more than 30m ago
$feedDb = Yadda_Db_Table::getInstance('feed');
$select = $feedDb->select()->setIntegrityCheck(false)->from('feed')->joinLeft('site', 'feed.site_id = site.id', array())->joinLeft('region', 'feed.region_id = region.id', array('region_lat' => 'lat', 'region_long' => 'long'))->where('feed.engine = ?', 'vuvuplaza')->order('feed.created');
$feeds = $feedDb->fetchAll($select);
foreach ($feeds as $feed) {
    $logger->log('Processing feed for region "' . $feed->region_id . '" from site "' . $feed->site_id . '"', Zend_Log::DEBUG);
    $logger->log('URL: ' . $feed->url, Zend_Log::DEBUG);
    $logger->log('Last fetch: ' . $feed->last_fetch, Zend_Log::DEBUG);
    try {
        Yadda_Model_Feed::import($feed);
        // mark the feed as fetched
        $feedDb->update(array('last_fetch' => Yadda_Db::now(), 'modified' => Yadda_Db::now()), array('id = ?' => $feed->id));
    } catch (Exception $e) {
        $logger->log('Exception: ' . (string) $e, Zend_Log::ERR);
    }
    sleep(rand(0, 10));
}
Example #6
0
 /**
  * Deletes the specified site.
  * 
  * @param string $id
  * @return void
  */
 public static function delete($id)
 {
     $site = self::find($id);
     $siteDb = Yadda_Db_Table::getInstance('site');
     $siteDb->update(array('status' => 'deleted', 'modified' => Yadda_Db::now()), array('id = ?' => $site['id']));
 }
Example #7
0
<?php

include_once '../bootstrap.php';
set_time_limit(0);
$logger = Yadda_Log::getInstance();
// get all images that need to be fetched
$imageDb = Yadda_Db_Table::getInstance('image');
$select = $imageDb->select()->from('image')->where('status = ?', 'new')->where('source LIKE ?', 'http%')->where('created >= ?', date('Y-m-d H:i:s', strtotime('-1 day')));
$images = $imageDb->fetchAll($select);
foreach ($images as $image) {
    // mark the image as 'fetching' -- if the update doesn't return > 0 then
    // either the row has been deleted or it is already being fetched by
    // another process
    $rows = $imageDb->update(array('status' => 'fetching', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id));
    // we need to process this image
    if ($rows > 0) {
        $logger->log('Fetching image #' . $image->id, Zend_Log::INFO);
        $logger->log('Source: ' . $image->source, Zend_log::DEBUG);
        try {
            Yadda_Model_Image::fetch($image);
            // mark the image as 'active'
            $image->getTable()->update(array('status' => 'active', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id));
        } catch (Zend_Exception $e) {
            $logger->log('Exception: ' . (string) $e, Zend_Log::ERR);
            // reset the image to 'new' so that it can be fetched next time
            $image->getTable()->update(array('status' => 'new', 'modified' => Yadda_Db::now()), array('id = ?' => $image->id));
        }
        sleep(rand(0, 10));
    }
}
Example #8
0
 /**
  * Retrieves the $count latest featured deals.
  * 
  * @param int $count
  * @return array
  */
 public static function featured($count)
 {
     $count = max(1, (int) $count);
     $dealDb = Yadda_Db_Table::getInstance('deal');
     $select = $dealDb->select()->setIntegrityCheck(false)->from('deal')->joinLeft('region', 'deal.region_id = region.id', array('region_name' => 'name'))->joinLeft('site', 'deal.site_id = site.id', array('site_name' => 'name'))->joinLeft('feature', 'deal.id = feature.deal_id', array())->where('deal.status = ?', 'active')->where('region.status = ?', 'active')->where('site.status = ?', 'active')->where('feature.deal_id IS NOT NULL')->order('feature.created DESC')->limit($count);
     $deals = $dealDb->fetchAll($select);
     $return = array();
     foreach ($deals as $deal) {
         $return[] = self::toArray($deal);
     }
     return $return;
 }
Example #9
0
<?php

include_once '../bootstrap.php';
set_time_limit(0);
$logger = Yadda_Log::getInstance();
// get base mailer URL
$config = Zend_Registry::get('config');
$router = new Www_Controller_Router();
$mailerUrl = 'http://' . $config->domain->www . $router->assemble(array(), 'mailer');
$unsubscribeUrl = 'http://' . $config->domain->www . $router->assemble(array(), 'unsubscribe');
// determine which subscriptions need to be sent
$hour = (int) gmdate('H');
$subscriptionDb = Yadda_Db_Table::getInstance('subscription');
$select = $subscriptionDb->select()->setIntegrityCheck(false)->from('subscription')->joinLeft('user', 'subscription.user_id = user.id', array('user_email' => 'email'))->where('subscription.status = ?', 'active')->where('user.status = ?', 'active')->where('subscription.hour = ?', $hour)->order('id');
$subscriptions = $subscriptionDb->fetchAll($select);
$since = gmdate('Y-m-d\\TH:00:00\\Z', strtotime('-24 hour'));
foreach ($subscriptions as $subscription) {
    $logger->log('Subscription #' . $subscription->id, Zend_Log::DEBUG);
    // run the search
    $params = array('query' => $subscription->query, 'region' => $subscription->region_id, 'price' => $subscription->price, 'since' => $since, 'count' => 10);
    try {
        $result = Yadda_Model_Deal::search($params);
    } catch (Yadda_Model_Exception $e) {
        $logger->log('Error while searching: ' . $e->getMessage(), Zend_Log::ERR);
        $logger->log('Skipping...', Zend_Log::ERR);
        continue;
    }
    $total = $result['total'];
    $logger->log('Found ' . $total . ' result(s)', Zend_Log::DEBUG);
    if ($total == 0) {
        continue;
Example #10
0
 /**
  * Imports the deals from a feed.
  * 
  * @param Zend_Db_Table_Row $feed
  * @throws Yadda_Model_Exception
  * @throws Yadda_Feed_Exception
  * @return void
  */
 public static function import($feed)
 {
     if (!isset(self::$engines[$feed->engine])) {
         throw new Yadda_Model_Exception('Unknown feed type "' . $feed->engine . '"');
     }
     $class = 'Yadda_Feed_Engine_' . ucfirst(strtolower($feed->engine));
     $engine = new $class();
     $stubs = $engine->import($feed);
     $logger = Yadda_Log::getInstance();
     $dealDb = Yadda_Db_Table::getInstance('deal');
     foreach ($stubs as $stub) {
         ob_start();
         print_r($stub);
         $logger->log('Stub:' . "\n" . ob_get_clean(), Zend_Log::DEBUG);
         // make sure we have the bare minimum
         if ($stub->getGuid() === null || $stub->getTitle() === null) {
             $logger->log('Rejected: Stub doesn\'t have GUID or title.', Zend_Log::DEBUG);
             continue;
         }
         // do we already have this guid for this site?
         $deal = $dealDb->fetchRow(array('site_id = ?' => $feed->site_id, 'guid = ?' => $stub->getGuid()));
         if ($deal !== null) {
             continue;
         }
         // do we already have an item with this title for this site today?
         $deal = $dealDb->fetchRow(array('site_id = ?' => $feed->site_id, 'title = ?' => $stub->getTitle(), 'DATE_FORMAT(display_date, \'%Y%m%d\') = ?' => date('Ymd', strtotime($stub->getDate()))));
         if ($deal !== null) {
             continue;
         }
         $logger->log('New entry: ' . $stub->getGuid(), Zend_Log::INFO);
         // determine geo coords
         $geo = $stub->getGeo();
         if ($geo === null && $feed->region_lat !== null && $feed->region_long !== null) {
             // get deal title
             $title = $stub->getTitle();
             $title = utf8_decode($title);
             $matches = array();
             preg_match_all('#(at|from)\\s+((([A-Z][\\w\']+)\\s*)+)#', $title, $matches);
             foreach ($matches[2] as $match) {
                 $logger->log('Testing "' . $match . '" for coords', Zend_Log::DEBUG);
                 $match = trim($match);
                 // remove prices/stopwords/etc.
                 if (preg_match('/^R\\d+$/', $match)) {
                     continue;
                 }
                 if ($match == 'On') {
                     continue;
                 }
                 // get coordinates
                 $test = Yadda_Model_Deal::getGeoFromPlaceName(array((double) $feed->region_lat, (double) $feed->region_long), $match);
                 if ($test !== null) {
                     $logger->log('Found geo coords: ' . join(' ', $test), Zend_Log::INFO);
                     $geo = $test;
                     break;
                 }
             }
         }
         // insert the new deal
         $dealDb->getAdapter()->beginTransaction();
         try {
             $insert = array('site_id' => $feed->site_id, 'region_id' => $feed->region_id, 'guid' => $stub->getGuid(), 'title' => $stub->getTitle(), 'description' => $stub->getDescription(), 'link' => $stub->getLink(), 'display_date' => date('Y-m-d H:i:s', strtotime($stub->getDate())), 'price' => $stub->getPrice(), 'value' => $stub->getValue(), 'discount' => $stub->getDiscount(), 'lat' => $geo !== null ? $geo[0] : null, 'long' => $geo !== null ? $geo[1] : null, 'status' => 'active', 'created' => Yadda_Db::now());
             $dealId = $dealDb->insert($insert);
             // check for images
             foreach ($stub->getImages() as $image) {
                 $logger->log('New image: ' . $image, Zend_Log::INFO);
                 $imageId = $dealDb->getAdapter()->insert('image', array('deal_id' => $dealId, 'source' => $image, 'created' => Yadda_Db::now()));
             }
             $dealDb->getAdapter()->commit();
         } catch (Zend_Exception $e) {
             $dealDb->getAdapter()->rollBack();
             throw new Yadda_Feed_Exception('Error importing deal: ' . $e->getMessage());
         }
     }
 }