Remove file execution permission.
[living-lab-site.git] / system / helpers / smiley_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * CodeIgniter Smiley Helpers
20  *
21  * @package             CodeIgniter
22  * @subpackage  Helpers
23  * @category    Helpers
24  * @author              ExpressionEngine Dev Team
25  * @link                http://codeigniter.com/user_guide/helpers/smiley_helper.html
26  */
27
28 // ------------------------------------------------------------------------
29
30 /**
31  * Smiley Javascript
32  *
33  * Returns the javascript required for the smiley insertion.  Optionally takes
34  * an array of aliases to loosely couple the smiley array to the view.
35  *
36  * @access      public
37  * @param       mixed   alias name or array of alias->field_id pairs
38  * @param       string  field_id if alias name was passed in
39  * @return      array
40  */
41 if ( ! function_exists('smiley_js'))
42 {
43         function smiley_js($alias = '', $field_id = '', $inline = TRUE)
44         {
45                 static $do_setup = TRUE;
46
47                 $r = '';
48
49                 if ($alias != '' && ! is_array($alias))
50                 {
51                         $alias = array($alias => $field_id);
52                 }
53
54                 if ($do_setup === TRUE)
55                 {
56                                 $do_setup = FALSE;
57
58                                 $m = array();
59
60                                 if (is_array($alias))
61                                 {
62                                         foreach ($alias as $name => $id)
63                                         {
64                                                 $m[] = '"'.$name.'" : "'.$id.'"';
65                                         }
66                                 }
67
68                                 $m = '{'.implode(',', $m).'}';
69
70                                 $r .= <<<EOF
71                                 var smiley_map = {$m};
72
73                                 function insert_smiley(smiley, field_id) {
74                                         var el = document.getElementById(field_id), newStart;
75
76                                         if ( ! el && smiley_map[field_id]) {
77                                                 el = document.getElementById(smiley_map[field_id]);
78
79                                                 if ( ! el)
80                                                         return false;
81                                         }
82
83                                         el.focus();
84                                         smiley = " " + smiley;
85
86                                         if ('selectionStart' in el) {
87                                                 newStart = el.selectionStart + smiley.length;
88
89                                                 el.value = el.value.substr(0, el.selectionStart) +
90                                                                                 smiley +
91                                                                                 el.value.substr(el.selectionEnd, el.value.length);
92                                                 el.setSelectionRange(newStart, newStart);
93                                         }
94                                         else if (document.selection) {
95                                                 document.selection.createRange().text = smiley;
96                                         }
97                                 }
98 EOF;
99                 }
100                 else
101                 {
102                         if (is_array($alias))
103                         {
104                                 foreach ($alias as $name => $id)
105                                 {
106                                         $r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n";
107                                 }
108                         }
109                 }
110
111                 if ($inline)
112                 {
113                         return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>';
114                 }
115                 else
116                 {
117                         return $r;
118                 }
119         }
120 }
121
122 // ------------------------------------------------------------------------
123
124 /**
125  * Get Clickable Smileys
126  *
127  * Returns an array of image tag links that can be clicked to be inserted
128  * into a form field.
129  *
130  * @access      public
131  * @param       string  the URL to the folder containing the smiley images
132  * @return      array
133  */
134 if ( ! function_exists('get_clickable_smileys'))
135 {
136         function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
137         {
138                 // For backward compatibility with js_insert_smiley
139
140                 if (is_array($alias))
141                 {
142                         $smileys = $alias;
143                 }
144
145                 if ( ! is_array($smileys))
146                 {
147                         if (FALSE === ($smileys = _get_smiley_array()))
148                         {
149                                 return $smileys;
150                         }
151                 }
152
153                 // Add a trailing slash to the file path if needed
154                 $image_url = rtrim($image_url, '/').'/';
155
156                 $used = array();
157                 foreach ($smileys as $key => $val)
158                 {
159                         // Keep duplicates from being used, which can happen if the
160                         // mapping array contains multiple identical replacements.  For example:
161                         // :-) and :) might be replaced with the same image so both smileys
162                         // will be in the array.
163                         if (isset($used[$smileys[$key][0]]))
164                         {
165                                 continue;
166                         }
167
168                         $link[] = "<a href=\"javascript:void(0);\" onclick=\"insert_smiley('".$key."', '".$alias."')\"><img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" /></a>";
169
170                         $used[$smileys[$key][0]] = TRUE;
171                 }
172
173                 return $link;
174         }
175 }
176
177 // ------------------------------------------------------------------------
178
179 /**
180  * Parse Smileys
181  *
182  * Takes a string as input and swaps any contained smileys for the actual image
183  *
184  * @access      public
185  * @param       string  the text to be parsed
186  * @param       string  the URL to the folder containing the smiley images
187  * @return      string
188  */
189 if ( ! function_exists('parse_smileys'))
190 {
191         function parse_smileys($str = '', $image_url = '', $smileys = NULL)
192         {
193                 if ($image_url == '')
194                 {
195                         return $str;
196                 }
197
198                 if ( ! is_array($smileys))
199                 {
200                         if (FALSE === ($smileys = _get_smiley_array()))
201                         {
202                                 return $str;
203                         }
204                 }
205
206                 // Add a trailing slash to the file path if needed
207                 $image_url = preg_replace("/(.+?)\/*$/", "\\1/",  $image_url);
208
209                 foreach ($smileys as $key => $val)
210                 {
211                         $str = str_replace($key, "<img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" />", $str);
212                 }
213
214                 return $str;
215         }
216 }
217
218 // ------------------------------------------------------------------------
219
220 /**
221  * Get Smiley Array
222  *
223  * Fetches the config/smiley.php file
224  *
225  * @access      private
226  * @return      mixed
227  */
228 if ( ! function_exists('_get_smiley_array'))
229 {
230         function _get_smiley_array()
231         {
232                 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys'.EXT))
233                 {
234                     include(APPPATH.'config/'.ENVIRONMENT.'/smileys'.EXT);
235                 }
236                 elseif (file_exists(APPPATH.'config/smileys'.EXT))
237                 {
238                         include(APPPATH.'config/smileys'.EXT);
239                 }
240                 
241                 if (isset($smileys) AND is_array($smileys))
242                 {
243                         return $smileys;
244                 }
245
246                 return FALSE;
247         }
248 }
249
250 // ------------------------------------------------------------------------
251
252 /**
253  * JS Insert Smiley
254  *
255  * Generates the javascript function needed to insert smileys into a form field
256  *
257  * DEPRECATED as of version 1.7.2, use smiley_js instead
258  *
259  * @access      public
260  * @param       string  form name
261  * @param       string  field name
262  * @return      string
263  */
264 if ( ! function_exists('js_insert_smiley'))
265 {
266         function js_insert_smiley($form_name = '', $form_field = '')
267         {
268                 return <<<EOF
269 <script type="text/javascript">
270         function insert_smiley(smiley)
271         {
272                 document.{$form_name}.{$form_field}.value += " " + smiley;
273         }
274 </script>
275 EOF;
276         }
277 }
278
279
280 /* End of file smiley_helper.php */
281 /* Location: ./system/helpers/smiley_helper.php */