PageRenderTime 38ms CodeModel.GetById 12ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

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