2012-03-16 22:55

[PHP] 檢查 XML 文件結構

利用 SimpleXML 去檢查 XML 結構是否符合規格,為了讓這個程式可以多用途,採用了一個基準文件的作為結構準則,依據裡面定義的節點跟屬性,去檢查文件是否符合基本要求的格式。

  1. <?php 
  2.  
  3. /**檢查 XML 文件結構 
  4. * @param string $baseFilePath 基準結構文件 
  5. * @param string $checkFilePath 待檢查文件 
  6. * @return bool 當結構與基準文件相符合時則回傳 true,否則是 false 
  7. * */ 
  8. function checkXmlFileStructure($baseFilePath,$checkFilePath){ 
  9.    /*開啟 Base File*/ 
  10.    if(!file_exists($baseFilePath)){ return false; } 
  11.    $base = simplexml_load_file($baseFilePath); 
  12.    if($base===false){ return false; } 
  13.  
  14.    /*開啟 Check File*/ 
  15.    if(!file_exists($checkFilePath)){ return false; } 
  16.    $check = simplexml_load_file($checkFilePath); 
  17.    if($check===false){ return false; } 
  18.  
  19.    /*比較起始點的名稱*/ 
  20.    if($base->getName() != $check->getName()){ return false; } 
  21.  
  22.    /*比較結構*/ 
  23.    return checkXmlStructure($base,$check); 
  24. } 
  25.  
  26. /**檢查 XML 結構 
  27. * @param SimpleXMLElement $base 基準結構物件 
  28. * @param SimpleXMLElement $check 待檢查 XML 物件 
  29. * @return bool 當結構與基準物件相符合時則回傳 true,否則是 false 
  30. * */ 
  31. function checkXmlStructure($base,$check){ 
  32.    /*檢查屬性*/ 
  33.    foreach ($base->attributes() as $name => $baseAttr){ 
  34.        /*必要的屬性不存在*/ 
  35.        if(!isset($check->attributes()->$name)){ return false; } 
  36.    } 
  37.  
  38.    /*當沒有子節點時,則檢查對象也不能有子節點*/ 
  39.    if(count($base->children())==0){ 
  40.        return (count($check->children())==0); 
  41.    } 
  42.  
  43.    /*將檢查對象的子節點分群*/ 
  44.    $checkChilds = array(); 
  45.    foreach($check->children() as $name => $child){ 
  46.        $checkChilds[$name][] = $child; 
  47.    } 
  48.  
  49.    /*檢查子節點*/ 
  50.    $checked = array(); 
  51.    foreach($base->children() as $name => $baseChild){ 
  52.        /*跳過已經檢查的子節點*/ 
  53.        if(in_array($name, $checked)){ continue; } 
  54.        $checked[] = $name; 
  55.  
  56.        /*檢查必要的子節點是否存在*/ 
  57.        if(empty($checkChilds[$name])){ return false; } 
  58.  
  59.        foreach ($checkChilds[$name] as $child){ 
  60.            /*遞迴檢查子節點*/ 
  61.            if( !checkXmlStructure($baseChild, $child) ){ return false; } 
  62.        } 
  63.    } 
  64.  
  65.    return true; 
  66. } 
  67.  
  68.  
  69. /*==============================================================================*/ 
  70.  
  71. if(isset($_SERVER['argv'])){ 
  72.    parse_str(preg_replace('/&[\-]+/','&',join('&',$_SERVER['argv'])), $_GET); 
  73.  
  74.    if(empty($_GET['base_file']) || empty($_GET['check_file'])){ 
  75.        echo "Run: ".basename(__FILE__)." base_file=base.xml check_file=check.xml\n"; exit(1); 
  76.    } 
  77.  
  78.    exit( checkXmlFileStructure($_GET['base_file'],$_GET['check_file']) ? 0 : 1); 
  79.  
  80. }else{ 
  81.    if(empty($_GET['base_file']) || empty($_GET['check_file'])){ 
  82.        echo "Run: ".basename(__FILE__)."?base_file=base.xml&check_file=check.xml<br />"; exit; 
  83.    } 
  84.  
  85.    echo( checkXmlFileStructure($_GET['base_file'],$_GET['check_file']) ? '1' : '0'); 
  86. } 


使用方式(shell)
  1. php check_xml_file_structure.php base_file=base.xml check_file=check.xml 
  2.  
  3. if [ "j$?" != "j0" ]; then 
  4.    echo "Run Error" 
  5. fi 


測試範例 1
base_1.xml
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <items> 
  3.    <item> 
  4.        <Category>Category文字</Category> 
  5.        <Title>Title文字</Title> 
  6.    </item> 
  7. </items> 

check_1.xml
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <items> 
  3.    <item> 
  4.        <Category>Category文字</Category> 
  5.        <Title>Title文字</Title> 
  6.    </item> 
  7.    <item> 
  8.        <Category>Category文字</Category> 
  9.        <Title>Title文字</Title> 
  10.        <Description>Description文字</Description> 
  11.    </item> 
  12. </items> 


測試範例 2
base_2.xml
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <items> 
  3.    <item category="Category文字" Title="Title文字"/> 
  4. </items> 

check_2.xml
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <items> 
  3.    <item category="Category文字" Title="Title文字" Description="Description文字" /> 
  4.    <item category="Category文字" Title="Title文字" /> 
  5.    <item category="Category文字" Title="Title文字" Description="Description文字" /> 
  6. </items> 
2012-03-14 14:48

[Ubuntu 11] 安裝 Redmine 與 SVN

安裝 LAMP & Redmine
  1. # 安裝 LAMP 
  2. apt-get install apache2 php5 mysql-server php5-mysql libapache2-mod-php5 libapache2-mod-auth-mysql  
  3.  
  4. # 安裝 svn, dav_svn 
  5. apt-get install subversion libapache2-svn  
  6.  
  7. # 安裝 redmine 
  8. apt-get install redmine redmine-mysql libapache2-mod-fcgid libapache2-mod-passenger 
  9.  
  10. # 可以使用下面的方式重新設定 redmine DB 連結 
  11. #dpkg-reconfigure redmine  
  12.  
  13. # 啟用 Apache 套件 php5, headers, expires 
  14. a2enmod php5 dav_svn auth_mysql cgid fcgid passenger rewrite ssl setenvif  
  15.  
  16. # 安裝 git, phpmyadmin 
  17. apt-get install mercurial git-core phpmyadmin 
  18.  
  19. # 連結 phpmyadmin 設定檔 
  20. cd /etc/apache2/conf.d/ 
  21. ln -s ../../phpmyadmin/apache.conf phpmyadmin.conf 
  22. service apache2 restart 


安裝 Redmine 套件
  1. gem install pandoc-ruby rdiscount rpeg-markdown bluefeather 
  2.  
  3. cd /usr/share/redmine/vendor/plugins 
  4.  
  5.  
  6. # 安裝 Code Review 套件 
  7. hg clone https://bitbucket.org/haru_iida/redmine_code_review 
  8. rake db:migrate_plugins RAILS_ENV=production 
  9.  
  10.  
  11. # 安裝 Markdown Extra formatter 套件 
  12. git clone git://github.com/juno/redmine_markdown_extra_formatter.git 
  13.  
  14.  
  15. # 安裝 reStructuredText formatting 套件 
  16. git clone git://github.com/alphabetum/redmine_restructuredtext_formatter.git 
  17. cd redmine_restructuredtext_formatter 
  18. git checkout pandoc-ruby 


建立SVN庫
  1. mkdir -p /home/repoadmin/repos 
  2. svnadmin create /home/repoadmin/repos/team1 
  3. svnadmin create /home/repoadmin/repos/team2 
  4. chown -R www-data:www-data /home/repoadmin/repos 


建立SVN認證時需要的DB使用者(redmine_svn_auth)
  1. CREATE USER 'redmine_svn_auth'@'localhost' IDENTIFIED BY 'redmine_svn_auth'; 
  2. GRANT SELECT(id, login, hashed_password, status ,type) ON redmine_default.users TO 'redmine_svn_auth'@'localhost'; 


設定 Apache2 Site
  1. cd /etc/apache2/sites-available/ 
  2. cp default redmine_svn 
  3. vim redmine_svn 

  1. <VirtualHost *:80> 
  2.    ServerAdmin webmaster@localhost 
  3.    DocumentRoot /usr/share/redmine/public 
  4.  
  5.    <Directory /> 
  6.        Options FollowSymLinks 
  7.        AllowOverride None 
  8.    </Directory> 
  9.  
  10.    <Directory /usr/share/redmine/public> 
  11.        Options ExecCGI FollowSymLinks 
  12.        AllowOverride None 
  13.        Order allow,deny 
  14.        Allow from all 
  15.  
  16.        RewriteEngine On 
  17.        RewriteRule ^$ index.html [QSA] 
  18.        RewriteRule ^([^.]+)$ $1.html [QSA] 
  19.        RewriteCond %{REQUEST_FILENAME} !-f [OR] 
  20.        RewriteCond %{REQUEST_FILENAME} dispatch.fcgi$ 
  21.        RewriteRule ^(.*)$ dispatch.fcgi [QSA,L] 
  22.    </Directory> 
  23.  
  24.    <Location /svn> 
  25.        DAV svn 
  26.        SVNParentPath /home/repoadmin/repos 
  27.        SVNPathAuthz off 
  28.  
  29.        AuthBasicAuthoritative Off 
  30.        AuthUserFile /dev/null 
  31.        AuthType Basic 
  32.        AuthName "Subversion Repository" 
  33.  
  34.        # auth_mysql help in http://localhost/doc/libapache2-mod-auth-mysql/DIRECTIVES.gz 
  35.        Auth_MYSQL On 
  36.        Auth_MySQL_Host localhost 
  37.        Auth_MYSQL_DB redmine_default 
  38.        Auth_MYSQL_Username redmine_svn_auth 
  39.        Auth_MYSQL_Password redmine_svn_auth 
  40.        Auth_MYSQL_Password_Table users 
  41.        Auth_MYSQL_Username_Field login 
  42.        Auth_MYSQL_Password_Field hashed_password 
  43.        Auth_MySQL_Password_Clause " AND status=1 AND type='User' " 
  44.        Auth_MYSQL_Empty_Passwords Off 
  45.        Auth_MYSQL_Encryption_Types SHA1Sum 
  46.        # Options: Crypt_DES, Crypt_MD5, Crypt, PHP_MD5, SHA1Sum, MySQL, Apache 
  47.  
  48.        Require valid-user 
  49.    </Location> 
  50.  
  51.    LogLevel warn 
  52.    CustomLog /var/log/apache2/redmine_access.log combined 
  53.    ErrorLog /var/log/apache2/redmine_error.log 
  54. </VirtualHost> 


重新啟動 Apache
  1. a2dissite default 
  2. a2ensite redmine_svn 
  3. service apache2 restart 


測試網址


參考來源:
將 Redmine 安裝於 Debian、Ubuntu Linux
Ubuntu 10.04 using Passenger
svn 使用和 redmine 相同帳號進行認證
2012-03-08 19:38

Aptana Unified Builder 卡住

Aptana 2 的建置器(Aptana Unified Builder)時不時就會卡住,通常會發生這種狀況的原因,就是專案中有某個檔案無法正常解析,或路徑不對造成的。

解決的辦法是先關閉『專案->自動建置』,然後將有問題的專案複製一份到新的工作區,然後用過濾法找出無效的檔案,然後先刪除再重新建立。
2012-03-08 16:43

Ubuntu 10 設定預設語系

  1. sudo vim /etc/default/locale 
  2. LANG="zh_TW.UTF-8" 
  3. LANGUAGE="zh_TW:zh" 
2012-03-08 16:00

[Linux] 使用 MD5 檢查 FTP 上傳的檔案

  1. #!/bin/bash 
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin 
  3. export PATH 
  4.  
  5. MAIL_TO="my_mail@gmail.com" 
  6. TARGET_PATH="/var/www/file/show.mp4" 
  7. FILE="upload_file" 
  8. EXTEND="mp4" 
  9.  
  10. cd /home/ftp_user 
  11.  
  12.  
  13. # md5 file is not exist 
  14. if [ ! -f $FILE.md5 ]; then exit 0; fi 
  15.  
  16. # MD5 check sum  
  17. md5sum -c $FILE.md5 >/dev/null 2>&1 
  18. if [ "j$?" != "j0" ]; then   
  19. # Notice: md5 check fail  
  20. echo "" |mail -s "[Failed] $FILE.$EXTEND md5 check sum fail" $MAIL_TO 
  21. rm -f ./$FILE.md5 
  22. exit 1;  
  23. fi 
  24.  
  25. mv ./$FILE.$EXTEND $TARGET_PATH 
  26. rm -f ./$FILE.md5 
  27.  
  28. exit 0; 
2012-03-07 14:06

[Ubuntu] 安裝 Oracle Client 與 PDO_OCI

Oracle Database Instant Client 下載 Client/SDK
(Version 10.2.0.4 Instant Client Package - Basic, Instant Client Package - SDK)
oracle-instantclient-basic-10.2.0.4-1.i386.rpm
oracle-instantclient-devel-10.2.0.4-1.i386.rpm

  1. # 安裝套件轉換器 
  2. apt-get install alien  
  3.  
  4. # 轉換 rpm 套件到 deb,並安裝 
  5. alien -i oracle-instantclient-basic*.rpm 
  6. alien -i oracle-instantclient-devel*.rpm 
  7.  
  8. # 安裝 Apache2,PHP,MySQL 
  9. apt-get install apache2 php5 mysql-server php5-mysql libapache2-mod-php5 
  10.  
  11. # 安裝 PEAR  
  12. apt-get install php-pear php5-dev dh-make-php make re2c 
  13.  
  14. # 下載 PDO_OCI 原始檔  
  15. pecl download pdo 
  16. pecl download pdo_oci 
  17. tar zxvf PDO-1.0.3.tgz 
  18. tar zxvf PDO_OCI-1.0.tgz 
  19.  
  20. mkdir -p PDO_OCI-1.0/include/php/ext/ 
  21. mv PDO-1.0.3 PDO_OCI-1.0/include/php/ext/pdo 
  22. cd PDO_OCI-1.0/ 
  23. phpize 
  24. ./configure --with-pdo-oci=instantclient,/usr,10.2.0.4 
  25.  
  26. make -j$(grep processor /proc/cpuinfo |wc -l) 
  27. make install  # /usr/lib/php5/20xxxxxx+lfs/pdo_oci.so 
  28.  
  29. vim /etc/php5/conf.d/pdo_oci.ini # 建立 pdo_oci.ini, 內容如下: 
  30. extension=pdo_oci.so 
  31.  
  32. vim /etc/apache2/envvars # 在最後面加入環境變數, 內容如下:  
  33. export NLS_LANG="TRADITIONAL CHINESE_TAIWAN.UTF8" 
  34. export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS" 
  35.  
  36. # 重新啟動 Apache  
  37. service apache2 restart 

參考來源:
Debian 安裝設定 PHP 連 Oracle extension 使用 PDO(PDO_OCI)
Oracle Instant Client