Wpcf7_Signature_Mail

The mail-specific functionality of the plugin.

Defined (1)

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

/includes/class-wpcf7-signature-mail.php  
  1. class Wpcf7_Signature_Mail { 
  2.  
  3. /** 
  4. * The ID of this plugin. 
  5. * @since 4.0.0 
  6. * @access private 
  7. * @var string $plugin_name The ID of this plugin. 
  8. */ 
  9. private $plugin_name; 
  10.  
  11. /** 
  12. * The version of this plugin. 
  13. * @since 4.0.0 
  14. * @access private 
  15. * @var string $version The current version of this plugin. 
  16. */ 
  17. private $version; 
  18.  
  19. /** 
  20. * Initialize the class and set its properties. 
  21. * @since 4.0.0 
  22. * @param string $plugin_name The name of this plugin. 
  23. * @param string $version The version of this plugin. 
  24. */ 
  25. public function __construct( $plugin_name, $version ) { 
  26.  
  27. $this->plugin_name = $plugin_name; 
  28. $this->version = $version; 
  29.  
  30.  
  31. /** 
  32. * Modifying mail components in order to add signatures as attachments if needed 
  33. * @since 4.0.0 
  34. */ 
  35. public function modify_components($components) { 
  36.  
  37. // Which email template is it ? 
  38. if ( $mail = WPCF7_Mail::get_current() ) { 
  39.  
  40. if ( $submission = WPCF7_Submission::get_instance() ) { 
  41.  
  42. if ( $contact_form = WPCF7_ContactForm::get_current() ) { 
  43.  
  44. // Dealing with main Email 
  45. $mail = $contact_form->prop($mail->name()); 
  46. $new_attachments = $mail['attachments']; 
  47.  
  48. // Getting attachments one by one in mail configuration 
  49. $attachments = preg_split("/\\r\\n|\\r|\\n/", $mail['attachments']); 
  50.  
  51. foreach ($attachments as $attachment) { 
  52.  
  53. preg_match_all("/\[(.*?)\]/", $attachment, $attachment_names); 
  54.  
  55. foreach ($attachment_names[1] as $attachment_name) { 
  56. $data = $submission->get_posted_data($attachment_name); 
  57.  
  58. // Is is matching a signature tag ? 
  59. $tags = $contact_form->scan_form_tags(); 
  60. foreach ($tags as $tag) { 
  61. if (("signature" == $tag['type'] || "signature*" == $tag['type']) && $tag['name'] == $attachment_name) { 
  62.  
  63. // File exists ? 
  64. if (@file_exists($data)) { 
  65.  
  66. // Adding file as attachment 
  67. $components['attachments'][] = $data; 
  68.  
  69.  
  70. return $components; 
  71.  
  72. /** 
  73. * When form data is posted, we save the image somewhere in WP public directory  
  74. * and change the posted value to the image URL 
  75. * @since 4.0.0 
  76. */ 
  77. public function manage_signature_data ($posted_data) { 
  78.  
  79. foreach ($posted_data as $key => $data) { 
  80. if (is_string($data) && strrpos($data, "data:image/png;base64", -strlen($data)) !== FALSE) { 
  81.  
  82. // Do we need to treat it as inline data ? 
  83. if ($posted_data[$key."-inline"] == 1) { 
  84.  
  85. // Sending a base64 encoded inline image 
  86. $posted_data[$key] = $data; 
  87. return $posted_data; 
  88.  
  89.  
  90. $data_pieces = explode(", ", $data); 
  91. $encoded_image = $data_pieces[1]; 
  92. $decoded_image = base64_decode($encoded_image); 
  93. $filename = sanitize_file_name(wpcf7_canonicalize($key."-".time().".png")); 
  94.  
  95. $signature_dir = trailingslashit($this->signature_dir()); 
  96.  
  97. // Do we need to treat it as attachement ? 
  98. $is_attachment = $posted_data[$key."-attachment"] == 1; 
  99.  
  100. if ($is_attachment) { 
  101. // Preparing to send signature as attachement 
  102.  
  103. wpcf7_init_uploads(); // Confirm upload dir 
  104. $uploads_dir = wpcf7_upload_tmp_dir(); 
  105. $uploads_dir = wpcf7_maybe_add_random_dir( $uploads_dir ); 
  106. $filename = wp_unique_filename( $uploads_dir, $filename ); 
  107.  
  108. $filepath = trailingslashit( $uploads_dir ) . $filename; 
  109.  
  110. // Writing signature 
  111. if ( $handle = @fopen( $filepath, 'w' ) ) { 
  112. fwrite( $handle, $decoded_image ); 
  113. fclose( $handle ); 
  114. @chmod( $filepath, 0400 ); // Make sure the uploaded file is only readable for the owner process 
  115.  
  116. if (file_exists($filepath)) { 
  117.  
  118. $posted_data[$key] = $filepath; 
  119.  
  120. }else{ 
  121. error_log("Cannot create signature file as attachment : ".$filepath); 
  122. }else{ 
  123.  
  124. // Sending signature asa server image 
  125.  
  126. if( !file_exists( $signature_dir ) ) { // Creating directory and htaccess file 
  127. if (wp_mkdir_p( $signature_dir )) { 
  128. $htaccess_file = $signature_dir . '.htaccess'; 
  129.  
  130. if ( !file_exists( $htaccess_file ) && $handle = @fopen( $htaccess_file, 'w' ) ) { 
  131. fwrite( $handle, 'Order deny, allow' . "\n" ); 
  132. fwrite( $handle, 'Deny from all' . "\n" ); 
  133. fwrite( $handle, '<Files ~ "^[0-9A-Za-z_-]+\\.(png)$">' . "\n" ); 
  134. fwrite( $handle, ' Allow from all' . "\n" ); 
  135. fwrite( $handle, '</Files>' . "\n" ); 
  136. fclose( $handle ); 
  137.  
  138. $filepath = wp_normalize_path( $signature_dir . $filename ); 
  139.  
  140. // Writing signature 
  141. if ( $handle = @fopen( $filepath, 'w' ) ) { 
  142. fwrite( $handle, $decoded_image ); 
  143. fclose( $handle ); 
  144. @chmod( $filepath, 0644 ); 
  145.  
  146. if (file_exists($filepath)) { 
  147.  
  148. $fileurl = $this->signature_url($filename); 
  149. $posted_data[$key] = $fileurl; 
  150.  
  151. }else{ 
  152. error_log("Cannot create signature file : ".$filepath); 
  153.  
  154.  
  155.  
  156. return $posted_data; 
  157.  
  158. private function signature_dir() { 
  159. if ( defined( 'WPCF7_SIGNATURE_DIR' ) ) 
  160. return WPCF7_SIGNATURE_DIR; 
  161. else 
  162. return wpcf7_upload_dir( 'dir' ) . '/wpcf7_signatures'; 
  163.  
  164. private function signature_dir_url() { 
  165. if ( defined( 'WPCF7_SIGNATURE_URL' ) ) 
  166. return WPCF7_SIGNATURE_URL; 
  167. else 
  168. return wpcf7_upload_dir( 'url' ) . '/wpcf7_signatures'; 
  169.  
  170. private function signature_url( $filename ) { 
  171. $url = trailingslashit( $this->signature_dir_url() ) . $filename; 
  172.  
  173. if ( is_ssl() && 'http:' == substr( $url, 0, 5 ) ) { 
  174. $url = 'https:' . substr( $url, 5 ); 
  175.  
  176. return apply_filters( 'wpcf7_signature_url', esc_url_raw( $url ) ); 
  177.