public function actionDelete() { if (!isset($_POST['imageId'])) { App::instance()->show404(); exit; } /** @var \models\Image $model */ $model = Image::findByID((int) $_POST['imageId']); if (!$model || $model->uid != App::instance()->getUser()->getId()) { App::instance()->show404(); exit; } if ($model->delete()) { $response['error'] = false; } else { $response = json_encode(['error' => true]); } if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 'XMLHttpRequest' === $_SERVER['HTTP_X_REQUESTED_WITH']) { $response['totalCount'] = Image::countByProp('uid', App::instance()->getUser()->getId()); echo json_encode($response); } else { $this->redirect('/site/index/'); echo $response; } }
public function actionLogout() { if (!App::instance()->isGuest()) { App::instance()->logoutUser(); } $this->redirect('/site/index/'); }
/** * Returns the singleton instance of this class. * * @return DatabaseService */ public static function instance() { if (!isset(self::$instance)) { $db = App::instance()->config['db']; self::$instance = new \PDO('mysql:host=' . $db['host'] . ';dbname=' . $db['name'], $db['username'], $db['password'], $db['options']); } return self::$instance; }
/** * Constructor */ public function __construct() { //create new App instance if no one $this->_connection = App::instance()->getDb(); $currencyDbName = Processor::$currencyDbInfo['tableName']; $sql = "SELECT id ,pair FROM {$currencyDbName}"; $stmnt = $this->_connection->query($sql); $results = $stmnt->fetchAll(PDO::FETCH_ASSOC); foreach ($results as $result) { $this->_currencyPairs[$result['pair']] = (int) $result['id']; } }
/** * @param BaseModel $model * @param $propName */ public static function isUnique(BaseModel $model, $propName, $scenario = []) { if (isset($model->{$propName}) && (empty($scenario) || in_array($model->scenario, $scenario))) { /** @var PDO $connection */ $connection = App::instance()->getDB(); $modelTable = $model::tableName(); $sql = "SELECT COUNT(id) AS cnt FROM {$modelTable} WHERE {$propName} = :value"; $stmnt = $connection->prepare($sql); $stmnt->execute([':value' => $model->{$propName}]); $result = $stmnt->fetch(PDO::FETCH_ASSOC); if (false !== $result && 0 == $result['cnt']) { return; } $model->addErrorMsg($propName, 'Already exists.'); } }
/** * Constructor */ public function __construct() { $this->_connection = App::instance()->getDb(); $tableName = static::$periodsDbInfo['tableName']; $sql = "SELECT id, value FROM {$tableName}"; $stmnt = $this->_connection->query($sql); $results = $stmnt->fetchAll(PDO::FETCH_ASSOC); foreach ($results as $result) { $this->_periods[(int) $result['id']] = (int) $result['value']; } $currencyDbName = static::$currencyDbInfo['tableName']; $sql = "SELECT id ,pair FROM {$currencyDbName}"; $stmnt = $this->_connection->query($sql); $results = $stmnt->fetchAll(PDO::FETCH_ASSOC); foreach ($results as $result) { $this->_currencyPairs[$result['pair']] = (int) $result['id']; } }
public function getBtceAvgData($periodId, $dateStart = null, $dateEnd = null) { $periodId = (int) $periodId; if (!in_array($periodId, array_keys($this->_periods))) { throw new \Exception('Parameter error. Wrong period ID.'); } $connection = App::instance()->getDb(); $tableName = Processor::$btceDbInfo['statTableName']; $currencytableName = Processor::$currencyDbInfo['tableName']; $tsQuery = ''; $tsQuery .= $dateStart ? ' AND ts >= ' . (int) $dateStart : ''; $tsQuery .= $dateEnd ? ' AND ts <= ' . (int) $dateEnd : ''; $sql = "SELECT cp.name AS name, ts, ask, bid, high, low, avg_val, vol, vol_cur FROM {$tableName} AS st\n LEFT JOIN {$currencytableName} AS cp ON cp.id = st.pair_id\n WHERE st.period_id = {$periodId} {$tsQuery}"; $stmnt = $connection->query($sql); if (!$stmnt) { return []; } $result = $stmnt->fetchAll(PDO::FETCH_ASSOC); return $result; }
<?php /** * Site index file */ namespace web; use app\App; include '../app/autoloader.php'; App::instance()->init(); $requestedPath = isset($_GET['requestedPath']) ? $_GET['requestedPath'] : ''; App::instance()->route($requestedPath);
/** * @return bool */ public function login() { $dbUser = User::findByProp('email', $this->email); if (isset($dbUser) && $this->generatePasswordHash($this->password) === $dbUser->passwordHash) { //change user App for dbUser App::instance()->setUser($dbUser); return true; } $this->addErrorMsg('email', 'Unknown email or password. Try again.'); return false; }
<?php /** * @var app\View $this */ use app\App; ?> <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title><?php echo isset($this->title) ? $this->title : App::instance()->appName; ?> </title> <link rel="stylesheet" href="/css/bootstrap.min.css"> <link rel="stylesheet" href="/css/site.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> <script type="text/javascript" src="/js/bootstrap.min.js"></script> </head> <body> <div id="content-container"> <?php echo $this->content; ?> <div class="clearfix"></div> </div> <div class="footer"> <span><?php echo date('Y'); ?>
/** * @return string */ public function getUserImgDirectory() { $path = str_replace('/', DIRECTORY_SEPARATOR, App::instance()->getWebRootPath() . '/' . $this->getUserImgDirectoryRel()); if (!file_exists($path)) { mkdir($path, 0777, true); } return $path; }
/** * Render "Method Not Allowed" error response. * * @param $method String - Requested method * @param $url String - Requested URI. */ public static function methodNotAllowed($method, $url) { self::setHeaders(405); $errorMessage = ['error' => 'Method Not Allowed', 'message' => 'The requested method ' . $method . ' is not allowed for the URL ' . $url . '.']; echo json_encode($errorMessage, App::instance()->config['json_options']); exit; }
<?php /** * @var app\View $this * @var models\Image $image */ use app\App; $this->title = "Gallery: Add new image."; ?> <div class="container col-sm-8 col-sm-offset-2"> <div id="upload-inner-container"> <h1><?php echo App::instance()->getUser()->name; ?> 's Gallery: add new image.</h1> <hr> <form action="/site/upload/" method="POST" enctype="multipart/form-data"> <div class="row"> <div class="form-group<?php echo $image->hasErrors('imageFile') ? ' has-error' : ''; ?> "> <label class="control-label" for="image-file">File:</label> <input type="file" class="form-control" name="Image[imageFile]" id="image-file" accept="image/*"> </div> <?php if ($image->hasErrors('imageFile')) { ?> <?php foreach ($image->getErrorMsg('imageFile') as $msg) { ?>
<?php $config = (require_once __DIR__ . '/app/config/config.php'); require_once __DIR__ . '/app/services/AutoLoaderService.php'; require_once __DIR__ . '/app/App.php'; use App\App; $app = App::instance(); $app->run($config);
public static function countByProp($propName, $propVal) { if (!is_string($propName)) { throw new \Exception('PropName type should be string!'); } if (!property_exists(get_called_class(), $propName)) { throw new \Exception('Property "' . $propName . '" not found in ' . get_called_class() . '!'); } if (!in_array($propName, static::$dbProperties)) { throw new \Exception('Property "' . $propName . '" should be in "static::$dbProperties" array!'); } $tableName = static::tableName(); $sql = "SELECT COUNT(id) FROM {$tableName}\n WHERE {$propName} = :{$propName}\n LIMIT 1"; $connection = App::instance()->getDB(); $stmnt = $connection->prepare($sql); $stmnt->bindValue(":{$propName}", $propVal); $stmnt->setFetchMode(PDO::FETCH_NUM); $stmnt->execute(); $result = $stmnt->fetch(); if (isset($result[0])) { return $result[0]; } return null; }