function lookup_consumer($consumer_key)
 {
     $con = Consumer::staticGet('consumer_key', $consumer_key);
     if (!$con) {
         $con = new Consumer();
         $con->consumer_key = $consumer_key;
         $con->seed = common_good_rand(16);
         $con->created = DB_DataObject_Cast::dateTime();
         if (!$con->insert()) {
             return null;
         }
     }
     return new OAuthConsumer($con->consumer_key, '');
 }
 public function authenticate()
 {
     $db = Zend_Registry::get('db');
     $rs = $db->fetchAll("SELECT id  FROM consumer WHERE weiboid=:weiboid and state='ACTIVE'", array('weiboid' => $this->_weiboid));
     if (count($rs) > 0) {
         return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $this->_weiboid);
     } else {
         if (isset($this->_weiboid) && '' != $this->_weiboid) {
             $consumerModel = new Consumer();
             $consumerModel->insert(array('name' => '微博用户', 'weiboid' => $this->_weiboid, 'state' => 'ACTIVE'));
         }
         return new Zend_Auth_Result(Zend_Auth_Result::FAILURE, $this->_weiboid);
     }
 }
 function lookup_consumer($consumerKey)
 {
     $con = Consumer::getKV('consumer_key', $consumerKey);
     if (!$con instanceof Consumer) {
         // Create an anon consumer and anon application if one
         // doesn't exist already
         if ($consumerKey == 'anonymous') {
             common_debug("API OAuth - creating anonymous consumer");
             $con = new Consumer();
             $con->consumer_key = $consumerKey;
             $con->consumer_secret = $consumerKey;
             $con->created = common_sql_now();
             $result = $con->insert();
             if (!$result) {
                 // TRANS: Server error displayed when trying to create an anynymous OAuth consumer.
                 $this->serverError(_('Could not create anonymous consumer.'));
             }
             $app = Oauth_application::getByConsumerKey('anonymous');
             if (!$app) {
                 common_debug("API OAuth - creating anonymous application");
                 $app = new OAuth_application();
                 $app->owner = 1;
                 // XXX: What to do here?
                 $app->consumer_key = $con->consumer_key;
                 $app->name = 'anonymous';
                 $app->icon = 'default-avatar-stream.png';
                 // XXX: Fix this!
                 $app->description = "An anonymous application";
                 // XXX: allow the user to set the access type when
                 // authorizing? Currently we default to r+w for anonymous
                 // OAuth client applications
                 $app->access_type = 3;
                 // read + write
                 $app->type = 2;
                 // desktop
                 $app->created = common_sql_now();
                 $id = $app->insert();
                 if (!$id) {
                     // TRANS: Server error displayed when trying to create an anynymous OAuth application.
                     $this->serverError(_("Could not create anonymous OAuth application."));
                 }
             }
         } else {
             return null;
         }
     }
     return new OAuthConsumer($con->consumer_key, $con->consumer_secret);
 }