PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/application/maxsite/plugins/spoiler/index.php

https://github.com/dimmaq/MaxSiteCMSTest
PHP | 202 lines | 170 code | 13 blank | 19 comment | 13 complexity | dc02508a4e9bcf4da27d86918034b11d MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * For MaxSite CMS
  4. * Spoiler Plugin
  5. * Author: (c) Tux
  6. * Plugin URL: http://6log.ru/spoiler
  7. */
  8. # функция автоподключения плагина
  9. function spoiler_autoload($args = array())
  10. {
  11. mso_hook_add( 'head', 'spoiler_head');
  12. mso_hook_add( 'content', 'spoiler_custom'); # хук на вывод контента
  13. $options_key = 'plugin_spoiler';
  14. $options = mso_get_option($options_key, 'plugins', array());
  15. if ( isset($options['comments']) and ( $options['comments'] == 1) )
  16. {
  17. mso_hook_add( 'comments_content_out', 'spoiler_custom');
  18. }
  19. }
  20. # функция выполняется при деинсталяции плагина
  21. function spoiler_uninstall($args = array())
  22. {
  23. // константа
  24. $options_key = 'plugin_spoiler';
  25. mso_delete_option($options_key,'plugins');
  26. return $args;
  27. }
  28. # функции плагина
  29. function spoiler_custom($text)
  30. {
  31. // константа
  32. $options_key = 'plugin_spoiler';
  33. /* Настройки*/
  34. $options = mso_get_option($options_key, 'plugins', array());
  35. if ( !isset($options['hide']) ) $options['hide'] = t('Скрыть');
  36. if ( !isset($options['show']) ) $options['show'] = t('Показать...');
  37. if ( !isset($options['comments']) ) $options['comments'] = 0;
  38. $showtext = $options['show'];
  39. $hidetext = $options['hide'];
  40. // dont edit!
  41. //$pattern = '@(\[spoiler\](.*?)\[/spoiler\])@is';
  42. $pattern = "@\[spoiler(=)?(.*?)\](.*?)\[\/spoiler\]@is";
  43. // замена [spoiler]...[/spoiler] тегов
  44. if (preg_match_all($pattern, $text, $matches))
  45. {
  46. for ($i = 0; $i < count($matches[0]); $i++)
  47. {
  48. //$id = 'id'.rand();
  49. $id = 'id' . rand(100,999);
  50. $html = '';
  51. if ($matches[1][$i] == '=')
  52. {
  53. if ( strpos($matches[2][$i], "/") !== false )
  54. {
  55. $matches[2][$i] = str_replace("'", "\'", $matches[2][$i]);
  56. $matches[2][$i] = str_replace("\"", "&quot;", $matches[2][$i]);
  57. $tm = explode("/", $matches[2][$i]);
  58. if ( strpos($matches[2][$i], "/") === 0 )
  59. {
  60. $hidetext = $tm[1];
  61. $showtext = $options['show'];
  62. }
  63. else
  64. {
  65. $hidetext = $tm[1];
  66. $showtext = $tm[0];
  67. }
  68. }
  69. else
  70. {
  71. $showtext = $matches[2][$i];
  72. $hidetext = $options['hide'];
  73. }
  74. }
  75. else
  76. {
  77. $showtext = $options['show'];
  78. $hidetext = $options['hide'];
  79. }
  80. $html .= '<p class="spoiler"><a class="spoiler_link_show" href="javascript:void(0)" onclick="SpoilerToggle(\'' . $id . '\', this, \'' . $showtext.'\', \'' . $hidetext . '\')">' . $showtext . '</a></p>';
  81. $html .= '<div class="spoiler_div" id="' . $id . '" style="display:none"><p>' . $matches[3][$i] . '</p></div>';
  82. //$text = str_replace($matches[0][$i], $html, $text);
  83. $text = preg_replace($pattern, $html, $text, 1);
  84. }
  85. }
  86. return $text;
  87. }
  88. # JavaScript & css text добавляем в head
  89. function spoiler_head($args = array())
  90. {
  91. $options_key = 'plugin_spoiler';
  92. $options = mso_get_option($options_key, 'plugins', array());
  93. if ( !isset($options['style']) ) $options['style'] = '';
  94. if ($options['style'] != '')
  95. {
  96. echo '
  97. <link rel="stylesheet" href="' . getinfo('plugins_url') . 'spoiler/style/'.$options['style']. '">';
  98. }
  99. echo '
  100. <script>
  101. function SpoilerToggle(id, link, showtext, hidetext)
  102. {
  103. var spoiler = document.getElementById(id);
  104. if (spoiler.style.display != "none")
  105. {
  106. spoiler.style.display = "none";
  107. link.innerHTML = showtext;
  108. link.className = "spoiler_link_show";
  109. }
  110. else
  111. {
  112. spoiler.style.display = "block";
  113. link.innerHTML = hidetext;
  114. link.className = "spoiler_link_hide";
  115. }
  116. }
  117. </script>
  118. ';
  119. }
  120. # функция отрабатывающая миниопции плагина (function плагин_mso_options)
  121. function spoiler_mso_options()
  122. {
  123. //// Взято из wp-converter
  124. $CI = & get_instance();
  125. // найдем все файлы по маске *.css
  126. $CI->load->helper('directory');
  127. // $dir = directory_map(getinfo('uploads_dir'), true); // только в текущем каталоге
  128. $path = getinfo('plugins_dir').'spoiler/style/';
  129. $dir = directory_map($path, true);
  130. if (!$dir) $dir = array();
  131. natsort($dir);
  132. $option_select = '';
  133. $option_select .= '||' . t('без стилей');
  134. foreach ($dir as $file)
  135. {
  136. if (@is_dir(getinfo('plugins_url').'spoiler/style/' . $file)) continue; // это каталог
  137. if (preg_match('|(.*?)\.css|', $file))
  138. {
  139. $option_select .= '#'. $file . '||' . $file;
  140. }
  141. }
  142. ////
  143. # ключ, тип, ключи массива
  144. mso_admin_plugin_options('plugin_spoiler', 'plugins',
  145. array(
  146. 'hide' => array(
  147. 'type' => 'text',
  148. 'name' => t('Спрятать:'),
  149. 'description' => t('Можно настроить какой текст появится в раскрытом виде'),
  150. 'default' => t('Скрыть')
  151. ),
  152. 'show' => array(
  153. 'type' => 'text',
  154. 'name' => t('Показать:'),
  155. 'description' => t('Можно настроить какой текст появится в скрытом виде'),
  156. 'default' => t('Показать...')
  157. ),
  158. 'style' => array(
  159. 'type' => 'select',
  160. 'name' => t('Файл стилей:'),
  161. 'description' => t('Стили лежат в следеющей папке: (.../plugins/spoiler/style/)'),
  162. 'values' => $option_select,
  163. 'default' => ''
  164. ),
  165. 'comments' => array(
  166. 'type' => 'checkbox',
  167. 'name' => t('Использовать спойлеры в комментариях'),
  168. 'description' => t(' '),
  169. 'default' => 0
  170. ),
  171. ),
  172. t('Настройки плагина Spoiler'), // титул
  173. t('<p>С помощью этого плагина вы можете скрывать текст под спойлер.<br>Для использования плагина обрамите нужный текст в код [spoiler]ваш текст[/spoiler]</p><p class="info">Также возможны такие варианты: <br>[spoiler=показать]ваш текст[/spoiler], [spoiler=показать/спрятать]ваш текст[/spoiler], [spoiler=/спрятать]ваш текст[/spoiler]</p>') // инфа
  174. );
  175. }
  176. # end file