name = Yandex PDD description = Yandex PDD mailboxes autocreation. core = 7.x dependencies[] = transliteration
function yandex_pdd_schema() { $schema['yandex_pdd'] = array( 'fields' => array( 'id' => array('type' => 'serial', 'not null' => TRUE), // 'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0,), // 'login' => array('type' => 'varchar', 'length' => 100, 'not null' => TRUE), // 'activated' => array('type' => 'varchar', 'length' => 1, 'not null' => TRUE) // ), 'primary key' => array('id'), ); return $schema; }
function yandex_pdd_uninstall() { variable_del('yandex_pdd_domain'); variable_del('yandex_pdd_authtoken'); cache_clear_all('yandex_pdd','cache',TRUE); menu_rebuild(); }
function yandex_pdd_menu(){ $items = array(); $items['admin/config/content/yandex_pdd'] = array( // - 'title' => t('Yandex PDD'), // <title> 'page callback' => 'main_config', // , 'type' => MENU_NORMAL_ITEM, // CMS 'access callback' => TRUE, // ); $items['mailbox'] = array( 'title' => 'Yandex PDD login', 'page callback' => 'mailbox_login', 'type' => MENU_CALLBACK, 'access callback' => TRUE, ); return $items; }
function main_config(){ $form = drupal_get_form('pdd_config_form'); // $form = drupal_render($form); // return $form; }
function pdd_config_form($form, &$form_state){ $form=array(); $form['pdd_domain'] = array( // - '#type' => 'textfield', // '#title' => t('Domain zone'), // , '#description' => t('A domain zone in which email should be created.'), // '#default_value' => variable_get('yandex_pdd_domain'), // '#required' => 1, // ); $form['authtoken'] = array( '#type' => 'textfield', '#title' => t('Auth Token'), '#description' => t('Authorization token obtained at Yandex.PDD service.'), '#default_value' => variable_get('yandex_pdd_authtoken'), '#required' => 1, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Submit'), ); return $form; }
function pdd_config_form_submit($form, &$form_state){ variable_set('yandex_pdd_domain', $form_state['values']['pdd_domain']); variable_set('yandex_pdd_authtoken', $form_state['values']['authtoken']); }
function yandex_pdd_help($path, $arg) { switch ($path) { case "admin/help#yandex_pdd": return '<p>'. t("Yandex PDD mailboxes management module.") .'</p>'; break; } }
function yandex_pdd_form_alter(&$form, &$form_state, $form_id){ if($form_id=='user_register_form'){ // $form['account']['createmail']=array( // '#type' => 'checkbox', '#title' => t('Create a mailbox'), '#description' => t('Check this box if you want to create a mailbox @'.variable_get('yandex_pdd_domain').'.'), '#required' => 0, '#access' => 1, // '#weight' => 10 // ( ) ); } }
function yandex_pdd_user_insert(&$edit, $account, $category){ if($account->createmail){ $transliterated = transliteration_get($account->name, '_'); // $pattern = '/[^a-zA-Z0-9]/'; // , $transliterated = preg_replace($pattern, '-', $transliterated); // alphanumeric $newbox = db_insert('yandex_pdd'); // $newbox->fields(array('uid' => $account->uid, 'login' => strtolower($transliterated), 'activated' => '0')); // $res = $newbox->execute(); // watchdog('yandex_pdd',print_r('Res: '.$res,1)); // CMS } }
function yandex_pdd_field_attach_submit($entity_type, $entity, $form, &$form_state) { global $user; if($entity_type == 'user' and $user->uid > 0) { // user $select = db_select('yandex_pdd','ypdd'); $select->addField('ypdd', 'id'); $select->addField('ypdd', 'login'); $select->addField('ypdd', 'activated'); $select->condition('uid', $user->uid); $entries = $select->execute()->fetchAssoc(); // if (array_key_exists('login', $entries) and $entries['login'] != '' and $entries['activated'] == 0) { // $mailboxcreate = simplexml_load_file('https://pddimp.yandex.ru/reg_user_token.xml?token='.variable_get('yandex_pdd_authtoken').'&u_login='.$entries['login'].'&u_password='.$form["#user"]->pass); // XML- if ($mailboxcreate->ok[0]) { // $num_updated = db_update('yandex_pdd'); $num_updated->fields(array('activated' => '1')); $num_updated->condition('uid', $user->uid); $res = $num_updated->execute(); // } elseif ($mailboxcreate->error[0]) { // API foreach($mailboxcreate->error[0]->attributes() as $key => $value) { $mbc[$key] = (string)$value; } watchdog('yandex_pdd',"Can't create new mailbox. Reason: ".$mbc['reason']); // } else { // watchdog('yandex_pdd','Unknown error while creating mailbox.'); // } } } }
function yandex_pdd_block_info() { $blocks['mailbox_status'] = array( // 'info' => t('Mailbox status'), // 'cache' => DRUPAL_CACHE_PER_ROLE, // ); return $blocks; }
function yandex_pdd_theme() { return array( 'yandex_pdd_block' => array( // 'variables' => array( // 'newmail' => NULL ), 'template' => 'yandex-pdd-block', // ) ); }
<div class="yandexpdd"><?php print t('You have ').'<a href="/mailbox" target="_blank">'.$newmail.t(' new messages').'</a>.'; ?></div>
function yandex_pdd_block_view($delta = '') { global $user; if ($user->uid > 0) { // , $select = db_select('yandex_pdd','ypdd'); $select->addField('ypdd', 'login'); $select->condition('uid', $user->uid); $entries = $select->execute()->fetchAssoc(); // $unreadmailxml = simplexml_load_file('https://pddimp.yandex.ru/get_mail_info.xml?token='.variable_get('yandex_pdd_authtoken').'&login='.$entries['login']); // if ($unreadmailxml->ok[0]) { // foreach($unreadmailxml->ok[0]->attributes() as $key => $value) { // $unreadmail[$key] = (string)$value; } $blocks = array(); $blocks['subject'] = null; $blocks['content'] = theme('yandex_pdd_block', array('newmail' => $unreadmail['new_messages'])); return $blocks; // } elseif ($unreadmailxml->error[0]) { foreach($unreadmailxml->error[0]->attributes() as $key => $value) { $unreadmail[$key] = (string)$value; } watchdog('yandex_pdd',"Can't get new mail info. Reason: ".$unreadmail['reason']); } else { watchdog('yandex_pdd','Unknown error while loading new mail info'); } } }
function mailbox_login(){ global $user; global $base_url; if ($user->uid > 0) { $select = db_select('yandex_pdd','ypdd'); $select->addField('ypdd', 'login'); $select->condition('uid', $user->uid); $entries = $select->execute()->fetchAssoc(); $tokenxml = simplexml_load_file('https://pddimp.yandex.ru/api/user_oauth_token.xml?token='.variable_get('yandex_pdd_authtoken').'&domain='.variable_get('yandex_pdd_domain').'&login='.$entries['login']); // if ($tokenxml->xpath('status/success')) { $tokenarr = $tokenxml->xpath('domains/domain/email/oauth-token'); header('Location: http://passport.yandex.ru/passport?mode=oauth&type=trusted-pdd-partner&error_retpath='.urlencode($base_url.'/').'&access_token='.(string)$tokenarr[0]); // . drupal_exit(); // CMS } elseif ($tokenxml->xpath('status/error')) { watchdog('yandex_pdd',"Can't get short-term auth token info. Reason: ".(string)$tokenxml->xpath('action/status/error')); } } }
Source: https://habr.com/ru/post/245117/
All Articles