[ Index ]

MailPress 7.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_WP_Ajax.class.php (source)

   1  <?php
   2  class MP_WP_Ajax
   3  {
   4      public static $get_;
   5      public static $pst_;
   6      public static $req_;
   7  
   8  	function __construct()
   9      {
  10          self::get_request();
  11  
  12          if ( !isset( self::$req_['mp_action'] ) ) MP_::mp_die( -1 );
  13  
  14          $action = str_replace( '-', '_', self::$req_['mp_action'] );
  15  
  16          if ( method_exists( __CLASS__, $action ) ) self::$action();
  17          else do_action( "mp_action_{$action}" );
  18                  
  19          die();
  20      }
  21  
  22  	public static function get_request()
  23      {
  24          foreach( array( 'get_' =>     INPUT_GET, 'pst_' => INPUT_POST ) as $k => $v )
  25          {
  26              self::$$k = filter_input_array( $v );
  27              if ( is_null( self::$$k ) ) self::$$k = array();
  28          }
  29          self::$req_ = array_merge( self::$get_, self::$pst_ );
  30      }
  31  
  32  //// LIST ////
  33  
  34  	public static function dim_mail() 
  35      {
  36          MP_Mail::dim_object();
  37      }
  38  
  39  	public static function add_mail() 
  40      {
  41          MP_Mail::add_object();
  42      }
  43  
  44  	public static function delete_mail() 
  45      {
  46          MP_Mail::delete_object();
  47      }
  48  
  49  	public static function add_user() 
  50      {
  51          MP_User::add_object();
  52      }
  53  
  54  	public static function dim_user() 
  55      {
  56          MP_User::dim_object();
  57      }
  58  
  59  	public static function delete_user() 
  60      {
  61          MP_User::delete_object();
  62      }
  63  
  64  //// CUSTOM FIELDS ////
  65  
  66  	public static function add_mailmeta()
  67      {
  68          MP_Mail_customfields::add();
  69      }
  70  
  71  	public static function delete_mailmeta()
  72      {
  73          MP_Mail_customfields::delete();
  74      }
  75  
  76  	public static function add_usermeta()
  77      {
  78          MP_User_customfields::add();
  79      }
  80  
  81  	public static function delete_usermeta()
  82      {
  83          MP_User_customfields::delete();
  84      }
  85  
  86  ////  VIEW MAIL/THEME in thickbox  ////
  87  
  88  	public static function get_previewlink()
  89      {
  90          $args = array( 'id' => 0, 'main_id' => 0, 'action' => 'mp_ajax', 'mp_action' => 'iview', 'TB_iframe' => 'true' );
  91          $args['id']        = ( isset( self::$pst_['id'] ) ) ? intval( self::$pst_['id'] ) : 0;
  92          $args['main_id']    = ( isset( self::$pst_['main_id'] ) ) ? intval( self::$pst_['main_id'] ) : 0;
  93          $view_url = esc_url( add_query_arg( $args, admin_url( 'admin-ajax.php' ) ) );    
  94  
  95          MP_::mp_die( $view_url );
  96      }
  97  
  98  	public static function iview()
  99      {
 100          $mp_general = get_option( MailPress::option_name_general );
 101  
 102          $id         = self::$get_['id'];
 103          $main_id    = ( isset( self::$get_['main_id'] ) ) ? self::$get_['main_id'] : $id;
 104  
 105          $mail     = MP_Mail::get( $id );
 106                  
 107                  if ( !$mail ) return false;
 108  
 109          $theme     = ( isset( self::$get_['theme'] ) && !empty( self::$get_['theme'] ) ) ? self::$get_['theme'] : ( !empty( $mail->theme ) ? $mail->theme : false );
 110          $mp_user_id    = ( isset( self::$get_['mp_user_id'] )  && !empty( self::$get_['mp_user_id'] ) )  ? self::$get_['mp_user_id']  : false;
 111  
 112      // from
 113          $from     = ( !empty( $mail->fromemail ) ) ? MP_Mail::display_toemail( $mail->fromemail, $mail->fromname ) : MP_Mail::display_toemail( $mp_general['fromemail'], $mp_general['fromname'] );
 114      // to
 115          $to         = MP_Mail::display_toemail( $mail->toemail, $mail->toname, '', $mp_user_id );
 116      // subject
 117          $x = new MP_Mail();
 118          $subject     = ( in_array( $mail->status, array( 'sent', 'archived' ) ) ) ? $mail->subject : $x->do_eval( $mail->subject );
 119          $subject     = $x->viewsubject( $subject, $id, $main_id, $mp_user_id );
 120      // template
 121          $template   = ( in_array( $mail->status, array( 'sent', 'archived' ) ) ) ? false : apply_filters( 'MailPress_draft_template', false, $main_id );
 122  
 123      // content
 124          $args            = array();
 125          $args['action']     = 'mp_ajax';
 126          $args['mp_action']     = 'viewadmin';
 127          foreach( array( 'id', 'main_id', 'theme', 'template', 'mp_user_id' ) as $x ) if ( $$x ) $args[$x] = $$x;
 128  
 129          foreach( array( 'html', 'plaintext' ) as $type )
 130          {
 131              $args['type'] = $type;
 132              if ( !empty( $mail->{$type} ) ) $$type = '<iframe id="i' . $type . '" style="width:100%;border:0;height:550px" src="' . esc_url( add_query_arg( $args, admin_url( 'admin-ajax.php' ) ) ) . '"></iframe>';
 133          }
 134  
 135      // attachments
 136          $attachments = '';
 137          $metas = MP_Mail_meta::has( $args['main_id'], '_MailPress_attached_file' );
 138          if ( $metas )
 139          {
 140              foreach( $metas as $meta )
 141              {
 142                                  $meta_value = unserialize( $meta['meta_value'] );
 143                  $attachments .= '<tr><td><div class="' . self::get_html_mail_icon( $meta_value['name'] ) . '"></div><div>' . MP_Mail::get_attachment_link( $meta, $mail->status ) . '</div></td></tr>';
 144              }
 145          }
 146          $view = true;
 147  
 148          include ( MP_ABSPATH . 'mp-includes/html/mail.php' );
 149      }
 150  
 151  	public static function viewadmin() 
 152      {
 153          self::$get_['type'] = ( isset( self::$get_['type'] ) ) ? self::$get_['type'] : 'html';
 154          self::$get_['template'] = apply_filters( 'MailPress_draft_template', isset( self::$get_['template'] ) ? self::$get_['template'] : false, self::$get_['main_id'] );
 155  
 156          $x = new MP_Mail();
 157          $x->view( self::$get_ );
 158      }
 159  
 160  
 161  ////  THEMES  ////
 162  
 163  	public static function theme_preview() 
 164      {
 165          $args            = array( 'action' => 'mp_ajax', 'mp_action' => 'previewtheme', 'template' => self::$get_['template'], 'stylesheet'=> self::$get_['stylesheet'] );
 166  
 167          foreach( array( 'html', 'plaintext' ) as $type )
 168          {
 169              $args['type']     = $type;
 170              $$type        = '<iframe id="i' . $type . '" style="width:100%;border:0;height:550px" src="' . esc_url( add_query_arg( $args, admin_url( 'admin-ajax.php' ) ) ) . '"></iframe>';
 171          }
 172  
 173          unset( $view );
 174          include  ( MP_ABSPATH . 'mp-includes/html/mail.php' );
 175      }
 176  
 177  	public static function previewtheme() 
 178      {
 179          $url             = home_url();
 180  
 181          $mail            = new stdClass();
 182          $mail->Theme     = self::$get_['stylesheet'];
 183          $mail->Template     = 'confirmed';
 184  
 185          $message  = __( 'Congratulations !', 'MailPress' );
 186          $message .= "\n\n";
 187          $message .= sprintf( __( 'We confirm your subscription to %1$s emails', 'MailPress' ), get_bloginfo( 'name' ) );
 188          $message .= "\n\n";
 189  
 190          $mail->plaintext     = $message;
 191  
 192          $message  = __( 'Congratulations !', 'MailPress' );
 193          $message .= '<br /><br />';
 194          $message .= sprintf( __( 'We confirm your subscription to %1$s emails', 'MailPress' ), '<a href="' . $url . '">' . get_bloginfo( 'name' ) . '</a>' );
 195          $message .= '<br /><br />';
 196  
 197          $mail->html     = $message;
 198  
 199          $mail->unsubscribe= __( '"Subscription management link"', 'MailPress' );
 200          $mail->viewhtml     = __( '"Trouble reading link"', 'MailPress' );
 201  
 202          $x = new MP_Mail();
 203          $x->args = new stdClass();
 204          $x->args = $mail;
 205  
 206          $type  = self::$get_['type'];
 207          $$type = $x->build_mail_content( $type );
 208          $$type = ( 'html' == $type ) ? $x->process_img( $$type, $x->mail->themedir, 'draft' ) : $$type;
 209          include MP_ABSPATH . "mp-includes/html/{$type}.php";
 210      }
 211  
 212  
 213  ////  WRITE  ////
 214  
 215  	public static function html2txt() 
 216      {
 217          if ( !isset( self::$pst_['html'] ) ) return '';
 218          $content = trim( stripslashes( self::$pst_['html'] ) );
 219          if ( empty( $content ) ) return '';
 220          $content = apply_filters( 'the_content', $content );
 221                 if ( empty( $content ) ) return '';
 222                  
 223          $txt = new MP_Html2txt( $content );
 224          echo $txt->get_text();
 225          die();
 226      }
 227  
 228  	public static function autosave()
 229      {
 230          global $current_user;
 231  
 232          $data = '';
 233          $supplemental = array();
 234          $do_lock     = true;
 235  
 236          $working_id = $main_id     = ( int ) self::$pst_['id'];
 237          $do_autosave= ( bool ) self::$pst_['autosave'];
 238  
 239          if ( -1 == self::$pst_['revision'] )
 240          {
 241              if ( $do_autosave ) 
 242              {
 243                  if ( !$working_id ) $working_id = $main_id = MP_Mail::get_id( __CLASS__ . ' 1 ' . __METHOD__ );
 244  
 245                  MP_Mail_draft::update( $working_id );
 246                  $data = sprintf( __( 'Draft saved at %s.', 'MailPress' ), date( __( 'g:i:s a' ), current_time( 'timestamp' ) ) );
 247                  $supplemental['tipe'] = 'mail';
 248              }
 249          }
 250          else
 251          {
 252              if ( $last = MP_Mail_lock::check( $main_id ) ) 
 253              {
 254                  $do_autosave     = $do_lock = false;
 255                  $last_user         = get_userdata( $last );
 256                  $last_user_name     = ( $last_user ) ? $last_user->display_name : __( 'Someone' );    
 257                  $data         = new WP_Error( 'locked', sprintf( __( 'Autosave disabled: %s is currently editing this mail.' ) , esc_html( $last_user_name )     ) );
 258                  $supplemental['disable_autosave'] = 'disable';
 259              }
 260  
 261              if ( $do_autosave ) 
 262              {
 263                  $working_id = ( int ) self::$pst_['revision'];
 264                  if ( !$working_id )
 265                  {
 266                      $working_id = MP_Mail::get_id( __CLASS__ . ' 2 ' . __METHOD__ );
 267                      MP_Mail_revision::update( $main_id, $working_id, $current_user->ID );
 268                  }
 269  
 270                  MP_Mail_draft::update( $working_id, '' );
 271                  $data = sprintf( __( 'Revision saved at %s.', 'MailPress' ), date( __( 'g:i:s a' ), current_time( 'timestamp', true ) ) );
 272                  $supplemental['tipe'] = 'revision';
 273              }
 274              else
 275              {
 276                  if ( self::$pst_['revision'] ) $working_id = ( int ) self::$pst_['revision'];
 277                  $supplemental['tipe'] = 'revision';
 278              }
 279          }
 280  
 281          if ( $do_lock && $working_id ) MP_Mail_lock::set( $main_id );
 282  
 283          $x = new WP_Ajax_Response( array (     'what'     => 'autosave', 
 284                                  'id'         => $working_id, 
 285                                  'old_id'     => $main_id, 
 286                                  'type'     => false, 
 287                                  'data'     => $working_id ? $data : '', 
 288                                  'supplemental' => $supplemental
 289           ) );
 290  
 291          $x->send();
 292      }
 293  
 294  	public static function email() 
 295      {
 296          echo ( MailPress::is_email( self::$pst_['email'] ) ) ? 1 : 0;
 297      }
 298  
 299  ////  REVISIONS  ////
 300  
 301  	function get_revision_diffs() 
 302      {
 303          $return = array();
 304          @set_time_limit( 0 );
 305    
 306          foreach ( self::$req_['compare'] as $compare_key ) 
 307          {
 308              list( $compare_from, $compare_to ) = explode( ':', $compare_key ); // from:to
 309    
 310              $return[] = array( 
 311                  'id' => $compare_key,
 312                  'fields' => MP_Mail_revision::get_ui_diff( self::$req_['mail_id'], $compare_from, $compare_to ),
 313               );
 314          }
 315          wp_send_json_success( $return );
 316      }
 317  
 318  
 319  ////  ATTACHMENTS  UPLOAD  ////
 320  
 321  	public static function upload_iframe_html()
 322      {
 323          $id         = self::$get_['id'];
 324          $draft_id     = self::$get_['draft_id'];
 325          $bytes     = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
 326  
 327          wp_register_script( 'upload_iframe', '/' . MP_PATH . 'mp-includes/js/fileupload/upload_iframe.js', array( 'jquery' ), false, 1 );
 328          wp_localize_script( 'upload_iframe', 'uploadhtmlL10n', array( 
 329              'id' => $id
 330           ) );
 331          wp_enqueue_script( 'upload_iframe' );
 332  
 333          include  MP_ABSPATH . 'mp-includes/html/upload_iframe.php';
 334      }
 335  
 336  	public static function html_mail_attachment() 
 337      {
 338          $draft_id     = self::$req_['draft_id'];
 339          $id        = self::$req_['id'];
 340          $file        = self::$req_['file'];
 341  
 342          $xml = self::mail_attachment();
 343  
 344          $xml = str_replace( '>', '&gt;', $xml );
 345          $xml = str_replace( '<', '&lt;', $xml );
 346  
 347          wp_register_script( 'upload_iframe_xml', '/' . MP_PATH . 'mp-includes/js/fileupload/upload_iframe_xml.js', array( 'jquery' ), false, 1 );
 348          wp_localize_script( 'upload_iframe_xml', 'uploadxmlL10n', array( 
 349              'id'        => $id,
 350              'draft_id'     => $draft_id,
 351              'file'     => $file
 352           ) );
 353          wp_enqueue_script( 'upload_iframe_xml' );
 354  
 355          ob_end_clean();
 356          ob_start();
 357              include  MP_ABSPATH . 'mp-includes/html/upload_iframe_xml.php';
 358              $html = ob_get_contents();
 359          ob_end_clean();
 360  
 361          MP_::mp_die( $html );
 362      }
 363  
 364  	public static function html_mail_icon() 
 365      {
 366          $fname = self::$req_['fname'];
 367  
 368          MP_::mp_die( self::get_html_mail_icon( $fname ) );
 369      }
 370  
 371  	public static function get_html_mail_icon( $fname ) 
 372      {
 373          if ( !class_exists( 'MP_Tracking_files', false ) ) new MP_Tracking_files();
 374  
 375          $fe = apply_filters( 'MailPress_tracking_files_ext_get', pathinfo( $fname, PATHINFO_EXTENSION ) );
 376  
 377          return ( isset( $fe->classes ) ) ? $fe->classes : 'mp_ext mp_ext_unknown';
 378      }
 379  
 380  	public static function mail_attachment()
 381      {
 382          $data = self::handle_upload( 'async-upload', self::$req_['draft_id'] );
 383  
 384          if ( is_wp_error( $data ) ) 
 385          {
 386              $xml  = "<error><![CDATA[" . $data->get_error_message() . "]]></error>";
 387          }
 388          else
 389          {
 390              $xml  = "<id><![CDATA[" . $data['id'] . "]]></id>";
 391              $xml .= "<url><![CDATA[" . $data['url'] . "]]></url>";
 392              $xml .= "<file><![CDATA[" . $data['file'] . "]]></file>";
 393          }
 394  
 395          return "<?xml version='1.0' standalone='yes'?><mp_fileupload>$xml</mp_fileupload>";
 396      }
 397  
 398  	public static function handle_upload( $file_id, $draft_id ) 
 399      {
 400          $overrides = array( 'test_form'=>false, 'unique_filename_callback' => 'mp_unique_filename_callback' );
 401          $time = current_time( 'mysql' );
 402  
 403          $uploaded_file = wp_handle_upload( $_FILES[$file_id], $overrides, $time );
 404  
 405          if ( isset( $uploaded_file['error'] ) )
 406              return new WP_Error( 'upload_error', $uploaded_file['error'] );
 407  
 408  // Check file path is ok
 409          $uploads = wp_upload_dir();
 410          if ( $uploads && ( false === $uploads['error'] ) )                             // Get upload directory
 411          {     
 412              if ( 0 === strpos( $uploaded_file['file'], $uploads['basedir'] ) )                 // Check that the upload base exists in the file path
 413              {
 414                  $file = str_replace( $uploads['basedir'], '', $uploaded_file['file'] );         // Remove upload dir from the file path
 415                  $file = ltrim( $file, '/' );
 416              }
 417          }
 418  
 419  // Construct the attachment array
 420          $object = array( 
 421                      'name'     => $_FILES['async-upload']['name'], 
 422                      'mime_type'    => $uploaded_file['type'], 
 423                      'file'    => $file, 
 424                      'file_fullpath'    => str_replace( "\\", "/", $uploaded_file['file'] ), 
 425                      'guid'     => $uploaded_file['url']
 426                   );
 427  // Save the data
 428          $id = MP_Mail_meta::add( $draft_id, '_MailPress_attached_file', $object );
 429  
 430          $href = esc_url( add_query_arg( array( 'action' => 'mp_ajax', 'mp_action' => 'attach_download', 'id' => $id ), admin_url( 'admin-ajax.php' ) ) );
 431          return array( 'id' => $id, 'url' => $href, 'file' => $object['file_fullpath'] );
 432      }
 433  
 434  	public static function attach_download()
 435      {
 436          $meta_id     = ( int ) self::$get_['id'];
 437  
 438          $meta = MP_Mail_meta::get_by_id( $meta_id );
 439  
 440          if ( !$meta ) MP_::mp_die( __( 'Cannot Open Attachment 1!', 'MailPress' ) );
 441          if ( !is_file( $meta->meta_value['file_fullpath'] ) )    MP_::mp_die( __( 'Cannot Open Attachment 2! ' . $meta->meta_value['file_fullpath'], 'MailPress' ) );
 442  
 443          self::download( $meta->meta_value['name'], $meta->meta_value['file_fullpath'], $meta->meta_value['mime_type'] );
 444      }
 445  
 446  	public static function delete_attachment()
 447      {
 448          if ( !isset( self::$pst_['meta_id'] ) ) return;
 449          if ( !is_numeric( self::$pst_['meta_id'] ) ) return;
 450  
 451          $meta_id = ( int ) self::$pst_['meta_id'];
 452          MP_Mail_meta::delete_by_id( $meta_id );
 453          MP_::mp_die( 1 );
 454      }
 455  
 456  ////  MAPS  ////
 457  
 458  	public static function latlng()
 459      {
 460          $g = MP_Ip::get_current_latlng();
 461  
 462          if (empty( $g ) ) wp_send_json_error( $g );
 463  
 464          wp_send_json_success( $g );
 465      }
 466  
 467  	public static function map_settings()
 468      {
 469          if ( 'mp_user' == self::$pst_['type'] )
 470          {
 471              if ( !MP_User_meta::add(   self::$pst_['id'], '_MailPress_' . self::$pst_['prefix'], self::$pst_['settings'], true ) )
 472                  MP_User_meta::update( self::$pst_['id'], '_MailPress_' . self::$pst_['prefix'], self::$pst_['settings'] );
 473          }
 474          else
 475          {
 476              if ( !MP_Mail_meta::add(   self::$pst_['id'], '_MailPress_' . self::$pst_['prefix'], self::$pst_['settings'], true ) )
 477                  MP_Mail_meta::update( self::$pst_['id'], '_MailPress_' . self::$pst_['prefix'], self::$pst_['settings'] );
 478          }
 479          MP_::mp_die();
 480  
 481          update_user_meta( MP_WP_User::get_id(), '_MailPress_' . self::$pst_['prefix'], self::$pst_['settings'] );
 482          MP_::mp_die();
 483      }
 484  
 485  	public static function staticmap()
 486      {
 487          unset( self::$pst_['action'] );
 488  
 489          $s = MP_Map::get_staticmap( false, self::$pst_ );
 490  
 491          if (empty( $s ) ) wp_send_json_error( $s );
 492  
 493          wp_send_json_success( $s );
 494      }
 495  
 496  	public static function geocoding()
 497      {
 498          $g = MP_Map::get_lnglat( self::$pst_['addr'] );
 499  
 500          if (empty( $g ) ) wp_send_json_error( $g );
 501  
 502          wp_send_json_success( $g );
 503      }
 504  
 505  	public static function rgeocoding()
 506      {
 507          $r = MP_Map::get_address( self::$pst_['lng'],  self::$pst_['lat'] );
 508  
 509          if ( empty( $r ) ) wp_send_json_error( $r );
 510  
 511          wp_send_json_success( $r );
 512      }
 513  
 514  ////  MISC  ////
 515  
 516  	public static function download( $file, $file_fullpath, $mime_type, $name = false )
 517      {
 518          $HTTP_USER_AGENT = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT' );
 519  
 520          if ( !$name ) $name = $file;
 521          if ( strstr( $HTTP_USER_AGENT, 'MSIE' ) ) $file = preg_replace( '/\./', '%2e', $file, substr_count( $file, '.' ) - 1 );
 522  
 523          if( !$fdl = @fopen( $file_fullpath, 'r' ) )     MP_::mp_die( __( 'Cannot Open File !', 'MailPress' ) );
 524  
 525          header( "Cache-Control: " );# leave blank to avoid IE errors
 526          header( "Pragma: " );# leave blank to avoid IE errors
 527          header( "Content-type: " . $mime_type );
 528          header( "Content-Disposition: attachment; filename=\"".$file."\"" );
 529          header( "Content-length:".( string )( filesize( $file_fullpath ) ) );
 530          sleep( 1 );
 531          fpassthru( $fdl );
 532          MP_::mp_die();
 533      }
 534  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1