luolongfei /
freenom
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * 助手函数 |
||
| 4 | * |
||
| 5 | * @author mybsdc <[email protected]> |
||
| 6 | * @date 2019/3/3 |
||
| 7 | * @time 16:34 |
||
| 8 | */ |
||
| 9 | |||
| 10 | use Luolongfei\App\Exceptions\LlfException; |
||
| 11 | use Luolongfei\Lib\Argv; |
||
| 12 | use Luolongfei\Lib\Config; |
||
| 13 | use Luolongfei\Lib\Log; |
||
| 14 | use Luolongfei\Lib\Env; |
||
| 15 | use Luolongfei\Lib\Lang; |
||
| 16 | use Luolongfei\Lib\PhpColor; |
||
| 17 | |||
| 18 | if (!function_exists('config')) { |
||
| 19 | /** |
||
| 20 | * 获取配置 |
||
| 21 | * |
||
| 22 | * @param string $key 键,支持点式访问 |
||
| 23 | * @param string $default 默认值 |
||
| 24 | * |
||
| 25 | * @return array|mixed |
||
| 26 | */ |
||
| 27 | function config($key = '', $default = null) |
||
| 28 | { |
||
| 29 | return Config::instance()->get($key, $default); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | if (!function_exists('lang')) { |
||
| 34 | /** |
||
| 35 | * 读取语言包 |
||
| 36 | * |
||
| 37 | * @param string $key 键,支持点式访问 |
||
| 38 | * |
||
| 39 | * @return array|mixed|null |
||
| 40 | */ |
||
| 41 | function lang($key = '') |
||
| 42 | { |
||
| 43 | return Lang::instance()->get($key); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!function_exists('system_log')) { |
||
| 48 | /** |
||
| 49 | * 写日志 |
||
| 50 | * |
||
| 51 | * @param $content |
||
| 52 | * @param array $response |
||
| 53 | * @param string $fileName |
||
| 54 | * @description 受支持的着色标签 |
||
| 55 | * 'reset', 'bold', 'dark', 'italic', 'underline', 'blink', 'reverse', 'concealed', 'default', 'black', 'red', |
||
| 56 | * 'green', 'yellow', 'blue', 'magenta', 'cyan', 'light_gray', 'dark_gray', 'light_red', 'light_green', |
||
| 57 | * 'light_yellow', 'light_blue', 'light_magenta', 'light_cyan', 'white', 'bg_default', 'bg_black', 'bg_red', |
||
| 58 | * 'bg_green', 'bg_yellow', 'bg_blue', 'bg_magenta', 'bg_cyan', 'bg_light_gray', 'bg_dark_gray', 'bg_light_red', |
||
| 59 | * 'bg_light_green','bg_light_yellow', 'bg_light_blue', 'bg_light_magenta', 'bg_light_cyan', 'bg_white' |
||
| 60 | */ |
||
| 61 | function system_log($content, array $response = [], $fileName = '') |
||
| 62 | { |
||
| 63 | try { |
||
| 64 | $path = sprintf('%s/logs/%s/', ROOT_PATH, date('Y-m')); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 65 | $file = $path . ($fileName ?: date('d')) . '.log'; |
||
| 66 | |||
| 67 | if (!is_dir($path)) { |
||
| 68 | mkdir($path, 0777, true); |
||
| 69 | chmod($path, 0777); |
||
| 70 | } |
||
| 71 | |||
| 72 | $handle = fopen($file, 'a'); // 追加而非覆盖 |
||
| 73 | |||
| 74 | if (!filesize($file)) { |
||
| 75 | chmod($file, 0666); |
||
| 76 | } |
||
| 77 | |||
| 78 | $msg = sprintf( |
||
| 79 | "[%s] %s %s\n", |
||
| 80 | date('Y-m-d H:i:s'), |
||
| 81 | is_string($content) ? $content : json_encode($content), |
||
| 82 | $response ? json_encode($response, JSON_UNESCAPED_UNICODE) : ''); |
||
| 83 | |||
| 84 | // 在 Github Actions 上运行,过滤敏感信息 |
||
| 85 | if (env('ON_GITHUB_ACTIONS')) { |
||
| 86 | $msg = preg_replace_callback('/(?P<secret>[\w-.]{1,4}?)(?=@[\w-.]+)/i', function ($m) { |
||
| 87 | return str_ireplace($m['secret'], str_repeat('*', strlen($m['secret'])), $m['secret']); |
||
| 88 | }, $msg); |
||
| 89 | } |
||
| 90 | |||
| 91 | // 尝试为消息着色 |
||
| 92 | $c = PhpColor::instance()->getColorInstance(); |
||
| 93 | echo $c($msg)->colorize(); |
||
| 94 | |||
| 95 | // 干掉着色标签 |
||
| 96 | $msg = strip_tags($msg); // 不完整或者破损标签将导致更多的数据被删除 |
||
| 97 | |||
| 98 | fwrite($handle, $msg); |
||
| 99 | fclose($handle); |
||
| 100 | |||
| 101 | flush(); |
||
| 102 | } catch (\Exception $e) { |
||
| 103 | // do nothing |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!function_exists('is_locked')) { |
||
| 109 | /** |
||
| 110 | * 检查任务是否已被锁定 |
||
| 111 | * |
||
| 112 | * @param string $taskName |
||
| 113 | * @param bool $always 是否被永久锁定 |
||
| 114 | * |
||
| 115 | * @return bool |
||
| 116 | * @throws Exception |
||
| 117 | */ |
||
| 118 | function is_locked($taskName = '', $always = false) |
||
| 119 | { |
||
| 120 | try { |
||
| 121 | $lock = sprintf( |
||
| 122 | '%s/num_limit/%s/%s.lock', |
||
| 123 | APP_PATH, |
||
|
0 ignored issues
–
show
|
|||
| 124 | $always ? 'always' : date('Y-m-d'), |
||
| 125 | $taskName |
||
| 126 | ); |
||
| 127 | |||
| 128 | return file_exists($lock); |
||
| 129 | } catch (\Exception $e) { |
||
| 130 | system_log(sprintf('检查任务%s是否锁定时出错,错误原因:%s', $taskName, $e->getMessage())); |
||
| 131 | } |
||
| 132 | |||
| 133 | return false; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | if (!function_exists('lock_task')) { |
||
| 138 | /** |
||
| 139 | * 锁定任务 |
||
| 140 | * |
||
| 141 | * 防止重复执行 |
||
| 142 | * |
||
| 143 | * @param string $taskName |
||
| 144 | * @param bool $always 是否永久锁定 |
||
| 145 | * |
||
| 146 | * @return bool |
||
| 147 | */ |
||
| 148 | function lock_task($taskName = '', $always = false) |
||
| 149 | { |
||
| 150 | try { |
||
| 151 | $lock = sprintf( |
||
| 152 | '%s/num_limit/%s/%s.lock', |
||
| 153 | APP_PATH, |
||
|
0 ignored issues
–
show
|
|||
| 154 | $always ? 'always' : date('Y-m-d'), |
||
| 155 | $taskName |
||
| 156 | ); |
||
| 157 | |||
| 158 | $path = dirname($lock); |
||
| 159 | if (!is_dir($path)) { |
||
| 160 | mkdir($path, 0777, true); |
||
| 161 | chmod($path, 0777); |
||
| 162 | } |
||
| 163 | |||
| 164 | if (file_exists($lock)) { |
||
| 165 | return true; |
||
| 166 | } |
||
| 167 | |||
| 168 | $handle = fopen($lock, 'a'); // 追加而非覆盖 |
||
| 169 | |||
| 170 | if (!filesize($lock)) { |
||
| 171 | chmod($lock, 0666); |
||
| 172 | } |
||
| 173 | |||
| 174 | fwrite($handle, sprintf( |
||
| 175 | "Locked at %s.\n", |
||
| 176 | date('Y-m-d H:i:s') |
||
| 177 | ) |
||
| 178 | ); |
||
| 179 | |||
| 180 | fclose($handle); |
||
| 181 | |||
| 182 | Log::info(sprintf('%s已被锁定,此任务%s已不会再执行,请知悉', $taskName, $always ? '' : '今天内')); |
||
| 183 | } catch (\Exception $e) { |
||
| 184 | system_log(sprintf('创建锁定任务文件%s时出错,错误原因:%s', $lock, $e->getMessage())); |
||
| 185 | |||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | return true; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if (!function_exists('env')) { |
||
| 194 | /** |
||
| 195 | * 获取环境变量值 |
||
| 196 | * |
||
| 197 | * @param string $key |
||
| 198 | * @param string $default 默认值 |
||
| 199 | * |
||
| 200 | * @return array | bool | false | null | string |
||
| 201 | */ |
||
| 202 | function env($key = '', $default = null) |
||
| 203 | { |
||
| 204 | return Env::instance()->get($key, $default); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | if (!function_exists('get_argv')) { |
||
| 209 | /** |
||
| 210 | * 获取命令行传参 |
||
| 211 | * |
||
| 212 | * @param string $name |
||
| 213 | * @param string $default 默认值 |
||
| 214 | * |
||
| 215 | * @return mixed|string |
||
| 216 | */ |
||
| 217 | function get_argv(string $name, string $default = '') |
||
| 218 | { |
||
| 219 | return Argv::instance()->get($name, $default); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | if (!function_exists('system_check')) { |
||
| 224 | /** |
||
| 225 | * 检查环境是否满足要求 |
||
| 226 | * |
||
| 227 | * @throws LlfException |
||
| 228 | */ |
||
| 229 | function system_check() |
||
| 230 | { |
||
| 231 | if (!function_exists('putenv')) { |
||
| 232 | throw new LlfException(34520005); |
||
| 233 | } |
||
| 234 | |||
| 235 | if (version_compare(PHP_VERSION, '7.0.0') < 0) { |
||
| 236 | throw new LlfException(34520006); |
||
| 237 | } |
||
| 238 | |||
| 239 | $envFile = ROOT_PATH . '/.env'; |
||
|
0 ignored issues
–
show
|
|||
| 240 | if (!file_exists($envFile)) { |
||
| 241 | throw new LlfException(copy(ROOT_PATH . '/.env.example', $envFile) ? 34520007 : 34520008); |
||
| 242 | } |
||
| 243 | |||
| 244 | if (!extension_loaded('curl')) { |
||
| 245 | throw new LlfException(34520010); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |