Remove the Avatar upload function from the ucenter

Source: Internet
Author: User
Tags tmp folder

The content of this article is only for technical exchange and learning. The relevant code does not take into account the user authentication and other functions required in the actual application system. Due to the limitations of the ucenter issuance license, do not directly use the code included in this article for actual products or projects.

 

Discuz! Similar to the portrait upload function in uchome, many people want to figure out how it is implemented and even want to transplant it to their own application systems.

 

This function is actually implemented in ucenter (this is discuz! And uchome and other related products are dependent on the public module), through a flash file (camera.swf), with the server's PHP program completed.

 

Ucenter/uchome itself is open-source, but the camera.swf does not provide source code. We can analyze and study the PHP program to find out the details of this function operation, and finally strip it out to run independently of the ucenter. This article is based on ucenter1.5.0/uchome2.0.

 

First, let's take a look at how this function works in ucenter/uchome.

 

■ Basic steps

 

0. The browser accesses a web page in uchome, which contains camera.swf. To ensure that camera.swf works properly, a locale. xml file is required in the same path.

 

The HTML code for loading camera.swf can be generated by the following program:

 

Home/uc_client/client. php: uc_avatar ()

 

The generated content is roughly as follows:

 

<Object classid = "CLSID: d27cdb6e-ae6d-11cf-96b8-444553540000" codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width = "447" Height = "477" id = "mycamera" align = "Middle">
<Param name = "allowScriptAccess" value = "always"/>
<Param name = "scale" value = "exactfit"/>
<Param name = "wmode" value = "Transparent"/>
<Param name = "quality" value = "high"/>
<Param name = "bgcolor" value = "# ffffff"/>
<Param name = "movie" value = "http: // localhost/center/images/camera.swf? Inajax = 1 & appid = 1 & input = 6b49vkhr % signature % 2 bxvfjpivy % signature % 2 bposttsk % 2be3u & agent = signature & ucapi = http % 3A % 2f % 2 flocalhost % 2 fcenter & avatartype = Virtual "/>
<Param name = "menu" value = "false"/>
<Embed src = "http: // localhost/center/images/camera.swf? Inajax = 1 & appid = 1 & input = 6b49vkhr % signature % 2 bxvfjpivy % signature % 2 bposttsk % 2be3u & agent = signature & ucapi = http % 3A % 2f % 2 flocalhost % 2 fcenter & avatartype = Virtual"
Quality = "high"
Bgcolor = "# ffffff"
Width = "447"
Height = "477"
Name = "mycamera"
Align = "Middle"
AllowScriptAccess = "always"
Allowfullscreen = "false"
Scale = "exactfit"
Wmode = "Transparent"
Type = "application/X-Shockwave-flash"
Pluginspage = "http://www.macromedia.com/go/getflashplayer"/>
</Object>

 

The parameter breakdown is as follows:

 

Inajax 1
Appid 1
Input 6b49vkhr % 2fc4vpmimcat07kr2eqk8jjy % 2f6vtvdonod47du7jk % 2 bxvfjpivy % bytes % 2 bposttsk % 2be3u
Agent a523e70c80e13e4eaee37c7f4bde4f2c
Ucapi http: // localhost/center
Avatartype virtual

 

The key parameter is ucapi, And the URL of post in the next two steps is <ucapi>/index. php

 

"/Index. PHP "seems to have hardcode in camera.swf, but we can see in the final program code that if the ucapi we specify explicitly specifies a PHP file, we can bypass the index. PHP file name. In other words,If the server is not using PHP, but another programming language (such as Java/. net), it can also be implemented.

 

1. After you select an image watermark in camera.swf, camera.swf will immediately upload the selected image to the server.

 

$ _ Server

 

Request_uri/center/index. php? M = user & inajax = 1 & A = uploadavatar & appid = 1 & input = %%%%%%& agent = a523e70c80e13e4eaee37c7f4bde4f2c & avatartype = Virtual
Method post

 

$ _ Get

 

M User
Inajax 1
A uploadavatar
Appid 1
Input ca68o1l41a1hkbmhmjvghu0ofjxyxwadnut9hnfpnyz5agizcsvgecpp38w + Signature
Agent a523e70c80e13e4eaee37c7f4bde4f2c
Avatartype virtual

 

$ _ Post

 

Filename something.jpg
Upload submit Query

 

$ _ Files

 

Image File Content

 

The handler is:

 

Center/control/user. php: onuploadavatar ()

 

The content returned by the program is the temporary location for saving the file, for example:

 

Http: // localhost/center/data/tmp/upload5.jpg

 

Camera.swf reads the image from the URL above and displays it for operation. The following return values can also be used as the error code:

 

-1: invalid identity!
-2: Invalid photograph!
-3: No photograph be upload!
-4: can not write to the data/tmp folder!
-5: server can not upload!

 

2. After the user confirms the scheme in camera.swf, camera.swf uploads three image data to the server as big, medium, and small profile pictures.

 

$ _ Server

 

Request_uri/center/index. php? M = user & inajax = 1 & A = rectavatar & appid = 1 & input = %%%%%%& agent = a523e70c80e13e4eaee37c7f4bde4f2c & avatartype = Virtual & randomnumber = 5478
Method post

 

$ _ Get

 

M User
Inajax 1
A rectavatar
Appid 1
Input ca68o1l41a1hkbmhmjvghu0ofjxyxwadnut9hnfpnyz5agizcsvgecpp38w + Signature
Agent a523e70c80e13e4eaee37c7f4bde4f2c
Avatartype virtual
Randomnumber 5478

 

$ _ Post

 

Avatar1... <big>
Avatar3... <middle>
Avatar2... <small>

 

The handler is:

 

Center/control/user. php: onrectavatar ()

 

The program returns the following content:

 

Successful: <? XML version = "1.0"?> <Root> <face success = "1"/> </root>

Failed: <root> <message type = "error" value = "-1"/> </root>

 

3. After the upload is complete, if camera.swf can find the JavaScript function named updateavatar (), it will be called.

■ Parameter Analysis

 

In the HTML generated in step 1 above, the URL pointing to camera.swf carries some parameters. The most important of these parameters is ucapi, which determines where the HTTP request in step 2 and Step 2 is sent.

 

Other parameters are required by the ucenter application system. Camera.swf is designed for ucenter, so it particularly supports these parameter values, that is, what values are contained in the URL that accesses camera.swf, when you access ucapi in the future, it will be passed back as is.

 

Camera.swf does not provide source code and cannot be modified, but it does not prevent us from using it. For example, you can use the input parameter to encode the custom parameters (such as user ID) required by your application.

■ Strip implementation

 

After figuring out the details and parameter meanings of function operations, it is easy to implement this function again.

 

The so-called re-stripping implementation is to re-compile the server-side processing program on the premise of directly borrowing the two files camera.swf/locale. XML to implement the Avatar upload function and run independently from the ucenter.

 

Theoretically, any programming language on the server can be used. I will only provide PHP code here.

 

Contains three program files:

 

Avatar. php -- implements all the functions mentioned in the above "basic steps"
Camera.swf -- extracted from ucenter
Locale. xml -- extracted from ucenter

 

Deploy these three files on the web server and then access http: // localhost/avatar. php. You can also specify a UID as a "User ID" on the URL, such as http: // localhost/avatar. php? Uid = 5

 

Program: http://maquan.download.csdn.net/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.