PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/source/admincp/admincp_hooker.php

https://github.com/jinbo51/DiscuzX
PHP | 220 lines | 165 code | 16 blank | 39 comment | 44 complexity | b768aef6dd87d9227f51103f72ecad77 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: admincp_flinks.php 31327 2012-08-13 07:01:41Z liulanbo $
  7. */
  8. if (!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
  9. exit('Access Denied');
  10. }
  11. if (empty($admincp) || !is_object($admincp) || !$admincp->isfounder) {
  12. exit('Access Denied');
  13. }
  14. loadcache('plugin');
  15. global $_G;
  16. if (!submitcheck('settingsubmit') && !submitcheck('inserthook')) {
  17. $_CA = C::t('common_setting')->fetch_all(null);
  18. cpheader();
  19. shownav('sitemanager', 'menu_sitemanager_hooker');
  20. showsubmenu('menu_sitemanager_hooker');
  21. showformheader('hooker');
  22. showtableheader('');
  23. $tableClasses = array('class="td25"', 'class="td29"', 'class="td29"', 'class="td29"', 'class="td29"', 'class="td31"');
  24. showtablerow('', $tableClasses, array('', cplang('嵌入点($hooker)'), cplang('嵌入点所在文件($file)'), cplang('匹配规则($pattern)'), cplang('替换($replacement)'), '',));
  25. print "
  26. <script type=\"text/JavaScript\">
  27. var rowtypedata = [
  28. [
  29. [1,'', 'td25'],
  30. [1,'<input type=\"text\" class=\"txt\" name=\"newhooker[]\" size=\"20\">', 'td29'],
  31. [1,'<input type=\"text\" class=\"txt\" name=\"newfile[]\" size=\"20\">', 'td29'],
  32. [1,'<input type=\"text\" class=\"txt\" name=\"newpattern[]\" size=\"20\">', 'td29'],
  33. [1,'<input type=\"text\" class=\"txt\" name=\"newreplacement[]\" size=\"20\">', 'td29'],
  34. [1,'', 'td31'],
  35. ]
  36. ];
  37. </script>";
  38. $_CA['templatehooker'] = (array)dunserialize($_CA['templatehooker']);
  39. foreach ($_CA['templatehooker'] as $templatehooker) {
  40. $str = '';
  41. $file = DISCUZ_ROOT . $_G['style']['tpldir'] . '/' . $templatehooker['file'];
  42. if (!file_exists($file)) {
  43. $file = DISCUZ_ROOT . './template/default/' . $templatehooker['file'];
  44. if (!file_exists($file)) {
  45. $str = '找不到对应模板文件';
  46. }
  47. }
  48. if ($str == '') {
  49. $hooker = html_entity_decode($templatehooker['hooker'], ENT_QUOTES, 'UTF-8');
  50. $pattern = html_entity_decode($templatehooker['pattern'], ENT_QUOTES, 'UTF-8');
  51. $replacement = html_entity_decode($templatehooker['replacement'], ENT_QUOTES, 'UTF-8');
  52. $hooker_exist = xm_file_content_exists($file, $hooker);
  53. $tpd = htmlentities($templatehooker['templatehookerid'], ENT_QUOTES, 'UTF-8');
  54. if ($hooker_exist) {
  55. $str = '<span style="color:#999999;">找到嵌入点</span>';
  56. } else {
  57. $flag_exist = xm_file_content_exists($file, $pattern);
  58. if ($flag_exist) {
  59. $str = "<input type=\"submit\" name=\"inserthook[$tpd]\" value=\"插入嵌入点\" />";
  60. //$result = xm_file_replace($file, $pattern, $replacement, $hooker);
  61. } else {
  62. $str = '找不到参照点';
  63. }
  64. }
  65. }
  66. if ($templatehooker['hooker'] !== '') {
  67. showtablerow('', $tableClasses, array("<input class=\"checkbox\" type=\"checkbox\" name=\"delete[]\" value=\"$templatehooker[templatehookerid]\">", "<input type=\"text\" class=\"txt\" size=\"20\" name=\"hooker[$templatehooker[templatehookerid]]\" value=\"$templatehooker[hooker]\">", "<input type=\"text\" class=\"txt\" size=\"20\" name=\"file[$templatehooker[templatehookerid]]\" value=\"$templatehooker[file]\" >", "<input type=\"text\" class=\"txt\" size=\"20\" name=\"pattern[$templatehooker[templatehookerid]]\" value=\"$templatehooker[pattern]\">", "<input type=\"text\" class=\"txt\" size=\"20\" name=\"replacement[$templatehooker[templatehookerid]]\" value=\"$templatehooker[replacement]\">", $str));
  68. }
  69. }
  70. echo '<tr><td></td><td colspan="8"><div><a href="###" onclick="addrow(this, 0)" class="addtr">' . '添加嵌入点' . '</a></div></td></tr>';
  71. // foreach ($_G['cache']['plugin'] as $plugin => $value) {
  72. // echo '$_G[\'cache\'][\'plugin\']:' . /*serialize*/
  73. // ($plugin) . ';<br />';
  74. // }
  75. showsubmit('settingsubmit', 'submit', 'del');
  76. showtablefooter();
  77. showformfooter();
  78. } else if (submitcheck('inserthook')) {
  79. $_CA = C::t('common_setting')->fetch_all(null);
  80. $_CA['templatehooker'] = (array)dunserialize($_CA['templatehooker']);
  81. foreach ($_GET['inserthook'] as $inserthook => $value) {
  82. $templatehooker = $_CA['templatehooker'][$inserthook];
  83. //print_r($templatehooker);
  84. $str = '';
  85. $file = DISCUZ_ROOT . $_G['style']['tpldir'] . '/' . $templatehooker['file'];
  86. if (!file_exists($file)) {
  87. $file = DISCUZ_ROOT . './template/default/' . $templatehooker['file'];
  88. if (!file_exists($file)) {
  89. $str = '找不到对应模板文件';
  90. }
  91. }
  92. if ($str == '') {
  93. $hooker = html_entity_decode($templatehooker['hooker'], ENT_QUOTES, 'UTF-8');
  94. $pattern = html_entity_decode($templatehooker['pattern'], ENT_QUOTES, 'UTF-8');
  95. $replacement = html_entity_decode($templatehooker['replacement'], ENT_QUOTES, 'UTF-8');
  96. $hooker_exist = xm_file_content_exists($file, $hooker);
  97. if ($hooker_exist) {
  98. $str = '<span style="color:#999999;">找到嵌入点</span>';
  99. } else {
  100. $flag_exist = xm_file_content_exists($file, $pattern);
  101. if ($flag_exist) {
  102. $str = "插入嵌入点:<input type=\"submit\" name=\"inserthook[$templatehooker[templatehookerid]]\" value=\"$templatehooker[templatehookerid]\" />";
  103. $result = xm_file_replace($file, $pattern, $replacement, $hooker);
  104. } else {
  105. $str = '找不到参照点';
  106. }
  107. }
  108. }
  109. if ($result) {
  110. cpmsg('嵌入点插入成功', 'hooker', 'succeed');
  111. } else {
  112. cpmsg(('Error ' . $str), 'hooker', 'error');
  113. }
  114. }
  115. ;
  116. /*
  117. foreach(($_GET['inserthook']) as $inserthook => $value){
  118. print_r($inserthook);
  119. //$templatehooker = $_CA['templatehooker'][html_entity_decode($inserthook, ENT_QUOTES, 'UTF-8')];
  120. //print_r($templatehooker);
  121. }
  122. print_r($_CA['templatehooker']['<p><a>Powered by</a> <strong>']);
  123. */
  124. } else {
  125. $settingnew = $_GET['settingnew'];
  126. if (is_array($_GET['hooker'])) {
  127. foreach ($_GET['hooker'] as $templatehookerid => $val) {
  128. //$templatehookerid = intval($templatehookerid);
  129. //print_r($templatehookerid );
  130. //print_r($val);
  131. //echo intval($templatehookerid == '');
  132. $updatearr = array('templatehookerid' => /*dhtmlspecialchars($_GET['hooker'][$templatehookerid])*/
  133. htmlentities($_GET['hooker'][$templatehookerid], ENT_QUOTES, 'UTF-8'), 'hooker' => /*dhtmlspecialchars($_GET['hooker'][$templatehookerid])*/
  134. htmlentities($_GET['hooker'][$templatehookerid], ENT_QUOTES, 'UTF-8'), 'file' => $_GET['file'][$templatehookerid], 'pattern' => /*dhtmlspecialchars($_GET['pattern'][$templatehookerid])*/
  135. htmlentities($_GET['pattern'][$templatehookerid], ENT_QUOTES, 'UTF-8'), 'replacement' => /*dhtmlspecialchars($_GET['replacement'][$templatehookerid])*/
  136. htmlentities($_GET['replacement'][$templatehookerid], ENT_QUOTES, 'UTF-8'),);
  137. //C::t('home_click')->update($id, $updatearr);
  138. $settingnew['templatehooker'][htmlentities($templatehookerid, ENT_QUOTES, 'UTF-8')] = $updatearr;
  139. }
  140. }
  141. if (is_array($_GET['delete'])) {
  142. foreach ($_GET['delete'] as $id => $val) {
  143. //$ids[] = $id;
  144. //echo $_GET['delete'][$id];
  145. //echo '=';
  146. echo $_GET['delete'][$id];
  147. //echo ';';
  148. //$templatehooker[($id)] = array();
  149. //$templatehooker = array_splice($templatehooker, intval($id), 1);
  150. unset($settingnew['templatehooker'][$_GET['delete'][$id]]);
  151. }
  152. if ($ids) {
  153. //C::t('home_click')->delete($ids, true);
  154. }
  155. }
  156. //print_r($_GET['newhooker']);
  157. if (is_array($_GET['newhooker'])) {
  158. foreach ($_GET['newhooker'] as $key => $value) {
  159. //echo $key;
  160. //echo "=";
  161. //echo $value;
  162. if ($value != '' && $_GET['newhooker'][$key] != '') {
  163. $data = array('templatehookerid' => dhtmlspecialchars($_GET['newhooker'][$key]), 'hooker' => dhtmlspecialchars($_GET['newhooker'][$key]), 'file' => dhtmlspecialchars($_GET['newfile'][$key]), 'pattern' => dhtmlspecialchars($_GET['newpattern'][$key]), 'replacement' => dhtmlspecialchars($_GET['newreplacement'][$key]));
  164. //C::t('home_click')->insert($data);
  165. //print_r( $data);
  166. //array_push($templatehooker, $data);
  167. $settingnew['templatehooker'][dhtmlspecialchars($_GET['newhooker'][$key])] = $data;
  168. }
  169. }
  170. }
  171. $settingnew['templatehooker'] = serialize($settingnew['templatehooker']);
  172. C::t('common_setting')->update_batch($settingnew);
  173. updatecache('setting');
  174. cpmsg('setting_update_succeed', 'hooker', 'succeed');
  175. }
  176. function xm_file_content_exists($file, $message) {
  177. if (file_exists($file)) {
  178. $content = file_get_contents($file);
  179. if (substr($message, 0, 1) !== '/') {
  180. return stripos($content, $message) !== false;
  181. } else {
  182. return preg_match($message, $content);
  183. }
  184. }
  185. return false;
  186. }
  187. function xm_file_replace($file, $pattern, $replace, $hooker, $limit = -1) {
  188. if (file_exists($file)) {
  189. $content = file_get_contents($file);
  190. if (is_callable($replace)) {
  191. $content = preg_replace_callback($pattern, $replace, $content, $limit);
  192. } elseif (substr($pattern, 0, 1) !== '/') {
  193. $content = str_replace($pattern, str_replace('$hooker', $hooker, $replace), $content, $limit);
  194. } else {
  195. $content = preg_replace($pattern, str_replace('\r', "\r", str_replace('$hooker', $hooker, str_replace('\t', "\t", str_replace('\n', "\n", $replace)))), $content, $limit);
  196. }
  197. if ($content !== false) {
  198. file_put_contents($file, $content);
  199. return true;
  200. }
  201. }
  202. return false;
  203. }
  204. ?>