Passed
Push — main ( 327d19...e70ca5 )
by Julian
05:02 queued 20s
created

setBlockAssetContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
function findFile($name)
4
{
5
    $directory = base_path("/assets/linkstack/images/");
6
    $files = scandir($directory);
7
    $pathinfo = "error.error";
8
    $pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
9
    foreach ($files as $file) {
10
        if (preg_match($pattern, $file)) {
11
            $pathinfo = $file;
12
            break;
13
        }
14
    }
15
    return $pathinfo;
16
}
17
18
function findAvatar($name)
19
{
20
    $directory = base_path("assets/img");
21
    $files = scandir($directory);
22
    $pathinfo = "error.error";
23
    $pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
24
    foreach ($files as $file) {
25
        if (preg_match($pattern, $file)) {
26
            $pathinfo = "assets/img/" . $file;
27
            break;
28
        }
29
    }
30
    return $pathinfo;
31
}
32
33
function findBackground($name)
34
{
35
    $directory = base_path("assets/img/background-img/");
36
    $files = scandir($directory);
37
    $pathinfo = "error.error";
38
    $pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
39
    foreach ($files as $file) {
40
        if (preg_match($pattern, $file)) {
41
            $pathinfo = $file;
42
            break;
43
        }
44
    }
45
    return $pathinfo;
46
}
47
48
function analyzeImageBrightness($file) {
49
    try {
50
    $file = base_path('assets/img/background-img/'.$file);
51
  
52
    // Get image information using getimagesize
53
    $imageInfo = getimagesize($file);
54
    if (!$imageInfo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $imageInfo of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
55
      return 'dark';
56
    }
57
  
58
    // Get the image type
59
    $type = $imageInfo[2];
60
  
61
    // Load the image based on its type
62
    switch ($type) {
63
      case IMAGETYPE_JPEG:
64
      case IMAGETYPE_JPEG2000:
65
        $img = imagecreatefromjpeg($file);
66
        break;
67
      case IMAGETYPE_PNG:
68
        $img = imagecreatefrompng($file);
69
        break;
70
      default:
71
        return 'dark';
72
    }
73
  
74
    // Get image dimensions
75
    $width = imagesx($img);
76
    $height = imagesy($img);
77
  
78
    // Calculate the average brightness of the image
79
    $total_brightness = 0;
80
    for ($x=0; $x<$width; $x++) {
81
      for ($y=0; $y<$height; $y++) {
82
        $rgb = imagecolorat($img, $x, $y);
83
        $r = ($rgb >> 16) & 0xFF;
84
        $g = ($rgb >> 8) & 0xFF;
85
        $b = $rgb & 0xFF;
86
        $brightness = (int)(($r + $g + $b) / 3);
87
        $total_brightness += $brightness;
88
      }
89
    }
90
    $avg_brightness = $total_brightness / ($width * $height);
91
  
92
    // Determine if the image is more dark or light
93
    if ($avg_brightness < 128) {
94
      return 'dark';
95
    } else {
96
      return 'light';
97
    }
98
      } catch (\Throwable $th) {
99
          return null;
100
      }
101
  }
102
  
103
  function infoIcon($tip) {
104
    echo '
105
      <div class="d-flex justify-content-center align-items-center">
106
        <a data-bs-toggle="tooltip" data-bs-placement="bottom" title="' . $tip . '">
107
          <svg class="icon-32" width="32" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
108
            <path fill-rule="evenodd" clip-rule="evenodd" d="M7.67 1.99927H16.34C19.73 1.99927 22 4.37927 22 7.91927V16.0903C22 19.6203 19.73 21.9993 16.34 21.9993H7.67C4.28 21.9993 2 19.6203 2 16.0903V7.91927C2 4.37927 4.28 1.99927 7.67 1.99927ZM11.99 9.06027C11.52 9.06027 11.13 8.66927 11.13 8.19027C11.13 7.70027 11.52 7.31027 12.01 7.31027C12.49 7.31027 12.88 7.70027 12.88 8.19027C12.88 8.66927 12.49 9.06027 11.99 9.06027ZM12.87 15.7803C12.87 16.2603 12.48 16.6503 11.99 16.6503C11.51 16.6503 11.12 16.2603 11.12 15.7803V11.3603C11.12 10.8793 11.51 10.4803 11.99 10.4803C12.48 10.4803 12.87 10.8793 12.87 11.3603V15.7803Z" fill="currentColor"></path>
109
          </svg>
110
        </a>
111
      </div>
112
    ';
113
  }
114
115
function external_file_get_contents($url) {
116
    $ch = curl_init();
117
    curl_setopt($ch, CURLOPT_URL, $url);
118
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
119
    curl_setopt($ch, CURLOPT_HEADER, false);
120
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
121
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0');
122
    $data = curl_exec($ch);
123
    curl_close($ch);
124
    return $data;
125
}
126
127
function uri($path) {
128
    $url = str_replace(['http://', 'https://'], '', url(''));
129
    return "//" . $url . "/" . $path;
130
}
131
132
function footer($key)
133
{
134
    $upperStr = strtoupper($key);
135
    if (env('TITLE_FOOTER_'.$upperStr) == "") {
136
        $title = __('messages.footer.'.$key);
137
    } else {
138
        $title = env('TITLE_FOOTER_'.$upperStr);
139
    }
140
    return $title;
141
}
142
143
function strip_tags_except_allowed_protocols($str) {
144
    preg_match_all('/<a[^>]+>(.*?)<\/a>/i', $str, $matches, PREG_SET_ORDER);
145
146
    foreach ($matches as $val) {
147
        if (!preg_match('/href=["\'](http:|https:|mailto:|tel:)[^"\']*["\']/', $val[0])) {
148
            $str = str_replace($val[0], $val[1], $str);
149
        }
150
    }
151
152
    return $str;
153
}
154
155
if(!function_exists('setBlockAssetContext')) {
156
  function setBlockAssetContext($type = null) {
157
      static $currentType = null;
158
      if ($type !== null) {
159
          $currentType = $type;
160
      }
161
      return $currentType;
162
  }
163
}
164
165
// Get custom block assets
166
if(!function_exists('block_asset')) {
167
  function block_asset($file) {
168
      $type = setBlockAssetContext(); // Retrieve the current type context
169
      return url("block-asset/$type?asset=$file");
170
  }
171
}
172
173
if(!function_exists('get_block_file_contents')) {
174
  function get_block_file_contents($file) {
175
      $type = setBlockAssetContext(); // Retrieve the current type context
176
      return file_get_contents(base_path("blocks/$type/$file"));
177
  }
178
}
179
180
function block_text_translation_check($text) {
181
  if (empty($text)) {
182
    return false;
183
  }
184
  $translate = __("messages.$text");
185
  return $translate === "messages.$text" ? true : false;
186
}
187
188
function block_text($text) {
189
  $translate = __("messages.$text");
190
  return $translate === "messages.$text" ? $text : $translate;
191
}
192
193
function bt($text) {
194
  return block_text($text);
195
}