diff --git a/app.php b/app.php index 841481e..cb14912 100644 --- a/app.php +++ b/app.php @@ -3,8 +3,8 @@ define('ENCODING', 'UTF-8'); $app->get('/skins/{nickname}', function ($nickname) use ($app) { - $systemVersion = $app->request->get('version', 'int'); - $minecraftVersion = $app->request->get('minecraft_version', 'string'); + // $systemVersion = $app->request->get('version', 'int'); + // $minecraftVersion = $app->request->get('minecraft_version', 'string'); // На всякий случай проверка на наличие .png для файла if (strrpos($nickname, '.png') != -1) { @@ -13,10 +13,7 @@ $app->get('/skins/{nickname}', function ($nickname) use ($app) { // TODO: восстановить функцию деградации скинов - $skin = Skins::findFirst(array(array( - 'nickname' => mb_convert_case($nickname, MB_CASE_LOWER, ENCODING) - ))); - + $skin = Skins::findByNickname($nickname); if (!$skin || $skin->skinId == 0) { return $app->response->redirect('http://skins.minecraft.net/MinecraftSkins/' . $nickname . '.png', true); } @@ -34,33 +31,30 @@ $app->get('/cloaks/{nickname}', function ($nickname) use ($app) { }); $app->get('/textures/{nickname}', function($nickname) use ($app) { - $skin = Skins::findFirst(array(array( - 'nickname' => mb_convert_case($nickname, MB_CASE_LOWER, ENCODING) - ))); - + $skin = Skins::findByNickname($nickname); if ($skin && $skin->skinId != 0) { $url = $skin->url; $hash = $skin->hash; } else { $url = 'http://skins.minecraft.net/MinecraftSkins/'.$nickname.'.png'; - $hash = md5('non-ely-'.mktime(date('H'), 0, 0).'-'.$nickname); + $hash = md5('non-ely-' . mktime(date('H'), 0, 0) . '-' . $nickname); } - $textures = array( - 'SKIN' => array( + // TODO: в authserver.ely.by есть готовый класс для работы с форматом текстур. Так что если мы его вынесем в + // common library, то нужно будет заменить его здесь + + $textures = [ + 'SKIN' => [ 'url' => $url, 'hash' => $hash, - 'metadata' => array( - 'model' => ($skin && $skin->isSlim) ? 'slim' : 'default' - ) - ), - 'CAPE' => array( - 'url' => '', - 'hash' => '' - ) - ); + ], + ]; - return $app->response->setJsonContent($textures); + if ($skin && $skin->isSlim) { + $textures['SKIN']['metadata']['model'] = 'slim'; + } + + return $app->response->setContentType('application/json')->setJsonContent($textures); }); $app->post('/system/setSkin', function() use ($app) { @@ -72,10 +66,7 @@ $app->post('/system/setSkin', function() use ($app) { $request = $app->request; $nickname = mb_convert_case($request->getPost('nickname', 'string'), MB_CASE_LOWER, ENCODING); - $skin = Skins::findFirst(array(array( - 'nickname' => $nickname - ))); - + $skin = Skins::findByNickname($nickname); if (!$skin) { $skin = new Skins(); $skin->nickname = $nickname; @@ -88,16 +79,9 @@ $app->post('/system/setSkin', function() use ($app) { $skin->isSlim = (bool) $request->getPost('isSlim', 'int'); $skin->url = $request->getPost('url', 'string'); - if ($skin->save()) { - echo 'OK'; - } else { - echo 'ERROR'; - } + return $app->view->setContent($skin->save() ? 'OK' : 'ERROR'); }); -/** - * Not found handler - */ $app->notFound(function () use ($app) { $app->response ->setStatusCode(404, 'Not Found') diff --git a/config/config.php b/config/config.php index 21704e2..4060a06 100644 --- a/config/config.php +++ b/config/config.php @@ -1,15 +1,15 @@ array( - 'host' => 'localhost', - 'port' => 27017, - 'username' => '', - 'password' => '', - 'dbname' => 'ely_skins', - ), - 'application' => array( - 'modelsDir' => __DIR__ . '/../models/', - 'baseUri' => '/', - ) -)); +return new \Phalcon\Config([ + 'mongo' => [ + 'host' => 'localhost', + 'port' => 27017, + 'username' => '', + 'password' => '', + 'dbname' => 'ely_skins', + ], + 'application' => [ + 'modelsDir' => __DIR__ . '/../models/', + 'baseUri' => '/', + ] +]); diff --git a/config/loader.php b/config/loader.php index 1b18081..eda8dce 100644 --- a/config/loader.php +++ b/config/loader.php @@ -1,4 +1,7 @@ set("view", function () { - $view = new \Phalcon\Mvc\View(); +$di->set('view', function () { + $view = new View(); $view->disable(); return $view; @@ -16,35 +20,27 @@ $di->set("view", function () { /** * The URL component is used to generate all kind of urls in the application */ -$di->set("url", function () use ($config) { +$di->set('url', function () use ($config) { $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); return $url; }); -$di->set("mongo", function() use ($config) { - if (!$config->mongo->username || !$config->mongo->password) { - $mongo = new MongoClient( - "mongodb://". - $config->mongo->host.":". - $config->mongo->port - ); - } else { - $mongo = new MongoClient( - "mongodb://". - $config->mongo->username.":". - $config->mongo->password."@". - $config->mongo->host.":". - $config->mongo->port - ); +$di->set('mongo', function() use ($config) { + /** @var StdClass $mongoConfig */ + $mongoConfig = $config->mongo; + $connectionString = 'mongodb://'; + if ($mongoConfig->username && $mongoConfig->password) { + $connectionString .= "{$mongoConfig->username}:{$mongoConfig->password}@"; } - return $mongo->selectDb($config->mongo->dbname); + $connectionString .= $mongoConfig->host . ':' . $mongoConfig->port; + $mongo = new MongoClient($connectionString); + + return $mongo->selectDb($mongoConfig->dbname); }); -//Registering the collectionManager service $di->setShared('collectionManager', function() { - $modelsManager = new Phalcon\Mvc\Collection\Manager(); - return $modelsManager; -}); \ No newline at end of file + return new Manager(); +}); diff --git a/models/Skins.php b/models/Skins.php index 235730e..07a69cc 100644 --- a/models/Skins.php +++ b/models/Skins.php @@ -3,11 +3,10 @@ use Phalcon\Mvc\Collection; /** - * @method static Skins findFirst() - * * @property string $id */ class Skins extends Collection { + public $_id; public $userId; public $nickname; @@ -22,6 +21,19 @@ class Skins extends Collection { } public function getSource() { - return "skins"; + return 'skins'; } -} \ No newline at end of file + + /** + * @param string $nickname + * @return bool|Skins + */ + public static function findByNickname($nickname) { + return static::findFirst([ + [ + 'nickname' => mb_convert_case($nickname, MB_CASE_LOWER, ENCODING), + ], + ]); + } + +} diff --git a/public/index.php b/public/index.php index ecdaaee..3583edf 100644 --- a/public/index.php +++ b/public/index.php @@ -1,16 +1,18 @@ handle();