<?php /*~ textreplace.inc.php
.---------------------------------------------------------------------------.
|  Software: Text Replacement Library                                        |
|   Version: 0.1                                                            |
|   Contact: via http://www.uniwits.com/contact                             |
|      Info: http://www.uniwits.com/                                        |
|   Support: http://www.uniwits.com/support                                 |
|      Note: This library is designed for Linux and compatible systems.     |
| ------------------------------------------------------------------------- |
|     Admin: Yuxiang Mao (project admininistrator)                          |
|   Authors: Yuxiang Mao (Uniwits) contact@uniwits.com                      |
|   Founder: Yuxiang Mao                                                    |
| Copyright (c) 2010-2012, Yuxiang Mao. All Rights Reserved.                |
| ------------------------------------------------------------------------- |
|   License: Distributed under Uniwits Shareware License (USL)              |
|            http://www.uniwits.com/licenses/shareware/                     |
| This program is distributed as a part of software provided by Uniwits     |
| System WITHOUT ANY WARRANTY; without even the implied warranty of         |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                      |
| ------------------------------------------------------------------------- |
*/ function textreplace_transform_files($tr_table_file_or_xml,$textfiles=array()) { clearstatcache(); $tr_table_xml=null; $trtf_time=0; if (is_string($tr_table_file_or_xml) && is_file($tr_table_file_or_xml)) $trtf_time=filemtime($tr_table_file_or_xml); foreach ($textfiles as $tf) { $dest_labels=array(); $savable=true; if (is_file($tf['src'])) { $src_time=max($trtf_time,@filemtime($tf['src'])); $dest_time=0+@filemtime($tf['dest']); if ($dest_time<$src_time) { if (!$tr_table_xml) { if ($trtf_time) $tr_table_xml=@simplexml_load_file($tr_table_file_or_xml); if ($tr_table_file_or_xml instanceof SimpleXMLElement) $tr_table_xml=$tr_table_file_or_xml; } if ($tr_table_xml) { $src_labels=TemplateLoader::loadLabelFromFile($tf['src']); $dest_labels=textreplace_transform_labels($tr_table_xml,$src_labels); } } else $savable=false; } if ($savable) textreplace_save_labels($dest_labels,$tf['dest']); } } function textreplace_transform_labels($tr_table_xml,$src_labels) { $dest_labels=array(); foreach ($src_labels as $key=>$text) { foreach ($tr_table_xml->rule as $rule) $text=str_replace($rule->from,$rule->to,$text); $dest_labels[$key]=$text; } return $dest_labels; } function textreplace_save_labels($dest_labels,$dest_file,$line_sep="\n") { $dir=dirname($dest_file); @mkdir($dir,0777,true); $fp=fopen($dest_file,"w"); if (!$fp) return false; foreach ($dest_labels as $key=>$text) { if (strpbrk($text,"\r\n")) { $eot="EOT"; while (strpos($text,$eot)!==false) $eot.=rand(0,9); fwrite($fp,$key."=<<<".$eot.$line_sep.$text.$line_sep.$eot.$line_sep); } else fwrite($fp,$key."=".$text.$line_sep); } fclose($fp); return true; } ?>