2015-04-08 15:19:17 +02:00
|
|
|
<?php namespace app\Http\Middleware;
|
2015-03-17 02:30:56 +01:00
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use Utils;
|
|
|
|
use App;
|
2015-03-23 07:52:01 +01:00
|
|
|
use Auth;
|
|
|
|
use Input;
|
|
|
|
use Redirect;
|
2015-03-30 21:45:10 +02:00
|
|
|
use Cache;
|
2015-04-02 15:06:16 +02:00
|
|
|
use Session;
|
|
|
|
use Event;
|
2015-04-14 05:22:33 +02:00
|
|
|
use App\Models\Language;
|
2015-04-15 18:35:41 +02:00
|
|
|
use App\Events\UserSettingsChanged;
|
2015-03-17 02:30:56 +01:00
|
|
|
|
2015-04-08 15:19:17 +02:00
|
|
|
class StartupCheck
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
// Ensure all request are over HTTPS in production
|
|
|
|
if (App::environment() == ENV_PRODUCTION) {
|
|
|
|
if (!Request::secure()) {
|
|
|
|
return Redirect::secure(Request::getRequestUri());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the database doens't yet exist we'll skip the rest
|
|
|
|
if (!Utils::isNinja() && !Utils::isDatabaseSetup()) {
|
|
|
|
return $next($request);
|
2015-03-30 21:45:10 +02:00
|
|
|
}
|
2015-03-17 02:30:56 +01:00
|
|
|
|
2015-04-08 15:19:17 +02:00
|
|
|
// Check data has been cached
|
|
|
|
$cachedTables = [
|
|
|
|
'currencies' => 'App\Models\Currency',
|
|
|
|
'sizes' => 'App\Models\Size',
|
|
|
|
'industries' => 'App\Models\Industry',
|
|
|
|
'timezones' => 'App\Models\Timezone',
|
|
|
|
'dateFormats' => 'App\Models\DateFormat',
|
|
|
|
'datetimeFormats' => 'App\Models\DatetimeFormat',
|
|
|
|
'languages' => 'App\Models\Language',
|
|
|
|
'paymentTerms' => 'App\Models\PaymentTerm',
|
|
|
|
'paymentTypes' => 'App\Models\PaymentType',
|
2015-04-28 22:13:52 +02:00
|
|
|
'countries' => 'App\Models\Country',
|
2015-04-08 15:19:17 +02:00
|
|
|
];
|
|
|
|
foreach ($cachedTables as $name => $class) {
|
|
|
|
if (!Cache::has($name)) {
|
|
|
|
if ($name == 'paymentTerms') {
|
|
|
|
$orderBy = 'num_days';
|
2015-04-28 22:13:52 +02:00
|
|
|
} elseif (in_array($name, ['currencies', 'sizes', 'industries', 'languages', 'countries'])) {
|
2015-04-08 15:19:17 +02:00
|
|
|
$orderBy = 'name';
|
2015-04-14 20:58:07 +02:00
|
|
|
} else {
|
|
|
|
$orderBy = 'id';
|
2015-04-08 15:19:17 +02:00
|
|
|
}
|
|
|
|
Cache::forever($name, $class::orderBy($orderBy)->get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the application is up to date and for any news feed messages
|
|
|
|
if (Auth::check()) {
|
|
|
|
$count = Session::get(SESSION_COUNTER, 0);
|
|
|
|
Session::put(SESSION_COUNTER, ++$count);
|
|
|
|
|
2015-04-30 19:54:19 +02:00
|
|
|
//if (!Utils::startsWith($_SERVER['REQUEST_URI'], '/news_feed') && !Session::has('news_feed_id')) {
|
|
|
|
if (true) {
|
2015-04-08 15:19:17 +02:00
|
|
|
$data = false;
|
|
|
|
if (Utils::isNinja()) {
|
|
|
|
$data = Utils::getNewsFeedResponse();
|
|
|
|
} else {
|
|
|
|
$file = @file_get_contents(NINJA_APP_URL.'/news_feed/'.Utils::getUserType().'/'.NINJA_VERSION);
|
|
|
|
$data = @json_decode($file);
|
|
|
|
}
|
|
|
|
if ($data) {
|
2015-04-30 19:54:19 +02:00
|
|
|
if (version_compare(NINJA_VERSION, $data->version, '<')) {
|
2015-04-08 15:19:17 +02:00
|
|
|
$params = [
|
2015-04-30 19:54:19 +02:00
|
|
|
'user_version' => NINJA_VERSION,
|
|
|
|
'latest_version' => $data->version,
|
|
|
|
'releases_link' => link_to(RELEASES_URL, 'Invoice Ninja', ['target' => '_blank']),
|
|
|
|
];
|
2015-04-08 15:19:17 +02:00
|
|
|
Session::put('news_feed_id', NEW_VERSION_AVAILABLE);
|
|
|
|
Session::put('news_feed_message', trans('texts.new_version_available', $params));
|
|
|
|
} else {
|
|
|
|
Session::put('news_feed_id', $data->id);
|
|
|
|
if ($data->message && $data->id > Auth::user()->news_feed_id) {
|
|
|
|
Session::put('news_feed_message', $data->message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Session::put('news_feed_id', true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-30 21:56:01 +02:00
|
|
|
|
2015-04-08 15:19:17 +02:00
|
|
|
// Check if we're requesting to change the account's language
|
|
|
|
if (Input::has('lang')) {
|
|
|
|
$locale = Input::get('lang');
|
|
|
|
App::setLocale($locale);
|
|
|
|
Session::set(SESSION_LOCALE, $locale);
|
|
|
|
|
|
|
|
if (Auth::check()) {
|
|
|
|
if ($language = Language::whereLocale($locale)->first()) {
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
$account->language_id = $language->id;
|
|
|
|
$account->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif (Auth::check()) {
|
|
|
|
$locale = Session::get(SESSION_LOCALE, DEFAULT_LOCALE);
|
|
|
|
App::setLocale($locale);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the account/user localization settings are in the session
|
|
|
|
if (Auth::check() && !Session::has(SESSION_TIMEZONE)) {
|
2015-03-31 20:50:58 +02:00
|
|
|
Event::fire(new UserSettingsChanged());
|
2015-04-08 15:19:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the user is claiming a license (ie, additional invoices, white label, etc.)
|
|
|
|
$claimingLicense = Utils::startsWith($_SERVER['REQUEST_URI'], '/claim_license');
|
|
|
|
if (!$claimingLicense && Input::has('license_key') && Input::has('product_id')) {
|
|
|
|
$licenseKey = Input::get('license_key');
|
|
|
|
$productId = Input::get('product_id');
|
|
|
|
|
|
|
|
$data = trim(file_get_contents((Utils::isNinjaDev() ? 'http://ninja.dev' : NINJA_APP_URL)."/claim_license?license_key={$licenseKey}&product_id={$productId}"));
|
|
|
|
|
|
|
|
if ($productId == PRODUCT_INVOICE_DESIGNS) {
|
|
|
|
if ($data = json_decode($data)) {
|
|
|
|
foreach ($data as $item) {
|
|
|
|
$design = new InvoiceDesign();
|
|
|
|
$design->id = $item->id;
|
|
|
|
$design->name = $item->name;
|
|
|
|
$design->javascript = $item->javascript;
|
|
|
|
$design->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Utils::isNinjaProd()) {
|
|
|
|
Cache::forget('invoice_designs_cache_'.Auth::user()->maxInvoiceDesignId());
|
|
|
|
}
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.bought_designs'));
|
|
|
|
}
|
|
|
|
} elseif ($productId == PRODUCT_WHITE_LABEL) {
|
|
|
|
if ($data == 'valid') {
|
|
|
|
$account = Auth::user()->account;
|
|
|
|
$account->pro_plan_paid = NINJA_DATE;
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
Session::flash('message', trans('texts.bought_white_label'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-17 02:30:56 +01:00
|
|
|
|
2015-04-08 15:19:17 +02:00
|
|
|
return $next($request);
|
|
|
|
}
|
2015-03-17 02:30:56 +01:00
|
|
|
}
|