| [7725] | 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | * Project: NodeMap2.0
|
|---|
| 4 | * File: FileHandler.class.php
|
|---|
| 5 | * Purpose: Checking and reading node files from the server
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | class FileHandler {
|
|---|
| 9 | private $filename;
|
|---|
| 10 | private $handle;
|
|---|
| 11 |
|
|---|
| 12 | /*
|
|---|
| 13 | * Function: __construct (constructor)
|
|---|
| 14 | * Description: Handling with a node file
|
|---|
| 15 | * Parameters: string $filename, string $mode
|
|---|
| 16 | * Returns: true is we've succesfully opened the file, otherwise false
|
|---|
| 17 | */
|
|---|
| 18 | public function __construct($filename, $mode) {
|
|---|
| 19 | $this->filename = $filename;
|
|---|
| 20 |
|
|---|
| 21 | try {
|
|---|
| 22 | trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $this->filename . '"', __FILE__, __LINE__);
|
|---|
| 23 | $this->handle = fopen($this->filename, $mode);
|
|---|
| 24 | } catch (Exception $err) {
|
|---|
| 25 | trigger_log(SYSLOG_ERR, 'Reading from file "' . $this->filename . '" failed', __FILE__, __LINE__);
|
|---|
| 26 | return false;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | return true;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /*
|
|---|
| 33 | * Function: read
|
|---|
| 34 | * Description: Try to read the file we've opened in the constructor
|
|---|
| 35 | * Parameters: -
|
|---|
| 36 | * Returns: The content of the file if we can read from the file, otherwise false
|
|---|
| 37 | */
|
|---|
| 38 | public function read() {
|
|---|
| 39 | $file = '';
|
|---|
| 40 |
|
|---|
| 41 | try {
|
|---|
| 42 | trigger_log(SYSLOG_DEBUG, 'Reading from file "' . $this->filename . '"', __FILE__, __LINE__);
|
|---|
| 43 | $file = stream_get_contents($this->handle);
|
|---|
| 44 | } catch (Exception $err) {
|
|---|
| 45 | trigger_log(SYSLOG_ERR, 'Reading from file "' . $this->filename . '" failed', __FILE__, __LINE__);
|
|---|
| 46 | return false;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | return $file;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| [7780] | 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| [7725] | 58 | /*
|
|---|
| 59 | * Function: write
|
|---|
| 60 | * Description: Try to write to the file we've opened in the constructor
|
|---|
| 61 | * Parameters: string $data
|
|---|
| 62 | * Returns: true if we can write to the file, otherwise false
|
|---|
| 63 | */
|
|---|
| 64 | public function write($data) {
|
|---|
| [7780] | 65 |
|
|---|
| 66 | $filename = date( "m_d_y.kml" );
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 | try{
|
|---|
| 70 | trigger_log(SYSLOG_DEBUG, 'Opening the file "' . $filename . '"', __FILE__, __LINE__);
|
|---|
| 71 | $file = new FileHandler($filename, 'w');
|
|---|
| 72 | } catch (Exception $err) {
|
|---|
| 73 | trigger_log(SYSLOG_ERR, 'Opening the file "' . $filename . '"', __FILE__, __LINE__);
|
|---|
| 74 | return false;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | try{
|
|---|
| 78 | trigger_log(SYSLOG_DEBUG, 'Writing file "' . $filename . '"', __FILE__, __LINE__);
|
|---|
| 79 | $file->write($file,$this->$data);
|
|---|
| 80 | } catch (Exception $err) {
|
|---|
| 81 | trigger_log(SYSLOG_ERR, 'Writing the file "' . $filename . '"', __FILE__, __LINE__);
|
|---|
| 82 | return false;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | try{
|
|---|
| 87 | trigger_log(SYSLOG_DEBUG, 'Closing file "' . $filename . '"', __FILE__, __LINE__);
|
|---|
| 88 | fclose($file);
|
|---|
| 89 | } catch (Exception $err) {
|
|---|
| 90 | trigger_log(SYSLOG_ERR, 'Closing the file "' . $filename . '"', __FILE__, __LINE__);
|
|---|
| 91 | return false;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | return true;
|
|---|
| 95 |
|
|---|
| [7725] | 96 |
|
|---|
| [7780] | 97 |
|
|---|
| [7725] | 98 | }
|
|---|
| 99 |
|
|---|
| 100 | public function __destruct() {
|
|---|
| 101 | trigger_log(SYSLOG_DEBUG, 'Closing file "' . $this->filename . '"', __FILE__, __LINE__);
|
|---|
| 102 | fclose($this->handle);
|
|---|
| 103 | }
|
|---|
| [7620] | 104 | }
|
|---|