代码之家  ›  专栏  ›  技术社区  ›  anschauung

当由服务器的cron运行时,Drupal的profile\u save\u profile在hook\u cron中不起作用

  •  0
  • anschauung  · 技术社区  · 15 年前

    我对以下的实现有问题 hook_cron 在Drupal 6.1.3中。

    问题在于,当服务器上的“真实”cron调用Drupal cron时,最后一行——更新概要文件——似乎不起作用。

    当我手动运行cron时(例如通过 /admin/reports/status/run-cron

    有什么建议可以解释是什么原因造成的吗?

    (注意,因为有人会建议:成员通过Drupal之外的方式加入,并且每晚都会上传到Drupal,所以Drupal内置的欢迎信不会起作用(我认为)。)

    <?php
    function foo_cron() {
        // Find users who have not received the new member letter, 
        // and send them a welcome email
    
        // Get users who have not recd a message, as per the profile value setting
        $pending_count_sql = "SELECT COUNT(*) FROM {profile_values} v 
         WHERE (v.value = 0) AND (v.fid = 7)"; //fid 7 is the profile field for profile_intro_email_sent
        if (db_result(db_query($pending_count_sql))) { 
            // Load the message template, since we 
            // know we have users to feed into it.
            $email_template_file    =   "/home/foo/public_html/drupal/" . 
                                        drupal_get_path('module', 'foo') . 
                                        "/emails/foo-new-member-email-template.txt";
            $email_template_data    = file_get_contents($email_template_file);
            fclose($email_template_fh);
            //We'll just pull the uid, since we have to run user_load anyway
            $query = "SELECT v.uid FROM {profile_values} v 
            WHERE (v.value = 0) AND (v.fid = 7)";    
            $result = db_query(($query));
            // Loop through the uids, loading profiles so as to access string replacement variables
            while ($item = db_fetch_object($result)) {
                $new_member = user_load($item->uid);
                $translation_key = array(
                    // ... code that generates the string replacement array ...
                    );
                // Compose the email component of the message, and send to user
                $email_text = t($email_template_data, $translation_key);
                $language = user_preferred_language($new_member);  // use member's language preference
                $params['subject'] = 'New Member Benefits - Welcome to FOO!';
                $params['content-type'] = 'text/plain; charset=UTF-8; format=flowed;';
                $params['content'] = $email_text;
                drupal_mail('foo', 'welcome_letter', $new_member->mail, $language, $params, 'webmaster@foo.org');
                // Mark the user's profile to indicate that the message was sent
                $change = array(
                    // Rebuild all of the profile fields in this category, 
                    // since they'll be deleted otherwise
                    'profile_first_name' => $new_member->profile_first_name,
                    'profile_last_name' => $new_member->profile_last_name,
                    'profile_intro_email_sent' => 1);
                profile_save_profile($change, $new_member, "Membership Data");
            }
        }
    }
    
    4 回复  |  直到 15 年前
        1
  •  2
  •   Houssem    15 年前

    是的,我确认drupal cron用户配置文件为“匿名”,因此您必须为“匿名”用户添加权限de manager用户,这在安全性方面不是很好。。

        2
  •  2
  •   Bevan    15 年前

    不完全是随机猜测。。。但是很接近。。。

    当“真正的”cron运行代码时,它以特定用户的身份运行代码。

    我怀疑这两个用户是不同的,拥有不同的权限,这就是导致失败的原因。

    cron作业的用户有权写入数据库还是只读?

    使现代化 :通过上面的“user”,我指的是主机服务器上的用户帐户,而不是Drupal帐户。例如,在我用于测试Drupal更改的沙盒系统上,Apache在 noone

        3
  •  2
  •   jethro    15 年前

    安全地切换用户 http://api.drupal.org/api/function/session_save_session/6

    <?php
    global $user;
    $original_user = $user;
    session_save_session(FALSE); // D7: use drupal_save_session(FALSE);
    $user = user_load(array('uid' => 1)); // D7: use user_load(1);
    
    // Take your action here where you pretend to be the user with UID = 1 (typically the admin user on a site)
    // If your code fails, it's not a problem because the session will not be saved
    $user = $original_user;
    session_save_session(TRUE); // // D7: use drupal_save_session(TRUE);
    
    // From here on the $user is back to normal so it's OK for the session to be saved
    ?>
    

    摘自这里: drupal dot org/node/218104

        4
  •  0
  •   Jeremy French    15 年前

    profile_save_profile 没有检查权限,或者在我看来是“全局$user”,所以我不知道这是否是真正的问题。

     $new_member = user_load($item->uid);
    

    这看起来不对 user_load 应该采用数组而不是uid(除非您使用的是Drupal7),但如果这不起作用,其余代码也会失败。