PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/xoops_trust_path/modules/d3pipes/include/common_functions.php

http://xoopscube-modules.googlecode.com/
PHP | 313 lines | 225 code | 64 blank | 24 comment | 56 complexity | 7527e9711ea5aeb1ae8fe6cd24e0b55c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. function d3pipes_common_convert_encoding_utf8toie( $mydirname , $string_utf8 )
  3. {
  4. if( _CHARSET == 'UTF-8' ) {
  5. return $string_utf8 ;
  6. } else if( function_exists( 'mb_convert_encoding' ) ) {
  7. return mb_convert_encoding( $string_utf8 , _CHARSET , 'UTF-8' ) ;
  8. } else if( function_exists( 'iconv' ) ) {
  9. return iconv( $string_utf8 , 'UTF-8' , _CHARSET ) ;
  10. } else {
  11. return utf8_decode( $string_utf8 ) ;
  12. }
  13. }
  14. function d3pipes_common_convert_encoding_ietoutf8( $mydirname , $string_ie )
  15. {
  16. if( _CHARSET == 'UTF-8' ) {
  17. return $string_ie ;
  18. } else if( function_exists( 'mb_convert_encoding' ) ) {
  19. return mb_convert_encoding( $string_ie , 'UTF-8' , _CHARSET ) ;
  20. } else if( function_exists( 'iconv' ) ) {
  21. return iconv( $string_ie , _CHARSET , 'UTF-8' ) ;
  22. } else {
  23. return utf8_encode( $string_ie ) ;
  24. }
  25. }
  26. function d3pipes_common_filter_ietoutf8( $string_ie )
  27. {
  28. if( is_string( $string_ie ) ) return d3pipes_common_convert_encoding_ietoutf8( '' , $string_ie ) ;
  29. return $string_ie ;
  30. }
  31. function d3pipes_common_get_submenu( $mydirname , $caller = 'xoops_version' )
  32. {
  33. $module_handler =& xoops_gethandler('module') ;
  34. $module =& $module_handler->getByDirname( $mydirname ) ;
  35. if( ! is_object( $module ) ) return array() ;
  36. $config_handler =& xoops_gethandler('config') ;
  37. $mod_config =& $config_handler->getConfigsByCat( 0 , $module->getVar('mid') ) ;
  38. $db =& Database::getInstance() ;
  39. $myts =& MyTextSanitizer::getInstance();
  40. // pipes query
  41. $sql = "SELECT pipe_id,name FROM ".$db->prefix($mydirname."_pipes")." WHERE in_submenu ORDER BY weight" ;
  42. $prs = $db->query( $sql ) ;
  43. $submenus = array() ;
  44. if( $prs ) while( $pipe_row = $db->fetchArray( $prs ) ) {
  45. $pipe_id = intval( $pipe_row['pipe_id'] ) ;
  46. $submenus[ $pipe_id ] = array(
  47. 'name' => $myts->makeTboxData4Show( $pipe_row['name'] ) ,
  48. 'url' => 'index.php?page=eachpipe&amp;pipe_id='.$pipe_id ,
  49. ) ;
  50. }
  51. return $submenus ;
  52. }
  53. function d3pipes_common_update_joint_option( $mydirname , $pipe_id , $joint_type , $option )
  54. {
  55. $db =& Database::getInstance() ;
  56. $pipe_id = intval( $pipe_id ) ;
  57. list( $joints_serialized ) = $db->fetchRow( $db->query( "SELECT joints FROM ".$db->prefix($mydirname."_pipes")." WHERE pipe_id=$pipe_id" ) ) ;
  58. if( ! empty( $joints_serialized ) && $joints = unserialize( $joints_serialized ) ) {
  59. foreach( array_keys( $joints ) as $i ) {
  60. if( $joints[ $i ]['joint'] == $joint_type ) {
  61. $joints[ $i ][ 'option' ] = $option ;
  62. }
  63. }
  64. $db->queryF( "UPDATE ".$db->prefix($mydirname."_pipes")." SET joints='".mysql_real_escape_string(serialize($joints))."' WHERE pipe_id=$pipe_id" ) ;
  65. }
  66. }
  67. function d3pipes_common_get_default_joint_class( $mydirname , $joint_type )
  68. {
  69. $db =& Database::getInstance() ;
  70. list( $ret ) = $db->fetchRow( $db->query( "SELECT default_class FROM ".$db->prefix($mydirname."_joints")." WHERE joint_type='".mysql_real_escape_string($joint_type)."'" ) ) ;
  71. if( empty( $ret ) ) {
  72. $classes_base = XOOPS_TRUST_PATH.'/modules/d3pipes/joints/'.$joint_type ;
  73. if( $handler = @opendir( $classes_base ) ) {
  74. while( ( $file = readdir( $handler ) ) !== false ) {
  75. if( substr( $file , 0 , 1 ) == '.' ) continue ;
  76. $file = str_replace( '..' , '' , $file ) ;
  77. if( file_exists( $classes_base . '/' . $file ) ) {
  78. $ret = strtolower( substr( $file , strlen( 'D3pipes'.$joint_type ) , - strlen( '.class.php' ) ) ) ;
  79. break ;
  80. }
  81. }
  82. }
  83. }
  84. return $ret ;
  85. }
  86. function d3pipes_common_cache_path_base( $mydirname )
  87. {
  88. $salt = substr( md5( XOOPS_ROOT_PATH . XOOPS_DB_USER . XOOPS_DB_PREFIX ) , 0 , 6 ) ;
  89. $base = XOOPS_TRUST_PATH.'/cache/'.$mydirname.'_'.$salt.'_' ;
  90. return $base ;
  91. }
  92. function d3pipes_common_delete_all_cache( $mydirname , $pipe_id = 0 , $with_fetch = true , $with_ping = true )
  93. {
  94. $base = d3pipes_common_cache_path_base( $mydirname ) ;
  95. $prefix = substr( strrchr( $base , '/' ) , 1 ) ;
  96. $prefix_length = strlen( $prefix ) ;
  97. $basedir = substr( $base , 0 , - $prefix_length ) ;
  98. if( $handler = @opendir( $basedir ) ) {
  99. while( ( $file = readdir( $handler ) ) !== false ) {
  100. if( strncmp( $file , $prefix , $prefix_length ) === 0 ) {
  101. // save 'fetch' cache if necessary
  102. if( ! $with_fetch && substr( $file , -5 ) == 'fetch' ) continue ;
  103. // save 'ping' cache if necessary
  104. if( ! $with_ping && substr( $file , -4 ) == 'ping' ) continue ;
  105. if( $pipe_id > 0 ) {
  106. // only specified pipe's cache
  107. if( intval( substr( $file , $prefix_length + 1 ) ) == $pipe_id ) {
  108. @unlink( $basedir . $file ) ;
  109. }
  110. } else {
  111. // all pipe's cache
  112. @unlink( $basedir . $file ) ;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. function d3pipes_common_get_clipping( $mydirname , $clipping_id )
  119. {
  120. require_once dirname(dirname(__FILE__)).'/joints/clip/D3pipesClipModuledb.class.php' ;
  121. $clip_obj = new D3pipesClipModuledb( $mydirname , 0 , '' ) ;
  122. return $clip_obj->getClipping( $clipping_id ) ;
  123. }
  124. function d3pipes_common_get_joint_objects( $mydirname , $joint_type = '' )
  125. {
  126. if( ! $joint_type ) return array() ;
  127. $type_base = dirname(dirname(__FILE__)).'/joints/'.$joint_type ;
  128. $dh = opendir( $type_base ) ;
  129. $ret = array() ;
  130. while( $file = readdir( $dh ) ) {
  131. if( substr( $file , 0 , 7 + strlen( $joint_type ) ) != 'D3pipes'.ucfirst($joint_type) ) continue ;
  132. if( substr( $file , -10 ) != '.class.php' ) continue ;
  133. $class_name = substr( $file , 0 , -10 ) ;
  134. require_once $type_base.'/'.$file ;
  135. $ret[] = new $class_name( $mydirname , 0 , '' ) ;
  136. }
  137. return $ret ;
  138. }
  139. function &d3pipes_common_get_joint_object_default( $mydirname , $joint_type , $option = '' )
  140. {
  141. $class_name = 'D3pipes'.ucfirst($joint_type).ucfirst(d3pipes_common_get_default_joint_class( $mydirname , $joint_type )) ;
  142. require_once dirname(dirname(__FILE__)).'/joints/'.$joint_type.'/'.$class_name.'.class.php' ;
  143. $ret = new $class_name( $mydirname , 0 , $option ) ;
  144. return $ret ;
  145. }
  146. function &d3pipes_common_get_joint_object( $mydirname , $joint_type , $joint_class , $option = '' )
  147. {
  148. $class_name = 'D3pipes'.ucfirst($joint_type).ucfirst($joint_class) ;
  149. require_once dirname(dirname(__FILE__)).'/joints/'.$joint_type.'/'.$class_name.'.class.php' ;
  150. $ret = new $class_name( $mydirname , 0 , $option ) ;
  151. return $ret ;
  152. }
  153. function d3pipes_common_get_pipe4assign( $mydirname , $pipe_id )
  154. {
  155. $db =& Database::getInstance() ;
  156. $myts =& MyTextSanitizer::getInstance() ;
  157. // fetch pipe_row
  158. $pipe_row = $db->fetchArray( $db->query( "SELECT * FROM ".$db->prefix($mydirname."_pipes")." WHERE pipe_id=".intval($pipe_id) ) ) ;
  159. if( empty( $pipe_row ) ) return false ;
  160. $pipe4assign = array(
  161. 'dirname' => $mydirname ,
  162. 'id' => intval( $pipe_id ) ,
  163. 'name' => $myts->makeTboxData4Show( $pipe_row['name'] ) ,
  164. 'name4xml' => htmlspecialchars( $pipe_row['name'] , ENT_QUOTES ) ,
  165. 'url' => $myts->makeTboxData4Show( $pipe_row['url'] ) ,
  166. 'url4xml' => htmlspecialchars( $pipe_row['url'] , ENT_QUOTES ) ,
  167. 'image' => $myts->makeTboxData4Show( $pipe_row['image'] ) ,
  168. 'image4xml' => htmlspecialchars( $pipe_row['image'] , ENT_QUOTES ) ,
  169. 'description' => $myts->displayTarea( $pipe_row['description'] ) ,
  170. 'description4xml' => htmlspecialchars( $myts->displayTarea( $pipe_row['description'] ) , ENT_QUOTES ) ,
  171. 'description_raw' => $pipe_row['description'] ,
  172. 'created_time_formatted' => formatTimestamp( $pipe_row['created_time'] , 'm' ) ,
  173. 'modified_time_formatted' => formatTimestamp( $pipe_row['modified_time'] , 'm' ) ,
  174. 'lastfetch_time_formatted' => $pipe_row['lastfetch_time'] ? formatTimestamp( $pipe_row['lastfetch_time'] , 'm' ) : '----' ,
  175. 'joints' => unserialize( $pipe_row['joints'] ) ,
  176. ) + $pipe_row ;
  177. if( empty( $pipe4assign['joints'] ) ) return false ;
  178. else return $pipe4assign ;
  179. }
  180. function d3pipes_common_fetch_entries( $mydirname , $pipe_row , $max_entries , &$errors , $mod_configs )
  181. {
  182. // var_dump( microtime() ) ;
  183. $errors = array() ;
  184. if( empty( $pipe_row ) ) return array() ;
  185. $db =& Database::getInstance() ;
  186. $myts =& MyTextSanitizer::getInstance() ;
  187. $joints = $pipe_row['joints'] ;
  188. $pipe_id = $pipe_row['pipe_id'] ;
  189. $joints_dir = dirname(dirname(__FILE__)).'/joints' ;
  190. include dirname(__FILE__).'/start_joints.inc.php' ;
  191. $objects = array() ;
  192. // make objects and prefetch of each joints (reversed order)
  193. $stage = sizeof( $joints ) ;
  194. foreach( array_reverse( $joints ) as $joint ) {
  195. -- $stage ;
  196. $class_name = 'D3pipes'.ucfirst($joint['joint']).ucfirst($joint['joint_class']) ;
  197. require_once $joints_dir.'/'.$joint['joint'].'/'.$class_name.'.class.php' ;
  198. if( ! class_exists( $class_name ) ) die( 'Class '.$class_name.' does not exist' ) ;
  199. //COMENT by domifara for php5.2-
  200. /*
  201. $obj =& new $class_name( $mydirname , $pipe_id , $joint['option'] ) ;
  202. $obj->setModConfigs( $mod_configs ) ;
  203. $obj->setStage( $stage ) ;
  204. $objects[ $stage ] =& $obj ;
  205. if( $obj->isCached() ) break ;
  206. */
  207. //HACK by domifara for php5.2-
  208. ${$class_name} = new $class_name( $mydirname , $pipe_id , $joint['option'] ) ;
  209. ${$class_name}->setModConfigs( $mod_configs ) ;
  210. ${$class_name}->setStage( $stage ) ;
  211. $objects[ $stage ] =& ${$class_name} ;
  212. if( ${$class_name}->isCached() ) break ;
  213. }
  214. if( empty( $objects ) ) return false ;
  215. ksort( $objects ) ;
  216. // chain data is initialized
  217. $data = array() ;
  218. // joint chains
  219. foreach( $objects as $obj ) {
  220. $data = $obj->execute( $data , $max_entries ) ;
  221. $errors = array_merge( $errors , $obj->getErrors() ) ;
  222. }
  223. // var_dump( microtime() ) ;
  224. return is_array( $data ) ? array_slice( $data , 0 , $max_entries ) : $data ;
  225. }
  226. function d3pipes_common_unserialize( $serialized_data )
  227. {
  228. if( empty( $serialized_data ) ) return array() ;
  229. // rightly formatted data
  230. $data = @unserialize( $serialized_data ) ;
  231. if( is_array( $data ) ) return $data ;
  232. // only linear arrays can be parsed
  233. $elements = preg_split( '/(s\:\d+\:\"|b\:\d+\;|i\:\d+\;)/' , $serialized_data , -1 , PREG_SPLIT_DELIM_CAPTURE ) ;
  234. $canonical_sdata = '' ;
  235. foreach( array_keys( $elements ) as $i ) {
  236. if( preg_match( '/^s\:(\d+)\:\"$/' , $elements[$i] , $regs ) ) {
  237. $length = strlen( $elements[$i+1] ) - 2 ;
  238. $canonical_sdata .= str_replace( $regs[1] , $length , $elements[$i] ) ;
  239. } else {
  240. $canonical_sdata .= $elements[$i] ;
  241. }
  242. }
  243. //var_dump( $serialized_data ) ;
  244. //var_dump( $canonical_sdata ) ;
  245. $data = @unserialize( $canonical_sdata ) ;
  246. if( is_array( $data ) ) return $data ;
  247. else return array() ;
  248. }
  249. ?>