source: trunk/common/utils.php @ 1778

Revision 1778, 6.3 KB checked in by admin, 22 months ago (diff)

Merge omcollabu_ui back into trunk

Line 
1<?php
2
3    /*
4     * Moved from explore/includes/utils.php
5     */
6
7    function GetLoggedInStateFromWikiCookies(){
8            # we can only check cookies (there's no DB with omExplore), so
9            # we'll check cookie from the wiki against omExplore cookie;
10            # we have to have usernames matching
11        if( isset( $_COOKIE[ OMWIKI_DATABASE_NAME . '_' . OMWIKI_DB_TABLE_PREFIX . 'UserName' ] )
12               && isset( $_COOKIE[ 'omCollabExplore' ] )
13                 && $_COOKIE[ OMWIKI_DATABASE_NAME . '_' . OMWIKI_DB_TABLE_PREFIX . 'UserName' ] == $_COOKIE[ 'omCollabExplore' ] ){
14                    return true;
15            }
16    }
17
18    function GetAvatar( $iUserId, $sSize = 's' ){
19            $sWikiUploadDir  = OMCOLLAB_DOCROOT . OMWIKI_SCRIPT_PATH . '/images';
20            $aMatches        = glob("$sWikiUploadDir/avatars/" . OMWIKI_DATABASE_NAME . '_' . "{$iUserId}_$sSize.*");
21            if (count($aMatches)<1) $aMatches = glob("$sWikiUploadDir/avatars/default_$sSize.*");
22            if (count($aMatches)) {
23                    $sAvatarSrc = str_ireplace( OMCOLLAB_DOCROOT, '', $aMatches[0] );
24            }
25            return $sAvatarSrc;
26    }
27
28    /*
29     * Moved from home/includes/utils.php
30     */
31   
32    /**
33     * Generate Server Doman like http://waterloo.openmethodology.org
34     * TODO: it can be replaced by a setting options
35     *
36     * Xiping Wang 2009-04-29
37     *
38     * @return unknown_type
39     */
40    if(!function_exists('gethostname')){
41      function getHostName(){
42   
43        $server_name = (!empty($_SERVER['HTTP_HOST'])) ? strtolower($_SERVER['HTTP_HOST']) : ((!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME'));
44        $server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT');
45        $secure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 1 : 0;
46
47        $url = (($secure) ? 'https://' : 'http://') . $server_name;
48
49        if ($server_port && (($secure && $server_port <> 443) || (!$secure && $server_port <> 80)))
50        {
51            // HTTP HOST can carry a port number...
52            if (strpos($server_name, ':') === false)
53            {
54                $url .= ':' . $server_port;
55            }
56        }
57        return $url;
58      }
59    }
60
61    /**
62     * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
63     * array containing the HTTP server response header fields and content.
64     */
65    function getWebPage( $url ){
66
67        //Xiping Wang
68        //fix 10, descrease the timeout value(CURLOPT_CONNECTTIMEOUT|CURLOPT_TIMEOUT) from 120 to 60
69        //set up options for the http call
70        $options = array( CURLOPT_RETURNTRANSFER => true,     // return web page
71            CURLOPT_HEADER         => false,    // don't return headers
72            CURLOPT_FOLLOWLOCATION => true,     // follow redirects
73            CURLOPT_ENCODING       => "",       // handle all encodings
74            CURLOPT_USERAGENT      => "spider", // who am i
75            CURLOPT_AUTOREFERER    => true,     // set referer on redirect
76            CURLOPT_CONNECTTIMEOUT => 60,      // timeout on connect
77            CURLOPT_TIMEOUT        => 60,      // timeout on response
78            CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
79        );
80
81        //initiate and send post
82        $ch      = curl_init( $url );
83        curl_setopt_array( $ch, $options );
84        $content = curl_exec( $ch );
85        $err     = curl_errno( $ch );
86        $errmsg  = curl_error( $ch );
87        $header  = curl_getinfo( $ch );
88        curl_close( $ch );
89
90        //prepare content
91        $header['errno']   = $err;
92        $header['errmsg']  = $errmsg;
93
94        if(trim($content)!=='')
95            $header['content'] = $content;
96        else
97            $header['content'] = '<api></api>';
98        return $header;
99    }
100
101    function splitHjmsChars($xstr, $xlenint, $xlaststr) {
102        $texttoshow = chunk_split($xstr,$xlenint,"\r\n");
103        $texttoshow  = preg_split('/\r\n/',$texttoshow);
104        $texttoshow = trim($texttoshow[0].$xlaststr);
105
106        //confirm there is not open tag "<", if so , we should end it with ">"
107        if(strripos($texttoshow,"<")> strripos($texttoshow,">"))
108            $texttoshow .= "\"\">";
109
110       //confirm there is not open tag "<", if so , we should end it with ">"
111        if(strripos($texttoshow,"<")> strripos($texttoshow,">"))
112            $texttoshow .= "\"\">";
113
114        if (strlen($xstr)>$xlenint){
115            $texttoshow .= "...";
116        }   
117       
118        return $texttoshow;
119    }
120   
121    /**
122     *
123     * @param $url the wiki text from other wiki site
124     * @return html segment
125     */
126    function loadWikiMainPage($title,$length=200){
127        //$url is from
128
129            $url =  getHostName().OMWIKI_SCRIPT_PATH."/api.php?action=parse&text={{:$title}}";
130        $url .= '&format=xml';
131        $wgResponse = getWebPage($url);
132
133        //initialize data
134        $doc = new DOMDocument();
135        $doc->preserveWhiteSpace = FALSE;
136
137        $content = '';
138
139        try {
140            //load api content
141            $doc->loadXML(trim($wgResponse['content']));
142            //please check
143            $content = $doc->getElementsByTagName("text")->item(0)->nodeValue;
144            //generate result
145           
146            //remove HTML comment
147            $content = preg_replace('/<!--\s*NewPP limit report[^>]*-->\s*$/siu', '', $content);
148        } catch (Exception $e) {
149            error_log( "Exception caught");
150            error_log($e->getMessage());
151        }
152
153        //result was cut into the assigned length
154        return splitHjmsChars($content,$length,' ');
155    }
156   
157    /**
158         * Replace message parameter keys on the given formatted output.
159         *
160         * @param string $message
161         * @param array $args
162         * @return string
163         * @private
164         */
165        function msgReplaceArgs( $message, $args ) {
166                # Fix windows line-endings
167                # Some messages are split with explode("\n", $msg)
168                $message = str_replace( "\r", '', $message );
169       
170                // Replace arguments
171                if ( count( $args ) ) {
172                        if ( is_array( $args[0] ) ) {
173                                $args = array_values( $args[0] );
174                        }
175                        $replacementKeys = array();
176                        foreach( $args as $n => $param ) {
177                                $replacementKeys['$' . ($n + 1)] = $param;
178                        }
179                        $message = strtr( $message, $replacementKeys );
180                }
181       
182                return $message;
183        }
184
185
186?>
Note: See TracBrowser for help on using the repository browser.