1: <?php
   2: 
   3:    4:    5:    6:    7:    8:    9:   10:   11:   12:   13:   14:   15: 
  16: 
  17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
  18: 
  19:   20:   21:   22:   23:   24:   25:   26:   27: 
  28: class cModuleHandler {
  29: 
  30:       31:   32:   33:   34: 
  35:     private $_modulePath;
  36: 
  37:       38:   39:   40:   41:   42: 
  43:     private $_path;
  44: 
  45:       46:   47:   48:   49: 
  50:     protected $_idmod = NULL;
  51: 
  52:       53:   54:   55:   56: 
  57:     private $_moduleName = NULL;
  58: 
  59:       60:   61:   62:   63: 
  64:     protected $_description;
  65: 
  66:       67:   68:   69:   70: 
  71:     protected $_type;
  72: 
  73:       74:   75:   76:   77: 
  78:     protected $_moduleAlias;
  79: 
  80:       81:   82:   83:   84: 
  85:     protected $_directories = array(
  86:         'css' => 'css/',
  87:         'js' => 'js/',
  88:         'template' => 'template/',
  89:         'image' => 'image/',
  90:         'lang' => 'lang/',
  91:         'php' => 'php/'
  92:     );
  93: 
  94:       95:   96:   97:   98: 
  99:     protected $_cfg = NULL;
 100: 
 101:      102:  103:  104:  105: 
 106:     protected $_cfgClient = NULL;
 107: 
 108:      109:  110:  111:  112: 
 113:     protected $_client = '0';
 114: 
 115:      116:  117:  118:  119: 
 120:     protected $_input = '';
 121: 
 122:      123:  124:  125:  126: 
 127:     protected $_output = '';
 128: 
 129:      130:  131:  132:  133: 
 134:     protected $_encoding = '';
 135: 
 136:      137:  138:  139:  140:  141:  142: 
 143:     protected $_fileEncoding = '';
 144: 
 145:      146:  147:  148:  149: 
 150:     protected $_idlang = -1;
 151: 
 152:      153:  154:  155: 
 156:     private $_db = NULL;
 157: 
 158:      159:  160:  161: 
 162:     protected static $_encodingStore = array();
 163: 
 164:      165:  166:  167:  168:  169:  170:  171:  172:  173:  174:  175:  176:  177: 
 178:     public function __construct($idmod = NULL) {
 179:         global $cfg, $cfgClient, $lang, $client;
 180:         $this->_cfg = $cfg;
 181:         $this->_client = $client;
 182:         $this->_cfgClient = $cfgClient;
 183:         $this->_idlang = $lang;
 184:         $this->_encoding = self::getEncoding();
 185:         $this->_fileEncoding = getEffectiveSetting('encoding', 'file_encoding', 'UTF-8');
 186: 
 187:         $this->_db = cRegistry::getDb();
 188: 
 189:         $this->_idmod = $idmod;
 190: 
 191:         $this->_initByModule($idmod);
 192: 
 193:         if ($this->_makeModuleDirectory() == false) {
 194:             throw new cException('Can not create main module directory.');
 195:         }
 196:     }
 197: 
 198:      199:  200:  201:  202:  203:  204: 
 205:     public static function getEncoding($overrideLanguageId = 0) {
 206:         $lang = cRegistry::getLanguageId();
 207: 
 208:         if ((int) $overrideLanguageId != 0) {
 209:             $lang = $overrideLanguageId;
 210:         }
 211: 
 212:         if ((int) $lang == 0) {
 213:             $clientId = cRegistry::getClientId();
 214: 
 215:             $clientsLangColl = new cApiClientLanguageCollection();
 216:             $clientLanguages = $clientsLangColl->getLanguagesByClient($clientId);
 217:             sort($clientLanguages);
 218: 
 219:             if (isset($clientLanguages[0]) && (int) $clientLanguages[0] != 0) {
 220:                 $lang = $clientLanguages[0];
 221:             }
 222:         }
 223: 
 224:         if (!isset(self::$_encodingStore[$lang])) {
 225:             $cApiLanguage = new cApiLanguage($lang);
 226:             self::$_encodingStore[$lang] = $cApiLanguage->get('encoding');
 227:         }
 228: 
 229:         return self::$_encodingStore[$lang];
 230:     }
 231: 
 232:      233:  234:  235:  236:  237: 
 238:     public function modulePathExistsInDirectory($name) {
 239:         return is_dir($this->_cfgClient[$this->_client]['module']['path'] . $name . '/');
 240:     }
 241: 
 242:      243:  244:  245:  246:  247:  248:  249:  250:  251:  252: 
 253:     public function saveContentToFile($templateName, $fileType, $fileContent, $saveDirectory = 'cache') {
 254:         $sSaveDirectory = $this->_cfgClient[$this->_client]['path']['frontend'] . $saveDirectory . '/';
 255:         if (!is_dir($sSaveDirectory)) {
 256:             return false;
 257:         }
 258: 
 259:         $templateName = str_replace(' ', '_', $templateName);
 260:         $templateName = cString::toLowerCase($templateName);
 261:         $fileOperation = cFileHandler::write($sSaveDirectory . $templateName . '.' . $fileType, $fileContent);
 262:         if ($fileOperation === false) {
 263:             return false;
 264:         }
 265:         $url = $this->_cfgClient[$this->_client]['path']['htmlpath'] . $saveDirectory . '/' . $templateName . '.' . $fileType;
 266: 
 267:         
 268:         $url = str_replace('http://', '//', $url);
 269: 
 270:         return $url;
 271:     }
 272: 
 273:      274:  275:  276:  277:  278:  279:  280:  281: 
 282:     public static function getCleanName($name, $defaultChar = '_') {
 283:         
 284:         $name = cString::cleanURLCharacters($name);
 285:         
 286:         $firstChar = cString::getPartOfString($name, 0, 1);
 287:         if (!preg_match('/^[a-zA-Z0-9]|_|-$/', $firstChar)) {
 288:             
 289:             $name = $defaultChar . cString::getPartOfString($name, 1);
 290:         }
 291: 
 292:         return $name;
 293:     }
 294: 
 295:      296:  297:  298:  299: 
 300:     public function initWithDatabaseRow($db) {
 301:         if (is_object($db)) {
 302:             $this->_initByModule($db->f('idmod'));
 303:         }
 304:     }
 305: 
 306:      307:  308:  309:  310:  311: 
 312:     protected function _initByModule($idmod = NULL) {
 313:         if ((int) $idmod == 0) {
 314:             return;
 315:         }
 316: 
 317:         $cApiModule = new cApiModule($idmod);
 318: 
 319:         if (true === $cApiModule->isLoaded()) {
 320:             $this->_idmod = $idmod;
 321:             $this->_client = $cApiModule->get('idclient');
 322:             $this->_description = $cApiModule->get('description');
 323:             $this->_type = $cApiModule->get('type');
 324:             $this->_input = '';
 325:             $this->_output = '';
 326: 
 327:             $this->_moduleAlias = $cApiModule->get('alias');
 328:             $this->_moduleName = $cApiModule->get('name');
 329:             $this->_path = $this->_cfgClient[$this->_client]['module']['path'];
 330:             $this->_modulePath = $this->_path . $this->_moduleAlias . '/';
 331:         }
 332:     }
 333: 
 334:      335:  336:  337:  338: 
 339:     public function getModuleName() {
 340:         return $this->_moduleName;
 341:     }
 342: 
 343:      344:  345:  346:  347: 
 348:     public function getModulePath() {
 349:         return $this->_modulePath;
 350:     }
 351: 
 352:      353:  354:  355:  356:  357:  358:  359: 
 360:     public function getTemplatePath($file = '') {
 361:         return $this->_modulePath . $this->_directories['template'] . $file;
 362:     }
 363: 
 364:      365:  366:  367:  368: 
 369:     public function getCssPath() {
 370:         return $this->_modulePath . $this->_directories['css'];
 371:     }
 372: 
 373:      374:  375:  376:  377: 
 378:     public function getPhpPath() {
 379:         return $this->_modulePath . $this->_directories['php'];
 380:     }
 381: 
 382:      383:  384:  385:  386: 
 387:     public function getJsPath() {
 388:         return $this->_modulePath . $this->_directories['js'];
 389:     }
 390: 
 391:      392:  393:  394:  395: 
 396:     public function getCssFileName() {
 397:         return $this->_moduleAlias . '.css';
 398:     }
 399: 
 400:      401:  402:  403:  404:  405:  406: 
 407:     protected function getRandomCharacters($count) {
 408:         $micro1 = microtime();
 409:         $rand1 = rand(0, time());
 410:         $rand2 = rand(0, time());
 411:         return cString::getPartOfString(md5($micro1 . $rand1 . $rand2), 0, $count);
 412:     }
 413: 
 414:      415:  416:  417:  418:  419:  420:  421:  422: 
 423:     public function existFile($type, $fileName) {
 424:         return cFileHandler::exists($this->_modulePath . $this->_directories[$type] . $fileName);
 425:     }
 426: 
 427:      428:  429:  430:  431:  432:  433:  434:  435: 
 436:     public function deleteFile($type, $fileName) {
 437:         if ($this->existFile($type, $fileName)) {
 438:             return unlink($this->_modulePath . $this->_directories[$type] . $fileName);
 439:         } else {
 440:             return false;
 441:         }
 442:     }
 443: 
 444:      445:  446:  447:  448:  449:  450:  451:  452:  453:  454:  455:  456: 
 457:     public function createModuleFile($type, $fileName = NULL, $content = '') {
 458:         
 459:         if (!$this->createModuleDirectory($type)) {
 460:             return false;
 461:         }
 462: 
 463:         
 464:         if ($fileName == NULL || $fileName == '') {
 465:             $fileName = $this->_moduleAlias;
 466: 
 467:             if ($type == 'template') {
 468:                 $fileName = $fileName . '.html';
 469:             } else {
 470:                 $fileName = $fileName . '.' . $type;
 471:             }
 472:         } else {
 473:             $fileName = cString::replaceDiacritics($fileName);
 474:         }
 475: 
 476:         
 477:         if ($type == 'css' || $type == 'js' || $type == 'template') {
 478:             if (!$this->existFile($type, $fileName)) {
 479:                 $content = cString::recodeString($content, $this->_encoding, $this->_fileEncoding);
 480:                 if (!$this->isWritable($this->_modulePath . $this->_directories[$type] . $fileName, $this->_modulePath . $this->_directories[$type])) {
 481:                     return false;
 482:                 }
 483: 
 484:                 if (cFileHandler::write($this->_modulePath . $this->_directories[$type] . $fileName, $content) === false) {
 485:                     $notification = new cGuiNotification();
 486:                     $notification->displayNotification('error', i18n("Can't make file: ") . $fileName);
 487:                     return false;
 488:                 }
 489:             } else {
 490:                 $content = cString::recodeString($content, $this->_encoding, $this->_fileEncoding);
 491:                 if (!$this->isWritable($this->_modulePath . $this->_directories[$type] . $fileName, $this->_modulePath . $this->_directories[$type])) {
 492:                     return false;
 493:                 }
 494:                 if (cFileHandler::write($this->_modulePath . $this->_directories[$type] . $fileName, $content) === false) {
 495:                     $notification = new cGuiNotification();
 496:                     $notification->displayNotification('error', i18n("Can't make file: ") . $fileName);
 497:                     return false;
 498:                 }
 499:             }
 500:         } else {
 501:             return false;
 502:         }
 503: 
 504:         return true;
 505:     }
 506: 
 507:      508:  509:  510:  511:  512:  513:  514:  515:  516:  517:  518: 
 519:     public function renameModuleFile($type, $oldFileName, $newFileName) {
 520: 
 521:         $newFileName = cString::replaceDiacritics($newFileName);
 522: 
 523:         if ($this->existFile($type, $newFileName)) {
 524:             return false;
 525:         }
 526: 
 527:         if (!$this->existFile($type, $oldFileName)) {
 528:             return false;
 529:         }
 530: 
 531:         return rename($this->_modulePath . $this->_directories[$type] . $oldFileName, $this->_modulePath . $this->_directories[$type] . $newFileName);
 532:     }
 533: 
 534:      535:  536:  537:  538:  539: 
 540:     public function getJsFileName() {
 541:         return $this->_moduleAlias . '.js';
 542:     }
 543: 
 544:      545:  546:  547:  548:  549:  550:  551:  552:  553:  554:  555: 
 556:     public function getFilesContent($directory, $fileTyp, $fileName = NULL) {
 557:         if ($fileName == NULL) {
 558:             $fileName = $this->_moduleAlias . '.' . $fileTyp;
 559:         }
 560: 
 561:         if ($this->existFile($directory, $fileName)) {
 562:             $content = cFileHandler::read($this->_modulePath . $this->_directories[$directory] . $fileName);
 563:             $content = iconv($this->_fileEncoding, $this->_encoding . '//IGNORE', $content);
 564:             return $content;
 565:         }
 566: 
 567:         return false;
 568:     }
 569: 
 570:      571:  572:  573:  574:  575:  576: 
 577:     protected function _makeModuleDirectory() {
 578:         
 579:         if ((int) $this->_client == 0) {
 580:             return true;
 581:         }
 582: 
 583:         $sMainModuleDirectory = $this->_cfgClient[$this->_client]['module']['path'];
 584: 
 585:         
 586:         if (!is_dir($sMainModuleDirectory) && $sMainModuleDirectory != NULL) {
 587:             if (mkdir($sMainModuleDirectory, cDirHandler::getDefaultPermissions(), true) == false) {
 588:                 return false;
 589:             } else {
 590:                 cDirHandler::setDefaultPermissions($sMainModuleDirectory);
 591:             }
 592:         }
 593: 
 594:         return true;
 595:     }
 596: 
 597:      598:  599:  600:  601:  602:  603: 
 604:     public function getAllFilesFromDirectory($moduleDirectory) {
 605:         $dir = $this->_modulePath . $this->_directories[$moduleDirectory];
 606:         return cDirHandler::read($dir);
 607:     }
 608: 
 609:      610:  611:  612:  613: 
 614:     public function changeModuleName($name) {
 615:         $this->_moduleAlias = $name;
 616:         $this->_modulePath = $this->_path . $this->_moduleAlias . '/';
 617:     }
 618: 
 619:      620:  621:  622:  623:  624:  625:  626: 
 627:     public function eraseModule() {
 628:         
 629:         if (cFileHandler::exists($this->_modulePath . 'info.xml')) {
 630:             return cDirHandler::recursiveRmdir($this->_modulePath);
 631:         } else {
 632:             return false;
 633:         }
 634:     }
 635: 
 636:      637:  638:  639:  640:  641:  642:  643: 
 644:     public function readInput($issource = false) {
 645:         if (cFileHandler::exists($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_input.php') == false) {
 646:             return false;
 647:         }
 648: 
 649:         $content = cFileHandler::read($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_input.php');
 650: 
 651:         if ($issource == true) {
 652:             $content = conHtmlentities($content);
 653:         }
 654: 
 655:         return iconv($this->_fileEncoding, $this->_encoding . '//IGNORE', $content);
 656:     }
 657: 
 658:      659:  660:  661:  662:  663:  664:  665: 
 666:     public function readOutput($issource = false) {
 667:         if (cFileHandler::exists($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_output.php') == false) {
 668:             return false;
 669:         }
 670: 
 671:         $content = cFileHandler::read($this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_output.php');
 672: 
 673:         if ($issource == true) {
 674:             $content = conHtmlentities($content);
 675:         }
 676: 
 677:         return iconv($this->_fileEncoding, $this->_encoding . '//IGNORE', $content);
 678:     }
 679: 
 680:      681:  682:  683:  684:  685:  686:  687:  688: 
 689:     protected function createModuleDirectory($type) {
 690:         if (array_key_exists($type, $this->_directories)) {
 691:             if (!is_dir($this->_modulePath . $this->_directories[$type])) {
 692:                 if (cDirHandler::create($this->_modulePath . $this->_directories[$type]) == false) {
 693:                     return false;
 694:                 } else
 695:                     cDirHandler::setDefaultPermissions($this->_modulePath . $this->_directories[$type]);
 696:             } else {
 697:                 return true;
 698:             }
 699:         }
 700: 
 701:         return true;
 702:     }
 703: 
 704:      705:  706:  707:  708:  709:  710:  711:  712:  713: 
 714:     public function isWritable($fileName, $directory) {
 715:         if (cFileHandler::exists($fileName)) {
 716:             if (!is_writable($fileName)) {
 717:                 return false;
 718:             }
 719:         } else {
 720:             if (!is_writable($directory)) {
 721:                 return false;
 722:             }
 723:         }
 724:         return true;
 725:     }
 726: 
 727:      728:  729:  730:  731:  732:  733: 
 734:     public function moduleWriteable($type) {
 735:         
 736:         if (true === cFileHandler::exists($this->_modulePath . $this->_directories[$type])) {
 737:             return cFileHandler::writeable($this->_modulePath . $this->_directories[$type]);
 738:         }
 739: 
 740:         
 741:         if (true === cFileHandler::exists($this->_modulePath)) {
 742:             return cFileHandler::writeable($this->_modulePath);
 743:         }
 744: 
 745:         return false;
 746:     }
 747: 
 748:      749:  750:  751:  752:  753:  754:  755: 
 756:     public function saveOutput($output = NULL) {
 757:         $fileName = $this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_output.php';
 758: 
 759:         if (!$this->createModuleDirectory('php') || !$this->isWritable($fileName, $this->_modulePath . $this->_directories['php'])) {
 760:             return false;
 761:         }
 762: 
 763:         if ($output == NULL) {
 764:             $output = $this->_output;
 765:         }
 766: 
 767:         $output = cString::recodeString($output, $this->_encoding, $this->_fileEncoding);
 768: 
 769:         $fileOperation = cFileHandler::write($fileName, $output);
 770: 
 771:         if ($fileOperation === false) {
 772:             return false; 
 773:         } else {
 774:             cFileHandler::setDefaultPermissions($fileName);
 775:             return true; 
 776:         }
 777:     }
 778: 
 779:      780:  781:  782:  783:  784:  785:  786: 
 787:     public function saveInput($input = NULL) {
 788:         $fileName = $this->_modulePath . $this->_directories['php'] . $this->_moduleAlias . '_input.php';
 789: 
 790:         if (!$this->createModuleDirectory('php') || !$this->isWritable($fileName, $this->_modulePath . $this->_directories['php'])) {
 791:             return false;
 792:         }
 793: 
 794:         if ($input == NULL) {
 795:             $input = $this->_input;
 796:         }
 797: 
 798:         $input = cString::recodeString($input, $this->_encoding, $this->_fileEncoding);
 799: 
 800:         $fileOperation = cFileHandler::write($fileName, $input);
 801: 
 802:         if ($fileOperation === false) {
 803:             return false; 
 804:         } else {
 805:             cFileHandler::setDefaultPermissions($fileName);
 806:             return true; 
 807:         }
 808:     }
 809: 
 810:      811:  812:  813:  814:  815:  816:  817:  818:  819:  820:  821:  822:  823:  824: 
 825:     public function saveInfoXML($moduleName = NULL, $description = NULL, $type = NULL, $alias = NULL) {
 826:         if ($moduleName === NULL) {
 827:             $moduleName = $this->_moduleName;
 828:         }
 829: 
 830:         if ($description === NULL) {
 831:             $description = $this->_description;
 832:         }
 833: 
 834:         if ($type === NULL) {
 835:             $type = $this->_type;
 836:         }
 837: 
 838:         if ($alias === NULL) {
 839:             $alias = $this->_moduleAlias;
 840:         }
 841: 
 842:         $oWriter = new cXmlWriter();
 843:         $oRootElement = $oWriter->addElement('module', '', NULL);
 844: 
 845:         $oWriter->addElement('name', conHtmlSpecialChars($moduleName), $oRootElement);
 846:         $oWriter->addElement('description', conHtmlSpecialChars($description), $oRootElement);
 847:         $oWriter->addElement('type', conHtmlSpecialChars($type), $oRootElement);
 848:         $oWriter->addElement('alias', conHtmlSpecialChars($alias), $oRootElement);
 849: 
 850:         return $oWriter->saveToFile($this->_modulePath, 'info.xml');
 851:     }
 852: 
 853:      854:  855:  856:  857:  858:  859:  860:  861:  862:  863:  864:  865:  866:  867: 
 868:     public function createModule($input = '', $output = '') {
 869:         if ($input != '') {
 870:             $this->_input = $input;
 871:         }
 872: 
 873:         if ($output != '') {
 874:             $this->_output = $output;
 875:         }
 876: 
 877:         if ($this->modulePathExists()) {
 878:             return true;
 879:         }
 880: 
 881:         if (mkdir($this->_modulePath) == false) {
 882:             return false;
 883:         } else {
 884:             cDirHandler::setDefaultPermissions($this->_modulePath);
 885:         }
 886: 
 887:         
 888:         foreach ($this->_directories as $directory) {
 889:             if (!is_dir($this->_modulePath . $directory)) {
 890:                 if (mkdir($this->_modulePath . $directory) == false) {
 891:                     return false;
 892:                 } else {
 893:                     cDirHandler::setDefaultPermissions($this->_modulePath . $directory);
 894:                 }
 895:             }
 896:         }
 897: 
 898:         
 899:         if ($this->saveInfoXML() == false) {
 900:             return false;
 901:         }
 902: 
 903:         
 904:         
 905:         $retInput = $this->saveInput();
 906:         $retOutput = $this->saveOutput();
 907: 
 908:         if ($retInput == false || $retOutput == false) {
 909:             return false;
 910:         }
 911: 
 912:         return true;
 913:     }
 914: 
 915:      916:  917:  918:  919:  920:  921:  922:  923:  924: 
 925:     public function renameModul($old, $new) {
 926:         
 927:         if (rename($this->_path . $old, $this->_path . $new) == false) {
 928:             return false;
 929:         } else {
 930:             $retInput = true;
 931:             $retOutput = true;
 932: 
 933:             
 934:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['php'] . $old . '_input.php'))
 935:                 $retInput = rename($this->_path . $new . '/' . $this->_directories['php'] . $old . '_input.php', $this->_path . $new . '/' . $this->_directories['php'] . $new . '_input.php');
 936: 
 937:             
 938:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['php'] . $old . '_output.php'))
 939:                 $retOutput = rename($this->_path . $new . '/' . $this->_directories['php'] . $old . '_output.php', $this->_path . $new . '/' . $this->_directories['php'] . $new . '_output.php');
 940: 
 941:             
 942:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['css'] . $old . '.css'))
 943:                 rename($this->_path . $new . '/' . $this->_directories['css'] . $old . '.css', $this->_path . $new . '/' . $this->_directories['css'] . $new . '.css');
 944: 
 945:             
 946:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['js'] . $old . '.js'))
 947:                 rename($this->_path . $new . '/' . $this->_directories['js'] . $old . '.js', $this->_path . $new . '/' . $this->_directories['js'] . $new . '.js');
 948: 
 949:             
 950:             if (cFileHandler::exists($this->_path . $new . '/' . $this->_directories['template'] . $old . '.html'))
 951:                 rename($this->_path . $new . '/' . $this->_directories['template'] . $old . '.html', $this->_path . $new . '/' . $this->_directories['template'] . $new . '.html');
 952: 
 953:             if ($retInput == true && $retOutput == true) {
 954:                 return true;
 955:             } else {
 956:                 return false;
 957:             }
 958:         }
 959:     }
 960: 
 961:      962:  963:  964:  965:  966: 
 967:     public function modulePathExists() {
 968:         return is_dir($this->_modulePath);
 969:     }
 970: 
 971:      972:  973:  974:  975:  976:  977:  978: 
 979:     public function testInput() {
 980: 
 981:         return $this->_testCode('input');
 982:     }
 983: 
 984:      985:  986:  987:  988:  989:  990:  991: 
 992:     public function testOutput() {
 993: 
 994:         return $this->_testCode('output');
 995:     }
 996: 
 997:      998:  999: 1000: 1001: 1002: 1003: 1004: 1005: 1006: 
1007:     protected function _testCode($inputType) {
1008: 
1009:         $result = array(
1010:             'state' => false,
1011:             'errorMessage' => 'Module path not exist'
1012:         );
1013: 
1014:         if (!$this->modulePathExists()) return $result;
1015: 
1016:         $module  = new cApiModule($this->_idmod);
1017:         $isError = 'none';
1018: 
1019:         
1020:         switch ($module->get("error")) {
1021:             case 'none';
1022:                 $toCheck = $inputType;
1023:                 break;
1024:             case 'input';
1025:                 if ($inputType == 'output') $toCheck = 'both';
1026:                 break;
1027:             case 'output';
1028:                 if ($inputType == 'input') $toCheck = 'both';
1029:                 break;
1030:             case 'both';
1031:                 break;
1032:         }
1033:         if ($toCheck !== $module->get("error")) {
1034:             $module->set("error", $toCheck);
1035: 
1036:             
1037:             
1038:             $module->store(true);
1039:         }
1040: 
1041:         
1042:         switch($inputType) {
1043:             case 'input':
1044:                 $code       = $this->readInput();
1045:                 $result = $this->_verifyCode($code, $this->_idmod . "i");
1046:                 if ($result['state'] !== true) $isError = 'input';
1047:                 break;
1048:             case 'output':
1049:                 $code       = $this->readOutput();
1050:                 $result = $this->_verifyCode($code, $this->_idmod . "o", true);
1051:                 if ($result['state'] !== true) $isError = 'output';
1052:                 break;
1053:         }
1054: 
1055:         
1056:         switch ($module->get("error")) {
1057:             case 'none';
1058:                 break;
1059:             case 'input';
1060:                 if ($isError == 'none' && $inputType == 'output') $isError = 'input';
1061:                 if ($isError == 'output') $isError = 'both';
1062:                 break;
1063:             case 'output';
1064:                 if ($isError == 'none' && $inputType == 'input') $isError = 'output';
1065:                 if ($isError == 'input') $isError = 'both';
1066:                 break;
1067:             case 'both';
1068:                 if ($isError == 'none' && $inputType == 'input') $isError = 'output';
1069:                 if ($isError == 'none' && $inputType == 'output') $isError = 'input';
1070:                 break;
1071:         }
1072: 
1073:         
1074:         
1075:         if ($isError !== $module->get("error")) {
1076:             $module->set("error", $isError);
1077: 
1078:             
1079:             
1080:             $module->store(true);
1081:         }
1082: 
1083:         return $result;
1084:     }
1085: 
1086:     1087: 1088: 1089: 1090: 1091: 1092: 1093: 1094: 1095: 1096: 1097: 1098: 
1099:     protected function _verifyCode($code, $id, $output = false) {
1100:         $isError = false;
1101:         $result = array(
1102:             'state' => false,
1103:             'errorMessage' => NULL
1104:         );
1105: 
1106:         
1107:         $sql = 'SELECT type FROM ' . $this->_cfg['tab']['type'];
1108:         $this->_db->query($sql);
1109:         while ($this->_db->nextRecord()) {
1110:             $code = str_replace($this->_db->f('type') . '[', '$' . $this->_db->f('type') . '[', $code);
1111:         }
1112: 
1113:         $code = preg_replace(',\[(\d+)?CMS_VALUE\[(\d+)\](\d+)?\],i', '[\1\2\3]', $code);
1114:         $code = str_replace('CMS_VALUE', '$CMS_VALUE', $code);
1115:         $code = str_replace('CMS_VAR', '$CMS_VAR', $code);
1116: 
1117:         
1118:         
1119:         if ($output === true) {
1120:             $code = "?>\n" . $code . "\n<?php";
1121:         }
1122: 
1123:         
1124:         $code = 'function foo' . $id . ' () {' . $code;
1125:         $code .= "\n}\n";
1126: 
1127:         $html_errors = ini_get('html_errors');
1128: 
1129:         
1130:         
1131:         $sErs = ini_get('error_prepend_string'); 
1132:         $sEas = ini_get('error_append_string'); 
1133:         @ini_set('error_prepend_string', '<phperror>');
1134:         @ini_set('error_append_string', '</phperror>');
1135: 
1136:         
1137:         ob_start();
1138:         $display_errors = ini_get('display_errors');
1139:         @ini_set('html_errors', false);
1140:         @ini_set('display_errors', true);
1141:         $output = eval($code);
1142:         @ini_set('display_errors', $display_errors);
1143: 
1144:         
1145:         $output = ob_get_contents();
1146:         ob_end_clean();
1147: 
1148:         
1149:         @ini_set('html_errors', $html_errors);
1150: 
1151:         
1152:         @ini_set('error_prepend_string', $sErs); 
1153:         @ini_set('error_append_string', $sEas); 
1154: 
1155:         
1156:         if ($isError === false) {
1157:             $isError = cString::findFirstPos($output, '<phperror>');
1158:         }
1159: 
1160:         
1161:         
1162:         if ($isError !== false) {
1163:             if (isset($modErrorMessage) === false) {
1164:                 $pattern         = '/(<phperror>|<\/phperror>|<b>|<\/b>|<br>|<br \/>)/im';
1165:                 $modErrorMessage = trim(preg_replace($pattern, '', $output));
1166:                 $errorPart1      = cString::getPartOfString($modErrorMessage, 0, cString::findFirstPos($modErrorMessage, ' in '));
1167:                 $errorPart2      = cString::getPartOfString($modErrorMessage, cString::findFirstPos($modErrorMessage, ' on line '));
1168:                 $modErrorMessage = $errorPart1 . $errorPart2;
1169:             }
1170:             $result['errorMessage'] = sprintf(i18n("Error in module. Error location: %s"), $modErrorMessage);
1171: 
1172:         }
1173: 
1174:         
1175:         $bHasShortTags = false;
1176:         if (preg_match('/<\?\s+/', $code)) {
1177:             $bHasShortTags = true;
1178:             $result['errorMessage'] = i18n('Please do not use short open tags. (Use <?php instead of <?).');
1179:         }
1180: 
1181:         
1182:         if ($bHasShortTags || $isError !== false) {
1183:             $result['state'] = false;
1184:         } else {
1185:             $result['state'] = true;
1186:         }
1187: 
1188:         return $result;
1189: 
1190:     }
1191: }
1192: