Document MySQL setup for websub
[phinde.git] / src / phinde / Helper.php
1 <?php
2 namespace phinde;
3
4 class Helper
5 {
6     public static function isUrlAllowed($url)
7     {
8         $urlDomain = parse_url($url, PHP_URL_HOST);
9         if (!in_array($urlDomain, $GLOBALS['phinde']['domains'])) {
10             return false;
11         }
12         return true;
13     }
14
15     public static function noSchema($url)
16     {
17         return str_replace(
18             array('http://', 'https://'),
19             '',
20             $url
21         );
22     }
23
24     public static function addSchema($url)
25     {
26         if (substr($url, 0, 7) == 'http://'
27             || substr($url, 0, 8) == 'https://'
28         ) {
29             return $url;
30         }
31         return 'http://' . $url;
32     }
33
34     public static function removeAnchor($url)
35     {
36         $parts = explode('#', $url, 2);
37         return $parts[0];
38     }
39
40     public static function sanitizeTitle($str)
41     {
42         return trim(
43             str_replace(
44                 array("\r", "\n", '  ', '  '),
45                 array('', ' ', ' ', ' '),
46                 $str
47             )
48         );
49     }
50
51     /**
52      * Create a full URL with protocol and host name
53      *
54      * @param string $path Path to the file, with leading /
55      *
56      * @return string Full URL
57      */
58     public static function fullUrl($path = '/')
59     {
60         if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
61             $prot = 'https';
62         } else {
63             $prot = 'http';
64         }
65         return $prot . '://' . $_SERVER['HTTP_HOST'] . $path;
66     }
67
68     static $timer = [];
69
70     public static function start($timer = 'timer')
71     {
72         static::$timer[$timer] = microtime(true);
73     }
74
75     public static function stop($timer = 'timer')
76     {
77         $diff = microtime(true) - static::$timer[$timer];
78         echo '+timer: ' . number_format($diff, 3) . 'ms ' . $timer . "\n";
79     }
80
81     public static function baseDoc($url)
82     {
83         $esDoc = new \stdClass();
84         $esDoc->status = new \stdClass();
85         $esDoc->url = $url;
86         $esDoc->schemalessUrl = Helper::noSchema($url);
87         return $esDoc;
88     }
89 }
90 ?>