source: trunk/scripts/link-task-forum @ 1184

Revision 997, 4.4 KB checked in by strat.cristian@…, 3 years ago (diff)

Log remote IP and X-Forwarded-For header on job submit, textblock create/edit, and attachment upload. For privacy considerations, IP addresses are only displayed to admins and helpers.

  • Contains database migration script.
  • Adds new simplified security action. (sensitive-info)
  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1#! /usr/bin/env php
2<?php
3require_once(dirname($argv[0]) . "/utilities.php");
4require_once(IA_ROOT_DIR."common/db/task.php");
5require_once(IA_ROOT_DIR."common/textblock.php");
6
7function match($a, $b) {
8    $len_a = strlen($a); 
9    $len_b = strlen($b);
10    for ($i = 0, $j = -1; $i < $len_b; ++$i) {
11        for (++$j; $j < $len_a && $a[$j] != $b[$i]; ++$j);
12        if ($j == $len_a) return false;
13    }
14    return true;
15}
16
17function link_task($task, $page, $topic, $verbose = false) {
18    if (!is_null($page["forum_topic"])) {
19        $topic_id = $page["forum_topic"];
20        // WTF, wrong topic?
21        if ($topic_id == $topic['ID_TOPIC']) {
22            if ($verbose) {
23                log_print('"'.$task['id'].'" era deja legat de topic-ul "'.$topic['subject'].'".');
24            }
25            return false;
26        }
27        log_warn($task['id'].': Topic-ul vechi "'.$topic_id.'" este diferit de cel gasit: "'.$topic['ID_TOPIC'].'"!');
28    }
29
30    // Build new page
31    $new_page = $page;
32    $new_page["forum_topic"] = $topic["ID_TOPIC"];
33    $new_page["timestamp"] = null;
34    $new_page["user_id"] = 1; // update as "domino" :)
35
36    // It worked
37    if (count(textblock_validate($new_page)) == 0) {
38        textblock_add_revision($new_page['name'], $new_page['title'],
39                               $new_page['text'], $new_page['user_id'],
40                               $new_page['security'], $new_page['timestamp'],
41                               $new_page['creation_timestamp'],
42                               $new_page['remote_ip_info']);
43    } else {
44        log_error('Eroare la validarea textblock-ului!');
45        return false;
46    }
47
48    if ($verbose) {
49        log_print('Am legat "'.$task['id'].'" de topic-ul "'.$topic['subject'].'".');
50    }
51    return true;
52}
53
54ini_set("memory_limit", "128M");
55
56db_connect();
57
58// validate argv
59log_assert($argc >= 3, "Expecting arguments: round board [verbose]");
60$round = $argv[1];
61$board = $argv[2];
62$verbose = getattr($argv, 3, false);
63
64// get subjects from board
65$query = sprintf("SELECT msg_in.ID_TOPIC, msg_in.subject FROM ia_smf_topics top_out
66                  LEFT JOIN ia_smf_messages msg_in ON
67                  msg_in.ID_MSG = (SELECT MIN(ID_MSG) FROM ia_smf_messages msg_in WHERE msg_in.ID_TOPIC = top_out.ID_TOPIC)
68                  WHERE top_out.ID_BOARD = %d AND locked = 0", 
69                  db_escape($board));
70// uppercase topic subject
71$topics = db_fetch_all($query);
72foreach ($topics as &$topic) {
73    $topic['subject'] = strtoupper($topic['subject']);
74}
75
76// get tasks
77$total = 0;
78$tasks = task_get_all();
79
80// try to match each task
81foreach ($tasks as $task) {
82    // does this task belong to the round we want?
83    $parent_rounds = task_get_parent_rounds($task['id']);
84    $found = false;
85    foreach ($parent_rounds as $round_id) {
86        if ($round_id == $round) {
87            $found = true;
88        }
89    }
90    if (!$found) {
91        continue;
92    }
93
94    // match based on task id
95    $str1 = strtoupper(($task['order']-1).$task['id']);
96   
97    // match based on textblock title
98    $page = textblock_get_revision($task['page_name']);
99    if (!$page) {
100        log_warn("Lipseste pagina pentru ".$task['id']."!");
101        continue;
102    }
103    $str2 = strtoupper(($task['order']-1).$task['title']);
104   
105    if ($task['order'] <= 10) { 
106        $str1 = "00".$str1; 
107        $str2 = "00".$str2; 
108    }
109    if ($task['order'] > 10 && $task['order'] <= 100) { 
110        $str1 = "0".$str1; 
111        $str2 = "0".$str2; 
112    }
113
114    $found = null;
115    for ($i = count($topics)-1; $i >= 0; --$i) {
116        if (match($topics[$i]['subject'], $str1) || match($topics[$i]['subject'], $str2)) {
117            $found = $topics[$i];
118            break;
119        }
120    }
121
122    if (!$found) {
123        // number in forum might be wrong
124        log_warn('Numarul pentru problema "'.$task['id'].'" este gresit! Am cautat '.$str1.' si '.$str2);
125        $str1 = strtoupper($task['id']);
126        $str2 = strtoupper($task['title']);
127        for ($i = count($topics)-1; $i >= 0; --$i) {
128            if (match($topics[$i]['subject'], $str1) || match($topics[$i]['subject'], $str2)) {
129                $found = $topics[$i];
130                break;
131            }
132        }
133    }
134
135    if (!$found) {
136        log_warn('Nu am gasit topic pentru "'.$task['id']."\"!");
137        continue;
138    }
139
140    if (link_task($task, $page, $found, $verbose)) {
141        $total++;
142    }
143}
144log_print('*** S-au modificat '.$total.' probleme!');
145
146?>
Note: See TracBrowser for help on using the repository browser.