WYSIJA_help_server

The MailPoet Newsletters WYSIJA help server class.

Defined (1)

The class is defined in the following location(s).

/helpers/server.php  
  1. class WYSIJA_help_server extends WYSIJA_object { 
  2.  
  3. function __construct() { 
  4. parent::__construct(); 
  5.  
  6. /** 
  7. * test if the server on which you are running wysija is unhealthy and may cause troubles 
  8. * @return mixed 
  9. */ 
  10. function unhealthy($return_result=false) { 
  11.  
  12. $server_missing_capabilities=array(); 
  13.  
  14. $missing_functions_result=$this->missing_php_functions(); 
  15. // can we mkdir 
  16. if($missing_functions_result) $server_missing_capabilities['functions']=$missing_functions_result; 
  17.  
  18. // can we mkdir 
  19. if(!$this->can_make_dir()) $server_missing_capabilities['mkdir']=true; 
  20.  
  21. // can we unzip a file to a temp folder 
  22. if(!$this->can_unzip()) $server_missing_capabilities['unzip']=true; 
  23.  
  24. // can we move files to a new folder 
  25. if(!$this->can_move()) $server_missing_capabilities['move']=true; 
  26.  
  27. // can we create, alter tables to the database 
  28. if(!$this->can_sql_create_tables()) $server_missing_capabilities['sql_create']=true; 
  29.  
  30. // can we create, alter tables to the database 
  31. if(!$this->can_sql_alter_tables()) $server_missing_capabilities['sql_alter']=true; 
  32.  
  33. if(!empty($server_missing_capabilities)) return $server_missing_capabilities; 
  34. else return false; 
  35.  
  36. /** 
  37. * check if the server miss some vital functions to work properly 
  38. * @return mixed 
  39. */ 
  40. function missing_php_functions() { 
  41. // can we use those functions on the server? 
  42. $functions_per_environment=array( 
  43. 'required'=> array( 
  44. 'functions' => array('base64_decode', 'base64_encode'),  
  45. ),  
  46. 'remote calls' => array( 
  47. 'functions'=>array('curl_init', 'http_get', 'file_get_contents'),  
  48. ),  
  49. 'DKIM signature'=>array( 
  50. 'functions'=>array('openssl_sign') 
  51. ); 
  52.  
  53. $missing_functions=array(); 
  54. foreach($functions_per_environment as $environment => &$data) { 
  55. // if the function we're testing has an alternative we try it 
  56. foreach($data['functions'] as $function_name) { 
  57. if($this->is_function_available($function_name)) { 
  58. $data['functions'][$function_name]=true; 
  59. }else{ 
  60. $missing_functions[$environment][$function_name]=true; 
  61.  
  62. if(!empty($missing_functions)) return $missing_functions; 
  63. return false; 
  64.  
  65.  
  66. /** 
  67. * check if a php function is usable on this host 
  68. * some hosts disable base64_encode or base64_decode for instance ... 
  69. * @param string $function_name 
  70. * @return boolean 
  71. */ 
  72. function is_function_available($function_name) { 
  73. // we just want string here 
  74. if(!is_string($function_name) || $function_name=='') return false; 
  75.  
  76. // get the list of disabled function recorded in the php.ini 
  77. $disabled = explode(', ', ini_get('disable_functions')); 
  78.  
  79. // does our function exists and is not in the disabled functions list of php.ini 
  80. if(function_exists($function_name) && !in_array(strtolower($function_name), $disabled)) return true; 
  81.  
  82. return false; 
  83.  
  84. /** 
  85. * check if the php user can make a dir on the server 
  86. * @return boolean 
  87. */ 
  88. function can_make_dir() { 
  89. // Test temp directory creation. 
  90. $hFile = WYSIJA::get('file', 'helper'); 
  91. $upload_dir = wp_upload_dir(); 
  92. $temp_dir = $hFile->makeDir(); 
  93. if (!$temp_dir) { 
  94. $this->error(sprintf(__('The folder "%1$s" is not writable, please change the access rights to this folder so that MailPoet can setup itself properly.', WYSIJA), $upload_dir['basedir']).'<a target="_blank" href="http://codex.wordpress.org/Changing_File_Permissions">'.__('Read documentation', WYSIJA).'</a>'); 
  95. return false; 
  96. } else { 
  97. // Create index.html to protect the temp directory. 
  98. $index_file = 'index.html'; 
  99. fclose(fopen($temp_dir.$index_file, 'w')); 
  100. return true; 
  101.  
  102. /** 
  103. * check if the server allows us to unzip files through php 
  104. * @return boolean 
  105. */ 
  106. function can_unzip() { 
  107. return true; 
  108.  
  109. /** 
  110. * check if the server can move a directory with nested content and directories to another directory 
  111. * @return boolean 
  112. */ 
  113. function can_move() { 
  114. return true; 
  115.  
  116. /** 
  117. * check if the SQL user is allowed to create tables onto the database 
  118. * @global type $wpdb 
  119. * @return boolean 
  120. */ 
  121. function can_sql_create_tables() { 
  122.  
  123. // test that we can create tables on the mysql server 
  124. $model_user = WYSIJA::get('user', 'model'); 
  125.  
  126. $this->_create_temp_sql_table_if_not_exists(); 
  127.  
  128. $query="SHOW TABLES like '".$model_user->getPrefix()."user_list_temp';"; 
  129.  
  130. global $wpdb; 
  131. $res = $wpdb->get_var($query); 
  132.  
  133. if(!$res) { 
  134. $this->error(sprintf( 
  135. __('The MySQL user you have setup on your WordPress site (wp-config.php) doesn\'t have enough privileges to CREATE MySQL tables. Please change this user yourself or contact the administrator of your site in order to complete MailPoet\'s installation. mysql errors:(%1$s)', WYSIJA), $wpdb->last_error)); 
  136. return false; 
  137. return true; 
  138.  
  139.  
  140. /** 
  141. * check if the SQL user is allowed to alter tables onto the database 
  142. * @return boolean 
  143. */ 
  144. function can_sql_alter_tables() { 
  145. // if we call that function out of the main process we make sure the table exists 
  146. $this->_create_temp_sql_table_if_not_exists(); 
  147.  
  148. // test that we can alter tables on the mysql server 
  149. $model_user=WYSIJA::get('user', 'model'); 
  150.  
  151. $query='ALTER TABLE `'.$model_user->getPrefix().'user_list_temp` ADD `namekey` VARCHAR( 255 ) NULL;'; 
  152.  
  153. global $wpdb; 
  154. $wpdb->query($query); 
  155. if(!$wpdb->result) { 
  156. $error_message=__('The MySQL user you have setup on your WordPress site (wp-config.php) doesn\'t have enough privileges to CREATE MySQL tables. Please change this user yourself or contact the administrator of your site in order to complete MailPoet\'s installation. mysql errors:(%1$s)', WYSIJA); 
  157. $this->error(sprintf(str_replace('CREATE', 'ALTER', $error_message), $wpdb->last_error )); 
  158. $this->_drop_temp_sql_table(); 
  159. return false; 
  160. $this->_drop_temp_sql_table(); 
  161. return true; 
  162.  
  163. /** 
  164. * create the temporary table we need for SQL access rights tests 
  165. * @global type $wpdb 
  166. * @return boolean 
  167. */ 
  168. function _create_temp_sql_table_if_not_exists() { 
  169. $model_user=WYSIJA::get('user', 'model'); 
  170. $query='CREATE TABLE IF NOT EXISTS `'.$model_user->getPrefix().'user_list_temp` ( 
  171. `list_id` INT unsigned NOT NULL,  
  172. `user_id` INT unsigned NOT NULL,  
  173. `sub_date` INT unsigned DEFAULT 0,  
  174. `unsub_date` INT unsigned DEFAULT 0,  
  175. PRIMARY KEY (`list_id`, `user_id`) 
  176. )'; 
  177.  
  178. global $wpdb; 
  179.  
  180. $wpdb->query($query); 
  181. return true; 
  182.  
  183.  
  184. /** 
  185. * drop the temporary table we've created 
  186. * @return boolean 
  187. */ 
  188. function _drop_temp_sql_table() { 
  189. $model_user=WYSIJA::get('user', 'model'); 
  190. global $wpdb; 
  191. $query='DROP TABLE `'.$model_user->getPrefix().'user_list_temp`;'; 
  192.  
  193. $wpdb->query($query); 
  194. if(!$wpdb->result) { 
  195. return false; 
  196. return true; 
  197.