public function lookupConsumer($key, $secret)
 {
     try {
         $api = new DTAPI($this->db->filter(array("consumer_key" => $key)));
         if ($api["status"] == 0) {
             return OAUTH_CONSUMER_KEY_REFUSED;
         }
         return $api;
     } catch (Exception $e) {
         DTLog::debug($e->getMessage());
     }
     return null;
 }
 /** performs standard authentication, authorizing the relevant token if necessary */
 public function actionAuthenticate()
 {
     $u = parent::actionAuthenticate();
     if (isset($u)) {
         try {
             //create oauth token
             $token = DTOAuthToken::upsert($this->db->qb()->fail(), array("type" => 0, "status" => 1, "user_id" => $u["id"]));
             $redirect_uri = $this->params->stringParam("redirect_uri");
             $state = $this->params->stringParam("state");
             $url = $this->appendParams($redirect_uri, array("code" => $token["token"], "state" => $state));
             DTLog::debug($url);
             header('HTTP/1.1 278 Client Redirect', true, 278);
             return array("location" => $url);
         } catch (Exception $e) {
             DTLog::Debug("auth error: " . $e->getMessage());
         }
         //the user failed to authenticate (maybe bad user/pass)
     }
     return $u;
 }
 public function processEach($proc_f)
 {
     $row_i = 0;
     $this->db->begin();
     while (($row = $this->getRow($row_i)) !== false) {
         /* it's important that we iterate the mapping to preserve key order */
         $mapping = $this->mapping;
         $headers = $this->headers;
         $params = new DTParams(array_reduce(array_keys($mapping), function ($out, $k) use($mapping, $headers, $row) {
             $i = $headers[$k];
             $out[$mapping[$k]] = isset($row[$i]) ? $row[$i] : null;
             return $out;
         }, array()));
         $mapped = $params->allParams();
         try {
             $proc_f($mapped, $params);
         } catch (Exception $e) {
             DTLog::debug("Failed to ingest (%s):\n%s", $e->getMessage(), $mapped);
         }
         $row_i++;
         if ($row_i % $this->transaction_size == 0) {
             if ($this->dry_run) {
                 $this->db->rollback();
             } else {
                 $this->db->commit();
             }
             $this->db->begin();
             DTLog::debug("* ({$row_i})");
         }
     }
     if ($this->dry_run) {
         $this->db->rollback();
     } else {
         $this->db->commit();
     }
 }
 public function setCreators($vals)
 {
     $authors = array();
     $editors = array();
     foreach ($vals as $i => $c) {
         $params = array();
         $params["name_last"] = isset($c["name"]) ? $c["name"] : "";
         //start with 'name' (e.g. institutes)
         $params["name_first"] = isset($c["firstName"]) ? $c["firstName"] : "";
         if (isset($c["lastName"])) {
             $params["name_last"] = $c["lastName"];
         }
         //override if we have it
         switch ($c["creatorType"]) {
             case "editor":
             case "seriesEditor":
                 $model = static::modelFor("editors");
                 $obj = $model::bestMatch($this->db->filter(), $params);
                 array_push($editors, $obj["id"]);
                 break;
             default:
                 $model = static::modelFor("authors");
                 $obj = $model::bestMatch($this->db->filter(), $params);
                 array_push($authors, $obj["id"]);
         }
     }
     $this->authors = $this->setMany("authors", array_unique($authors));
     $this->editors = $this->setMany("editors", array_unique($editors));
     $manifest = $this->hasManyManifest();
     $model = $manifest["authors"][0];
     foreach ($this->authors as $i => $a) {
         try {
             $pa = $model::upsert($this->db->filter(array("publication_id" => $this["id"], "author_id" => $a["id"])), array("publication_id" => $this["id"], "author_id" => $a["id"], "position" => $i));
         } catch (Exception $e) {
             DTLog::debug($e->getMessage());
         }
     }
     $model = $manifest["editors"][0];
     foreach ($this->editors as $i => $e) {
         try {
             $pe = $model::upsert($this->db->filter(array("publication_id" => $this["id"], "editor_id" => $e["id"])), array("publication_id" => $this["id"], "editor_id" => $e["id"], "position" => $i));
         } catch (Exception $e) {
             DTLog::debug($e->getMessage());
         }
     }
 }
 /**
  * Handle commit hook post from bitbucket or github
  * @return string
  */
 public function commit_hook()
 {
     if (isset($_POST['payload'])) {
         // github and bitbucket use a 'payload' parameter
         $json = $_POST['payload'];
     } else {
         $json = file_get_contents('php://input');
     }
     if (!$json) {
         DTLog::debug('ignored #1');
         return 'ignored';
     }
     $data = $json ? json_decode($json, true) : null;
     if (!$data || !is_array($data['commits'])) {
         DTLog::debug('ignored #2');
         return 'ignored';
     }
     // look through the commits
     $found = false;
     $tags = array();
     foreach ($data['commits'] as $commit) {
         if (preg_match('/\\[deploy(:.+)?\\]/', $commit['message'], $matches)) {
             $found = true;
             if (count($matches) > 1 && $matches[1] != '') {
                 $tags[] = substr($matches[1], 1);
             } else {
                 $tags[] = 'live';
             }
         }
     }
     if (!$found) {
         return 'ignored';
     }
     if (defined('DEPLOY_TAG') && !in_array(DEPLOY_TAG, $tags)) {
         DTLog::debug('ignored #3');
         return 'ignored';
     }
     // create the deployment
     increase_time_limit_to(600);
     $deploy = new Deploy(BASE_PATH, array());
     $deploy->post_deploy = function () use($deploy) {
         global $_FILE_TO_URL_MAPPING;
         // composer install if detected
         if (file_exists(BASE_PATH . DIRECTORY_SEPARATOR . 'composer.json')) {
             if (file_exists('/usr/local/bin/composer')) {
                 // TODO: more flexible composer detection
                 exec('composer install', $output);
                 DTLog::info('Executing composer install...' . implode("\n", $output));
                 //Checking for composer.phar
             } elseif (file_exists('/usr/local/bin/composer.phar')) {
                 exec('/usr/local/bin/composer.phar install', $output);
                 DTLog::info('Executing composer install...' . implode("\n", $output));
             } else {
                 DTLog::info('composer.json detected but unable to locate composer.');
             }
         }
         // clear cache
         DTLog::info('Clearing cache...');
         DeployController::clear_cache();
         // update database
         if (isset($_FILE_TO_URL_MAPPING[BASE_PATH])) {
             exec('php framework/cli-script.php dev/build', $output2);
             DTLog::info('Updating database...' . implode("\n", $output2));
         } else {
             DTLog::info('Database not updated. $_FILE_TO_URL_MAPPING must be set for ' . BASE_PATH);
         }
         //    		SS_ClassLoader::instance()->getManifest()->regenerate();
         //            ob_start();
         //            DatabaseAdmin::create()->doBuild(false, true, false);
         //            DTLog::info('dev/build complete: '.ob_get_contents());
         //            ob_end_clean();
     };
     $deploy->execute();
     return 'ok';
 }
 /**
  * Debugs are printed in yellow
  * See this for reference:
  * http://www.bashguru.com/2010/01/shell-colors-colorizing-shell-scripts.html
  */
 protected function debugmsg($message)
 {
     DTLog::debug($message);
     echo "{$message}\n";
 }
 public function lookupConsumer($provider)
 {
     try {
         $api = new DTAPI($this->db->select1("SELECT * FROM consumers WHERE consumer_key='{$provider->consumer_key}'"));
         if ($api["status"] == 0) {
             return OAUTH_CONSUMER_KEY_REFUSED;
         }
         $this->consumer_id = $api["id"];
         $provider->consumer_secret = $api["secret"];
         return OAUTH_OK;
     } catch (Exception $e) {
         DTLog::debug($e->getMessage());
     }
     return OAUTH_CONSUMER_KEY_UNKNOWN;
 }