/other/converter/TBDevYSE_pre6/root/converter/functions.php

http://torrentpier2.googlecode.com/ · PHP · 445 lines · 372 code · 47 blank · 26 comment · 17 complexity · 7096210d70349a136b8fd57718c68714 MD5 · raw file

  1. <?php
  2. function print_ok ($sql)
  3. {
  4. global $err;
  5. echo ($err) ? "\n<br />" : '';
  6. $err = '';
  7. echo '<div>';
  8. echo "<font color=darkgreen><b>OK</b> - $sql</font>". str_repeat(' ', 256) ."\n<br />";
  9. echo '</div>';
  10. }
  11. function hex2bin($h)
  12. {
  13. if (!is_string($h)) return null;
  14. $r='';
  15. for ($a=0; $a<strlen($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
  16. return $r;
  17. }
  18. function get_max_val($table_name, $column)
  19. {
  20. $row = DB()->fetch_row("SELECT MAX($column) AS $column FROM $table_name LIMIT 1");
  21. return $row[$column];
  22. }
  23. function get_count($table_name, $column)
  24. {
  25. $row = DB()->fetch_row("SELECT COUNT($column) AS $column FROM $table_name LIMIT 1");
  26. return $row[$column];
  27. }
  28. function set_auto_increment($table_name, $column, $val = null)
  29. {
  30. if (empty($val))
  31. {
  32. $row = DB()->fetch_row("SELECT MAX($column) AS val FROM $table_name LIMIT 1");
  33. DB()->sql_freeresult();
  34. $val = (int) $row['val'] + 1;
  35. }
  36. DB()->query("ALTER TABLE $table_name auto_increment = $val");
  37. }
  38. //Users functions
  39. function tp_users_cleanup()
  40. {
  41. /*
  42. if (!function_exists('user_delete')) require_once(INC_DIR .'functions_admin.php');
  43. if ($row = DB()->fetch_row("SELECT user_id FROM ". BB_USERS ." WHERE user_id NOT IN(". EXCLUDED_USERS_CSV .');'))
  44. {
  45. foreach ($row as $user)
  46. {
  47. user_delete($user['user_id']);
  48. }
  49. }*/
  50. DB()->query('DELETE FROM '. BB_USERS .' WHERE user_id NOT IN('. EXCLUDED_USERS_CSV .')');
  51. DB()->query('TRUNCATE '. BB_BT_USERS);
  52. }
  53. function tp_user_level($tb_class)
  54. {
  55. switch($tb_class)
  56. {
  57. case 0:
  58. case 1:
  59. case 2:
  60. case 3:
  61. $level = 0;
  62. break;
  63. case 4:
  64. $level = 2;
  65. break;
  66. case 5:
  67. case 6:
  68. case 7:
  69. $level = 1;
  70. break;
  71. default:
  72. $level = 0;
  73. break;
  74. }
  75. return $level;
  76. }
  77. function convert_user($user)
  78. {
  79. $user_data = array(
  80. "user_id" => $user['id'],
  81. "user_active" => ($user['enabled'] == 'yes') ? true : false,
  82. "username" => $user['username'],
  83. "user_password" => md5($user['password']),
  84. "user_lastvisit" => $user['last_access'],
  85. "user_regdate" => $user['added'],
  86. "user_level" => tp_user_level($user['class']),
  87. "user_lang" => $user['language'],
  88. "user_dateformat" => "Y-m-d H:i",
  89. "user_opt" => 0,
  90. "user_avatar" => !empty($user['avatar']) ? $user['avatar'] : null,
  91. "user_avatar_type" => !empty($user['avatar']) ? 2 : null,
  92. "user_email" => $user['email'],
  93. "user_website" => $user['website'],
  94. );
  95. $columns = $values = array();
  96. foreach ($user_data as $column => $value)
  97. {
  98. $columns[] = $column;
  99. $values[] = "'". DB()->escape($value) ."'";
  100. }
  101. $sql_columns = implode(',', $columns);
  102. $sql_values = implode(',', $values);
  103. DB()->query("INSERT IGNORE INTO ". BB_USERS . " ($sql_columns) VALUES($sql_values);");
  104. $bt_user_data = array(
  105. "user_id" => $user['id'],
  106. "auth_key" => make_rand_str(BT_AUTH_KEY_LENGTH),
  107. "u_up_total" => $user['uploaded'],
  108. "u_down_total" => $user['downloaded'],
  109. );
  110. $columns = $values = array();
  111. foreach ($bt_user_data as $column => $value)
  112. {
  113. $columns[] = $column;
  114. $values[] = "'". DB()->escape($value) ."'";
  115. }
  116. $sql_bt_columns = implode(',', $columns);
  117. $sql_bt_values = implode(',', $values);
  118. DB()->query("INSERT IGNORE INTO ". BB_BT_USERS . " ($sql_bt_columns) VALUES($sql_bt_values);");
  119. }
  120. //Torrents and categories functions
  121. function tp_categories_cleanup()
  122. {
  123. DB()->query('DELETE FROM '. BB_CATEGORIES);
  124. }
  125. function tp_add_category_old($id, $cat_title)
  126. {
  127. DB()->query("INSERT IGNORE INTO ". BB_CATEGORIES ." (cat_id, cat_title)
  128. VALUES ($id, '". DB()->escape($cat_title) ."')");
  129. return;
  130. }
  131. function tp_add_category($cat_data)
  132. {
  133. $columns = $values = array();
  134. foreach ($cat_data as $column => $value)
  135. {
  136. $columns[] = $column;
  137. $values[] = "'". DB()->escape($value) ."'";
  138. }
  139. $sql_bt_columns = implode(',', $columns);
  140. $sql_bt_values = implode(',', $values);
  141. DB()->query("INSERT IGNORE INTO ". BB_CATEGORIES . " ($sql_bt_columns) VALUES($sql_bt_values);");
  142. }
  143. function tp_topics_cleanup()
  144. {
  145. DB()->query("TRUNCATE ". BB_ATTACHMENTS);
  146. DB()->query("TRUNCATE ". BB_ATTACHMENTS_DESC);
  147. DB()->query("TRUNCATE ". BB_BT_TORRENTS);
  148. DB()->query("TRUNCATE ". BB_POSTS);
  149. DB()->query("TRUNCATE ". BB_POSTS_HTML);
  150. DB()->query("TRUNCATE ". BB_POSTS_SEARCH);
  151. DB()->query("TRUNCATE ". BB_POSTS_TEXT);
  152. DB()->query("TRUNCATE ". BB_TOPICS);
  153. /*if (!function_exists('topic_delete')) require_once(INC_DIR .'functions_admin.php');
  154. if ($row = DB()->fetch_row("SELECT topic_id FROM ". BB_TOPICS))
  155. {
  156. foreach ($row as $topic)
  157. {
  158. topic_delete($topic['topic_id']);
  159. }
  160. }*/
  161. return;
  162. }
  163. function tp_add_topic($topic_data)
  164. {
  165. $columns = $values = array();
  166. foreach ($topic_data as $column => $value)
  167. {
  168. $columns[] = $column;
  169. $values[] = "'". DB()->escape($value) ."'";
  170. }
  171. $sql_columns = implode(',', $columns);
  172. $sql_values = implode(',', $values);
  173. DB()->query("INSERT IGNORE INTO ". BB_TOPICS . " ($sql_columns) VALUES($sql_values);");
  174. return;
  175. }
  176. function tp_add_post($post_data)
  177. {
  178. foreach ($post_data as $key => $data)
  179. {
  180. $columns = $values = array();
  181. foreach ($data as $column => $value)
  182. {
  183. $columns[] = $column;
  184. $values[] = "'". DB()->escape($value) ."'";
  185. }
  186. $sql_columns = implode(',', $columns);
  187. $sql_values = implode(',', $values);
  188. DB()->query("INSERT IGNORE INTO bb_{$key} ($sql_columns) VALUES($sql_values);");
  189. }
  190. return;
  191. }
  192. function tp_add_attach($attach_data)
  193. {
  194. foreach ($attach_data as $key => $data)
  195. {
  196. $columns = $values = array();
  197. foreach ($data as $column => $value)
  198. {
  199. $columns[] = $column;
  200. $values[] = "'". DB()->escape($value) ."'";
  201. }
  202. $sql_columns = implode(',', $columns);
  203. $sql_values = implode(',', $values);
  204. DB()->query("INSERT IGNORE INTO bb_{$key} ($sql_columns) VALUES($sql_values);");
  205. }
  206. return;
  207. }
  208. function make_img_path ($name)
  209. {
  210. global $bb_cfg;
  211. return make_url("files/images/" . $name);
  212. }
  213. function append_images($tor)
  214. {
  215. $poster = $screens = '';
  216. switch(TR_TYPE)
  217. {
  218. case 'yse':
  219. if(!empty($tor['image1']))
  220. {
  221. $poster = "[img=right]".make_img_path($tor['image1'])."[/img]";
  222. }
  223. if(!empty($tor['image2']))
  224. {
  225. $screens = '[spoiler="?????????"][img]'.make_img_path($tor['image2'])."[/img][/spoiler]";
  226. }
  227. break;
  228. case 'sky':
  229. if(!empty($tor['poster']))
  230. {
  231. $poster = "[img=right]".make_img_path($tor['poster'])."[/img]";
  232. }
  233. $has_screens = !empty($tor['screenshot1']) || !empty($tor['screenshot2']) || !empty($tor['screenshot3']) || !empty($tor['screenshot4']);
  234. if ($has_screens)
  235. {
  236. $screens .= '[spoiler="?????????"]';
  237. for ($i = 1; $i<=4; $i++)
  238. {
  239. if(!empty($tor['screenshot'.$i]))
  240. {
  241. $screens .= "[img]".make_img_path($tor['screenshot'.$i])."[/img] \n";
  242. }
  243. }
  244. $screens .= "[/spoiler]";
  245. }
  246. break;
  247. }
  248. return ($poster . $tor['descr'] . $screens);
  249. }
  250. function convert_torrent($torrent)
  251. {
  252. $topic_data = array(
  253. "topic_id" => $torrent['topic_id'],
  254. "forum_id" => $torrent['category'],
  255. "topic_title" => $torrent['name'],
  256. "topic_poster" => $torrent['owner'],
  257. "topic_time" => $torrent['added'],
  258. "topic_views" => $torrent['views'],
  259. "topic_type" => ($torrent['sticky'] == 'yes') ? 1 : 0,
  260. "topic_first_post_id" => $torrent['id'],
  261. "topic_last_post_id" => $torrent['id'],
  262. "topic_attachment" => 1,
  263. "topic_dl_type" => 1,
  264. "topic_last_post_time" => $torrent['added'],
  265. );
  266. tp_add_topic($topic_data);
  267. //$post_text = prepare_message($torrent['descr'], true, true);
  268. $post_text = stripslashes(prepare_message(addslashes(unprepare_message($torrent['descr'])), true, true));
  269. $post_data = array(
  270. "posts" => array(
  271. "post_id" => $torrent['post_id'],
  272. "topic_id" => $torrent['topic_id'],
  273. "forum_id" => $torrent['category'],
  274. "poster_id" => $torrent['owner'],
  275. "post_time" => $torrent['added'],
  276. "post_attachment" => 1,
  277. ),
  278. "posts_text" => array(
  279. "post_id" => $torrent['post_id'],
  280. "post_text" => $post_text,
  281. ),
  282. "posts_search" => array(
  283. "post_id" => $torrent['post_id'],
  284. "search_words" => $torrent['search_text'],
  285. ),
  286. );
  287. tp_add_post($post_data);
  288. $attach_data = array(
  289. "attachments" => array(
  290. "attach_id" => $torrent['attach_id'],
  291. "post_id" => $torrent['post_id'],
  292. "user_id_1" => $torrent['owner'],
  293. ),
  294. "attachments_desc" => array(
  295. "attach_id" => $torrent['attach_id'],
  296. "physical_filename" => $torrent['id'] . ".torrent",
  297. "real_filename" => $torrent['filename'],
  298. "extension" => "torrent",
  299. "mimetype" => "application/x-bittorrent",
  300. "filesize" => @filesize(get_attachments_dir() .'/'. $torrent['id'] .".torrent"),
  301. "filetime" => $torrent['added'],
  302. "tracker_status" => 1,
  303. ),
  304. );
  305. tp_add_attach($attach_data);
  306. //Torrents
  307. if (BDECODE)
  308. {
  309. $filename = get_attachments_dir() .'/'. $torrent['id'] .".torrent";
  310. if (!file_exists($filename))
  311. {
  312. return;
  313. }
  314. if (!function_exists('bdecode_file')) include_once(INC_DIR .'functions_torrent.php');
  315. $tor = bdecode_file($filename);
  316. $info = ($tor['info']) ? $tor['info'] : array();
  317. $info_hash = pack('H*', sha1(bencode($info)));
  318. $info_hash_sql = rtrim(DB()->escape($info_hash), ' ');
  319. }
  320. else
  321. {
  322. $info_hash_sql = hex2bin($torrent['info_hash']);
  323. }
  324. $torrent_data = array(
  325. "info_hash" => $info_hash_sql,
  326. "post_id" => $torrent['post_id'],
  327. "poster_id" => $torrent['owner'],
  328. "topic_id" => $torrent['topic_id'],
  329. "forum_id" => $torrent['category'],
  330. "attach_id" => $torrent['attach_id'],
  331. "size" => $torrent['size'],
  332. "reg_time" => $torrent['added'],
  333. "complete_count" => $torrent['times_completed'],
  334. "seeder_last_seen" => $torrent['lastseed'],
  335. );
  336. $columns = $values = array();
  337. foreach ($torrent_data as $column => $value)
  338. {
  339. $columns[] = $column;
  340. $values[] = "'". $db->escape($value) ."'";
  341. }
  342. $sql_columns = implode(', ', $columns);
  343. $sql_values = implode(', ', $values);
  344. DB()->query("INSERT IGNORE INTO ". BB_BT_TORRENTS . " ($sql_columns) VALUES($sql_values);");
  345. return;
  346. }
  347. //Comments functions
  348. function convert_comment($comment)
  349. {
  350. $post_text = prepare_message($comment['text'], true, true);
  351. $post_data = array(
  352. "posts" => array(
  353. "post_id" => $comment['id'],
  354. "topic_id" => $comment['torrent'],
  355. "forum_id" => $comment['category'],
  356. "poster_id" => $comment['user'],
  357. "post_time" => $comment['added'],
  358. "poster_ip" => encode_ip($comment['ip']),
  359. "post_edit_time" => $comment['editedat'],
  360. "post_edit_count" => $comment['editedat'] ? 1 : 0,
  361. ),
  362. "posts_text" => array(
  363. "post_id" => $comment['id'],
  364. "post_text" => $post_text,
  365. ),
  366. );
  367. tp_add_post($post_data);
  368. //add_search_words($comment['id'], stripslashes($post_text));
  369. return;
  370. }
  371. //Forums functions
  372. function tp_forums_cleanup()
  373. {
  374. DB()->query('TRUNCATE '. BB_FORUMS);
  375. }
  376. function convert_cat($forum, $allow_torrents = true)
  377. {
  378. $forum_data = array(
  379. "forum_id" => $forum['id'],
  380. "cat_id" => $forum['cat_id'],
  381. "forum_name" => $forum['name'],
  382. "forum_order" => $forum['sort'],
  383. "allow_reg_tracker" => $allow_torrents,
  384. "allow_porno_topic" => $allow_torrents,
  385. );
  386. $columns = $values = array();
  387. foreach ($forum_data as $column => $value)
  388. {
  389. $columns[] = $column;
  390. $values[] = "'". DB()->escape($value) ."'";
  391. }
  392. $sql_columns = implode(',', $columns);
  393. $sql_values = implode(',', $values);
  394. DB()->query("INSERT IGNORE INTO ". BB_FORUMS . " ($sql_columns) VALUES($sql_values);");
  395. return;
  396. }