PHP Classes

File: convert.class.php

Recommend this page to a friend!
  Classes of Eper Kalman   PHP Comment File   convert.class.php   Download  
File: convert.class.php
Role: Auxiliary script
Content type: text/plain
Description: Converter
Class: PHP Comment File
Record and display page comments in files
Author: By
Last change:
Date: 11 years ago
Size: 2,618 bytes
 

Contents

Class file image Download
<?php
   
/**
     * The MIT License (MIT)
     *
     * Copyright (c) <2013> <Eper Kalman>
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
     * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
     * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
     * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
     *
     * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
     * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
     * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
     * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     *
     */

    /**
     * Converter from comments file format 1.0 to file format 2.0
     */
    

   
class Convert {
   
        var
$_directory = '';
       
        public function
directory($dir) {
       
            if (!
is_writable($dir)) exit("Directory $dir is not writable !");
       
           
$this->_directory = $dir;
        }
         
        public function
start() {
     
            if (
$dh =opendir($this->_directory)) {
           
                while ((
$file = readdir($dh)) != FALSE ) {
               
                   
$filename = $this->_directory.'/'.$file;

                    if ( (
$file==".") or ($file=="..") or
                         ((
strtolower(substr($file, -4)) != '.cmm') && (is_file($filename))) ) continue;

                   
$lines = $this->update(file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
                   
                   
$this->write($lines, $filename);
                   
                    echo
"<br>File $filename updated.";
                }
               
               
closedir($dh);
            }
        }
       
        private function
update($lines) {
       
           
$updated = array();
       
           
$count = count($lines);
           
            for (
$i=0; $i<$count; $i+=4) {
           
               
$updated[] = $lines[$i];
               
$updated[] = '-';
               
$updated[] = $lines[$i+1];
               
$updated[] = $lines[$i+2];
               
$updated[] = $lines[$i+3];
            }
           
            return
$updated;
        }
       
        private function
write($lines, $file) {
       
            if (
$fh = @fopen($file, 'w')) {
           
               
fwrite($fh, implode($lines, PHP_EOL).PHP_EOL);
               
               
fclose($fh);
            }
        }
    }
   
   
# Uncheck to start converting files
   
    /*
   
    $convert = new Convert();
   
    $convert->directory('comments');
   
    $convert->start();
   
    */
   
?>