PageRenderTime 73ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/xoops_trust_path/modules/pico/class/PicoAutoRegisterWraps.class.php

http://xoopscube-modules.googlecode.com/
PHP | 203 lines | 131 code | 46 blank | 26 comment | 13 complexity | 486366f4bda0dc9b1b8537013f7c06ca MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. class PicoAutoRegisterWraps {
  3. var $mydirname = '' ;
  4. var $config = array() ;
  5. var $wrap_base = '' ;
  6. // constructor
  7. function PicoAutoRegisterWraps( $mydirname , $config )
  8. {
  9. $this->mydirname = $mydirname ;
  10. $this->config = $config ;
  11. $this->wrap_base = XOOPS_TRUST_PATH._MD_PICO_WRAPBASE.'/'.$mydirname ;
  12. }
  13. // public
  14. function updateContent( $content_id , $vpath )
  15. {
  16. $db =& Database::getInstance() ;
  17. $content_id = intval( $content_id ) ;
  18. $file_info = $this->getFileInfo( $vpath ) ;
  19. // check the file is newer than the contents
  20. $sql = "SELECT modified_time FROM ".$db->prefix($this->mydirname."_contents")." WHERE content_id=$content_id" ;
  21. list( $modified_time ) = $db->fetchRow( $db->query( $sql ) ) ;
  22. if( $modified_time < $file_info['mtime'] ) {
  23. // make backup
  24. require_once dirname(dirname(__FILE__)).'/include/transact_functions.php' ;
  25. pico_transact_backupcontent( $this->mydirname , $content_id ) ;
  26. // update the content
  27. $set4subject = $file_info['subject'] ? "`subject`='".mysql_real_escape_string($file_info['subject'])."'," : '' ;
  28. $sql = "UPDATE ".$db->prefix($this->mydirname."_contents")." SET $set4subject `modified_time`={$file_info['mtime']},body_cached='',for_search='',`last_cached_time`=0,modifier_uid=0,modifier_ip='' WHERE content_id=$content_id" ;
  29. $db->queryF( $sql ) ;
  30. return $db->getAffectedRows() ;
  31. }
  32. return 0 ;
  33. }
  34. // protected
  35. function getRegisteringWeight( $cat_id , $vpath )
  36. {
  37. $db =& Database::getInstance() ;
  38. list( $weight ) = $db->fetchRow( $db->query( "SELECT MAX(weight) FROM ".$db->prefix($this->mydirname."_contents")." WHERE `cat_id`=$cat_id" ) ) ;
  39. return $weight + 1 ;
  40. }
  41. // protected
  42. function getInsertSQL( $cat_id , $vpath )
  43. {
  44. $weight = $this->getRegisteringWeight( $cat_id , $vpath ) ;
  45. $file_info = $this->getFileInfo( $vpath ) ;
  46. return "SET `cat_id`=$cat_id,`vpath`='".mysql_real_escape_string($vpath)."',`subject`='".mysql_real_escape_string($file_info['subject_alt'])."',`body`='".mysql_real_escape_string($file_info['body'])."',`created_time`={$file_info['mtime']},`modified_time`={$file_info['mtime']},expiring_time=0x7fffffff,poster_uid=0,modifier_uid=0,poster_ip='',modifier_ip='',use_cache=0,weight=$weight,filters='wraps',show_in_navi=1,show_in_menu=1,allow_comment=0,visible=1,approval=1,htmlheader='',htmlheader_waiting='',body_waiting='',body_cached='',tags='',extra_fields='',for_search=''" ;
  47. }
  48. // public
  49. function registerContent( $cat_id , $vpath )
  50. {
  51. $db =& Database::getInstance() ;
  52. $cat_id = intval( $cat_id ) ;
  53. // insert a new record into the category
  54. $sql = "INSERT INTO ".$db->prefix($this->mydirname."_contents")." ".$this->getInsertSQL( $cat_id , $vpath ) ;
  55. // dare to ignore duplicate key error
  56. // if( ! $db->queryF( $sql ) ) die( _MD_PICO_ERR_SQL.__LINE__.__CLASS__ ) ;
  57. if( $db->queryF( $sql ) ) {
  58. $content_id = $db->getInsertId() ;
  59. // rebuild category tree
  60. require_once dirname(dirname(__FILE__)).'/include/transact_functions.php' ;
  61. pico_sync_cattree( $this->mydirname ) ;
  62. return $content_id ;
  63. } else {
  64. return false ;
  65. }
  66. }
  67. // protected
  68. function removeContent( $content_id )
  69. {
  70. // delete transaction
  71. require_once dirname(dirname(__FILE__)).'/include/transact_functions.php' ;
  72. pico_delete_content( $this->mydirname , $content_id ) ;
  73. }
  74. // public
  75. function syncCatvpath( $cat_id , $cat_vpath , $wrap_dir )
  76. {
  77. $content_handler = new PicoContentHandler( $this->mydirname ) ;
  78. $registered_vpaths = array_flip( $content_handler->getAutoRegisteredContents( $cat_id ) ) ;
  79. $removal_vpaths = $registered_vpaths ;
  80. $affected_rows = 0 ;
  81. $dh = opendir( $wrap_dir ) ;
  82. $additional_vpaths = array() ;
  83. while( ( $file = readdir( $dh ) ) !== false ) {
  84. if( preg_match( _MD_PICO_AUTOREGIST4PREGEX , $file ) ) {
  85. $vpath = $cat_vpath . '/' . $file ;
  86. if( isset( $removal_vpaths[ $vpath ] ) ) {
  87. // already registered
  88. unset( $removal_vpaths[ $vpath ] ) ;
  89. $affected_rows += $this->updateContent( $registered_vpaths[ $vpath ] , $vpath ) ;
  90. } else {
  91. // to be registered
  92. $additional_vpaths[ $vpath ] = 0 ;
  93. }
  94. }
  95. }
  96. closedir( $dh ) ;
  97. // remove
  98. foreach( $removal_vpaths as $vpath => $content_id ) {
  99. $this->removeContent( $content_id ) ;
  100. $affected_rows ++ ;
  101. }
  102. // register
  103. foreach( $additional_vpaths as $vpath => $content_id ) {
  104. if( $this->registerContent( $cat_id , $vpath ) ) {
  105. $affected_rows ++ ;
  106. }
  107. }
  108. return $affected_rows ;
  109. }
  110. // public
  111. function registerByCatvpath( $category_row )
  112. {
  113. $cat_id = intval( $category_row['cat_id'] ) ;
  114. $cat_vpath = str_replace( '..' , '' , $category_row['cat_vpath'] ) ;
  115. $wrap_dir = $this->wrap_base . $cat_vpath ;
  116. $affected_rows = 0 ;
  117. // trigger: mtime of the directory
  118. if( is_dir( $wrap_dir ) && filemtime( $wrap_dir ) > $category_row['cat_vpath_mtime'] ) {
  119. // do sync between the_category/contents and cat_vpath/files
  120. $affected_rows = $this->syncCatvpath( $cat_id , $cat_vpath , $wrap_dir ) ;
  121. // touch `cat_vpath_mtime`
  122. $categoryHandler = new PicoCategoryHandler( $this->mydirname ) ;
  123. $categoryHandler->touchVpathMtime( $cat_id ) ;
  124. }
  125. return $affected_rows ;
  126. }
  127. // protected
  128. function getFileInfo( $vpath )
  129. {
  130. $wrap_full_path = $this->wrap_base . $vpath ;
  131. ob_start() ;
  132. include $wrap_full_path ;
  133. $full_content = pico_convert_encoding_to_ie( ob_get_contents() ) ;
  134. ob_end_clean() ;
  135. // file name
  136. $filename = substr( strrchr( $wrap_full_path , '/' ) , 1 ) ;
  137. // parse full_content (get subject, body etc.)
  138. if( preg_match( '/\<title\>([^<>]+)\<\/title\>/is' , $full_content , $regs ) ) {
  139. $subject = pico_common_unhtmlspecialchars( $regs[1] ) ;
  140. } else {
  141. $subject = false ;
  142. }
  143. if( preg_match( '/\<body[^<>]*\>(.*)\<\/body\>/is' , $full_content , $regs ) ) {
  144. $body = $regs[1] ;
  145. } else {
  146. $body = $full_content ;
  147. }
  148. return array(
  149. 'mtime' => intval( @filemtime( $wrap_full_path ) ) ,
  150. 'subject' => $subject ,
  151. 'subject_alt' => $subject ? $subject : $filename ,
  152. 'filename' => $filename ,
  153. 'body' => $body ,
  154. ) ;
  155. }
  156. }
  157. ?>