1 | <?php |
---|
2 | require_once './config.inc.php'; |
---|
3 | chdir(OPENPNE_PUBLIC_HTML_DIR); |
---|
4 | require_once(OPENPNE_WEBAPP_DIR . "/init.inc"); |
---|
5 | |
---|
6 | //---------- |
---|
7 | //- 登録済み画像のファイルサイズを求める |
---|
8 | function get_image_size($category, $params, $handle) |
---|
9 | { |
---|
10 | // request |
---|
11 | $table = $params[0]; |
---|
12 | $filename = $params[1]; |
---|
13 | $c_member_id = $params[2]; |
---|
14 | |
---|
15 | // c_image 参照先 |
---|
16 | $image_dsn = array('main','image'); |
---|
17 | foreach ($image_dsn as $key => $dsn) { |
---|
18 | if (!isset($GLOBALS['_OPENPNE_DSN_LIST'][$dsn])) { |
---|
19 | continue; |
---|
20 | } |
---|
21 | $imagedb =& db_get_instance($dsn); |
---|
22 | |
---|
23 | // 取り出し |
---|
24 | $sql = "SELECT "; |
---|
25 | if ($table <> "biz_shisetsu") { |
---|
26 | $sql .= " $c_member_id as c_member_id, "; |
---|
27 | } |
---|
28 | $sql .= "$filename as image_filename "; |
---|
29 | if ($table == "c_member") { |
---|
30 | $sql .= ",r_date "; |
---|
31 | } elseif ($table <> "biz_group" && $table <> "biz_shisetsu") { |
---|
32 | $sql .= ",r_datetime "; |
---|
33 | } |
---|
34 | $sql .= "FROM "; |
---|
35 | $sql .= " $table "; |
---|
36 | $sql .= "WHERE "; |
---|
37 | $sql .= " $filename <> '' "; |
---|
38 | $sql .= "AND "; |
---|
39 | $sql .= " $filename is not NULL"; |
---|
40 | $data_list = db_get_all($sql); |
---|
41 | |
---|
42 | $lines = 0; |
---|
43 | if ($data_list) { |
---|
44 | fwrite($handle, "INSERT INTO c_image_size VALUES"); |
---|
45 | } |
---|
46 | foreach($data_list as $data) { |
---|
47 | // get image size |
---|
48 | $sql = 'SELECT bin FROM c_image WHERE filename = ?'; |
---|
49 | $params = array($data['image_filename']); |
---|
50 | $c_image_list = $imagedb->get_all($sql, $params); |
---|
51 | |
---|
52 | foreach ($c_image_list as $c_image) { |
---|
53 | $filesize = strlen(base64_decode($c_image['bin'])); |
---|
54 | } |
---|
55 | |
---|
56 | $ins_data = ""; |
---|
57 | if ($lines) { |
---|
58 | $ins_data .= ","; |
---|
59 | } |
---|
60 | $ins_data .= "("; |
---|
61 | if ($GLOBALS['_OPENPNE_DSN_LIST']['main']['dsn']['phptype'] == 'pgsql') { |
---|
62 | $ins_data .= " nextval('c_image_size_c_image_size_id_seq')"; |
---|
63 | } else { |
---|
64 | $ins_data .= " null"; |
---|
65 | } |
---|
66 | $ins_data .= ",'" . $data['image_filename'] . "'"; |
---|
67 | $ins_data .= ",'" . $data['c_member_id'] . "'"; |
---|
68 | $ins_data .= ",'" . $filesize . "'"; |
---|
69 | $ins_data .= ",'" . $category . "'"; |
---|
70 | if ($table == "biz_group" || $table == "biz_shisetsu") { |
---|
71 | $ins_data .= ",'" . db_now() . "'"; |
---|
72 | } elseif ($table == "c_member") { |
---|
73 | $ins_data .= ",'" . $data['r_date'] . "'"; |
---|
74 | } else { |
---|
75 | $ins_data .= ",'" . $data['r_datetime'] . "'"; |
---|
76 | } |
---|
77 | $ins_data .= ")"; |
---|
78 | fwrite($handle, $ins_data); |
---|
79 | $lines ++; |
---|
80 | } |
---|
81 | if ($lines) { |
---|
82 | fwrite($handle, ";\n"); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | } |
---|
87 | |
---|
88 | //---------- |
---|
89 | //- テンポラリファイルを作成 |
---|
90 | function open_temp_file($mode, $filename = '') |
---|
91 | { |
---|
92 | if (!$filename) { |
---|
93 | $w_filename = tempnam(OPENPNE_VAR_DIR . "/tmp", "image_size"); |
---|
94 | } else { |
---|
95 | $w_filename = $filename; |
---|
96 | } |
---|
97 | $handle = fopen($w_filename, $mode); |
---|
98 | |
---|
99 | return array($w_filename, $handle); |
---|
100 | } |
---|
101 | |
---|
102 | //---------- |
---|
103 | //- load data infile |
---|
104 | function load_data_infile($filename) |
---|
105 | { |
---|
106 | list($r_filename, $r_handle) = open_temp_file("r", $filename); |
---|
107 | while (!feof($r_handle)) { |
---|
108 | $sql = fgets($r_handle); |
---|
109 | if ($sql) db_query($sql); |
---|
110 | } |
---|
111 | close_temp_file($filename, $r_handle); |
---|
112 | } |
---|
113 | |
---|
114 | //---------- |
---|
115 | //- テンポラリを閉じる |
---|
116 | function close_temp_file($filename, $handle) |
---|
117 | { |
---|
118 | fclose($handle); |
---|
119 | chmod($filename, 0666); |
---|
120 | } |
---|
121 | |
---|
122 | |
---|
123 | //---------- |
---|
124 | //- 処理本体 |
---|
125 | //---------- |
---|
126 | // biz_shisetsuは、member_idを持っていないため、設定不可 |
---|
127 | |
---|
128 | // array([category] => array([table],[filename field],[c_member_id field])) |
---|
129 | |
---|
130 | $category_list = array( |
---|
131 | 'album' => array( |
---|
132 | array('c_album_image', 'image_filename', 'c_member_id') |
---|
133 | ), |
---|
134 | 'commu' => array( |
---|
135 | array('c_commu_topic_comment', 'image_filename1', 'c_member_id') |
---|
136 | ,array('c_commu_topic_comment', 'image_filename2', 'c_member_id') |
---|
137 | ,array('c_commu_topic_comment', 'image_filename3', 'c_member_id') |
---|
138 | ), |
---|
139 | 'diary' => array( |
---|
140 | array('c_diary', 'image_filename_1', 'c_member_id') |
---|
141 | ,array('c_diary', 'image_filename_2', 'c_member_id') |
---|
142 | ,array('c_diary', 'image_filename_3', 'c_member_id') |
---|
143 | ,array('c_diary_comment', 'image_filename_1', 'c_member_id') |
---|
144 | ,array('c_diary_comment', 'image_filename_2', 'c_member_id') |
---|
145 | ,array('c_diary_comment', 'image_filename_3', 'c_member_id') |
---|
146 | ,array('c_album', 'album_cover_image', 'c_member_id') |
---|
147 | ), |
---|
148 | 'other' => array( |
---|
149 | array('c_commu', 'image_filename', 'c_member_id_admin') |
---|
150 | ,array('biz_group', 'image_filename', 'admin_id') |
---|
151 | ,array('biz_shisetsu', 'image_filename', 'c_member_id') |
---|
152 | ,array('c_member', 'image_filename_1', 'c_member_id') |
---|
153 | ,array('c_member', 'image_filename_2', 'c_member_id') |
---|
154 | ,array('c_member', 'image_filename_3', 'c_member_id') |
---|
155 | ,array('c_message', 'image_filename_1', 'c_member_id_from') |
---|
156 | ,array('c_message', 'image_filename_2', 'c_member_id_from') |
---|
157 | ,array('c_message', 'image_filename_3', 'c_member_id_from') |
---|
158 | ) |
---|
159 | ); |
---|
160 | |
---|
161 | // Insert格納用テンポラリファイル作成 |
---|
162 | list($filename, $handle) = open_temp_file("w"); |
---|
163 | |
---|
164 | //テーブル分ループ |
---|
165 | foreach ($category_list as $category => $table_list) { |
---|
166 | |
---|
167 | // サイズ取り出し |
---|
168 | // 1テーブルづつInsert文を作成していく |
---|
169 | foreach ($table_list as $target) { |
---|
170 | get_image_size($category, $target, $handle); |
---|
171 | } |
---|
172 | |
---|
173 | } |
---|
174 | // テンポラリファイルクローズ |
---|
175 | close_temp_file($filename, $handle); |
---|
176 | |
---|
177 | // Insert実行 |
---|
178 | load_data_infile($filename); |
---|
179 | |
---|
180 | // テンポラリファイル削除 |
---|
181 | //unlink($filename); |
---|
182 | ?> |
---|
183 | |
---|
184 | |
---|