2009-08-05 2 views
0

Drupal 6.1.3의 hook_cron 구현에 문제가 있습니다.Drupal의 profile_save_profile이 hook_cron에서 작동하지 않습니다. 서버의 cron에 의해 실행될 때

아래 스크립트는 예상대로 실행됩니다. 새 구성원에게 환영 문자를 보내고 프로필의 숨겨진 필드를 업데이트하여 문자가 보내 졌음을 나타냅니다. 편지에 오류가 없으며 모든 신입 회원이 차지하고 있습니다.

문제는 마지막 라인 - 프로필 업데이트가 작동하지 않는 것처럼 보입니다. Drupal cron이 'real '서버의 cron.

수동으로 (예 : /admin/reports/status/run-cron을 통해) cron을 실행하면 프로필 필드가 예상대로 업데이트됩니다.

무엇이 발생했는지에 대한 제안 사항이 있으십니까?

(누군가를 제안하기 때문에, 참고 : 회원 드루팔 이외의 방법으로 참여하고, 야간 드루팔에 업로드 있도록하는 드루팔에 내장 된 환영 편지 (내 생각) 작동하지 않습니다.)

<?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, '[email protected]'); 
      // 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"); 
     } 
    } 
} 

답변

2

안전하게 전환하려면 사용자 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 
?> 

: 대답에 대한 드루팔 도트 조직이/노드/218,104

+0

완벽 ... 고마워요! – anschauung

+0

감사! 나에게는 같은 질문이있다. – farzan

2

임의의 추측은 아니지만 닫습니다 ...

"실제"cron이 코드를 실행하면 특정 사용자로 실행됩니다.

마찬가지로 Drupal cron 코드를 수동으로 실행하면 해당 코드가 특정 사용자로 실행됩니다.

두 명의 사용자가 서로 다른 권한을 가지고 있으며 그 원인이 오류의 원인입니다.

cron 작업의 사용자는 데이터베이스에 쓸 수있는 권한이 있거나 읽기 전용입니까?

cron 작업에서 생성 된 로그 파일이 있습니까?

업데이트 : : 위의 '사용자'는 호스트 서버의 사용자 계정이며 Drupal 계정은 아닙니다. 예를 들어, Drupal 변경 사항을 테스트하는 데 사용하는 샌드 박스 시스템에서 Apache는 noone 계정으로 실행됩니다.

+0

감사합니다 - 데이터베이스 권한은 문제가되지 않습니다,하지만 난 것으로 의심 그것들은 해결책의 일부일 수 있습니다[email protected]이 인용 한 한계를 극복하는 방법에 대한 또 다른 질문을 게시하고 있습니다. – anschauung

2

그래 난 .. 보안의 용어에서 아주 좋은되지 않습니다 "익명"사용자에 대한 권한 드 관리자 사용자를 추가 할 수 있도록 드루팔 크론 사용자 프로필 "익명"입니다 dosn't

+0

오, 이런. 나는 당신의 대답에 따라 인터넷 검색을 한 것처럼 보였습니다. 그리고 그것은 그것을 설명하는 것 같습니다. 통찰력에 감사드립니다. 이 문제를 해결하는 가장 좋은 방법은 다른 질문입니다. – anschauung

0

profile_save_profile 확인 사용 권한을 확인하거나 내가 볼 수있는 한 'global $ user'를 실행하면 실제 문제인지 여부를 알 수 없습니다.

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

이 (당신이 Drupal7을 사용하지 않는 경우) user_load가 배열이 아닌에게 UID를해야 잘못 보이지만, 문제가 해결되지 않은 경우 코드의 나머지 부분도 실패합니다. 여기에서 촬영

+1

user_load는 사용자의 UID를 이미 알고있는 경우 배열 대신 UID를 사용할 수 있습니다. 이 배열은 UID에 쉽게 액세스 할 수없는 경우 다른 방법으로 사용자를 찾는 데 사용됩니다. 참고로, user_load의 소스에 6 번 줄을 입력하십시오 : if (is_numeric ($ array)) {$ array = array ('uid'=> $ array);} – anschauung

+0

코드가 아닌 문서를 보도록 가르쳐줍니다. –

관련 문제