Working with plugin (control panel vs API)

Discussion in 'Developers' Forum' started by gody, Jan 25, 2021.

  1. gody

    gody Member

    Hello Guys,

    I have write several plugins who work perfectly with the ISPConfig control panel but not with the API And I'm not sure how to fix this...

    Here it's a simple demonstration of the trouble:

    1. Add error login to your ispconfig vhost (/etc/apache2/sites-enabled/000-ispconfig.vhost)
      • add / edit the line : ErrorLog /var/log/ispconfig/httpd/YourVHOST.DOMAIN.TLS/error.log
    2. Create the file (/usr/local/ispconfig/interface/lib/plugins/exemple_plugin.inc.php)
      PHP:
      <?php
      [LIST]
      class 
      exemple_plugin {
           var 
      $plugin_name 'exemple_plugin';
           var 
      $class_name 'exemple_plugin';
           function 
      onLoad() {
               global 
      $app;
               
      $app->plugin->registerEvent('mail:mail_user:on_before_insert''exemple_plugin''fonction_edit');
               
      $app->plugin->registerEvent('mail:mail_user:on_before_update''exemple_plugin''fonction_edit');
               
      $app->plugin->registerEvent('mail:mail_user:on_before_delete''exemple_plugin''fonction_del');
           }
           function 
      fonction_edit($event_name$page_form){
              
      error_log('You should see this line in the log when you add / edit an email');
           }
           function 
      fonction_del($event_name$page_form){
               
      error_log('You should see this line in the log when you remove an email');
           }
      }
    3. IMPORTANT : Relog into your ISPConfig control panel
    4. Time to try
      1. Go to Ispconfig;
        • Email > Email Mailbox > Select an existing mailbox > Change something > Save
        • When you do that you should see this inside your logfile [​IMG][​IMG]
      2. When you make the same change by the API, nothing is wrote inside the logfile.
        • That mean the plugin isn't call by the API.
    Any idea how to solve that ?

    Thanks
     
    Steveorevo likes this.
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    Most likely the api function does not fire the event 'mail:mail_user:eek:n_before_insert' then, that's not an issue of your code, more a missing part of the api.
     
  3. Jesse Norell

    Jesse Norell Well-Known Member Staff Member Howtoforge Staff

    The plugin should be called if you use the api to insert, update or delete a mail_user. Ensure the server_id is correct.
     
  4. Jesse Norell

    Jesse Norell Well-Known Member Staff Member Howtoforge Staff

    Oh, yeah, I was thinking 'mail_user_insert'/'mail_user_update'/'mail_user_delete' events.
     
  5. till

    till Super Moderator Staff Member ISPConfig Developer

    I just checked the code and it seems that event support is still not implemented in most API functions. The event fires only when updateQuery and insertQuery has the $event_identifier parameter set as last parameter after $params. Currently, this seems to be only the case for the mail_user_filter* functions. So in general, everything is in place code-wise in the insert and update functions globally, but the event parameter is missing in most functions to make it work.
     
  6. gody

    gody Member

    I guess you are talking about that part in : /usr/local/ispconfig/interface/lib/classes/remote.d/mail.inc.php
    upload_2021-1-25_14-8-17.png
     
  7. till

    till Super Moderator Staff Member ISPConfig Developer

    Yes, that's the parameter that is missing. You must add the same event string that your plugin is using.
     
  8. gody

    gody Member

    Okay very cool.

    But how it's work when I have on_BEFORE_insert and on_AFTER_insert, can we pass an array ?

    I use on_before_insert to do some verification with external system first.
     
  9. till

    till Super Moderator Staff Member ISPConfig Developer

    That#s not possible at the moment. I guess we might have to rework this in the api completely. Pass only e.g. 'mail:mail_user' as identifier parameter and then change the code in updateQuery and insertQuery to raise the on_before_insert and on_after_insert at the right time before and after the sql query. So that's a bit more work, but needs to be done I guess to make the API behavior more compatible with the GUI.
     
  10. gody

    gody Member

    Ho crap ... But yeah this kind of modifications would be great !

    So do I have to repost it somewhere as a feature request ? :p
     
  11. till

    till Super Moderator Staff Member ISPConfig Developer

    Yes, this time the git is the right place :) Sorry for having been so strict, but if we start doing support in git, then more and more users will post support questions there and overload the system.
     
  12. gody

    gody Member

    Another question for you guys :)

    How can I interrupt the processing ?
    For the moment I use exit(), it's work's well with the control pannel but not with the API ...

    PHP:
    <?php
    class exemple_plugin {
         var 
    $plugin_name 'exemple_plugin';
         var 
    $class_name 'exemple_plugin';
         function 
    onLoad() {
             global 
    $app;
             
    $app->plugin->registerEvent('mail:mail_user:on_before_insert''exemple_plugin''fonction_edit');
             
    $app->plugin->registerEvent('mail:mail_user:on_before_update''exemple_plugin''fonction_edit');
             
    $app->plugin->registerEvent('mail:mail_user:on_before_delete''exemple_plugin''fonction_del');
         }
      
        function 
    fonction_edit($event_name$page_form){
           global 
    $app$conf;         
             
              if( 
    $_SESSION["s"]["user"]["typ"] != 'admin'  )
             {
             
               
    $regex_pattern = ['.*other.*','.*test.*']; // TODO check local config file for pattern
             
               
    foreach ($regex_pattern as $pattern)
               {
                   if(
    preg_match("/{$pattern}/"$page_form->dataRecord['email']))
                   {
                       
    error_log("MATCH FOUND ::"$pattern );
                       exit(
    'not allowed');
                   }
               }
             }
       }
         function 
    fonction_del($event_name$page_form){
             
    error_log('You should see this line in the log when you remove an email');
         }
    }
    Maybe you have a better way ?

    Additionnal informaitons:
    • The modification isn't send to the datalog history. So i think the modification isn't propagated to other servers (in multi server environnement)
    • Only the main database is modified by the API that isn't good at all.
    • In my exemple I check for a pattern inside the email, if the pattern match i wan't to stop the email creation and return a message
    Regards,
     
    Last edited: Jan 26, 2021

Share This Page