PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/published/wbsadmin/html/scripts/includes/functions.php

https://github.com/foxluck/otdelstroy
PHP | 502 lines | 491 code | 4 blank | 7 comment | 1 complexity | 22782f2e942fed8834765d6060d97ed5 MD5 | raw file
  1. <?php
  2. /*****************************************************************************
  3. * *
  4. * Shop-Script PREMIUM *
  5. * Copyright (c) 2005 WebAsyst LLC. All rights reserved. *
  6. * *
  7. *****************************************************************************/
  8. ?><?php
  9. //frequently used functions
  10. define( 'STRING_PRODUCT_NAME', 'PREMIUM' );
  11. define( 'STRING_VERSION', '1.26' );
  12. define( 'STRING_COULDNT_REWRITE_FILE', 'cfg....' );
  13. function CallInstallfunctions()
  14. {
  15. ostInstall();
  16. catInstall();
  17. settingInstall();
  18. verInstall();
  19. }
  20. function CreateTablesIncFile($TablesIncFfileName, $XmlFileName)
  21. {
  22. $f = fopen($TablesIncFfileName,"w");
  23. $xmlTables=new XmlNode();
  24. $xmlTables->LoadInnerXmlFromFile( $XmlFileName );
  25. $array=$xmlTables->SelectNodes( "DataBaseStructure/tables/table" );
  26. fputs( $f, "<?php\n");
  27. fputs( $f, "\n");
  28. foreach( $array as $xmlTable )
  29. {
  30. $attrubtes = $xmlTable->GetXmlNodeAttributes();
  31. fputs( $f, "if ( !defined('".$attrubtes["ALIAS"]."') ) \n" );
  32. fputs( $f, "{\n" );
  33. $s = " define('".$attrubtes["ALIAS"]."', '".$attrubtes["NAME"]."');";
  34. fputs( $f, $s."\n" );
  35. fputs( $f, "\n" );
  36. fputs( $f, "}\n" );
  37. }
  38. fputs( $f, "?>" );
  39. fclose( $f);
  40. }
  41. function myfile_get_contents( $fileName )
  42. {
  43. return implode( "", file($fileName) );
  44. }
  45. function TransformStringToDataBase( $str )
  46. {
  47. if (is_array($str))
  48. {
  49. foreach ($str as $key => $val)
  50. {
  51. $str[$key] = stripslashes($val);
  52. }
  53. $str = str_replace("\\","\\\\",$str);
  54. }
  55. else
  56. {
  57. $str = str_replace("\\","\\\\",stripslashes($str));
  58. }
  59. return str_replace( "'", "''", $str );
  60. }
  61. function regGetIdByLogin( $login )
  62. {
  63. $q = ss_db_query("select customerID from ".CUSTOMERS_TABLE.
  64. " where Login='".xEscapeSQLstring($login)."'");
  65. if ( ($r=db_fetch_row($q)) )
  66. return $r["customerID"];
  67. else
  68. return NULL;
  69. }
  70. function get_current_time() // gets current date and time as a string in MySQL format
  71. {
  72. return strftime("%Y-%m-%d %H:%M:%S", time());
  73. }
  74. function catInstall()
  75. {
  76. ss_db_query("insert into ".CATEGORIES_TABLE."( name, parent, categoryID )".
  77. "values( 'ROOT', NULL, 1 )");
  78. }
  79. // *****************************************************************************
  80. // Purpose
  81. // Inputs
  82. // Remarks
  83. // Returns
  84. function auxpgAddAuxPage( $aux_page_name,
  85. $aux_page_text, $aux_page_text_type,
  86. $meta_keywords, $meta_description )
  87. {
  88. $aux_page_name = TransformStringToDataBase( $aux_page_name );
  89. $meta_keywords = TransformStringToDataBase( $meta_keywords );
  90. $meta_description = TransformStringToDataBase( $meta_description );
  91. $aux_page_text = TransformStringToDataBase( $aux_page_text );
  92. ss_db_query( "insert into ".AUX_PAGES_TABLE.
  93. " ( aux_page_name, aux_page_text, aux_page_text_type, meta_keywords, meta_description ) ".
  94. " values( '$aux_page_name', '$aux_page_text', $aux_page_text_type, ".
  95. " '$meta_keywords', '$meta_description' ) " );
  96. }
  97. /*
  98. function ostInstall()
  99. {
  100. ss_db_query("insert into ".ORDER_STATUES_TABLE.
  101. " ( status_name, sort_order ) ".
  102. " values( 'STRING_CANCELED_ORDER_STATUS', 0 ) ");
  103. }
  104. */
  105. function verInstall()
  106. {
  107. ss_db_query("insert into ".SYSTEM_TABLE.
  108. " ( varName, value ) ".
  109. " values( 'version_number', '".STRING_VERSION."' ) ");
  110. ss_db_query("insert into ".SYSTEM_TABLE.
  111. " ( varName, value ) ".
  112. " values( 'version_name', '".STRING_PRODUCT_NAME."' ) ");
  113. }
  114. function ostInstall()
  115. {
  116. ss_db_query("insert into ".ORDER_STATUES_TABLE.
  117. " ( status_name, sort_order ) ".
  118. " values( 'STRING_CANCELED_ORDER_STATUS', 0 ) ");
  119. }
  120. function settingInstall()
  121. {
  122. ss_db_query("insert into ".SETTINGS_GROUPS_TABLE.
  123. " ( settings_groupID, settings_group_name, sort_order ) ".
  124. " values( ".settingGetFreeGroupId().", 'MODULES', 0 ) " );
  125. }
  126. function settingGetFreeGroupId()
  127. {
  128. return 1;
  129. }
  130. function verGetPackageVersion()
  131. {
  132. $q = ss_db_query("select varName, value from ".SYSTEM_TABLE);
  133. $row = array("");
  134. while ( $row && strcmp($row[0], "version_number") )
  135. {
  136. $row = db_fetch_row($q);
  137. }
  138. return (float) $row[1];
  139. }
  140. function verUpdatePackageVersion()
  141. {
  142. ss_db_query("update ".SYSTEM_TABLE." set value = '".STRING_VERSION."' where varName = 'version_number'");
  143. }
  144. function verUpdatePackageName(){
  145. ss_db_query("update ".SYSTEM_TABLE." set value = '".STRING_PRODUCT_NAME."' where varName = 'version_name'");
  146. }
  147. //functions from xml_installer.php
  148. function GetXmlTableNodeArray( $fileName )
  149. {
  150. $xmlTables = new XmlNode();
  151. $xmlTables->LoadInnerXmlFromFile( $fileName );
  152. $array = $xmlTables->SelectNodes( "DataBaseStructure/tables/table" );
  153. return $array;
  154. }
  155. function GetCreateTableSQL($xmlTable)
  156. {
  157. $attributes=$xmlTable->GetXmlNodeAttributes();
  158. $sql = "CREATE TABLE ".trim($attributes["NAME"])." (";
  159. $array=$xmlTable->SelectNodes("table/column");
  160. $_indexes = GetIndexesSQL($array);
  161. if($_indexes) $sql .= $_indexes.',';
  162. $firstFlag=true;
  163. $isComplexPrimaryKey = IsComplexPrimaryKey($array);
  164. foreach($array as $xmlColumn)
  165. {
  166. $columnSql=GetColumnSQL($xmlColumn, $isComplexPrimaryKey);
  167. if ( is_bool($columnSql) )
  168. return false;
  169. if ( $firstFlag )
  170. $sql .= GetColumnSQL($xmlColumn, $isComplexPrimaryKey);
  171. else
  172. $sql .= ", ".GetColumnSQL($xmlColumn, $isComplexPrimaryKey);
  173. $firstFlag = false;
  174. }
  175. if ( $isComplexPrimaryKey )
  176. $sql .= ", ".GetComplexPrimaryKeySQL($array);
  177. $sql .= ")";
  178. //$sql .= " TYPE=InnoDB";
  179. return $sql;
  180. }
  181. /**
  182. * Return indexes sql-injection
  183. *
  184. * @param array $array - columns
  185. * @return string - sql-injection
  186. */
  187. function GetIndexesSQL($array){
  188. $sql = array();
  189. foreach($array as $xmlColumn)
  190. {
  191. $attributes=$xmlColumn->GetXmlNodeAttributes();
  192. foreach($attributes as $key => $value)
  193. {
  194. if ( $key == "INDEX" )
  195. {
  196. $value = strtoupper($value);
  197. $columnName = trim($xmlColumn->GetXmlNodeData());
  198. $sql[] = '
  199. KEY '.$value.' (`'.$columnName.'`)';
  200. break;
  201. }
  202. }
  203. }
  204. return implode(',', $sql);
  205. }
  206. // Purpose determine complex primary key fact
  207. // Inputs array of column node
  208. // Remarks
  209. // Returns true if primary key is complex false otherwise
  210. function IsComplexPrimaryKey($array)
  211. {
  212. $primaryKeyCountPart = 0;
  213. foreach($array as $xmlColumn)
  214. {
  215. $attributes=$xmlColumn->GetXmlNodeAttributes();
  216. foreach($attributes as $key => $value)
  217. {
  218. if ( $key == "PRIMARYKEY" )
  219. {
  220. $primaryKeyCountPart++;
  221. break;
  222. }
  223. }
  224. }
  225. return ( $primaryKeyCountPart > 1 );
  226. }
  227. // Purpose parses column node
  228. // Inputs column node ( that is XmlNode object )
  229. // Remarks
  230. // Returns SQL column clause
  231. function GetColumnSQL($xmlColumn, $isComplexPrimaryKey)
  232. {
  233. $attributes=$xmlColumn->GetXmlNodeAttributes();
  234. $type = "";
  235. $nullable = true;
  236. $defaultValue = false;
  237. $primaryKey = false;
  238. $identity = false;
  239. foreach($attributes as $key => $value)
  240. {
  241. $value = strtoupper($value);
  242. switch( $key )
  243. {
  244. case "TYPE" :
  245. if ( _verifyVarChar($value) )
  246. $type = GetTypeColumnSQL( $value );
  247. else if ( _verifyChar($value) )
  248. $type = GetTypeColumnSQL( $value );
  249. else if (
  250. $value == "BIT" || $value == "INT" ||
  251. $value == "DATETIME" || $value == "FLOAT" ||
  252. $value == "TEXT" || $value == 'DATE'
  253. )
  254. $type = GetTypeColumnSQL( $value );
  255. else
  256. {
  257. echo( "Unknown datatype ".$value );
  258. return false;
  259. }
  260. break;
  261. case "NULLABLE" :
  262. if ( $value=="TRUE" )
  263. $nullable = true;
  264. else if ( $value=="FALSE" )
  265. $nullable = false;
  266. else
  267. {
  268. echo( "Invalid 'NULLABLE' attribute value '".$value."'" );
  269. return false;
  270. }
  271. break;
  272. case "DEFAULT" :
  273. $defaultValue = $value;
  274. break;
  275. case "PRIMARYKEY" :
  276. $primaryKey = true;
  277. break;
  278. case "IDENTITY" :
  279. $identity = true;
  280. break;
  281. case "INDEX":
  282. break;
  283. default :
  284. echo( "Unknown attribute '".$key."'" );
  285. return false;
  286. }
  287. }
  288. $columnName = trim($xmlColumn->GetXmlNodeData());
  289. return GetColumnMYSQL($columnName, $type,
  290. $nullable, $primaryKey, $identity, $defaultValue, $isComplexPrimaryKey);
  291. }
  292. function _verifyVarChar($value)
  293. {
  294. if ( strstr( $value, "VARCHAR" ) )
  295. {
  296. $val=str_replace( "VARCHAR", "", $value);
  297. $val=trim($val);
  298. if ( $val[0] == '(' && $val[ strlen($val) - 1 ] == ')' )
  299. {
  300. $val = str_replace( "(", "", $val );
  301. $val = str_replace( ")", "", $val );
  302. $val = (int)$val;
  303. return !( $val == 0 );
  304. }
  305. return false;
  306. }
  307. }
  308. function _verifyChar($value)
  309. {
  310. if ( strstr( $value, "CHAR" ) )
  311. {
  312. $val=str_replace( "CHAR", "", $value);
  313. $val=trim($val);
  314. if ( $val[0] == '(' && $val[ strlen($val) - 1 ] == ')' )
  315. {
  316. $val = str_replace( "(", "", $val );
  317. $val = str_replace( ")", "", $val );
  318. $val = (int)$val;
  319. return !( $val == 0 );
  320. }
  321. return false;
  322. }
  323. }
  324. // Inputs column node ( that is XmlNode object )
  325. // Remarks
  326. // Returns SQL column clause
  327. function GetTypeColumnSQL($type)
  328. {
  329. if ( strstr( $type, "VARCHAR" ) )
  330. return $type;
  331. else if ( strstr( $type, "CHAR" ) )
  332. return $type;
  333. else{
  334. return $type;
  335. }
  336. }
  337. // *****************************************************************************
  338. // Purpose gets column clause for MYSQL DBMS
  339. // Inputs
  340. // $columnName - column name (string)
  341. // $type - data type (string)
  342. // $nullable - true if column is nullable (bool)
  343. // $primaryKey - true if column is primary key (bool)
  344. // $identity - true if column is identity (bool)
  345. // $defaultValue - false if column does not have default value
  346. // $isComplexPrimaryKey - true if primary key is complex (bool)
  347. // Remarks
  348. // Returns SQL column clause
  349. function GetColumnMYSQL($columnName, $type,
  350. $nullable, $primaryKey, $identity, $defaultValue, $isComplexPrimaryKey)
  351. {
  352. $sql = "";
  353. if ( $nullable )
  354. $nullableStr = "NULL";
  355. else
  356. $nullableStr = "NOT NULL";
  357. if ( $identity )
  358. $identityStr = "AUTO_INCREMENT";
  359. else
  360. $identityStr = "";
  361. $defaultValueClause = GetDefaultValueClause($type, $defaultValue);
  362. if ( $primaryKey && !$isComplexPrimaryKey )
  363. $sql .= $columnName." ".$type." PRIMARY KEY ".$identityStr;
  364. else if ( $primaryKey && $isComplexPrimaryKey )
  365. $sql .= $columnName." ".$type." NOT NULL ".$identityStr;
  366. else
  367. $sql .= $columnName." ".$type." ".$nullableStr." ".$identityStr." ".$defaultValueClause;
  368. return $sql;
  369. }
  370. // *****************************************************************************
  371. // Purpose gets default value clause
  372. // Inputs
  373. // $type - data type (string)
  374. // $defaultValue - false if column does not have default value
  375. // Remarks
  376. // Returns
  377. function GetDefaultValueClause($type, $defaultValue)
  378. {
  379. if ( is_bool($defaultValue) )
  380. return "";
  381. $defaultClauseOpen = "DEFAULT ";
  382. $defaultClauseClose = "";
  383. print ( strstr("VARCHAR",strtoupper($type)) );
  384. if ( strstr("VARCHAR",strtoupper($type)) )
  385. return $defaultClauseOpen."'".$defaultValue."'".$defaultClauseClose;
  386. else
  387. return $defaultClauseOpen.$defaultValue.$defaultClauseClose;
  388. }
  389. // *****************************************************************************
  390. // Purpose gets primary key clause for complex key
  391. // Inputs $array is array of column node
  392. // Remarks
  393. // Returns
  394. function GetComplexPrimaryKeySQL($array)
  395. {
  396. $columns = "";
  397. $firstFlag = true;
  398. foreach($array as $xmlColumn)
  399. {
  400. $attributes=$xmlColumn->GetXmlNodeAttributes();
  401. foreach($attributes as $key => $value)
  402. {
  403. if ( $key == "PRIMARYKEY" )
  404. {
  405. if ( $firstFlag )
  406. {
  407. $columns .= $xmlColumn->GetXmlNodeData();
  408. $firstFlag = false;
  409. }
  410. else
  411. $columns .= ", ".$xmlColumn->GetXmlNodeData();
  412. break;
  413. }
  414. }
  415. }
  416. return "PRIMARY KEY (".$columns.")";
  417. }
  418. // *****************************************************************************
  419. // Purpose creates refer constraints corresponded to structure database XML file
  420. // Inputs file name
  421. // Remarks
  422. // Returns SQL script to be shown
  423. function CreateReferConstraintsXML($fileName)
  424. {
  425. $_SESSION["ForeignKeyIndex"] = 0;
  426. $xmlTables=new XmlNode();
  427. $xmlTables->LoadInnerXmlFromFile($fileName);
  428. $array=$xmlTables->SelectNodes("DataBaseStructure/tables/table");
  429. $sqlToShow = "<table>";
  430. // adds "alter table " SQL statements into $sqlToShow
  431. foreach($array as $xmlTable)
  432. {
  433. $sqlArray = GetReferConstraint($xmlTable);
  434. foreach( $sqlArray as $constraintSql )
  435. {
  436. if ( $constraintSql != "" )
  437. {
  438. ss_db_query( $constraintSql );
  439. $sqlToShow .= "<tr><td>".$constraintSql."</td></tr>";
  440. }
  441. }
  442. }
  443. unset( $_SESSION["ForeignKeyIndex"] );
  444. $sqlToShow .= "</table>";
  445. return $sqlToShow;
  446. }
  447. function GetReferConstraint($xmlTable)
  448. {
  449. return array();
  450. }
  451. //functions from settings_functions.php
  452. function _setSettingOptionValue( $settings_constant_name, $value )
  453. {
  454. $value = xEscapeSQLstring( $value );
  455. ss_db_query("update ".SETTINGS_TABLE." set settings_value='$value' ".
  456. " where settings_constant_name='$settings_constant_name'" );
  457. }
  458. ?>