예제 #1
0
    public function postOffice()
    {
        set_time_limit(600);
        //check if available
        if (!Yii::app()->db->createCommand("SELECT * FROM crawler WHERE action='takingMail'")->queryRow()) {
            Yii::app()->db->createCommand("INSERT INTO crawler (action,active) VALUES('takingMail',1)")->execute();
            //blocking function
            //read emails to send
            if ($emails = Yii::app()->db->createCommand("SELECT * FROM mailToSent WHERE indexmail IS NULL LIMIT 4000")->queryAll()) {
                Yii::app()->db->createCommand("INSERT INTO mailToSent (indexmail) VALUES(1)")->execute();
                //print_r($emails);
                foreach ($emails as $row) {
                    if ($row['outside'] == 1) {
                        //send email third server
                        if ($row['pass'] == '') {
                            if (Crawler::sendMailOutWithPin($row)) {
                                $meta = substr(hex2bin($row['meta']), 0, 16) . substr(hex2bin($row['meta']), 16);
                                $body = substr(hex2bin($row['body']), 0, 16) . substr(hex2bin($row['body']), 16);
                                $person[] = array("oldId" => $row['messageId'], "meta" => new MongoBinData($meta, MongoBinData::GENERIC), "body" => new MongoBinData($body, MongoBinData::GENERIC), "modKey" => $row['modKey'], "file" => $row['file'], "pinHash" => $row['pinHash'], "tryCounter" => 0, "expireAfter" => new MongoDate(strtotime('now' . '+ 4 weeks')));
                                if (Yii::app()->mongo->insert('mailQueue', $person)) {
                                    Yii::app()->db->createCommand("DELETE FROM mailToSent WHERE id=" . $row['id'])->execute();
                                }
                                unset($person);
                            }
                        } else {
                            if (Crawler::sendMailOutWithoutPin($row)) {
                                if (isset($row['file']) && $row['file'] != '') {
                                    $person[] = array("oldId" => $row['messageId'], "modKey" => $row['modKey'], "file" => $row['file'], "expireAfter" => new MongoDate(strtotime('now' . '+ 4 weeks')));
                                    Yii::app()->mongo->insert('mailQueue', $person);
                                    unset($person);
                                }
                                Yii::app()->db->createCommand("DELETE FROM mailToSent WHERE id=" . $row['id'])->execute();
                            }
                        }
                    }
                    //end send email third server
                    if ($row['fromOut'] == 1 || $row['outside'] == 0 && $row['fromOut'] == 0) {
                        $fileSize = 0;
                        if (isset($row['file'])) {
                            //calculate space occupied by files
                            $files = json_decode($row['file']);
                            foreach ($files as $file) {
                                $fileSize += FileWorks::getFileSize($file);
                            }
                        }
                        $trans = Yii::app()->db->beginTransaction();
                        $par[':id'] = $row['id'];
                        $par[':meta'] = $row['seedMeta'];
                        $par[':modKey'] = $row['modKeySeed'];
                        $par[':password'] = $row['seedPass'];
                        $par[':rcpnt'] = $row['rcpnt'];
                        $par[':v1'] = 1;
                        if (Yii::app()->db->createCommand("INSERT INTO seedTable (id,meta,modKey,password,rcpnt,v1) VALUES (:id,:meta,:modKey,:password,:rcpnt,:v1)")->execute($par)) {
                            unset($par);
                            $meta = substr(hex2bin($row['meta']), 0, 16) . substr(hex2bin($row['meta']), 16);
                            $body = substr(hex2bin($row['body']), 0, 16) . substr(hex2bin($row['body']), 16);
                            $person[] = array("oldId" => $row['messageId'], "meta" => new MongoBinData($meta, MongoBinData::GENERIC), "body" => new MongoBinData($body, MongoBinData::GENERIC), "pass" => $row['pass'], "modKey" => $row['modKey'], "file" => $row['file'], "emailSize" => strlen($meta) + strlen($body) + $fileSize);
                            if (Yii::app()->mongo->insert('mailQueue', $person)) {
                                unset($fileSize, $person);
                                if (Yii::app()->db->createCommand("DELETE FROM mailToSent WHERE id=" . $row['id'])->execute()) {
                                    $trans->commit();
                                } else {
                                    $trans->rollback();
                                }
                            } else {
                                $trans->rollback();
                            }
                        }
                    }
                }
            }
            Yii::app()->db->createCommand("DELETE FROM crawler WHERE action ='takingMail'")->execute();
        }
        echo 'success
		';
    }