PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/nabserv/nabaztag.php

https://github.com/mkweb/NabServ
PHP | 425 lines | 162 code | 81 blank | 182 comment | 16 complexity | 8cfeb5ba2b274fa1b8619786f90ac250 MD5 | raw file
  1. <?php
  2. namespace nabserv;
  3. use \base\Logger;
  4. /**
  5. * Class Nabaztag
  6. *
  7. * Handling relevant data for current Nabaztag
  8. * Data is saved serialized in cache-file
  9. *
  10. * @package nabserv
  11. *
  12. * @author Mario Klug <mario.klug@mk-web.at>
  13. */
  14. class Nabaztag {
  15. /**
  16. * Random Hash to identify this Object
  17. * @param String
  18. */
  19. private $uid;
  20. /**
  21. * Serialnumber
  22. * @param String
  23. */
  24. private $serial;
  25. /**
  26. * Path to cache with all relevant for this Nabaztag
  27. * @param String
  28. */
  29. private $file;
  30. /**
  31. * Path to cache with all configurations for this Nabaztag
  32. * @param String
  33. */
  34. private $configFile;
  35. /**
  36. * All relevant data for this Nabaztag
  37. * @param Array
  38. */
  39. private $data = array();
  40. /**
  41. * All configurations for this Nabaztag
  42. * @param Array
  43. */
  44. private $configuration = array();
  45. /**
  46. * This var tells destructor if config should be saved
  47. * @param Boolean
  48. */
  49. private $configurationChanged = false;
  50. /**
  51. * Singleton-Instances
  52. * @param Array
  53. */
  54. private static $instances = array();
  55. /**
  56. * Singleton
  57. *
  58. * @static
  59. * @access public
  60. *
  61. * @param Int
  62. *
  63. * @return Nabaztag
  64. */
  65. public static function getInstance($serial) {
  66. if(!isset(self::$instances[$serial])) {
  67. self::$instances[$serial] = new Nabaztag($serial);
  68. }
  69. return self::$instances[$serial];
  70. }
  71. /**
  72. * Constructor
  73. *
  74. * Prepare data-cache and starts loading saved data
  75. *
  76. * @access public
  77. *
  78. * @param String
  79. */
  80. public function __construct($serial) {
  81. $this->uid = md5(microtime());
  82. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  83. $this->serial = $serial;
  84. $this->file = PATH_DB . DS . 'nabaztag' . DS . join(DS, str_split($this->serial, 2)) . DS . $this->serial . DS . 'nabaztag.serialized';
  85. $this->configFile = PATH_DB . DS . 'nabaztag' . DS . join(DS, str_split($this->serial, 2)) . DS . $this->serial . DS . 'configuration.serialized';
  86. $this->createFileIfNotExists($this->file);
  87. $this->createFileIfNotExists($this->configFile);
  88. $this->load();
  89. $this->loadConfiguration();
  90. }
  91. /**
  92. * Loads saved data from cache
  93. *
  94. * @access public
  95. */
  96. public function load() {
  97. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  98. $this->data = array();
  99. if(file_exists($this->file)) {
  100. $this->data = unserialize(file_get_contents($this->file));
  101. }
  102. if(!isset($this->data['ears'])) {
  103. $this->data['ears'] = array();
  104. $this->data['ears']['left'] = 0;
  105. $this->data['ears']['right'] = 0;
  106. }
  107. }
  108. /**
  109. * Loads configuration
  110. *
  111. * @access public
  112. */
  113. public function loadConfiguration() {
  114. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  115. $this->configuration = unserialize(file_get_contents($this->configFile));
  116. }
  117. /**
  118. * Creates directory and cache-file if not exists
  119. *
  120. * @access private
  121. *
  122. * @return Boolean
  123. */
  124. private function createFileIfNotExists($path) {
  125. if(file_exists($path)) {
  126. return false;
  127. }
  128. $file = substr($path, strlen(PATH_ROOT));
  129. $tmp = explode("/", $file);
  130. if($tmp[0] == '') array_shift($tmp);
  131. $fileName = array_pop($tmp);
  132. $currentPath = PATH_ROOT;
  133. foreach($tmp as $dir) {
  134. $currentPath .= '/' . $dir;
  135. if(!file_exists($currentPath)) {
  136. mkdir($currentPath, 0777);
  137. } else {
  138. }
  139. }
  140. touch($path);
  141. chmod($path, 0777);
  142. return true;
  143. }
  144. /**
  145. * Returns data by key
  146. *
  147. * @access public
  148. *
  149. * @param String
  150. *
  151. * @return mixed (String|Array)
  152. */
  153. public function setData($key, $data) {
  154. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  155. $this->load();
  156. $this->data[$key] = $data;
  157. $this->save();
  158. }
  159. /**
  160. * Set data by key - seen = false is appended, so that plugins can read this
  161. *
  162. * @access public
  163. *
  164. * @param String
  165. * @param mixed (String|Array)
  166. */
  167. public function setNewData($key, $data) {
  168. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  169. $this->load();
  170. Logger::debug(__METHOD__ . ' loaded data: ' . print_r($this->data, true), 'nabaztag');
  171. $this->data[$key] = array('data' => $data, 'seen' => false);
  172. Logger::debug(__METHOD__ . ' after modification: ' . print_r($this->data, true), 'nazaztag');
  173. $this->save();
  174. }
  175. /**
  176. * Set Configuration-Value
  177. *
  178. * @access public
  179. *
  180. * @param String
  181. * @param mixed (String|Array)
  182. */
  183. public function setConfig($key, $value) {
  184. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  185. $this->configuration[$key] = $value;
  186. $this->saveConfiguration();
  187. }
  188. /**
  189. * Getter for Serialnumber
  190. *
  191. * @access public
  192. *
  193. * @return String
  194. */
  195. public function getSerial() {
  196. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  197. return $this->serial;
  198. }
  199. /**
  200. * Returns data by key
  201. *
  202. * @access public
  203. *
  204. * @param String
  205. *
  206. * @return mixed (String|Array)
  207. */
  208. public function getData($key) {
  209. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  210. $this->load();
  211. return (isset($this->data[$key]) ? $this->data[$key] : null);
  212. }
  213. /**
  214. * Returns all data
  215. *
  216. * @access public
  217. *
  218. * @return Array
  219. */
  220. public function getAllData() {
  221. $this->load();
  222. return $this->data;
  223. }
  224. /**
  225. * Returns config by key
  226. *
  227. * @access public
  228. *
  229. * @param String
  230. *
  231. * @return mixed (String|Array)
  232. */
  233. public function getConfig($key) {
  234. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  235. $this->load();
  236. return (isset($this->configuration[$key]) ? $this->configuration[$key] : null);
  237. }
  238. /**
  239. * Returns all config
  240. *
  241. * @access public
  242. *
  243. * @return Array
  244. */
  245. public function getAllConfig() {
  246. $this->loadConfiguration();
  247. return $this->configuration;
  248. }
  249. /**
  250. * Remove key from config
  251. *
  252. * @access public
  253. *
  254. * @param String
  255. */
  256. public function removeConfig($key) {
  257. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  258. $this->load();
  259. if(isset($this->configuration[$key])) {
  260. unset($this->configuration[$key]);
  261. $this->saveConfiguration();
  262. }
  263. }
  264. /**
  265. * Returns array with all unseen data
  266. *
  267. * @access public
  268. *
  269. * @return Array
  270. */
  271. public function getNewData() {
  272. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  273. $this->load();
  274. $return = array();
  275. foreach($this->data as $key => $value) {
  276. if(isset($value) && (!isset($value['seen']) || $value['seen'] == false)) {
  277. if(isset($value['data'])) {
  278. $return[$key] = $value['data'];
  279. }
  280. }
  281. }
  282. return $return;
  283. }
  284. /**
  285. * Set data as seen by Nabaztag
  286. * Date will be appended too
  287. *
  288. * @access public
  289. *
  290. * @param String
  291. * @param Bool
  292. */
  293. public function setSeen($key, $value) {
  294. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  295. $this->load();
  296. if(isset($this->data[$key])) {
  297. $this->data[$key]['seen'] = $value;
  298. $this->data[$key]['last'] = ($value ? time() : null);
  299. }
  300. $this->save();
  301. }
  302. /**
  303. * Saves data to cache-file
  304. *
  305. * @access public
  306. */
  307. public function save() {
  308. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  309. $content = serialize($this->data);
  310. if(!is_writeable($this->file)) {
  311. Logger::warn('File ' . $this->file . ' is not writeable', 'nabaztag');
  312. }
  313. $fh = fopen($this->file, 'w');
  314. fputs($fh, $content);
  315. fclose($fh);
  316. Logger::debug('File: ' . $this->file, 'nabaztag');
  317. Logger::debug("File after save " . file_get_contents($this->file), 'nabaztag');
  318. }
  319. /**
  320. * Saves configuration
  321. *
  322. * @access public
  323. */
  324. public function saveConfiguration() {
  325. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  326. $content = serialize($this->configuration);
  327. $fh = fopen($this->configFile, 'w');
  328. fputs($fh, $content);
  329. fclose($fh);
  330. Logger::debug("File " . $this->configFile, 'nabaztag');
  331. Logger::debug("File after save " . file_get_contents($this->configFile), 'nabaztag');
  332. }
  333. /**
  334. * Destructor
  335. *
  336. * Last try to save
  337. *
  338. * @access public
  339. */
  340. public function __destruct() {
  341. Logger::debug($this->uid . ' - ' . __METHOD__, 'nabaztag');
  342. }
  343. }
  344. ?>