| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace MySociety\TheyWorkForYou\Utility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Alert Utilities | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Utility functions related to alerts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  | class Alert { | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     #XXX don't calculate this every time | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     private static function sectionToTitle($section) { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 | 3 |  |         global $hansardmajors; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 | 3 |  |         $section_map = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         foreach ($hansardmajors as $major => $details) { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 | 3 |  |             $section_map[$details["page_all"]] = $details["title"]; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 3 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         return $section_map[$section]; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 | 3 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |     public static function detailsToCriteria($details) { | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $criteria = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 | 3 |  |         if (!empty($details['keyword'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 | 3 |  |             $criteria[] = $details['keyword']; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         if (!empty($details['pid'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |             $criteria[] = 'speaker:' . $details['pid']; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $criteria = join(' ', $criteria); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         return $criteria; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public static function forUser($email) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $db = new \ParlDB(); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $q = $db->query('SELECT * FROM alerts WHERE email = :email | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             AND deleted != 1 ORDER BY created', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             ':email' => $email, | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $alerts = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         foreach ($q as $row) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             $criteria = self::prettifyCriteria($row['criteria']); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $parts = self::prettifyCriteria($row['criteria'], true); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             $token = $row['alert_id'] . '-' . $row['registrationtoken']; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             $status = 'confirmed'; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             if (!$row['confirmed']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 $status = 'unconfirmed'; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             } elseif ($row['deleted'] == 2) { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 $status = 'suspended'; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $alert = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 'token' => $token, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 'status' => $status, | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 'criteria' => $criteria, | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 'raw' => $row['criteria'], | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 'keywords' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 'exclusions' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 'sections' => [], | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             $alert = array_merge($alert, $parts); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             $alerts[] = $alert; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         return $alerts; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public static function prettifyCriteria($alert_criteria, $as_parts = false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $text = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if ($alert_criteria) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $criteria = explode(' ', $alert_criteria); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $parts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $words = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $sections = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $sections_verbose = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $spokenby = array_values(\MySociety\TheyWorkForYou\Utility\Search::speakerNamesForIDs($alert_criteria)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             foreach ($criteria as $c) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 if (preg_match('#^section:(\w+)#', $c, $m)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     $sections[] = $m[1]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     $sections_verbose[] = self::sectionToTitle($m[1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 } elseif (!preg_match('#^speaker:(\d+)#', $c, $m)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     $words[] = $c; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             if ($spokenby && count($words)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $text = implode(' or ', $spokenby) . ' mentions [' . implode(' ', $words) . ']'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $parts['spokenby'] = $spokenby; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $parts['words'] = $words; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } elseif (count($words)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $text = '[' . implode(' ', $words) . ']' . ' is mentioned'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $parts['words'] = $words; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } elseif ($spokenby) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $text = implode(' or ', $spokenby) . " speaks"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $parts['spokenby'] = $spokenby; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             if ($sections) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 $text = $text . " in " . implode(' or ', $sections_verbose); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $parts['sections'] = $sections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $parts['sections_verbose'] = $sections_verbose; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if ($as_parts) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             return $parts; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 114 |  |  |         return $text; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 118 |  |  |  | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.