示例#1
0
 /**
  * Function to check if a username is of the correct format
  *
  * @param   string  $username Wanted username
  * @param   array   $options
  *
  * @return boolean True if username is of the correct format
  * @author Modified by Ivan Tcholakov, 15-SEP-2009.
  * @see HTML_QuickForm_Rule
  * The validation rule is served by the UserManager class as of this moment.
  */
 public function validate($username, $options)
 {
     if (api_get_setting('login_is_email') == 'true') {
         return api_valid_email($username);
     } else {
         return UserManager::is_username_valid($username);
     }
 }
示例#2
0
/**
 * Sends an HTML email using the phpmailer class (and multipart/alternative to downgrade gracefully)
 * Sender name and email can be specified, if not specified
 * name and email of the platform admin are used
 *
 * @author Bert Vanderkimpen ICT&O UGent
 * @author Yannick Warnier <*****@*****.**>
 *
 * @param string    name of recipient
 * @param string    email of recipient
 * @param string    email subject
 * @param string    email body
 * @param string    sender name
 * @param string    sender e-mail
 * @param array     extra headers in form $headers = array($name => $value) to allow parsing
 * @param array     data file (path and filename)
 * @param array     data to attach a file (optional)
 * @param bool      True for attaching a embedded file inside content html (optional)
 * @return          returns true if mail was sent
 * @see             class.phpmailer.php
 */
function api_mail_html($recipient_name, $recipient_email, $subject, $message, $senderName = '', $senderEmail = '', $extra_headers = array(), $data_file = array(), $embedded_image = false, $additionalParameters = array())
{
    global $platform_email;
    $mail = new PHPMailer();
    $mail->Mailer = $platform_email['SMTP_MAILER'];
    $mail->Host = $platform_email['SMTP_HOST'];
    $mail->Port = $platform_email['SMTP_PORT'];
    $mail->CharSet = $platform_email['SMTP_CHARSET'];
    // Stay far below SMTP protocol 980 chars limit.
    $mail->WordWrap = 200;
    if ($platform_email['SMTP_AUTH']) {
        $mail->SMTPAuth = 1;
        $mail->Username = $platform_email['SMTP_USER'];
        $mail->Password = $platform_email['SMTP_PASS'];
    }
    // 5 = low, 1 = high
    $mail->Priority = 3;
    $mail->SMTPKeepAlive = true;
    // Default values
    $notification = new Notification();
    $defaultEmail = $notification->getDefaultPlatformSenderEmail();
    $defaultName = $notification->getDefaultPlatformSenderName();
    // Error to admin.
    $mail->AddCustomHeader('Errors-To: ' . $defaultEmail);
    // If the parameter is set don't use the admin.
    $senderName = !empty($senderName) ? $senderName : $defaultEmail;
    $senderEmail = !empty($senderEmail) ? $senderEmail : $defaultName;
    // Reply to first
    if (isset($extra_headers['reply_to'])) {
        $mail->AddReplyTo($extra_headers['reply_to']['mail'], $extra_headers['reply_to']['name']);
        $mail->Sender = $extra_headers['reply_to']['mail'];
        unset($extra_headers['reply_to']);
    }
    $mail->SetFrom($senderEmail, $senderName);
    $mail->Subject = $subject;
    $mail->AltBody = strip_tags(str_replace('<br />', "\n", api_html_entity_decode($message)));
    // Send embedded image.
    if ($embedded_image) {
        // Get all images html inside content.
        preg_match_all("/<img\\s+.*?src=[\"\\']?([^\"\\' >]*)[\"\\']?[^>]*>/i", $message, $m);
        // Prepare new tag images.
        $new_images_html = array();
        $i = 1;
        if (!empty($m[1])) {
            foreach ($m[1] as $image_path) {
                $real_path = realpath($image_path);
                $filename = basename($image_path);
                $image_cid = $filename . '_' . $i;
                $encoding = 'base64';
                $image_type = mime_content_type($real_path);
                $mail->AddEmbeddedImage($real_path, $image_cid, $filename, $encoding, $image_type);
                $new_images_html[] = '<img src="cid:' . $image_cid . '" />';
                $i++;
            }
        }
        // Replace origin image for new embedded image html.
        $x = 0;
        if (!empty($m[0])) {
            foreach ($m[0] as $orig_img) {
                $message = str_replace($orig_img, $new_images_html[$x], $message);
                $x++;
            }
        }
    }
    $message = str_replace(array("\n\r", "\n", "\r"), '<br />', $message);
    $mail->Body = '<html><head></head><body>' . $message . '</body></html>';
    // Attachment ...
    if (!empty($data_file)) {
        $mail->AddAttachment($data_file['path'], $data_file['filename']);
    }
    // Only valid addresses are accepted.
    if (is_array($recipient_email)) {
        foreach ($recipient_email as $dest) {
            if (api_valid_email($dest)) {
                $mail->AddAddress($dest, $recipient_name);
            }
        }
    } else {
        if (api_valid_email($recipient_email)) {
            $mail->AddAddress($recipient_email, $recipient_name);
        } else {
            return 0;
        }
    }
    if (is_array($extra_headers) && count($extra_headers) > 0) {
        foreach ($extra_headers as $key => $value) {
            switch (strtolower($key)) {
                case 'encoding':
                case 'content-transfer-encoding':
                    $mail->Encoding = $value;
                    break;
                case 'charset':
                    $mail->Charset = $value;
                    break;
                case 'contenttype':
                case 'content-type':
                    $mail->ContentType = $value;
                    break;
                default:
                    $mail->AddCustomHeader($key . ':' . $value);
                    break;
            }
        }
    } else {
        if (!empty($extra_headers)) {
            $mail->AddCustomHeader($extra_headers);
        }
    }
    // WordWrap the html body (phpMailer only fixes AltBody) FS#2988
    $mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap);
    // Send the mail message.
    if (!$mail->Send()) {
        error_log('ERROR: mail not sent to ' . $recipient_name . ' (' . $recipient_email . ') because of ' . $mail->ErrorInfo . '<br />');
        return 0;
    }
    $plugin = new AppPlugin();
    $installedPluginsList = $plugin->getInstalledPluginListObject();
    foreach ($installedPluginsList as $installedPlugin) {
        if ($installedPlugin->isMailPlugin and array_key_exists("smsType", $additionalParameters)) {
            $clockworksmsObject = new Clockworksms();
            $clockworksmsObject->send($additionalParameters);
        }
    }
    // Clear all the addresses.
    $mail->ClearAddresses();
    return 1;
}
示例#3
0
                 // Here we accept absolute URLs only.
                 if (strpos($value, '://') === false) {
                     $value = 'http://' . $value;
                 }
                 if (!api_valid_url($value, true)) {
                     // If the new (non-empty) URL value is invalid, then the old URL value stays.
                     $value = $old_value;
                 }
             }
             // If the new URL value is empty, then it will be stored (i.e. the setting will be deleted).
             break;
             // Validation against e-mail address for some settings.
         // Validation against e-mail address for some settings.
         case 'emailAdministrator':
             $value = trim(Security::remove_XSS($value));
             if ($value != '' && !api_valid_email($value)) {
                 // If the new (non-empty) e-mail address is invalid, then the old e-mail address stays.
                 // If the new e-mail address is empty, then it will be stored (i.e. the setting will be deleted).
                 $value = $old_value;
             }
             break;
     }
     if ($old_value != $value) {
         $keys[] = $key;
     }
     $result = api_set_setting($key, $value, null, null, $url_id);
 } else {
     $sql = "SELECT subkey FROM {$table_settings_current} WHERE variable = '{$key}'";
     $res = Database::query($sql);
     while ($row_subkeys = Database::fetch_array($res)) {
         // If subkey is changed:
示例#4
0
/**
 * Sends an HTML email using the phpmailer class (and multipart/alternative to downgrade gracefully)
 * Sender name and email can be specified, if not specified
 * name and email of the platform admin are used
 *
 * @author Bert Vanderkimpen ICT&O UGent
 * @author Yannick Warnier <*****@*****.**>
 *
 * @param string    name of recipient
 * @param string    email of recipient
 * @param string    email subject
 * @param string    email body
 * @param string    sender name
 * @param string    sender e-mail
 * @param array     extra headers in form $headers = array($name => $value) to allow parsing
 * @param array     data file (path and filename)
 * @param array     data to attach a file (optional)
 * @param bool      True for attaching a embedded file inside content html (optional)
 * @return          returns true if mail was sent
 * @see             class.phpmailer.php
 */
function api_mail_html($recipient_name, $recipient_email, $subject, $message, $senderName = '', $senderEmail = '', $extra_headers = array(), $data_file = array(), $embedded_image = false, $additionalParameters = array())
{
    // Default values
    $notification = new Notification();
    $defaultEmail = $notification->getDefaultPlatformSenderEmail();
    $defaultName = $notification->getDefaultPlatformSenderName();
    // If the parameter is set don't use the admin.
    $senderName = !empty($senderName) ? $senderName : $defaultName;
    $senderEmail = !empty($senderEmail) ? $senderEmail : $defaultEmail;
    $link = isset($additionalParameters['link']) ? $additionalParameters['link'] : '';
    $swiftMessage = \Swift_Message::newInstance()->setSubject($subject)->setFrom($senderEmail, $senderName)->setTo($recipient_email, $recipient_name)->setBody(Container::getTemplating()->render('ChamiloCoreBundle:default/mail:mail.html.twig', array('content' => $message, 'link' => $link)), 'text/html');
    if (!empty($additionalParameters)) {
        $plugin = new AppPlugin();
        $smsPlugin = $plugin->getSMSPluginLibrary();
        if ($smsPlugin) {
            $smsPlugin->send($additionalParameters);
        }
    }
    Container::getMailer()->send($swiftMessage);
    return 1;
    global $platform_email;
    $mail = new PHPMailer();
    $mail->Mailer = $platform_email['SMTP_MAILER'];
    $mail->Host = $platform_email['SMTP_HOST'];
    $mail->Port = $platform_email['SMTP_PORT'];
    $mail->CharSet = $platform_email['SMTP_CHARSET'];
    // Stay far below SMTP protocol 980 chars limit.
    $mail->WordWrap = 200;
    if ($platform_email['SMTP_AUTH']) {
        $mail->SMTPAuth = 1;
        $mail->Username = $platform_email['SMTP_USER'];
        $mail->Password = $platform_email['SMTP_PASS'];
    }
    // 5 = low, 1 = high
    $mail->Priority = 3;
    $mail->SMTPKeepAlive = true;
    // Default values
    $notification = new Notification();
    $defaultEmail = $notification->getDefaultPlatformSenderEmail();
    $defaultName = $notification->getDefaultPlatformSenderName();
    // Error to admin.
    $mail->AddCustomHeader('Errors-To: ' . $defaultEmail);
    // If the parameter is set don't use the admin.
    $senderName = !empty($senderName) ? $senderName : $defaultName;
    $senderEmail = !empty($senderEmail) ? $senderEmail : $defaultEmail;
    // Reply to first
    if (isset($extra_headers['reply_to'])) {
        $mail->AddReplyTo($extra_headers['reply_to']['mail'], $extra_headers['reply_to']['name']);
        $mail->Sender = $extra_headers['reply_to']['mail'];
        unset($extra_headers['reply_to']);
    }
    //If the SMTP configuration only accept one sender
    if ($platform_email['SMTP_UNIQUE_SENDER']) {
        $senderName = $platform_email['SMTP_FROM_NAME'];
        $senderEmail = $platform_email['SMTP_FROM_EMAIL'];
    }
    $mail->SetFrom($senderEmail, $senderName);
    $mail->Subject = $subject;
    $mail->AltBody = strip_tags(str_replace('<br />', "\n", api_html_entity_decode($message)));
    // Send embedded image.
    if ($embedded_image) {
        // Get all images html inside content.
        preg_match_all("/<img\\s+.*?src=[\"\\']?([^\"\\' >]*)[\"\\']?[^>]*>/i", $message, $m);
        // Prepare new tag images.
        $new_images_html = array();
        $i = 1;
        if (!empty($m[1])) {
            foreach ($m[1] as $image_path) {
                $real_path = realpath($image_path);
                $filename = basename($image_path);
                $image_cid = $filename . '_' . $i;
                $encoding = 'base64';
                $image_type = mime_content_type($real_path);
                $mail->AddEmbeddedImage($real_path, $image_cid, $filename, $encoding, $image_type);
                $new_images_html[] = '<img src="cid:' . $image_cid . '" />';
                $i++;
            }
        }
        // Replace origin image for new embedded image html.
        $x = 0;
        if (!empty($m[0])) {
            foreach ($m[0] as $orig_img) {
                $message = str_replace($orig_img, $new_images_html[$x], $message);
                $x++;
            }
        }
    }
    $message = str_replace(array("\n\r", "\n", "\r"), '<br />', $message);
    $mailView = new Template(null, false, false, false, false, false, false);
    $mailView->assign('content', $message);
    $link = $additionalParameters['link'];
    $mailView->assign('link', $link);
    $layout = $mailView->get_template('mail/mail.tpl');
    $mail->Body = $mailView->fetch($layout);
    // Attachment ...
    if (!empty($data_file)) {
        $mail->AddAttachment($data_file['path'], $data_file['filename']);
    }
    // Only valid addresses are accepted.
    if (is_array($recipient_email)) {
        foreach ($recipient_email as $dest) {
            if (api_valid_email($dest)) {
                $mail->AddAddress($dest, $recipient_name);
            }
        }
    } else {
        if (api_valid_email($recipient_email)) {
            $mail->AddAddress($recipient_email, $recipient_name);
        } else {
            return 0;
        }
    }
    if (is_array($extra_headers) && count($extra_headers) > 0) {
        foreach ($extra_headers as $key => $value) {
            switch (strtolower($key)) {
                case 'encoding':
                case 'content-transfer-encoding':
                    $mail->Encoding = $value;
                    break;
                case 'charset':
                    $mail->Charset = $value;
                    break;
                case 'contenttype':
                case 'content-type':
                    $mail->ContentType = $value;
                    break;
                default:
                    $mail->AddCustomHeader($key . ':' . $value);
                    break;
            }
        }
    } else {
        if (!empty($extra_headers)) {
            $mail->AddCustomHeader($extra_headers);
        }
    }
    // WordWrap the html body (phpMailer only fixes AltBody) FS#2988
    $mail->Body = $mail->WrapText($mail->Body, $mail->WordWrap);
    // Send the mail message.
    if (!$mail->Send()) {
        error_log('ERROR: mail not sent to ' . $recipient_name . ' (' . $recipient_email . ') because of ' . $mail->ErrorInfo . '<br />');
        return 0;
    }
    if (!empty($additionalParameters)) {
        $plugin = new AppPlugin();
        $smsPlugin = $plugin->getSMSPluginLibrary();
        if ($smsPlugin) {
            $smsPlugin->send($additionalParameters);
        }
    }
    // Clear all the addresses.
    $mail->ClearAddresses();
    return 1;
}
 /**
  * Simpler version of create_user(). Doesn't send an e-mail and doesn't manage extra
  * fields, between other things
  * @param array Array of user details (array('status'=>...,'username'=>..., ...))
  * @return mixed Array of user information
  */
 public static function add($params)
 {
     global $_configuration;
     $access_url_id = 1;
     if (api_get_multiple_access_url()) {
         $access_url_id = api_get_current_access_url_id();
     }
     // Hosting verifications
     $status = isset($params['status']) ? $params['status'] : STUDENT;
     if (api_get_setting('login_is_email') == 'true') {
         $params['username'] = $params['email'];
     }
     if (is_array($_configuration[$access_url_id]) && isset($_configuration[$access_url_id]['hosting_limit_users']) && $_configuration[$access_url_id]['hosting_limit_users'] > 0) {
         $num = self::get_number_of_users();
         if ($num >= $_configuration[$access_url_id]['hosting_limit_users']) {
             return api_set_failure('portal users limit reached');
         }
     }
     if ($status === 1 && is_array($_configuration[$access_url_id]) && isset($_configuration[$access_url_id]['hosting_limit_teachers']) && $_configuration[$access_url_id]['hosting_limit_teachers'] > 0) {
         $num = self::get_number_of_users(1);
         if ($num >= $_configuration[$access_url_id]['hosting_limit_teachers']) {
             return api_set_failure('portal teachers limit reached');
         }
     }
     $params['email'] = api_valid_email($params['email']) ? $params['email'] : null;
     if (isset($params['user_id'])) {
         unset($params['user_id']);
     }
     if (empty($params['username'])) {
         return api_set_failure('provide a username');
     }
     $params['username'] = self::purify_username($params['username']);
     // First check wether the login already exists
     if (!self::is_username_available($params['username'])) {
         //Already added it
         if (isset($params['return_item_if_already_exists']) && $params['return_item_if_already_exists']) {
             $user_info = self::get_user_info_simple($params['username']);
             return $user_info;
         }
         return api_set_failure('login-pass already taken');
     }
     unset($params['return_item_if_already_exists']);
     //Checking the user language
     $languages = api_get_languages();
     if (!isset($params['language']) || !in_array($params['language'], $languages['folder'])) {
         $params['language'] = api_get_setting('platformLanguage');
     }
     if (!isset($params['creator_id'])) {
         $params['creator_id'] = api_get_user_id();
     }
     if (empty($params['encrypt_method'])) {
         $params['password'] = api_get_encrypted_password($params['password']);
     } else {
         if ($_configuration['password_encryption'] === $params['encrypt_method']) {
             if ($params['encrypt_method'] == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $params['password'])) {
                 return api_set_failure('encrypt_method invalid');
             } else {
                 if ($params['encrypt_method'] == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $params['password'])) {
                     return api_set_failure('encrypt_method invalid');
                 }
             }
         } else {
             return api_set_failure('encrypt_method invalid');
         }
     }
     $params['registration_date'] = api_get_utc_datetime();
     // Database table definition
     $table = Database::get_main_table(TABLE_MAIN_USER);
     $clean_params = self::clean_params($params);
     $user_id = Database::insert($table, $clean_params);
     if ($user_id) {
         if (api_get_multiple_access_url()) {
             UrlManager::add_user_to_url($user_id, api_get_current_access_url_id());
         } else {
             //we are adding by default the access_url_user table with access_url_id = 1
             UrlManager::add_user_to_url($user_id, 1);
         }
         //saving extra fields
         $field_value = new ExtraFieldValue('user');
         $params['user_id'] = $user_id;
         $field_value->save_field_values($params);
         // Add event to system log
         $user_id_manager = api_get_user_id();
         $user_info = api_get_user_info($user_id);
         event_system(LOG_USER_CREATE, LOG_USER_ID, $user_id, api_get_utc_datetime(), $user_id_manager);
         event_system(LOG_USER_CREATE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), $user_id_manager);
         return $user_info;
     } else {
         return api_set_failure('error inserting in Database');
     }
 }
示例#6
0
/**
 * @deprecated 25-JAN-2010: See api_mail() and api_mail_html(), mail.lib.inc.php
 *
 * Send an email.
 *
 * Wrapper function for the standard php mail() function. Change this function
 * to your needs. The parameters must follow the same rules as the standard php
 * mail() function. Please look at the documentation on http://php.net/manual/en/function.mail.php
 * @param string $to
 * @param string $subject
 * @param string $message
 * @param string $additional_headers
 * @param string $additionalParameters
 * @author Ivan Tcholakov, 04-OCT-2009, a reworked version of this function.
 * @link http://www.dokeos.com/forum/viewtopic.php?t=15557
 */
function api_send_mail($to, $subject, $message, $additional_headers = null, $additionalParameters = array())
{
    require_once api_get_path(LIBRARY_PATH) . 'phpmailer/class.phpmailer.php';
    if (empty($platform_email['SMTP_FROM_NAME'])) {
        $platform_email['SMTP_FROM_NAME'] = api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS);
    }
    if (empty($platform_email['SMTP_FROM_EMAIL'])) {
        $platform_email['SMTP_FROM_EMAIL'] = api_get_setting('emailAdministrator');
    }
    $matches = array();
    if (preg_match('/([^<]*)<(.+)>/si', $to, $matches)) {
        $recipient_name = trim($matches[1]);
        $recipient_email = trim($matches[2]);
    } else {
        $recipient_name = '';
        $recipient_email = trim($to);
    }
    $sender_name = '';
    $sender_email = '';
    $extra_headers = $additional_headers;
    // Regular expression to test for valid email address.
    // This should actually be revised to use the complete RFC3696 description.
    // http://tools.ietf.org/html/rfc3696#section-3
    //$regexp = "^[0-9a-z_\.+-]+@(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2,3})$"; // Deprecated, 13-OCT-2010.
    $mail = new PHPMailer();
    $mail->CharSet = $platform_email['SMTP_CHARSET'];
    $mail->Mailer = $platform_email['SMTP_MAILER'];
    $mail->Host = $platform_email['SMTP_HOST'];
    $mail->Port = $platform_email['SMTP_PORT'];
    if ($platform_email['SMTP_AUTH']) {
        $mail->SMTPAuth = 1;
        $mail->Username = $platform_email['SMTP_USER'];
        $mail->Password = $platform_email['SMTP_PASS'];
    }
    $mail->Priority = 3;
    // 5 = low, 1 = high
    $mail->AddCustomHeader('Errors-To: ' . $platform_email['SMTP_FROM_EMAIL']);
    $mail->IsHTML(0);
    $mail->SMTPKeepAlive = true;
    // Attachments.
    // $mail->AddAttachment($path);
    // $mail->AddAttachment($path, $filename);
    if ($sender_email != '') {
        $mail->From = $sender_email;
        $mail->Sender = $sender_email;
    } else {
        $mail->From = $platform_email['SMTP_FROM_EMAIL'];
        $mail->Sender = $platform_email['SMTP_FROM_EMAIL'];
    }
    if ($sender_name != '') {
        $mail->FromName = $sender_name;
    } else {
        $mail->FromName = $platform_email['SMTP_FROM_NAME'];
    }
    $mail->Subject = $subject;
    $mail->Body = $message;
    // Only valid address are to be accepted.
    if (api_valid_email($recipient_email)) {
        $mail->AddAddress($recipient_email, $recipient_name);
    }
    if ($extra_headers != '') {
        $mail->AddCustomHeader($extra_headers);
    }
    // Send mail.
    if (!$mail->Send()) {
        return 0;
    }
    $plugin = new AppPlugin();
    $installedPluginsList = $plugin->getInstalledPluginListObject();
    foreach ($installedPluginsList as $installedPlugin) {
        if ($installedPlugin->isMailPlugin and array_key_exists("smsType", $additionalParameters)) {
            $clockworksmsObject = new Clockworksms();
            $clockworksmsObject->send($additionalParameters);
        }
    }
    // Clear all the addresses.
    $mail->ClearAddresses();
    return 1;
}