XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
importer.admin.controller.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 @set_time_limit(0);
4 require_once('./modules/importer/extract.class.php');
5 
15 {
20  var $unit_count = 300;
25  var $oXmlParser = null;
26 
31  function init()
32  {
33  }
34 
40  {
41  global $lang;
42 
43  $filename = Context::get('filename');
44  $isExists = 'false';
45 
46  if(strncasecmp('http://', $filename, 7) === 0)
47  {
48  if(ini_get('allow_url_fopen'))
49  {
50  $fp = @fopen($filename, "r");
51  if($fp)
52  {
53  $str = fgets($fp, 100);
54  if(strlen($str) > 0)
55  {
56  $isExists = 'true';
57  $type = 'XML';
58  if(stristr($str, 'tattertools')) $type = 'TTXML';
59 
60  $this->add('type', $type);
61  }
62  fclose($fp);
63  $resultMessage = $lang->found_xml_file;
64  }
65  else $resultMessage = $lang->cannot_url_file;
66  }
67  else $resultMessage = $lang->cannot_allow_fopen_in_phpini;
68 
69  $this->add('exists', $isExists);
70  }
71  else
72  {
73  $realPath = FileHandler::getRealPath($filename);
74 
75  if(file_exists($realPath) && is_file($realPath)) $isExists = 'true';
76  $this->add('exists', $isExists);
77 
78  if($isExists == 'true')
79  {
80  $type = 'XML';
81 
82  $fp = fopen($realPath, "r");
83  $str = fgets($fp, 100);
84  if(stristr($str, 'tattertools')) $type = 'TTXML';
85  fclose($fp);
86 
87  $this->add('type', $type);
88  $resultMessage = $lang->found_xml_file;
89  }
90  else $resultMessage = $lang->not_found_xml_file;
91  }
92  $this->add('result_message', $resultMessage);
93  }
94 
100  {
101  $oMemberModel = getModel('member');
102  $member_config = $oMemberModel->getMemberConfig();
103 
104  $postFix = ($member_config->identifier == 'email_address') ? 'ByEmail' : '';
105 
106  // 계정이 이메일인 경우 이메일 정보로 사용자를 싱크하도록 한다. 이때 변수명은 그대로 user_id를 사용한다.
107 
108  /* DBMS가 CUBRID인 경우 MySQL과 동일한 방법으로는 문서 및 댓글에 대한 사용자 정보를 동기화 할 수 없으므로 예외 처리 합니다.
109  CUBRID를 사용하지 않는 경우에만 보편적인 기존 질의문을 사용합니다. */
110  $db_info = Context::getDBInfo ();
111  if($db_info->db_type != "cubrid")
112  {
113  $output = executeQuery('importer.updateDocumentSync'.$postFix);
114  $output = executeQuery('importer.updateCommentSync'.$postFix);
115  }
116  else
117  {
118  $output = executeQueryArray ('importer.getDocumentMemberSrlWithUserID'.$postFix);
119  if(is_array ($output->data) && count ($output->data))
120  {
121  $success_count = 0;
122  $error_count = 0;
123  $total_count = 0;
124  foreach ($output->data as $val)
125  {
126  $args->user_id = $val->user_id;
127  $args->member_srl = $val->member_srl;
128  $tmp = executeQuery ('importer.updateDocumentSyncForCUBRID'.$postFix, $args);
129  if($tmp->toBool () === true)
130  {
131  $success_count++;
132  }
133  else
134  {
135  $error_count++;
136  }
137  $total_count++;
138  }
139  } // documents section
140 
141  $output = executeQueryArray ('importer.getCommentMemberSrlWithUserID'.$postFix);
142  if(is_array ($output->data) && count ($output->data))
143  {
144  $success_count = 0;
145  $error_count = 0;
146  $total_count = 0;
147  foreach ($output->data as $val)
148  {
149  $args->user_id = $val->user_id;
150  $args->member_srl = $val->member_srl;
151  $tmp = executeQuery ('importer.updateCommentSyncForCUBRID'.$postFix, $args);
152  if($tmp->toBool () === true)
153  {
154  $success_count++;
155  }
156  else
157  {
158  $error_count++;
159  }
160  $total_count++;
161  }
162  } // comments section
163  }
164 
165  $this->setMessage('msg_sync_completed');
166  }
167 
173  {
174  // Get the target xml file to import
175  $xml_file = Context::get('xml_file');
176  // Get a type of the target
177  $type = Context::get('type');
178  // Extract and cache information from the xml file
179  $oExtract = new extract();
180 
181  switch($type)
182  {
183  case 'member' :
184  $output = $oExtract->set($xml_file,'<members ', '</members>', '<member>', '</member>');
185  if($output->toBool()) $oExtract->saveItems();
186  break;
187  case 'message' :
188  $output = $oExtract->set($xml_file,'<messages ', '</messages>', '<message>','</message>');
189  if($output->toBool()) $oExtract->saveItems();
190  break;
191  case 'ttxml' :
192  // Category information
193  $output = $oExtract->set($xml_file, '', '', '', '');
194  if ($output->toBool())
195  {
196  // Get a category of ttxml separately
197  $started = false;
198  $buff = '';
199  while (!feof($oExtract->fd))
200  {
201  $str = fgets($oExtract->fd, 1024);
202  if(strstr($str, '<category>'))
203  {
204  $started = true;
205  $str = strstr($str, '<category>');
206  }
207  if(substr($str,0,strlen('<post ')) == '<post ') break;
208  if ($started) $buff .= $str;
209  }
210  $buff = '<categories>'.$buff.'</categories>';
211  $oExtract->closeFile();
212  $category_filename = sprintf('%s/%s', $oExtract->cache_path, 'category.xml');
213  FileHandler::writeFile($category_filename, $buff);
214 
215  // Guestbook information
216  $output = $oExtract->set($xml_file, '', '', '', '');
217  if($output->toBool())
218  {
219  $started = false;
220  $buff = '';
221  while (!feof($oExtract->fd))
222  {
223  $str = fgets($oExtract->fd, 1024);
224  if(strstr($str, '<guestbook>'))
225  {
226  $started = true;
227  $str = strstr($str, '<guestbook>');
228  }
229  if($started)
230  {
231  $pos = strpos($str, '</guestbook>');
232  if($pos !== false)
233  {
234  $buff .= substr($str, 0, $pos + strlen('</guestbook>'));
235  break;
236  }
237  $buff .= $str;
238  }
239  }
240  $oExtract->closeFile();
241  $guestbook_filename = sprintf('%s/%s', $oExtract->cache_path, 'guestbook.xml');
242  FileHandler::writeFile($guestbook_filename, $buff);
243  // Individual items
244  $output = $oExtract->set($xml_file,'<blog', '</blog>', '<post ', '</post>');
245  if($output->toBool()) $oExtract->saveItems();
246  }
247  }
248  break;
249  default :
250  // First get category information
251  $output = $oExtract->set($xml_file,'<categories>', '</categories>', '<category','</category>');
252  if($output->toBool())
253  {
254  $oExtract->mergeItems('category.xml');
255  // Get each item
256  $output = $oExtract->set($xml_file,'<posts ', '</posts>', '<post>', '</post>');
257  if($output->toBool()) $oExtract->saveItems();
258  }
259  break;
260  }
261 
262  if(!$output->toBool())
263  {
264  $this->add('error',0);
265  $this->add('status',-1);
266  $this->setMessage($output->getMessage());
267  return;
268  }
269  // Notify that all data completely extracted
270  $this->add('type',$type);
271  $this->add('total',$oExtract->getTotalCount());
272  $this->add('cur',0);
273  $this->add('key', $oExtract->getKey());
274  $this->add('status',0);
275  }
276 
282  {
283  // Variable setting
284  $type = Context::get('type');
285  $total = Context::get('total');
286  $cur = Context::get('cur');
287  $key = Context::get('key');
288  $user_id = Context::get('user_id');
289  $target_module = Context::get('target_module');
290  $guestbook_target_module = Context::get('guestbook_target_module');
291  $this->unit_count = Context::get('unit_count');
292  // Check if an index file exists
293  $index_file = './files/cache/importer/'.$key.'/index';
294  if(!file_exists($index_file)) return new BaseObject(-1, 'msg_invalid_xml_file');
295 
296  switch($type)
297  {
298  case 'ttxml' :
299  if(!$target_module) return new BaseObject(-1,'msg_invalid_request');
300 
301  $oModuleModel = getModel('module');
302  $columnList = array('module_srl', 'module');
303  $target_module_info = $oModuleModel->getModuleInfoByModuleSrl($target_module, $columnList);
304 
305  $ttimporter = FileHandler::exists(_XE_PATH_ . 'modules/importer/ttimport.class.php');
306  if($ttimporter) require_once($ttimporter);
307 
308  $oTT = new ttimport();
309  $cur = $oTT->importModule($key, $cur, $index_file, $this->unit_count, $target_module, $guestbook_target_module, $user_id, $target_module_info->module);
310  break;
311  case 'message' :
312  $cur = $this->importMessage($key, $cur, $index_file);
313  break;
314  case 'member' :
315  $cur = $this->importMember($key, $cur, $index_file);
316  break;
317  case 'module' :
318  // Check if the target module exists
319  if(!$target_module) return new BaseObject(-1,'msg_invalid_request');
320  $cur = $this->importModule($key, $cur, $index_file, $target_module);
321  break;
322  }
323  // Notify that all data completely extracted
324  $this->add('type',$type);
325  $this->add('total',$total);
326  $this->add('cur',$cur);
327  $this->add('key', $key);
328  $this->add('target_module', $target_module);
329  // When completing, success message appears and remove the cache files
330  if($total <= $cur)
331  {
332  $this->setMessage( sprintf(Context::getLang('msg_import_finished'), $cur, $total) );
333  FileHandler::removeDir('./files/cache/importer/'.$key);
334  }
335  else $this->setMessage( sprintf(Context::getLang('msg_importing'), $total, $cur) );
336  }
337 
345  function importMember($key, $cur, $index_file)
346  {
347  if(!$cur) $cur = 0;
348  // Create the xmlParser object
349  $oXmlParser = new XmlParser();
350  // Create objects for importing member information
351  $this->oMemberController = getController('member');
352  $this->oMemberModel = getModel('member');
353  // Get a default member group
354  $default_group = $this->oMemberModel->getDefaultGroup();
355  $default_group_srl = $default_group->group_srl;
356  // Get information of the Webmaster
357  $oModuleModel = getModel('module');
358  $member_config = $oModuleModel->getModuleConfig('member');
359  // Open an index file
360  $f = fopen($index_file,"r");
361  // Pass if already read
362  for($i=0;$i<$cur;$i++) fgets($f, 1024);
363  // Read by each line until the condition meets
364  for($idx=$cur;$idx<$cur+$this->unit_count;$idx++)
365  {
366  if(feof($f)) break;
367  // Find a given location
368  $target_file = trim(fgets($f, 1024));
369  // Load and parse the file
370  $xmlObj = $oXmlParser->loadXmlFile($target_file);
371  FileHandler::removeFile($target_file);
372  if(!$xmlObj) continue;
373  // List Objects
374  $obj = new stdClass();
375  $obj->member_srl = getNextSequence();
376  $obj->user_id = base64_decode($xmlObj->member->user_id->body);
377  $obj->password = base64_decode($xmlObj->member->password->body);
378  $obj->user_name = base64_decode($xmlObj->member->user_name->body);
379  $obj->nick_name = base64_decode($xmlObj->member->nick_name->body);
380  if(!$obj->user_name) $obj->user_name = $obj->nick_name;
381  $obj->email_address = base64_decode($xmlObj->member->email->body);
382  $obj->homepage = base64_decode($xmlObj->member->homepage->body);
383  $obj->blog = base64_decode($xmlObj->member->blog->body);
384  $obj->birthday = substr(base64_decode($xmlObj->member->birthday->body),0,8);
385  $obj->allow_mailing = base64_decode($xmlObj->member->allow_mailing->body);
386  $obj->point = base64_decode($xmlObj->member->point->body);
387  $obj->image_nickname = base64_decode($xmlObj->member->image_nickname->buff->body);
388  $obj->image_mark = base64_decode($xmlObj->member->image_mark->buff->body);
389  $obj->profile_image = base64_decode($xmlObj->member->profile_image->buff->body);
390  $obj->signature = base64_decode($xmlObj->member->signature->body);
391  $obj->regdate = base64_decode($xmlObj->member->regdate->body);
392  $obj->last_login = base64_decode($xmlObj->member->last_login->body);
393 
394  if($xmlObj->member->extra_vars)
395  {
396  foreach($xmlObj->member->extra_vars as $key => $val)
397  {
398  if(in_array($key, array('node_name','attrs','body'))) continue;
399  $obj->extra_vars->{$key} = base64_decode($val->body);
400  }
401  }
402  // Create url for homepage and blog
403  if($obj->homepage && strncasecmp('http://', $obj->homepage, 7) !== 0 && strncasecmp('https://', $obj->homepage, 8) !== 0) $obj->homepage = 'http://'.$obj->homepage;
404  // Check user ID
405  if(!preg_match('/^[a-z]+[\w-]*[a-z0-9_]+$/i', $obj->user_id))
406  {
407  $obj->user_id = preg_replace('/[^a-z0-9_-]+/i', '', $obj->user_id);
408  }
409  if(!preg_match('/^[a-z]+[\w-]*[a-z0-9_]+$/i', $obj->user_id))
410  {
411  $obj->user_id = 't' . $obj->member_srl;
412  }
413  // Check email address
414  if(!preg_match('/^[\w-]+((?:\.|\+|\~)[\w-]+)*@[\w-]+(\.[\w-]+)+$/', $obj->email_address))
415  {
416  $obj->email_address = $obj->user_id . '@example.com';
417  }
418  list($obj->email_id, $obj->email_host) = explode('@', $obj->email);
419  // Set the mailing option
420  if($obj->allow_mailing!='Y') $obj->allow_mailing = 'N';
421  // Set the message option
422  $obj->allow_message = 'Y';
423  if(!in_array($obj->allow_message, array('Y','N','F'))) $obj->allow_message= 'Y';
424  // Get member-join date if the last login time is not found
425  if(!$obj->last_login) $obj->last_login = $obj->regdate;
426  // Set the list order
427  $obj->list_order = -1 * $obj->member_srl;
428  // List extra vars
429  $extra_vars = $obj->extra_vars;
430  unset($obj->extra_vars);
431  $obj->extra_vars = serialize($extra_vars);
432  // Check if the same user ID exists
433  $args = new stdClass;
434  $args->user_id = $obj->user_id;
435  $output = executeQuery('member.getMemberSrl', $args);
436  if(!$output->toBool() || $output->data)
437  {
438  $obj->user_id .= '_'.$obj->member_srl;
439  }
440  // Check if the same nickname exists
441  $args = new stdClass;
442  $args->nick_name = $obj->nick_name;
443  $output = executeQuery('member.getMemberSrl', $args);
444  if(!$output->toBool() || $output->data)
445  {
446  $obj->user_id .= '_'.$obj->member_srl;
447  }
448  // Check if the same email address exists
449  $args = new stdClass;
450  $args->email_address = $obj->email_address;
451  $output = executeQuery('member.getMemberSrl', $args);
452  if(!$output->toBool() || $output->data)
453  {
454  $obj->email_address = $obj->user_id . '@example.com';
455  }
456 
457  // Add a member
458  $output = executeQuery('member.insertMember', $obj);
459 
460  if($output->toBool() && !($obj->password))
461  {
462  // Send a mail telling the user to reset his password.
463  $oMail = new Mail();
464  $oMail->setTitle("Password update for your " . getFullSiteUrl() . " account");
465  $webmaster_name = $member_config->webmaster_name?$member_config->webmaster_name:'Webmaster';
466  $oMail->setContent("Dear $obj->user_name, <br /><br />
467  We recently migrated our site to XpressEngine. Since you password was encrypted we could not migrate it too, so please reset it by following this link:
468  <a href='" . getFullSiteUrl() . "/?act=dispMemberFindAccount' >" . getFullSiteUrl() . "?act=dispMemberFindAccount</a>. You need to enter you email address and hit the 'Find account' button. You will then receive an email with a new, generated password that you can change after login. <br /><br />
469 
470  Thank you for your understanding,<br />
471  {$webmaster_name}"
472  );
473  $oMail->setSender($webmaster_name, $member_config->webmaster_email);
474  $oMail->setReceiptor( $obj->user_name, $obj->email);
475  $oMail->send();
476  }
477 
478  // add group join/image name-mark-signiture and so on if a new member successfully added
479  if($output->toBool())
480  {
481  // Join to the default group
482  $obj->group_srl = $default_group_srl;
483  executeQuery('member.addMemberToGroup',$obj);
484  // Image name
485  if($obj->image_nickname)
486  {
487  $target_path = sprintf('files/member_extra_info/image_name/%s/', getNumberingPath($obj->member_srl));
488  $target_filename = sprintf('%s%d.gif', $target_path, $obj->member_srl);
489  FileHandler::writeFile($target_filename, $obj->image_nickname);
490  }
491  // Image mark
492  if($obj->image_mark && file_exists($obj->image_mark))
493  {
494  $target_path = sprintf('files/member_extra_info/image_mark/%s/', getNumberingPath($obj->member_srl));
495  $target_filename = sprintf('%s%d.gif', $target_path, $obj->member_srl);
496  FileHandler::writeFile($target_filename, $obj->image_mark);
497  }
498  // Profile image
499  if($obj->profile_image)
500  {
501  $target_path = sprintf('files/member_extra_info/profile_image/%s/', getNumberingPath($obj->member_srl));
502  $target_filename = sprintf('%s%d.gif', $target_path, $obj->member_srl);
503  FileHandler::writeFile($target_filename, $obj->profile_image);
504  }
505  // Signiture
506  if($obj->signature)
507  {
508  $signature = removeHackTag($obj->signature);
509  $signature_buff = sprintf('<?php if(!defined("__XE__")) exit();?>%s', $signature);
510 
511  $target_path = sprintf('files/member_extra_info/signature/%s/', getNumberingPath($obj->member_srl));
512  if(!is_dir($target_path)) FileHandler::makeDir($target_path);
513  $target_filename = sprintf('%s%d.signature.php', $target_path, $obj->member_srl);
514 
515  FileHandler::writeFile($target_filename, $signature_buff);
516  }
517  }
518  }
519 
520  fclose($f);
521 
522  return $idx-1;
523  }
524 
532  function importMessage($key, $cur, $index_file)
533  {
534  if(!$cur) $cur = 0;
535  // Create the xmlParser object
536  $oXmlParser = new XmlParser();
537  // Open an index file
538  $f = fopen($index_file,"r");
539  // Pass if already read
540  for($i=0;$i<$cur;$i++) fgets($f, 1024);
541  // Read each line until the condition meets
542  for($idx=$cur;$idx<$cur+$this->unit_count;$idx++)
543  {
544  if(feof($f)) break;
545  // Find a location
546  $target_file = trim(fgets($f, 1024));
547  // Load and parse the file
548  $xmlObj = $oXmlParser->loadXmlFile($target_file);
549  FileHandler::removeFile($target_file);
550  if(!$xmlObj) continue;
551  // List objects
552  $obj = null;
553  $obj->receiver = base64_decode($xmlObj->message->receiver->body);
554  $obj->sender = base64_decode($xmlObj->message->sender->body);
555  $obj->title = base64_decode($xmlObj->message->title->body);
556  $obj->content = base64_decode($xmlObj->message->content->body);
557  $obj->readed = base64_decode($xmlObj->message->readed->body)=='Y'?'Y':'N';
558  $obj->regdate = base64_decode($xmlObj->message->regdate->body);
559  $obj->readed_date = base64_decode($xmlObj->message->readed_date->body);
560  // Get member_srl of sender/recipient (If not exists, pass)
561  if(!$obj->sender) continue;
562  $sender_args->user_id = $obj->sender;
563  $sender_output = executeQuery('member.getMemberInfo',$sender_args);
564  $sender_srl = $sender_output->data->member_srl;
565  if(!$sender_srl)
566  {
567  unset($sender_args);
568  $sender_args->email_address = $obj->sender;
569  $sender_output = executeQuery('member.getMemberInfoByEmailAddress',$sender_args);
570  $sender_srl = $sender_output->data->member_srl;
571  }
572  if(!$sender_srl) continue;
573 
574  $receiver_args->user_id = $obj->receiver;
575  if(!$obj->receiver) continue;
576  $receiver_output = executeQuery('member.getMemberInfo',$receiver_args);
577  $receiver_srl = $receiver_output->data->member_srl;
578  if(!$receiver_srl)
579  {
580  unset($receiver_args);
581  $receiver_args->email_address = $obj->receiver;
582  $receiver_output = executeQuery('member.getMemberInfoByEmailAddress',$receiver_args);
583  $receiver_srl = $receiver_output->data->member_srl;
584  }
585  if(!$receiver_srl) continue;
586  // Message to save into sender's message box
587  $sender_args->sender_srl = $sender_srl;
588  $sender_args->receiver_srl = $receiver_srl;
589  $sender_args->message_type = 'S';
590  $sender_args->title = $obj->title;
591  $sender_args->content = $obj->content;
592  $sender_args->readed = $obj->readed;
593  $sender_args->regdate = $obj->regdate;
594  $sender_args->readed_date = $obj->readed_date;
595  $sender_args->related_srl = getNextSequence();
596  $sender_args->message_srl = getNextSequence();
597  $sender_args->list_order = $sender_args->message_srl * -1;
598 
599  $output = executeQuery('communication.sendMessage', $sender_args);
600  if($output->toBool())
601  {
602  // Message to save into recipient's massage box
603  $receiver_args->message_srl = $sender_args->related_srl;
604  $receiver_args->list_order = $sender_args->related_srl*-1;
605  $receiver_args->sender_srl = $sender_srl;
606  if(!$receiver_args->sender_srl) $receiver_args->sender_srl = $receiver_srl;
607  $receiver_args->receiver_srl = $receiver_srl;
608  $receiver_args->message_type = 'R';
609  $receiver_args->title = $obj->title;
610  $receiver_args->content = $obj->content;
611  $receiver_args->readed = $obj->readed;
612  $receiver_args->regdate = $obj->regdate;
613  $receiver_args->readed_date = $obj->readed_date;
614  $output = executeQuery('communication.sendMessage', $receiver_args);
615  }
616  }
617 
618  fclose($f);
619 
620  return $idx-1;
621  }
622 
631  function importModule($key, $cur, $index_file, $module_srl)
632  {
633  // Pre-create the objects needed
634  $this->oXmlParser = new XmlParser();
635  // Get category information of the target module
636  $oDocumentController = getController('document');
637  $oDocumentModel = getModel('document');
638  $category_list = $category_titles = array();
639  $category_list = $oDocumentModel->getCategoryList($module_srl);
640  if(count($category_list)) foreach($category_list as $key => $val) $category_titles[$val->title] = $val->category_srl;
641  // Extract category information
642  $category_file = preg_replace('/index$/i', 'category.xml', $index_file);
643  if(file_exists($category_file))
644  {
645  $buff = FileHandler::readFile($category_file);
646 
647  // Create the xmlParser object
648  $xmlDoc = $this->oXmlParser->loadXmlFile($category_file);
649 
650  $categories = $xmlDoc->items->category;
651  if($categories)
652  {
653  if(!is_array($categories)) $categories = array($categories);
654  $match_sequence = array();
655  foreach($categories as $k => $v)
656  {
657  $category = trim(base64_decode($v->body));
658  if(!$category || $category_titles[$category]) continue;
659 
660  $sequence = $v->attrs->sequence;
661  $parent = $v->attrs->parent;
662 
663  $obj = null;
664  $obj->title = $category;
665  $obj->module_srl = $module_srl;
666  if($parent) $obj->parent_srl = $match_sequence[$parent];
667 
668  $output = $oDocumentController->insertCategory($obj);
669  if($output->toBool()) $match_sequence[$sequence] = $output->get('category_srl');
670  }
671  $oDocumentController = getController('document');
672  $oDocumentController->makeCategoryFile($module_srl);
673  }
674  FileHandler::removeFile($category_file);
675  }
676 
677  $category_list = $category_titles = array();
678  $category_list = $oDocumentModel->getCategoryList($module_srl);
679  if(count($category_list)) foreach($category_list as $key => $val) $category_titles[$val->title] = $val->category_srl;
680 
681  $ek_args->module_srl = $module_srl;
682  $output = executeQueryArray('document.getDocumentExtraKeys', $ek_args);
683  if($output->data)
684  {
685  foreach($output->data as $key => $val) $extra_keys[$val->eid] = true;
686  }
687 
688  if(!$cur) $cur = 0;
689  // Open an index file
690  $f = fopen($index_file,"r");
691  // Pass if already read
692  for($i=0;$i<$cur;$i++) fgets($f, 1024);
693  // Read each line until the condition meets
694  for($idx=$cur;$idx<$cur+$this->unit_count;$idx++)
695  {
696  if(feof($f)) break;
697  // Find a location
698  $target_file = trim(fgets($f, 1024));
699 
700  if(!file_exists($target_file)) continue;
701  // Importing data from now on
702  $fp = fopen($target_file,"r");
703  if(!$fp) continue;
704 
705  $obj = new stdClass;
706  $obj->module_srl = $module_srl;
707  $obj->document_srl = getNextSequence();
708 
709  $files = array();
710  $extra_vars = array();
711 
712  $started = false;
713  $buff = array();
714  // Start from the body data
715  while(!feof($fp))
716  {
717  $str = fgets($fp, 1024);
718  // Prepare an item
719  if(trim($str) == '<post>')
720  {
721  $started = true;
722  // Trackback inserted
723  }
724  else if(substr($str,0,11) == '<trackbacks')
725  {
726  $obj->trackback_count = $this->importTrackbacks($fp, $module_srl, $obj->document_srl);
727  continue;
728  // Comments inserted
729  }
730  else if(substr($str,0,9) == '<comments')
731  {
732  $obj->comment_count = $this->importComments($fp, $module_srl, $obj->document_srl);
733  continue;
734  // Attachment inserted
735  }
736  else if(substr($str,0,9) == '<attaches')
737  {
738  $obj->uploaded_count = $this->importAttaches($fp, $module_srl, $obj->document_srl, $files);
739  continue;
740  // When starting extra variabls
741  }
742  elseif(trim($str) == '<extra_vars>')
743  {
744  $extra_vars = $this->importExtraVars($fp);
745  continue;
746  }
747 
748  if($started) $buff[] = $str;
749  }
750 
751  $xmlDoc = $this->oXmlParser->parse(implode('', $buff));
752 
753  $category = base64_decode($xmlDoc->post->category->body);
754  if($category_titles[$category]) $obj->category_srl = $category_titles[$category];
755 
756  $obj->member_srl = 0;
757 
758  $obj->is_notice = base64_decode($xmlDoc->post->is_notice->body)=='Y'?'Y':'N';
759  $obj->status = base64_decode($xmlDoc->post->is_secret->body)=='Y'?$oDocumentModel->getConfigStatus('secret'):$oDocumentModel->getConfigStatus('public');
760  $obj->title = base64_decode($xmlDoc->post->title->body);
761  $obj->content = base64_decode($xmlDoc->post->content->body);
762  $obj->readed_count = base64_decode($xmlDoc->post->readed_count->body);
763  $obj->voted_count = base64_decode($xmlDoc->post->voted_count->body);
764  $obj->blamed_count = base64_decode($xmlDoc->post->blamed_count->body);
765  $obj->password = base64_decode($xmlDoc->post->password->body);
766  $obj->user_name = base64_decode($xmlDoc->post->user_name->body);
767  $obj->nick_name = base64_decode($xmlDoc->post->nick_name->body);
768  if(!$obj->user_name) $obj->user_name = $obj->nick_name;
769  $obj->user_id = base64_decode($xmlDoc->post->user_id->body);
770  $obj->email_address = base64_decode($xmlDoc->post->email->body);
771  $obj->homepage = base64_decode($xmlDoc->post->homepage->body);
772  if($obj->homepage && strncasecmp('http://', $obj->homepage, 7) !== 0 && strncasecmp('https://', $obj->homepage, 8) !== 0) $obj->homepage = 'http://'.$obj->homepage;
773  $obj->tags = base64_decode($xmlDoc->post->tags->body);
774  $obj->regdate = base64_decode($xmlDoc->post->regdate->body);
775  $obj->last_update = base64_decode($xmlDoc->post->update->body);
776  $obj->last_updater = base64_decode($xmlDoc->post->last_updater->body);
777  if(!$obj->last_update) $obj->last_update = $obj->regdate;
778  $obj->ipaddress = base64_decode($xmlDoc->post->ipaddress->body);
779  $obj->list_order = $obj->update_order = $obj->document_srl*-1;
780  $obj->commentStatus = base64_decode($xmlDoc->post->allow_comment->body)!='N'?'ALLOW':'DENY';
781  $obj->allow_trackback = base64_decode($xmlDoc->post->allow_trackback->body)!='N'?'Y':'N';
782  $obj->notify_message = base64_decode($xmlDoc->post->is_notice->body);
783  // Check user ID
784  if(!preg_match('/^[a-z]+[\w-]*[a-z0-9_]+$/i', $obj->user_id))
785  {
786  $obj->user_id = preg_replace('/[^a-z0-9_-]+/i', '', $obj->user_id);
787  }
788  if(!preg_match('/^[a-z]+[\w-]*[a-z0-9_]+$/i', $obj->user_id))
789  {
790  $obj->user_id = 't' . $obj->member_srl;
791  }
792  // Check email address
793  if(!preg_match('/^[\w-]+((?:\.|\+|\~)[\w-]+)*@[\w-]+(\.[\w-]+)+$/', $obj->email_address))
794  {
795  $obj->email_address = $obj->user_id . '@example.com';
796  }
797  // Change content information (attachment)
798  if(count($files))
799  {
800  foreach($files as $key => $val)
801  {
802  $obj->content = preg_replace('/(src|href)\=(["\']?)'.preg_quote($key).'(["\']?)/i','$1="'.$val.'"',$obj->content);
803  $obj->content = preg_replace('/(["\']?).\/files\/(.+)\/'.preg_quote($key).'([^"\']+)(["\']?)/i','"'.$val.'"',$obj->content);
804  $obj->content = preg_replace('/(["\']?)files\/(.+)\/'.preg_quote($key).'([^"\']+)(["\']?)/i','"'.$val.'"',$obj->content);
805  }
806  }
807 
808  $output = executeQuery('document.insertDocument', $obj);
809 
810  if($output->toBool() && $obj->tags)
811  {
812  $tag_list = explode(',',$obj->tags);
813  $tag_count = count($tag_list);
814  for($i=0;$i<$tag_count;$i++)
815  {
816  $args = new stdClass;
817  $args->tag_srl = getNextSequence();
818  $args->module_srl = $module_srl;
819  $args->document_srl = $obj->document_srl;
820  $args->tag = trim($tag_list[$i]);
821  $args->regdate = $obj->regdate;
822  if(!$args->tag) continue;
823  $output = executeQuery('tag.insertTag', $args);
824  }
825 
826  }
827  // Add extra variables
828  if(count($extra_vars))
829  {
830  foreach($extra_vars as $key => $val)
831  {
832  if(!$val->value) continue;
833  unset($e_args);
834  $e_args->module_srl = $module_srl;
835  $e_args->document_srl = $obj->document_srl;
836  $e_args->var_idx = $val->var_idx;
837  $e_args->value = $val->value;
838  $e_args->lang_code = $val->lang_code;
839  $e_args->eid = $val->eid;
840  // Create a key for extra vars if not exists (except vars for title and content)
841  if(!preg_match('/^(title|content)_(.+)$/i',$e_args->eid) && !$extra_keys[$e_args->eid])
842  {
843  unset($ek_args);
844  $ek_args->module_srl = $module_srl;
845  $ek_args->var_idx = $val->var_idx;
846  $ek_args->var_name = $val->eid;
847  $ek_args->var_type = 'text';
848  $ek_args->var_is_required = 'N';
849  $ek_args->var_default = '';
850  $ek_args->eid = $val->eid;
851  $output = executeQuery('document.insertDocumentExtraKey', $ek_args);
852  $extra_keys[$ek_args->eid] = true;
853  }
854 
855  $output = executeQuery('document.insertDocumentExtraVar', $e_args);
856  }
857  }
858 
859  fclose($fp);
860  FileHandler::removeFile($target_file);
861  }
862 
863  fclose($f);
864  // Sync category counts
865  if(count($category_list)) foreach($category_list as $key => $val) $oDocumentController->updateCategoryCount($module_srl, $val->category_srl);
866 
867  return $idx-1;
868  }
869 
878  {
879  $started = false;
880  $buff = null;
881  $cnt = 0;
882 
883  while(!feof($fp))
884  {
885  $str = fgets($fp, 1024);
886  // If </trackbacks> is, break
887  if(trim($str) == '</trackbacks>') break;
888  // If <trackback>, start importing
889  if(trim($str) == '<trackback>') $started = true;
890 
891  if($started) $buff .= $str;
892  // If </trackback>, insert to the DB
893  if(trim($str) == '</trackback>')
894  {
895  $xmlDoc = $this->oXmlParser->parse($buff);
896 
897  $obj = new stdClass;
898  $obj->trackback_srl = getNextSequence();
899  $obj->module_srl = $module_srl;
900  $obj->document_srl = $document_srl;
901  $obj->url = base64_decode($xmlDoc->trackback->url->body);
902  $obj->title = base64_decode($xmlDoc->trackback->title->body);
903  $obj->blog_name = base64_decode($xmlDoc->trackback->blog_name->body);
904  $obj->excerpt = base64_decode($xmlDoc->trackback->excerpt->body);
905  $obj->regdate = base64_decode($xmlDoc->trackback->regdate->body);
906  $obj->ipaddress = base64_decode($xmlDoc->trackback->ipaddress->body);
907  $obj->list_order = -1*$obj->trackback_srl;
908  $output = executeQuery('trackback.insertTrackback', $obj);
909  if($output->toBool()) $cnt++;
910 
911  $buff = null;
912  $started = false;
913  }
914  }
915  return $cnt;
916  }
917 
926  {
927  $started = false;
928  $buff = null;
929  $cnt = 0;
930 
931  $sequences = array();
932 
933  while(!feof($fp))
934  {
935  $str = fgets($fp, 1024);
936  // If </comments> is, break
937  if(trim($str) == '</comments>') break;
938  // If <comment> is, start importing
939  if(trim($str) == '<comment>')
940  {
941  $started = true;
942  $obj = new stdClass;
943  $obj->comment_srl = getNextSequence();
944  $files = array();
945  }
946  // If <attaches is, start importing attachments
947  if(substr($str,0,9) == '<attaches')
948  {
949  $obj->uploaded_count = $this->importAttaches($fp, $module_srl, $obj->comment_srl, $files);
950  continue;
951  }
952 
953  if($started) $buff .= $str;
954  // If </comment> is, insert to the DB
955  if(trim($str) == '</comment>')
956  {
957  $xmlDoc = $this->oXmlParser->parse($buff);
958 
959  $sequence = base64_decode($xmlDoc->comment->sequence->body);
960  $sequences[$sequence] = $obj->comment_srl;
961  $parent = base64_decode($xmlDoc->comment->parent->body);
962 
963  $obj->module_srl = $module_srl;
964 
965  if($parent) $obj->parent_srl = $sequences[$parent];
966  else $obj->parent_srl = 0;
967 
968  $obj->document_srl = $document_srl;
969  $obj->is_secret = base64_decode($xmlDoc->comment->is_secret->body)=='Y'?'Y':'N';
970  $obj->notify_message = base64_decode($xmlDoc->comment->notify_message->body)=='Y'?'Y':'N';
971  $obj->content = base64_decode($xmlDoc->comment->content->body);
972  $obj->voted_count = base64_decode($xmlDoc->comment->voted_count->body);
973  $obj->blamed_count = base64_decode($xmlDoc->comment->blamed_count->body);
974  $obj->password = base64_decode($xmlDoc->comment->password->body);
975  $obj->user_name =base64_decode($xmlDoc->comment->user_name->body);
976  $obj->nick_name = base64_decode($xmlDoc->comment->nick_name->body);
977  if(!$obj->user_name) $obj->user_name = $obj->nick_name;
978  $obj->user_id = base64_decode($xmlDoc->comment->user_id->body);
979  $obj->member_srl = 0;
980  $obj->email_address = base64_decode($xmlDoc->comment->email->body);
981  $obj->homepage = base64_decode($xmlDoc->comment->homepage->body);
982  $obj->regdate = base64_decode($xmlDoc->comment->regdate->body);
983  $obj->last_update = base64_decode($xmlDoc->comment->update->body);
984  if(!$obj->last_update) $obj->last_update = $obj->regdate;
985  $obj->ipaddress = base64_decode($xmlDoc->comment->ipaddress->body);
986  $obj->status = base64_decode($xmlDoc->comment->status->body)==''?'1':base64_decode($xmlDoc->comment->status->body);
987  $obj->list_order = $obj->comment_srl*-1;
988  // Check user ID
989  if(!preg_match('/^[a-z]+[\w-]*[a-z0-9_]+$/i', $obj->user_id))
990  {
991  $obj->user_id = preg_replace('/[^a-z0-9_-]+/i', '', $obj->user_id);
992  }
993  if(!preg_match('/^[a-z]+[\w-]*[a-z0-9_]+$/i', $obj->user_id))
994  {
995  $obj->user_id = 't' . $obj->member_srl;
996  }
997  // Check email address
998  if(!preg_match('/^[\w-]+((?:\.|\+|\~)[\w-]+)*@[\w-]+(\.[\w-]+)+$/', $obj->email_address))
999  {
1000  $obj->email_address = $obj->user_id . '@example.com';
1001  }
1002  // Change content information (attachment)
1003  if(count($files))
1004  {
1005  foreach($files as $key => $val)
1006  {
1007  $obj->content = preg_replace('/(src|href)\=(["\']?)'.preg_quote($key).'(["\']?)/i','$1="'.$val.'"',$obj->content);
1008  }
1009  }
1010  // Comment list first
1011  $list_args = new stdClass;
1012  $list_args->comment_srl = $obj->comment_srl;
1013  $list_args->document_srl = $obj->document_srl;
1014  $list_args->module_srl = $obj->module_srl;
1015  $list_args->regdate = $obj->regdate;
1016  // Set data directly if parent comment doesn't exist
1017  if(!$obj->parent_srl)
1018  {
1019  $list_args->head = $list_args->arrange = $obj->comment_srl;
1020  $list_args->depth = 0;
1021  // Get parent_srl if parent comment exists
1022  }
1023  else
1024  {
1025  // Get parent comment infomation
1026  $parent_args->comment_srl = $obj->parent_srl;
1027  $parent_output = executeQuery('comment.getCommentListItem', $parent_args);
1028  // Return if parent comment doesn't exist
1029  if(!$parent_output->toBool() || !$parent_output->data) continue;
1030  $parent = $parent_output->data;
1031 
1032  $list_args->head = $parent->head;
1033  $list_args->depth = $parent->depth+1;
1034  if($list_args->depth<2) $list_args->arrange = $obj->comment_srl;
1035  else
1036  {
1037  $list_args->arrange = $parent->arrange;
1038  $output = executeQuery('comment.updateCommentListArrange', $list_args);
1039  if(!$output->toBool()) return $output;
1040  }
1041  }
1042 
1043  $output = executeQuery('comment.insertCommentList', $list_args);
1044  if($output->toBool())
1045  {
1046  $output = executeQuery('comment.insertComment', $obj);
1047  if($output->toBool()) $cnt++;
1048  }
1049 
1050  $buff = null;
1051  $started = false;
1052  }
1053  }
1054  return $cnt;
1055  }
1056 
1065  function importAttaches($fp, $module_srl, $upload_target_srl, &$files)
1066  {
1067  $uploaded_count = 0;
1068 
1069  $started = false;
1070  $buff = null;
1071 
1072  $file_obj = new stdClass;
1073  while(!feof($fp))
1074  {
1075  $str = trim(fgets($fp, 1024));
1076  // If it ends with </attaches>, break
1077  if(trim($str) == '</attaches>') break;
1078  // If it starts with <attach>, collect attachments
1079  if(trim($str) == '<attach>')
1080  {
1081  $file_obj->file_srl = getNextSequence();
1082  $file_obj->upload_target_srl = $upload_target_srl;
1083  $file_obj->module_srl = $module_srl;
1084 
1085  $started = true;
1086  $buff = null;
1087  // If it starts with <file>, handle the attachement in xml file
1088  }
1089  else if(trim($str) == '<file>')
1090  {
1091  $file_obj->file = $this->saveTemporaryFile($fp);
1092  continue;
1093  }
1094 
1095  if($started) $buff .= $str;
1096  // If it ends with </attach>, handle attachements
1097  if(trim($str) == '</attach>')
1098  {
1099  $xmlDoc = $this->oXmlParser->parse($buff.$str);
1100 
1101  $file_obj->source_filename = base64_decode($xmlDoc->attach->filename->body);
1102  $file_obj->download_count = base64_decode($xmlDoc->attach->download_count->body);
1103 
1104  if(!$file_obj->file)
1105  {
1106  $url = base64_decode($xmlDoc->attach->url->body);
1107  $path = base64_decode($xmlDoc->attach->path->body);
1108  if($path && file_exists($path)) $file_obj->file = $path;
1109  else
1110  {
1111  $file_obj->file = $this->getTmpFilename();
1112  FileHandler::getRemoteFile($url, $file_obj->file);
1113  }
1114  }
1115 
1116  if(file_exists($file_obj->file))
1117  {
1118  $random = new Password();
1119  // Set upload path by checking if the attachement is an image or other kind of file
1120  if(preg_match("/\.(jpe?g|gif|png|wm[va]|mpe?g|avi|flv|mp[1-4]|as[fx]|wav|midi?|moo?v|qt|r[am]{1,2}|m4v)$/i", $file_obj->source_filename))
1121  {
1122  // Immediately remove the direct file if it has any kind of extensions for hacking
1123  $file_obj->source_filename = preg_replace('/\.(php|phtm|phar|html?|cgi|pl|exe|jsp|asp|inc)/i', '$0-x', $file_obj->source_filename);
1124  $file_obj->source_filename = str_replace(array('<', '>'), array('%3C', '%3E'), $file_obj->source_filename);
1125 
1126  $path = sprintf("./files/attach/images/%s/%s", $module_srl, getNumberingPath($upload_target_srl, 3));
1127 
1128  $ext = substr(strrchr($file_obj->source_filename,'.'),1);
1129  $_filename = $random->createSecureSalt(32, 'hex').'.'.$ext;
1130  $filename = $path.$_filename;
1131 
1132  $idx = 1;
1133  while(file_exists($filename))
1134  {
1135  $filename = $path.preg_replace('/\.([a-z0-9]+)$/i','_'.$idx.'.$1', $_filename);
1136  $idx++;
1137  }
1138 
1139  $file_obj->direct_download = 'Y';
1140  }
1141  else
1142  {
1143  $path = sprintf("./files/attach/binaries/%s/%s", $module_srl, getNumberingPath($upload_target_srl,3));
1144  $filename = $path.$random->createSecureSalt(32, 'hex');
1145  $file_obj->direct_download = 'N';
1146  }
1147  // Create a directory
1148  if(!FileHandler::makeDir($path)) continue;
1149 
1150  if(strncmp('./files/cache/importer/', $file_obj->file, 23) === 0)
1151  {
1152  FileHandler::rename($file_obj->file, $filename);
1153  }
1154  else
1155  {
1156  copy($file_obj->file, $filename);
1157  }
1158 
1159  // Insert the file to the DB
1160  unset($file_obj->file);
1161  if(file_exists($filename))
1162  {
1163  $file_obj->uploaded_filename = $filename;
1164  $file_obj->file_size = filesize($filename);
1165  $file_obj->comment = NULL;
1166  $file_obj->member_srl = 0;
1167  $file_obj->sid = $random->createSecureSalt(32, 'hex');
1168  $file_obj->isvalid = 'Y';
1169  $output = executeQuery('file.insertFile', $file_obj);
1170 
1171  if($output->toBool())
1172  {
1173  $uploaded_count++;
1174  $tmp_obj = null;
1175  $tmp_obj->source_filename = $file_obj->source_filename;
1176  if($file_obj->direct_download == 'Y') $files[$file_obj->source_filename] = $file_obj->uploaded_filename;
1177  else $files[$file_obj->source_filename] = getUrl('','module','file','act','procFileDownload','file_srl',$file_obj->file_srl,'sid',$file_obj->sid);
1178  }
1179  }
1180  }
1181  }
1182  }
1183  return $uploaded_count;
1184  }
1185 
1190  function getTmpFilename()
1191  {
1192  $path = "./files/cache/importer";
1193  FileHandler::makeDir($path);
1194  $filename = sprintf("%s/%d", $path, rand(11111111,99999999));
1195  if(file_exists($filename)) $filename .= rand(111,999);
1196  return $filename;
1197  }
1198 
1204  function saveTemporaryFile($fp)
1205  {
1206  $temp_filename = $this->getTmpFilename();
1207  $f = fopen($temp_filename, "w");
1208 
1209  $buff = '';
1210  while(!feof($fp))
1211  {
1212  $str = trim(fgets($fp, 1024));
1213  if(trim($str) == '</file>') break;
1214 
1215  $buff .= $str;
1216 
1217  if(substr($buff,-7)=='</buff>')
1218  {
1219  fwrite($f, base64_decode(substr($buff, 6, -7)));
1220  $buff = '';
1221  }
1222  }
1223  fclose($f);
1224  return $temp_filename;
1225  }
1226 
1227 
1233  function importExtraVars($fp)
1234  {
1235  $buff = null;
1236  while(!feof($fp))
1237  {
1238  $buff .= $str = trim(fgets($fp, 1024));
1239  if(trim($str) == '</extra_vars>') break;
1240  }
1241  if(!$buff) return array();
1242 
1243  $buff = '<extra_vars>'.$buff;
1244  $oXmlParser = new XmlParser();
1245  $xmlDoc = $this->oXmlParser->parse($buff);
1246  if(!count($xmlDoc->extra_vars->key)) return array();
1247 
1248  $index = 1;
1249  foreach($xmlDoc->extra_vars->key as $k => $v)
1250  {
1251  unset($vobj);
1252  if($v->var_idx)
1253  {
1254  $vobj->var_idx = base64_decode($v->var_idx->body);
1255  $vobj->lang_code = base64_decode($v->lang_code->body);
1256  $vobj->value = base64_decode($v->value->body);
1257  $vobj->eid = base64_decode($v->eid->body);
1258 
1259  }
1260  else if($v->body)
1261  {
1262  $vobj->var_idx = $index;
1263  $vobj->lang_code = Context::getLangType();
1264  $vobj->value = base64_decode($v->body);
1265  $vobj->eid = 'extra_vars'.$index;
1266  }
1267  $extra_vars["extra_vars".$index] = $vobj;
1268  $index++;
1269  }
1270  return $extra_vars;
1271  }
1272 }
1273 /* End of file importer.admin.controller.php */
1274 /* Location: ./modules/importer/importer.admin.controller.php */
$oModuleModel
Definition: ko.install.php:236
setMessage($message= 'success', $type=NULL)
getController($module_name)
Definition: func.inc.php:90
getNumberingPath($no, $size=3)
Definition: func.inc.php:1081
importModule($key, $cur, $index_file, $module_srl)
removeFile($filename)
$obj
Definition: ko.install.php:262
$output
Definition: ko.install.php:193
add($key, $val)
foreach($sitemap as $id=> &$val) $extra_vars
Definition: ko.install.php:180
writeFile($filename, $buff, $mode="w")
rename($source, $target)
$args
Definition: ko.install.php:185
getRemoteFile($url, $target_filename, $body=null, $timeout=3, $method= 'GET', $content_type=null, $headers=array(), $cookies=array(), $post_data=array(), $request_config=array())
$document_srl
Definition: ko.install.php:279
$oDocumentModel
Definition: ko.install.php:259
importMessage($key, $cur, $index_file)
getRealPath($source)
makeDir($path_string)
getLang($code)
const _XE_PATH_
Definition: config.inc.php:49
removeHackTag($content)
Definition: func.inc.php:1123
readFile($filename)
$oDocumentController
Definition: ko.install.php:260
getNextSequence()
Definition: func.inc.php:236
getModel($module_name)
Definition: func.inc.php:145
importComments($fp, $module_srl, $document_srl)
getFullSiteUrl()
Definition: func.inc.php:454
executeQueryArray($query_id, $args=NULL, $arg_columns=NULL)
Definition: func.inc.php:219
$module_srl
Definition: ko.install.php:254
importTrackbacks($fp, $module_srl, $document_srl)
executeQuery($query_id, $args=NULL, $arg_columns=NULL)
Definition: func.inc.php:203
importAttaches($fp, $module_srl, $upload_target_srl, &$files)
getUrl()
Definition: func.inc.php:297
importMember($key, $cur, $index_file)
if(isset($_REQUEST['encode'])) if(isset($_REQUEST['decode'])) $lang
Definition: example.php:23