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

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 
0024 class JsonController extends Zend_Controller_Action
0025 {
0026 
0027     const chat_avatarUrl = 'https://chat.opendesktop.org/_matrix/media/v1/thumbnail';
0028     const chat_roomPublicUrl = 'https://chat.opendesktop.org/_matrix/client/unstable/publicRooms';
0029     const chat_roomsUrl = 'https://chat.opendesktop.org/_matrix/client/unstable/rooms/';
0030     const chat_roomUrl = 'https://chat.opendesktop.org/#/room/';
0031     const chat_userProfileUrl = 'https://chat.opendesktop.org/_matrix/client/r0/profile/';
0032     const chat_userPresense = 'https://chat.opendesktop.org/_matrix/client/r0/presence/';
0033     protected $_format = 'json';
0034     public function init()
0035     {
0036         parent::init();
0037         $this->initView();
0038         $this->log = Zend_Registry::get('logger');
0039     }
0040 
0041     public function initView()
0042     {
0043         // Disable render view
0044         $this->_helper->layout->disableLayout();
0045         $this->_helper->viewRenderer->setNoRender(true);
0046     }
0047 
0048     public function indexAction()
0049     {
0050         $this->_sendErrorResponse(999, 'unknown request');
0051     }
0052 
0053     protected function _initResponseHeader()
0054     {
0055         http_response_code(200);
0056 
0057         if (!empty($_SERVER['HTTP_ORIGIN'])) {
0058             header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN'], true);
0059             header('Access-Control-Allow-Credentials: true', true);
0060             header('Access-Control-Max-Age: 1728000', true);
0061         }
0062 
0063 
0064         if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
0065             header('Access-Control-Allow-Methods: ' . implode(', ', array_unique([
0066                 'OPTIONS', 'HEAD', 'GET', 'POST', 'PUT',
0067                 strtoupper($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])
0068             ])), true);
0069         }
0070 
0071         if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
0072             header('Access-Control-Allow-Headers: ' . $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'], true);
0073             header('Access-Control-Expose-Headers: Authorization, Content-Type, Accept', true);
0074         }
0075 
0076         if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
0077             exit;
0078         }
0079 
0080         header('Content-Type: application/json; charset=UTF-8', true);
0081     }
0082 
0083     protected function _sendResponse($response, $format = 'json', $xmlRootTag = 'ocs')
0084     {          
0085         header('Content-Type: application/json; charset=UTF-8');
0086         echo json_encode($response);
0087     }
0088 
0089 
0090     public function chatAction()
0091     {
0092 
0093         $this->_initResponseHeader();
0094         $config = Zend_Registry::get('config')->settings->client->default;
0095         $access_token = $config->riot_access_token;
0096         $urlRooms = JsonController::chat_roomPublicUrl . '?access_token=' . $access_token;
0097 
0098         $ch = curl_init();
0099         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
0100         curl_setopt($ch, CURLOPT_HEADER, 0);
0101         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
0102         curl_setopt($ch, CURLOPT_URL, $urlRooms);
0103         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
0104         $data = curl_exec($ch);
0105         curl_close($ch);
0106         $results = json_decode($data);
0107         // https://chat.opendesktop.org/_matrix/client/unstable/publicRooms?access_token=
0108 
0109         $rooms = array();
0110         foreach ($results->chunk as &$room) {
0111             if ($room->guest_can_join) continue;
0112             $urlMembers = JsonController::chat_roomsUrl . $room->room_id . '/joined_members?access_token=' . $access_token;
0113             //https://chat.opendesktop.org/_matrix/client/unstable/rooms/!LNQABMgCYqWKSysjJK%3Achat.opendesktop.org/joined_members?access_token=
0114             $k = curl_init();
0115             curl_setopt($k, CURLOPT_AUTOREFERER, true);
0116             curl_setopt($k, CURLOPT_HEADER, 0);
0117             curl_setopt($k, CURLOPT_RETURNTRANSFER, 1);
0118             curl_setopt($k, CURLOPT_URL, $urlMembers);
0119             curl_setopt($k, CURLOPT_FOLLOWLOCATION, true);
0120             $t = curl_exec($k);
0121             curl_close($k);
0122             $r = json_decode($t);
0123             $room->members = $r->joined;
0124             $rooms[] = $room;
0125         }
0126         $this->_sendResponse($rooms, $this->_format);
0127     }
0128 
0129     private function curlRiot($url)
0130     {
0131         $ch = curl_init();
0132         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
0133         curl_setopt($ch, CURLOPT_HEADER, 0);
0134         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
0135         curl_setopt($ch, CURLOPT_URL, $url);
0136         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
0137         $data = curl_exec($ch);
0138         curl_close($ch);
0139         $results = json_decode($data);  
0140         return $results;
0141     }
0142 
0143     public function riotAction()
0144     {
0145         $chatServer='chat.opendesktop.org';
0146         $this->_initResponseHeader();
0147         $config = Zend_Registry::get('config')->settings->client->default;
0148         $access_token = $config->riot_access_token;
0149         $p_username = $this->getParam('username');
0150         $member_data = $this->getUserData($p_username);
0151         $urlProfile = JsonController::chat_userProfileUrl.'@'.$member_data['username'].':'.$chatServer.'?access_token=' . $access_token;
0152         //https://chat.opendesktop.org/_matrix/client/r0/profile/@rvs75:chat.opendesktop.org?access_token=       
0153         $results = $this->curlRiot($urlProfile);   
0154         $urlPresense = JsonController::chat_userPresense.'@'.$member_data['username'].':'.$chatServer.'/status?access_token=' . $access_token;
0155         $status =  $this->curlRiot($urlPresense);   
0156         $resonse=array("user" => $results,"status" => $status);     
0157         $this->_sendResponse($resonse, $this->_format);
0158     }
0159 
0160     private function curlNextcloud($url)
0161     {
0162         $config = Zend_Registry::get('config')->settings->server->nextcloud;
0163         $ch = curl_init();
0164         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
0165         curl_setopt($ch, CURLOPT_HEADER, 0);
0166         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
0167         curl_setopt($ch, CURLOPT_URL, $url);       
0168         curl_setopt($ch, CURLOPT_USERPWD, $config->user_sodo.':'.$config->user_sodo_pw);
0169         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
0170         curl_setopt($ch, CURLOPT_HTTPHEADER, array('OCS-APIRequest: true'));         
0171         $data = curl_exec($ch);
0172         curl_close($ch);
0173         $results = json_decode($data);   
0174         return $results;
0175     }
0176     public function plingAction()
0177     {
0178         $p_username = $this->getParam('username');
0179         $member_data = $this->getUserData($p_username);
0180         if(!$member_data)
0181         {
0182             $this->_sendResponse(null, $this->_format);
0183             return;
0184         }
0185 
0186        
0187         $helpPrintDate = new Default_View_Helper_PrintDate();
0188         $helperImage = new Default_View_Helper_Image();
0189 
0190         $tableProduct = new Default_Model_Project();
0191         $productsRowset = $tableProduct->fetchAllProjectsForMember($member_data['member_id'],5);
0192         
0193         $products = array();
0194         foreach ($productsRowset as $row) {
0195             $products[] = $row;
0196         }
0197     
0198         $parray=array();
0199         foreach ($products as $p) {            
0200             $tmp= array('project_id'=> $p->project_id    
0201                 , 'image_small'=>$helperImage->Image($p->image_small, array('width' => 200, 'height' => 200))  
0202                 , 'title' => $p->title
0203                 ,'laplace_score' =>$p->laplace_score*10
0204                 ,'cat_title' =>$p->catTitle
0205                 ,'updated_at' => $helpPrintDate->printDate(($p->changed_at==null?$p->created_at:$p->changed_at))
0206             ) ; 
0207             $parray[] = $tmp;
0208         }
0209 
0210         
0211         $result = array('user'=>$member_data,'products'=>$parray);
0212         $this->_sendResponse($result, $this->_format);
0213 
0214     }
0215 
0216     public function nextcloudAction()
0217     {
0218         
0219         $config = Zend_Registry::get('config')->settings->server->nextcloud;
0220         
0221         $p_username = $this->getParam('username');
0222         $member_data = $this->getUserData($p_username);
0223         if(!$member_data)
0224         {
0225             $this->_sendResponse(null, $this->_format);
0226             return;
0227         }
0228         $url = $config->host."/ocs/v1.php/cloud/users?search=".$member_data['username']."&format=json";                     
0229         $results = $this->curlNextcloud($url);
0230         $status =$results->ocs->meta->status; 
0231         $usersArray=array();
0232         if($status== 'ok' && sizeof($results->ocs->data->users)>0)
0233         {   
0234             $users = $results->ocs->data->users;
0235             foreach ($users as $user) {
0236                 $urlUser = $config->host."/ocs/v1.php/cloud/users/".$user."?format=json";  
0237                 $u = $this->curlNextcloud($urlUser);
0238                 if($u->ocs->meta->status=='ok')
0239                 {
0240                     $usersArray[]= $u->ocs->data;   
0241                 }
0242                 
0243             }
0244         }
0245         $reternUsers = array("users" => $usersArray);
0246 
0247         $this->_sendResponse($reternUsers, $this->_format);
0248     }
0249 
0250     public static function printDateSinceForum($last_posted_at)
0251     {
0252         $now = new DateTime();
0253         $now->setTimezone(new DateTimeZone('UTC'));
0254 
0255         $last_posted_at = new DateTime($last_posted_at, new DateTimeZone('UTC'));
0256 
0257         $interval = $last_posted_at->diff($now);
0258 
0259         $tokens = array(
0260             'y' => 'year',
0261             'm' => 'month',
0262             'd' => 'day',
0263             'h' => 'hour',
0264             'i' => 'minute',
0265             's' => 'second',
0266         );
0267         foreach ($tokens as $unit => $text) {
0268             if ($interval->$unit == 0) continue;
0269 
0270             return $interval->$unit . ' ' . $text . (($interval->$unit > 1) ? 's' : '') . ' ago';
0271         }
0272 
0273         return null;
0274     }
0275 
0276     public function forumAction()
0277     {
0278 
0279         $this->_initResponseHeader();
0280 
0281         $url_forum = Zend_Registry::get('config')->settings->client->default->url_forum;
0282         $url = $url_forum . '/latest.json';
0283         $ch = curl_init();
0284         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
0285         curl_setopt($ch, CURLOPT_HEADER, 0);
0286         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
0287         curl_setopt($ch, CURLOPT_URL, $url);
0288         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
0289         $data = curl_exec($ch);
0290         curl_close($ch);
0291         $results = json_decode($data);
0292         $timeago = new Default_View_Helper_PrintDateSince();
0293         foreach ($results->topic_list->topics as &$t) {
0294 
0295             $t->timeago = self::printDateSinceForum($t->last_posted_at);
0296 
0297             // $date = new DateTime($t->last_posted_at, new DateTimeZone('UTC'));
0298             // $t->timeago = $timeago->printDateSince($date->format('Y-m-d h:s:m'));
0299             // $strTime = str_replace('T', ' ', substr($t->last_posted_at, 0, 19));
0300 
0301             // //$t->timeago = $timeago->printDateSince($strTime);
0302 
0303             // $fromFormat = 'Y-m-d H:i:s';
0304             // $date = DateTime::createFromFormat($fromFormat, $strTime);
0305             // // forum/latest.json last_posted_at is 5 hours later as server somehow.. quick workaround
0306             // $date->sub(new DateInterval('PT4H10M'));
0307             // $t->timeago = $timeago->printDateSince($date->format('Y-m-d h:s:m'));
0308             //$t->timeago =  $date->format('Y-m-d H:i:s');
0309             $r = 'Reply';
0310             $counts = $t->posts_count - 1;
0311             if ($counts == 0) {
0312                 $r = 'Replies';
0313             } else if ($counts == 1) {
0314                 $r = 'Reply';
0315             } else {
0316                 $r = 'Replies';
0317             }
0318             $t->replyMsg = $counts . ' ' . $r;
0319         }
0320         $this->_sendResponse($results, $this->_format);
0321     }
0322 
0323     protected function getUserData($p_username)
0324     {
0325         $helperUserRole = new Backend_View_Helper_UserRole();
0326         $userRoleName = $helperUserRole->userRole();
0327         $isAdmin = false;
0328         if (Default_Model_DbTable_MemberRole::ROLE_NAME_ADMIN == $userRoleName) {
0329                 $isAdmin = true;
0330         }
0331         $member_data = null;
0332         $auth = Zend_Auth::getInstance();
0333         if ($auth->hasIdentity()) {
0334             $modelSubSystem = new Default_Model_Ocs_Forum();
0335             if($isAdmin)
0336             {               
0337                 if($p_username && $p_username!='null')
0338                 {                
0339                     $modelMember = new Default_Model_Member();
0340                     $memberId = $modelMember->fetchActiveUserByUsername($p_username);   
0341                     if($memberId)
0342                     {
0343                         $member = $modelMember->fetchMemberData($memberId);                                         
0344                         $member_data = array('username'=>$p_username,'member_id' =>$memberId, 'mail'=>$member->mail,'avatar'=>$member->profile_image_url);
0345                     }
0346                     
0347                 }else{
0348                     $authMember = $auth->getStorage()->read();                              
0349                     $member_data = array('username'=>$authMember->username,'member_id' =>$authMember->member_id 
0350                     , 'mail'=>$authMember->mail,'avatar'=>$authMember->profile_image_url);    
0351                 }
0352             }else
0353             {
0354                 $authMember = $auth->getStorage()->read();                              
0355                 $member_data = array('username'=>$authMember->username,'member_id' =>$authMember->member_id 
0356                 , 'mail'=>$authMember->mail,'avatar'=>$authMember->profile_image_url);
0357             }
0358         }           
0359         return $member_data;  
0360     }
0361 
0362     public function forumpostsAction()
0363     {        
0364         $this->_initResponseHeader();
0365         $p_username = $this->getParam('username');
0366         $results=null;        
0367         $member_data = $this->getUserData($p_username);        
0368         if($member_data)
0369         {          
0370             $modelSubSystem = new Default_Model_Ocs_Forum();
0371             $user = $modelSubSystem->getUserByUsername($member_data['username']);    
0372             if($user)
0373             {
0374                 $results['user'] = $user;
0375             }
0376             $posts = $modelSubSystem->getPostsFromUser($member_data);  
0377                 
0378            
0379             if($posts)
0380             {
0381                 $results['posts'] = $posts['posts'];
0382             }
0383              
0384         }            
0385         $this->_sendResponse($results, $this->_format);
0386     }
0387 
0388     public function gitlabAction()
0389     {
0390         $this->_initResponseHeader();
0391         $p_username = $this->getParam('username');
0392         $results=null;        
0393         $member_data = $this->getUserData($p_username);        
0394         if ($member_data) {
0395             $modelSubSystem = new Default_Model_Ocs_Gitlab();                              
0396             $user = $modelSubSystem->getUserWithName($member_data['username']);    
0397             //$this->log->info(">>>>>>>results>>>>".json_encode($user));     
0398             if($user)
0399             {
0400                 $results['user'] = $user;
0401                 $posts = $modelSubSystem->getUserProjects($user['id']);    
0402                 if($posts)
0403                 {
0404                     $results['projects'] = $posts;
0405                 }
0406             }
0407 
0408             //$this->log->info(">>>>>>>results>>>>".json_encode($results));      
0409         }               
0410         $this->_sendResponse($results, $this->_format);
0411     }
0412 
0413     public function gitlabnewprojectsAction()
0414     {
0415 
0416         $this->_initResponseHeader();
0417         $url_git = Zend_Registry::get('config')->settings->server->opencode->host;
0418         $url = $url_git . '/api/v4/projects?order_by=created_at&sort=desc&visibility=public&page=1&per_page=5';
0419         $ch = curl_init();
0420         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
0421         curl_setopt($ch, CURLOPT_HEADER, 0);
0422         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
0423         curl_setopt($ch, CURLOPT_URL, $url);
0424         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
0425         $data = curl_exec($ch);
0426         curl_close($ch);
0427         $results = json_decode($data);
0428         $timeago = new Default_View_Helper_PrintDateSince();
0429         foreach ($results as &$t) {
0430             $tmp = str_replace('T', ' ', substr($t->created_at, 0, 19));
0431             $t->timeago = $timeago->printDateSince($tmp);
0432         }
0433         $this->_sendResponse($results, $this->_format);
0434     }
0435 
0436     public function gitlabfetchuserAction()
0437     {        
0438         $this->_initResponseHeader();
0439         $url_git = Zend_Registry::get('config')->settings->server->opencode->host;
0440         $url = $url_git . '/api/v4/users?username=' . $this->getParam('username');
0441         $ch = curl_init();
0442         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
0443         curl_setopt($ch, CURLOPT_HEADER, 0);
0444         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
0445         curl_setopt($ch, CURLOPT_URL, $url);
0446         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
0447         $data = curl_exec($ch);
0448         curl_close($ch);        
0449         $results = json_decode($data);
0450         $this->_sendResponse($results, $this->_format);
0451     }
0452 
0453     public function socialtimelineAction()
0454     {
0455         $this->_initResponseHeader();
0456         /** @var Zend_Cache_Backend_Memcached $cache */
0457         $cache = Zend_Registry::get('cache');
0458         $cacheName = __FUNCTION__;
0459         if (false === ($timelines = $cache->load($cacheName))) {
0460 
0461             $model = new Default_Model_Ocs_Mastodon();
0462             $timelines = $model->getTimelines();
0463             if($timelines)
0464             {
0465                 $helpPrintDate = new Default_View_Helper_PrintDateSince();
0466                 foreach ($timelines as &$m) {                                   
0467                     if (array_key_exists('created_at', $m) && $m['created_at'])
0468                     {                                  
0469                         $m['created_at'] = $helpPrintDate->printDateSince(str_replace('T', ' ', substr($m['created_at'], 0, 19)));      
0470                     }                            
0471                 }
0472             }
0473             else
0474             {
0475                 $timelines=array();
0476             }        
0477             $cache->save($timelines, $cacheName, array(), 60 * 60);           
0478         }
0479         $this->_sendResponse($timelines, $this->_format);
0480     }
0481 
0482     public function socialuserstatusesAction()
0483     {
0484         $this->_initResponseHeader();        
0485         $p_username = $this->getParam('username');
0486         $member_data = $this->getUserData($p_username);        
0487         $result=array();
0488         $model = new Default_Model_Ocs_Mastodon();
0489         $user = $model->getUserByUsername($member_data['username']);        
0490         if(sizeof($user)>0)
0491         {
0492             $user=$user[0];
0493         }else{
0494             $user=null;
0495         }
0496         $result['user'] = $user;
0497         $statuses=null;        
0498         if($user && $user['id'])
0499         {
0500             $statuses = $model->getUserStatuses($user['id']);
0501         }
0502         $result['statuses'] = $statuses;
0503         $this->_sendResponse($result, $this->_format);      
0504     }
0505 
0506     public function newsAction()
0507     {
0508         $this->_initResponseHeader();
0509 
0510         /** @var Zend_Cache_Backend_Memcached $cache */
0511         $cache = Zend_Registry::get('cache');
0512         $cacheName = __FUNCTION__;
0513         if (false === ($news = $cache->load($cacheName))) {
0514             $url = 'https://blog.opendesktop.org/?json=1';
0515             $ch = curl_init();
0516             curl_setopt($ch, CURLOPT_AUTOREFERER, true);
0517             curl_setopt($ch, CURLOPT_HEADER, 0);
0518             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
0519             curl_setopt($ch, CURLOPT_URL, $url);
0520             curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
0521             $data = curl_exec($ch);
0522             curl_close($ch);
0523             $news = json_decode($data);
0524             $news->posts = array_slice($news->posts, 0, 3);
0525             $cache->save($news, $cacheName, array(), 60 * 60);
0526         }
0527 
0528         $this->_sendResponse($news, $this->_format);
0529     }
0530 
0531 
0532     public function cattagsAction()
0533     {
0534 
0535         $this->_initResponseHeader();
0536         $catid = $this->getParam('id');
0537         $results = array();
0538         if ($catid) {
0539             $m = new Default_Model_Tags();
0540             $results = $m->getTagsPerCategory($catid);
0541         }
0542         $this->_sendResponse($results, $this->_format);
0543     }
0544 
0545 
0546     public function searchAction()
0547     {  
0548         $this->_initResponseHeader();
0549         $projectSearchText = $this->getParam('p');
0550         if(is_array($projectSearchText)) {
0551             $projectSearchText = array_pop(array_values($projectSearchText));
0552         }
0553         
0554         $store = null;
0555         if($this->hasParam('s')){
0556             $store = $this->getParam('s');
0557         }
0558         if($store=='Opendesktop')
0559         {
0560             $store = null;
0561         }
0562 
0563         // $storemodel = new Default_Model_DbTable_ConfigStore(); 
0564         // $s = $storemodel->fetchDomainObjectsByName($store);
0565         
0566         // $currentStoreConfig = new Default_Model_ConfigStore($s['host']);
0567         // var_dump($currentStoreConfig);        
0568         // die;
0569         
0570         $param = array('q' => $projectSearchText,'store'=>$store,'page' => 1
0571             , 'count' => 10);
0572         $viewHelperImage = new Default_View_Helper_Image();
0573         $modelSearch = new Default_Model_Solr();   
0574         try {
0575             $result = $modelSearch->search($param);
0576             $products = $result['hits'];                      
0577             $ps=array();
0578             foreach ($products as $p) {
0579                 $img = $viewHelperImage->Image($p->image_small, array(
0580                     'width'  => 50,
0581                     'height' => 50
0582                 ));
0583                 $ps[] =array('type'=>'project'                    
0584                     ,'title' =>$p->title
0585                     ,'project_id' =>$p->project_id
0586                     ,'member_id'=>$p->member_id
0587                     ,'username' => $p->username
0588                     ,'laplace_score' =>$p->laplace_score
0589                     ,'score' =>$p->score
0590                     ,'cat_title' =>$p->cat_title
0591                     ,'image_small' =>$img);
0592             }
0593 
0594             $model = new Default_Model_Member();
0595             $results = $model->findActiveMemberByName($projectSearchText);
0596             $helperImage = new Default_View_Helper_Image();
0597             $ps_user=array();
0598             foreach ($results as $value) {
0599                 $avatar = $helperImage->image($value['profile_image_url'],
0600                     array('width' => 100, 'height' => 100, 'crop' => 2));
0601                 
0602                 $ps_user[] =array('type'=>'user'
0603                 ,'username'=>$value['username']
0604                 ,'member_id'=>$value['member_id']
0605                 ,'image_small' =>$avatar
0606                 );
0607             }
0608 
0609             $searchresult=array();
0610             $searchresult[] = array('title' =>'Products','values' =>$ps);
0611             $searchresult[] = array('title' =>'Users','values' =>$ps_user);
0612 
0613             $this->_sendResponse($searchresult, $this->_format);
0614             
0615         } catch (Exception $e) {
0616             $this->_sendResponse(null, $this->_format);
0617         }            
0618     }
0619 
0620 
0621     public function searchpAction()
0622     {  
0623         $this->_initResponseHeader();
0624         $projectSearchText = $this->getParam('p');
0625         $projectSearchCategory = $this->getParam('c');
0626         $store = null;
0627         if($this->hasParam('s')){
0628             $store = $this->getParam('s');
0629         }                
0630         //$filterCat = 'project_category_id:('.$projectSearchCategory.')';
0631         //$param = array('q' => $projectSearchText,'store'=>$store,'page' => 1
0632           //  , 'count' => 10,'fq' => array($filterCat));
0633         $param = array('q' => $projectSearchText,'store'=>$store,'page' => 1, 'count' => 10);
0634         $viewHelperImage = new Default_View_Helper_Image();
0635         $modelSearch = new Default_Model_Solr();   
0636         try {
0637             $result = $modelSearch->search($param);
0638             $products = $result['hits'];                      
0639             $ps=array();
0640             foreach ($products as $p) {
0641                 $img = $viewHelperImage->Image($p->image_small, array(
0642                     'width'  => 50,
0643                     'height' => 50
0644                 ));
0645                 $ps[] =array('type'=>'project'                    
0646                     ,'title' =>$p->title
0647                     ,'project_id' =>$p->project_id
0648                     ,'member_id'=>$p->member_id
0649                     ,'username' => $p->username
0650                     ,'laplace_score' =>$p->laplace_score
0651                     ,'score' =>$p->score
0652                     ,'cat_title' =>$p->cat_title
0653                     ,'image_small' =>$img);
0654             }
0655            
0656             $this->_sendResponse($ps, $this->_format);
0657             
0658         } catch (Exception $e) {
0659             $this->_sendResponse(null, $this->_format);
0660         }            
0661     }
0662 
0663     public function anonymousdlAction()
0664     {
0665         $this->_initResponseHeader();
0666         $identity = Zend_Auth::getInstance()->getStorage()->read();
0667 
0668         $config = Zend_Registry::get('config');
0669         $cookieName = $config->settings->session->auth->anonymous;
0670         $storedInCookie = isset($_COOKIE[$cookieName]) ? $_COOKIE[$cookieName] : NULL;
0671         if ($storedInCookie) {
0672             $model = new Default_Model_DbTable_MemberDownloadHistory();
0673             if ($identity && $identity->member_id) {
0674                 $dlsection = $model->getAnonymousDLSection($storedInCookie, $identity->member_id);
0675             } else {
0676                 $dlsection = $model->getAnonymousDLSection($storedInCookie);
0677             }
0678             $dls = 0;
0679             foreach ($dlsection as $value) {
0680                 $dls = $dls + $value['dls'];
0681             }
0682             //$dls = $model->countDownloadsAnonymous($storedInCookie);
0683             $response = array(
0684                 'status'     => 'ok',
0685                 'section' => $dlsection,
0686                 'dls'    => $dls
0687             );
0688             $this->_sendResponse($response, $this->_format);
0689             return;
0690         }
0691         $response = array(
0692             'status'     => 'ok',
0693             'dls'    => 0
0694         );
0695         $this->_sendResponse($response, $this->_format);
0696     }
0697 
0698     public function fetchrandomsupporterAction()
0699     {
0700         $this->_initResponseHeader();
0701         $section_id = $this->getParam('s');
0702         $info = new Default_Model_Info();
0703         $s = $info->getRandomSupporterForSection($section_id);        
0704         $response = array(
0705             'status'     => 'ok',
0706             'supporter'    => $s
0707         );
0708         $this->_sendResponse($response, $this->_format);
0709     }
0710 
0711 
0712 }