PageRenderTime 81ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/xoops_trust_path/modules/pico/include/main_functions.php

http://xoopscube-modules.googlecode.com/
PHP | 404 lines | 293 code | 81 blank | 30 comment | 55 complexity | afe10be53bf2fbd547e98a057bd97ce7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. // this file can be included only from main or admin (not from blocks)
  3. // add fields for tree structure into $categories
  4. function pico_main_make_treeinformations( $data )
  5. {
  6. $previous_depth = -1 ;
  7. $path_to_i = array() ;
  8. for( $i = 0 ; $i < sizeof( $data ) ; $i ++ ) {
  9. $unique_path = $data[$i]['unique_path'] ;
  10. $path_to_i[ $unique_path ] = $i ;
  11. $parent_path = substr( $unique_path , 0 , strrpos( $unique_path , '.' ) ) ;
  12. if( $parent_path && isset( $path_to_i[ $parent_path ] ) ) {
  13. $data[ $path_to_i[ $parent_path ] ]['f1s'][ $data[$i]['id'] ] = strrchr( $data[$i]['unique_path'] , '.' ) ;
  14. }
  15. $depth_diff = $data[$i]['depth_in_tree'] - @$previous_depth ;
  16. $previous_depth = $data[$i]['depth_in_tree'] ;
  17. $data[$i]['ul_in'] = '' ;
  18. $data[$i]['ul_out'] = '' ;
  19. if( $depth_diff > 0 ) {
  20. if( $i > 0 ) {
  21. $data[$i-1]['first_child_id'] = $data[$i]['id'] ;
  22. }
  23. for( $j = 0 ; $j < $depth_diff ; $j ++ ) {
  24. $data[$i]['ul_in'] .= '<ul><li>' ;
  25. }
  26. } else if( $depth_diff < 0 ) {
  27. for( $j = 0 ; $j < - $depth_diff ; $j ++ ) {
  28. $data[$i-1]['ul_out'] .= '</li></ul>' ;
  29. }
  30. $data[$i-1]['ul_out'] .= '</li>' ;
  31. $data[$i]['ul_in'] = '<li>' ;
  32. } else {
  33. $data[$i-1]['ul_out'] .= '</li>' ;
  34. $data[$i]['ul_in'] = '<li>' ;
  35. }
  36. if( $i > 0 ) {
  37. $data[$i-1]['next_id'] = $data[$i]['id'] ;
  38. $data[$i]['prev_id'] = $data[$i-1]['id'] ;
  39. }
  40. }
  41. $data[ sizeof( $data ) - 1 ]['ul_out'] = str_repeat( '</li></ul>' , $previous_depth + 1 ) ;
  42. return $data ;
  43. }
  44. // get permissions of current user
  45. function pico_main_get_category_permissions_of_current_user( $mydirname , $uid = null )
  46. {
  47. $db =& Database::getInstance() ;
  48. if( $uid > 0 ) {
  49. $user_handler =& xoops_gethandler( 'user' ) ;
  50. $user =& $user_handler->get( $uid ) ;
  51. } else {
  52. $user = @$GLOBALS['xoopsUser'] ;
  53. }
  54. if( is_object( $user ) ) {
  55. $uid = intval( $user->getVar('uid') ) ;
  56. $groups = $user->getGroups() ;
  57. if( ! empty( $groups ) ) $whr = "`uid`=$uid || `groupid` IN (".implode(",",$groups).")" ;
  58. else $whr = "`uid`=$uid" ;
  59. } else {
  60. $whr = "`groupid`=".intval(XOOPS_GROUP_ANONYMOUS) ;
  61. }
  62. $sql = "SELECT c.cat_id,cp.permissions FROM ".$db->prefix($mydirname."_categories")." c LEFT JOIN ".$db->prefix($mydirname."_category_permissions")." cp ON c.cat_permission_id=cp.cat_id WHERE ($whr)" ;
  63. $result = $db->query( $sql ) ;
  64. if( $result ) while( list( $cat_id , $serialized_permissions ) = $db->fetchRow( $result ) ) {
  65. $permissions = pico_common_unserialize( $serialized_permissions ) ;
  66. if( is_array( @$ret[ $cat_id ] ) ) {
  67. foreach( $permissions as $perm_name => $value ) {
  68. @$ret[ $cat_id ][ $perm_name ] |= $value ;
  69. }
  70. } else {
  71. $ret[ $cat_id ] = $permissions ;
  72. }
  73. }
  74. if( empty( $ret ) ) return array( 0 => array() ) ;
  75. else return $ret ;
  76. }
  77. // moderator groups
  78. function pico_main_get_category_moderate_groups4show( $mydirname , $cat_id )
  79. {
  80. $db =& Database::getInstance() ;
  81. $cat_id = intval( $cat_id ) ;
  82. $ret = array() ;
  83. $sql = "SELECT g.groupid, g.name FROM ".$db->prefix($mydirname."_categories")." c LEFT JOIN ".$db->prefix($mydirname."_category_permissions")." cp ON c.cat_permission_id=cp.cat_id LEFT JOIN ".$db->prefix("groups")." g ON cp.groupid=g.groupid WHERE cp.groupid IS NOT NULL AND c.cat_id=".$cat_id." AND cp.permissions LIKE '%s:12:\"is\\_moderator\";i:1;%'" ;
  84. $mrs = $db->query( $sql ) ;
  85. while( list( $mod_gid , $mod_gname ) = $db->fetchRow( $mrs ) ) {
  86. $ret[] = array(
  87. 'gid' => $mod_gid ,
  88. 'gname' => htmlspecialchars( $mod_gname , ENT_QUOTES ) ,
  89. ) ;
  90. }
  91. return $ret ;
  92. }
  93. // moderator users
  94. function pico_main_get_category_moderate_users4show( $mydirname , $cat_id )
  95. {
  96. $db =& Database::getInstance() ;
  97. $cat_id = intval( $cat_id ) ;
  98. $ret = array() ;
  99. $sql = "SELECT u.uid, u.uname FROM ".$db->prefix($mydirname."_categories")." c LEFT JOIN ".$db->prefix($mydirname."_category_permissions")." cp ON c.cat_permission_id=cp.cat_id LEFT JOIN ".$db->prefix("users")." u ON cp.uid=u.uid WHERE cp.uid IS NOT NULL AND c.cat_id=".$cat_id." AND cp.permissions LIKE '%s:12:\"is\\_moderator\";i:1;%'" ;
  100. $mrs = $db->query( $sql ) ;
  101. while( list( $mod_uid , $mod_uname ) = $db->fetchRow( $mrs ) ) {
  102. $ret[] = array(
  103. 'uid' => $mod_uid ,
  104. 'uname' => htmlspecialchars( $mod_uname , ENT_QUOTES ) ,
  105. ) ;
  106. }
  107. return $ret ;
  108. }
  109. // select box for jumping into a specified category
  110. function pico_main_make_cat_jumpbox_options( $mydirname , $whr4cat , $cat_selected = 0 )
  111. {
  112. global $myts ;
  113. $db =& Database::getInstance() ;
  114. $ret = "" ;
  115. $sql = "SELECT c.cat_id, c.cat_title, c.cat_depth_in_tree FROM ".$db->prefix($mydirname."_categories")." c WHERE ($whr4cat) ORDER BY c.cat_order_in_tree" ;
  116. if( $result = $db->query( $sql ) ) {
  117. while( list( $cat_id , $cat_title , $cat_depth ) = $db->fetchRow( $result ) ) {
  118. $selected = $cat_id == $cat_selected ? 'selected="selected"' : '' ;
  119. $ret .= "<option value='$cat_id' $selected>".str_repeat('--',$cat_depth).$myts->makeTboxData4Show($cat_title,1,1)."</option>\n" ;
  120. }
  121. } else {
  122. $ret = "<option value=\"-1\">ERROR</option>\n";
  123. }
  124. return $ret ;
  125. }
  126. // trigger event for D3
  127. function pico_main_trigger_event( $mydirname , $category , $item_id , $event , $extra_tags=array() , $user_list=array() , $omit_user_id=null )
  128. {
  129. require_once XOOPS_TRUST_PATH.'/libs/altsys/class/D3NotificationHandler.class.php' ;
  130. $not_handler =& D3NotificationHandler::getInstance() ;
  131. $not_handler->triggerEvent( $mydirname , 'pico' , $category , $item_id , $event , $extra_tags , $user_list , $omit_user_id ) ;
  132. }
  133. // get category's moderators as array
  134. function pico_main_get_moderators( $mydirname , $cat_id )
  135. {
  136. $db =& Database::getInstance() ;
  137. $cat_id = intval( $cat_id ) ;
  138. $cat_uids = array() ;
  139. // get uid directly
  140. $sql = "SELECT `uid` FROM ".$db->prefix($mydirname."_categories")." c LEFT JOIN ".$db->prefix($mydirname."_category_permissions")." cp ON c.cat_permission_id=cp.cat_id WHERE c.`cat_id`=$cat_id AND `uid` IS NOT NULL AND permissions LIKE '%is\\_moderator\";i:1%'" ;
  141. $result = $db->query( $sql ) ;
  142. while( list( $uid ) = $db->fetchRow( $result ) ) {
  143. $cat_uids[] = $uid ;
  144. }
  145. // get uid via groupid
  146. $sql = "SELECT distinct cp.groupid FROM ".$db->prefix($mydirname."_categories")." c LEFT JOIN ".$db->prefix($mydirname."_category_permissions")." cp ON c.cat_permission_id=cp.cat_id WHERE c.`cat_id`=$cat_id AND cp.`groupid` IS NOT NULL AND permissions LIKE '%is\\_moderator\";i:1%'" ;
  147. $result = $db->query( $sql ) ;
  148. $groupids = array() ;
  149. while( list( $groupid ) = $db->fetchRow( $result ) ) {
  150. $groupids[] = $groupid ;
  151. }
  152. if( ! empty( $groupids ) ) {
  153. $sql = "SELECT distinct uid FROM ".$db->prefix("groups_users_link")." WHERE groupid IN (".implode(",",$groupids).")" ;
  154. $result = $db->query( $sql ) ;
  155. while( list( $uid ) = $db->fetchRow( $result ) ) {
  156. $cat_uids[] = $uid ;
  157. }
  158. }
  159. return array_unique( $cat_uids ) ;
  160. }
  161. // get top $content_id from $cat_id
  162. function pico_main_get_top_content_id_from_cat_id( $mydirname , $cat_id )
  163. {
  164. $db =& Database::getInstance() ;
  165. list( $content_id ) = $db->fetchRow( $db->query( "SELECT o.content_id FROM ".$db->prefix($mydirname."_contents")." o WHERE o.cat_id=".intval($cat_id)." AND o.visible AND o.created_time <= UNIX_TIMESTAMP() AND o.expiring_time > UNIX_TIMESTAMP() ORDER BY o.weight,o.content_id LIMIT 1" ) ) ;
  166. return intval( $content_id ) ;
  167. }
  168. // escape string for <a href="mailto:..."> (eg. tellafriend)
  169. function pico_main_escape4mailto( $text )
  170. {
  171. if( function_exists( 'mb_convert_encoding' ) && defined( '_MD_PICO_MAILTOENCODING' ) ) {
  172. $text = mb_convert_encoding( $text , _MD_PICO_MAILTOENCODING ) ;
  173. }
  174. return rawurlencode( $text ) ;
  175. }
  176. // get filter's informations under XOOPS_TRUST_PATH/modules/pico/filters/
  177. function pico_main_get_filter_infos( $filters_separated_pipe , $isadminormod = false )
  178. {
  179. global $xoopsModuleConfig ;
  180. // forced & prohibited filters
  181. $filters_forced = array_map( 'trim' , explode( ',' , str_replace( ':LAST' , '' , @$xoopsModuleConfig['filters_forced'] ) ) ) ;
  182. $filters_prohibited = array_map( 'trim' , explode( ',' , @$xoopsModuleConfig['filters_prohibited'] ) ) ;
  183. $filters = array() ;
  184. $dh = opendir( XOOPS_TRUST_PATH.'/modules/pico/filters' ) ;
  185. while( ( $file = readdir( $dh ) ) !== false ) {
  186. if( preg_match( '/^pico\_(.*)\.php$/' , $file , $regs ) ) {
  187. $name = $regs[1] ;
  188. $constpref = '_MD_PICO_FILTERS_' . strtoupper( $name ) ;
  189. require_once dirname(dirname(__FILE__)).'/filters/pico_'.$name.'.php' ;
  190. // check the filter is secure or not
  191. if( ! $isadminormod && defined( $constpref.'ISINSECURE' ) ) continue ;
  192. // prohibited
  193. if( in_array( $name , $filters_prohibited ) ) continue ;
  194. $filters[ $name ] = array(
  195. 'title' => defined( $constpref.'TITLE' ) ? constant( $constpref.'TITLE' ) : $name ,
  196. 'desc' => defined( $constpref.'DESC' ) ? constant( $constpref.'DESC' ) : '' ,
  197. 'weight' => defined( $constpref.'INITWEIGHT' ) ? constant( $constpref.'INITWEIGHT' ) : 0 ,
  198. 'enabled' => false ,
  199. ) ;
  200. // forced
  201. if( in_array( $name , $filters_forced ) ) {
  202. $filters[ $name ]['enabled'] = true ;
  203. $filters[ $name ]['fixed'] = true ;
  204. }
  205. }
  206. }
  207. $current_filters = explode( '|' , $filters_separated_pipe ) ;
  208. $weight = 0 ;
  209. foreach( $current_filters as $current_filter ) {
  210. if( ! empty( $filters[ $current_filter ] ) ) {
  211. $weight += 10 ;
  212. $filters[ $current_filter ]['weight'] = $weight ;
  213. $filters[ $current_filter ]['enabled'] = true ;
  214. }
  215. }
  216. uasort( $filters , 'pico_main_filter_cmp' ) ;
  217. return $filters ;
  218. }
  219. // for usort() in pico_main_get_filter_infos()
  220. function pico_main_filter_cmp( $a , $b )
  221. {
  222. if( $a['enabled'] != $b['enabled'] ) {
  223. return $a['enabled'] ? -1 : 1 ;
  224. } else {
  225. return $a['weight'] > $b['weight'] ? 1 : -1 ;
  226. }
  227. }
  228. // get return_uri from "ret" after editing
  229. function pico_main_parse_ret2uri( $mydirname , $ret )
  230. {
  231. if( preg_match( '/^([a-z]{2})([0-9-]*)$/' , $ret , $regs ) ) {
  232. // specify it by codes inside the module like ret=mm, ret=mc0 or ret=ac0
  233. $id = intval( $regs[2] ) ;
  234. switch( $regs[1] ) {
  235. case 'ac' :
  236. return XOOPS_URL.'/modules/'.$mydirname.'/admin/index.php?page=contents&cat_id='.$id ;
  237. case 'mc' :
  238. return XOOPS_URL.'/modules/'.$mydirname.'/index.php?cat_id='.$id ;
  239. case 'mm' :
  240. return XOOPS_URL.'/modules/'.$mydirname.'/index.php?page=menu' ;
  241. default :
  242. return false ;
  243. }
  244. } else if( $ret{0} == '/' ) {
  245. // specify the relative link inside XOOPS_URL
  246. return XOOPS_URL.str_replace( '..' , '' , preg_replace( '/[\x00-\x1f]/' , '' , $ret ) ) ;
  247. } else {
  248. return false ;
  249. }
  250. }
  251. // get <link> to CSS for main
  252. function pico_main_render_moduleheader( $mydirname , $mod_config , $appendix_header4disp = '' )
  253. {
  254. $css_uri4disp = htmlspecialchars( @$mod_config['css_uri'] , ENT_QUOTES ) ;
  255. $header4disp = '<link rel="stylesheet" type="text/css" media="all" href="'.$css_uri4disp.'" />'."\n".@$mod_config['htmlheader']."\n".$appendix_header4disp."\n" ;
  256. $searches = array( '{mod_url}' , '<{$mod_url}>' , '<{$mydirname}>' , '{X_SITEURL}' , '<{$xoops_url}>' ) ;
  257. $replacements = array( XOOPS_URL.'/modules/'.$mydirname , XOOPS_URL.'/modules/'.$mydirname , $mydirname , XOOPS_URL.'/' , XOOPS_URL ) ;
  258. return str_replace( $searches , $replacements , $header4disp ) ;
  259. }
  260. // get directories recursively under WRAP
  261. function pico_main_get_wraps_directories_recursively( $mydirname , $dir_path = '/' )
  262. {
  263. $full_dir_path = XOOPS_TRUST_PATH._MD_PICO_WRAPBASE.'/'.$mydirname.$dir_path ; if( ! is_dir( $full_dir_path ) ) return array() ;
  264. $dir_path4key = substr( $dir_path , 0 , -1 ) ;
  265. $full_dir_path4disp = htmlspecialchars( 'XOOPS_TRUST_PATH'._MD_PICO_WRAPBASE.'/'.$mydirname.$dir_path4key , ENT_QUOTES ) ;
  266. // make an option will be displayed
  267. $db =& Database::getInstance() ;
  268. $myrow = $db->fetchArray( $db->query( "SELECT cat_title,cat_depth_in_tree FROM ".$db->prefix($mydirname."_categories")." WHERE cat_vpath='".addslashes($dir_path4key)."'" ) ) ;
  269. $ret[ $dir_path4key ] = empty( $myrow ) ? $full_dir_path4disp : $full_dir_path4disp.' ('.str_repeat('--',$myrow['cat_depth_in_tree']).htmlspecialchars( $myrow['cat_title'] , ENT_QUOTES ).')' ;
  270. // sub directries loop (1)
  271. $dir_tmps = array() ;
  272. $dh = opendir( $full_dir_path ) ;
  273. while( ( $file = readdir( $dh ) ) !== false ) {
  274. if( substr( $file , 0 , 1 ) == '.' ) continue ;
  275. if( is_dir( $full_dir_path . $file ) ) {
  276. $dir_tmps[] = $file ;
  277. }
  278. }
  279. closedir( $dh ) ;
  280. // sub directries loop (2)
  281. foreach( $dir_tmps as $dir_tmp ) {
  282. $ret += pico_main_get_wraps_directories_recursively( $mydirname , $dir_path.$dir_tmp.'/' ) ;
  283. }
  284. return $ret ;
  285. }
  286. // get files recursively under WRAP
  287. function pico_main_get_wraps_files_recursively( $mydirname , $dir_path = '/' )
  288. {
  289. $full_dir_path = XOOPS_TRUST_PATH._MD_PICO_WRAPBASE.'/'.$mydirname.$dir_path ; if( ! is_dir( $full_dir_path ) ) return array() ;
  290. $ret = array() ;
  291. $db =& Database::getInstance() ;
  292. // parse currenct directry
  293. $dir_tmps = array() ;
  294. $file_tmps = array() ;
  295. $dh = opendir( $full_dir_path ) ;
  296. while( ( $file = readdir( $dh ) ) !== false ) {
  297. if( substr( $file , 0 , 1 ) == '.' ) continue ;
  298. if( is_dir( $full_dir_path . $file ) ) {
  299. $dir_tmps[] = $file ;
  300. } else if( is_file( $full_dir_path . $file ) ) {
  301. $ext = strtolower( substr( strrchr( $file , '.' ) , 1 ) ) ;
  302. if( in_array( $ext , explode( '|' , _MD_PICO_EXTS4HTMLWRAPPING ) ) ) {
  303. $file_tmps[] = $file ;
  304. }
  305. }
  306. }
  307. closedir( $dh ) ;
  308. // files
  309. foreach( $file_tmps as $file_tmp ) {
  310. $file_path4key = $dir_path . $file_tmp ;
  311. $ret[ $file_path4key ] = htmlspecialchars( 'XOOPS_TRUST_PATH'._MD_PICO_WRAPBASE.'/'.$mydirname.$file_path4key , ENT_QUOTES ) ;
  312. $myrow = $db->fetchArray( $db->query( "SELECT subject FROM ".$db->prefix($mydirname."_contents")." WHERE vpath='".addslashes($file_path4key)."'" ) ) ;
  313. if( ! empty( $myrow ) ) {
  314. $ret[ $file_path4key ] .= ' (' . htmlspecialchars( xoops_substr( $myrow['subject'] , 0 , 20 ) , ENT_QUOTES ) . ')' ;
  315. }
  316. }
  317. // subdirs
  318. foreach( $dir_tmps as $dir_tmp ) {
  319. $ret += pico_main_get_wraps_files_recursively( $mydirname , $dir_path.$dir_tmp.'/' ) ;
  320. }
  321. return $ret ;
  322. }
  323. ?>