Server.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Artificial Neural Network - Version 2.2
  4. *
  5. * For updates and changes visit the project page at http://ann.thwien.de/
  6. *
  7. *
  8. *
  9. * <b>LICENCE</b>
  10. *
  11. * The BSD 2-Clause License
  12. *
  13. * http://opensource.org/licenses/bsd-license.php
  14. *
  15. * Copyright (c) 2007 - 2012, Thomas Wien
  16. * All rights reserved.
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. *
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. *
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  32. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  33. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  34. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  35. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  36. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  37. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  39. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGE.
  41. *
  42. * @author Thomas Wien <info_at_thwien_dot_de>
  43. * @version ANN Version 2.2 by Thomas Wien
  44. * @copyright Copyright (c) 2007-2012 by Thomas Wien
  45. * @package ANN
  46. */
  47. namespace ANN;
  48. /**
  49. * @package ANN
  50. * @access public
  51. */
  52. class Server
  53. {
  54. /**#@+
  55. * @ignore
  56. */
  57. /**
  58. * @var boolean
  59. */
  60. protected $boolLogin = FALSE;
  61. /**
  62. * @var Network
  63. */
  64. protected $objNetwork = null;
  65. /**
  66. * @var string
  67. */
  68. protected $strNetworkSerialized = null;
  69. /**
  70. * @var string
  71. */
  72. protected $strDir = '';
  73. /**#@-*/
  74. /**
  75. * @param string $strDir (Default: 'networks')
  76. * @uses Exception::__construct()
  77. * @uses onPost()
  78. * @throws Exception
  79. */
  80. public function __construct($strDir = 'networks')
  81. {
  82. if(!is_dir($strDir) && is_writable($strDir))
  83. throw new Exception('Directory '. $strDir .' does not exists or has no writing permissions');
  84. $this->strDir = $strDir;
  85. if(isset($_POST) && count($_POST))
  86. $this->OnPost();
  87. }
  88. /**
  89. * @uses loadFromHost()
  90. * @uses checkLogin()
  91. * @uses saveToHost()
  92. * @uses trainByHost()
  93. */
  94. protected function onPost()
  95. {
  96. if(!isset($_POST['username']))
  97. $_POST['username'] = '';
  98. if(!isset($_POST['password']))
  99. $_POST['password'] = '';
  100. settype($_POST['username'], 'string');
  101. settype($_POST['password'], 'string');
  102. $this->boolLogin = $this->checkLogin($_POST['username'], $_POST['password']);
  103. if(!$this->boolLogin)
  104. return;
  105. if(isset($_POST['mode']))
  106. switch($_POST['mode'])
  107. {
  108. case 'savetohost':
  109. $this->strNetworkSerialized = $_POST['network'];
  110. $this->saveToHost();
  111. break;
  112. case 'loadfromhost':
  113. $this->loadFromHost();
  114. break;
  115. case 'trainbyhost':
  116. $this->strNetworkSerialized = $_POST['network'];
  117. $this->trainByHost();
  118. break;
  119. }
  120. }
  121. /**
  122. * @param string $strUsername
  123. * @param string $strPassword
  124. * @return boolean
  125. */
  126. protected function checkLogin($strUsername, $strPassword)
  127. {
  128. return TRUE;
  129. }
  130. /**
  131. * @uses Network::saveToFile()
  132. */
  133. protected function saveToHost()
  134. {
  135. $this->objNetwork = unserialize($this->strNetworkSerialized);
  136. if($this->objNetwork instanceof Network)
  137. $this->objNetwork->saveToFile($this->strDir .'/'. $_POST['username'] .'.dat');
  138. }
  139. /**
  140. * @uses Network::loadFromFile()
  141. */
  142. protected function loadFromHost()
  143. {
  144. $this->objNetwork = Network::loadFromFile($this->strDir .'/'. $_POST['username'] .'.dat');
  145. }
  146. /**
  147. * @uses Network::saveToFile()
  148. * @uses Network::train()
  149. * @uses saveToHost()
  150. */
  151. protected function trainByHost()
  152. {
  153. $this->saveToHost();
  154. if($this->objNetwork instanceof Network)
  155. {
  156. $this->objNetwork->saveToFile($this->strDir .'/'. $_POST['username'] .'.dat');
  157. $this->objNetwork->train();
  158. }
  159. }
  160. protected function printNetwork()
  161. {
  162. header('Content-Type: text/plain');
  163. print serialize($this->objNetwork);
  164. }
  165. /**
  166. * @uses printNetwork()
  167. */
  168. public function __destruct()
  169. {
  170. if(isset($_POST['mode']))
  171. switch($_POST['mode'])
  172. {
  173. case 'loadfromhost':
  174. case 'trainbyhost':
  175. $this->printNetwork();
  176. }
  177. }
  178. }