PageRenderTime 26ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/components/log/FileTarget.php

https://gitlab.com/nitm/yii2-module
PHP | 240 lines | 215 code | 6 blank | 19 comment | 0 complexity | bb78500e426493271d757f7658d33210 MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://www.ninjasitm.com/
  4. * @copyright Copyright (c) 2014 NinjasITM INC
  5. * @license http://www.ninjasitm.com/license/
  6. */
  7. namespace nitm\components\log;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\helpers\FileHelper;
  11. /**
  12. * FileTarget records log messages in a file.
  13. *
  14. * The log file is specified via [[logFile]]. If the size of the log file exceeds
  15. * [[maxFileSize]] (in kilo-bytes), a rotation will be performed, which renames
  16. * the current log file by suffixing the file name with '.1'. All existing log
  17. * files are moved backwards by one place, i.e., '.2' to '.3', '.1' to '.2', and so on.
  18. * The property [[maxLogFiles]] specifies how many history files to keep.
  19. *
  20. * @author Qiang Xue <qiang.xue@gmail.com>
  21. * @since 2.0
  22. */
  23. class FileTarget extends \yii\log\FileTarget
  24. {
  25. public $collectionName = 'nitmlog';
  26. public $filename; // default log file name
  27. public $fullpath;
  28. //protected data
  29. protected $msgClose = "End of Log Transaction";
  30. protected $msgNew = "Begining New Log";
  31. protected $msgApp = "Adding New Log Information";
  32. //private data
  33. private $handle; // this will hold the resource for the open file
  34. private $logDir = "@runtime/logs/logger";
  35. const L_FERM = -10001; //code to write a default close message
  36. const L_SHIN = -10002; //code to write a default new data message
  37. const L_CONC = -10003; //code to write default append message
  38. const ERR_NOFNAME = "Error : Empty File (Enter filename as first argument to constructor)"; //code to indicate empty filename
  39. const ERR_BADDIR = "Error : Log directory (%s) doesn't exist"; //code to indicate invalid directory
  40. const ERR_DIRCREATE = "Error : Unable to create sub diretory (%s)"; //code to indicate invalid directory
  41. const SEP = DIRECTORY_SEPARATOR;
  42. public function read()
  43. {
  44. // open file
  45. // call file opening function
  46. $this->fullpath = $this->logDir.$this->filename;
  47. $this->handle = fopen($this->fullpath,'rb');
  48. if(!is_resource($this->handle))
  49. {
  50. $errmsg = "Log error: Unable to open file: {$this->fullpath}\n
  51. <br>Suggestion: Check file path and folder permissions.\n
  52. <br>script exiting(3)\n\n";
  53. echo $errmsg;
  54. $this->endLog();
  55. exit(3);
  56. }
  57. $contents = fread($this->handle);
  58. $this->endLog();
  59. return $contents;
  60. }
  61. public function __destruct()
  62. {
  63. unset($this->handle, $this);
  64. }
  65. // end open
  66. public function write($logtext="", $notime=false, $tag=false)
  67. {
  68. // call file opening function
  69. if(!$this->handle)
  70. {
  71. $this->open();
  72. }
  73. $text = ($notime == false) ? date("D-M-d-Y [H:i:s]")." - $logtext\n" : $logtext."\n";
  74. // write log file heading
  75. // now add defined log content
  76. switch($this->ext)
  77. {
  78. case ".txt":
  79. $text = strip_tags($text);
  80. break;
  81. case ".html":
  82. case ".xml":
  83. switch($tag == false)
  84. {
  85. case true:
  86. case 1:
  87. break;
  88. case false:
  89. case 0:
  90. $o = preg_replace("/($tag)/", "<\\1>", $tag);
  91. $c = preg_replace("/($tag)/", "</\\1>", $tag);
  92. $logtext = $o.$logtext.$c;
  93. break;
  94. default:
  95. break;
  96. }
  97. break;
  98. default:
  99. $text = strip_tags($text);
  100. break;
  101. }
  102. if(Session::getVal("settings.globals.allow_log") == true)
  103. {
  104. if(fwrite($this->handle, wordwrap($text, 512, "\n\n")) === false)
  105. {
  106. // write faled issue error
  107. $errmsg = "Log error: Unable to write to file: {$this->fullpath}\n
  108. <br>Suggestion: Check file path and folder permissions.\n
  109. <br>script exiting(4)\n\n";
  110. echo $errmsg;
  111. $this->__destruct();
  112. exit(4);
  113. }
  114. }
  115. return true;
  116. }
  117. public function changeFile($filename, $dir=null)
  118. {
  119. if($filename)
  120. {
  121. $this->filename = $filename."-".date("Y-m-d")."-log".$this->ext; //set filename
  122. $this->logDir = ($dir == null) ? $this->logDir : $dir;
  123. }
  124. else
  125. {
  126. echo self::ERR_NOFNAME."\r\n";
  127. }
  128. }
  129. public function changeDir($dir)
  130. {
  131. if(is_dir($dir))
  132. {
  133. $this->logDir = ($dir == null) ? $this->logDir : $dir;
  134. }
  135. }
  136. public function changeExt($ext)
  137. {
  138. if($ext)
  139. {
  140. $this->ext = ($ext == null) ? $this->ext : $ext;
  141. }
  142. }
  143. private function open()
  144. {
  145. // open file
  146. $this->fullpath = $this->logDir.$this->filename;
  147. if(file_exists($this->fullpath))
  148. {
  149. $this->handle = fopen($this->fullpath,'a+');
  150. if(!$this->handle)
  151. {
  152. return false;
  153. }
  154. $msg = $this->msgApp;
  155. }
  156. else
  157. {
  158. $this->handle = fopen($this->fullpath,'w+b');
  159. if(!$this->handle)
  160. {
  161. return false;
  162. }
  163. $msg = $this->msgNew;
  164. }
  165. if(!is_resource($this->handle))
  166. {
  167. $errmsg = "Log error: Unable to open file: {$this->fullpath}\n
  168. <br>Suggestion: Check file path and folder permissions.\n
  169. <br>script exiting(2)\n\n";
  170. echo $errmsg;
  171. $this->endLog();
  172. exit(3);
  173. }
  174. @chmod($this->fullpath, 0775);
  175. $this->write("\n".str_repeat("-", 100), true);
  176. $this->write(date("[M-d-Y H:i:s] ").$msg." user: ".$this->currentUser->username, 1);
  177. $this->write("\n".str_repeat("-", 100), true);
  178. return $this->fullpath;
  179. }
  180. // end open
  181. public function endLog()
  182. {
  183. if(is_resource($this->handle))
  184. {
  185. $this->write("\n".str_repeat("-", 100), true);
  186. $this->write(date("[M-d-Y H:i:s] ").$this->msgClose, 1);
  187. $this->write("\n".str_repeat("-", 100), true);
  188. fclose($this->handle);
  189. $this->handle = false;
  190. }
  191. }
  192. //-- end endLog
  193. public function prepareFile($filename, $dir=null, $ext=null)
  194. {
  195. if($filename)
  196. {
  197. $this->ext = ($ext == null) ? $this->ext : $ext;
  198. $this->filename = $filename."-".date("Y-m-d")."-log".$this->ext; //set filen name
  199. $dir = ($dir == null) ? null : (($dir[strlen($dir) -1] == self::SEP) ? $dir : $dir.self::SEP);
  200. $this->logDir = ($dir == null) ? $this->logDir.$filename.self::SEP : $dir.$filename.self::SEP;
  201. if(!is_dir($this->logDir))
  202. {
  203. if(!mkdir($this->logDir, 0775, true))
  204. {
  205. die(sprintf(self::ERR_DIRCREATE, $this->logDir)."\r\n");
  206. }
  207. }
  208. /*else
  209. {
  210. die(sprintf(self::ERR_BADDIR, $this->logDir)."\r\n");
  211. }*/
  212. }
  213. else
  214. {
  215. //echo self::ERR_NOFNAME."\r\n";
  216. }
  217. // parent::__construct();
  218. // echo "<h1>currentUser == $this->currentUser sccess string == ".parent::securer.".username</h1>";
  219. // exit;
  220. }
  221. }