function get() { if ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] and $_SERVER['REMOTE_ADDR'] != '127.0.0.1') { Error::http(403, "Requests for the sim/records resource must originate from the server environment."); } $sql = "SELECT brand_id FROM brands WHERE type_system='sim'"; $rows = DBquery::get($sql); $this->numBrands = count($rows); shuffle($rows); $brands = array(); foreach ($rows as $b) { $brands[] = DBquery::get("CALL budgetRevExp(" . $b['brand_id'] . ")")[0]; if (!$this->rating[$b['brand_id']]) { $this->setRatings($b['brand_id']); } } foreach ($brands as &$b) { $b['revBal'] = 1 * $b['revBal']; $b['expBal'] = 1 * $b['expBal']; $b['inflow'] = 1 * $b['inflow']; $b['lastWeekAdded'] = 1 * $b['lastWeekAdded']; if (!$b['revBal'] and !$b['expBal']) { $this->addBudget($b); } else { if ($b['lastWeekAdded'] < $this->weekNum and $b['revBal'] > $b['inflow']) { $this->addBudget($b); } else { $this->transact($b, $brands[mt_rand(0, $this->numBrands - 1)]); } } } return $brands; }
function asApplied($filters) { if (!is_array($filters['brand_id'])) { $filters['brand_id'] = explode(",", $filters['brand_id']); } if (!$filters['brand_id']) { Error::http(400, "An array of brand_id values are required when testing the applicability of throttle #{$this->throttle_id}."); } $currtime = time(); $throttle_id = $filters['throttle_id'] ? $filters['throttle_id'] : $this->throttle_id; $sql = "SELECT a.brand_id, from_user, amount, r.created\n\t\t\tFROM records r\n\t\t\tJOIN accounts a ON a.account_id=from_acct\n\t\t\tWHERE r.throttle_id=? AND r.status>-1 AND r.txntype='pn' AND {$currtime} - UNIX_TIMESTAMP(r.created) < {$this->period};"; $rows = DBquery::get($sql, array($throttle_id)); $used = array('by_all' => 0, 'by_brand' => array("0" => 0), 'by_user' => 0); foreach ($rows as $r) { $used['by_all'] += $r['amount']; if (in_array($r['brand_id'], $filters['brand_id'])) { $used['by_brand'][$r['brand_id']] += $r['amount']; } if (Requester::$user_id == $r['from_user']) { $used['by_user'] += $r['amount']; } } $this->unusedAmt = min(max($this->by_all - $used['by_all'], 0), max($this->by_brand - min($used['by_brand']), 0), max($this->by_user - $used['by_user'], 0)); unset($used['by_brand']["0"]); $this->used = $used; return array($this); }
function prepOther($data) { if (is_numeric($data->brand_id)) { return; } if (substr($data->brand_id, 0, 1) != "~") { $data->brand_id = "~" . $data->brand_id; } $data->brand_id = substr($data->brand_id, 0, 200); $sql = "SELECT brand_id FROM brands WHERE name LIKE ? LIMIT 1"; $rows = DBquery::get($sql, array($data->brand_id)); if ($rows) { $data->brand_id = $rows[0]['brand_id']; } else { require_once "models/BrandCollection.php"; $Brand = (new BrandCollection(json_decode('{ "name": "' . $data->brand_id . '", "mission": "simulate a well-known brand for whitelisting or blacklisting", "description": "This is a simulated brand to be used for testing the tatag system.", "type_system": "sim" }')))->add()[0]; $data->brand_id = $Brand->brand_id; if (!$data->brand_id) { Error::http(500, "Failed to create or use a new brand_id for what you are rating."); } } }
function get() { if (!$this->txntype) { $this->issued = $this->{'@id'} . "?txntype=np"; $this->revTransfer = $this->{'@id'} . "?txntype=nn"; $this->expTransfer = $this->{'@id'} . "?txntype=pp"; $this->intrause = $this->{'@id'} . "?txntype=pn&subtype=intrause"; $this->inflow = $this->{'@id'} . "?txntype=pn&subtype=inflow"; $this->outflow = $this->{'@id'} . "?txntype=pn&subtype=outflow"; return array_merge(array($this), $this->getByTxnType('np'), $this->getByTxnType('nn'), $this->getByTxnType('pp'), $this->getIntrause(), $this->getInflow(), $this->getOutflow()); } else { if ($this->txntype != 'pn') { return $this->getByTxnType(); } else { if ($this->subtype == 'intrause') { return $this->getIntrause(); } else { if ($this->subtype == 'inflow') { return $this->getInflow(); } else { if ($this->subtype == 'outflow') { return $this->getOutflow(); } else { Error::http(403, "When requesting records with txntype='pn', a URL query parameter value must be provided for 'subtype' and the value must equal 'intrause', 'inflow', OR 'outflow'. Actual value='{$this->subtype}'."); } } } } } }
function __construct() { if ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] and $_SERVER['REMOTE_ADDR'] != '127.0.0.1') { Error::http(403, "Requests for cron resources must originate from the server environment."); } $this->{"@id"} = "/cron/budgetAdd"; $this->{"@type"} = "CronAdd"; }
function __construct() { if ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] and $_SERVER['REMOTE_ADDR'] != '127.0.0.1') { Error::http(403, "Requests for cron resources must originate from the localhost environment."); } $this->{"@id"} = "/cron/report"; $this->{"@type"} = "report"; }
function __construct() { if ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR'] and $_SERVER['REMOTE_ADDR'] != '127.0.0.1') { Error::http(403, "Requests for cron resources must originate from the server environment."); } $this->{"@id"} = "{$this->root}/cron/tally"; $this->{'@type'} = "cronTally"; }
function add() { include_once "models/BrandMembers.php"; include_once "models/Accounts.php"; include_once "models/Holders.php"; $this->okToAdd = array('name', 'mission', 'description', 'type_system', 'type_id', 'country_code', 'area_code', 'url', 'advisor', 'logo'); $this->addKeyVal("type_system", "nonprofit", "ifMissing"); $this->addKeyVal("type_id", 10, "ifMissing"); $this->addKeyVal("country_code", "USA", "ifMissing"); $this->addKeyVal("area_code", 206, "ifMissing"); $this->addKeyVal("logo", 'NULL', "ifMissing"); //$this->addKeyVal("mission", 'NULL', "ifMissing"); $this->addKeyVal("url", 'NULL', "ifMissing"); $this->addKeyVal("advisor", 'NULL', "ifMissing"); $Brand = $this->obj; $Brand->brand_id = $this->insert(); //print_r($Brand); print_r(Requester); if (!$Brand->brand_id) { Error::http(500, "Failed to fully create a new brand."); } $Members = new BrandMembers(json_decode('{ "brand_id":' . $Brand->brand_id . ', "user_id":' . Requester::$user_id . ', "role":"admin", "hours":0 }')); $Brand->members = $Members->add(); $MainRev = (new Accounts(json_decode('{ "brand_id": ' . $Brand->brand_id . ', "name": "Main Revenue", "authcode": "cftix", "unit": "hour", "sign": -1 }')))->add(); $MainExp = (new Accounts(json_decode('{ "brand_id": ' . $Brand->brand_id . ', "name": "Main Expense", "authcode": "cftix", "unit": "hour", "sign": 1 }')))->add(); $Brand->accounts = array($MainRev, $MainExp); if ($Brand->type_system == 'sim') { $Members->resetSimMember(); } else { $Brand->holders[] = (new Holders(json_decode('{ "account_id": ' . $MainRev->account_id . ', "user_id":' . Requester::$user_id . ', "authcode": "cftix" }')))->add(); $Brand->holders[] = (new Holders(json_decode('{ "account_id": ' . $MainExp->account_id . ', "user_id":' . Requester::$user_id . ', "authcode": "cftix" }')))->add(); } return array($Brand); }
function get() { if (isset($_GET['save'])) { $this->set(); } $f = "ref/forms/" . Router::$subresource . ".json"; if (!file_exists($f)) { Error::http(404, "The form='" . Router::$subresource . "' was not found."); } return array(json_decode(file_get_contents($f))); }
function setDetails($throttle_id = 0) { $sql = "SELECT * FROM {$this->table} WHERE throttle_id=?"; $row = DBquery::get($sql, array($throttle_id ? $throttle_id : $this->throttle_id)); if (!$row) { Error::http(404, "No details were found for throttle #'{$this->throttle_id}'."); } foreach ($row[0] as $k => $v) { $this->{$k} = $v; } }
function __construct($data = '') { //move to advisor $this->from_brand = ($data and isset($data->from_brand)) ? $data->from_brand : $_GET['from_brand']; $this->to_brand = ($data and isset($data->to_brand)) ? $data->to_brand : $_GET['to_brand']; if (!$this->from_brand or !$this->to_brand) { Error::http(400, "A non-zero integer value must be specified for from_brand (specified '{$this->from_brand}') and to_brand ('{$this->to_brand}') as GET query parameters."); } $this->consumer_id = ($data and isset($data->from_brand)) ? 0 : $this->getID(); $this->{"@id"} = $this->consumer_id ? "{$this->root}/app/{$this->consumer_id}/advise" : "{$this->root}/app/advise"; $this->{"@type"} = "appAdvise"; }
function __construct($data = '') { $this->{"@type"} = "teamOrders"; $this->brand_id = $this->getID(); if (!Requester::isMember($this->brand_id)) { Error::http(403, "Only members or admins of brand #{$this->brand_id} can view its orders."); } $this->{'@id'} = "{$this->root}/team/{$this->brand_id}/orders"; $this->table = "records"; $this->collectionOf = "order"; $this->init($data); $this->okToFilterBy = array("record_id"); }
function __construct($data = '') { $this->brand_id = $this->getID(); if (!Requester::isMember($this->brand_id)) { Error::http(403, "The '/team/{$this->brand_id}' resource is only viewable by members of brand #{$this->brand_id}."); } $this->{"@type"} = 'brandTally'; $this->{"@id"} = "{$this->root}/team/{$this->brand_id}/tally"; $this->table = "members"; $this->init($data); $this->okToFilterBy = array("brand_id", "member_id"); $this->okToSet = array("joined", "revoked"); }
function __construct($data = '') { $this->user_id = $this->getID(); if (!Requester::isUser($this->user_id)) { Error::http(401, "The requester must be logged in as the requested user."); } $this->{"@id"} = "/user/{$this->user_id}/apps"; $this->{'@type'} = "userApps"; $this->collectionOf = "app"; $this->table = "consumers"; $this->init($data); $this->okToAdd = array("name", "secret", "type", "redirect_url"); }
function set() { $this->setFilters($_GET); $sql = "SELECT user_id, member_id FROM members WHERE {$this->filterCond}"; $rows = DBquery::get($sql, $this->filterValArr); foreach ($rows as $r) { if ($r['user_id'] != Requester::$user_id) { Error::http(403, "The requester cannot set another member's information. \n\t\t\t\tPlease check that requester (#{$this->user_id}) is filtering by his or her own member_id (#" . $r['member_id'] . ")."); } } $this->update(); return array($this->obj); }
function set() { if (!$_GET['record_id']) { Error::http(403, 'Missing record_id GET query parameter value, which is required for updating the record status.'); } if ($this->status == 7) { $row = DBquery::get("CALL approveRecord(" . $_GET['record_id'] . ")"); } else { $this->setFilters($_GET); $row = $this->update(); } return array($this->obj); }
function __construct($data = '') { $this->brand_id = $this->getID(); if (!Requester::isBrandAdmin($this->brand_id)) { Error::http(403, "The requester is not an admin for brand #{$this->brand_id}."); } $this->{"@type"} = 'brand'; $this->{"@id"} = "{$this->root}/brand/{$this->brand_id}"; $this->role = "admin"; $this->table = "brands"; $this->init($data); $this->okToSet = array("name", "ended", "mission", "description", "url", "advisor", "type_system", "type_id", "country_code", "area_code", "logo"); $this->okToFilterBy = array("brand_id"); }
function setDetails() { $sql = "SELECT brand_id, account_id FROM accounts WHERE account_id=? AND ended IS NULL"; $row = DBquery::get($sql, array($this->account_id)); if (!$row) { Error::http(404, "Account #{$this->account_id} was not found."); } foreach ($row[0] as $key => $val) { $this->{$key} = $val; } $this->id = $this->account_id; $this->brand = "{$this->root}/brand/" . $row[0]['brand_id']; return; }
function __construct($data = '') { $this->brand_id = $this->getID(); if (!Requester::isMember($this->brand_id)) { Error::http(403, "The requester is not a member of brand #{$this->brand_id}."); } $this->{"@type"} = 'accounts'; $this->{"@id"} = "{$this->root}/team/{$this->brand_id}/accounts"; $this->table = "accounts"; $this->idkey = 'account_id'; $this->collectionOf = "account"; $this->init($data); $this->okToGet = array("brand_id", "account_id", "name", "balance", "unit", "authcode"); }
function get() { if ($_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR'] or $_SERVER['REMOTE_ADDR'] == '127.0.0.1') { /*allowed*/ } else { Error::http(403, "Requests for the sim/records resource must originate from the server environment."); } $sql = "SELECT record_id FROM records WHERE status=0 AND TIMESTAMPDIFF(SECOND,created,NOW()) > 300"; $rows = DBquery::get($sql); $pending = count($rows); foreach ($rows as $r) { DBquery::get("CALL approveRecord(" . $r['record_id'] . ")"); } return array(array("numApproved" => $pending)); }
function matchBrandToRelay() { $sql = "SELECT brand_id FROM accounts a JOIN holders USING (account_id) WHERE holder_id=?"; $rows = DBquery::get($sql, array($this->relay->holder_id)); if (!$rows) { Error::http(403, "The holder_id value='{$this->holder_id}' was not found."); } if (!$this->brand_id) { $this->brand_id = $rows[0]['brand_id']; } else { if ($this->brand_id != $rows[0]['brand_id']) { Error::http(403, "Brand# {$this->brand_id} does not own the account of holder #" . $this->relay->holder_id . "."); } } }
function __construct($data = '') { $this->brand_id = $this->getID(); if (!Requester::isMember($this->brand_id)) { Error::http(403, "The '/team/{$this->brand_id}/members' resource is only viewable by members of brand #{$this->brand_id}."); } $this->{"@type"} = 'members'; $this->{"@id"} = "{$this->root}/team/{$this->brand_id}/members"; $this->table = "members"; $this->idkey = 'member_id'; $this->collectionOf = "member"; $this->init($data); $this->okToSet = array("role", 'hours', 'ended'); $this->okToFilterBy = array("member_id", "user_id"); }
function __construct($data = '') { $this->brand_id = $this->getID(); if (!Requester::isBrandAdmin($this->brand_id)) { Error::http(403, "The requester is not an admin for brand #{$this->brand_id}."); } $this->{"@type"} = 'brandAccounts'; $this->{"@id"} = "{$this->root}/brand/{$this->brand_id}/accounts"; $this->table = "accounts"; $this->idkey = 'account_id'; $this->collectionOf = "account"; $this->init($data); $this->okToGet = array("brand_id", "account_id", "name", "balance", "unit", "authcode"); $this->okToAdd = array("brand_id", 'name', 'authcode', 'unit', 'sign'); $this->okToSet = array("name", "authcode", "throttle_id"); $this->okToFilterBy = array("brand_id", "account_id"); }
function __construct($data = '') { $this->brand_id = $this->getID(); if (!Requester::isBrandAdmin($this->brand_id)) { Error::http(403, "The requester is not an admin for brand #{$this->brand_id}."); } $this->{"@type"} = 'brandHolders'; $this->{"@id"} = "{$this->root}/brand/{$this->brand_id}/holders"; $this->table = "holders"; $this->idkey = 'holder_id'; $this->collectionOf = "holder"; $this->init($data); $this->okToAdd = array("user_id", "account_id", "authcode", "limkey"); $this->okToSet = array("authcode", "ended"); $this->okToFilterBy = array("account_id", "user_id", "holder_id"); $this->okToGet = array('holder_id', 'account_id', 'authcode'); }
function __construct($data = '') { $this->member_id = $this->getID(); $this->setDetails(); if (!Requester::isBrandAdmin($this->brand_id)) { Error::http(403, "Only brand admins can access member accounts resource."); } $this->{"@type"} = "memberAccounts"; $this->{'@id'} = "{$this->root}/member/{$this->member_id}/accounts"; $this->table = "holders"; $this->idkey = 'holder_id'; $this->collectionOf = "holding"; $this->init($data); $this->okToAdd = array("user_id", "account_id", "authcode", "limkey"); $this->okToGet = array("holder_id", "holder_auth", "brand_id", "account_id", "name", "account_auth"); $this->okToSet = array("authcode", "ended"); $this->okToFilterBy = array("holder_id"); }
function getMemberId($brand_id = 0, $user_id = 0) { if (!$brand_id) { if (!$this->brand_id) { Error::http(400, "Missing brand_id property for Member->isMember()."); } $brand_id = $this->brand_id; } if (!$user_id) { if (!$this->user_id) { Error::http(400, "Missing user_id argument for Member->isMember()."); } $user_id = $this->user_id; } $sql = "SELECT member_id FROM members WHERE user_id={$user_id} AND brand_id={$brand_id} AND ended IS NULL"; $row = DBquery::get($sql); return $row[0]['member_id']; }
function add($data = '') { if (!isset($this->email) and !isset($this->fb_id) and !isset($this->gp_id) and !isset($this->tw_id)) { Error::http(400, "When registerng a user, an email, facebook id (fb_id), google+ id (gp_id) , or twitter id (tw_id) must be used as input."); } foreach ($this->okToAdd as $key) { $this->addKeyVal($key, "NULL", "ifMissing"); } $this->obj->password = password_hash($this->obj->password, PASSWORD_DEFAULT); $this->valArr[array_search('password', $this->keyArr)] = $this->obj->password; $User = $this->obj; $User->user_id = $this->insert(); require_once "utils/Router.php"; Requester::$user_id = $User->user_id; unset($User->password); //no need to communicate this back for privacy $this->setDefaultBrand(); return array($User); }
function get() { $sql = "SELECT * FROM consumers WHERE consumer_id=?"; $rows = DBquery::get($sql, array($this->consumer_id)); if (!$rows) { return array(null); } if ($rows[0]['user_id'] != Requester::$user_id) { Error::http(403, "The details for app #{$this->consumer_id} is viewable to its developer only."); } foreach ($rows[0] as $k => $v) { $this->{$k} = $v; } if ($rows[0]['type'] == 'advisor') { $this->advise = "{$this->root}/app/{$this->consumer_id}/advise"; $this->config = "{$this->root}/app/{$this->consumer_id}/config"; } return array($this); }
function setTokenUserID() { $info = json_decode(file_get_contents("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={$this->access_token}")); if (!$info) { Error::http(400, "No information was retrieved for access_token='{$this->access_token}'."); } if ($info->error) { Error::http(500, "Error retrieving information for access_token='{$this->access_token}': [{$info->error}]."); } if ($info->audience != GAPI_CLIENT_ID) { Error::http(401, "An invalid client_id was retrieved for access_token='{$this->access_token}'."); } $this->okToSet = array("otk", "user_id", "login_provider"); $this->user_id = $this->getByOauthID($info); $this->addKeyVal('user_id', $this->user_id); $this->addKeyVal('otk', mt_rand(1, 99999999)); $this->addKeyVal('login_provider', 'gp'); $this->update(array("token_id" => $this->token_id, "otk" => Requester::$otk, "token_val" => '0')); return array($this); }
function __construct($data = '') { $this->brand_id = $this->getID(); if (!Requester::isMember($this->brand_id)) { Error::http(403, "The requester is not a member of brand #{$this->brand_id}."); } $this->{"@type"} = "brandPromos"; $params = $_GET ? '?' . http_build_query($_GET) : ''; $this->{'@id'} = "{$this->root}/brand/{$this->brand_id}/promos" . $params; $this->table = "promos"; $this->idkey = 'promo_id'; $this->collectionOf = "promo"; if (Router::$method == 'add' or Router::$method == 'set') { $this->translateInput($data); } $this->pageOrder = "desc"; //prevents being reset $this->init($data); $this->okToFilterBy = array("brand_id", "promo_id", 'expires'); }