smiley_helper.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  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:
  17. *
  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
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Smiley Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/helpers/smiley_helper.html
  47. * @deprecated 3.0.0 This helper is too specific for CI.
  48. */
  49. // ------------------------------------------------------------------------
  50. if ( ! function_exists('smiley_js'))
  51. {
  52. /**
  53. * Smiley Javascript
  54. *
  55. * Returns the javascript required for the smiley insertion. Optionally takes
  56. * an array of aliases to loosely couple the smiley array to the view.
  57. *
  58. * @param mixed alias name or array of alias->field_id pairs
  59. * @param string field_id if alias name was passed in
  60. * @param bool
  61. * @return array
  62. */
  63. function smiley_js($alias = '', $field_id = '', $inline = TRUE)
  64. {
  65. static $do_setup = TRUE;
  66. $r = '';
  67. if ($alias !== '' && ! is_array($alias))
  68. {
  69. $alias = array($alias => $field_id);
  70. }
  71. if ($do_setup === TRUE)
  72. {
  73. $do_setup = FALSE;
  74. $m = array();
  75. if (is_array($alias))
  76. {
  77. foreach ($alias as $name => $id)
  78. {
  79. $m[] = '"'.$name.'" : "'.$id.'"';
  80. }
  81. }
  82. $m = '{'.implode(',', $m).'}';
  83. $r .= <<<EOF
  84. var smiley_map = {$m};
  85. function insert_smiley(smiley, field_id) {
  86. var el = document.getElementById(field_id), newStart;
  87. if ( ! el && smiley_map[field_id]) {
  88. el = document.getElementById(smiley_map[field_id]);
  89. if ( ! el)
  90. return false;
  91. }
  92. el.focus();
  93. smiley = " " + smiley;
  94. if ('selectionStart' in el) {
  95. newStart = el.selectionStart + smiley.length;
  96. el.value = el.value.substr(0, el.selectionStart) +
  97. smiley +
  98. el.value.substr(el.selectionEnd, el.value.length);
  99. el.setSelectionRange(newStart, newStart);
  100. }
  101. else if (document.selection) {
  102. document.selection.createRange().text = smiley;
  103. }
  104. }
  105. EOF;
  106. }
  107. elseif (is_array($alias))
  108. {
  109. foreach ($alias as $name => $id)
  110. {
  111. $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n";
  112. }
  113. }
  114. return ($inline)
  115. ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>'
  116. : $r;
  117. }
  118. }
  119. // ------------------------------------------------------------------------
  120. if ( ! function_exists('get_clickable_smileys'))
  121. {
  122. /**
  123. * Get Clickable Smileys
  124. *
  125. * Returns an array of image tag links that can be clicked to be inserted
  126. * into a form field.
  127. *
  128. * @param string the URL to the folder containing the smiley images
  129. * @param array
  130. * @return array
  131. */
  132. function get_clickable_smileys($image_url, $alias = '')
  133. {
  134. // For backward compatibility with js_insert_smiley
  135. if (is_array($alias))
  136. {
  137. $smileys = $alias;
  138. }
  139. elseif (FALSE === ($smileys = _get_smiley_array()))
  140. {
  141. return FALSE;
  142. }
  143. // Add a trailing slash to the file path if needed
  144. $image_url = rtrim($image_url, '/').'/';
  145. $used = array();
  146. foreach ($smileys as $key => $val)
  147. {
  148. // Keep duplicates from being used, which can happen if the
  149. // mapping array contains multiple identical replacements. For example:
  150. // :-) and :) might be replaced with the same image so both smileys
  151. // will be in the array.
  152. if (isset($used[$smileys[$key][0]]))
  153. {
  154. continue;
  155. }
  156. $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>';
  157. $used[$smileys[$key][0]] = TRUE;
  158. }
  159. return $link;
  160. }
  161. }
  162. // ------------------------------------------------------------------------
  163. if ( ! function_exists('parse_smileys'))
  164. {
  165. /**
  166. * Parse Smileys
  167. *
  168. * Takes a string as input and swaps any contained smileys for the actual image
  169. *
  170. * @param string the text to be parsed
  171. * @param string the URL to the folder containing the smiley images
  172. * @param array
  173. * @return string
  174. */
  175. function parse_smileys($str = '', $image_url = '', $smileys = NULL)
  176. {
  177. if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array())))
  178. {
  179. return $str;
  180. }
  181. // Add a trailing slash to the file path if needed
  182. $image_url = rtrim($image_url, '/').'/';
  183. foreach ($smileys as $key => $val)
  184. {
  185. $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);
  186. }
  187. return $str;
  188. }
  189. }
  190. // ------------------------------------------------------------------------
  191. if ( ! function_exists('_get_smiley_array'))
  192. {
  193. /**
  194. * Get Smiley Array
  195. *
  196. * Fetches the config/smiley.php file
  197. *
  198. * @return mixed
  199. */
  200. function _get_smiley_array()
  201. {
  202. static $_smileys;
  203. if ( ! is_array($_smileys))
  204. {
  205. if (file_exists(APPPATH.'config/smileys.php'))
  206. {
  207. include(APPPATH.'config/smileys.php');
  208. }
  209. if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
  210. {
  211. include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
  212. }
  213. if (empty($smileys) OR ! is_array($smileys))
  214. {
  215. $_smileys = array();
  216. return FALSE;
  217. }
  218. $_smileys = $smileys;
  219. }
  220. return $_smileys;
  221. }
  222. }