/** * Prepare the url of Google authorization call * * @return string */ protected function makeAuthUrl() { $state = \Lampcms\String::makeRandomString(16); $_SESSION[self::STATE_KEY] = $state; $vars = array('{prompt}' => LAMPCMS_DEBUG ? '&approval_prompt=force' : '', '{redirect}' => $this->redirectUri, '{client_id}' => $this->configSection['CLIENT_ID'], '{scope}' => \urlencode(\implode(' ', $this->configSection['SCOPE'])), '{state}' => $state); $res = \strtr(self::AUTH_URL, $vars); return $res; }
/** * Generates a random string * to be use in password reset url * It checks to make sure this string does not already exist * in the PASSWORD_CHANGE table * * @return object $this * * @throws LampcmsException in case a unique string * could not be generated */ protected function generateCode() { d('cp'); $counter = 0; $done = false; do { $counter++; $aData = array(); $aData['_id'] = \strtolower(\Lampcms\String::makeRandomString(12)); $aData['i_ts'] = time(); $aData['i_uid'] = $this->uid; /** * @todo * Don't use _id for string, * instead use unique index on string + 'y'/'n' value of 'used' * This way string can be duplicate as long as no same * string is used */ try { $coll = $this->Registry->Mongo->PASSWORD_CHANGE; $coll->insert($aData, array('fsync' => true)); $done = true; d('cp'); } catch (\MongoException $e) { d('code already exists, trying again...'); } } while (!$done && $counter < 50); if (!$done) { throw new \Lampcms\Exception('Error: Unable to generate random string at this time, please try again in 30 seconds'); } $this->randomString = $aData['_id']; return $this; }
/** * Save the submitted form values * by setting the $this->oApi object * and then calling insert() or save() on it * * @return object $this * */ protected function save() { $isUpdate = false; $vals = $this->Form->getSubmittedValues(); d('vals: ' . print_r($vals, 1)); $appid = (int) $vals['app_id']; if ($appid > 0) { $isUpdate = true; d('has appid, editing mode'); $this->validateAppIdOwnership($appid); } else { /** * Auto-generate app_id * Use USERS auto-increment value * because we can then store the image in the same * way we store avatar - in the same directory * using hex based path. * */ $appid = $this->Registry->Incrementor->nextValue('USERS'); } d('$appid: ' . $appid); $this->oApi['_id'] = $appid; $this->oApi['i_uid'] = $this->Registry->Viewer->getUid(); $this->oApi['app_name'] = (string) $this->Request->getUTF8('app_name')->trim()->stripTags(); $this->oApi['appsite'] = (string) $this->Request->getUTF8('appsite')->trim()->stripTags(); $this->oApi['company'] = (string) $this->Request->getUTF8('company')->trim()->stripTags(); $this->oApi['app_type'] = (string) $this->Request->getUTF8('app_type')->trim()->stripTags(); $this->oApi['about'] = (string) $this->Request->getUTF8('about')->trim()->stripTags(); $this->oApi['api_key'] = $appid . '.' . String::makeRandomString(12); $this->parseIcon(); /** * Ensure that app is a unique field * app is the name of application */ $coll = $this->Registry->Mongo->API_CLIENTS; $coll->ensureIndex(array('app_name' => 1), array('unique' => true)); $coll->ensureIndex(array('api_key' => 1), array('unique' => true)); $coll->ensureIndex(array('i_uid' => 1)); try { if ($isUpdate) { d('cp'); $this->oApi['edited_time'] = date('F j, Y g:i a T'); $this->oApi['edit_ip'] = Request::getIP(); $res = $this->oApi->save(); } else { d('cp'); $this->oApi['created_time'] = date('F j, Y g:i a T'); $this->oApi['ip'] = Request::getIP(); $res = $this->oApi->insert(); } } catch (\Exception $e) { throw new \OutOfBoundsException($e->getMessage()); } d('$res: ' . $res); return $this; }