module_api.class.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * position_api.class.php 模块安装接口类
  4. *
  5. * @copyright (C) 2005-2010 PHPCMS
  6. * @license http://www.phpcms.cn/license/
  7. * @lastmodify 2010-8-31
  8. */
  9. defined('IN_PHPCMS') or exit('No permission resources.');
  10. pc_base::load_sys_func('dir');
  11. class module_api {
  12. private $db, $m_db, $installdir, $uninstaldir, $module, $isall;
  13. public $error_msg = '';
  14. public function __construct() {
  15. $this->db = pc_base::load_model('module_model');
  16. }
  17. /**
  18. * 模块安装
  19. * @param string $module 模块名
  20. */
  21. public function install($module = '') {
  22. define('INSTALL', true);
  23. if ($module) $this->module = $module;
  24. $this->installdir = PC_PATH.'modules'.DIRECTORY_SEPARATOR.$this->module.DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR;
  25. $this->check();
  26. $models = @require($this->installdir.'model.php');
  27. if (!is_array($models) || empty($models)) {
  28. $models = array('module');
  29. }
  30. if (!in_array('module', $models)) {
  31. array_unshift($models, 'module');
  32. }
  33. if (is_array($models) && !empty($models)) {
  34. foreach ($models as $m) {
  35. $this->m_db = pc_base::load_model($m.'_model');
  36. $sql = file_get_contents($this->installdir.$m.'.sql');
  37. $this->sql_execute($sql);
  38. }
  39. }
  40. if (file_exists($this->installdir.'extention.inc.php')) {
  41. $menu_db = pc_base::load_model('menu_model');
  42. @include ($this->installdir.'extention.inc.php');
  43. if(!defined('INSTALL_MODULE')) {
  44. $file = PC_PATH.'languages'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'lang').DIRECTORY_SEPARATOR.'system_menu.lang.php';
  45. if(file_exists($file)) {
  46. $content = file_get_contents($file);
  47. $content = substr($content,0,-2);
  48. $data = '';
  49. foreach ($language as $key => $l) {
  50. if (L($key, '', 'system_menu')==$key) {
  51. $data .= "\$LANG['".$key."'] = '".$l."';\r\n";
  52. }
  53. }
  54. $data = $content.$data."?>";
  55. file_put_contents($file,$data);
  56. } else {
  57. foreach ($language as $key =>$l) {
  58. if (L($key, '', 'system_menu')==$key) {
  59. $data .= "\$LANG['".$key."'] = '".$l."';\r\n";
  60. }
  61. }
  62. $data = "<?"."php\r\n\$data?>";
  63. file_put_contents($file,$data);
  64. }
  65. }
  66. }
  67. if(!defined('INSTALL_MODULE')) {
  68. if (file_exists($this->installdir.'languages'.DIRECTORY_SEPARATOR)) {
  69. dir_copy($this->installdir.'languages'.DIRECTORY_SEPARATOR, PC_PATH.'languages'.DIRECTORY_SEPARATOR);
  70. }
  71. if(file_exists($this->installdir.'templates'.DIRECTORY_SEPARATOR)) {
  72. dir_copy($this->installdir.'templates'.DIRECTORY_SEPARATOR, PC_PATH.'templates'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'tpl_name').DIRECTORY_SEPARATOR.$this->module.DIRECTORY_SEPARATOR);
  73. if (file_exists($this->installdir.'templates'.DIRECTORY_SEPARATOR.'name.inc.php')) {
  74. $keyid = 'templates|'.pc_base::load_config('system', 'tpl_name').'|'.$this->module;
  75. $file_explan[$keyid] = include $this->installdir.'templates'.DIRECTORY_SEPARATOR.'name.inc.php';
  76. $templatepath = PC_PATH.'templates'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'tpl_name').DIRECTORY_SEPARATOR;
  77. if (file_exists($templatepath.'config.php')) {
  78. $style_info = include $templatepath.'config.php';
  79. $style_info['file_explan'] = array_merge($style_info['file_explan'], $file_explan);
  80. @file_put_contents($templatepath.'config.php', '<?php return '.var_export($style_info, true).';?>');
  81. }
  82. unlink(PC_PATH.'templates'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'tpl_name').DIRECTORY_SEPARATOR.$this->module.DIRECTORY_SEPARATOR.'name.inc.php');
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. /**
  89. * 检查安装目录
  90. * @param string $module 模块名
  91. */
  92. public function check($module = '') {
  93. define('INSTALL', true);
  94. if ($module) $this->module = $module;
  95. if(!$this->module) {
  96. $this->error_msg = L('no_module');
  97. return false;
  98. }
  99. if(!defined('INSTALL_MODULE')) {
  100. if (dir_create(PC_PATH.'languages'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'lang').DIRECTORY_SEPARATOR.'test_create_dir')) {
  101. sleep(1);
  102. dir_delete(PC_PATH.'languages'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'lang').DIRECTORY_SEPARATOR.'test_create_dir');
  103. } else {
  104. $this->error_msg = L('lang_dir_no_write');
  105. return false;
  106. }
  107. }
  108. $r = $this->db->get_one(array('module'=>$this->module));
  109. if ($r) {
  110. $this->error_msg = L('this_module_installed');
  111. return false;
  112. }
  113. if (!$this->installdir) {
  114. $this->installdir = PC_PATH.'modules'.DIRECTORY_SEPARATOR.$this->module.DIRECTORY_SEPARATOR.'install'.DIRECTORY_SEPARATOR;
  115. }
  116. if (!is_dir($this->installdir)) {
  117. $this->error_msg = L('install_dir_no_exist');
  118. return false;
  119. }
  120. if (!file_exists($this->installdir.'module.sql')) {
  121. $this->error_msg = L('module_sql_no_exist');
  122. return false;
  123. }
  124. $models = @require($this->installdir.'model.php');
  125. if (is_array($models) && !empty($models)) {
  126. foreach ($models as $m) {
  127. if (!file_exists(PC_PATH.'model'.DIRECTORY_SEPARATOR.$m.'_model.class.php')) {
  128. $this->error_msg = $m.L('model_clas_no_exist');
  129. return false;
  130. }
  131. if (!file_exists($this->installdir.$m.'.sql')) {
  132. $this->error_msg = $m.L('sql_no_exist');
  133. return false;
  134. }
  135. }
  136. }
  137. return true;
  138. }
  139. /**
  140. * 模块卸载
  141. * @param string $module 模块名
  142. */
  143. public function uninstall($module) {
  144. define('UNINSTALL', true);
  145. if (!$module) {
  146. $this->error_msg = L('illegal_parameters');
  147. return false;
  148. }
  149. $this->module = $module;
  150. $this->uninstalldir = PC_PATH.'modules'.DIRECTORY_SEPARATOR.$this->module.DIRECTORY_SEPARATOR.'uninstall'.DIRECTORY_SEPARATOR;
  151. if (!is_dir($this->uninstalldir)) {
  152. $this->error_msg = L('uninstall_dir_no_exist');
  153. return false;
  154. }
  155. if (file_exists($this->uninstalldir.'model.php')) {
  156. $models = @require($this->uninstalldir.'model.php');
  157. if (is_array($models) && !empty($models)) {
  158. foreach ($models as $m) {
  159. if (!file_exists($this->uninstalldir.$m.'.sql')) {
  160. $this->error_msg = $this->module.DIRECTORY_SEPARATOR.'uninstall'.DIRECTORY_SEPARATOR.$m.L('sql_no_exist');
  161. return false;
  162. }
  163. }
  164. }
  165. }
  166. if (is_array($models) && !empty($models)) {
  167. foreach ($models as $m) {
  168. $this->m_db = pc_base::load_model($m.'_model');
  169. $sql = file_get_contents($this->uninstalldir.$m.'.sql');
  170. $this->sql_execute($sql);
  171. }
  172. }
  173. if (file_exists($this->uninstalldir.'extention.inc.php')) {
  174. @include ($this->uninstalldir.'extention.inc.php');
  175. }
  176. if (file_exists(PC_PATH.'languages'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'lang').DIRECTORY_SEPARATOR.$this->module.'.lang.php')) {
  177. @unlink(PC_PATH.'languages'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'lang').DIRECTORY_SEPARATOR.$this->module.'.lang.php');
  178. }
  179. if (is_dir(PC_PATH.'templates'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'tpl_name').DIRECTORY_SEPARATOR.$this->module)) {
  180. @dir_delete(PC_PATH.'templates'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'tpl_name').DIRECTORY_SEPARATOR.$this->module);
  181. }
  182. $templatepath = PC_PATH.'templates'.DIRECTORY_SEPARATOR.pc_base::load_config('system', 'tpl_name').DIRECTORY_SEPARATOR;
  183. if (file_exists($templatepath.'config.php')) {
  184. $keyid = 'templates|'.pc_base::load_config('system', 'tpl_name').'|'.$this->module;
  185. $style_info = include $templatepath.'config.php';
  186. unset($style_info['file_explan'][$keyid]);
  187. @file_put_contents($templatepath.'config.php', '<?php return '.var_export($style_info, true).';?>');
  188. }
  189. $menu_db = pc_base::load_model('menu_model');
  190. $menu_db->delete(array('m'=>$this->module));
  191. $this->db->delete(array('module'=>$this->module));
  192. return true;
  193. }
  194. /**
  195. * 执行mysql.sql文件,创建数据表等
  196. * @param string $sql sql语句
  197. */
  198. private function sql_execute($sql) {
  199. $sqls = $this->sql_split($sql);
  200. if (is_array($sqls)) {
  201. foreach ($sqls as $sql) {
  202. if (trim($sql) != '') {
  203. $this->m_db->query($sql);
  204. }
  205. }
  206. } else {
  207. $this->m_db->query($sqls);
  208. }
  209. return true;
  210. }
  211. /**
  212. * 处理sql语句,执行替换前缀都功能。
  213. * @param string $sql 原始的sql,将一些大众的部分替换成私有的
  214. */
  215. private function sql_split($sql) {
  216. $dbcharset = $GLOBALS['dbcharset'];
  217. if (!$dbcharset) {
  218. $dbcharset = pc_base::load_config('database','default');
  219. $dbcharset = $dbcharset['charset'];
  220. }
  221. if($this->m_db->version() > '4.1' && $dbcharset) {
  222. $sql = preg_replace("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/", "ENGINE=\\1 DEFAULT CHARSET=".$dbcharset, $sql);
  223. }
  224. if($this->m_db->db_tablepre != "phpcms_") $sql = str_replace("phpcms_", $this->m_db->db_tablepre, $sql);
  225. $sql = str_replace(array("\r", '2010-9-05'), array("\n", date('Y-m-d')), $sql);
  226. $ret = array();
  227. $num = 0;
  228. $queriesarray = explode(";\n", trim($sql));
  229. unset($sql);
  230. foreach ($queriesarray as $query) {
  231. $ret[$num] = '';
  232. $queries = explode("\n", trim($query));
  233. $queries = array_filter($queries);
  234. foreach ($queries as $query) {
  235. $str1 = substr($query, 0, 1);
  236. if($str1 != '#' && $str1 != '-') $ret[$num] .= $query;
  237. }
  238. $num++;
  239. }
  240. return $ret;
  241. }
  242. }
  243. ?>