Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * CodeIgniter Smiley Helpers |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Helpers |
| 45 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/helpers/smiley_helper.html |
Andrey Andreev | 21c3c22 | 2014-12-04 12:10:00 +0200 | [diff] [blame] | 48 | * @deprecated 3.0.0 This helper is too specific for CI. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
| 51 | // ------------------------------------------------------------------------ |
| 52 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 53 | if ( ! function_exists('smiley_js')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 55 | /** |
| 56 | * Smiley Javascript |
| 57 | * |
| 58 | * Returns the javascript required for the smiley insertion. Optionally takes |
| 59 | * an array of aliases to loosely couple the smiley array to the view. |
| 60 | * |
| 61 | * @param mixed alias name or array of alias->field_id pairs |
| 62 | * @param string field_id if alias name was passed in |
| 63 | * @param bool |
| 64 | * @return array |
| 65 | */ |
Greg Aker | 60e78c7 | 2010-04-20 12:45:14 -0500 | [diff] [blame] | 66 | function smiley_js($alias = '', $field_id = '', $inline = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 68 | static $do_setup = TRUE; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 69 | $r = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 71 | if ($alias !== '' && ! is_array($alias)) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 72 | { |
| 73 | $alias = array($alias => $field_id); |
| 74 | } |
| 75 | |
| 76 | if ($do_setup === TRUE) |
| 77 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 78 | $do_setup = FALSE; |
| 79 | $m = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 80 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 81 | if (is_array($alias)) |
| 82 | { |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 83 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 84 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 85 | $m[] = '"'.$name.'" : "'.$id.'"'; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 88 | |
| 89 | $m = '{'.implode(',', $m).'}'; |
| 90 | |
| 91 | $r .= <<<EOF |
| 92 | var smiley_map = {$m}; |
| 93 | |
| 94 | function insert_smiley(smiley, field_id) { |
| 95 | var el = document.getElementById(field_id), newStart; |
| 96 | |
| 97 | if ( ! el && smiley_map[field_id]) { |
| 98 | el = document.getElementById(smiley_map[field_id]); |
| 99 | |
| 100 | if ( ! el) |
| 101 | return false; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 104 | el.focus(); |
| 105 | smiley = " " + smiley; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 107 | if ('selectionStart' in el) { |
| 108 | newStart = el.selectionStart + smiley.length; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 109 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 110 | el.value = el.value.substr(0, el.selectionStart) + |
| 111 | smiley + |
| 112 | el.value.substr(el.selectionEnd, el.value.length); |
| 113 | el.setSelectionRange(newStart, newStart); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | } |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 115 | else if (document.selection) { |
| 116 | document.selection.createRange().text = smiley; |
| 117 | } |
| 118 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 119 | EOF; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 120 | } |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 121 | elseif (is_array($alias)) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 122 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 123 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 124 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 125 | $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n"; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 129 | return ($inline) |
| 130 | ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' |
| 131 | : $r; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 134 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | // ------------------------------------------------------------------------ |
| 136 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | if ( ! function_exists('get_clickable_smileys')) |
| 138 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 139 | /** |
| 140 | * Get Clickable Smileys |
| 141 | * |
| 142 | * Returns an array of image tag links that can be clicked to be inserted |
| 143 | * into a form field. |
| 144 | * |
| 145 | * @param string the URL to the folder containing the smiley images |
| 146 | * @param array |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 147 | * @return array |
| 148 | */ |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 149 | function get_clickable_smileys($image_url, $alias = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 151 | // For backward compatibility with js_insert_smiley |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 152 | if (is_array($alias)) |
| 153 | { |
| 154 | $smileys = $alias; |
| 155 | } |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 156 | elseif (FALSE === ($smileys = _get_smiley_array())) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 158 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | // Add a trailing slash to the file path if needed |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 162 | $image_url = rtrim($image_url, '/').'/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | $used = array(); |
| 165 | foreach ($smileys as $key => $val) |
| 166 | { |
| 167 | // Keep duplicates from being used, which can happen if the |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 168 | // mapping array contains multiple identical replacements. For example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | // :-) and :) might be replaced with the same image so both smileys |
| 170 | // will be in the array. |
| 171 | if (isset($used[$smileys[$key][0]])) |
| 172 | { |
| 173 | continue; |
| 174 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 175 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 176 | $link[] = '<a href="javascript:void(0);" onclick="insert_smiley(\''.$key.'\', \''.$alias.'\')"><img src="'.$image_url.$smileys[$key][0].'" alt="'.$smileys[$key][3].'" style="width: '.$smileys[$key][1].'; height: '.$smileys[$key][2].'; border: 0;" /></a>'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | $used[$smileys[$key][0]] = TRUE; |
| 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | return $link; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // ------------------------------------------------------------------------ |
| 185 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | if ( ! function_exists('parse_smileys')) |
| 187 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 188 | /** |
| 189 | * Parse Smileys |
| 190 | * |
| 191 | * Takes a string as input and swaps any contained smileys for the actual image |
| 192 | * |
| 193 | * @param string the text to be parsed |
| 194 | * @param string the URL to the folder containing the smiley images |
| 195 | * @param array |
| 196 | * @return string |
| 197 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | function parse_smileys($str = '', $image_url = '', $smileys = NULL) |
| 199 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 200 | if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array()))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
| 202 | return $str; |
| 203 | } |
| 204 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | // Add a trailing slash to the file path if needed |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 206 | $image_url = rtrim($image_url, '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | |
| 208 | foreach ($smileys as $key => $val) |
| 209 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 210 | $str = str_replace($key, '<img src="'.$image_url.$smileys[$key][0].'" alt="'.$smileys[$key][3].'" style="width: '.$smileys[$key][1].'; height: '.$smileys[$key][2].'; border: 0;" />', $str); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return $str; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // ------------------------------------------------------------------------ |
| 218 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 219 | if ( ! function_exists('_get_smiley_array')) |
| 220 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 221 | /** |
| 222 | * Get Smiley Array |
| 223 | * |
| 224 | * Fetches the config/smiley.php file |
| 225 | * |
| 226 | * @return mixed |
| 227 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | function _get_smiley_array() |
| 229 | { |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 230 | static $_smileys; |
| 231 | |
Andrey Andreev | 6609614 | 2014-11-23 17:39:39 +0200 | [diff] [blame] | 232 | if ( ! is_array($_smileys)) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 233 | { |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 234 | if (file_exists(APPPATH.'config/smileys.php')) |
| 235 | { |
| 236 | include(APPPATH.'config/smileys.php'); |
| 237 | } |
| 238 | |
| 239 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php')) |
| 240 | { |
| 241 | include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'); |
| 242 | } |
| 243 | |
| 244 | if (empty($smileys) OR ! is_array($smileys)) |
| 245 | { |
| 246 | $_smileys = array(); |
| 247 | return FALSE; |
| 248 | } |
| 249 | |
| 250 | $_smileys = $smileys; |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 251 | } |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 252 | |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 253 | return $_smileys; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | } |
| 255 | } |