Subversion Repositories akismet

[/] [trunk/] [extension/] [akismet/] [eventtypes/] [event/] [ezakismet/] [ezakismettype.php] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1 cyberwolf
<?php
2
 
3
include_once( 'kernel/classes/collaborationhandlers/ezapprove/ezapprovecollaborationhandler.php' );
4
include_once( 'kernel/classes/workflowtypes/event/ezapprove/ezapprovetype.php' );
5 11 Cyberwolf
include_once( 'kernel/classes/ezaudit.php' );
6 1 cyberwolf
 
7
define( "EZ_WORKFLOW_TYPE_AKISMET_ID", "ezakismet" );
8
 
9
/*!
10
 The eZAkismetType workflow event type will check wether a content object version is recognized as spam by using
11
 the Akismet web service. If the test is affirmitive, the object will have to be approved before it is published.
12
 The default approve collaboration handler is used. If the approver approves the content, the content object will be
13
 reported as a false positive to the Akismet web service.
14
*/
15
class eZAkismetType extends eZApproveType
16
{
17
    function eZAkismetType()
18
    {
19
        $this->eZWorkflowEventType( EZ_WORKFLOW_TYPE_AKISMET_ID, ezi18n( 'kernel/workflow/event', "Akismet spam test" ) );
20
        $this->setTriggerTypes( array( 'content' => array( 'publish' => array( 'before' ) ) ) );
21
    }
22
 
23
    function execute( &$process, &$event )
24
    {
25
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $process, 'eZAkismetType::execute' );
26
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'eZAkismetType::execute' );
27
        $parameters = $process->attribute( 'parameter_list' );
28
        $versionID =& $parameters['version'];
29
        $object = eZContentObject::fetch( $parameters['object_id'] );
30
 
31
        if ( !$object )
32
        {
33
            eZDebugSetting::writeError( 'kernel-workflow-approve', $parameters['object_id'], 'eZAkismetType::execute' );
34
            return EZ_WORKFLOW_TYPE_STATUS_WORKFLOW_CANCELLED;
35
        }
36
 
37
        // version option checking
38
        $version_option = $event->attribute( 'version_option' );
39
        if ( ( $version_option == EZ_APPROVE_VERSION_OPTION_FIRST_ONLY and $parameters['version'] > 1 ) or
40
             ( $version_option == EZ_APPROVE_VERSION_OPTION_EXCEPT_FIRST and $parameters['version'] == 1 ) )
41
        {
42
            return EZ_WORKFLOW_TYPE_STATUS_ACCEPTED;
43
        }
44
 
45
        /*
46
          If we run event first time ( when we click publish in admin ) we do not have user_id set in workflow process,
47
          so we take current user and store it in workflow process, so next time when we run event from cronjob we fetch
48
          user_id from there.
49
         */
50
        if ( $process->attribute( 'user_id' ) == 0 )
51
        {
52
            $user = eZUser::currentUser();
53
            $process->setAttribute( 'user_id', $user->id() );
54
        }
55
        else
56
        {
57
            $user = eZUser::instance( $process->attribute( 'user_id' ) );
58
        }
59
 
60
        $userGroups = array_merge( $user->attribute( 'groups' ), array( $user->attribute( 'contentobject_id' ) ) );
61
        $workflowSections = explode( ',', $event->attribute( 'data_text1' ) );
62 12 Cyberwolf
        $workflowGroups = $event->attribute( 'data_text2' ) == '' ? array() : explode( ',', $event->attribute( 'data_text2' ) );
63
        $editors =        $event->attribute( 'data_text3' ) == '' ? array() : explode( ',', $event->attribute( 'data_text3' ) );
64
        $approveGroups =  $event->attribute( 'data_text4' ) == '' ? array() : explode( ',', $event->attribute( 'data_text4' ) );
65 1 cyberwolf
        $languageMask = $event->attribute( 'data_int2' );
66
 
67
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $user, 'eZAkismetType::execute::user' );
68
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $userGroups, 'eZAkismetType::execute::userGroups' );
69
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $editors, 'eZAkismetType::execute::editor' );
70
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowSections, 'eZAkismetType::execute::workflowSections' );
71
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowGroups, 'eZAkismetType::execute::workflowGroups' );
72
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $languageMask, 'eZAkismetType::execute::languageMask' );
73
        eZDebugSetting::writeDebug( 'kernel-workflow-approve', $object->attribute( 'section_id'), 'eZAkismetType::execute::section_id' );
74
 
75
        $section = $object->attribute( 'section_id' );
76
        $correctSection = false;
77
 
78
        if ( !in_array( $section, $workflowSections ) && !in_array( -1, $workflowSections ) )
79
        {
80
            $assignedNodes = $object->attribute( 'assigned_nodes' );
81
            if ( $assignedNodes )
82
            {
83
                foreach( $assignedNodes as $assignedNode )
84
                {
85
                    $parent = $assignedNode->attribute( 'parent' );
86
                    $parentObject = $parent->object();
87
                    $section = $parentObject->attribute( 'section_id');
88
 
89
                    if ( in_array( $section, $workflowSections ) )
90
                    {
91
                        $correctSection = true;
92
                        break;
93
                    }
94
                }
95
            }
96
        }
97
        else
98
            $correctSection = true;
99
 
100
        $inExcludeGroups = count( array_intersect( $userGroups, $workflowGroups ) ) != 0;
101
 
102
        $userIsEditor = ( in_array( $user->id(), $editors ) ||
103
                          count( array_intersect( $userGroups, $approveGroups ) ) != 0 );
104
 
105
        // All languages match by default
106
        $hasLanguageMatch = true;
107
        if ( $languageMask != 0 )
108
        {
109
            // Examine if the published version contains one of the languages we
110
            // match for.
111
            $version = $object->version( $versionID );
112
            // If the language ID is part of the mask the result is non-zero.
113
            $languageID = (int)$version->attribute( 'initial_language_id' );
114
            $hasLanguageMatch = (bool)( $languageMask & $languageID );
115
        }
116
 
117
        if ( $hasLanguageMatch and
118
             !$userIsEditor and
119
             !$inExcludeGroups and
120
             $correctSection )
121
        {
122 11 Cyberwolf
            $collaborationID = false;
123
            $db = & eZDB::instance();
124
            $taskResult = $db->arrayQuery( 'select workflow_process_id, collaboration_id from ezapprove_items where workflow_process_id = ' . $process->attribute( 'id' )  );
125
            if ( count( $taskResult ) > 0 )
126
                $collaborationID = $taskResult[0]['collaboration_id'];
127 1 cyberwolf
 
128 11 Cyberwolf
            eZDebugSetting::writeDebug( 'kernel-workflow-approve', $collaborationID, 'approve collaborationID' );
129
            eZDebugSetting::writeDebug( 'kernel-workflow-approve', $process->attribute( 'event_state'), 'approve $process->attribute( \'event_state\')' );
130
            if ( $collaborationID === false )
131 1 cyberwolf
            {
132 11 Cyberwolf
                /*
133
                  only create approve event when Akismet does recognize the published content as spam
134
                */
135
                if ( !eZAkismetType::isSpam( $object, $versionID ) )
136
                {
137
                    eZAudit::writeAudit( 'akismet', array( 'Action' => 'not recognized as spam',
138
                                                           'Object' => $object->attribute( 'id' ),
139
                                                           'Version' => $versionID ) );
140 1 cyberwolf
 
141 11 Cyberwolf
                    return EZ_WORKFLOW_TYPE_STATUS_ACCEPTED;
142
                }
143
 
144
                eZAudit::writeAudit( 'akismet', array( 'Action' => 'recognized as spam',
145
                                                       'Object' => $object->attribute( 'id' ),
146
                                                       'Version' => $versionID ) );
147
 
148
                /* Get user IDs from approve user groups */
149
                $ini = eZINI::instance();
150
                $userClassIDArray = array( $ini->variable( 'UserSettings', 'UserClassID' ) );
151
                $approveUserIDArray = array();
152
                foreach( $approveGroups as $approveUserGroupID )
153 1 cyberwolf
                {
154 11 Cyberwolf
                    if (  $approveUserGroupID != false )
155
                    {
156
                        $approveUserGroup = eZContentObject::fetch( $approveUserGroupID );
157
                        if ( isset( $approveUserGroup ) )
158 1 cyberwolf
                        {
159 11 Cyberwolf
                            foreach( $approveUserGroup->attribute( 'assigned_nodes' ) as $assignedNode )
160 1 cyberwolf
                            {
161 11 Cyberwolf
                                $userNodeArray =& $assignedNode->subTree( array( 'ClassFilterType' => 'include',
162
                                                                                     'ClassFilterArray' => $userClassIDArray,
163
                                                                                     'Limitation' => array() ) );
164
                                foreach( $userNodeArray as $userNode )
165
                                {
166
                                   $approveUserIDArray[] = $userNode->attribute( 'contentobject_id' );
167
                                }
168 1 cyberwolf
                            }
169
                        }
170 11 Cyberwolf
                    }
171 1 cyberwolf
                }
172 11 Cyberwolf
                $approveUserIDArray = array_merge( $approveUserIDArray, $editors );
173
                $approveUserIDArray = array_unique( $approveUserIDArray );
174 1 cyberwolf
 
175
                $this->createApproveCollaboration( $process, $event, $user->id(), $object->attribute( 'id' ), $versionID, $approveUserIDArray );
176
                $this->setInformation( "We are going to create approval" );
177
                $process->setAttribute( 'event_state', EZ_APPROVE_COLLABORATION_CREATED );
178
                $process->store();
179
                eZDebugSetting::writeDebug( 'kernel-workflow-approve', $this, 'approve execute' );
180
                return EZ_WORKFLOW_TYPE_STATUS_DEFERRED_TO_CRON_REPEAT;
181
            }
182
            else if ( $process->attribute( 'event_state') == EZ_APPROVE_COLLABORATION_NOT_CREATED )
183
            {
184
                eZApproveCollaborationHandler::activateApproval( $collaborationID );
185
                $process->setAttribute( 'event_state', EZ_APPROVE_COLLABORATION_CREATED );
186
                $process->store();
187
                eZDebugSetting::writeDebug( 'kernel-workflow-approve', $this, 'approve re-execute' );
188
                return EZ_WORKFLOW_TYPE_STATUS_DEFERRED_TO_CRON_REPEAT;
189
            }
190
            else //EZ_APPROVE_COLLABORATION_CREATED
191
            {
192
                $this->setInformation( "we are checking approval now" );
193
                eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'check approval' );
194 9 cyberwolf
                $workflowStatus = $this->checkApproveCollaboration(  $process, $event );
195
                if ( $workflowStatus == EZ_WORKFLOW_TYPE_STATUS_ACCEPTED )
196 1 cyberwolf
                {
197 11 Cyberwolf
                    eZAudit::writeAudit( 'akismet', array( 'Action' => 'submitting ham',
198
                                         'Object' => $object->attribute( 'id' ),
199
                                         'Version' => $versionID ) );
200 1 cyberwolf
                    $this->submitHam( $object, $versionID );
201
                }
202 9 cyberwolf
 
203
                return $workflowStatus;
204 1 cyberwolf
            }
205
        }
206
        else
207
        {
208
            eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowSections , "we are not going to create approval " . $object->attribute( 'section_id') );
209
            eZDebugSetting::writeDebug( 'kernel-workflow-approve', $userGroups, "we are not going to create approval" );
210
            eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowGroups,  "we are not going to create approval" );
211
            eZDebugSetting::writeDebug( 'kernel-workflow-approve', $user->id(), "we are not going to create approval "  );
212
            return EZ_WORKFLOW_TYPE_STATUS_ACCEPTED;
213
        }
214
    }
215
 
216
    function isSpam ( $object, $versionID )
217
    {
218
        $version =& $object->version( $versionID );
219
 
220
        if ( !is_object( $version ) )
221
        {
222
            return false;
223
        }
224
 
225
        include_once( 'extension/akismet/classes/ezcontentobjectakismet.php' );
226
        $infoExtractor = eZContentObjectAkismet::getInfoExtractor( $version );
227
        if ( !$infoExtractor )
228
        {
229
            return false;
230
        }
231
 
232
        $debug = eZDebug::instance();
233
 
234
        $comment = $infoExtractor->getCommentArray();
235
        $debug->writeDebug( $comment, 'eZAkismetType::isSpam' );
236
 
237
        include_once( 'extension/akismet/classes/ezakismet.php' );
238
        $akismet = new eZAkismet( $comment );
239
 
240
        if ( $akismet->errorsExist() )
241
        {
242
            return false;
243
        }
244
 
245
        $isSpam = $akismet->isSpam();
246
        $debug->writeDebug( $isSpam ? 'Recognized as spam.' : 'Not recognized as spam', 'eZAkismetType::isSpam' );
247
 
248
        return $isSpam;
249
    }
250
 
251
    function submitHam( $object, $versionID )
252
    {
253
        $version =& $object->version( $versionID );
254
 
255
        if ( !is_object( $version ) )
256
        {
257
            return false;
258
        }
259
 
260
        include_once( 'extension/akismet/classes/ezcontentobjectakismet.php' );
261
        $infoExtractor = eZContentObjectAkismet::getInfoExtractor( $version );
262
        if ( !$infoExtractor )
263
        {
264
            return false;
265
        }
266
 
267
        $comment = $infoExtractor->getCommentArray();
268
 
269
        include_once( 'extension/akismet/classes/ezakismet.php' );
270
        $akismet = new eZAkismet( $comment );
271
 
272
        if ( $akismet->errorsExist() )
273
        {
274
            return false;
275
        }
276
 
277
        $response = $akismet->submitHam();
278
        $debug = eZDebug::instance();
279
        $debug->writeNotice( $response, 'Akismet web service response' );
280
 
281
        return true;
282
    }
283
}
284
 
285
eZWorkflowEventType::registerType( EZ_WORKFLOW_TYPE_AKISMET_ID, "ezakismettype" );
286
 
287 11 Cyberwolf
?>