Completed
Push — master ( 3d3760...b0d42a )
by John
30s queued 13s
created

app/Helpers/functions.php (3 issues)

Labels
Severity
1
<?php
2
3
use Illuminate\Support\Str;
4
use Illuminate\Support\Carbon;
5
use Illuminate\Support\HtmlString;
6
use Illuminate\Container\Container;
7
use Illuminate\Queue\CallQueuedClosure;
8
use Illuminate\Contracts\Bus\Dispatcher;
9
use Illuminate\Queue\SerializableClosure;
10
use Illuminate\Contracts\Auth\Access\Gate;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Gate. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use Illuminate\Contracts\Support\Responsable;
12
use Illuminate\Contracts\Routing\UrlGenerator;
13
use Illuminate\Foundation\Bus\PendingDispatch;
14
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Response. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
15
use Illuminate\Contracts\Debug\ExceptionHandler;
16
use Illuminate\Contracts\Routing\ResponseFactory;
17
use Illuminate\Contracts\Auth\Factory as AuthFactory;
18
use Illuminate\Contracts\View\Factory as ViewFactory;
19
use Illuminate\Http\Exceptions\HttpResponseException;
0 ignored issues
show
This use statement conflicts with another class in this namespace, HttpResponseException. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
20
use Illuminate\Contracts\Cookie\Factory as CookieFactory;
21
use Symfony\Component\Debug\Exception\FatalThrowableError;
22
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
23
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
24
use Illuminate\Contracts\Broadcasting\Factory as BroadcastFactory;
25
use Illuminate\Support\Facades\DB;
26
use GrahamCampbell\Markdown\Facades\Markdown;
27
use App\Models\Eloquent\Message;
28
use App\Models\Latex\LatexModel;
29
use App\Models\Eloquent\Tool\Theme;
30
use App\Models\Eloquent\Tool\AppSettings;
31
32
if (!function_exists('version')) {
33
    function version()
34
    {
35
        $version=new Version(
36
            '0.0.0',
37
            base_path()
38
        );
39
        return $version->getVersion();
40
    }
41
}
42
43
if (!function_exists('getCustomUrl')) {
44
    function getCustomUrl()
45
    {
46
        $customUrlCached=Cache::tags(['custom'])->get('url');
47
48
        if ($customUrlCached==null) {
49
            $urls=DB::table("custom_url")->where(["available"=>1])->get()->all();
50
            Cache::tags(['custom'])->put('url', $urls, 1200);
51
            return $urls;
52
        }
53
54
        return $customUrlCached;
55
    }
56
}
57
58
if (!function_exists('emailVerified')) {
59
    function emailVerified()
60
    {
61
        if (Auth::guard('web')->check()) {
62
            return !is_null(Auth::guard('web')->user()->email_verified_at);
63
        }
64
65
        return null;
66
    }
67
}
68
69
if (!function_exists('babel_path')) {
70
    /**
71
     * Get the path to the application folder.
72
     *
73
     * @param  string  $path
74
     * @return string
75
     */
76
    function babel_path($path='')
77
    {
78
        return app('path').DIRECTORY_SEPARATOR.'Babel'.($path ? DIRECTORY_SEPARATOR.$path : $path);
79
    }
80
}
81
82
if (!function_exists('glob_recursive')) {
83
    /**
84
     * Find pathnames matching a pattern recursively.
85
     *
86
     * @param  string  $pattern The pattern. No tilde expansion or parameter substitution is done.
87
     * @param  int     $flags   Valid flags: GLOB_MARK
88
     * @return array|false      an array containing the matched files/directories, an empty array if no file matched or false on error.
89
     */
90
    function glob_recursive($pattern, $flags=0)
91
    {
92
        $files=glob($pattern, $flags);
93
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
94
            $files=array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
95
        }
96
        return $files;
97
    }
98
}
99
100
if (!function_exists('adminMenu')) {
101
    function adminMenu()
102
    {
103
        return json_decode(file_get_contents(app_path('Admin/menu.json')), true);
104
    }
105
}
106
107
if (!function_exists('getOpenSearchXML')) {
108
    function getOpenSearchXML()
109
    {
110
        $url=config("app.url");
111
112
        return '<?xml version="1.0" encoding="UTF-8"?>
113
        <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
114
            <ShortName>'.config('app.name').'</ShortName>
115
            <Description>Gracefully Searching '.config('app.displayName').' Problems and others.</Description>
116
            <InputEncoding>UTF-8</InputEncoding>
117
            <Image width="16" height="16" type="image/x-icon">'.$url.'/favicon.ico</Image>
118
            <Url type="text/html" method="get" template="'.$url.'/search/?q={searchTerms}&amp;tab=problems&amp;opensearch=1" />
119
            <moz:SearchForm>'.$url.'/search</moz:SearchForm>
120
        </OpenSearchDescription>';
121
    }
122
}
123
124
if (!function_exists('delFile')) {
125
    function delFile($dirName)
126
    {
127
        if (file_exists($dirName) && $handle=opendir($dirName)) {
128
            while (false!==($item=readdir($handle))) {
129
                if ($item!="." && $item!="..") {
130
                    if (file_exists($dirName.'/'.$item) && is_dir($dirName.'/'.$item)) {
131
                        delFile($dirName.'/'.$item);
132
                    } else {
133
                        if (unlink($dirName.'/'.$item)) {
134
                            return true;
135
                        }
136
                    }
137
                }
138
            }
139
            closedir($handle);
140
        }
141
    }
142
}
143
144
if (!function_exists('convertMarkdownToHtml')) {
145
    function convertMarkdownToHtml($md)
146
    {
147
        return is_string($md) ?Markdown::convertToHtml($md) : '';
148
    }
149
}
150
151
if (!function_exists('sendMessage')) {
152
    function sendMessage($config)
153
    {
154
        return Message::send($config);
155
    }
156
}
157
158
if (!function_exists('formatHumanReadableTime')) {
159
    function formatHumanReadableTime($date)
160
    {
161
        $periods=["second", "minute", "hour", "day", "week", "month", "year", "decade"];
162
        $lengths=["60", "60", "24", "7", "4.35", "12", "10"];
163
164
        $now=time();
165
        $unix_date=strtotime($date);
166
167
        if (empty($unix_date)) {
168
            return __("helper.time.malformatter");
169
        }
170
171
        if ($now>$unix_date) {
172
            $difference=$now-$unix_date;
173
            $tense=__("helper.time.after");
174
        } else {
175
            $difference=$unix_date-$now;
176
            $tense=__("helper.time.before");
177
        }
178
179
        for ($j=0; $difference>=$lengths[$j] && $j<count($lengths)-1; $j++) {
180
            $difference/=$lengths[$j];
181
        }
182
183
        $difference=round($difference);
184
185
        if ($difference!=1) {
186
            $periods[$j]=__("helper.time.plural.$periods[$j]");
187
        } else {
188
            $periods[$j]=__("helper.time.singular.$periods[$j]");
189
        }
190
191
        return __("helper.time.formatter", [
192
            "time" => $difference,
193
            "unit" => $periods[$j],
194
            "tense" => $tense,
195
        ]);
196
    }
197
}
198
199
if (!function_exists('formatProblemSolvedTime')) {
200
    function formatProblemSolvedTime($seconds)
201
    {
202
        if ($seconds>3600) {
203
            $hours=intval($seconds / 3600);
204
            $minutes=$seconds % 3600;
205
            $time=$hours.":".gmstrftime('%M:%S', $minutes);
206
        } else {
207
            $time=gmstrftime('%H:%M:%S', $seconds);
208
        }
209
        return $time;
210
    }
211
}
212
213
if (!function_exists('latex2Image')) {
214
    function latex2Image($content)
215
    {
216
        $callback=function($matches) use (&$patch, &$display) {
217
            [$url, $width, $height]=LatexModel::info("$patch$matches[1]$patch");
218
            return "<img src=\"$url\" style=\"display: $display;\" class=\"rendered-tex\" width=\"$width\" height=\"$height\">";
219
        };
220
        $patch='$';
221
        $display='inline-block';
222
        $content=preg_replace_callback('/\\$\\$\\$(.*?)\\$\\$\\$/', $callback, $content);
223
        $content=preg_replace_callback('/\\\\\\((.*?)\\\\\\)/', $callback, $content);
224
        $patch='$$';
225
        $display='block';
226
        $content=preg_replace_callback('/\\$\\$(.*?)\\$\\$/', $callback, $content);
227
        $content=preg_replace_callback('/\\\\\\[(.*?)\\\\\\]/', $callback, $content);
228
        return $content;
229
    }
230
}
231
232
if (!function_exists('vscodeLocale')) {
233
    function vscodeLocale()
234
    {
235
        $locale=Str::lower(App::getLocale());
236
        $vscodelocale='';
237
        if (in_array($locale, ['de', 'es', 'fr', 'it', 'ja', 'ko', 'ru', 'zh-cn', 'zh-tw'])) {
238
            $vscodelocale=$locale;
239
        }
240
        return $vscodelocale;
241
    }
242
}
243
244
if (!function_exists('getTheme')) {
245
    function getTheme($id=null)
246
    {
247
        if (is_null($id)) {
248
            $id=config('app.theme');
249
        }
250
        return Theme::getTheme($id);
251
    }
252
}
253
254
if (!function_exists('setting')) {
255
    function setting($identifier, $default=null)
256
    {
257
        if (is_array($identifier)) {
258
            foreach ($identifier as $key=>$content) {
259
                AppSettings::set($key, $content);
260
            }
261
            return true;
262
        }
263
        return AppSettings::get($identifier, $default);
264
    }
265
}
266