| 
									
										
										
										
											2015-12-17 01:09:44 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-02 03:11:49 +08:00
										 |  |  | use BookStack\Ownable; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 18:27:23 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Get the path to a versioned file. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param  string $file | 
					
						
							|  |  |  |  * @return string | 
					
						
							| 
									
										
										
										
											2016-09-03 19:08:58 +08:00
										 |  |  |  * @throws Exception | 
					
						
							| 
									
										
										
										
											2016-08-27 18:27:23 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-09-03 19:08:58 +08:00
										 |  |  | function versioned_asset($file = '') | 
					
						
							| 
									
										
										
										
											2016-08-27 18:27:23 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-10-31 01:44:00 +08:00
										 |  |  |     static $version = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (is_null($version)) { | 
					
						
							|  |  |  |         $versionFile = base_path('version'); | 
					
						
							|  |  |  |         $version = trim(file_get_contents($versionFile)); | 
					
						
							| 
									
										
										
										
											2016-08-27 18:27:23 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-17 01:09:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-31 01:44:00 +08:00
										 |  |  |     $additional = ''; | 
					
						
							|  |  |  |     if (config('app.env') === 'development') { | 
					
						
							|  |  |  |         $additional = sha1_file(public_path($file)); | 
					
						
							| 
									
										
										
										
											2016-08-27 18:27:23 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-17 01:09:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-31 01:44:00 +08:00
										 |  |  |     $path = $file . '?version=' . urlencode($version) . $additional; | 
					
						
							|  |  |  |     return baseUrl($path); | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-29 19:43:46 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Helper method to get the current User. | 
					
						
							|  |  |  |  * Defaults to public 'Guest' user if not logged in. | 
					
						
							|  |  |  |  * @return \BookStack\User | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function user() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return auth()->user() ?: \BookStack\User::getDefault(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 05:19:29 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Check if current user is a signed in user. | 
					
						
							|  |  |  |  * @return bool | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function signedInUser() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return auth()->user() && !auth()->user()->isDefault(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Check if the current user has a permission. | 
					
						
							| 
									
										
										
										
											2016-05-02 04:20:50 +08:00
										 |  |  |  * If an ownable element is passed in the jointPermissions are checked against | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |  * that particular item. | 
					
						
							|  |  |  |  * @param $permission | 
					
						
							| 
									
										
										
										
											2016-07-02 03:11:49 +08:00
										 |  |  |  * @param Ownable $ownable | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |  * @return mixed | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-07-02 03:11:49 +08:00
										 |  |  | function userCan($permission, Ownable $ownable = null) | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     if ($ownable === null) { | 
					
						
							| 
									
										
										
										
											2016-09-29 19:43:46 +08:00
										 |  |  |         return user() && user()->can($permission); | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-01 04:31:21 +08:00
										 |  |  |     // Check permission on ownable item
 | 
					
						
							| 
									
										
										
										
											2016-07-02 03:11:49 +08:00
										 |  |  |     $permissionService = app(\BookStack\Services\PermissionService::class); | 
					
						
							|  |  |  |     return $permissionService->checkOwnableUserAccess($ownable, $permission); | 
					
						
							| 
									
										
										
										
											2016-03-06 20:55:08 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Helper to access system settings. | 
					
						
							|  |  |  |  * @param $key | 
					
						
							|  |  |  |  * @param bool $default | 
					
						
							| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  |  * @return bool|string|\BookStack\Services\SettingService | 
					
						
							| 
									
										
										
										
											2016-03-06 20:55:08 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  | function setting($key = null, $default = false) | 
					
						
							| 
									
										
										
										
											2016-03-06 20:55:08 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-06 02:57:57 +08:00
										 |  |  |     $settingService = resolve(\BookStack\Services\SettingService::class); | 
					
						
							| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  |     if (is_null($key)) return $settingService; | 
					
						
							| 
									
										
										
										
											2016-03-06 20:55:08 +08:00
										 |  |  |     return $settingService->get($key, $default); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 00:56:20 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Helper to create url's relative to the applications root path. | 
					
						
							| 
									
										
										
										
											2016-08-21 21:49:40 +08:00
										 |  |  |  * @param string $path | 
					
						
							|  |  |  |  * @param bool $forceAppDomain | 
					
						
							| 
									
										
										
										
											2016-08-14 00:56:20 +08:00
										 |  |  |  * @return string | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-08-21 21:49:40 +08:00
										 |  |  | function baseUrl($path, $forceAppDomain = false) | 
					
						
							| 
									
										
										
										
											2016-08-14 00:56:20 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-08-21 21:49:40 +08:00
										 |  |  |     $isFullUrl = strpos($path, 'http') === 0; | 
					
						
							|  |  |  |     if ($isFullUrl && !$forceAppDomain) return $path; | 
					
						
							| 
									
										
										
										
											2016-08-14 00:56:20 +08:00
										 |  |  |     $path = trim($path, '/'); | 
					
						
							| 
									
										
										
										
											2016-08-21 21:49:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |     // Remove non-specified domain if forced and we have a domain
 | 
					
						
							| 
									
										
										
										
											2016-08-21 21:49:40 +08:00
										 |  |  |     if ($isFullUrl && $forceAppDomain) { | 
					
						
							|  |  |  |         $explodedPath = explode('/', $path); | 
					
						
							|  |  |  |         $path = implode('/', array_splice($explodedPath, 3)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-18 01:22:04 +08:00
										 |  |  |     // Return normal url path if not specified in config
 | 
					
						
							|  |  |  |     if (config('app.url') === '') { | 
					
						
							|  |  |  |         return url($path); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 00:56:20 +08:00
										 |  |  |     return rtrim(config('app.url'), '/') . '/' . $path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 19:29:35 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Get an instance of the redirector. | 
					
						
							|  |  |  |  * Overrides the default laravel redirect helper. | 
					
						
							|  |  |  |  * Ensures it redirects even when the app is in a subdirectory. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param  string|null  $to | 
					
						
							|  |  |  |  * @param  int     $status | 
					
						
							|  |  |  |  * @param  array   $headers | 
					
						
							|  |  |  |  * @param  bool    $secure | 
					
						
							|  |  |  |  * @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function redirect($to = null, $status = 302, $headers = [], $secure = null) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (is_null($to)) { | 
					
						
							|  |  |  |         return app('redirect'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $to = baseUrl($to); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return app('redirect')->to($to, $status, $headers, $secure); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-04 19:01:49 +08:00
										 |  |  | function icon($name, $attrs = []) { | 
					
						
							|  |  |  |     $iconPath = resource_path('assets/icons/' . $name . '.svg'); | 
					
						
							|  |  |  |     $attrString = ' '; | 
					
						
							|  |  |  |     foreach ($attrs as $attrName => $attr) { | 
					
						
							|  |  |  |         $attrString .=  $attrName . '="' . $attr . '" '; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     $fileContents = file_get_contents($iconPath); | 
					
						
							|  |  |  |     return  str_replace('<svg', '<svg' . $attrString, $fileContents); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Generate a url with multiple parameters for sorting purposes. | 
					
						
							|  |  |  |  * Works out the logic to set the correct sorting direction | 
					
						
							|  |  |  |  * Discards empty parameters and allows overriding. | 
					
						
							|  |  |  |  * @param $path | 
					
						
							|  |  |  |  * @param array $data | 
					
						
							|  |  |  |  * @param array $overrideData | 
					
						
							|  |  |  |  * @return string | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function sortUrl($path, $data, $overrideData = []) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     $queryStringSections = []; | 
					
						
							|  |  |  |     $queryData = array_merge($data, $overrideData); | 
					
						
							| 
									
										
										
										
											2016-10-31 01:44:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  |     // Change sorting direction is already sorted on current attribute
 | 
					
						
							|  |  |  |     if (isset($overrideData['sort']) && $overrideData['sort'] === $data['sort']) { | 
					
						
							|  |  |  |         $queryData['order'] = ($data['order'] === 'asc') ? 'desc' : 'asc'; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         $queryData['order'] = 'asc'; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-10-31 01:44:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  |     foreach ($queryData as $name => $value) { | 
					
						
							|  |  |  |         $trimmedVal = trim($value); | 
					
						
							|  |  |  |         if ($trimmedVal === '') continue; | 
					
						
							|  |  |  |         $queryStringSections[] = urlencode($name) . '=' . urlencode($trimmedVal); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (count($queryStringSections) === 0) return $path; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-14 19:29:35 +08:00
										 |  |  |     return baseUrl($path . '?' . implode('&', $queryStringSections)); | 
					
						
							| 
									
										
										
										
											2017-02-04 19:01:49 +08:00
										 |  |  | } |