PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/explay/classes/Module/Module.php

http://explay-cms.googlecode.com/
PHP | 372 lines | 231 code | 62 blank | 79 comment | 88 complexity | 14d8d8a1a2056c6b4185640eae1b527b MD5 | raw file
  1. <?php
  2. /**
  3. * ??????????? ???????? ??? ???????? ???????
  4. */
  5. abstract class Module {
  6. protected $moduleName = '';
  7. protected static $baseMethods = array();
  8. protected static $subMethods = array ();
  9. protected static $subObjects = array ();
  10. protected function __construct ($moduleName) {
  11. $this->moduleName = $moduleName;
  12. self::$baseMethods = get_class_methods($moduleName);
  13. }
  14. final public function __clone () {
  15. throw new CoreException (lang('error_module_clone',__CLASS__));
  16. }
  17. public function __call ($method, $params) {
  18. if (!isset (self::$subMethods[$this->moduleName][$method])) {
  19. if (in_array($method, self::$baseMethods)) {
  20. $oBaseModuleClass = CMSController::getInstance()->getModule($this->moduleName);
  21. return call_user_func_array(array($oBaseModuleClass, $method), $params);
  22. }
  23. else {
  24. throw new NotFound;
  25. }
  26. }
  27. $className = self::$subMethods[$this->moduleName][$method];
  28. $oRefMethod = new ReflectionMethod ($className, $method);
  29. if (!$oRefMethod->isPublic() || $oRefMethod->isStatic()) {
  30. throw new NotFound;
  31. }
  32. /*$sParams = '';
  33. $cntParams = count ($params);
  34. for ($i = 0; $i < $cntParams; ++$i) {
  35. $sParams .= '$params[' . $i . ']';
  36. if ($i != $cntParams - 1) {
  37. $sParams .= ', ';
  38. }
  39. }*/
  40. return call_user_func_array(array(self::$subObjects[$this->moduleName][$className], $method), $params);
  41. //eval ('$response = self::$subObjects[$this->moduleName][\'' . $className . '\']->' . $method . ' (' . $sParams . ');');
  42. //return $response;
  43. }
  44. /**
  45. * ?????????? ???????? ???????? ??????, ??? ?????????? ???????? ?????
  46. * @param string $method
  47. * @return string
  48. */
  49. public function getMethodClassName ($method) {
  50. if (isset (self::$subMethods[$this->moduleName])) {
  51. if (isset (self::$subMethods[$this->moduleName][$method])) {
  52. return self::$subMethods[$this->moduleName][$method];
  53. }
  54. }
  55. if (method_exists ($this, $method)) {
  56. return $this->moduleName;
  57. }
  58. return false;
  59. //return isset (self::$subMethods[$this->moduleName][$method]) ? self::$subMethods[$this->moduleName][$method] : false;
  60. }
  61. /**
  62. * ?????????? ???????? ?????. ??? ?????????? ?????????? ?????? ? ???????? ?????? ?????? ????? ?????? ????? ????????????? ??????
  63. * @param string $sClassName ??? ??????, ???? ????? ?????? ?????? ????? ????? ?? ???????? ? ?????????? ? ????? ????? ? ??????? ???????
  64. * @return void
  65. */
  66. protected function extend ($className) {
  67. $file = ENGINE_ROOT . '/modules/' . $this->moduleName . '/' . $className . '.php';
  68. if (!file_exists ($file)) {
  69. trigger_error (lang('error_try_extend_not_exists_file',__CLASS__) . $file, E_USER_ERROR);
  70. }
  71. include $file;
  72. if (!class_exists ($className, false)) {
  73. trigger_error (lang('error_try_extend_class_not_exists',__CLASS__) . $className, E_USER_ERROR);
  74. }
  75. self::$subObjects[$this->moduleName][$className] = new $className;
  76. $methods = get_class_methods($className);
  77. foreach ($methods as $methodName) {
  78. self::$subMethods[$this->moduleName][$methodName] = $className;
  79. }
  80. }
  81. /**
  82. * ?????????, ?????????? ?? ????? ? ??????? ??????, ???????? ????????????? ????????????
  83. * @param string $sMethodName
  84. * @return boolean
  85. */
  86. public function isMethodExists ($sMethodName) {
  87. if (isset (self::$subMethods[$this->moduleName])) {
  88. if (isset (self::$subMethods[$this->moduleName][$sMethodName])) {
  89. return true;
  90. }
  91. }
  92. if (method_exists ($this, $sMethodName)) {
  93. return true;
  94. }
  95. /*if (isset (self::$subMethods[$this->moduleName])) {
  96. foreach (self::$subMethods[$this->moduleName] as $sSubName => $c) {
  97. if ($sMethodName == $sSubName) {
  98. return true;
  99. }
  100. }
  101. }*/
  102. return false;
  103. }
  104. /**
  105. * ?????????? ?????? ???????? ?????, ?????????? POST ????????, ??????????? ? ???????
  106. * @param integer $objectId id ???????
  107. * @param Type $type ???
  108. * @param boolean $bGetNotEditable ????????? ???????? ?????, ??????????? ??? ?????????????? ????? ?????
  109. * @return array ??????: ???? - ??? ????, ???????? - ????????
  110. */
  111. protected function getRequestData ($objectId, Type $type, $bGetNotEditable = false) {
  112. $aFields = $type->getFields ();
  113. $response = array ();
  114. foreach ($aFields as $fieldName => $oField) {
  115. if (!$oField->isReal () || (!$oField->isEditable () && !$bGetNotEditable)) {
  116. continue;
  117. }
  118. if ($oField->getCharacter () == 'boolean') {
  119. if (isset ($_POST['data'][$objectId][$fieldName])) {
  120. $response[$fieldName] = 1;
  121. } else {
  122. $response[$fieldName] = 0;
  123. }
  124. continue;
  125. }
  126. if (isset ($_POST['data'][$objectId][$fieldName])) {
  127. $character = $oField->getCharacter ();
  128. if ($character == 'guide' || $character == 'multiguide') {
  129. $response[$fieldName] = array ();
  130. if (is_array ($_POST['data'][$objectId][$fieldName])) {
  131. foreach ($_POST['data'][$objectId][$fieldName] as $value) {
  132. $value = (int) $value;
  133. if ($value > 0) {
  134. $response[$fieldName][] = $value;
  135. }
  136. }
  137. }
  138. }
  139. elseif ($character == 'tags') {
  140. $requestString = trim ((string) $_POST['data'][$objectId][$fieldName]);
  141. $aGarbage = explode (',', $requestString);
  142. $aGarbage = array_unique ($aGarbage);
  143. $response[$fieldName] = array ();
  144. foreach ($aGarbage as $value) {
  145. $value = trim ($value);
  146. if ($value != '') {
  147. $response[$fieldName][] = $value;
  148. }
  149. }
  150. }
  151. elseif ($character == 'birthdate' || $character == 'date') {
  152. $requestValue = $_POST['data'][$objectId][$fieldName];
  153. if ($requestValue == '') {
  154. $response[$fieldName] = 0;
  155. } else {
  156. $response[$fieldName] = strtotime ($_POST['data'][$objectId][$fieldName]);
  157. }
  158. } else {
  159. $response[$fieldName] = (string) $_POST['data'][$objectId][$fieldName];
  160. }
  161. } else {
  162. continue;
  163. }
  164. }
  165. return $response;
  166. }
  167. /**
  168. * ?????? ???????? ??????? ? ????????? ?????????
  169. * @param mixed $content ???????
  170. * @param string $contentType ??? ????????
  171. * @return void
  172. */
  173. protected function flush ($content, $contentType = 'text/html') {
  174. header ("Content-type: " . $contentType . "; charset=utf-8");
  175. print $content;
  176. exit;
  177. }
  178. /**
  179. * ????????????? ????????? ????????
  180. * @param string $sTitle
  181. * @return void
  182. */
  183. protected function setPageTitle ($sTitle) {
  184. if ($sTitle) {
  185. CMSController::setPageTitle ($sTitle);
  186. }
  187. }
  188. /**
  189. * ????????? ???????? ??????? ??????? ?? ???????????? ????? ????????????? ????
  190. * @param Object $object
  191. * @return array ?????? ?????? ?????, ??? ???? - ???????? ????
  192. */
  193. protected function getValidateErrors (Object $object) {
  194. $oType = $object->getType ();
  195. $aFields = $oType->getFields ();
  196. $aValues = $object->getValues ();
  197. $aErrors = array ();
  198. foreach ($aFields as $fieldName => $oField) {
  199. $character = $oField->getCharacter ();
  200. if ($oField->isRequired () && $oField->isReal ()) {
  201. // ???? ????????? ?????? ? ??????
  202. if ($character != 'text' && isset($aValues[$fieldName]) && !is_array($aValues[$fieldName])) {
  203. $aValues[$fieldName] = strip_tags($aValues[$fieldName]);
  204. }
  205. // ???????? ???????? ???????????. ???, ?? ??? ?????? ?? ????????? =\
  206. if ($character == 'image') {
  207. // ????? ??????
  208. if ($object->getId () == 0) {
  209. if (!isset ($aValues[$fieldName]) || $aValues[$fieldName] == '') {
  210. if ((isset ($_FILES['data']['tmp_name']['new'][$fieldName]) && $_FILES['data']['tmp_name']['new'][$fieldName] == '') || !isset ($_FILES['data']['tmp_name']['new'][$fieldName])) {
  211. $aErrors[$fieldName] = $oField->getErrorMessage ();
  212. continue;
  213. }
  214. }
  215. }
  216. // ?? ?????
  217. else {
  218. if (!isset ($aValues[$fieldName])) {
  219. $aErrors[$fieldName] = $oField->getErrorMessage ();
  220. continue;
  221. }
  222. }
  223. continue;
  224. }
  225. // ?????? ????????
  226. if (!isset ($aValues[$fieldName]) || $aValues[$fieldName] === '') {
  227. $aErrors[$fieldName] = $oField->getErrorMessage ();
  228. continue;
  229. }
  230. // ???-?? ??? ???????????
  231. if (($character == 'guide' || $character == 'multiguide') && (!is_array ($aValues[$fieldName]) || count ($aValues[$fieldName]) == 0)) {
  232. $aErrors[$fieldName] = $oField->getErrorMessage ();
  233. continue;
  234. }
  235. // ?? ??????????? ????
  236. if ($character == 'tags' && ($aValues[$fieldName] === '' || count ($aValues[$fieldName]) == 0)) {
  237. $aErrors[$fieldName] = $oField->getErrorMessage ();
  238. continue;
  239. }
  240. // ??????????? ?????
  241. if (is_numeric ($oField->getMaxlength ())) {
  242. if (mb_strlen ($aValues[$fieldName]) > (int) $oField->getMaxlength ()) {
  243. $aErrors[$fieldName] = lang ('validation_error_value_so_long',__CLASS__) . ' (' . $oField->getMaxlength() . ')';
  244. }
  245. }
  246. }
  247. $fieldValue = isset ($aValues[$fieldName]) ? $aValues[$fieldName] : '';
  248. if ($fieldValue == '') {
  249. continue;
  250. }
  251. // ???????? ????????? ????????
  252. switch ($character) {
  253. case 'integer' : {
  254. if (!is_numeric ($fieldValue)) {
  255. $aErrors[$fieldName] = lang ('validation_error_value_int',__CLASS__);
  256. }
  257. break;
  258. }
  259. case 'latin' : {
  260. if (!preg_match("#^[a-zA-Z0-9_-]+$#", $fieldValue)) {
  261. $aErrors[$fieldName] = lang ('validation_error_value_latin',__CLASS__);
  262. }
  263. break;
  264. }
  265. case 'email' : {
  266. if (!preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $fieldValue)) {
  267. $aErrors[$fieldName] = lang ('validation_error_value_email',__CLASS__);
  268. }
  269. break;
  270. }
  271. default : {
  272. continue;
  273. }
  274. }
  275. }
  276. return $aErrors;
  277. }
  278. /**
  279. * ????????? ??????? ? Selection, ?????????? ? URI
  280. * @param Type $oType ???, ???? ???????? ????? ???????? ? ????????
  281. * @param Selection $selection
  282. * @return void
  283. */
  284. protected function detectFilters (Type $oType, Selection $selection) {
  285. $aFields = $oType->getFields ();
  286. if (!isset ($_REQUEST['filter']) || !is_array ($_REQUEST['filter'])) {
  287. return;
  288. }
  289. $aFilters = $_REQUEST['filter'];
  290. foreach ($aFilters as $fieldName => $value) {
  291. if (!is_array ($value) && isset ($aFields[$fieldName]) && $aFields[$fieldName]->isSearch () && $aFields[$fieldName]->isReal ()) {
  292. $selection->addFilter ($fieldName, $value);
  293. }
  294. }
  295. }
  296. /**
  297. * ??????? ?????????? AccessException ??? ???????? GET-????????? scode
  298. * @return void
  299. */
  300. protected function expectSecureGET () {
  301. $requestValue = getRequest ('scode');
  302. if (SCODE != $requestValue) {
  303. throw new AccessException (lang('error_scode',__CLASS__));
  304. }
  305. }
  306. protected function setResponseFormat ($format) {
  307. PageParser::getInstance()->setCurrentFormat ($format);
  308. }
  309. }