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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
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 | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014, 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 |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/) |
| 33 | * @license http://opensource.org/licenses/MIT MIT License |
| 34 | * @link http://codeigniter.com |
| 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 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html |
| 48 | */ |
| 49 | |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 52 | if ( ! function_exists('smiley_js')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 53 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 54 | /** |
| 55 | * Smiley Javascript |
| 56 | * |
| 57 | * Returns the javascript required for the smiley insertion. Optionally takes |
| 58 | * an array of aliases to loosely couple the smiley array to the view. |
| 59 | * |
| 60 | * @param mixed alias name or array of alias->field_id pairs |
| 61 | * @param string field_id if alias name was passed in |
| 62 | * @param bool |
| 63 | * @return array |
| 64 | */ |
Greg Aker | 60e78c7 | 2010-04-20 12:45:14 -0500 | [diff] [blame] | 65 | function smiley_js($alias = '', $field_id = '', $inline = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 67 | static $do_setup = TRUE; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 68 | $r = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 70 | if ($alias !== '' && ! is_array($alias)) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 71 | { |
| 72 | $alias = array($alias => $field_id); |
| 73 | } |
| 74 | |
| 75 | if ($do_setup === TRUE) |
| 76 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 77 | $do_setup = FALSE; |
| 78 | $m = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 80 | if (is_array($alias)) |
| 81 | { |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 82 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 83 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 84 | $m[] = '"'.$name.'" : "'.$id.'"'; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 87 | |
| 88 | $m = '{'.implode(',', $m).'}'; |
| 89 | |
| 90 | $r .= <<<EOF |
| 91 | var smiley_map = {$m}; |
| 92 | |
| 93 | function insert_smiley(smiley, field_id) { |
| 94 | var el = document.getElementById(field_id), newStart; |
| 95 | |
| 96 | if ( ! el && smiley_map[field_id]) { |
| 97 | el = document.getElementById(smiley_map[field_id]); |
| 98 | |
| 99 | if ( ! el) |
| 100 | return false; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 103 | el.focus(); |
| 104 | smiley = " " + smiley; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 106 | if ('selectionStart' in el) { |
| 107 | newStart = el.selectionStart + smiley.length; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 109 | el.value = el.value.substr(0, el.selectionStart) + |
| 110 | smiley + |
| 111 | el.value.substr(el.selectionEnd, el.value.length); |
| 112 | el.setSelectionRange(newStart, newStart); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | } |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 114 | else if (document.selection) { |
| 115 | document.selection.createRange().text = smiley; |
| 116 | } |
| 117 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | EOF; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 119 | } |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 120 | elseif (is_array($alias)) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 121 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 122 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 123 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 124 | $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n"; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Andrey Andreev | ea41c8a | 2014-02-26 18:31:02 +0200 | [diff] [blame] | 128 | return ($inline) |
| 129 | ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' |
| 130 | : $r; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | // ------------------------------------------------------------------------ |
| 135 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | if ( ! function_exists('get_clickable_smileys')) |
| 137 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 138 | /** |
| 139 | * Get Clickable Smileys |
| 140 | * |
| 141 | * Returns an array of image tag links that can be clicked to be inserted |
| 142 | * into a form field. |
| 143 | * |
| 144 | * @param string the URL to the folder containing the smiley images |
| 145 | * @param array |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 146 | * @return array |
| 147 | */ |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 148 | function get_clickable_smileys($image_url, $alias = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 150 | // For backward compatibility with js_insert_smiley |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 151 | if (is_array($alias)) |
| 152 | { |
| 153 | $smileys = $alias; |
| 154 | } |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 155 | elseif (FALSE === ($smileys = _get_smiley_array())) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | { |
Andrey Andreev | 53b8ef5 | 2012-11-08 21:38:53 +0200 | [diff] [blame] | 157 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Add a trailing slash to the file path if needed |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 161 | $image_url = rtrim($image_url, '/').'/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | $used = array(); |
| 164 | foreach ($smileys as $key => $val) |
| 165 | { |
| 166 | // Keep duplicates from being used, which can happen if the |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 167 | // mapping array contains multiple identical replacements. For example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | // :-) and :) might be replaced with the same image so both smileys |
| 169 | // will be in the array. |
| 170 | if (isset($used[$smileys[$key][0]])) |
| 171 | { |
| 172 | continue; |
| 173 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 174 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 175 | $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] | 176 | $used[$smileys[$key][0]] = TRUE; |
| 177 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 178 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | return $link; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // ------------------------------------------------------------------------ |
| 184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | if ( ! function_exists('parse_smileys')) |
| 186 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 187 | /** |
| 188 | * Parse Smileys |
| 189 | * |
| 190 | * Takes a string as input and swaps any contained smileys for the actual image |
| 191 | * |
| 192 | * @param string the text to be parsed |
| 193 | * @param string the URL to the folder containing the smiley images |
| 194 | * @param array |
| 195 | * @return string |
| 196 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | function parse_smileys($str = '', $image_url = '', $smileys = NULL) |
| 198 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame] | 199 | if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array()))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | { |
| 201 | return $str; |
| 202 | } |
| 203 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | // Add a trailing slash to the file path if needed |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 205 | $image_url = rtrim($image_url, '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | |
| 207 | foreach ($smileys as $key => $val) |
| 208 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 209 | $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] | 210 | } |
| 211 | |
| 212 | return $str; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // ------------------------------------------------------------------------ |
| 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | if ( ! function_exists('_get_smiley_array')) |
| 219 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 220 | /** |
| 221 | * Get Smiley Array |
| 222 | * |
| 223 | * Fetches the config/smiley.php file |
| 224 | * |
| 225 | * @return mixed |
| 226 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | function _get_smiley_array() |
| 228 | { |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 229 | static $_smileys; |
| 230 | |
| 231 | if ( ! is_array($smileys)) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 232 | { |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 233 | if (file_exists(APPPATH.'config/smileys.php')) |
| 234 | { |
| 235 | include(APPPATH.'config/smileys.php'); |
| 236 | } |
| 237 | |
| 238 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php')) |
| 239 | { |
| 240 | include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'); |
| 241 | } |
| 242 | |
| 243 | if (empty($smileys) OR ! is_array($smileys)) |
| 244 | { |
| 245 | $_smileys = array(); |
| 246 | return FALSE; |
| 247 | } |
| 248 | |
| 249 | $_smileys = $smileys; |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 250 | } |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 251 | |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 252 | return $_smileys; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | /* End of file smiley_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 257 | /* Location: ./system/helpers/smiley_helper.php */ |