PageRenderTime 53ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/install/shared.php

https://github.com/rjdjohnston/core
PHP | 220 lines | 166 code | 49 blank | 5 comment | 39 complexity | 907e077be6ff3228e721b353216e1e4c MD5 | raw file
  1. <?php
  2. class Check {
  3. protected static $errors = 0;
  4. protected static $include_found = true;
  5. public static function no_errors () {
  6. return ( 0 == self::$errors );
  7. }
  8. public static function restart () {
  9. self::$errors = 0;
  10. }
  11. public static function error_count () {
  12. return self::$errors;
  13. }
  14. protected static function show_check_div ( $string, $class ) {
  15. echo '<div class="' . $class . '">' . $string . '</div>';
  16. }
  17. public static function good ( $string ) { self::show_check_div( $string, 'good' ); }
  18. public static function bad ( $string ) { self::$errors++; self::show_check_div( $string, 'bad' ); }
  19. public static function warn ( $string ) { self::show_check_div( $string, 'warn' ); }
  20. // Check the level of php available
  21. public static function PHP ( $required ) {
  22. if( 1 == version_compare( $required, phpversion() ) )
  23. Check::bad( "Your PHP version is too low, the minimum required is $required." );
  24. else
  25. Check::good( "PHP Version " . phpversion() . " meets requirement." );
  26. }
  27. public static function SettingValue ( $setting, $expected ) {
  28. if( $expected != ini_get( $setting ) )
  29. Check::bad( "PHP Setting '$setting' should be '". var_export( $expected, true ) . "'." );
  30. else
  31. Check::good( "PHP Setting '$setting' is '" . var_export( $expected, true ) ."'." );
  32. }
  33. // Check if a class exists
  34. public static function ClassExists ( $class, $name, $warn_only=false ) {
  35. if( class_exists( $class, false ) )
  36. Check::good( "Found $name." );
  37. else if( $warn_only )
  38. Check::warn( "Can not find $name." );
  39. else
  40. Check::bad( "Can not find $name." );
  41. }
  42. // Check if a function exists.
  43. public static function FunctionExists ( $function, $name, $warn_only=false ) {
  44. if( function_exists( $function ) )
  45. Check::good( "Found $name." );
  46. else if( $warn_only )
  47. Check::warn( "Can not find $name." );
  48. else
  49. Check::bad( "Can not find $name." );
  50. }
  51. // Check if a file can be included, is on the path.
  52. public static function CanInclude ( $include, $name, $warn_only=false ) {
  53. self::$include_found = true;
  54. set_error_handler( 'Check::include_error_handler', E_WARNING );
  55. include_once( $include );
  56. restore_error_handler();
  57. if( self::$include_found )
  58. Check::good( "Found $name." );
  59. else if( $warn_only )
  60. Check::warn( "Can not find $name." );
  61. else
  62. Check::bad( "Can not find $name." );
  63. return self::$include_found;
  64. }
  65. protected static function include_error_handler ( $errno, $errstr ) {
  66. self::$include_found = false;
  67. }
  68. // Checks an extension existence by phpversion. Doesn't work for all extensions.
  69. public static function ExtensionExists ( $extension, $name, $warn_only=false ) {
  70. if( false !== phpversion( $extension ) )
  71. Check::good( "Found $name." );
  72. else if( $warn_only )
  73. Check::warn( "Can not find $name." );
  74. else
  75. Check::bad( "Can not find $name." );
  76. }
  77. public static function PathWritable ( $path, $warn_only=false ) {
  78. $root = dirname( __FILE__ ) . '/../../';
  79. if( is_writable( $root . $path ) )
  80. Check::good( "$path is writable." );
  81. else if( $warn_only )
  82. Check::warn( "$path is not writable." );
  83. else
  84. Check::bad( "$path is not writable." );
  85. }
  86. } // Class Check
  87. class Form {
  88. public function __construct ( $errors, $values ) {
  89. $this->errors = $errors;
  90. $this->values = $values;
  91. }
  92. public function input ( $type, $set, $name, $label = null ) {
  93. $label = ( is_null( $label ) ? ucwords( str_replace( '_', ' ', $name ) ) : $label );
  94. $iname = $set . '_' . $name;
  95. $value = isset($this->values[$iname]) ? $this->values[$iname] : '';
  96. print '<label for="' . $iname . '">'. $label . ':</label> <input type="' . $type . '" id="'. $iname . '" name="' . $iname . '" value="' . ( ( 'password' == $type ) ? '' : $value ) . '"/>';
  97. if( isset( $this->errors[$iname] ) )
  98. print '<div class="install-error">' . $this->errors[$iname] . '</div>';
  99. print '<br/>';
  100. }
  101. public function text ( $set, $name, $label = null ) {
  102. $this->input( 'text', $set, $name, $label );
  103. }
  104. public function password ( $set, $name, $label = null ) {
  105. $this->input( 'password', $set, $name, $label );
  106. }
  107. } // Class Form
  108. class Database {
  109. protected static $link = null;
  110. public static function connect ( $host, $db, $user, $password ) {
  111. $link = @mysql_connect( $host, $user, $password );
  112. if( ! $link )
  113. return 'Could not connect to host: ' . mysql_error();
  114. if( ! @mysql_select_db( $db, $link ) )
  115. return 'Could not select database: ' . mysql_error();
  116. self::$link = $link;
  117. return true;
  118. }
  119. public static function RunFile ( $file, $substitutions = array() ) {
  120. if( null == self::$link )
  121. return 'Not connected to a database.';
  122. if( ! file_exists( $file ) )
  123. return 'File does not exist: ' . $file;
  124. $data = file_get_contents( $file );
  125. foreach( $substitutions as $key => $value )
  126. $data = str_replace( "[:$key]", mysql_real_escape_string( $value, self::$link ), $data );
  127. $queries = explode( ';', $data );
  128. foreach( $queries as $query ) {
  129. $query = trim( $query );
  130. if( empty( $query ) )
  131. continue;
  132. if( false === mysql_query( trim( $query ), self::$link ) )
  133. return mysql_error();
  134. }
  135. return true;
  136. }
  137. public static function RunFolder ($folder, $substitutions = array() ) {
  138. if (!is_dir($folder)) {
  139. return "Not a folder: $folder.";
  140. }
  141. if (!$handle = opendir($folder)) {
  142. return "Could not open folder $folder.";
  143. }
  144. while (false !== ($file = readdir($handle))) {
  145. if ($file != "." && $file != "..") {
  146. $res = Database::RunFile($folder . "/" . $file, $substitutions);
  147. if (true !== $res) {
  148. return $res;
  149. }
  150. }
  151. }
  152. closedir($handle);
  153. return true;
  154. }
  155. } // Class Database
  156. class Config {
  157. public static function RenderFile ( $template_file, $substitutions ) {
  158. if( ! file_exists( $template_file ) )
  159. return false;
  160. $data = file_get_contents( $template_file );
  161. foreach( $substitutions as $key => $value )
  162. $data = str_replace( "[:$key]", $value, $data );
  163. return $data;
  164. }
  165. public static function SaveFile ( $template_file, $dest, $substitutions ) {
  166. $contents = Config::RenderFile( $template_file, $substitutions );
  167. if( false === $contents )
  168. return false;
  169. return ( false !== @file_put_contents( $dest, $contents ) );
  170. }
  171. }