/** * Рассылка информации по трекерам */ function send2trackers($name, $data) { global $tracklist; $out = array('status' => 1); $error = array(); switch ($name) { // Обновление правил case 'rules_update': $rules_cache = array(); foreach ($data as $rule_name => $arr_items) { $i = 1; $arr_rules = array(); foreach ($arr_items as $row) { if ($row['parent_id'] > 0) { $arr_rules[$arr_items[$row['parent_id']]['type']][] = array('value' => $arr_items[$row['parent_id']]['value'], 'rule_id' => $row['rule_id'], 'out_id' => $row['value'], 'order' => $i); $i++; } } $str_rules = serialize($arr_rules); $rules_cache[$rule_name] = $str_rules; } foreach ($tracklist as $track) { $type = substr($track['path'], 0, 4) == 'http' ? 'remote' : 'local'; if ($type == 'local') { $rules_path = $track['path'] . '/cache/rules'; if (!is_dir($rules_path)) { mkdir($rules_path); chmod($rules_path, 0777); } // Записываем новые хеши foreach ($rules_cache as $rule_name => $str_rules) { $path = $rules_path . '/.' . $rule_name; if (file_put_contents($path, $str_rules, LOCK_EX)) { chmod($path, 0777); } else { $error[] = 'Can\'t create file ' . $path; } } // Удаляем неактуальные кэши $files = dir_files($rules_path); foreach ($files as $f) { if (!array_key_exists(substr($f, 1), $rules_cache)) { unlink($rules_path . '/' . $f); } } } else { $url = $track['path'] . '/api.php?act=ping&key=' . $track['key']; $answer_text = api_send($url); $answer = json_decode($answer_text, true); if ($answer['status'] == 1) { $url = $track['path'] . '/api.php?act=rules_update&key=' . $track['key']; $answer_text = api_send($url, array('rules' => $rules_cache)); $answer = json_decode($answer_text, true); if ($answer['status'] != 1) { if (empty($answer['error'])) { $error[] = $answer['error']; } else { $error[] = 'Unknown error'; } } } else { $str_error = 'Don\'t have access to host ' . $url; if ($answer_text != '') { $str_error .= ' Answer: ' . $answer_text; } $error[] = $str_error; } } } break; // Обновление ссылок // Обновление ссылок case 'links_update': foreach ($tracklist as $track) { $type = substr($track['path'], 0, 4) == 'http' ? 'remote' : 'local'; if ($type == 'local') { $outs_path = $track['path'] . '/cache/outs'; if (!is_dir($outs_path)) { mkdir($outs_path); chmod($outs_path, 0777); } foreach ($data as $id => $link) { $path = $outs_path . '/.' . $id; if (file_put_contents($path, $link, LOCK_EX)) { chmod($path, 0777); } else { $error[] = 'Can\'t create file ' . $path; } } // Удаляем неактуальные кэши $files = dir_files($outs_path); foreach ($files as $f) { if (!array_key_exists(substr($f, 1), $data)) { unlink($outs_path . '/' . $f); } } } else { $url = $track['path'] . '/api.php?act=ping&key=' . $track['key']; $answer_text = api_send($url); $answer = json_decode($answer_text, true); if ($answer['status'] == 1) { $url = $track['path'] . '/api.php?act=links_update&key=' . $track['key']; $answer_text = api_send($url, array('links' => $data)); $answer = json_decode($answer_text, true); if ($answer['status'] != 1) { if (empty($answer['error'])) { $error[] = $answer['error']; } else { $error[] = 'Unknown error'; } } } else { $str_error = 'Don\'t have access to host ' . $url . '.'; if ($answer_text != '') { $str_error .= ' Answer: ' . $answer_text; } $error[] = $str_error; } } } break; } if (!empty($error)) { $out = array('status' => 0, 'error' => join("\n", $error)); } return $out; }
/** * Рассылка информации по трекерам */ function send2trackers($name, $data) { global $tracklist; $out = array('status' => 1); $error = array(); switch ($name) { case 'rule_update': // Обновление ОДНОГО правила // Обновление ОДНОГО правила case 'rules_update': // Обновление ВСЕХ правил $rules_cache = array(); foreach ($data as $rule_name => $arr_items) { $i = 1; $arr_rules = array(); foreach ($arr_items as $row) { if ($row['parent_id'] > 0) { $arr_rules[$arr_items[$row['parent_id']]['type']][] = array('value' => $arr_items[$row['parent_id']]['value'], 'rule_id' => $row['rule_id'], 'out_id' => $row['value'], 'order' => $i); $i++; } } $str_rules = empty($arr_rules) ? '' : serialize($arr_rules); $rules_cache[$rule_name] = $str_rules; } foreach ($tracklist as $track) { $type = substr($track['path'], 0, 4) == 'http' ? 'remote' : 'local'; // Локальный трекер if ($type == 'local') { $rules_path = $track['path'] . '/cache/rules'; dir_exists($rules_path); // Записываем новые хеши foreach ($rules_cache as $rule_name => $str_rules) { $path = $rules_path . '/.' . $rule_name; if (file_put_contents($path, $str_rules, LOCK_EX)) { chmod($path, 0777); } else { $error[] = 'Can\'t create file ' . $path; } } // Удаляем кэши, которые есть, но нам их не прислали в обновлениях if ($name == 'rules_update') { $files = dir_files($rules_path); foreach ($files as $f) { if (!array_key_exists(substr($f, 1), $rules_cache)) { unlink($rules_path . '/' . $f); } } } // Удаленный трекер } else { $url = $track['path'] . '/api.php?act=ping'; $answer_text = api_send($url); $answer = json_decode($answer_text, true); if ($answer['status'] == 1) { // Полное или частичное обновление $url = $track['path'] . '/api.php?act=rules_update' . ($name == 'rules_update' ? '&full=1' : ''); $answer_text = api_send($url, array('cache' => $rules_cache, 'key' => $track['key'])); $answer = json_decode($answer_text, true); if ($answer['status'] != 1) { if (empty($answer['error'])) { $error[] = $answer['error']; } else { $error[] = 'Unknown error. Answer: ' . $answer_text; } } } else { $str_error = 'Don\'t have access to host ' . $url; if ($answer_text != '') { $str_error .= ' Answer: ' . $answer_text; } $error[] = $str_error; } } } break; case 'out_update': // Обновление одной ссылки // Обновление одной ссылки case 'outs_update': // Обновление всех ссылок foreach ($tracklist as $track) { $type = substr($track['path'], 0, 4) == 'http' ? 'remote' : 'local'; if ($type == 'local') { $outs_path = $track['path'] . '/cache/outs'; dir_exists($outs_path); foreach ($data as $id => $link) { $path = $outs_path . '/.' . $id; if (file_put_contents($path, $link, LOCK_EX)) { chmod($path, 0777); } else { $error[] = 'Can\'t create file ' . $path; } } // Удаляем неактуальные кэши $files = dir_files($outs_path); foreach ($files as $f) { if (!array_key_exists(substr($f, 1), $data)) { unlink($outs_path . '/' . $f); } } } else { $url = $track['path'] . '/api.php?act=ping'; $answer_text = api_send($url); $answer = json_decode($answer_text, true); if ($answer['status'] == 1) { $url = $track['path'] . '/api.php?act=outs_update' . ($name == 'outs_update' ? '&full=1' : ''); $answer_text = api_send($url, array('cache' => $data, 'key' => $track['key'])); $answer = json_decode($answer_text, true); if ($answer['status'] != 1) { if (empty($answer['error'])) { $error[] = $answer['error']; } else { $error[] = 'Unknown error'; } } } else { $str_error = 'Don\'t have access to host ' . $url . '.'; if ($answer_text != '') { $str_error .= ' Answer: ' . $answer_text; } $error[] = $str_error; } } } break; } if (!empty($error)) { $out = array('status' => 0, 'error' => join("\n", $error)); } return $out; }