示例#1
0
 public static function generate(array $params)
 {
     $code = Lib\Url::Get('code', null);
     $action = array_shift($params);
     if ($action === 'logout') {
         $user = Api\User::getCurrentUser();
         if ($user) {
             $user->logout();
             header('Location: /brackets/');
         }
     }
     if ($code) {
         $success = Api\User::authenticateUser($code);
         if ($success) {
             $redirect = Lib\Url::Get('state', '/');
             header('Location: ' . $redirect);
             exit;
         } else {
             Lib\Display::addKey('content', 'We were unable to verify your account at this time or your account age does not meet the requirements.');
         }
     } else {
         $obj = new stdClass();
         $obj->loginUrl = Api\User::getLoginUrl(Lib\Url::Get('redirect'));
         // Do a mobile check
         if (preg_match('/iphone|android|windows phone/i', $_SERVER['HTTP_USER_AGENT'])) {
             $obj->loginUrl = str_replace('authorize', 'authorize.compact', $obj->loginUrl);
         }
         $obj->originalUrl = Lib\Url::Get('redirect');
         Lib\Display::addKey('page', 'login');
         Lib\Display::addKey('title', 'Login' . DEFAULT_TITLE_SUFFIX);
         Lib\Display::renderAndAddKey('content', 'login', $obj);
     }
 }
示例#2
0
 private static function _cropImage()
 {
     $out = new stdClass();
     $out->success = false;
     $out->message = 'Unable to crop image';
     $imageFile = Lib\Url::Post('imageFile');
     $x = Lib\Url::Post('x', true);
     $y = Lib\Url::Post('y', true);
     $width = Lib\Url::Post('width', true);
     $height = Lib\Url::Post('height', true);
     if ($imageFile && null !== $x && null !== $y && null !== $width && null !== $height) {
         $imageFile = $imageFile[0] === '/' ? '.' . $imageFile : $imageFile;
         $image = Lib\ImageLoader::loadImage($imageFile);
         if ($image) {
             $image = self::_sizeUp($image->image);
             $croppedImage = imagecreatetruecolor(BRACKET_IMAGE_SIZE, BRACKET_IMAGE_SIZE);
             imagecopyresampled($croppedImage, $image, 0, 0, $x, $y, BRACKET_IMAGE_SIZE, BRACKET_IMAGE_SIZE, $width, $height);
             $fileName = '/cache/' . md5($imageFile) . '.jpg';
             imagejpeg($croppedImage, '.' . $fileName);
             imagedestroy($image);
             imagedestroy($croppedImage);
             $out->success = true;
             $out->fileName = $fileName;
         }
     } else {
         $out->message = 'Parameters missing';
     }
     Lib\Display::renderJson($out);
 }
示例#3
0
 public static function generate(array $params)
 {
     $bracket = self::_getBracket(array_shift($params));
     if ($bracket) {
         // Create the bracket on POST
         if ($_POST) {
             $id = Lib\Url::Post('bracketId', true);
             $name = Lib\Url::Post('bracketName');
             $rules = Lib\Url::Post('rules');
             if ($name && $rules) {
                 $bracket->name = trim($name);
                 $bracket->rules = $rules;
                 $bracket->nameLabel = Lib\Url::Post('nameLabel');
                 $sourceOn = Lib\Url::Post('hideSource') !== 'on';
                 $bracket->sourceLabel = $sourceOn ? Lib\Url::Post('sourceLabel') : 'NO_SOURCE';
                 $advanceHour = Lib\Url::Post('advanceHour', true);
                 $advanceHour = null !== $advanceHour ? $advanceHour : -1;
                 $bracket->advanceHour = $advanceHour;
                 if ($bracket->sync()) {
                     // Clear the generic bracket related caches
                     self::_refreshCaches($bracket);
                     header('Location: /me/?edited');
                     exit;
                 }
             }
         }
         $bracket->sourceHidden = $bracket->sourceLabel === 'NO_SOURCE';
         $bracket->times = self::_generateAdvanceTimes($bracket->advanceHour);
         Lib\Display::renderAndAddKey('content', 'admin/bracket', $bracket);
     }
 }
示例#4
0
 private static function _getVotesOverTime($vars, $users = false)
 {
     $time = time();
     $startDate = Lib\Url::GetInt('startDate', $time - 3600 * 24, $vars);
     // Default to the last 24 hours
     $endDate = Lib\Url::GetInt('endDate', $time, $vars);
     $bracketId = Lib\Url::GetInt('bracketId', null, $vars);
     $granularity = Lib\Url::GetInt('granularity', 2, $vars);
     $cacheKey = '_getVotesOverTime_' . implode('_', [$startDate, $endDate, $bracketId, $granularity, $users]);
     $retVal = Lib\Cache::Get($cacheKey);
     if (false === $retVal && $bracketId) {
         $selectCount = $users ? 'DISTINCT user_id' : '1';
         $result = Lib\Db::Query('SELECT COUNT(' . $selectCount . ') AS total, DATE(FROM_UNIXTIME(vote_date)) AS date, HOUR(FROM_UNIXTIME(vote_date)) AS hour, (MINUTE(FROM_UNIXTIME(vote_date)) % :granularity) AS hour_fraction FROM votes WHERE bracket_id = :bracketId AND vote_date BETWEEN :start AND :end GROUP BY date, hour, hour_fraction ORDER BY date, hour, hour_fraction', [':granularity' => $granularity, ':bracketId' => $bracketId, ':start' => $startDate, ':end' => $endDate]);
         if ($result && $result->count) {
             $retVal = [];
             while ($row = Lib\Db::Fetch($result)) {
                 $obj = new stdClass();
                 $obj->date = (int) $row->date;
                 $obj->hour = (int) $row->hour;
                 $obj->minutes = $row->hour_fraction == 0 ? 0 : 60 * ((int) $row->hour_fraction / $granularity);
                 $obj->count = (int) $row->total;
                 $retVal[] = $obj;
             }
             Lib\Cache::Set($cacheKey, $retVal, STATS_CACHE_DURATION);
         }
     }
     return $retVal;
 }
示例#5
0
 public static function start()
 {
     self::$_id = Url::Get(SESSION_NAME, null, $_COOKIE);
     if (!self::$_id) {
         self::$_id = bin2hex(openssl_random_pseudo_bytes(32));
         setcookie(SESSION_NAME, self::$_id, time() + SESSION_EXPIRE, '/', SESSION_DOMAIN);
     }
     self::$_sess = Cache::Get(SESSION_NAME . '_' . self::$_id, true);
 }
示例#6
0
 /**
  * Creates a cache key using selected values from an array of values (usually _GET)
  */
 public static function createCacheKey($prefix, $params, $values)
 {
     $retVal = [$prefix];
     foreach ($params as $param) {
         $value = Url::Get($param, 'null', $values);
         if (is_array($value)) {
             $value = implode(',', $value);
         }
         $retVal[] = $value;
     }
     return implode('_', $retVal);
 }
示例#7
0
 public static function render()
 {
     $query = Lib\Url::Get('q');
     $bracketId = Lib\Url::GetInt('bracketId');
     $out = Api\MalItem::getNameTypeahead($query, 'character');
     if ($bracketId) {
         $out = array_merge($out, self::_getSimilarCharacters($bracketId, $query));
     }
     // Standardize the output
     $out = self::_standardizeData($out);
     Lib\Display::renderJson($out);
 }
示例#8
0
 private static function _getBracketCharacters()
 {
     $retVal = null;
     $bracketId = Lib\Url::GetInt('bracketId', null);
     $count = Lib\Url::GetInt('count', null);
     if ($bracketId) {
         //If $count has a value, get random characters from the given bracket
         if ($count) {
             $bracket = \Api\Bracket::getById($bracketId);
             if ($bracket) {
                 //3 levels of IFs. This is getting rediculous
                 $retVal = \Api\Character::getRandomCharacters($bracket, $count);
             }
         } else {
             $retVal = \Api\Character::getByBracketId($bracketId);
         }
     }
     return $retVal;
 }
示例#9
0
 public static function generate(array $params)
 {
     // Create the bracket on POST
     if ($_POST) {
         $name = Lib\Url::Post('name');
         $rules = Lib\Url::Post('rules');
         if ($name && $rules) {
             $bracket = new Api\Bracket();
             $bracket->name = trim($name);
             $bracket->rules = $rules;
             $bracket->state = 0;
             $bracket->start = time();
             $bracket->generatePerma();
             $bracket->nameLabel = Lib\Url::Post('nameLabel');
             $bracket->minAge = Lib\Url::Post('minAge', true);
             $hideSource = Lib\Url::Post('hideSource') === 'on';
             $bracket->sourceLabel = $hideSource ? 'NO_SOURCE' : Lib\Url::Post('sourceLabel');
             $advanceHour = Lib\Url::Post('advanceHour', true);
             if ($advanceHour !== null) {
                 $utcOffset = Lib\Url::Post('utcOffset', true);
                 $advanceHour += $utcOffset !== null ? $utcOffset : 0;
             } else {
                 $advanceHour = -1;
             }
             $bracket->advanceHour = $advanceHour;
             if ($bracket->sync()) {
                 $bracket->addUser(self::$_user);
                 self::_refreshCaches();
                 // Clear the generic bracket related caches
                 header('Location: /me/?created');
                 exit;
             }
         }
     }
     // Or display the form
     $_POST['times'] = self::_generateAdvanceTimes();
     $_POST['ages'] = self::_generateAges(REDDIT_MINAGE);
     Lib\Display::renderAndAddKey('content', 'admin/bracket', $_POST);
 }
示例#10
0
 private static function _updateCharacter(Api\Bracket $bracket)
 {
     $out = new stdClass();
     $out->success = false;
     $id = Lib\Url::Post('characterId', true);
     $name = Lib\Url::Post('name');
     $source = Lib\Url::Post('source');
     $action = Lib\Url::Post('action');
     if ($id && $name && $action) {
         $out->action = $action;
         $character = Api\Character::getById($id);
         if ($character && $character->bracketId == $bracket->id) {
             if ($action == 'update') {
                 $character->name = $name;
                 $character->source = $source;
                 if ($character->sync()) {
                     $out->success = true;
                 } else {
                     $out->message = 'Error updating database';
                 }
             } else {
                 if ($action == 'delete') {
                     if ($bracket->state == BS_NOMINATIONS || $bracket->state == BS_ELIMINATIONS) {
                         if ($character->delete()) {
                             $out->success = true;
                         } else {
                             $out->message = 'Delete failed';
                         }
                     } else {
                         $out->message = 'Cannot delete characters after voting has started';
                     }
                 } else {
                     $out->message = 'Unknown action';
                 }
             }
         } else {
             $out->message = 'Character does not belong to this bracket';
         }
     } else {
         $out->message = 'Missing fields';
     }
     Lib\Display::renderJson($out);
 }
示例#11
0
 protected static function _checkLogin()
 {
     $user = Api\User::getCurrentUser();
     $readonly = Lib\Url::GetBool('readonly', null);
     if (!$user && !$readonly && stripos($_SERVER['HTTP_USER_AGENT'], 'google') === false) {
         header('Location: /user/login/?redirect=' . urlencode($_GET['q']));
         exit;
     }
     // Setup a default user if we're in readonly
     if (!$user) {
         $user = new stdClass();
         $user->id = 0;
     }
     // Seed the test bucket with the user's ID
     Lib\TestBucket::initialize($user->id);
     return $user;
 }
示例#12
0
 /**
  * silme
  *
  * @param int $post_id
  * @return mixed
  */
 public function delete($post_id)
 {
     if ($this->request->getMethod() == 'GET') {
         $this->post->Catdelete($post_id);
         \Lib\Url::redirect('admin/' . $this->post_name);
     }
 }
示例#13
0
 private static function _vote($user)
 {
     $out = new stdClass();
     $out->success = false;
     $bracketId = Lib\Url::Post('bracketId', true);
     $bracket = Api\Bracket::getById($bracketId);
     if ($bracket) {
         $state = $bracket ? (int) $bracket->state : null;
         if ($bracket->isLocked()) {
             $out->message = 'Voting is closed for this round. Please refresh to see the latest round.';
         } else {
             if ($state === BS_ELIMINATIONS || $state === BS_VOTING) {
                 if (self::_verifyAccountAge($user, $bracket)) {
                     // Break the votes down into an array of round/character objects
                     $votes = [];
                     foreach ($_POST as $key => $val) {
                         if (strpos($key, 'round:') === 0) {
                             $key = str_replace('round:', '', $key);
                             $obj = new stdClass();
                             $obj->roundId = (int) $key;
                             $obj->characterId = (int) $val;
                             $votes[] = $obj;
                         }
                     }
                     $count = count($votes);
                     if ($count > 0) {
                         $query = 'INSERT INTO `votes` (`user_id`, `vote_date`, `round_id`, `character_id`, `bracket_id`) VALUES ';
                         $params = [':userId' => $user->id, ':date' => time(), ':bracketId' => $bracketId];
                         $insertCount = 0;
                         // Only run an insert for rounds that haven't been voted on
                         $rounds = Api\Votes::getOpenRounds($user, $votes);
                         for ($i = 0; $i < $count; $i++) {
                             if (!isset($rounds[$votes[$i]->roundId])) {
                                 $query .= '(:userId, :date, :round' . $i . ', :character' . $i . ', :bracketId),';
                                 $params[':round' . $i] = $votes[$i]->roundId;
                                 $params[':character' . $i] = $votes[$i]->characterId;
                                 $insertCount++;
                                 $rounds[$votes[$i]->roundId] = true;
                             }
                         }
                         if ($insertCount > 0) {
                             $query = substr($query, 0, strlen($query) - 1);
                             if (Lib\Db::Query($query, $params)) {
                                 $out->success = true;
                                 // I am vehemently against putting markup in the controller, but there's much refactor needed to make this right
                                 // So, that's a note that it will be changed in the future
                                 $out->message = 'Your votes were successfully submitted! <a href="/results/' . $bracket->perma . '">View Results</a>';
                                 // Oops, I did it again...
                                 if ($bracket->externalId) {
                                     $out->message .= ' or <a href="http://redd.it/' . $bracket->externalId . '" target="_blank">discuss on reddit</a>.';
                                 }
                                 // Clear any user related caches
                                 $round = Api\Round::getById($votes[0]->roundId);
                                 Lib\Cache::Set('GetBracketRounds_' . $bracketId . '_' . $round->tier . '_' . $round->group . '_' . $user->id, false);
                                 Lib\Cache::Set('GetBracketRounds_' . $bracketId . '_' . $round->tier . '_all_' . $user->id, false);
                                 Lib\Cache::Set('CurrentRound_' . $bracketId . '_' . $user->id, false);
                                 $bracket->getVotesForUser($user, true);
                             } else {
                                 $out->message = 'There was an unexpected error. Please try again in a few moments.';
                             }
                         } else {
                             $out->message = 'Voting for this round has closed';
                             $out->code = 'closed';
                         }
                     } else {
                         $out->message = 'No votes were submitted';
                     }
                 } else {
                     $out->message = 'Your reddit account is not old enough to vote in this bracket';
                 }
             } else {
                 $out->message = 'Voting is closed on this bracket';
                 $out->code = 'closed';
             }
         }
     } else {
         $out->message = 'Invalid parameters';
     }
     return $out;
 }
示例#14
0
 public static function _generateBracket(Api\Bracket $bracket)
 {
     $retVal = null;
     if ($bracket) {
         $availableEntrants = Api\Round::getRoundCountForTier($bracket, 0);
         // Can't have much of a bracket with only two entrants...
         if ($availableEntrants < 2) {
             $message = self::_createMessage('error', 'There are not enough entrants to generate a bracket :(');
             self::_main($message);
         } else {
             if (count($_POST) > 0) {
                 $entrants = Lib\Url::Post('entrants', true);
                 $groups = Lib\Url::Post('groups', true);
                 if ($entrants && $groups) {
                     // Verify that the entrants/groups combo doesn't exceed to number of available entrants
                     if ($entrants * $groups > $availableEntrants) {
                         $message = self::_createMessage('error', 'Cannot generate a bracket of that size');
                         self::_main($message);
                     } else {
                         $bracket->advance();
                         if ($bracket->createBracketFromEliminations($entrants * $groups, $groups)) {
                             $message = self::_createMessage('success', 'Voting for bracket "' . $bracket->name . '" has successfully started!');
                             self::_refreshCaches($bracket);
                             self::_main($message);
                         } else {
                             $message = self::_createMessage('error', 'There are not enough entrants to create a bracket of that size');
                             self::_main($message);
                         }
                     }
                 } else {
                     $message = self::_createMessage('error', 'There was an error starting the bracket');
                     self::_main($message);
                 }
             } else {
                 $out = (object) ['bracket' => $bracket, 'count' => $availableEntrants];
                 Lib\Display::renderAndAddKey('content', 'admin/start_bracket', $out);
             }
         }
     }
 }
示例#15
0
 /**
  * Returns the list of times for the edit/create forms
  */
 protected static function _generateAdvanceTimes($selectedTime = -1)
 {
     $retVal = [(object) ['label' => 'I want to manage this manually', 'value' => -1]];
     $offset = Lib\Url::GetInt('utcOffset', 0, $_COOKIE);
     $offset /= 60;
     for ($i = 0; $i < 24; $i++) {
         // Offset for the user's timezone
         $hour = $i + $offset;
         if ($hour > 23) {
             $hour -= 24;
         } else {
             if ($hour < 0) {
                 $hour += 24;
             }
         }
         // gross...
         if ($i === 0) {
             $label = '12am';
         } else {
             if ($i < 12) {
                 $label = $i . 'am';
             } else {
                 if ($i === 12) {
                     $label = $i . 'pm';
                 } else {
                     $label = $i - 12 . 'pm';
                 }
             }
         }
         $retVal[] = (object) ['label' => $label, 'value' => $hour, 'selected' => $hour == $selectedTime];
     }
     return $retVal;
 }
示例#16
0
 /**
  * çıkış yap
  */
 public function logout()
 {
     \Lib\Session::destroy('loggedin');
     \Lib\Session::destroy('kullanici_bilgileri');
     \Lib\Session::destroy('stncart');
     \Lib\Session::destroy('teslimatZamani');
     \Lib\Session::destroy('sepetStokSorunu');
     \Lib\Url::redirect('uyelik');
 }