public function submitVolunteerApplication($req, $res) { $currentUser = $this->app['user']; // make sure the user is logged in if (!$currentUser->isLoggedIn()) { setcookie('redirect', '/volunteers/application', time() + 3600, '/'); return $res->redirect('/login'); } if (!$req->request('accept')) { $req->setParams(['accept_error' => true]); return $this->volunteerApplication($req, $res); } $input = $req->request(); $input['uid'] = $currentUser->id(); $input['birth_date'] = mktime(0, 0, 0, $input['month'] + 1, $input['day'], $input['year']); $input['first_time_volunteer'] = !U::array_value($input, 'volunteered_before'); $application = $currentUser->volunteerApplication(); if (!$application->exists()) { $application = new VolunteerApplication(); if ($application->create($input)) { return $res->redirect('/volunteers/application/thanks'); } } else { if ($application->set($input)) { return $res->redirect('/volunteers/application/thanks'); } } return $this->volunteerApplication($req, $res); }
public function getFriendsCount() { $facebook = $this->app['facebook_service']; $facebook->setAccessTokenFromProfile($this); $friends = $facebook->api('me/friends', 'get'); if (is_array($friends)) { return count((array) U::array_value($friends, 'data')); } return -1; }
/** * Performs an API call on the facebook API (if available) or * returns a mock response. * * @param string $endpoint * @param string $method HTTP method * @param array $params optional params * * @return object */ public function api($endpoint, $method = null, $params = null) { $response = false; try { return $this->app['facebook']->api($endpoint, $method, $params); } catch (\FacebookApiException $e) { // access token has expired $result = $e->getResult(); $code = U::array_value($result, 'error.code'); if ($code == 190) { // clear the access token of the user's profile if ($this->profile) { $this->profile->grantAllPermissions(); $this->profile->set('access_token', ''); $this->profile->enforcePermissions(); } } else { $this->app['logger']->error($e); } return false; } }
protected function preSetHook(&$data) { // make sure the place name is unique $name = U::array_value($data, 'name'); if (!empty($name) && $name != $this->name && self::totalRecords(['organization' => $this->organization, 'name' => $name]) > 0) { $errorStack = $this->app['errors']; $errorStack->push(['error' => ERROR_VOLUNTEER_PLACE_NAME_TAKEN, 'params' => ['place_name' => $name]]); return false; } // geocode if (isset($data['address'])) { $data['coordinates'] = $this->geocode($data['address']); } $this->justApproved = isset($data['verify_approved']) && $data['verify_approved'] && !$this->verify_approved; return true; }
/** * Generates the output of a report for a given type. * * @param string $type html|pdf|csv * @param bool $stream when true, streams the resulting file to the client (pdf, csv only) * @param Response $res when streaming, response object to use * * @return string|array|false */ public function output($type, $stream = false, Response $res = null) { // $this->organization->useTimezone(); $type = strtolower($type); if ($type == 'html') { $this->htmlOutput = true; // NOTE host name has the development port number stripped, // otherwise the css is not loaded $data = ['css' => 'file://' . INFUSE_PUBLIC_DIR . '/css/report.css', 'header' => $this->getHeader(), 'sections' => $this->getSections()]; $this->htmlOutput = false; $view = new View('report', $data); return $view->render(); } elseif ($type == 'pdf') { $html = $this->output('html'); // Run wkhtmltopdf $descriptorspec = [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']]; $process = proc_open(WKHTMLTOPDF_CMD, $descriptorspec, $pipes); // Send the HTML on stdin fwrite($pipes[0], $html); fclose($pipes[0]); // Read the outputs $pdf = stream_get_contents($pipes[1]); $errors = stream_get_contents($pipes[2]); // Close the process fclose($pipes[1]); $return_value = proc_close($process); // Handle errors if ($errors) { error_log($errors); } // Output the results if ($stream) { $res->setContentType('application/pdf')->setHeader('Cache-Control', 'public, must-revalidate, max-age=0')->setHeader('Pragma', 'public')->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT')->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT')->setHeader('Content-Length', strlen($pdf))->setHeader('Content-Disposition', 'attachment; filename="' . $this->baseFilename() . '.pdf";')->setBody($pdf); } else { return $pdf; } } elseif ($type == 'csv') { $output = []; $header = $this->getHeader(); foreach ($header as $key => $value) { $output[] = [$key, $value]; } $output[] = []; $sections = $this->getSections(); foreach ($sections as $section) { if (isset($section['title'])) { $output[] = [$section['title']]; } if (isset($section['keyvalue'])) { foreach ($section['keyvalue'] as $key => $value) { $output[] = [$key, $value]; } $output[] = []; } $entireTable = array_merge([(array) U::array_value($section, 'header')], (array) U::array_value($section, 'rows'), [(array) U::array_value($section, 'footer')]); foreach ($entireTable as $row) { $output[] = $row; } $output[] = []; } $csv = fopen('php://output', 'w'); ob_start(); foreach ($output as $row) { fputcsv($csv, $row); } fclose($csv); $output = ob_get_clean(); if ($stream) { $res->setContentType('text/csv')->setHeader('Cache-Control', 'public, must-revalidate, max-age=0')->setHeader('Pragma', 'public')->setHeader('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT')->setHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT')->setHeader('Content-Length', strlen($output))->setHeader('Content-Disposition', 'attachment; filename="' . $this->baseFilename() . '".csv')->setBody($output); } else { return $output; } } return false; }
private function loginOrRegister($fbid, $user_profile, $req, $res) { $currentUser = $this->app['user']; $facebook = $this->app['facebook']; // get friend count $friendCount = 0; try { $friends = $facebook->api('me/friends'); $friendCount = count((array) U::array_value($friends, 'data')); } catch (\FacebookApiException $e) { $this->app['logger']->error($e); } // generate parameters to update profile $profileUpdateArray = ['id' => $fbid, 'access_token' => $facebook->getAccessToken(), 'friends_count' => $friendCount]; // fbid matches existing user? $user = User::findOne(['where' => ['facebook_id' => $fbid]]); if ($user) { // check if we are dealing with a temporary user if (!$user->isTemporary()) { if ($user->id() != $currentUser->id()) { if ($req->query('forceLogin') || !$currentUser->isLoggedIn()) { // log the user in $this->app['auth']->signInUser($user->id(), 'facebook'); } else { $logoutNextUrl = $this->app['base_url'] . 'facebook/connect?logout=t'; // inform the user that the facebook account they are trying to connect // belongs to someone else return new View('switchingAccounts/facebook', ['title' => 'Switch accounts?', 'otherUser' => $user, 'otherProfile' => $user->facebookProfile(), 'logoutUrl' => $facebook->getLogoutUrl(['next' => $logoutNextUrl])]); } } $profile = new FacebookProfile($fbid); // create or update the profile if ($profile->exists()) { $profile->set($profileUpdateArray); } else { $profile = new FacebookProfile(); $profile->create($profileUpdateArray); } // refresh profile from API $profile->refreshProfile($user_profile); return $this->finalRedirect($req, $res); } else { // show finish signup screen $req->setSessoin('fbid', $fbid); return $res->redirect('/signup/finish'); } } if ($currentUser->isLoggedIn()) { // add to current user's account $currentUser->set('facebook_id', $fbid); } else { // save this for later $req->setSession('fbid', $fbid); } $profile = new FacebookProfile($fbid); // create or update the profile if ($profile->exists()) { $profile->set($profileUpdateArray); } else { $profile = new FacebookProfile(); $profile->create($profileUpdateArray); } // refresh profile from API $profile->refreshProfile($user_profile); // get outta here if ($currentUser->isLoggedIn()) { $this->finalRedirect($req, $res); } else { $res->redirect('/signup/finish'); } }
private function getModelForAdmin($req, $res) { // lookup model class // index derived from /organizations/:username/admin/SECTION/.... $section = $req->paths(3); $modelClass = U::array_value(self::$sectionModels, $section); if (!$modelClass) { $res->setCode(404); return false; } // lookup org $org = $this->getOrgForAdmin($req, $res); if (!is_object($org)) { return false; } $model = new $modelClass($req->params('id')); if ($section == 'volunteers') { $model = new $modelClass([$req->params('id'), $org->id()]); } if (!$model->exists()) { $res->setCode(404); return false; } if (!$model->can('view', $this->app['user'])) { $res->setCode(401); return false; } return [$org, $model, $section]; }
/** * Increments the keys in an input array by some delta. * NOTE stats cannot be less than 0. * * @param array $source values to be incremented * @param array $delta values to be added * * @return array incremented source */ public static function increment(array $source, array $delta) { $return = []; foreach ($source as $k => $v) { $return[$k] = max(0, (int) $v + (int) U::array_value($delta, $k)); } return $return; }
protected function preCreateHook(&$data) { $org = new Organization(U::array_value($data, 'organization')); // check creator permission $requester = $this->app['user']; $role = $org->getRoleOfUser($requester); if ($role < Volunteer::ROLE_VOLUNTEER && !$requester->isAdmin()) { $this->app['errors']->push(['error' => ERROR_NO_PERMISSION]); return false; } // volunteers cannot approve own hours if ($role < Volunteer::ROLE_ADMIN && !$requester->isAdmin()) { $data['approved'] = false; } // validate number of hours $hours = $data['hours'] = floor($data['hours']); if ($hours <= 0 || $hours >= 13) { $this->app['errors']->push(['error' => 'invalid_num_volunteer_hours']); return false; } // convert day timestamp to beginning of day $data['timestamp'] = self::timestampToStartOfDay($data['timestamp']); // the timestamp on hours cannot be more than 1 day in the future if ($data['timestamp'] - 86400 > time()) { $this->app['errors']->push(['error' => 'invalid_hours_timestamp']); return false; } // approval link if (!U::array_value($data, 'approved')) { $data['approval_link'] = U::guid(false); } if (isset($data['tags'])) { self::$createTags = $data['tags']; if (!is_array(self::$createTags)) { self::$createTags = explode(' ', self::$createTags); } } return true; }
public function preSetHook(&$data) { $organization = $this->relation('organization'); $currentUser = $this->app['user']; $currentRole = $organization->getRoleOfUser($currentUser); $isAdmin = $currentUser->isAdmin() || $currentRole == self::ROLE_ADMIN; // volunteers can only be promoted if current user is admin $maxLevel = $isAdmin ? self::ROLE_ADMIN : self::ROLE_AWAITING_APPROVAL; $role = U::array_value($data, 'role'); if ($role > $maxLevel) { $this->app['errors']->push(['error' => ERROR_NO_PERMISSION]); return false; } // email user if going from not approved to approved if ($role >= self::ROLE_VOLUNTEER && $this->role == self::ROLE_AWAITING_APPROVAL) { $data['approval_link'] = null; $this->needsApproveEmail = true; } return true; }
/** * Maps the properties of the user profile from the API * to the properties in our model. * * @param array $user_profile user profile from API * * @return array */ protected function mapPropertiesFromApi(array $user_profile) { $info = []; foreach ($this->apiPropertyMapping() as $modelProperty => $apiProperty) { $info[$modelProperty] = U::array_value($user_profile, $apiProperty); } return $info; }
/** * Fetches the models for a given controller. * * @param object $controller * * @return array */ private function models($controller) { $properties = $controller::$properties; $module = $this->name($controller); $models = []; foreach ((array) U::array_value($properties, 'models') as $model) { $modelClassName = '\\app\\' . $module . '\\models\\' . $model; $info = $modelClassName::metadata(); $models[$model] = array_replace($info, ['route_base' => '/' . $module . '/' . $info['plural_key']]); } return $models; }