This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Laravel Administrator integration (#56)
- Loading branch information
Zsombor Franczia
committed
Sep 11, 2014
1 parent
4d10cb9
commit e04dd1f
Showing
1 changed file
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php namespace kcfinder\integration; | ||
|
||
/** This file is part of KCFinder project | ||
* | ||
* @desc Integration code: Laravel Administrator (https://github.com/FrozenNode/Laravel-Administrator) | ||
* @package KCFinder | ||
* @version 3.12 | ||
* @author Zsombor Franczia <[email protected]> | ||
* @copyright 2010-2014 KCFinder Project | ||
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 | ||
* @license http://opensource.org/licenses/LGPL-3.0 LGPLv3 | ||
* @link http://kcfinder.sunhater.com | ||
*/ | ||
|
||
class LaravelAdministrator { | ||
protected static $authenticated = false; | ||
protected static $bootstrapAutoload = '/bootstrap/autoload.php'; | ||
protected static $bootstrapStart = '/bootstrap/start.php'; | ||
|
||
static function getLaravelPath() { | ||
|
||
//absolute path method | ||
if (!empty($_SERVER['SCRIPT_FILENAME'])) { | ||
$laravelPath = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])))))))); | ||
if (!file_exists($laravelPath . self::$bootstrapAutoload)) { | ||
$laravelPath = dirname(dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])))); | ||
$depth = 3; | ||
do { | ||
$laravelPath = dirname($laravelPath); | ||
$depth++; | ||
$autoloadFound = file_exists($laravelPath . self::$bootstrapAutoload); | ||
} while (!$autoloadFound && $depth < 10); | ||
} | ||
else { | ||
$autoloadFound = true; | ||
} | ||
} | ||
|
||
//relative path method | ||
if (!isset($autoloadFound) || !$autoloadFound) { | ||
$laravelPath = '../../../../../../..'; | ||
if (!file_exists($laravelPath . self::$bootstrapAutoload)) { | ||
$laravelPath = '../../..'; | ||
$depth = 3; | ||
do { | ||
$laravelPath .= '/..'; | ||
$depth++; | ||
} while (!($autoloadFound = file_exists($laravelPath . self::$bootstrapAutoload)) && $depth < 10); | ||
} | ||
else { | ||
$autoloadFound = true; | ||
} | ||
} | ||
|
||
return $laravelPath; | ||
} | ||
|
||
static function runIntegration() { | ||
|
||
$laravelPath = self::getLaravelPath(); | ||
|
||
if(file_exists($laravelPath . '/bootstrap/autoload.php')) { | ||
$currentCwd = getcwd(); | ||
|
||
// Simulate being in the laravel root folder so we can share the session (is this really needed?) | ||
// chdir($laravelPath); | ||
|
||
// bootstrap | ||
require $laravelPath.'/bootstrap/autoload.php'; | ||
$app = require_once $laravelPath.'/bootstrap/start.php'; | ||
$app->boot(); //Boot the application's service providers. | ||
|
||
//get the admin check closure that should be supplied in the admin config | ||
$permission = \Illuminate\Support\Facades\Config::get('administrator::administrator.permission'); | ||
$hasPermission = $permission(); | ||
self::$authenticated = $hasPermission; | ||
|
||
//start session if not started already | ||
if (session_id() == "") { | ||
session_start(); | ||
$iStartedTheSession = true; | ||
} | ||
|
||
if (!isset($_SESSION['KCFINDER'])) { | ||
$_SESSION['KCFINDER'] = array(); | ||
} | ||
|
||
//if this is a simple true value, user is logged in | ||
if ($hasPermission == true) { | ||
|
||
$configFactory = \Illuminate\Support\Facades\App::make('admin_config_factory'); | ||
$modelName = \Illuminate\Support\Facades\Input::get('model'); | ||
$fieldName = \Illuminate\Support\Facades\Input::get('field'); | ||
|
||
if(!empty($modelName) && !empty($fieldName)) { | ||
|
||
$modelConfig = $configFactory->make($modelName, true); | ||
$modelConfigOptions = $modelConfig->getOption('edit_fields'); | ||
$kcfinderOptions = $modelConfigOptions[$fieldName]["kcfinder"]; | ||
|
||
//allow users to use an option called 'enabled' instead of 'disabled' | ||
if(isset($kcfinderOptions["enabled"])) { | ||
$kcfinderOptions["disabled"] = !$kcfinderOptions["enabled"]; | ||
} | ||
|
||
//save all options to the session | ||
foreach ($kcfinderOptions as $optKey => $optValue) { | ||
$_SESSION['KCFINDER'][$optKey] = $optValue; | ||
} | ||
|
||
self::$authenticated = !$_SESSION['KCFINDER']['disabled']; | ||
|
||
} | ||
} | ||
else { | ||
$_SESSION['KCFINDER']['disabled'] = true; | ||
} | ||
|
||
//close the session if I started it | ||
if (isset($iStartedTheSession)) { | ||
session_write_close(); | ||
} | ||
|
||
// chdir($currentCwd); | ||
return self::$authenticated; | ||
} | ||
else { | ||
die("The integration service for 'Laravel Administrator' failed. Laravel root path not found!"); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
\kcfinder\integration\LaravelAdministrator::runIntegration(); |