PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/install/installer/assets/class.masks.php

http://section-cms.googlecode.com/
PHP | 229 lines | 129 code | 33 blank | 67 comment | 23 complexity | bcce0bb33a93660fd217479c70dcbe0e MD5 | raw file
  1. <?php if(!defined('INST_BASEDIR')) die('Direct access is not allowed!');
  2. /* ====================================================================
  3. *
  4. * PHP Setup Wizard
  5. *
  6. * -= MASK READ/WRITER =-
  7. *
  8. * ================================================================= */
  9. /**
  10. * Manipulates mask files
  11. */
  12. class Inst_Masks
  13. {
  14. private $words;
  15. /**
  16. * Manipulates mask files
  17. */
  18. function Inst_Masks()
  19. {
  20. $this->SetKeywords();
  21. }
  22. /********************************[ CORE FUNCTIONS ]********************************/
  23. /**
  24. * Set the keywords that will be replaced in the masks
  25. */
  26. function SetKeywords()
  27. {
  28. global $keywords;
  29. // Merge the keyword arrays together, if there is collision in the arrays the
  30. // connection array will be dominant - meaning that it will override the
  31. // keys in special and admin arrays
  32. $this->words = array();
  33. $this->words = array_merge($keywords['special'], $keywords['connection']);
  34. $this->words = array_merge($keywords['admin'], $this->words);
  35. }
  36. /**
  37. * Replace keywords in a mask content
  38. */
  39. function ReplaceKeywords($maskContent)
  40. {
  41. global $keywords;
  42. // Skip replace if no string to work with
  43. if(!$maskContent || strlen($maskContent) == 0)
  44. return $maskContent;
  45. // Do the replacement
  46. foreach($this->words as $keyword=>$value)
  47. {
  48. $keyword = $keywords['open_bracket'].$keyword.$keywords['close_bracket'];
  49. $maskContent = str_replace($keyword, $value, $maskContent);
  50. }
  51. return $maskContent;
  52. }
  53. /**
  54. * Get how many keywords are replaced in some mask content
  55. */
  56. function GetReplaceKeywordCount($maskContent)
  57. {
  58. global $keywords;
  59. // Skip replace if no string to work with
  60. if(!$maskContent || strlen($maskContent) == 0)
  61. return $maskContent;
  62. // Do the replacement
  63. $count = array();
  64. foreach($this->words as $keyword=>$value)
  65. {
  66. $searchWord = $keywords['open_bracket'].$keyword.$keywords['close_bracket'];
  67. $wordCount = substr_count($maskContent, $searchWord);
  68. if($wordCount > 0)
  69. $count[$keyword] = $wordCount;
  70. }
  71. return $count;
  72. }
  73. /**
  74. * Check if the mask file exists and is readable
  75. */
  76. function DoesMaskExistAndIsReadable($maskname)
  77. {
  78. global $config;
  79. $file = INST_RUNFOLDER.$config['mask_folder_name'].DIRECTORY_SEPARATOR.$maskname;
  80. return (is_file($file) && is_readable($file)) ? true : false;
  81. }
  82. /**
  83. * Get a mask content with the keywords replaced
  84. */
  85. function GetMask($maskname, $replaceKeywords=true)
  86. {
  87. global $config;
  88. $file = INST_RUNFOLDER.$config['mask_folder_name'].DIRECTORY_SEPARATOR.$maskname;
  89. if(is_file($file) && is_readable($file))
  90. {
  91. $content = file_get_contents($file);
  92. if($replaceKeywords)
  93. return $this->ReplaceKeywords($content);
  94. else return $content;
  95. }
  96. else
  97. return false;
  98. }
  99. function GetMaskExtension($maskname, $getInLowercase=false)
  100. {
  101. // Get the file extension (+1 to skip the dot)
  102. if(strrpos($maskname, '.') != false)
  103. {
  104. $ext = substr($maskname, (strrpos($maskname, '.') + 1 ));
  105. if($getInLowercase)
  106. return strtolower($ext);
  107. else return $ext;
  108. }
  109. else
  110. return false;
  111. }
  112. /**
  113. * How many times does the SQL separator occur in some mask content
  114. */
  115. function GetSqlSeparatorCount($maskContent)
  116. {
  117. global $keywords;
  118. return substr_count($maskContent, $keywords['next_query']);
  119. }
  120. /**
  121. * Should the "next query" separator be filtered out of
  122. * the SQL query block (or some string for that matter)
  123. */
  124. function FilterSqlSeparator($maskContent, $reduceNewlines=true)
  125. {
  126. global $keywords;
  127. $maskContent = str_replace($keywords['next_query'], "", $maskContent);
  128. if($reduceNewlines)
  129. {
  130. $maskContent = str_replace("\r", "", $maskContent);
  131. $maskContent = str_replace("\n\n", "\n", $maskContent);
  132. }
  133. return $maskContent;
  134. }
  135. /********************************[ GET SPESIFIC MASKS ]********************************/
  136. /**
  137. * Get the welcome message with keywords replaced
  138. */
  139. function GetWelcomeMessage()
  140. {
  141. global $steps;
  142. return $this->GetMask($steps[STEP_WELCOME]['maskname']);
  143. }
  144. /**
  145. * Get the Terms-Of-Agreement with keywords replaced
  146. */
  147. function GetTermsOfAgreement()
  148. {
  149. global $steps;
  150. return $this->GetMask($steps[STEP_TERMSOFUSE]['maskname']);
  151. }
  152. /**
  153. * Get the SQL queries with keywords replaced
  154. */
  155. function GetConfigFile()
  156. {
  157. global $steps;
  158. return $this->GetMask($steps[STEP_WRITECONFIG]['maskname']);
  159. }
  160. /**
  161. * Get the finished message with keywords replaced
  162. */
  163. function GetFinishedMessage()
  164. {
  165. global $steps;
  166. return $this->GetMask($steps[STEP_FINISHED]['maskname']);
  167. }
  168. /**
  169. * Get the SQL queries that will create tables for the system
  170. */
  171. function GetSqlInstallQueries($splitBySeperator=false)
  172. {
  173. global $steps;
  174. global $keywords;
  175. $maskContent = $this->GetMask($steps[STEP_RUNSQL]['maskname']);
  176. // If the SQL mask should be splitted by the seperator
  177. // string or not - return either one string or array of strings
  178. if($splitBySeperator)
  179. return explode($keywords['next_query'], $maskContent);
  180. else return $maskContent;
  181. }
  182. /**
  183. * Get the SQL queries that will be used to insert root access
  184. */
  185. function GetSqlRootAccessQueries($splitBySeperator=false)
  186. {
  187. global $steps;
  188. global $keywords;
  189. $maskContent = $this->GetMask($steps[STEP_ROOTUSER]['maskname']);
  190. // If the SQL mask should be splitted by the seperator
  191. // string or not - return either one string or array of strings
  192. if($splitBySeperator)
  193. return explode($keywords['next_query'], $maskContent);
  194. else return $maskContent;
  195. }
  196. }