source: extensions/typetags/include/functions.inc.php @ 26665

Last change on this file since 26665 was 26665, checked in by mistic100, 10 years ago

integrate in new tags manager + rename in Coloured Tags
(my apologies to translators :-) )

File size: 1.6 KB
Line 
1<?php
2defined('TYPETAGS_PATH') or die('Hacking attempt!');
3
4function get_color_text($color)
5{
6  if (empty($color))
7  {
8    return '';
9  }
10
11  if (strlen($color) == 7)
12  {
13    $rgb[] = hexdec(substr($color, 1, 2))/255;
14    $rgb[] = hexdec(substr($color, 3, 2))/255;
15    $rgb[] = hexdec(substr($color, 5, 2))/255;
16  }
17  else if (strlen($color) == 4)
18  {
19    $rgb[] = hexdec(substr($color, 1, 1))/15;
20    $rgb[] = hexdec(substr($color, 2, 1))/15;
21    $rgb[] = hexdec(substr($color, 3, 1))/15;
22  }
23
24  $l = (min($rgb) + max($rgb)) / 2;
25
26  return $l > 0.45 ? '#000' : '#fff';
27}
28
29function check_color($hex)
30{
31  global $page;
32
33  $hex = ltrim($hex, '#');
34
35  if (strlen($hex) == 3)
36  {
37    $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
38  }
39  else if (strlen($hex) != 6)
40  {
41    return false;
42  }
43
44  if (!ctype_xdigit($hex))
45  {
46    return false;
47  }
48
49  return '#'.$hex;
50}
51
52function get_typetag_id($input)
53{
54  if (preg_match('#^~~([0-9]+)~~$#', $input, $matches))
55  {
56    return $matches[1];
57  }
58  else if (strpos($input, '|') !== false)
59  {
60    list($color, $name) = explode('|', $input, 2);
61
62    if ( ($color = check_color($color)) === false)
63    {
64      return false;
65    }
66
67    $query = '
68SELECT id FROM ' . TYPETAGS_TABLE . '
69  WHERE color = "' . $color . '"
70  LIMIT 1
71;';
72    $result = pwg_query($query);
73
74    if (pwg_db_num_rows($result))
75    {
76      list($tt_id) = pwg_db_fetch_row($result);
77      return $tt_id;
78    }
79
80    $query = '
81INSERT INTO ' . TYPETAGS_TABLE . '(
82    name,
83    color
84  )
85  VALUES(
86    "' . pwg_db_real_escape_string($name) . '",
87    "' . $color . '"
88  )
89;';
90    pwg_query($query);
91
92    return pwg_db_insert_id();
93  }
94  else
95  {
96    return false;
97  }
98}
Note: See TracBrowser for help on using the repository browser.