File indexing completed on 2024-12-22 05:33:26

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  *
0023  * Created: 06.08.2018
0024  */
0025 class Backend_CdiscourseController extends Local_Controller_Action_CliAbstract
0026 {
0027 
0028     const filename = "members";
0029     const filename_errors = "members";
0030 
0031     protected $logfile;
0032     protected $errorlogfile;
0033     /** @var Zend_Config */
0034     protected $config;
0035     protected $log;
0036 
0037     /**
0038      * @inheritDoc
0039      */
0040     public function __construct(
0041         Zend_Controller_Request_Abstract $request,
0042         Zend_Controller_Response_Abstract $response,
0043         array $invokeArgs = array()
0044     ) {
0045         parent::__construct($request, $response, $invokeArgs);
0046         $this->config = Zend_Registry::get('config')->settings->server->forum;
0047         $this->log = Zend_Registry::get('logger');
0048         $this->_helper->viewRenderer->setNoRender(false);
0049     }
0050 
0051 
0052     /**
0053      * @throws Zend_Db_Statement_Exception
0054      * @throws Zend_Exception
0055      */
0056     public function runAction()
0057     {
0058         ini_set('memory_limit', '1024M');
0059 
0060         $force = (boolean)$this->getParam('force', false);
0061         $method = $this->getParam('method', 'create');
0062 
0063         $this->log->info("METHOD: {$method}\n--------------\n");
0064 
0065         if ('delete' == $method) {
0066             //$this->deleteMember($this->getParam('member_id'));
0067             echo "not implemented";
0068 
0069             return;
0070         }
0071 
0072         if ($this->hasParam('member_id')) {
0073             $memberId = $this->getParam('member_id');
0074             $operator = $this->getParam('op', null);
0075             $members = $this->getMemberList($memberId, $operator);
0076         } else {
0077             $members = $this->getMemberList();
0078         }
0079 
0080         if ('create' == $method) {
0081             $this->exportMembers($members, $force);
0082 
0083             return;
0084         }
0085         if ('update' == $method) {
0086             //$this->updateMembers($members);
0087             echo "not implemented";
0088 
0089             return;
0090         }
0091         if ('validate' == $method) {
0092             //$this->validateMembers($members);
0093             echo "not implemented";
0094 
0095             return;
0096         }
0097         if ('block' == $method) {
0098             $this->blockMembers($members);
0099 
0100             return;
0101         }
0102         if ('posts' == $method) {
0103             $this->postsMembers($members);
0104 
0105             return;
0106         }
0107         if ('posts_delete' == $method) {
0108             $this->deletePostsMembers($members);
0109 
0110             return;
0111         }
0112         if ('posts_undelete' == $method) {
0113             $this->undeletePostsMembers($members);
0114 
0115             return;
0116         }
0117         if ('silence' == $method) {
0118             $this->silenceMembers($members);
0119 
0120             return;
0121         }
0122     }
0123 
0124     /**
0125      * @param null   $member_id
0126      * @param string $operator
0127      *
0128      * @return Zend_Db_Statement_Interface
0129      * @throws Zend_Db_Statement_Exception
0130      */
0131     private function getMemberList($member_id = null, $operator = "=")
0132     {
0133         $filter = "";
0134         if (empty($operator)) {
0135             $operator = "=";
0136         }
0137         if ($operator == "gt") {
0138             $operator = ">";
0139         }
0140         if ($operator == "lt") {
0141             $operator = "<";
0142         }
0143         if (isset($member_id)) {
0144             $filter = " AND `m`.`member_id` {$operator} " . $member_id;
0145         }
0146 
0147         $sql = "
0148             SELECT `mei`.`external_id`,`m`.`member_id`, `m`.`username`, `me`.`email_address`, `m`.`password`, `m`.`roleId`, `m`.`firstname`, `m`.`lastname`, `m`.`profile_image_url`, `m`.`created_at`, `m`.`changed_at`, `m`.`source_id`, `m`.`biography`, `m`.`is_active`, `me`.`email_checked`
0149             FROM `member` AS `m`
0150             LEFT JOIN `member_email` AS `me` ON `me`.`email_member_id` = `m`.`member_id` AND `me`.`email_primary` = 1
0151             LEFT JOIN `member_external_id` AS `mei` ON `mei`.`member_id` = `m`.`member_id`
0152             WHERE `m`.`is_active` = 1 
0153               AND `m`.`is_deleted` = 0 
0154               AND `me`.`email_checked` IS NOT NULL 
0155               AND `me`.`email_deleted` = 0
0156               AND LOCATE('_double', `m`.`username`) = 0 
0157               AND LOCATE('_double', `me`.`email_address`) = 0
0158             " . $filter . "
0159             ORDER BY `m`.`member_id` ASC
0160         ";
0161 
0162         $result = Zend_Db_Table::getDefaultAdapter()->query($sql);
0163 
0164         $this->log->info("Load : " . $result->rowCount() . " members...");
0165 
0166         return $result;
0167     }
0168 
0169     /**
0170      * @param Zend_Db_Statement_Interface $members
0171      *
0172      * @param bool                        $force
0173      *
0174      * @return bool
0175      * @throws Zend_Db_Statement_Exception
0176      * @throws Zend_Exception
0177      */
0178     private function exportMembers($members, $force = false)
0179     {
0180         // only usernames which are valid in github/gitlab
0181         $usernameValidChars = new Local_Validate_UsernameValid();
0182         $emailValidate = new Zend_Validate_EmailAddress();
0183         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0184 
0185         while ($member = $members->fetch()) {
0186             $this->log->info("process " . Zend_Json::encode($member));
0187             echo "process " . Zend_Json::encode($member) . PHP_EOL;
0188 
0189             //if (false === $usernameValidChars->isValid($member['username'])) {
0190             //    file_put_contents($this->errorlogfile, print_r($member, true) . "user name validation error" . "\n\n", FILE_APPEND);
0191             //    continue;
0192             //}
0193             if (false === $emailValidate->isValid($member["email_address"])) {
0194                 $this->log->info("messages [\"email address validation error\"] ");
0195                 echo "response [\"email address validation error\"]" . PHP_EOL;
0196                 continue;
0197             }
0198             try {
0199                 //Export User, if he not exists
0200                 $result = $modelSubSystem->createUserFromArray($member, $force);
0201                 if (false === $result AND $modelSubSystem->hasRateLimitError()) {
0202                     $this->log->info("RateLimitError: Wait " . $modelSubSystem->getRateLimitWaitSeconds() . " seconds for next execution.");
0203 
0204                     sleep((int)$modelSubSystem->getRateLimitWaitSeconds()+5);
0205                     $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0206                     $modelSubSystem->createUserFromArray($member, $force);
0207                 }
0208             } catch (Exception $e) {
0209                 $this->log->info($e->getMessage() . PHP_EOL . $e->getTraceAsString());
0210             }
0211             $messages = $modelSubSystem->getMessages();
0212             $this->log->info("messages " . Zend_Json::encode($messages));
0213             echo "response " . Zend_Json::encode($messages) . PHP_EOL;
0214         }
0215 
0216         return true;
0217     }
0218 
0219     private function blockMembers($members)
0220     {
0221         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0222 
0223         while ($member = $members->fetch()) {
0224             $result = $modelSubSystem->blockUser($member);
0225             if (false == $result) {
0226                 $this->log->info('Fail');
0227             }
0228             $messages = $modelSubSystem->getMessages();
0229             if (false === empty($messages)) {
0230                 $this->log->info("Message : " . Zend_Json::encode($messages));
0231             }
0232         }
0233     }
0234 
0235     private function postsMembers($members)
0236     {
0237         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0238 
0239         while ($member = $members->fetch()) {
0240             $result = $modelSubSystem->getPostsFromUser($member);
0241             if (false == $result) {
0242                 $this->log->info('Fail');
0243             }
0244             $this->log->info("Posts : " . Zend_Json::encode($result));
0245             $messages = $modelSubSystem->getMessages();
0246             if (false === empty($messages)) {
0247                 $this->log->info("Message : " . Zend_Json::encode($messages));
0248             }
0249         }
0250     }
0251 
0252     private function deletePostsMembers($members)
0253     {
0254         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0255 
0256         while ($member = $members->fetch()) {
0257             $result = $modelSubSystem->blockUserPosts($member);
0258             $this->log->info("Posts : " . Zend_Json::encode($result));
0259             $messages = $modelSubSystem->getMessages();
0260             if (false === empty($messages)) {
0261                 $this->log->info("Message : " . Zend_Json::encode($messages));
0262             }
0263         }
0264     }
0265 
0266     private function undeletePostsMembers($members)
0267     {
0268         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0269 
0270         while ($member = $members->fetch()) {
0271             $result = $modelSubSystem->unblockUserPosts($member);
0272             $this->log->info("Posts : " . Zend_Json::encode($result));
0273             $messages = $modelSubSystem->getMessages();
0274             if (false === empty($messages)) {
0275                 $this->log->info("Message : " . Zend_Json::encode($messages));
0276             }
0277         }
0278     }
0279 
0280     private function silenceMembers($members)
0281     {
0282         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0283 
0284         while ($member = $members->fetch()) {
0285             $result = $modelSubSystem->silenceUser($member);
0286             if (false == $result) {
0287                 $this->log->info('Fail');
0288             }
0289             $messages = $modelSubSystem->getMessages();
0290             if (false === empty($messages)) {
0291                 $this->log->info("Message : " . Zend_Json::encode($messages));
0292             }
0293         }
0294     }
0295 
0296     public function groupAction()
0297     {
0298         $groupname = $this->getParam('name', null);
0299 
0300         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0301 
0302         //$result = $modelSubSystem->createGroup($groupname);
0303         //
0304         //echo $result;
0305         //
0306         //$result = $modelSubSystem->deleteGroup($result);
0307         //
0308         //echo $result;
0309 
0310         $result = $modelSubSystem->getUserByEmail("info@dschinnweb.de");
0311 
0312         print_r($result);
0313 
0314         echo json_encode($result);
0315     }
0316 
0317     private function deleteMember($member)
0318     {
0319         $modelSubSystem = new Default_Model_Ocs_Forum($this->config);
0320         try {
0321             //Export User, if he not exists
0322             $modelSubSystem->deleteUser($member);
0323         } catch (Exception $e) {
0324             $this->log->info($e->getMessage() . PHP_EOL . $e->getTraceAsString());
0325         }
0326         $messages = $modelSubSystem->getMessages();
0327         $this->log->info("messages " . Zend_Json::encode($messages));
0328         echo "response " . Zend_Json::encode($messages) . PHP_EOL;
0329     }
0330 
0331 }