示例#1
0
 /**
  * Run cron tasks. This is a simple implementation without
  * any bells and whistles.
  *
  * @static
  * @return void
  */
 public static function run()
 {
     // We run the cron tasks once every 5 minutes
     Cache::remember('site.cron', 5, function () {
         $expired = array();
         $storage = storage_path();
         // Retrieve expired pastes
         $pastes = Paste::where('expire', '>', 0)->where('expire', '<', time())->get();
         if ($pastes->count() > 0) {
             // Check if the comments table exists
             $hasComments = Schema::hasTable('comments');
             // Build the expired pastes array
             // Also delete associated comments and attachments
             foreach ($pastes as $paste) {
                 $expired[] = $paste->urlkey;
                 $paste->comments()->delete();
                 $attachment = "{$storage}/uploads/{$paste->urlkey}";
                 if ($paste->attachment and File::exists($attachment)) {
                     File::delete($attachment);
                 }
             }
             // Remove expired pastes
             Paste::whereIn('urlkey', $expired)->delete();
             // Remove expired revisions
             Revision::whereIn('urlkey', $expired)->delete();
         }
         // Delete paste statistics older than configured age
         $ttl = Site::config('general')->statsTTL;
         $date = date('Y-m-d', strtotime($ttl));
         Statistics::where('date', '<', $date)->delete();
         // Crun run successfully
         return TRUE;
     });
 }
示例#2
0
 /**
  * Return flags indicating whether each filter is
  * enabled or disabled
  *
  * @static
  * @return object
  */
 public static function flags()
 {
     return Cache::rememberForever('antispam.flags', function () {
         $flags = new stdClass();
         $services = Antispam::services();
         // Fetching all enabled filters. This value can be defined
         // from the antispam screen in the admin panel
         $enabled = preg_split('/\\||,/', Site::config('antispam')->services);
         foreach ($services as $service) {
             $flags->{$service} = in_array($service, $enabled);
         }
         return $flags;
     });
 }
示例#3
0
 /**
  * Generates a navigation menu
  *
  * @access public
  * @param  string  $menu
  * @return string
  */
 public static function menu($menu)
 {
     // Current path - will be used to highlight menu item
     $path = Request::path();
     // Current user ID for role based menus
     $user = Auth::check() ? Auth::user()->id : 0;
     // Get current project name
     $project = System::project();
     // Grab and parse all the menus
     $group = Config::get("menus.{$menu}");
     // The cache key is not only menu and path specific but also
     // unique for a user and a project
     $cacheKey = "site.menu.{$menu}.{$path}.{$user}.{$project}";
     // Build the menu items. Items are cached for 60 minutes
     $output = Cache::remember($cacheKey, 60, function () use($path, $user, $group) {
         $output = NULL;
         foreach ($group as $key => $item) {
             if (!str_contains($key, '_')) {
                 $label = Lang::get($item['label']);
                 $current = FALSE;
                 // Check if visibility of the item is bound
                 if (isset($item['visible'])) {
                     $visible = FALSE;
                     $bindings = preg_split('/\\||,/', $item['visible']);
                     // Iterate through each binding
                     foreach ($bindings as $binding) {
                         $components = explode('.', $binding);
                         // Check for the invert flag
                         if (starts_with($components[0], '!')) {
                             $components[0] = substr($components[0], 1);
                             $invert = TRUE;
                         } else {
                             $invert = FALSE;
                         }
                         // Check for a value
                         if (str_contains($components[1], '=')) {
                             $expression = explode('=', $components[1]);
                             $components[1] = $expression[0];
                             $value = $expression[1];
                         } else {
                             $value = TRUE;
                         }
                         // Get the binding flags
                         switch ($components[0]) {
                             case 'role':
                                 $flags = Auth::roles();
                                 break;
                             case 'config':
                                 $flags = Site::config('general');
                                 break;
                             default:
                                 $flags = NULL;
                                 break;
                         }
                         // Do not parse the menu item if the flag does not
                         // evaluate to true
                         if (!is_null($flags)) {
                             $visible = ($visible or $flags->{$components}[1] == $value xor $invert);
                         }
                     }
                     // Set the visibility of the item
                     if (!$visible) {
                         continue;
                     }
                 }
                 // Determine whether this is the active link
                 if ($group['_exact'] and $key === $path) {
                     $current = TRUE;
                 } else {
                     if (!$group['_exact'] and starts_with($path, $key)) {
                         $current = TRUE;
                     }
                 }
                 // Highlight the active item
                 if ($current) {
                     $active = 'class="active"';
                     $href = '';
                 } else {
                     $active = '';
                     $href = 'href="' . url($key) . '"';
                 }
                 // Set the entry icon
                 if (isset($item['icon'])) {
                     $icon = View::make('common/icon', array('icon' => $item['icon']), FALSE);
                 } else {
                     $icon = NULL;
                 }
                 // Generate the item markup
                 $output .= "<li {$active}><a {$href}>{$icon} {$label}</a></li>";
             }
         }
         // Add login/logout link if menu is set for that
         if ($group['_showLogin']) {
             if ($user) {
                 $label = Lang::get('global.logout');
                 $href = 'href="' . url('user/logout') . '"';
             } else {
                 $label = Lang::get('global.login');
                 $href = 'href="' . url('user/login') . '"';
             }
             // Are we on the login screen?
             $active = $path == 'user/login' ? 'class="active"' : '';
             $icon = View::make('common/icon', array('icon' => 'user'), FALSE);
             // Generate the markup
             $output .= "<li {$active}><a {$href}>{$icon} {$label}</a></li>";
         }
         return $output;
     });
     return $output;
 }
示例#4
0
 /**
  * Processes AJAX requests for upgrades.
  * The updater starts with the passed $version and goes on until the
  * last one.
  *
  * The response is in the following format:
  * <percent-complete>|<next-action>|<status-message>
  *
  * @static
  * @param  string  $version
  * @return string
  */
 public static function update($action)
 {
     // Get the update versions and current scope
     $versions = Config::get('schema.update');
     $versionNames = array_keys($versions);
     // Initialize everything
     if (!Session::has('setup.updating')) {
         Session::put('setup.updating', TRUE);
         Session::forget('setup.messages');
         return "5|{$action}|" . sprintf(Lang::get('setup.process_version'), $action);
     } else {
         if ($action == '~complete') {
             // Set the final stage
             Session::put('setup.stage', 3);
             // Update the version number in the database
             Site::config('general', array('version' => Config::get('app.version')));
             // Flush the cache
             Cache::flush();
             // All done!
             return "100||" . Lang::get('setup.update_complete');
         } else {
             if (array_key_exists($action, $versions)) {
                 try {
                     // In case the exception is not caught
                     static::setHandler('mainProcess');
                     // Scope is the current version being processed
                     $scope = $versions[$action];
                     // Create new tables
                     if (isset($scope['newTables'])) {
                         foreach ($scope['newTables'] as $tableName => $schema) {
                             // Drop the table
                             Schema::dropIfExists($tableName);
                             // Generate schema and create the table
                             Schema::create($tableName, function ($table) use($schema) {
                                 Setup::schema($table, $schema);
                             });
                         }
                     }
                     // Update existing tables
                     if (isset($scope['modifyTables'])) {
                         foreach ($scope['modifyTables'] as $tableName => $schema) {
                             // Generate schema and modify the table
                             Schema::table($tableName, function ($table) use($schema) {
                                 Setup::schema($table, $schema);
                             });
                         }
                     }
                     // Run the closure for this version
                     if (isset($scope['closure'])) {
                         call_user_func($scope['closure']);
                     }
                     // Output the next action in queue
                     return Setup::nextAction($action, $versionNames, 'setup.process_version');
                 } catch (Exception $e) {
                     Session::put('setup.error', $e->getMessage());
                     return '-1||' . Lang::get('setup.error_occurred');
                 }
             }
         }
     }
 }
示例#5
0
 /**
  * Determines the installed state of the system
  *
  * @static
  * @return bool
  */
 public static function installed()
 {
     return Cache::rememberForever('site.installed', function () {
         return Schema::hasTable('main');
     });
 }
示例#6
0
 /**
  * Parses and outputs highlighted code
  *
  * @param  string  $key
  * @param  string  $code
  * @param  string  $language
  * @return string
  */
 public function parse($key, $code, $language)
 {
     $geshi = $this->geshi;
     $parsed = Cache::remember("site.code.{$key}", 45000, function () use($geshi, $code, $language) {
         $geshi->set_source($code);
         $geshi->set_language($language);
         return @$geshi->parse_code($code);
     });
     return $parsed ?: $code;
 }