Advertisement
Guest User

Untitled

a guest
Jul 4th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.78 KB | None | 0 0
  1. <?php class WPFB_Sync {
  2.  
  3. static function DEcho($str) {
  4. echo $str;
  5. @ob_flush();
  6. @flush();
  7. }
  8.  
  9. static function UpdateItemsPath() {
  10. wpfb_loadclass('File','Category');
  11. $cats = WPFB_Category::GetCats();
  12. $files = WPFB_File::GetFiles2();
  13. foreach(array_keys($cats) as $i) $cats[$i]->Lock(true);
  14. foreach(array_keys($files) as $i) $files[$i]->GetLocalPath(true);
  15. foreach(array_keys($cats) as $i) {
  16. $cats[$i]->Lock(false);
  17. $cats[$i]->DBSave();
  18. }
  19. }
  20.  
  21. static function SyncCats()
  22. {
  23. $updated_cats = array();
  24.  
  25. // sync file count
  26. $cats = WPFB_Category::GetCats();
  27. foreach(array_keys($cats) as $i)
  28. {
  29. $cat = $cats[$i];
  30. $child_files = $cat->GetChildFiles(false);
  31. $num_files = (int)count($child_files);
  32. $num_files_total = (int)count($cat->GetChildFiles(true));
  33. if($num_files != $cat->cat_num_files || $num_files_total != $cat->cat_num_files_total)
  34. {
  35. $cat->cat_num_files = $num_files;
  36. $cat->cat_num_files_total = $num_files_total;
  37. $cat->DBSave();
  38. $updated_cats[] = $cat;
  39. }
  40.  
  41. // update category names
  42. if($child_files) {
  43. foreach($child_files as $file) {
  44. if($file->file_category_name != $cat->GetTitle()) {
  45. $file->file_category_name = $cat->GetTitle();
  46. if(!$file->locked)
  47. $file->DBSave();
  48. }
  49. }
  50. }
  51.  
  52. @chmod ($cat->GetLocalPath(), octdec(WPFB_PERM_DIR));
  53. }
  54.  
  55. return $updated_cats;
  56. }
  57.  
  58. static function Sync($hash_sync=false, $output=false)
  59. {
  60. @ini_set('max_execution_time', '0');
  61. @set_time_limit(0);
  62.  
  63. wpfb_loadclass("Admin", "GetID3");
  64. require_once(ABSPATH . 'wp-admin/includes/file.php');
  65.  
  66. $result = array('missing_files' => array(), 'missing_folders' => array(), 'changed' => array(), 'not_added' => array(), 'error' => array(), 'updated_categories' => array());
  67.  
  68. $sync_id3 = !WPFB_Core::GetOpt('disable_id3');
  69.  
  70. // some syncing/updating
  71. self::UpdateItemsPath();
  72. WPFB_Admin::SyncCustomFields();
  73.  
  74. $files = WPFB_File::GetFiles2();
  75. $cats = WPFB_Category::GetCats();
  76.  
  77. if($output) self::DEcho('<p>'. __('Checking for file changes...',WPFB).' ');
  78. $db_files = array();
  79. foreach($files as $id => /* & PHP 4 compability */ $file)
  80. {
  81. $file_path = str_replace('//','/',str_replace('\\', '/', $file->GetLocalPath(true)));
  82. $db_files[] = $file_path;
  83. if($file->GetThumbPath())
  84. $db_files[] = str_replace('//','/',str_replace('\\', '/', $file->GetThumbPath()));
  85.  
  86. if($file->file_category > 0 && is_null($file->GetParent()))
  87. $result['warnings'][] = sprintf(__('Category (ID %d) of file %s does not exist!', WPFB), $file->file_category, $file->GetLocalPathRel());
  88.  
  89. // TODO: check for file changes remotly
  90. if($file->IsRemote())
  91. continue;
  92.  
  93. if(!@is_file($file_path) || !@is_readable($file_path))
  94. {
  95. $result['missing_files'][$id] = $file;
  96. continue;
  97. }
  98.  
  99. if($hash_sync) $file_hash = @md5_file($file_path);
  100. $file_size = (int)@filesize($file_path);
  101. $file_mtime = filemtime($file_path);
  102. $file_analyzetime = !$sync_id3 ? $file_mtime : WPFB_GetID3::GetFileAnalyzeTime($file);
  103. if(is_null($file_analyzetime)) $file_analyzetime = 0;
  104.  
  105. if( ($hash_sync && $file->file_hash != $file_hash)
  106. || $file->file_size != $file_size || $file->file_mtime != $file_mtime
  107. || $file_analyzetime < $file_mtime)
  108. {
  109. $file->file_size = $file_size;
  110. $file->file_mtime = $file_mtime;
  111. $file->file_hash = $hash_sync ? $file_hash : @md5_file($file_path);
  112.  
  113. if($sync_id3)
  114. WPFB_GetID3::UpdateCachedFileInfo($file);
  115.  
  116. $res = $file->DBSave();
  117.  
  118. if(!empty($res['error']))
  119. $result['error'][$id] = $file;
  120. else
  121. $result['changed'][$id] = $file;
  122. }
  123. }
  124. if($output) self::DEcho('done!</p>');
  125.  
  126. foreach($cats as $id => $cat) {
  127. $cat_path = $cat->GetLocalPath(true);
  128. if(!@is_dir($cat_path) || !@is_readable($cat_path))
  129. {
  130. $result['missing_folders'][$id] = $cat;
  131. continue;
  132. }
  133. }
  134.  
  135. if($output) self::DEcho('<p>'. __('Searching for new files...',WPFB).' ');
  136.  
  137. // search for not added files
  138. $upload_dir = str_replace('//','/',str_replace('\\', '/', WPFB_Core::UploadDir()));
  139. $upload_dir_len = strlen($upload_dir);
  140.  
  141. $all_files = str_replace('//','/',str_replace('\\', '/', list_files($upload_dir)));
  142. $num_all_files = count($all_files);
  143.  
  144. $new_files = array();
  145. $num_new_files = 0;
  146. $num_files_to_add = 0;
  147.  
  148. // 1ps filter (check extension, special file names, and filter existing file names and thumbnails)
  149. $fext_blacklist = array_map('strtolower', array_map('trim', explode(',', WPFB_Core::GetOpt('fext_blacklist'))));
  150. for($i = 0; $i < $num_all_files; $i++)
  151. {
  152. $fn = $all_files[$i];
  153. $fbn = basename($fn);
  154. if(strlen($fn) < 2 || $fbn{0} == '.' || strpos($fn, '/.tmp') !== false
  155. || $fbn == '_wp-filebase.css' || strpos($fbn, '_caticon.') !== false
  156. || in_array($fn, $db_files)
  157. || !is_file($fn) || !is_readable($fn)
  158. || (!empty($fext_blacklist) && in_array(trim(strrchr($fbn, '.'),'.'), $fext_blacklist)) // check for blacklisted extension
  159. )
  160. continue;
  161. $new_files[$num_new_files] = $fn;
  162. $num_new_files++;
  163. }
  164.  
  165. $num_files_to_add = $num_new_files;
  166.  
  167.  
  168.  
  169. $thumbnails = array();
  170. // look for thumnails
  171. // find files that have names formatted like thumbnails e.g. file-XXxYY.(jpg|jpeg|png|gif)
  172. for($i = 1; $i < $num_new_files; $i++)
  173. {
  174. $len = strrpos($new_files[$i], '.');
  175.  
  176. // file and thumbnail should be neighbours in the list, so only check the prev element for matching name
  177. if(strlen($new_files[$i-1]) > ($len+2) && substr($new_files[$i-1],0,$len) == substr($new_files[$i],0,$len) && !in_array($new_files[$i-1], $db_files))
  178. {
  179. $suffix = substr($new_files[$i-1], $len);
  180.  
  181. $matches = array();
  182. if(preg_match(WPFB_File::$thumbnail_regex, $suffix, $matches) && ($is = getimagesize($new_files[$i-1])))
  183. {
  184. if($is[0] == $matches[1] && $is[1] == $matches[2])
  185. {
  186. //ok, found a thumbnail here
  187. $thumbnails[$new_files[$i]] = basename($new_files[$i-1]);
  188. $new_files[$i-1] = ''; // remove the file from the list
  189. $num_files_to_add--;
  190. continue;
  191. }
  192. }
  193. }
  194. }
  195.  
  196.  
  197. if(WPFB_Core::GetOpt('base_auto_thumb')) {
  198. for($i = 0; $i < $num_new_files; $i++)
  199. {
  200. $len = strrpos($new_files[$i], '.');
  201. $ext = strtolower(substr($new_files[$i], $len+1));
  202.  
  203. if($ext != 'jpg' && $ext != 'png' && $ext != 'gif') {
  204. $prefix = substr($new_files[$i], 0, $len);
  205.  
  206. for($ii = $i-1; $ii >= 0; $ii--)
  207. {
  208. if(substr($new_files[$ii],0, $len) != $prefix) break;
  209. $e = strtolower(substr($new_files[$ii], $len+1));
  210. if($e == 'jpg' || $e == 'png' || $e == 'gif') {
  211. $thumbnails[$new_files[$i]] = basename($new_files[$ii]);
  212. $new_files[$ii] = ''; // remove the file from the list
  213. $num_files_to_add--;
  214. break;
  215. }
  216. }
  217.  
  218. for($ii = $i+1; $ii < $num_new_files; $ii++)
  219. {
  220. if(substr($new_files[$ii],0, $len) != $prefix) break;
  221. $e = strtolower(substr($new_files[$ii], $len+1));
  222. if($e == 'jpg' || $e == 'png' || $e == 'gif') {
  223. $thumbnails[$new_files[$i]] = basename($new_files[$ii]);
  224. $new_files[$ii] = ''; // remove the file from the list
  225. $num_files_to_add--;
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. }
  232.  
  233. if($output && $num_files_to_add > 0) {
  234. echo "<p>";
  235. printf(__('%d Files found, %d new.', WPFB), $num_all_files, $num_files_to_add);
  236. echo "</p>";
  237.  
  238. include(WPFB_PLUGIN_ROOT.'extras/progressbar.class.php');
  239. $progress_bar = new progressbar(0, $num_files_to_add);
  240. $progress_bar->print_code();
  241. } else {
  242. if($output) self::DEcho('done!</p>');
  243. }
  244.  
  245. for($i = 0; $i < $num_new_files; $i++)
  246. {
  247. $fn = $new_files[$i];
  248. if(empty($fn)) continue;
  249. $fbn = basename($fn);
  250.  
  251. $res = WPFB_Admin::AddExistingFile($fn, empty($thumbnails[$fn]) ? null : $thumbnails[$fn]);
  252. if(empty($res['error']))
  253. $result['added'][] = empty($res['file']) ? substr($fn, $upload_dir_len) : $res['file'];
  254. else
  255. $result['error'][] = $res['error'] . " (file $fn)";
  256.  
  257. if(!empty($progress_bar))
  258. $progress_bar->step(1);
  259. }
  260.  
  261. if(!empty($progress_bar))
  262. $progress_bar->complete();
  263.  
  264. // chmod
  265. if($output) self::DEcho('<p>Setting permissions...');
  266. @chmod ($upload_dir, octdec(WPFB_PERM_DIR));
  267. for($i = 0; $i < count($db_files); $i++)
  268. {
  269. if(file_exists($db_files[$i]))
  270. {
  271. @chmod ($db_files[$i], octdec(WPFB_PERM_FILE));
  272. if(!is_writable($db_files[$i]) && !is_writable(dirname($db_files[$i])))
  273. $result['warnings'][] = sprintf(__('File <b>%s</b> is not writable!', WPFB), substr($db_files[$i], $upload_dir_len));
  274. }
  275. }
  276. if($output) self::DEcho('done!</p>');
  277.  
  278. // sync categories
  279. if($output) self::DEcho('<p>Syncing categories... ');
  280. $result['updated_categories'] = self::SyncCats();
  281. if($output) self::DEcho('done!</p>');
  282.  
  283. wpfb_call('Setup','ProtectUploadPath');
  284. WPFB_File::UpdateTags();
  285.  
  286. return $result;
  287. }
  288.  
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement