PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/xoops_trust_path/modules/bulletin/oninstall.php

http://xoopscube-modules.googlecode.com/
PHP | 161 lines | 138 code | 15 blank | 8 comment | 36 complexity | ae4fa0149cd808dc679f686bcd55eaf4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. eval( ' function xoops_module_install_'.$mydirname.'( $module ) { return bulletin_oninstall_base( $module , "'.$mydirname.'" ) ; } ' ) ;
  3. function bulletin_oninstall_base( $module , $mydirname )
  4. {
  5. // transations on module install
  6. global $ret ;
  7. $db =& Database::getInstance() ;
  8. $mid = $module->getVar('mid') ;
  9. // for Cube 2.1
  10. if( defined('XOOPS_CUBE_LEGACY')) {
  11. $isCube = true ;
  12. $root =& XCube_Root::getSingleton();
  13. $root->mDelegateManager->add("Module.Legacy.ModuleInstall.Success", 'bulletin_message_append_oninstall') ;
  14. $ret = array() ;
  15. } else {
  16. $isCube = false ;
  17. if( ! is_array( $ret ) ) $ret = array() ;
  18. }
  19. // transations on module installation
  20. $bulletin_posting_permissions = array( 1 , 2 , 3 , 7 ) ;
  21. $gperm_handler = xoops_gethandler('groupperm') ;
  22. foreach( $bulletin_posting_permissions as $itemid ) {
  23. $gperm =& $gperm_handler->create() ;
  24. $gperm->setVar( 'gperm_groupid' , 1 ) ;
  25. $gperm->setVar( 'gperm_name' , 'bulletin_permit' );
  26. $gperm->setVar( 'gperm_modid' , $mid ) ;
  27. $gperm->setVar( 'gperm_itemid' , $itemid ) ;
  28. $gperm_handler->insert( $gperm ) ;
  29. }
  30. // TABLES (loading mysql.sql)
  31. $sql_file_path = dirname(__FILE__).'/sql/mysql.sql' ;
  32. $prefix_mod = $db->prefix() . '_' . $mydirname ;
  33. if( file_exists( $sql_file_path ) ) {
  34. $ret[] = "SQL file found at <b>".htmlspecialchars($sql_file_path)."</b>.<br /> Creating tables...<br />";
  35. if( file_exists( XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php' ) ) {
  36. include_once XOOPS_ROOT_PATH.'/class/database/oldsqlutility.php' ;
  37. $sqlutil =& new OldSqlUtility ;
  38. } else {
  39. include_once XOOPS_ROOT_PATH.'/class/database/sqlutility.php' ;
  40. $sqlutil =& new SqlUtility ;
  41. }
  42. $sql_query = trim( file_get_contents( $sql_file_path ) ) ;
  43. $sqlutil->splitMySqlFile( $pieces , $sql_query ) ;
  44. $created_tables = array() ;
  45. foreach( $pieces as $piece ) {
  46. $prefixed_query = $sqlutil->prefixQuery( $piece , $prefix_mod ) ;
  47. if( ! $prefixed_query ) {
  48. $ret[] = "Invalid SQL <b>".htmlspecialchars($piece)."</b><br />";
  49. return false ;
  50. }
  51. if( ! $db->query( $prefixed_query[0] ) ) {
  52. $ret[] = '<b>'.htmlspecialchars( $db->error() ).'</b><br />' ;
  53. var_dump( $db->error() ) ;
  54. return false ;
  55. } else {
  56. if( ! in_array( $prefixed_query[4] , $created_tables ) ) {
  57. $ret[] = '&nbsp;&nbsp;Table <b>'.htmlspecialchars($prefix_mod.'_'.$prefixed_query[4]).'</b> created.<br />';
  58. $created_tables[] = $prefixed_query[4];
  59. } else {
  60. $ret[] = '&nbsp;&nbsp;Data inserted to table <b>'.htmlspecialchars($prefix_mod.'_'.$prefixed_query[4]).'</b>.</br />';
  61. }
  62. }
  63. }
  64. }
  65. // TEMPLATES
  66. $tplfile_handler =& xoops_gethandler( 'tplfile' ) ;
  67. $tpl_path = dirname(__FILE__).'/templates' ;
  68. if( $handler = @opendir( $tpl_path . '/' ) ) {
  69. while( ( $file = readdir( $handler ) ) !== false ) {
  70. if( substr( $file , 0 , 1 ) == '.' ) continue ;
  71. $file_path = $tpl_path . '/' . $file ;
  72. if( is_file( $file_path ) && substr( $file , -5 ) == '.html' ) {
  73. $mtime = intval( @filemtime( $file_path ) ) ;
  74. $tplfile =& $tplfile_handler->create() ;
  75. $tplfile->setVar( 'tpl_source' , file_get_contents( $file_path ) , true ) ;
  76. $tplfile->setVar( 'tpl_refid' , $mid ) ;
  77. $tplfile->setVar( 'tpl_tplset' , 'default' ) ;
  78. $tplfile->setVar( 'tpl_file' , $mydirname . '_' . $file ) ;
  79. $tplfile->setVar( 'tpl_desc' , '' , true ) ;
  80. $tplfile->setVar( 'tpl_module' , $mydirname ) ;
  81. $tplfile->setVar( 'tpl_lastmodified' , $mtime ) ;
  82. $tplfile->setVar( 'tpl_lastimported' , 0 ) ;
  83. $tplfile->setVar( 'tpl_type' , 'module' ) ;
  84. if( ! $tplfile_handler->insert( $tplfile ) ) {
  85. $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> to the database.</span><br />';
  86. } else {
  87. $tplid = $tplfile->getVar( 'tpl_id' ) ;
  88. $ret[] = 'Template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> added to the database. (ID: <b>'.$tplid.'</b>)<br />';
  89. // generate compiled file
  90. include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
  91. include_once XOOPS_ROOT_PATH.'/class/template.php';
  92. if( ! xoops_template_touch( $tplid ) ) {
  93. $ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b>.</span><br />';
  94. } else {
  95. $ret[] = 'Template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> compiled.</span><br />';
  96. }
  97. }
  98. }
  99. }
  100. closedir( $handler ) ;
  101. }
  102. include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php' ;
  103. include_once XOOPS_ROOT_PATH.'/class/template.php' ;
  104. xoops_template_clear_module_cache( $mid ) ;
  105. // BLOCKS
  106. $tpl_path = dirname(__FILE__).'/templates/blocks' ;
  107. if( $handler = @opendir( $tpl_path . '/' ) ) {
  108. while( ( $file = readdir( $handler ) ) !== false ) {
  109. if( substr( $file , 0 , 1 ) == '.' ) continue ;
  110. $file_path = $tpl_path . '/' . $file ;
  111. if( is_file( $file_path ) && substr( $file , -5 ) == '.html' ) {
  112. $mtime = intval( @filemtime( $file_path ) ) ;
  113. $tpl_file = $mydirname . '_' . $file;
  114. $sql = "SELECT tpl_id FROM ".$db->prefix('tplfile')." WHERE tpl_module='$mydirname' AND tpl_file='".mysql_escape_string($tpl_file)."'";
  115. list($tpl_id) = $db->fetchRow($db->query($sql));
  116. $tpl_source = file_get_contents( $file_path );
  117. if (!empty($tpl_id) && isset($tpl_source) && $tpl_source != '') {
  118. $sql = sprintf("INSERT INTO %s (tpl_id, tpl_source) VALUES (%u, %s)", $db->prefix('tplsource'), $tpl_id, $db->quoteString($tpl_source));
  119. if( !$result = $db->query($sql) ) {
  120. $ret[] = '<span style="color:#ff0000;">ERROR: Could not insert template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> to the database.</span><br />';
  121. } else {
  122. $ret[] = 'Template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> added to the database. (ID: <b>'.$tpl_id.'</b>)<br />';
  123. // generate compiled file
  124. include_once XOOPS_ROOT_PATH.'/class/template.php';
  125. if( ! xoops_template_touch( $tpl_id ) ) {
  126. $ret[] = '<span style="color:#ff0000;">ERROR: Failed compiling template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b>.</span><br />';
  127. } else {
  128. $ret[] = 'Template <b>'.htmlspecialchars($mydirname.'_'.$file).'</b> compiled.</span><br />';
  129. }
  130. }
  131. }
  132. }
  133. }
  134. closedir( $handler ) ;
  135. }
  136. return true ;
  137. }
  138. function bulletin_message_append_oninstall( &$controller , &$eventArgs )
  139. {
  140. if( is_array( @$GLOBALS['ret'] ) ) {
  141. foreach( $GLOBALS['ret'] as $message ) {
  142. $controller->mLog->add( $message ) ;
  143. }
  144. }
  145. }
  146. ?>